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 ngroups = getgroups(0, 0);
134 /* If that didn't work, perhaps 0 isn't treated specially? */
136 ngroups = NGROUPS_MAX;
137 gidset = new_array(GETGROUPS_T, ngroups+1);
139 ngroups = getgroups(ngroups, gidset);
140 /* The default gid might not be in the list on some systems. */
141 for (n = 0; n < ngroups; n++) {
142 if (gidset[n] == mygid)
146 gidset[ngroups++] = mygid;
148 char gidbuf[NGROUPS_MAX*16+32];
150 sprintf(gidbuf, "process has %d gid%s: ",
151 ngroups, ngroups == 1? "" : "s");
152 pos = strlen(gidbuf);
153 for (n = 0; n < ngroups; n++) {
154 sprintf(gidbuf+pos, " %ld", (long)gidset[n]);
155 pos += strlen(gidbuf+pos);
157 rprintf(FINFO, "%s\n", gidbuf);
162 for (n = 0; n < ngroups; n++) {
163 if (gidset[n] == gid)
169 static gid_t mygid = GID_NONE;
170 if (mygid == GID_NONE) {
173 rprintf(FINFO, "process has gid %ld\n", (long)mygid);
179 static gid_t match_gid(gid_t gid)
181 static gid_t last_in = GID_NONE, last_out = GID_NONE;
182 struct idlist *list = gidlist;
190 if (list->id == (int)gid) {
191 last_out = (gid_t)list->id2;
204 /* add a uid to the list of uids */
205 void add_uid(uid_t uid)
207 struct idlist *list = uidlist;
210 if (numeric_ids) return;
216 if (!(name = uid_to_name(uid))) return;
217 uidlist = add_list((int)uid, name);
222 if (list->id == (int)uid) return;
226 if (list->id == (int)uid) return;
228 if (!(name = uid_to_name(uid))) return;
230 list->next = add_list((int)uid, name);
233 /* add a gid to the list of gids */
234 void add_gid(gid_t gid)
236 struct idlist *list = gidlist;
239 if (numeric_ids) return;
245 if (!(name = gid_to_name(gid))) return;
246 gidlist = add_list((int)gid, name);
251 if (list->id == (int)gid) return;
255 if (list->id == (int)gid) return;
257 if (!(name = gid_to_name(gid))) return;
259 list->next = add_list((int)gid, name);
263 /* send a complete uid/gid mapping to the peer */
264 void send_uid_list(int f)
268 if (numeric_ids) return;
271 /* we send sequences of uid/byte-length/name */
274 int len = strlen(list->name);
275 write_int(f, list->id);
277 write_buf(f, list->name, len);
281 /* terminate the uid list with a 0 uid. We explicitly exclude
289 int len = strlen(list->name);
290 write_int(f, list->id);
292 write_buf(f, list->name, len);
299 /* recv a complete uid/gid mapping from the peer and map the uid/gid
300 * in the file list to local names */
301 void recv_uid_list(int f, struct file_list *flist)
307 if (numeric_ids) return;
310 /* read the uid list */
312 while ((id = read_int(f)) != 0) {
313 int len = read_byte(f);
314 name = new_array(char, len+1);
315 if (!name) out_of_memory("recv_uid_list");
316 read_sbuf(f, name, len);
318 uidlist = add_list(id, name);
321 list->next = add_list(id, name);
324 list->id2 = map_uid(id, name);
328 for (list = uidlist; list; list = list->next) {
329 rprintf(FINFO, "uid %ld (%s) maps to %ld\n",
330 (long)list->id, list->name,
338 /* and the gid list */
340 while ((id = read_int(f)) != 0) {
341 int len = read_byte(f);
342 name = new_array(char, len+1);
343 if (!name) out_of_memory("recv_uid_list");
344 read_sbuf(f, name, len);
346 gidlist = add_list(id, name);
349 list->next = add_list(id, name);
352 list->id2 = map_gid(id, name);
353 if (!am_root && !is_in_group(list->id2))
354 list->id2 = GID_NONE;
358 for (list = gidlist; list; list = list->next) {
359 rprintf(FINFO, "gid %ld (%s) maps to %ld\n",
360 (long)list->id, list->name,
366 if (!(am_root && preserve_uid) && !preserve_gid) return;
368 /* now convert the uid/gid of all files in the list to the mapped
370 for (i = 0; i < flist->count; i++) {
371 if (am_root && preserve_uid && flist->files[i]->uid != 0)
372 flist->files[i]->uid = match_uid(flist->files[i]->uid);
373 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
374 flist->files[i]->gid = match_gid(flist->files[i]->gid);