Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2008, 03:19 AM   PM User | #1
Dustfinger
New Coder

 
Join Date: Jun 2007
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Dustfinger is an unknown quantity at this point
Bad file descriptor on system() call

I'm running a fairly simple program that lets users add an account onto my server so they can connect through ftp. The problem comes in in the following line (I left in the debug lines as well):

Code:
system("sudo /usr/sbin/useradd $username");
print "exit status: $?<br>\n";
print "errors: $!<br>\n";
The exit status is always 256, errors is always a "Bad file descriptor". In some fairly last ditch efforts to get this thing working, I've put chmod 777 on useradd and put the following line in my sudoers:

Code:
ALL     ALL=(ALL)       NOPASSWD: /usr/sbin/useradd
I'll fix these to be more secure after this thing gets sorted out. I've looked on google for a solution to this, but most people are having this problem with windows, and it's because they're actually opening files. I'm not opening any files, and running 'sudo /usr/sbin/useradd username' in a terminal works fine. Does anyone have any idea of what's wrong, or do I need to give more details?
Dustfinger is offline   Reply With Quote
Old 11-04-2008, 03:33 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Code:
system("sudo /usr/sbin/useradd $username") == 0 or die "code $?";
see perlfunc man pages for system, there is some code there to test the value of $?.

best regards
oesxyl is offline   Reply With Quote
Old 11-04-2008, 02:47 PM   PM User | #3
Dustfinger
New Coder

 
Join Date: Jun 2007
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Dustfinger is an unknown quantity at this point
I placed the following after the system call, hopefully it's what you were talking about:
Code:
    if ($? == -1) {
	print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
	printf "child died with signal %d, %s coredump\n",
	    ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
	printf "child exited with value %d\n", $? >> 8;
    }
When I run it it outputs the following:
child exited with value 1
Dustfinger is offline   Reply With Quote
Old 11-04-2008, 09:26 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by Dustfinger View Post
When I run it it outputs the following:
child exited with value 1
from sudo man page:
Quote:
RETURN VALUES
Upon successful execution of a program, the return value from sudo will simply be the return value of the program that was executed.

Otherwise, sudo quits with an exit value of 1 if there is a configuration/permission problem or if sudo cannot execute the given command. In the
latter case the error string is printed to stderr.
If sudo cannot stat(2) one or more entries in the user's PATH an error is printed on stderr.
(If the directory does not exist or if it is not really a directory, the entry is ignored and no error is printed.) This should not happen under
normal circumstances. The most common reason for stat(2) to return "permission denied" is if you are running an automounter and one of the directo-
ries in your PATH is on a machine that is currently unreachable.
from useradd man page:
Quote:
EXIT VALUES
The useradd command exits with the following values:

0
success

1
can't update password file
I don't have enought experience with runing sudo and useradd from perl, I guess you must check both cases.
If this don't fix the problem another thing with runing from perl is the difference of enviroment, commands work in a terminal shell but not in perl script. This can be checked runing env from both and see the difference.

best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
Dustfinger (11-06-2008)
Old 11-06-2008, 02:04 AM   PM User | #5
Dustfinger
New Coder

 
Join Date: Jun 2007
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Dustfinger is an unknown quantity at this point
Alright, I think I'm on the right track. I'm gonna take this problem to a different forum, since I don't think it's a problem with my perl script. Here's the new thread, if anyone wants to follow this there:

http://www.linuxforums.org/forum/ser...tml#post640506

Thanks oesxyl for your help, I really appreciate it.
Dustfinger is offline   Reply With Quote
Old 11-06-2008, 02:17 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by Dustfinger View Post
Alright, I think I'm on the right track. I'm gonna take this problem to a different forum, since I don't think it's a problem with my perl script. Here's the new thread, if anyone wants to follow this there:

http://www.linuxforums.org/forum/ser...tml#post640506

Thanks oesxyl for your help, I really appreciate it.
you are welcome. When you fix the problem please post a link or a comment here to help other who have same problem.

good luck and best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:15 PM.


Advertisement
Log in to turn off these ads.