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