r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / ntvfs / common / sidmap.c
index 89ad2e24306e50093158197c58ea7226da3555db..b2408d42bd21a302248089ea197f0b89cf55569b 100644 (file)
 */
 
 #include "includes.h"
-#include "librpc/gen_ndr/ndr_security.h"
+#include "system/passwd.h"
+#include "ads.h"
+#include "dsdb/samdb/samdb.h"
+#include "auth/auth.h"
+#include "libcli/ldap/ldap.h"
 
 /*
   these are used for the fallback local uid/gid to sid mapping
@@ -45,11 +49,11 @@ struct sidmap_context {
 struct sidmap_context *sidmap_open(TALLOC_CTX *mem_ctx)
 {
        struct sidmap_context *sidmap;
-       sidmap = talloc_p(mem_ctx, struct sidmap_context);
+       sidmap = talloc(mem_ctx, struct sidmap_context);
        if (sidmap == NULL) {
                return NULL;
        }
-       sidmap->samctx = samdb_connect(sidmap);
+       sidmap->samctx = samdb_connect(sidmap, system_session(sidmap));
        if (sidmap->samctx == NULL) {
                talloc_free(sidmap);
                return NULL;
@@ -94,26 +98,18 @@ static NTSTATUS sidmap_primary_domain_sid(struct sidmap_context *sidmap,
                                          TALLOC_CTX *mem_ctx, struct dom_sid **sid)
 {
        const char *attrs[] = { "objectSid", NULL };
-       void *ctx = talloc(mem_ctx, 0);
-       const char *sidstr;
        int ret;
-       struct ldb_message **res;
+       struct ldb_message **res = NULL;
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs
-                          "(&(objectClass=domain)(name=%s))", lp_workgroup());
+       ret = gendb_search_dn(sidmap->samctx, mem_ctx, samdb_base_dn(mem_ctx)
+                             &res, attrs);
        if (ret != 1) {
-               talloc_free(ctx);
+               talloc_free(res);
                return NT_STATUS_NO_SUCH_DOMAIN;
        }
        
-       sidstr = samdb_result_string(res[0], "objectSid", NULL);
-       if (sidstr == NULL) {
-               talloc_free(ctx);
-               return NT_STATUS_NO_SUCH_DOMAIN;
-       }
-
-       *sid = dom_sid_parse_talloc(mem_ctx, sidstr);
-       talloc_free(ctx);
+       *sid = samdb_result_dom_sid(mem_ctx, res[0], "objectSid");
+       talloc_free(res);
        if (*sid == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -134,26 +130,21 @@ NTSTATUS sidmap_sid_to_unixuid(struct sidmap_context *sidmap,
        const char *s;
        void *ctx;
        struct ldb_message **res;
-       const char *sidstr;
        struct dom_sid *domain_sid;
        NTSTATUS status;
 
-       ctx = talloc(sidmap, 0);
-       sidstr = dom_sid_string(ctx, sid);
-       if (sidstr == NULL) {
-               talloc_free(ctx);
-               return NT_STATUS_NO_MEMORY;
-       }
+       ctx = talloc_new(sidmap);
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
-                          "objectSid=%s", sidstr);
+       ret = gendb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
+                          "objectSid=%s", ldap_encode_ndr_dom_sid(ctx, sid));
        if (ret != 1) {
                goto allocated_sid;
        }
 
        /* make sure its a user, not a group */
        if (!is_user_account(res[0])) {
-               DEBUG(0,("sid_to_unixuid: sid %s is not an account!\n", sidstr));
+               DEBUG(0,("sid_to_unixuid: sid %s is not an account!\n", 
+                        dom_sid_string(ctx, sid)));
                talloc_free(ctx);
                return NT_STATUS_INVALID_SID;
        }
@@ -171,7 +162,7 @@ NTSTATUS sidmap_sid_to_unixuid(struct sidmap_context *sidmap,
        if (s != NULL) {
                struct passwd *pwd = getpwnam(s);
                if (!pwd) {
-                       DEBUG(0,("unixName %s for sid %s does not exist as a local user\n", s, sidstr));
+                       DEBUG(0,("unixName %s for sid %s does not exist as a local user\n", s, dom_sid_string(ctx, sid)));
                        talloc_free(ctx);
                        return NT_STATUS_NO_SUCH_USER;
                }
@@ -185,7 +176,8 @@ NTSTATUS sidmap_sid_to_unixuid(struct sidmap_context *sidmap,
        if (s != NULL) {
                struct passwd *pwd = getpwnam(s);
                if (!pwd) {
-                       DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local user\n", s, sidstr));
+                       DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local user\n", 
+                                s, dom_sid_string(ctx, sid)));
                        talloc_free(ctx);
                        return NT_STATUS_NO_SUCH_USER;
                }
@@ -214,7 +206,7 @@ allocated_sid:
        
 
        DEBUG(0,("sid_to_unixuid: no unixID, unixName or sAMAccountName for sid %s\n", 
-                sidstr));
+                dom_sid_string(ctx, sid)));
 
        talloc_free(ctx);
        return NT_STATUS_INVALID_SID;
@@ -233,26 +225,21 @@ NTSTATUS sidmap_sid_to_unixgid(struct sidmap_context *sidmap,
        const char *s;
        void *ctx;
        struct ldb_message **res;
-       const char *sidstr;
        NTSTATUS status;
        struct dom_sid *domain_sid;
 
-       ctx = talloc(sidmap, 0);
-       sidstr = dom_sid_string(ctx, sid);
-       if (sidstr == NULL) {
-               talloc_free(ctx);
-               return NT_STATUS_NO_MEMORY;
-       }
+       ctx = talloc_new(sidmap);
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
-                          "objectSid=%s", sidstr);
+       ret = gendb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
+                          "objectSid=%s", ldap_encode_ndr_dom_sid(ctx, sid));
        if (ret != 1) {
                goto allocated_sid;
        }
 
        /* make sure its not a user */
        if (!is_group_account(res[0])) {
-               DEBUG(0,("sid_to_unixgid: sid %s is a ATYPE_NORMAL_ACCOUNT\n", sidstr));
+               DEBUG(0,("sid_to_unixgid: sid %s is a ATYPE_NORMAL_ACCOUNT\n", 
+                        dom_sid_string(ctx, sid)));
                talloc_free(ctx);
                return NT_STATUS_INVALID_SID;
        }
@@ -271,7 +258,7 @@ NTSTATUS sidmap_sid_to_unixgid(struct sidmap_context *sidmap,
                struct group *grp = getgrnam(s);
                if (!grp) {
                        DEBUG(0,("unixName '%s' for sid %s does not exist as a local group\n", 
-                                s, sidstr));
+                                s, dom_sid_string(ctx, sid)));
                        talloc_free(ctx);
                        return NT_STATUS_NO_SUCH_USER;
                }
@@ -285,7 +272,7 @@ NTSTATUS sidmap_sid_to_unixgid(struct sidmap_context *sidmap,
        if (s != NULL) {
                struct group *grp = getgrnam(s);
                if (!grp) {
-                       DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local group\n", s, sidstr));
+                       DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local group\n", s, dom_sid_string(ctx, sid)));
                        talloc_free(ctx);
                        return NT_STATUS_NO_SUCH_USER;
                }
@@ -311,7 +298,7 @@ allocated_sid:
        }
 
        DEBUG(0,("sid_to_unixgid: no unixID, unixName or sAMAccountName for sid %s\n", 
-                sidstr));
+                dom_sid_string(ctx, sid)));
 
        talloc_free(ctx);
        return NT_STATUS_INVALID_SID;
@@ -349,7 +336,7 @@ NTSTATUS sidmap_uid_to_sid(struct sidmap_context *sidmap,
        */
 
 
-       ctx = talloc(sidmap, 0);
+       ctx = talloc_new(sidmap);
 
 
        /*
@@ -357,21 +344,14 @@ NTSTATUS sidmap_uid_to_sid(struct sidmap_context *sidmap,
                   given uid
        */
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
+       ret = gendb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
                           "unixID=%u", (unsigned int)uid);
        for (i=0;i<ret;i++) {
-               const char *sidstr;
-
                if (!is_user_account(res[i])) continue;
 
-               sidstr = samdb_result_string(res[i], "objectSid", NULL);
-               if (sidstr == NULL) continue;
-
-               *sid = dom_sid_parse_talloc(mem_ctx, sidstr);
+               *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
                talloc_free(ctx);
-               if (*sid == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(*sid);
                return NT_STATUS_OK;
        }
 
@@ -384,22 +364,15 @@ NTSTATUS sidmap_uid_to_sid(struct sidmap_context *sidmap,
                goto allocate_sid;
        }
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
+       ret = gendb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
                           "(|(unixName=%s)(sAMAccountName=%s))", 
                           pwd->pw_name, pwd->pw_name);
        for (i=0;i<ret;i++) {
-               const char *sidstr;
-
                if (!is_user_account(res[i])) continue;
 
-               sidstr = samdb_result_string(res[i], "objectSid", NULL);
-               if (sidstr == NULL) continue;
-
-               *sid = dom_sid_parse_talloc(mem_ctx, sidstr);
+               *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
                talloc_free(ctx);
-               if (*sid == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(*sid);
                return NT_STATUS_OK;
        }
 
@@ -461,7 +434,7 @@ NTSTATUS sidmap_gid_to_sid(struct sidmap_context *sidmap,
        */
 
 
-       ctx = talloc(sidmap, 0);
+       ctx = talloc_new(sidmap);
 
 
        /*
@@ -469,21 +442,14 @@ NTSTATUS sidmap_gid_to_sid(struct sidmap_context *sidmap,
                   given gid
        */
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
+       ret = gendb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
                           "unixID=%u", (unsigned int)gid);
        for (i=0;i<ret;i++) {
-               const char *sidstr;
-
                if (!is_group_account(res[i])) continue;
 
-               sidstr = samdb_result_string(res[i], "objectSid", NULL);
-               if (sidstr == NULL) continue;
-
-               *sid = dom_sid_parse_talloc(mem_ctx, sidstr);
+               *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
                talloc_free(ctx);
-               if (*sid == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(*sid);
                return NT_STATUS_OK;
        }
 
@@ -496,22 +462,15 @@ NTSTATUS sidmap_gid_to_sid(struct sidmap_context *sidmap,
                goto allocate_sid;
        }
 
-       ret = samdb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
+       ret = gendb_search(sidmap->samctx, ctx, NULL, &res, attrs, 
                           "(|(unixName=%s)(sAMAccountName=%s))", 
                           grp->gr_name, grp->gr_name);
        for (i=0;i<ret;i++) {
-               const char *sidstr;
-
                if (!is_group_account(res[i])) continue;
 
-               sidstr = samdb_result_string(res[i], "objectSid", NULL);
-               if (sidstr == NULL) continue;
-
-               *sid = dom_sid_parse_talloc(mem_ctx, sidstr);
+               *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
                talloc_free(ctx);
-               if (*sid == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(*sid);
                return NT_STATUS_OK;
        }
 
@@ -553,7 +512,7 @@ NTSTATUS sidmap_allocated_sid_lookup(struct sidmap_context *sidmap,
 {
        NTSTATUS status;
        struct dom_sid *domain_sid;
-       void *ctx = talloc(mem_ctx, 0);
+       void *ctx = talloc_new(mem_ctx);
        uint32_t rid;
 
        status = sidmap_primary_domain_sid(sidmap, ctx, &domain_sid);