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-2007 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 version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
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
31 # define GETGROUPS_T gid_t
37 extern int preserve_uid;
38 extern int preserve_gid;
39 extern int preserve_acls;
40 extern int numeric_ids;
49 static struct idlist *uidlist;
50 static struct idlist *gidlist;
52 static struct idlist *add_to_list(struct idlist **root, id_t id, char *name,
53 id_t id2, uint16 flags)
55 struct idlist *node = new(struct idlist);
57 out_of_memory("add_to_list");
67 /* turn a uid into a user name */
68 static char *uid_to_name(uid_t uid)
70 struct passwd *pass = getpwuid(uid);
72 return strdup(pass->pw_name);
76 /* turn a gid into a group name */
77 static char *gid_to_name(gid_t gid)
79 struct group *grp = getgrgid(gid);
81 return strdup(grp->gr_name);
85 static uid_t map_uid(uid_t id, char *name)
88 if (id != 0 && name_to_uid(name, &uid))
93 static gid_t map_gid(gid_t id, char *name)
96 if (id != 0 && name_to_gid(name, &gid))
101 static int is_in_group(gid_t gid)
103 #ifdef HAVE_GETGROUPS
104 static gid_t last_in = GID_NONE, last_out;
105 static int ngroups = -2;
106 static GETGROUPS_T *gidset;
112 gid_t mygid = MY_GID();
113 if ((ngroups = getgroups(0, NULL)) < 0)
115 gidset = new_array(GETGROUPS_T, ngroups+1);
117 out_of_memory("is_in_group");
119 ngroups = getgroups(ngroups, gidset);
120 /* The default gid might not be in the list on some systems. */
121 for (n = 0; n < ngroups; n++) {
122 if (gidset[n] == mygid)
126 gidset[ngroups++] = mygid;
129 char *gidbuf = new_array(char, ngroups*21+32);
131 out_of_memory("is_in_group");
132 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
133 ngroups, ngroups == 1? "" : "s");
134 for (n = 0; n < ngroups; n++) {
135 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
137 rprintf(FINFO, "%s\n", gidbuf);
143 for (n = 0; n < ngroups; n++) {
144 if (gidset[n] == gid)
150 static gid_t mygid = GID_NONE;
151 if (mygid == GID_NONE) {
154 rprintf(FINFO, "process has gid %u\n", (unsigned)mygid);
160 /* Add a uid to the list of uids. Only called on receiving side. */
161 static struct idlist *recv_add_uid(uid_t id, char *name)
163 uid_t id2 = name ? map_uid(id, name) : id;
166 node = add_to_list(&uidlist, id, name, id2, 0);
169 rprintf(FINFO, "uid %u(%s) maps to %u\n",
170 (unsigned)id, name ? name : "", (unsigned)id2);
176 /* Add a gid to the list of gids. Only called on receiving side. */
177 static struct idlist *recv_add_gid(gid_t id, char *name)
179 gid_t id2 = name ? map_gid(id, name) : id;
182 node = add_to_list(&gidlist, id, name, id2,
183 !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
186 rprintf(FINFO, "gid %u(%s) maps to %u\n",
187 (unsigned)id, name ? name : "", (unsigned)id2);
193 /* this function is a definate candidate for a faster algorithm */
194 uid_t match_uid(uid_t uid)
196 static uid_t last_in, last_out;
207 for (list = uidlist; list; list = list->next) {
209 return last_out = list->id2;
212 return last_out = uid;
215 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
217 static gid_t last_in = GID_NONE, last_out = GID_NONE;
228 for (list = gidlist; list; list = list->next) {
234 list = recv_add_gid(gid, NULL);
236 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
237 *flags_ptr |= FLAG_SKIP_GROUP;
238 return last_out = list->id2;
241 /* Add a uid to the list of uids. Only called on sending side. */
242 char *add_uid(uid_t uid)
247 if (uid == 0) /* don't map root */
250 for (list = uidlist; list; list = list->next) {
255 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
259 /* Add a gid to the list of gids. Only called on sending side. */
260 char *add_gid(gid_t gid)
265 if (gid == 0) /* don't map root */
268 for (list = gidlist; list; list = list->next) {
273 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
277 /* send a complete uid/gid mapping to the peer */
278 void send_id_list(int f)
282 if (preserve_uid || preserve_acls) {
284 /* we send sequences of uid/byte-length/name */
285 for (list = uidlist; list; list = list->next) {
288 len = strlen(list->name);
289 write_varint30(f, list->id);
291 write_buf(f, list->name, len);
294 /* terminate the uid list with a 0 uid. We explicitly exclude
296 write_varint30(f, 0);
299 if (preserve_gid || preserve_acls) {
301 for (list = gidlist; list; list = list->next) {
304 len = strlen(list->name);
305 write_varint30(f, list->id);
307 write_buf(f, list->name, len);
309 write_varint30(f, 0);
313 uid_t recv_user_name(int f, uid_t uid)
316 int len = read_byte(f);
317 char *name = new_array(char, len+1);
319 out_of_memory("recv_user_name");
320 read_sbuf(f, name, len);
321 node = recv_add_uid(uid, name); /* node keeps name's memory */
325 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
328 int len = read_byte(f);
329 char *name = new_array(char, len+1);
331 out_of_memory("recv_group_name");
332 read_sbuf(f, name, len);
333 node = recv_add_gid(gid, name); /* node keeps name's memory */
334 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
335 *flags_ptr |= FLAG_SKIP_GROUP;
339 /* recv a complete uid/gid mapping from the peer and map the uid/gid
340 * in the file list to local names */
341 void recv_id_list(int f, struct file_list *flist)
346 if ((preserve_uid || preserve_acls) && !numeric_ids) {
347 /* read the uid list */
348 while ((id = read_varint30(f)) != 0)
349 recv_user_name(f, id);
352 if ((preserve_gid || preserve_acls) && !numeric_ids) {
353 /* read the gid list */
354 while ((id = read_varint30(f)) != 0)
355 recv_group_name(f, id, NULL);
358 /* Now convert all the uids/gids from sender values to our values. */
360 if (preserve_acls && !numeric_ids)
363 if (am_root && preserve_uid && !numeric_ids) {
364 for (i = 0; i < flist->count; i++)
365 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
367 if (preserve_gid && (!am_root || !numeric_ids)) {
368 for (i = 0; i < flist->count; i++) {
369 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
370 &flist->files[i]->flags);