Tweaked the doc & opening description.
[rsync/rsync-patches.git] / usermap.diff
CommitLineData
409bd73e
WD
1This adds a --usermap and a --groupmap option. See the man page for
2more details.
4122278d 3
409bd73e
WD
4To use this patch, run these commands for a successful build:
5
6 patch -p1 <patches/usermap.diff
7 ./configure (optional if already run)
8 make
4122278d
WD
9
10--- old/flist.c
11+++ new/flist.c
12@@ -61,6 +61,8 @@ extern int copy_links;
13 extern int copy_unsafe_links;
14 extern int protocol_version;
15 extern int sanitize_paths;
16+extern char *usermap;
17+extern char *groupmap;
18 extern struct stats stats;
19
20 extern char curr_dir[MAXPATHLEN];
409bd73e
WD
21@@ -741,7 +743,7 @@ static struct file_struct *recv_file_ent
22 uid = (uid_t)read_varint(f);
23 if (flags & XMIT_USER_NAME_FOLLOWS)
24 uid = recv_user_name(f, uid);
25- else if (inc_recurse && am_root && !numeric_ids)
f62e6e48 26+ else if (inc_recurse && am_root && (!numeric_ids || usermap))
409bd73e
WD
27 uid = match_uid(uid);
28 }
29 }
30@@ -752,7 +754,7 @@ static struct file_struct *recv_file_ent
31 gid = (gid_t)read_varint(f);
32 if (flags & XMIT_GROUP_NAME_FOLLOWS)
33 gid = recv_group_name(f, gid);
34- else if (inc_recurse && (!am_root || !numeric_ids))
f62e6e48 35+ else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
409bd73e
WD
36 gid = match_gid(gid);
37 }
38 }
4122278d
WD
39@@ -1882,8 +1884,13 @@ struct file_list *recv_file_list(int f)
40 int dstart, flags;
41 int64 start_read;
42
43- if (!first_flist)
44+ if (!first_flist) {
45 rprintf(FLOG, "receiving file list\n");
46+ if (usermap)
47+ parse_name_map(usermap, 1);
48+ if (groupmap)
49+ parse_name_map(groupmap, 0);
50+ }
51 if (show_filelist_p())
52 start_filelist_progress("receiving file list");
53 else if (inc_recurse && verbose && !am_server && !first_flist)
54--- old/options.c
55+++ new/options.c
56@@ -156,6 +156,8 @@ char *rsync_path = RSYNC_PATH;
57 char *backup_dir = NULL;
58 char backup_dir_buf[MAXPATHLEN];
59 char *sockopts = NULL;
60+char *usermap = NULL;
61+char *groupmap = NULL;
62 int rsync_port = 0;
63 int compare_dest = 0;
64 int copy_dest = 0;
65@@ -367,6 +369,8 @@ void usage(enum logcode F)
66 rprintf(F," --delay-updates put all updated files into place at transfer's end\n");
67 rprintf(F," -m, --prune-empty-dirs prune empty directory chains from the file-list\n");
68 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
69+ rprintf(F," --usermap=STRING custom username mapping\n");
70+ rprintf(F," --groupmap=STRING custom groupname mapping\n");
71 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
72 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
73 rprintf(F," --size-only skip files that match in size\n");
74@@ -568,6 +572,8 @@ static struct poptOption long_options[]
75 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
76 {"from0", '0', POPT_ARG_NONE, &eol_nulls, 0, 0, 0},
77 {"numeric-ids", 0, POPT_ARG_NONE, &numeric_ids, 0, 0, 0 },
78+ {"usermap", 0, POPT_ARG_STRING, &usermap, 0, 0, 0 },
79+ {"groupmap", 0, POPT_ARG_STRING, &groupmap, 0, 0, 0 },
80 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
81 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
82 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
f62e6e48 83@@ -1857,6 +1863,18 @@ void server_options(char **args,int *arg
4122278d
WD
84 args[ac++] = "--numeric-ids";
85
86 if (am_sender) {
87+ if (usermap) {
f62e6e48 88+ if (asprintf(&arg, "--usermap=%s", usermap) < 0)
4122278d
WD
89+ goto oom;
90+ args[ac++] = arg;
91+ }
92+
93+ if (groupmap) {
f62e6e48 94+ if (asprintf(&arg, "--groupmap=%s", groupmap) < 0)
4122278d
WD
95+ goto oom;
96+ args[ac++] = arg;
97+ }
98+
99 if (ignore_existing)
100 args[ac++] = "--ignore-existing";
101
102--- old/rsync.yo
103+++ new/rsync.yo
104@@ -361,6 +361,8 @@ to the detailed description below for a
105 --delay-updates put all updated files into place at end
106 -m, --prune-empty-dirs prune empty directory chains from file-list
107 --numeric-ids don't map uid/gid values by user/group name
108+ --usermap=STRING custom username mapping
109+ --groupmap=STRING custom groupname mapping
110 --timeout=TIME set I/O timeout in seconds
111 -I, --ignore-times don't skip files that match size and time
112 --size-only skip files that match in size
409bd73e 113@@ -1445,6 +1447,42 @@ from the source system is used instead.
4122278d
WD
114 the chroot setting affects rsync's ability to look up the names of the
115 users and groups and what you can do about it.
116
117+dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
409bd73e
WD
118+specify users and groups that should be mapped to other values by the
119+receiving side. The bf(STRING) is one or more bf(FROM):bf(TO) pairs of
120+values separated by commas. Any matching bf(FROM) value from the sender is
121+replaced with a bf(TO) value from the receiver. You may specify usernames
122+or user IDs for the bf(FROM) and bf(TO) values, and the bf(FROM) value may
123+also be a wild-card string, which will be matched against the sender's
f62e6e48 124+names (wild-cards do NOT match against ID numbers). For example:
409bd73e 125+
f62e6e48 126+verb( --usermap=0:bin,wayne:admin,*:nobody --groupmap=usr:1,1:usr)
409bd73e
WD
127+
128+The first match in the list is the one that is used. You should not use
129+multiple options of the same type, but instead include all the user
130+mappings you need separated by commas to a single bf(--usermap) option,
131+and likewise for groups with the bf(--groupmap) option.
4122278d 132+
409bd73e
WD
133+Note that the sender's name for the 0 uid/gid is not actually transmitted
134+to the receiver, so you should either match these values using a 0, or use
135+the names in effect on the receiving side. All other bf(FROM) names match
136+those in use on sending side. All bf(TO) names match those in use on the
137+receiving side.
4122278d 138+
409bd73e
WD
139+If the bf(--numeric-ids) option is used, you must specify numeric bf(FROM)
140+values for them to be effective, since the sender does not send any names
141+when this option is used, and wild-card rules don't match nameless ID
142+values. The only exceptions to this are (1) the names for the 0 uid/gid,
143+which are always supplied by the receiving side, and (2) a "*", which
144+matches even an empty name.
4122278d 145+
409bd73e
WD
146+For the bf(--usermap) option to have any effect, the bf(-o) (bf(--owner))
147+option must be used (or implied), and the receiver will need to be running
f62e6e48 148+as root (see also the bf(--fake-super) option). For the bf(--groupmap)
409bd73e
WD
149+option to have any effect, the bf(-g) (bf(--groups)) option must be used
150+(or implied), and the receiver will need to have permissions to set that
151+group.
4122278d
WD
152+
153 dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O
154 timeout in seconds. If no data is transferred for the specified time
155 then rsync will exit. The default is 0, which means no timeout.
156--- old/uidlist.c
157+++ new/uidlist.c
409bd73e 158@@ -38,6 +38,8 @@ extern int preserve_uid;
4122278d
WD
159 extern int preserve_gid;
160 extern int preserve_acls;
161 extern int numeric_ids;
409bd73e
WD
162+extern char *usermap;
163+extern char *groupmap;
4122278d
WD
164
165 struct idlist {
166 struct idlist *next;
409bd73e 167@@ -45,8 +47,8 @@ struct idlist {
4122278d
WD
168 char *name;
169 };
170
171-static struct idlist *uidlist;
172-static struct idlist *gidlist;
173+static struct idlist *uidlist, *uidmap;
174+static struct idlist *gidlist, *gidmap;
175
176 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
177 int id2)
409bd73e 178@@ -158,14 +160,41 @@ static int is_in_group(gid_t gid)
4122278d
WD
179 /* Add a uid to the list of uids. Only called on receiving side. */
180 static uid_t recv_add_uid(uid_t id, char *name)
181 {
182- uid_t id2 = name ? map_uid(id, name) : id;
183 struct idlist *node;
184+ uid_t id2;
409bd73e
WD
185
186- node = add_to_list(&uidlist, (int)id, name, (int)id2);
187+ if (!name)
188+ name = "";
4122278d 189+
409bd73e
WD
190+ for (node = uidmap; node; node = node->next) {
191+ switch (node->id) {
192+ case -2:
193+ if (!wildmatch(node->name, name))
194+ continue;
195+ break;
196+ case -1:
197+ if (strcmp(node->name, name) != 0)
198+ continue;
199+ break;
200+ default:
201+ if (node->id != (int)id)
202+ continue;
4122278d
WD
203+ break;
204+ }
409bd73e
WD
205+ break;
206+ }
207+ if (node)
208+ id2 = node->id2;
209+ else if (*name && id)
210+ id2 = map_uid(id, name);
211+ else
212+ id2 = id;
213+
214+ node = add_to_list(&uidlist, (int)id, *name ? name : NULL, (int)id2);
4122278d 215
409bd73e
WD
216 if (verbose > 3) {
217 rprintf(FINFO, "uid %d(%s) maps to %d\n",
218- (int)id, name ? name : "", (int)id2);
219+ (int)id, name, (int)id2);
220 }
4122278d 221
409bd73e
WD
222 return id2;
223@@ -174,16 +203,43 @@ static uid_t recv_add_uid(uid_t id, char
4122278d
WD
224 /* Add a gid to the list of gids. Only called on receiving side. */
225 static gid_t recv_add_gid(gid_t id, char *name)
226 {
227- gid_t id2 = name ? map_gid(id, name) : id;
228 struct idlist *node;
229+ gid_t id2;
230+
409bd73e
WD
231+ if (!name)
232+ name = "";
233+
234+ for (node = gidmap; node; node = node->next) {
235+ switch (node->id) {
236+ case -2:
237+ if (!wildmatch(node->name, name))
238+ continue;
239+ break;
240+ case -1:
241+ if (strcmp(node->name, name) != 0)
242+ continue;
243+ break;
244+ default:
245+ if (node->id != (int)id)
246+ continue;
4122278d
WD
247+ break;
248+ }
409bd73e
WD
249+ break;
250+ }
251+ if (node)
252+ id2 = node->id2;
253+ else if (*name && id)
254+ id2 = map_gid(id, name);
255+ else
256+ id2 = id;
4122278d
WD
257
258 if (!am_root && !is_in_group(id2))
259 id2 = GID_NONE;
409bd73e
WD
260- node = add_to_list(&gidlist, (int)id, name, (int)id2);
261+ node = add_to_list(&gidlist, (int)id, name ? name : NULL, (int)id2);
262
263 if (verbose > 3) {
264 rprintf(FINFO, "gid %d(%s) maps to %d\n",
265- (int)id, name ? name : "", (int)id2);
266+ (int)id, name, (int)id2);
267 }
268
269 return id2;
270@@ -192,12 +248,9 @@ static gid_t recv_add_gid(gid_t id, char
271 /* this function is a definate candidate for a faster algorithm */
272 uid_t match_uid(uid_t uid)
273 {
274- static uid_t last_in, last_out;
275+ static uid_t last_in = -1, last_out = -1;
4122278d
WD
276 struct idlist *list;
277
278- if (uid == 0)
279- return 0;
280-
281 if (uid == last_in)
282 return last_out;
283
409bd73e
WD
284@@ -208,7 +261,7 @@ uid_t match_uid(uid_t uid)
285 return last_out = (uid_t)list->id2;
286 }
4122278d 287
409bd73e
WD
288- return last_out = uid;
289+ return last_out = recv_add_uid(uid, NULL);
290 }
4122278d 291
409bd73e
WD
292 gid_t match_gid(gid_t gid)
293@@ -344,15 +397,95 @@ void recv_uid_list(int f, struct file_li
4122278d 294
409bd73e
WD
295 /* Now convert all the uids/gids from sender values to our values. */
296 #ifdef SUPPORT_ACLS
297- if (preserve_acls && !numeric_ids)
298+ if (preserve_acls && (!numeric_ids || usermap))
299 match_acl_ids();
300 #endif
301- if (am_root && preserve_uid && !numeric_ids) {
302+ if (am_root && preserve_uid && (!numeric_ids || usermap)) {
303 for (i = 0; i < flist->count; i++)
304 F_OWNER(flist->files[i]) = match_uid(F_UID(flist->files[i]));
305 }
306- if (preserve_gid && (!am_root || !numeric_ids)) {
307+ if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
308 for (i = 0; i < flist->count; i++)
4122278d
WD
309 F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i]));
310 }
311 }
312+
313+void parse_name_map(char *map, int usernames)
314+{
315+ char *colon, *end, *cp = map + strlen(map);
316+ int id1, id2;
317+
318+ while (1) {
319+ end = cp;
320+ while (cp > map && cp[-1] != ',') cp--;
321+ if (!(colon = strchr(cp, ':'))) {
322+ rprintf(FERROR, "No colon found in --%smap: %s\n",
323+ usernames ? "user" : "group", cp);
324+ exit_cleanup(RERR_SYNTAX);
325+ }
326+ *colon = '\0';
327+
328+ if (isDigit(cp)) {
329+ if (strspn(cp, "0123456789") != (size_t)(colon - cp)) {
330+ bad_number:
331+ rprintf(FERROR, "Invalid number in --%smap: %s\n",
332+ usernames ? "user" : "group", cp);
333+ exit_cleanup(RERR_SYNTAX);
334+ }
335+ id1 = atoi(cp);
336+ } else if (strpbrk(cp, "*[?"))
337+ id1 = -2;
338+ else
339+ id1 = -1;
340+
341+ if (isDigit(colon+1)) {
342+ if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
343+ cp = colon+1;
344+ goto bad_number;
345+ }
346+ id2 = atoi(colon+1);
347+ } else {
348+ if (usernames) {
349+ uid_t uid;
350+ if (name_to_uid(colon+1, &uid))
351+ id2 = (int)uid;
352+ else
353+ id2 = -1;
354+ } else {
355+ gid_t gid;
356+ if (name_to_gid(colon+1, &gid))
357+ id2 = (int)gid;
358+ else
359+ id2 = -1;
360+ }
361+ if (id2 < 0) {
362+ rprintf(FERROR, "Invalid name in --%smap: %s\n",
363+ usernames ? "user" : "group", colon+1);
364+ exit_cleanup(RERR_SYNTAX);
365+ }
366+ }
367+
409bd73e 368+ if (usernames) {
4122278d 369+ add_to_list(&uidmap, id1, id1 < 0 ? cp : NULL, id2);
409bd73e
WD
370+ if (numeric_ids && id2 >= 0)
371+ add_to_list(&uidlist, id1, NULL, id2);
372+ } else {
4122278d 373+ add_to_list(&gidmap, id1, id1 < 0 ? cp : NULL, id2);
409bd73e
WD
374+ if (numeric_ids && id2 >= 0)
375+ add_to_list(&gidlist, id1, NULL, id2);
376+ }
4122278d
WD
377+
378+ if (cp == map)
379+ break;
380+
381+ *--cp = '\0'; /* replace comma */
382+ }
409bd73e
WD
383+
384+ if (usernames) {
385+ char *name = uid_to_name(0);
386+ recv_add_uid(0, name ? name : "root");
387+ } else {
388+ char *name = gid_to_name(0);
389+ recv_add_gid(0, name ? name : "root");
390+ }
4122278d 391+}