home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / gshadow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  4.5 KB  |  279 lines

  1. /*
  2.  * Copyright 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include "shadow.h"
  13. #include "config.h"
  14. #include <stdio.h>
  15. #ifndef    BSD
  16. #include <string.h>
  17. #include <memory.h>
  18. #else
  19. #include <strings.h>
  20. #define    strchr    index
  21. #define    strrchr    rindex
  22. #endif
  23.  
  24. #ifdef    NDBM
  25. #include <ndbm.h>
  26. #include <fcntl.h>
  27. DBM    *sgr_dbm;
  28. int    sg_dbm_mode = -1;
  29. static    int    dbmopened;
  30. static    int    dbmerror;
  31. #endif
  32.  
  33.  
  34. #ifndef    lint
  35. static    char    sccsid[] = "@(#)gshadow.c    3.7    08:45:58    9/12/91";
  36. #endif
  37.  
  38. #define    MAXMEM    1024
  39.  
  40. static    FILE    *shadow;
  41. static    char    *sgrpfile = "/etc/gshadow";
  42. static    char    sgrbuf[BUFSIZ*4];
  43. static    char    *members[MAXMEM+1];
  44. static    char    *admins[MAXMEM+1];
  45. static    struct    sgrp    sgroup;
  46.  
  47. extern    char    *fgetsx();
  48. extern    int    fputsx();
  49.  
  50. #define    FIELDS    4
  51.  
  52. static char **
  53. list (s, l)
  54. char    *s;
  55. char    **l;
  56. {
  57.     int    nmembers = 0;
  58.  
  59.     while (s && *s) {
  60.         l[nmembers++] = s;
  61.         if (s = strchr (s, ','))
  62.             *s++ = '\0';
  63.     }
  64.     l[nmembers] = (char *) 0;
  65.     return l;
  66. }
  67.  
  68. void
  69. setsgent ()
  70. {
  71. #ifdef    NDBM
  72.     int    mode;
  73. #endif    /* NDBM */
  74.  
  75.     if (shadow)
  76.         rewind (shadow);
  77.     else
  78.         shadow = fopen (GSHADOW, "r");
  79.  
  80.     /*
  81.      * Attempt to open the DBM files if they have never been opened
  82.      * and an error has never been returned.
  83.      */
  84.  
  85. #ifdef NDBM
  86.     if (! dbmerror && ! dbmopened) {
  87.         char    dbmfiles[BUFSIZ];
  88.  
  89.         strcpy (dbmfiles, sgrpfile);
  90.         strcat (dbmfiles, ".pag");
  91.  
  92.         if (sg_dbm_mode == -1)
  93.             mode = O_RDWR;
  94.         else
  95.             mode = (sg_dbm_mode == O_RDWR) ? O_RDWR:O_RDONLY;
  96.  
  97.         if (access (dbmfiles, 0) ||
  98.             (! (sgr_dbm = dbm_open (sgrpfile, mode, 0))))
  99.             dbmerror = 1;
  100.         else
  101.             dbmopened = 1;
  102.     }
  103. #endif    /* NDBM */
  104. }
  105.  
  106. void
  107. endsgent ()
  108. {
  109.     if (shadow)
  110.         (void) fclose (shadow);
  111.  
  112.     shadow = (FILE *) 0;
  113. #ifdef    NDBM
  114.     if (dbmopened && sgr_dbm) {
  115.         dbm_close (sgr_dbm);
  116.         dbmopened = 0;
  117.         sgr_dbm = 0;
  118.     }
  119. #endif
  120. }
  121.  
  122. struct sgrp *
  123. sgetsgent (string)
  124. char    *string;
  125. {
  126.     char    *fields[FIELDS];
  127.     char    *cp;
  128.     int    atoi ();
  129.     long    atol ();
  130.     int    i;
  131.  
  132.     strncpy (sgrbuf, string, (int) sizeof sgrbuf - 1);
  133.     sgrbuf[sizeof sgrbuf - 1] = '\0';
  134.  
  135.     if (cp = strrchr (sgrbuf, '\n'))
  136.         *cp = '\0';
  137.  
  138.     for (cp = sgrbuf, i = 0;i < FIELDS && cp;i++) {
  139.         fields[i] = cp;
  140.         if (cp = strchr (cp, ':'))
  141.             *cp++ = '\0';
  142.     }
  143.     if (*cp || i != FIELDS)
  144.         return 0;
  145.  
  146.     sgroup.sg_name = fields[0];
  147.     sgroup.sg_passwd = fields[1];
  148.     sgroup.sg_adm = list (fields[2], admins);
  149.     sgroup.sg_mem = list (fields[3], members);
  150.  
  151.     return &sgroup;
  152. }
  153.  
  154. struct sgrp
  155. *fgetsgent (fp)
  156. FILE    *fp;
  157. {
  158.     char    buf[sizeof sgrbuf];
  159.  
  160.     if (! fp)
  161.         return (0);
  162.  
  163.     if (fgetsx (buf, sizeof buf, fp) == (char *) 0)
  164.         return (0);
  165.  
  166.     return sgetsgent (buf);
  167. }
  168.  
  169. struct sgrp
  170. *getsgent ()
  171. {
  172.     if (! shadow)
  173.         setsgent ();
  174.  
  175.     return (fgetsgent (shadow));
  176. }
  177.  
  178. struct sgrp *
  179. getsgnam (name)
  180. char    *name;
  181. {
  182.     struct    sgrp    *sgrp;
  183. #ifdef NDBM
  184.     datum    key;
  185.     datum    content;
  186. #endif
  187.  
  188.     setsgent ();
  189.  
  190. #ifdef NDBM
  191.  
  192.     /*
  193.      * If the DBM file are now open, create a key for this group and
  194.      * try to fetch the entry from the database.  A matching record
  195.      * will be unpacked into a static structure and returned to
  196.      * the user.
  197.      */
  198.  
  199.     if (dbmopened) {
  200.         key.dsize = strlen (name);
  201.         key.dptr = name;
  202.  
  203.         content = dbm_fetch (sgr_dbm, key);
  204.         if (content.dptr != 0) {
  205.             memcpy (sgrbuf, content.dptr, content.dsize);
  206.             sgroup.sg_mem = members;
  207.             sgroup.sg_adm = admins;
  208.             sgr_unpack (sgrbuf, content.dsize, &sgroup);
  209.             return &sgroup;
  210.         }
  211.     }
  212. #endif
  213.     while ((sgrp = getsgent ()) != (struct sgrp *) 0) {
  214.         if (strcmp (name, sgrp->sg_name) == 0)
  215.             return (sgrp);
  216.     }
  217.     return (0);
  218. }
  219.  
  220. int
  221. putsgent (sgrp, fp)
  222. struct    sgrp    *sgrp;
  223. FILE    *fp;
  224. {
  225.     char    buf[sizeof sgrbuf];
  226.     char    *cp = buf;
  227.     int    i;
  228.  
  229.     if (! fp || ! sgrp)
  230.         return -1;
  231.  
  232.     /*
  233.      * Copy the group name and passwd.
  234.      */
  235.  
  236.     strcpy (cp, sgrp->sg_name);
  237.     cp += strlen (cp);
  238.     *cp++ = ':';
  239.  
  240.     strcpy (cp, sgrp->sg_passwd);
  241.     cp += strlen (cp);
  242.     *cp++ = ':';
  243.  
  244.     /*
  245.      * Copy the administrators, separating each from the other
  246.      * with a ",".
  247.      */
  248.  
  249.     for (i = 0;sgrp->sg_adm[i];i++) {
  250.         if (i > 0)
  251.             *cp++ = ',';
  252.  
  253.         strcpy (cp, sgrp->sg_adm[i]);
  254.         cp += strlen (cp);
  255.     }
  256.     *cp++ = ':';
  257.  
  258.     /*
  259.      * Now do likewise with the group members.
  260.      */
  261.  
  262.     for (i = 0;sgrp->sg_mem[i];i++) {
  263.         if (i > 0)
  264.             *cp++ = ',';
  265.  
  266.         strcpy (cp, sgrp->sg_mem[i]);
  267.         cp += strlen (cp);
  268.     }
  269.     *cp++ = '\n';
  270.     *cp = '\0';
  271.  
  272.     /*
  273.      * Output using the function which understands the line
  274.      * continuation conventions.
  275.      */
  276.  
  277.     return fputsx (buf, fp);
  278. }
  279.