9

I would like to build a program which takes a username as parameter and creates the user and its home folder (with some hard-coded specifications like folder, and security checks like username cannot be root or an existing user).

My application needs to create users in order to give SSH access.

The program will be executed using sudo. I've read it should be written in C or C++ instead of scripts because scripts could easily be exploited.

  • Can you give me some advices or good practices about how to achieve this?
  • Should I use some Pam library? Is there any examples?
  • What are the possible security flaws?

I know C/C++, and running Ubuntu Lucid.

Edit:

The user will only have sudo access to run that specific command, I do not want it to be able to run a shell or bypass some programs (by changing PATH environment, for example).

As a result, for example, I will need to override the PATH, what else should I worry about?

3
  • 2
    You mean like adduser or useradd? Try looking at their source code. Commented Aug 10, 2010 at 22:50
  • 6
    Why is a script easier to exploit than an executable? If you have root access you have root access and their is nothing you can't do. Do it the simple way and use a script that uses the normal linux commands (and thus have been completely tested). Commented Aug 11, 2010 at 0:15
  • The user will only have sudo access to run that specific command, I do not want it to be able to run a shell or bypass some programs (by changing PATH environment, for example). Commented Aug 11, 2010 at 10:34

3 Answers 3

10

Probably your best bet is to invoke useradd; it will do the right things (given appropriate parameters).

Trying to create one manually by calling the appropriate APIs is possible but not desirable.

Sign up to request clarification or add additional context in comments.

2 Comments

This sounds much more desirable to me. Putting together a Perl script and then calling useradd is how I would think to solve this problem.
There are no "appropriate APIs" in the Linux C library.
9

Actually, there is a C API method to create a Linux user. It is in the pwd.h include file.

Here you have a sample test:

#include <pwd.h>
#include <stdio.h>
#include <string.h>

static void createUser(char *userName, char *homeDir, int uid) {
    struct passwd * pwd = getpwent ();
    struct passwd pwd2;

    pwd =  getpwnam(userName);
    if (pwd != NULL) {
        return;
    }
    pwd2.pw_dir = homeDir;
    pwd2.pw_gecos=userName;
    pwd2.pw_name=userName;
    pwd2.pw_gid=uid;
    pwd2.pw_uid=uid;
    pwd2.pw_shell=strdup("/bin/bash");
    FILE *f = fopen("/etc/passwd", "a");
    if (f != NULL) {
        putpwent(&pwd2, f);
        fclose(f);
    }
    free (pwd2.pw_shell);
}

int main (int argc, char **argv) {
   createUser("test", "/home/test", 12345);
   return 0;
}

2 Comments

Best answer so far. I could not accept that people were saying, you have to manually write to a file, which will break anytime you make a semantic error. Great job & thanks.
putpwent is not POSIX standard, but it is in glibc (not sure of versions) and musl libc (since 1.0.0 at least). So it's pretty broadly available.
3

There is no API for this. You just write into /etc/passwd and /etc/group (and possibly the shadow versions as well) using normal file access system calls.

2 Comments

Don't forget that before modifying these values you should always acquire an exclusive lock on the files (all system commands that modify these files do that). Otherwise there is the possibility of file corruption and then you are in really bad shape.
I think this is the "only way" if your /etc/passwd is blank and so you cannot add users to groups using adduser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.