Added testing of the --itemize-changes output for devices.
[rsync/rsync.git] / uidlist.c
CommitLineData
ade7292a 1/*
f6c34742
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
ade7292a 4
f6c34742
AT
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.
ade7292a 9
f6c34742
AT
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.
ade7292a 14
f6c34742
AT
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.
18*/
19
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
d49def48 22 the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
f6c34742
AT
23 are special.
24*/
25
26#include "rsync.h"
27
4f5b0756 28#ifdef HAVE_GETGROUPS
25ff0441 29# ifndef GETGROUPS_T
1df395f7
WD
30# define GETGROUPS_T gid_t
31# endif
72fc7ec5
WD
32#endif
33
84fa865c 34extern int verbose;
f6c34742
AT
35extern int preserve_uid;
36extern int preserve_gid;
37extern int numeric_ids;
460f6b99 38extern int am_root;
f6c34742
AT
39
40struct idlist {
41 struct idlist *next;
42 int id, id2;
43 char *name;
44};
45
46static struct idlist *uidlist;
47static struct idlist *gidlist;
48
5e58e3f9
WD
49static struct id_pair *pair_list;
50static int pair_cnt = 0, pair_alloc = 0;
51
d49def48
WD
52static struct idlist *add_to_list(struct idlist **root, int id, char *name,
53 int id2)
f6c34742 54{
d49def48
WD
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 *root = node;
63 return node;
f6c34742
AT
64}
65
f6c34742
AT
66/* turn a uid into a user name */
67static char *uid_to_name(uid_t uid)
68{
69 struct passwd *pass = getpwuid(uid);
d49def48
WD
70 if (pass)
71 return strdup(pass->pw_name);
f6c34742
AT
72 return NULL;
73}
74
75/* turn a gid into a group name */
76static char *gid_to_name(gid_t gid)
77{
78 struct group *grp = getgrgid(gid);
d49def48
WD
79 if (grp)
80 return strdup(grp->gr_name);
f6c34742
AT
81 return NULL;
82}
83
f6c34742
AT
84static int map_uid(int id, char *name)
85{
8ef4ffd6 86 uid_t uid;
b66d0085 87 if (id != 0 && name_to_uid(name, &uid))
8ef4ffd6 88 return uid;
f6c34742
AT
89 return id;
90}
91
92static int map_gid(int id, char *name)
93{
8ef4ffd6 94 gid_t gid;
b66d0085 95 if (id != 0 && name_to_gid(name, &gid))
8ef4ffd6 96 return gid;
f6c34742
AT
97 return id;
98}
99
5b540e86
WD
100static int is_in_group(gid_t gid)
101{
4f5b0756 102#ifdef HAVE_GETGROUPS
a2687b64 103 static gid_t last_in = GID_NONE, last_out;
5b540e86
WD
104 static int ngroups = -2;
105 static GETGROUPS_T *gidset;
106 int n;
107
108 if (gid == last_in)
109 return last_out;
110 if (ngroups < -1) {
670d8abf 111 gid_t mygid = MY_GID();
f567e9b3 112 if ((ngroups = getgroups(0, NULL)) < 0)
dbd8811b 113 ngroups = 0;
72fc7ec5 114 gidset = new_array(GETGROUPS_T, ngroups+1);
f567e9b3
WD
115 if (!gidset)
116 out_of_memory("is_in_group");
72fc7ec5 117 if (ngroups > 0)
5b540e86 118 ngroups = getgroups(ngroups, gidset);
72fc7ec5
WD
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)
122 break;
5b540e86 123 }
72fc7ec5
WD
124 if (n == ngroups)
125 gidset[ngroups++] = mygid;
84fa865c 126 if (verbose > 3) {
187e9c24 127 int pos;
f567e9b3
WD
128 char *gidbuf = new_array(char, ngroups*21+32);
129 if (!gidbuf)
130 out_of_memory("is_in_group");
187e9c24
WD
131 sprintf(gidbuf, "process has %d gid%s: ",
132 ngroups, ngroups == 1? "" : "s");
133 pos = strlen(gidbuf);
84fa865c 134 for (n = 0; n < ngroups; n++) {
d49def48 135 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
187e9c24 136 pos += strlen(gidbuf+pos);
84fa865c 137 }
187e9c24 138 rprintf(FINFO, "%s\n", gidbuf);
f567e9b3 139 free(gidbuf);
84fa865c 140 }
5b540e86
WD
141 }
142
143 last_in = gid;
5b540e86 144 for (n = 0; n < ngroups; n++) {
a2687b64
WD
145 if (gidset[n] == gid)
146 return last_out = 1;
5b540e86 147 }
a2687b64 148 return last_out = 0;
5b540e86
WD
149
150#else
a2687b64 151 static gid_t mygid = GID_NONE;
187e9c24 152 if (mygid == GID_NONE) {
670d8abf 153 mygid = MY_GID();
187e9c24 154 if (verbose > 3)
d49def48 155 rprintf(FINFO, "process has gid %d\n", (int)mygid);
187e9c24 156 }
a2687b64 157 return gid == mygid;
5b540e86
WD
158#endif
159}
160
d49def48
WD
161/* Add a uid to the list of uids. Only called on receiving side. */
162static struct idlist *recv_add_uid(int id, char *name)
163{
164 int id2 = name ? map_uid(id, name) : id;
165 struct idlist *node;
166
b66d0085 167 node = add_to_list(&uidlist, id, name, id2);
d49def48
WD
168
169 if (verbose > 3) {
170 rprintf(FINFO, "uid %d(%s) maps to %d\n",
171 id, name ? name : "", id2);
172 }
173
174 return node;
175}
176
177/* Add a gid to the list of gids. Only called on receiving side. */
178static struct idlist *recv_add_gid(int id, char *name)
179{
180 int id2 = name ? map_gid(id, name) : id;
181 struct idlist *node;
182
183 if (!am_root && !is_in_group(id2))
184 id2 = GID_NONE;
185 node = add_to_list(&gidlist, id, name, id2);
186
187 if (verbose > 3) {
188 rprintf(FINFO, "gid %d(%s) maps to %d\n",
189 id, name ? name : "", id2);
190 }
191
192 return node;
193}
194
ade7292a
WD
195/* this function is a definate candidate for a faster algorithm */
196static uid_t match_uid(uid_t uid)
197{
198 static uid_t last_in, last_out;
d49def48
WD
199 struct idlist *list;
200
201 if (uid == 0)
202 return 0;
ade7292a
WD
203
204 if (uid == last_in)
205 return last_out;
206
207 last_in = uid;
208
d49def48
WD
209 for (list = uidlist; list; list = list->next) {
210 if (list->id == (int)uid)
211 return last_out = (uid_t)list->id2;
ade7292a
WD
212 }
213
d49def48 214 return last_out = uid;
ade7292a
WD
215}
216
f6c34742
AT
217static gid_t match_gid(gid_t gid)
218{
a2687b64 219 static gid_t last_in = GID_NONE, last_out = GID_NONE;
d49def48
WD
220 struct idlist *list;
221
01363a24
WD
222 if (gid == GID_NONE)
223 return GID_NONE;
f6c34742 224
a2687b64
WD
225 if (gid == last_in)
226 return last_out;
f6c34742
AT
227
228 last_in = gid;
229
d49def48
WD
230 for (list = gidlist; list; list = list->next) {
231 if (list->id == (int)gid)
232 return last_out = (gid_t)list->id2;
f6c34742 233 }
d49def48
WD
234
235 list = recv_add_gid(gid, NULL);
236 return last_out = list->id2;
f6c34742
AT
237}
238
d49def48 239/* Add a uid to the list of uids. Only called on sending side. */
f6c34742
AT
240void add_uid(uid_t uid)
241{
d49def48 242 struct idlist *list;
f6c34742 243
d49def48 244 if (uid == 0) /* don't map root */
f6c34742 245 return;
f6c34742 246
d49def48
WD
247 for (list = uidlist; list; list = list->next) {
248 if (list->id == (int)uid)
249 return;
f6c34742
AT
250 }
251
d49def48 252 add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
f6c34742
AT
253}
254
d49def48 255/* Add a gid to the list of gids. Only called on sending side. */
f6c34742
AT
256void add_gid(gid_t gid)
257{
d49def48 258 struct idlist *list;
f6c34742 259
d49def48 260 if (gid == 0) /* don't map root */
f6c34742 261 return;
f6c34742 262
d49def48
WD
263 for (list = gidlist; list; list = list->next) {
264 if (list->id == (int)gid)
265 return;
f6c34742
AT
266 }
267
d49def48 268 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
f6c34742
AT
269}
270
271
272/* send a complete uid/gid mapping to the peer */
273void send_uid_list(int f)
274{
275 struct idlist *list;
276
d49def48
WD
277 if (numeric_ids)
278 return;
f6c34742
AT
279
280 if (preserve_uid) {
d49def48 281 int len;
f6c34742 282 /* we send sequences of uid/byte-length/name */
d49def48
WD
283 for (list = uidlist; list; list = list->next) {
284 if (!list->name)
285 continue;
286 len = strlen(list->name);
f6c34742
AT
287 write_int(f, list->id);
288 write_byte(f, len);
289 write_buf(f, list->name, len);
f6c34742
AT
290 }
291
292 /* terminate the uid list with a 0 uid. We explicitly exclude
84fa865c 293 * 0 from the list */
f6c34742
AT
294 write_int(f, 0);
295 }
296
297 if (preserve_gid) {
d49def48
WD
298 int len;
299 for (list = gidlist; list; list = list->next) {
300 if (!list->name)
301 continue;
302 len = strlen(list->name);
f6c34742
AT
303 write_int(f, list->id);
304 write_byte(f, len);
305 write_buf(f, list->name, len);
f6c34742
AT
306 }
307 write_int(f, 0);
308 }
309}
310
311/* recv a complete uid/gid mapping from the peer and map the uid/gid
84fa865c 312 * in the file list to local names */
5e58e3f9 313void recv_uid_list(int f)
f6c34742
AT
314{
315 int id, i;
316 char *name;
f6c34742 317
d49def48 318 if (preserve_uid && !numeric_ids) {
f6c34742 319 /* read the uid list */
5b540e86 320 while ((id = read_int(f)) != 0) {
f6c34742 321 int len = read_byte(f);
58cadc86 322 name = new_array(char, len+1);
d49def48
WD
323 if (!name)
324 out_of_memory("recv_uid_list");
575f2fca 325 read_sbuf(f, name, len);
d49def48 326 recv_add_uid(id, name); /* node keeps name's memory */
84fa865c 327 }
f6c34742
AT
328 }
329
d49def48
WD
330 if (preserve_gid && !numeric_ids) {
331 /* read the gid list */
5b540e86 332 while ((id = read_int(f)) != 0) {
f6c34742 333 int len = read_byte(f);
58cadc86 334 name = new_array(char, len+1);
d49def48
WD
335 if (!name)
336 out_of_memory("recv_uid_list");
575f2fca 337 read_sbuf(f, name, len);
d49def48 338 recv_add_gid(id, name); /* node keeps name's memory */
84fa865c 339 }
f6c34742
AT
340 }
341
5e58e3f9 342 /* Now convert the id_pair array over to mapped uid/gid values. */
d49def48 343 if (am_root && preserve_uid && !numeric_ids) {
5e58e3f9
WD
344 for (i = 0; i < pair_cnt; i++)
345 pair_list[i].uid = match_uid(pair_list[i].uid);
d49def48
WD
346 }
347 if (preserve_gid && (!am_root || !numeric_ids)) {
5e58e3f9
WD
348 for (i = 0; i < pair_cnt; i++)
349 pair_list[i].gid = match_gid(pair_list[i].gid);
350 }
351}
352
353struct id_pair *id_pair(uid_t uid, gid_t gid)
354{
67e4043e
WD
355 static int j = 0;
356
357 if (pair_cnt) {
358 int start = j;
67e4043e
WD
359 /* We start our search where we left off because
360 * the IDs usually come in clumps. */
361 do {
67e4043e
WD
362 if (uid == pair_list[j].uid && gid == pair_list[j].gid)
363 return pair_list + j;
364 if (++j == pair_cnt)
365 j = 0;
366 } while (j != start);
5e58e3f9
WD
367 }
368
369 if (pair_cnt == pair_alloc) {
370 pair_alloc += 128;
371 pair_list = realloc_array(pair_list, struct id_pair,
372 pair_alloc);
f6c34742 373 }
67e4043e
WD
374
375 j = pair_cnt++;
376 pair_list[j].uid = uid;
377 pair_list[j].gid = gid;
378
379 return pair_list + j;
f6c34742 380}