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 | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 28 | static int uid=-1; |
| 29 | static int gid=0; |
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 | |
| 51 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 52 | static int fileAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 54 | switch (whichApp) { |
| 55 | case CHGRP_APP: |
| 56 | case CHOWN_APP: |
| 57 | if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statbuf->st_uid), gid) < 0) |
| 58 | return( TRUE); |
| 59 | case CHMOD_APP: |
| 60 | fprintf(stderr, "%s, %d\n", fileName, mode); |
| 61 | if (chmod(fileName, mode)) |
| 62 | return( TRUE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 63 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 64 | perror(fileName); |
| 65 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 68 | int chmod_chown_chgrp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 69 | { |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 70 | struct group *grp; |
| 71 | struct passwd *pwd; |
| 72 | int recursiveFlag=FALSE; |
| 73 | char *groupName; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 74 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 75 | 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] | 76 | |
| 77 | if (argc < 2) { |
| 78 | fprintf(stderr, "Usage: %s %s", *argv, |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 79 | (whichApp==TRUE)? chown_usage : chgrp_usage); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 80 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 81 | } |
| 82 | invocationName=*argv; |
| 83 | argc--; |
| 84 | argv++; |
| 85 | |
| 86 | /* Parse options */ |
| 87 | while (**argv == '-') { |
| 88 | while (*++(*argv)) switch (**argv) { |
| 89 | case 'R': |
| 90 | recursiveFlag = TRUE; |
| 91 | break; |
| 92 | default: |
| 93 | fprintf(stderr, "Unknown option: %c\n", **argv); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 94 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | argc--; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 97 | argv++; |
| 98 | } |
| 99 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 100 | if ( whichApp == CHMOD_APP ) { |
| 101 | /* Find the specified modes */ |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame^] | 102 | 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] | 103 | if ( parse_mode(*argv, &mode) == FALSE ) { |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 104 | fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 105 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 106 | } |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 107 | //mode &= andWithMode; |
| 108 | fprintf(stderr, "mode %d\n", mode); |
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 */ |
| 112 | groupName = strchr(*argv, '.'); |
| 113 | if ( whichApp==TRUE && groupName ) |
| 114 | *groupName++ = '\0'; |
| 115 | else |
| 116 | groupName = *argv; |
| 117 | grp = getgrnam(groupName); |
| 118 | if (grp == NULL) { |
| 119 | fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName); |
| 120 | exit( FALSE); |
| 121 | } |
| 122 | gid = grp->gr_gid; |
| 123 | |
| 124 | /* Find the selected user (if appropriate) */ |
| 125 | if (whichApp==TRUE) { |
| 126 | pwd = getpwnam(*argv); |
| 127 | if (pwd == NULL) { |
| 128 | fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv); |
| 129 | exit( FALSE); |
| 130 | } |
| 131 | uid = pwd->pw_uid; |
| 132 | } |
| 133 | } |
| 134 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 135 | /* Ok, ready to do the deed now */ |
| 136 | if (argc <= 1) { |
| 137 | fprintf(stderr, "%s: too few arguments", invocationName); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 138 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 139 | } |
| 140 | while (argc-- > 1) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 141 | if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 142 | exit( FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 143 | } |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 144 | exit(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 146 | |