+ gid_t match_gid(gid_t gid, uint16 *flags_ptr)
+@@ -227,7 +231,7 @@ gid_t match_gid(gid_t gid, uint16 *flags
+ break;
+ }
+ if (!list)
+- list = recv_add_gid(gid, NULL);
++ list = recv_add_id(&gidmap, gid, NULL);
+ last = list;
+ }
+
+@@ -316,7 +320,7 @@ uid_t recv_user_name(int f, uid_t uid)
+ if (!name)
+ out_of_memory("recv_user_name");
+ read_sbuf(f, name, len);
+- node = recv_add_uid(uid, name); /* node keeps name's memory */
++ node = recv_add_id(&uidmap, uid, name); /* node keeps name's memory */
+ return node->id2;
+ }
+
+@@ -328,7 +332,7 @@ gid_t recv_group_name(int f, gid_t gid,
+ if (!name)
+ out_of_memory("recv_group_name");
+ read_sbuf(f, name, len);
+- node = recv_add_gid(gid, name); /* node keeps name's memory */
++ node = recv_add_id(&gidmap, gid, name); /* node keeps name's memory */
+ if (flags_ptr && node->flags & FLAG_SKIP_GROUP)
+ *flags_ptr |= FLAG_SKIP_GROUP;
+ return node->id2;
+@@ -355,17 +359,96 @@ void recv_id_list(int f, struct file_lis
+
+ /* 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 || groupmap))
+ 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_OWNER(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_GROUP(flist->files[i]),
+ &flist->files[i]->flags);
+ }