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 @@ -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;
20 extern char curr_dir[MAXPATHLEN];
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)
26 + else if (inc_recurse && am_root && (!numeric_ids || usermap))
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))
35 + else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
39 @@ -1882,8 +1884,13 @@ struct file_list *recv_file_list(int f)
45 rprintf(FLOG, "receiving file list\n");
47 + parse_name_map(usermap, 1);
49 + parse_name_map(groupmap, 0);
51 if (show_filelist_p())
52 start_filelist_progress("receiving file list");
53 else if (inc_recurse && verbose && !am_server && !first_flist)
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;
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 },
83 @@ -1857,6 +1863,18 @@ void server_options(char **args,int *arg
84 args[ac++] = "--numeric-ids";
88 + if (asprintf(&arg, "--usermap=%s", usermap) < 0)
94 + if (asprintf(&arg, "--groupmap=%s", groupmap) < 0)
100 args[ac++] = "--ignore-existing";
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
113 @@ -1445,6 +1447,42 @@ from the source system is used instead.
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.
117 +dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
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
124 +names (wild-cards do NOT match against ID numbers). For example:
126 +verb( --usermap=0:bin,wayne:admin,*:nobody --groupmap=usr:1,1:usr)
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.
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
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.
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
148 +as root (see also the bf(--fake-super) option). For the bf(--groupmap)
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
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.
158 @@ -38,6 +38,8 @@ extern int preserve_uid;
159 extern int preserve_gid;
160 extern int preserve_acls;
161 extern int numeric_ids;
162 +extern char *usermap;
163 +extern char *groupmap;
167 @@ -45,8 +47,8 @@ struct idlist {
171 -static struct idlist *uidlist;
172 -static struct idlist *gidlist;
173 +static struct idlist *uidlist, *uidmap;
174 +static struct idlist *gidlist, *gidmap;
176 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
178 @@ -158,14 +160,41 @@ static int is_in_group(gid_t gid)
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)
182 - uid_t id2 = name ? map_uid(id, name) : id;
186 - node = add_to_list(&uidlist, (int)id, name, (int)id2);
190 + for (node = uidmap; node; node = node->next) {
191 + switch (node->id) {
193 + if (!wildmatch(node->name, name))
197 + if (strcmp(node->name, name) != 0)
201 + if (node->id != (int)id)
209 + else if (*name && id)
210 + id2 = map_uid(id, name);
214 + node = add_to_list(&uidlist, (int)id, *name ? name : NULL, (int)id2);
217 rprintf(FINFO, "uid %d(%s) maps to %d\n",
218 - (int)id, name ? name : "", (int)id2);
219 + (int)id, name, (int)id2);
223 @@ -174,16 +203,43 @@ static uid_t recv_add_uid(uid_t id, char
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)
227 - gid_t id2 = name ? map_gid(id, name) : id;
234 + for (node = gidmap; node; node = node->next) {
235 + switch (node->id) {
237 + if (!wildmatch(node->name, name))
241 + if (strcmp(node->name, name) != 0)
245 + if (node->id != (int)id)
253 + else if (*name && id)
254 + id2 = map_gid(id, name);
258 if (!am_root && !is_in_group(id2))
260 - node = add_to_list(&gidlist, (int)id, name, (int)id2);
261 + node = add_to_list(&gidlist, (int)id, name ? name : NULL, (int)id2);
264 rprintf(FINFO, "gid %d(%s) maps to %d\n",
265 - (int)id, name ? name : "", (int)id2);
266 + (int)id, name, (int)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)
274 - static uid_t last_in, last_out;
275 + static uid_t last_in = -1, last_out = -1;
284 @@ -208,7 +261,7 @@ uid_t match_uid(uid_t uid)
285 return last_out = (uid_t)list->id2;
288 - return last_out = uid;
289 + return last_out = recv_add_uid(uid, NULL);
292 gid_t match_gid(gid_t gid)
293 @@ -344,15 +397,95 @@ void recv_uid_list(int f, struct file_li
295 /* Now convert all the uids/gids from sender values to our values. */
297 - if (preserve_acls && !numeric_ids)
298 + if (preserve_acls && (!numeric_ids || usermap))
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]));
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++)
309 F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i]));
313 +void parse_name_map(char *map, int usernames)
315 + char *colon, *end, *cp = map + strlen(map);
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);
329 + if (strspn(cp, "0123456789") != (size_t)(colon - cp)) {
331 + rprintf(FERROR, "Invalid number in --%smap: %s\n",
332 + usernames ? "user" : "group", cp);
333 + exit_cleanup(RERR_SYNTAX);
336 + } else if (strpbrk(cp, "*[?"))
341 + if (isDigit(colon+1)) {
342 + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
346 + id2 = atoi(colon+1);
350 + if (name_to_uid(colon+1, &uid))
356 + if (name_to_gid(colon+1, &gid))
362 + rprintf(FERROR, "Invalid name in --%smap: %s\n",
363 + usernames ? "user" : "group", colon+1);
364 + exit_cleanup(RERR_SYNTAX);
369 + add_to_list(&uidmap, id1, id1 < 0 ? cp : NULL, id2);
370 + if (numeric_ids && id2 >= 0)
371 + add_to_list(&uidlist, id1, NULL, id2);
373 + add_to_list(&gidmap, id1, id1 < 0 ? cp : NULL, id2);
374 + if (numeric_ids && id2 >= 0)
375 + add_to_list(&gidlist, id1, NULL, id2);
381 + *--cp = '\0'; /* replace comma */
385 + char *name = uid_to_name(0);
386 + recv_add_uid(0, name ? name : "root");
388 + char *name = gid_to_name(0);
389 + recv_add_gid(0, name ? name : "root");