blob: 156a0a9f9a634d9fcc79d15c705179af402fbe3f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen2b69c401999-10-05 22:58:32 +00002/*
Eric Andersen1b61f411999-10-13 18:56:42 +00003 * Mini chown/chmod/chgrp implementation for busybox
Eric Andersen2b69c401999-10-05 22:58:32 +00004 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Erik Andersen61677fe2000-04-13 01:18:56 +00006 * Copyright (C) 1999,2000 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00007 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersen2b69c401999-10-05 22:58:32 +00008 *
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
Erik Andersenfac10d72000-02-07 05:29:42 +000025#include "internal.h"
26#define BB_DECLARE_EXTERN
27#define bb_need_invalid_option
Mark Whitleyeec2f632000-06-06 18:11:46 +000028#define bb_need_too_few_args
Erik Andersenfac10d72000-02-07 05:29:42 +000029#include "messages.c"
30
Eric Andersencc8ed391999-10-05 16:24:54 +000031#include <stdio.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000032#include <grp.h>
33#include <pwd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Eric Andersen58361a42000-07-14 06:27:54 +000036static long uid = -1;
37static long gid = -1;
Eric Andersen1b61f411999-10-13 18:56:42 +000038static int whichApp;
Erik Andersene49d5ec2000-02-08 19:58:47 +000039static char *theMode = NULL;
Eric Andersen2b69c401999-10-05 22:58:32 +000040
41
Eric Andersen1b61f411999-10-13 18:56:42 +000042#define CHGRP_APP 1
43#define CHOWN_APP 2
44#define CHMOD_APP 3
45
Erik Andersen1d1d9502000-04-21 01:26:49 +000046static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n"
47#ifndef BB_FEATURE_TRIVIAL_HELP
48 "\nChange the group membership of each FILE to GROUP.\n"
49 "\nOptions:\n\t-R\tChanges files and directories recursively.\n"
50#endif
51 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000052static const char chown_usage[] =
Erik Andersen1d1d9502000-04-21 01:26:49 +000053 "chown [OPTION]... OWNER[<.|:>[GROUP] FILE...\n"
54#ifndef BB_FEATURE_TRIVIAL_HELP
55 "\nChange the owner and/or group of each FILE to OWNER and/or GROUP.\n"
56 "\nOptions:\n\t-R\tChanges files and directories recursively.\n"
57#endif
58 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000059static const char chmod_usage[] =
Erik Andersen1d1d9502000-04-21 01:26:49 +000060 "chmod [-R] MODE[,MODE]... FILE...\n"
61#ifndef BB_FEATURE_TRIVIAL_HELP
62 "\nEach MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 "one or more of the letters rwxst.\n\n"
Erik Andersen1d1d9502000-04-21 01:26:49 +000064 "\nOptions:\n\t-R\tChanges files and directories recursively.\n"
65#endif
66 ;
Eric Andersen2b69c401999-10-05 22:58:32 +000067
68
Erik Andersen3364d782000-03-28 00:58:14 +000069static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersencc8ed391999-10-05 16:24:54 +000070{
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 switch (whichApp) {
Eric Andersen9b587181999-10-17 05:43:39 +000072 case CHGRP_APP:
73 case CHOWN_APP:
John Beppubf3a8382000-01-24 12:06:22 +000074#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 if (lchown
76 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
77 (gid == -1) ? statbuf->st_gid : gid) == 0)
John Beppubf3a8382000-01-24 12:06:22 +000078#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 if (chown
80 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
81 (gid == -1) ? statbuf->st_gid : gid) == 0)
John Beppubf3a8382000-01-24 12:06:22 +000082#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 {
84 return (TRUE);
85 }
86 break;
Eric Andersen9b587181999-10-17 05:43:39 +000087 case CHMOD_APP:
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 /* Parse the specified modes */
89 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +000090 fatalError( "unknown mode: %s\n", theMode);
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 }
92 if (chmod(fileName, statbuf->st_mode) == 0)
93 return (TRUE);
94 break;
95 }
96 perror(fileName);
97 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000098}
99
Eric Andersen1b61f411999-10-13 18:56:42 +0000100int chmod_chown_chgrp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000101{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 int recursiveFlag = FALSE;
Eric Andersenb6106152000-06-19 17:25:40 +0000103 char *groupName=NULL;
104 char *p=NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 const char *appUsage;
Eric Andersen2b69c401999-10-05 22:58:32 +0000106
Matt Kraaie714bce2000-07-12 00:53:06 +0000107 whichApp = (strcmp(applet_name, "chown") == 0)?
108 CHOWN_APP : (strcmp(applet_name, "chmod") == 0)?
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109 CHMOD_APP : CHGRP_APP;
Eric Andersen2b69c401999-10-05 22:58:32 +0000110
Erik Andersen246cc6d2000-03-07 07:41:42 +0000111 appUsage = (whichApp == CHOWN_APP)?
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000112 chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage;
Eric Andersend73dc5b1999-11-10 23:13:02 +0000113
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 if (argc < 2)
115 usage(appUsage);
Eric Andersen2b69c401999-10-05 22:58:32 +0000116 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 /* Parse options */
Mark Whitleyeec2f632000-06-06 18:11:46 +0000119 while (--argc >= 0 && *argv && (**argv == '-')) {
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000120 while (*++(*argv)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 switch (**argv) {
Mark Whitleyeec2f632000-06-06 18:11:46 +0000122 case 'R':
123 recursiveFlag = TRUE;
124 break;
125 default:
Matt Kraaid537a952000-07-14 01:51:25 +0000126 errorMsg(invalid_option, **argv);
Mark Whitleyeec2f632000-06-06 18:11:46 +0000127 usage(appUsage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000129 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 argv++;
131 }
132
Mark Whitleyeec2f632000-06-06 18:11:46 +0000133 if (argc == 0 || *argv == NULL) {
Matt Kraaid537a952000-07-14 01:51:25 +0000134 errorMsg(too_few_args);
Mark Whitleyeec2f632000-06-06 18:11:46 +0000135 usage(appUsage);
136 }
137
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 if (whichApp == CHMOD_APP) {
139 theMode = *argv;
Eric Andersence8f3b91999-10-20 07:03:36 +0000140 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141
142 /* Find the selected group */
143 if (whichApp == CHGRP_APP) {
144 groupName = *argv;
145 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */
146 if (groupName == p)
147 gid = my_getgrnam(groupName);
148 if (gid == -1)
149 goto bad_group;
150 } else {
151 groupName = strchr(*argv, '.');
Erik Andersen632bb572000-04-18 22:09:06 +0000152 if (groupName == NULL)
153 groupName = strchr(*argv, ':');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 if (groupName) {
155 *groupName++ = '\0';
156 gid = strtoul(groupName, &p, 10);
157 if (groupName == p)
158 gid = my_getgrnam(groupName);
159 if (gid == -1)
160 goto bad_group;
161 } else
162 gid = -1;
163 }
164
165
166 /* Find the selected user (if appropriate) */
167 if (whichApp == CHOWN_APP) {
168 uid = strtoul(*argv, &p, 10); /* if numeric ... */
169 if (*argv == p)
170 uid = my_getpwnam(*argv);
171 if (uid == -1) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000172 fatalError( "unknown user name: %s\n", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 }
174 }
Eric Andersen1b61f411999-10-13 18:56:42 +0000175 }
Eric Andersence8f3b91999-10-20 07:03:36 +0000176
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 /* Ok, ready to do the deed now */
178 if (argc <= 1) {
Matt Kraaid537a952000-07-14 01:51:25 +0000179 fatalError(too_few_args);
Eric Andersen1b61f411999-10-13 18:56:42 +0000180 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 while (argc-- > 1) {
Erik Andersen632bb572000-04-18 22:09:06 +0000182 if (recursiveAction (*(++argv), recursiveFlag, FALSE, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000183 fileAction, fileAction, NULL) == FALSE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 exit(FALSE);
185 }
186 exit(TRUE);
Eric Andersence8f3b91999-10-20 07:03:36 +0000187
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 bad_group:
Matt Kraaibe84cd42000-07-12 17:02:35 +0000189 fatalError( "unknown group name: %s\n", groupName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000190}
Erik Andersen029011b2000-03-04 21:19:32 +0000191
192/*
193Local Variables:
194c-file-style: "linux"
195c-basic-offset: 4
196tab-width: 4
197End:
198*/