Set the "max verbosity" global to 9.
[rsync/rsync.git] / uidlist.c
CommitLineData
f6c34742
AT
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
72fc7ec5
WD
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
84fa865c 35extern int verbose;
f6c34742
AT
36extern int preserve_uid;
37extern int preserve_gid;
38extern int numeric_ids;
460f6b99 39extern int am_root;
f6c34742
AT
40
41struct idlist {
42 struct idlist *next;
43 int id, id2;
44 char *name;
45};
46
47static struct idlist *uidlist;
48static struct idlist *gidlist;
49
50static struct idlist *add_list(int id, char *name)
51{
58cadc86 52 struct idlist *list = new(struct idlist);
f6c34742
AT
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 */
64static 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 */
72static 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
f6c34742
AT
79static int map_uid(int id, char *name)
80{
8ef4ffd6 81 uid_t uid;
0be976ec 82 if (uid != 0 && name_to_uid(name, &uid))
8ef4ffd6 83 return uid;
f6c34742
AT
84 return id;
85}
86
87static int map_gid(int id, char *name)
88{
8ef4ffd6 89 gid_t gid;
0be976ec 90 if (gid != 0 && name_to_gid(name, &gid))
8ef4ffd6 91 return gid;
f6c34742
AT
92 return id;
93}
94
95/* this function is a definate candidate for a faster algorithm */
96static uid_t match_uid(uid_t uid)
97{
98 static uid_t last_in, last_out;
99 struct idlist *list = uidlist;
100
a2687b64
WD
101 if (uid == last_in)
102 return last_out;
f6c34742
AT
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
5b540e86
WD
118static int is_in_group(gid_t gid)
119{
120#ifdef GETGROUPS_T
a2687b64 121 static gid_t last_in = GID_NONE, last_out;
5b540e86
WD
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) {
72fc7ec5 129 gid_t mygid = getgid();
5b540e86 130 ngroups = getgroups(0, 0);
72fc7ec5 131 /* If that didn't work, perhaps 0 isn't treated specially? */
a2687b64 132 if (ngroups <= 0)
72fc7ec5
WD
133 ngroups = NGROUPS_MAX;
134 gidset = new_array(GETGROUPS_T, ngroups+1);
135 if (ngroups > 0)
5b540e86 136 ngroups = getgroups(ngroups, gidset);
72fc7ec5
WD
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;
5b540e86 141 }
72fc7ec5
WD
142 if (n == ngroups)
143 gidset[ngroups++] = mygid;
84fa865c
WD
144 if (verbose > 3) {
145 for (n = 0; n < ngroups; n++) {
555b0e20
WD
146 rprintf(FINFO, "process has gid %ld\n",
147 (long)gidset[n]);
84fa865c
WD
148 }
149 }
5b540e86
WD
150 }
151
152 last_in = gid;
5b540e86 153 for (n = 0; n < ngroups; n++) {
a2687b64
WD
154 if (gidset[n] == gid)
155 return last_out = 1;
5b540e86 156 }
a2687b64 157 return last_out = 0;
5b540e86
WD
158
159#else
a2687b64
WD
160 static gid_t mygid = GID_NONE;
161 if (mygid == GID_NONE)
162 mygid = getgid();
163 return gid == mygid;
5b540e86
WD
164#endif
165}
166
f6c34742
AT
167static gid_t match_gid(gid_t gid)
168{
a2687b64 169 static gid_t last_in = GID_NONE, last_out = GID_NONE;
f6c34742
AT
170 struct idlist *list = gidlist;
171
a2687b64
WD
172 if (gid == last_in)
173 return last_out;
f6c34742
AT
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
460f6b99
DD
185 if (am_root)
186 last_out = gid;
187 else
a60e2dca 188 last_out = GID_NONE;
f6c34742
AT
189 return last_out;
190}
191
192/* add a uid to the list of uids */
193void 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 */
222void 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 */
252void 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
84fa865c 270 * 0 from the list */
f6c34742
AT
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
84fa865c 288 * in the file list to local names */
f6c34742
AT
289void 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;
5b540e86 300 while ((id = read_int(f)) != 0) {
f6c34742 301 int len = read_byte(f);
58cadc86 302 name = new_array(char, len+1);
f6c34742 303 if (!name) out_of_memory("recv_uid_list");
575f2fca 304 read_sbuf(f, name, len);
f6c34742
AT
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);
f6c34742 314 }
84fa865c
WD
315 if (verbose > 3) {
316 for (list = uidlist; list; list = list->next) {
555b0e20
WD
317 rprintf(FINFO, "uid %ld (%s) maps to %ld\n",
318 (long)list->id, list->name,
84fa865c
WD
319 (long)list->id2);
320 }
321 }
f6c34742
AT
322 }
323
324
325 if (preserve_gid) {
326 /* and the gid list */
327 list = gidlist;
5b540e86 328 while ((id = read_int(f)) != 0) {
f6c34742 329 int len = read_byte(f);
58cadc86 330 name = new_array(char, len+1);
f6c34742 331 if (!name) out_of_memory("recv_uid_list");
575f2fca 332 read_sbuf(f, name, len);
f6c34742
AT
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);
5b540e86 341 if (!am_root && !is_in_group(list->id2))
a60e2dca 342 list->id2 = GID_NONE;
f6c34742 343 free(name);
f6c34742 344 }
84fa865c
WD
345 if (verbose > 3) {
346 for (list = gidlist; list; list = list->next) {
555b0e20
WD
347 rprintf(FINFO, "gid %ld (%s) maps to %ld\n",
348 (long)list->id, list->name,
84fa865c
WD
349 (long)list->id2);
350 }
351 }
f6c34742
AT
352 }
353
460f6b99 354 if (!(am_root && preserve_uid) && !preserve_gid) return;
f6c34742
AT
355
356 /* now convert the uid/gid of all files in the list to the mapped
84fa865c 357 * uid/gid */
0be976ec
WD
358 for (i = 0; i < flist->count; i++) {
359 if (am_root && preserve_uid && flist->files[i]->uid != 0)
3ec4dd97 360 flist->files[i]->uid = match_uid(flist->files[i]->uid);
0be976ec 361 if (preserve_gid && (!am_root || flist->files[i]->gid != 0))
3ec4dd97 362 flist->files[i]->gid = match_gid(flist->files[i]->gid);
f6c34742
AT
363 }
364}