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

  1. /*
  2.  * Copyright 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 <stdio.h>
  13. #include "shadow.h"
  14. #ifdef    BSD
  15. #include <strings.h>
  16. #else
  17. #include <string.h>
  18. #endif
  19.  
  20. #ifndef    lint
  21. static    char    sccsid[] = "@(#)gspack.c    3.1    09:13:50    13 Dec 1990";
  22. #endif
  23.  
  24. /*
  25.  * sgr_pack - convert a shadow group structure to a packed
  26.  *          shadow group record
  27.  *
  28.  *    sgr_pack takes the shadow group structure and packs
  29.  *    the components in a record.  this record will be
  30.  *    unpacked later by sgr_unpack.
  31.  */
  32.  
  33. int
  34. sgr_pack (sgrp, buf)
  35. struct    sgrp    *sgrp;
  36. char    *buf;
  37. {
  38.     char    *cp;
  39.     int    i;
  40.  
  41.     /*
  42.      * The name and password are both easy - append each string
  43.      * to the buffer.  These are always the first two strings
  44.      * in a record.
  45.      */
  46.  
  47.     cp = buf;
  48.     strcpy (cp, sgrp->sg_name);
  49.     cp += strlen (cp) + 1;
  50.  
  51.     strcpy (cp, sgrp->sg_passwd);
  52.     cp += strlen (cp) + 1;
  53.  
  54.     /*
  55.      * The arrays of administrators and members are slightly
  56.      * harder.  Each element is appended as a string, with a
  57.      * final '\0' appended to serve as a blank string.  The
  58.      * number of elements is not known in advance, so the
  59.      * entire collection of administrators must be scanned to
  60.      * find the start of the members.
  61.      */
  62.  
  63.     for (i = 0;sgrp->sg_adm[i];i++) {
  64.         strcpy (cp, sgrp->sg_adm[i]);
  65.         cp += strlen (cp) + 1;
  66.     }
  67.     *cp++ = '\0';
  68.  
  69.     for (i = 0;sgrp->sg_mem[i];i++) {
  70.         strcpy (cp, sgrp->sg_mem[i]);
  71.         cp += strlen (cp) + 1;
  72.     }
  73.     *cp++ = '\0';
  74.  
  75.     return cp - buf;
  76. }
  77.  
  78. /*
  79.  * sgr_unpack - convert a packed shadow group record to an
  80.  *            unpacked record
  81.  *
  82.  *    sgr_unpack converts a record which was packed by sgr_pack
  83.  *    into the normal shadow group structure format.
  84.  */
  85.  
  86. int
  87. sgr_unpack (buf, len, sgrp)
  88. char    *buf;
  89. int    len;
  90. struct    sgrp    *sgrp;
  91. {
  92.     char    *org = buf;
  93.     int    i;
  94.  
  95.     /*
  96.      * The name and password are both easy - they are the first
  97.      * two strings in the record.
  98.      */
  99.  
  100.     sgrp->sg_name = buf;
  101.     buf += strlen (buf) + 1;
  102.     if (buf - org > len)
  103.         return -1;
  104.  
  105.     sgrp->sg_passwd = buf;
  106.     buf += strlen (buf) + 1;
  107.     if (buf - org > len)
  108.         return -1;
  109.  
  110.     /*
  111.      * The administrators and members are slightly more difficult.
  112.      * The arrays are lists of strings.  Each list is terminated
  113.      * by a string of length zero.  This string is detected by
  114.      * looking for an initial character of '\0'.
  115.      */
  116.  
  117.     for (i = 0;*buf && i < 1024;i++) {
  118.         sgrp->sg_adm[i] = buf;
  119.         buf += strlen (buf) + 1;
  120.  
  121.         if (buf - org > len)
  122.             return -1;
  123.     }
  124.     sgrp->sg_adm[i] = (char *) 0;
  125.     if (! *buf)
  126.         buf++;
  127.  
  128.     for (i = 0;*buf && i < 1024;i++) {
  129.         sgrp->sg_mem[i] = buf;
  130.         buf += strlen (buf) + 1;
  131.  
  132.         if (buf - org > len)
  133.             return -1;
  134.     }
  135.     sgrp->sg_mem[i] = (char *) 0;
  136.  
  137.     return 0;
  138. }
  139.