A few minor tweaks to handle the newest variable names.
[rsync/rsync.git] / uidlist.c
... / ...
CommitLineData
1/*
2 * Handle the mapping of uid/gid and user/group names between systems.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004, 2005, 2006 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23/* If the source username/group does not exist on the target then use
24 * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
25 * are special. */
26
27#include "rsync.h"
28
29#ifdef HAVE_GETGROUPS
30# ifndef GETGROUPS_T
31# define GETGROUPS_T gid_t
32# endif
33#endif
34
35extern int verbose;
36extern int am_root;
37extern int preserve_uid;
38extern int preserve_gid;
39extern int numeric_ids;
40
41struct idlist {
42 struct idlist *next;
43 int id, id2;
44 char *name;
45};
46
47static struct idlist *uidlist;
48static struct idlist *gidlist;
49
50static struct idlist *add_to_list(struct idlist **root, int id, char *name,
51 int id2)
52{
53 struct idlist *node = new(struct idlist);
54 if (!node)
55 out_of_memory("add_to_list");
56 node->next = *root;
57 node->name = name;
58 node->id = id;
59 node->id2 = id2;
60 *root = node;
61 return node;
62}
63
64/* turn a uid into a user name */
65static char *uid_to_name(uid_t uid)
66{
67 struct passwd *pass = getpwuid(uid);
68 if (pass)
69 return strdup(pass->pw_name);
70 return NULL;
71}
72
73/* turn a gid into a group name */
74static char *gid_to_name(gid_t gid)
75{
76 struct group *grp = getgrgid(gid);
77 if (grp)
78 return strdup(grp->gr_name);
79 return NULL;
80}
81
82static uid_t map_uid(uid_t id, char *name)
83{
84 uid_t uid;
85 if (id != 0 && name_to_uid(name, &uid))
86 return uid;
87 return id;
88}
89
90static gid_t map_gid(gid_t id, char *name)
91{
92 gid_t gid;
93 if (id != 0 && name_to_gid(name, &gid))
94 return gid;
95 return id;
96}
97
98static int is_in_group(gid_t gid)
99{
100#ifdef HAVE_GETGROUPS
101 static gid_t last_in = GID_NONE, last_out;
102 static int ngroups = -2;
103 static GETGROUPS_T *gidset;
104 int n;
105
106 if (gid == last_in)
107 return last_out;
108 if (ngroups < -1) {
109 gid_t mygid = MY_GID();
110 if ((ngroups = getgroups(0, NULL)) < 0)
111 ngroups = 0;
112 gidset = new_array(GETGROUPS_T, ngroups+1);
113 if (!gidset)
114 out_of_memory("is_in_group");
115 if (ngroups > 0)
116 ngroups = getgroups(ngroups, gidset);
117 /* The default gid might not be in the list on some systems. */
118 for (n = 0; n < ngroups; n++) {
119 if (gidset[n] == mygid)
120 break;
121 }
122 if (n == ngroups)
123 gidset[ngroups++] = mygid;
124 if (verbose > 3) {
125 int pos;
126 char *gidbuf = new_array(char, ngroups*21+32);
127 if (!gidbuf)
128 out_of_memory("is_in_group");
129 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
130 ngroups, ngroups == 1? "" : "s");
131 for (n = 0; n < ngroups; n++) {
132 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
133 }
134 rprintf(FINFO, "%s\n", gidbuf);
135 free(gidbuf);
136 }
137 }
138
139 last_in = gid;
140 for (n = 0; n < ngroups; n++) {
141 if (gidset[n] == gid)
142 return last_out = 1;
143 }
144 return last_out = 0;
145
146#else
147 static gid_t mygid = GID_NONE;
148 if (mygid == GID_NONE) {
149 mygid = MY_GID();
150 if (verbose > 3)
151 rprintf(FINFO, "process has gid %d\n", (int)mygid);
152 }
153 return gid == mygid;
154#endif
155}
156
157/* Add a uid to the list of uids. Only called on receiving side. */
158static uid_t recv_add_uid(uid_t id, char *name)
159{
160 uid_t id2 = name ? map_uid(id, name) : id;
161 struct idlist *node;
162
163 node = add_to_list(&uidlist, (int)id, name, (int)id2);
164
165 if (verbose > 3) {
166 rprintf(FINFO, "uid %d(%s) maps to %d\n",
167 (int)id, name ? name : "", (int)id2);
168 }
169
170 return id2;
171}
172
173/* Add a gid to the list of gids. Only called on receiving side. */
174static gid_t recv_add_gid(gid_t id, char *name)
175{
176 gid_t id2 = name ? map_gid(id, name) : id;
177 struct idlist *node;
178
179 if (!am_root && !is_in_group(id2))
180 id2 = GID_NONE;
181 node = add_to_list(&gidlist, (int)id, name, (int)id2);
182
183 if (verbose > 3) {
184 rprintf(FINFO, "gid %d(%s) maps to %d\n",
185 (int)id, name ? name : "", (int)id2);
186 }
187
188 return id2;
189}
190
191/* this function is a definate candidate for a faster algorithm */
192static uid_t match_uid(uid_t uid)
193{
194 static uid_t last_in, last_out;
195 struct idlist *list;
196
197 if (uid == 0)
198 return 0;
199
200 if (uid == last_in)
201 return last_out;
202
203 last_in = uid;
204
205 for (list = uidlist; list; list = list->next) {
206 if (list->id == (int)uid)
207 return last_out = (uid_t)list->id2;
208 }
209
210 return last_out = uid;
211}
212
213static gid_t match_gid(gid_t gid)
214{
215 static gid_t last_in = GID_NONE, last_out = GID_NONE;
216 struct idlist *list;
217
218 if (gid == GID_NONE)
219 return GID_NONE;
220
221 if (gid == last_in)
222 return last_out;
223
224 last_in = gid;
225
226 for (list = gidlist; list; list = list->next) {
227 if (list->id == (int)gid)
228 return last_out = (gid_t)list->id2;
229 }
230
231 return last_out = recv_add_gid(gid, NULL);
232}
233
234/* Add a uid to the list of uids. Only called on sending side. */
235void add_uid(uid_t uid)
236{
237 struct idlist *list;
238
239 if (uid == 0) /* don't map root */
240 return;
241
242 for (list = uidlist; list; list = list->next) {
243 if (list->id == (int)uid)
244 return;
245 }
246
247 add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
248}
249
250/* Add a gid to the list of gids. Only called on sending side. */
251void add_gid(gid_t gid)
252{
253 struct idlist *list;
254
255 if (gid == 0) /* don't map root */
256 return;
257
258 for (list = gidlist; list; list = list->next) {
259 if (list->id == (int)gid)
260 return;
261 }
262
263 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
264}
265
266/* send a complete uid/gid mapping to the peer */
267void send_uid_list(int f)
268{
269 struct idlist *list;
270
271 if (preserve_uid) {
272 int len;
273 /* we send sequences of uid/byte-length/name */
274 for (list = uidlist; list; list = list->next) {
275 if (!list->name)
276 continue;
277 len = strlen(list->name);
278 write_int(f, list->id);
279 write_byte(f, len);
280 write_buf(f, list->name, len);
281 }
282
283 /* terminate the uid list with a 0 uid. We explicitly exclude
284 * 0 from the list */
285 write_int(f, 0);
286 }
287
288 if (preserve_gid) {
289 int len;
290 for (list = gidlist; list; list = list->next) {
291 if (!list->name)
292 continue;
293 len = strlen(list->name);
294 write_int(f, list->id);
295 write_byte(f, len);
296 write_buf(f, list->name, len);
297 }
298 write_int(f, 0);
299 }
300}
301
302static uid_t recv_user_name(int f, uid_t uid)
303{
304 int len = read_byte(f);
305 char *name = new_array(char, len+1);
306 if (!name)
307 out_of_memory("recv_user_name");
308 read_sbuf(f, name, len);
309 return recv_add_uid(uid, name); /* node keeps name's memory */
310}
311
312static gid_t recv_group_name(int f, gid_t gid)
313{
314 int len = read_byte(f);
315 char *name = new_array(char, len+1);
316 if (!name)
317 out_of_memory("recv_group_name");
318 read_sbuf(f, name, len);
319 return recv_add_gid(gid, name); /* node keeps name's memory */
320}
321
322/* recv a complete uid/gid mapping from the peer and map the uid/gid
323 * in the file list to local names */
324void recv_uid_list(int f, struct file_list *flist)
325{
326 int id, i;
327
328 if (preserve_uid && !numeric_ids) {
329 /* read the uid list */
330 while ((id = read_int(f)) != 0)
331 recv_user_name(f, (uid_t)id);
332 }
333
334 if (preserve_gid && !numeric_ids) {
335 /* read the gid list */
336 while ((id = read_int(f)) != 0)
337 recv_group_name(f, (gid_t)id);
338 }
339
340 /* Now convert all the uids/gids from sender values to our values. */
341 if (am_root && preserve_uid && !numeric_ids) {
342 for (i = 0; i < flist->count; i++)
343 F_UID(flist->files[i]) = match_uid(F_UID(flist->files[i]));
344 }
345 if (preserve_gid && (!am_root || !numeric_ids)) {
346 for (i = 0; i < flist->count; i++)
347 F_GID(flist->files[i]) = match_gid(F_GID(flist->files[i]));
348 }
349}