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)
10 diff --git a/flist.c b/flist.c
13 @@ -71,6 +71,7 @@ extern int sender_symlink_iconv;
14 extern int unsort_ndx;
15 extern struct stats stats;
16 extern char *filesfrom_host;
17 +extern char *usermap, *groupmap;
19 extern char curr_dir[MAXPATHLEN];
21 @@ -767,7 +768,7 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
22 uid = (uid_t)read_varint(f);
23 if (xflags & 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 @@ -779,7 +780,7 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
32 if (xflags & XMIT_GROUP_NAME_FOLLOWS)
33 gid = recv_group_name(f, gid, &gid_flags);
34 - else if (inc_recurse && (!am_root || !numeric_ids))
35 + else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
36 gid = match_gid(gid, &gid_flags);
39 @@ -2264,8 +2265,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)
54 diff --git a/options.c b/options.c
57 @@ -167,6 +167,8 @@ char *rsync_path = RSYNC_PATH;
58 char *backup_dir = NULL;
59 char backup_dir_buf[MAXPATHLEN];
60 char *sockopts = NULL;
61 +char *usermap = NULL;
62 +char *groupmap = NULL;
66 @@ -384,6 +386,8 @@ void usage(enum logcode F)
67 rprintf(F," --delay-updates put all updated files into place at transfer's end\n");
68 rprintf(F," -m, --prune-empty-dirs prune empty directory chains from the file-list\n");
69 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
70 + rprintf(F," --usermap=STRING custom username mapping\n");
71 + rprintf(F," --groupmap=STRING custom groupname mapping\n");
72 rprintf(F," --timeout=SECONDS set I/O timeout in seconds\n");
73 rprintf(F," --contimeout=SECONDS set daemon connection timeout in seconds\n");
74 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
75 @@ -446,7 +450,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
76 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
77 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
78 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
79 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
80 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_USERMAP, OPT_GROUPMAP,
81 OPT_SERVER, OPT_REFUSED_BASE = 9000};
83 static struct poptOption long_options[] = {
84 @@ -622,6 +626,8 @@ static struct poptOption long_options[] = {
85 {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
86 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
87 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
88 + {"usermap", 0, POPT_ARG_STRING, 0, OPT_USERMAP, 0, 0 },
89 + {"groupmap", 0, POPT_ARG_STRING, 0, OPT_GROUPMAP, 0, 0 },
90 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
91 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
92 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
93 @@ -1228,6 +1234,24 @@ int parse_arguments(int *argc_p, const char ***argv_p)
99 + snprintf(err_buf, sizeof err_buf,
100 + "You can only specify --usermap once.\n");
103 + usermap = (char *)poptGetOptArg(pc);
108 + snprintf(err_buf, sizeof err_buf,
109 + "You can only specify --groupmap once.\n");
112 + groupmap = (char *)poptGetOptArg(pc);
118 @@ -2005,6 +2029,18 @@ void server_options(char **args, int *argc_p)
119 args[ac++] = "--use-qsort";
123 + if (asprintf(&arg, "--usermap=%s", usermap) < 0)
129 + if (asprintf(&arg, "--groupmap=%s", groupmap) < 0)
135 args[ac++] = "--ignore-existing";
137 diff --git a/rsync.yo b/rsync.yo
140 @@ -378,6 +378,8 @@ to the detailed description below for a complete description. verb(
141 --delay-updates put all updated files into place at end
142 -m, --prune-empty-dirs prune empty directory chains from file-list
143 --numeric-ids don't map uid/gid values by user/group name
144 + --usermap=STRING custom username mapping
145 + --groupmap=STRING custom groupname mapping
146 --timeout=SECONDS set I/O timeout in seconds
147 --contimeout=SECONDS set daemon connection timeout in seconds
148 -I, --ignore-times don't skip files that match size and time
149 @@ -1620,6 +1622,47 @@ from the source system is used instead. See also the comments on the
150 the chroot setting affects rsync's ability to look up the names of the
151 users and groups and what you can do about it.
153 +dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
154 +specify users and groups that should be mapped to other values by the
155 +receiving side. The bf(STRING) is one or more bf(FROM):bf(TO) pairs of
156 +values separated by commas. Any matching bf(FROM) value from the sender is
157 +replaced with a bf(TO) value from the receiver. You may specify usernames
158 +or user IDs for the bf(FROM) and bf(TO) values, and the bf(FROM) value may
159 +also be a wild-card string, which will be matched against the sender's
160 +names (wild-cards do NOT match against ID numbers, though see below for
161 +why a '*' matches everything). You may instead specify a range of ID
162 +numbers via an inclusive range: LOW-HIGH. For example:
164 +verb( --usermap=0-99:nobody,wayne:admin,*:normal --groupmap=usr:1,1:usr)
166 +The first match in the list is the one that is used. You should specify
167 +all your user mappings using a single bf(--usermap) option, and/or all
168 +your group mappings using a single bf(--groupmap) option.
170 +Note that the sender's name for the 0 user and group are not transmitted
171 +to the receiver, so you should either match these values using a 0, or use
172 +the names in effect on the receiving side (typically "root"). All other
173 +bf(FROM) names match those in use on the sending side. All bf(TO) names
174 +match those in use on the receiving side.
176 +Any IDs that do not have a name on the sending side are treated as having an
177 +empty name for the purpose of matching. This allows them to be matched via
178 +a "*" or using an empty name. For instance:
180 +verb( --usermap=:nobody --groupmap=*:nobody)
182 +When the bf(--numeric-ids) option is used,the sender does not send any
183 +names, so all the IDs are treated as having an empty name. This means that
184 +you will need to specify numeric bf(FROM) values if you want to map these
185 +nameless IDs to different values.
187 +For the bf(--usermap) option to have any effect, the bf(-o) (bf(--owner))
188 +option must be used (or implied), and the receiver will need to be running
189 +as a super-user (see also the bf(--fake-super) option). For the bf(--groupmap)
190 +option to have any effect, the bf(-g) (bf(--groups)) option must be used
191 +(or implied), and the receiver will need to have permissions to set that
194 dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O
195 timeout in seconds. If no data is transferred for the specified time
196 then rsync will exit. The default is 0, which means no timeout.
197 diff --git a/support/mapfrom b/support/mapfrom
200 +++ b/support/mapfrom
204 + push @_, "$2:$1" if /^(\w+):[^:]+:(\d+)/;
206 +print join(',', @_), "\n";
207 diff --git a/support/mapto b/support/mapto
214 + push @_, "$1:$2" if /^(\w+):[^:]+:(\d+)/;
216 +print join(',', @_), "\n";
217 diff --git a/uidlist.c b/uidlist.c
228 @@ -32,6 +33,8 @@ extern int preserve_uid;
229 extern int preserve_gid;
230 extern int preserve_acls;
231 extern int numeric_ids;
232 +extern char *usermap;
233 +extern char *groupmap;
235 #ifdef HAVE_GETGROUPS
237 @@ -41,6 +44,9 @@ extern int numeric_ids;
239 #define GID_NONE ((gid_t)-1)
241 +#define NFLAGS_WILD_NAME_MATCH (1<<0)
242 +#define NFLAGS_NAME_MATCH (1<<1)
247 @@ -48,8 +54,8 @@ struct idlist {
251 -static struct idlist *uidlist;
252 -static struct idlist *gidlist;
253 +static struct idlist *uidlist, *uidmap;
254 +static struct idlist *gidlist, *gidmap;
256 static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
257 id_t id2, uint16 flags)
258 @@ -84,22 +90,6 @@ static const char *gid_to_name(gid_t gid)
262 -static uid_t map_uid(uid_t id, const char *name)
265 - if (id != 0 && name_to_uid(name, &uid))
270 -static gid_t map_gid(gid_t id, const char *name)
273 - if (id != 0 && name_to_gid(name, &gid))
278 static int is_in_group(gid_t gid)
280 #ifdef HAVE_GETGROUPS
281 @@ -159,34 +149,53 @@ static int is_in_group(gid_t gid)
285 -/* Add a uid to the list of uids. Only called on receiving side. */
286 -static struct idlist *recv_add_uid(uid_t id, const char *name)
287 +/* Add a uid/gid to its list of ids. Only called on receiving side. */
288 +static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
289 + id_t id, const char *name)
291 - uid_t id2 = name ? map_uid(id, name) : id;
296 - node = add_to_list(&uidlist, id, name, id2, 0);
301 - rprintf(FINFO, "uid %u(%s) maps to %u\n",
302 - (unsigned)id, name ? name : "", (unsigned)id2);
303 + for (node = idmap; node; node = node->next) {
304 + if (node->flags & NFLAGS_WILD_NAME_MATCH) {
305 + if (!wildmatch(node->name, name))
307 + } else if (node->flags & NFLAGS_NAME_MATCH) {
308 + if (strcmp(node->name, name) != 0)
310 + } else if (node->name) {
311 + if (id < node->id || id > (unsigned long)node->name)
314 + if (node->id != id)
321 + else if (*name && id) {
322 + if (idmap == uidmap) {
324 + id2 = name_to_uid(name, &uid) ? uid : id;
327 + id2 = name_to_gid(name, &gid) ? gid : id;
335 -/* Add a gid to the list of gids. Only called on receiving side. */
336 -static struct idlist *recv_add_gid(gid_t id, const char *name)
338 - gid_t id2 = name ? map_gid(id, name) : id;
339 - struct idlist *node;
341 - node = add_to_list(&gidlist, id, name, id2,
342 - !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
343 + flag = idmap == gidmap && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
344 + node = add_to_list(idlist_ptr, id, *name ? name : NULL, id2, flag);
347 - rprintf(FINFO, "gid %u(%s) maps to %u\n",
348 - (unsigned)id, name ? name : "", (unsigned)id2);
349 + rprintf(FINFO, "%sid %u(%s) maps to %u\n",
350 + idmap == uidmap ? "u" : "g",
351 + (unsigned)id, name, (unsigned)id2);
355 @@ -195,12 +204,9 @@ static struct idlist *recv_add_gid(gid_t id, const char *name)
356 /* this function is a definate candidate for a faster algorithm */
357 uid_t match_uid(uid_t uid)
359 - static uid_t last_in, last_out;
360 + static uid_t last_in = -1, last_out = -1;
369 @@ -208,10 +214,13 @@ uid_t match_uid(uid_t uid)
371 for (list = uidlist; list; list = list->next) {
373 - return last_out = list->id2;
377 - return last_out = uid;
379 + list = recv_add_id(&uidlist, uidmap, uid, NULL);
381 + return last_out = list->id2;
384 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
385 @@ -227,7 +236,7 @@ gid_t match_gid(gid_t gid, uint16 *flags_ptr)
389 - list = recv_add_gid(gid, NULL);
390 + list = recv_add_id(&gidlist, gidmap, gid, NULL);
394 @@ -320,7 +329,7 @@ uid_t recv_user_name(int f, uid_t uid)
398 - node = recv_add_uid(uid, name); /* node keeps name's memory */
399 + node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
403 @@ -336,7 +345,7 @@ gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
407 - node = recv_add_gid(gid, name); /* node keeps name's memory */
408 + node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
409 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
410 *flags_ptr |= FLAG_SKIP_GROUP;
412 @@ -363,17 +372,103 @@ void recv_id_list(int f, struct file_list *flist)
414 /* Now convert all the uids/gids from sender values to our values. */
416 - if (preserve_acls && !numeric_ids)
417 + if (preserve_acls && (!numeric_ids || usermap || groupmap))
420 - if (am_root && preserve_uid && !numeric_ids) {
421 + if (am_root && preserve_uid && (!numeric_ids || usermap)) {
422 for (i = 0; i < flist->used; i++)
423 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
425 - if (preserve_gid && (!am_root || !numeric_ids)) {
426 + if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
427 for (i = 0; i < flist->used; i++) {
428 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
429 &flist->files[i]->flags);
434 +void parse_name_map(char *map, int usernames)
436 + struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
437 + struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
438 + char *colon, *end, *name, *cp = map + strlen(map);
442 + /* Parse the list in reverse, so the order in the struct is right. */
445 + while (cp > map && cp[-1] != ',') cp--;
446 + if (!(colon = strchr(cp, ':'))) {
447 + rprintf(FERROR, "No colon found in --%smap: %s\n",
448 + usernames ? "user" : "group", cp);
449 + exit_cleanup(RERR_SYNTAX);
452 + rprintf(FERROR, "No name found after colon --%smap: %s\n",
453 + usernames ? "user" : "group", cp);
454 + exit_cleanup(RERR_SYNTAX);
459 + char *dash = strchr(cp, '-');
460 + if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
461 + || (dash && (!dash[1] || strchr(dash+1, '-')))) {
463 + rprintf(FERROR, "Invalid number in --%smap: %s\n",
464 + usernames ? "user" : "group", cp);
465 + exit_cleanup(RERR_SYNTAX);
468 + name = (char *)atol(dash+1);
473 + } else if (strpbrk(cp, "*[?")) {
474 + flags = NFLAGS_WILD_NAME_MATCH;
478 + flags = NFLAGS_NAME_MATCH;
483 + if (isDigit(colon+1)) {
484 + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
488 + add_to_list(idmap_ptr, id1, name, atol(colon+1), flags);
489 + } else if (usernames) {
491 + if (name_to_uid(colon+1, &uid))
492 + add_to_list(idmap_ptr, id1, name, uid, flags);
495 + "Unknown --usermap name on receiver: %s\n",
500 + if (name_to_gid(colon+1, &gid))
501 + add_to_list(idmap_ptr, id1, name, gid, flags);
504 + "Unknown --groupmap name on receiver: %s\n",
512 + *--cp = '\0'; /* replace comma */
515 + /* The 0 user/group doesn't get its name sent, so add it explicitly. */
516 + recv_add_id(idlist_ptr, *idmap_ptr, 0,
517 + numeric_ids ? NULL : usernames ? uid_to_name(0) : gid_to_name(0));