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
32 extern int preserve_uid;
33 extern int preserve_gid;
34 extern int preserve_acls;
35 extern int numeric_ids;
36 extern int xmit_id0_names;
39 extern char *groupmap;
43 # define GETGROUPS_T gid_t
47 #define NFLAGS_WILD_NAME_MATCH (1<<0)
48 #define NFLAGS_NAME_MATCH (1<<1)
62 static struct idlist *uidlist, *uidmap;
63 static struct idlist *gidlist, *gidmap;
65 static id_t id_parse(const char *num_str)
68 const char *cp = num_str;
73 rprintf(FERROR, "Invalid ID number: %s\n", num_str);
74 exit_cleanup(RERR_SYNTAX);
76 tmp = num * 10 + *cp++ - '0';
85 static struct idlist *add_to_list(struct idlist **root, id_t id, union name_or_id noiu,
86 id_t id2, uint16 flags)
88 struct idlist *node = new(struct idlist);
98 /* turn a uid into a user name */
99 const char *uid_to_user(uid_t uid)
101 struct passwd *pass = getpwuid(uid);
103 return strdup(pass->pw_name);
107 /* turn a gid into a group name */
108 const char *gid_to_group(gid_t gid)
110 struct group *grp = getgrgid(gid);
112 return strdup(grp->gr_name);
116 /* Parse a user name or (optionally) a number into a uid */
117 int user_to_uid(const char *name, uid_t *uid_p, BOOL num_ok)
122 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
123 *uid_p = id_parse(name);
126 if (!(pass = getpwnam(name)))
128 *uid_p = pass->pw_uid;
132 /* Parse a group name or (optionally) a number into a gid */
133 int group_to_gid(const char *name, gid_t *gid_p, BOOL num_ok)
138 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
139 *gid_p = id_parse(name);
142 if (!(grp = getgrnam(name)))
144 *gid_p = grp->gr_gid;
148 static int is_in_group(gid_t gid)
150 #ifdef HAVE_GETGROUPS
151 static gid_t last_in;
152 static int ngroups = -2, last_out = -1;
153 static GETGROUPS_T *gidset;
156 if (gid == last_in && last_out >= 0)
159 if ((ngroups = getgroups(0, NULL)) < 0)
161 gidset = new_array(GETGROUPS_T, ngroups+1);
163 ngroups = getgroups(ngroups, gidset);
164 /* The default gid might not be in the list on some systems. */
165 for (n = 0; n < ngroups; n++) {
166 if (gidset[n] == our_gid)
170 gidset[ngroups++] = our_gid;
171 if (DEBUG_GTE(OWN, 2)) {
173 char *gidbuf = new_array(char, ngroups*21+32);
174 pos = snprintf(gidbuf, 32, "process has %d gid%s: ", ngroups, ngroups == 1? "" : "s");
175 for (n = 0; n < ngroups; n++) {
176 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
178 rprintf(FINFO, "%s\n", gidbuf);
184 for (n = 0; n < ngroups; n++) {
185 if (gidset[n] == gid)
191 return gid == our_gid;
195 /* Add a uid/gid to its list of ids. Only called on receiving side. */
196 static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
197 id_t id, const char *name)
200 union name_or_id noiu;
204 noiu.name = name; /* ensure that add_to_list() gets the raw value. */
208 for (node = idmap; node; node = node->next) {
209 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
210 if (!wildmatch(node->u.name, name))
212 } else if (node->flags & NFLAGS_NAME_MATCH) {
213 if (strcmp(node->u.name, name) != 0)
215 } else if (node->u.max_id) {
216 if (id < node->id || id > node->u.max_id)
226 else if (*name && id) {
227 if (idlist_ptr == &uidlist) {
229 id2 = user_to_uid(name, &uid, False) ? uid : id;
232 id2 = group_to_gid(name, &gid, False) ? gid : id;
237 flag = idlist_ptr == &gidlist && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
238 node = add_to_list(idlist_ptr, id, noiu, id2, flag);
240 if (DEBUG_GTE(OWN, 2)) {
241 rprintf(FINFO, "%sid %u(%s) maps to %u\n",
242 idlist_ptr == &uidlist ? "u" : "g",
243 (unsigned)id, name, (unsigned)id2);
249 /* this function is a definite candidate for a faster algorithm */
250 uid_t match_uid(uid_t uid)
252 static struct idlist *last = NULL;
255 if (last && uid == last->id)
258 for (list = uidlist; list; list = list->next) {
264 list = recv_add_id(&uidlist, uidmap, uid, NULL);
270 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
272 static struct idlist *last = NULL;
275 if (last && gid == last->id)
278 for (list = gidlist; list; list = list->next) {
283 list = recv_add_id(&gidlist, gidmap, gid, NULL);
287 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
288 *flags_ptr |= FLAG_SKIP_GROUP;
292 /* Add a uid to the list of uids. Only called on sending side. */
293 const char *add_uid(uid_t uid)
297 union name_or_id noiu;
299 for (list = uidlist; list; list = list->next) {
304 noiu.name = uid_to_user(uid);
305 node = add_to_list(&uidlist, uid, noiu, 0, 0);
309 /* Add a gid to the list of gids. Only called on sending side. */
310 const char *add_gid(gid_t gid)
314 union name_or_id noiu;
316 for (list = gidlist; list; list = list->next) {
321 noiu.name = gid_to_group(gid);
322 node = add_to_list(&gidlist, gid, noiu, 0, 0);
326 static void send_one_name(int f, id_t id, const char *name)
328 int len = strlen(name);
329 if (len > 255) /* Impossible? */
332 write_varint30(f, id);
334 write_buf(f, name, len);
337 static void send_one_list(int f, struct idlist *idlist, int usernames)
341 /* we send sequences of id/byte-len/name */
342 for (list = idlist; list; list = list->next) {
343 if (list->id && list->u.name)
344 send_one_name(f, list->id, list->u.name);
347 /* Terminate the uid list with 0 (which was excluded above).
348 * A modern rsync also sends the name of id 0. */
350 send_one_name(f, 0, usernames ? uid_to_user(0) : gid_to_group(0));
352 write_varint30(f, 0);
355 /* send a complete uid/gid mapping to the peer */
356 void send_id_lists(int f)
358 if (preserve_uid || preserve_acls)
359 send_one_list(f, uidlist, 1);
361 if (preserve_gid || preserve_acls)
362 send_one_list(f, gidlist, 0);
365 uid_t recv_user_name(int f, uid_t uid)
368 int len = read_byte(f);
369 char *name = new_array(char, len+1);
370 read_sbuf(f, name, len);
371 if (numeric_ids < 0) {
375 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
379 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
382 int len = read_byte(f);
383 char *name = new_array(char, len+1);
384 read_sbuf(f, name, len);
385 if (numeric_ids < 0) {
389 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
390 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
391 *flags_ptr |= FLAG_SKIP_GROUP;
395 /* recv a complete uid/gid mapping from the peer and map the uid/gid
396 * in the file list to local names */
397 void recv_id_list(int f, struct file_list *flist)
402 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
403 /* read the uid list */
404 while ((id = read_varint30(f)) != 0)
405 recv_user_name(f, id);
407 recv_user_name(f, 0);
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);
415 recv_group_name(f, 0, NULL);
418 /* Now convert all the uids/gids from sender values to our values. */
420 if (preserve_acls && (!numeric_ids || usermap || groupmap))
423 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
424 for (i = 0; i < flist->used; i++)
425 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
427 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
428 for (i = 0; i < flist->used; i++) {
429 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), &flist->files[i]->flags);
434 void parse_name_map(char *map, BOOL usernames)
436 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
437 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
438 char *colon, *cp = map + strlen(map);
439 union name_or_id noiu;
443 /* Parse the list in reverse, so the order in the struct is right. */
445 while (cp > map && cp[-1] != ',') cp--;
446 if (!(colon = strchr(cp, ':'))) {
447 rprintf(FERROR, "No colon found in --%smap: %s\n",
448 usernames ? "user" : "group", cp);
449 exit_cleanup(RERR_SYNTAX);
452 rprintf(FERROR, "No name found after colon --%smap: %s\n",
453 usernames ? "user" : "group", cp);
454 exit_cleanup(RERR_SYNTAX);
459 char *dash = strchr(cp, '-');
460 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
461 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
462 rprintf(FERROR, "Invalid number in --%smap: %s\n",
463 usernames ? "user" : "group", cp);
464 exit_cleanup(RERR_SYNTAX);
468 noiu.max_id = id_parse(dash+1);
475 } else if (strpbrk(cp, "*[?")) {
476 flags = NFLAGS_WILD_NAME_MATCH;
480 flags = NFLAGS_NAME_MATCH;
487 if (user_to_uid(colon+1, &uid, True))
488 add_to_list(idmap_ptr, id1, noiu, uid, flags);
490 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
494 if (group_to_gid(colon+1, &gid, True))
495 add_to_list(idmap_ptr, id1, noiu, gid, flags);
497 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
504 *--cp = '\0'; /* replace comma */
507 /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
509 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : "root");
512 #ifdef HAVE_GETGROUPLIST
513 const char *getallgroups(uid_t uid, item_list *gid_list)
519 if ((pw = getpwuid(uid)) == NULL)
520 return "getpwuid failed";
522 gid_list->count = 0; /* We're overwriting any items in the list */
523 (void)EXPAND_ITEM_LIST(gid_list, gid_t, 32);
524 size = gid_list->malloced;
526 /* Get all the process's groups, with the pw_gid group first. */
527 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0) {
528 if (size > (int)gid_list->malloced) {
529 gid_list->count = gid_list->malloced;
530 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size);
531 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0)
536 return "getgrouplist failed";
538 gid_list->count = size;
539 gid_array = gid_list->items;
541 /* Paranoia: is the default group not first in the list? */
542 if (gid_array[0] != pw->pw_gid) {
544 for (j = 1; j < size; j++) {
545 if (gid_array[j] == pw->pw_gid)
548 if (j == size) { /* The default group wasn't found! */
549 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size+1);
550 gid_array = gid_list->items;
552 gid_array[j] = gid_array[0];
553 gid_array[0] = pw->pw_gid;