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