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;
106 if (list->id == (int)uid) {
107 last_out = (uid_t)list->id2;
117 static int is_in_group(gid_t gid)
120 static gid_t last_in = GID_NONE, last_out;
121 static int ngroups = -2;
122 static GETGROUPS_T *gidset;
128 gid_t mygid = getgid();
129 ngroups = getgroups(0, 0);
130 /* If that didn't work, perhaps 0 isn't treated specially? */
132 ngroups = NGROUPS_MAX;
133 gidset = new_array(GETGROUPS_T, ngroups+1);
135 ngroups = getgroups(ngroups, gidset);
136 /* The default gid might not be in the list on some systems. */
137 for (n = 0; n < ngroups; n++) {
138 if (gidset[n] == mygid)
142 gidset[ngroups++] = mygid;
146 for (n = 0; n < ngroups; n++) {
147 if (gidset[n] == gid)
153 static gid_t mygid = GID_NONE;
154 if (mygid == GID_NONE)
160 static gid_t match_gid(gid_t gid)
162 static gid_t last_in = GID_NONE, last_out = GID_NONE;
163 struct idlist *list = gidlist;
171 if (list->id == (int)gid) {
172 last_out = (gid_t)list->id2;
185 /* add a uid to the list of uids */
186 void add_uid(uid_t uid)
188 struct idlist *list = uidlist;
191 if (numeric_ids) return;
197 if (!(name = uid_to_name(uid))) return;
198 uidlist = add_list((int)uid, name);
203 if (list->id == (int)uid) return;
207 if (list->id == (int)uid) return;
209 if (!(name = uid_to_name(uid))) return;
211 list->next = add_list((int)uid, name);
214 /* add a gid to the list of gids */
215 void add_gid(gid_t gid)
217 struct idlist *list = gidlist;
220 if (numeric_ids) return;
226 if (!(name = gid_to_name(gid))) return;
227 gidlist = add_list((int)gid, name);
232 if (list->id == (int)gid) return;
236 if (list->id == (int)gid) return;
238 if (!(name = gid_to_name(gid))) return;
240 list->next = add_list((int)gid, name);
244 /* send a complete uid/gid mapping to the peer */
245 void send_uid_list(int f)
249 if (numeric_ids) return;
252 /* we send sequences of uid/byte-length/name */
255 int len = strlen(list->name);
256 write_int(f, list->id);
258 write_buf(f, list->name, len);
262 /* terminate the uid list with a 0 uid. We explicitly exclude
270 int len = strlen(list->name);
271 write_int(f, list->id);
273 write_buf(f, list->name, len);
280 /* recv a complete uid/gid mapping from the peer and map the uid/gid
281 in the file list to local names */
282 void recv_uid_list(int f, struct file_list *flist)
288 if (numeric_ids) return;
291 /* read the uid list */
293 while ((id = read_int(f)) != 0) {
294 int len = read_byte(f);
295 name = new_array(char, len+1);
296 if (!name) out_of_memory("recv_uid_list");
297 read_sbuf(f, name, len);
299 uidlist = add_list(id, name);
302 list->next = add_list(id, name);
305 list->id2 = map_uid(id, name);
312 /* and the gid list */
314 while ((id = read_int(f)) != 0) {
315 int len = read_byte(f);
316 name = new_array(char, len+1);
317 if (!name) out_of_memory("recv_uid_list");
318 read_sbuf(f, name, len);
320 gidlist = add_list(id, name);
323 list->next = add_list(id, name);
326 list->id2 = map_gid(id, name);
327 if (!am_root && !is_in_group(list->id2))
328 list->id2 = GID_NONE;
333 if (!(am_root && preserve_uid) && !preserve_gid) return;
335 /* now convert the uid/gid of all files in the list to the mapped
337 for (i = 0; i < flist->count; i++) {
338 if (am_root && preserve_uid && flist->files[i]->uid != 0)
339 flist->files[i]->uid = match_uid(flist->files[i]->uid);
340 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
341 flist->files[i]->gid = match_gid(flist->files[i]->gid);