Preparing for release of 3.2.2pre3
[rsync.git] / uidlist.c
blob7359d9dbc973a2f9a56ec532599e2674ad340177
1 /*
2 * Handle the mapping of uid/gid and user/group names between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2020 Wayne Davison
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.
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.
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.
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. */
26 #include "rsync.h"
27 #include "ifuncs.h"
28 #include "itypes.h"
29 #include "io.h"
31 extern int am_root;
32 extern int preserve_uid;
33 extern int preserve_gid;
34 extern int preserve_acls;
35 extern int numeric_ids;
36 extern gid_t our_gid;
37 extern char *usermap;
38 extern char *groupmap;
40 #ifdef HAVE_GETGROUPS
41 # ifndef GETGROUPS_T
42 # define GETGROUPS_T gid_t
43 # endif
44 #endif
46 #define NFLAGS_WILD_NAME_MATCH (1<<0)
47 #define NFLAGS_NAME_MATCH (1<<1)
49 union name_or_id {
50 const char *name;
51 id_t max_id;
54 struct idlist {
55 struct idlist *next;
56 union name_or_id u;
57 id_t id, id2;
58 uint16 flags;
61 static struct idlist *uidlist, *uidmap;
62 static struct idlist *gidlist, *gidmap;
64 static id_t id_parse(const char *num_str)
66 id_t tmp, num = 0;
67 const char *cp = num_str;
69 while (*cp) {
70 if (!isDigit(cp)) {
71 invalid_num:
72 rprintf(FERROR, "Invalid ID number: %s\n", num_str);
73 exit_cleanup(RERR_SYNTAX);
75 tmp = num * 10 + *cp++ - '0';
76 if (tmp < num)
77 goto invalid_num;
78 num = tmp;
81 return num;
84 static struct idlist *add_to_list(struct idlist **root, id_t id, union name_or_id noiu,
85 id_t id2, uint16 flags)
87 struct idlist *node = new(struct idlist);
88 node->next = *root;
89 node->u = noiu;
90 node->id = id;
91 node->id2 = id2;
92 node->flags = flags;
93 *root = node;
94 return node;
97 /* turn a uid into a user name */
98 char *uid_to_user(uid_t uid)
100 struct passwd *pass = getpwuid(uid);
101 if (pass)
102 return strdup(pass->pw_name);
103 return NULL;
106 /* turn a gid into a group name */
107 char *gid_to_group(gid_t gid)
109 struct group *grp = getgrgid(gid);
110 if (grp)
111 return strdup(grp->gr_name);
112 return NULL;
115 /* Parse a user name or (optionally) a number into a uid */
116 int user_to_uid(const char *name, uid_t *uid_p, BOOL num_ok)
118 struct passwd *pass;
119 if (!name || !*name)
120 return 0;
121 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
122 *uid_p = id_parse(name);
123 return 1;
125 if (!(pass = getpwnam(name)))
126 return 0;
127 *uid_p = pass->pw_uid;
128 return 1;
131 /* Parse a group name or (optionally) a number into a gid */
132 int group_to_gid(const char *name, gid_t *gid_p, BOOL num_ok)
134 struct group *grp;
135 if (!name || !*name)
136 return 0;
137 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
138 *gid_p = id_parse(name);
139 return 1;
141 if (!(grp = getgrnam(name)))
142 return 0;
143 *gid_p = grp->gr_gid;
144 return 1;
147 static int is_in_group(gid_t gid)
149 #ifdef HAVE_GETGROUPS
150 static gid_t last_in;
151 static int ngroups = -2, last_out = -1;
152 static GETGROUPS_T *gidset;
153 int n;
155 if (gid == last_in && last_out >= 0)
156 return last_out;
157 if (ngroups < -1) {
158 if ((ngroups = getgroups(0, NULL)) < 0)
159 ngroups = 0;
160 gidset = new_array(GETGROUPS_T, ngroups+1);
161 if (ngroups > 0)
162 ngroups = getgroups(ngroups, gidset);
163 /* The default gid might not be in the list on some systems. */
164 for (n = 0; n < ngroups; n++) {
165 if (gidset[n] == our_gid)
166 break;
168 if (n == ngroups)
169 gidset[ngroups++] = our_gid;
170 if (DEBUG_GTE(OWN, 2)) {
171 int pos;
172 char *gidbuf = new_array(char, ngroups*21+32);
173 pos = snprintf(gidbuf, 32, "process has %d gid%s: ", ngroups, ngroups == 1? "" : "s");
174 for (n = 0; n < ngroups; n++) {
175 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
177 rprintf(FINFO, "%s\n", gidbuf);
178 free(gidbuf);
182 last_in = gid;
183 for (n = 0; n < ngroups; n++) {
184 if (gidset[n] == gid)
185 return last_out = 1;
187 return last_out = 0;
189 #else
190 return gid == our_gid;
191 #endif
194 /* Add a uid/gid to its list of ids. Only called on receiving side. */
195 static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
196 id_t id, const char *name)
198 struct idlist *node;
199 union name_or_id noiu;
200 int flag;
201 id_t id2;
203 noiu.name = name; /* ensure that add_to_list() gets the raw value. */
204 if (!name)
205 name = "";
207 for (node = idmap; node; node = node->next) {
208 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
209 if (!wildmatch(node->u.name, name))
210 continue;
211 } else if (node->flags & NFLAGS_NAME_MATCH) {
212 if (strcmp(node->u.name, name) != 0)
213 continue;
214 } else if (node->u.max_id) {
215 if (id < node->id || id > node->u.max_id)
216 continue;
217 } else {
218 if (node->id != id)
219 continue;
221 break;
223 if (node)
224 id2 = node->id2;
225 else if (*name && id) {
226 if (idlist_ptr == &uidlist) {
227 uid_t uid;
228 id2 = user_to_uid(name, &uid, False) ? uid : id;
229 } else {
230 gid_t gid;
231 id2 = group_to_gid(name, &gid, False) ? gid : id;
233 } else
234 id2 = id;
236 flag = idlist_ptr == &gidlist && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
237 node = add_to_list(idlist_ptr, id, noiu, id2, flag);
239 if (DEBUG_GTE(OWN, 2)) {
240 rprintf(FINFO, "%sid %u(%s) maps to %u\n",
241 idlist_ptr == &uidlist ? "u" : "g",
242 (unsigned)id, name, (unsigned)id2);
245 return node;
248 /* this function is a definite candidate for a faster algorithm */
249 uid_t match_uid(uid_t uid)
251 static struct idlist *last = NULL;
252 struct idlist *list;
254 if (last && uid == last->id)
255 return last->id2;
257 for (list = uidlist; list; list = list->next) {
258 if (list->id == uid)
259 break;
262 if (!list)
263 list = recv_add_id(&uidlist, uidmap, uid, NULL);
264 last = list;
266 return list->id2;
269 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
271 static struct idlist *last = NULL;
272 struct idlist *list;
274 if (last && gid == last->id)
275 list = last;
276 else {
277 for (list = gidlist; list; list = list->next) {
278 if (list->id == gid)
279 break;
281 if (!list)
282 list = recv_add_id(&gidlist, gidmap, gid, NULL);
283 last = list;
286 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
287 *flags_ptr |= FLAG_SKIP_GROUP;
288 return list->id2;
291 /* Add a uid to the list of uids. Only called on sending side. */
292 const char *add_uid(uid_t uid)
294 struct idlist *list;
295 struct idlist *node;
296 union name_or_id noiu;
298 if (uid == 0) /* don't map root */
299 return NULL;
301 for (list = uidlist; list; list = list->next) {
302 if (list->id == uid)
303 return NULL;
306 noiu.name = uid_to_user(uid);
307 node = add_to_list(&uidlist, uid, noiu, 0, 0);
308 return node->u.name;
311 /* Add a gid to the list of gids. Only called on sending side. */
312 const char *add_gid(gid_t gid)
314 struct idlist *list;
315 struct idlist *node;
316 union name_or_id noiu;
318 if (gid == 0) /* don't map root */
319 return NULL;
321 for (list = gidlist; list; list = list->next) {
322 if (list->id == gid)
323 return NULL;
326 noiu.name = gid_to_group(gid);
327 node = add_to_list(&gidlist, gid, noiu, 0, 0);
328 return node->u.name;
331 /* send a complete uid/gid mapping to the peer */
332 void send_id_list(int f)
334 struct idlist *list;
336 if (preserve_uid || preserve_acls) {
337 int len;
338 /* we send sequences of uid/byte-length/name */
339 for (list = uidlist; list; list = list->next) {
340 if (!list->u.name)
341 continue;
342 len = strlen(list->u.name);
343 write_varint30(f, list->id);
344 write_byte(f, len);
345 write_buf(f, list->u.name, len);
348 /* terminate the uid list with a 0 uid. We explicitly exclude
349 * 0 from the list */
350 write_varint30(f, 0);
353 if (preserve_gid || preserve_acls) {
354 int len;
355 for (list = gidlist; list; list = list->next) {
356 if (!list->u.name)
357 continue;
358 len = strlen(list->u.name);
359 write_varint30(f, list->id);
360 write_byte(f, len);
361 write_buf(f, list->u.name, len);
363 write_varint30(f, 0);
367 uid_t recv_user_name(int f, uid_t uid)
369 struct idlist *node;
370 int len = read_byte(f);
371 char *name = new_array(char, len+1);
372 read_sbuf(f, name, len);
373 if (numeric_ids < 0) {
374 free(name);
375 name = NULL;
377 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
378 return node->id2;
381 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
383 struct idlist *node;
384 int len = read_byte(f);
385 char *name = new_array(char, len+1);
386 read_sbuf(f, name, len);
387 if (numeric_ids < 0) {
388 free(name);
389 name = NULL;
391 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
392 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
393 *flags_ptr |= FLAG_SKIP_GROUP;
394 return node->id2;
397 /* recv a complete uid/gid mapping from the peer and map the uid/gid
398 * in the file list to local names */
399 void recv_id_list(int f, struct file_list *flist)
401 id_t id;
402 int i;
404 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
405 /* read the uid list */
406 while ((id = read_varint30(f)) != 0)
407 recv_user_name(f, id);
410 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
411 /* read the gid list */
412 while ((id = read_varint30(f)) != 0)
413 recv_group_name(f, id, NULL);
416 /* Now convert all the uids/gids from sender values to our values. */
417 #ifdef SUPPORT_ACLS
418 if (preserve_acls && (!numeric_ids || usermap || groupmap))
419 match_acl_ids();
420 #endif
421 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
422 for (i = 0; i < flist->used; i++)
423 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
425 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
426 for (i = 0; i < flist->used; i++) {
427 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), &flist->files[i]->flags);
432 void parse_name_map(char *map, BOOL usernames)
434 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
435 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
436 char *colon, *cp = map + strlen(map);
437 union name_or_id noiu;
438 id_t id1;
439 uint16 flags;
441 /* Parse the list in reverse, so the order in the struct is right. */
442 while (1) {
443 while (cp > map && cp[-1] != ',') cp--;
444 if (!(colon = strchr(cp, ':'))) {
445 rprintf(FERROR, "No colon found in --%smap: %s\n",
446 usernames ? "user" : "group", cp);
447 exit_cleanup(RERR_SYNTAX);
449 if (!colon[1]) {
450 rprintf(FERROR, "No name found after colon --%smap: %s\n",
451 usernames ? "user" : "group", cp);
452 exit_cleanup(RERR_SYNTAX);
454 *colon = '\0';
456 if (isDigit(cp)) {
457 char *dash = strchr(cp, '-');
458 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
459 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
460 rprintf(FERROR, "Invalid number in --%smap: %s\n",
461 usernames ? "user" : "group", cp);
462 exit_cleanup(RERR_SYNTAX);
464 if (dash) {
465 *dash = '\0';
466 noiu.max_id = id_parse(dash+1);
467 } else
468 noiu.max_id = 0;
469 flags = 0;
470 id1 = id_parse(cp);
471 if (dash)
472 *dash = '-';
473 } else if (strpbrk(cp, "*[?")) {
474 flags = NFLAGS_WILD_NAME_MATCH;
475 noiu.name = cp;
476 id1 = 0;
477 } else {
478 flags = NFLAGS_NAME_MATCH;
479 noiu.name = cp;
480 id1 = 0;
483 if (usernames) {
484 uid_t uid;
485 if (user_to_uid(colon+1, &uid, True))
486 add_to_list(idmap_ptr, id1, noiu, uid, flags);
487 else {
488 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
490 } else {
491 gid_t gid;
492 if (group_to_gid(colon+1, &gid, True))
493 add_to_list(idmap_ptr, id1, noiu, gid, flags);
494 else {
495 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
499 if (cp == map)
500 break;
502 *--cp = '\0'; /* replace comma */
505 /* The 0 user/group doesn't get its name sent, so add it explicitly. */
506 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : usernames ? uid_to_user(0) : gid_to_group(0));
509 #ifdef HAVE_GETGROUPLIST
510 const char *getallgroups(uid_t uid, item_list *gid_list)
512 struct passwd *pw;
513 gid_t *gid_array;
514 int size;
516 if ((pw = getpwuid(uid)) == NULL)
517 return "getpwuid failed";
519 gid_list->count = 0; /* We're overwriting any items in the list */
520 (void)EXPAND_ITEM_LIST(gid_list, gid_t, 32);
521 size = gid_list->malloced;
523 /* Get all the process's groups, with the pw_gid group first. */
524 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0) {
525 if (size > (int)gid_list->malloced) {
526 gid_list->count = gid_list->malloced;
527 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size);
528 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0)
529 size = -1;
530 } else
531 size = -1;
532 if (size < 0)
533 return "getgrouplist failed";
535 gid_list->count = size;
536 gid_array = gid_list->items;
538 /* Paranoia: is the default group not first in the list? */
539 if (gid_array[0] != pw->pw_gid) {
540 int j;
541 for (j = 1; j < size; j++) {
542 if (gid_array[j] == pw->pw_gid)
543 break;
545 if (j == size) { /* The default group wasn't found! */
546 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size+1);
547 gid_array = gid_list->items;
549 gid_array[j] = gid_array[0];
550 gid_array[0] = pw->pw_gid;
553 return NULL;
555 #endif