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-2008 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
30 extern int preserve_uid;
31 extern int preserve_gid;
32 extern int preserve_acls;
33 extern int numeric_ids;
37 # define GETGROUPS_T gid_t
41 #define GID_NONE ((gid_t)-1)
50 static struct idlist *uidlist;
51 static struct idlist *gidlist;
53 static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
54 id_t id2, uint16 flags)
56 struct idlist *node = new(struct idlist);
58 out_of_memory("add_to_list");
68 /* turn a uid into a user name */
69 static const char *uid_to_name(uid_t uid)
71 struct passwd *pass = getpwuid(uid);
73 return strdup(pass->pw_name);
77 /* turn a gid into a group name */
78 static const char *gid_to_name(gid_t gid)
80 struct group *grp = getgrgid(gid);
82 return strdup(grp->gr_name);
86 static uid_t map_uid(uid_t id, const char *name)
89 if (id != 0 && name_to_uid(name, &uid))
94 static gid_t map_gid(gid_t id, const char *name)
97 if (id != 0 && name_to_gid(name, &gid))
102 static int is_in_group(gid_t gid)
104 #ifdef HAVE_GETGROUPS
105 static gid_t last_in = GID_NONE, last_out;
106 static int ngroups = -2;
107 static GETGROUPS_T *gidset;
113 gid_t mygid = MY_GID();
114 if ((ngroups = getgroups(0, NULL)) < 0)
116 gidset = new_array(GETGROUPS_T, ngroups+1);
118 out_of_memory("is_in_group");
120 ngroups = getgroups(ngroups, gidset);
121 /* The default gid might not be in the list on some systems. */
122 for (n = 0; n < ngroups; n++) {
123 if (gidset[n] == mygid)
127 gidset[ngroups++] = mygid;
128 if (DEBUG_GTE(OWN, 2)) {
130 char *gidbuf = new_array(char, ngroups*21+32);
132 out_of_memory("is_in_group");
133 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
134 ngroups, ngroups == 1? "" : "s");
135 for (n = 0; n < ngroups; n++) {
136 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
138 rprintf(FINFO, "%s\n", gidbuf);
144 for (n = 0; n < ngroups; n++) {
145 if (gidset[n] == gid)
151 static gid_t mygid = GID_NONE;
152 if (mygid == GID_NONE) {
154 if (DEBUG_GTE(OWN, 2))
155 rprintf(FINFO, "process has gid %u\n", (unsigned)mygid);
161 /* Add a uid to the list of uids. Only called on receiving side. */
162 static struct idlist *recv_add_uid(uid_t id, const char *name)
164 uid_t id2 = name ? map_uid(id, name) : id;
167 node = add_to_list(&uidlist, id, name, id2, 0);
169 if (DEBUG_GTE(OWN, 2)) {
170 rprintf(FINFO, "uid %u(%s) maps to %u\n",
171 (unsigned)id, name ? name : "", (unsigned)id2);
177 /* Add a gid to the list of gids. Only called on receiving side. */
178 static struct idlist *recv_add_gid(gid_t id, const char *name)
180 gid_t id2 = name ? map_gid(id, name) : id;
183 node = add_to_list(&gidlist, id, name, id2,
184 !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
186 if (DEBUG_GTE(OWN, 2)) {
187 rprintf(FINFO, "gid %u(%s) maps to %u\n",
188 (unsigned)id, name ? name : "", (unsigned)id2);
194 /* this function is a definate candidate for a faster algorithm */
195 uid_t match_uid(uid_t uid)
197 static uid_t last_in, last_out;
208 for (list = uidlist; list; list = list->next) {
210 return last_out = list->id2;
213 return last_out = uid;
216 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
218 static struct idlist *last = NULL;
221 if (last && gid == last->id)
224 for (list = gidlist; list; list = list->next) {
229 list = recv_add_gid(gid, NULL);
233 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
234 *flags_ptr |= FLAG_SKIP_GROUP;
238 /* Add a uid to the list of uids. Only called on sending side. */
239 const char *add_uid(uid_t uid)
244 if (uid == 0) /* don't map root */
247 for (list = uidlist; list; list = list->next) {
252 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
256 /* Add a gid to the list of gids. Only called on sending side. */
257 const char *add_gid(gid_t gid)
262 if (gid == 0) /* don't map root */
265 for (list = gidlist; list; list = list->next) {
270 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
274 /* send a complete uid/gid mapping to the peer */
275 void send_id_list(int f)
279 if (preserve_uid || preserve_acls) {
281 /* we send sequences of uid/byte-length/name */
282 for (list = uidlist; list; list = list->next) {
285 len = strlen(list->name);
286 write_varint30(f, list->id);
288 write_buf(f, list->name, len);
291 /* terminate the uid list with a 0 uid. We explicitly exclude
293 write_varint30(f, 0);
296 if (preserve_gid || preserve_acls) {
298 for (list = gidlist; list; list = list->next) {
301 len = strlen(list->name);
302 write_varint30(f, list->id);
304 write_buf(f, list->name, len);
306 write_varint30(f, 0);
310 uid_t recv_user_name(int f, uid_t uid)
313 int len = read_byte(f);
314 char *name = new_array(char, len+1);
316 out_of_memory("recv_user_name");
317 read_sbuf(f, name, len);
318 if (numeric_ids < 0) {
322 node = recv_add_uid(uid, name); /* node keeps name's memory */
326 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
329 int len = read_byte(f);
330 char *name = new_array(char, len+1);
332 out_of_memory("recv_group_name");
333 read_sbuf(f, name, len);
334 if (numeric_ids < 0) {
338 node = recv_add_gid(gid, name); /* node keeps name's memory */
339 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
340 *flags_ptr |= FLAG_SKIP_GROUP;
344 /* recv a complete uid/gid mapping from the peer and map the uid/gid
345 * in the file list to local names */
346 void recv_id_list(int f, struct file_list *flist)
351 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
352 /* read the uid list */
353 while ((id = read_varint30(f)) != 0)
354 recv_user_name(f, id);
357 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
358 /* read the gid list */
359 while ((id = read_varint30(f)) != 0)
360 recv_group_name(f, id, NULL);
363 /* Now convert all the uids/gids from sender values to our values. */
365 if (preserve_acls && !numeric_ids)
368 if (am_root && preserve_uid && !numeric_ids) {
369 for (i = 0; i < flist->used; i++)
370 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
372 if (preserve_gid && (!am_root || !numeric_ids)) {
373 for (i = 0; i < flist->used; i++) {
374 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
375 &flist->files[i]->flags);