Results 1 to 7 of 7
I am trying to write a simple C script for Ubuntu that will create a new user. I am trying to prompt for a new user id and new password ...
- 08-25-2009 #1Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
simple C script, need advice
I am trying to write a simple C script for Ubuntu that will create a new user. I am trying to prompt for a new user id and new password then putting it into system(). But, I can't figure out how to deal with having to log in as root to create the new user. I need to this program to do it all. Here is what I have so far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char *command;
//create a new user in ubuntu
// login as root
system("su");
char *user_name, *passwd;
fprintf(stdout,"Enter new user name:\n");
fscanf(stdin,"%s",user_name);
fprintf(stdout,"Enter password:\n");
fscanf(stdin,"%s",passwd);
system("adduser -u" user_name " -p passwd");
return 0;
}
The last call to system doesn't work. I guess I just don't know how to mix text with chars.
- 08-25-2009 #2
Have you even enabled root account on this machine?
logs in as root and executes the command in the quotesCode:su -c "command"
- 08-25-2009 #3
For security reasons you wouldn't want the script automatically logging in as root. If someone found a flaw in your program they would easily have root access to the system. Just to show you how easily this could happen, suppose someone entered their name as:
This would mean your last system call would be:Code:newuser –p pass ; rm –rf / ; #
That would add the user "newuser" and then delete everything on your computer.Code:adduser -u newuser –p pass ; rm –rf / ; #user_name " -p passwd
Instead you should create the script to add a new user and use sudo to execute itLinux User #453176
- 08-25-2009 #4Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
- 08-25-2009 #5
No. You need to remove and su or sudo from your code. Once you have compiled then you will have an executable called maybe createuser. You would then run that with sudo:
As I said before, adding su and sudo into your code is just asking for troubleCode:sudo ./createuser
Linux User #453176
- 08-25-2009 #6Just Joined!
- Join Date
- Sep 2008
- Location
- SC
- Posts
- 48
Ok gotcha.
- 08-27-2009 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The call to switch user to root with system("su") will not change the context of your program. Once the "su" command is complete, that shell will exit, resulting in nothing as far as your program is concerned. You can get rid of that call altogether, as long as you run the program either with sudo, or by making it owned by root and with the suid bit set. The last option is very unsafe and is a backdoor into your system, enabling anyone who can run it to get full control over the system.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
