Getpwnam no such file or directory

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    File
Click For Summary
SUMMARY

The forum discussion addresses an issue with the C function getpwnam() returning 'no such file or directory' when attempting to authenticate a user against /etc/master.passwd. The problem arises from the inclusion of a trailing newline character in the username buffer, which can be resolved by removing it. Additionally, the discussion highlights the complexities of replicating FreeBSD PAM modules due to portability issues across different systems. It emphasizes the importance of reviewing FreeBSD source code for better understanding and implementation.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with Unix/Linux system calls, specifically getpwnam()
  • Knowledge of password hashing techniques using crypt()
  • Basic understanding of FreeBSD PAM modules
NEXT STEPS
  • Review FreeBSD source code for PAM module implementation
  • Learn about handling strings in C to avoid common pitfalls like trailing newlines
  • Explore the differences between various password hashing algorithms
  • Investigate best practices for user authentication in Unix/Linux environments
USEFUL FOR

Developers working on authentication systems, system administrators managing Unix/Linux environments, and anyone interested in understanding PAM module complexities and password handling in C programming.

James889
Messages
190
Reaction score
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() which fails with 'no such file or directory'

Relevant code below
Code:
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?
 
Technology news on Phys.org
Your username string has a trailing '\n' remove it and your call will work.
 
Duh, silly newbie mistake :)
 
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.
 
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.
 

Similar threads

Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 11 ·
Replies
11
Views
35K
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 19 ·
Replies
19
Views
4K