2 * Handle the mapping of uid/gid and user/group names between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2009 Wayne Davison
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.
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.
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.
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
32 extern int preserve_uid;
33 extern int preserve_gid;
34 extern int preserve_acls;
35 extern int numeric_ids;
38 extern char *groupmap;
42 # define GETGROUPS_T gid_t
46 #define GID_NONE ((gid_t)-1)
48 #define NFLAGS_WILD_NAME_MATCH (1<<0)
49 #define NFLAGS_NAME_MATCH (1<<1)
58 static struct idlist *uidlist, *uidmap;
59 static struct idlist *gidlist, *gidmap;
61 static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
62 id_t id2, uint16 flags)
64 struct idlist *node = new(struct idlist);
66 out_of_memory("add_to_list");
76 /* turn a uid into a user name */
77 static const char *uid_to_name(uid_t uid)
79 struct passwd *pass = getpwuid(uid);
81 return strdup(pass->pw_name);
85 /* turn a gid into a group name */
86 static const char *gid_to_name(gid_t gid)
88 struct group *grp = getgrgid(gid);
90 return strdup(grp->gr_name);
94 static int is_in_group(gid_t gid)
97 static gid_t last_in = GID_NONE, last_out;
98 static int ngroups = -2;
99 static GETGROUPS_T *gidset;
105 if ((ngroups = getgroups(0, NULL)) < 0)
107 gidset = new_array(GETGROUPS_T, ngroups+1);
109 out_of_memory("is_in_group");
111 ngroups = getgroups(ngroups, gidset);
112 /* The default gid might not be in the list on some systems. */
113 for (n = 0; n < ngroups; n++) {
114 if (gidset[n] == our_gid)
118 gidset[ngroups++] = our_gid;
119 if (DEBUG_GTE(OWN, 2)) {
121 char *gidbuf = new_array(char, ngroups*21+32);
123 out_of_memory("is_in_group");
124 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
125 ngroups, ngroups == 1? "" : "s");
126 for (n = 0; n < ngroups; n++) {
127 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
129 rprintf(FINFO, "%s\n", gidbuf);
135 for (n = 0; n < ngroups; n++) {
136 if (gidset[n] == gid)
142 return gid == our_gid;
146 /* Add a uid/gid to its list of ids. Only called on receiving side. */
147 static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
148 id_t id, const char *name)
157 for (node = idmap; node; node = node->next) {
158 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
159 if (!wildmatch(node->name, name))
161 } else if (node->flags & NFLAGS_NAME_MATCH) {
162 if (strcmp(node->name, name) != 0)
164 } else if (node->name) {
165 if (id < node->id || id > (unsigned long)node->name)
175 else if (*name && id) {
176 if (idlist_ptr == &uidlist) {
178 id2 = user_to_uid(name, &uid, False) ? uid : id;
181 id2 = group_to_gid(name, &gid, False) ? gid : id;
186 flag = idlist_ptr == &gidlist && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
187 node = add_to_list(idlist_ptr, id, *name ? name : NULL, id2, flag);
189 if (DEBUG_GTE(OWN, 2)) {
190 rprintf(FINFO, "%sid %u(%s) maps to %u\n",
191 idlist_ptr == &uidlist ? "u" : "g",
192 (unsigned)id, name, (unsigned)id2);
198 /* this function is a definate candidate for a faster algorithm */
199 uid_t match_uid(uid_t uid)
201 static uid_t last_in = -1, last_out = -1;
209 for (list = uidlist; list; list = list->next) {
215 list = recv_add_id(&uidlist, uidmap, uid, NULL);
217 return last_out = list->id2;
220 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
222 static struct idlist *last = NULL;
225 if (last && gid == last->id)
228 for (list = gidlist; list; list = list->next) {
233 list = recv_add_id(&gidlist, gidmap, gid, NULL);
237 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
238 *flags_ptr |= FLAG_SKIP_GROUP;
242 /* Add a uid to the list of uids. Only called on sending side. */
243 const char *add_uid(uid_t uid)
248 if (uid == 0) /* don't map root */
251 for (list = uidlist; list; list = list->next) {
256 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
260 /* Add a gid to the list of gids. Only called on sending side. */
261 const char *add_gid(gid_t gid)
266 if (gid == 0) /* don't map root */
269 for (list = gidlist; list; list = list->next) {
274 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
278 /* send a complete uid/gid mapping to the peer */
279 void send_id_list(int f)
283 if (preserve_uid || preserve_acls) {
285 /* we send sequences of uid/byte-length/name */
286 for (list = uidlist; list; list = list->next) {
289 len = strlen(list->name);
290 write_varint30(f, list->id);
292 write_buf(f, list->name, len);
295 /* terminate the uid list with a 0 uid. We explicitly exclude
297 write_varint30(f, 0);
300 if (preserve_gid || preserve_acls) {
302 for (list = gidlist; list; list = list->next) {
305 len = strlen(list->name);
306 write_varint30(f, list->id);
308 write_buf(f, list->name, len);
310 write_varint30(f, 0);
314 uid_t recv_user_name(int f, uid_t uid)
317 int len = read_byte(f);
318 char *name = new_array(char, len+1);
320 out_of_memory("recv_user_name");
321 read_sbuf(f, name, len);
322 if (numeric_ids < 0) {
326 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
330 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
333 int len = read_byte(f);
334 char *name = new_array(char, len+1);
336 out_of_memory("recv_group_name");
337 read_sbuf(f, name, len);
338 if (numeric_ids < 0) {
342 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
343 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
344 *flags_ptr |= FLAG_SKIP_GROUP;
348 /* recv a complete uid/gid mapping from the peer and map the uid/gid
349 * in the file list to local names */
350 void recv_id_list(int f, struct file_list *flist)
355 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
356 /* read the uid list */
357 while ((id = read_varint30(f)) != 0)
358 recv_user_name(f, id);
361 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
362 /* read the gid list */
363 while ((id = read_varint30(f)) != 0)
364 recv_group_name(f, id, NULL);
367 /* Now convert all the uids/gids from sender values to our values. */
369 if (preserve_acls && (!numeric_ids || usermap || groupmap))
372 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
373 for (i = 0; i < flist->used; i++)
374 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
376 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
377 for (i = 0; i < flist->used; i++) {
378 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
379 &flist->files[i]->flags);
384 void parse_name_map(char *map, BOOL usernames)
386 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
387 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
388 char *colon, *end, *name, *cp = map + strlen(map);
392 /* Parse the list in reverse, so the order in the struct is right. */
395 while (cp > map && cp[-1] != ',') cp--;
396 if (!(colon = strchr(cp, ':'))) {
397 rprintf(FERROR, "No colon found in --%smap: %s\n",
398 usernames ? "user" : "group", cp);
399 exit_cleanup(RERR_SYNTAX);
402 rprintf(FERROR, "No name found after colon --%smap: %s\n",
403 usernames ? "user" : "group", cp);
404 exit_cleanup(RERR_SYNTAX);
409 char *dash = strchr(cp, '-');
410 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
411 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
412 rprintf(FERROR, "Invalid number in --%smap: %s\n",
413 usernames ? "user" : "group", cp);
414 exit_cleanup(RERR_SYNTAX);
417 name = (char *)atol(dash+1);
422 } else if (strpbrk(cp, "*[?")) {
423 flags = NFLAGS_WILD_NAME_MATCH;
427 flags = NFLAGS_NAME_MATCH;
434 if (user_to_uid(colon+1, &uid, True))
435 add_to_list(idmap_ptr, id1, name, uid, flags);
438 "Unknown --usermap name on receiver: %s\n",
443 if (group_to_gid(colon+1, &gid, True))
444 add_to_list(idmap_ptr, id1, name, gid, flags);
447 "Unknown --groupmap name on receiver: %s\n",
455 *--cp = '\0'; /* replace comma */
458 /* The 0 user/group doesn't get its name sent, so add it explicitly. */
459 recv_add_id(idlist_ptr, *idmap_ptr, 0,
460 numeric_ids ? NULL : usernames ? uid_to_name(0) : gid_to_name(0));