home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3340 / shadow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  5.7 KB  |  318 lines

  1. /*
  2.  * Copyright 1989, 1990, 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.  
  16. #ifndef    BSD
  17. #include <string.h>
  18. #include <memory.h>
  19. #else
  20. #include <strings.h>
  21. #define    strchr    index
  22. #define    strrchr    rindex
  23. #endif
  24.  
  25. #ifdef    NDBM
  26. #include <ndbm.h>
  27. #include <fcntl.h>
  28. DBM    *sp_dbm;
  29. int    sp_dbm_mode = -1;
  30. static    int    dbmopened;
  31. static    int    dbmerror;
  32. #endif
  33.  
  34.  
  35. #ifndef    lint
  36. static    char    sccsid[] = "@(#)shadow.c    3.8    07:57:47    2/8/91";
  37. #endif
  38.  
  39. static    FILE    *shadow;
  40. static    char    spwbuf[BUFSIZ];
  41. static    struct    spwd    spwd;
  42.  
  43. #define    FIELDS    9
  44. #define    OFIELDS    5
  45.  
  46. void
  47. setspent ()
  48. {
  49.     int    mode;
  50.  
  51.     if (shadow)
  52.         rewind (shadow);
  53.     else
  54.         shadow = fopen (SHADOW, "r");
  55.  
  56.     /*
  57.      * Attempt to open the DBM files if they have never been opened
  58.      * and an error has never been returned.
  59.      */
  60.  
  61. #ifdef NDBM
  62.     if (! dbmerror && ! dbmopened) {
  63.         int    mode;
  64.         char    dbmfiles[BUFSIZ];
  65.  
  66.         strcpy (dbmfiles, SHADOW);
  67.         strcat (dbmfiles, ".pag");
  68.  
  69.         if (sp_dbm_mode == -1)
  70.             mode = O_RDWR;
  71.         else
  72.             mode = (sp_dbm_mode == O_RDWR) ? O_RDWR:O_RDONLY;
  73.  
  74.         if (! (sp_dbm = dbm_open (SHADOW, mode, 0)))
  75.             dbmerror = 1;
  76.         else
  77.             dbmopened = 1;
  78.     }
  79. #endif
  80. }
  81.  
  82. void
  83. endspent ()
  84. {
  85.     if (shadow)
  86.         (void) fclose (shadow);
  87.  
  88.     shadow = (FILE *) 0;
  89. #ifdef    NDBM
  90.     if (dbmopened && sp_dbm) {
  91.         dbm_close (sp_dbm);
  92.         sp_dbm = 0;
  93.     }
  94.     dbmopened = 0;
  95.     dbmerror = 0;
  96. #endif
  97. }
  98.  
  99. struct spwd *
  100. sgetspent (string)
  101. char    *string;
  102. {
  103.     char    *fields[FIELDS];
  104.     char    *cp;
  105.     char    *cpp;
  106.     int    atoi ();
  107.     long    atol ();
  108.     int    i;
  109.  
  110.     strncpy (spwbuf, string, BUFSIZ-1);
  111.     spwbuf[BUFSIZ-1] = '\0';
  112.  
  113.     if (cp = strrchr (spwbuf, '\n'))
  114.         *cp = '\0';
  115.  
  116.     for (cp = spwbuf, i = 0;*cp && i < FIELDS;i++) {
  117.         fields[i] = cp;
  118.         while (*cp && *cp != ':')
  119.             cp++;
  120.  
  121.         if (*cp)
  122.             *cp++ = '\0';
  123.     }
  124.     if (i == (FIELDS-1))
  125.         fields[i++] = cp;
  126.  
  127.     if (*cp || (i != FIELDS && i != OFIELDS))
  128.         return 0;
  129.  
  130.     spwd.sp_namp = fields[0];
  131.     spwd.sp_pwdp = fields[1];
  132.  
  133.     if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp)
  134.         return 0;
  135.     else if (fields[2][0] == '\0')
  136.         spwd.sp_lstchg = -1;
  137.  
  138.     if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp)
  139.         return 0;
  140.     else if (fields[3][0] == '\0')
  141.         spwd.sp_min = -1;
  142.  
  143.     if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp)
  144.         return 0;
  145.     else if (fields[4][0] == '\0')
  146.         spwd.sp_max = -1;
  147.  
  148.     if (i == OFIELDS) {
  149.         spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
  150.             spwd.sp_flag = -1;
  151.  
  152.         return &spwd;
  153.     }
  154.     if ((spwd.sp_warn = strtol (fields[5], &cpp, 10)) == 0 && *cpp)
  155.         return 0;
  156.     else if (fields[5][0] == '\0')
  157.         spwd.sp_warn = -1;
  158.  
  159.     if ((spwd.sp_inact = strtol (fields[6], &cpp, 10)) == 0 && *cpp)
  160.         return 0;
  161.     else if (fields[6][0] == '\0')
  162.         spwd.sp_inact = -1;
  163.  
  164.     if ((spwd.sp_expire = strtol (fields[7], &cpp, 10)) == 0 && *cpp)
  165.         return 0;
  166.     else if (fields[7][0] == '\0')
  167.         spwd.sp_expire = -1;
  168.  
  169.     if ((spwd.sp_flag = strtol (fields[8], &cpp, 10)) == 0 && *cpp)
  170.         return 0;
  171.     else if (fields[8][0] == '\0')
  172.         spwd.sp_flag = -1;
  173.  
  174.     return (&spwd);
  175. }
  176.  
  177. struct spwd
  178. *fgetspent (fp)
  179. FILE    *fp;
  180. {
  181.     char    buf[BUFSIZ];
  182.  
  183.     if (! fp)
  184.         return (0);
  185.  
  186.     if (fgets (buf, BUFSIZ, fp) == (char *) 0)
  187.         return (0);
  188.  
  189.     return sgetspent (buf);
  190. }
  191.  
  192. struct spwd
  193. *getspent ()
  194. {
  195.     if (! shadow)
  196.         setspent ();
  197.  
  198.     return (fgetspent (shadow));
  199. }
  200.  
  201. struct spwd
  202. *getspnam (name)
  203. char    *name;
  204. {
  205.     struct    spwd    *sp;
  206. #ifdef NDBM
  207.     datum    key;
  208.     datum    content;
  209. #endif
  210.  
  211.     setspent ();
  212.  
  213. #ifdef NDBM
  214.  
  215.     /*
  216.      * If the DBM file are now open, create a key for this UID and
  217.      * try to fetch the entry from the database.  A matching record
  218.      * will be unpacked into a static structure and returned to
  219.      * the user.
  220.      */
  221.  
  222.     if (dbmopened) {
  223.         key.dsize = strlen (name);
  224.         key.dptr = name;
  225.  
  226.         content = dbm_fetch (sp_dbm, key);
  227.         if (content.dptr != 0) {
  228.             memcpy (spwbuf, content.dptr, content.dsize);
  229.             spw_unpack (spwbuf, content.dsize, &spwd);
  230.             return &spwd;
  231.         }
  232.     }
  233. #endif
  234.     while ((sp = getspent ()) != (struct spwd *) 0) {
  235.         if (strcmp (name, sp->sp_namp) == 0)
  236.             return (sp);
  237.     }
  238.     return (0);
  239. }
  240.  
  241. int
  242. putspent (sp, fp)
  243. struct    spwd    *sp;
  244. FILE    *fp;
  245. {
  246.     int    errors = 0;
  247.  
  248.     if (! fp || ! sp)
  249.         return -1;
  250.  
  251.     if (fprintf (fp, "%s:%s:", sp->sp_namp, sp->sp_pwdp) < 0)
  252.         errors++;
  253.  
  254.     if (sp->sp_lstchg != -1) {
  255.         if (fprintf (fp, "%ld:", sp->sp_lstchg) < 0)
  256.             errors++;
  257.     } else if (putc (':', fp) == EOF)
  258.         errors++;
  259.  
  260.     if (sp->sp_min != -1) {
  261.         if (fprintf (fp, "%ld:", sp->sp_min) < 0)
  262.             errors++;
  263.     } else if (putc (':', fp) == EOF)
  264.         errors++;
  265.  
  266.     if (sp->sp_max != -1) {
  267.         if (fprintf (fp, "%ld", sp->sp_max) < 0)
  268.             errors++;
  269.     }
  270.  
  271.     /*
  272.      * See if the structure has any of the SVR4 fields in
  273.      * it.  If none of those fields have any data there is
  274.      * no reason to write them out since they will be filled
  275.      * in the same way when they are read back in.  Otherwise
  276.      * there is at least one SVR4 field that must be output.
  277.      */
  278.  
  279.     if (sp->sp_warn == -1 && sp->sp_inact == -1 &&
  280.             sp->sp_expire == -1 && sp->sp_flag == -1) {
  281.         if (putc ('\n', fp) == EOF || errors)
  282.             return -1;
  283.         else
  284.             return 0;
  285.     } else if (putc (':', fp) == EOF)
  286.         errors++;
  287.  
  288.     if (sp->sp_warn != -1) {
  289.         if (fprintf (fp, "%ld:", sp->sp_warn) < 0)
  290.             errors++;
  291.     } else if (putc (':', fp) == EOF)
  292.         errors++;
  293.  
  294.     if (sp->sp_inact != -1) {
  295.         if (fprintf (fp, "%ld:", sp->sp_inact) < 0)
  296.             errors++;
  297.     } else if (putc (':', fp) == EOF)
  298.         errors++;
  299.  
  300.     if (sp->sp_expire != -1) {
  301.         if (fprintf (fp, "%ld:", sp->sp_expire) < 0)
  302.             errors++;
  303.     } else if (putc (':', fp) == EOF)
  304.         errors++;
  305.  
  306.     if (sp->sp_flag != -1) {
  307.         if (fprintf (fp, "%ld", sp->sp_flag) < 0)
  308.             errors++;
  309.     }
  310.     if (putc ('\n', fp) == EOF)
  311.         errors++;
  312.  
  313.     if (errors)
  314.         return -1;
  315.     else
  316.         return 0;
  317. }
  318.