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;
48 static struct idlist *uidlist;
49 static struct idlist *gidlist;
51 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
54 struct idlist *node = new(struct idlist);
56 out_of_memory("add_to_list");
65 /* turn a uid into a user name */
66 static char *uid_to_name(uid_t uid)
68 struct passwd *pass = getpwuid(uid);
70 return strdup(pass->pw_name);
74 /* turn a gid into a group name */
75 static char *gid_to_name(gid_t gid)
77 struct group *grp = getgrgid(gid);
79 return strdup(grp->gr_name);
83 static uid_t map_uid(uid_t id, char *name)
86 if (id != 0 && name_to_uid(name, &uid))
91 static gid_t map_gid(gid_t id, char *name)
94 if (id != 0 && name_to_gid(name, &gid))
99 static int is_in_group(gid_t gid)
101 #ifdef HAVE_GETGROUPS
102 static gid_t last_in = GID_NONE, last_out;
103 static int ngroups = -2;
104 static GETGROUPS_T *gidset;
110 gid_t mygid = MY_GID();
111 if ((ngroups = getgroups(0, NULL)) < 0)
113 gidset = new_array(GETGROUPS_T, ngroups+1);
115 out_of_memory("is_in_group");
117 ngroups = getgroups(ngroups, gidset);
118 /* The default gid might not be in the list on some systems. */
119 for (n = 0; n < ngroups; n++) {
120 if (gidset[n] == mygid)
124 gidset[ngroups++] = mygid;
127 char *gidbuf = new_array(char, ngroups*21+32);
129 out_of_memory("is_in_group");
130 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
131 ngroups, ngroups == 1? "" : "s");
132 for (n = 0; n < ngroups; n++) {
133 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
135 rprintf(FINFO, "%s\n", gidbuf);
141 for (n = 0; n < ngroups; n++) {
142 if (gidset[n] == gid)
148 static gid_t mygid = GID_NONE;
149 if (mygid == GID_NONE) {
152 rprintf(FINFO, "process has gid %d\n", (int)mygid);
158 /* Add a uid to the list of uids. Only called on receiving side. */
159 static uid_t recv_add_uid(uid_t id, char *name)
161 uid_t id2 = name ? map_uid(id, name) : id;
164 node = add_to_list(&uidlist, (int)id, name, (int)id2);
167 rprintf(FINFO, "uid %d(%s) maps to %d\n",
168 (int)id, name ? name : "", (int)id2);
174 /* Add a gid to the list of gids. Only called on receiving side. */
175 static gid_t recv_add_gid(gid_t id, char *name)
177 gid_t id2 = name ? map_gid(id, name) : id;
180 if (!am_root && !is_in_group(id2))
182 node = add_to_list(&gidlist, (int)id, name, (int)id2);
185 rprintf(FINFO, "gid %d(%s) maps to %d\n",
186 (int)id, name ? name : "", (int)id2);
192 /* this function is a definate candidate for a faster algorithm */
193 uid_t match_uid(uid_t uid)
195 static uid_t last_in, last_out;
206 for (list = uidlist; list; list = list->next) {
207 if (list->id == (int)uid)
208 return last_out = (uid_t)list->id2;
211 return last_out = uid;
214 gid_t match_gid(gid_t gid)
216 static gid_t last_in = GID_NONE, last_out = GID_NONE;
227 for (list = gidlist; list; list = list->next) {
228 if (list->id == (int)gid)
229 return last_out = (gid_t)list->id2;
232 return last_out = recv_add_gid(gid, NULL);
235 /* Add a uid to the list of uids. Only called on sending side. */
236 char *add_uid(uid_t uid)
241 if (uid == 0) /* don't map root */
244 for (list = uidlist; list; list = list->next) {
245 if (list->id == (int)uid)
249 node = add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
253 /* Add a gid to the list of gids. Only called on sending side. */
254 char *add_gid(gid_t gid)
259 if (gid == 0) /* don't map root */
262 for (list = gidlist; list; list = list->next) {
263 if (list->id == (int)gid)
267 node = add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
271 /* send a complete uid/gid mapping to the peer */
272 void send_uid_list(int f)
276 if (preserve_uid || preserve_acls) {
278 /* we send sequences of uid/byte-length/name */
279 for (list = uidlist; list; list = list->next) {
282 len = strlen(list->name);
283 write_varint30(f, list->id);
285 write_buf(f, list->name, len);
288 /* terminate the uid list with a 0 uid. We explicitly exclude
290 write_varint30(f, 0);
293 if (preserve_gid || preserve_acls) {
295 for (list = gidlist; list; list = list->next) {
298 len = strlen(list->name);
299 write_varint30(f, list->id);
301 write_buf(f, list->name, len);
303 write_varint30(f, 0);
307 uid_t recv_user_name(int f, uid_t uid)
309 int len = read_byte(f);
310 char *name = new_array(char, len+1);
312 out_of_memory("recv_user_name");
313 read_sbuf(f, name, len);
314 return recv_add_uid(uid, name); /* node keeps name's memory */
317 gid_t recv_group_name(int f, gid_t gid)
319 int len = read_byte(f);
320 char *name = new_array(char, len+1);
322 out_of_memory("recv_group_name");
323 read_sbuf(f, name, len);
324 return recv_add_gid(gid, name); /* node keeps name's memory */
327 /* recv a complete uid/gid mapping from the peer and map the uid/gid
328 * in the file list to local names */
329 void recv_uid_list(int f, struct file_list *flist)
333 if ((preserve_uid || preserve_acls) && !numeric_ids) {
334 /* read the uid list */
335 while ((id = read_varint30(f)) != 0)
336 recv_user_name(f, (uid_t)id);
339 if ((preserve_gid || preserve_acls) && !numeric_ids) {
340 /* read the gid list */
341 while ((id = read_varint30(f)) != 0)
342 recv_group_name(f, (gid_t)id);
345 /* Now convert all the uids/gids from sender values to our values. */
347 if (preserve_acls && !numeric_ids)
350 if (am_root && preserve_uid && !numeric_ids) {
351 for (i = 0; i < flist->count; i++)
352 F_OWNER(flist->files[i]) = match_uid(F_UID(flist->files[i]));
354 if (preserve_gid && (!am_root || !numeric_ids)) {
355 for (i = 0; i < flist->count; i++)
356 F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i]));