1 This adds a --usermap and a --groupmap option. See the man page for
4 To use this patch, run these commands for a successful build:
6 patch -p1 <patches/usermap.diff
7 ./configure (optional if already run)
12 @@ -67,6 +67,7 @@ extern int need_unsorted_flist;
13 extern int unsort_ndx;
14 extern struct stats stats;
15 extern char *filesfrom_host;
16 +extern char *usermap, *groupmap;
18 extern char curr_dir[MAXPATHLEN];
20 @@ -747,7 +748,7 @@ static struct file_struct *recv_file_ent
21 uid = (uid_t)read_varint(f);
22 if (xflags & XMIT_USER_NAME_FOLLOWS)
23 uid = recv_user_name(f, uid);
24 - else if (inc_recurse && am_root && !numeric_ids)
25 + else if (inc_recurse && am_root && (!numeric_ids || usermap))
29 @@ -759,7 +760,7 @@ static struct file_struct *recv_file_ent
31 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
32 gid = recv_group_name(f, gid, &gid_flags);
33 - else if (inc_recurse && (!am_root || !numeric_ids))
34 + else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
35 gid = match_gid(gid, &gid_flags);
38 @@ -2076,8 +2077,13 @@ struct file_list *recv_file_list(int f)
44 rprintf(FLOG, "receiving file list\n");
46 + parse_name_map(usermap, 1);
48 + parse_name_map(groupmap, 0);
50 if (show_filelist_p())
51 start_filelist_progress("receiving file list");
52 else if (inc_recurse && verbose && !am_server && !first_flist)
55 @@ -164,6 +164,8 @@ char *rsync_path = RSYNC_PATH;
56 char *backup_dir = NULL;
57 char backup_dir_buf[MAXPATHLEN];
58 char *sockopts = NULL;
59 +char *usermap = NULL;
60 +char *groupmap = NULL;
64 @@ -377,6 +379,8 @@ void usage(enum logcode F)
65 rprintf(F," --delay-updates put all updated files into place at transfer's end\n");
66 rprintf(F," -m, --prune-empty-dirs prune empty directory chains from the file-list\n");
67 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
68 + rprintf(F," --usermap=STRING custom username mapping\n");
69 + rprintf(F," --groupmap=STRING custom groupname mapping\n");
70 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
71 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
72 rprintf(F," --size-only skip files that match in size\n");
73 @@ -601,6 +605,8 @@ static struct poptOption long_options[]
74 {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
75 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
76 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
77 + {"usermap", 0, POPT_ARG_STRING, &usermap, 0, 0, 0 },
78 + {"groupmap", 0, POPT_ARG_STRING, &groupmap, 0, 0, 0 },
79 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
80 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
81 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
82 @@ -1931,6 +1937,18 @@ void server_options(char **args, int *ar
83 args[ac++] = "--use-qsort";
87 + if (asprintf(&arg, "--usermap=%s", usermap) < 0)
93 + if (asprintf(&arg, "--groupmap=%s", groupmap) < 0)
99 args[ac++] = "--ignore-existing";
103 @@ -378,6 +378,8 @@ to the detailed description below for a
104 --delay-updates put all updated files into place at end
105 -m, --prune-empty-dirs prune empty directory chains from file-list
106 --numeric-ids don't map uid/gid values by user/group name
107 + --usermap=STRING custom username mapping
108 + --groupmap=STRING custom groupname mapping
109 --timeout=TIME set I/O timeout in seconds
110 -I, --ignore-times don't skip files that match size and time
111 --size-only skip files that match in size
112 @@ -1566,6 +1568,46 @@ from the source system is used instead.
113 the chroot setting affects rsync's ability to look up the names of the
114 users and groups and what you can do about it.
116 +dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
117 +specify users and groups that should be mapped to other values by the
118 +receiving side. The bf(STRING) is one or more bf(FROM):bf(TO) pairs of
119 +values separated by commas. Any matching bf(FROM) value from the sender is
120 +replaced with a bf(TO) value from the receiver. You may specify usernames
121 +or user IDs for the bf(FROM) and bf(TO) values, and the bf(FROM) value may
122 +also be a wild-card string, which will be matched against the sender's
123 +names (wild-cards do NOT match against ID numbers). For example:
125 +verb( --usermap=0:bin,wayne:admin,*:nobody --groupmap=usr:1,1:usr)
127 +The first match in the list is the one that is used. You should not use
128 +multiple options of the same type, but instead include all the user
129 +mappings you need separated by commas to a single bf(--usermap) option,
130 +and likewise for groups with the bf(--groupmap) option.
132 +Note that the sender's name for the 0 user and group are not transmitted
133 +to the receiver, so you should either match these values using a 0, or use
134 +the names in effect on the receiving side. All other bf(FROM) names match
135 +those in use on the sending side. All bf(TO) names match those in use on
138 +Any IDs that do not have name on the sending side are treaded as having an
139 +empty name for the purpose of matching. This allows them to be matched via
140 +a "*" as well as an empty name mapping. For instance:
142 +verb( --usermap=:nobody --groupmap=*:nobody)
144 +When the bf(--numeric-ids) option is used,the sender does not send any
145 +names, so all the IDs are treaded as having an empty name. This means that
146 +you will need to specify numeric bf(FROM) values if you want to map these
147 +nameless IDs to different values.
149 +For the bf(--usermap) option to have any effect, the bf(-o) (bf(--owner))
150 +option must be used (or implied), and the receiver will need to be running
151 +as a super-user (see also the bf(--fake-super) option). For the bf(--groupmap)
152 +option to have any effect, the bf(-g) (bf(--groups)) option must be used
153 +(or implied), and the receiver will need to have permissions to set that
156 dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O
157 timeout in seconds. If no data is transferred for the specified time
158 then rsync will exit. The default is 0, which means no timeout.
169 @@ -32,6 +33,8 @@ extern int preserve_uid;
170 extern int preserve_gid;
171 extern int preserve_acls;
172 extern int numeric_ids;
173 +extern char *usermap;
174 +extern char *groupmap;
176 #ifdef HAVE_GETGROUPS
178 @@ -41,6 +44,9 @@ extern int numeric_ids;
180 #define GID_NONE ((gid_t)-1)
182 +#define NFLAGS_WILD_NAME_MATCH (1<<0)
183 +#define NFLAGS_NAME_MATCH (1<<1)
188 @@ -48,8 +54,8 @@ struct idlist {
192 -static struct idlist *uidlist;
193 -static struct idlist *gidlist;
194 +static struct idlist *uidlist, *uidmap;
195 +static struct idlist *gidlist, *gidmap;
197 static struct idlist *add_to_list(struct idlist **root, id_t id, char *name,
198 id_t id2, uint16 flags)
199 @@ -84,22 +90,6 @@ static char *gid_to_name(gid_t gid)
203 -static uid_t map_uid(uid_t id, char *name)
206 - if (id != 0 && name_to_uid(name, &uid))
211 -static gid_t map_gid(gid_t id, char *name)
214 - if (id != 0 && name_to_gid(name, &gid))
219 static int is_in_group(gid_t gid)
221 #ifdef HAVE_GETGROUPS
222 @@ -159,34 +149,49 @@ static int is_in_group(gid_t gid)
226 -/* Add a uid to the list of uids. Only called on receiving side. */
227 -static struct idlist *recv_add_uid(uid_t id, char *name)
228 +/* Add a uid/gid to its list of ids. Only called on receiving side. */
229 +static struct idlist *recv_add_id(struct idlist **idmap_ptr, id_t id, char *name)
231 - uid_t id2 = name ? map_uid(id, name) : id;
235 - node = add_to_list(&uidlist, id, name, id2, 0);
240 - rprintf(FINFO, "uid %u(%s) maps to %u\n",
241 - (unsigned)id, name ? name : "", (unsigned)id2);
242 + for (node = *idmap_ptr; node; node = node->next) {
243 + if (node->flags & NFLAGS_WILD_NAME_MATCH) {
244 + if (!wildmatch(node->name, name))
246 + } else if (node->flags & NFLAGS_NAME_MATCH) {
247 + if (strcmp(node->name, name) != 0)
250 + if (node->id != id)
257 + else if (*name && id) {
258 + if (idmap_ptr == &uidmap) {
260 + id2 = name_to_uid(name, &uid) ? uid : id;
263 + id2 = name_to_gid(name, &gid) ? gid : id;
271 -/* Add a gid to the list of gids. Only called on receiving side. */
272 -static struct idlist *recv_add_gid(gid_t id, char *name)
274 - gid_t id2 = name ? map_gid(id, name) : id;
275 - struct idlist *node;
277 - node = add_to_list(&gidlist, id, name, id2,
278 - !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
279 + node = add_to_list(idmap_ptr, id, *name ? name : NULL, id2,
280 + !am_root && idmap_ptr == &gidmap
281 + && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
284 - rprintf(FINFO, "gid %u(%s) maps to %u\n",
285 - (unsigned)id, name ? name : "", (unsigned)id2);
286 + rprintf(FINFO, "%sid %u(%s) maps to %u\n",
287 + idmap_ptr == &uidmap ? "u" : "g",
288 + (unsigned)id, name, (unsigned)id2);
292 @@ -195,12 +200,9 @@ static struct idlist *recv_add_gid(gid_t
293 /* this function is a definate candidate for a faster algorithm */
294 uid_t match_uid(uid_t uid)
296 - static uid_t last_in, last_out;
297 + static uid_t last_in = -1, last_out = -1;
306 @@ -208,10 +210,13 @@ uid_t match_uid(uid_t uid)
308 for (list = uidlist; list; list = list->next) {
310 - return last_out = list->id2;
314 - return last_out = uid;
316 + list = recv_add_id(&uidmap, uid, NULL);
318 + return last_out = list->id2;
321 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
322 @@ -227,7 +232,7 @@ gid_t match_gid(gid_t gid, uint16 *flags
326 - list = recv_add_gid(gid, NULL);
327 + list = recv_add_id(&gidmap, gid, NULL);
331 @@ -316,7 +321,7 @@ uid_t recv_user_name(int f, uid_t uid)
333 out_of_memory("recv_user_name");
334 read_sbuf(f, name, len);
335 - node = recv_add_uid(uid, name); /* node keeps name's memory */
336 + node = recv_add_id(&uidmap, uid, name); /* node keeps name's memory */
340 @@ -328,7 +333,7 @@ gid_t recv_group_name(int f, gid_t gid,
342 out_of_memory("recv_group_name");
343 read_sbuf(f, name, len);
344 - node = recv_add_gid(gid, name); /* node keeps name's memory */
345 + node = recv_add_id(&gidmap, gid, name); /* node keeps name's memory */
346 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
347 *flags_ptr |= FLAG_SKIP_GROUP;
349 @@ -355,17 +360,96 @@ void recv_id_list(int f, struct file_lis
351 /* Now convert all the uids/gids from sender values to our values. */
353 - if (preserve_acls && !numeric_ids)
354 + if (preserve_acls && (!numeric_ids || usermap || groupmap))
357 - if (am_root && preserve_uid && !numeric_ids) {
358 + if (am_root && preserve_uid && (!numeric_ids || usermap)) {
359 for (i = 0; i < flist->used; i++)
360 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
362 - if (preserve_gid && (!am_root || !numeric_ids)) {
363 + if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
364 for (i = 0; i < flist->used; i++) {
365 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
366 &flist->files[i]->flags);
371 +void parse_name_map(char *map, int usernames)
373 + struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
374 + struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
375 + char *colon, *end, *cp = map + strlen(map);
381 + while (cp > map && cp[-1] != ',') cp--;
382 + if (!(colon = strchr(cp, ':'))) {
383 + rprintf(FERROR, "No colon found in --%smap: %s\n",
384 + usernames ? "user" : "group", cp);
385 + exit_cleanup(RERR_SYNTAX);
390 + if (strspn(cp, "0123456789") != (size_t)(colon - cp)) {
392 + rprintf(FERROR, "Invalid number in --%smap: %s\n",
393 + usernames ? "user" : "group", cp);
394 + exit_cleanup(RERR_SYNTAX);
398 + } else if (strpbrk(cp, "*[?")) {
399 + flags = NFLAGS_WILD_NAME_MATCH;
402 + flags = NFLAGS_NAME_MATCH;
406 + if (isDigit(colon+1)) {
407 + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
411 + id2 = atoi(colon+1);
415 + if (!name_to_uid(colon+1, &uid)) {
418 + "Invalid name in --%smap: %s\n",
419 + usernames ? "user" : "group",
421 + exit_cleanup(RERR_SYNTAX);
426 + if (!name_to_gid(colon+1, &gid))
432 + add_to_list(idmap_ptr, id1, flags ? cp : NULL, id2, flags);
433 + if (numeric_ids && !flags)
434 + add_to_list(idlist_ptr, id1, NULL, id2, flags);
439 + *--cp = '\0'; /* replace comma */
442 + /* The 0 user/group doesn't get its name sent, or add it explicitly. */
446 + cp = usernames ? uid_to_name(0) : gid_to_name(0);
447 + recv_add_id(idmap_ptr, 0, cp);