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
29 # if !defined(GETGROUPS_T)
30 # define GETGROUPS_T gid_t
33 /* It ought to be defined, but just in case. */
34 # define NGROUPS_MAX 32
39 extern int preserve_uid;
40 extern int preserve_gid;
41 extern int numeric_ids;
50 static struct idlist *uidlist;
51 static struct idlist *gidlist;
53 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
56 struct idlist *node = new(struct idlist);
58 out_of_memory("add_to_list");
67 /* turn a uid into a user name */
68 static char *uid_to_name(uid_t uid)
70 struct passwd *pass = getpwuid(uid);
72 return strdup(pass->pw_name);
76 /* turn a gid into a group name */
77 static char *gid_to_name(gid_t gid)
79 struct group *grp = getgrgid(gid);
81 return strdup(grp->gr_name);
85 static int map_uid(int id, char *name)
88 if (uid != 0 && name_to_uid(name, &uid))
93 static int map_gid(int id, char *name)
96 if (gid != 0 && name_to_gid(name, &gid))
101 static int is_in_group(gid_t gid)
103 #ifdef HAVE_GETGROUPS
104 static gid_t last_in = GID_NONE, last_out;
105 static int ngroups = -2;
106 static GETGROUPS_T *gidset;
112 gid_t mygid = MY_GID();
113 if ((ngroups = getgroups(0, 0)) < 0)
115 gidset = new_array(GETGROUPS_T, ngroups+1);
117 ngroups = getgroups(ngroups, gidset);
118 /* The default gid might not be in the list on some systems. */
119 for (n = 0; n < ngroups; n++) {
120 if (gidset[n] == mygid)
124 gidset[ngroups++] = mygid;
126 char gidbuf[NGROUPS_MAX*16+32];
128 sprintf(gidbuf, "process has %d gid%s: ",
129 ngroups, ngroups == 1? "" : "s");
130 pos = strlen(gidbuf);
131 for (n = 0; n < ngroups; n++) {
132 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
133 pos += strlen(gidbuf+pos);
135 rprintf(FINFO, "%s\n", gidbuf);
140 for (n = 0; n < ngroups; n++) {
141 if (gidset[n] == gid)
147 static gid_t mygid = GID_NONE;
148 if (mygid == GID_NONE) {
151 rprintf(FINFO, "process has gid %d\n", (int)mygid);
157 /* Add a uid to the list of uids. Only called on receiving side. */
158 static struct idlist *recv_add_uid(int id, char *name)
160 int id2 = name ? map_uid(id, name) : id;
163 node = add_to_list(&uidlist, id, name, map_uid(id, name));
166 rprintf(FINFO, "uid %d(%s) maps to %d\n",
167 id, name ? name : "", id2);
173 /* Add a gid to the list of gids. Only called on receiving side. */
174 static struct idlist *recv_add_gid(int id, char *name)
176 int id2 = name ? map_gid(id, name) : id;
179 if (!am_root && !is_in_group(id2))
181 node = add_to_list(&gidlist, id, name, id2);
184 rprintf(FINFO, "gid %d(%s) maps to %d\n",
185 id, name ? name : "", id2);
191 /* this function is a definate candidate for a faster algorithm */
192 static uid_t match_uid(uid_t uid)
194 static uid_t last_in, last_out;
205 for (list = uidlist; list; list = list->next) {
206 if (list->id == (int)uid)
207 return last_out = (uid_t)list->id2;
210 return last_out = uid;
213 static gid_t match_gid(gid_t gid)
215 static gid_t last_in = GID_NONE, last_out = GID_NONE;
226 for (list = gidlist; list; list = list->next) {
227 if (list->id == (int)gid)
228 return last_out = (gid_t)list->id2;
231 list = recv_add_gid(gid, NULL);
232 return last_out = list->id2;
235 /* Add a uid to the list of uids. Only called on sending side. */
236 void add_uid(uid_t uid)
240 if (uid == 0) /* don't map root */
243 for (list = uidlist; list; list = list->next) {
244 if (list->id == (int)uid)
248 add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
251 /* Add a gid to the list of gids. Only called on sending side. */
252 void add_gid(gid_t gid)
256 if (gid == 0) /* don't map root */
259 for (list = gidlist; list; list = list->next) {
260 if (list->id == (int)gid)
264 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
268 /* send a complete uid/gid mapping to the peer */
269 void send_uid_list(int f)
278 /* we send sequences of uid/byte-length/name */
279 for (list = uidlist; list; list = list->next) {
282 len = strlen(list->name);
283 write_int(f, list->id);
285 write_buf(f, list->name, len);
288 /* terminate the uid list with a 0 uid. We explicitly exclude
295 for (list = gidlist; list; list = list->next) {
298 len = strlen(list->name);
299 write_int(f, list->id);
301 write_buf(f, list->name, len);
307 /* recv a complete uid/gid mapping from the peer and map the uid/gid
308 * in the file list to local names */
309 void recv_uid_list(int f, struct file_list *flist)
314 if (preserve_uid && !numeric_ids) {
315 /* read the uid list */
316 while ((id = read_int(f)) != 0) {
317 int len = read_byte(f);
318 name = new_array(char, len+1);
320 out_of_memory("recv_uid_list");
321 read_sbuf(f, name, len);
322 recv_add_uid(id, name); /* node keeps name's memory */
327 if (preserve_gid && !numeric_ids) {
328 /* read the gid list */
329 while ((id = read_int(f)) != 0) {
330 int len = read_byte(f);
331 name = new_array(char, len+1);
333 out_of_memory("recv_uid_list");
334 read_sbuf(f, name, len);
335 recv_add_gid(id, name); /* node keeps name's memory */
339 /* now convert the uid/gid of all files in the list to the mapped
341 if (am_root && preserve_uid && !numeric_ids) {
342 for (i = 0; i < flist->count; i++)
343 flist->files[i]->uid = match_uid(flist->files[i]->uid);
345 if (preserve_gid && (!am_root || !numeric_ids)) {
346 for (i = 0; i < flist->count; i++)
347 flist->files[i]->gid = match_gid(flist->files[i]->gid);