Send the uid/gid 0 name since not all systems use 0 for root.
[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 = strlen(name);
329         if (len > 255) /* Impossible? */
330                 len = 255;
331
332         write_varint30(f, id);
333         write_byte(f, len);
334         write_buf(f, name, len);
335 }
336
337 static void send_one_list(int f, struct idlist *idlist, int usernames)
338 {
339         struct idlist *list;
340
341         /* we send sequences of id/byte-len/name */
342         for (list = idlist; list; list = list->next) {
343                 if (list->id && list->u.name)
344                         send_one_name(f, list->id, list->u.name);
345         }
346
347         /* Terminate the uid list with 0 (which was excluded above).
348          * A modern rsync also sends the name of id 0. */
349         if (xmit_id0_names)
350                 send_one_name(f, 0, usernames ? uid_to_user(0) : gid_to_group(0));
351         else
352                 write_varint30(f, 0);
353 }
354
355 /* send a complete uid/gid mapping to the peer */
356 void send_id_lists(int f)
357 {
358         if (preserve_uid || preserve_acls)
359                 send_one_list(f, uidlist, 1);
360
361         if (preserve_gid || preserve_acls)
362                 send_one_list(f, gidlist, 0);
363 }
364
365 uid_t recv_user_name(int f, uid_t uid)
366 {
367         struct idlist *node;
368         int len = read_byte(f);
369         char *name = new_array(char, len+1);
370         read_sbuf(f, name, len);
371         if (numeric_ids < 0) {
372                 free(name);
373                 name = NULL;
374         }
375         node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
376         return node->id2;
377 }
378
379 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
380 {
381         struct idlist *node;
382         int len = read_byte(f);
383         char *name = new_array(char, len+1);
384         read_sbuf(f, name, len);
385         if (numeric_ids < 0) {
386                 free(name);
387                 name = NULL;
388         }
389         node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
390         if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
391                 *flags_ptr |= FLAG_SKIP_GROUP;
392         return node->id2;
393 }
394
395 /* recv a complete uid/gid mapping from the peer and map the uid/gid
396  * in the file list to local names */
397 void recv_id_list(int f, struct file_list *flist)
398 {
399         id_t id;
400         int i;
401
402         if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
403                 /* read the uid list */
404                 while ((id = read_varint30(f)) != 0)
405                         recv_user_name(f, id);
406                 if (xmit_id0_names)
407                         recv_user_name(f, 0);
408         }
409
410         if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
411                 /* read the gid list */
412                 while ((id = read_varint30(f)) != 0)
413                         recv_group_name(f, id, NULL);
414                 if (xmit_id0_names)
415                         recv_group_name(f, 0, NULL);
416         }
417
418         /* Now convert all the uids/gids from sender values to our values. */
419 #ifdef SUPPORT_ACLS
420         if (preserve_acls && (!numeric_ids || usermap || groupmap))
421                 match_acl_ids();
422 #endif
423         if (am_root && preserve_uid && (!numeric_ids || usermap)) {
424                 for (i = 0; i < flist->used; i++)
425                         F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
426         }
427         if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
428                 for (i = 0; i < flist->used; i++) {
429                         F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), &flist->files[i]->flags);
430                 }
431         }
432 }
433
434 void parse_name_map(char *map, BOOL usernames)
435 {
436         struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
437         struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
438         char *colon, *cp = map + strlen(map);
439         union name_or_id noiu;
440         id_t id1;
441         uint16 flags;
442
443         /* Parse the list in reverse, so the order in the struct is right. */
444         while (1) {
445                 while (cp > map && cp[-1] != ',') cp--;
446                 if (!(colon = strchr(cp, ':'))) {
447                         rprintf(FERROR, "No colon found in --%smap: %s\n",
448                                 usernames ? "user" : "group", cp);
449                         exit_cleanup(RERR_SYNTAX);
450                 }
451                 if (!colon[1]) {
452                         rprintf(FERROR, "No name found after colon --%smap: %s\n",
453                                 usernames ? "user" : "group", cp);
454                         exit_cleanup(RERR_SYNTAX);
455                 }
456                 *colon = '\0';
457
458                 if (isDigit(cp)) {
459                         char *dash = strchr(cp, '-');
460                         if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
461                          || (dash && (!dash[1] || strchr(dash+1, '-')))) {
462                                 rprintf(FERROR, "Invalid number in --%smap: %s\n",
463                                         usernames ? "user" : "group", cp);
464                                 exit_cleanup(RERR_SYNTAX);
465                         }
466                         if (dash) {
467                                 *dash = '\0';
468                                 noiu.max_id = id_parse(dash+1);
469                         } else
470                                 noiu.max_id = 0;
471                         flags = 0;
472                         id1 = id_parse(cp);
473                         if (dash)
474                                 *dash = '-';
475                 } else if (strpbrk(cp, "*[?")) {
476                         flags = NFLAGS_WILD_NAME_MATCH;
477                         noiu.name = cp;
478                         id1 = 0;
479                 } else {
480                         flags = NFLAGS_NAME_MATCH;
481                         noiu.name = cp;
482                         id1 = 0;
483                 }
484
485                 if (usernames) {
486                         uid_t uid;
487                         if (user_to_uid(colon+1, &uid, True))
488                                 add_to_list(idmap_ptr, id1, noiu, uid, flags);
489                         else {
490                                 rprintf(FERROR, "Unknown --usermap name on receiver: %s\n", colon+1);
491                         }
492                 } else {
493                         gid_t gid;
494                         if (group_to_gid(colon+1, &gid, True))
495                                 add_to_list(idmap_ptr, id1, noiu, gid, flags);
496                         else {
497                                 rprintf(FERROR, "Unknown --groupmap name on receiver: %s\n", colon+1);
498                         }
499                 }
500
501                 if (cp == map)
502                         break;
503
504                 *--cp = '\0'; /* replace comma */
505         }
506
507         /* If the sender isn't going to xmit the id0 name, we assume it's "root". */
508         if (!xmit_id0_names)
509                 recv_add_id(idlist_ptr, *idmap_ptr, 0, numeric_ids ? NULL : "root");
510 }
511
512 #ifdef HAVE_GETGROUPLIST
513 const char *getallgroups(uid_t uid, item_list *gid_list)
514 {
515         struct passwd *pw;
516         gid_t *gid_array;
517         int size;
518
519         if ((pw = getpwuid(uid)) == NULL)
520                 return "getpwuid failed";
521
522         gid_list->count = 0; /* We're overwriting any items in the list */
523         (void)EXPAND_ITEM_LIST(gid_list, gid_t, 32);
524         size = gid_list->malloced;
525
526         /* Get all the process's groups, with the pw_gid group first. */
527         if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0) {
528                 if (size > (int)gid_list->malloced) {
529                         gid_list->count = gid_list->malloced;
530                         (void)EXPAND_ITEM_LIST(gid_list, gid_t, size);
531                         if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list->items, &size) < 0)
532                                 size = -1;
533                 } else
534                         size = -1;
535                 if (size < 0)
536                         return "getgrouplist failed";
537         }
538         gid_list->count = size;
539         gid_array = gid_list->items;
540
541         /* Paranoia: is the default group not first in the list? */
542         if (gid_array[0] != pw->pw_gid) {
543                 int j;
544                 for (j = 1; j < size; j++) {
545                         if (gid_array[j] == pw->pw_gid)
546                                 break;
547                 }
548                 if (j == size) { /* The default group wasn't found! */
549                         (void)EXPAND_ITEM_LIST(gid_list, gid_t, size+1);
550                         gid_array = gid_list->items;
551                 }
552                 gid_array[j] = gid_array[0];
553                 gid_array[0] = pw->pw_gid;
554         }
555
556         return NULL;
557 }
558 #endif