1 This adds a --usermap and a --groupmap option.
3 TODO: make this work when --numeric-ids was specified.
7 @@ -61,6 +61,8 @@ extern int copy_links;
8 extern int copy_unsafe_links;
9 extern int protocol_version;
10 extern int sanitize_paths;
11 +extern char *usermap;
12 +extern char *groupmap;
13 extern struct stats stats;
15 extern char curr_dir[MAXPATHLEN];
16 @@ -1882,8 +1884,13 @@ struct file_list *recv_file_list(int f)
22 rprintf(FLOG, "receiving file list\n");
24 + parse_name_map(usermap, 1);
26 + parse_name_map(groupmap, 0);
28 if (show_filelist_p())
29 start_filelist_progress("receiving file list");
30 else if (inc_recurse && verbose && !am_server && !first_flist)
33 @@ -156,6 +156,8 @@ char *rsync_path = RSYNC_PATH;
34 char *backup_dir = NULL;
35 char backup_dir_buf[MAXPATHLEN];
36 char *sockopts = NULL;
37 +char *usermap = NULL;
38 +char *groupmap = NULL;
42 @@ -367,6 +369,8 @@ void usage(enum logcode F)
43 rprintf(F," --delay-updates put all updated files into place at transfer's end\n");
44 rprintf(F," -m, --prune-empty-dirs prune empty directory chains from the file-list\n");
45 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
46 + rprintf(F," --usermap=STRING custom username mapping\n");
47 + rprintf(F," --groupmap=STRING custom groupname mapping\n");
48 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
49 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
50 rprintf(F," --size-only skip files that match in size\n");
51 @@ -568,6 +572,8 @@ static struct poptOption long_options[]
52 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
53 {"from0", '0', POPT_ARG_NONE, &eol_nulls, 0, 0, 0},
54 {"numeric-ids", 0, POPT_ARG_NONE, &numeric_ids, 0, 0, 0 },
55 + {"usermap", 0, POPT_ARG_STRING, &usermap, 0, 0, 0 },
56 + {"groupmap", 0, POPT_ARG_STRING, &groupmap, 0, 0, 0 },
57 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
58 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
59 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
60 @@ -1857,6 +1863,22 @@ void server_options(char **args,int *arg
61 args[ac++] = "--numeric-ids";
65 + if (strchr(usermap, '\'') != NULL)
66 + usermap = "INVALID";
67 + if (asprintf(&arg, "--usermap='%s'", usermap) < 0)
73 + if (strchr(groupmap, '\'') != NULL)
74 + groupmap = "INVALID";
75 + if (asprintf(&arg, "--groupmap='%s'", groupmap) < 0)
81 args[ac++] = "--ignore-existing";
85 @@ -361,6 +361,8 @@ to the detailed description below for a
86 --delay-updates put all updated files into place at end
87 -m, --prune-empty-dirs prune empty directory chains from file-list
88 --numeric-ids don't map uid/gid values by user/group name
89 + --usermap=STRING custom username mapping
90 + --groupmap=STRING custom groupname mapping
91 --timeout=TIME set I/O timeout in seconds
92 -I, --ignore-times don't skip files that match size and time
93 --size-only skip files that match in size
94 @@ -1445,6 +1447,25 @@ from the source system is used instead.
95 the chroot setting affects rsync's ability to look up the names of the
96 users and groups and what you can do about it.
98 +dit(bf(--usermap=STRING, --groupmap=STRING)) These options allow you to
99 +specify user/group names and IDs that should be mapped to other values by
100 +the receiving side. The bf(STRING) is one or more FROM:TO pairs of values
101 +separated by commas. Any matching FROM value from the sender is replaced
102 +with a TO value from the receiver. You may specify usernames or user IDs
103 +for the FROM and TO values, and the FROM value may also be a wild-card
104 +string, which will be matched against the sender's names (it will not match
107 + --usermap=0:foo,bar:baz,*:nobody --groupmap=root:1,1:root
109 +The first match in the list is the one that is used.
111 +For the bf(--usermap) option to be effective you will need to have specified
112 +the bf(-o) (bf(--owner)) option and the receiver will need to be running as
113 +root (see also the bf(--fake-root) option). For the bf(--groupmap) option
114 +to be effective you will need to have specified the bf(-g) (bf(--groups))
115 +option, and the receiver will need to have permissions to set that group.
117 dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O
118 timeout in seconds. If no data is transferred for the specified time
119 then rsync will exit. The default is 0, which means no timeout.
122 @@ -38,6 +38,7 @@ extern int preserve_uid;
123 extern int preserve_gid;
124 extern int preserve_acls;
125 extern int numeric_ids;
126 +extern int protocol_version;
130 @@ -45,8 +46,8 @@ struct idlist {
134 -static struct idlist *uidlist;
135 -static struct idlist *gidlist;
136 +static struct idlist *uidlist, *uidmap;
137 +static struct idlist *gidlist, *gidmap;
139 static struct idlist *add_to_list(struct idlist **root, int id, char *name,
141 @@ -158,8 +159,33 @@ static int is_in_group(gid_t gid)
142 /* Add a uid to the list of uids. Only called on receiving side. */
143 static uid_t recv_add_uid(uid_t id, char *name)
145 - uid_t id2 = name ? map_uid(id, name) : id;
150 + struct idlist *list;
151 + for (list = uidmap; list; list = list->next) {
152 + switch (list->id) {
154 + if (!wildmatch(list->name, name))
158 + if (strcmp(list->name, name) != 0)
162 + if (list->id != (int)id)
170 + id2 = id ? map_uid(id, name) : 0; /* don't map root */
174 node = add_to_list(&uidlist, (int)id, name, (int)id2);
176 @@ -174,8 +200,33 @@ static uid_t recv_add_uid(uid_t id, char
177 /* Add a gid to the list of gids. Only called on receiving side. */
178 static gid_t recv_add_gid(gid_t id, char *name)
180 - gid_t id2 = name ? map_gid(id, name) : id;
185 + struct idlist *list;
186 + for (list = gidmap; list; list = list->next) {
187 + switch (list->id) {
189 + if (!wildmatch(list->name, name))
193 + if (strcmp(list->name, name) != 0)
197 + if (list->id != (int)id)
205 + id2 = id ? map_gid(id, name) : 0; /* don't map root */
209 if (!am_root && !is_in_group(id2))
211 @@ -195,9 +246,6 @@ uid_t match_uid(uid_t uid)
212 static uid_t last_in, last_out;
221 @@ -238,7 +286,7 @@ char *add_uid(uid_t uid)
225 - if (uid == 0) /* don't map root */
226 + if (uid == 0 && protocol_version < 30)
229 for (list = uidlist; list; list = list->next) {
230 @@ -256,7 +304,7 @@ char *add_gid(gid_t gid)
234 - if (gid == 0) /* don't map root */
235 + if (gid == 0 && protocol_version < 30)
238 for (list = gidlist; list; list = list->next) {
239 @@ -356,3 +404,70 @@ void recv_uid_list(int f, struct file_li
240 F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i]));
244 +void parse_name_map(char *map, int usernames)
246 + char *colon, *end, *cp = map + strlen(map);
251 + while (cp > map && cp[-1] != ',') cp--;
252 + if (!(colon = strchr(cp, ':'))) {
253 + rprintf(FERROR, "No colon found in --%smap: %s\n",
254 + usernames ? "user" : "group", cp);
255 + exit_cleanup(RERR_SYNTAX);
260 + if (strspn(cp, "0123456789") != (size_t)(colon - cp)) {
262 + rprintf(FERROR, "Invalid number in --%smap: %s\n",
263 + usernames ? "user" : "group", cp);
264 + exit_cleanup(RERR_SYNTAX);
267 + } else if (strpbrk(cp, "*[?"))
272 + if (isDigit(colon+1)) {
273 + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) {
277 + id2 = atoi(colon+1);
281 + if (name_to_uid(colon+1, &uid))
287 + if (name_to_gid(colon+1, &gid))
293 + rprintf(FERROR, "Invalid name in --%smap: %s\n",
294 + usernames ? "user" : "group", colon+1);
295 + exit_cleanup(RERR_SYNTAX);
300 + add_to_list(&uidmap, id1, id1 < 0 ? cp : NULL, id2);
302 + add_to_list(&gidmap, id1, id1 < 0 ? cp : NULL, id2);
307 + *--cp = '\0'; /* replace comma */