Commit | Line | Data |
---|---|---|
f6c34742 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1996 | |
3 | Copyright (C) Paul Mackerras 1996 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* handle the mapping of uid/gid and user/group names between systems. | |
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. | |
24 | */ | |
25 | ||
26 | #include "rsync.h" | |
27 | ||
72fc7ec5 WD |
28 | #ifdef GETGROUPS_T |
29 | # ifndef NGROUPS_MAX | |
30 | /* It ought to be defined, but just in case. */ | |
31 | # define NGROUPS_MAX 32 | |
32 | # endif | |
33 | #endif | |
34 | ||
84fa865c | 35 | extern int verbose; |
f6c34742 AT |
36 | extern int preserve_uid; |
37 | extern int preserve_gid; | |
38 | extern int numeric_ids; | |
460f6b99 | 39 | extern int am_root; |
f6c34742 AT |
40 | |
41 | struct idlist { | |
42 | struct idlist *next; | |
43 | int id, id2; | |
44 | char *name; | |
45 | }; | |
46 | ||
47 | static struct idlist *uidlist; | |
48 | static struct idlist *gidlist; | |
49 | ||
50 | static struct idlist *add_list(int id, char *name) | |
51 | { | |
58cadc86 | 52 | struct idlist *list = new(struct idlist); |
f6c34742 AT |
53 | if (!list) out_of_memory("add_list"); |
54 | list->next = NULL; | |
55 | list->name = strdup(name); | |
56 | if (!list->name) out_of_memory("add_list"); | |
57 | list->id = (int)id; | |
58 | return list; | |
59 | } | |
60 | ||
61 | ||
62 | ||
63 | /* turn a uid into a user name */ | |
64 | static char *uid_to_name(uid_t uid) | |
65 | { | |
66 | struct passwd *pass = getpwuid(uid); | |
67 | if (pass) return(pass->pw_name); | |
68 | return NULL; | |
69 | } | |
70 | ||
71 | /* turn a gid into a group name */ | |
72 | static char *gid_to_name(gid_t gid) | |
73 | { | |
74 | struct group *grp = getgrgid(gid); | |
75 | if (grp) return(grp->gr_name); | |
76 | return NULL; | |
77 | } | |
78 | ||
f6c34742 AT |
79 | static int map_uid(int id, char *name) |
80 | { | |
8ef4ffd6 | 81 | uid_t uid; |
0be976ec | 82 | if (uid != 0 && name_to_uid(name, &uid)) |
8ef4ffd6 | 83 | return uid; |
f6c34742 AT |
84 | return id; |
85 | } | |
86 | ||
87 | static int map_gid(int id, char *name) | |
88 | { | |
8ef4ffd6 | 89 | gid_t gid; |
0be976ec | 90 | if (gid != 0 && name_to_gid(name, &gid)) |
8ef4ffd6 | 91 | return gid; |
f6c34742 AT |
92 | return id; |
93 | } | |
94 | ||
95 | /* this function is a definate candidate for a faster algorithm */ | |
96 | static uid_t match_uid(uid_t uid) | |
97 | { | |
98 | static uid_t last_in, last_out; | |
99 | struct idlist *list = uidlist; | |
100 | ||
a2687b64 WD |
101 | if (uid == last_in) |
102 | return last_out; | |
f6c34742 AT |
103 | |
104 | last_in = uid; | |
105 | ||
106 | while (list) { | |
107 | if (list->id == (int)uid) { | |
108 | last_out = (uid_t)list->id2; | |
109 | return last_out; | |
110 | } | |
111 | list = list->next; | |
112 | } | |
113 | ||
114 | last_out = uid; | |
115 | return last_out; | |
116 | } | |
117 | ||
5b540e86 WD |
118 | static int is_in_group(gid_t gid) |
119 | { | |
120 | #ifdef GETGROUPS_T | |
a2687b64 | 121 | static gid_t last_in = GID_NONE, last_out; |
5b540e86 WD |
122 | static int ngroups = -2; |
123 | static GETGROUPS_T *gidset; | |
124 | int n; | |
125 | ||
126 | if (gid == last_in) | |
127 | return last_out; | |
128 | if (ngroups < -1) { | |
670d8abf | 129 | gid_t mygid = MY_GID(); |
5b540e86 | 130 | ngroups = getgroups(0, 0); |
72fc7ec5 | 131 | /* If that didn't work, perhaps 0 isn't treated specially? */ |
a2687b64 | 132 | if (ngroups <= 0) |
72fc7ec5 WD |
133 | ngroups = NGROUPS_MAX; |
134 | gidset = new_array(GETGROUPS_T, ngroups+1); | |
135 | if (ngroups > 0) | |
5b540e86 | 136 | ngroups = getgroups(ngroups, gidset); |
72fc7ec5 WD |
137 | /* The default gid might not be in the list on some systems. */ |
138 | for (n = 0; n < ngroups; n++) { | |
139 | if (gidset[n] == mygid) | |
140 | break; | |
5b540e86 | 141 | } |
72fc7ec5 WD |
142 | if (n == ngroups) |
143 | gidset[ngroups++] = mygid; | |
84fa865c | 144 | if (verbose > 3) { |
187e9c24 WD |
145 | char gidbuf[NGROUPS_MAX*16+32]; |
146 | int pos; | |
147 | sprintf(gidbuf, "process has %d gid%s: ", | |
148 | ngroups, ngroups == 1? "" : "s"); | |
149 | pos = strlen(gidbuf); | |
84fa865c | 150 | for (n = 0; n < ngroups; n++) { |
187e9c24 WD |
151 | sprintf(gidbuf+pos, " %ld", (long)gidset[n]); |
152 | pos += strlen(gidbuf+pos); | |
84fa865c | 153 | } |
187e9c24 | 154 | rprintf(FINFO, "%s\n", gidbuf); |
84fa865c | 155 | } |
5b540e86 WD |
156 | } |
157 | ||
158 | last_in = gid; | |
5b540e86 | 159 | for (n = 0; n < ngroups; n++) { |
a2687b64 WD |
160 | if (gidset[n] == gid) |
161 | return last_out = 1; | |
5b540e86 | 162 | } |
a2687b64 | 163 | return last_out = 0; |
5b540e86 WD |
164 | |
165 | #else | |
a2687b64 | 166 | static gid_t mygid = GID_NONE; |
187e9c24 | 167 | if (mygid == GID_NONE) { |
670d8abf | 168 | mygid = MY_GID(); |
187e9c24 WD |
169 | if (verbose > 3) |
170 | rprintf(FINFO, "process has gid %ld\n", (long)mygid); | |
171 | } | |
a2687b64 | 172 | return gid == mygid; |
5b540e86 WD |
173 | #endif |
174 | } | |
175 | ||
f6c34742 AT |
176 | static gid_t match_gid(gid_t gid) |
177 | { | |
a2687b64 | 178 | static gid_t last_in = GID_NONE, last_out = GID_NONE; |
f6c34742 AT |
179 | struct idlist *list = gidlist; |
180 | ||
a2687b64 WD |
181 | if (gid == last_in) |
182 | return last_out; | |
f6c34742 AT |
183 | |
184 | last_in = gid; | |
185 | ||
186 | while (list) { | |
187 | if (list->id == (int)gid) { | |
188 | last_out = (gid_t)list->id2; | |
189 | return last_out; | |
190 | } | |
191 | list = list->next; | |
192 | } | |
193 | ||
460f6b99 DD |
194 | if (am_root) |
195 | last_out = gid; | |
196 | else | |
a60e2dca | 197 | last_out = GID_NONE; |
f6c34742 AT |
198 | return last_out; |
199 | } | |
200 | ||
201 | /* add a uid to the list of uids */ | |
202 | void add_uid(uid_t uid) | |
203 | { | |
204 | struct idlist *list = uidlist; | |
205 | char *name; | |
206 | ||
207 | if (numeric_ids) return; | |
208 | ||
209 | /* don't map root */ | |
210 | if (uid==0) return; | |
211 | ||
212 | if (!list) { | |
213 | if (!(name = uid_to_name(uid))) return; | |
214 | uidlist = add_list((int)uid, name); | |
215 | return; | |
216 | } | |
217 | ||
218 | while (list->next) { | |
219 | if (list->id == (int)uid) return; | |
220 | list = list->next; | |
221 | } | |
222 | ||
223 | if (list->id == (int)uid) return; | |
224 | ||
225 | if (!(name = uid_to_name(uid))) return; | |
226 | ||
227 | list->next = add_list((int)uid, name); | |
228 | } | |
229 | ||
230 | /* add a gid to the list of gids */ | |
231 | void add_gid(gid_t gid) | |
232 | { | |
233 | struct idlist *list = gidlist; | |
234 | char *name; | |
235 | ||
236 | if (numeric_ids) return; | |
237 | ||
238 | /* don't map root */ | |
239 | if (gid==0) return; | |
240 | ||
241 | if (!list) { | |
242 | if (!(name = gid_to_name(gid))) return; | |
243 | gidlist = add_list((int)gid, name); | |
244 | return; | |
245 | } | |
246 | ||
247 | while (list->next) { | |
248 | if (list->id == (int)gid) return; | |
249 | list = list->next; | |
250 | } | |
251 | ||
252 | if (list->id == (int)gid) return; | |
253 | ||
254 | if (!(name = gid_to_name(gid))) return; | |
255 | ||
256 | list->next = add_list((int)gid, name); | |
257 | } | |
258 | ||
259 | ||
260 | /* send a complete uid/gid mapping to the peer */ | |
261 | void send_uid_list(int f) | |
262 | { | |
263 | struct idlist *list; | |
264 | ||
265 | if (numeric_ids) return; | |
266 | ||
267 | if (preserve_uid) { | |
268 | /* we send sequences of uid/byte-length/name */ | |
269 | list = uidlist; | |
270 | while (list) { | |
271 | int len = strlen(list->name); | |
272 | write_int(f, list->id); | |
273 | write_byte(f, len); | |
274 | write_buf(f, list->name, len); | |
275 | list = list->next; | |
276 | } | |
277 | ||
278 | /* terminate the uid list with a 0 uid. We explicitly exclude | |
84fa865c | 279 | * 0 from the list */ |
f6c34742 AT |
280 | write_int(f, 0); |
281 | } | |
282 | ||
283 | if (preserve_gid) { | |
284 | list = gidlist; | |
285 | while (list) { | |
286 | int len = strlen(list->name); | |
287 | write_int(f, list->id); | |
288 | write_byte(f, len); | |
289 | write_buf(f, list->name, len); | |
290 | list = list->next; | |
291 | } | |
292 | write_int(f, 0); | |
293 | } | |
294 | } | |
295 | ||
296 | /* recv a complete uid/gid mapping from the peer and map the uid/gid | |
84fa865c | 297 | * in the file list to local names */ |
f6c34742 AT |
298 | void recv_uid_list(int f, struct file_list *flist) |
299 | { | |
300 | int id, i; | |
301 | char *name; | |
302 | struct idlist *list; | |
303 | ||
304 | if (numeric_ids) return; | |
305 | ||
306 | if (preserve_uid) { | |
307 | /* read the uid list */ | |
308 | list = uidlist; | |
5b540e86 | 309 | while ((id = read_int(f)) != 0) { |
f6c34742 | 310 | int len = read_byte(f); |
58cadc86 | 311 | name = new_array(char, len+1); |
f6c34742 | 312 | if (!name) out_of_memory("recv_uid_list"); |
575f2fca | 313 | read_sbuf(f, name, len); |
f6c34742 AT |
314 | if (!list) { |
315 | uidlist = add_list(id, name); | |
316 | list = uidlist; | |
317 | } else { | |
318 | list->next = add_list(id, name); | |
319 | list = list->next; | |
320 | } | |
321 | list->id2 = map_uid(id, name); | |
322 | free(name); | |
f6c34742 | 323 | } |
84fa865c WD |
324 | if (verbose > 3) { |
325 | for (list = uidlist; list; list = list->next) { | |
555b0e20 WD |
326 | rprintf(FINFO, "uid %ld (%s) maps to %ld\n", |
327 | (long)list->id, list->name, | |
84fa865c WD |
328 | (long)list->id2); |
329 | } | |
330 | } | |
f6c34742 AT |
331 | } |
332 | ||
333 | ||
334 | if (preserve_gid) { | |
335 | /* and the gid list */ | |
336 | list = gidlist; | |
5b540e86 | 337 | while ((id = read_int(f)) != 0) { |
f6c34742 | 338 | int len = read_byte(f); |
58cadc86 | 339 | name = new_array(char, len+1); |
f6c34742 | 340 | if (!name) out_of_memory("recv_uid_list"); |
575f2fca | 341 | read_sbuf(f, name, len); |
f6c34742 AT |
342 | if (!list) { |
343 | gidlist = add_list(id, name); | |
344 | list = gidlist; | |
345 | } else { | |
346 | list->next = add_list(id, name); | |
347 | list = list->next; | |
348 | } | |
349 | list->id2 = map_gid(id, name); | |
5b540e86 | 350 | if (!am_root && !is_in_group(list->id2)) |
a60e2dca | 351 | list->id2 = GID_NONE; |
f6c34742 | 352 | free(name); |
f6c34742 | 353 | } |
84fa865c WD |
354 | if (verbose > 3) { |
355 | for (list = gidlist; list; list = list->next) { | |
555b0e20 WD |
356 | rprintf(FINFO, "gid %ld (%s) maps to %ld\n", |
357 | (long)list->id, list->name, | |
84fa865c WD |
358 | (long)list->id2); |
359 | } | |
360 | } | |
f6c34742 AT |
361 | } |
362 | ||
460f6b99 | 363 | if (!(am_root && preserve_uid) && !preserve_gid) return; |
f6c34742 AT |
364 | |
365 | /* now convert the uid/gid of all files in the list to the mapped | |
84fa865c | 366 | * uid/gid */ |
0be976ec WD |
367 | for (i = 0; i < flist->count; i++) { |
368 | if (am_root && preserve_uid && flist->files[i]->uid != 0) | |
3ec4dd97 | 369 | flist->files[i]->uid = match_uid(flist->files[i]->uid); |
0be976ec | 370 | if (preserve_gid && (!am_root || flist->files[i]->gid != 0)) |
3ec4dd97 | 371 | flist->files[i]->gid = match_gid(flist->files[i]->gid); |
f6c34742 AT |
372 | } |
373 | } |