Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Getpwnam no such file or directory

  1. Nov 8, 2015 #1
    Hi,

    I'm trying to write a simple authentication plugin which authenticates a user against /etc/master.passwd.
    The problem is the call to getpwnam() wich fails with 'no such file or directory'

    Relevant code below
    Code (C):

    char username[10];
    memset(username,0,sizeof(username));

    /* Read the username */
            printf("Username:");
            fgets(username,sizeof(username),stdin);


            /* Read the password */
            crypt_set_format("sha512");
            char *pass;
            pass = getpass("password:");
            char *p = (char *)calloc(strlen(pass),sizeof(char));


             strlcpy(p,crypt(pass,salt),sizeof(p));
             struct passwd *local_pass = getpwnam(username);
                    if( local_pass == NULL){
                    printf("%s",strerror(errno));
                    exit(1);
            }
     
    Replacing the reference to the username buffer with "<actual username>" works.
    Any ideas?
     
  2. jcsd
  3. Nov 8, 2015 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    Your username string has a trailing '\n' remove it and your call will work.
     
  4. Nov 8, 2015 #3
    Duh, silly newbie mistake :)
     
  5. Nov 8, 2015 #4

    jim mcnamara

    User Avatar

    Staff: Mentor

    Well, it looks like you are trying to duplicate what the getlogin modules already do. That is not an easy to duplicate: FreeBSD pam modules are going to be different from box to box, so this will have zero portability. Any-hoo, get and read the FreeBSD source before attempting something like this, just so you no longer think this is a slam dunk if nothing else. It is okay to pilfer and use what hundreds of programmers have already worked on for years, and tried to perfect. That is the whole idea of opensource.
     
  6. Nov 22, 2015 #5
    There seems to be a similar situation with the hashed password found in master.password and the hash generated by crypt(),
    they are different, despite hashing being done on the same clear text password. In this case there are no trailing newlines.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook