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;
37 extern pid_t namecvt_pid;
40 extern char *groupmap;
44 # define GETGROUPS_T gid_t
48 #define NFLAGS_WILD_NAME_MATCH (1<<0)
49 #define NFLAGS_NAME_MATCH (1<<1)
63 static struct idlist *uidlist, *uidmap;
64 static struct idlist *gidlist, *gidmap;
66 static inline int id_eq_uid(id_t id, uid_t uid)
68 return id == (id_t)uid;
71 static inline int id_eq_gid(id_t id, gid_t gid)
73 return id == (id_t)gid;
76 static id_t id_parse(const char *num_str)
79 const char *cp = num_str;
84 rprintf(FERROR, "Invalid ID number: %s\n", num_str);
85 exit_cleanup(RERR_SYNTAX);
87 tmp = num * 10 + *cp++ - '0';
96 static struct idlist *add_to_list(struct idlist **root, id_t id, union name_or_id noiu,
97 id_t id2, uint16 flags)
99 struct idlist *node = new(struct idlist);
109 /* turn a uid into a user name */
110 const char *uid_to_user(uid_t uid)
112 const char *name = NULL;
116 namecvt_call("uid", &name, &id);
118 struct passwd *pass = getpwuid(uid);
120 name = strdup(pass->pw_name);
126 /* turn a gid into a group name */
127 const char *gid_to_group(gid_t gid)
129 const char *name = NULL;
133 namecvt_call("gid", &name, &id);
135 struct group *grp = getgrgid(gid);
137 name = strdup(grp->gr_name);
143 /* Parse a user name or (optionally) a number into a uid */
144 int user_to_uid(const char *name, uid_t *uid_p, BOOL num_ok)
149 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
150 *uid_p = id_parse(name);
156 if (!namecvt_call("usr", &name, &id))
160 struct passwd *pass = getpwnam(name);
163 *uid_p = pass->pw_uid;
169 /* Parse a group name or (optionally) a number into a gid */
170 int group_to_gid(const char *name, gid_t *gid_p, BOOL num_ok)
175 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
176 *gid_p = id_parse(name);
182 if (!namecvt_call("grp", &name, &id))
186 struct group *grp = getgrnam(name);
189 *gid_p = grp->gr_gid;
195 static int is_in_group(gid_t gid)
197 #ifdef HAVE_GETGROUPS
198 static gid_t last_in;
199 static int ngroups = -2, last_out = -1;
200 static GETGROUPS_T *gidset;
203 if (gid == last_in && last_out >= 0)
206 if ((ngroups = getgroups(0, NULL)) < 0)
208 gidset = new_array(GETGROUPS_T, ngroups+1);
210 ngroups = getgroups(ngroups, gidset);
211 /* The default gid might not be in the list on some systems. */
212 for (n = 0; n < ngroups; n++) {
213 if (gidset[n] == our_gid)
217 gidset[ngroups++] = our_gid;
218 if (DEBUG_GTE(OWN, 2)) {
220 char *gidbuf = new_array(char, ngroups*21+32);
221 pos = snprintf(gidbuf, 32, "process has %d gid%s: ", ngroups, ngroups == 1? "" : "s");
222 for (n = 0; n < ngroups; n++) {
223 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
225 rprintf(FINFO, "%s\n", gidbuf);
231 for (n = 0; n < ngroups; n++) {
232 if (gidset[n] == gid)
238 return gid == our_gid;
242 /* Add a uid/gid to its list of ids. Only called on receiving side. */
243 static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
244 id_t id, const char *name)
247 union name_or_id noiu;
251 noiu.name = name; /* ensure that add_to_list() gets the raw value. */
255 for (node = idmap; node; node = node->next) {
256 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
257 if (!wildmatch(node->u.name, name))
259 } else if (node->flags & NFLAGS_NAME_MATCH) {
260 if (strcmp(node->u.name, name) != 0)
262 } else if (node->u.max_id) {
263 if (id < node->id || id > node->u.max_id)
273 else if (*name && id) {
274 if (idlist_ptr == &uidlist) {
276 id2 = user_to_uid(name, &uid, False) ? (id_t)uid : id;
279 id2 = group_to_gid(name, &gid, False) ? (id_t)gid : id;
284 flag = idlist_ptr == &gidlist && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
285 node = add_to_list(idlist_ptr, id, noiu, id2, flag);
287 if (DEBUG_GTE(OWN, 2)) {
288 rprintf(FINFO, "%sid %u(%s) maps to %u\n",
289 idlist_ptr == &uidlist ? "u" : "g",
290 (unsigned)id, name, (unsigned)id2);
296 /* this function is a definite candidate for a faster algorithm */
297 uid_t match_uid(uid_t uid)
299 static struct idlist *last = NULL;
302 if (last && id_eq_uid(last->id, uid))
305 for (list = uidlist; list; list = list->next) {
306 if (id_eq_uid(list->id, uid))
311 list = recv_add_id(&uidlist, uidmap, uid, NULL);
317 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
319 static struct idlist *last = NULL;
322 if (last && id_eq_gid(last->id, gid))
325 for (list = gidlist; list; list = list->next) {
326 if (id_eq_gid(list->id, gid))
330 list = recv_add_id(&gidlist, gidmap, gid, NULL);
334 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
335 *flags_ptr |= FLAG_SKIP_GROUP;
339 /* Add a uid to the list of uids. Only called on sending side. */
340 const char *add_uid(uid_t uid)
344 union name_or_id noiu;
346 for (list = uidlist; list; list = list->next) {
347 if (id_eq_uid(list->id, uid))
351 noiu.name = uid_to_user(uid);
352 node = add_to_list(&uidlist, uid, noiu, 0, 0);
356 /* Add a gid to the list of gids. Only called on sending side. */
357 const char *add_gid(gid_t gid)
361 union name_or_id noiu;
363 for (list = gidlist; list; list = list->next) {
364 if (id_eq_gid(list->id, gid))
368 noiu.name = gid_to_group(gid);
369 node = add_to_list(&gidlist, gid, noiu, 0, 0);
373 static void send_one_name(int f, id_t id, const char *name)
379 if ((len = strlen(name)) > 255) /* Impossible? */
382 write_varint30(f, id);
385 write_buf(f, name, len);
388 static void send_one_list(int f, struct idlist *idlist, int usernames)
392 /* we send sequences of id/byte-len/name */
393 for (list = idlist; list; list = list->next) {
394 if (list->id && list->u.name)
395 send_one_name(f, list->id, list->u.name);
398 /* Terminate the uid list with 0 (which was excluded above).
399 * A modern rsync also sends the name of id 0. */
401 send_one_name(f, 0, usernames ? uid_to_user(0) : gid_to_group(0));
403 write_varint30(f, 0);
406 /* send a complete uid/gid mapping to the peer */
407 void send_id_lists(int f)
409 if (preserve_uid || preserve_acls)
410 send_one_list(f, uidlist, 1);
412 if (preserve_gid || preserve_acls)
413 send_one_list(f, gidlist, 0);
416 uid_t recv_user_name(int f, uid_t uid)
419 int len = read_byte(f);
423 name = new_array(char, len+1);
424 read_sbuf(f, name, len);
425 if (numeric_ids < 0) {
432 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
436 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
439 int len = read_byte(f);
443 name = new_array(char, len+1);
444 read_sbuf(f, name, len);
445 if (numeric_ids < 0) {
452 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
453 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
454 *flags_ptr |= FLAG_SKIP_GROUP;
458 /* recv a complete uid/gid mapping from the peer and map the uid/gid
459 * in the file list to local names */
460 void recv_id_list(int f, struct file_list *flist)
465 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
466 /* read the uid list */
467 while ((id = read_varint30(f)) != 0)
468 recv_user_name(f, id);
470 recv_user_name(f, 0);
473 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
474 /* read the gid list */
475 while ((id = read_varint30(f)) != 0)
476 recv_group_name(f, id, NULL);
478 recv_group_name(f, 0, NULL);
481 /* Now convert all the uids/gids from sender values to our values. */
483 if (preserve_acls && (!numeric_ids || usermap || groupmap))
486 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
487 for (i = 0; i < flist->used; i++)
488 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
490 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
491 for (i = 0; i < flist->used; i++) {
492 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), &flist->files[i]->flags);
497 void parse_name_map(char *map, BOOL usernames)
499 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
500 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
501 char *colon, *cp = map + strlen(map);
502 union name_or_id noiu;
506 /* Parse the list in reverse, so the order in the struct is right. */
508 while (cp > map && cp[-1] != ',') cp--;
509 if (!(colon = strchr(cp, ':'))) {
510 rprintf(FERROR, "No colon found in --%smap: %s\n",
511 usernames ? "user" : "group", cp);
512 exit_cleanup(RERR_SYNTAX);
515 rprintf(FERROR, "No name found after colon --%smap: %s\n",
516 usernames ? "user" : "group", cp);
517 exit_cleanup(RERR_SYNTAX);
522 char *dash = strchr(cp, '-');
523 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
524 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
525 rprintf(FERROR, "Invalid number in --%smap: %s\n",
526 usernames ? "user" : "group", cp);
527 exit_cleanup(RERR_SYNTAX);
531 noiu.max_id = id_parse(dash+1);
538 } else if (strpbrk(cp, "*[?")) {
539 flags = NFLAGS_WILD_NAME_MATCH;
543 flags = NFLAGS_NAME_MATCH;
550 if (user_to_uid(colon+1, &uid, True))
551 add_to_list(idmap_ptr, id1, noiu, uid, flags);
553 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
557 if (group_to_gid(colon+1, &gid, True))
558 add_to_list(idmap_ptr, id1, noiu, gid, flags);
560 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
567 *--cp = '\0'; /* replace comma */
570 /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
572 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : "root");
575 #ifdef HAVE_GETGROUPLIST
576 const char *getallgroups(uid_t uid, item_list *gid_list)
582 if ((pw = getpwuid(uid)) == NULL)
583 return "getpwuid failed";
585 gid_list->count = 0; /* We're overwriting any items in the list */
586 (void)EXPAND_ITEM_LIST(gid_list, gid_t, 32);
587 size = gid_list->malloced;
589 /* Get all the process's groups, with the pw_gid group first. */
590 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0) {
591 if (size > (int)gid_list->malloced) {
592 gid_list->count = gid_list->malloced;
593 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size);
594 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0)
599 return "getgrouplist failed";
601 gid_list->count = size;
602 gid_array = gid_list->items;
604 /* Paranoia: is the default group not first in the list? */
605 if (gid_array[0] != pw->pw_gid) {
607 for (j = 1; j < size; j++) {
608 if (gid_array[j] == pw->pw_gid)
611 if (j == size) { /* The default group wasn't found! */
612 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size+1);
613 gid_array = gid_list->items;
615 gid_array[j] = gid_array[0];
616 gid_array[0] = pw->pw_gid;