Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 3 | * Mini chown/chmod/chgrp implementation for busybox |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 by Lineo, inc. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 25 | #include "internal.h" |
| 26 | #define BB_DECLARE_EXTERN |
| 27 | #define bb_need_invalid_option |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 28 | #define bb_need_too_few_args |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 29 | #include "messages.c" |
| 30 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 32 | #include <grp.h> |
| 33 | #include <pwd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 58361a4 | 2000-07-14 06:27:54 +0000 | [diff] [blame^] | 36 | static long uid = -1; |
| 37 | static long gid = -1; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 38 | static int whichApp; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | static char *theMode = NULL; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 40 | |
| 41 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 42 | #define CHGRP_APP 1 |
| 43 | #define CHOWN_APP 2 |
| 44 | #define CHMOD_APP 3 |
| 45 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 46 | static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n" |
| 47 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 48 | "\nChange the group membership of each FILE to GROUP.\n" |
| 49 | "\nOptions:\n\t-R\tChanges files and directories recursively.\n" |
| 50 | #endif |
| 51 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | static const char chown_usage[] = |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 53 | "chown [OPTION]... OWNER[<.|:>[GROUP] FILE...\n" |
| 54 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 55 | "\nChange the owner and/or group of each FILE to OWNER and/or GROUP.\n" |
| 56 | "\nOptions:\n\t-R\tChanges files and directories recursively.\n" |
| 57 | #endif |
| 58 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | static const char chmod_usage[] = |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 60 | "chmod [-R] MODE[,MODE]... FILE...\n" |
| 61 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 62 | "\nEach MODE is one or more of the letters ugoa, one of the symbols +-= and\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | "one or more of the letters rwxst.\n\n" |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 64 | "\nOptions:\n\t-R\tChanges files and directories recursively.\n" |
| 65 | #endif |
| 66 | ; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 67 | |
| 68 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 69 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | switch (whichApp) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 72 | case CHGRP_APP: |
| 73 | case CHOWN_APP: |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 74 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | if (lchown |
| 76 | (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid, |
| 77 | (gid == -1) ? statbuf->st_gid : gid) == 0) |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 78 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | if (chown |
| 80 | (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid, |
| 81 | (gid == -1) ? statbuf->st_gid : gid) == 0) |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 82 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | { |
| 84 | return (TRUE); |
| 85 | } |
| 86 | break; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 87 | case CHMOD_APP: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | /* Parse the specified modes */ |
| 89 | if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 90 | fatalError( "unknown mode: %s\n", theMode); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | } |
| 92 | if (chmod(fileName, statbuf->st_mode) == 0) |
| 93 | return (TRUE); |
| 94 | break; |
| 95 | } |
| 96 | perror(fileName); |
| 97 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 100 | int chmod_chown_chgrp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 101 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | int recursiveFlag = FALSE; |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 103 | char *groupName=NULL; |
| 104 | char *p=NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | const char *appUsage; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 106 | |
Matt Kraai | e714bce | 2000-07-12 00:53:06 +0000 | [diff] [blame] | 107 | whichApp = (strcmp(applet_name, "chown") == 0)? |
| 108 | CHOWN_APP : (strcmp(applet_name, "chmod") == 0)? |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 109 | CHMOD_APP : CHGRP_APP; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 110 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 111 | appUsage = (whichApp == CHOWN_APP)? |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 112 | chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage; |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 113 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 114 | if (argc < 2) |
| 115 | usage(appUsage); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 116 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | /* Parse options */ |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 119 | while (--argc >= 0 && *argv && (**argv == '-')) { |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 120 | while (*++(*argv)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | switch (**argv) { |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 122 | case 'R': |
| 123 | recursiveFlag = TRUE; |
| 124 | break; |
| 125 | default: |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 126 | errorMsg(invalid_option, **argv); |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 127 | usage(appUsage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 129 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 130 | argv++; |
| 131 | } |
| 132 | |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 133 | if (argc == 0 || *argv == NULL) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 134 | errorMsg(too_few_args); |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 135 | usage(appUsage); |
| 136 | } |
| 137 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | if (whichApp == CHMOD_APP) { |
| 139 | theMode = *argv; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 140 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 141 | |
| 142 | /* Find the selected group */ |
| 143 | if (whichApp == CHGRP_APP) { |
| 144 | groupName = *argv; |
| 145 | gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */ |
| 146 | if (groupName == p) |
| 147 | gid = my_getgrnam(groupName); |
| 148 | if (gid == -1) |
| 149 | goto bad_group; |
| 150 | } else { |
| 151 | groupName = strchr(*argv, '.'); |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 152 | if (groupName == NULL) |
| 153 | groupName = strchr(*argv, ':'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 154 | if (groupName) { |
| 155 | *groupName++ = '\0'; |
| 156 | gid = strtoul(groupName, &p, 10); |
| 157 | if (groupName == p) |
| 158 | gid = my_getgrnam(groupName); |
| 159 | if (gid == -1) |
| 160 | goto bad_group; |
| 161 | } else |
| 162 | gid = -1; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* Find the selected user (if appropriate) */ |
| 167 | if (whichApp == CHOWN_APP) { |
| 168 | uid = strtoul(*argv, &p, 10); /* if numeric ... */ |
| 169 | if (*argv == p) |
| 170 | uid = my_getpwnam(*argv); |
| 171 | if (uid == -1) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 172 | fatalError( "unknown user name: %s\n", *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 175 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 176 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 177 | /* Ok, ready to do the deed now */ |
| 178 | if (argc <= 1) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 179 | fatalError(too_few_args); |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 180 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | while (argc-- > 1) { |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 182 | if (recursiveAction (*(++argv), recursiveFlag, FALSE, FALSE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 183 | fileAction, fileAction, NULL) == FALSE) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | exit(FALSE); |
| 185 | } |
| 186 | exit(TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 187 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 188 | bad_group: |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 189 | fatalError( "unknown group name: %s\n", groupName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 190 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | Local Variables: |
| 194 | c-file-style: "linux" |
| 195 | c-basic-offset: 4 |
| 196 | tab-width: 4 |
| 197 | End: |
| 198 | */ |