Fix the %P logfile escape inside a chroot.
[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
d3d07a5e 6 * Copyright (C) 2004-2008 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
0f78b815
WD
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 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
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
283887d7 29extern int am_root;
f6c34742
AT
30extern int preserve_uid;
31extern int preserve_gid;
1c3344a1 32extern int preserve_acls;
f6c34742
AT
33extern int numeric_ids;
34
142a5e7b
WD
35#ifdef HAVE_GETGROUPS
36# ifndef GETGROUPS_T
37# define GETGROUPS_T gid_t
38# endif
39#endif
40
41#define GID_NONE ((gid_t)-1)
42
f6c34742
AT
43struct idlist {
44 struct idlist *next;
c78cb8f3 45 const char *name;
d6b422a6
WD
46 id_t id, id2;
47 uint16 flags;
f6c34742
AT
48};
49
50static struct idlist *uidlist;
51static struct idlist *gidlist;
52
c78cb8f3 53static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
d6b422a6 54 id_t id2, uint16 flags)
f6c34742 55{
d49def48
WD
56 struct idlist *node = new(struct idlist);
57 if (!node)
58 out_of_memory("add_to_list");
59 node->next = *root;
60 node->name = name;
61 node->id = id;
62 node->id2 = id2;
d6b422a6 63 node->flags = flags;
d49def48
WD
64 *root = node;
65 return node;
f6c34742
AT
66}
67
f6c34742 68/* turn a uid into a user name */
c78cb8f3 69static const char *uid_to_name(uid_t uid)
f6c34742
AT
70{
71 struct passwd *pass = getpwuid(uid);
d49def48
WD
72 if (pass)
73 return strdup(pass->pw_name);
f6c34742
AT
74 return NULL;
75}
76
77/* turn a gid into a group name */
c78cb8f3 78static const char *gid_to_name(gid_t gid)
f6c34742
AT
79{
80 struct group *grp = getgrgid(gid);
d49def48
WD
81 if (grp)
82 return strdup(grp->gr_name);
f6c34742
AT
83 return NULL;
84}
85
7b4f4865 86static uid_t map_uid(uid_t id, const char *name)
f6c34742 87{
8ef4ffd6 88 uid_t uid;
b66d0085 89 if (id != 0 && name_to_uid(name, &uid))
8ef4ffd6 90 return uid;
f6c34742
AT
91 return id;
92}
93
7b4f4865 94static gid_t map_gid(gid_t id, const char *name)
f6c34742 95{
8ef4ffd6 96 gid_t gid;
b66d0085 97 if (id != 0 && name_to_gid(name, &gid))
8ef4ffd6 98 return gid;
f6c34742
AT
99 return id;
100}
101
5b540e86
WD
102static int is_in_group(gid_t gid)
103{
4f5b0756 104#ifdef HAVE_GETGROUPS
a2687b64 105 static gid_t last_in = GID_NONE, last_out;
5b540e86
WD
106 static int ngroups = -2;
107 static GETGROUPS_T *gidset;
108 int n;
109
110 if (gid == last_in)
111 return last_out;
112 if (ngroups < -1) {
670d8abf 113 gid_t mygid = MY_GID();
f567e9b3 114 if ((ngroups = getgroups(0, NULL)) < 0)
dbd8811b 115 ngroups = 0;
72fc7ec5 116 gidset = new_array(GETGROUPS_T, ngroups+1);
f567e9b3
WD
117 if (!gidset)
118 out_of_memory("is_in_group");
72fc7ec5 119 if (ngroups > 0)
5b540e86 120 ngroups = getgroups(ngroups, gidset);
72fc7ec5
WD
121 /* The default gid might not be in the list on some systems. */
122 for (n = 0; n < ngroups; n++) {
123 if (gidset[n] == mygid)
124 break;
5b540e86 125 }
72fc7ec5
WD
126 if (n == ngroups)
127 gidset[ngroups++] = mygid;
951e826b 128 if (DEBUG_GTE(OWN, 2)) {
187e9c24 129 int pos;
f567e9b3
WD
130 char *gidbuf = new_array(char, ngroups*21+32);
131 if (!gidbuf)
132 out_of_memory("is_in_group");
10944395
WD
133 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
134 ngroups, ngroups == 1? "" : "s");
84fa865c 135 for (n = 0; n < ngroups; n++) {
10944395 136 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
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();
951e826b 154 if (DEBUG_GTE(OWN, 2))
d6b422a6 155 rprintf(FINFO, "process has gid %u\n", (unsigned)mygid);
187e9c24 156 }
a2687b64 157 return gid == mygid;
5b540e86
WD
158#endif
159}
160
d49def48 161/* Add a uid to the list of uids. Only called on receiving side. */
7b4f4865 162static struct idlist *recv_add_uid(uid_t id, const char *name)
d49def48 163{
283887d7 164 uid_t id2 = name ? map_uid(id, name) : id;
d49def48
WD
165 struct idlist *node;
166
d6b422a6 167 node = add_to_list(&uidlist, id, name, id2, 0);
d49def48 168
951e826b 169 if (DEBUG_GTE(OWN, 2)) {
d6b422a6
WD
170 rprintf(FINFO, "uid %u(%s) maps to %u\n",
171 (unsigned)id, name ? name : "", (unsigned)id2);
d49def48
WD
172 }
173
d6b422a6 174 return node;
d49def48
WD
175}
176
177/* Add a gid to the list of gids. Only called on receiving side. */
7b4f4865 178static struct idlist *recv_add_gid(gid_t id, const char *name)
d49def48 179{
283887d7 180 gid_t id2 = name ? map_gid(id, name) : id;
d49def48
WD
181 struct idlist *node;
182
d6b422a6
WD
183 node = add_to_list(&gidlist, id, name, id2,
184 !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
d49def48 185
951e826b 186 if (DEBUG_GTE(OWN, 2)) {
d6b422a6
WD
187 rprintf(FINFO, "gid %u(%s) maps to %u\n",
188 (unsigned)id, name ? name : "", (unsigned)id2);
d49def48
WD
189 }
190
d6b422a6 191 return node;
d49def48
WD
192}
193
ade7292a 194/* this function is a definate candidate for a faster algorithm */
496c809f 195uid_t match_uid(uid_t uid)
ade7292a
WD
196{
197 static uid_t last_in, last_out;
d49def48
WD
198 struct idlist *list;
199
200 if (uid == 0)
201 return 0;
ade7292a
WD
202
203 if (uid == last_in)
204 return last_out;
205
206 last_in = uid;
207
d49def48 208 for (list = uidlist; list; list = list->next) {
d6b422a6
WD
209 if (list->id == uid)
210 return last_out = list->id2;
ade7292a
WD
211 }
212
d49def48 213 return last_out = uid;
ade7292a
WD
214}
215
d6b422a6 216gid_t match_gid(gid_t gid, uint16 *flags_ptr)
f6c34742 217{
4504b225 218 static struct idlist *last = NULL;
d49def48
WD
219 struct idlist *list;
220
4504b225
WD
221 if (last && gid == last->id)
222 list = last;
223 else {
224 for (list = gidlist; list; list = list->next) {
225 if (list->id == gid)
226 break;
227 }
228 if (!list)
fe8d61e5 229 list = recv_add_gid(gid, NULL);
4504b225 230 last = list;
f6c34742 231 }
d49def48 232
d6b422a6
WD
233 if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
234 *flags_ptr |= FLAG_SKIP_GROUP;
4504b225 235 return list->id2;
f6c34742
AT
236}
237
d49def48 238/* Add a uid to the list of uids. Only called on sending side. */
c78cb8f3 239const char *add_uid(uid_t uid)
f6c34742 240{
d49def48 241 struct idlist *list;
496c809f 242 struct idlist *node;
f6c34742 243
d49def48 244 if (uid == 0) /* don't map root */
496c809f 245 return NULL;
f6c34742 246
d49def48 247 for (list = uidlist; list; list = list->next) {
d6b422a6 248 if (list->id == uid)
496c809f 249 return NULL;
f6c34742
AT
250 }
251
d6b422a6 252 node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
496c809f 253 return node->name;
f6c34742
AT
254}
255
d49def48 256/* Add a gid to the list of gids. Only called on sending side. */
c78cb8f3 257const char *add_gid(gid_t gid)
f6c34742 258{
d49def48 259 struct idlist *list;
496c809f 260 struct idlist *node;
f6c34742 261
d49def48 262 if (gid == 0) /* don't map root */
496c809f 263 return NULL;
f6c34742 264
d49def48 265 for (list = gidlist; list; list = list->next) {
d6b422a6 266 if (list->id == gid)
496c809f 267 return NULL;
f6c34742
AT
268 }
269
d6b422a6 270 node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
496c809f 271 return node->name;
f6c34742
AT
272}
273
f6c34742 274/* send a complete uid/gid mapping to the peer */
d6b422a6 275void send_id_list(int f)
f6c34742
AT
276{
277 struct idlist *list;
278
1c3344a1 279 if (preserve_uid || preserve_acls) {
d49def48 280 int len;
f6c34742 281 /* we send sequences of uid/byte-length/name */
d49def48
WD
282 for (list = uidlist; list; list = list->next) {
283 if (!list->name)
284 continue;
285 len = strlen(list->name);
f31514ad 286 write_varint30(f, list->id);
f6c34742
AT
287 write_byte(f, len);
288 write_buf(f, list->name, len);
f6c34742
AT
289 }
290
291 /* terminate the uid list with a 0 uid. We explicitly exclude
84fa865c 292 * 0 from the list */
f31514ad 293 write_varint30(f, 0);
f6c34742
AT
294 }
295
1c3344a1 296 if (preserve_gid || preserve_acls) {
d49def48
WD
297 int len;
298 for (list = gidlist; list; list = list->next) {
299 if (!list->name)
300 continue;
301 len = strlen(list->name);
f31514ad 302 write_varint30(f, list->id);
f6c34742
AT
303 write_byte(f, len);
304 write_buf(f, list->name, len);
f6c34742 305 }
f31514ad 306 write_varint30(f, 0);
f6c34742
AT
307 }
308}
309
496c809f 310uid_t recv_user_name(int f, uid_t uid)
283887d7 311{
d6b422a6 312 struct idlist *node;
283887d7
WD
313 int len = read_byte(f);
314 char *name = new_array(char, len+1);
315 if (!name)
316 out_of_memory("recv_user_name");
317 read_sbuf(f, name, len);
0b52f94d
WD
318 if (numeric_ids < 0) {
319 free(name);
320 name = NULL;
321 }
d6b422a6
WD
322 node = recv_add_uid(uid, name); /* node keeps name's memory */
323 return node->id2;
283887d7
WD
324}
325
d6b422a6 326gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
283887d7 327{
d6b422a6 328 struct idlist *node;
283887d7
WD
329 int len = read_byte(f);
330 char *name = new_array(char, len+1);
331 if (!name)
332 out_of_memory("recv_group_name");
333 read_sbuf(f, name, len);
0b52f94d
WD
334 if (numeric_ids < 0) {
335 free(name);
336 name = NULL;
337 }
d6b422a6
WD
338 node = recv_add_gid(gid, name); /* node keeps name's memory */
339 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
340 *flags_ptr |= FLAG_SKIP_GROUP;
341 return node->id2;
283887d7
WD
342}
343
f6c34742 344/* recv a complete uid/gid mapping from the peer and map the uid/gid
84fa865c 345 * in the file list to local names */
d6b422a6 346void recv_id_list(int f, struct file_list *flist)
f6c34742 347{
d6b422a6
WD
348 id_t id;
349 int i;
f6c34742 350
0b52f94d 351 if ((preserve_uid || preserve_acls) && numeric_ids <= 0) {
f6c34742 352 /* read the uid list */
f31514ad 353 while ((id = read_varint30(f)) != 0)
d6b422a6 354 recv_user_name(f, id);
f6c34742
AT
355 }
356
0b52f94d 357 if ((preserve_gid || preserve_acls) && numeric_ids <= 0) {
d49def48 358 /* read the gid list */
f31514ad 359 while ((id = read_varint30(f)) != 0)
d6b422a6 360 recv_group_name(f, id, NULL);
f6c34742
AT
361 }
362
a217c453 363 /* Now convert all the uids/gids from sender values to our values. */
1c3344a1 364#ifdef SUPPORT_ACLS
a217c453
WD
365 if (preserve_acls && !numeric_ids)
366 match_acl_ids();
1c3344a1 367#endif
d49def48 368 if (am_root && preserve_uid && !numeric_ids) {
9decb4d2 369 for (i = 0; i < flist->used; i++)
d6b422a6 370 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
d49def48
WD
371 }
372 if (preserve_gid && (!am_root || !numeric_ids)) {
9decb4d2 373 for (i = 0; i < flist->used; i++) {
d6b422a6
WD
374 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
375 &flist->files[i]->flags);
376 }
5e58e3f9 377 }
f6c34742 378}