2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* handle the mapping of uid/gid and user/group names between systems.
21 If the source username/group does not exist on the target then use
22 the numeric ids. Never do any mapping for uid=0 or gid=0 as these
29 # if !defined(GETGROUPS_T)
30 # define GETGROUPS_T gid_t
33 /* It ought to be defined, but just in case. */
34 # define NGROUPS_MAX 32
39 extern int preserve_uid;
40 extern int preserve_gid;
41 extern int numeric_ids;
50 static struct idlist *uidlist;
51 static struct idlist *gidlist;
53 static struct idlist *add_list(int id, char *name)
55 struct idlist *list = new(struct idlist);
56 if (!list) out_of_memory("add_list");
58 list->name = strdup(name);
59 if (!list->name) out_of_memory("add_list");
66 /* turn a uid into a user name */
67 static char *uid_to_name(uid_t uid)
69 struct passwd *pass = getpwuid(uid);
70 if (pass) return(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);
78 if (grp) return(grp->gr_name);
82 static int map_uid(int id, char *name)
85 if (uid != 0 && name_to_uid(name, &uid))
90 static int map_gid(int id, char *name)
93 if (gid != 0 && name_to_gid(name, &gid))
98 /* this function is a definate candidate for a faster algorithm */
99 static uid_t match_uid(uid_t uid)
101 static uid_t last_in, last_out;
102 struct idlist *list = uidlist;
110 if (list->id == (int)uid) {
111 last_out = (uid_t)list->id2;
121 static int is_in_group(gid_t gid)
123 #ifdef HAVE_GETGROUPS
124 static gid_t last_in = GID_NONE, last_out;
125 static int ngroups = -2;
126 static GETGROUPS_T *gidset;
132 gid_t mygid = MY_GID();
133 if ((ngroups = getgroups(0, 0)) < 0)
135 gidset = new_array(GETGROUPS_T, ngroups+1);
137 ngroups = getgroups(ngroups, gidset);
138 /* The default gid might not be in the list on some systems. */
139 for (n = 0; n < ngroups; n++) {
140 if (gidset[n] == mygid)
144 gidset[ngroups++] = mygid;
146 char gidbuf[NGROUPS_MAX*16+32];
148 sprintf(gidbuf, "process has %d gid%s: ",
149 ngroups, ngroups == 1? "" : "s");
150 pos = strlen(gidbuf);
151 for (n = 0; n < ngroups; n++) {
152 sprintf(gidbuf+pos, " %ld", (long)gidset[n]);
153 pos += strlen(gidbuf+pos);
155 rprintf(FINFO, "%s\n", gidbuf);
160 for (n = 0; n < ngroups; n++) {
161 if (gidset[n] == gid)
167 static gid_t mygid = GID_NONE;
168 if (mygid == GID_NONE) {
171 rprintf(FINFO, "process has gid %ld\n", (long)mygid);
177 static gid_t match_gid(gid_t gid)
179 static gid_t last_in = GID_NONE, last_out = GID_NONE;
180 struct idlist *list = gidlist;
188 if (list->id == (int)gid) {
189 last_out = (gid_t)list->id2;
195 if (am_root || is_in_group(gid))
202 /* add a uid to the list of uids */
203 void add_uid(uid_t uid)
205 struct idlist *list = uidlist;
208 if (numeric_ids) return;
214 if (!(name = uid_to_name(uid))) return;
215 uidlist = add_list((int)uid, name);
220 if (list->id == (int)uid) return;
224 if (list->id == (int)uid) return;
226 if (!(name = uid_to_name(uid))) return;
228 list->next = add_list((int)uid, name);
231 /* add a gid to the list of gids */
232 void add_gid(gid_t gid)
234 struct idlist *list = gidlist;
237 if (numeric_ids) return;
243 if (!(name = gid_to_name(gid))) return;
244 gidlist = add_list((int)gid, name);
249 if (list->id == (int)gid) return;
253 if (list->id == (int)gid) return;
255 if (!(name = gid_to_name(gid))) return;
257 list->next = add_list((int)gid, name);
261 /* send a complete uid/gid mapping to the peer */
262 void send_uid_list(int f)
266 if (numeric_ids) return;
269 /* we send sequences of uid/byte-length/name */
272 int len = strlen(list->name);
273 write_int(f, list->id);
275 write_buf(f, list->name, len);
279 /* terminate the uid list with a 0 uid. We explicitly exclude
287 int len = strlen(list->name);
288 write_int(f, list->id);
290 write_buf(f, list->name, len);
297 /* recv a complete uid/gid mapping from the peer and map the uid/gid
298 * in the file list to local names */
299 void recv_uid_list(int f, struct file_list *flist)
305 if (numeric_ids) return;
308 /* read the uid list */
310 while ((id = read_int(f)) != 0) {
311 int len = read_byte(f);
312 name = new_array(char, len+1);
313 if (!name) out_of_memory("recv_uid_list");
314 read_sbuf(f, name, len);
316 uidlist = add_list(id, name);
319 list->next = add_list(id, name);
322 list->id2 = map_uid(id, name);
326 for (list = uidlist; list; list = list->next) {
327 rprintf(FINFO, "uid %ld (%s) maps to %ld\n",
328 (long)list->id, list->name,
336 /* and the gid list */
338 while ((id = read_int(f)) != 0) {
339 int len = read_byte(f);
340 name = new_array(char, len+1);
341 if (!name) out_of_memory("recv_uid_list");
342 read_sbuf(f, name, len);
344 gidlist = add_list(id, name);
347 list->next = add_list(id, name);
350 list->id2 = map_gid(id, name);
351 if (!am_root && !is_in_group(list->id2))
352 list->id2 = GID_NONE;
356 for (list = gidlist; list; list = list->next) {
357 rprintf(FINFO, "gid %ld (%s) maps to %ld\n",
358 (long)list->id, list->name,
364 if (!(am_root && preserve_uid) && !preserve_gid) return;
366 /* now convert the uid/gid of all files in the list to the mapped
368 for (i = 0; i < flist->count; i++) {
369 if (am_root && preserve_uid && flist->files[i]->uid != 0)
370 flist->files[i]->uid = match_uid(flist->files[i]->uid);
371 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
372 flist->files[i]->gid = match_gid(flist->files[i]->gid);