LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2017, 02:32 PM   #16
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143

Quote:
Originally Posted by scasey View Post
Yes, that's true. Point is that the first thing from the script needs to be a "Content-Type:..." header
Yep! But you made it sound like you HAD to send text/html. Just pointing that out... no prob.
 
1 members found this post helpful.
Old 07-07-2017, 02:48 PM   #17
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
> I tried to assemble it and it wouldn't run.
How did you try to assemble it? What command did you use?
 
1 members found this post helpful.
Old 07-07-2017, 04:41 PM   #18
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by NevemTeve View Post
> I tried to assemble it and it wouldn't run.
How did you try to assemble it? What command did you use?
Among others (including yasm)... this seemed most promising but it still didn't run:

Code:
nasm -f macho64 -o test.cgi cgi.asm
results:

Code:
| => nasm -f macho64 -o test.cgi cgi.asm
/* no output */
| => ./test.cgi
-bash: ./test.cgi: cannot execute binary file

| => file test.cgi
test.cgi: Mach-O 64-bit object x86_64
This is for Mac OS X:

Code:
| => uname -a
Darwin SpocksBrain 17.0.0 Darwin Kernel Version 17.0.0: Tue Jun 13 21:06:15 PDT 2017; root:xnu-4481.0.0.1.1~1/RELEASE_X86_64 x86_64

LOL I can almost run it this way, but it's in reverse order:

Code:
| => strings test.cgi
Hello world!
Content-type: text/html
The source code explicitly says "ELF entry point" but this is Mach-O. If I tried it on Solaris that uses ELF format binaries, it'd probably work fine.

Last edited by Laserbeak; 07-07-2017 at 04:54 PM.
 
1 members found this post helpful.
Old 07-07-2017, 05:19 PM   #19
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
I compiled it in Solaris:

Code:
root@sunshine:~# uname -a
SunOS sunshine 5.11 11.3 i86pc i386 i86pc
Code:
root@sunshine:~# nasm -f elf64 -o test.cgi test.asm
Code:
root@sunshine:~# ./test.cgi
bash: ./test.cgi: Invalid argument
???
 
1 members found this post helpful.
Old 07-07-2017, 10:49 PM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I would try link it:
Code:
nasm -f elf64 -o test.o test.asm
ld -o test.cgi test.o
 
1 members found this post helpful.
Old 07-07-2017, 11:34 PM   #21
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by NevemTeve View Post
I would try link it:
Code:
nasm -f elf64 -o test.o test.asm
ld -o test.cgi test.o
This did work as far as I came up with an executable but it ran and didn't print anything out at all and I just returned to the prompt. Perhaps it went to some log somewhere?

It did not work for Mac OS X replacing -f elf64 with -f macho64 though
 
1 members found this post helpful.
Old 07-08-2017, 03:14 AM   #22
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Works for me:
Code:
uname -mr
3.16.0-4-amd64 x86_64
nasm -f elf64 -o rb_cgi.o rb_cgi.asm
ld -o rb_cgi.cgi rb_cgi.o
./rb_cgi.cgi
Content-type: text/html

Hello world!
file rb_cgi.*
rb_cgi.asm: ASCII text
rb_cgi.cgi: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
rb_cgi.o:   ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Last edited by NevemTeve; 07-08-2017 at 03:15 AM.
 
1 members found this post helpful.
Old 07-13-2017, 12:17 AM   #23
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
Quote:
How did you try to assemble it? What command did you use?
Code:
nasm -f elf64 test.asm  -o test.o
ld  test.o -o test
<form... action="cgi-bin/test">
<input type="submit" ..>
I did not try to add any extension in the linker command but in theory, the server does not need one. "document root" and "cgi-bin" directories have been left as per original default Debian 7 (no need for added "exec .extension" or "alias .." since whatever is in that directory is considered by Apache2 to be cgi executable)

This code does not call the server and also work in my case
Quote:
uname -mr
3.16.0-4-amd64 x86_64
nasm -f elf64 -o rb_cgi.o rb_cgi.asm
ld -o rb_cgi.cgi rb_cgi.o
./rb_cgi.cgi
Content-type: text/html
but the funny thing is that, calling the server through html submit, itdoes not work in C either even though gcc has no complaint about the source code, doing the same thing in "YABASIC" with two "print" statements in the code (one to screen and the other to file) does not print correctly to the screen (possibly not at all, I cannot remember) altough printing to file shows the correct output (6 'variable name=value' blocks), not code. So it apears that the garbage printed is not code but the correct output (always proportional to the string used) that for some reason Apache2 cannot render correctly. Since the browser is UTF-8 by default which YASM does not understand (although the data is temporarily ASCII only), perhaps, not printing to screen in YABASIC had to be expected so I did not worry too much about that.
I have used verbatim a more involved example from the NCSA (32bit) and it does not work either - same result.

Last edited by rblampain; 07-13-2017 at 01:04 AM.
 
Old 07-13-2017, 02:37 AM   #24
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Works for me:
Code:
$ ls -l /usr/local/www/cgi-bin/rb_cgi
-rwxr-xr-x 1 projects devel 1096 Jul  8 10:11 /usr/local/www/cgi-bin/rb_cgi

$ file /usr/local/www/cgi-bin/rb_cgi
/usr/local/www/cgi-bin/rb_cgi: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

$ wget -S -O cgiout http://127.0.0.1:/cgi-bin/rb_cgi
--2017-07-13 10:46:16--  http://127.0.0.1/cgi-bin/rb_cgi
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Date: Thu, 13 Jul 2017 08:46:16 GMT
  Server: Apache/2.4.18 (Unix) PHP/7.1.7 OpenSSL/1.0.2l
  Content-Length: 13
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Content-Type: text/html
Length: 13 [text/html]
Saving to: 'cgiout'

$ cat cgiout
Hello world!

Last edited by NevemTeve; 07-13-2017 at 03:47 AM.
 
2 members found this post helpful.
Old 07-13-2017, 01:52 PM   #25
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by rblampain View Post
I did not try to add any extension in the linker command but in theory, the server does not need one. "document root" and "cgi-bin" directories have been left as per original default Debian 7 (no need for added "exec .extension" or "alias .." since whatever is in that directory is considered by Apache2 to be cgi executable)
I'm sorry, but that is not correct. The server does need an extension which matches one of the values in AddHandler in the httpd.conf file.
Without a matching extension, apache will not execute the script/program, but will just send the contents of the file, which in your case, is a binary file.

Please add a .cgi extension to your compiled file and try again. I don't believe it needs to be re-compiled with the extension, the file just has to have that name for apache to execute it instead of delivering it.

Last edited by scasey; 07-13-2017 at 02:03 PM.
 
1 members found this post helpful.
Old 07-13-2017, 01:58 PM   #26
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
I believe if it is in a designated cgi-bin directory the web server should try to execute the file and if that fails for some reason (anything from not being able to execute it at all to returning no header or a bad header) it should just give a server error. It shouldn't send the contents of the file, which means wherever the file was, it wasn't in a properly declared cgi-bin directory.
 
2 members found this post helpful.
Old 07-13-2017, 02:08 PM   #27
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
As a last straw, one could check log-files access_log and error_log in directory /var/log/apache2
 
2 members found this post helpful.
Old 07-13-2017, 05:39 PM   #28
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by Laserbeak View Post
I believe if it is in a designated cgi-bin directory the web server should try to execute the file and if that fails for some reason (anything from not being able to execute it at all to returning no header or a bad header) it should just give a server error. It shouldn't send the contents of the file, which means wherever the file was, it wasn't in a properly declared cgi-bin directory.
I stand corrected. It appears I've been very lazy and have just been applying the
Code:
Option ExecCGI
directive to directories containing executable scripts rather than creating ScriptAlias for them. When doing that, the
Code:
AddHandler cgi-script .cgi .pl
directive is also required.

When a ScriptAlias directive is defined for a folder, then neither of the other directives are required.

I believe I'll do some tweaking of the config file over the next couple of days. Thank you, Laserbeak and NevemTeve

rblampain, what I've been saying will work, it's just not the only way, and probably not the best way. Please confirm that your cgi-bin does have a ScriptAlias defined for it. Something like:
Code:
ScriptAlias /cgi-bin/ /path/to/the/cgi-bin/
...and I would not expect that to be provided by default,because the realname directory needs to be specified. There may be a default example commented out:
Code:
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin"
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
(which I copied from my current httpd.conf -- which is why I need to use the other two directives
 
1 members found this post helpful.
Old 07-21-2017, 12:42 AM   #29
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
I cannot understand what the problem was. Initially having "charset=urf-8" (maybe not the exact spelling) in the header seemed to make it worse, so I took it out on the assumption that I only had ascci in the testing files. In my last test, I put that back and it works and I have no idea what the difference is. There was no need to alter any configuration, as explained in my previous post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Laserjet 6P printing pages of garbage shane601 Linux - Hardware 4 11-03-2009 03:58 PM
Printing garbage, not sure what to do John Teisberg Linux - Newbie 1 03-29-2009 08:11 AM
How to stop printer from printing garbage greengrocer Linux - Newbie 5 10-22-2005 11:25 PM
Printers printing garbage? inkysplat Linux - Hardware 1 11-03-2003 02:27 PM
Printing garbage sakkie Slackware 1 09-19-2003 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration