Getpwnam no such file or directory

  • Thread starter James889
  • Start date
  • Tags
    File
In summary, the conversation discusses a problem with an authentication plugin that calls the getpwnam() function and fails due to a "no such file or directory" error. The code for reading the username and password is shown, along with the use of the crypt() function. The issue is resolved by removing a trailing newline in the username string. There is also a mention of the difficulty of duplicating what the getlogin modules already do, and the recommendation to look at the FreeBSD source for guidance. Finally, there is a discussion about the differences between the hashed password in master.password and the hash generated by crypt().
  • #1
James889
192
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
  • #2
Your username string has a trailing '\n' remove it and your call will work.
 
  • #3
Duh, silly newbie mistake :)
 
  • #4
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.
 
  • #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.
 

1. What does "Getpwnam no such file or directory" mean?

"Getpwnam no such file or directory" is an error message that typically occurs in a Unix or Linux operating system. It means that the system was unable to find the user account information for a given username.

2. Why does "Getpwnam no such file or directory" occur?

This error can occur for several reasons, such as a misspelled username, a deleted user account, or a corrupt system file. It can also happen if the user account does not exist in the system's password database, or if the user does not have proper permissions to access the file or directory.

3. How can I fix "Getpwnam no such file or directory"?

The solution to this error depends on the cause. If the username is misspelled, simply correcting it should resolve the issue. If the user account was deleted, it will need to be recreated. If the system file is corrupt, it may need to be repaired or replaced. In some cases, changing the permissions of the file or directory may also fix the error.

4. Can "Getpwnam no such file or directory" be prevented?

While this error can be frustrating, it is often unavoidable. However, ensuring that usernames are spelled correctly and that user accounts are not deleted without proper backup can help prevent this error from occurring.

5. Is "Getpwnam no such file or directory" a serious issue?

Generally, this error is not considered a serious issue. It is a common error and can usually be resolved without causing any major problems. However, if the error is persistent and cannot be fixed, it may indicate a larger issue with the system's configuration or files.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
10
Views
5K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
11
Views
34K
  • Programming and Computer Science
Replies
16
Views
4K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
19
Views
3K
Back
Top