s4-dsdb: use GUID_to_ndr_blob()
[kai/samba-autobuild/.git] / source4 / dsdb / common / util.c
index f86a842fb4aeb244dc5df934e744c533c0b69dfe..bfb2f0caa5a213f18a4bcc8c7767b58f90c7a946 100644 (file)
@@ -37,6 +37,7 @@
 #include "param/param.h"
 #include "libcli/auth/libcli_auth.h"
 #include "librpc/gen_ndr/ndr_drsblobs.h"
+#include "system/locale.h"
 
 /*
   search the sam for the specified attributes in a specific domain, filter on
@@ -186,18 +187,19 @@ struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
   return the count of the number of records in the sam matching the query
 */
 int samdb_search_count(struct ldb_context *sam_ldb,
-                      TALLOC_CTX *mem_ctx,
                       struct ldb_dn *basedn,
-                      const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
+                      const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
 {
        va_list ap;
        struct ldb_message **res;
-       const char * const attrs[] = { NULL };
+       const char *attrs[] = { NULL };
        int ret;
+       TALLOC_CTX *tmp_ctx = talloc_new(sam_ldb);
 
        va_start(ap, format);
-       ret = gendb_search_v(sam_ldb, mem_ctx, basedn, &res, attrs, format, ap);
+       ret = gendb_search_v(sam_ldb, tmp_ctx, basedn, &res, attrs, format, ap);
        va_end(ap);
+       talloc_free(tmp_ctx);
 
        return ret;
 }
@@ -394,22 +396,15 @@ struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_messa
 struct GUID samdb_result_guid(const struct ldb_message *msg, const char *attr)
 {
        const struct ldb_val *v;
-       enum ndr_err_code ndr_err;
        struct GUID guid;
-       TALLOC_CTX *mem_ctx;
-
-       ZERO_STRUCT(guid);
+       NTSTATUS status;
 
        v = ldb_msg_find_ldb_val(msg, attr);
        if (!v) return guid;
 
-       mem_ctx = talloc_named_const(NULL, 0, "samdb_result_guid");
-       if (!mem_ctx) return guid;
-       ndr_err = ndr_pull_struct_blob(v, mem_ctx, NULL, &guid,
-                                      (ndr_pull_flags_fn_t)ndr_pull_GUID);
-       talloc_free(mem_ctx);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               return guid;
+       status = GUID_from_ndr_blob(v, &guid);
+       if (!NT_STATUS_IS_OK(status)) {
+               return GUID_zero();
        }
 
        return guid;
@@ -563,9 +558,8 @@ struct samr_Password *samdb_result_hash(TALLOC_CTX *mem_ctx, const struct ldb_me
 uint_t samdb_result_hashes(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, 
                           const char *attr, struct samr_Password **hashes)
 {
-       uint_t count = 0;
+       uint_t count, i;
        const struct ldb_val *val = ldb_msg_find_ldb_val(msg, attr);
-       int i;
 
        *hashes = NULL;
        if (!val) {
@@ -1027,6 +1021,24 @@ struct ldb_dn *samdb_schema_dn(struct ldb_context *sam_ctx)
        return ldb_get_schema_basedn(sam_ctx);
 }
 
+struct ldb_dn *samdb_aggregate_schema_dn(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx) 
+{
+       struct ldb_dn *schema_dn = ldb_get_schema_basedn(sam_ctx);
+       struct ldb_dn *aggregate_dn;
+       if (!schema_dn) {
+               return NULL;
+       }
+
+       aggregate_dn = ldb_dn_copy(mem_ctx, schema_dn);
+       if (!aggregate_dn) {
+               return NULL;
+       }
+       if (!ldb_dn_add_child_fmt(aggregate_dn, "CN=Aggregate")) {
+               return NULL;
+       }
+       return aggregate_dn;
+}
+
 struct ldb_dn *samdb_root_dn(struct ldb_context *sam_ctx) 
 {
        return ldb_get_root_basedn(sam_ctx);
@@ -1432,6 +1444,16 @@ struct ldb_dn *samdb_server_site_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx
        return server_site_dn;
 }
 
+const char *samdb_server_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
+{
+       const struct ldb_val *val = ldb_dn_get_rdn_val(samdb_server_site_dn(ldb, mem_ctx));
+
+       if (val != NULL)
+               return (const char *) val->data;
+       else
+               return NULL;
+}
+
 /*
   work out if we are the PDC for the domain of the current open ldb
 */
@@ -1564,20 +1586,43 @@ int samdb_search_for_parent_domain(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
 
 
 /*
-  set the user password using plaintext, obeying any user or domain
-  password restrictions
+ * Performs checks on a user password (plaintext UNIX format - attribute
+ * "password"). The remaining parameters have to be extracted from the domain
+ * object in the AD.
+ *
+ * Result codes from "enum samr_ValidationStatus" (consider "samr.idl")
+ */
+enum samr_ValidationStatus samdb_check_password(const DATA_BLOB *password,
+                                               const uint32_t pwdProperties,
+                                               const uint32_t minPwdLength)
+{
+       /* checks if the "minPwdLength" property is satisfied */
+       if (minPwdLength > password->length)
+               return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
 
-  note that this function doesn't actually store the result in the
-  database, it just fills in the "mod" structure with ldb modify
-  elements to setup the correct change when samdb_replace() is
-  called. This allows the caller to combine the change with other
-  changes (as is needed by some of the set user info levels)
+       /* checks the password complexity */
+       if (((pwdProperties & DOMAIN_PASSWORD_COMPLEX) != 0)
+                       && (password->data != NULL)
+                       && (!check_password_quality((const char *) password->data)))
+               return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
 
-  The caller should probably have a transaction wrapping this
-*/
+       return SAMR_VALIDATION_STATUS_SUCCESS;
+}
+
+/*
+ * Sets the user password using plaintext UTF16 (attribute "new_password") or
+ * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
+ * as parameter if it's a user change or not ("userChange"). The "rejectReason"
+ * gives some more informations if the changed failed.
+ *
+ * The caller should have a LDB transaction wrapping this.
+ *
+ * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
+ *   NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
+ *   NT_STATUS_PASSWORD_RESTRICTION
+ */
 NTSTATUS samdb_set_password(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
-                           struct ldb_dn *user_dn,
-                           struct ldb_dn *domain_dn,
+                           struct ldb_dn *user_dn, struct ldb_dn *domain_dn,
                            struct ldb_message *mod,
                            const DATA_BLOB *new_password,
                            struct samr_Password *param_lmNewHash,
@@ -1841,16 +1886,24 @@ NTSTATUS samdb_set_password(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
 
 
 /*
-  set the user password using plaintext, obeying any user or domain
-  password restrictions
-
-  This wrapper function takes a SID as input, rather than a user DN,
-  and actually performs the password change
-
-*/
-NTSTATUS samdb_set_password_sid(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
+ * Sets the user password using plaintext UTF16 (attribute "new_password") or
+ * LM (attribute "lmNewHash") or NT (attribute "ntNewHash") hash. Also pass
+ * as parameter if it's a user change or not ("userChange"). The "rejectReason"
+ * gives some more informations if the changed failed.
+ *
+ * This wrapper function for "samdb_set_password" takes a SID as input rather
+ * than a user DN.
+ *
+ * This call encapsulates a new LDB transaction for changing the password;
+ * therefore the user hasn't to start a new one.
+ *
+ * Results: NT_STATUS_OK, NT_STATUS_INTERNAL_DB_CORRUPTION,
+ *   NT_STATUS_INVALID_PARAMETER, NT_STATUS_UNSUCCESSFUL,
+ *   NT_STATUS_PASSWORD_RESTRICTION
+ */
+NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                                const struct dom_sid *user_sid,
-                               const DATA_BLOB *new_pass,
+                               const DATA_BLOB *new_password,
                                struct samr_Password *lmNewHash, 
                                struct samr_Password *ntNewHash,
                                bool user_change,
@@ -1862,17 +1915,17 @@ NTSTATUS samdb_set_password_sid(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
        struct ldb_message *msg;
        int ret;
 
-       ret = ldb_transaction_start(ctx);
-       if (ret) {
-               DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ctx)));
+       ret = ldb_transaction_start(ldb);
+       if (ret != LDB_SUCCESS) {
+               DEBUG(1, ("Failed to start transaction: %s\n", ldb_errstring(ldb)));
                return NT_STATUS_TRANSACTION_ABORTED;
        }
 
-       user_dn = samdb_search_dn(ctx, mem_ctx, NULL, 
+       user_dn = samdb_search_dn(ldb, mem_ctx, NULL,
                                  "(&(objectSid=%s)(objectClass=user))", 
                                  ldap_encode_ndr_dom_sid(mem_ctx, user_sid));
        if (!user_dn) {
-               ldb_transaction_cancel(ctx);
+               ldb_transaction_cancel(ldb);
                DEBUG(3, ("samdb_set_password_sid: SID %s not found in samdb, returning NO_SUCH_USER\n",
                          dom_sid_string(mem_ctx, user_sid)));
                return NT_STATUS_NO_SUCH_USER;
@@ -1880,46 +1933,45 @@ NTSTATUS samdb_set_password_sid(struct ldb_context *ctx, TALLOC_CTX *mem_ctx,
 
        msg = ldb_msg_new(mem_ctx);
        if (msg == NULL) {
-               ldb_transaction_cancel(ctx);
+               ldb_transaction_cancel(ldb);
                return NT_STATUS_NO_MEMORY;
        }
 
        msg->dn = ldb_dn_copy(msg, user_dn);
        if (!msg->dn) {
-               ldb_transaction_cancel(ctx);
+               ldb_transaction_cancel(ldb);
                return NT_STATUS_NO_MEMORY;
        }
 
-       nt_status = samdb_set_password(ctx, mem_ctx,
+       nt_status = samdb_set_password(ldb, mem_ctx,
                                       user_dn, NULL,
-                                      msg, new_pass
+                                      msg, new_password,
                                       lmNewHash, ntNewHash,
                                       user_change, /* This is a password set, not change */
                                       reject_reason, _dominfo);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               ldb_transaction_cancel(ctx);
+               ldb_transaction_cancel(ldb);
                return nt_status;
        }
 
        /* modify the samdb record */
-       ret = samdb_replace(ctx, mem_ctx, msg);
-       if (ret != 0) {
-               ldb_transaction_cancel(ctx);
+       ret = samdb_replace(ldb, mem_ctx, msg);
+       if (ret != LDB_SUCCESS) {
+               ldb_transaction_cancel(ldb);
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       ret = ldb_transaction_commit(ctx);
-       if (ret != 0) {
+       ret = ldb_transaction_commit(ldb);
+       if (ret != LDB_SUCCESS) {
                DEBUG(0,("Failed to commit transaction to change password on %s: %s\n",
                         ldb_dn_get_linearized(msg->dn),
-                        ldb_errstring(ctx)));
+                        ldb_errstring(ldb)));
                return NT_STATUS_TRANSACTION_ABORTED;
        }
        return NT_STATUS_OK;
 }
 
 
-
 NTSTATUS samdb_create_foreign_security_principal(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx, 
                                                 struct dom_sid *sid, struct ldb_dn **ret_dn) 
 {
@@ -2226,6 +2278,43 @@ int dsdb_find_guid_by_dn(struct ldb_context *ldb,
        return LDB_SUCCESS;
 }
 
+
+
+/*
+ adds the given GUID to the given ldb_message. This value is added
+ for the given attr_name (may be either "objectGUID" or "parentGUID").
+ */
+int dsdb_msg_add_guid(struct ldb_message *msg,
+               struct GUID *guid,
+               const char *attr_name)
+{
+       int ret;
+       struct ldb_val v;
+       NTSTATUS status;
+       TALLOC_CTX *tmp_ctx =  talloc_init("dsdb_msg_add_guid");
+
+       status = GUID_to_ndr_blob(guid, tmp_ctx, &v);
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = LDB_ERR_OPERATIONS_ERROR;
+               goto done;
+       }
+
+       ret = ldb_msg_add_steal_value(msg, attr_name, &v);
+       if (ret != LDB_SUCCESS) {
+               DEBUG(4,(__location__ ": Failed to add %s to the message\n",
+                                        attr_name));
+               goto done;
+       }
+
+       ret = LDB_SUCCESS;
+
+done:
+       talloc_free(tmp_ctx);
+       return ret;
+
+}
+
+
 /*
   use a DN to find a SID
  */
@@ -2528,3 +2617,110 @@ int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
 {
        return GUID_compare(&c1->source_dsa_invocation_id, &c2->source_dsa_invocation_id);
 }
+
+/*
+  see if we are a RODC
+
+  TODO: This should take a sam_ctx, and lookup the right object (with
+  a cache)
+*/
+bool samdb_rodc(struct loadparm_context *lp_ctx)
+{
+       return lp_parm_bool(lp_ctx, NULL, "repl", "RODC", false);
+}
+
+
+/*
+  return NTDS options flags. See MS-ADTS 7.1.1.2.2.1.2.1.1 
+
+  flags are DS_NTDS_OPTION_*
+*/
+int samdb_ntds_options(struct ldb_context *ldb, uint32_t *options)
+{
+       TALLOC_CTX *tmp_ctx;
+       const char *attrs[] = { "options", NULL };
+       int ret;
+       struct ldb_result *res;
+
+       tmp_ctx = talloc_new(ldb);
+       if (tmp_ctx == NULL) {
+               goto failed;
+       }
+
+       ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb), LDB_SCOPE_BASE, attrs, NULL);
+       if (ret) {
+               goto failed;
+       }
+
+       if (res->count != 1) {
+               goto failed;
+       }
+
+       *options = samdb_result_uint(res->msgs[0], "options", 0);
+
+       talloc_free(tmp_ctx);
+
+       return LDB_SUCCESS;
+
+failed:
+       DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
+       talloc_free(tmp_ctx);
+       return LDB_ERR_NO_SUCH_OBJECT;
+}
+
+/*
+ * Function which generates a "lDAPDisplayName" attribute from a "CN" one.
+ * Algorithm implemented according to MS-ADTS 3.1.1.2.3.4
+ */
+const char *samdb_cn_to_lDAPDisplayName(TALLOC_CTX *mem_ctx, const char *cn)
+{
+       char **tokens, *ret;
+       size_t i;
+
+       tokens = str_list_make(mem_ctx, cn, " -_");
+       if (tokens == NULL)
+               return NULL;
+
+       /* "tolower()" and "toupper()" should also work properly on 0x00 */
+       tokens[0][0] = tolower(tokens[0][0]);
+       for (i = 1; i < str_list_length((const char **)tokens); i++)
+               tokens[i][0] = toupper(tokens[i][0]);
+
+       ret = talloc_strdup(mem_ctx, tokens[0]);
+       for (i = 1; i < str_list_length((const char **)tokens); i++)
+               ret = talloc_asprintf_append_buffer(ret, "%s", tokens[i]);
+
+       talloc_free(tokens);
+
+       return ret;
+}
+
+/*
+  return domain functional level
+  returns DS_DOMAIN_FUNCTION_*
+ */
+int dsdb_functional_level(struct ldb_context *ldb)
+{
+       int *domainFunctionality =
+               talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
+       if (!domainFunctionality) {
+               DEBUG(0,(__location__ ": WARNING: domainFunctionality not setup\n"));
+               return DS_DOMAIN_FUNCTION_2000;
+       }
+       return *domainFunctionality;
+}
+
+/*
+  return a GUID from a extended DN structure
+ */
+NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid)
+{
+       const struct ldb_val *v;
+
+       v = ldb_dn_get_extended_component(dn, "GUID");
+       if (v == NULL) {
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       return GUID_from_ndr_blob(v, guid);
+}