Allow safe_fname() to tweak up to two name at a time.
[rsync.git] / uidlist.c
1 /*
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* handle the mapping of uid/gid and user/group names between systems.
21    If the source username/group does not exist on the target then use
22    the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
23    are special.
24 */
25
26 #include "rsync.h"
27
28 #ifdef HAVE_GETGROUPS
29 # if !defined(GETGROUPS_T)
30 #  define GETGROUPS_T gid_t
31 # endif
32 #endif
33
34 extern int verbose;
35 extern int preserve_uid;
36 extern int preserve_gid;
37 extern int numeric_ids;
38 extern int am_root;
39
40 struct idlist {
41         struct idlist *next;
42         int id, id2;
43         char *name;
44 };
45
46 static struct idlist *uidlist;
47 static struct idlist *gidlist;
48
49 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
50                                   int id2)
51 {
52         struct idlist *node = new(struct idlist);
53         if (!node)
54                 out_of_memory("add_to_list");
55         node->next = *root;
56         node->name = name;
57         node->id = id;
58         node->id2 = id2;
59         *root = node;
60         return node;
61 }
62
63 /* turn a uid into a user name */
64 static char *uid_to_name(uid_t uid)
65 {
66         struct passwd *pass = getpwuid(uid);
67         if (pass)
68                 return strdup(pass->pw_name);
69         return NULL;
70 }
71
72 /* turn a gid into a group name */
73 static char *gid_to_name(gid_t gid)
74 {
75         struct group *grp = getgrgid(gid);
76         if (grp)
77                 return strdup(grp->gr_name);
78         return NULL;
79 }
80
81 static int map_uid(int id, char *name)
82 {
83         uid_t uid;
84         if (id != 0 && name_to_uid(name, &uid))
85                 return uid;
86         return id;
87 }
88
89 static int map_gid(int id, char *name)
90 {
91         gid_t gid;
92         if (id != 0 && name_to_gid(name, &gid))
93                 return gid;
94         return id;
95 }
96
97 static int is_in_group(gid_t gid)
98 {
99 #ifdef HAVE_GETGROUPS
100         static gid_t last_in = GID_NONE, last_out;
101         static int ngroups = -2;
102         static GETGROUPS_T *gidset;
103         int n;
104
105         if (gid == last_in)
106                 return last_out;
107         if (ngroups < -1) {
108                 gid_t mygid = MY_GID();
109                 if ((ngroups = getgroups(0, NULL)) < 0)
110                         ngroups = 0;
111                 gidset = new_array(GETGROUPS_T, ngroups+1);
112                 if (!gidset)
113                         out_of_memory("is_in_group");
114                 if (ngroups > 0)
115                         ngroups = getgroups(ngroups, gidset);
116                 /* The default gid might not be in the list on some systems. */
117                 for (n = 0; n < ngroups; n++) {
118                         if (gidset[n] == mygid)
119                                 break;
120                 }
121                 if (n == ngroups)
122                         gidset[ngroups++] = mygid;
123                 if (verbose > 3) {
124                         int pos;
125                         char *gidbuf = new_array(char, ngroups*21+32);
126                         if (!gidbuf)
127                                 out_of_memory("is_in_group");
128                         sprintf(gidbuf, "process has %d gid%s: ",
129                             ngroups, ngroups == 1? "" : "s");
130                         pos = strlen(gidbuf);
131                         for (n = 0; n < ngroups; n++) {
132                                 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
133                                 pos += strlen(gidbuf+pos);
134                         }
135                         rprintf(FINFO, "%s\n", gidbuf);
136                         free(gidbuf);
137                 }
138         }
139
140         last_in = gid;
141         for (n = 0; n < ngroups; n++) {
142                 if (gidset[n] == gid)
143                         return last_out = 1;
144         }
145         return last_out = 0;
146
147 #else
148         static gid_t mygid = GID_NONE;
149         if (mygid == GID_NONE) {
150                 mygid = MY_GID();
151                 if (verbose > 3)
152                         rprintf(FINFO, "process has gid %d\n", (int)mygid);
153         }
154         return gid == mygid;
155 #endif
156 }
157
158 /* Add a uid to the list of uids.  Only called on receiving side. */
159 static struct idlist *recv_add_uid(int id, char *name)
160 {
161         int id2 = name ? map_uid(id, name) : id;
162         struct idlist *node;
163
164         node = add_to_list(&uidlist, id, name, id2);
165
166         if (verbose > 3) {
167                 rprintf(FINFO, "uid %d(%s) maps to %d\n",
168                     id, name ? name : "", id2);
169         }
170
171         return node;
172 }
173
174 /* Add a gid to the list of gids.  Only called on receiving side. */
175 static struct idlist *recv_add_gid(int id, char *name)
176 {
177         int id2 = name ? map_gid(id, name) : id;
178         struct idlist *node;
179
180         if (!am_root && !is_in_group(id2))
181                 id2 = GID_NONE;
182         node = add_to_list(&gidlist, id, name, id2);
183
184         if (verbose > 3) {
185                 rprintf(FINFO, "gid %d(%s) maps to %d\n",
186                     id, name ? name : "", id2);
187         }
188
189         return node;
190 }
191
192 /* this function is a definate candidate for a faster algorithm */
193 static uid_t match_uid(uid_t uid)
194 {
195         static uid_t last_in, last_out;
196         struct idlist *list;
197
198         if (uid == 0)
199                 return 0;
200
201         if (uid == last_in)
202                 return last_out;
203
204         last_in = uid;
205
206         for (list = uidlist; list; list = list->next) {
207                 if (list->id == (int)uid)
208                         return last_out = (uid_t)list->id2;
209         }
210
211         return last_out = uid;
212 }
213
214 static gid_t match_gid(gid_t gid)
215 {
216         static gid_t last_in = GID_NONE, last_out = GID_NONE;
217         struct idlist *list;
218
219         if (gid == GID_NONE)
220                 return GID_NONE;
221
222         if (gid == last_in)
223                 return last_out;
224
225         last_in = gid;
226
227         for (list = gidlist; list; list = list->next) {
228                 if (list->id == (int)gid)
229                         return last_out = (gid_t)list->id2;
230         }
231
232         list = recv_add_gid(gid, NULL);
233         return last_out = list->id2;
234 }
235
236 /* Add a uid to the list of uids.  Only called on sending side. */
237 void add_uid(uid_t uid)
238 {
239         struct idlist *list;
240
241         if (uid == 0)   /* don't map root */
242                 return;
243
244         for (list = uidlist; list; list = list->next) {
245                 if (list->id == (int)uid)
246                         return;
247         }
248
249         add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
250 }
251
252 /* Add a gid to the list of gids.  Only called on sending side. */
253 void add_gid(gid_t gid)
254 {
255         struct idlist *list;
256
257         if (gid == 0)   /* don't map root */
258                 return;
259
260         for (list = gidlist; list; list = list->next) {
261                 if (list->id == (int)gid)
262                         return;
263         }
264
265         add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
266 }
267
268
269 /* send a complete uid/gid mapping to the peer */
270 void send_uid_list(int f)
271 {
272         struct idlist *list;
273
274         if (numeric_ids)
275                 return;
276
277         if (preserve_uid) {
278                 int len;
279                 /* we send sequences of uid/byte-length/name */
280                 for (list = uidlist; list; list = list->next) {
281                         if (!list->name)
282                                 continue;
283                         len = strlen(list->name);
284                         write_int(f, list->id);
285                         write_byte(f, len);
286                         write_buf(f, list->name, len);
287                 }
288
289                 /* terminate the uid list with a 0 uid. We explicitly exclude
290                  * 0 from the list */
291                 write_int(f, 0);
292         }
293
294         if (preserve_gid) {
295                 int len;
296                 for (list = gidlist; list; list = list->next) {
297                         if (!list->name)
298                                 continue;
299                         len = strlen(list->name);
300                         write_int(f, list->id);
301                         write_byte(f, len);
302                         write_buf(f, list->name, len);
303                 }
304                 write_int(f, 0);
305         }
306 }
307
308 /* recv a complete uid/gid mapping from the peer and map the uid/gid
309  * in the file list to local names */
310 void recv_uid_list(int f, struct file_list *flist)
311 {
312         int id, i;
313         char *name;
314
315         if (preserve_uid && !numeric_ids) {
316                 /* read the uid list */
317                 while ((id = read_int(f)) != 0) {
318                         int len = read_byte(f);
319                         name = new_array(char, len+1);
320                         if (!name)
321                                 out_of_memory("recv_uid_list");
322                         read_sbuf(f, name, len);
323                         recv_add_uid(id, name); /* node keeps name's memory */
324                 }
325         }
326
327
328         if (preserve_gid && !numeric_ids) {
329                 /* read the gid list */
330                 while ((id = read_int(f)) != 0) {
331                         int len = read_byte(f);
332                         name = new_array(char, len+1);
333                         if (!name)
334                                 out_of_memory("recv_uid_list");
335                         read_sbuf(f, name, len);
336                         recv_add_gid(id, name); /* node keeps name's memory */
337                 }
338         }
339
340         /* now convert the uid/gid of all files in the list to the mapped
341          * uid/gid */
342         if (am_root && preserve_uid && !numeric_ids) {
343                 for (i = 0; i < flist->count; i++)
344                         flist->files[i]->uid = match_uid(flist->files[i]->uid);
345         }
346         if (preserve_gid && (!am_root || !numeric_ids)) {
347                 for (i = 0; i < flist->count; i++)
348                         flist->files[i]->gid = match_gid(flist->files[i]->gid);
349         }
350 }