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 | |
6 | * Copyright (C) 2004, 2005, 2006 Wayne Davison | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
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 WD |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 WD |
21 | */ |
22 | ||
23 | /* If the source username/group does not exist on the target then use | |
24 | * the numeric IDs. Never do any mapping for uid=0 or gid=0 as these | |
25 | * are special. */ | |
f6c34742 AT |
26 | |
27 | #include "rsync.h" | |
28 | ||
4f5b0756 | 29 | #ifdef HAVE_GETGROUPS |
25ff0441 | 30 | # ifndef GETGROUPS_T |
1df395f7 WD |
31 | # define GETGROUPS_T gid_t |
32 | # endif | |
72fc7ec5 WD |
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 | ||
d49def48 WD |
50 | static 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 */ |
65 | static 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 */ | |
74 | static 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 | ||
f6c34742 AT |
82 | static int map_uid(int id, char *name) |
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 | ||
90 | static int map_gid(int id, char *name) | |
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 |
98 | static 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"); | |
187e9c24 WD |
129 | sprintf(gidbuf, "process has %d gid%s: ", |
130 | ngroups, ngroups == 1? "" : "s"); | |
131 | pos = strlen(gidbuf); | |
84fa865c | 132 | for (n = 0; n < ngroups; n++) { |
d49def48 | 133 | sprintf(gidbuf+pos, " %d", (int)gidset[n]); |
187e9c24 | 134 | pos += strlen(gidbuf+pos); |
84fa865c | 135 | } |
187e9c24 | 136 | rprintf(FINFO, "%s\n", gidbuf); |
f567e9b3 | 137 | free(gidbuf); |
84fa865c | 138 | } |
5b540e86 WD |
139 | } |
140 | ||
141 | last_in = gid; | |
5b540e86 | 142 | for (n = 0; n < ngroups; n++) { |
a2687b64 WD |
143 | if (gidset[n] == gid) |
144 | return last_out = 1; | |
5b540e86 | 145 | } |
a2687b64 | 146 | return last_out = 0; |
5b540e86 WD |
147 | |
148 | #else | |
a2687b64 | 149 | static gid_t mygid = GID_NONE; |
187e9c24 | 150 | if (mygid == GID_NONE) { |
670d8abf | 151 | mygid = MY_GID(); |
187e9c24 | 152 | if (verbose > 3) |
d49def48 | 153 | rprintf(FINFO, "process has gid %d\n", (int)mygid); |
187e9c24 | 154 | } |
a2687b64 | 155 | return gid == mygid; |
5b540e86 WD |
156 | #endif |
157 | } | |
158 | ||
d49def48 WD |
159 | /* Add a uid to the list of uids. Only called on receiving side. */ |
160 | static struct idlist *recv_add_uid(int id, char *name) | |
161 | { | |
162 | int id2 = name ? map_uid(id, name) : id; | |
163 | struct idlist *node; | |
164 | ||
b66d0085 | 165 | node = add_to_list(&uidlist, id, name, id2); |
d49def48 WD |
166 | |
167 | if (verbose > 3) { | |
168 | rprintf(FINFO, "uid %d(%s) maps to %d\n", | |
169 | id, name ? name : "", id2); | |
170 | } | |
171 | ||
172 | return node; | |
173 | } | |
174 | ||
175 | /* Add a gid to the list of gids. Only called on receiving side. */ | |
176 | static struct idlist *recv_add_gid(int id, char *name) | |
177 | { | |
178 | int id2 = name ? map_gid(id, name) : id; | |
179 | struct idlist *node; | |
180 | ||
181 | if (!am_root && !is_in_group(id2)) | |
182 | id2 = GID_NONE; | |
183 | node = add_to_list(&gidlist, id, name, id2); | |
184 | ||
185 | if (verbose > 3) { | |
186 | rprintf(FINFO, "gid %d(%s) maps to %d\n", | |
187 | id, name ? name : "", id2); | |
188 | } | |
189 | ||
190 | return node; | |
191 | } | |
192 | ||
ade7292a WD |
193 | /* this function is a definate candidate for a faster algorithm */ |
194 | static uid_t match_uid(uid_t uid) | |
195 | { | |
196 | static uid_t last_in, last_out; | |
d49def48 WD |
197 | struct idlist *list; |
198 | ||
199 | if (uid == 0) | |
200 | return 0; | |
ade7292a WD |
201 | |
202 | if (uid == last_in) | |
203 | return last_out; | |
204 | ||
205 | last_in = uid; | |
206 | ||
d49def48 WD |
207 | for (list = uidlist; list; list = list->next) { |
208 | if (list->id == (int)uid) | |
209 | return last_out = (uid_t)list->id2; | |
ade7292a WD |
210 | } |
211 | ||
d49def48 | 212 | return last_out = uid; |
ade7292a WD |
213 | } |
214 | ||
f6c34742 AT |
215 | static gid_t match_gid(gid_t gid) |
216 | { | |
a2687b64 | 217 | static gid_t last_in = GID_NONE, last_out = GID_NONE; |
d49def48 WD |
218 | struct idlist *list; |
219 | ||
01363a24 WD |
220 | if (gid == GID_NONE) |
221 | return GID_NONE; | |
f6c34742 | 222 | |
a2687b64 WD |
223 | if (gid == last_in) |
224 | return last_out; | |
f6c34742 AT |
225 | |
226 | last_in = gid; | |
227 | ||
d49def48 WD |
228 | for (list = gidlist; list; list = list->next) { |
229 | if (list->id == (int)gid) | |
230 | return last_out = (gid_t)list->id2; | |
f6c34742 | 231 | } |
d49def48 WD |
232 | |
233 | list = recv_add_gid(gid, NULL); | |
234 | return last_out = list->id2; | |
f6c34742 AT |
235 | } |
236 | ||
d49def48 | 237 | /* Add a uid to the list of uids. Only called on sending side. */ |
f6c34742 AT |
238 | void add_uid(uid_t uid) |
239 | { | |
d49def48 | 240 | struct idlist *list; |
f6c34742 | 241 | |
d49def48 | 242 | if (uid == 0) /* don't map root */ |
f6c34742 | 243 | return; |
f6c34742 | 244 | |
d49def48 WD |
245 | for (list = uidlist; list; list = list->next) { |
246 | if (list->id == (int)uid) | |
247 | return; | |
f6c34742 AT |
248 | } |
249 | ||
d49def48 | 250 | add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0); |
f6c34742 AT |
251 | } |
252 | ||
d49def48 | 253 | /* Add a gid to the list of gids. Only called on sending side. */ |
f6c34742 AT |
254 | void add_gid(gid_t gid) |
255 | { | |
d49def48 | 256 | struct idlist *list; |
f6c34742 | 257 | |
d49def48 | 258 | if (gid == 0) /* don't map root */ |
f6c34742 | 259 | return; |
f6c34742 | 260 | |
d49def48 WD |
261 | for (list = gidlist; list; list = list->next) { |
262 | if (list->id == (int)gid) | |
263 | return; | |
f6c34742 AT |
264 | } |
265 | ||
d49def48 | 266 | add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0); |
f6c34742 AT |
267 | } |
268 | ||
269 | ||
270 | /* send a complete uid/gid mapping to the peer */ | |
271 | void send_uid_list(int f) | |
272 | { | |
273 | struct idlist *list; | |
274 | ||
d49def48 WD |
275 | if (numeric_ids) |
276 | return; | |
f6c34742 AT |
277 | |
278 | if (preserve_uid) { | |
d49def48 | 279 | int len; |
f6c34742 | 280 | /* we send sequences of uid/byte-length/name */ |
d49def48 WD |
281 | for (list = uidlist; list; list = list->next) { |
282 | if (!list->name) | |
283 | continue; | |
284 | len = strlen(list->name); | |
f6c34742 AT |
285 | write_int(f, list->id); |
286 | write_byte(f, len); | |
287 | write_buf(f, list->name, len); | |
f6c34742 AT |
288 | } |
289 | ||
290 | /* terminate the uid list with a 0 uid. We explicitly exclude | |
84fa865c | 291 | * 0 from the list */ |
f6c34742 AT |
292 | write_int(f, 0); |
293 | } | |
294 | ||
295 | if (preserve_gid) { | |
d49def48 WD |
296 | int len; |
297 | for (list = gidlist; list; list = list->next) { | |
298 | if (!list->name) | |
299 | continue; | |
300 | len = strlen(list->name); | |
f6c34742 AT |
301 | write_int(f, list->id); |
302 | write_byte(f, len); | |
303 | write_buf(f, list->name, len); | |
f6c34742 AT |
304 | } |
305 | write_int(f, 0); | |
306 | } | |
307 | } | |
308 | ||
309 | /* recv a complete uid/gid mapping from the peer and map the uid/gid | |
84fa865c | 310 | * in the file list to local names */ |
7b6fa00f | 311 | void recv_uid_list(int f, struct file_list *flist) |
f6c34742 AT |
312 | { |
313 | int id, i; | |
314 | char *name; | |
f6c34742 | 315 | |
d49def48 | 316 | if (preserve_uid && !numeric_ids) { |
f6c34742 | 317 | /* read the uid list */ |
5b540e86 | 318 | while ((id = read_int(f)) != 0) { |
f6c34742 | 319 | int len = read_byte(f); |
58cadc86 | 320 | name = new_array(char, len+1); |
d49def48 WD |
321 | if (!name) |
322 | out_of_memory("recv_uid_list"); | |
575f2fca | 323 | read_sbuf(f, name, len); |
d49def48 | 324 | recv_add_uid(id, name); /* node keeps name's memory */ |
84fa865c | 325 | } |
f6c34742 AT |
326 | } |
327 | ||
d49def48 WD |
328 | if (preserve_gid && !numeric_ids) { |
329 | /* read the gid list */ | |
5b540e86 | 330 | while ((id = read_int(f)) != 0) { |
f6c34742 | 331 | int len = read_byte(f); |
58cadc86 | 332 | name = new_array(char, len+1); |
d49def48 WD |
333 | if (!name) |
334 | out_of_memory("recv_uid_list"); | |
575f2fca | 335 | read_sbuf(f, name, len); |
d49def48 | 336 | recv_add_gid(id, name); /* node keeps name's memory */ |
84fa865c | 337 | } |
f6c34742 AT |
338 | } |
339 | ||
7b6fa00f | 340 | /* Now convert all the uids/gids from sender values to our values. */ |
d49def48 | 341 | if (am_root && preserve_uid && !numeric_ids) { |
7b6fa00f WD |
342 | for (i = 0; i < flist->count; i++) |
343 | flist->files[i]->uid = match_uid(flist->files[i]->uid); | |
d49def48 WD |
344 | } |
345 | if (preserve_gid && (!am_root || !numeric_ids)) { | |
7b6fa00f WD |
346 | for (i = 0; i < flist->count; i++) |
347 | flist->files[i]->gid = match_gid(flist->files[i]->gid); | |
5e58e3f9 | 348 | } |
f6c34742 | 349 | } |