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);
71 /* turn a user name into a uid */
72 static uid_t name_to_uid(char *name)
75 if (!name || !*name) return 0;
76 pass = getpwnam(name);
77 if (pass) return(pass->pw_uid);
81 /* turn a group name into a gid */
82 static gid_t name_to_gid(char *name)
85 if (!name || !*name) return 0;
87 if (grp) return(grp->gr_gid);
91 static int map_uid(int id, char *name)
93 uid_t uid = name_to_uid(name);
94 if (uid != 0) return uid;
98 static int map_gid(int id, char *name)
100 gid_t gid = name_to_gid(name);
101 if (gid != 0) return gid;
105 /* this function is a definate candidate for a faster algorithm */
106 static uid_t match_uid(uid_t uid)
108 static uid_t last_in, last_out;
109 struct idlist *list = uidlist;
111 if (uid == last_in) return last_out;
116 if (list->id == (int)uid) {
117 last_out = (uid_t)list->id2;
127 static gid_t match_gid(gid_t gid)
129 static gid_t last_in, last_out;
130 struct idlist *list = gidlist;
132 if (gid == last_in) return last_out;
137 if (list->id == (int)gid) {
138 last_out = (gid_t)list->id2;
148 /* add a uid to the list of uids */
149 void add_uid(uid_t uid)
151 struct idlist *list = uidlist;
154 if (numeric_ids) return;
160 if (!(name = uid_to_name(uid))) return;
161 uidlist = add_list((int)uid, name);
166 if (list->id == (int)uid) return;
170 if (list->id == (int)uid) return;
172 if (!(name = uid_to_name(uid))) return;
174 list->next = add_list((int)uid, name);
177 /* add a gid to the list of gids */
178 void add_gid(gid_t gid)
180 struct idlist *list = gidlist;
183 if (numeric_ids) return;
189 if (!(name = gid_to_name(gid))) return;
190 gidlist = add_list((int)gid, name);
195 if (list->id == (int)gid) return;
199 if (list->id == (int)gid) return;
201 if (!(name = gid_to_name(gid))) return;
203 list->next = add_list((int)gid, name);
207 /* send a complete uid/gid mapping to the peer */
208 void send_uid_list(int f)
212 if (numeric_ids) return;
215 /* we send sequences of uid/byte-length/name */
218 int len = strlen(list->name);
219 write_int(f, list->id);
221 write_buf(f, list->name, len);
225 /* terminate the uid list with a 0 uid. We explicitly exclude
233 int len = strlen(list->name);
234 write_int(f, list->id);
236 write_buf(f, list->name, len);
243 /* recv a complete uid/gid mapping from the peer and map the uid/gid
244 in the file list to local names */
245 void recv_uid_list(int f, struct file_list *flist)
251 if (numeric_ids) return;
254 /* read the uid list */
258 int len = read_byte(f);
259 name = (char *)malloc(len);
260 if (!name) out_of_memory("recv_uid_list");
261 read_buf(f, name, len);
263 uidlist = add_list(id, name);
266 list->next = add_list(id, name);
269 list->id2 = map_uid(id, name);
277 /* and the gid list */
281 int len = read_byte(f);
282 name = (char *)malloc(len);
283 if (!name) out_of_memory("recv_uid_list");
284 read_buf(f, name, len);
286 gidlist = add_list(id, name);
289 list->next = add_list(id, name);
292 list->id2 = map_gid(id, name);
298 if (!uidlist && !gidlist) return;
300 /* now convert the uid/gid of all files in the list to the mapped
302 for (i=0;i<flist->count;i++) {
303 if (preserve_uid && flist->files[i].uid != 0) {
304 flist->files[i].uid = match_uid(flist->files[i].uid);
306 if (preserve_gid && flist->files[i].gid != 0) {
307 flist->files[i].gid = match_gid(flist->files[i].gid);