TODO test/sq sort meta data after adding isDeleted
[metze/samba/wip.git] / source4 / dsdb / samdb / samdb.c
index 57de27a22e099c55f3e01a140c2dcd118607bd9e..713448c4e819a8d338adc4e91c9ad33c443c61fd 100644 (file)
 
 #include "includes.h"
 #include "librpc/gen_ndr/ndr_netlogon.h"
-#include "librpc/gen_ndr/ndr_misc.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "lib/events/events.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_errors.h"
+#include "lib/ldb-samba/ldb_wrap.h"
+#include <ldb.h>
+#include <ldb_errors.h>
 #include "libcli/security/security.h"
 #include "libcli/auth/libcli_auth.h"
 #include "libcli/ldap/ldap_ndr.h"
 #include "auth/auth.h"
 
 /*
-  make sure the static credentials are not freed
- */
-static int samdb_credentials_destructor(struct cli_credentials *creds)
-{
-       return -1;
-}
-
-/*
-  this returns a static set of system credentials. It is static so
-  that we always get the same pointer in ldb_wrap_connect()
- */
-struct cli_credentials *samdb_credentials(struct tevent_context *event_ctx, 
-                                         struct loadparm_context *lp_ctx)
-{
-       static struct cli_credentials *static_credentials;
-       struct cli_credentials *cred;
-       char *error_string;
-
-       if (static_credentials) {
-               return static_credentials;
-       }
-
-       cred = cli_credentials_init(talloc_autofree_context());
-       if (!cred) {
-               return NULL;
-       }
-       cli_credentials_set_conf(cred, lp_ctx);
-
-       /* We don't want to use krb5 to talk to our samdb - recursion
-        * here would be bad, and this account isn't in the KDC
-        * anyway */
-       cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
-
-       if (!NT_STATUS_IS_OK(cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL, NULL,
-                                                        SECRETS_LDAP_FILTER, &error_string))) {
-               DEBUG(5, ("(normal if no LDAP backend) %s", error_string));
-               /* Perfectly OK - if not against an LDAP backend */
-               talloc_free(cred);
-               return NULL;
-       }
-       static_credentials = cred;
-       talloc_set_destructor(cred, samdb_credentials_destructor);
-       return cred;
-}
-
-/*
-  connect to the SAM database
+  connect to the SAM database specified by URL
   return an opaque context pointer on success, or NULL on failure
  */
-struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx, 
+struct ldb_context *samdb_connect_url(TALLOC_CTX *mem_ctx,
                                  struct tevent_context *ev_ctx,
                                  struct loadparm_context *lp_ctx,
                                  struct auth_session_info *session_info,
-                                 int flags)
+                                 unsigned int flags, const char *url)
 {
        struct ldb_context *ldb;
        struct dsdb_schema *schema;
-       const char *url;
-       struct cli_credentials *credentials;
        int ret;
 
-       url  = lpcfg_sam_url(lp_ctx);
-       credentials = samdb_credentials(ev_ctx, lp_ctx);
-
-       ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, credentials, flags);
+       ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, NULL, flags);
        if (ldb != NULL)
                return talloc_reference(mem_ctx, ldb);
 
-       ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, credentials);
+       ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, NULL);
 
        if (ldb == NULL)
                return NULL;
@@ -131,7 +80,7 @@ struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
                dsdb_make_schema_global(ldb, schema);
        }
 
-       if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, credentials, flags, ldb)) {
+       if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, NULL, flags, ldb)) {
                talloc_free(ldb);
                return NULL;
        }
@@ -140,16 +89,26 @@ struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
 }
 
 
+/*
+  connect to the SAM database
+  return an opaque context pointer on success, or NULL on failure
+ */
+struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
+                                 struct tevent_context *ev_ctx,
+                                 struct loadparm_context *lp_ctx,
+                                 struct auth_session_info *session_info,
+                                 unsigned int flags)
+{
+       return samdb_connect_url(mem_ctx, ev_ctx, lp_ctx, session_info, flags, "sam.ldb");
+}
+
 /****************************************************************************
  Create the SID list for this user.
 ****************************************************************************/
 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
-                              struct tevent_context *ev_ctx, 
                               struct loadparm_context *lp_ctx,
-                              struct dom_sid *user_sid,
-                              struct dom_sid *group_sid, 
-                              unsigned int n_groupSIDs,
-                              struct dom_sid **groupSIDs, 
+                              unsigned int num_sids,
+                              struct dom_sid *sids,
                               uint32_t session_info_flags,
                               struct security_token **token)
 {
@@ -160,22 +119,32 @@ NTSTATUS security_token_create(TALLOC_CTX *mem_ctx,
        ptoken = security_token_initialise(mem_ctx);
        NT_STATUS_HAVE_NO_MEMORY(ptoken);
 
-       ptoken->sids = talloc_array(ptoken, struct dom_sid, n_groupSIDs + 6 /* over-allocate */);
+       ptoken->sids = talloc_array(ptoken, struct dom_sid, num_sids + 6 /* over-allocate */);
        NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
 
-       ptoken->num_sids = 1;
+       ptoken->num_sids = 0;
 
-       ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
-       NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
+       for (i = 0; i < num_sids; i++) {
+               size_t check_sid_idx;
+               for (check_sid_idx = 0;
+                    check_sid_idx < ptoken->num_sids;
+                    check_sid_idx++) {
+                       if (dom_sid_equal(&ptoken->sids[check_sid_idx], &sids[i])) {
+                               break;
+                       }
+               }
 
-       ptoken->sids[PRIMARY_USER_SID_INDEX] = *user_sid;
-       if (!dom_sid_equal(user_sid, group_sid)) {
-               ptoken->sids[PRIMARY_GROUP_SID_INDEX] = *group_sid;
-               ptoken->num_sids++;
+               if (check_sid_idx == ptoken->num_sids) {
+                       ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
+                       NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
+
+                       ptoken->sids[ptoken->num_sids] = sids[i];
+                       ptoken->num_sids++;
+               }
        }
 
        /*
-        * Finally add the "standard" SIDs.
+        * Finally add the "standard" sids.
         * The only difference between guest and "anonymous"
         * is the addition of Authenticated_Users.
         */
@@ -205,33 +174,32 @@ NTSTATUS security_token_create(TALLOC_CTX *mem_ctx,
                ptoken->num_sids++;
        }
 
-       for (i = 0; i < n_groupSIDs; i++) {
-               size_t check_sid_idx;
-               for (check_sid_idx = 1; 
-                    check_sid_idx < ptoken->num_sids; 
-                    check_sid_idx++) {
-                       if (dom_sid_equal(&ptoken->sids[check_sid_idx], groupSIDs[i])) {
-                               break;
-                       }
+       /* The caller may have requested simple privilages, for example if there isn't a local DB */
+       if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
+               /* Shortcuts to prevent recursion and avoid lookups */
+               if (ptoken->sids == NULL) {
+                       ptoken->privilege_mask = 0;
+               } else if (security_token_is_system(ptoken)) {
+                       ptoken->privilege_mask = ~0;
+               } else if (security_token_is_anonymous(ptoken)) {
+                       ptoken->privilege_mask = 0;
+               } else if (security_token_has_builtin_administrators(ptoken)) {
+                       ptoken->privilege_mask = ~0;
+               } else {
+                       /* All other 'users' get a empty priv set so far */
+                       ptoken->privilege_mask = 0;
                }
-
-               if (check_sid_idx == ptoken->num_sids) {
-                       ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
-                       NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
-
-                       ptoken->sids[ptoken->num_sids] = *groupSIDs[i];
-                       ptoken->num_sids++;
+       } else {
+               /* setup the privilege mask for this token */
+               status = samdb_privilege_setup(lp_ctx, ptoken);
+               if (!NT_STATUS_IS_OK(status)) {
+                       talloc_free(ptoken);
+                       DEBUG(1,("Unable to access privileges database\n"));
+                       return status;
                }
        }
 
-       /* setup the privilege mask for this token */
-       status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
-       if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(ptoken);
-               return status;
-       }
-
-       security_token_debug(10, ptoken);
+       security_token_debug(0, 10, ptoken);
 
        *token = ptoken;