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 | ||
28 | extern int preserve_uid; | |
29 | extern int preserve_gid; | |
30 | extern int numeric_ids; | |
460f6b99 | 31 | extern int am_root; |
f6c34742 AT |
32 | |
33 | struct idlist { | |
34 | struct idlist *next; | |
35 | int id, id2; | |
36 | char *name; | |
37 | }; | |
38 | ||
39 | static struct idlist *uidlist; | |
40 | static struct idlist *gidlist; | |
41 | ||
42 | static struct idlist *add_list(int id, char *name) | |
43 | { | |
58cadc86 | 44 | struct idlist *list = new(struct idlist); |
f6c34742 AT |
45 | if (!list) out_of_memory("add_list"); |
46 | list->next = NULL; | |
47 | list->name = strdup(name); | |
48 | if (!list->name) out_of_memory("add_list"); | |
49 | list->id = (int)id; | |
50 | return list; | |
51 | } | |
52 | ||
53 | ||
54 | ||
55 | /* turn a uid into a user name */ | |
56 | static char *uid_to_name(uid_t uid) | |
57 | { | |
58 | struct passwd *pass = getpwuid(uid); | |
59 | if (pass) return(pass->pw_name); | |
60 | return NULL; | |
61 | } | |
62 | ||
63 | /* turn a gid into a group name */ | |
64 | static char *gid_to_name(gid_t gid) | |
65 | { | |
66 | struct group *grp = getgrgid(gid); | |
67 | if (grp) return(grp->gr_name); | |
68 | return NULL; | |
69 | } | |
70 | ||
f6c34742 AT |
71 | static int map_uid(int id, char *name) |
72 | { | |
8ef4ffd6 AT |
73 | uid_t uid; |
74 | if (name_to_uid(name, &uid) && uid != 0) | |
75 | return uid; | |
f6c34742 AT |
76 | return id; |
77 | } | |
78 | ||
79 | static int map_gid(int id, char *name) | |
80 | { | |
8ef4ffd6 AT |
81 | gid_t gid; |
82 | if (name_to_gid(name, &gid) && gid != 0) | |
83 | return gid; | |
f6c34742 AT |
84 | return id; |
85 | } | |
86 | ||
87 | /* this function is a definate candidate for a faster algorithm */ | |
88 | static uid_t match_uid(uid_t uid) | |
89 | { | |
90 | static uid_t last_in, last_out; | |
91 | struct idlist *list = uidlist; | |
92 | ||
93 | if (uid == last_in) return last_out; | |
94 | ||
95 | last_in = uid; | |
96 | ||
97 | while (list) { | |
98 | if (list->id == (int)uid) { | |
99 | last_out = (uid_t)list->id2; | |
100 | return last_out; | |
101 | } | |
102 | list = list->next; | |
103 | } | |
104 | ||
105 | last_out = uid; | |
106 | return last_out; | |
107 | } | |
108 | ||
5b540e86 WD |
109 | static int is_in_group(gid_t gid) |
110 | { | |
111 | #ifdef GETGROUPS_T | |
112 | static gid_t last_in = (gid_t) -2, last_out; | |
113 | static int ngroups = -2; | |
114 | static GETGROUPS_T *gidset; | |
115 | int n; | |
116 | ||
117 | if (gid == last_in) | |
118 | return last_out; | |
119 | if (ngroups < -1) { | |
120 | /* treat failure (-1) as if not member of any group */ | |
121 | ngroups = getgroups(0, 0); | |
122 | if (ngroups > 0) { | |
123 | gidset = new_array(GETGROUPS_T, ngroups); | |
124 | ngroups = getgroups(ngroups, gidset); | |
125 | } | |
126 | } | |
127 | ||
128 | last_in = gid; | |
129 | last_out = 0; | |
130 | for (n = 0; n < ngroups; n++) { | |
131 | if (gidset[n] == gid) { | |
132 | last_out = 1; | |
133 | break; | |
134 | } | |
135 | } | |
136 | return last_out; | |
137 | ||
138 | #else | |
139 | return 0; | |
140 | #endif | |
141 | } | |
142 | ||
f6c34742 AT |
143 | static gid_t match_gid(gid_t gid) |
144 | { | |
145 | static gid_t last_in, last_out; | |
146 | struct idlist *list = gidlist; | |
147 | ||
148 | if (gid == last_in) return last_out; | |
149 | ||
150 | last_in = gid; | |
151 | ||
152 | while (list) { | |
153 | if (list->id == (int)gid) { | |
154 | last_out = (gid_t)list->id2; | |
155 | return last_out; | |
156 | } | |
157 | list = list->next; | |
158 | } | |
159 | ||
460f6b99 DD |
160 | if (am_root) |
161 | last_out = gid; | |
162 | else | |
a60e2dca | 163 | last_out = GID_NONE; |
f6c34742 AT |
164 | return last_out; |
165 | } | |
166 | ||
167 | /* add a uid to the list of uids */ | |
168 | void add_uid(uid_t uid) | |
169 | { | |
170 | struct idlist *list = uidlist; | |
171 | char *name; | |
172 | ||
173 | if (numeric_ids) return; | |
174 | ||
175 | /* don't map root */ | |
176 | if (uid==0) return; | |
177 | ||
178 | if (!list) { | |
179 | if (!(name = uid_to_name(uid))) return; | |
180 | uidlist = add_list((int)uid, name); | |
181 | return; | |
182 | } | |
183 | ||
184 | while (list->next) { | |
185 | if (list->id == (int)uid) return; | |
186 | list = list->next; | |
187 | } | |
188 | ||
189 | if (list->id == (int)uid) return; | |
190 | ||
191 | if (!(name = uid_to_name(uid))) return; | |
192 | ||
193 | list->next = add_list((int)uid, name); | |
194 | } | |
195 | ||
196 | /* add a gid to the list of gids */ | |
197 | void add_gid(gid_t gid) | |
198 | { | |
199 | struct idlist *list = gidlist; | |
200 | char *name; | |
201 | ||
202 | if (numeric_ids) return; | |
203 | ||
204 | /* don't map root */ | |
205 | if (gid==0) return; | |
206 | ||
207 | if (!list) { | |
208 | if (!(name = gid_to_name(gid))) return; | |
209 | gidlist = add_list((int)gid, name); | |
210 | return; | |
211 | } | |
212 | ||
213 | while (list->next) { | |
214 | if (list->id == (int)gid) return; | |
215 | list = list->next; | |
216 | } | |
217 | ||
218 | if (list->id == (int)gid) return; | |
219 | ||
220 | if (!(name = gid_to_name(gid))) return; | |
221 | ||
222 | list->next = add_list((int)gid, name); | |
223 | } | |
224 | ||
225 | ||
226 | /* send a complete uid/gid mapping to the peer */ | |
227 | void send_uid_list(int f) | |
228 | { | |
229 | struct idlist *list; | |
230 | ||
231 | if (numeric_ids) return; | |
232 | ||
233 | if (preserve_uid) { | |
234 | /* we send sequences of uid/byte-length/name */ | |
235 | list = uidlist; | |
236 | while (list) { | |
237 | int len = strlen(list->name); | |
238 | write_int(f, list->id); | |
239 | write_byte(f, len); | |
240 | write_buf(f, list->name, len); | |
241 | list = list->next; | |
242 | } | |
243 | ||
244 | /* terminate the uid list with a 0 uid. We explicitly exclude | |
245 | 0 from the list */ | |
246 | write_int(f, 0); | |
247 | } | |
248 | ||
249 | if (preserve_gid) { | |
250 | list = gidlist; | |
251 | while (list) { | |
252 | int len = strlen(list->name); | |
253 | write_int(f, list->id); | |
254 | write_byte(f, len); | |
255 | write_buf(f, list->name, len); | |
256 | list = list->next; | |
257 | } | |
258 | write_int(f, 0); | |
259 | } | |
260 | } | |
261 | ||
262 | /* recv a complete uid/gid mapping from the peer and map the uid/gid | |
263 | in the file list to local names */ | |
264 | void recv_uid_list(int f, struct file_list *flist) | |
265 | { | |
266 | int id, i; | |
267 | char *name; | |
268 | struct idlist *list; | |
269 | ||
270 | if (numeric_ids) return; | |
271 | ||
272 | if (preserve_uid) { | |
273 | /* read the uid list */ | |
274 | list = uidlist; | |
5b540e86 | 275 | while ((id = read_int(f)) != 0) { |
f6c34742 | 276 | int len = read_byte(f); |
58cadc86 | 277 | name = new_array(char, len+1); |
f6c34742 | 278 | if (!name) out_of_memory("recv_uid_list"); |
575f2fca | 279 | read_sbuf(f, name, len); |
f6c34742 AT |
280 | if (!list) { |
281 | uidlist = add_list(id, name); | |
282 | list = uidlist; | |
283 | } else { | |
284 | list->next = add_list(id, name); | |
285 | list = list->next; | |
286 | } | |
287 | list->id2 = map_uid(id, name); | |
288 | free(name); | |
f6c34742 AT |
289 | } |
290 | } | |
291 | ||
292 | ||
293 | if (preserve_gid) { | |
294 | /* and the gid list */ | |
295 | list = gidlist; | |
5b540e86 | 296 | while ((id = read_int(f)) != 0) { |
f6c34742 | 297 | int len = read_byte(f); |
58cadc86 | 298 | name = new_array(char, len+1); |
f6c34742 | 299 | if (!name) out_of_memory("recv_uid_list"); |
575f2fca | 300 | read_sbuf(f, name, len); |
f6c34742 AT |
301 | if (!list) { |
302 | gidlist = add_list(id, name); | |
303 | list = gidlist; | |
304 | } else { | |
305 | list->next = add_list(id, name); | |
306 | list = list->next; | |
307 | } | |
308 | list->id2 = map_gid(id, name); | |
5b540e86 | 309 | if (!am_root && !is_in_group(list->id2)) |
a60e2dca | 310 | list->id2 = GID_NONE; |
f6c34742 | 311 | free(name); |
f6c34742 AT |
312 | } |
313 | } | |
314 | ||
460f6b99 | 315 | if (!(am_root && preserve_uid) && !preserve_gid) return; |
f6c34742 AT |
316 | |
317 | /* now convert the uid/gid of all files in the list to the mapped | |
318 | uid/gid */ | |
319 | for (i=0;i<flist->count;i++) { | |
460f6b99 | 320 | if (am_root && preserve_uid && flist->files[i]->uid != 0) { |
3ec4dd97 | 321 | flist->files[i]->uid = match_uid(flist->files[i]->uid); |
f6c34742 | 322 | } |
3ec4dd97 AT |
323 | if (preserve_gid && flist->files[i]->gid != 0) { |
324 | flist->files[i]->gid = match_gid(flist->files[i]->gid); | |
f6c34742 AT |
325 | } |
326 | } | |
327 | } |