blob: 66add48edfb479553f8b3b3e550b68aeec4d8ce5 [file] [log] [blame]
Eric Andersen9f0fedb2001-04-24 18:07:19 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Mini chown implementation for busybox
Eric Andersen9f0fedb2001-04-24 18:07:19 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen9f0fedb2001-04-24 18:07:19 +00006 *
Rob Landleyf8ec1b52006-01-06 18:22:05 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen9f0fedb2001-04-24 18:07:19 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
11/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
12/* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
13/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
14
Eric Andersen9f0fedb2001-04-24 18:07:19 +000015#include <stdlib.h>
Eric Andersen9f0fedb2001-04-24 18:07:19 +000016#include <unistd.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000017#include <string.h>
Eric Andersen9f0fedb2001-04-24 18:07:19 +000018#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000019
Rob Landleyfb8c4982006-01-09 03:45:43 +000020static uid_t uid = -1;
21static gid_t gid = -1;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000022
Glenn L McGratha88cb502003-02-08 23:36:16 +000023static int (*chown_func)(const char *, uid_t, gid_t) = chown;
Eric Andersen2b02ab92001-05-11 15:55:41 +000024
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000025static int fileAction(const char *fileName, struct stat *statbuf,
26 void ATTRIBUTE_UNUSED *junk)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000027{
Rob Landleyf8ec1b52006-01-06 18:22:05 +000028 if (!chown_func(fileName,
"Vladimir N. Oleynik"57545c82006-01-31 12:06:57 +000029 (uid == (uid_t)-1) ? statbuf->st_uid : uid,
30 (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
Rob Landleyf8ec1b52006-01-06 18:22:05 +000031 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000032 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000033 bb_perror_msg("%s", fileName); /* A filename could have % in it... */
34 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000035}
36
Manuel Novoa III cad53642003-03-19 09:13:01 +000037#define FLAG_R 1
38#define FLAG_h 2
39
Eric Andersen9f0fedb2001-04-24 18:07:19 +000040int chown_main(int argc, char **argv)
41{
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 int flags;
43 int retval = EXIT_SUCCESS;
44 char *groupName;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000045
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 flags = bb_getopt_ulflags(argc, argv, "Rh");
47
48 if (flags & FLAG_h) chown_func = lchown;
49
50 if (argc - optind < 2) {
51 bb_show_usage();
Eric Andersen9f0fedb2001-04-24 18:07:19 +000052 }
53
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 argv += optind;
Eric Andersen2b02ab92001-05-11 15:55:41 +000055
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 /* First, check if there is a group name here */
57 if ((groupName = strchr(*argv, '.')) == NULL) {
58 groupName = strchr(*argv, ':');
Eric Andersen9f0fedb2001-04-24 18:07:19 +000059 }
60
Rob Landleyfb8c4982006-01-09 03:45:43 +000061 /* Check for the username and groupname */
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 if (groupName) {
63 *groupName++ = '\0';
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000064 gid = get_ug_id(groupName, bb_xgetgrnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 }
Rob Landleyfb8c4982006-01-09 03:45:43 +000066 if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 ++argv;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068
Eric Andersen9f0fedb2001-04-24 18:07:19 +000069 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 do {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 fileAction, fileAction, NULL)) {
73 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000074 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +000076
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000078}
79
80/*
81Local Variables:
82c-file-style: "linux"
83c-basic-offset: 4
84tab-width: 4
85End:
86*/