This adds a --usermap and a --groupmap option. See the man page for more details. To use this patch, run these commands for a successful build: patch -p1 next) { + switch (node->id) { + case -2: + if (!wildmatch(node->name, name)) + continue; + break; + case -1: + if (strcmp(node->name, name) != 0) + continue; + break; + default: + if (node->id != (int)id) + continue; + break; + } + break; + } + if (node) + id2 = node->id2; + else if (*name && id) + id2 = map_uid(id, name); + else + id2 = id; + + node = add_to_list(&uidlist, (int)id, *name ? name : NULL, (int)id2); if (verbose > 3) { rprintf(FINFO, "uid %d(%s) maps to %d\n", - (int)id, name ? name : "", (int)id2); + (int)id, name, (int)id2); } return id2; @@ -174,16 +203,43 @@ static uid_t recv_add_uid(uid_t id, char /* Add a gid to the list of gids. Only called on receiving side. */ static gid_t recv_add_gid(gid_t id, char *name) { - gid_t id2 = name ? map_gid(id, name) : id; struct idlist *node; + gid_t id2; + + if (!name) + name = ""; + + for (node = gidmap; node; node = node->next) { + switch (node->id) { + case -2: + if (!wildmatch(node->name, name)) + continue; + break; + case -1: + if (strcmp(node->name, name) != 0) + continue; + break; + default: + if (node->id != (int)id) + continue; + break; + } + break; + } + if (node) + id2 = node->id2; + else if (*name && id) + id2 = map_gid(id, name); + else + id2 = id; if (!am_root && !is_in_group(id2)) id2 = GID_NONE; - node = add_to_list(&gidlist, (int)id, name, (int)id2); + node = add_to_list(&gidlist, (int)id, name ? name : NULL, (int)id2); if (verbose > 3) { rprintf(FINFO, "gid %d(%s) maps to %d\n", - (int)id, name ? name : "", (int)id2); + (int)id, name, (int)id2); } return id2; @@ -192,12 +248,9 @@ static gid_t recv_add_gid(gid_t id, char /* this function is a definate candidate for a faster algorithm */ uid_t match_uid(uid_t uid) { - static uid_t last_in, last_out; + static uid_t last_in = -1, last_out = -1; struct idlist *list; - if (uid == 0) - return 0; - if (uid == last_in) return last_out; @@ -208,7 +261,7 @@ uid_t match_uid(uid_t uid) return last_out = (uid_t)list->id2; } - return last_out = uid; + return last_out = recv_add_uid(uid, NULL); } gid_t match_gid(gid_t gid) @@ -344,15 +397,95 @@ void recv_uid_list(int f, struct file_li /* Now convert all the uids/gids from sender values to our values. */ #ifdef SUPPORT_ACLS - if (preserve_acls && !numeric_ids) + if (preserve_acls && (!numeric_ids || usermap)) match_acl_ids(); #endif - if (am_root && preserve_uid && !numeric_ids) { + if (am_root && preserve_uid && (!numeric_ids || usermap)) { for (i = 0; i < flist->count; i++) F_OWNER(flist->files[i]) = match_uid(F_UID(flist->files[i])); } - if (preserve_gid && (!am_root || !numeric_ids)) { + if (preserve_gid && (!am_root || !numeric_ids || groupmap)) { for (i = 0; i < flist->count; i++) F_GROUP(flist->files[i]) = match_gid(F_GID(flist->files[i])); } } + +void parse_name_map(char *map, int usernames) +{ + char *colon, *end, *cp = map + strlen(map); + int id1, id2; + + while (1) { + end = cp; + while (cp > map && cp[-1] != ',') cp--; + if (!(colon = strchr(cp, ':'))) { + rprintf(FERROR, "No colon found in --%smap: %s\n", + usernames ? "user" : "group", cp); + exit_cleanup(RERR_SYNTAX); + } + *colon = '\0'; + + if (isDigit(cp)) { + if (strspn(cp, "0123456789") != (size_t)(colon - cp)) { + bad_number: + rprintf(FERROR, "Invalid number in --%smap: %s\n", + usernames ? "user" : "group", cp); + exit_cleanup(RERR_SYNTAX); + } + id1 = atoi(cp); + } else if (strpbrk(cp, "*[?")) + id1 = -2; + else + id1 = -1; + + if (isDigit(colon+1)) { + if (strspn(colon+1, "0123456789") != (size_t)(end - colon - 1)) { + cp = colon+1; + goto bad_number; + } + id2 = atoi(colon+1); + } else { + if (usernames) { + uid_t uid; + if (name_to_uid(colon+1, &uid)) + id2 = (int)uid; + else + id2 = -1; + } else { + gid_t gid; + if (name_to_gid(colon+1, &gid)) + id2 = (int)gid; + else + id2 = -1; + } + if (id2 < 0) { + rprintf(FERROR, "Invalid name in --%smap: %s\n", + usernames ? "user" : "group", colon+1); + exit_cleanup(RERR_SYNTAX); + } + } + + if (usernames) { + add_to_list(&uidmap, id1, id1 < 0 ? cp : NULL, id2); + if (numeric_ids && id2 >= 0) + add_to_list(&uidlist, id1, NULL, id2); + } else { + add_to_list(&gidmap, id1, id1 < 0 ? cp : NULL, id2); + if (numeric_ids && id2 >= 0) + add_to_list(&gidlist, id1, NULL, id2); + } + + if (cp == map) + break; + + *--cp = '\0'; /* replace comma */ + } + + if (usernames) { + char *name = uid_to_name(0); + recv_add_uid(0, name ? name : "root"); + } else { + char *name = gid_to_name(0); + recv_add_gid(0, name ? name : "root"); + } +}