c - Checking username: getpwnam / getpwnam_r: No such file or directory -


i'm trying make web logging , use getpwnam() function check username existing. valid username getpwnam returns error: no such file or directory. tried getpwnam_r(), failed same error. i'm running on embedded arm linux , use /etc/passwd password storing (i don't have /etc/shadow). test program is:

#include <pwd.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h>  int main(int argc, char *argv[]) {     struct passwd pwd;     struct passwd *result;     char *buf;     size_t bufsize;     int s;     if (argc != 2) {         fprintf(stderr, "usage: %s username\n", argv[0]);         exit(exit_failure);     }     bufsize = sysconf(_sc_getpw_r_size_max);     if (bufsize == -1)          /* value indeterminate */         bufsize = 16384;        /* should more enough */     buf = malloc(bufsize);     if (buf == null) {         perror("malloc");         exit(exit_failure);     }     s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);     if (result == null) {         if (s == 0)             printf("not found\n");         else {             errno = s;             perror("getpwnam_r");         }         exit(exit_failure);     }     printf("name: %s; uid: %ld\n", pwd.pw_gecos, (long) pwd.pw_uid);     exit(exit_success); }  

password file can written root:

/ # ls -l /etc/passwd -rw-r--r--    1 root     root           207 jan  1 00:29 /etc/passwd / # 

i tried run program (test) root rights, failed when gave existing username.

/ # /tmp/test admin getpwnam_r: no such file or directory / #  

1) so, forgot about, or should additionally?

2) need use /etc/shadow file storing passwords system users?


update:

my passwd file is:

~ # cat /etc/passwd  root:b6mvch7fplasn:0:0:root:/home/root:/bin/ash       admin:8mt/jtxcyg8ay:1000:1000:admin:/tmp:/tmp/cli  user:5v4hopra9ntuo:1001:1000:user:/tmp:/tmp/cli ~ # 

thanks in advance! bakir

1) search service or method used in password database (/etc/passwd) defined in /etc/nsswitch.conf. use service getpwnam function calls shared library in lib directory: /lib/libnss_service.so.x, service search method. in case compat default method because of absent of /etc/nsswitch.conf. so, need add libnss_compat.so.2 /lib.

strace useful thing!

many osqx , alk!


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -