Initial commit for Tizen
[profile/extras/shadow-utils.git] / libmisc / chowndir.c
1 /*
2  * Copyright (c) 1992 - 1993, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
30  */
31
32 #include <config.h>
33
34 #ident "$Id: chowndir.c 2020 2008-05-25 21:23:28Z nekral-guest $"
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include "prototypes.h"
39 #include "defines.h"
40 #include <fcntl.h>
41 #include <stdio.h>
42 /*
43  * chown_tree - change ownership of files in a directory tree
44  *
45  *      chown_dir() walks a directory tree and changes the ownership
46  *      of all files owned by the provided user ID.
47  */
48 int
49 chown_tree (const char *root, uid_t old_uid, uid_t new_uid, gid_t old_gid,
50             gid_t new_gid)
51 {
52         char new_name[1024];
53         int rc = 0;
54         struct DIRECT *ent;
55         struct stat sb;
56         DIR *dir;
57
58         /*
59          * Make certain the directory exists.  This routine is called
60          * directory by the invoker, or recursively.
61          */
62
63         if (access (root, F_OK) != 0)
64                 return -1;
65
66         /*
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
70          * old user ID.
71          */
72
73         if (!(dir = opendir (root)))
74                 return -1;
75
76         while ((ent = readdir (dir))) {
77
78                 /*
79                  * Skip the "." and ".." entries
80                  */
81
82                 if (strcmp (ent->d_name, ".") == 0 ||
83                     strcmp (ent->d_name, "..") == 0)
84                         continue;
85
86                 /*
87                  * Make the filename for both the source and the
88                  * destination files.
89                  */
90
91                 if (strlen (root) + strlen (ent->d_name) + 2 > sizeof new_name)
92                         break;
93
94                 snprintf (new_name, sizeof new_name, "%s/%s", root,
95                           ent->d_name);
96
97                 /* Don't follow symbolic links! */
98                 if (LSTAT (new_name, &sb) == -1)
99                         continue;
100
101                 if (S_ISDIR (sb.st_mode) && !S_ISLNK (sb.st_mode)) {
102
103                         /*
104                          * Do the entire subdirectory.
105                          */
106
107                         rc = chown_tree (new_name, old_uid, new_uid,
108                                          old_gid, new_gid);
109                         if (0 != rc) {
110                                 break;
111                         }
112                 }
113 #ifndef HAVE_LCHOWN
114                 /* don't use chown (follows symbolic links!) */
115                 if (S_ISLNK (sb.st_mode))
116                         continue;
117 #endif
118                 if (sb.st_uid == old_uid)
119                         LCHOWN (new_name, new_uid,
120                                 sb.st_gid == old_gid ? new_gid : sb.st_gid);
121         }
122         (void) closedir (dir);
123
124         /*
125          * Now do the root of the tree
126          */
127
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);
132                 }
133         }
134         return rc;
135 }
136