Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 2 | * Mini chown/chmod/chgrp implementation for busybox |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 23 | #include <grp.h> |
| 24 | #include <pwd.h> |
| 25 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 28 | static uid_t uid=-1; |
| 29 | static gid_t gid=-1; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 30 | static int whichApp; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 31 | static char* invocationName=NULL; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 32 | static mode_t mode=0644; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 33 | |
| 34 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 35 | #define CHGRP_APP 1 |
| 36 | #define CHOWN_APP 2 |
| 37 | #define CHMOD_APP 3 |
| 38 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 39 | static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n" |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 40 | "Change the group membership of each FILE to GROUP.\n" |
| 41 | "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n"; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 42 | static const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n" |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 43 | "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n" |
| 44 | "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n"; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 45 | static const char chmod_usage[] = "[-R] MODE[,MODE]... FILE...\n" |
| 46 | "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n" |
| 47 | "one or more of the letters rwxst.\n\n" |
| 48 | "\t-R\tchange files and directories recursively.\n"; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 49 | |
| 50 | |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 51 | uid_t my_getid(const char *filename, const char *name) |
| 52 | { |
| 53 | FILE *stream; |
| 54 | char *rname, *start, *end, buf[128]; |
| 55 | uid_t rid; |
| 56 | |
| 57 | stream=fopen(filename,"r"); |
| 58 | |
| 59 | while (fgets (buf, 128, stream) != NULL) { |
| 60 | if (buf[0] == '#') |
| 61 | continue; |
| 62 | |
| 63 | start = buf; |
| 64 | end = strchr (start, ':'); |
| 65 | if (end == NULL) |
| 66 | continue; |
| 67 | *end = '\0'; |
| 68 | rname = start; |
| 69 | |
| 70 | start = end + 1; |
| 71 | end = strchr (start, ':'); |
| 72 | if (end == NULL) |
| 73 | continue; |
| 74 | |
| 75 | start = end + 1; |
| 76 | rid = (uid_t) strtol (start, &end, 10); |
| 77 | if (end == start) |
| 78 | continue; |
| 79 | |
| 80 | if (name) { |
| 81 | if (0 == strcmp(rname, name)) |
| 82 | return( rid); |
| 83 | } |
| 84 | } |
| 85 | fclose(stream); |
| 86 | return (-1); |
| 87 | } |
| 88 | |
| 89 | uid_t |
| 90 | my_getpwnam(char *name) |
| 91 | { |
| 92 | return my_getid("/etc/passwd", name); |
| 93 | } |
| 94 | |
| 95 | gid_t |
| 96 | my_getgrnam(char *name) |
| 97 | { |
| 98 | return my_getid("/etc/group", name); |
| 99 | } |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 100 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 101 | static int fileAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 103 | switch (whichApp) { |
| 104 | case CHGRP_APP: |
| 105 | case CHOWN_APP: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 106 | if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, |
| 107 | (gid==-1)? statbuf->st_gid : gid) == 0) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 108 | return( TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 109 | } |
| 110 | break; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 111 | case CHMOD_APP: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 112 | if (chmod(fileName, mode) == 0) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 113 | return( TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 114 | break; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 115 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 116 | perror(fileName); |
| 117 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 120 | int chmod_chown_chgrp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 121 | { |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 122 | int recursiveFlag=FALSE; |
| 123 | char *groupName; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 124 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 125 | whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 126 | |
| 127 | if (argc < 2) { |
| 128 | fprintf(stderr, "Usage: %s %s", *argv, |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 129 | (whichApp==TRUE)? chown_usage : chgrp_usage); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 130 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 131 | } |
| 132 | invocationName=*argv; |
| 133 | argc--; |
| 134 | argv++; |
| 135 | |
| 136 | /* Parse options */ |
| 137 | while (**argv == '-') { |
| 138 | while (*++(*argv)) switch (**argv) { |
| 139 | case 'R': |
| 140 | recursiveFlag = TRUE; |
| 141 | break; |
| 142 | default: |
| 143 | fprintf(stderr, "Unknown option: %c\n", **argv); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 144 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 146 | argc--; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 147 | argv++; |
| 148 | } |
| 149 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 150 | if ( whichApp == CHMOD_APP ) { |
| 151 | /* Find the specified modes */ |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 152 | mode &= S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 153 | if ( parse_mode(*argv, &mode) == FALSE ) { |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 154 | fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 155 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 156 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 157 | } else { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 159 | /* Find the selected group */ |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 160 | if ( whichApp==CHGRP_APP && groupName ) { |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 161 | groupName = *argv; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 162 | gid = my_getgrnam(groupName); |
| 163 | if (gid == -1) |
| 164 | goto bad_group; |
| 165 | } else { |
| 166 | groupName = strchr(*argv, '.'); |
| 167 | if (groupName) { |
| 168 | *groupName++ = '\0'; |
| 169 | gid = my_getgrnam(groupName); |
| 170 | if (gid == -1) |
| 171 | goto bad_group; |
| 172 | } else |
| 173 | gid = -1; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 174 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 175 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 176 | |
| 177 | /* Find the selected user (if appropriate) */ |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 178 | if (whichApp==CHOWN_APP) { |
| 179 | uid = my_getpwnam(*argv); |
| 180 | if (uid == -1) { |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 181 | fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv); |
| 182 | exit( FALSE); |
| 183 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 187 | /* Ok, ready to do the deed now */ |
| 188 | if (argc <= 1) { |
| 189 | fprintf(stderr, "%s: too few arguments", invocationName); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 190 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 191 | } |
| 192 | while (argc-- > 1) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 193 | if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 194 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 195 | } |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 196 | exit(TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame^] | 197 | |
| 198 | bad_group: |
| 199 | fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName); |
| 200 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 202 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 203 | |