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