home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tnlogin.zip / pwd.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  4KB  |  206 lines

  1. /* pwd.c - /etc/passwd access library
  2.  *
  3.  * Author:  Kai Uwe Rommel <rommel@ars.muc.de>
  4.  * Created: Mon Mar 28 1994
  5.  */
  6.  
  7. static char *rcsid =
  8. "$Id: pwd.c,v 1.1 1994/09/16 08:32:48 rommel Exp $";
  9. static char *rcsrev = "$Revision: 1.1 $";
  10.  
  11. /*
  12.  * $Log: pwd.c,v $
  13.  * Revision 1.1  1994/09/16 08:32:48  rommel
  14.  * Initial revision
  15.  * 
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22.  
  23. #include <pwd.h>
  24.  
  25. static struct passwd *getpwent(char *name, int uid)
  26. {
  27.   static struct passwd pw;
  28.   static char buffer[256];
  29.   char *ptr;
  30.   FILE *passwd;
  31.   int found = 0;
  32.  
  33.   if ((ptr = getenv("ETC")) == NULL)
  34.     return NULL;
  35.  
  36.   strcpy(buffer, ptr);
  37.   strcat(buffer, "/passwd");
  38.  
  39.   if ((passwd = fopen(buffer, "r")) == NULL)
  40.     return NULL;
  41.  
  42.   while (!found && fgets(buffer, sizeof(buffer), passwd) != NULL)
  43.   {
  44.     if (buffer[strlen(buffer) - 1] == '\n')
  45.       buffer[strlen(buffer) - 1] = 0;
  46.  
  47.     if (buffer[0] == '#')
  48.       continue;
  49.  
  50.     pw.pw_dir = ""; /* these are optional */
  51.     pw.pw_shell = "";
  52.  
  53.     pw.pw_name = buffer;
  54.  
  55.     if ((ptr = strchr(buffer, ':')) != NULL)
  56.       *ptr++ = 0;
  57.     else
  58.       continue;
  59.  
  60.     pw.pw_passwd = ptr;
  61.  
  62.     if ((ptr = strchr(ptr, ':')) != NULL)
  63.       *ptr++ = 0;
  64.     else
  65.       continue;
  66.  
  67.     pw.pw_uid = atoi(ptr);
  68.  
  69.     if ((ptr = strchr(ptr, ':')) != NULL)
  70.       *ptr++ = 0;
  71.     else
  72.       continue;
  73.  
  74.     pw.pw_gid = atoi(ptr);
  75.  
  76.     if ((ptr = strchr(ptr, ':')) != NULL)
  77.       *ptr++ = 0;
  78.     else
  79.       continue;
  80.  
  81.     if (name != NULL && stricmp(name, pw.pw_name) == 0 ||
  82.     uid != -1 && uid == pw.pw_uid)
  83.       found = 1;
  84.  
  85.     pw.pw_gecos = ptr;
  86.  
  87.     if ((ptr = strchr(ptr, ':')) != NULL)
  88.       *ptr++ = 0;
  89.     else
  90.       continue;
  91.  
  92.     pw.pw_dir = ptr;
  93.  
  94.     if ((ptr = strchr(ptr, ':')) != NULL)
  95.       *ptr++ = 0;
  96.     else
  97.       continue;
  98.  
  99.     pw.pw_shell = ptr;
  100.  
  101.     for (ptr = pw.pw_dir; (ptr = strchr(ptr, ';')) != NULL; *ptr = ':');
  102.     for (ptr = pw.pw_shell; (ptr = strchr(ptr, ';')) != NULL; *ptr = ':');
  103.   }
  104.  
  105.   fclose(passwd);
  106.  
  107.   return found ? &pw : NULL;
  108. }
  109.  
  110. struct passwd *getpwnam(char *name)
  111. {
  112.   return getpwent(name, -1);
  113. }
  114.  
  115. struct passwd *getpwuid(int uid)
  116. {
  117.   return getpwent(NULL, uid);
  118. }
  119.  
  120. int setpwent(struct passwd *pw)
  121. {
  122.   char real[256], new[256], bak[256], line[256], buffer[256], newent[256];
  123.   char *ptr, *name;
  124.   FILE *passwd, *newpasswd;
  125.   int found = 0;
  126.  
  127.   if ((ptr = getenv("ETC")) == NULL)
  128.     return -1;
  129.  
  130.   strcpy(real, ptr);
  131.   strcat(real, "/passwd");
  132.   strcpy(new, real);
  133.   strcat(new, ".new");
  134.   strcpy(bak, real);
  135.   strcat(bak, ".bak");
  136.  
  137.   if ((passwd = fopen(real, "r")) == NULL)
  138.     /* ignore */;
  139.  
  140.   if ((newpasswd = fopen(new, "w")) == NULL)
  141.     return -1;
  142.  
  143.   sprintf(newent, "%s:%s:%d:%d:%s:", pw->pw_name, pw->pw_passwd,
  144.       pw->pw_uid, pw->pw_gid, pw->pw_gecos);
  145.  
  146.   strcpy(line, pw->pw_dir);
  147.   for (ptr = line; (ptr = strchr(ptr, ':')) != NULL; *ptr = ';');
  148.  
  149.   strcat(newent, line);
  150.   strcat(newent, ":");
  151.  
  152.   strcpy(line, pw->pw_shell);
  153.   for (ptr = line; (ptr = strchr(ptr, ':')) != NULL; *ptr = ';');
  154.  
  155.   strcat(newent, line);
  156.   strcat(newent, "\n");
  157.  
  158.   while (passwd && fgets(buffer, sizeof(buffer), passwd) != NULL)
  159.   {
  160.     strcpy(line, buffer);
  161.  
  162.     if (buffer[strlen(buffer) - 1] == '\n')
  163.       buffer[strlen(buffer) - 1] = 0;
  164.  
  165.     if (buffer[0] != '#')
  166.     {
  167.       name = buffer;
  168.  
  169.       if ((ptr = strchr(buffer, ':')) != NULL)
  170.       {
  171.     *ptr++ = 0;
  172.  
  173.     if (strcmp(pw->pw_name, name) == 0)
  174.     {
  175.       found = 1;
  176.  
  177.       if (pw->pw_uid != -1) /* that would mean 'delete' */
  178.         fputs(newent, newpasswd);
  179.  
  180.       continue;
  181.     }
  182.       }
  183.     }
  184.  
  185.     fputs(line, newpasswd);
  186.   }
  187.  
  188.   if (!found && pw->pw_uid != -1) /* append new entry */
  189.     fputs(newent, newpasswd);
  190.  
  191.   fclose(newpasswd);
  192.  
  193.   if (passwd)
  194.     fclose(passwd);
  195.  
  196.   unlink(bak);
  197.   rename(real, bak);
  198.  
  199.   if (rename(new, real))
  200.     return -1;
  201.  
  202.   return 0;
  203. }
  204.  
  205. /* end of pwd.c */
  206.