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;
38 static struct idlist *uidlist;
39 static struct idlist *gidlist;
41 static struct idlist *add_list(int id, char *name)
43 struct idlist *list = (struct idlist *)malloc(sizeof(list[0]));
44 if (!list) out_of_memory("add_list");
46 list->name = strdup(name);
47 if (!list->name) out_of_memory("add_list");
54 /* turn a uid into a user name */
55 static char *uid_to_name(uid_t uid)
57 struct passwd *pass = getpwuid(uid);
58 if (pass) return(pass->pw_name);
62 /* turn a gid into a group name */
63 static char *gid_to_name(gid_t gid)
65 struct group *grp = getgrgid(gid);
66 if (grp) return(grp->gr_name);
70 static int map_uid(int id, char *name)
73 if (name_to_uid(name, &uid) && uid != 0)
78 static int map_gid(int id, char *name)
81 if (name_to_gid(name, &gid) && gid != 0)
86 /* this function is a definate candidate for a faster algorithm */
87 static uid_t match_uid(uid_t uid)
89 static uid_t last_in, last_out;
90 struct idlist *list = uidlist;
92 if (uid == last_in) return last_out;
97 if (list->id == (int)uid) {
98 last_out = (uid_t)list->id2;
108 static gid_t match_gid(gid_t gid)
110 static gid_t last_in, last_out;
111 struct idlist *list = gidlist;
113 if (gid == last_in) return last_out;
118 if (list->id == (int)gid) {
119 last_out = (gid_t)list->id2;
129 /* add a uid to the list of uids */
130 void add_uid(uid_t uid)
132 struct idlist *list = uidlist;
135 if (numeric_ids) return;
141 if (!(name = uid_to_name(uid))) return;
142 uidlist = add_list((int)uid, name);
147 if (list->id == (int)uid) return;
151 if (list->id == (int)uid) return;
153 if (!(name = uid_to_name(uid))) return;
155 list->next = add_list((int)uid, name);
158 /* add a gid to the list of gids */
159 void add_gid(gid_t gid)
161 struct idlist *list = gidlist;
164 if (numeric_ids) return;
170 if (!(name = gid_to_name(gid))) return;
171 gidlist = add_list((int)gid, name);
176 if (list->id == (int)gid) return;
180 if (list->id == (int)gid) return;
182 if (!(name = gid_to_name(gid))) return;
184 list->next = add_list((int)gid, name);
188 /* send a complete uid/gid mapping to the peer */
189 void send_uid_list(int f)
193 if (numeric_ids) return;
196 /* we send sequences of uid/byte-length/name */
199 int len = strlen(list->name);
200 write_int(f, list->id);
202 write_buf(f, list->name, len);
206 /* terminate the uid list with a 0 uid. We explicitly exclude
214 int len = strlen(list->name);
215 write_int(f, list->id);
217 write_buf(f, list->name, len);
224 /* recv a complete uid/gid mapping from the peer and map the uid/gid
225 in the file list to local names */
226 void recv_uid_list(int f, struct file_list *flist)
232 if (numeric_ids) return;
235 /* read the uid list */
239 int len = read_byte(f);
240 name = (char *)malloc(len+1);
241 if (!name) out_of_memory("recv_uid_list");
242 read_sbuf(f, name, len);
244 uidlist = add_list(id, name);
247 list->next = add_list(id, name);
250 list->id2 = map_uid(id, name);
258 /* and the gid list */
262 int len = read_byte(f);
263 name = (char *)malloc(len+1);
264 if (!name) out_of_memory("recv_uid_list");
265 read_sbuf(f, name, len);
267 gidlist = add_list(id, name);
270 list->next = add_list(id, name);
273 list->id2 = map_gid(id, name);
279 if (!uidlist && !gidlist) return;
281 /* now convert the uid/gid of all files in the list to the mapped
283 for (i=0;i<flist->count;i++) {
284 if (preserve_uid && flist->files[i]->uid != 0) {
285 flist->files[i]->uid = match_uid(flist->files[i]->uid);
287 if (preserve_gid && flist->files[i]->gid != 0) {
288 flist->files[i]->gid = match_gid(flist->files[i]->gid);