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