Avoid a crash if id-0 doesn't exist.
[rsync.git] / uidlist.c
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-2020 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 gid_t our_gid;
38 extern char *usermap;
39 extern char *groupmap;
40
41 #ifdef HAVE_GETGROUPS
42 # ifndef GETGROUPS_T
43 #  define GETGROUPS_T gid_t
44 # endif
45 #endif
46
47 #define NFLAGS_WILD_NAME_MATCH (1<<0)
48 #define NFLAGS_NAME_MATCH (1<<1)
49
50 union name_or_id {
51         const char *name;
52         id_t max_id;
53 };
54
55 struct idlist {
56         struct idlist *next;
57         union name_or_id u;
58         id_t id, id2;
59         uint16 flags;
60 };
61
62 static struct idlist *uidlist, *uidmap;
63 static struct idlist *gidlist, *gidmap;
64
65 static id_t id_parse(const char *num_str)
66 {
67         id_t tmp, num = 0;
68         const char *cp = num_str;
69
70         while (*cp) {
71                 if (!isDigit(cp)) {
72                   invalid_num:
73                         rprintf(FERROR, "Invalid ID number: %s\n", num_str);
74                         exit_cleanup(RERR_SYNTAX);
75                 }
76                 tmp = num * 10 + *cp++ - '0';
77                 if (tmp < num)
78                         goto invalid_num;
79                 num = tmp;
80         }
81
82         return num;
83 }
84
85 static struct idlist *add_to_list(struct idlist **root, id_t id, union name_or_id noiu,
86                                   id_t id2, uint16 flags)
87 {
88         struct idlist *node = new(struct idlist);
89         node->next = *root;
90         node->u = noiu;
91         node->id = id;
92         node->id2 = id2;
93         node->flags = flags;
94         *root = node;
95         return node;
96 }
97
98 /* turn a uid into a user name */
99 const char *uid_to_user(uid_t uid)
100 {
101         struct passwd *pass = getpwuid(uid);
102         if (pass)
103                 return strdup(pass->pw_name);
104         return NULL;
105 }
106
107 /* turn a gid into a group name */
108 const char *gid_to_group(gid_t gid)
109 {
110         struct group *grp = getgrgid(gid);
111         if (grp)
112                 return strdup(grp->gr_name);
113         return NULL;
114 }
115
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)
118 {
119         struct passwd *pass;
120         if (!name || !*name)
121                 return 0;
122         if (num_ok && name[strspn(name, "0123456789")] == '\0') {
123                 *uid_p = id_parse(name);
124                 return 1;
125         }
126         if (!(pass = getpwnam(name)))
127                 return 0;
128         *uid_p = pass->pw_uid;
129         return 1;
130 }
131
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)
134 {
135         struct group *grp;
136         if (!name || !*name)
137                 return 0;
138         if (num_ok && name[strspn(name, "0123456789")] == '\0') {
139                 *gid_p = id_parse(name);
140                 return 1;
141         }
142         if (!(grp = getgrnam(name)))
143                 return 0;
144         *gid_p = grp->gr_gid;
145         return 1;
146 }
147
148 static int is_in_group(gid_t gid)
149 {
150 #ifdef HAVE_GETGROUPS
151         static gid_t last_in;
152         static int ngroups = -2, last_out = -1;
153         static GETGROUPS_T *gidset;
154         int n;
155
156         if (gid == last_in && last_out >= 0)
157                 return last_out;
158         if (ngroups < -1) {
159                 if ((ngroups = getgroups(0, NULL)) < 0)
160                         ngroups = 0;
161                 gidset = new_array(GETGROUPS_T, ngroups+1);
162                 if (ngroups > 0)
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)
167                                 break;
168                 }
169                 if (n == ngroups)
170                         gidset[ngroups++] = our_gid;
171                 if (DEBUG_GTE(OWN, 2)) {
172                         int pos;
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]);
177                         }
178                         rprintf(FINFO, "%s\n", gidbuf);
179                         free(gidbuf);
180                 }
181         }
182
183         last_in = gid;
184         for (n = 0; n < ngroups; n++) {
185                 if (gidset[n] == gid)
186                         return last_out = 1;
187         }
188         return last_out = 0;
189
190 #else
191         return gid == our_gid;
192 #endif
193 }
194
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)
198 {
199         struct idlist *node;
200         union name_or_id noiu;
201         int flag;
202         id_t id2;
203
204         noiu.name = name; /* ensure that add_to_list() gets the raw value. */
205         if (!name)
206                 name = "";
207
208         for (node = idmap; node; node = node->next) {
209                 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
210                         if (!wildmatch(node->u.name, name))
211                                 continue;
212                 } else if (node->flags & NFLAGS_NAME_MATCH) {
213                         if (strcmp(node->u.name, name) != 0)
214                                 continue;
215                 } else if (node->u.max_id) {
216                         if (id < node->id || id > node->u.max_id)
217                                 continue;
218                 } else {
219                         if (node->id != id)
220                                 continue;
221                 }
222                 break;
223         }
224         if (node)
225                 id2 = node->id2;
226         else if (*name && id) {
227                 if (idlist_ptr == &uidlist) {
228                         uid_t uid;
229                         id2 = user_to_uid(name, &uid, False) ? uid : id;
230                 } else {
231                         gid_t gid;
232                         id2 = group_to_gid(name, &gid, False) ? gid : id;
233                 }
234         } else
235                 id2 = id;
236
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);
239
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);
244         }
245
246         return node;
247 }
248
249 /* this function is a definite candidate for a faster algorithm */
250 uid_t match_uid(uid_t uid)
251 {
252         static struct idlist *last = NULL;
253         struct idlist *list;
254
255         if (last && uid == last->id)
256                 return last->id2;
257
258         for (list = uidlist; list; list = list->next) {
259                 if (list->id == uid)
260                         break;
261         }
262
263         if (!list)
264                 list = recv_add_id(&uidlist, uidmap, uid, NULL);
265         last = list;
266
267         return list->id2;
268 }
269
270 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
271 {
272         static struct idlist *last = NULL;
273         struct idlist *list;
274
275         if (last && gid == last->id)
276                 list = last;
277         else {
278                 for (list = gidlist; list; list = list->next) {
279                         if (list->id == gid)
280                                 break;
281                 }
282                 if (!list)
283                         list = recv_add_id(&gidlist, gidmap, gid, NULL);
284                 last = list;
285         }
286
287         if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
288                 *flags_ptr |= FLAG_SKIP_GROUP;
289         return list->id2;
290 }
291
292 /* Add a uid to the list of uids.  Only called on sending side. */
293 const char *add_uid(uid_t uid)
294 {
295         struct idlist *list;
296         struct idlist *node;
297         union name_or_id noiu;
298
299         for (list = uidlist; list; list = list->next) {
300                 if (list->id == uid)
301                         return NULL;
302         }
303
304         noiu.name = uid_to_user(uid);
305         node = add_to_list(&uidlist, uid, noiu, 0, 0);
306         return node->u.name;
307 }
308
309 /* Add a gid to the list of gids.  Only called on sending side. */
310 const char *add_gid(gid_t gid)
311 {
312         struct idlist *list;
313         struct idlist *node;
314         union name_or_id noiu;
315
316         for (list = gidlist; list; list = list->next) {
317                 if (list->id == gid)
318                         return NULL;
319         }
320
321         noiu.name = gid_to_group(gid);
322         node = add_to_list(&gidlist, gid, noiu, 0, 0);
323         return node->u.name;
324 }
325
326 static void send_one_name(int f, id_t id, const char *name)
327 {
328         int len;
329
330         if (!name)
331                 name = "";
332         if ((len = strlen(name)) > 255) /* Impossible? */
333                 len = 255;
334
335         write_varint30(f, id);
336         write_byte(f, len);
337         if (len)
338                 write_buf(f, name, len);
339 }
340
341 static void send_one_list(int f, struct idlist *idlist, int usernames)
342 {
343         struct idlist *list;
344
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);
349         }
350
351         /* Terminate the uid list with 0 (which was excluded above).
352          * A modern rsync also sends the name of id 0. */
353         if (xmit_id0_names)
354                 send_one_name(f, 0, usernames ? uid_to_user(0) : gid_to_group(0));
355         else
356                 write_varint30(f, 0);
357 }
358
359 /* send a complete uid/gid mapping to the peer */
360 void send_id_lists(int f)
361 {
362         if (preserve_uid || preserve_acls)
363                 send_one_list(f, uidlist, 1);
364
365         if (preserve_gid || preserve_acls)
366                 send_one_list(f, gidlist, 0);
367 }
368
369 uid_t recv_user_name(int f, uid_t uid)
370 {
371         struct idlist *node;
372         int len = read_byte(f);
373         char *name;
374
375         if (len) {
376                 name = new_array(char, len+1);
377                 read_sbuf(f, name, len);
378                 if (numeric_ids < 0) {
379                         free(name);
380                         name = NULL;
381                 }
382         } else
383                 name = NULL;
384
385         node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
386         return node->id2;
387 }
388
389 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
390 {
391         struct idlist *node;
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) {
396                 free(name);
397                 name = NULL;
398         }
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;
402         return node->id2;
403 }
404
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)
408 {
409         id_t id;
410         int i;
411
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);
416                 if (xmit_id0_names)
417                         recv_user_name(f, 0);
418         }
419
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);
424                 if (xmit_id0_names)
425                         recv_group_name(f, 0, NULL);
426         }
427
428         /* Now convert all the uids/gids from sender values to our values. */
429 #ifdef SUPPORT_ACLS
430         if (preserve_acls && (!numeric_ids || usermap || groupmap))
431                 match_acl_ids();
432 #endif
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]));
436         }
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);
440                 }
441         }
442 }
443
444 void parse_name_map(char *map, BOOL usernames)
445 {
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;
450         id_t id1;
451         uint16 flags;
452
453         /* Parse the list in reverse, so the order in the struct is right. */
454         while (1) {
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);
460                 }
461                 if (!colon[1]) {
462                         rprintf(FERROR, "No name found after colon --%smap: %s\n",
463                                 usernames ? "user" : "group", cp);
464                         exit_cleanup(RERR_SYNTAX);
465                 }
466                 *colon = '\0';
467
468                 if (isDigit(cp)) {
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);
475                         }
476                         if (dash) {
477                                 *dash = '\0';
478                                 noiu.max_id = id_parse(dash+1);
479                         } else
480                                 noiu.max_id = 0;
481                         flags = 0;
482                         id1 = id_parse(cp);
483                         if (dash)
484                                 *dash = '-';
485                 } else if (strpbrk(cp, "*[?")) {
486                         flags = NFLAGS_WILD_NAME_MATCH;
487                         noiu.name = cp;
488                         id1 = 0;
489                 } else {
490                         flags = NFLAGS_NAME_MATCH;
491                         noiu.name = cp;
492                         id1 = 0;
493                 }
494
495                 if (usernames) {
496                         uid_t uid;
497                         if (user_to_uid(colon+1, &uid, True))
498                                 add_to_list(idmap_ptr, id1, noiu, uid, flags);
499                         else {
500                                 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
501                         }
502                 } else {
503                         gid_t gid;
504                         if (group_to_gid(colon+1, &gid, True))
505                                 add_to_list(idmap_ptr, id1, noiu, gid, flags);
506                         else {
507                                 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
508                         }
509                 }
510
511                 if (cp == map)
512                         break;
513
514                 *--cp = '\0'; /* replace comma */
515         }
516
517         /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
518         if (!xmit_id0_names)
519                 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : "root");
520 }
521
522 #ifdef HAVE_GETGROUPLIST
523 const char *getallgroups(uid_t uid, item_list *gid_list)
524 {
525         struct passwd *pw;
526         gid_t *gid_array;
527         int size;
528
529         if ((pw = getpwuid(uid)) == NULL)
530                 return "getpwuid failed";
531
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;
535
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)
542                                 size = -1;
543                 } else
544                         size = -1;
545                 if (size < 0)
546                         return "getgrouplist failed";
547         }
548         gid_list->count = size;
549         gid_array = gid_list->items;
550
551         /* Paranoia: is the default group not first in the list? */
552         if (gid_array[0] != pw->pw_gid) {
553                 int j;
554                 for (j = 1; j < size; j++) {
555                         if (gid_array[j] == pw->pw_gid)
556                                 break;
557                 }
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;
561                 }
562                 gid_array[j] = gid_array[0];
563                 gid_array[0] = pw->pw_gid;
564         }
565
566         return NULL;
567 }
568 #endif