home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / gsdbm.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  3KB  |  148 lines

  1. /*
  2.  * Copyright 1990, 1991, 1992, 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.  * This software is provided on an AS-IS basis and the author makes
  12.  * no warrantee of any kind.
  13.  */
  14.  
  15. #ifndef    lint
  16. static    char    sccsid[] = "@(#)gsdbm.c    3.6    11:32:14    28 Jul 1992";
  17. #endif
  18.  
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include "shadow.h"
  22. #include "config.h"
  23.  
  24. #ifdef    NDBM
  25. #include <ndbm.h>
  26. DBM    *sg_dbm;
  27.  
  28. #define    GRP_FRAG    256
  29.  
  30. /*
  31.  * sg_dbm_update
  32.  *
  33.  * Updates the DBM password files, if they exist.
  34.  */
  35.  
  36. int
  37. sg_dbm_update (sgr)
  38. struct    sgrp    *sgr;
  39. {
  40.     datum    key;
  41.     datum    content;
  42.     char    data[BUFSIZ*8];
  43.     char    sgrpkey[60];
  44.     char    *cp;
  45.     int    len;
  46.     int    i;
  47.     int    cnt;
  48.     static    int    once;
  49.  
  50.     if (! once) {
  51.         if (! sg_dbm)
  52.             setsgent ();
  53.  
  54.         once++;
  55.     }
  56.     if (! sg_dbm)
  57.         return 0;
  58.  
  59.     len = sgr_pack (sgr, data);
  60.  
  61.     if (len <= GRP_FRAG) {
  62.         content.dsize = len;
  63.         content.dptr = data;
  64.  
  65.         key.dsize = strlen (sgr->sg_name);
  66.         key.dptr = sgr->sg_name;
  67.         if (dbm_store (sg_dbm, key, content, DBM_REPLACE))
  68.             return 0;
  69.     } else {
  70.         content.dsize = sizeof cnt;
  71.         content.dptr = (char *) &cnt;
  72.         cnt = (len + (GRP_FRAG-1)) / GRP_FRAG;
  73.  
  74.         key.dsize = strlen (sgr->sg_name);
  75.         key.dptr = sgr->sg_name;
  76.         if (dbm_store (sg_dbm, key, content, DBM_REPLACE))
  77.             return 0;
  78.  
  79.         for (cp = data, i = 0;i < cnt;i++) {
  80.             content.dsize = len > GRP_FRAG ? GRP_FRAG:len;
  81.             len -= content.dsize;
  82.             content.dptr = cp;
  83.             cp += content.dsize;
  84.  
  85.             key.dsize = sizeof i + strlen (sgr->sg_name);
  86.             key.dptr = sgrpkey;
  87.             memcpy (sgrpkey, (char *) &i, sizeof i);
  88.             strcpy (sgrpkey + sizeof i, sgr->sg_name);
  89.             if (dbm_store (sg_dbm, key, content, DBM_REPLACE))
  90.                 return 0;
  91.         }
  92.     }
  93.     return 1;
  94. }
  95.  
  96. /*
  97.  * sg_dbm_remove
  98.  *
  99.  * Deletes the DBM shadow group file entries, if they exist.
  100.  */
  101.  
  102. int
  103. sg_dbm_remove (name)
  104. char    *name;
  105. {
  106.     datum    key;
  107.     datum    content;
  108.     char    grpkey[60];
  109.     int    i;
  110.     int    cnt;
  111.     int    errors = 0;
  112.     static    int    once;
  113.  
  114.     if (! once) {
  115.         if (! sg_dbm)
  116.             setsgent ();
  117.  
  118.         once++;
  119.     }
  120.     if (! sg_dbm)
  121.         return 0;
  122.  
  123.     key.dsize = strlen (name);
  124.     key.dptr = name;
  125.     content = dbm_fetch (sg_dbm, key);
  126.     if (content.dptr == 0)
  127.         ++errors;
  128.     else {
  129.         if (content.dsize == sizeof (int)) {
  130.             memcpy ((char *) &cnt, content.dptr, sizeof cnt);
  131.  
  132.             for (i = 0;i < cnt;i++) {
  133.                 key.dsize = sizeof i + strlen (name);
  134.                 key.dptr = grpkey;
  135.                 memcpy (grpkey, (char *) &i, sizeof i);
  136.                 strcpy (grpkey + sizeof i, name);
  137.                 if (dbm_delete (sg_dbm, key))
  138.                     ++errors;
  139.             }
  140.         } else {
  141.             if (dbm_delete (sg_dbm, key))
  142.                 ++errors;
  143.         }
  144.     }
  145.     return errors ? 0:1;
  146. }
  147. #endif
  148.