Commit | Line | Data |
---|---|---|
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 | |
4fd842f9 | 9 | * it under the terms of the GNU General Public License version 3 as |
ba2133d6 | 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 | 17 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 18 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 WD |
19 | */ |
20 | ||
21 | /* If the source username/group does not exist on the target then use | |
22 | * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these | |
23 | * are special. */ | |
f6c34742 AT |
24 | |
25 | #include "rsync.h" | |
ab14d01a | 26 | #include "io.h" |
f6c34742 | 27 | |
84fa865c | 28 | extern int verbose; |
283887d7 | 29 | extern int am_root; |
f6c34742 AT |
30 | extern int preserve_uid; |
31 | extern int preserve_gid; | |
1c3344a1 | 32 | extern int preserve_acls; |
f6c34742 AT |
33 | extern 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 |
43 | struct idlist { |
44 | struct idlist *next; | |
f6c34742 | 45 | char *name; |
d6b422a6 WD |
46 | id_t id, id2; |
47 | uint16 flags; | |
f6c34742 AT |
48 | }; |
49 | ||
50 | static struct idlist *uidlist; | |
51 | static struct idlist *gidlist; | |
52 | ||
d6b422a6 WD |
53 | static struct idlist *add_to_list(struct idlist **root, id_t id, char *name, |
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 AT |
68 | /* turn a uid into a user name */ |
69 | static char *uid_to_name(uid_t uid) | |
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 */ | |
78 | static char *gid_to_name(gid_t gid) | |
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 | ||
283887d7 | 86 | static uid_t map_uid(uid_t id, 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 | ||
283887d7 | 94 | static gid_t map_gid(gid_t id, 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 |
102 | static 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; | |
84fa865c | 128 | if (verbose > 3) { |
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(); |
187e9c24 | 154 | if (verbose > 3) |
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. */ |
d6b422a6 | 162 | static struct idlist *recv_add_uid(uid_t id, 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 WD |
168 | |
169 | if (verbose > 3) { | |
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. */ | |
d6b422a6 | 178 | static struct idlist *recv_add_gid(gid_t id, 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 WD |
185 | |
186 | if (verbose > 3) { | |
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 | 195 | uid_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 | 216 | gid_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. */ |
496c809f | 239 | 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. */ |
496c809f | 257 | 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 | 275 | void 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 | 310 | uid_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); | |
d6b422a6 WD |
318 | node = recv_add_uid(uid, name); /* node keeps name's memory */ |
319 | return node->id2; | |
283887d7 WD |
320 | } |
321 | ||
d6b422a6 | 322 | gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr) |
283887d7 | 323 | { |
d6b422a6 | 324 | struct idlist *node; |
283887d7 WD |
325 | int len = read_byte(f); |
326 | char *name = new_array(char, len+1); | |
327 | if (!name) | |
328 | out_of_memory("recv_group_name"); | |
329 | read_sbuf(f, name, len); | |
d6b422a6 WD |
330 | node = recv_add_gid(gid, name); /* node keeps name's memory */ |
331 | if (flags_ptr && node->flags & FLAG_SKIP_GROUP) | |
332 | *flags_ptr |= FLAG_SKIP_GROUP; | |
333 | return node->id2; | |
283887d7 WD |
334 | } |
335 | ||
f6c34742 | 336 | /* recv a complete uid/gid mapping from the peer and map the uid/gid |
84fa865c | 337 | * in the file list to local names */ |
d6b422a6 | 338 | void recv_id_list(int f, struct file_list *flist) |
f6c34742 | 339 | { |
d6b422a6 WD |
340 | id_t id; |
341 | int i; | |
f6c34742 | 342 | |
1c3344a1 | 343 | if ((preserve_uid || preserve_acls) && !numeric_ids) { |
f6c34742 | 344 | /* read the uid list */ |
f31514ad | 345 | while ((id = read_varint30(f)) != 0) |
d6b422a6 | 346 | recv_user_name(f, id); |
f6c34742 AT |
347 | } |
348 | ||
1c3344a1 | 349 | if ((preserve_gid || preserve_acls) && !numeric_ids) { |
d49def48 | 350 | /* read the gid list */ |
f31514ad | 351 | while ((id = read_varint30(f)) != 0) |
d6b422a6 | 352 | recv_group_name(f, id, NULL); |
f6c34742 AT |
353 | } |
354 | ||
a217c453 | 355 | /* Now convert all the uids/gids from sender values to our values. */ |
1c3344a1 | 356 | #ifdef SUPPORT_ACLS |
a217c453 WD |
357 | if (preserve_acls && !numeric_ids) |
358 | match_acl_ids(); | |
1c3344a1 | 359 | #endif |
d49def48 | 360 | if (am_root && preserve_uid && !numeric_ids) { |
9decb4d2 | 361 | for (i = 0; i < flist->used; i++) |
d6b422a6 | 362 | F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i])); |
d49def48 WD |
363 | } |
364 | if (preserve_gid && (!am_root || !numeric_ids)) { | |
9decb4d2 | 365 | for (i = 0; i < flist->used; i++) { |
d6b422a6 WD |
366 | F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]), |
367 | &flist->files[i]->flags); | |
368 | } | |
5e58e3f9 | 369 | } |
f6c34742 | 370 | } |