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;
37 extern char *groupmap;
41 # define GETGROUPS_T gid_t
45 #define GID_NONE ((gid_t)-1)
47 #define NFLAGS_WILD_NAME_MATCH (1<<0)
48 #define NFLAGS_NAME_MATCH (1<<1)
57 static struct idlist *uidlist, *uidmap;
58 static struct idlist *gidlist, *gidmap;
60 static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
61 id_t id2, uint16 flags)
63 struct idlist *node = new(struct idlist);
65 out_of_memory("add_to_list");
75 /* turn a uid into a user name */
76 static const char *uid_to_name(uid_t uid)
78 struct passwd *pass = getpwuid(uid);
80 return strdup(pass->pw_name);
84 /* turn a gid into a group name */
85 static const char *gid_to_name(gid_t gid)
87 struct group *grp = getgrgid(gid);
89 return strdup(grp->gr_name);
93 static int is_in_group(gid_t gid)
96 static gid_t last_in = GID_NONE, last_out;
97 static int ngroups = -2;
98 static GETGROUPS_T *gidset;
104 gid_t mygid = MY_GID();
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] == mygid)
118 gidset[ngroups++] = mygid;
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 static gid_t mygid = GID_NONE;
143 if (mygid == GID_NONE) {
145 if (DEBUG_GTE(OWN, 2))
146 rprintf(FINFO, "process has gid %u\n", (unsigned)mygid);
152 /* Add a uid/gid to its list of ids. Only called on receiving side. */
153 static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
154 id_t id, const char *name)
163 for (node = idmap; node; node = node->next) {
164 if (node->flags & NFLAGS_WILD_NAME_MATCH) {
165 if (!wildmatch(node->name, name))
167 } else if (node->flags & NFLAGS_NAME_MATCH) {
168 if (strcmp(node->name, name) != 0)
170 } else if (node->name) {
171 if (id < node->id || id > (unsigned long)node->name)
181 else if (*name && id) {
182 if (idmap == uidmap) {
184 id2 = name_to_uid(name, &uid) ? uid : id;
187 id2 = name_to_gid(name, &gid) ? gid : id;
192 flag = idmap == gidmap && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
193 node = add_to_list(idlist_ptr, id, *name ? name : NULL, id2, flag);
195 if (DEBUG_GTE(OWN, 2)) {
196 rprintf(FINFO, "%sid %u(%s) maps to %u\n",
197 idmap == uidmap ? "u" : "g",
198 (unsigned)id, name, (unsigned)id2);
204 /* this function is a definate candidate for a faster algorithm */
205 uid_t match_uid(uid_t uid)
207 static uid_t last_in = -1, last_out = -1;
215 for (list = uidlist; list; list = list->next) {
221 list = recv_add_id(&uidlist, uidmap, uid, NULL);
223 return last_out = list->id2;
226 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
228 static struct idlist *last = NULL;
231 if (last && gid == last->id)
234 for (list = gidlist; list; list = list->next) {
239 list = recv_add_id(&gidlist, gidmap, gid, NULL);
243 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
244 *flags_ptr |= FLAG_SKIP_GROUP;
248 /* Add a uid to the list of uids. Only called on sending side. */
249 const char *add_uid(uid_t uid)
254 if (uid == 0) /* don't map root */
257 for (list = uidlist; list; list = list->next) {
262 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
266 /* Add a gid to the list of gids. Only called on sending side. */
267 const char *add_gid(gid_t gid)
272 if (gid == 0) /* don't map root */
275 for (list = gidlist; list; list = list->next) {
280 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
284 /* send a complete uid/gid mapping to the peer */
285 void send_id_list(int f)
289 if (preserve_uid || preserve_acls) {
291 /* we send sequences of uid/byte-length/name */
292 for (list = uidlist; list; list = list->next) {
295 len = strlen(list->name);
296 write_varint30(f, list->id);
298 write_buf(f, list->name, len);
301 /* terminate the uid list with a 0 uid. We explicitly exclude
303 write_varint30(f, 0);
306 if (preserve_gid || preserve_acls) {
308 for (list = gidlist; list; list = list->next) {
311 len = strlen(list->name);
312 write_varint30(f, list->id);
314 write_buf(f, list->name, len);
316 write_varint30(f, 0);
320 uid_t recv_user_name(int f, uid_t uid)
323 int len = read_byte(f);
324 char *name = new_array(char, len+1);
326 out_of_memory("recv_user_name");
327 read_sbuf(f, name, len);
328 if (numeric_ids < 0) {
332 node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
336 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
339 int len = read_byte(f);
340 char *name = new_array(char, len+1);
342 out_of_memory("recv_group_name");
343 read_sbuf(f, name, len);
344 if (numeric_ids < 0) {
348 node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
349 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
350 *flags_ptr |= FLAG_SKIP_GROUP;
354 /* recv a complete uid/gid mapping from the peer and map the uid/gid
355 * in the file list to local names */
356 void recv_id_list(int f, struct file_list *flist)
361 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
362 /* read the uid list */
363 while ((id = read_varint30(f)) != 0)
364 recv_user_name(f, id);
367 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
368 /* read the gid list */
369 while ((id = read_varint30(f)) != 0)
370 recv_group_name(f, id, NULL);
373 /* Now convert all the uids/gids from sender values to our values. */
375 if (preserve_acls && (!numeric_ids || usermap || groupmap))
378 if (am_root && preserve_uid && (!numeric_ids || usermap)) {
379 for (i = 0; i < flist->used; i++)
380 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
382 if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
383 for (i = 0; i < flist->used; i++) {
384 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
385 &flist->files[i]->flags);
390 void parse_name_map(char *map, BOOL usernames)
392 struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
393 struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
394 char *colon, *end, *name, *cp = map + strlen(map);
398 /* Parse the list in reverse, so the order in the struct is right. */
401 while (cp > map && cp[-1] != ',') cp--;
402 if (!(colon = strchr(cp, ':'))) {
403 rprintf(FERROR, "No colon found in --%smap: %s\n",
404 usernames ? "user" : "group", cp);
405 exit_cleanup(RERR_SYNTAX);
408 rprintf(FERROR, "No name found after colon --%smap: %s\n",
409 usernames ? "user" : "group", cp);
410 exit_cleanup(RERR_SYNTAX);
415 char *dash = strchr(cp, '-');
416 if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
417 || (dash && (!dash[1] || strchr(dash+1, '-')))) {
419 rprintf(FERROR, "Invalid number in --%smap: %s\n",
420 usernames ? "user" : "group", cp);
421 exit_cleanup(RERR_SYNTAX);
424 name = (char *)atol(dash+1);
429 } else if (strpbrk(cp, "*[?")) {
430 flags = NFLAGS_WILD_NAME_MATCH;
434 flags = NFLAGS_NAME_MATCH;
439 if (isDigit(colon+1)) {
440 if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
444 add_to_list(idmap_ptr, id1, name, atol(colon+1), flags);
445 } else if (usernames) {
447 if (name_to_uid(colon+1, &uid))
448 add_to_list(idmap_ptr, id1, name, uid, flags);
451 "Unknown --usermap name on receiver: %s\n",
456 if (name_to_gid(colon+1, &gid))
457 add_to_list(idmap_ptr, id1, name, gid, flags);
460 "Unknown --groupmap name on receiver: %s\n",
468 *--cp = '\0'; /* replace comma */
471 /* The 0 user/group doesn't get its name sent, so add it explicitly. */
472 recv_add_id(idlist_ptr, *idmap_ptr, 0,
473 numeric_ids ? NULL : usernames ? uid_to_name(0) : gid_to_name(0));