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)
332 if ((len = strlen(name)) > 255) /* Impossible? */
335 write_varint30(f, id);
338 write_buf(f, name, len);
341 static void send_one_list(int f, struct idlist *idlist, int usernames)
345 /* we send sequences of id/byte-len/name */
346 for (list = idlist; list; list = list->next) {
347 if (list->id && list->u.name)
348 send_one_name(f, list->id, list->u.name);
351 /* Terminate the uid list with 0 (which was excluded above).
352 * A modern rsync also sends the name of id 0. */
354 send_one_name(f, 0, usernames ? uid_to_user(0) : gid_to_group(0));
356 write_varint30(f, 0);
359 /* send a complete uid/gid mapping to the peer */
360 void send_id_lists(int f)
362 if (preserve_uid || preserve_acls)
363 send_one_list(f, uidlist, 1);
365 if (preserve_gid || preserve_acls)
366 send_one_list(f, gidlist, 0);
369 uid_t recv_user_name(int f, uid_t uid)
372 int len = read_byte(f);
376 name = new_array(char, len+1);
377 read_sbuf(f, name, len);
378 if (numeric_ids < 0) {
385 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
389 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
392 int len = read_byte(f);
393 char *name = new_array(char, len+1);
394 read_sbuf(f, name, len);
395 if (numeric_ids < 0) {
399 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
400 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
401 *flags_ptr |= FLAG_SKIP_GROUP;
405 /* recv a complete uid/gid mapping from the peer and map the uid/gid
406 * in the file list to local names */
407 void recv_id_list(int f, struct file_list *flist)
412 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
413 /* read the uid list */
414 while ((id = read_varint30(f)) != 0)
415 recv_user_name(f, id);
417 recv_user_name(f, 0);
420 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
421 /* read the gid list */
422 while ((id = read_varint30(f)) != 0)
423 recv_group_name(f, id, NULL);
425 recv_group_name(f, 0, NULL);
428 /* Now convert all the uids/gids from sender values to our values. */
430 if (preserve_acls && (!numeric_ids || usermap || groupmap))
433 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
434 for (i = 0; i < flist->used; i++)
435 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
437 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
438 for (i = 0; i < flist->used; i++) {
439 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), &flist->files[i]->flags);
444 void parse_name_map(char *map, BOOL usernames)
446 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
447 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
448 char *colon, *cp = map + strlen(map);
449 union name_or_id noiu;
453 /* Parse the list in reverse, so the order in the struct is right. */
455 while (cp > map && cp[-1] != ',') cp--;
456 if (!(colon = strchr(cp, ':'))) {
457 rprintf(FERROR, "No colon found in --%smap: %s\n",
458 usernames ? "user" : "group", cp);
459 exit_cleanup(RERR_SYNTAX);
462 rprintf(FERROR, "No name found after colon --%smap: %s\n",
463 usernames ? "user" : "group", cp);
464 exit_cleanup(RERR_SYNTAX);
469 char *dash = strchr(cp, '-');
470 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
471 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
472 rprintf(FERROR, "Invalid number in --%smap: %s\n",
473 usernames ? "user" : "group", cp);
474 exit_cleanup(RERR_SYNTAX);
478 noiu.max_id = id_parse(dash+1);
485 } else if (strpbrk(cp, "*[?")) {
486 flags = NFLAGS_WILD_NAME_MATCH;
490 flags = NFLAGS_NAME_MATCH;
497 if (user_to_uid(colon+1, &uid, True))
498 add_to_list(idmap_ptr, id1, noiu, uid, flags);
500 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
504 if (group_to_gid(colon+1, &gid, True))
505 add_to_list(idmap_ptr, id1, noiu, gid, flags);
507 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
514 *--cp = '\0'; /* replace comma */
517 /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
519 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : "root");
522 #ifdef HAVE_GETGROUPLIST
523 const char *getallgroups(uid_t uid, item_list *gid_list)
529 if ((pw = getpwuid(uid)) == NULL)
530 return "getpwuid failed";
532 gid_list->count = 0; /* We're overwriting any items in the list */
533 (void)EXPAND_ITEM_LIST(gid_list, gid_t, 32);
534 size = gid_list->malloced;
536 /* Get all the process's groups, with the pw_gid group first. */
537 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0) {
538 if (size > (int)gid_list->malloced) {
539 gid_list->count = gid_list->malloced;
540 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size);
541 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0)
546 return "getgrouplist failed";
548 gid_list->count = size;
549 gid_array = gid_list->items;
551 /* Paranoia: is the default group not first in the list? */
552 if (gid_array[0] != pw->pw_gid) {
554 for (j = 1; j < size; j++) {
555 if (gid_array[j] == pw->pw_gid)
558 if (j == size) { /* The default group wasn't found! */
559 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size+1);
560 gid_array = gid_list->items;
562 gid_array[j] = gid_array[0];
563 gid_array[0] = pw->pw_gid;