10f66f451Sopenharmony_ci/* groupdel.c - delete a group
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2013 Ashwini Kumar <ak.ashwini1981@gmail.com>
40f66f451Sopenharmony_ci * Copyright 2013 Kyungwan Han <asura321@gmail.com>
50f66f451Sopenharmony_ci *
60f66f451Sopenharmony_ci * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/groupdel.html
70f66f451Sopenharmony_ci
80f66f451Sopenharmony_ciUSE_GROUPDEL(NEWTOY(groupdel, "<1>2", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
90f66f451Sopenharmony_ciUSE_GROUPDEL(OLDTOY(delgroup, groupdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
100f66f451Sopenharmony_ci
110f66f451Sopenharmony_ciconfig GROUPDEL
120f66f451Sopenharmony_ci  bool "groupdel"
130f66f451Sopenharmony_ci  default n
140f66f451Sopenharmony_ci  help
150f66f451Sopenharmony_ci    usage: groupdel [USER] GROUP
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci    Delete a group or remove a user from a group
180f66f451Sopenharmony_ci*/
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci#define FOR_groupdel
210f66f451Sopenharmony_ci#include "toys.h"
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_civoid groupdel_main(void)
240f66f451Sopenharmony_ci{
250f66f451Sopenharmony_ci  struct group *grp = xgetgrnam(toys.optargs[toys.optc-1]);
260f66f451Sopenharmony_ci  char *entry = 0;
270f66f451Sopenharmony_ci
280f66f451Sopenharmony_ci  // delete user from group
290f66f451Sopenharmony_ci  if (toys.optc == 2) {
300f66f451Sopenharmony_ci    int i, len = 0, found = 0;
310f66f451Sopenharmony_ci    char *s;
320f66f451Sopenharmony_ci
330f66f451Sopenharmony_ci    xgetpwnam(*toys.optargs);
340f66f451Sopenharmony_ci    if (grp->gr_mem) for (i = 0; grp->gr_mem[i]; i++) {
350f66f451Sopenharmony_ci      if (!found && !strcmp(*toys.optargs, grp->gr_mem[i])) found++;
360f66f451Sopenharmony_ci      else len += strlen(grp->gr_mem[i]) + 1;
370f66f451Sopenharmony_ci    }
380f66f451Sopenharmony_ci    if (!found)
390f66f451Sopenharmony_ci      error_exit("user '%s' not in group '%s'", *toys.optargs, toys.optargs[1]);
400f66f451Sopenharmony_ci
410f66f451Sopenharmony_ci    entry = s = xmalloc(len);
420f66f451Sopenharmony_ci    for (i = 0; grp->gr_mem[i]; ) {
430f66f451Sopenharmony_ci      if (i) *(s++) = ',';
440f66f451Sopenharmony_ci      s = stpcpy(s, grp->gr_mem[i]);
450f66f451Sopenharmony_ci    }
460f66f451Sopenharmony_ci
470f66f451Sopenharmony_ci  // delete group
480f66f451Sopenharmony_ci  } else {
490f66f451Sopenharmony_ci    struct passwd *pw;
500f66f451Sopenharmony_ci
510f66f451Sopenharmony_ci    for (endpwent(); (pw = getpwent());)
520f66f451Sopenharmony_ci      if (pw->pw_gid == grp->gr_gid) break;
530f66f451Sopenharmony_ci
540f66f451Sopenharmony_ci    if (pw) error_exit("can't remove primary group of user '%s'", pw->pw_name);
550f66f451Sopenharmony_ci    if (CFG_TOYBOX_FREE) endpwent();
560f66f451Sopenharmony_ci  }
570f66f451Sopenharmony_ci
580f66f451Sopenharmony_ci  update_password("/etc/group", grp->gr_name, entry);
590f66f451Sopenharmony_ci  update_password("/etc/gshadow", grp->gr_name, entry);
600f66f451Sopenharmony_ci  if (CFG_TOYBOX_FREE) free(entry);
610f66f451Sopenharmony_ci}
62