2 * Copyright (c) 1992 - 1993, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the copyright holders or contributors may not be used to
16 * endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
38 #include "prototypes.h"
43 * chown_tree - change ownership of files in a directory tree
45 * chown_dir() walks a directory tree and changes the ownership
46 * of all files owned by the provided user ID.
49 chown_tree (const char *root, uid_t old_uid, uid_t new_uid, gid_t old_gid,
59 * Make certain the directory exists. This routine is called
60 * directory by the invoker, or recursively.
63 if (access (root, F_OK) != 0)
67 * Open the directory and read each entry. Every entry is tested
68 * to see if it is a directory, and if so this routine is called
69 * recursively. If not, it is checked to see if it is owned by
73 if (!(dir = opendir (root)))
76 while ((ent = readdir (dir))) {
79 * Skip the "." and ".." entries
82 if (strcmp (ent->d_name, ".") == 0 ||
83 strcmp (ent->d_name, "..") == 0)
87 * Make the filename for both the source and the
91 if (strlen (root) + strlen (ent->d_name) + 2 > sizeof new_name)
94 snprintf (new_name, sizeof new_name, "%s/%s", root,
97 /* Don't follow symbolic links! */
98 if (LSTAT (new_name, &sb) == -1)
101 if (S_ISDIR (sb.st_mode) && !S_ISLNK (sb.st_mode)) {
104 * Do the entire subdirectory.
107 rc = chown_tree (new_name, old_uid, new_uid,
114 /* don't use chown (follows symbolic links!) */
115 if (S_ISLNK (sb.st_mode))
118 if (sb.st_uid == old_uid)
119 LCHOWN (new_name, new_uid,
120 sb.st_gid == old_gid ? new_gid : sb.st_gid);
122 (void) closedir (dir);
125 * Now do the root of the tree
128 if (stat (root, &sb) == 0) {
129 if (sb.st_uid == old_uid) {
130 LCHOWN (root, new_uid,
131 sb.st_gid == old_gid ? new_gid : sb.st_gid);