blob: 888c3fc89abf5dde64d8ca6072500ab4b8b155a9 [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
Eric Andersen9f0fedb2001-04-24 18:07:19 +000025static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
26{
Rob Landleyf8ec1b52006-01-06 18:22:05 +000027 if (!chown_func(fileName,
28 (uid == -1) ? statbuf->st_uid : uid,
29 (gid == -1) ? statbuf->st_gid : gid)) {
30 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000031 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000032 bb_perror_msg("%s", fileName); /* A filename could have % in it... */
33 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000034}
35
Manuel Novoa III cad53642003-03-19 09:13:01 +000036#define FLAG_R 1
37#define FLAG_h 2
38
Eric Andersen9f0fedb2001-04-24 18:07:19 +000039int chown_main(int argc, char **argv)
40{
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 int flags;
42 int retval = EXIT_SUCCESS;
43 char *groupName;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000044
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 flags = bb_getopt_ulflags(argc, argv, "Rh");
46
47 if (flags & FLAG_h) chown_func = lchown;
48
49 if (argc - optind < 2) {
50 bb_show_usage();
Eric Andersen9f0fedb2001-04-24 18:07:19 +000051 }
52
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 argv += optind;
Eric Andersen2b02ab92001-05-11 15:55:41 +000054
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 /* First, check if there is a group name here */
56 if ((groupName = strchr(*argv, '.')) == NULL) {
57 groupName = strchr(*argv, ':');
Eric Andersen9f0fedb2001-04-24 18:07:19 +000058 }
59
Rob Landleyfb8c4982006-01-09 03:45:43 +000060 /* Check for the username and groupname */
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 if (groupName) {
62 *groupName++ = '\0';
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000063 gid = get_ug_id(groupName, bb_xgetgrnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 }
Rob Landleyfb8c4982006-01-09 03:45:43 +000065 if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 ++argv;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000067
Eric Andersen9f0fedb2001-04-24 18:07:19 +000068 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 do {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070 if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 fileAction, fileAction, NULL)) {
72 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000073 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +000075
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000077}
78
79/*
80Local Variables:
81c-file-style: "linux"
82c-basic-offset: 4
83tab-width: 4
84End:
85*/