aboutsummaryrefslogtreecommitdiff
path: root/loginutils/deluser.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-07-14 20:20:45 +0000
committerEric Andersen <andersen@codepoet.org>2003-07-14 20:20:45 +0000
commit74f270a3177a43b29d98b6c5b6a2c5338be8b98f (patch)
treef07a9933508907612bc6d4af818447ff099a7c63 /loginutils/deluser.c
parente5642119ee566520a098027746808dd6b9226d99 (diff)
downloadbusybox-w32-74f270a3177a43b29d98b6c5b6a2c5338be8b98f.tar.gz
busybox-w32-74f270a3177a43b29d98b6c5b6a2c5338be8b98f.tar.bz2
busybox-w32-74f270a3177a43b29d98b6c5b6a2c5338be8b98f.zip
decouple delgroup and deluser
Diffstat (limited to 'loginutils/deluser.c')
-rw-r--r--loginutils/deluser.c127
1 files changed, 8 insertions, 119 deletions
diff --git a/loginutils/deluser.c b/loginutils/deluser.c
index ad62d41e0..1cd2b01e3 100644
--- a/loginutils/deluser.c
+++ b/loginutils/deluser.c
@@ -29,113 +29,10 @@
29#include "busybox.h" 29#include "busybox.h"
30 30
31 31
32#include "delline.c"
32 33
33/* where to start and stop deletion */ 34static const char deluser_format[]="%s: User could not be removed from %s";
34typedef struct {
35 size_t start;
36 size_t stop;
37} Bounds;
38 35
39/* An interesting side-effect of boundary()'s
40 * implementation is that the first user (typically root)
41 * cannot be removed. Let's call it a feature. */
42static inline Bounds boundary(const char *buffer, const char *login)
43{
44 char needle[256];
45 char *start;
46 char *stop;
47 Bounds b;
48
49 snprintf(needle, 256, "\n%s:", login);
50 needle[255] = 0;
51 start = strstr(buffer, needle);
52 if (!start) {
53 b.start = 0;
54 b.stop = 0;
55 return b;
56 }
57 start++;
58
59 stop = index(start, '\n'); /* index is a BSD-ism */
60 b.start = start - buffer;
61 b.stop = stop - buffer;
62 return b;
63}
64
65/* grep -v ^login (except it only deletes the first match) */
66/* ...in fact, I think I'm going to simplify this later */
67static int del_line_matching(const char *login, const char *filename)
68{
69 char *buffer;
70 FILE *passwd;
71 size_t len;
72 Bounds b;
73 struct stat statbuf;
74
75 /* load into buffer */
76 passwd = fopen(filename, "r");
77 if (!passwd) {
78 return 1;
79 }
80 stat(filename, &statbuf);
81 len = statbuf.st_size;
82 buffer = (char *) malloc(len * sizeof(char));
83
84 if (!buffer) {
85 fclose(passwd);
86 return 1;
87 }
88 fread(buffer, len, sizeof(char), passwd);
89
90 fclose(passwd);
91
92 /* find the user to remove */
93 b = boundary(buffer, login);
94 if (b.stop == 0) {
95 free(buffer);
96 return 1;
97 }
98
99 /* write the file w/o the user */
100 passwd = fopen(filename, "w");
101 if (!passwd) {
102 return 1;
103 }
104 fwrite(buffer, (b.start - 1), sizeof(char), passwd);
105 fwrite(&buffer[b.stop], (len - b.stop), sizeof(char), passwd);
106
107 fclose(passwd);
108
109 return 0;
110}
111
112/* ________________________________________________________________________ */
113int delgroup_main(int argc, char **argv)
114{
115 /* int successful; */
116 int failure;
117
118 if (argc != 2) {
119 bb_show_usage();
120 } else {
121
122 failure = del_line_matching(argv[1], bb_path_group_file);
123#ifdef CONFIG_FEATURE_SHADOWPASSWDS
124 if (access(bb_path_gshadow_file, W_OK) == 0) {
125 /* EDR the |= works if the error is not 0, so he had it wrong */
126 failure |= del_line_matching(argv[1], bb_path_gshadow_file);
127 }
128#endif /* CONFIG_FEATURE_SHADOWPASSWDS */
129 /* if (!successful) { */
130 if (failure) {
131 bb_error_msg_and_die("%s: Group could not be removed\n", argv[1]);
132 }
133
134 }
135 return (EXIT_SUCCESS);
136}
137
138/* ________________________________________________________________________ */
139int deluser_main(int argc, char **argv) 36int deluser_main(int argc, char **argv)
140{ 37{
141 /* int successful; */ 38 /* int successful; */
@@ -146,34 +43,26 @@ int deluser_main(int argc, char **argv)
146 } else { 43 } else {
147 44
148 failure = del_line_matching(argv[1], bb_path_passwd_file); 45 failure = del_line_matching(argv[1], bb_path_passwd_file);
149 /* if (!successful) { */
150 if (failure) { 46 if (failure) {
151 bb_error_msg_and_die("%s: User could not be removed from %s\n", 47 bb_error_msg_and_die(deluser_format, argv[1], bb_path_passwd_file);
152 argv[1], bb_path_passwd_file);
153 } 48 }
154#ifdef CONFIG_FEATURE_SHADOWPASSWDS 49#ifdef CONFIG_FEATURE_SHADOWPASSWDS
155 failure = del_line_matching(argv[1], bb_path_shadow_file); 50 failure = del_line_matching(argv[1], bb_path_shadow_file);
156 /* if (!successful) { */
157 if (failure) { 51 if (failure) {
158 bb_error_msg_and_die("%s: User could not be removed from %s\n", 52 bb_error_msg_and_die(deluser_format, argv[1], bb_path_shadow_file);
159 argv[1], bb_path_shadow_file);
160 } 53 }
161 failure = del_line_matching(argv[1], bb_path_gshadow_file); 54 failure = del_line_matching(argv[1], bb_path_gshadow_file);
162 /* if (!successful) { */
163 if (failure) { 55 if (failure) {
164 bb_error_msg_and_die("%s: User could not be removed from %s\n", 56 bb_error_msg_and_die(deluser_format, argv[1], bb_path_gshadow_file);
165 argv[1], bb_path_gshadow_file);
166 } 57 }
167#endif /* CONFIG_FEATURE_SHADOWPASSWDS */ 58#endif
168 failure = del_line_matching(argv[1], bb_path_group_file); 59 failure = del_line_matching(argv[1], bb_path_group_file);
169 /* if (!successful) { */
170 if (failure) { 60 if (failure) {
171 bb_error_msg_and_die("%s: User could not be removed from %s\n", 61 bb_error_msg_and_die(deluser_format, argv[1], bb_path_group_file);
172 argv[1], bb_path_group_file);
173 } 62 }
174 63
175 } 64 }
176 return (EXIT_SUCCESS); 65 return (EXIT_SUCCESS);
177} 66}
178 67
179/* $Id: deluser.c,v 1.3 2003/03/19 09:12:20 mjn3 Exp $ */ 68/* $Id: deluser.c,v 1.4 2003/07/14 20:20:45 andersen Exp $ */