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 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 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 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 24 | #include "internal.h" |
| 25 | #define BB_DECLARE_EXTERN |
| 26 | #define bb_need_invalid_option |
| 27 | #include "messages.c" |
| 28 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 30 | #include <grp.h> |
| 31 | #include <pwd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | |
Erik Andersen | 04e9702 | 2000-01-29 07:06:24 +0000 | [diff] [blame] | 34 | static uid_t uid = -1; |
| 35 | static gid_t gid = -1; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 36 | static int whichApp; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 37 | static char* invocationName=NULL; |
Eric Andersen | ded6259 | 1999-11-18 00:19:26 +0000 | [diff] [blame] | 38 | static char* theMode=NULL; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 39 | |
| 40 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 41 | #define CHGRP_APP 1 |
| 42 | #define CHOWN_APP 2 |
| 43 | #define CHMOD_APP 3 |
| 44 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 45 | static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n" |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 46 | "Change the group membership of each FILE to GROUP.\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 47 | "\nOptions:\n\t-R\tchange files and directories recursively\n"; |
| 48 | static const char chown_usage[] = "chown [OPTION]... OWNER[.[GROUP] FILE...\n\n" |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 49 | "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 50 | "\nOptions:\n\t-R\tchange files and directories recursively\n"; |
| 51 | static const char chmod_usage[] = "chmod [-R] MODE[,MODE]... FILE...\n\n" |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 52 | "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n" |
| 53 | "one or more of the letters rwxst.\n\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 54 | "\nOptions:\n\t-R\tchange files and directories recursively.\n"; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 55 | |
| 56 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 57 | static int fileAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 59 | switch (whichApp) { |
| 60 | case CHGRP_APP: |
| 61 | case CHOWN_APP: |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 62 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
John Beppu | a4a8fd0 | 2000-01-23 13:38:32 +0000 | [diff] [blame] | 63 | if (lchown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 64 | (gid==-1)? statbuf->st_gid : gid) == 0) |
| 65 | #else |
| 66 | if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, |
| 67 | (gid==-1)? statbuf->st_gid : gid) == 0) |
| 68 | #endif |
| 69 | { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 70 | return( TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 71 | } |
| 72 | break; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 73 | case CHMOD_APP: |
Eric Andersen | ded6259 | 1999-11-18 00:19:26 +0000 | [diff] [blame] | 74 | /* Parse the specified modes */ |
| 75 | if ( parse_mode(theMode, &(statbuf->st_mode)) == FALSE ) { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 76 | fprintf(stderr, "%s: unknown mode: %s\n", invocationName, theMode); |
Eric Andersen | ded6259 | 1999-11-18 00:19:26 +0000 | [diff] [blame] | 77 | exit( FALSE); |
| 78 | } |
| 79 | if (chmod(fileName, statbuf->st_mode) == 0) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 80 | return( TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 81 | break; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 82 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 83 | perror(fileName); |
| 84 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 87 | int chmod_chown_chgrp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | { |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 89 | int recursiveFlag=FALSE; |
| 90 | char *groupName; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 91 | char *p; |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 92 | const char *appUsage; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 93 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 94 | 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] | 95 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 96 | appUsage = (whichApp==CHOWN_APP)? chown_usage : (whichApp==CHMOD_APP)? chmod_usage : chgrp_usage; |
| 97 | |
| 98 | if (argc < 2) |
| 99 | usage( appUsage); |
Erik Andersen | 04e9702 | 2000-01-29 07:06:24 +0000 | [diff] [blame] | 100 | invocationName = *argv; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 101 | argc--; |
| 102 | argv++; |
| 103 | |
| 104 | /* Parse options */ |
| 105 | while (**argv == '-') { |
| 106 | while (*++(*argv)) switch (**argv) { |
| 107 | case 'R': |
| 108 | recursiveFlag = TRUE; |
| 109 | break; |
| 110 | default: |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 111 | fprintf(stderr, invalid_option, invocationName, **argv); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 112 | usage( appUsage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 113 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | argc--; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 115 | argv++; |
| 116 | } |
| 117 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 118 | if ( whichApp == CHMOD_APP ) { |
Erik Andersen | 04e9702 | 2000-01-29 07:06:24 +0000 | [diff] [blame] | 119 | theMode = *argv; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 120 | } else { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 121 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 122 | /* Find the selected group */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 123 | if ( whichApp==CHGRP_APP ) { |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 124 | groupName = *argv; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 125 | gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */ |
| 126 | if (groupName == p) |
| 127 | gid = my_getgrnam(groupName); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 128 | if (gid == -1) |
| 129 | goto bad_group; |
| 130 | } else { |
| 131 | groupName = strchr(*argv, '.'); |
| 132 | if (groupName) { |
| 133 | *groupName++ = '\0'; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 134 | gid = strtoul(groupName, &p, 10); |
| 135 | if (groupName == p) |
| 136 | gid = my_getgrnam(groupName); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 137 | if (gid == -1) |
| 138 | goto bad_group; |
| 139 | } else |
| 140 | gid = -1; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 141 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 142 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 143 | |
| 144 | /* Find the selected user (if appropriate) */ |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 145 | if (whichApp==CHOWN_APP) { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 146 | uid = strtoul(*argv, &p, 10); /* if numeric ...*/ |
| 147 | if (*argv == p) |
| 148 | uid = my_getpwnam(*argv); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 149 | if (uid == -1) { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 150 | fprintf(stderr, "%s: unknown user name: %s\n", invocationName, *argv); |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 151 | exit( FALSE); |
| 152 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 156 | /* Ok, ready to do the deed now */ |
| 157 | if (argc <= 1) { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 158 | fprintf(stderr, "%s: too few arguments\n", invocationName); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 159 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 160 | } |
| 161 | while (argc-- > 1) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 162 | if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 163 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 164 | } |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 165 | exit(TRUE); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 166 | |
| 167 | bad_group: |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame^] | 168 | fprintf(stderr, "%s: unknown group name: %s\n", invocationName, groupName); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 169 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 170 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 171 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 172 | |