home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / pwdbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  2.5 KB  |  157 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. #ifndef    lint
  13. static    char    sccsid[] = "@(#)pwdbm.c    3.6    12:10:31    12/28/91";
  14. #endif
  15.  
  16. #ifdef    BSD
  17. #include <strings.h>
  18. #define    strchr    index
  19. #define    strrchr    rindex
  20. #else
  21. #include <string.h>
  22. #endif
  23. #include <stdio.h>
  24. #include "pwd.h"
  25. #include "config.h"
  26.  
  27. #if defined(DBM) || defined(NDBM) /*{*/
  28.  
  29. #ifdef    DBM
  30. #include <dbm.h>
  31. #endif
  32. #ifdef    NDBM
  33. #include <ndbm.h>
  34. DBM    *pw_dbm;
  35. #endif
  36.  
  37. /*
  38.  * pw_dbm_update
  39.  *
  40.  * Updates the DBM password files, if they exist.
  41.  */
  42.  
  43. int
  44. pw_dbm_update (pw)
  45. struct    passwd    *pw;
  46. {
  47.     datum    key;
  48.     datum    content;
  49.     char    data[BUFSIZ];
  50.     int    len;
  51.     static    int    once;
  52.  
  53.     if (! once) {
  54. #ifdef    NDBM
  55.         if (! pw_dbm)
  56.             setpwent ();
  57. #else
  58.         setpwent ();
  59. #endif
  60.         once++;
  61.     }
  62. #ifdef    DBM
  63.     strcpy (data, PWDFILE);
  64.     strcat (data, ".pag");
  65.     if (access (data, 0))
  66.         return 0;
  67. #endif
  68. #ifdef    NDBM
  69.     if (! pw_dbm)
  70.         return 0;
  71. #endif
  72.     len = pw_pack (pw, data);
  73.     content.dsize = len;
  74.     content.dptr = data;
  75.  
  76.     key.dsize = strlen (pw->pw_name);
  77.     key.dptr = pw->pw_name;
  78. #ifdef    DBM
  79.     if (store (key, content))
  80.         return 0;
  81. #endif
  82. #ifdef    NDBM
  83.     if (dbm_store (pw_dbm, key, content, DBM_REPLACE))
  84.         return 0;
  85. #endif
  86.  
  87.     key.dsize = sizeof pw->pw_uid;
  88.     key.dptr = (char *) &pw->pw_uid;
  89. #ifdef    DBM
  90.     if (store (key, content))
  91.         return 0;
  92. #endif
  93. #ifdef    NDBM
  94.     if (dbm_store (pw_dbm, key, content, DBM_REPLACE))
  95.         return 0;
  96. #endif
  97.     return 1;
  98. }
  99.  
  100. /*
  101.  * pw_dbm_remove
  102.  *
  103.  * Removes the DBM password entry, if it exists.
  104.  */
  105.  
  106. int
  107. pw_dbm_remove (pw)
  108. struct    passwd    *pw;
  109. {
  110.     datum    key;
  111.     static    int    once;
  112.     char    data[BUFSIZ];
  113.  
  114.     if (! once) {
  115. #ifdef    NDBM
  116.         if (! pw_dbm)
  117.             setpwent ();
  118. #else
  119.         setpwent ();
  120. #endif
  121.         once++;
  122.     }
  123. #ifdef    DBM
  124.     strcpy (data, PWDFILE);
  125.     strcat (data, ".pag");
  126.     if (access (data, 0))
  127.         return 0;
  128. #endif
  129. #ifdef    NDBM
  130.     if (! pw_dbm)
  131.         return 0;
  132. #endif
  133.     key.dsize = strlen (pw->pw_name);
  134.     key.dptr = pw->pw_name;
  135. #ifdef    DBM
  136.     if (delete (key))
  137.         return 0;
  138. #endif
  139. #ifdef    NDBM
  140.     if (dbm_delete (pw_dbm, key))
  141.         return 0;
  142. #endif
  143.     key.dsize = sizeof pw->pw_uid;
  144.     key.dptr = (char *) &pw->pw_uid;
  145. #ifdef    DBM
  146.     if (delete (key))
  147.         return 0;
  148. #endif
  149. #ifdef    NDBM
  150.     if (dbm_delete (pw_dbm, key))
  151.         return 0;
  152. #endif
  153.     return 1;
  154. }
  155.  
  156. #endif    /*} defined(NDBM) || defined(DBM) */
  157.