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 | |
ba2133d6 WD |
9 | * it under the terms of the GNU General Public License version 2 as |
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 WD |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
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" | |
27 | ||
4f5b0756 | 28 | #ifdef HAVE_GETGROUPS |
25ff0441 | 29 | # ifndef GETGROUPS_T |
1df395f7 WD |
30 | # define GETGROUPS_T gid_t |
31 | # endif | |
72fc7ec5 WD |
32 | #endif |
33 | ||
84fa865c | 34 | extern int verbose; |
283887d7 | 35 | extern int am_root; |
f6c34742 AT |
36 | extern int preserve_uid; |
37 | extern int preserve_gid; | |
38 | extern int numeric_ids; | |
39 | ||
40 | struct idlist { | |
41 | struct idlist *next; | |
42 | int id, id2; | |
43 | char *name; | |
44 | }; | |
45 | ||
46 | static struct idlist *uidlist; | |
47 | static struct idlist *gidlist; | |
48 | ||
d49def48 WD |
49 | static struct idlist *add_to_list(struct idlist **root, int id, char *name, |
50 | int id2) | |
f6c34742 | 51 | { |
d49def48 WD |
52 | struct idlist *node = new(struct idlist); |
53 | if (!node) | |
54 | out_of_memory("add_to_list"); | |
55 | node->next = *root; | |
56 | node->name = name; | |
57 | node->id = id; | |
58 | node->id2 = id2; | |
59 | *root = node; | |
60 | return node; | |
f6c34742 AT |
61 | } |
62 | ||
f6c34742 AT |
63 | /* turn a uid into a user name */ |
64 | static char *uid_to_name(uid_t uid) | |
65 | { | |
66 | struct passwd *pass = getpwuid(uid); | |
d49def48 WD |
67 | if (pass) |
68 | return strdup(pass->pw_name); | |
f6c34742 AT |
69 | return NULL; |
70 | } | |
71 | ||
72 | /* turn a gid into a group name */ | |
73 | static char *gid_to_name(gid_t gid) | |
74 | { | |
75 | struct group *grp = getgrgid(gid); | |
d49def48 WD |
76 | if (grp) |
77 | return strdup(grp->gr_name); | |
f6c34742 AT |
78 | return NULL; |
79 | } | |
80 | ||
283887d7 | 81 | static uid_t map_uid(uid_t id, char *name) |
f6c34742 | 82 | { |
8ef4ffd6 | 83 | uid_t uid; |
b66d0085 | 84 | if (id != 0 && name_to_uid(name, &uid)) |
8ef4ffd6 | 85 | return uid; |
f6c34742 AT |
86 | return id; |
87 | } | |
88 | ||
283887d7 | 89 | static gid_t map_gid(gid_t id, char *name) |
f6c34742 | 90 | { |
8ef4ffd6 | 91 | gid_t gid; |
b66d0085 | 92 | if (id != 0 && name_to_gid(name, &gid)) |
8ef4ffd6 | 93 | return gid; |
f6c34742 AT |
94 | return id; |
95 | } | |
96 | ||
5b540e86 WD |
97 | static int is_in_group(gid_t gid) |
98 | { | |
4f5b0756 | 99 | #ifdef HAVE_GETGROUPS |
a2687b64 | 100 | static gid_t last_in = GID_NONE, last_out; |
5b540e86 WD |
101 | static int ngroups = -2; |
102 | static GETGROUPS_T *gidset; | |
103 | int n; | |
104 | ||
105 | if (gid == last_in) | |
106 | return last_out; | |
107 | if (ngroups < -1) { | |
670d8abf | 108 | gid_t mygid = MY_GID(); |
f567e9b3 | 109 | if ((ngroups = getgroups(0, NULL)) < 0) |
dbd8811b | 110 | ngroups = 0; |
72fc7ec5 | 111 | gidset = new_array(GETGROUPS_T, ngroups+1); |
f567e9b3 WD |
112 | if (!gidset) |
113 | out_of_memory("is_in_group"); | |
72fc7ec5 | 114 | if (ngroups > 0) |
5b540e86 | 115 | ngroups = getgroups(ngroups, gidset); |
72fc7ec5 WD |
116 | /* The default gid might not be in the list on some systems. */ |
117 | for (n = 0; n < ngroups; n++) { | |
118 | if (gidset[n] == mygid) | |
119 | break; | |
5b540e86 | 120 | } |
72fc7ec5 WD |
121 | if (n == ngroups) |
122 | gidset[ngroups++] = mygid; | |
84fa865c | 123 | if (verbose > 3) { |
187e9c24 | 124 | int pos; |
f567e9b3 WD |
125 | char *gidbuf = new_array(char, ngroups*21+32); |
126 | if (!gidbuf) | |
127 | out_of_memory("is_in_group"); | |
10944395 WD |
128 | pos = snprintf(gidbuf, 32, "process has %d gid%s: ", |
129 | ngroups, ngroups == 1? "" : "s"); | |
84fa865c | 130 | for (n = 0; n < ngroups; n++) { |
10944395 | 131 | pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]); |
84fa865c | 132 | } |
187e9c24 | 133 | rprintf(FINFO, "%s\n", gidbuf); |
f567e9b3 | 134 | free(gidbuf); |
84fa865c | 135 | } |
5b540e86 WD |
136 | } |
137 | ||
138 | last_in = gid; | |
5b540e86 | 139 | for (n = 0; n < ngroups; n++) { |
a2687b64 WD |
140 | if (gidset[n] == gid) |
141 | return last_out = 1; | |
5b540e86 | 142 | } |
a2687b64 | 143 | return last_out = 0; |
5b540e86 WD |
144 | |
145 | #else | |
a2687b64 | 146 | static gid_t mygid = GID_NONE; |
187e9c24 | 147 | if (mygid == GID_NONE) { |
670d8abf | 148 | mygid = MY_GID(); |
187e9c24 | 149 | if (verbose > 3) |
d49def48 | 150 | rprintf(FINFO, "process has gid %d\n", (int)mygid); |
187e9c24 | 151 | } |
a2687b64 | 152 | return gid == mygid; |
5b540e86 WD |
153 | #endif |
154 | } | |
155 | ||
d49def48 | 156 | /* Add a uid to the list of uids. Only called on receiving side. */ |
283887d7 | 157 | static uid_t recv_add_uid(uid_t id, char *name) |
d49def48 | 158 | { |
283887d7 | 159 | uid_t id2 = name ? map_uid(id, name) : id; |
d49def48 WD |
160 | struct idlist *node; |
161 | ||
283887d7 | 162 | node = add_to_list(&uidlist, (int)id, name, (int)id2); |
d49def48 WD |
163 | |
164 | if (verbose > 3) { | |
165 | rprintf(FINFO, "uid %d(%s) maps to %d\n", | |
283887d7 | 166 | (int)id, name ? name : "", (int)id2); |
d49def48 WD |
167 | } |
168 | ||
283887d7 | 169 | return id2; |
d49def48 WD |
170 | } |
171 | ||
172 | /* Add a gid to the list of gids. Only called on receiving side. */ | |
283887d7 | 173 | static gid_t recv_add_gid(gid_t id, char *name) |
d49def48 | 174 | { |
283887d7 | 175 | gid_t id2 = name ? map_gid(id, name) : id; |
d49def48 WD |
176 | struct idlist *node; |
177 | ||
178 | if (!am_root && !is_in_group(id2)) | |
179 | id2 = GID_NONE; | |
283887d7 | 180 | node = add_to_list(&gidlist, (int)id, name, (int)id2); |
d49def48 WD |
181 | |
182 | if (verbose > 3) { | |
183 | rprintf(FINFO, "gid %d(%s) maps to %d\n", | |
283887d7 | 184 | (int)id, name ? name : "", (int)id2); |
d49def48 WD |
185 | } |
186 | ||
283887d7 | 187 | return id2; |
d49def48 WD |
188 | } |
189 | ||
ade7292a | 190 | /* this function is a definate candidate for a faster algorithm */ |
496c809f | 191 | uid_t match_uid(uid_t uid) |
ade7292a WD |
192 | { |
193 | static uid_t last_in, last_out; | |
d49def48 WD |
194 | struct idlist *list; |
195 | ||
196 | if (uid == 0) | |
197 | return 0; | |
ade7292a WD |
198 | |
199 | if (uid == last_in) | |
200 | return last_out; | |
201 | ||
202 | last_in = uid; | |
203 | ||
d49def48 WD |
204 | for (list = uidlist; list; list = list->next) { |
205 | if (list->id == (int)uid) | |
206 | return last_out = (uid_t)list->id2; | |
ade7292a WD |
207 | } |
208 | ||
d49def48 | 209 | return last_out = uid; |
ade7292a WD |
210 | } |
211 | ||
496c809f | 212 | gid_t match_gid(gid_t gid) |
f6c34742 | 213 | { |
a2687b64 | 214 | static gid_t last_in = GID_NONE, last_out = GID_NONE; |
d49def48 WD |
215 | struct idlist *list; |
216 | ||
01363a24 WD |
217 | if (gid == GID_NONE) |
218 | return GID_NONE; | |
f6c34742 | 219 | |
a2687b64 WD |
220 | if (gid == last_in) |
221 | return last_out; | |
f6c34742 AT |
222 | |
223 | last_in = gid; | |
224 | ||
d49def48 WD |
225 | for (list = gidlist; list; list = list->next) { |
226 | if (list->id == (int)gid) | |
227 | return last_out = (gid_t)list->id2; | |
f6c34742 | 228 | } |
d49def48 | 229 | |
283887d7 | 230 | return last_out = recv_add_gid(gid, NULL); |
f6c34742 AT |
231 | } |
232 | ||
d49def48 | 233 | /* Add a uid to the list of uids. Only called on sending side. */ |
496c809f | 234 | char *add_uid(uid_t uid) |
f6c34742 | 235 | { |
d49def48 | 236 | struct idlist *list; |
496c809f | 237 | struct idlist *node; |
f6c34742 | 238 | |
d49def48 | 239 | if (uid == 0) /* don't map root */ |
496c809f | 240 | return NULL; |
f6c34742 | 241 | |
d49def48 WD |
242 | for (list = uidlist; list; list = list->next) { |
243 | if (list->id == (int)uid) | |
496c809f | 244 | return NULL; |
f6c34742 AT |
245 | } |
246 | ||
496c809f WD |
247 | node = add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0); |
248 | return node->name; | |
f6c34742 AT |
249 | } |
250 | ||
d49def48 | 251 | /* Add a gid to the list of gids. Only called on sending side. */ |
496c809f | 252 | char *add_gid(gid_t gid) |
f6c34742 | 253 | { |
d49def48 | 254 | struct idlist *list; |
496c809f | 255 | struct idlist *node; |
f6c34742 | 256 | |
d49def48 | 257 | if (gid == 0) /* don't map root */ |
496c809f | 258 | return NULL; |
f6c34742 | 259 | |
d49def48 WD |
260 | for (list = gidlist; list; list = list->next) { |
261 | if (list->id == (int)gid) | |
496c809f | 262 | return NULL; |
f6c34742 AT |
263 | } |
264 | ||
496c809f WD |
265 | node = add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0); |
266 | return node->name; | |
f6c34742 AT |
267 | } |
268 | ||
f6c34742 AT |
269 | /* send a complete uid/gid mapping to the peer */ |
270 | void send_uid_list(int f) | |
271 | { | |
272 | struct idlist *list; | |
273 | ||
f6c34742 | 274 | if (preserve_uid) { |
d49def48 | 275 | int len; |
f6c34742 | 276 | /* we send sequences of uid/byte-length/name */ |
d49def48 WD |
277 | for (list = uidlist; list; list = list->next) { |
278 | if (!list->name) | |
279 | continue; | |
280 | len = strlen(list->name); | |
f6c34742 AT |
281 | write_int(f, list->id); |
282 | write_byte(f, len); | |
283 | write_buf(f, list->name, len); | |
f6c34742 AT |
284 | } |
285 | ||
286 | /* terminate the uid list with a 0 uid. We explicitly exclude | |
84fa865c | 287 | * 0 from the list */ |
f6c34742 AT |
288 | write_int(f, 0); |
289 | } | |
290 | ||
291 | if (preserve_gid) { | |
d49def48 WD |
292 | int len; |
293 | for (list = gidlist; list; list = list->next) { | |
294 | if (!list->name) | |
295 | continue; | |
296 | len = strlen(list->name); | |
f6c34742 AT |
297 | write_int(f, list->id); |
298 | write_byte(f, len); | |
299 | write_buf(f, list->name, len); | |
f6c34742 AT |
300 | } |
301 | write_int(f, 0); | |
302 | } | |
303 | } | |
304 | ||
496c809f | 305 | uid_t recv_user_name(int f, uid_t uid) |
283887d7 WD |
306 | { |
307 | int len = read_byte(f); | |
308 | char *name = new_array(char, len+1); | |
309 | if (!name) | |
310 | out_of_memory("recv_user_name"); | |
311 | read_sbuf(f, name, len); | |
312 | return recv_add_uid(uid, name); /* node keeps name's memory */ | |
313 | } | |
314 | ||
496c809f | 315 | gid_t recv_group_name(int f, gid_t gid) |
283887d7 WD |
316 | { |
317 | int len = read_byte(f); | |
318 | char *name = new_array(char, len+1); | |
319 | if (!name) | |
320 | out_of_memory("recv_group_name"); | |
321 | read_sbuf(f, name, len); | |
322 | return recv_add_gid(gid, name); /* node keeps name's memory */ | |
323 | } | |
324 | ||
f6c34742 | 325 | /* recv a complete uid/gid mapping from the peer and map the uid/gid |
84fa865c | 326 | * in the file list to local names */ |
7b6fa00f | 327 | void recv_uid_list(int f, struct file_list *flist) |
f6c34742 AT |
328 | { |
329 | int id, i; | |
f6c34742 | 330 | |
d49def48 | 331 | if (preserve_uid && !numeric_ids) { |
f6c34742 | 332 | /* read the uid list */ |
283887d7 WD |
333 | while ((id = read_int(f)) != 0) |
334 | recv_user_name(f, (uid_t)id); | |
f6c34742 AT |
335 | } |
336 | ||
d49def48 WD |
337 | if (preserve_gid && !numeric_ids) { |
338 | /* read the gid list */ | |
283887d7 WD |
339 | while ((id = read_int(f)) != 0) |
340 | recv_group_name(f, (gid_t)id); | |
f6c34742 AT |
341 | } |
342 | ||
7b6fa00f | 343 | /* Now convert all the uids/gids from sender values to our values. */ |
d49def48 | 344 | if (am_root && preserve_uid && !numeric_ids) { |
7b6fa00f | 345 | for (i = 0; i < flist->count; i++) |
f1482c33 | 346 | F_OWNER(flist->files[i]) = match_uid(F_UID(flist->files[i])); |
d49def48 WD |
347 | } |
348 | if (preserve_gid && (!am_root || !numeric_ids)) { | |
7b6fa00f | 349 | for (i = 0; i < flist->count; i++) |
f1482c33 | 350 | F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i])); |
5e58e3f9 | 351 | } |
f6c34742 | 352 | } |