1/* chown -- change user and group ownership of files
2   Copyright (C) 1989-1991, 1995-2010 Free Software Foundation, Inc.
3
4   This program is free software: you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation, either version 3 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
18
19#include <config.h>
20#include <stdio.h>
21#include <sys/types.h>
22#include <getopt.h>
23
24#include "system.h"
25#include "chown-core.h"
26#include "error.h"
27#include "fts_.h"
28#include "quote.h"
29#include "root-dev-ino.h"
30#include "userspec.h"
31
32/* The official name of this program (e.g., no `g' prefix).  */
33#define PROGRAM_NAME "chown"
34
35#define AUTHORS \
36  proper_name ("David MacKenzie"), \
37  proper_name ("Jim Meyering")
38
39/* The argument to the --reference option.  Use the owner and group IDs
40   of this file.  This file must exist.  */
41static char *reference_file;
42
43/* For long options that have no equivalent short option, use a
44   non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
45enum
46{
47  DEREFERENCE_OPTION = CHAR_MAX + 1,
48  FROM_OPTION,
49  NO_PRESERVE_ROOT,
50  PRESERVE_ROOT,
51  REFERENCE_FILE_OPTION
52};
53
54static struct option const long_options[] =
55{
56  {"recursive", no_argument, NULL, 'R'},
57  {"changes", no_argument, NULL, 'c'},
58  {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
59  {"from", required_argument, NULL, FROM_OPTION},
60  {"no-dereference", no_argument, NULL, 'h'},
61  {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
62  {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
63  {"quiet", no_argument, NULL, 'f'},
64  {"silent", no_argument, NULL, 'f'},
65  {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
66  {"verbose", no_argument, NULL, 'v'},
67  {GETOPT_HELP_OPTION_DECL},
68  {GETOPT_VERSION_OPTION_DECL},
69  {NULL, 0, NULL, 0}
70};
71
72void
73usage (int status)
74{
75  if (status != EXIT_SUCCESS)
76    fprintf (stderr, _("Try `%s --help' for more information.\n"),
77             program_name);
78  else
79    {
80      printf (_("\
81Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
82  or:  %s [OPTION]... --reference=RFILE FILE...\n\
83"),
84              program_name, program_name);
85      fputs (_("\
86Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
87With --reference, change the owner and group of each FILE to those of RFILE.\n\
88\n\
89  -c, --changes          like verbose but report only when a change is made\n\
90      --dereference      affect the referent of each symbolic link (this is\n\
91                         the default), rather than the symbolic link itself\n\
92"), stdout);
93      fputs (_("\
94  -h, --no-dereference   affect each symbolic link instead of any referenced\n\
95                         file (useful only on systems that can change the\n\
96                         ownership of a symlink)\n\
97"), stdout);
98      fputs (_("\
99      --from=CURRENT_OWNER:CURRENT_GROUP\n\
100                         change the owner and/or group of each file only if\n\
101                         its current owner and/or group match those specified\n\
102                         here.  Either may be omitted, in which case a match\n\
103                         is not required for the omitted attribute.\n\
104"), stdout);
105      fputs (_("\
106      --no-preserve-root  do not treat `/' specially (the default)\n\
107      --preserve-root    fail to operate recursively on `/'\n\
108"), stdout);
109      fputs (_("\
110  -f, --silent, --quiet  suppress most error messages\n\
111      --reference=RFILE  use RFILE's owner and group rather than\n\
112                         specifying OWNER:GROUP values\n\
113  -R, --recursive        operate on files and directories recursively\n\
114  -v, --verbose          output a diagnostic for every file processed\n\
115\n\
116"), stdout);
117      fputs (_("\
118The following options modify how a hierarchy is traversed when the -R\n\
119option is also specified.  If more than one is specified, only the final\n\
120one takes effect.\n\
121\n\
122  -H                     if a command line argument is a symbolic link\n\
123                         to a directory, traverse it\n\
124  -L                     traverse every symbolic link to a directory\n\
125                         encountered\n\
126  -P                     do not traverse any symbolic links (default)\n\
127\n\
128"), stdout);
129      fputs (HELP_OPTION_DESCRIPTION, stdout);
130      fputs (VERSION_OPTION_DESCRIPTION, stdout);
131      fputs (_("\
132\n\
133Owner is unchanged if missing.  Group is unchanged if missing, but changed\n\
134to login group if implied by a `:' following a symbolic OWNER.\n\
135OWNER and GROUP may be numeric as well as symbolic.\n\
136"), stdout);
137      printf (_("\
138\n\
139Examples:\n\
140  %s root /u        Change the owner of /u to \"root\".\n\
141  %s root:staff /u  Likewise, but also change its group to \"staff\".\n\
142  %s -hR root /u    Change the owner of /u and subfiles to \"root\".\n\
143"),
144              program_name, program_name, program_name);
145      emit_ancillary_info ();
146    }
147  exit (status);
148}
149
150int
151main (int argc, char **argv)
152{
153  bool preserve_root = false;
154
155  uid_t uid = -1;	/* Specified uid; -1 if not to be changed. */
156  gid_t gid = -1;	/* Specified gid; -1 if not to be changed. */
157
158  /* Change the owner (group) of a file only if it has this uid (gid).
159     -1 means there's no restriction.  */
160  uid_t required_uid = -1;
161  gid_t required_gid = -1;
162
163  /* Bit flags that control how fts works.  */
164  int bit_flags = FTS_PHYSICAL;
165
166  /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
167     specified.  */
168  int dereference = -1;
169
170  struct Chown_option chopt;
171  bool ok;
172  int optc;
173
174  initialize_main (&argc, &argv);
175  set_program_name (argv[0]);
176  setlocale (LC_ALL, "");
177  bindtextdomain (PACKAGE, LOCALEDIR);
178  textdomain (PACKAGE);
179
180  atexit (close_stdout);
181
182  chopt_init (&chopt);
183
184  while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
185         != -1)
186    {
187      switch (optc)
188        {
189        case 'H': /* Traverse command-line symlinks-to-directories.  */
190          bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
191          break;
192
193        case 'L': /* Traverse all symlinks-to-directories.  */
194          bit_flags = FTS_LOGICAL;
195          break;
196
197        case 'P': /* Traverse no symlinks-to-directories.  */
198          bit_flags = FTS_PHYSICAL;
199          break;
200
201        case 'h': /* --no-dereference: affect symlinks */
202          dereference = 0;
203          break;
204
205        case DEREFERENCE_OPTION: /* --dereference: affect the referent
206                                    of each symlink */
207          dereference = 1;
208          break;
209
210        case NO_PRESERVE_ROOT:
211          preserve_root = false;
212          break;
213
214        case PRESERVE_ROOT:
215          preserve_root = true;
216          break;
217
218        case REFERENCE_FILE_OPTION:
219          reference_file = optarg;
220          break;
221
222        case FROM_OPTION:
223          {
224            char *u_dummy, *g_dummy;
225            const char *e = parse_user_spec (optarg,
226                                             &required_uid, &required_gid,
227                                             &u_dummy, &g_dummy);
228            if (e)
229              error (EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
230            break;
231          }
232
233        case 'R':
234          chopt.recurse = true;
235          break;
236
237        case 'c':
238          chopt.verbosity = V_changes_only;
239          break;
240
241        case 'f':
242          chopt.force_silent = true;
243          break;
244
245        case 'v':
246          chopt.verbosity = V_high;
247          break;
248
249        case_GETOPT_HELP_CHAR;
250        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
251        default:
252          usage (EXIT_FAILURE);
253        }
254    }
255
256  if (chopt.recurse)
257    {
258      if (bit_flags == FTS_PHYSICAL)
259        {
260          if (dereference == 1)
261            error (EXIT_FAILURE, 0,
262                   _("-R --dereference requires either -H or -L"));
263          dereference = 0;
264        }
265    }
266  else
267    {
268      bit_flags = FTS_PHYSICAL;
269    }
270  chopt.affect_symlink_referent = (dereference != 0);
271
272  if (argc - optind < (reference_file ? 1 : 2))
273    {
274      if (argc <= optind)
275        error (0, 0, _("missing operand"));
276      else
277        error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
278      usage (EXIT_FAILURE);
279    }
280
281  if (reference_file)
282    {
283      struct stat ref_stats;
284      if (stat (reference_file, &ref_stats))
285        error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
286               quote (reference_file));
287
288      uid = ref_stats.st_uid;
289      gid = ref_stats.st_gid;
290      chopt.user_name = uid_to_name (ref_stats.st_uid);
291      chopt.group_name = gid_to_name (ref_stats.st_gid);
292    }
293  else
294    {
295      const char *e = parse_user_spec (argv[optind], &uid, &gid,
296                                       &chopt.user_name, &chopt.group_name);
297      if (e)
298        error (EXIT_FAILURE, 0, "%s: %s", e, quote (argv[optind]));
299
300      /* If a group is specified but no user, set the user name to the
301         empty string so that diagnostics say "ownership :GROUP"
302         rather than "group GROUP".  */
303      if (!chopt.user_name && chopt.group_name)
304        chopt.user_name = bad_cast ("");
305
306      optind++;
307    }
308
309  if (chopt.recurse && preserve_root)
310    {
311      static struct dev_ino dev_ino_buf;
312      chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
313      if (chopt.root_dev_ino == NULL)
314        error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
315               quote ("/"));
316    }
317
318  bit_flags |= FTS_DEFER_STAT;
319  ok = chown_files (argv + optind, bit_flags,
320                    uid, gid,
321                    required_uid, required_gid, &chopt);
322
323  chopt_free (&chopt);
324
325  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
326}
327