Can eliminate the testtmp.* ignore rule now.
[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;
44 int id, id2;
45 char *name;
46};
47
48static struct idlist *uidlist;
49static struct idlist *gidlist;
50
d49def48
WD
51static struct idlist *add_to_list(struct idlist **root, int id, char *name,
52 int id2)
f6c34742 53{
d49def48
WD
54 struct idlist *node = new(struct idlist);
55 if (!node)
56 out_of_memory("add_to_list");
57 node->next = *root;
58 node->name = name;
59 node->id = id;
60 node->id2 = id2;
61 *root = node;
62 return node;
f6c34742
AT
63}
64
f6c34742
AT
65/* turn a uid into a user name */
66static char *uid_to_name(uid_t uid)
67{
68 struct passwd *pass = getpwuid(uid);
d49def48
WD
69 if (pass)
70 return strdup(pass->pw_name);
f6c34742
AT
71 return NULL;
72}
73
74/* turn a gid into a group name */
75static char *gid_to_name(gid_t gid)
76{
77 struct group *grp = getgrgid(gid);
d49def48
WD
78 if (grp)
79 return strdup(grp->gr_name);
f6c34742
AT
80 return NULL;
81}
82
283887d7 83static uid_t map_uid(uid_t id, char *name)
f6c34742 84{
8ef4ffd6 85 uid_t uid;
b66d0085 86 if (id != 0 && name_to_uid(name, &uid))
8ef4ffd6 87 return uid;
f6c34742
AT
88 return id;
89}
90
283887d7 91static gid_t map_gid(gid_t id, char *name)
f6c34742 92{
8ef4ffd6 93 gid_t gid;
b66d0085 94 if (id != 0 && name_to_gid(name, &gid))
8ef4ffd6 95 return gid;
f6c34742
AT
96 return id;
97}
98
5b540e86
WD
99static int is_in_group(gid_t gid)
100{
4f5b0756 101#ifdef HAVE_GETGROUPS
a2687b64 102 static gid_t last_in = GID_NONE, last_out;
5b540e86
WD
103 static int ngroups = -2;
104 static GETGROUPS_T *gidset;
105 int n;
106
107 if (gid == last_in)
108 return last_out;
109 if (ngroups < -1) {
670d8abf 110 gid_t mygid = MY_GID();
f567e9b3 111 if ((ngroups = getgroups(0, NULL)) < 0)
dbd8811b 112 ngroups = 0;
72fc7ec5 113 gidset = new_array(GETGROUPS_T, ngroups+1);
f567e9b3
WD
114 if (!gidset)
115 out_of_memory("is_in_group");
72fc7ec5 116 if (ngroups > 0)
5b540e86 117 ngroups = getgroups(ngroups, gidset);
72fc7ec5
WD
118 /* The default gid might not be in the list on some systems. */
119 for (n = 0; n < ngroups; n++) {
120 if (gidset[n] == mygid)
121 break;
5b540e86 122 }
72fc7ec5
WD
123 if (n == ngroups)
124 gidset[ngroups++] = mygid;
84fa865c 125 if (verbose > 3) {
187e9c24 126 int pos;
f567e9b3
WD
127 char *gidbuf = new_array(char, ngroups*21+32);
128 if (!gidbuf)
129 out_of_memory("is_in_group");
10944395
WD
130 pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
131 ngroups, ngroups == 1? "" : "s");
84fa865c 132 for (n = 0; n < ngroups; n++) {
10944395 133 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
84fa865c 134 }
187e9c24 135 rprintf(FINFO, "%s\n", gidbuf);
f567e9b3 136 free(gidbuf);
84fa865c 137 }
5b540e86
WD
138 }
139
140 last_in = gid;
5b540e86 141 for (n = 0; n < ngroups; n++) {
a2687b64
WD
142 if (gidset[n] == gid)
143 return last_out = 1;
5b540e86 144 }
a2687b64 145 return last_out = 0;
5b540e86
WD
146
147#else
a2687b64 148 static gid_t mygid = GID_NONE;
187e9c24 149 if (mygid == GID_NONE) {
670d8abf 150 mygid = MY_GID();
187e9c24 151 if (verbose > 3)
d49def48 152 rprintf(FINFO, "process has gid %d\n", (int)mygid);
187e9c24 153 }
a2687b64 154 return gid == mygid;
5b540e86
WD
155#endif
156}
157
d49def48 158/* Add a uid to the list of uids. Only called on receiving side. */
283887d7 159static uid_t recv_add_uid(uid_t id, char *name)
d49def48 160{
283887d7 161 uid_t id2 = name ? map_uid(id, name) : id;
d49def48
WD
162 struct idlist *node;
163
283887d7 164 node = add_to_list(&uidlist, (int)id, name, (int)id2);
d49def48
WD
165
166 if (verbose > 3) {
167 rprintf(FINFO, "uid %d(%s) maps to %d\n",
283887d7 168 (int)id, name ? name : "", (int)id2);
d49def48
WD
169 }
170
283887d7 171 return id2;
d49def48
WD
172}
173
174/* Add a gid to the list of gids. Only called on receiving side. */
283887d7 175static gid_t recv_add_gid(gid_t id, char *name)
d49def48 176{
283887d7 177 gid_t id2 = name ? map_gid(id, name) : id;
d49def48
WD
178 struct idlist *node;
179
180 if (!am_root && !is_in_group(id2))
181 id2 = GID_NONE;
283887d7 182 node = add_to_list(&gidlist, (int)id, name, (int)id2);
d49def48
WD
183
184 if (verbose > 3) {
185 rprintf(FINFO, "gid %d(%s) maps to %d\n",
283887d7 186 (int)id, name ? name : "", (int)id2);
d49def48
WD
187 }
188
283887d7 189 return id2;
d49def48
WD
190}
191
ade7292a 192/* this function is a definate candidate for a faster algorithm */
496c809f 193uid_t match_uid(uid_t uid)
ade7292a
WD
194{
195 static uid_t last_in, last_out;
d49def48
WD
196 struct idlist *list;
197
198 if (uid == 0)
199 return 0;
ade7292a
WD
200
201 if (uid == last_in)
202 return last_out;
203
204 last_in = uid;
205
d49def48
WD
206 for (list = uidlist; list; list = list->next) {
207 if (list->id == (int)uid)
208 return last_out = (uid_t)list->id2;
ade7292a
WD
209 }
210
d49def48 211 return last_out = uid;
ade7292a
WD
212}
213
496c809f 214gid_t match_gid(gid_t gid)
f6c34742 215{
a2687b64 216 static gid_t last_in = GID_NONE, last_out = GID_NONE;
d49def48
WD
217 struct idlist *list;
218
01363a24
WD
219 if (gid == GID_NONE)
220 return GID_NONE;
f6c34742 221
a2687b64
WD
222 if (gid == last_in)
223 return last_out;
f6c34742
AT
224
225 last_in = gid;
226
d49def48
WD
227 for (list = gidlist; list; list = list->next) {
228 if (list->id == (int)gid)
229 return last_out = (gid_t)list->id2;
f6c34742 230 }
d49def48 231
283887d7 232 return last_out = recv_add_gid(gid, NULL);
f6c34742
AT
233}
234
d49def48 235/* Add a uid to the list of uids. Only called on sending side. */
496c809f 236char *add_uid(uid_t uid)
f6c34742 237{
d49def48 238 struct idlist *list;
496c809f 239 struct idlist *node;
f6c34742 240
d49def48 241 if (uid == 0) /* don't map root */
496c809f 242 return NULL;
f6c34742 243
d49def48
WD
244 for (list = uidlist; list; list = list->next) {
245 if (list->id == (int)uid)
496c809f 246 return NULL;
f6c34742
AT
247 }
248
496c809f
WD
249 node = add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
250 return node->name;
f6c34742
AT
251}
252
d49def48 253/* Add a gid to the list of gids. Only called on sending side. */
496c809f 254char *add_gid(gid_t gid)
f6c34742 255{
d49def48 256 struct idlist *list;
496c809f 257 struct idlist *node;
f6c34742 258
d49def48 259 if (gid == 0) /* don't map root */
496c809f 260 return NULL;
f6c34742 261
d49def48
WD
262 for (list = gidlist; list; list = list->next) {
263 if (list->id == (int)gid)
496c809f 264 return NULL;
f6c34742
AT
265 }
266
496c809f
WD
267 node = add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
268 return node->name;
f6c34742
AT
269}
270
f6c34742
AT
271/* send a complete uid/gid mapping to the peer */
272void send_uid_list(int f)
273{
274 struct idlist *list;
275
1c3344a1 276 if (preserve_uid || preserve_acls) {
d49def48 277 int len;
f6c34742 278 /* we send sequences of uid/byte-length/name */
d49def48
WD
279 for (list = uidlist; list; list = list->next) {
280 if (!list->name)
281 continue;
282 len = strlen(list->name);
ab14d01a 283 write_abbrevint30(f, list->id);
f6c34742
AT
284 write_byte(f, len);
285 write_buf(f, list->name, len);
f6c34742
AT
286 }
287
288 /* terminate the uid list with a 0 uid. We explicitly exclude
84fa865c 289 * 0 from the list */
ab14d01a 290 write_abbrevint30(f, 0);
f6c34742
AT
291 }
292
1c3344a1 293 if (preserve_gid || preserve_acls) {
d49def48
WD
294 int len;
295 for (list = gidlist; list; list = list->next) {
296 if (!list->name)
297 continue;
298 len = strlen(list->name);
ab14d01a 299 write_abbrevint30(f, list->id);
f6c34742
AT
300 write_byte(f, len);
301 write_buf(f, list->name, len);
f6c34742 302 }
ab14d01a 303 write_abbrevint30(f, 0);
f6c34742
AT
304 }
305}
306
496c809f 307uid_t recv_user_name(int f, uid_t uid)
283887d7
WD
308{
309 int len = read_byte(f);
310 char *name = new_array(char, len+1);
311 if (!name)
312 out_of_memory("recv_user_name");
313 read_sbuf(f, name, len);
314 return recv_add_uid(uid, name); /* node keeps name's memory */
315}
316
496c809f 317gid_t recv_group_name(int f, gid_t gid)
283887d7
WD
318{
319 int len = read_byte(f);
320 char *name = new_array(char, len+1);
321 if (!name)
322 out_of_memory("recv_group_name");
323 read_sbuf(f, name, len);
324 return recv_add_gid(gid, name); /* node keeps name's memory */
325}
326
f6c34742 327/* recv a complete uid/gid mapping from the peer and map the uid/gid
84fa865c 328 * in the file list to local names */
7b6fa00f 329void recv_uid_list(int f, struct file_list *flist)
f6c34742
AT
330{
331 int id, i;
f6c34742 332
1c3344a1 333 if ((preserve_uid || preserve_acls) && !numeric_ids) {
f6c34742 334 /* read the uid list */
ab14d01a 335 while ((id = read_abbrevint30(f)) != 0)
283887d7 336 recv_user_name(f, (uid_t)id);
f6c34742
AT
337 }
338
1c3344a1 339 if ((preserve_gid || preserve_acls) && !numeric_ids) {
d49def48 340 /* read the gid list */
ab14d01a 341 while ((id = read_abbrevint30(f)) != 0)
283887d7 342 recv_group_name(f, (gid_t)id);
f6c34742
AT
343 }
344
1c3344a1
WD
345#ifdef SUPPORT_ACLS
346 if (preserve_acls && !numeric_ids) {
347 id_t *id;
348 while ((id = next_acl_uid(flist)) != NULL)
349 *id = match_uid(*id);
350 while ((id = next_acl_gid(flist)) != NULL)
351 *id = match_gid(*id);
352 }
353#endif
354
7b6fa00f 355 /* Now convert all the uids/gids from sender values to our values. */
d49def48 356 if (am_root && preserve_uid && !numeric_ids) {
7b6fa00f 357 for (i = 0; i < flist->count; i++)
f1482c33 358 F_OWNER(flist->files[i]) = match_uid(F_UID(flist->files[i]));
d49def48
WD
359 }
360 if (preserve_gid && (!am_root || !numeric_ids)) {
7b6fa00f 361 for (i = 0; i < flist->count; i++)
f1482c33 362 F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i]));
5e58e3f9 363 }
f6c34742 364}