Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Created April 24, 2015 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirekfranc/f90a026441bf53692db9 to your computer and use it in GitHub Desktop.
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
#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