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 # define GETGROUPS_T gid_t
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 id_pair *pair_list;
50 static int pair_cnt = 0, pair_alloc = 0;
52 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
55 struct idlist *node = new(struct idlist);
57 out_of_memory("add_to_list");
66 /* turn a uid into a user name */
67 static char *uid_to_name(uid_t uid)
69 struct passwd *pass = getpwuid(uid);
71 return strdup(pass->pw_name);
75 /* turn a gid into a group name */
76 static char *gid_to_name(gid_t gid)
78 struct group *grp = getgrgid(gid);
80 return strdup(grp->gr_name);
84 static int map_uid(int id, char *name)
87 if (id != 0 && name_to_uid(name, &uid))
92 static int map_gid(int id, char *name)
95 if (id != 0 && name_to_gid(name, &gid))
100 static int is_in_group(gid_t gid)
102 #ifdef HAVE_GETGROUPS
103 static gid_t last_in = GID_NONE, last_out;
104 static int ngroups = -2;
105 static GETGROUPS_T *gidset;
111 gid_t mygid = MY_GID();
112 if ((ngroups = getgroups(0, NULL)) < 0)
114 gidset = new_array(GETGROUPS_T, ngroups+1);
116 out_of_memory("is_in_group");
118 ngroups = getgroups(ngroups, gidset);
119 /* The default gid might not be in the list on some systems. */
120 for (n = 0; n < ngroups; n++) {
121 if (gidset[n] == mygid)
125 gidset[ngroups++] = mygid;
128 char *gidbuf = new_array(char, ngroups*21+32);
130 out_of_memory("is_in_group");
131 sprintf(gidbuf, "process has %d gid%s: ",
132 ngroups, ngroups == 1? "" : "s");
133 pos = strlen(gidbuf);
134 for (n = 0; n < ngroups; n++) {
135 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
136 pos += strlen(gidbuf+pos);
138 rprintf(FINFO, "%s\n", gidbuf);
144 for (n = 0; n < ngroups; n++) {
145 if (gidset[n] == gid)
151 static gid_t mygid = GID_NONE;
152 if (mygid == GID_NONE) {
155 rprintf(FINFO, "process has gid %d\n", (int)mygid);
161 /* Add a uid to the list of uids. Only called on receiving side. */
162 static struct idlist *recv_add_uid(int id, char *name)
164 int id2 = name ? map_uid(id, name) : id;
167 node = add_to_list(&uidlist, id, name, id2);
170 rprintf(FINFO, "uid %d(%s) maps to %d\n",
171 id, name ? name : "", id2);
177 /* Add a gid to the list of gids. Only called on receiving side. */
178 static struct idlist *recv_add_gid(int id, char *name)
180 int id2 = name ? map_gid(id, name) : id;
183 if (!am_root && !is_in_group(id2))
185 node = add_to_list(&gidlist, id, name, id2);
188 rprintf(FINFO, "gid %d(%s) maps to %d\n",
189 id, name ? name : "", id2);
195 /* this function is a definate candidate for a faster algorithm */
196 static uid_t match_uid(uid_t uid)
198 static uid_t last_in, last_out;
209 for (list = uidlist; list; list = list->next) {
210 if (list->id == (int)uid)
211 return last_out = (uid_t)list->id2;
214 return last_out = uid;
217 static gid_t match_gid(gid_t gid)
219 static gid_t last_in = GID_NONE, last_out = GID_NONE;
230 for (list = gidlist; list; list = list->next) {
231 if (list->id == (int)gid)
232 return last_out = (gid_t)list->id2;
235 list = recv_add_gid(gid, NULL);
236 return last_out = list->id2;
239 /* Add a uid to the list of uids. Only called on sending side. */
240 void add_uid(uid_t uid)
244 if (uid == 0) /* don't map root */
247 for (list = uidlist; list; list = list->next) {
248 if (list->id == (int)uid)
252 add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
255 /* Add a gid to the list of gids. Only called on sending side. */
256 void add_gid(gid_t gid)
260 if (gid == 0) /* don't map root */
263 for (list = gidlist; list; list = list->next) {
264 if (list->id == (int)gid)
268 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
272 /* send a complete uid/gid mapping to the peer */
273 void send_uid_list(int f)
282 /* we send sequences of uid/byte-length/name */
283 for (list = uidlist; list; list = list->next) {
286 len = strlen(list->name);
287 write_int(f, list->id);
289 write_buf(f, list->name, len);
292 /* terminate the uid list with a 0 uid. We explicitly exclude
299 for (list = gidlist; list; list = list->next) {
302 len = strlen(list->name);
303 write_int(f, list->id);
305 write_buf(f, list->name, len);
311 /* recv a complete uid/gid mapping from the peer and map the uid/gid
312 * in the file list to local names */
313 void recv_uid_list(int f)
318 if (preserve_uid && !numeric_ids) {
319 /* read the uid list */
320 while ((id = read_int(f)) != 0) {
321 int len = read_byte(f);
322 name = new_array(char, len+1);
324 out_of_memory("recv_uid_list");
325 read_sbuf(f, name, len);
326 recv_add_uid(id, name); /* node keeps name's memory */
330 if (preserve_gid && !numeric_ids) {
331 /* read the gid list */
332 while ((id = read_int(f)) != 0) {
333 int len = read_byte(f);
334 name = new_array(char, len+1);
336 out_of_memory("recv_uid_list");
337 read_sbuf(f, name, len);
338 recv_add_gid(id, name); /* node keeps name's memory */
342 /* Now convert the id_pair array over to mapped uid/gid values. */
343 if (am_root && preserve_uid && !numeric_ids) {
344 for (i = 0; i < pair_cnt; i++)
345 pair_list[i].uid = match_uid(pair_list[i].uid);
347 if (preserve_gid && (!am_root || !numeric_ids)) {
348 for (i = 0; i < pair_cnt; i++)
349 pair_list[i].gid = match_gid(pair_list[i].gid);
353 struct id_pair *id_pair(uid_t uid, gid_t gid)
357 for (i = 0; i < pair_cnt; i++) {
358 if (uid == pair_list[i].uid && gid == pair_list[i].gid)
359 return pair_list + i;
362 if (pair_cnt == pair_alloc) {
364 pair_list = realloc_array(pair_list, struct id_pair,
367 pair_list[pair_cnt].uid = uid;
368 pair_list[pair_cnt].gid = gid;
369 return pair_list + pair_cnt++;