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, 2005, 2006 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 2 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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* If the source username/group does not exist on the target then use
24 * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
31 # define GETGROUPS_T gid_t
36 extern int preserve_uid;
37 extern int preserve_gid;
38 extern int numeric_ids;
47 static struct idlist *uidlist;
48 static struct idlist *gidlist;
50 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
53 struct idlist *node = new(struct idlist);
55 out_of_memory("add_to_list");
64 /* turn a uid into a user name */
65 static char *uid_to_name(uid_t uid)
67 struct passwd *pass = getpwuid(uid);
69 return strdup(pass->pw_name);
73 /* turn a gid into a group name */
74 static char *gid_to_name(gid_t gid)
76 struct group *grp = getgrgid(gid);
78 return strdup(grp->gr_name);
82 static int map_uid(int id, char *name)
85 if (id != 0 && name_to_uid(name, &uid))
90 static int map_gid(int id, char *name)
93 if (id != 0 && name_to_gid(name, &gid))
98 static int is_in_group(gid_t gid)
100 #ifdef HAVE_GETGROUPS
101 static gid_t last_in = GID_NONE, last_out;
102 static int ngroups = -2;
103 static GETGROUPS_T *gidset;
109 gid_t mygid = MY_GID();
110 if ((ngroups = getgroups(0, NULL)) < 0)
112 gidset = new_array(GETGROUPS_T, ngroups+1);
114 out_of_memory("is_in_group");
116 ngroups = getgroups(ngroups, gidset);
117 /* The default gid might not be in the list on some systems. */
118 for (n = 0; n < ngroups; n++) {
119 if (gidset[n] == mygid)
123 gidset[ngroups++] = mygid;
126 char *gidbuf = new_array(char, ngroups*21+32);
128 out_of_memory("is_in_group");
129 sprintf(gidbuf, "process has %d gid%s: ",
130 ngroups, ngroups == 1? "" : "s");
131 pos = strlen(gidbuf);
132 for (n = 0; n < ngroups; n++) {
133 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
134 pos += strlen(gidbuf+pos);
136 rprintf(FINFO, "%s\n", gidbuf);
142 for (n = 0; n < ngroups; n++) {
143 if (gidset[n] == gid)
149 static gid_t mygid = GID_NONE;
150 if (mygid == GID_NONE) {
153 rprintf(FINFO, "process has gid %d\n", (int)mygid);
159 /* Add a uid to the list of uids. Only called on receiving side. */
160 static struct idlist *recv_add_uid(int id, char *name)
162 int id2 = name ? map_uid(id, name) : id;
165 node = add_to_list(&uidlist, id, name, id2);
168 rprintf(FINFO, "uid %d(%s) maps to %d\n",
169 id, name ? name : "", id2);
175 /* Add a gid to the list of gids. Only called on receiving side. */
176 static struct idlist *recv_add_gid(int id, char *name)
178 int id2 = name ? map_gid(id, name) : id;
181 if (!am_root && !is_in_group(id2))
183 node = add_to_list(&gidlist, id, name, id2);
186 rprintf(FINFO, "gid %d(%s) maps to %d\n",
187 id, name ? name : "", id2);
193 /* this function is a definate candidate for a faster algorithm */
194 static uid_t match_uid(uid_t uid)
196 static uid_t last_in, last_out;
207 for (list = uidlist; list; list = list->next) {
208 if (list->id == (int)uid)
209 return last_out = (uid_t)list->id2;
212 return last_out = uid;
215 static gid_t match_gid(gid_t gid)
217 static gid_t last_in = GID_NONE, last_out = GID_NONE;
228 for (list = gidlist; list; list = list->next) {
229 if (list->id == (int)gid)
230 return last_out = (gid_t)list->id2;
233 list = recv_add_gid(gid, NULL);
234 return last_out = list->id2;
237 /* Add a uid to the list of uids. Only called on sending side. */
238 void add_uid(uid_t uid)
242 if (uid == 0) /* don't map root */
245 for (list = uidlist; list; list = list->next) {
246 if (list->id == (int)uid)
250 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 void add_gid(gid_t gid)
258 if (gid == 0) /* don't map root */
261 for (list = gidlist; list; list = list->next) {
262 if (list->id == (int)gid)
266 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
270 /* send a complete uid/gid mapping to the peer */
271 void send_uid_list(int f)
280 /* we send sequences of uid/byte-length/name */
281 for (list = uidlist; list; list = list->next) {
284 len = strlen(list->name);
285 write_int(f, list->id);
287 write_buf(f, list->name, len);
290 /* terminate the uid list with a 0 uid. We explicitly exclude
297 for (list = gidlist; list; list = list->next) {
300 len = strlen(list->name);
301 write_int(f, list->id);
303 write_buf(f, list->name, len);
309 /* recv a complete uid/gid mapping from the peer and map the uid/gid
310 * in the file list to local names */
311 void recv_uid_list(int f, struct file_list *flist)
316 if (preserve_uid && !numeric_ids) {
317 /* read the uid list */
318 while ((id = read_int(f)) != 0) {
319 int len = read_byte(f);
320 name = new_array(char, len+1);
322 out_of_memory("recv_uid_list");
323 read_sbuf(f, name, len);
324 recv_add_uid(id, name); /* node keeps name's memory */
328 if (preserve_gid && !numeric_ids) {
329 /* read the gid list */
330 while ((id = read_int(f)) != 0) {
331 int len = read_byte(f);
332 name = new_array(char, len+1);
334 out_of_memory("recv_uid_list");
335 read_sbuf(f, name, len);
336 recv_add_gid(id, name); /* node keeps name's memory */
340 /* Now convert all the uids/gids from sender values to our values. */
341 if (am_root && preserve_uid && !numeric_ids) {
342 for (i = 0; i < flist->count; i++)
343 flist->files[i]->uid = match_uid(flist->files[i]->uid);
345 if (preserve_gid && (!am_root || !numeric_ids)) {
346 for (i = 0; i < flist->count; i++)
347 flist->files[i]->gid = match_gid(flist->files[i]->gid);