Made the getgroups() code a little more portable. This will hopefully
[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) return last_out;
101
102         last_in = uid;
103
104         while (list) {
105                 if (list->id == (int)uid) {
106                         last_out = (uid_t)list->id2;
107                         return last_out;
108                 }
109                 list = list->next;
110         }
111         
112         last_out = uid;
113         return last_out;
114 }
115
116 static int is_in_group(gid_t gid)
117 {
118 #ifdef GETGROUPS_T
119         static gid_t last_in = (gid_t) -2, last_out;
120         static int ngroups = -2;
121         static GETGROUPS_T *gidset;
122         int n;
123
124         if (gid == last_in)
125                 return last_out;
126         if (ngroups < -1) {
127                 gid_t mygid = getgid();
128                 ngroups = getgroups(0, 0);
129                 /* If that didn't work, perhaps 0 isn't treated specially? */
130                 if (ngroups < 0)
131                         ngroups = NGROUPS_MAX;
132                 gidset = new_array(GETGROUPS_T, ngroups+1);
133                 if (ngroups > 0)
134                         ngroups = getgroups(ngroups, gidset);
135                 /* The default gid might not be in the list on some systems. */
136                 for (n = 0; n < ngroups; n++) {
137                         if (gidset[n] == mygid)
138                                 break;
139                 }
140                 if (n == ngroups)
141                         gidset[ngroups++] = mygid;
142         }
143
144         last_in = gid;
145         last_out = 0;
146         for (n = 0; n < ngroups; n++) {
147                 if (gidset[n] == gid) {
148                         last_out = 1;
149                         break;
150                 }
151         }
152         return last_out;
153
154 #else
155         return 0;
156 #endif
157 }
158
159 static gid_t match_gid(gid_t gid)
160 {
161         static gid_t last_in, last_out;
162         struct idlist *list = gidlist;
163
164         if (gid == last_in) return last_out;
165
166         last_in = gid;
167
168         while (list) {
169                 if (list->id == (int)gid) {
170                         last_out = (gid_t)list->id2;
171                         return last_out;
172                 }
173                 list = list->next;
174         }
175         
176         if (am_root)
177                 last_out = gid;
178         else
179                 last_out = GID_NONE;
180         return last_out;
181 }
182
183 /* add a uid to the list of uids */
184 void add_uid(uid_t uid)
185 {
186         struct idlist *list = uidlist;
187         char *name;
188
189         if (numeric_ids) return;
190
191         /* don't map root */
192         if (uid==0) return;
193
194         if (!list) {
195                 if (!(name = uid_to_name(uid))) return;
196                 uidlist = add_list((int)uid, name);
197                 return;
198         }
199
200         while (list->next) {
201                 if (list->id == (int)uid) return;
202                 list = list->next;
203         }
204
205         if (list->id == (int)uid) return;
206
207         if (!(name = uid_to_name(uid))) return;
208
209         list->next = add_list((int)uid, name);
210 }
211
212 /* add a gid to the list of gids */
213 void add_gid(gid_t gid)
214 {
215         struct idlist *list = gidlist;
216         char *name;
217
218         if (numeric_ids) return;
219
220         /* don't map root */
221         if (gid==0) return;
222
223         if (!list) {
224                 if (!(name = gid_to_name(gid))) return;
225                 gidlist = add_list((int)gid, name);
226                 return;
227         }
228
229         while (list->next) {
230                 if (list->id == (int)gid) return;
231                 list = list->next;
232         }
233
234         if (list->id == (int)gid) return;
235
236         if (!(name = gid_to_name(gid))) return;
237
238         list->next = add_list((int)gid, name);
239 }
240
241
242 /* send a complete uid/gid mapping to the peer */
243 void send_uid_list(int f)
244 {
245         struct idlist *list;
246
247         if (numeric_ids) return;
248
249         if (preserve_uid) {
250                 /* we send sequences of uid/byte-length/name */
251                 list = uidlist;
252                 while (list) {
253                         int len = strlen(list->name);
254                         write_int(f, list->id);
255                         write_byte(f, len);
256                         write_buf(f, list->name, len);
257                         list = list->next;
258                 }
259
260                 /* terminate the uid list with a 0 uid. We explicitly exclude
261                    0 from the list */
262                 write_int(f, 0);
263         }
264
265         if (preserve_gid) {
266                 list = gidlist;
267                 while (list) {
268                         int len = strlen(list->name);
269                         write_int(f, list->id);
270                         write_byte(f, len);
271                         write_buf(f, list->name, len);
272                         list = list->next;
273                 }
274                 write_int(f, 0);
275         }
276 }
277
278 /* recv a complete uid/gid mapping from the peer and map the uid/gid
279    in the file list to local names */
280 void recv_uid_list(int f, struct file_list *flist)
281 {
282         int id, i;
283         char *name;
284         struct idlist *list;
285
286         if (numeric_ids) return;
287
288         if (preserve_uid) {
289                 /* read the uid list */
290                 list = uidlist;
291                 while ((id = read_int(f)) != 0) {
292                         int len = read_byte(f);
293                         name = new_array(char, len+1);
294                         if (!name) out_of_memory("recv_uid_list");
295                         read_sbuf(f, name, len);
296                         if (!list) {
297                                 uidlist = add_list(id, name);
298                                 list = uidlist;
299                         } else {
300                                 list->next = add_list(id, name);
301                                 list = list->next;
302                         }
303                         list->id2 = map_uid(id, name);
304                         free(name);
305                 }
306         }
307
308
309         if (preserve_gid) {
310                 /* and the gid list */
311                 list = gidlist;
312                 while ((id = read_int(f)) != 0) {
313                         int len = read_byte(f);
314                         name = new_array(char, len+1);
315                         if (!name) out_of_memory("recv_uid_list");
316                         read_sbuf(f, name, len);
317                         if (!list) {
318                                 gidlist = add_list(id, name);
319                                 list = gidlist;
320                         } else {
321                                 list->next = add_list(id, name);
322                                 list = list->next;
323                         }
324                         list->id2 = map_gid(id, name);
325                         if (!am_root && !is_in_group(list->id2))
326                                 list->id2 = GID_NONE;
327                         free(name);
328                 }
329         }
330
331         if (!(am_root && preserve_uid) && !preserve_gid) return;
332
333         /* now convert the uid/gid of all files in the list to the mapped
334            uid/gid */
335         for (i = 0; i < flist->count; i++) {
336                 if (am_root && preserve_uid && flist->files[i]->uid != 0)
337                         flist->files[i]->uid = match_uid(flist->files[i]->uid);
338                 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
339                         flist->files[i]->gid = match_gid(flist->files[i]->gid);
340         }
341 }