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
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_list(int id, char *name)
52 struct idlist *list = new(struct idlist);
53 if (!list) out_of_memory("add_list");
55 list->name = strdup(name);
56 if (!list->name) out_of_memory("add_list");
63 /* turn a uid into a user name */
64 static char *uid_to_name(uid_t uid)
66 struct passwd *pass = getpwuid(uid);
67 if (pass) return(pass->pw_name);
71 /* turn a gid into a group name */
72 static char *gid_to_name(gid_t gid)
74 struct group *grp = getgrgid(gid);
75 if (grp) return(grp->gr_name);
79 static int map_uid(int id, char *name)
82 if (uid != 0 && name_to_uid(name, &uid))
87 static int map_gid(int id, char *name)
90 if (gid != 0 && name_to_gid(name, &gid))
95 /* this function is a definate candidate for a faster algorithm */
96 static uid_t match_uid(uid_t uid)
98 static uid_t last_in, last_out;
99 struct idlist *list = uidlist;
107 if (list->id == (int)uid) {
108 last_out = (uid_t)list->id2;
118 static int is_in_group(gid_t gid)
121 static gid_t last_in = GID_NONE, last_out;
122 static int ngroups = -2;
123 static GETGROUPS_T *gidset;
129 gid_t mygid = getgid();
130 ngroups = getgroups(0, 0);
131 /* If that didn't work, perhaps 0 isn't treated specially? */
133 ngroups = NGROUPS_MAX;
134 gidset = new_array(GETGROUPS_T, ngroups+1);
136 ngroups = getgroups(ngroups, gidset);
137 /* The default gid might not be in the list on some systems. */
138 for (n = 0; n < ngroups; n++) {
139 if (gidset[n] == mygid)
143 gidset[ngroups++] = mygid;
145 char gidbuf[NGROUPS_MAX*16+32];
147 sprintf(gidbuf, "process has %d gid%s: ",
148 ngroups, ngroups == 1? "" : "s");
149 pos = strlen(gidbuf);
150 for (n = 0; n < ngroups; n++) {
151 sprintf(gidbuf+pos, " %ld", (long)gidset[n]);
152 pos += strlen(gidbuf+pos);
154 rprintf(FINFO, "%s\n", gidbuf);
159 for (n = 0; n < ngroups; n++) {
160 if (gidset[n] == gid)
166 static gid_t mygid = GID_NONE;
167 if (mygid == GID_NONE) {
170 rprintf(FINFO, "process has gid %ld\n", (long)mygid);
176 static gid_t match_gid(gid_t gid)
178 static gid_t last_in = GID_NONE, last_out = GID_NONE;
179 struct idlist *list = gidlist;
187 if (list->id == (int)gid) {
188 last_out = (gid_t)list->id2;
201 /* add a uid to the list of uids */
202 void add_uid(uid_t uid)
204 struct idlist *list = uidlist;
207 if (numeric_ids) return;
213 if (!(name = uid_to_name(uid))) return;
214 uidlist = add_list((int)uid, name);
219 if (list->id == (int)uid) return;
223 if (list->id == (int)uid) return;
225 if (!(name = uid_to_name(uid))) return;
227 list->next = add_list((int)uid, name);
230 /* add a gid to the list of gids */
231 void add_gid(gid_t gid)
233 struct idlist *list = gidlist;
236 if (numeric_ids) return;
242 if (!(name = gid_to_name(gid))) return;
243 gidlist = add_list((int)gid, name);
248 if (list->id == (int)gid) return;
252 if (list->id == (int)gid) return;
254 if (!(name = gid_to_name(gid))) return;
256 list->next = add_list((int)gid, name);
260 /* send a complete uid/gid mapping to the peer */
261 void send_uid_list(int f)
265 if (numeric_ids) return;
268 /* we send sequences of uid/byte-length/name */
271 int len = strlen(list->name);
272 write_int(f, list->id);
274 write_buf(f, list->name, len);
278 /* terminate the uid list with a 0 uid. We explicitly exclude
286 int len = strlen(list->name);
287 write_int(f, list->id);
289 write_buf(f, list->name, len);
296 /* recv a complete uid/gid mapping from the peer and map the uid/gid
297 * in the file list to local names */
298 void recv_uid_list(int f, struct file_list *flist)
304 if (numeric_ids) return;
307 /* read the uid list */
309 while ((id = read_int(f)) != 0) {
310 int len = read_byte(f);
311 name = new_array(char, len+1);
312 if (!name) out_of_memory("recv_uid_list");
313 read_sbuf(f, name, len);
315 uidlist = add_list(id, name);
318 list->next = add_list(id, name);
321 list->id2 = map_uid(id, name);
325 for (list = uidlist; list; list = list->next) {
326 rprintf(FINFO, "uid %ld (%s) maps to %ld\n",
327 (long)list->id, list->name,
335 /* and the gid list */
337 while ((id = read_int(f)) != 0) {
338 int len = read_byte(f);
339 name = new_array(char, len+1);
340 if (!name) out_of_memory("recv_uid_list");
341 read_sbuf(f, name, len);
343 gidlist = add_list(id, name);
346 list->next = add_list(id, name);
349 list->id2 = map_gid(id, name);
350 if (!am_root && !is_in_group(list->id2))
351 list->id2 = GID_NONE;
355 for (list = gidlist; list; list = list->next) {
356 rprintf(FINFO, "gid %ld (%s) maps to %ld\n",
357 (long)list->id, list->name,
363 if (!(am_root && preserve_uid) && !preserve_gid) return;
365 /* now convert the uid/gid of all files in the list to the mapped
367 for (i = 0; i < flist->count; i++) {
368 if (am_root && preserve_uid && flist->files[i]->uid != 0)
369 flist->files[i]->uid = match_uid(flist->files[i]->uid);
370 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
371 flist->files[i]->gid = match_gid(flist->files[i]->gid);