diff options
author | Rob Landley <rob@landley.net> | 2006-04-10 18:03:17 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-04-10 18:03:17 +0000 |
commit | 81c40b39cbae084777c93fe5303c4ba9af8137c6 (patch) | |
tree | aa263624d56f211217fa9d62d6237b5f79f717fb /loginutils/deluser.c | |
parent | ca7166fe9d723aeabf886520749dcfeb2221d8cc (diff) | |
download | busybox-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.
Diffstat (limited to 'loginutils/deluser.c')
-rw-r--r-- | loginutils/deluser.c | 106 |
1 files changed, 69 insertions, 37 deletions
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 */ | ||
21 | typedef 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. */ | ||
29 | static 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 | ||
34 | static 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 | ||
36 | int 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 */ | ||
54 | static 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 | |||
83 | int 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 | } |