A few more tests.
[rsync.git] / uidlist.c
blob14611a55ff3e6a72027787dddbaf4458843fbb44
1 /*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
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.
26 #include "rsync.h"
28 extern int preserve_uid;
29 extern int preserve_gid;
30 extern int numeric_ids;
31 extern int am_root;
33 struct idlist {
34 struct idlist *next;
35 int id, id2;
36 char *name;
39 static struct idlist *uidlist;
40 static struct idlist *gidlist;
42 static struct idlist *add_list(int id, char *name)
44 struct idlist *list = (struct idlist *)malloc(sizeof(list[0]));
45 if (!list) out_of_memory("add_list");
46 list->next = NULL;
47 list->name = strdup(name);
48 if (!list->name) out_of_memory("add_list");
49 list->id = (int)id;
50 return list;
55 /* turn a uid into a user name */
56 static char *uid_to_name(uid_t uid)
58 struct passwd *pass = getpwuid(uid);
59 if (pass) return(pass->pw_name);
60 return NULL;
63 /* turn a gid into a group name */
64 static char *gid_to_name(gid_t gid)
66 struct group *grp = getgrgid(gid);
67 if (grp) return(grp->gr_name);
68 return NULL;
71 static int map_uid(int id, char *name)
73 uid_t uid;
74 if (name_to_uid(name, &uid) && uid != 0)
75 return uid;
76 return id;
79 static int map_gid(int id, char *name)
81 gid_t gid;
82 if (name_to_gid(name, &gid) && gid != 0)
83 return gid;
84 return id;
87 /* this function is a definate candidate for a faster algorithm */
88 static uid_t match_uid(uid_t uid)
90 static uid_t last_in, last_out;
91 struct idlist *list = uidlist;
93 if (uid == last_in) return last_out;
95 last_in = uid;
97 while (list) {
98 if (list->id == (int)uid) {
99 last_out = (uid_t)list->id2;
100 return last_out;
102 list = list->next;
105 last_out = uid;
106 return last_out;
109 static gid_t match_gid(gid_t gid)
111 static gid_t last_in, last_out;
112 struct idlist *list = gidlist;
114 if (gid == last_in) return last_out;
116 last_in = gid;
118 while (list) {
119 if (list->id == (int)gid) {
120 last_out = (gid_t)list->id2;
121 return last_out;
123 list = list->next;
126 if (am_root)
127 last_out = gid;
128 else
129 last_out = (gid_t) -1;
130 return last_out;
133 /* add a uid to the list of uids */
134 void add_uid(uid_t uid)
136 struct idlist *list = uidlist;
137 char *name;
139 if (numeric_ids) return;
141 /* don't map root */
142 if (uid==0) return;
144 if (!list) {
145 if (!(name = uid_to_name(uid))) return;
146 uidlist = add_list((int)uid, name);
147 return;
150 while (list->next) {
151 if (list->id == (int)uid) return;
152 list = list->next;
155 if (list->id == (int)uid) return;
157 if (!(name = uid_to_name(uid))) return;
159 list->next = add_list((int)uid, name);
162 /* add a gid to the list of gids */
163 void add_gid(gid_t gid)
165 struct idlist *list = gidlist;
166 char *name;
168 if (numeric_ids) return;
170 /* don't map root */
171 if (gid==0) return;
173 if (!list) {
174 if (!(name = gid_to_name(gid))) return;
175 gidlist = add_list((int)gid, name);
176 return;
179 while (list->next) {
180 if (list->id == (int)gid) return;
181 list = list->next;
184 if (list->id == (int)gid) return;
186 if (!(name = gid_to_name(gid))) return;
188 list->next = add_list((int)gid, name);
192 /* send a complete uid/gid mapping to the peer */
193 void send_uid_list(int f)
195 struct idlist *list;
197 if (numeric_ids) return;
199 if (preserve_uid) {
200 /* we send sequences of uid/byte-length/name */
201 list = uidlist;
202 while (list) {
203 int len = strlen(list->name);
204 write_int(f, list->id);
205 write_byte(f, len);
206 write_buf(f, list->name, len);
207 list = list->next;
210 /* terminate the uid list with a 0 uid. We explicitly exclude
211 0 from the list */
212 write_int(f, 0);
215 if (preserve_gid) {
216 list = gidlist;
217 while (list) {
218 int len = strlen(list->name);
219 write_int(f, list->id);
220 write_byte(f, len);
221 write_buf(f, list->name, len);
222 list = list->next;
224 write_int(f, 0);
228 /* recv a complete uid/gid mapping from the peer and map the uid/gid
229 in the file list to local names */
230 void recv_uid_list(int f, struct file_list *flist)
232 int id, i;
233 char *name;
234 struct idlist *list;
236 if (numeric_ids) return;
238 if (preserve_uid) {
239 /* read the uid list */
240 list = uidlist;
241 id = read_int(f);
242 while (id != 0) {
243 int len = read_byte(f);
244 name = (char *)malloc(len+1);
245 if (!name) out_of_memory("recv_uid_list");
246 read_sbuf(f, name, len);
247 if (!list) {
248 uidlist = add_list(id, name);
249 list = uidlist;
250 } else {
251 list->next = add_list(id, name);
252 list = list->next;
254 list->id2 = map_uid(id, name);
255 free(name);
256 id = read_int(f);
261 if (preserve_gid) {
262 /* and the gid list */
263 list = gidlist;
264 id = read_int(f);
265 while (id != 0) {
266 int len = read_byte(f);
267 name = (char *)malloc(len+1);
268 if (!name) out_of_memory("recv_uid_list");
269 read_sbuf(f, name, len);
270 if (!list) {
271 gidlist = add_list(id, name);
272 list = gidlist;
273 } else {
274 list->next = add_list(id, name);
275 list = list->next;
277 list->id2 = map_gid(id, name);
278 free(name);
279 id = read_int(f);
283 if (!(am_root && preserve_uid) && !preserve_gid) return;
285 /* now convert the uid/gid of all files in the list to the mapped
286 uid/gid */
287 for (i=0;i<flist->count;i++) {
288 if (am_root && preserve_uid && flist->files[i]->uid != 0) {
289 flist->files[i]->uid = match_uid(flist->files[i]->uid);
291 if (preserve_gid && flist->files[i]->gid != 0) {
292 flist->files[i]->gid = match_gid(flist->files[i]->gid);