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 | * |
Eric Andersen | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000,2001 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 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 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 | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 58361a4 | 2000-07-14 06:27:54 +0000 | [diff] [blame] | 37 | static long uid = -1; |
| 38 | static long gid = -1; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 39 | static int whichApp; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | static char *theMode = NULL; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 41 | |
| 42 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 43 | #define CHGRP_APP 1 |
| 44 | #define CHOWN_APP 2 |
| 45 | #define CHMOD_APP 3 |
| 46 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 47 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | switch (whichApp) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 50 | case CHGRP_APP: |
| 51 | case CHOWN_APP: |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 52 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | if (lchown |
| 54 | (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid, |
| 55 | (gid == -1) ? statbuf->st_gid : gid) == 0) |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 56 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | if (chown |
| 58 | (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid, |
| 59 | (gid == -1) ? statbuf->st_gid : gid) == 0) |
John Beppu | bf3a838 | 2000-01-24 12:06:22 +0000 | [diff] [blame] | 60 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | { |
| 62 | return (TRUE); |
| 63 | } |
| 64 | break; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 65 | case CHMOD_APP: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | /* Parse the specified modes */ |
| 67 | if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 68 | error_msg_and_die( "unknown mode: %s", theMode); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | } |
| 70 | if (chmod(fileName, statbuf->st_mode) == 0) |
| 71 | return (TRUE); |
| 72 | break; |
| 73 | } |
| 74 | perror(fileName); |
| 75 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 78 | int chmod_chown_chgrp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | { |
Eric Andersen | ce82c8a | 2000-08-22 01:00:56 +0000 | [diff] [blame] | 80 | int stopIt = FALSE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | int recursiveFlag = FALSE; |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 82 | char *groupName=NULL; |
| 83 | char *p=NULL; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 84 | |
Eric Andersen | 704d2a6 | 2001-01-31 17:29:12 +0000 | [diff] [blame] | 85 | whichApp = (applet_name[2]=='o')? /* chown */ |
| 86 | CHOWN_APP : (applet_name[2]=='m')? /* chmod */ |
| 87 | CHMOD_APP : CHGRP_APP; |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 88 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | if (argc < 2) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 90 | show_usage(); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 91 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | /* Parse options */ |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 94 | while (--argc >= 0 && *argv && (**argv == '-')) { |
Eric Andersen | ce82c8a | 2000-08-22 01:00:56 +0000 | [diff] [blame] | 95 | while (stopIt==FALSE && *++(*argv)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | switch (**argv) { |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 97 | case 'R': |
| 98 | recursiveFlag = TRUE; |
| 99 | break; |
| 100 | default: |
Eric Andersen | ce82c8a | 2000-08-22 01:00:56 +0000 | [diff] [blame] | 101 | theMode=*argv-1; |
| 102 | stopIt = TRUE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 104 | } |
Eric Andersen | ce82c8a | 2000-08-22 01:00:56 +0000 | [diff] [blame] | 105 | if (stopIt==TRUE) |
| 106 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | argv++; |
| 108 | } |
| 109 | |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 110 | if (argc == 0 || *argv == NULL) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 111 | error_msg(too_few_args); |
Mark Whitley | eec2f63 | 2000-06-06 18:11:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 114 | if (whichApp == CHMOD_APP) { |
Eric Andersen | ce82c8a | 2000-08-22 01:00:56 +0000 | [diff] [blame] | 115 | if (theMode==NULL) |
| 116 | theMode = *argv; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 117 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | |
| 119 | /* Find the selected group */ |
| 120 | if (whichApp == CHGRP_APP) { |
| 121 | groupName = *argv; |
| 122 | gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */ |
| 123 | if (groupName == p) |
| 124 | gid = my_getgrnam(groupName); |
| 125 | if (gid == -1) |
| 126 | goto bad_group; |
| 127 | } else { |
| 128 | groupName = strchr(*argv, '.'); |
Erik Andersen | 632bb57 | 2000-04-18 22:09:06 +0000 | [diff] [blame] | 129 | if (groupName == NULL) |
| 130 | groupName = strchr(*argv, ':'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | if (groupName) { |
| 132 | *groupName++ = '\0'; |
| 133 | gid = strtoul(groupName, &p, 10); |
| 134 | if (groupName == p) |
| 135 | gid = my_getgrnam(groupName); |
| 136 | if (gid == -1) |
| 137 | goto bad_group; |
| 138 | } else |
| 139 | gid = -1; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* Find the selected user (if appropriate) */ |
| 144 | if (whichApp == CHOWN_APP) { |
| 145 | uid = strtoul(*argv, &p, 10); /* if numeric ... */ |
| 146 | if (*argv == p) |
| 147 | uid = my_getpwnam(*argv); |
| 148 | if (uid == -1) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 149 | error_msg_and_die( "unknown user name: %s", *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 152 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 153 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 154 | /* Ok, ready to do the deed now */ |
Eric Andersen | ce82c8a | 2000-08-22 01:00:56 +0000 | [diff] [blame] | 155 | if (argc < 1) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 156 | error_msg_and_die(too_few_args); |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 157 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | while (argc-- > 1) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 159 | if (recursive_action (*(++argv), recursiveFlag, FALSE, FALSE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 160 | fileAction, fileAction, NULL) == FALSE) |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 161 | return EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 162 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 163 | return EXIT_SUCCESS; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 164 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 165 | bad_group: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 166 | error_msg_and_die( "unknown group name: %s", groupName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 167 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | Local Variables: |
| 171 | c-file-style: "linux" |
| 172 | c-basic-offset: 4 |
| 173 | tab-width: 4 |
| 174 | End: |
| 175 | */ |