- Added more users and some groups to one of the files we're copying.
[rsync/rsync.git] / uidlist.c
CommitLineData
ade7292a 1/*
0f78b815
WD
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
ba2133d6 6 * Copyright (C) 2004-2007 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
0f78b815
WD
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 *
e7c67065
WD
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.
0f78b815
WD
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. */
f6c34742
AT
25
26#include "rsync.h"
ab14d01a 27#include "io.h"
f6c34742 28
4f5b0756 29#ifdef HAVE_GETGROUPS
25ff0441 30# ifndef GETGROUPS_T
1df395f7
WD
31# define GETGROUPS_T gid_t
32# endif
72fc7ec5
WD
33#endif
34
84fa865c 35extern int verbose;
283887d7 36extern int am_root;
f6c34742
AT
37extern int preserve_uid;
38extern int preserve_gid;
1c3344a1 39extern int preserve_acls;
f6c34742
AT
40extern int numeric_ids;
41
42struct idlist {
43 struct idlist *next;
f6c34742 44 char *name;
d6b422a6
WD
45 id_t id, id2;
46 uint16 flags;
f6c34742
AT
47};
48
49static struct idlist *uidlist;
50static struct idlist *gidlist;
51
d6b422a6
WD
52static struct idlist *add_to_list(struct idlist **root, id_t id, char *name,
53 id_t id2, uint16 flags)
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;
d6b422a6 62 node->flags = flags;
d49def48
WD
63 *root = node;
64 return node;
f6c34742
AT
65}
66
f6c34742
AT
67/* turn a uid into a user name */
68static char *uid_to_name(uid_t uid)
69{
70 struct passwd *pass = getpwuid(uid);
d49def48
WD
71 if (pass)
72 return strdup(pass->pw_name);
f6c34742
AT
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);
d49def48
WD
80 if (grp)
81 return strdup(grp->gr_name);
f6c34742
AT
82 return NULL;
83}
84
283887d7 85static uid_t map_uid(uid_t id, char *name)
f6c34742 86{
8ef4ffd6 87 uid_t uid;
b66d0085 88 if (id != 0 && name_to_uid(name, &uid))
8ef4ffd6 89 return uid;
f6c34742
AT
90 return id;
91}
92
283887d7 93static gid_t map_gid(gid_t id, char *name)
f6c34742 94{
8ef4ffd6 95 gid_t gid;
b66d0085 96 if (id != 0 && name_to_gid(name, &gid))
8ef4ffd6 97 return gid;
f6c34742
AT
98 return id;
99}
100
5b540e86
WD
101static int is_in_group(gid_t gid)
102{
4f5b0756 103#ifdef HAVE_GETGROUPS
a2687b64 104 static gid_t last_in = GID_NONE, last_out;
5b540e86
WD
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) {
670d8abf 112 gid_t mygid = MY_GID();
f567e9b3 113 if ((ngroups = getgroups(0, NULL)) < 0)
dbd8811b 114 ngroups = 0;
72fc7ec5 115 gidset = new_array(GETGROUPS_T, ngroups+1);
f567e9b3
WD
116 if (!gidset)
117 out_of_memory("is_in_group");
72fc7ec5 118 if (ngroups > 0)
5b540e86 119 ngroups = getgroups(ngroups, gidset);
72fc7ec5
WD
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;
5b540e86 124 }
72fc7ec5
WD
125 if (n == ngroups)
126 gidset[ngroups++] = mygid;
84fa865c 127 if (verbose > 3) {
187e9c24 128 int pos;
f567e9b3
WD
129 char *gidbuf = new_array(char, ngroups*21+32);
130 if (!gidbuf)
131 out_of_memory("is_in_group");
10944395
WD
132 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
133 ngroups, ngroups == 1? "" : "s");
84fa865c 134 for (n = 0; n < ngroups; n++) {
10944395 135 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
84fa865c 136 }
187e9c24 137 rprintf(FINFO, "%s\n", gidbuf);
f567e9b3 138 free(gidbuf);
84fa865c 139 }
5b540e86
WD
140 }
141
142 last_in = gid;
5b540e86 143 for (n = 0; n < ngroups; n++) {
a2687b64
WD
144 if (gidset[n] == gid)
145 return last_out = 1;
5b540e86 146 }
a2687b64 147 return last_out = 0;
5b540e86
WD
148
149#else
a2687b64 150 static gid_t mygid = GID_NONE;
187e9c24 151 if (mygid == GID_NONE) {
670d8abf 152 mygid = MY_GID();
187e9c24 153 if (verbose > 3)
d6b422a6 154 rprintf(FINFO, "process has gid %u\n", (unsigned)mygid);
187e9c24 155 }
a2687b64 156 return gid == mygid;
5b540e86
WD
157#endif
158}
159
d49def48 160/* Add a uid to the list of uids. Only called on receiving side. */
d6b422a6 161static struct idlist *recv_add_uid(uid_t id, char *name)
d49def48 162{
283887d7 163 uid_t id2 = name ? map_uid(id, name) : id;
d49def48
WD
164 struct idlist *node;
165
d6b422a6 166 node = add_to_list(&uidlist, id, name, id2, 0);
d49def48
WD
167
168 if (verbose > 3) {
d6b422a6
WD
169 rprintf(FINFO, "uid %u(%s) maps to %u\n",
170 (unsigned)id, name ? name : "", (unsigned)id2);
d49def48
WD
171 }
172
d6b422a6 173 return node;
d49def48
WD
174}
175
176/* Add a gid to the list of gids. Only called on receiving side. */
d6b422a6 177static struct idlist *recv_add_gid(gid_t id, char *name)
d49def48 178{
283887d7 179 gid_t id2 = name ? map_gid(id, name) : id;
d49def48
WD
180 struct idlist *node;
181
d6b422a6
WD
182 node = add_to_list(&gidlist, id, name, id2,
183 !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
d49def48
WD
184
185 if (verbose > 3) {
d6b422a6
WD
186 rprintf(FINFO, "gid %u(%s) maps to %u\n",
187 (unsigned)id, name ? name : "", (unsigned)id2);
d49def48
WD
188 }
189
d6b422a6 190 return node;
d49def48
WD
191}
192
ade7292a 193/* this function is a definate candidate for a faster algorithm */
496c809f 194uid_t match_uid(uid_t uid)
ade7292a
WD
195{
196 static uid_t last_in, last_out;
d49def48
WD
197 struct idlist *list;
198
199 if (uid == 0)
200 return 0;
ade7292a
WD
201
202 if (uid == last_in)
203 return last_out;
204
205 last_in = uid;
206
d49def48 207 for (list = uidlist; list; list = list->next) {
d6b422a6
WD
208 if (list->id == uid)
209 return last_out = list->id2;
ade7292a
WD
210 }
211
d49def48 212 return last_out = uid;
ade7292a
WD
213}
214
d6b422a6 215gid_t match_gid(gid_t gid, uint16 *flags_ptr)
f6c34742 216{
a2687b64 217 static gid_t last_in = GID_NONE, last_out = GID_NONE;
d49def48
WD
218 struct idlist *list;
219
01363a24
WD
220 if (gid == GID_NONE)
221 return GID_NONE;
f6c34742 222
a2687b64
WD
223 if (gid == last_in)
224 return last_out;
f6c34742
AT
225
226 last_in = gid;
227
d49def48 228 for (list = gidlist; list; list = list->next) {
d6b422a6
WD
229 if (list->id == gid)
230 break;
f6c34742 231 }
d49def48 232
d6b422a6
WD
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;
f6c34742
AT
239}
240
d49def48 241/* Add a uid to the list of uids. Only called on sending side. */
496c809f 242char *add_uid(uid_t uid)
f6c34742 243{
d49def48 244 struct idlist *list;
496c809f 245 struct idlist *node;
f6c34742 246
d49def48 247 if (uid == 0) /* don't map root */
496c809f 248 return NULL;
f6c34742 249
d49def48 250 for (list = uidlist; list; list = list->next) {
d6b422a6 251 if (list->id == uid)
496c809f 252 return NULL;
f6c34742
AT
253 }
254
d6b422a6 255 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
496c809f 256 return node->name;
f6c34742
AT
257}
258
d49def48 259/* Add a gid to the list of gids. Only called on sending side. */
496c809f 260char *add_gid(gid_t gid)
f6c34742 261{
d49def48 262 struct idlist *list;
496c809f 263 struct idlist *node;
f6c34742 264
d49def48 265 if (gid == 0) /* don't map root */
496c809f 266 return NULL;
f6c34742 267
d49def48 268 for (list = gidlist; list; list = list->next) {
d6b422a6 269 if (list->id == gid)
496c809f 270 return NULL;
f6c34742
AT
271 }
272
d6b422a6 273 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
496c809f 274 return node->name;
f6c34742
AT
275}
276
f6c34742 277/* send a complete uid/gid mapping to the peer */
d6b422a6 278void send_id_list(int f)
f6c34742
AT
279{
280 struct idlist *list;
281
1c3344a1 282 if (preserve_uid || preserve_acls) {
d49def48 283 int len;
f6c34742 284 /* we send sequences of uid/byte-length/name */
d49def48
WD
285 for (list = uidlist; list; list = list->next) {
286 if (!list->name)
287 continue;
288 len = strlen(list->name);
f31514ad 289 write_varint30(f, list->id);
f6c34742
AT
290 write_byte(f, len);
291 write_buf(f, list->name, len);
f6c34742
AT
292 }
293
294 /* terminate the uid list with a 0 uid. We explicitly exclude
84fa865c 295 * 0 from the list */
f31514ad 296 write_varint30(f, 0);
f6c34742
AT
297 }
298
1c3344a1 299 if (preserve_gid || preserve_acls) {
d49def48
WD
300 int len;
301 for (list = gidlist; list; list = list->next) {
302 if (!list->name)
303 continue;
304 len = strlen(list->name);
f31514ad 305 write_varint30(f, list->id);
f6c34742
AT
306 write_byte(f, len);
307 write_buf(f, list->name, len);
f6c34742 308 }
f31514ad 309 write_varint30(f, 0);
f6c34742
AT
310 }
311}
312
496c809f 313uid_t recv_user_name(int f, uid_t uid)
283887d7 314{
d6b422a6 315 struct idlist *node;
283887d7
WD
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);
d6b422a6
WD
321 node = recv_add_uid(uid, name); /* node keeps name's memory */
322 return node->id2;
283887d7
WD
323}
324
d6b422a6 325gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
283887d7 326{
d6b422a6 327 struct idlist *node;
283887d7
WD
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);
d6b422a6
WD
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;
283887d7
WD
337}
338
f6c34742 339/* recv a complete uid/gid mapping from the peer and map the uid/gid
84fa865c 340 * in the file list to local names */
d6b422a6 341void recv_id_list(int f, struct file_list *flist)
f6c34742 342{
d6b422a6
WD
343 id_t id;
344 int i;
f6c34742 345
1c3344a1 346 if ((preserve_uid || preserve_acls) && !numeric_ids) {
f6c34742 347 /* read the uid list */
f31514ad 348 while ((id = read_varint30(f)) != 0)
d6b422a6 349 recv_user_name(f, id);
f6c34742
AT
350 }
351
1c3344a1 352 if ((preserve_gid || preserve_acls) && !numeric_ids) {
d49def48 353 /* read the gid list */
f31514ad 354 while ((id = read_varint30(f)) != 0)
d6b422a6 355 recv_group_name(f, id, NULL);
f6c34742
AT
356 }
357
a217c453 358 /* Now convert all the uids/gids from sender values to our values. */
1c3344a1 359#ifdef SUPPORT_ACLS
a217c453
WD
360 if (preserve_acls && !numeric_ids)
361 match_acl_ids();
1c3344a1 362#endif
d49def48 363 if (am_root && preserve_uid && !numeric_ids) {
7b6fa00f 364 for (i = 0; i < flist->count; i++)
d6b422a6 365 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
d49def48
WD
366 }
367 if (preserve_gid && (!am_root || !numeric_ids)) {
d6b422a6
WD
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 }
5e58e3f9 372 }
f6c34742 373}