Commit | Line | Data |
---|---|---|
ade7292a | 1 | /* |
f6c34742 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
ade7292a | 4 | |
f6c34742 AT |
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. | |
ade7292a | 9 | |
f6c34742 AT |
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. | |
ade7292a | 14 | |
f6c34742 AT |
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 | |
d49def48 | 22 | the numeric IDs. Never do any mapping for uid=0 or gid=0 as these |
f6c34742 AT |
23 | are special. |
24 | */ | |
25 | ||
26 | #include "rsync.h" | |
27 | ||
1df395f7 WD |
28 | #ifdef HAVE_GETGROUPS |
29 | # if !defined(GETGROUPS_T) | |
30 | # define GETGROUPS_T gid_t | |
31 | # endif | |
72fc7ec5 WD |
32 | # ifndef NGROUPS_MAX |
33 | /* It ought to be defined, but just in case. */ | |
34 | # define NGROUPS_MAX 32 | |
35 | # endif | |
36 | #endif | |
37 | ||
84fa865c | 38 | extern int verbose; |
f6c34742 AT |
39 | extern int preserve_uid; |
40 | extern int preserve_gid; | |
41 | extern int numeric_ids; | |
460f6b99 | 42 | extern int am_root; |
f6c34742 AT |
43 | |
44 | struct idlist { | |
45 | struct idlist *next; | |
46 | int id, id2; | |
47 | char *name; | |
48 | }; | |
49 | ||
50 | static struct idlist *uidlist; | |
51 | static struct idlist *gidlist; | |
52 | ||
d49def48 WD |
53 | static struct idlist *add_to_list(struct idlist **root, int id, char *name, |
54 | int id2) | |
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; | |
63 | *root = node; | |
64 | return node; | |
f6c34742 AT |
65 | } |
66 | ||
f6c34742 AT |
67 | /* turn a uid into a user name */ |
68 | static char *uid_to_name(uid_t uid) | |
69 | { | |
70 | struct passwd *pass = getpwuid(uid); | |
d49def48 WD |
71 | if (pass) |
72 | return strdup(pass->pw_name); | |
f6c34742 AT |
73 | return NULL; |
74 | } | |
75 | ||
76 | /* turn a gid into a group name */ | |
77 | static char *gid_to_name(gid_t gid) | |
78 | { | |
79 | struct group *grp = getgrgid(gid); | |
d49def48 WD |
80 | if (grp) |
81 | return strdup(grp->gr_name); | |
f6c34742 AT |
82 | return NULL; |
83 | } | |
84 | ||
f6c34742 AT |
85 | static int map_uid(int id, char *name) |
86 | { | |
8ef4ffd6 | 87 | uid_t uid; |
0be976ec | 88 | if (uid != 0 && name_to_uid(name, &uid)) |
8ef4ffd6 | 89 | return uid; |
f6c34742 AT |
90 | return id; |
91 | } | |
92 | ||
93 | static int map_gid(int id, char *name) | |
94 | { | |
8ef4ffd6 | 95 | gid_t gid; |
0be976ec | 96 | if (gid != 0 && name_to_gid(name, &gid)) |
8ef4ffd6 | 97 | return gid; |
f6c34742 AT |
98 | return id; |
99 | } | |
100 | ||
5b540e86 WD |
101 | static int is_in_group(gid_t gid) |
102 | { | |
1df395f7 | 103 | #ifdef HAVE_GETGROUPS |
a2687b64 | 104 | static gid_t last_in = GID_NONE, last_out; |
5b540e86 WD |
105 | static int ngroups = -2; |
106 | static GETGROUPS_T *gidset; | |
107 | int n; | |
108 | ||
109 | if (gid == last_in) | |
110 | return last_out; | |
111 | if (ngroups < -1) { | |
670d8abf | 112 | gid_t mygid = MY_GID(); |
dbd8811b WD |
113 | if ((ngroups = getgroups(0, 0)) < 0) |
114 | ngroups = 0; | |
72fc7ec5 WD |
115 | gidset = new_array(GETGROUPS_T, ngroups+1); |
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 WD |
126 | char gidbuf[NGROUPS_MAX*16+32]; |
127 | int pos; | |
128 | sprintf(gidbuf, "process has %d gid%s: ", | |
129 | ngroups, ngroups == 1? "" : "s"); | |
130 | pos = strlen(gidbuf); | |
84fa865c | 131 | for (n = 0; n < ngroups; n++) { |
d49def48 | 132 | sprintf(gidbuf+pos, " %d", (int)gidset[n]); |
187e9c24 | 133 | pos += strlen(gidbuf+pos); |
84fa865c | 134 | } |
187e9c24 | 135 | rprintf(FINFO, "%s\n", 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 WD |
157 | /* Add a uid to the list of uids. Only called on receiving side. */ |
158 | static struct idlist *recv_add_uid(int id, char *name) | |
159 | { | |
160 | int id2 = name ? map_uid(id, name) : id; | |
161 | struct idlist *node; | |
162 | ||
163 | node = add_to_list(&uidlist, id, name, map_uid(id, name)); | |
164 | ||
165 | if (verbose > 3) { | |
166 | rprintf(FINFO, "uid %d(%s) maps to %d\n", | |
167 | id, name ? name : "", id2); | |
168 | } | |
169 | ||
170 | return node; | |
171 | } | |
172 | ||
173 | /* Add a gid to the list of gids. Only called on receiving side. */ | |
174 | static struct idlist *recv_add_gid(int id, char *name) | |
175 | { | |
176 | int id2 = name ? map_gid(id, name) : id; | |
177 | struct idlist *node; | |
178 | ||
179 | if (!am_root && !is_in_group(id2)) | |
180 | id2 = GID_NONE; | |
181 | node = add_to_list(&gidlist, id, name, id2); | |
182 | ||
183 | if (verbose > 3) { | |
184 | rprintf(FINFO, "gid %d(%s) maps to %d\n", | |
185 | id, name ? name : "", id2); | |
186 | } | |
187 | ||
188 | return node; | |
189 | } | |
190 | ||
ade7292a WD |
191 | /* this function is a definate candidate for a faster algorithm */ |
192 | static uid_t match_uid(uid_t uid) | |
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 | ||
f6c34742 AT |
213 | static gid_t match_gid(gid_t gid) |
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 WD |
230 | |
231 | list = recv_add_gid(gid, NULL); | |
232 | return last_out = list->id2; | |
f6c34742 AT |
233 | } |
234 | ||
d49def48 | 235 | /* Add a uid to the list of uids. Only called on sending side. */ |
f6c34742 AT |
236 | void add_uid(uid_t uid) |
237 | { | |
d49def48 | 238 | struct idlist *list; |
f6c34742 | 239 | |
d49def48 | 240 | if (uid == 0) /* don't map root */ |
f6c34742 | 241 | return; |
f6c34742 | 242 | |
d49def48 WD |
243 | for (list = uidlist; list; list = list->next) { |
244 | if (list->id == (int)uid) | |
245 | return; | |
f6c34742 AT |
246 | } |
247 | ||
d49def48 | 248 | add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0); |
f6c34742 AT |
249 | } |
250 | ||
d49def48 | 251 | /* Add a gid to the list of gids. Only called on sending side. */ |
f6c34742 AT |
252 | void add_gid(gid_t gid) |
253 | { | |
d49def48 | 254 | struct idlist *list; |
f6c34742 | 255 | |
d49def48 | 256 | if (gid == 0) /* don't map root */ |
f6c34742 | 257 | return; |
f6c34742 | 258 | |
d49def48 WD |
259 | for (list = gidlist; list; list = list->next) { |
260 | if (list->id == (int)gid) | |
261 | return; | |
f6c34742 AT |
262 | } |
263 | ||
d49def48 | 264 | add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0); |
f6c34742 AT |
265 | } |
266 | ||
267 | ||
268 | /* send a complete uid/gid mapping to the peer */ | |
269 | void send_uid_list(int f) | |
270 | { | |
271 | struct idlist *list; | |
272 | ||
d49def48 WD |
273 | if (numeric_ids) |
274 | return; | |
f6c34742 AT |
275 | |
276 | if (preserve_uid) { | |
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); | |
f6c34742 AT |
283 | write_int(f, list->id); |
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 */ |
f6c34742 AT |
290 | write_int(f, 0); |
291 | } | |
292 | ||
293 | if (preserve_gid) { | |
d49def48 WD |
294 | int len; |
295 | for (list = gidlist; list; list = list->next) { | |
296 | if (!list->name) | |
297 | continue; | |
298 | len = strlen(list->name); | |
f6c34742 AT |
299 | write_int(f, list->id); |
300 | write_byte(f, len); | |
301 | write_buf(f, list->name, len); | |
f6c34742 AT |
302 | } |
303 | write_int(f, 0); | |
304 | } | |
305 | } | |
306 | ||
307 | /* recv a complete uid/gid mapping from the peer and map the uid/gid | |
84fa865c | 308 | * in the file list to local names */ |
f6c34742 AT |
309 | void recv_uid_list(int f, struct file_list *flist) |
310 | { | |
311 | int id, i; | |
312 | char *name; | |
f6c34742 | 313 | |
d49def48 | 314 | if (preserve_uid && !numeric_ids) { |
f6c34742 | 315 | /* read the uid list */ |
5b540e86 | 316 | while ((id = read_int(f)) != 0) { |
f6c34742 | 317 | int len = read_byte(f); |
58cadc86 | 318 | name = new_array(char, len+1); |
d49def48 WD |
319 | if (!name) |
320 | out_of_memory("recv_uid_list"); | |
575f2fca | 321 | read_sbuf(f, name, len); |
d49def48 | 322 | recv_add_uid(id, name); /* node keeps name's memory */ |
84fa865c | 323 | } |
f6c34742 AT |
324 | } |
325 | ||
326 | ||
d49def48 WD |
327 | if (preserve_gid && !numeric_ids) { |
328 | /* read the gid list */ | |
5b540e86 | 329 | while ((id = read_int(f)) != 0) { |
f6c34742 | 330 | int len = read_byte(f); |
58cadc86 | 331 | name = new_array(char, len+1); |
d49def48 WD |
332 | if (!name) |
333 | out_of_memory("recv_uid_list"); | |
575f2fca | 334 | read_sbuf(f, name, len); |
d49def48 | 335 | recv_add_gid(id, name); /* node keeps name's memory */ |
84fa865c | 336 | } |
f6c34742 AT |
337 | } |
338 | ||
f6c34742 | 339 | /* now convert the uid/gid of all files in the list to the mapped |
84fa865c | 340 | * uid/gid */ |
d49def48 WD |
341 | if (am_root && preserve_uid && !numeric_ids) { |
342 | for (i = 0; i < flist->count; i++) | |
3ec4dd97 | 343 | flist->files[i]->uid = match_uid(flist->files[i]->uid); |
d49def48 WD |
344 | } |
345 | if (preserve_gid && (!am_root || !numeric_ids)) { | |
346 | for (i = 0; i < flist->count; i++) | |
3ec4dd97 | 347 | flist->files[i]->gid = match_gid(flist->files[i]->gid); |
f6c34742 AT |
348 | } |
349 | } |