aboutsummaryrefslogtreecommitdiff
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
parente5642119ee566520a098027746808dd6b9226d99 (diff)
downloadbusybox-w32-74f270a3177a43b29d98b6c5b6a2c5338be8b98f.tar.gz
busybox-w32-74f270a3177a43b29d98b6c5b6a2c5338be8b98f.tar.bz2
busybox-w32-74f270a3177a43b29d98b6c5b6a2c5338be8b98f.zip
decouple delgroup and deluser
-rw-r--r--loginutils/Makefile.in3
-rw-r--r--loginutils/delgroup.c60
-rw-r--r--loginutils/delline.c113
-rw-r--r--loginutils/deluser.c127
4 files changed, 183 insertions, 120 deletions
diff --git a/loginutils/Makefile.in b/loginutils/Makefile.in
index 1c5680aa5..5027e8079 100644
--- a/loginutils/Makefile.in
+++ b/loginutils/Makefile.in
@@ -25,13 +25,14 @@ endif
25LOGINUTILS-y:= 25LOGINUTILS-y:=
26LOGINUTILS-$(CONFIG_ADDGROUP) += addgroup.o 26LOGINUTILS-$(CONFIG_ADDGROUP) += addgroup.o
27LOGINUTILS-$(CONFIG_ADDUSER) += adduser.o 27LOGINUTILS-$(CONFIG_ADDUSER) += adduser.o
28LOGINUTILS-$(CONFIG_DELUSER) += deluser.o
29LOGINUTILS-$(CONFIG_GETTY) += getty.o 28LOGINUTILS-$(CONFIG_GETTY) += getty.o
30LOGINUTILS-$(CONFIG_LOGIN) += login.o 29LOGINUTILS-$(CONFIG_LOGIN) += login.o
31LOGINUTILS-$(CONFIG_PASSWD) += passwd.o 30LOGINUTILS-$(CONFIG_PASSWD) += passwd.o
32LOGINUTILS-$(CONFIG_SU) += su.o 31LOGINUTILS-$(CONFIG_SU) += su.o
33LOGINUTILS-$(CONFIG_SULOGIN) += sulogin.o 32LOGINUTILS-$(CONFIG_SULOGIN) += sulogin.o
34LOGINUTILS-$(CONFIG_VLOCK) += vlock.o 33LOGINUTILS-$(CONFIG_VLOCK) += vlock.o
34LOGINUTILS-$(CONFIG_DELUSER) += deluser.o
35LOGINUTILS-$(CONFIG_DELGROUP) += delgroup.o
35 36
36libraries-y+=$(LOGINUTILS_DIR)$(LOGINUTILS_AR) 37libraries-y+=$(LOGINUTILS_DIR)$(LOGINUTILS_AR)
37 38
diff --git a/loginutils/delgroup.c b/loginutils/delgroup.c
new file mode 100644
index 000000000..f74f64fb7
--- /dev/null
+++ b/loginutils/delgroup.c
@@ -0,0 +1,60 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * deluser (remove lusers from the system ;) for TinyLogin
4 *
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <sys/stat.h>
25#include <unistd.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include "busybox.h"
30
31
32#if ! defined CONFIG_DELUSER
33#include "delline.c"
34#endif
35
36int delgroup_main(int argc, char **argv)
37{
38 /* int successful; */
39 int failure;
40
41 if (argc != 2) {
42 bb_show_usage();
43 } else {
44
45 failure = del_line_matching(argv[1], bb_path_group_file);
46#ifdef CONFIG_FEATURE_SHADOWPASSWDS
47 if (access(bb_path_gshadow_file, W_OK) == 0) {
48 /* EDR the |= works if the error is not 0, so he had it wrong */
49 failure |= del_line_matching(argv[1], bb_path_gshadow_file);
50 }
51#endif
52 if (failure) {
53 bb_error_msg_and_die("%s: Group could not be removed\n", argv[1]);
54 }
55
56 }
57 return (EXIT_SUCCESS);
58}
59
60/* $Id: delgroup.c,v 1.1 2003/07/14 20:20:45 andersen Exp $ */
diff --git a/loginutils/delline.c b/loginutils/delline.c
new file mode 100644
index 000000000..ed29ca0d2
--- /dev/null
+++ b/loginutils/delline.c
@@ -0,0 +1,113 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * deluser (remove lusers from the system ;) for TinyLogin
4 *
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <sys/stat.h>
25#include <unistd.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include "busybox.h"
30
31
32
33/* where to start and stop deletion */
34typedef struct {
35 size_t start;
36 size_t stop;
37} Bounds;
38
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
113/* $Id: delline.c,v 1.1 2003/07/14 20:20:45 andersen Exp $ */
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 $ */