Tweaked some whitespace to match the latest version from autoconf.
[rsync/rsync.git] / uidlist.c
CommitLineData
ade7292a 1/*
f6c34742
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
ade7292a 4
f6c34742
AT
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.
ade7292a 9
f6c34742
AT
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.
ade7292a 14
f6c34742
AT
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
d49def48 22 the numeric IDs. Never do any mapping for uid=0 or gid=0 as these
f6c34742
AT
23 are special.
24*/
25
26#include "rsync.h"
27
4f5b0756 28#ifdef HAVE_GETGROUPS
25ff0441 29# ifndef GETGROUPS_T
1df395f7
WD
30# define GETGROUPS_T gid_t
31# endif
72fc7ec5
WD
32#endif
33
84fa865c 34extern int verbose;
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
d49def48
WD
49static struct idlist *add_to_list(struct idlist **root, int id, char *name,
50 int id2)
f6c34742 51{
d49def48
WD
52 struct idlist *node = new(struct idlist);
53 if (!node)
54 out_of_memory("add_to_list");
55 node->next = *root;
56 node->name = name;
57 node->id = id;
58 node->id2 = id2;
59 *root = node;
60 return node;
f6c34742
AT
61}
62
f6c34742
AT
63/* turn a uid into a user name */
64static char *uid_to_name(uid_t uid)
65{
66 struct passwd *pass = getpwuid(uid);
d49def48
WD
67 if (pass)
68 return strdup(pass->pw_name);
f6c34742
AT
69 return NULL;
70}
71
72/* turn a gid into a group name */
73static char *gid_to_name(gid_t gid)
74{
75 struct group *grp = getgrgid(gid);
d49def48
WD
76 if (grp)
77 return strdup(grp->gr_name);
f6c34742
AT
78 return NULL;
79}
80
f6c34742
AT
81static int map_uid(int id, char *name)
82{
8ef4ffd6 83 uid_t uid;
b66d0085 84 if (id != 0 && name_to_uid(name, &uid))
8ef4ffd6 85 return uid;
f6c34742
AT
86 return id;
87}
88
89static int map_gid(int id, char *name)
90{
8ef4ffd6 91 gid_t gid;
b66d0085 92 if (id != 0 && name_to_gid(name, &gid))
8ef4ffd6 93 return gid;
f6c34742
AT
94 return id;
95}
96
5b540e86
WD
97static int is_in_group(gid_t gid)
98{
4f5b0756 99#ifdef HAVE_GETGROUPS
a2687b64 100 static gid_t last_in = GID_NONE, last_out;
5b540e86
WD
101 static int ngroups = -2;
102 static GETGROUPS_T *gidset;
103 int n;
104
105 if (gid == last_in)
106 return last_out;
107 if (ngroups < -1) {
670d8abf 108 gid_t mygid = MY_GID();
f567e9b3 109 if ((ngroups = getgroups(0, NULL)) < 0)
dbd8811b 110 ngroups = 0;
72fc7ec5 111 gidset = new_array(GETGROUPS_T, ngroups+1);
f567e9b3
WD
112 if (!gidset)
113 out_of_memory("is_in_group");
72fc7ec5 114 if (ngroups > 0)
5b540e86 115 ngroups = getgroups(ngroups, gidset);
72fc7ec5
WD
116 /* The default gid might not be in the list on some systems. */
117 for (n = 0; n < ngroups; n++) {
118 if (gidset[n] == mygid)
119 break;
5b540e86 120 }
72fc7ec5
WD
121 if (n == ngroups)
122 gidset[ngroups++] = mygid;
84fa865c 123 if (verbose > 3) {
187e9c24 124 int pos;
f567e9b3
WD
125 char *gidbuf = new_array(char, ngroups*21+32);
126 if (!gidbuf)
127 out_of_memory("is_in_group");
187e9c24
WD
128 sprintf(gidbuf, "process has %d gid%s: ",
129 ngroups, ngroups == 1? "" : "s");
130 pos = strlen(gidbuf);
84fa865c 131 for (n = 0; n < ngroups; n++) {
d49def48 132 sprintf(gidbuf+pos, " %d", (int)gidset[n]);
187e9c24 133 pos += strlen(gidbuf+pos);
84fa865c 134 }
187e9c24 135 rprintf(FINFO, "%s\n", gidbuf);
f567e9b3 136 free(gidbuf);
84fa865c 137 }
5b540e86
WD
138 }
139
140 last_in = gid;
5b540e86 141 for (n = 0; n < ngroups; n++) {
a2687b64
WD
142 if (gidset[n] == gid)
143 return last_out = 1;
5b540e86 144 }
a2687b64 145 return last_out = 0;
5b540e86
WD
146
147#else
a2687b64 148 static gid_t mygid = GID_NONE;
187e9c24 149 if (mygid == GID_NONE) {
670d8abf 150 mygid = MY_GID();
187e9c24 151 if (verbose > 3)
d49def48 152 rprintf(FINFO, "process has gid %d\n", (int)mygid);
187e9c24 153 }
a2687b64 154 return gid == mygid;
5b540e86
WD
155#endif
156}
157
d49def48
WD
158/* Add a uid to the list of uids. Only called on receiving side. */
159static struct idlist *recv_add_uid(int id, char *name)
160{
161 int id2 = name ? map_uid(id, name) : id;
162 struct idlist *node;
163
b66d0085 164 node = add_to_list(&uidlist, id, name, id2);
d49def48
WD
165
166 if (verbose > 3) {
167 rprintf(FINFO, "uid %d(%s) maps to %d\n",
168 id, name ? name : "", id2);
169 }
170
171 return node;
172}
173
174/* Add a gid to the list of gids. Only called on receiving side. */
175static struct idlist *recv_add_gid(int id, char *name)
176{
177 int id2 = name ? map_gid(id, name) : id;
178 struct idlist *node;
179
180 if (!am_root && !is_in_group(id2))
181 id2 = GID_NONE;
182 node = add_to_list(&gidlist, id, name, id2);
183
184 if (verbose > 3) {
185 rprintf(FINFO, "gid %d(%s) maps to %d\n",
186 id, name ? name : "", id2);
187 }
188
189 return node;
190}
191
ade7292a
WD
192/* this function is a definate candidate for a faster algorithm */
193static uid_t match_uid(uid_t uid)
194{
195 static uid_t last_in, last_out;
d49def48
WD
196 struct idlist *list;
197
198 if (uid == 0)
199 return 0;
ade7292a
WD
200
201 if (uid == last_in)
202 return last_out;
203
204 last_in = uid;
205
d49def48
WD
206 for (list = uidlist; list; list = list->next) {
207 if (list->id == (int)uid)
208 return last_out = (uid_t)list->id2;
ade7292a
WD
209 }
210
d49def48 211 return last_out = uid;
ade7292a
WD
212}
213
f6c34742
AT
214static gid_t match_gid(gid_t gid)
215{
a2687b64 216 static gid_t last_in = GID_NONE, last_out = GID_NONE;
d49def48
WD
217 struct idlist *list;
218
01363a24
WD
219 if (gid == GID_NONE)
220 return GID_NONE;
f6c34742 221
a2687b64
WD
222 if (gid == last_in)
223 return last_out;
f6c34742
AT
224
225 last_in = gid;
226
d49def48
WD
227 for (list = gidlist; list; list = list->next) {
228 if (list->id == (int)gid)
229 return last_out = (gid_t)list->id2;
f6c34742 230 }
d49def48
WD
231
232 list = recv_add_gid(gid, NULL);
233 return last_out = list->id2;
f6c34742
AT
234}
235
d49def48 236/* Add a uid to the list of uids. Only called on sending side. */
f6c34742
AT
237void add_uid(uid_t uid)
238{
d49def48 239 struct idlist *list;
f6c34742 240
d49def48 241 if (uid == 0) /* don't map root */
f6c34742 242 return;
f6c34742 243
d49def48
WD
244 for (list = uidlist; list; list = list->next) {
245 if (list->id == (int)uid)
246 return;
f6c34742
AT
247 }
248
d49def48 249 add_to_list(&uidlist, (int)uid, uid_to_name(uid), 0);
f6c34742
AT
250}
251
d49def48 252/* Add a gid to the list of gids. Only called on sending side. */
f6c34742
AT
253void add_gid(gid_t gid)
254{
d49def48 255 struct idlist *list;
f6c34742 256
d49def48 257 if (gid == 0) /* don't map root */
f6c34742 258 return;
f6c34742 259
d49def48
WD
260 for (list = gidlist; list; list = list->next) {
261 if (list->id == (int)gid)
262 return;
f6c34742
AT
263 }
264
d49def48 265 add_to_list(&gidlist, (int)gid, gid_to_name(gid), 0);
f6c34742
AT
266}
267
268
269/* send a complete uid/gid mapping to the peer */
270void send_uid_list(int f)
271{
272 struct idlist *list;
273
d49def48
WD
274 if (numeric_ids)
275 return;
f6c34742
AT
276
277 if (preserve_uid) {
d49def48 278 int len;
f6c34742 279 /* we send sequences of uid/byte-length/name */
d49def48
WD
280 for (list = uidlist; list; list = list->next) {
281 if (!list->name)
282 continue;
283 len = strlen(list->name);
f6c34742
AT
284 write_int(f, list->id);
285 write_byte(f, len);
286 write_buf(f, list->name, len);
f6c34742
AT
287 }
288
289 /* terminate the uid list with a 0 uid. We explicitly exclude
84fa865c 290 * 0 from the list */
f6c34742
AT
291 write_int(f, 0);
292 }
293
294 if (preserve_gid) {
d49def48
WD
295 int len;
296 for (list = gidlist; list; list = list->next) {
297 if (!list->name)
298 continue;
299 len = strlen(list->name);
f6c34742
AT
300 write_int(f, list->id);
301 write_byte(f, len);
302 write_buf(f, list->name, len);
f6c34742
AT
303 }
304 write_int(f, 0);
305 }
306}
307
308/* recv a complete uid/gid mapping from the peer and map the uid/gid
84fa865c 309 * in the file list to local names */
7b6fa00f 310void recv_uid_list(int f, struct file_list *flist)
f6c34742
AT
311{
312 int id, i;
313 char *name;
f6c34742 314
d49def48 315 if (preserve_uid && !numeric_ids) {
f6c34742 316 /* read the uid list */
5b540e86 317 while ((id = read_int(f)) != 0) {
f6c34742 318 int len = read_byte(f);
58cadc86 319 name = new_array(char, len+1);
d49def48
WD
320 if (!name)
321 out_of_memory("recv_uid_list");
575f2fca 322 read_sbuf(f, name, len);
d49def48 323 recv_add_uid(id, name); /* node keeps name's memory */
84fa865c 324 }
f6c34742
AT
325 }
326
d49def48
WD
327 if (preserve_gid && !numeric_ids) {
328 /* read the gid list */
5b540e86 329 while ((id = read_int(f)) != 0) {
f6c34742 330 int len = read_byte(f);
58cadc86 331 name = new_array(char, len+1);
d49def48
WD
332 if (!name)
333 out_of_memory("recv_uid_list");
575f2fca 334 read_sbuf(f, name, len);
d49def48 335 recv_add_gid(id, name); /* node keeps name's memory */
84fa865c 336 }
f6c34742
AT
337 }
338
7b6fa00f 339 /* Now convert all the uids/gids from sender values to our values. */
d49def48 340 if (am_root && preserve_uid && !numeric_ids) {
7b6fa00f
WD
341 for (i = 0; i < flist->count; i++)
342 flist->files[i]->uid = match_uid(flist->files[i]->uid);
d49def48
WD
343 }
344 if (preserve_gid && (!am_root || !numeric_ids)) {
7b6fa00f
WD
345 for (i = 0; i < flist->count; i++)
346 flist->files[i]->gid = match_gid(flist->files[i]->gid);
5e58e3f9 347 }
f6c34742 348}