4 Copyright (C) Simo Sorce 2004-2006
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2006
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Component: ldb password_hash module
28 * Description: correctly update hash values based on changes to sambaPassword and friends
30 * Author: Andrew Bartlett
34 #include "libcli/ldap/ldap.h"
35 #include "ldb/include/ldb_errors.h"
36 #include "ldb/include/ldb_private.h"
37 #include "librpc/gen_ndr/misc.h"
38 #include "librpc/gen_ndr/samr.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "libcli/security/security.h"
41 #include "system/kerberos.h"
42 #include "auth/kerberos/kerberos.h"
43 #include "system/time.h"
44 #include "dsdb/samdb/samdb.h"
45 #include "dsdb/common/flags.h"
47 #include "dsdb/samdb/ldb_modules/password_modules.h"
49 /* If we have decided there is reason to work on this request, then
50 * setup all the password hash types correctly.
52 * If the administrator doesn't want the sambaPassword stored (set in the
53 * domain and per-account policies) then we must strip that out before
54 * we do the first operation.
56 * Once this is done (which could update anything at all), we
57 * calculate the password hashes.
59 * This function must not only update the ntPwdHash, lmPwdHash and
60 * krb5Key fields, it must also atomicly increment the
61 * msDS-KeyVersionNumber. We should be in a transaction, so all this
62 * should be quite safe...
64 * Finally, if the administrator has requested that a password history
65 * be maintained, then this should also be written out.
71 enum ph_type {PH_ADD, PH_MOD} type;
72 enum ph_step {PH_ADD_SEARCH_DOM, PH_ADD_DO_ADD, PH_MOD_DO_REQ, PH_MOD_SEARCH_SELF, PH_MOD_SEARCH_DOM, PH_MOD_DO_MOD} step;
74 struct ldb_module *module;
75 struct ldb_request *orig_req;
77 struct ldb_request *dom_req;
78 struct ldb_reply *dom_res;
80 struct ldb_request *down_req;
82 struct ldb_request *search_req;
83 struct ldb_reply *search_res;
85 struct ldb_request *mod_req;
87 struct dom_sid *domain_sid;
93 uint_t pwdHistoryLength;
98 static int add_password_hashes(struct ldb_module *module, struct ldb_message *msg, int is_mod)
100 const char *sambaPassword;
101 struct samr_Password tmp_hash;
103 sambaPassword = ldb_msg_find_attr_as_string(msg, "sambaPassword", NULL);
104 if (sambaPassword == NULL) { /* impossible, what happened ?! */
105 return LDB_ERR_CONSTRAINT_VIOLATION;
109 if (ldb_msg_add_empty(msg, "ntPwdHash", LDB_FLAG_MOD_REPLACE) != 0) {
110 return LDB_ERR_OPERATIONS_ERROR;
112 if (ldb_msg_add_empty(msg, "lmPwdHash", LDB_FLAG_MOD_REPLACE) != 0) {
113 return LDB_ERR_OPERATIONS_ERROR;
117 /* compute the new nt and lm hashes */
118 E_md4hash(sambaPassword, tmp_hash.hash);
119 if (samdb_msg_add_hash(module->ldb, msg, msg, "ntPwdHash", &tmp_hash) != 0) {
120 return LDB_ERR_OPERATIONS_ERROR;
123 if (E_deshash(sambaPassword, tmp_hash.hash)) {
124 if (samdb_msg_add_hash(module->ldb, msg, msg, "lmPwdHash", &tmp_hash) != 0) {
125 return LDB_ERR_OPERATIONS_ERROR;
132 static int add_krb5_keys_from_password(struct ldb_module *module, struct ldb_message *msg,
133 struct smb_krb5_context *smb_krb5_context,
134 struct domain_data *domain,
135 const char *samAccountName,
136 const char *user_principal_name,
139 const char *sambaPassword;
140 Principal *salt_principal;
141 krb5_error_code krb5_ret;
146 /* Many, many thanks to lukeh@padl.com for this
147 * algorithm, described in his Nov 10 2004 mail to
148 * samba-technical@samba.org */
150 sambaPassword = ldb_msg_find_attr_as_string(msg, "sambaPassword", NULL);
151 if (sambaPassword == NULL) { /* impossible, what happened ?! */
152 return LDB_ERR_OPERATIONS_ERROR;
156 /* Determine a salting principal */
157 char *name = talloc_strdup(msg, samAccountName);
160 ldb_asprintf_errstring(module->ldb,
161 "password_hash_handle: "
162 "generation of new kerberos keys failed: %s is a computer without a samAccountName",
163 ldb_dn_linearize(msg, msg->dn));
164 return LDB_ERR_OPERATIONS_ERROR;
166 if (name[strlen(name)-1] == '$') {
167 name[strlen(name)-1] = '\0';
169 saltbody = talloc_asprintf(msg, "%s.%s", name, domain->dns_domain);
171 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
173 domain->realm, "host",
175 } else if (user_principal_name) {
177 user_principal_name = talloc_strdup(msg, user_principal_name);
178 if (user_principal_name == NULL) {
179 return LDB_ERR_OPERATIONS_ERROR;
181 p = strchr(user_principal_name, '@');
185 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
187 domain->realm, user_principal_name, NULL);
190 if (!samAccountName) {
191 ldb_asprintf_errstring(module->ldb,
192 "password_hash_handle: "
193 "generation of new kerberos keys failed: %s has no samAccountName",
194 ldb_dn_linearize(msg, msg->dn));
195 return LDB_ERR_OPERATIONS_ERROR;
197 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
199 domain->realm, samAccountName,
204 ldb_asprintf_errstring(module->ldb,
205 "password_hash_handle: "
206 "generation of a saltking principal failed: %s",
207 smb_get_krb5_error_message(smb_krb5_context->krb5_context, krb5_ret, msg));
208 return LDB_ERR_OPERATIONS_ERROR;
211 /* TODO: We may wish to control the encryption types chosen in future */
212 krb5_ret = hdb_generate_key_set_password(smb_krb5_context->krb5_context,
213 salt_principal, sambaPassword, &keys, &num_keys);
214 krb5_free_principal(smb_krb5_context->krb5_context, salt_principal);
217 ldb_asprintf_errstring(module->ldb,
218 "password_hash_handle: "
219 "generation of new kerberos keys failed: %s",
220 smb_get_krb5_error_message(smb_krb5_context->krb5_context, krb5_ret, msg));
221 return LDB_ERR_OPERATIONS_ERROR;
224 /* Walking all the key types generated, transform each
225 * key into an ASN.1 blob
227 for (i=0; i < num_keys; i++) {
234 if (keys[i].key.keytype == ETYPE_ARCFOUR_HMAC_MD5) {
235 /* We might end up doing this below:
236 * This ensures we get the unicode
237 * conversion right. This should also
238 * be fixed in the Heimdal libs */
241 ASN1_MALLOC_ENCODE(Key, buf, buf_size, &keys[i], &len, krb5_ret);
243 return LDB_ERR_OPERATIONS_ERROR;
246 val.data = talloc_memdup(msg, buf, len);
249 if (!val.data || krb5_ret) {
250 hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
251 return LDB_ERR_OPERATIONS_ERROR;
253 ret = ldb_msg_add_value(msg, "krb5Key", &val);
254 if (ret != LDB_SUCCESS) {
255 hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
260 hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
265 static int add_krb5_keys_from_NThash(struct ldb_module *module, struct ldb_message *msg,
266 struct smb_krb5_context *smb_krb5_context)
268 struct samr_Password *ntPwdHash;
269 krb5_error_code krb5_ret;
277 key.salt = NULL; /* No salt for this enc type */
279 ntPwdHash = samdb_result_hash(msg, msg, "ntPwdHash");
280 if (ntPwdHash == NULL) { /* what happened ?! */
281 return LDB_ERR_OPERATIONS_ERROR;
284 krb5_ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
285 ETYPE_ARCFOUR_HMAC_MD5,
286 ntPwdHash->hash, sizeof(ntPwdHash->hash),
289 return LDB_ERR_OPERATIONS_ERROR;
291 ASN1_MALLOC_ENCODE(Key, buf, buf_size, &key, &len, krb5_ret);
293 return LDB_ERR_OPERATIONS_ERROR;
295 krb5_free_keyblock_contents(smb_krb5_context->krb5_context,
298 val.data = talloc_memdup(msg, buf, len);
302 return LDB_ERR_OPERATIONS_ERROR;
304 if (ldb_msg_add_value(msg, "krb5Key", &val) != 0) {
305 return LDB_ERR_OPERATIONS_ERROR;
311 static int set_pwdLastSet(struct ldb_module *module, struct ldb_message *msg, int is_mod)
316 unix_to_nt_time(&now_nt, time(NULL));
319 /* be sure there isn't a 0 value set (eg. coming from the template) */
320 ldb_msg_remove_attr(msg, "pwdLastSet");
322 if (ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_ADD) != 0) {
323 return LDB_ERR_OPERATIONS_ERROR;
327 if (ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_REPLACE) != 0) {
328 return LDB_ERR_OPERATIONS_ERROR;
332 if (samdb_msg_add_uint64(module->ldb, msg, msg, "pwdLastSet", now_nt) != 0) {
333 return LDB_ERR_OPERATIONS_ERROR;
339 static int add_keyVersionNumber(struct ldb_module *module, struct ldb_message *msg, int previous)
342 if (ldb_msg_add_empty(msg, "msDS-KeyVersionNumber", LDB_FLAG_MOD_REPLACE) != 0) {
343 return LDB_ERR_OPERATIONS_ERROR;
346 if (samdb_msg_add_uint(module->ldb, msg, msg, "msDS-KeyVersionNumber", previous+1) != 0) {
347 return LDB_ERR_OPERATIONS_ERROR;
353 static int setPwdHistory(struct ldb_module *module, struct ldb_message *msg, struct ldb_message *old_msg, int hlen)
355 struct samr_Password *nt_hash;
356 struct samr_Password *lm_hash;
357 struct samr_Password *nt_history;
358 struct samr_Password *lm_history;
359 struct samr_Password *new_nt_history;
360 struct samr_Password *new_lm_history;
365 nt_hash = samdb_result_hash(msg, old_msg, "ntPwdHash");
366 lm_hash = samdb_result_hash(msg, old_msg, "lmPwdHash");
368 /* if no previous passwords just return */
369 if (nt_hash == NULL && lm_hash == NULL) return LDB_SUCCESS;
371 nt_hist_len = samdb_result_hashes(msg, old_msg, "sambaNTPwdHistory", &nt_history);
372 lm_hist_len = samdb_result_hashes(msg, old_msg, "sambaLMPwdHistory", &lm_history);
374 /* We might not have an old NT password */
375 new_nt_history = talloc_array(msg, struct samr_Password, hlen);
376 if (new_nt_history == NULL) {
377 return LDB_ERR_OPERATIONS_ERROR;
379 for (i = 0; i < MIN(hlen-1, nt_hist_len); i++) {
380 new_nt_history[i+1] = nt_history[i];
384 new_nt_history[0] = *nt_hash;
386 ZERO_STRUCT(new_nt_history[0]);
388 if (ldb_msg_add_empty(msg, "sambaNTPwdHistory", LDB_FLAG_MOD_REPLACE) != LDB_SUCCESS) {
389 return LDB_ERR_OPERATIONS_ERROR;
391 if (samdb_msg_add_hashes(msg, msg, "sambaNTPwdHistory", new_nt_history, nt_hist_len) != LDB_SUCCESS) {
392 return LDB_ERR_OPERATIONS_ERROR;
396 /* Don't store 'long' passwords in the LM history,
397 but make sure to 'expire' one password off the other end */
398 new_lm_history = talloc_array(msg, struct samr_Password, hlen);
399 if (new_lm_history == NULL) {
400 return LDB_ERR_OPERATIONS_ERROR;
402 for (i = 0; i < MIN(hlen-1, lm_hist_len); i++) {
403 new_lm_history[i+1] = lm_history[i];
407 new_lm_history[0] = *lm_hash;
409 ZERO_STRUCT(new_lm_history[0]);
411 if (ldb_msg_add_empty(msg, "sambaLMPwdHistory", LDB_FLAG_MOD_REPLACE) != LDB_SUCCESS) {
412 return LDB_ERR_OPERATIONS_ERROR;
414 if (samdb_msg_add_hashes(msg, msg, "sambaLMPwdHistory", new_lm_history, lm_hist_len) != LDB_SUCCESS) {
415 return LDB_ERR_OPERATIONS_ERROR;
421 static struct ldb_handle *ph_init_handle(struct ldb_request *req, struct ldb_module *module, enum ph_type type)
423 struct ph_context *ac;
424 struct ldb_handle *h;
426 h = talloc_zero(req, struct ldb_handle);
428 ldb_set_errstring(module->ldb, "Out of Memory");
434 ac = talloc_zero(h, struct ph_context);
436 ldb_set_errstring(module->ldb, "Out of Memory");
441 h->private_data = (void *)ac;
443 h->state = LDB_ASYNC_INIT;
444 h->status = LDB_SUCCESS;
453 static int get_domain_data_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
455 struct ph_context *ac;
457 if (!context || !ares) {
458 ldb_set_errstring(ldb, "NULL Context or Result in callback");
459 return LDB_ERR_OPERATIONS_ERROR;
462 ac = talloc_get_type(context, struct ph_context);
464 /* we are interested only in the single reply (base search) we receive here */
465 if (ares->type == LDB_REPLY_ENTRY) {
466 if (ac->dom_res != NULL) {
467 ldb_set_errstring(ldb, "Too many results");
469 return LDB_ERR_OPERATIONS_ERROR;
471 ac->dom_res = talloc_steal(ac, ares);
479 static int build_domain_data_request(struct ph_context *ac)
481 /* attrs[] is returned from this function in
482 ac->dom_req->op.search.attrs, so it must be static, as
483 otherwise the compiler can put it on the stack */
484 static const char * const attrs[] = { "pwdProperties", "pwdHistoryLength", NULL };
487 ac->dom_req = talloc_zero(ac, struct ldb_request);
488 if (ac->dom_req == NULL) {
489 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
490 return LDB_ERR_OPERATIONS_ERROR;
492 ac->dom_req->operation = LDB_SEARCH;
493 ac->dom_req->op.search.base = ldb_get_default_basedn(ac->module->ldb);
494 ac->dom_req->op.search.scope = LDB_SCOPE_SUBTREE;
496 filter = talloc_asprintf(ac->dom_req, "(&(objectSid=%s)(|(objectClass=domain)(objectClass=builtinDomain)))",
497 ldap_encode_ndr_dom_sid(ac->dom_req, ac->domain_sid));
498 if (filter == NULL) {
499 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
500 talloc_free(ac->dom_req);
501 return LDB_ERR_OPERATIONS_ERROR;
504 ac->dom_req->op.search.tree = ldb_parse_tree(ac->dom_req, filter);
505 if (ac->dom_req->op.search.tree == NULL) {
506 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
507 talloc_free(ac->dom_req);
508 return LDB_ERR_OPERATIONS_ERROR;
510 ac->dom_req->op.search.attrs = attrs;
511 ac->dom_req->controls = NULL;
512 ac->dom_req->context = ac;
513 ac->dom_req->callback = get_domain_data_callback;
514 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->dom_req);
519 static struct domain_data *get_domain_data(struct ldb_module *module, void *ctx, struct ldb_reply *res)
521 struct domain_data *data;
523 struct ph_context *ac;
526 ac = talloc_get_type(ctx, struct ph_context);
528 data = talloc_zero(ac, struct domain_data);
534 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Could not find this user's domain: %s!\n", dom_sid_string(data, ac->domain_sid));
539 data->pwdProperties= samdb_result_uint(res->message, "pwdProperties", 0);
540 data->store_cleartext = data->pwdProperties & DOMAIN_PASSWORD_STORE_CLEARTEXT;
541 data->pwdHistoryLength = samdb_result_uint(res->message, "pwdHistoryLength", 0);
543 /* For a domain DN, this puts things in dotted notation */
544 /* For builtin domains, this will give details for the host,
545 * but that doesn't really matter, as it's just used for salt
546 * and kerberos principals, which don't exist here */
548 tmp = ldb_dn_canonical_string(ctx, res->message->dn);
553 /* But it puts a trailing (or just before 'builtin') / on things, so kill that */
554 p = strchr(tmp, '/');
560 data->dns_domain = strlower_talloc(data, tmp);
561 if (data->dns_domain == NULL) {
562 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
565 data->realm = strupper_talloc(data, tmp);
566 if (data->realm == NULL) {
567 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
575 static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
577 struct ldb_handle *h;
578 struct ph_context *ac;
579 struct ldb_message_element *sambaAttr;
580 struct ldb_message_element *ntAttr;
581 struct ldb_message_element *lmAttr;
584 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
586 if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
587 return ldb_next_request(module, req);
590 /* If the caller is manipulating the local passwords directly, let them pass */
591 if (ldb_dn_compare_base(module->ldb,
592 ldb_dn_explode(req, LOCAL_BASE),
593 req->op.add.message->dn) == 0) {
594 return ldb_next_request(module, req);
597 /* nobody must touch password Histories */
598 if (ldb_msg_find_element(req->op.add.message, "sambaNTPwdHistory") ||
599 ldb_msg_find_element(req->op.add.message, "sambaLMPwdHistory")) {
600 return LDB_ERR_UNWILLING_TO_PERFORM;
603 /* If no part of this ADD touches the sambaPassword, or the NT
604 * or LM hashes, then we don't need to make any changes. */
606 sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
607 ntAttr = ldb_msg_find_element(req->op.mod.message, "ntPwdHash");
608 lmAttr = ldb_msg_find_element(req->op.mod.message, "lmPwdHash");
610 if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
611 return ldb_next_request(module, req);
614 /* if it is not an entry of type person its an error */
615 /* TODO: remove this when sambaPassword will be in schema */
616 if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
617 ldb_set_errstring(module->ldb, "Cannot set a password on entry that does not have objectClass 'person'");
618 return LDB_ERR_OBJECT_CLASS_VIOLATION;
621 /* check sambaPassword is single valued here */
622 /* TODO: remove this when sambaPassword will be single valued in schema */
623 if (sambaAttr && sambaAttr->num_values > 1) {
624 ldb_set_errstring(module->ldb, "mupltiple values for sambaPassword not allowed!\n");
625 return LDB_ERR_CONSTRAINT_VIOLATION;
628 if (ntAttr && (ntAttr->num_values > 1)) {
629 ldb_set_errstring(module->ldb, "mupltiple values for lmPwdHash not allowed!\n");
630 return LDB_ERR_CONSTRAINT_VIOLATION;
632 if (lmAttr && (lmAttr->num_values > 1)) {
633 ldb_set_errstring(module->ldb, "mupltiple values for lmPwdHash not allowed!\n");
634 return LDB_ERR_CONSTRAINT_VIOLATION;
637 if (sambaAttr && sambaAttr->num_values == 0) {
638 ldb_set_errstring(module->ldb, "sambaPassword must have a value!\n");
639 return LDB_ERR_CONSTRAINT_VIOLATION;
642 if (ntAttr && (ntAttr->num_values == 0)) {
643 ldb_set_errstring(module->ldb, "lmPwdHash must have a value!\n");
644 return LDB_ERR_CONSTRAINT_VIOLATION;
646 if (lmAttr && (lmAttr->num_values == 0)) {
647 ldb_set_errstring(module->ldb, "lmPwdHash must have a value!\n");
648 return LDB_ERR_CONSTRAINT_VIOLATION;
651 h = ph_init_handle(req, module, PH_ADD);
653 return LDB_ERR_OPERATIONS_ERROR;
655 ac = talloc_get_type(h->private_data, struct ph_context);
657 /* get user domain data */
658 ac->domain_sid = samdb_result_sid_prefix(ac, req->op.add.message, "objectSid");
659 if (ac->domain_sid == NULL) {
660 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
661 return LDB_ERR_OPERATIONS_ERROR;
664 ret = build_domain_data_request(ac);
665 if (ret != LDB_SUCCESS) {
669 ac->step = PH_ADD_SEARCH_DOM;
673 return ldb_next_request(module, ac->dom_req);
676 static int password_hash_add_do_add(struct ldb_handle *h) {
678 struct ph_context *ac;
679 struct domain_data *domain;
680 struct smb_krb5_context *smb_krb5_context;
681 struct ldb_message_element *sambaAttr;
682 struct ldb_message *msg;
685 ac = talloc_get_type(h->private_data, struct ph_context);
687 domain = get_domain_data(ac->module, ac, ac->dom_res);
688 if (domain == NULL) {
689 return LDB_ERR_OPERATIONS_ERROR;
692 ac->down_req = talloc(ac, struct ldb_request);
693 if (ac->down_req == NULL) {
694 return LDB_ERR_OPERATIONS_ERROR;
697 *(ac->down_req) = *(ac->orig_req);
698 ac->down_req->op.add.message = msg = ldb_msg_copy_shallow(ac->down_req, ac->orig_req->op.add.message);
699 if (ac->down_req->op.add.message == NULL) {
700 return LDB_ERR_OPERATIONS_ERROR;
703 /* Some operations below require kerberos contexts */
704 if (smb_krb5_init_context(ac->down_req, &smb_krb5_context) != 0) {
705 return LDB_ERR_OPERATIONS_ERROR;
708 /* if we have sambaPassword in the original message add the operatio on it here */
709 sambaAttr = ldb_msg_find_element(msg, "sambaPassword");
711 unsigned int user_account_control;
712 ret = add_password_hashes(ac->module, msg, 0);
713 /* we can compute new password hashes from the unicode password */
714 if (ret != LDB_SUCCESS) {
718 /* now add krb5 keys based on unicode password */
719 ret = add_krb5_keys_from_password(ac->module, msg, smb_krb5_context, domain,
720 ldb_msg_find_attr_as_string(msg, "samAccountName", NULL),
721 ldb_msg_find_attr_as_string(msg, "userPrincipalName", NULL),
722 ldb_msg_check_string_attribute(msg, "objectClass", "computer"));
723 if (ret != LDB_SUCCESS) {
727 /* if both the domain properties and the user account controls do not permit
728 * clear text passwords then wipe out the sambaPassword */
729 user_account_control = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
730 if (domain->store_cleartext && (user_account_control & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
731 /* Keep sambaPassword attribute */
733 ldb_msg_remove_attr(msg, "sambaPassword");
737 /* add also krb5 keys based on NT the hash (we might have ntPwdHash, but not the cleartext */
738 ret = add_krb5_keys_from_NThash(ac->module, msg, smb_krb5_context);
739 if (ret != LDB_SUCCESS) {
743 /* don't touch it if a value is set. It could be an incoming samsync */
744 if (ldb_msg_find_attr_as_uint64(msg, "pwdLastSet", 0) == 0) {
745 if (set_pwdLastSet(ac->module, msg, 0) != LDB_SUCCESS) {
746 return LDB_ERR_OPERATIONS_ERROR;
750 /* don't touch it if a value is set. It could be an incoming samsync */
751 if (!ldb_msg_find_element(msg, "msDS-KeyVersionNumber")) {
752 if (add_keyVersionNumber(ac->module, msg, 0) != LDB_SUCCESS) {
753 return LDB_ERR_OPERATIONS_ERROR;
757 h->state = LDB_ASYNC_INIT;
758 h->status = LDB_SUCCESS;
760 ac->step = PH_ADD_DO_ADD;
762 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->down_req);
764 /* perform the operation */
765 return ldb_next_request(ac->module, ac->down_req);
768 static int password_hash_mod_search_self(struct ldb_handle *h);
770 static int password_hash_modify(struct ldb_module *module, struct ldb_request *req)
772 struct ldb_handle *h;
773 struct ph_context *ac;
774 struct ldb_message_element *sambaAttr;
775 struct ldb_message_element *ntAttr;
776 struct ldb_message_element *lmAttr;
777 struct ldb_message *msg;
779 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
781 if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
782 return ldb_next_request(module, req);
785 /* If the caller is manipulating the local passwords directly, let them pass */
786 if (ldb_dn_compare_base(module->ldb,
787 ldb_dn_explode(req, LOCAL_BASE),
788 req->op.mod.message->dn) == 0) {
789 return ldb_next_request(module, req);
792 /* nobody must touch password Histories */
793 if (ldb_msg_find_element(req->op.mod.message, "sambaNTPwdHistory") ||
794 ldb_msg_find_element(req->op.mod.message, "sambaLMPwdHistory")) {
795 return LDB_ERR_UNWILLING_TO_PERFORM;
798 sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
799 ntAttr = ldb_msg_find_element(req->op.mod.message, "ntPwdHash");
800 lmAttr = ldb_msg_find_element(req->op.mod.message, "lmPwdHash");
802 /* check passwords are single valued here */
803 /* TODO: remove this when passwords will be single valued in schema */
804 if (sambaAttr && (sambaAttr->num_values > 1)) {
805 return LDB_ERR_CONSTRAINT_VIOLATION;
807 if (ntAttr && (ntAttr->num_values > 1)) {
808 return LDB_ERR_CONSTRAINT_VIOLATION;
810 if (lmAttr && (lmAttr->num_values > 1)) {
811 return LDB_ERR_CONSTRAINT_VIOLATION;
814 /* If no part of this touches the sambaPassword OR ntPwdHash and/or lmPwdHash, then we don't
815 * need to make any changes. For password changes/set there should
816 * be a 'delete' or a 'modify' on this attribute. */
817 /* If the only operation is the deletion of the passwords then go on */
818 if ( ((!sambaAttr) || ((sambaAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))
819 && ((!ntAttr) || ((ntAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))
820 && ((!lmAttr) || ((lmAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE)) ) {
822 return ldb_next_request(module, req);
825 h = ph_init_handle(req, module, PH_MOD);
827 return LDB_ERR_OPERATIONS_ERROR;
829 ac = talloc_get_type(h->private_data, struct ph_context);
831 /* return or own handle to deal with this call */
834 /* prepare the first operation */
835 ac->down_req = talloc_zero(ac, struct ldb_request);
836 if (ac->down_req == NULL) {
837 ldb_set_errstring(module->ldb, "Out of memory!");
838 return LDB_ERR_OPERATIONS_ERROR;
841 *(ac->down_req) = *req; /* copy the request */
843 /* use a new message structure so that we can modify it */
844 ac->down_req->op.mod.message = msg = ldb_msg_copy_shallow(ac->down_req, req->op.mod.message);
846 /* - remove any imodification to the password from the first commit
847 * we will make the real modification later */
848 if (sambaAttr) ldb_msg_remove_attr(msg, "sambaPassword");
849 if (ntAttr) ldb_msg_remove_attr(msg, "ntPwdHash");
850 if (lmAttr) ldb_msg_remove_attr(msg, "lmPwdHash");
852 /* if there was nothing else to be modify skip to next step */
853 if (msg->num_elements == 0) {
854 talloc_free(ac->down_req);
856 return password_hash_mod_search_self(h);
859 ac->down_req->context = NULL;
860 ac->down_req->callback = NULL;
862 ac->step = PH_MOD_DO_REQ;
864 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
866 return ldb_next_request(module, ac->down_req);
869 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
871 struct ph_context *ac;
873 if (!context || !ares) {
874 ldb_set_errstring(ldb, "NULL Context or Result in callback");
875 return LDB_ERR_OPERATIONS_ERROR;
878 ac = talloc_get_type(context, struct ph_context);
880 /* we are interested only in the single reply (base search) we receive here */
881 if (ares->type == LDB_REPLY_ENTRY) {
882 if (ac->search_res != NULL) {
883 ldb_set_errstring(ldb, "Too many results");
885 return LDB_ERR_OPERATIONS_ERROR;
888 /* if it is not an entry of type person this is an error */
889 /* TODO: remove this when sambaPassword will be in schema */
890 if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
891 ldb_set_errstring(ldb, "Object class violation");
893 return LDB_ERR_OBJECT_CLASS_VIOLATION;
896 ac->search_res = talloc_steal(ac, ares);
904 static int password_hash_mod_search_self(struct ldb_handle *h) {
906 struct ph_context *ac;
907 static const char * const attrs[] = { "userAccountControl", "sambaLMPwdHistory",
909 "objectSid", "msDS-KeyVersionNumber",
910 "objectClass", "userPrincipalName",
912 "lmPwdHash", "ntPwdHash",
915 ac = talloc_get_type(h->private_data, struct ph_context);
917 /* prepare the search operation */
918 ac->search_req = talloc_zero(ac, struct ldb_request);
919 if (ac->search_req == NULL) {
920 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
921 return LDB_ERR_OPERATIONS_ERROR;
924 ac->search_req->operation = LDB_SEARCH;
925 ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
926 ac->search_req->op.search.scope = LDB_SCOPE_BASE;
927 ac->search_req->op.search.tree = ldb_parse_tree(ac->module->ldb, NULL);
928 if (ac->search_req->op.search.tree == NULL) {
929 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
930 return LDB_ERR_OPERATIONS_ERROR;
932 ac->search_req->op.search.attrs = attrs;
933 ac->search_req->controls = NULL;
934 ac->search_req->context = ac;
935 ac->search_req->callback = get_self_callback;
936 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
938 ac->step = PH_MOD_SEARCH_SELF;
940 return ldb_next_request(ac->module, ac->search_req);
943 static int password_hash_mod_search_dom(struct ldb_handle *h) {
945 struct ph_context *ac;
948 ac = talloc_get_type(h->private_data, struct ph_context);
950 /* get object domain sid */
951 ac->domain_sid = samdb_result_sid_prefix(ac, ac->search_res->message, "objectSid");
952 if (ac->domain_sid == NULL) {
953 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
954 return LDB_ERR_OPERATIONS_ERROR;
957 /* get user domain data */
958 ret = build_domain_data_request(ac);
959 if (ret != LDB_SUCCESS) {
963 ac->step = PH_MOD_SEARCH_DOM;
965 return ldb_next_request(ac->module, ac->dom_req);
968 static int password_hash_mod_do_mod(struct ldb_handle *h) {
970 struct ph_context *ac;
971 struct domain_data *domain;
972 struct smb_krb5_context *smb_krb5_context;
973 struct ldb_message_element *sambaAttr;
974 struct ldb_message *msg;
977 BOOL added_hashes = False;
979 ac = talloc_get_type(h->private_data, struct ph_context);
981 domain = get_domain_data(ac->module, ac, ac->dom_res);
982 if (domain == NULL) {
983 return LDB_ERR_OPERATIONS_ERROR;
986 ac->mod_req = talloc(ac, struct ldb_request);
987 if (ac->mod_req == NULL) {
988 return LDB_ERR_OPERATIONS_ERROR;
991 *(ac->mod_req) = *(ac->orig_req);
993 /* use a new message structure so that we can modify it */
994 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
996 return LDB_ERR_OPERATIONS_ERROR;
1000 msg->dn = ac->orig_req->op.mod.message->dn;
1002 /* Some operations below require kerberos contexts */
1003 if (smb_krb5_init_context(ac->mod_req, &smb_krb5_context) != 0) {
1004 return LDB_ERR_OPERATIONS_ERROR;
1007 /* we are going to replace the existing krb5key or delete it */
1008 if (ldb_msg_add_empty(msg, "krb5key", LDB_FLAG_MOD_REPLACE) != 0) {
1009 return LDB_ERR_OPERATIONS_ERROR;
1012 /* if we have sambaPassword in the original message add the operation on it here */
1013 sambaAttr = ldb_msg_find_element(ac->orig_req->op.mod.message, "sambaPassword");
1016 if (ldb_msg_add(msg, sambaAttr, sambaAttr->flags) != 0) {
1017 return LDB_ERR_OPERATIONS_ERROR;
1020 /* if we are actually settting a new unicode password,
1021 * use it to generate the password hashes */
1022 if (((sambaAttr->flags & LDB_FLAG_MOD_MASK) != LDB_FLAG_MOD_DELETE)
1023 && (sambaAttr->num_values == 1)) {
1024 /* we can compute new password hashes from the unicode password */
1025 ret = add_password_hashes(ac->module, msg, 1);
1026 if (ret != LDB_SUCCESS) {
1030 added_hashes = True;
1032 /* now add krb5 keys based on unicode password */
1033 ret = add_krb5_keys_from_password(ac->module, msg, smb_krb5_context, domain,
1034 ldb_msg_find_attr_as_string(ac->search_res->message, "samAccountName", NULL),
1035 ldb_msg_find_attr_as_string(ac->search_res->message, "userPrincipalName", NULL),
1036 ldb_msg_check_string_attribute(ac->search_res->message, "objectClass", "computer"));
1038 if (ret != LDB_SUCCESS) {
1042 /* if the domain properties or the user account controls do not permit
1043 * clear text passwords then wipe out the sambaPassword */
1044 if (domain->store_cleartext &&
1045 (ldb_msg_find_attr_as_uint(ac->search_res->message, "userAccountControl", 0) & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
1046 /* Keep sambaPassword attribute */
1048 ldb_msg_remove_attr(msg, "sambaPassword");
1054 /* if we didn't create the hashes above, try using values supplied directly */
1055 if (!added_hashes) {
1056 struct ldb_message_element *el;
1058 el = ldb_msg_find_element(ac->orig_req->op.mod.message, "ntPwdHash");
1059 if (ldb_msg_add(msg, el, el->flags) != 0) {
1060 return LDB_ERR_OPERATIONS_ERROR;
1063 el = ldb_msg_find_element(ac->orig_req->op.mod.message, "lmPwdHash");
1064 if (ldb_msg_add(msg, el, el->flags) != 0) {
1065 return LDB_ERR_OPERATIONS_ERROR;
1069 /* add also krb5 keys based on NT the hash */
1070 if (add_krb5_keys_from_NThash(ac->module, msg, smb_krb5_context) != LDB_SUCCESS) {
1071 return LDB_ERR_OPERATIONS_ERROR;
1074 /* set change time */
1075 if (set_pwdLastSet(ac->module, msg, 1) != LDB_SUCCESS) {
1076 return LDB_ERR_OPERATIONS_ERROR;
1079 /* don't touch it if a value is set. It could be an incoming samsync */
1080 if (!ldb_msg_find_element(ac->orig_req->op.mod.message,
1081 "msDS-KeyVersionNumber")) {
1082 if (add_keyVersionNumber(ac->module, msg,
1083 ldb_msg_find_attr_as_uint(ac->search_res->message,
1084 "msDS-KeyVersionNumber", 0)
1086 return LDB_ERR_OPERATIONS_ERROR;
1090 if ((phlen = samdb_result_uint(ac->dom_res->message, "pwdHistoryLength", 0)) > 0) {
1091 if (setPwdHistory(ac->module, msg, ac->search_res->message, phlen) != LDB_SUCCESS) {
1092 return LDB_ERR_OPERATIONS_ERROR;
1096 h->state = LDB_ASYNC_INIT;
1097 h->status = LDB_SUCCESS;
1099 ac->step = PH_MOD_DO_MOD;
1101 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
1103 /* perform the search */
1104 return ldb_next_request(ac->module, ac->mod_req);
1107 static int ph_wait(struct ldb_handle *handle) {
1108 struct ph_context *ac;
1111 if (!handle || !handle->private_data) {
1112 return LDB_ERR_OPERATIONS_ERROR;
1115 if (handle->state == LDB_ASYNC_DONE) {
1116 return handle->status;
1119 handle->state = LDB_ASYNC_PENDING;
1120 handle->status = LDB_SUCCESS;
1122 ac = talloc_get_type(handle->private_data, struct ph_context);
1125 case PH_ADD_SEARCH_DOM:
1126 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1128 if (ret != LDB_SUCCESS) {
1129 handle->status = ret;
1132 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1133 handle->status = ac->dom_req->handle->status;
1137 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1141 /* domain search done, go on */
1142 return password_hash_add_do_add(handle);
1145 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1147 if (ret != LDB_SUCCESS) {
1148 handle->status = ret;
1151 if (ac->down_req->handle->status != LDB_SUCCESS) {
1152 handle->status = ac->down_req->handle->status;
1156 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1163 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1165 if (ret != LDB_SUCCESS) {
1166 handle->status = ret;
1169 if (ac->down_req->handle->status != LDB_SUCCESS) {
1170 handle->status = ac->down_req->handle->status;
1174 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1178 /* non-password mods done, go on */
1179 return password_hash_mod_search_self(handle);
1181 case PH_MOD_SEARCH_SELF:
1182 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1184 if (ret != LDB_SUCCESS) {
1185 handle->status = ret;
1188 if (ac->search_req->handle->status != LDB_SUCCESS) {
1189 handle->status = ac->search_req->handle->status;
1193 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1197 /* self search done, go on */
1198 return password_hash_mod_search_dom(handle);
1200 case PH_MOD_SEARCH_DOM:
1201 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1203 if (ret != LDB_SUCCESS) {
1204 handle->status = ret;
1207 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1208 handle->status = ac->dom_req->handle->status;
1212 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1216 /* domain search done, go on */
1217 return password_hash_mod_do_mod(handle);
1220 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
1222 if (ret != LDB_SUCCESS) {
1223 handle->status = ret;
1226 if (ac->mod_req->handle->status != LDB_SUCCESS) {
1227 handle->status = ac->mod_req->handle->status;
1231 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
1238 ret = LDB_ERR_OPERATIONS_ERROR;
1245 handle->state = LDB_ASYNC_DONE;
1249 static int ph_wait_all(struct ldb_handle *handle) {
1253 while (handle->state != LDB_ASYNC_DONE) {
1254 ret = ph_wait(handle);
1255 if (ret != LDB_SUCCESS) {
1260 return handle->status;
1263 static int password_hash_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1265 if (type == LDB_WAIT_ALL) {
1266 return ph_wait_all(handle);
1268 return ph_wait(handle);
1272 static const struct ldb_module_ops password_hash_ops = {
1273 .name = "password_hash",
1274 .add = password_hash_add,
1275 .modify = password_hash_modify,
1276 .wait = password_hash_wait
1280 int password_hash_module_init(void)
1282 return ldb_register_module(&password_hash_ops);