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