fix typo in manual page
[rsync.git] / uidlist.c
blob99a34679a8500e14492a259e40a9433c02820129
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-2022 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 int xmit_id0_names;
37 extern pid_t namecvt_pid;
38 extern gid_t our_gid;
39 extern char *usermap;
40 extern char *groupmap;
42 #ifdef HAVE_GETGROUPS
43 # ifndef GETGROUPS_T
44 # define GETGROUPS_T gid_t
45 # endif
46 #endif
48 #define NFLAGS_WILD_NAME_MATCH (1<<0)
49 #define NFLAGS_NAME_MATCH (1<<1)
51 union name_or_id {
52 const char *name;
53 id_t max_id;
56 struct idlist {
57 struct idlist *next;
58 union name_or_id u;
59 id_t id, id2;
60 uint16 flags;
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)
78 id_t tmp, num = 0;
79 const char *cp = num_str;
81 while (*cp) {
82 if (!isDigit(cp)) {
83 invalid_num:
84 rprintf(FERROR, "Invalid ID number: %s\n", num_str);
85 exit_cleanup(RERR_SYNTAX);
87 tmp = num * 10 + *cp++ - '0';
88 if (tmp < num)
89 goto invalid_num;
90 num = tmp;
93 return num;
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);
100 node->next = *root;
101 node->u = noiu;
102 node->id = id;
103 node->id2 = id2;
104 node->flags = flags;
105 *root = node;
106 return node;
109 /* turn a uid into a user name */
110 const char *uid_to_user(uid_t uid)
112 const char *name = NULL;
114 if (namecvt_pid) {
115 id_t id = uid;
116 namecvt_call("uid", &name, &id);
117 } else {
118 struct passwd *pass = getpwuid(uid);
119 if (pass)
120 name = strdup(pass->pw_name);
123 return name;
126 /* turn a gid into a group name */
127 const char *gid_to_group(gid_t gid)
129 const char *name = NULL;
131 if (namecvt_pid) {
132 id_t id = gid;
133 namecvt_call("gid", &name, &id);
134 } else {
135 struct group *grp = getgrgid(gid);
136 if (grp)
137 name = strdup(grp->gr_name);
140 return 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)
146 if (!name || !*name)
147 return 0;
149 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
150 *uid_p = id_parse(name);
151 return 1;
154 if (namecvt_pid) {
155 id_t id;
156 if (!namecvt_call("usr", &name, &id))
157 return 0;
158 *uid_p = id;
159 } else {
160 struct passwd *pass = getpwnam(name);
161 if (!pass)
162 return 0;
163 *uid_p = pass->pw_uid;
166 return 1;
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)
172 if (!name || !*name)
173 return 0;
175 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
176 *gid_p = id_parse(name);
177 return 1;
180 if (namecvt_pid) {
181 id_t id;
182 if (!namecvt_call("grp", &name, &id))
183 return 0;
184 *gid_p = id;
185 } else {
186 struct group *grp = getgrnam(name);
187 if (!grp)
188 return 0;
189 *gid_p = grp->gr_gid;
192 return 1;
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;
201 int n;
203 if (gid == last_in && last_out >= 0)
204 return last_out;
205 if (ngroups < -1) {
206 if ((ngroups = getgroups(0, NULL)) < 0)
207 ngroups = 0;
208 gidset = new_array(GETGROUPS_T, ngroups+1);
209 if (ngroups > 0)
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 ((gid_t)gidset[n] == our_gid)
214 break;
216 if (n == ngroups)
217 gidset[ngroups++] = our_gid;
218 if (DEBUG_GTE(OWN, 2)) {
219 int pos;
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);
226 free(gidbuf);
230 last_in = gid;
231 for (n = 0; n < ngroups; n++) {
232 if ((gid_t)gidset[n] == gid)
233 return last_out = 1;
235 return last_out = 0;
237 #else
238 return gid == our_gid;
239 #endif
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)
246 struct idlist *node;
247 union name_or_id noiu;
248 int flag;
249 id_t id2;
251 noiu.name = name; /* ensure that add_to_list() gets the raw value. */
252 if (!name)
253 name = "";
255 for (node = idmap; node; node = node->next) {
256 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
257 if (!wildmatch(node->u.name, name))
258 continue;
259 } else if (node->flags & NFLAGS_NAME_MATCH) {
260 if (strcmp(node->u.name, name) != 0)
261 continue;
262 } else if (node->u.max_id) {
263 if (id < node->id || id > node->u.max_id)
264 continue;
265 } else {
266 if (node->id != id)
267 continue;
269 break;
271 if (node)
272 id2 = node->id2;
273 else if (*name && id) {
274 if (idlist_ptr == &uidlist) {
275 uid_t uid;
276 id2 = user_to_uid(name, &uid, False) ? (id_t)uid : id;
277 } else {
278 gid_t gid;
279 id2 = group_to_gid(name, &gid, False) ? (id_t)gid : id;
281 } else
282 id2 = 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);
293 return node;
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;
300 struct idlist *list;
302 if (last && id_eq_uid(last->id, uid))
303 return last->id2;
305 for (list = uidlist; list; list = list->next) {
306 if (id_eq_uid(list->id, uid))
307 break;
310 if (!list)
311 list = recv_add_id(&uidlist, uidmap, uid, NULL);
312 last = list;
314 return list->id2;
317 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
319 static struct idlist *last = NULL;
320 struct idlist *list;
322 if (last && id_eq_gid(last->id, gid))
323 list = last;
324 else {
325 for (list = gidlist; list; list = list->next) {
326 if (id_eq_gid(list->id, gid))
327 break;
329 if (!list)
330 list = recv_add_id(&gidlist, gidmap, gid, NULL);
331 last = list;
334 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
335 *flags_ptr |= FLAG_SKIP_GROUP;
336 return list->id2;
339 /* Add a uid to the list of uids. Only called on sending side. */
340 const char *add_uid(uid_t uid)
342 struct idlist *list;
343 struct idlist *node;
344 union name_or_id noiu;
346 for (list = uidlist; list; list = list->next) {
347 if (id_eq_uid(list->id, uid))
348 return NULL;
351 noiu.name = uid_to_user(uid);
352 node = add_to_list(&uidlist, uid, noiu, 0, 0);
353 return node->u.name;
356 /* Add a gid to the list of gids. Only called on sending side. */
357 const char *add_gid(gid_t gid)
359 struct idlist *list;
360 struct idlist *node;
361 union name_or_id noiu;
363 for (list = gidlist; list; list = list->next) {
364 if (id_eq_gid(list->id, gid))
365 return NULL;
368 noiu.name = gid_to_group(gid);
369 node = add_to_list(&gidlist, gid, noiu, 0, 0);
370 return node->u.name;
373 static void send_one_name(int f, id_t id, const char *name)
375 int len;
377 if (!name)
378 name = "";
379 if ((len = strlen(name)) > 255) /* Impossible? */
380 len = 255;
382 write_varint30(f, id);
383 write_byte(f, len);
384 if (len)
385 write_buf(f, name, len);
388 static void send_one_list(int f, struct idlist *idlist, int usernames)
390 struct idlist *list;
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. */
400 if (xmit_id0_names)
401 send_one_name(f, 0, usernames ? uid_to_user(0) : gid_to_group(0));
402 else
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)
418 struct idlist *node;
419 int len = read_byte(f);
420 char *name;
422 if (len) {
423 name = new_array(char, len+1);
424 read_sbuf(f, name, len);
425 if (numeric_ids < 0) {
426 free(name);
427 name = NULL;
429 } else
430 name = NULL;
432 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
433 return node->id2;
436 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
438 struct idlist *node;
439 int len = read_byte(f);
440 char *name;
442 if (len) {
443 name = new_array(char, len+1);
444 read_sbuf(f, name, len);
445 if (numeric_ids < 0) {
446 free(name);
447 name = NULL;
449 } else
450 name = NULL;
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;
455 return node->id2;
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)
462 id_t id;
463 int i;
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);
469 if (xmit_id0_names)
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);
477 if (xmit_id0_names)
478 recv_group_name(f, 0, NULL);
481 /* Now convert all the uids/gids from sender values to our values. */
482 #ifdef SUPPORT_ACLS
483 if (preserve_acls && (!numeric_ids || usermap || groupmap))
484 match_acl_ids();
485 #endif
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;
503 id_t id1;
504 uint16 flags;
506 /* Parse the list in reverse, so the order in the struct is right. */
507 while (1) {
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);
514 if (!colon[1]) {
515 rprintf(FERROR, "No name found after colon --%smap: %s\n",
516 usernames ? "user" : "group", cp);
517 exit_cleanup(RERR_SYNTAX);
519 *colon = '\0';
521 if (isDigit(cp)) {
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);
529 if (dash) {
530 *dash = '\0';
531 noiu.max_id = id_parse(dash+1);
532 } else
533 noiu.max_id = 0;
534 flags = 0;
535 id1 = id_parse(cp);
536 if (dash)
537 *dash = '-';
538 } else if (strpbrk(cp, "*[?")) {
539 flags = NFLAGS_WILD_NAME_MATCH;
540 noiu.name = cp;
541 id1 = 0;
542 } else {
543 flags = NFLAGS_NAME_MATCH;
544 noiu.name = cp;
545 id1 = 0;
548 if (usernames) {
549 uid_t uid;
550 if (user_to_uid(colon+1, &uid, True))
551 add_to_list(idmap_ptr, id1, noiu, uid, flags);
552 else {
553 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
555 } else {
556 gid_t gid;
557 if (group_to_gid(colon+1, &gid, True))
558 add_to_list(idmap_ptr, id1, noiu, gid, flags);
559 else {
560 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
564 if (cp == map)
565 break;
567 *--cp = '\0'; /* replace comma */
570 /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
571 if (!xmit_id0_names)
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)
578 struct passwd *pw;
579 gid_t *gid_array;
580 int size;
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)
595 size = -1;
596 } else
597 size = -1;
598 if (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) {
606 int j;
607 for (j = 1; j < size; j++) {
608 if (gid_array[j] == pw->pw_gid)
609 break;
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;
619 return NULL;
621 #endif