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 @@ -68,6 +68,7 @@ extern int need_unsorted_flist;
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 @@ -752,7 +753,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 @@ -764,7 +765,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 @@ -2074,8 +2075,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 @@ -166,6 +166,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 @@ -379,6 +381,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 @@ -604,6 +608,8 @@ static struct poptOption long_options[] = {
76 {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
77 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
78 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
79 + {"usermap", 0, POPT_ARG_STRING, &usermap, 0, 0, 0 },
80 + {"groupmap", 0, POPT_ARG_STRING, &groupmap, 0, 0, 0 },
81 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
82 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
83 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
84 @@ -1942,6 +1948,18 @@ void server_options(char **args, int *argc_p)
85 args[ac++] = "--use-qsort";
89 + if (asprintf(&arg, "--usermap=%s", usermap) < 0)
95 + if (asprintf(&arg, "--groupmap=%s", groupmap) < 0)
101 args[ac++] = "--ignore-existing";
103 diff --git a/rsync.yo b/rsync.yo
106 @@ -378,6 +378,8 @@ to the detailed description below for a complete description. verb(
107 --delay-updates put all updated files into place at end
108 -m, --prune-empty-dirs prune empty directory chains from file-list
109 --numeric-ids don't map uid/gid values by user/group name
110 + --usermap=STRING custom username mapping
111 + --groupmap=STRING custom groupname mapping
112 --timeout=SECONDS set I/O timeout in seconds
113 --contimeout=SECONDS set daemon connection timeout in seconds
114 -I, --ignore-times don't skip files that match size and time
115 @@ -1579,6 +1581,46 @@ from the source system is used instead. See also the comments on the
116 the chroot setting affects rsync's ability to look up the names of the
117 users and groups and what you can do about it.
119 +dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
120 +specify users and groups that should be mapped to other values by the
121 +receiving side. The bf(STRING) is one or more bf(FROM):bf(TO) pairs of
122 +values separated by commas. Any matching bf(FROM) value from the sender is
123 +replaced with a bf(TO) value from the receiver. You may specify usernames
124 +or user IDs for the bf(FROM) and bf(TO) values, and the bf(FROM) value may
125 +also be a wild-card string, which will be matched against the sender's
126 +names (wild-cards do NOT match against ID numbers). For example:
128 +verb( --usermap=0:bin,wayne:admin,*:nobody --groupmap=usr:1,1:usr)
130 +The first match in the list is the one that is used. You should not use
131 +multiple options of the same type, but instead include all the user
132 +mappings you need separated by commas to a single bf(--usermap) option,
133 +and likewise for groups with the bf(--groupmap) option.
135 +Note that the sender's name for the 0 user and group are not transmitted
136 +to the receiver, so you should either match these values using a 0, or use
137 +the names in effect on the receiving side. All other bf(FROM) names match
138 +those in use on the sending side. All bf(TO) names match those in use on
141 +Any IDs that do not have name on the sending side are treaded as having an
142 +empty name for the purpose of matching. This allows them to be matched via
143 +a "*" as well as an empty name mapping. For instance:
145 +verb( --usermap=:nobody --groupmap=*:nobody)
147 +When the bf(--numeric-ids) option is used,the sender does not send any
148 +names, so all the IDs are treaded as having an empty name. This means that
149 +you will need to specify numeric bf(FROM) values if you want to map these
150 +nameless IDs to different values.
152 +For the bf(--usermap) option to have any effect, the bf(-o) (bf(--owner))
153 +option must be used (or implied), and the receiver will need to be running
154 +as a super-user (see also the bf(--fake-super) option). For the bf(--groupmap)
155 +option to have any effect, the bf(-g) (bf(--groups)) option must be used
156 +(or implied), and the receiver will need to have permissions to set that
159 dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O
160 timeout in seconds. If no data is transferred for the specified time
161 then rsync will exit. The default is 0, which means no timeout.
162 diff --git a/uidlist.c b/uidlist.c
173 @@ -32,6 +33,8 @@ extern int preserve_uid;
174 extern int preserve_gid;
175 extern int preserve_acls;
176 extern int numeric_ids;
177 +extern char *usermap;
178 +extern char *groupmap;
180 #ifdef HAVE_GETGROUPS
182 @@ -41,6 +44,9 @@ extern int numeric_ids;
184 #define GID_NONE ((gid_t)-1)
186 +#define NFLAGS_WILD_NAME_MATCH (1<<0)
187 +#define NFLAGS_NAME_MATCH (1<<1)
192 @@ -48,8 +54,8 @@ struct idlist {
196 -static struct idlist *uidlist;
197 -static struct idlist *gidlist;
198 +static struct idlist *uidlist, *uidmap;
199 +static struct idlist *gidlist, *gidmap;
201 static struct idlist *add_to_list(struct idlist **root, id_t id, const char *name,
202 id_t id2, uint16 flags)
203 @@ -84,22 +90,6 @@ static const char *gid_to_name(gid_t gid)
207 -static uid_t map_uid(uid_t id, char *name)
210 - if (id != 0 && name_to_uid(name, &uid))
215 -static gid_t map_gid(gid_t id, char *name)
218 - if (id != 0 && name_to_gid(name, &gid))
223 static int is_in_group(gid_t gid)
225 #ifdef HAVE_GETGROUPS
226 @@ -159,34 +149,49 @@ static int is_in_group(gid_t gid)
230 -/* Add a uid to the list of uids. Only called on receiving side. */
231 -static struct idlist *recv_add_uid(uid_t id, char *name)
232 +/* Add a uid/gid to its list of ids. Only called on receiving side. */
233 +static struct idlist *recv_add_id(struct idlist **idmap_ptr, id_t id, char *name)
235 - uid_t id2 = name ? map_uid(id, name) : id;
239 - node = add_to_list(&uidlist, id, name, id2, 0);
244 - rprintf(FINFO, "uid %u(%s) maps to %u\n",
245 - (unsigned)id, name ? name : "", (unsigned)id2);
246 + for (node = *idmap_ptr; node; node = node->next) {
247 + if (node->flags & NFLAGS_WILD_NAME_MATCH) {
248 + if (!wildmatch(node->name, name))
250 + } else if (node->flags & NFLAGS_NAME_MATCH) {
251 + if (strcmp(node->name, name) != 0)
254 + if (node->id != id)
261 + else if (*name && id) {
262 + if (idmap_ptr == &uidmap) {
264 + id2 = name_to_uid(name, &uid) ? uid : id;
267 + id2 = name_to_gid(name, &gid) ? gid : id;
275 -/* Add a gid to the list of gids. Only called on receiving side. */
276 -static struct idlist *recv_add_gid(gid_t id, char *name)
278 - gid_t id2 = name ? map_gid(id, name) : id;
279 - struct idlist *node;
281 - node = add_to_list(&gidlist, id, name, id2,
282 - !am_root && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
283 + node = add_to_list(idmap_ptr, id, *name ? name : NULL, id2,
284 + !am_root && idmap_ptr == &gidmap
285 + && !is_in_group(id2) ? FLAG_SKIP_GROUP : 0);
288 - rprintf(FINFO, "gid %u(%s) maps to %u\n",
289 - (unsigned)id, name ? name : "", (unsigned)id2);
290 + rprintf(FINFO, "%sid %u(%s) maps to %u\n",
291 + idmap_ptr == &uidmap ? "u" : "g",
292 + (unsigned)id, name, (unsigned)id2);
296 @@ -195,12 +200,9 @@ static struct idlist *recv_add_gid(gid_t id, char *name)
297 /* this function is a definate candidate for a faster algorithm */
298 uid_t match_uid(uid_t uid)
300 - static uid_t last_in, last_out;
301 + static uid_t last_in = -1, last_out = -1;
310 @@ -208,10 +210,13 @@ uid_t match_uid(uid_t uid)
312 for (list = uidlist; list; list = list->next) {
314 - return last_out = list->id2;
318 - return last_out = uid;
320 + list = recv_add_id(&uidmap, uid, NULL);
322 + return last_out = list->id2;
325 gid_t match_gid(gid_t gid, uint16 *flags_ptr)
326 @@ -227,7 +232,7 @@ gid_t match_gid(gid_t gid, uint16 *flags_ptr)
330 - list = recv_add_gid(gid, NULL);
331 + list = recv_add_id(&gidmap, gid, NULL);
335 @@ -316,7 +321,7 @@ uid_t recv_user_name(int f, uid_t uid)
337 out_of_memory("recv_user_name");
338 read_sbuf(f, name, len);
339 - node = recv_add_uid(uid, name); /* node keeps name's memory */
340 + node = recv_add_id(&uidmap, uid, name); /* node keeps name's memory */
344 @@ -328,7 +333,7 @@ gid_t recv_group_name(int f, gid_t gid, uint16 *flags_ptr)
346 out_of_memory("recv_group_name");
347 read_sbuf(f, name, len);
348 - node = recv_add_gid(gid, name); /* node keeps name's memory */
349 + node = recv_add_id(&gidmap, gid, name); /* node keeps name's memory */
350 if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
351 *flags_ptr |= FLAG_SKIP_GROUP;
353 @@ -355,17 +360,96 @@ void recv_id_list(int f, struct file_list *flist)
355 /* Now convert all the uids/gids from sender values to our values. */
357 - if (preserve_acls && !numeric_ids)
358 + if (preserve_acls && (!numeric_ids || usermap || groupmap))
361 - if (am_root && preserve_uid && !numeric_ids) {
362 + if (am_root && preserve_uid && (!numeric_ids || usermap)) {
363 for (i = 0; i < flist->used; i++)
364 F_OWNER(flist->files[i]) = match_uid(F_OWNER(flist->files[i]));
366 - if (preserve_gid && (!am_root || !numeric_ids)) {
367 + if (preserve_gid && (!am_root || !numeric_ids || groupmap)) {
368 for (i = 0; i < flist->used; i++) {
369 F_GROUP(flist->files[i]) = match_gid(F_GROUP(flist->files[i]),
370 &flist->files[i]->flags);
375 +void parse_name_map(char *map, int usernames)
377 + struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap;
378 + struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist;
379 + char *colon, *end, *cp = map + strlen(map);
385 + while (cp > map && cp[-1] != ',') cp--;
386 + if (!(colon = strchr(cp, ':'))) {
387 + rprintf(FERROR, "No colon found in --%smap: %s\n",
388 + usernames ? "user" : "group", cp);
389 + exit_cleanup(RERR_SYNTAX);
394 + if (strspn(cp, "0123456789") != (size_t)(colon - cp)) {
396 + rprintf(FERROR, "Invalid number in --%smap: %s\n",
397 + usernames ? "user" : "group", cp);
398 + exit_cleanup(RERR_SYNTAX);
402 + } else if (strpbrk(cp, "*[?")) {
403 + flags = NFLAGS_WILD_NAME_MATCH;
406 + flags = NFLAGS_NAME_MATCH;
410 + if (isDigit(colon+1)) {
411 + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
415 + id2 = atoi(colon+1);
419 + if (!name_to_uid(colon+1, &uid)) {
422 + "Invalid name in --%smap: %s\n",
423 + usernames ? "user" : "group",
425 + exit_cleanup(RERR_SYNTAX);
430 + if (!name_to_gid(colon+1, &gid))
436 + add_to_list(idmap_ptr, id1, flags ? cp : NULL, id2, flags);
437 + if (numeric_ids && !flags)
438 + add_to_list(idlist_ptr, id1, NULL, id2, flags);
443 + *--cp = '\0'; /* replace comma */
446 + /* The 0 user/group doesn't get its name sent, or add it explicitly. */
450 + cp = usernames ? uid_to_name(0) : gid_to_name(0);
451 + recv_add_id(idmap_ptr, 0, cp);