Put file descriptor arg at the start of the arg list for consistency.
[rsync/rsync.git] / uidlist.c
... / ...
CommitLineData
1/*
2 * Handle the mapping of uid/gid and user/group names between systems.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2009 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22/* If the source username/group does not exist on the target then use
23 * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
24 * are special. */
25
26#include "rsync.h"
27#include "ifuncs.h"
28#include "itypes.h"
29#include "io.h"
30
31extern int am_root;
32extern int preserve_uid;
33extern int preserve_gid;
34extern int preserve_acls;
35extern int numeric_ids;
36extern gid_t our_gid;
37extern char *usermap;
38extern char *groupmap;
39
40#ifdef HAVE_GETGROUPS
41# ifndef GETGROUPS_T
42# define GETGROUPS_T gid_t
43# endif
44#endif
45
46#define GID_NONE ((gid_t)-1)
47
48#define NFLAGS_WILD_NAME_MATCH (1<<0)
49#define NFLAGS_NAME_MATCH (1<<1)
50
51struct idlist {
52 struct idlist *next;
53 const char *name;
54 id_t id, id2;
55 uint16 flags;
56};
57
58static struct idlist *uidlist, *uidmap;
59static struct idlist *gidlist, *gidmap;
60
61static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
62 id_t id2, uint16 flags)
63{
64 struct idlist *node = new(struct idlist);
65 if (!node)
66 out_of_memory("add_to_list");
67 node->next = *root;
68 node->name = name;
69 node->id = id;
70 node->id2 = id2;
71 node->flags = flags;
72 *root = node;
73 return node;
74}
75
76/* turn a uid into a user name */
77static const char *uid_to_name(uid_t uid)
78{
79 struct passwd *pass = getpwuid(uid);
80 if (pass)
81 return strdup(pass->pw_name);
82 return NULL;
83}
84
85/* turn a gid into a group name */
86static const char *gid_to_name(gid_t gid)
87{
88 struct group *grp = getgrgid(gid);
89 if (grp)
90 return strdup(grp->gr_name);
91 return NULL;
92}
93
94static int is_in_group(gid_t gid)
95{
96#ifdef HAVE_GETGROUPS
97 static gid_t last_in = GID_NONE, last_out;
98 static int ngroups = -2;
99 static GETGROUPS_T *gidset;
100 int n;
101
102 if (gid == last_in)
103 return last_out;
104 if (ngroups < -1) {
105 if ((ngroups = getgroups(0, NULL)) < 0)
106 ngroups = 0;
107 gidset = new_array(GETGROUPS_T, ngroups+1);
108 if (!gidset)
109 out_of_memory("is_in_group");
110 if (ngroups > 0)
111 ngroups = getgroups(ngroups, gidset);
112 /* The default gid might not be in the list on some systems. */
113 for (n = 0; n < ngroups; n++) {
114 if (gidset[n] == our_gid)
115 break;
116 }
117 if (n == ngroups)
118 gidset[ngroups++] = our_gid;
119 if (DEBUG_GTE(OWN, 2)) {
120 int pos;
121 char *gidbuf = new_array(char, ngroups*21+32);
122 if (!gidbuf)
123 out_of_memory("is_in_group");
124 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
125 ngroups, ngroups == 1? "" : "s");
126 for (n = 0; n < ngroups; n++) {
127 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
128 }
129 rprintf(FINFO, "%s\n", gidbuf);
130 free(gidbuf);
131 }
132 }
133
134 last_in = gid;
135 for (n = 0; n < ngroups; n++) {
136 if (gidset[n] == gid)
137 return last_out = 1;
138 }
139 return last_out = 0;
140
141#else
142 return gid == our_gid;
143#endif
144}
145
146/* Add a uid/gid to its list of ids. Only called on receiving side. */
147static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
148 id_t id, const char *name)
149{
150 struct idlist *node;
151 int flag;
152 id_t id2;
153
154 if (!name)
155 name = "";
156
157 for (node = idmap; node; node = node->next) {
158 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
159 if (!wildmatch(node->name, name))
160 continue;
161 } else if (node->flags & NFLAGS_NAME_MATCH) {
162 if (strcmp(node->name, name) != 0)
163 continue;
164 } else if (node->name) {
165 if (id < node->id || id > (unsigned long)node->name)
166 continue;
167 } else {
168 if (node->id != id)
169 continue;
170 }
171 break;
172 }
173 if (node)
174 id2 = node->id2;
175 else if (*name && id) {
176 if (idlist_ptr == &uidlist) {
177 uid_t uid;
178 id2 = user_to_uid(name, &uid, False) ? uid : id;
179 } else {
180 gid_t gid;
181 id2 = group_to_gid(name, &gid, False) ? gid : id;
182 }
183 } else
184 id2 = id;
185
186 flag = idlist_ptr == &gidlist && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
187 node = add_to_list(idlist_ptr, id, *name ? name : NULL, id2, flag);
188
189 if (DEBUG_GTE(OWN, 2)) {
190 rprintf(FINFO, "%sid %u(%s) maps to %u\n",
191 idlist_ptr == &uidlist ? "u" : "g",
192 (unsigned)id, name, (unsigned)id2);
193 }
194
195 return node;
196}
197
198/* this function is a definate candidate for a faster algorithm */
199uid_t match_uid(uid_t uid)
200{
201 static uid_t last_in = -1, last_out = -1;
202 struct idlist *list;
203
204 if (uid == last_in)
205 return last_out;
206
207 last_in = uid;
208
209 for (list = uidlist; list; list = list->next) {
210 if (list->id == uid)
211 break;
212 }
213
214 if (!list)
215 list = recv_add_id(&uidlist, uidmap, uid, NULL);
216
217 return last_out = list->id2;
218}
219
220gid_t match_gid(gid_t gid, uint16 *flags_ptr)
221{
222 static struct idlist *last = NULL;
223 struct idlist *list;
224
225 if (last && gid == last->id)
226 list = last;
227 else {
228 for (list = gidlist; list; list = list->next) {
229 if (list->id == gid)
230 break;
231 }
232 if (!list)
233 list = recv_add_id(&gidlist, gidmap, gid, NULL);
234 last = list;
235 }
236
237 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
238 *flags_ptr |= FLAG_SKIP_GROUP;
239 return list->id2;
240}
241
242/* Add a uid to the list of uids. Only called on sending side. */
243const char *add_uid(uid_t uid)
244{
245 struct idlist *list;
246 struct idlist *node;
247
248 if (uid == 0) /* don't map root */
249 return NULL;
250
251 for (list = uidlist; list; list = list->next) {
252 if (list->id == uid)
253 return NULL;
254 }
255
256 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
257 return node->name;
258}
259
260/* Add a gid to the list of gids. Only called on sending side. */
261const char *add_gid(gid_t gid)
262{
263 struct idlist *list;
264 struct idlist *node;
265
266 if (gid == 0) /* don't map root */
267 return NULL;
268
269 for (list = gidlist; list; list = list->next) {
270 if (list->id == gid)
271 return NULL;
272 }
273
274 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
275 return node->name;
276}
277
278/* send a complete uid/gid mapping to the peer */
279void send_id_list(int f)
280{
281 struct idlist *list;
282
283 if (preserve_uid || preserve_acls) {
284 int len;
285 /* we send sequences of uid/byte-length/name */
286 for (list = uidlist; list; list = list->next) {
287 if (!list->name)
288 continue;
289 len = strlen(list->name);
290 write_varint30(f, list->id);
291 write_byte(f, len);
292 write_buf(f, list->name, len);
293 }
294
295 /* terminate the uid list with a 0 uid. We explicitly exclude
296 * 0 from the list */
297 write_varint30(f, 0);
298 }
299
300 if (preserve_gid || preserve_acls) {
301 int len;
302 for (list = gidlist; list; list = list->next) {
303 if (!list->name)
304 continue;
305 len = strlen(list->name);
306 write_varint30(f, list->id);
307 write_byte(f, len);
308 write_buf(f, list->name, len);
309 }
310 write_varint30(f, 0);
311 }
312}
313
314uid_t recv_user_name(int f, uid_t uid)
315{
316 struct idlist *node;
317 int len = read_byte(f);
318 char *name = new_array(char, len+1);
319 if (!name)
320 out_of_memory("recv_user_name");
321 read_sbuf(f, name, len);
322 if (numeric_ids < 0) {
323 free(name);
324 name = NULL;
325 }
326 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
327 return node->id2;
328}
329
330gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
331{
332 struct idlist *node;
333 int len = read_byte(f);
334 char *name = new_array(char, len+1);
335 if (!name)
336 out_of_memory("recv_group_name");
337 read_sbuf(f, name, len);
338 if (numeric_ids < 0) {
339 free(name);
340 name = NULL;
341 }
342 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
343 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
344 *flags_ptr |= FLAG_SKIP_GROUP;
345 return node->id2;
346}
347
348/* recv a complete uid/gid mapping from the peer and map the uid/gid
349 * in the file list to local names */
350void recv_id_list(int f, struct file_list *flist)
351{
352 id_t id;
353 int i;
354
355 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
356 /* read the uid list */
357 while ((id = read_varint30(f)) != 0)
358 recv_user_name(f, id);
359 }
360
361 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
362 /* read the gid list */
363 while ((id = read_varint30(f)) != 0)
364 recv_group_name(f, id, NULL);
365 }
366
367 /* Now convert all the uids/gids from sender values to our values. */
368#ifdef SUPPORT_ACLS
369 if (preserve_acls && (!numeric_ids || usermap || groupmap))
370 match_acl_ids();
371#endif
372 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
373 for (i = 0; i < flist->used; i++)
374 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
375 }
376 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
377 for (i = 0; i < flist->used; i++) {
378 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
379 &flist->files[i]->flags);
380 }
381 }
382}
383
384void parse_name_map(char *map, BOOL usernames)
385{
386 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
387 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
388 char *colon, *end, *name, *cp = map + strlen(map);
389 id_t id1;
390 uint16 flags;
391
392 /* Parse the list in reverse, so the order in the struct is right. */
393 while (1) {
394 end = cp;
395 while (cp > map && cp[-1] != ',') cp--;
396 if (!(colon = strchr(cp, ':'))) {
397 rprintf(FERROR, "No colon found in --%smap: %s\n",
398 usernames ? "user" : "group", cp);
399 exit_cleanup(RERR_SYNTAX);
400 }
401 if (!colon[1]) {
402 rprintf(FERROR, "No name found after colon --%smap: %s\n",
403 usernames ? "user" : "group", cp);
404 exit_cleanup(RERR_SYNTAX);
405 }
406 *colon = '\0';
407
408 if (isDigit(cp)) {
409 char *dash = strchr(cp, '-');
410 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
411 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
412 rprintf(FERROR, "Invalid number in --%smap: %s\n",
413 usernames ? "user" : "group", cp);
414 exit_cleanup(RERR_SYNTAX);
415 }
416 if (dash)
417 name = (char *)atol(dash+1);
418 else
419 name = (char *)0;
420 flags = 0;
421 id1 = atol(cp);
422 } else if (strpbrk(cp, "*[?")) {
423 flags = NFLAGS_WILD_NAME_MATCH;
424 name = cp;
425 id1 = 0;
426 } else {
427 flags = NFLAGS_NAME_MATCH;
428 name = cp;
429 id1 = 0;
430 }
431
432 if (usernames) {
433 uid_t uid;
434 if (user_to_uid(colon+1, &uid, True))
435 add_to_list(idmap_ptr, id1, name, uid, flags);
436 else {
437 rprintf(FERROR,
438 "Unknown --usermap name on receiver: %s\n",
439 colon+1);
440 }
441 } else {
442 gid_t gid;
443 if (group_to_gid(colon+1, &gid, True))
444 add_to_list(idmap_ptr, id1, name, gid, flags);
445 else {
446 rprintf(FERROR,
447 "Unknown --groupmap name on receiver: %s\n",
448 colon+1);
449 }
450 }
451
452 if (cp == map)
453 break;
454
455 *--cp = '\0'; /* replace comma */
456 }
457
458 /* The 0 user/group doesn't get its name sent, so add it explicitly. */
459 recv_add_id(idlist_ptr, *idmap_ptr, 0,
460 numeric_ids ? NULL : usernames ? uid_to_name(0) : gid_to_name(0));
461}