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