home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / groupdel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  4.4 KB  |  254 lines

  1. /*
  2.  * Copyright 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[] = "@(#)groupdel.c    3.5    19:39:55    12/28/91";
  14. #endif
  15.  
  16. #include <sys/types.h>
  17. #include <stdio.h>
  18. #include <grp.h>
  19. #include <ctype.h>
  20. #include <fcntl.h>
  21.  
  22. #ifdef    BSD
  23. #include <strings.h>
  24. #else
  25. #include <string.h>
  26. #endif
  27.  
  28. #include "config.h"
  29. #include "shadow.h"
  30.  
  31. #ifdef    USE_SYSLOG
  32. #include <syslog.h>
  33. #endif
  34.  
  35. char    group_name[BUFSIZ];
  36. char    *Prog;
  37.  
  38. #ifdef    NDBM
  39. extern    int    gr_dbm_mode;
  40. extern    int    sg_dbm_mode;
  41. #endif
  42. extern    char    *malloc();
  43.  
  44. extern    struct    group    *getgrnam();
  45. extern    int    gr_lock();
  46. extern    int    gr_unlock();
  47. extern    int    gr_open();
  48.  
  49. #ifdef    SHADOWGRP
  50. extern    int    sgr_lock();
  51. extern    int    sgr_unlock();
  52. extern    int    sgr_open();
  53. #endif
  54.  
  55. /*
  56.  * usage - display usage message and exit
  57.  */
  58.  
  59. usage ()
  60. {
  61.     fprintf (stderr, "usage: groupdel group\n");
  62.     exit (2);
  63. }
  64.  
  65. /*
  66.  * grp_update - update group file entries
  67.  *
  68.  *    grp_update() writes the new records to the group files.
  69.  */
  70.  
  71. void
  72. grp_update ()
  73. {
  74. #ifdef    NDBM
  75.     struct    group    *ogrp;
  76. #endif
  77.  
  78.     if (! gr_remove (group_name)) {
  79.         fprintf (stderr, "%s: error removing group entry\n", Prog);
  80.         exit (1);
  81.     }
  82. #ifdef    NDBM
  83.  
  84.     /*
  85.      * Update the DBM group file
  86.      */
  87.  
  88.     if (access ("/etc/group.pag", 0) == 0) {
  89.         if ((ogrp = getgrnam (group_name)) &&
  90.                 ! gr_dbm_remove (ogrp)) {
  91.             fprintf (stderr, "%s: error removing group dbm entry\n",
  92.                 Prog);
  93.             exit (1);
  94.         }
  95.     }
  96.     endgrent ();
  97. #endif    /* NDBM */
  98.  
  99. #ifdef    SHADOWGRP
  100.  
  101.     /*
  102.      * Delete the shadow group entries as well.
  103.      */
  104.  
  105.     if (! sgr_remove (group_name)) {
  106.         fprintf (stderr, "%s: error removing shadow group entry\n",
  107.             Prog);
  108.         exit (1);
  109.     }
  110. #ifdef    NDBM
  111.  
  112.     /*
  113.      * Update the DBM shadow group file
  114.      */
  115.  
  116.     if (access ("/etc/gshadow.pag", 0) == 0) {
  117.         if (! sgr_dbm_remove (group_name)) {
  118.             fprintf (stderr,
  119.                 "%s: error removing shadow group dbm entry\n",
  120.                 Prog);
  121.             exit (1);
  122.         }
  123.     }
  124.     endsgent ();
  125. #endif    /* NDBM */
  126. #endif    /* SHADOWGRP */
  127. #ifdef    USE_SYSLOG
  128.     syslog (LOG_INFO, "remove group `%s'\n", group_name);
  129. #endif    /* USE_SYSLOG */
  130. }
  131.  
  132. /*
  133.  * close_files - close all of the files that were opened
  134.  *
  135.  *    close_files() closes all of the files that were opened for this
  136.  *    new group.  This causes any modified entries to be written out.
  137.  */
  138.  
  139. close_files ()
  140. {
  141.     if (! gr_close ()) {
  142.         fprintf (stderr, "%s: cannot rewrite group file\n", Prog);
  143.         exit (1);
  144.     }
  145.     (void) gr_unlock ();
  146. #ifdef    SHADOWGRP
  147.     if (! sgr_close ()) {
  148.         fprintf (stderr, "%s: cannot rewrite shadow group file\n",
  149.             Prog);
  150.         exit (1);
  151.     }
  152.     (void) sgr_unlock ();
  153. #endif    /* SHADOWGRP */
  154. }
  155.  
  156. /*
  157.  * open_files - lock and open the group files
  158.  *
  159.  *    open_files() opens the two group files.
  160.  */
  161.  
  162. open_files ()
  163. {
  164.     if (! gr_lock ()) {
  165.         fprintf (stderr, "%s: unable to lock group file\n", Prog);
  166.         exit (1);
  167.     }
  168.     if (! gr_open (O_RDWR)) {
  169.         fprintf (stderr, "%s: unable to open group file\n", Prog);
  170.         exit (1);
  171.     }
  172. #ifdef    SHADOWGRP
  173.     if (! sgr_lock ()) {
  174.         fprintf (stderr, "%s: unable to lock shadow group file\n",
  175.             Prog);
  176.         exit (1);
  177.     }
  178.     if (! sgr_open (O_RDWR)) {
  179.         fprintf (stderr, "%s: unable to open shadow group file\n",
  180.             Prog);
  181.         exit (1);
  182.     }
  183. #endif    /* SHADOWGRP */
  184. }
  185.  
  186. /*
  187.  * main - groupdel command
  188.  *
  189.  *    The syntax of the groupdel command is
  190.  *    
  191.  *    groupdel group
  192.  *
  193.  *    The named group will be deleted.
  194.  */
  195.  
  196. main (argc, argv)
  197. int    argc;
  198. char    **argv;
  199. {
  200.  
  201.     /*
  202.      * Get my name so that I can use it to report errors.
  203.      */
  204.  
  205.     if (Prog = strrchr (argv[0], '/'))
  206.         Prog++;
  207.     else
  208.         Prog = argv[0];
  209.  
  210.     if (argc != 2)
  211.         usage ();
  212.  
  213.     strncpy (group_name, argv[1], BUFSIZ);
  214.  
  215. #ifdef    USE_SYSLOG
  216.     openlog (Prog, LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_AUTH);
  217. #endif    /* USE_SYSLOG */
  218.  
  219.     /*
  220.      * The open routines for the DBM files don't use read-write
  221.      * as the mode, so we have to clue them in.
  222.      */
  223.  
  224. #ifdef    NDBM
  225.     gr_dbm_mode = O_RDWR;
  226. #ifdef    SHADOWGRP
  227.     sg_dbm_mode = O_RDWR;
  228. #endif    /* SHADOWGRP */
  229. #endif    /* NDBM */
  230.  
  231.     /*
  232.      * Start with a quick check to see if the group exists.
  233.      */
  234.  
  235.     if (! getgrnam (group_name)) {
  236.         fprintf (stderr, "%s: group %s does not exist\n",
  237.             Prog, group_name);
  238.         exit (9);
  239.     }
  240.  
  241.     /*
  242.      * Do the hard stuff - open the files, delete the group entries,
  243.      * then close and update the files.
  244.      */
  245.  
  246.     open_files ();
  247.  
  248.     grp_update ();
  249.  
  250.     close_files ();
  251.     exit (0);
  252.     /*NOTREACHED*/
  253. }
  254.