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