Changed SUBPROTOCOL_VERSION from 2 to 3 because of the ACL protocol changes.
[rsync/rsync.git] / uidlist.c
1 /*
2  * Handle the mapping of uid/gid and user/group names between systems.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2004-2007 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 /* If the source username/group does not exist on the target then use
23  * the numeric IDs.  Never do any mapping for uid=0 or gid=0 as these
24  * are special. */
25
26 #include "rsync.h"
27 #include "io.h"
28
29 extern int verbose;
30 extern int am_root;
31 extern int preserve_uid;
32 extern int preserve_gid;
33 extern int preserve_acls;
34 extern int numeric_ids;
35
36 #ifdef HAVE_GETGROUPS
37 # ifndef GETGROUPS_T
38 #  define GETGROUPS_T gid_t
39 # endif
40 #endif
41
42 #define GID_NONE ((gid_t)-1)
43
44 struct idlist {
45         struct idlist *next;
46         char *name;
47         id_t id, id2;
48         uint16 flags;
49 };
50
51 static struct idlist *uidlist;
52 static struct idlist *gidlist;
53
54 static struct idlist *add_to_list(struct idlist **root, id_t id, char *name,
55                                   id_t id2, uint16 flags)
56 {
57         struct idlist *node = new(struct idlist);
58         if (!node)
59                 out_of_memory("add_to_list");
60         node->next = *root;
61         node->name = name;
62         node->id = id;
63         node->id2 = id2;
64         node->flags = flags;
65         *root = node;
66         return node;
67 }
68
69 /* turn a uid into a user name */
70 static char *uid_to_name(uid_t uid)
71 {
72         struct passwd *pass = getpwuid(uid);
73         if (pass)
74                 return strdup(pass->pw_name);
75         return NULL;
76 }
77
78 /* turn a gid into a group name */
79 static char *gid_to_name(gid_t gid)
80 {
81         struct group *grp = getgrgid(gid);
82         if (grp)
83                 return strdup(grp->gr_name);
84         return NULL;
85 }
86
87 static uid_t map_uid(uid_t id, char *name)
88 {
89         uid_t uid;
90         if (id != 0 && name_to_uid(name, &uid))
91                 return uid;
92         return id;
93 }
94
95 static gid_t map_gid(gid_t id, char *name)
96 {
97         gid_t gid;
98         if (id != 0 && name_to_gid(name, &gid))
99                 return gid;
100         return id;
101 }
102
103 static int is_in_group(gid_t gid)
104 {
105 #ifdef HAVE_GETGROUPS
106         static gid_t last_in = GID_NONE, last_out;
107         static int ngroups = -2;
108         static GETGROUPS_T *gidset;
109         int n;
110
111         if (gid == last_in)
112                 return last_out;
113         if (ngroups < -1) {
114                 gid_t mygid = MY_GID();
115                 if ((ngroups = getgroups(0, NULL)) < 0)
116                         ngroups = 0;
117                 gidset = new_array(GETGROUPS_T, ngroups+1);
118                 if (!gidset)
119                         out_of_memory("is_in_group");
120                 if (ngroups > 0)
121                         ngroups = getgroups(ngroups, gidset);
122                 /* The default gid might not be in the list on some systems. */
123                 for (n = 0; n < ngroups; n++) {
124                         if (gidset[n] == mygid)
125                                 break;
126                 }
127                 if (n == ngroups)
128                         gidset[ngroups++] = mygid;
129                 if (verbose > 3) {
130                         int pos;
131                         char *gidbuf = new_array(char, ngroups*21+32);
132                         if (!gidbuf)
133                                 out_of_memory("is_in_group");
134                         pos = snprintf(gidbuf, 32, "process has %d gid%s: ",
135                                        ngroups, ngroups == 1? "" : "s");
136                         for (n = 0; n < ngroups; n++) {
137                                 pos += snprintf(gidbuf+pos, 21, " %d", (int)gidset[n]);
138                         }
139                         rprintf(FINFO, "%s\n", gidbuf);
140                         free(gidbuf);
141                 }
142         }
143
144         last_in = gid;
145         for (n = 0; n < ngroups; n++) {
146                 if (gidset[n] == gid)
147                         return last_out = 1;
148         }
149         return last_out = 0;
150
151 #else
152         static gid_t mygid = GID_NONE;
153         if (mygid == GID_NONE) {
154                 mygid = MY_GID();
155                 if (verbose > 3)
156                         rprintf(FINFO, "process has gid %u\n", (unsigned)mygid);
157         }
158         return gid == mygid;
159 #endif
160 }
161
162 /* Add a uid to the list of uids.  Only called on receiving side. */
163 static struct idlist *recv_add_uid(uid_t id, char *name)
164 {
165         uid_t id2 = name ? map_uid(id, name) : id;
166         struct idlist *node;
167
168         node = add_to_list(&uidlist, id, name, id2, 0);
169
170         if (verbose > 3) {
171                 rprintf(FINFO, "uid %u(%s) maps to %u\n",
172                         (unsigned)id, name ? name : "", (unsigned)id2);
173         }
174
175         return node;
176 }
177
178 /* Add a gid to the list of gids.  Only called on receiving side. */
179 static struct idlist *recv_add_gid(gid_t id, char *name)
180 {
181         gid_t id2 = name ? map_gid(id, name) : id;
182         struct idlist *node;
183
184         node = add_to_list(&gidlist, id, name, id2,
185                 !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
186
187         if (verbose > 3) {
188                 rprintf(FINFO, "gid %u(%s) maps to %u\n",
189                         (unsigned)id, name ? name : "", (unsigned)id2);
190         }
191
192         return node;
193 }
194
195 /* this function is a definate candidate for a faster algorithm */
196 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 == uid)
211                         return last_out = list->id2;
212         }
213
214         return last_out = uid;
215 }
216
217 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
218 {
219         static gid_t last_in = GID_NONE, last_out = GID_NONE;
220         struct idlist *list;
221
222         if (gid == last_in)
223                 return last_out;
224
225         last_in = gid;
226
227         for (list = gidlist; list; list = list->next) {
228                 if (list->id == gid)
229                         break;
230         }
231
232         if (!list)
233                 list = recv_add_gid(gid, NULL);
234
235         if (flags_ptr && list->flags & FLAG_SKIP_GROUP)
236                 *flags_ptr |= FLAG_SKIP_GROUP;
237         return last_out = list->id2;
238 }
239
240 /* Add a uid to the list of uids.  Only called on sending side. */
241 char *add_uid(uid_t uid)
242 {
243         struct idlist *list;
244         struct idlist *node;
245
246         if (uid == 0)   /* don't map root */
247                 return NULL;
248
249         for (list = uidlist; list; list = list->next) {
250                 if (list->id == uid)
251                         return NULL;
252         }
253
254         node = add_to_list(&uidlist, uid, uid_to_name(uid), 0, 0);
255         return node->name;
256 }
257
258 /* Add a gid to the list of gids.  Only called on sending side. */
259 char *add_gid(gid_t gid)
260 {
261         struct idlist *list;
262         struct idlist *node;
263
264         if (gid == 0)   /* don't map root */
265                 return NULL;
266
267         for (list = gidlist; list; list = list->next) {
268                 if (list->id == gid)
269                         return NULL;
270         }
271
272         node = add_to_list(&gidlist, gid, gid_to_name(gid), 0, 0);
273         return node->name;
274 }
275
276 /* send a complete uid/gid mapping to the peer */
277 void send_id_list(int f)
278 {
279         struct idlist *list;
280
281         if (preserve_uid || preserve_acls) {
282                 int len;
283                 /* we send sequences of uid/byte-length/name */
284                 for (list = uidlist; list; list = list->next) {
285                         if (!list->name)
286                                 continue;
287                         len = strlen(list->name);
288                         write_varint30(f, list->id);
289                         write_byte(f, len);
290                         write_buf(f, list->name, len);
291                 }
292
293                 /* terminate the uid list with a 0 uid. We explicitly exclude
294                  * 0 from the list */
295                 write_varint30(f, 0);
296         }
297
298         if (preserve_gid || preserve_acls) {
299                 int len;
300                 for (list = gidlist; list; list = list->next) {
301                         if (!list->name)
302                                 continue;
303                         len = strlen(list->name);
304                         write_varint30(f, list->id);
305                         write_byte(f, len);
306                         write_buf(f, list->name, len);
307                 }
308                 write_varint30(f, 0);
309         }
310 }
311
312 uid_t recv_user_name(int f, uid_t uid)
313 {
314         struct idlist *node;
315         int len = read_byte(f);
316         char *name = new_array(char, len+1);
317         if (!name)
318                 out_of_memory("recv_user_name");
319         read_sbuf(f, name, len);
320         node = recv_add_uid(uid, name); /* node keeps name's memory */
321         return node->id2;
322 }
323
324 gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
325 {
326         struct idlist *node;
327         int len = read_byte(f);
328         char *name = new_array(char, len+1);
329         if (!name)
330                 out_of_memory("recv_group_name");
331         read_sbuf(f, name, len);
332         node = recv_add_gid(gid, name); /* node keeps name's memory */
333         if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
334                 *flags_ptr |= FLAG_SKIP_GROUP;
335         return node->id2;
336 }
337
338 /* recv a complete uid/gid mapping from the peer and map the uid/gid
339  * in the file list to local names */
340 void recv_id_list(int f, struct file_list *flist)
341 {
342         id_t id;
343         int i;
344
345         if ((preserve_uid || preserve_acls) && !numeric_ids) {
346                 /* read the uid list */
347                 while ((id = read_varint30(f)) != 0)
348                         recv_user_name(f, id);
349         }
350
351         if ((preserve_gid || preserve_acls) && !numeric_ids) {
352                 /* read the gid list */
353                 while ((id = read_varint30(f)) != 0)
354                         recv_group_name(f, id, NULL);
355         }
356
357         /* Now convert all the uids/gids from sender values to our values. */
358 #ifdef SUPPORT_ACLS
359         if (preserve_acls && !numeric_ids)
360                 match_acl_ids();
361 #endif
362         if (am_root && preserve_uid && !numeric_ids) {
363                 for (i = 0; i < flist->count; i++)
364                         F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
365         }
366         if (preserve_gid && (!am_root || !numeric_ids)) {
367                 for (i = 0; i < flist->count; i++) {
368                         F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
369                                                              &flist->files[i]->flags);
370                 }
371         }
372 }