idmap4: Slightly simplify idmap_xid_to_sid
[samba.git] / source4 / winbind / idmap.c
index 54fea18c7fe8aeea50b08b7c13d3f3f090636048..bc3b57b585573a5827799bc7b85c6d980a412cb3 100644 (file)
@@ -166,31 +166,36 @@ struct idmap_context *idmap_init(TALLOC_CTX *mem_ctx,
 
        idmap_ctx->lp_ctx = lp_ctx;
 
-       idmap_ctx->ldb_ctx = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx,
+       idmap_ctx->ldb_ctx = ldb_wrap_connect(idmap_ctx, ev_ctx, lp_ctx,
                                              "idmap.ldb",
                                              system_session(lp_ctx),
                                              NULL, 0);
        if (idmap_ctx->ldb_ctx == NULL) {
-               return NULL;
+               goto fail;
        }
 
-       idmap_ctx->unix_groups_sid = dom_sid_parse_talloc(mem_ctx, "S-1-22-2");
+       idmap_ctx->unix_groups_sid = dom_sid_parse_talloc(
+               idmap_ctx, "S-1-22-2");
        if (idmap_ctx->unix_groups_sid == NULL) {
-               return NULL;
+               goto fail;
        }
 
-       idmap_ctx->unix_users_sid = dom_sid_parse_talloc(mem_ctx, "S-1-22-1");
+       idmap_ctx->unix_users_sid = dom_sid_parse_talloc(
+               idmap_ctx, "S-1-22-1");
        if (idmap_ctx->unix_users_sid == NULL) {
-               return NULL;
+               goto fail;
        }
        
        idmap_ctx->samdb = samdb_connect(idmap_ctx, ev_ctx, lp_ctx, system_session(lp_ctx), 0);
        if (idmap_ctx->samdb == NULL) {
                DEBUG(0, ("Failed to load sam.ldb in idmap_init\n"));
-               return NULL;
+               goto fail;
        }
 
        return idmap_ctx;
+fail:
+       TALLOC_FREE(idmap_ctx);
+       return NULL;
 }
 
 /**
@@ -208,7 +213,7 @@ struct idmap_context *idmap_init(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS idmap_xid_to_sid(struct idmap_context *idmap_ctx,
                                 TALLOC_CTX *mem_ctx,
-                                const struct unixid *unixid,
+                                struct unixid *unixid,
                                 struct dom_sid **sid)
 {
        int ret;
@@ -216,7 +221,8 @@ static NTSTATUS idmap_xid_to_sid(struct idmap_context *idmap_ctx,
        struct ldb_context *ldb = idmap_ctx->ldb_ctx;
        struct ldb_result *res = NULL;
        struct ldb_message *msg;
-       struct dom_sid *unix_sid, *new_sid;
+       const struct dom_sid *unix_sid;
+       struct dom_sid *new_sid;
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
        const char *id_type;
 
@@ -321,6 +327,9 @@ static NTSTATUS idmap_xid_to_sid(struct idmap_context *idmap_ctx,
        }
 
        if (res->count == 1) {
+               const char *type = ldb_msg_find_attr_as_string(res->msgs[0],
+                                                              "type", NULL);
+
                *sid = idmap_msg_get_dom_sid(mem_ctx, res->msgs[0],
                                             "objectSid");
                if (*sid == NULL) {
@@ -328,6 +337,21 @@ static NTSTATUS idmap_xid_to_sid(struct idmap_context *idmap_ctx,
                        status = NT_STATUS_NONE_MAPPED;
                        goto failed;
                }
+
+               if (type == NULL) {
+                       DEBUG(1, ("Invalid type for mapping entry.\n"));
+                       talloc_free(tmp_ctx);
+                       return NT_STATUS_NONE_MAPPED;
+               }
+
+               if (strcmp(type, "ID_TYPE_BOTH") == 0) {
+                       unixid->type = ID_TYPE_BOTH;
+               } else if (strcmp(type, "ID_TYPE_UID") == 0) {
+                       unixid->type = ID_TYPE_UID;
+               } else {
+                       unixid->type = ID_TYPE_GID;
+               }
+
                talloc_free(tmp_ctx);
                return NT_STATUS_OK;
        }
@@ -336,13 +360,9 @@ static NTSTATUS idmap_xid_to_sid(struct idmap_context *idmap_ctx,
 
        /* For local users/groups , we just create a rid = uid/gid */
        if (unixid->type == ID_TYPE_UID) {
-               unix_sid = dom_sid_parse_talloc(tmp_ctx, "S-1-22-1");
+               unix_sid = &global_sid_Unix_Users;
        } else {
-               unix_sid = dom_sid_parse_talloc(tmp_ctx, "S-1-22-2");
-       }
-       if (unix_sid == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto failed;
+               unix_sid = &global_sid_Unix_Groups;
        }
 
        new_sid = dom_sid_add_rid(mem_ctx, unix_sid, unixid->id);