diff options
| author | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-04-10 18:03:17 +0000 |
|---|---|---|
| committer | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-04-10 18:03:17 +0000 |
| commit | ff4fff181d4dc7ab2b34e9f1db7b108f2dcc5564 (patch) | |
| tree | aa263624d56f211217fa9d62d6237b5f79f717fb /loginutils | |
| parent | 10e53bbcbe775c7f09f8fbecadfca82ee014052e (diff) | |
| download | busybox-w32-ff4fff181d4dc7ab2b34e9f1db7b108f2dcc5564.tar.gz busybox-w32-ff4fff181d4dc7ab2b34e9f1db7b108f2dcc5564.tar.bz2 busybox-w32-ff4fff181d4dc7ab2b34e9f1db7b108f2dcc5564.zip | |
Patch from Tito to unify deluser and delgroup, and generally shrink code.
git-svn-id: svn://busybox.net/trunk/busybox@14793 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'loginutils')
| -rw-r--r-- | loginutils/Makefile.in | 4 | ||||
| -rw-r--r-- | loginutils/delgroup.c | 62 | ||||
| -rw-r--r-- | loginutils/delline.c | 113 | ||||
| -rw-r--r-- | loginutils/deluser.c | 106 |
4 files changed, 72 insertions, 213 deletions
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 | |||
| 20 | LOGINUTILS-$(CONFIG_SULOGIN) += sulogin.o | 20 | LOGINUTILS-$(CONFIG_SULOGIN) += sulogin.o |
| 21 | LOGINUTILS-$(CONFIG_VLOCK) += vlock.o | 21 | LOGINUTILS-$(CONFIG_VLOCK) += vlock.o |
| 22 | LOGINUTILS-$(CONFIG_DELUSER) += deluser.o | 22 | LOGINUTILS-$(CONFIG_DELUSER) += deluser.o |
| 23 | LOGINUTILS-$(CONFIG_DELGROUP) += delgroup.o | 23 | LOGINUTILS-$(CONFIG_DELGROUP) += deluser.o |
| 24 | |||
| 25 | LOGINUTILS-y:=$(sort $(LOGINUTILS-y)) | ||
| 24 | 26 | ||
| 25 | ifneq ($(strip $(LOGINUTILS-y)),) | 27 | ifneq ($(strip $(LOGINUTILS-y)),) |
| 26 | libraries-y+=$(LOGINUTILS_DIR)$(LOGINUTILS_AR) | 28 | libraries-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 | ||
| 35 | extern int del_line_matching(const char *login, const char *filename); | ||
| 36 | #endif | ||
| 37 | |||
| 38 | int 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 */ | ||
| 34 | typedef 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. */ | ||
| 42 | static 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 */ | ||
| 67 | 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.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 */ | ||
| 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 | } |
