Debian Codegraph @

uidlist.c

raw symbols SCIP
1 /*
2 * Handle the mapping of uid/gid and user/group names between systems.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2022 Wayne Davison
7 *
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.
12 *
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.
17 *
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.
20 */
21
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. */
25
26 #include "rsync.h"
27 #include "ifuncs.h"
28 #include "itypes.h"
29 #include "io.h"
30
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;
41
42 #ifdef HAVE_GETGROUPS
43 # ifndef GETGROUPS_T
44 # define GETGROUPS_T gid_t
45 # endif
46 #endif
47
48 #define NFLAGS_WILD_NAME_MATCH (1<<0)
49 #define NFLAGS_NAME_MATCH (1<<1)
50
51 union name_or_id {
52 const char *name;
53 id_t max_id;
54 };
55
56 struct idlist {
57 struct idlist *next;
58 union name_or_id u;
59 id_t id, id2;
60 uint16 flags;
61 };
62
63 static struct idlist *uidlist, *uidmap;
64 static struct idlist *gidlist, *gidmap;
65
66 static inline int id_eq_uid(id_t id, uid_t uid)
67 {
68 return id == (id_t)uid;
69 }
70
71 static inline int id_eq_gid(id_t id, gid_t gid)
72 {
73 return id == (id_t)gid;
74 }
75
76 static id_t id_parse(const char *num_str)
77 {
78 id_t tmp, num = 0;
79 const char *cp = num_str;
80
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);
86 }
87 tmp = num * 10 + *cp++ - '0';
88 if (tmp < num)
89 goto invalid_num;
90 num = tmp;
91 }
92
93 return num;
94 }
95
96 static struct idlist *add_to_list(struct idlist **root, id_t id, union name_or_id noiu,
97 id_t id2, uint16 flags)
98 {
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;
107 }
108
109 /* turn a uid into a user name */
110 const char *uid_to_user(uid_t uid)
111 {
112 const char *name = NULL;
113
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);
121 }
122
123 return name;
124 }
125
126 /* turn a gid into a group name */
127 const char *gid_to_group(gid_t gid)
128 {
129 const char *name = NULL;
130
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);
138 }
139
140 return name;
141 }
142
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)
145 {
146 if (!name || !*name)
147 return 0;
148
149 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
150 *uid_p = id_parse(name);
151 return 1;
152 }
153
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;
164 }
165
166 return 1;
167 }
168
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)
171 {
172 if (!name || !*name)
173 return 0;
174
175 if (num_ok && name[strspn(name, "0123456789")] == '\0') {
176 *gid_p = id_parse(name);
177 return 1;
178 }
179
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;
190 }
191
192 return 1;
193 }
194
195 static int is_in_group(gid_t gid)
196 {
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;
202
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;
215 }
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]);
224 }
225 rprintf(FINFO, "%s\n", gidbuf);
226 free(gidbuf);
227 }
228 }
229
230 last_in = gid;
231 for (n = 0; n < ngroups; n++) {
232 if ((gid_t)gidset[n] == gid)
233 return last_out = 1;
234 }
235 return last_out = 0;
236
237 #else
238 return gid == our_gid;
239 #endif
240 }
241
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)
245 {
246 struct idlist *node;
247 union name_or_id noiu;
248 int flag;
249 id_t id2;
250
251 noiu.name = name; /* ensure that add_to_list() gets the raw value. */
252 if (!name)
253 name = "";
254
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;
268 }
269 break;
270 }
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;
280 }
281 } else
282 id2 = id;
283
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);
286
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);
291 }
292
293 return node;
294 }
295
296 /* this function is a definite candidate for a faster algorithm */
297 uid_t match_uid(uid_t uid)
298 {
299 static struct idlist *last = NULL;
300 struct idlist *list;
301
302 if (last && id_eq_uid(last->id, uid))
303 return last->id2;
304
305 for (list = uidlist; list; list = list->next) {
306 if (id_eq_uid(list->id, uid))
307 break;
308 }
309
310 if (!list)
311 list = recv_add_id(&uidlist, uidmap, uid, NULL);
312 last = list;
313
314 return list->id2;
315 }
316
317 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
318 {
319 static struct idlist *last = NULL;
320 struct idlist *list;
321
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;
328 }
329 if (!list)
330 list = recv_add_id(&gidlist, gidmap, gid, NULL);
331 last = list;
332 }
333
334 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
335 *flags_ptr |= FLAG_SKIP_GROUP;
336 return list->id2;
337 }
338
339 /* Add a uid to the list of uids. Only called on sending side. */
340 const char *add_uid(uid_t uid)
341 {
342 struct idlist *list;
343 struct idlist *node;
344 union name_or_id noiu;
345
346 for (list = uidlist; list; list = list->next) {
347 if (id_eq_uid(list->id, uid))
348 return NULL;
349 }
350
351 noiu.name = uid_to_user(uid);
352 node = add_to_list(&uidlist, uid, noiu, 0, 0);
353 return node->u.name;
354 }
355
356 /* Add a gid to the list of gids. Only called on sending side. */
357 const char *add_gid(gid_t gid)
358 {
359 struct idlist *list;
360 struct idlist *node;
361 union name_or_id noiu;
362
363 for (list = gidlist; list; list = list->next) {
364 if (id_eq_gid(list->id, gid))
365 return NULL;
366 }
367
368 noiu.name = gid_to_group(gid);
369 node = add_to_list(&gidlist, gid, noiu, 0, 0);
370 return node->u.name;
371 }
372
373 static void send_one_name(int f, id_t id, const char *name)
374 {
375 int len;
376
377 if (!name)
378 name = "";
379 if ((len = strlen(name)) > 255) /* Impossible? */
380 len = 255;
381
382 write_varint30(f, id);
383 write_byte(f, len);
384 if (len)
385 write_buf(f, name, len);
386 }
387
388 static void send_one_list(int f, struct idlist *idlist, int usernames)
389 {
390 struct idlist *list;
391
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);
396 }
397
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 const char *name = usernames ? uid_to_user(0) : gid_to_group(0);
402 send_one_name(f, 0, name);
403 free((char *)name);
404 } else
405 write_varint30(f, 0);
406 }
407
408 /* send a complete uid/gid mapping to the peer */
409 void send_id_lists(int f)
410 {
411 if (preserve_uid || preserve_acls)
412 send_one_list(f, uidlist, 1);
413
414 if (preserve_gid || preserve_acls)
415 send_one_list(f, gidlist, 0);
416 }
417
418 uid_t recv_user_name(int f, uid_t uid)
419 {
420 struct idlist *node;
421 int len = read_byte(f);
422 char *name;
423
424 if (len) {
425 name = new_array(char, len+1);
426 read_sbuf(f, name, len);
427 if (numeric_ids < 0) {
428 free(name);
429 name = NULL;
430 }
431 } else
432 name = NULL;
433
434 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
435 return node->id2;
436 }
437
438 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
439 {
440 struct idlist *node;
441 int len = read_byte(f);
442 char *name;
443
444 if (len) {
445 name = new_array(char, len+1);
446 read_sbuf(f, name, len);
447 if (numeric_ids < 0) {
448 free(name);
449 name = NULL;
450 }
451 } else
452 name = NULL;
453
454 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
455 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
456 *flags_ptr |= FLAG_SKIP_GROUP;
457 return node->id2;
458 }
459
460 /* recv a complete uid/gid mapping from the peer and map the uid/gid
461 * in the file list to local names */
462 void recv_id_list(int f, struct file_list *flist)
463 {
464 id_t id;
465 int i;
466
467 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
468 /* read the uid list */
469 while ((id = read_varint30(f)) != 0)
470 recv_user_name(f, id);
471 if (xmit_id0_names)
472 recv_user_name(f, 0);
473 }
474
475 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
476 /* read the gid list */
477 while ((id = read_varint30(f)) != 0)
478 recv_group_name(f, id, NULL);
479 if (xmit_id0_names)
480 recv_group_name(f, 0, NULL);
481 }
482
483 /* Now convert all the uids/gids from sender values to our values. */
484 #ifdef SUPPORT_ACLS
485 if (preserve_acls && (!numeric_ids || usermap || groupmap))
486 match_acl_ids();
487 #endif
488 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
489 for (i = 0; i < flist->used; i++)
490 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
491 }
492 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
493 for (i = 0; i < flist->used; i++) {
494 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), &flist->files[i]->flags);
495 }
496 }
497 }
498
499 void parse_name_map(char *map, BOOL usernames)
500 {
501 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
502 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
503 char *colon, *cp = map + strlen(map);
504 union name_or_id noiu;
505 id_t id1;
506 uint16 flags;
507
508 /* Parse the list in reverse, so the order in the struct is right. */
509 while (1) {
510 while (cp > map && cp[-1] != ',') cp--;
511 if (!(colon = strchr(cp, ':'))) {
512 rprintf(FERROR, "No colon found in --%smap: %s\n",
513 usernames ? "user" : "group", cp);
514 exit_cleanup(RERR_SYNTAX);
515 }
516 if (!colon[1]) {
517 rprintf(FERROR, "No name found after colon --%smap: %s\n",
518 usernames ? "user" : "group", cp);
519 exit_cleanup(RERR_SYNTAX);
520 }
521 *colon = '\0';
522
523 if (isDigit(cp)) {
524 char *dash = strchr(cp, '-');
525 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
526 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
527 rprintf(FERROR, "Invalid number in --%smap: %s\n",
528 usernames ? "user" : "group", cp);
529 exit_cleanup(RERR_SYNTAX);
530 }
531 if (dash) {
532 *dash = '\0';
533 noiu.max_id = id_parse(dash+1);
534 } else
535 noiu.max_id = 0;
536 flags = 0;
537 id1 = id_parse(cp);
538 if (dash)
539 *dash = '-';
540 } else if (strpbrk(cp, "*[?")) {
541 flags = NFLAGS_WILD_NAME_MATCH;
542 noiu.name = cp;
543 id1 = 0;
544 } else {
545 flags = NFLAGS_NAME_MATCH;
546 noiu.name = cp;
547 id1 = 0;
548 }
549
550 if (usernames) {
551 uid_t uid;
552 if (user_to_uid(colon+1, &uid, True))
553 add_to_list(idmap_ptr, id1, noiu, uid, flags);
554 else {
555 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
556 }
557 } else {
558 gid_t gid;
559 if (group_to_gid(colon+1, &gid, True))
560 add_to_list(idmap_ptr, id1, noiu, gid, flags);
561 else {
562 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
563 }
564 }
565
566 if (cp == map)
567 break;
568
569 *--cp = '\0'; /* replace comma */
570 }
571
572 /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
573 if (!xmit_id0_names)
574 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : "root");
575 }
576
577 #ifdef HAVE_GETGROUPLIST
578 const char *getallgroups(uid_t uid, item_list *gid_list)
579 {
580 struct passwd *pw;
581 gid_t *gid_array;
582 int size;
583
584 if ((pw = getpwuid(uid)) == NULL)
585 return "getpwuid failed";
586
587 gid_list->count = 0; /* We're overwriting any items in the list */
588 (void)EXPAND_ITEM_LIST(gid_list, gid_t, 32);
589 size = gid_list->malloced;
590
591 /* Get all the process's groups, with the pw_gid group first. */
592 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0) {
593 if (size > (int)gid_list->malloced) {
594 gid_list->count = gid_list->malloced;
595 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size);
596 if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0)
597 size = -1;
598 } else
599 size = -1;
600 if (size < 0)
601 return "getgrouplist failed";
602 }
603 gid_list->count = size;
604 gid_array = gid_list->items;
605
606 /* Paranoia: is the default group not first in the list? */
607 if (gid_array[0] != pw->pw_gid) {
608 int j;
609 for (j = 1; j < size; j++) {
610 if (gid_array[j] == pw->pw_gid)
611 break;
612 }
613 if (j == size) { /* The default group wasn't found! */
614 (void)EXPAND_ITEM_LIST(gid_list, gid_t, size+1);
615 gid_array = gid_list->items;
616 }
617 gid_array[j] = gid_array[0];
618 gid_array[0] = pw->pw_gid;
619 }
620
621 return NULL;
622 }
623 #endif