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
28 extern int preserve_uid;
29 extern int preserve_gid;
30 extern int numeric_ids;
39 static struct idlist *uidlist;
40 static struct idlist *gidlist;
42 static struct idlist *add_list(int id, char *name)
44 struct idlist *list = (struct idlist *)malloc(sizeof(list[0]));
45 if (!list) out_of_memory("add_list");
47 list->name = strdup(name);
48 if (!list->name) out_of_memory("add_list");
55 /* turn a uid into a user name */
56 static char *uid_to_name(uid_t uid)
58 struct passwd *pass = getpwuid(uid);
59 if (pass) return(pass->pw_name);
63 /* turn a gid into a group name */
64 static char *gid_to_name(gid_t gid)
66 struct group *grp = getgrgid(gid);
67 if (grp) return(grp->gr_name);
71 static int map_uid(int id, char *name)
74 if (name_to_uid(name, &uid) && uid != 0)
79 static int map_gid(int id, char *name)
82 if (name_to_gid(name, &gid) && gid != 0)
87 /* this function is a definate candidate for a faster algorithm */
88 static uid_t match_uid(uid_t uid)
90 static uid_t last_in, last_out;
91 struct idlist *list = uidlist;
93 if (uid == last_in) return last_out;
98 if (list->id == (int)uid) {
99 last_out = (uid_t)list->id2;
109 static gid_t match_gid(gid_t gid)
111 static gid_t last_in, last_out;
112 struct idlist *list = gidlist;
114 if (gid == last_in) return last_out;
119 if (list->id == (int)gid) {
120 last_out = (gid_t)list->id2;
129 last_out = (gid_t) -1;
133 /* add a uid to the list of uids */
134 void add_uid(uid_t uid)
136 struct idlist *list = uidlist;
139 if (numeric_ids) return;
145 if (!(name = uid_to_name(uid))) return;
146 uidlist = add_list((int)uid, name);
151 if (list->id == (int)uid) return;
155 if (list->id == (int)uid) return;
157 if (!(name = uid_to_name(uid))) return;
159 list->next = add_list((int)uid, name);
162 /* add a gid to the list of gids */
163 void add_gid(gid_t gid)
165 struct idlist *list = gidlist;
168 if (numeric_ids) return;
174 if (!(name = gid_to_name(gid))) return;
175 gidlist = add_list((int)gid, name);
180 if (list->id == (int)gid) return;
184 if (list->id == (int)gid) return;
186 if (!(name = gid_to_name(gid))) return;
188 list->next = add_list((int)gid, name);
192 /* send a complete uid/gid mapping to the peer */
193 void send_uid_list(int f)
197 if (numeric_ids) return;
200 /* we send sequences of uid/byte-length/name */
203 int len = strlen(list->name);
204 write_int(f, list->id);
206 write_buf(f, list->name, len);
210 /* terminate the uid list with a 0 uid. We explicitly exclude
218 int len = strlen(list->name);
219 write_int(f, list->id);
221 write_buf(f, list->name, len);
228 /* recv a complete uid/gid mapping from the peer and map the uid/gid
229 in the file list to local names */
230 void recv_uid_list(int f, struct file_list *flist)
236 if (numeric_ids) return;
239 /* read the uid list */
243 int len = read_byte(f);
244 name = (char *)malloc(len+1);
245 if (!name) out_of_memory("recv_uid_list");
246 read_sbuf(f, name, len);
248 uidlist = add_list(id, name);
251 list->next = add_list(id, name);
254 list->id2 = map_uid(id, name);
262 /* and the gid list */
266 int len = read_byte(f);
267 name = (char *)malloc(len+1);
268 if (!name) out_of_memory("recv_uid_list");
269 read_sbuf(f, name, len);
271 gidlist = add_list(id, name);
274 list->next = add_list(id, name);
277 list->id2 = map_gid(id, name);
283 if (!(am_root && preserve_uid) && !preserve_gid) return;
285 /* now convert the uid/gid of all files in the list to the mapped
287 for (i=0;i<flist->count;i++) {
288 if (am_root && preserve_uid && flist->files[i]->uid != 0) {
289 flist->files[i]->uid = match_uid(flist->files[i]->uid);
291 if (preserve_gid && flist->files[i]->gid != 0) {
292 flist->files[i]->gid = match_gid(flist->files[i]->gid);