aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-04-10 18:03:17 +0000
committerRob Landley <rob@landley.net>2006-04-10 18:03:17 +0000
commit81c40b39cbae084777c93fe5303c4ba9af8137c6 (patch)
treeaa263624d56f211217fa9d62d6237b5f79f717fb
parentca7166fe9d723aeabf886520749dcfeb2221d8cc (diff)
downloadbusybox-w32-81c40b39cbae084777c93fe5303c4ba9af8137c6.tar.gz
busybox-w32-81c40b39cbae084777c93fe5303c4ba9af8137c6.tar.bz2
busybox-w32-81c40b39cbae084777c93fe5303c4ba9af8137c6.zip
Patch from Tito to unify deluser and delgroup, and generally shrink code.
-rw-r--r--include/applets.h8
-rw-r--r--loginutils/Makefile.in4
-rw-r--r--loginutils/delgroup.c62
-rw-r--r--loginutils/delline.c113
-rw-r--r--loginutils/deluser.c106
5 files changed, 76 insertions, 217 deletions
diff --git a/include/applets.h b/include/applets.h
index f0c6f7a0e..d3b75d03b 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -82,7 +82,7 @@ USE_DATE(APPLET(date, date_main, _BB_DIR_BIN, _BB_SUID_NEVER))
82USE_DC(APPLET(dc, dc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) 82USE_DC(APPLET(dc, dc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
83USE_DD(APPLET(dd, dd_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 83USE_DD(APPLET(dd, dd_main, _BB_DIR_BIN, _BB_SUID_NEVER))
84USE_DEALLOCVT(APPLET(deallocvt, deallocvt_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) 84USE_DEALLOCVT(APPLET(deallocvt, deallocvt_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
85USE_DELGROUP(APPLET(delgroup, delgroup_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 85USE_DELGROUP(APPLET(delgroup, deluser_main, _BB_DIR_BIN, _BB_SUID_NEVER))
86USE_DELUSER(APPLET(deluser, deluser_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 86USE_DELUSER(APPLET(deluser, deluser_main, _BB_DIR_BIN, _BB_SUID_NEVER))
87USE_DEVFSD(APPLET(devfsd, devfsd_main, _BB_DIR_SBIN, _BB_SUID_NEVER)) 87USE_DEVFSD(APPLET(devfsd, devfsd_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
88USE_DF(APPLET(df, df_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 88USE_DF(APPLET(df, df_main, _BB_DIR_BIN, _BB_SUID_NEVER))
@@ -95,7 +95,7 @@ USE_DPKG(APPLET(dpkg, dpkg_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
95USE_DPKG_DEB(APPLET_ODDNAME(dpkg-deb, dpkg_deb_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER, dpkg_deb)) 95USE_DPKG_DEB(APPLET_ODDNAME(dpkg-deb, dpkg_deb_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER, dpkg_deb))
96USE_DU(APPLET(du, du_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) 96USE_DU(APPLET(du, du_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
97USE_DUMPKMAP(APPLET(dumpkmap, dumpkmap_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 97USE_DUMPKMAP(APPLET(dumpkmap, dumpkmap_main, _BB_DIR_BIN, _BB_SUID_NEVER))
98USE_DUMPLEASES(APPLET(dumpleases, dumpleases_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) 98//USE_DUMPLEASES(APPLET(dumpleases, dumpleases_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
99USE_E2FSCK(APPLET(e2fsck, e2fsck_main, _BB_DIR_SBIN, _BB_SUID_NEVER)) 99USE_E2FSCK(APPLET(e2fsck, e2fsck_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
100USE_E2LABEL(APPLET_NOUSAGE(e2label, tune2fs_main, _BB_DIR_SBIN, _BB_SUID_NEVER)) 100USE_E2LABEL(APPLET_NOUSAGE(e2label, tune2fs_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
101USE_ECHO(APPLET(echo, echo_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 101USE_ECHO(APPLET(echo, echo_main, _BB_DIR_BIN, _BB_SUID_NEVER))
@@ -273,8 +273,8 @@ USE_TRACEROUTE(APPLET(traceroute, traceroute_main, _BB_DIR_USR_BIN, _BB_SUID_MAY
273USE_TRUE(APPLET(true, true_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 273USE_TRUE(APPLET(true, true_main, _BB_DIR_BIN, _BB_SUID_NEVER))
274USE_TTY(APPLET(tty, tty_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) 274USE_TTY(APPLET(tty, tty_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
275USE_TUNE2FS(APPLET(tune2fs, tune2fs_main, _BB_DIR_SBIN, _BB_SUID_NEVER)) 275USE_TUNE2FS(APPLET(tune2fs, tune2fs_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
276USE_UDHCPC(APPLET(udhcpc, udhcpc_main, _BB_DIR_SBIN, _BB_SUID_NEVER)) 276//USE_UDHCPC(APPLET(udhcpc, udhcpc_main, _BB_DIR_SBIN, _BB_SUID_NEVER))
277USE_UDHCPD(APPLET(udhcpd, udhcpd_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)) 277//USE_UDHCPD(APPLET(udhcpd, udhcpd_main, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
278USE_UMOUNT(APPLET(umount, umount_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 278USE_UMOUNT(APPLET(umount, umount_main, _BB_DIR_BIN, _BB_SUID_NEVER))
279USE_UNAME(APPLET(uname, uname_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 279USE_UNAME(APPLET(uname, uname_main, _BB_DIR_BIN, _BB_SUID_NEVER))
280USE_UNCOMPRESS(APPLET(uncompress, uncompress_main, _BB_DIR_BIN, _BB_SUID_NEVER)) 280USE_UNCOMPRESS(APPLET(uncompress, uncompress_main, _BB_DIR_BIN, _BB_SUID_NEVER))
diff --git a/loginutils/Makefile.in b/loginutils/Makefile.in
index c11bbfc03..0063762ab 100644
--- a/loginutils/Makefile.in
+++ b/loginutils/Makefile.in
@@ -20,7 +20,9 @@ LOGINUTILS-$(CONFIG_SU) += su.o
20LOGINUTILS-$(CONFIG_SULOGIN) += sulogin.o 20LOGINUTILS-$(CONFIG_SULOGIN) += sulogin.o
21LOGINUTILS-$(CONFIG_VLOCK) += vlock.o 21LOGINUTILS-$(CONFIG_VLOCK) += vlock.o
22LOGINUTILS-$(CONFIG_DELUSER) += deluser.o 22LOGINUTILS-$(CONFIG_DELUSER) += deluser.o
23LOGINUTILS-$(CONFIG_DELGROUP) += delgroup.o 23LOGINUTILS-$(CONFIG_DELGROUP) += deluser.o
24
25LOGINUTILS-y:=$(sort $(LOGINUTILS-y))
24 26
25ifneq ($(strip $(LOGINUTILS-y)),) 27ifneq ($(strip $(LOGINUTILS-y)),)
26libraries-y+=$(LOGINUTILS_DIR)$(LOGINUTILS_AR) 28libraries-y+=$(LOGINUTILS_DIR)$(LOGINUTILS_AR)
diff --git a/loginutils/delgroup.c b/loginutils/delgroup.c
deleted file mode 100644
index 91edf2989..000000000
--- a/loginutils/delgroup.c
+++ /dev/null
@@ -1,62 +0,0 @@
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#else
35extern int del_line_matching(const char *login, const char *filename);
36#endif
37
38int delgroup_main(int argc, char **argv)
39{
40 /* int successful; */
41 int failure;
42
43 if (argc != 2) {
44 bb_show_usage();
45 } else {
46
47 failure = del_line_matching(argv[1], bb_path_group_file);
48#ifdef CONFIG_FEATURE_SHADOWPASSWDS
49 if (access(bb_path_gshadow_file, W_OK) == 0) {
50 /* EDR the |= works if the error is not 0, so he had it wrong */
51 failure |= del_line_matching(argv[1], bb_path_gshadow_file);
52 }
53#endif
54 if (failure) {
55 bb_error_msg_and_die("%s: Group could not be removed\n", argv[1]);
56 }
57
58 }
59 return (EXIT_SUCCESS);
60}
61
62/* $Id: delgroup.c,v 1.2 2003/07/14 21:50:51 andersen Exp $ */
diff --git a/loginutils/delline.c b/loginutils/delline.c
deleted file mode 100644
index 8d534c861..000000000
--- a/loginutils/delline.c
+++ /dev/null
@@ -1,113 +0,0 @@
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 */
67int 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.2 2003/07/14 21:50:51 andersen Exp $ */
diff --git a/loginutils/deluser.c b/loginutils/deluser.c
index 1cd2b01e3..b647537d9 100644
--- a/loginutils/deluser.c
+++ b/loginutils/deluser.c
@@ -4,20 +4,9 @@
4 * 4 *
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu 5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> 6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 * Unified with delgroup by Tito Ragusa <farmatito@tiscali.it>
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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 * 10 *
22 */ 11 */
23 12
@@ -28,39 +17,82 @@
28#include <string.h> 17#include <string.h>
29#include "busybox.h" 18#include "busybox.h"
30 19
20/* where to start and stop deletion */
21typedef struct {
22 size_t start;
23 size_t stop;
24} Bounds;
31 25
32#include "delline.c" 26/* An interesting side-effect of boundary()'s
27 * implementation is that the first user (typically root)
28 * cannot be removed. Let's call it a feature. */
29static inline Bounds boundary(const char *buffer, const char *login)
30{
31 char needle[256];
32 char *start;
33 char *stop;
34 Bounds b;
33 35
34static const char deluser_format[]="%s: User could not be removed from %s"; 36 snprintf(needle, 256, "\n%s:", login);
37 needle[255] = 0;
38 start = strstr(buffer, needle);
39 if (!start) {
40 b.start = 0;
41 b.stop = 0;
42 return b;
43 }
44 start++;
35 45
36int deluser_main(int argc, char **argv) 46 stop = index(start, '\n'); /* index is a BSD-ism */
47 b.start = start - buffer;
48 b.stop = stop - buffer;
49 return b;
50}
51
52/* grep -v ^login (except it only deletes the first match) */
53/* ...in fact, I think I'm going to simplify this later */
54static void del_line_matching(const char *login, const char *filename)
37{ 55{
38 /* int successful; */ 56 char *buffer;
39 int failure; 57 FILE *passwd;
58 Bounds b;
59 struct stat statbuf;
60
40 61
62 if ((passwd = bb_wfopen(filename, "r"))) {
63 xstat(filename, &statbuf);
64 buffer = (char *) xmalloc(statbuf.st_size * sizeof(char));
65 fread(buffer, statbuf.st_size, sizeof(char), passwd);
66 fclose(passwd);
67 /* find the user to remove */
68 b = boundary(buffer, login);
69 if (b.stop != 0) {
70 /* write the file w/o the user */
71 if ((passwd = bb_wfopen(filename, "w"))) {
72 fwrite(buffer, (b.start - 1), sizeof(char), passwd);
73 fwrite(&buffer[b.stop], (statbuf.st_size - b.stop), sizeof(char), passwd);
74 fclose(passwd);
75 }
76 } else {
77 bb_error_msg("Can't find '%s' in '%s'", login, filename);
78 }
79 free(buffer);
80 }
81}
82
83int deluser_main(int argc, char **argv)
84{
41 if (argc != 2) { 85 if (argc != 2) {
42 bb_show_usage(); 86 bb_show_usage();
43 } else { 87 } else {
44 88 if (ENABLE_DELUSER && bb_applet_name[3] == 'u') {
45 failure = del_line_matching(argv[1], bb_path_passwd_file); 89 del_line_matching(argv[1], bb_path_passwd_file);
46 if (failure) { 90 if (ENABLE_FEATURE_SHADOWPASSWDS)
47 bb_error_msg_and_die(deluser_format, argv[1], bb_path_passwd_file); 91 del_line_matching(argv[1], bb_path_shadow_file);
48 } 92 }
49#ifdef CONFIG_FEATURE_SHADOWPASSWDS 93 del_line_matching(argv[1], bb_path_group_file);
50 failure = del_line_matching(argv[1], bb_path_shadow_file); 94 if (ENABLE_FEATURE_SHADOWPASSWDS)
51 if (failure) { 95 del_line_matching(argv[1], bb_path_gshadow_file);
52 bb_error_msg_and_die(deluser_format, argv[1], bb_path_shadow_file);
53 }
54 failure = del_line_matching(argv[1], bb_path_gshadow_file);
55 if (failure) {
56 bb_error_msg_and_die(deluser_format, argv[1], bb_path_gshadow_file);
57 }
58#endif
59 failure = del_line_matching(argv[1], bb_path_group_file);
60 if (failure) {
61 bb_error_msg_and_die(deluser_format, argv[1], bb_path_group_file);
62 }
63
64 } 96 }
65 return (EXIT_SUCCESS); 97 return (EXIT_SUCCESS);
66} 98}