|
|
1.1 ! root 1: /* ! 2: * The example shows the usage of getuid(), geteuid() COHERENT SYstem calls ! 3: * and getpwnam, getpwuid, setpwent, getpwent, and endpwent functions. ! 4: */ ! 5: #include <pwd.h> ! 6: #include <stdio.h> ! 7: ! 8: main() ! 9: { ! 10: int euid, /* Effective user id */ ! 11: ruid; /* Real user id */ ! 12: struct passwd *pstp; ! 13: int i; ! 14: ! 15: /* Print out all users and home directories */ ! 16: i = 0; ! 17: setpwent(); /* Rewind file /etc/passwd */ ! 18: while ((pstp = getpwent()) != NULL) ! 19: printf("%d: user name is %s, home directory is %s.\n", ! 20: ++i, pstp->pw_name, pstp->pw_dir); ! 21: ! 22: /* Find real user name. ! 23: * ! 24: * NOTE: functions getpwuid and getpwnam will rewind /etc/passwd file ! 25: * by calling setpwent. ! 26: */ ! 27: ruid = getuid(); ! 28: if ((pstp = getpwuid(ruid)) == NULL) ! 29: /* If this message come out too bad. Possible file /etc/passwd corrupted. */ ! 30: fprintf(stderr, "Cannot find user with id number %d\n", pstp); ! 31: else ! 32: printf("Real user name is %s\n", pstp->pw_name); ! 33: ! 34: /* Find the user id for superuser root */ ! 35: ((pstp = getpwnam("root")) == NULL) ? ! 36: fprintf(stderr, "Do you have user root on your system?\n") : ! 37: printf("root id is %d\n", pstp->pw_uid); ! 38: ! 39: /* Check is the effective process id is the superuser id * ! 40: * ! 41: * NOTE: if you would like to see how to enable root prileges, ! 42: * you can run next command from command line: ! 43: * cc pwfun.c ! 44: * su root chown root pwfun ! 45: * su root chmod 4511 pwfun ! 46: */ ! 47: euid = geteuid(); ! 48: printf("%d %d\n", ruid, euid); ! 49: printf("Process "); ! 50: (euid == pstp->pw_uid) ? printf("has ") : printf("doesn't have "); ! 51: printf("the root priveleges\n"); ! 52: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.