| 1 | This adds a --usermap and a --groupmap option. See the man page for |
| 2 | more details. |
| 3 | |
| 4 | To use this patch, run these commands for a successful build: |
| 5 | |
| 6 | patch -p1 <patches/usermap.diff |
| 7 | ./configure (optional if already run) |
| 8 | make |
| 9 | |
| 10 | --- old/flist.c |
| 11 | +++ new/flist.c |
| 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; |
| 19 | |
| 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)) |
| 27 | uid = match_uid(uid); |
| 28 | } |
| 29 | } |
| 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)) |
| 36 | gid = match_gid(gid); |
| 37 | } |
| 38 | } |
| 39 | @@ -1882,8 +1884,13 @@ struct file_list *recv_file_list(int f) |
| 40 | int dstart, flags; |
| 41 | int64 start_read; |
| 42 | |
| 43 | - if (!first_flist) |
| 44 | + if (!first_flist) { |
| 45 | rprintf(FLOG, "receiving file list\n"); |
| 46 | + if (usermap) |
| 47 | + parse_name_map(usermap, 1); |
| 48 | + if (groupmap) |
| 49 | + parse_name_map(groupmap, 0); |
| 50 | + } |
| 51 | if (show_filelist_p()) |
| 52 | start_filelist_progress("receiving file list"); |
| 53 | else if (inc_recurse && verbose && !am_server && !first_flist) |
| 54 | --- old/options.c |
| 55 | +++ new/options.c |
| 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; |
| 62 | int rsync_port = 0; |
| 63 | int compare_dest = 0; |
| 64 | int copy_dest = 0; |
| 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"; |
| 85 | |
| 86 | if (am_sender) { |
| 87 | + if (usermap) { |
| 88 | + if (asprintf(&arg, "--usermap=%s", usermap) < 0) |
| 89 | + goto oom; |
| 90 | + args[ac++] = arg; |
| 91 | + } |
| 92 | + |
| 93 | + if (groupmap) { |
| 94 | + if (asprintf(&arg, "--groupmap=%s", groupmap) < 0) |
| 95 | + goto oom; |
| 96 | + args[ac++] = arg; |
| 97 | + } |
| 98 | + |
| 99 | if (ignore_existing) |
| 100 | args[ac++] = "--ignore-existing"; |
| 101 | |
| 102 | --- old/rsync.yo |
| 103 | +++ new/rsync.yo |
| 104 | @@ -367,6 +367,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 | @@ -1451,6 +1453,46 @@ 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. |
| 116 | |
| 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: |
| 125 | + |
| 126 | +verb( --usermap=0:bin,wayne:admin,*:nobody --groupmap=usr:1,1:usr) |
| 127 | + |
| 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. |
| 132 | + |
| 133 | +Note that the sender's name for the 0 user and group are not 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 the sending side. All bf(TO) names match those in use on |
| 137 | +the receiving side. |
| 138 | + |
| 139 | +Any IDs that do not have name on the sending side are treaded as having an |
| 140 | +empty name for the purpose of matching. This allows them to be matched via |
| 141 | +a "*" as well as an empty name mapping. For instance: |
| 142 | + |
| 143 | +verb( --usermap=:nobody --groupmap=*:nobody) |
| 144 | + |
| 145 | +When the bf(--numeric-ids) option is used,the sender does not send any |
| 146 | +names, so all the IDs are treaded as having an empty name. This means that |
| 147 | +you will need to specify numeric bf(FROM) values if you want to map these |
| 148 | +nameless IDs to different values. |
| 149 | + |
| 150 | +For the bf(--usermap) option to have any effect, the bf(-o) (bf(--owner)) |
| 151 | +option must be used (or implied), and the receiver will need to be running |
| 152 | +as a super-user (see also the bf(--fake-super) option). For the bf(--groupmap) |
| 153 | +option to have any effect, the bf(-g) (bf(--groups)) option must be used |
| 154 | +(or implied), and the receiver will need to have permissions to set that |
| 155 | +group. |
| 156 | + |
| 157 | dit(bf(--timeout=TIMEOUT)) This option allows you to set a maximum I/O |
| 158 | timeout in seconds. If no data is transferred for the specified time |
| 159 | then rsync will exit. The default is 0, which means no timeout. |
| 160 | --- old/uidlist.c |
| 161 | +++ new/uidlist.c |
| 162 | @@ -38,6 +38,8 @@ extern int preserve_uid; |
| 163 | extern int preserve_gid; |
| 164 | extern int preserve_acls; |
| 165 | extern int numeric_ids; |
| 166 | +extern char *usermap; |
| 167 | +extern char *groupmap; |
| 168 | |
| 169 | struct idlist { |
| 170 | struct idlist *next; |
| 171 | @@ -45,8 +47,8 @@ struct idlist { |
| 172 | char *name; |
| 173 | }; |
| 174 | |
| 175 | -static struct idlist *uidlist; |
| 176 | -static struct idlist *gidlist; |
| 177 | +static struct idlist *uidlist, *uidmap; |
| 178 | +static struct idlist *gidlist, *gidmap; |
| 179 | |
| 180 | static struct idlist *add_to_list(struct idlist **root, int id, char *name, |
| 181 | int id2) |
| 182 | @@ -80,22 +82,6 @@ static char *gid_to_name(gid_t gid) |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | -static uid_t map_uid(uid_t id, char *name) |
| 187 | -{ |
| 188 | - uid_t uid; |
| 189 | - if (id != 0 && name_to_uid(name, &uid)) |
| 190 | - return uid; |
| 191 | - return id; |
| 192 | -} |
| 193 | - |
| 194 | -static gid_t map_gid(gid_t id, char *name) |
| 195 | -{ |
| 196 | - gid_t gid; |
| 197 | - if (id != 0 && name_to_gid(name, &gid)) |
| 198 | - return gid; |
| 199 | - return id; |
| 200 | -} |
| 201 | - |
| 202 | static int is_in_group(gid_t gid) |
| 203 | { |
| 204 | #ifdef HAVE_GETGROUPS |
| 205 | @@ -155,35 +141,52 @@ static int is_in_group(gid_t gid) |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | -/* Add a uid to the list of uids. Only called on receiving side. */ |
| 210 | -static uid_t recv_add_uid(uid_t id, char *name) |
| 211 | +/* Add a uid/gid to its list of ids. Only called on receiving side. */ |
| 212 | +static int recv_add_id(struct idlist **idmap_ptr, int id, char *name) |
| 213 | { |
| 214 | - uid_t id2 = name ? map_uid(id, name) : id; |
| 215 | struct idlist *node; |
| 216 | + int id2; |
| 217 | |
| 218 | - node = add_to_list(&uidlist, (int)id, name, (int)id2); |
| 219 | + if (!name) |
| 220 | + name = ""; |
| 221 | |
| 222 | - if (verbose > 3) { |
| 223 | - rprintf(FINFO, "uid %d(%s) maps to %d\n", |
| 224 | - (int)id, name ? name : "", (int)id2); |
| 225 | + for (node = *idmap_ptr; node; node = node->next) { |
| 226 | + switch (node->id) { |
| 227 | + case -2: |
| 228 | + if (!wildmatch(node->name, name)) |
| 229 | + continue; |
| 230 | + break; |
| 231 | + case -1: |
| 232 | + if (strcmp(node->name, name) != 0) |
| 233 | + continue; |
| 234 | + break; |
| 235 | + default: |
| 236 | + if (node->id != id) |
| 237 | + continue; |
| 238 | + break; |
| 239 | + } |
| 240 | + break; |
| 241 | } |
| 242 | + if (node) |
| 243 | + id2 = node->id2; |
| 244 | + else if (*name && id) { |
| 245 | + if (idmap_ptr == &uidmap) { |
| 246 | + uid_t uid; |
| 247 | + id2 = name_to_uid(name, &uid) ? (int)uid : id; |
| 248 | + } else { |
| 249 | + gid_t gid; |
| 250 | + id2 = name_to_gid(name, &gid) ? (int)gid : id; |
| 251 | + } |
| 252 | + } else |
| 253 | + id2 = id; |
| 254 | |
| 255 | - return id2; |
| 256 | -} |
| 257 | - |
| 258 | -/* Add a gid to the list of gids. Only called on receiving side. */ |
| 259 | -static gid_t recv_add_gid(gid_t id, char *name) |
| 260 | -{ |
| 261 | - gid_t id2 = name ? map_gid(id, name) : id; |
| 262 | - struct idlist *node; |
| 263 | - |
| 264 | - if (!am_root && !is_in_group(id2)) |
| 265 | + if (idmap_ptr == &gidmap && !am_root && !is_in_group(id2)) |
| 266 | id2 = GID_NONE; |
| 267 | - node = add_to_list(&gidlist, (int)id, name, (int)id2); |
| 268 | + node = add_to_list(idmap_ptr, id, *name ? name : NULL, id2); |
| 269 | |
| 270 | if (verbose > 3) { |
| 271 | - rprintf(FINFO, "gid %d(%s) maps to %d\n", |
| 272 | - (int)id, name ? name : "", (int)id2); |
| 273 | + rprintf(FINFO, "%sid %d(%s) maps to %d\n", |
| 274 | + idmap_ptr == &uidmap ? "u" : "g", id, name, id2); |
| 275 | } |
| 276 | |
| 277 | return id2; |
| 278 | @@ -192,12 +195,9 @@ static gid_t recv_add_gid(gid_t id, char |
| 279 | /* this function is a definate candidate for a faster algorithm */ |
| 280 | uid_t match_uid(uid_t uid) |
| 281 | { |
| 282 | - static uid_t last_in, last_out; |
| 283 | + static uid_t last_in = -1, last_out = -1; |
| 284 | struct idlist *list; |
| 285 | |
| 286 | - if (uid == 0) |
| 287 | - return 0; |
| 288 | - |
| 289 | if (uid == last_in) |
| 290 | return last_out; |
| 291 | |
| 292 | @@ -208,7 +208,7 @@ uid_t match_uid(uid_t uid) |
| 293 | return last_out = (uid_t)list->id2; |
| 294 | } |
| 295 | |
| 296 | - return last_out = uid; |
| 297 | + return last_out = (uid_t)recv_add_id(&uidmap, (int)uid, NULL); |
| 298 | } |
| 299 | |
| 300 | gid_t match_gid(gid_t gid) |
| 301 | @@ -229,7 +229,7 @@ gid_t match_gid(gid_t gid) |
| 302 | return last_out = (gid_t)list->id2; |
| 303 | } |
| 304 | |
| 305 | - return last_out = recv_add_gid(gid, NULL); |
| 306 | + return last_out = (gid_t)recv_add_id(&gidmap, (int)gid, NULL); |
| 307 | } |
| 308 | |
| 309 | /* Add a uid to the list of uids. Only called on sending side. */ |
| 310 | @@ -311,7 +311,7 @@ uid_t recv_user_name(int f, uid_t uid) |
| 311 | if (!name) |
| 312 | out_of_memory("recv_user_name"); |
| 313 | read_sbuf(f, name, len); |
| 314 | - return recv_add_uid(uid, name); /* node keeps name's memory */ |
| 315 | + return (uid_t)recv_add_id(&uidmap, (int)uid, name); /* node keeps name's memory */ |
| 316 | } |
| 317 | |
| 318 | gid_t recv_group_name(int f, gid_t gid) |
| 319 | @@ -321,7 +321,7 @@ gid_t recv_group_name(int f, gid_t gid) |
| 320 | if (!name) |
| 321 | out_of_memory("recv_group_name"); |
| 322 | read_sbuf(f, name, len); |
| 323 | - return recv_add_gid(gid, name); /* node keeps name's memory */ |
| 324 | + return (gid_t)recv_add_id(&gidmap, (int)gid, name); /* node keeps name's memory */ |
| 325 | } |
| 326 | |
| 327 | /* recv a complete uid/gid mapping from the peer and map the uid/gid |
| 328 | @@ -344,15 +344,88 @@ void recv_uid_list(int f, struct file_li |
| 329 | |
| 330 | /* Now convert all the uids/gids from sender values to our values. */ |
| 331 | #ifdef SUPPORT_ACLS |
| 332 | - if (preserve_acls && !numeric_ids) |
| 333 | + if (preserve_acls && (!numeric_ids || usermap)) |
| 334 | match_acl_ids(); |
| 335 | #endif |
| 336 | - if (am_root && preserve_uid && !numeric_ids) { |
| 337 | + if (am_root && preserve_uid && (!numeric_ids || usermap)) { |
| 338 | for (i = 0; i < flist->count; i++) |
| 339 | F_OWNER(flist->files[i]) = match_uid(F_UID(flist->files[i])); |
| 340 | } |
| 341 | - if (preserve_gid && (!am_root || !numeric_ids)) { |
| 342 | + if (preserve_gid && (!am_root || !numeric_ids || groupmap)) { |
| 343 | for (i = 0; i < flist->count; i++) |
| 344 | F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i])); |
| 345 | } |
| 346 | } |
| 347 | + |
| 348 | +void parse_name_map(char *map, int usernames) |
| 349 | +{ |
| 350 | + struct idlist **idmap_ptr = usernames ? &uidmap : &gidmap; |
| 351 | + struct idlist **idlist_ptr = usernames ? &uidlist : &gidlist; |
| 352 | + char *colon, *end, *cp = map + strlen(map); |
| 353 | + int id1, id2; |
| 354 | + |
| 355 | + while (1) { |
| 356 | + end = cp; |
| 357 | + while (cp > map && cp[-1] != ',') cp--; |
| 358 | + if (!(colon = strchr(cp, ':'))) { |
| 359 | + rprintf(FERROR, "No colon found in --%smap: %s\n", |
| 360 | + usernames ? "user" : "group", cp); |
| 361 | + exit_cleanup(RERR_SYNTAX); |
| 362 | + } |
| 363 | + *colon = '\0'; |
| 364 | + |
| 365 | + if (isDigit(cp)) { |
| 366 | + if (strspn(cp, "0123456789") != (size_t)(colon - cp)) { |
| 367 | + bad_number: |
| 368 | + rprintf(FERROR, "Invalid number in --%smap: %s\n", |
| 369 | + usernames ? "user" : "group", cp); |
| 370 | + exit_cleanup(RERR_SYNTAX); |
| 371 | + } |
| 372 | + id1 = atoi(cp); |
| 373 | + } else if (strpbrk(cp, "*[?")) |
| 374 | + id1 = -2; |
| 375 | + else |
| 376 | + id1 = -1; |
| 377 | + |
| 378 | + if (isDigit(colon+1)) { |
| 379 | + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) { |
| 380 | + cp = colon+1; |
| 381 | + goto bad_number; |
| 382 | + } |
| 383 | + id2 = atoi(colon+1); |
| 384 | + } else { |
| 385 | + if (usernames) { |
| 386 | + uid_t uid; |
| 387 | + if (!name_to_uid(colon+1, &uid)) { |
| 388 | + bad_name: |
| 389 | + rprintf(FERROR, |
| 390 | + "Invalid name in --%smap: %s\n", |
| 391 | + usernames ? "user" : "group", |
| 392 | + colon+1); |
| 393 | + exit_cleanup(RERR_SYNTAX); |
| 394 | + } |
| 395 | + id2 = (int)uid; |
| 396 | + } else { |
| 397 | + gid_t gid; |
| 398 | + if (!name_to_gid(colon+1, &gid)) |
| 399 | + goto bad_name; |
| 400 | + id2 = (int)gid; |
| 401 | + } |
| 402 | + } |
| 403 | + |
| 404 | + add_to_list(idmap_ptr, id1, id1 < 0 ? cp : NULL, id2); |
| 405 | + if (numeric_ids && id2 >= 0) |
| 406 | + add_to_list(idlist_ptr, id1, NULL, id2); |
| 407 | + |
| 408 | + if (cp == map) |
| 409 | + break; |
| 410 | + |
| 411 | + *--cp = '\0'; /* replace comma */ |
| 412 | + } |
| 413 | + |
| 414 | + if (numeric_ids) |
| 415 | + cp = NULL; |
| 416 | + else |
| 417 | + cp = usernames ? uid_to_name(0) : gid_to_name(0); |
| 418 | + recv_add_id(idmap_ptr, 0, cp); |
| 419 | +} |