Tweaked the new debug-logging strings.
[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                         for (n = 0; n < ngroups; n++) {
146                                 rprintf(FINFO, "process has gid %ld\n",
147                                     (long)gidset[n]);
148                         }
149                 }
150         }
151
152         last_in = gid;
153         for (n = 0; n < ngroups; n++) {
154                 if (gidset[n] == gid)
155                         return last_out = 1;
156         }
157         return last_out = 0;
158
159 #else
160         static gid_t mygid = GID_NONE;
161         if (mygid == GID_NONE)
162                 mygid = getgid();
163         return gid == mygid;
164 #endif
165 }
166
167 static gid_t match_gid(gid_t gid)
168 {
169         static gid_t last_in = GID_NONE, last_out = GID_NONE;
170         struct idlist *list = gidlist;
171
172         if (gid == last_in)
173                 return last_out;
174
175         last_in = gid;
176
177         while (list) {
178                 if (list->id == (int)gid) {
179                         last_out = (gid_t)list->id2;
180                         return last_out;
181                 }
182                 list = list->next;
183         }
184         
185         if (am_root)
186                 last_out = gid;
187         else
188                 last_out = GID_NONE;
189         return last_out;
190 }
191
192 /* add a uid to the list of uids */
193 void add_uid(uid_t uid)
194 {
195         struct idlist *list = uidlist;
196         char *name;
197
198         if (numeric_ids) return;
199
200         /* don't map root */
201         if (uid==0) return;
202
203         if (!list) {
204                 if (!(name = uid_to_name(uid))) return;
205                 uidlist = add_list((int)uid, name);
206                 return;
207         }
208
209         while (list->next) {
210                 if (list->id == (int)uid) return;
211                 list = list->next;
212         }
213
214         if (list->id == (int)uid) return;
215
216         if (!(name = uid_to_name(uid))) return;
217
218         list->next = add_list((int)uid, name);
219 }
220
221 /* add a gid to the list of gids */
222 void add_gid(gid_t gid)
223 {
224         struct idlist *list = gidlist;
225         char *name;
226
227         if (numeric_ids) return;
228
229         /* don't map root */
230         if (gid==0) return;
231
232         if (!list) {
233                 if (!(name = gid_to_name(gid))) return;
234                 gidlist = add_list((int)gid, name);
235                 return;
236         }
237
238         while (list->next) {
239                 if (list->id == (int)gid) return;
240                 list = list->next;
241         }
242
243         if (list->id == (int)gid) return;
244
245         if (!(name = gid_to_name(gid))) return;
246
247         list->next = add_list((int)gid, name);
248 }
249
250
251 /* send a complete uid/gid mapping to the peer */
252 void send_uid_list(int f)
253 {
254         struct idlist *list;
255
256         if (numeric_ids) return;
257
258         if (preserve_uid) {
259                 /* we send sequences of uid/byte-length/name */
260                 list = uidlist;
261                 while (list) {
262                         int len = strlen(list->name);
263                         write_int(f, list->id);
264                         write_byte(f, len);
265                         write_buf(f, list->name, len);
266                         list = list->next;
267                 }
268
269                 /* terminate the uid list with a 0 uid. We explicitly exclude
270                  * 0 from the list */
271                 write_int(f, 0);
272         }
273
274         if (preserve_gid) {
275                 list = gidlist;
276                 while (list) {
277                         int len = strlen(list->name);
278                         write_int(f, list->id);
279                         write_byte(f, len);
280                         write_buf(f, list->name, len);
281                         list = list->next;
282                 }
283                 write_int(f, 0);
284         }
285 }
286
287 /* recv a complete uid/gid mapping from the peer and map the uid/gid
288  * in the file list to local names */
289 void recv_uid_list(int f, struct file_list *flist)
290 {
291         int id, i;
292         char *name;
293         struct idlist *list;
294
295         if (numeric_ids) return;
296
297         if (preserve_uid) {
298                 /* read the uid list */
299                 list = uidlist;
300                 while ((id = read_int(f)) != 0) {
301                         int len = read_byte(f);
302                         name = new_array(char, len+1);
303                         if (!name) out_of_memory("recv_uid_list");
304                         read_sbuf(f, name, len);
305                         if (!list) {
306                                 uidlist = add_list(id, name);
307                                 list = uidlist;
308                         } else {
309                                 list->next = add_list(id, name);
310                                 list = list->next;
311                         }
312                         list->id2 = map_uid(id, name);
313                         free(name);
314                 }
315                 if (verbose > 3) {
316                         for (list = uidlist; list; list = list->next) {
317                                 rprintf(FINFO, "uid %ld (%s) maps to %ld\n",
318                                     (long)list->id, list->name,
319                                     (long)list->id2);
320                         }
321                 }
322         }
323
324
325         if (preserve_gid) {
326                 /* and the gid list */
327                 list = gidlist;
328                 while ((id = read_int(f)) != 0) {
329                         int len = read_byte(f);
330                         name = new_array(char, len+1);
331                         if (!name) out_of_memory("recv_uid_list");
332                         read_sbuf(f, name, len);
333                         if (!list) {
334                                 gidlist = add_list(id, name);
335                                 list = gidlist;
336                         } else {
337                                 list->next = add_list(id, name);
338                                 list = list->next;
339                         }
340                         list->id2 = map_gid(id, name);
341                         if (!am_root && !is_in_group(list->id2))
342                                 list->id2 = GID_NONE;
343                         free(name);
344                 }
345                 if (verbose > 3) {
346                         for (list = gidlist; list; list = list->next) {
347                                 rprintf(FINFO, "gid %ld (%s) maps to %ld\n",
348                                     (long)list->id, list->name,
349                                     (long)list->id2);
350                         }
351                 }
352         }
353
354         if (!(am_root && preserve_uid) && !preserve_gid) return;
355
356         /* now convert the uid/gid of all files in the list to the mapped
357          * uid/gid */
358         for (i = 0; i < flist->count; i++) {
359                 if (am_root && preserve_uid && flist->files[i]->uid != 0)
360                         flist->files[i]->uid = match_uid(flist->files[i]->uid);
361                 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
362                         flist->files[i]->gid = match_gid(flist->files[i]->gid);
363         }
364 }