annotate toys/pending/groupdel.c @ 1639:856b544f8fce draft

strncpy(optptr, hname, strlen(hname)) is really just strcpy().
author Rob Landley <rob@landley.net>
date Thu, 01 Jan 2015 16:49:55 -0600
parents 5fac2769a159
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* groupdel.c - delete a group
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2013 Ashwini Kumar <ak.ashwini1981@gmail.com>
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 *
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/groupdel.html
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8 USE_GROUPDEL(NEWTOY(groupdel, "<1>2", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1505
diff changeset
9 USE_GROUPDEL(OLDTOY(delgroup, groupdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 config GROUPDEL
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
12 bool "groupdel"
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
13 default n
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
14 help
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
15 usage: groupdel [USER] GROUP
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
16
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
17 Delete a group or remove a user from a group
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
18 */
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
19
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
20 #define FOR_groupdel
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
21 #include "toys.h"
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23 void groupdel_main(void)
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
24 {
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
25 struct group *grp = xgetgrnam(toys.optargs[toys.optc-1]);
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
26 char *entry = 0;
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
28 // delete user from group
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
29 if (toys.optc == 2) {
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
30 int i, len = 0, found = 0;
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
31 char *s;
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
33 xgetpwnam(*toys.optargs);
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
34 if (grp->gr_mem) for (i = 0; grp->gr_mem[i]; i++) {
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
35 if (!found && !strcmp(*toys.optargs, grp->gr_mem[i])) found++;
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
36 else len += strlen(grp->gr_mem[i]) + 1;
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
37 }
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
38 if (!found)
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
39 error_exit("user '%s' not in group '%s'", *toys.optargs, toys.optargs[1]);
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
40
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
41 entry = s = xmalloc(len);
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
42 for (i = 0; grp->gr_mem[i]; ) {
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
43 if (i) *(s++) = ',';
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
44 s = stpcpy(s, grp->gr_mem[i]);
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
45 }
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
46
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
47 // delete group
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
48 } else {
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
49 struct passwd *pw;
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
50
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
51 for (endpwent(); (pw = getpwent());)
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
52 if (pw->pw_gid == grp->gr_gid) break;
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
53
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
54 if (pw) error_exit("can't remove primary group of user '%s'", pw->pw_name);
1505
3fa0b2931082 Cleanup pass on groupdel.
Rob Landley <rob@landley.net>
parents: 1439
diff changeset
55 if (CFG_TOYBOX_FREE) endpwent();
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
56 }
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
57
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
58 update_password("/etc/group", grp->gr_name, entry);
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
59 update_password("/etc/gshadow", grp->gr_name, entry);
1439
ed28d4d9558d Cleanups on useradd/groupadd/groupdel, and put TOYBOX_UID_SYS and TOYBOX_UID_USR in the top level Config.
Rob Landley <rob@landley.net>
parents: 1159
diff changeset
60 if (CFG_TOYBOX_FREE) free(entry);
1159
f1c924e08e63 Attached is an implementation for groupdel.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
61 }