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,9 @@ 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," --chown=USER:GROUP simple username/groupname mapping\n");
73 rprintf(F," --timeout=SECONDS set I/O timeout in seconds\n");
74 rprintf(F," --contimeout=SECONDS set daemon connection timeout in seconds\n");
75 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
76 @@ -446,7 +451,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
77 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
78 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
79 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
80 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
81 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN,
82 OPT_SERVER, OPT_REFUSED_BASE = 9000};
84 static struct poptOption long_options[] = {
85 @@ -622,6 +627,9 @@ static struct poptOption long_options[] = {
86 {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
87 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
88 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
89 + {"usermap", 0, POPT_ARG_STRING, 0, OPT_USERMAP, 0, 0 },
90 + {"groupmap", 0, POPT_ARG_STRING, 0, OPT_GROUPMAP, 0, 0 },
91 + {"chown", 0, POPT_ARG_STRING, 0, OPT_CHOWN, 0, 0 },
92 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
93 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
94 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
95 @@ -1228,6 +1236,43 @@ int parse_arguments(int *argc_p, const char ***argv_p)
101 + snprintf(err_buf, sizeof err_buf,
102 + "You can only specify --usermap once.\n");
105 + usermap = (char *)poptGetOptArg(pc);
110 + snprintf(err_buf, sizeof err_buf,
111 + "You can only specify --groupmap once.\n");
114 + groupmap = (char *)poptGetOptArg(pc);
118 + if (usermap || groupmap) {
119 + snprintf(err_buf, sizeof err_buf,
120 + "You can only specify --chown once.\n");
123 + const char *chown = poptGetOptArg(pc);
125 + if ((arg = strchr(chown, ':')) != NULL) {
126 + if (arg[1] && asprintf(&groupmap, "*:%s", arg+1) < 0)
127 + out_of_memory("parse_arguments");
130 + len = strlen(chown);
131 + if (len && asprintf(&usermap, "*:%.*s", len, chown) < 0)
132 + out_of_memory("parse_arguments");
139 @@ -2006,6 +2051,18 @@ void server_options(char **args, int *argc_p)
140 args[ac++] = "--use-qsort";
144 + if (asprintf(&arg, "--usermap=%s", usermap) < 0)
150 + if (asprintf(&arg, "--groupmap=%s", groupmap) < 0)
156 args[ac++] = "--ignore-existing";
158 diff --git a/rsync.yo b/rsync.yo
161 @@ -378,6 +378,9 @@ to the detailed description below for a complete description. verb(
162 --delay-updates put all updated files into place at end
163 -m, --prune-empty-dirs prune empty directory chains from file-list
164 --numeric-ids don't map uid/gid values by user/group name
165 + --usermap=STRING custom username mapping
166 + --groupmap=STRING custom groupname mapping
167 + --chown=USER:GROUP simple username/groupname mapping
168 --timeout=SECONDS set I/O timeout in seconds
169 --contimeout=SECONDS set daemon connection timeout in seconds
170 -I, --ignore-times don't skip files that match size and time
171 @@ -1620,6 +1623,57 @@ from the source system is used instead. See also the comments on the
172 the chroot setting affects rsync's ability to look up the names of the
173 users and groups and what you can do about it.
175 +dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
176 +specify users and groups that should be mapped to other values by the
177 +receiving side. The bf(STRING) is one or more bf(FROM):bf(TO) pairs of
178 +values separated by commas. Any matching bf(FROM) value from the sender is
179 +replaced with a bf(TO) value from the receiver. You may specify usernames
180 +or user IDs for the bf(FROM) and bf(TO) values, and the bf(FROM) value may
181 +also be a wild-card string, which will be matched against the sender's
182 +names (wild-cards do NOT match against ID numbers, though see below for
183 +why a '*' matches everything). You may instead specify a range of ID
184 +numbers via an inclusive range: LOW-HIGH. For example:
186 +verb( --usermap=0-99:nobody,wayne:admin,*:normal --groupmap=usr:1,1:usr)
188 +The first match in the list is the one that is used. You should specify
189 +all your user mappings using a single bf(--usermap) option, and/or all
190 +your group mappings using a single bf(--groupmap) option.
192 +Note that the sender's name for the 0 user and group are not transmitted
193 +to the receiver, so you should either match these values using a 0, or use
194 +the names in effect on the receiving side (typically "root"). All other
195 +bf(FROM) names match those in use on the sending side. All bf(TO) names
196 +match those in use on the receiving side.
198 +Any IDs that do not have a name on the sending side are treated as having an
199 +empty name for the purpose of matching. This allows them to be matched via
200 +a "*" or using an empty name. For instance:
202 +verb( --usermap=:nobody --groupmap=*:nobody)
204 +When the bf(--numeric-ids) option is used,the sender does not send any
205 +names, so all the IDs are treated as having an empty name. This means that
206 +you will need to specify numeric bf(FROM) values if you want to map these
207 +nameless IDs to different values.
209 +For the bf(--usermap) option to have any effect, the bf(-o) (bf(--owner))
210 +option must be used (or implied), and the receiver will need to be running
211 +as a super-user (see also the bf(--fake-super) option). For the bf(--groupmap)
212 +option to have any effect, the bf(-g) (bf(--groups)) option must be used
213 +(or implied), and the receiver will need to have permissions to set that
216 +dit(bf(--chown=USER:GROUP)) This option forces all files to be owned by USER
217 +with group GROUP. This is a simpler interface than using bf(--usermap) and
218 +bf(--groupmap) directly, but it is implemented using those options internally,
219 +so you cannot mix them. If either the USER or GROUP is empty, no mapping for
220 +the omitted user/group will occur. If GROUP is empty, the trailing colon may
221 +be omitted, but if USER is empty, a leading colon must be supplied.
223 +If you specify "--chown=foo:bar, this is exactly the same as specifying
224 +"--usermap=*:foo --groupmap=*:bar", only easier.
226 dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O
227 timeout in seconds. If no data is transferred for the specified time
228 then rsync will exit. The default is 0, which means no timeout.
229 diff --git a/support/mapfrom b/support/mapfrom
232 +++ b/support/mapfrom
236 + push @_, "$2:$1" if /^(\w+):[^:]+:(\d+)/;
238 +print join(',', @_), "\n";
239 diff --git a/support/mapto b/support/mapto
246 + push @_, "$1:$2" if /^(\w+):[^:]+:(\d+)/;
248 +print join(',', @_), "\n";
249 diff --git a/uidlist.c b/uidlist.c
260 @@ -32,6 +33,8 @@ extern int preserve_uid;
261 extern int preserve_gid;
262 extern int preserve_acls;
263 extern int numeric_ids;
264 +extern char *usermap;
265 +extern char *groupmap;
267 #ifdef HAVE_GETGROUPS
269 @@ -41,6 +44,9 @@ extern int numeric_ids;
271 #define GID_NONE ((gid_t)-1)
273 +#define NFLAGS_WILD_NAME_MATCH (1<<0)
274 +#define NFLAGS_NAME_MATCH (1<<1)
279 @@ -48,8 +54,8 @@ struct idlist {
283 -static struct idlist *uidlist;
284 -static struct idlist *gidlist;
285 +static struct idlist *uidlist, *uidmap;
286 +static struct idlist *gidlist, *gidmap;
288 static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
289 id_t id2, uint16 flags)
290 @@ -84,22 +90,6 @@ static const char *gid_to_name(gid_t gid)
294 -static uid_t map_uid(uid_t id, const char *name)
297 - if (id != 0 && name_to_uid(name, &uid))
302 -static gid_t map_gid(gid_t id, const char *name)
305 - if (id != 0 && name_to_gid(name, &gid))
310 static int is_in_group(gid_t gid)
312 #ifdef HAVE_GETGROUPS
313 @@ -159,34 +149,53 @@ static int is_in_group(gid_t gid)
317 -/* Add a uid to the list of uids. Only called on receiving side. */
318 -static struct idlist *recv_add_uid(uid_t id, const char *name)
319 +/* Add a uid/gid to its list of ids. Only called on receiving side. */
320 +static struct idlist *recv_add_id(struct idlist **idlist_ptr, struct idlist *idmap,
321 + id_t id, const char *name)
323 - uid_t id2 = name ? map_uid(id, name) : id;
328 - node = add_to_list(&uidlist, id, name, id2, 0);
333 - rprintf(FINFO, "uid %u(%s) maps to %u\n",
334 - (unsigned)id, name ? name : "", (unsigned)id2);
335 + for (node = idmap; node; node = node->next) {
336 + if (node->flags & NFLAGS_WILD_NAME_MATCH) {
337 + if (!wildmatch(node->name, name))
339 + } else if (node->flags & NFLAGS_NAME_MATCH) {
340 + if (strcmp(node->name, name) != 0)
342 + } else if (node->name) {
343 + if (id < node->id || id > (unsigned long)node->name)
346 + if (node->id != id)
353 + else if (*name && id) {
354 + if (idmap == uidmap) {
356 + id2 = name_to_uid(name, &uid) ? uid : id;
359 + id2 = name_to_gid(name, &gid) ? gid : id;
367 -/* Add a gid to the list of gids. Only called on receiving side. */
368 -static struct idlist *recv_add_gid(gid_t id, const char *name)
370 - gid_t id2 = name ? map_gid(id, name) : id;
371 - struct idlist *node;
373 - node = add_to_list(&gidlist, id, name, id2,
374 - !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
375 + flag = idmap == gidmap && !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0;
376 + node = add_to_list(idlist_ptr, id, *name ? name : NULL, id2, flag);
379 - rprintf(FINFO, "gid %u(%s) maps to %u\n",
380 - (unsigned)id, name ? name : "", (unsigned)id2);
381 + rprintf(FINFO, "%sid %u(%s) maps to %u\n",
382 + idmap == uidmap ? "u" : "g",
383 + (unsigned)id, name, (unsigned)id2);
387 @@ -195,12 +204,9 @@ static struct idlist *recv_add_gid(gid_t id, const char *name)
388 /* this function is a definate candidate for a faster algorithm */
389 uid_t match_uid(uid_t uid)
391 - static uid_t last_in, last_out;
392 + static uid_t last_in = -1, last_out = -1;
401 @@ -208,10 +214,13 @@ uid_t match_uid(uid_t uid)
403 for (list = uidlist; list; list = list->next) {
405 - return last_out = list->id2;
409 - return last_out = uid;
411 + list = recv_add_id(&uidlist, uidmap, uid, NULL);
413 + return last_out = list->id2;
416 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
417 @@ -227,7 +236,7 @@ gid_t match_gid(gid_t gid, uint16 *flags_ptr)
421 - list = recv_add_gid(gid, NULL);
422 + list = recv_add_id(&gidlist, gidmap, gid, NULL);
426 @@ -320,7 +329,7 @@ uid_t recv_user_name(int f, uid_t uid)
430 - node = recv_add_uid(uid, name); /* node keeps name's memory */
431 + node = recv_add_id(&uidlist, uidmap, uid, name); /* node keeps name's memory */
435 @@ -336,7 +345,7 @@ gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
439 - node = recv_add_gid(gid, name); /* node keeps name's memory */
440 + node = recv_add_id(&gidlist, gidmap, gid, name); /* node keeps name's memory */
441 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
442 *flags_ptr |= FLAG_SKIP_GROUP;
444 @@ -363,17 +372,103 @@ void recv_id_list(int f, struct file_list *flist)
446 /* Now convert all the uids/gids from sender values to our values. */
448 - if (preserve_acls && !numeric_ids)
449 + if (preserve_acls && (!numeric_ids || usermap || groupmap))
452 - if (am_root && preserve_uid && !numeric_ids) {
453 + if (am_root && preserve_uid && (!numeric_ids || usermap)) {
454 for (i = 0; i < flist->used; i++)
455 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
457 - if (preserve_gid && (!am_root || !numeric_ids)) {
458 + if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
459 for (i = 0; i < flist->used; i++) {
460 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
461 &flist->files[i]->flags);
466 +void parse_name_map(char *map, int usernames)
468 + struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
469 + struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
470 + char *colon, *end, *name, *cp = map + strlen(map);
474 + /* Parse the list in reverse, so the order in the struct is right. */
477 + while (cp > map && cp[-1] != ',') cp--;
478 + if (!(colon = strchr(cp, ':'))) {
479 + rprintf(FERROR, "No colon found in --%smap: %s\n",
480 + usernames ? "user" : "group", cp);
481 + exit_cleanup(RERR_SYNTAX);
484 + rprintf(FERROR, "No name found after colon --%smap: %s\n",
485 + usernames ? "user" : "group", cp);
486 + exit_cleanup(RERR_SYNTAX);
491 + char *dash = strchr(cp, '-');
492 + if (strspn(cp, "0123456789-") != (size_t)(colon - cp)
493 + || (dash && (!dash[1] || strchr(dash+1, '-')))) {
495 + rprintf(FERROR, "Invalid number in --%smap: %s\n",
496 + usernames ? "user" : "group", cp);
497 + exit_cleanup(RERR_SYNTAX);
500 + name = (char *)atol(dash+1);
505 + } else if (strpbrk(cp, "*[?")) {
506 + flags = NFLAGS_WILD_NAME_MATCH;
510 + flags = NFLAGS_NAME_MATCH;
515 + if (isDigit(colon+1)) {
516 + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
520 + add_to_list(idmap_ptr, id1, name, atol(colon+1), flags);
521 + } else if (usernames) {
523 + if (name_to_uid(colon+1, &uid))
524 + add_to_list(idmap_ptr, id1, name, uid, flags);
527 + "Unknown --usermap name on receiver: %s\n",
532 + if (name_to_gid(colon+1, &gid))
533 + add_to_list(idmap_ptr, id1, name, gid, flags);
536 + "Unknown --groupmap name on receiver: %s\n",
544 + *--cp = '\0'; /* replace comma */
547 + /* The 0 user/group doesn't get its name sent, so add it explicitly. */
548 + recv_add_id(idlist_ptr, *idmap_ptr, 0,
549 + numeric_ids ? NULL : usernames ? uid_to_name(0) : gid_to_name(0));