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