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 | { | |
44 | struct idlist *list = (struct idlist *)malloc(sizeof(list[0])); | |
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 | ||
109 | static gid_t match_gid(gid_t gid) | |
110 | { | |
111 | static gid_t last_in, last_out; | |
112 | struct idlist *list = gidlist; | |
113 | ||
114 | if (gid == last_in) return last_out; | |
115 | ||
116 | last_in = gid; | |
117 | ||
118 | while (list) { | |
119 | if (list->id == (int)gid) { | |
120 | last_out = (gid_t)list->id2; | |
121 | return last_out; | |
122 | } | |
123 | list = list->next; | |
124 | } | |
125 | ||
460f6b99 DD |
126 | if (am_root) |
127 | last_out = gid; | |
128 | else | |
86692050 | 129 | last_out = (gid_t) -1; |
f6c34742 AT |
130 | return last_out; |
131 | } | |
132 | ||
133 | /* add a uid to the list of uids */ | |
134 | void add_uid(uid_t uid) | |
135 | { | |
136 | struct idlist *list = uidlist; | |
137 | char *name; | |
138 | ||
139 | if (numeric_ids) return; | |
140 | ||
141 | /* don't map root */ | |
142 | if (uid==0) return; | |
143 | ||
144 | if (!list) { | |
145 | if (!(name = uid_to_name(uid))) return; | |
146 | uidlist = add_list((int)uid, name); | |
147 | return; | |
148 | } | |
149 | ||
150 | while (list->next) { | |
151 | if (list->id == (int)uid) return; | |
152 | list = list->next; | |
153 | } | |
154 | ||
155 | if (list->id == (int)uid) return; | |
156 | ||
157 | if (!(name = uid_to_name(uid))) return; | |
158 | ||
159 | list->next = add_list((int)uid, name); | |
160 | } | |
161 | ||
162 | /* add a gid to the list of gids */ | |
163 | void add_gid(gid_t gid) | |
164 | { | |
165 | struct idlist *list = gidlist; | |
166 | char *name; | |
167 | ||
168 | if (numeric_ids) return; | |
169 | ||
170 | /* don't map root */ | |
171 | if (gid==0) return; | |
172 | ||
173 | if (!list) { | |
174 | if (!(name = gid_to_name(gid))) return; | |
175 | gidlist = add_list((int)gid, name); | |
176 | return; | |
177 | } | |
178 | ||
179 | while (list->next) { | |
180 | if (list->id == (int)gid) return; | |
181 | list = list->next; | |
182 | } | |
183 | ||
184 | if (list->id == (int)gid) return; | |
185 | ||
186 | if (!(name = gid_to_name(gid))) return; | |
187 | ||
188 | list->next = add_list((int)gid, name); | |
189 | } | |
190 | ||
191 | ||
192 | /* send a complete uid/gid mapping to the peer */ | |
193 | void send_uid_list(int f) | |
194 | { | |
195 | struct idlist *list; | |
196 | ||
197 | if (numeric_ids) return; | |
198 | ||
199 | if (preserve_uid) { | |
200 | /* we send sequences of uid/byte-length/name */ | |
201 | list = uidlist; | |
202 | while (list) { | |
203 | int len = strlen(list->name); | |
204 | write_int(f, list->id); | |
205 | write_byte(f, len); | |
206 | write_buf(f, list->name, len); | |
207 | list = list->next; | |
208 | } | |
209 | ||
210 | /* terminate the uid list with a 0 uid. We explicitly exclude | |
211 | 0 from the list */ | |
212 | write_int(f, 0); | |
213 | } | |
214 | ||
215 | if (preserve_gid) { | |
216 | list = gidlist; | |
217 | while (list) { | |
218 | int len = strlen(list->name); | |
219 | write_int(f, list->id); | |
220 | write_byte(f, len); | |
221 | write_buf(f, list->name, len); | |
222 | list = list->next; | |
223 | } | |
224 | write_int(f, 0); | |
225 | } | |
226 | } | |
227 | ||
228 | /* recv a complete uid/gid mapping from the peer and map the uid/gid | |
229 | in the file list to local names */ | |
230 | void recv_uid_list(int f, struct file_list *flist) | |
231 | { | |
232 | int id, i; | |
233 | char *name; | |
234 | struct idlist *list; | |
235 | ||
236 | if (numeric_ids) return; | |
237 | ||
238 | if (preserve_uid) { | |
239 | /* read the uid list */ | |
240 | list = uidlist; | |
241 | id = read_int(f); | |
242 | while (id != 0) { | |
243 | int len = read_byte(f); | |
575f2fca | 244 | name = (char *)malloc(len+1); |
f6c34742 | 245 | if (!name) out_of_memory("recv_uid_list"); |
575f2fca | 246 | read_sbuf(f, name, len); |
f6c34742 AT |
247 | if (!list) { |
248 | uidlist = add_list(id, name); | |
249 | list = uidlist; | |
250 | } else { | |
251 | list->next = add_list(id, name); | |
252 | list = list->next; | |
253 | } | |
254 | list->id2 = map_uid(id, name); | |
255 | free(name); | |
256 | id = read_int(f); | |
257 | } | |
258 | } | |
259 | ||
260 | ||
261 | if (preserve_gid) { | |
262 | /* and the gid list */ | |
263 | list = gidlist; | |
264 | id = read_int(f); | |
265 | while (id != 0) { | |
266 | int len = read_byte(f); | |
575f2fca | 267 | name = (char *)malloc(len+1); |
f6c34742 | 268 | if (!name) out_of_memory("recv_uid_list"); |
575f2fca | 269 | read_sbuf(f, name, len); |
f6c34742 AT |
270 | if (!list) { |
271 | gidlist = add_list(id, name); | |
272 | list = gidlist; | |
273 | } else { | |
274 | list->next = add_list(id, name); | |
275 | list = list->next; | |
276 | } | |
277 | list->id2 = map_gid(id, name); | |
278 | free(name); | |
279 | id = read_int(f); | |
280 | } | |
281 | } | |
282 | ||
460f6b99 | 283 | if (!(am_root && preserve_uid) && !preserve_gid) return; |
f6c34742 AT |
284 | |
285 | /* now convert the uid/gid of all files in the list to the mapped | |
286 | uid/gid */ | |
287 | for (i=0;i<flist->count;i++) { | |
460f6b99 | 288 | if (am_root && preserve_uid && flist->files[i]->uid != 0) { |
3ec4dd97 | 289 | flist->files[i]->uid = match_uid(flist->files[i]->uid); |
f6c34742 | 290 | } |
3ec4dd97 AT |
291 | if (preserve_gid && flist->files[i]->gid != 0) { |
292 | flist->files[i]->gid = match_gid(flist->files[i]->gid); | |
f6c34742 AT |
293 | } |
294 | } | |
295 | } |