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