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
30 /* It ought to be defined, but just in case. */
31 # define NGROUPS_MAX 32
35 extern int preserve_uid;
36 extern int preserve_gid;
37 extern int numeric_ids;
46 static struct idlist *uidlist;
47 static struct idlist *gidlist;
49 static struct idlist *add_list(int id, char *name)
51 struct idlist *list = new(struct idlist);
52 if (!list) out_of_memory("add_list");
54 list->name = strdup(name);
55 if (!list->name) out_of_memory("add_list");
62 /* turn a uid into a user name */
63 static char *uid_to_name(uid_t uid)
65 struct passwd *pass = getpwuid(uid);
66 if (pass) return(pass->pw_name);
70 /* turn a gid into a group name */
71 static char *gid_to_name(gid_t gid)
73 struct group *grp = getgrgid(gid);
74 if (grp) return(grp->gr_name);
78 static int map_uid(int id, char *name)
81 if (uid != 0 && name_to_uid(name, &uid))
86 static int map_gid(int id, char *name)
89 if (gid != 0 && name_to_gid(name, &gid))
94 /* this function is a definate candidate for a faster algorithm */
95 static uid_t match_uid(uid_t uid)
97 static uid_t last_in, last_out;
98 struct idlist *list = uidlist;
100 if (uid == last_in) return last_out;
105 if (list->id == (int)uid) {
106 last_out = (uid_t)list->id2;
116 static int is_in_group(gid_t gid)
119 static gid_t last_in = (gid_t) -2, last_out;
120 static int ngroups = -2;
121 static GETGROUPS_T *gidset;
127 gid_t mygid = getgid();
128 ngroups = getgroups(0, 0);
129 /* If that didn't work, perhaps 0 isn't treated specially? */
131 ngroups = NGROUPS_MAX;
132 gidset = new_array(GETGROUPS_T, ngroups+1);
134 ngroups = getgroups(ngroups, gidset);
135 /* The default gid might not be in the list on some systems. */
136 for (n = 0; n < ngroups; n++) {
137 if (gidset[n] == mygid)
141 gidset[ngroups++] = mygid;
146 for (n = 0; n < ngroups; n++) {
147 if (gidset[n] == gid) {
159 static gid_t match_gid(gid_t gid)
161 static gid_t last_in = (gid_t) -2, last_out;
162 struct idlist *list = gidlist;
164 if (gid == last_in) return last_out;
169 if (list->id == (int)gid) {
170 last_out = (gid_t)list->id2;
183 /* add a uid to the list of uids */
184 void add_uid(uid_t uid)
186 struct idlist *list = uidlist;
189 if (numeric_ids) return;
195 if (!(name = uid_to_name(uid))) return;
196 uidlist = add_list((int)uid, name);
201 if (list->id == (int)uid) return;
205 if (list->id == (int)uid) return;
207 if (!(name = uid_to_name(uid))) return;
209 list->next = add_list((int)uid, name);
212 /* add a gid to the list of gids */
213 void add_gid(gid_t gid)
215 struct idlist *list = gidlist;
218 if (numeric_ids) return;
224 if (!(name = gid_to_name(gid))) return;
225 gidlist = add_list((int)gid, name);
230 if (list->id == (int)gid) return;
234 if (list->id == (int)gid) return;
236 if (!(name = gid_to_name(gid))) return;
238 list->next = add_list((int)gid, name);
242 /* send a complete uid/gid mapping to the peer */
243 void send_uid_list(int f)
247 if (numeric_ids) return;
250 /* we send sequences of uid/byte-length/name */
253 int len = strlen(list->name);
254 write_int(f, list->id);
256 write_buf(f, list->name, len);
260 /* terminate the uid list with a 0 uid. We explicitly exclude
268 int len = strlen(list->name);
269 write_int(f, list->id);
271 write_buf(f, list->name, len);
278 /* recv a complete uid/gid mapping from the peer and map the uid/gid
279 in the file list to local names */
280 void recv_uid_list(int f, struct file_list *flist)
286 if (numeric_ids) return;
289 /* read the uid list */
291 while ((id = read_int(f)) != 0) {
292 int len = read_byte(f);
293 name = new_array(char, len+1);
294 if (!name) out_of_memory("recv_uid_list");
295 read_sbuf(f, name, len);
297 uidlist = add_list(id, name);
300 list->next = add_list(id, name);
303 list->id2 = map_uid(id, name);
310 /* and the gid 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 gidlist = add_list(id, name);
321 list->next = add_list(id, name);
324 list->id2 = map_gid(id, name);
325 if (!am_root && !is_in_group(list->id2))
326 list->id2 = GID_NONE;
331 if (!(am_root && preserve_uid) && !preserve_gid) return;
333 /* now convert the uid/gid of all files in the list to the mapped
335 for (i = 0; i < flist->count; i++) {
336 if (am_root && preserve_uid && flist->files[i]->uid != 0)
337 flist->files[i]->uid = match_uid(flist->files[i]->uid);
338 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
339 flist->files[i]->gid = match_gid(flist->files[i]->gid);