blob: 530c45658b5a2c26680a1799ddc6cf202a7f5a9e [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
Eric Andersen3570a342000-09-25 21:45:58 +000025#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000026#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 Andersen3364d782000-03-28 00:58:14 +000046static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersencc8ed391999-10-05 16:24:54 +000047{
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 switch (whichApp) {
Eric Andersen9b587181999-10-17 05:43:39 +000049 case CHGRP_APP:
50 case CHOWN_APP:
John Beppubf3a8382000-01-24 12:06:22 +000051#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 if (lchown
53 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
54 (gid == -1) ? statbuf->st_gid : gid) == 0)
John Beppubf3a8382000-01-24 12:06:22 +000055#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 if (chown
57 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
58 (gid == -1) ? statbuf->st_gid : gid) == 0)
John Beppubf3a8382000-01-24 12:06:22 +000059#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 {
61 return (TRUE);
62 }
63 break;
Eric Andersen9b587181999-10-17 05:43:39 +000064 case CHMOD_APP:
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 /* Parse the specified modes */
66 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000067 error_msg_and_die( "unknown mode: %s\n", theMode);
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 }
69 if (chmod(fileName, statbuf->st_mode) == 0)
70 return (TRUE);
71 break;
72 }
73 perror(fileName);
74 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000075}
76
Eric Andersen1b61f411999-10-13 18:56:42 +000077int chmod_chown_chgrp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000078{
Eric Andersence82c8a2000-08-22 01:00:56 +000079 int stopIt = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 int recursiveFlag = FALSE;
Eric Andersenb6106152000-06-19 17:25:40 +000081 char *groupName=NULL;
82 char *p=NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 const char *appUsage;
Eric Andersen2b69c401999-10-05 22:58:32 +000084
Matt Kraaie714bce2000-07-12 00:53:06 +000085 whichApp = (strcmp(applet_name, "chown") == 0)?
86 CHOWN_APP : (strcmp(applet_name, "chmod") == 0)?
Erik Andersen9ffdaa62000-02-11 21:55:04 +000087 CHMOD_APP : CHGRP_APP;
Eric Andersen2b69c401999-10-05 22:58:32 +000088
Erik Andersen246cc6d2000-03-07 07:41:42 +000089 appUsage = (whichApp == CHOWN_APP)?
Erik Andersen9ffdaa62000-02-11 21:55:04 +000090 chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage;
Eric Andersend73dc5b1999-11-10 23:13:02 +000091
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 if (argc < 2)
93 usage(appUsage);
Eric Andersen2b69c401999-10-05 22:58:32 +000094 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000095
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 /* Parse options */
Mark Whitleyeec2f632000-06-06 18:11:46 +000097 while (--argc >= 0 && *argv && (**argv == '-')) {
Eric Andersence82c8a2000-08-22 01:00:56 +000098 while (stopIt==FALSE && *++(*argv)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 switch (**argv) {
Mark Whitleyeec2f632000-06-06 18:11:46 +0000100 case 'R':
101 recursiveFlag = TRUE;
102 break;
103 default:
Eric Andersence82c8a2000-08-22 01:00:56 +0000104 theMode=*argv-1;
105 stopIt = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 }
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000107 }
Eric Andersence82c8a2000-08-22 01:00:56 +0000108 if (stopIt==TRUE)
109 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 argv++;
111 }
112
Mark Whitleyeec2f632000-06-06 18:11:46 +0000113 if (argc == 0 || *argv == NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000114 error_msg(too_few_args);
Mark Whitleyeec2f632000-06-06 18:11:46 +0000115 }
116
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 if (whichApp == CHMOD_APP) {
Eric Andersence82c8a2000-08-22 01:00:56 +0000118 if (theMode==NULL)
119 theMode = *argv;
Eric Andersence8f3b91999-10-20 07:03:36 +0000120 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121
122 /* Find the selected group */
123 if (whichApp == CHGRP_APP) {
124 groupName = *argv;
125 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */
126 if (groupName == p)
127 gid = my_getgrnam(groupName);
128 if (gid == -1)
129 goto bad_group;
130 } else {
131 groupName = strchr(*argv, '.');
Erik Andersen632bb572000-04-18 22:09:06 +0000132 if (groupName == NULL)
133 groupName = strchr(*argv, ':');
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 if (groupName) {
135 *groupName++ = '\0';
136 gid = strtoul(groupName, &p, 10);
137 if (groupName == p)
138 gid = my_getgrnam(groupName);
139 if (gid == -1)
140 goto bad_group;
141 } else
142 gid = -1;
143 }
144
145
146 /* Find the selected user (if appropriate) */
147 if (whichApp == CHOWN_APP) {
148 uid = strtoul(*argv, &p, 10); /* if numeric ... */
149 if (*argv == p)
150 uid = my_getpwnam(*argv);
151 if (uid == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000152 error_msg_and_die( "unknown user name: %s\n", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 }
154 }
Eric Andersen1b61f411999-10-13 18:56:42 +0000155 }
Eric Andersence8f3b91999-10-20 07:03:36 +0000156
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 /* Ok, ready to do the deed now */
Eric Andersence82c8a2000-08-22 01:00:56 +0000158 if (argc < 1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000159 error_msg_and_die(too_few_args);
Eric Andersen1b61f411999-10-13 18:56:42 +0000160 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 while (argc-- > 1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000162 if (recursive_action (*(++argv), recursiveFlag, FALSE, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000163 fileAction, fileAction, NULL) == FALSE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000164 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000166 return EXIT_SUCCESS;
Eric Andersence8f3b91999-10-20 07:03:36 +0000167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 bad_group:
Mark Whitleyf57c9442000-12-07 19:56:48 +0000169 error_msg_and_die( "unknown group name: %s\n", groupName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000170}
Erik Andersen029011b2000-03-04 21:19:32 +0000171
172/*
173Local Variables:
174c-file-style: "linux"
175c-basic-offset: 4
176tab-width: 4
177End:
178*/