Home | History | Annotate | Download | only in pending
      1 /* groupdel.c - delete a group
      2  *
      3  * Copyright 2013 Ashwini Kumar <ak.ashwini1981 (at) gmail.com>
      4  * Copyright 2013 Kyungwan Han <asura321 (at) gmail.com>
      5  *
      6  * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/groupdel.html
      7 
      8 USE_GROUPDEL(NEWTOY(groupdel, "<1>2", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
      9 USE_GROUPDEL(OLDTOY(delgroup, groupdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
     10 
     11 config GROUPDEL
     12   bool "groupdel"
     13   default n
     14   help
     15     usage: groupdel [USER] GROUP
     16 
     17     Delete a group or remove a user from a group
     18 */
     19 
     20 #define FOR_groupdel
     21 #include "toys.h"
     22 
     23 void groupdel_main(void)
     24 {
     25   struct group *grp = xgetgrnam(toys.optargs[toys.optc-1]);
     26   char *entry = 0;
     27 
     28   // delete user from group
     29   if (toys.optc == 2) {
     30     int i, len = 0, found = 0;
     31     char *s;
     32 
     33     xgetpwnam(*toys.optargs);
     34     if (grp->gr_mem) for (i = 0; grp->gr_mem[i]; i++) {
     35       if (!found && !strcmp(*toys.optargs, grp->gr_mem[i])) found++;
     36       else len += strlen(grp->gr_mem[i]) + 1;
     37     }
     38     if (!found)
     39       error_exit("user '%s' not in group '%s'", *toys.optargs, toys.optargs[1]);
     40 
     41     entry = s = xmalloc(len);
     42     for (i = 0; grp->gr_mem[i]; ) {
     43       if (i) *(s++) = ',';
     44       s = stpcpy(s, grp->gr_mem[i]);
     45     }
     46 
     47   // delete group
     48   } else {
     49     struct passwd *pw;
     50 
     51     for (endpwent(); (pw = getpwent());)
     52       if (pw->pw_gid == grp->gr_gid) break;
     53 
     54     if (pw) error_exit("can't remove primary group of user '%s'", pw->pw_name);
     55     if (CFG_TOYBOX_FREE) endpwent();
     56   }
     57 
     58   update_password("/etc/group", grp->gr_name, entry);
     59   update_password("/etc/gshadow", grp->gr_name, entry);
     60   if (CFG_TOYBOX_FREE) free(entry);
     61 }
     62