Created
April 24, 2015 20:34
-
-
Save mirekfranc/f90a026441bf53692db9 to your computer and use it in GitHub Desktop.
Reconstructing /etc/passwd based on API, in case of NIS or LDAP, the results will correctly differ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pwd.h> | |
#include <string.h> | |
#include <unistd.h> | |
int | |
main (int argc, char *argv[]) | |
{ | |
const int s = 4096; | |
char newpasswd[] = "/tmp/passwd.XXXXXX"; | |
char line[s]; | |
int fd; | |
struct passwd *ptr; | |
if ((fd = mkstemp (newpasswd)) < 0) | |
{ | |
perror ("mkstemp"); | |
return 1; | |
} | |
setpwent (); | |
while ((ptr = getpwent ()) != NULL) | |
{ | |
snprintf (line, s, "%s:%s:%ld:%ld:%s:%s:%s\n", | |
ptr->pw_name, ptr->pw_passwd, | |
(long) ptr->pw_uid, (long) ptr->pw_gid, | |
ptr->pw_gecos, ptr->pw_dir, ptr->pw_shell); | |
write (fd, line, strlen (line)); | |
} | |
endpwent (); | |
if (close (fd) < 0) | |
perror ("close"); | |
puts (newpasswd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment