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