annotate toys/pending/userdel.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents 5fac2769a159
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* userdel.c - delete a user
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Ashwini Kumar <ak.ashwini1981@gmail.com>
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 *
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/userdel.html
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7 USE_USERDEL(NEWTOY(userdel, "<1>1r", 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: 1413
diff changeset
8 USE_USERDEL(OLDTOY(deluser, userdel, TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 config USERDEL
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 bool "userdel"
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
12 default n
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
13 help
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
14 usage: userdel [-r] USER
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
15 usage: deluser [-r] USER
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
16
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
17 Options:
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
18 -r remove home directory
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
19 Delete USER from the SYSTEM
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
20 */
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
21
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22 #define FOR_userdel
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23 #include "toys.h"
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
24
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
25 static void update_groupfiles(char *filename, char* username)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
26 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27 char *filenamesfx = NULL, *sfx = NULL, *line = NULL;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
28 FILE *exfp, *newfp;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29 int ulen = strlen(username);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
30 struct flock lock;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
31
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32 filenamesfx = xmprintf("%s+", filename);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
33 sfx = strchr(filenamesfx, '+');
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
34 exfp = xfopen(filename, "r+");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
35
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
36 *sfx = '-';
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
37 unlink(filenamesfx);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
38 if (link(filename, filenamesfx)) error_msg("Can't create backup file");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
39
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
40 *sfx = '+';
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
41 lock.l_type = F_WRLCK;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
42 lock.l_whence = SEEK_SET;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
43 lock.l_start = lock.l_len = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
44
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
45 if (fcntl(fileno(exfp), F_SETLK, &lock) < 0)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
46 perror_msg("Couldn't lock file %s",filename);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
47
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
48 lock.l_type = F_UNLCK; //unlocking at a later stage
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
49
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
50 newfp = xfopen(filenamesfx, "w+");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
51
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
52 while ((line = get_line(fileno(exfp))) != NULL){
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
53 sprintf(toybuf, "%s:",username);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
54 if (!strncmp(line, toybuf, ulen+1)) goto LOOP;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
55 else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
56 char *n, *p = strrchr(line, ':');
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
57
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
58 if (p && *++p && (n = strstr(p, username))) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
59 do {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
60 if (n[ulen] == ',') {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
61 *n = '\0';
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
62 n += ulen + 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
63 fprintf(newfp, "%s%s\n", line, n);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
64 break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
65 } else if (!n[ulen]) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
66 if (n[-1] == ',') n[-1] = *n = '\0';
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
67 if (n[-1] == ':') *n = '\0';
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
68 fprintf(newfp, "%s%s\n", line, n);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
69 break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
70 } else n += ulen;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
71 } while (*n && (n=strstr(n, username)));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
72 if (!n) fprintf(newfp, "%s\n", line);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
73 } else fprintf(newfp, "%s\n", line);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
74 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
75 LOOP:
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
76 free(line);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
77 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
78 fcntl(fileno(exfp), F_SETLK, &lock);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
79 fclose(exfp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
80 errno = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
81 fflush(newfp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
82 fsync(fileno(newfp));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
83 fclose(newfp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
84 rename(filenamesfx, filename);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
85 if (errno){
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
86 perror_msg("File Writing/Saving failed: ");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
87 unlink(filenamesfx);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
88 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
89 free(filenamesfx);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
90 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
91
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
92 void userdel_main(void)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
93 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
94 struct passwd *pwd = NULL;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
95
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
96 pwd = xgetpwnam(*toys.optargs);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
97 update_password("/etc/passwd", pwd->pw_name, NULL);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
98 update_password("/etc/shadow", pwd->pw_name, NULL);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
99
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
100 // delete the group named USER, and remove user from group.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
101 // could update_password() be used for this?
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
102 // not a good idea, as update_passwd() updates one entry at a time
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
103 // in this case it will be modifying the files as many times the
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
104 // USER appears in group database files. So the customized version
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
105 // of update_passwd() is here.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
106 update_groupfiles("/etc/group", *toys.optargs);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
107 update_groupfiles("/etc/gshadow", *toys.optargs);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
108
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
109 if (toys.optflags & FLAG_r) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
110 char *arg[] = {"rm", "-fr", pwd->pw_dir, NULL, NULL};
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
111
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
112 sprintf(toybuf, "/var/spool/mail/%s",pwd->pw_name);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
113 arg[3] = toybuf;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
114 xexec(arg);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
115 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
116 }