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;
92 uint_t pwdHistoryLength;
97 static int add_password_hashes(struct ldb_module *module, struct ldb_message *msg, int is_mod)
99 const char *sambaPassword;
100 struct samr_Password tmp_hash;
102 sambaPassword = ldb_msg_find_attr_as_string(msg, "sambaPassword", NULL);
103 if (sambaPassword == NULL) { /* impossible, what happened ?! */
104 return LDB_ERR_OPERATIONS_ERROR;
108 if (ldb_msg_add_empty(msg, "ntPwdHash", LDB_FLAG_MOD_REPLACE) != 0) {
109 return LDB_ERR_OPERATIONS_ERROR;
111 if (ldb_msg_add_empty(msg, "lmPwdHash", LDB_FLAG_MOD_REPLACE) != 0) {
112 return LDB_ERR_OPERATIONS_ERROR;
116 /* compute the new nt and lm hashes */
117 E_md4hash(sambaPassword, tmp_hash.hash);
118 if (samdb_msg_add_hash(module->ldb, msg, msg, "ntPwdHash", &tmp_hash) != 0) {
119 return LDB_ERR_OPERATIONS_ERROR;
122 if (E_deshash(sambaPassword, tmp_hash.hash)) {
123 if (samdb_msg_add_hash(module->ldb, msg, msg, "lmPwdHash", &tmp_hash) != 0) {
124 return LDB_ERR_OPERATIONS_ERROR;
131 static int add_krb5_keys_from_password(struct ldb_module *module, struct ldb_message *msg,
132 struct smb_krb5_context *smb_krb5_context,
133 struct domain_data *domain,
134 const char *samAccountName,
135 const char *user_principal_name,
138 const char *sambaPassword;
139 Principal *salt_principal;
140 krb5_error_code krb5_ret;
145 /* Many, many thanks to lukeh@padl.com for this
146 * algorithm, described in his Nov 10 2004 mail to
147 * samba-technical@samba.org */
149 sambaPassword = ldb_msg_find_attr_as_string(msg, "sambaPassword", NULL);
150 if (sambaPassword == NULL) { /* impossible, what happened ?! */
151 return LDB_ERR_OPERATIONS_ERROR;
155 /* Determine a salting principal */
156 char *name = talloc_strdup(msg, samAccountName);
159 ldb_asprintf_errstring(module->ldb,
160 "password_hash_handle: "
161 "generation of new kerberos keys failed: %s is a computer without a samAccountName",
162 ldb_dn_linearize(msg, msg->dn));
163 return LDB_ERR_OPERATIONS_ERROR;
165 if (name[strlen(name)-1] == '$') {
166 name[strlen(name)-1] = '\0';
168 saltbody = talloc_asprintf(msg, "%s.%s", name, domain->dns_domain);
170 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
172 domain->realm, "host",
174 } else if (user_principal_name) {
176 user_principal_name = talloc_strdup(msg, user_principal_name);
177 if (user_principal_name == NULL) {
178 return LDB_ERR_OPERATIONS_ERROR;
180 p = strchr(user_principal_name, '@');
184 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
186 domain->realm, user_principal_name, NULL);
189 if (!samAccountName) {
190 ldb_asprintf_errstring(module->ldb,
191 "password_hash_handle: "
192 "generation of new kerberos keys failed: %s has no samAccountName",
193 ldb_dn_linearize(msg, msg->dn));
194 return LDB_ERR_OPERATIONS_ERROR;
196 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
198 domain->realm, samAccountName,
203 ldb_asprintf_errstring(module->ldb,
204 "password_hash_handle: "
205 "generation of a saltking principal failed: %s",
206 smb_get_krb5_error_message(smb_krb5_context->krb5_context, krb5_ret, msg));
207 return LDB_ERR_OPERATIONS_ERROR;
210 /* TODO: We may wish to control the encryption types chosen in future */
211 krb5_ret = hdb_generate_key_set_password(smb_krb5_context->krb5_context,
212 salt_principal, sambaPassword, &keys, &num_keys);
213 krb5_free_principal(smb_krb5_context->krb5_context, salt_principal);
216 ldb_asprintf_errstring(module->ldb,
217 "password_hash_handle: "
218 "generation of new kerberos keys failed: %s",
219 smb_get_krb5_error_message(smb_krb5_context->krb5_context, krb5_ret, msg));
220 return LDB_ERR_OPERATIONS_ERROR;
223 /* Walking all the key types generated, transform each
224 * key into an ASN.1 blob
226 for (i=0; i < num_keys; i++) {
233 if (keys[i].key.keytype == ETYPE_ARCFOUR_HMAC_MD5) {
234 /* We might end up doing this below:
235 * This ensures we get the unicode
236 * conversion right. This should also
237 * be fixed in the Heimdal libs */
240 ASN1_MALLOC_ENCODE(Key, buf, buf_size, &keys[i], &len, krb5_ret);
242 return LDB_ERR_OPERATIONS_ERROR;
245 val.data = talloc_memdup(msg, buf, len);
248 if (!val.data || krb5_ret) {
249 hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
250 return LDB_ERR_OPERATIONS_ERROR;
252 ret = ldb_msg_add_value(msg, "krb5Key", &val);
253 if (ret != LDB_SUCCESS) {
254 hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
259 hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
264 static int add_krb5_keys_from_NThash(struct ldb_module *module, struct ldb_message *msg,
265 struct smb_krb5_context *smb_krb5_context)
267 struct samr_Password *ntPwdHash;
268 krb5_error_code krb5_ret;
276 key.salt = NULL; /* No salt for this enc type */
278 ntPwdHash = samdb_result_hash(msg, msg, "ntPwdHash");
279 if (ntPwdHash == NULL) { /* what happened ?! */
280 return LDB_ERR_OPERATIONS_ERROR;
283 krb5_ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
284 ETYPE_ARCFOUR_HMAC_MD5,
285 ntPwdHash->hash, sizeof(ntPwdHash->hash),
288 return LDB_ERR_OPERATIONS_ERROR;
290 ASN1_MALLOC_ENCODE(Key, buf, buf_size, &key, &len, krb5_ret);
292 return LDB_ERR_OPERATIONS_ERROR;
294 krb5_free_keyblock_contents(smb_krb5_context->krb5_context,
297 val.data = talloc_memdup(msg, buf, len);
301 return LDB_ERR_OPERATIONS_ERROR;
303 if (ldb_msg_add_value(msg, "krb5Key", &val) != 0) {
304 return LDB_ERR_OPERATIONS_ERROR;
310 static int set_pwdLastSet(struct ldb_module *module, struct ldb_message *msg, int is_mod)
315 unix_to_nt_time(&now_nt, time(NULL));
318 /* be sure there isn't a 0 value set (eg. coming from the template) */
319 ldb_msg_remove_attr(msg, "pwdLastSet");
321 if (ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_ADD) != 0) {
322 return LDB_ERR_OPERATIONS_ERROR;
326 if (ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_REPLACE) != 0) {
327 return LDB_ERR_OPERATIONS_ERROR;
331 if (samdb_msg_add_uint64(module->ldb, msg, msg, "pwdLastSet", now_nt) != 0) {
332 return LDB_ERR_OPERATIONS_ERROR;
338 static int add_keyVersionNumber(struct ldb_module *module, struct ldb_message *msg, int previous)
341 if (ldb_msg_add_empty(msg, "msDS-KeyVersionNumber", LDB_FLAG_MOD_REPLACE) != 0) {
342 return LDB_ERR_OPERATIONS_ERROR;
345 if (samdb_msg_add_uint(module->ldb, msg, msg, "msDS-KeyVersionNumber", previous+1) != 0) {
346 return LDB_ERR_OPERATIONS_ERROR;
352 static int setPwdHistory(struct ldb_module *module, struct ldb_message *msg, struct ldb_message *old_msg, int hlen)
354 struct samr_Password *nt_hash;
355 struct samr_Password *lm_hash;
356 struct samr_Password *nt_history;
357 struct samr_Password *lm_history;
358 struct samr_Password *new_nt_history;
359 struct samr_Password *new_lm_history;
364 nt_hash = samdb_result_hash(msg, old_msg, "ntPwdHash");
365 lm_hash = samdb_result_hash(msg, old_msg, "lmPwdHash");
367 /* if no previous passwords just return */
368 if (nt_hash == NULL && lm_hash == NULL) return LDB_SUCCESS;
370 nt_hist_len = samdb_result_hashes(msg, old_msg, "sambaNTPwdHistory", &nt_history);
371 lm_hist_len = samdb_result_hashes(msg, old_msg, "sambaLMPwdHistory", &lm_history);
373 /* We might not have an old NT password */
374 new_nt_history = talloc_array(msg, struct samr_Password, hlen);
375 if (new_nt_history == NULL) {
376 return LDB_ERR_OPERATIONS_ERROR;
378 for (i = 0; i < MIN(hlen-1, nt_hist_len); i++) {
379 new_nt_history[i+1] = nt_history[i];
383 new_nt_history[0] = *nt_hash;
385 ZERO_STRUCT(new_nt_history[0]);
387 if (ldb_msg_add_empty(msg, "sambaNTPwdHistory", LDB_FLAG_MOD_REPLACE) != LDB_SUCCESS) {
388 return LDB_ERR_OPERATIONS_ERROR;
390 if (samdb_msg_add_hashes(msg, msg, "sambaNTPwdHistory", new_nt_history, nt_hist_len) != LDB_SUCCESS) {
391 return LDB_ERR_OPERATIONS_ERROR;
395 /* Don't store 'long' passwords in the LM history,
396 but make sure to 'expire' one password off the other end */
397 new_lm_history = talloc_array(msg, struct samr_Password, hlen);
398 if (new_lm_history == NULL) {
399 return LDB_ERR_OPERATIONS_ERROR;
401 for (i = 0; i < MIN(hlen-1, lm_hist_len); i++) {
402 new_lm_history[i+1] = lm_history[i];
406 new_lm_history[0] = *lm_hash;
408 ZERO_STRUCT(new_lm_history[0]);
410 if (ldb_msg_add_empty(msg, "sambaLMPwdHistory", LDB_FLAG_MOD_REPLACE) != LDB_SUCCESS) {
411 return LDB_ERR_OPERATIONS_ERROR;
413 if (samdb_msg_add_hashes(msg, msg, "sambaLMPwdHistory", new_lm_history, lm_hist_len) != LDB_SUCCESS) {
414 return LDB_ERR_OPERATIONS_ERROR;
420 static struct ldb_handle *ph_init_handle(struct ldb_request *req, struct ldb_module *module, enum ph_type type)
422 struct ph_context *ac;
423 struct ldb_handle *h;
425 h = talloc_zero(req, struct ldb_handle);
427 ldb_set_errstring(module->ldb, "Out of Memory");
433 ac = talloc_zero(h, struct ph_context);
435 ldb_set_errstring(module->ldb, "Out of Memory");
440 h->private_data = (void *)ac;
442 h->state = LDB_ASYNC_INIT;
443 h->status = LDB_SUCCESS;
452 static int get_domain_data_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
454 struct ph_context *ac;
456 if (!context || !ares) {
457 ldb_set_errstring(ldb, "NULL Context or Result in callback");
458 return LDB_ERR_OPERATIONS_ERROR;
461 ac = talloc_get_type(context, struct ph_context);
463 /* we are interested only in the single reply (base search) we receive here */
464 if (ares->type == LDB_REPLY_ENTRY) {
465 if (ac->dom_res != NULL) {
466 ldb_set_errstring(ldb, "Too many results");
468 return LDB_ERR_OPERATIONS_ERROR;
470 ac->dom_res = talloc_steal(ac, ares);
478 static int build_domain_data_request(struct ph_context *ac)
480 /* attrs[] is returned from this function in
481 ac->dom_req->op.search.attrs, so it must be static, as
482 otherwise the compiler can put it on the stack */
483 static const char * const attrs[] = { "pwdProperties", "pwdHistoryLength", NULL };
486 ac->dom_req = talloc_zero(ac, struct ldb_request);
487 if (ac->dom_req == NULL) {
488 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
489 return LDB_ERR_OPERATIONS_ERROR;
491 ac->dom_req->operation = LDB_SEARCH;
492 ac->dom_req->op.search.base = ldb_get_default_basedn(ac->module->ldb);
493 ac->dom_req->op.search.scope = LDB_SCOPE_SUBTREE;
495 filter = talloc_asprintf(ac->dom_req, "(&(objectSid=%s)(|(objectClass=domain)(objectClass=builtinDomain)))",
496 ldap_encode_ndr_dom_sid(ac->dom_req, ac->domain_sid));
497 if (filter == NULL) {
498 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
499 talloc_free(ac->dom_req);
500 return LDB_ERR_OPERATIONS_ERROR;
503 ac->dom_req->op.search.tree = ldb_parse_tree(ac->module->ldb, filter);
504 if (ac->dom_req->op.search.tree == NULL) {
505 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
506 talloc_free(ac->dom_req);
507 return LDB_ERR_OPERATIONS_ERROR;
509 ac->dom_req->op.search.attrs = attrs;
510 ac->dom_req->controls = NULL;
511 ac->dom_req->context = ac;
512 ac->dom_req->callback = get_domain_data_callback;
513 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->dom_req);
518 static struct domain_data *get_domain_data(struct ldb_module *module, void *ctx, struct ldb_reply *res)
520 struct domain_data *data;
522 struct ph_context *ac;
525 ac = talloc_get_type(ctx, struct ph_context);
527 data = talloc_zero(ac, struct domain_data);
533 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Could not find this user's domain: %s!\n", dom_sid_string(data, ac->domain_sid));
538 data->pwdProperties = samdb_result_uint(res->message, "pwdProperties", 0);
539 data->pwdHistoryLength = samdb_result_uint(res->message, "pwdHistoryLength", 0);
541 /* For a domain DN, this puts things in dotted notation */
542 /* For builtin domains, this will give details for the host,
543 * but that doesn't really matter, as it's just used for salt
544 * and kerberos principals, which don't exist here */
546 tmp = ldb_dn_canonical_string(ctx, res->message->dn);
551 /* But it puts a trailing (or just before 'builtin') / on things, so kill that */
552 p = strchr(tmp, '/');
558 data->dns_domain = strlower_talloc(data, tmp);
559 if (data->dns_domain == NULL) {
560 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
563 data->realm = strupper_talloc(data, tmp);
564 if (data->realm == NULL) {
565 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
573 static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
575 struct ldb_handle *h;
576 struct ph_context *ac;
577 struct ldb_message_element *sambaAttr;
578 struct ldb_message_element *ntAttr;
579 struct ldb_message_element *lmAttr;
582 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
584 if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
585 return ldb_next_request(module, req);
588 /* If the caller is manipulating the local passwords directly, let them pass */
589 if (ldb_dn_compare_base(module->ldb,
590 ldb_dn_explode(req, LOCAL_BASE),
591 req->op.add.message->dn) == 0) {
592 return ldb_next_request(module, req);
595 /* nobody must touch password Histories */
596 if (ldb_msg_find_element(req->op.add.message, "sambaNTPwdHistory") ||
597 ldb_msg_find_element(req->op.add.message, "sambaLMPwdHistory")) {
598 return LDB_ERR_UNWILLING_TO_PERFORM;
601 /* If no part of this ADD touches the sambaPassword, or the NT
602 * or LM hashes, then we don't need to make any changes. */
604 sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
605 ntAttr = ldb_msg_find_element(req->op.mod.message, "ntPwdHash");
606 lmAttr = ldb_msg_find_element(req->op.mod.message, "lmPwdHash");
608 if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
609 return ldb_next_request(module, req);
612 /* if it is not an entry of type person its an error */
613 /* TODO: remove this when sambaPassword will be in schema */
614 if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
615 ldb_set_errstring(module->ldb, "Cannot set a password on entry that does not have objectClass 'person'");
616 return LDB_ERR_OBJECT_CLASS_VIOLATION;
619 /* check sambaPassword is single valued here */
620 /* TODO: remove this when sambaPassword will be single valued in schema */
621 if (sambaAttr && sambaAttr->num_values > 1) {
622 ldb_set_errstring(module->ldb, "mupltiple values for sambaPassword not allowed!\n");
623 return LDB_ERR_CONSTRAINT_VIOLATION;
626 if (ntAttr && (ntAttr->num_values > 1)) {
627 ldb_set_errstring(module->ldb, "mupltiple values for lmPwdHash not allowed!\n");
628 return LDB_ERR_CONSTRAINT_VIOLATION;
630 if (lmAttr && (lmAttr->num_values > 1)) {
631 ldb_set_errstring(module->ldb, "mupltiple values for lmPwdHash not allowed!\n");
632 return LDB_ERR_CONSTRAINT_VIOLATION;
635 h = ph_init_handle(req, module, PH_ADD);
637 return LDB_ERR_OPERATIONS_ERROR;
639 ac = talloc_get_type(h->private_data, struct ph_context);
641 /* get user domain data */
642 ac->domain_sid = samdb_result_sid_prefix(ac, req->op.add.message, "objectSid");
643 if (ac->domain_sid == NULL) {
644 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
645 return LDB_ERR_OPERATIONS_ERROR;
648 ret = build_domain_data_request(ac);
649 if (ret != LDB_SUCCESS) {
653 ac->step = PH_ADD_SEARCH_DOM;
657 return ldb_next_request(module, ac->dom_req);
660 static int password_hash_add_do_add(struct ldb_handle *h) {
662 struct ph_context *ac;
663 struct domain_data *domain;
664 struct smb_krb5_context *smb_krb5_context;
665 struct ldb_message_element *sambaAttr;
666 struct ldb_message *msg;
669 ac = talloc_get_type(h->private_data, struct ph_context);
671 domain = get_domain_data(ac->module, ac, ac->dom_res);
672 if (domain == NULL) {
673 return LDB_ERR_OPERATIONS_ERROR;
676 ac->down_req = talloc(ac, struct ldb_request);
677 if (ac->down_req == NULL) {
678 return LDB_ERR_OPERATIONS_ERROR;
681 *(ac->down_req) = *(ac->orig_req);
682 ac->down_req->op.add.message = msg = ldb_msg_copy_shallow(ac->down_req, ac->orig_req->op.add.message);
683 if (ac->down_req->op.add.message == NULL) {
684 return LDB_ERR_OPERATIONS_ERROR;
687 /* Some operations below require kerberos contexts */
688 if (smb_krb5_init_context(ac->down_req, &smb_krb5_context) != 0) {
689 return LDB_ERR_OPERATIONS_ERROR;
692 /* if we have sambaPassword in the original message add the operatio on it here */
693 sambaAttr = ldb_msg_find_element(msg, "sambaPassword");
695 ret = add_password_hashes(ac->module, msg, 0);
696 /* we can compute new password hashes from the unicode password */
697 if (ret != LDB_SUCCESS) {
701 /* now add krb5 keys based on unicode password */
702 ret = add_krb5_keys_from_password(ac->module, msg, smb_krb5_context, domain,
703 ldb_msg_find_attr_as_string(msg, "samAccountName", NULL),
704 ldb_msg_find_attr_as_string(msg, "userPrincipalName", NULL),
705 ldb_msg_check_string_attribute(msg, "objectClass", "computer"));
706 if (ret != LDB_SUCCESS) {
710 /* add also kr5 keys based on NT the hash */
711 ret = add_krb5_keys_from_NThash(ac->module, msg, smb_krb5_context);
712 if (ret != LDB_SUCCESS) {
716 /* if both the domain properties and the user account controls do not permit
717 * clear text passwords then wipe out the sambaPassword */
718 if ((!(domain->pwdProperties & DOMAIN_PASSWORD_STORE_CLEARTEXT)) ||
719 (!(ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0) & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED))) {
720 ldb_msg_remove_attr(msg, "sambaPassword");
724 /* don't touch it if a value is set. It could be an incoming samsync */
725 if (ldb_msg_find_attr_as_uint64(msg, "pwdLastSet", 0) == 0) {
726 if (set_pwdLastSet(ac->module, msg, 0) != LDB_SUCCESS) {
727 return LDB_ERR_OPERATIONS_ERROR;
731 /* don't touch it if a value is set. It could be an incoming samsync */
732 if (!ldb_msg_find_element(msg, "msDS-KeyVersionNumber")) {
733 if (add_keyVersionNumber(ac->module, msg, 0) != LDB_SUCCESS) {
734 return LDB_ERR_OPERATIONS_ERROR;
738 h->state = LDB_ASYNC_INIT;
739 h->status = LDB_SUCCESS;
741 ac->step = PH_ADD_DO_ADD;
743 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->down_req);
745 /* perform the operation */
746 return ldb_next_request(ac->module, ac->down_req);
749 static int password_hash_mod_search_self(struct ldb_handle *h);
751 static int password_hash_modify(struct ldb_module *module, struct ldb_request *req)
753 struct ldb_handle *h;
754 struct ph_context *ac;
755 struct ldb_message_element *sambaAttr;
756 struct ldb_message_element *ntAttr;
757 struct ldb_message_element *lmAttr;
758 struct ldb_message *msg;
760 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
762 if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
763 return ldb_next_request(module, req);
766 /* If the caller is manipulating the local passwords directly, let them pass */
767 if (ldb_dn_compare_base(module->ldb,
768 ldb_dn_explode(req, LOCAL_BASE),
769 req->op.mod.message->dn) == 0) {
770 return ldb_next_request(module, req);
773 /* nobody must touch password Histories */
774 if (ldb_msg_find_element(req->op.mod.message, "sambaNTPwdHistory") ||
775 ldb_msg_find_element(req->op.mod.message, "sambaLMPwdHistory")) {
776 return LDB_ERR_UNWILLING_TO_PERFORM;
779 sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
780 ntAttr = ldb_msg_find_element(req->op.mod.message, "ntPwdHash");
781 lmAttr = ldb_msg_find_element(req->op.mod.message, "lmPwdHash");
783 /* check passwords are single valued here */
784 /* TODO: remove this when passwords will be single valued in schema */
785 if (sambaAttr && (sambaAttr->num_values > 1)) {
786 return LDB_ERR_CONSTRAINT_VIOLATION;
788 if (ntAttr && (ntAttr->num_values > 1)) {
789 return LDB_ERR_CONSTRAINT_VIOLATION;
791 if (lmAttr && (lmAttr->num_values > 1)) {
792 return LDB_ERR_CONSTRAINT_VIOLATION;
795 /* If no part of this touches the sambaPassword OR ntPwdHash and/or lmPwdHash, then we don't
796 * need to make any changes. For password changes/set there should
797 * be a 'delete' or a 'modify' on this attribute. */
798 /* If the only operation is the deletion of the passwords then go on */
799 if ( ((!sambaAttr) || ((sambaAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))
800 && ((!ntAttr) || ((ntAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))
801 && ((!lmAttr) || ((lmAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE)) ) {
803 return ldb_next_request(module, req);
806 h = ph_init_handle(req, module, PH_MOD);
808 return LDB_ERR_OPERATIONS_ERROR;
810 ac = talloc_get_type(h->private_data, struct ph_context);
812 /* return or own handle to deal with this call */
815 /* prepare the first operation */
816 ac->down_req = talloc_zero(ac, struct ldb_request);
817 if (ac->down_req == NULL) {
818 ldb_set_errstring(module->ldb, "Out of memory!");
819 return LDB_ERR_OPERATIONS_ERROR;
822 *(ac->down_req) = *req; /* copy the request */
824 /* use a new message structure so that we can modify it */
825 ac->down_req->op.mod.message = msg = ldb_msg_copy_shallow(ac->down_req, req->op.mod.message);
827 /* - remove any imodification to the password from the first commit
828 * we will make the real modification later */
829 if (sambaAttr) ldb_msg_remove_attr(msg, "sambaPassword");
830 if (ntAttr) ldb_msg_remove_attr(msg, "ntPwdHash");
831 if (lmAttr) ldb_msg_remove_attr(msg, "lmPwdHash");
833 /* if there was nothing else to be modify skip to next step */
834 if (msg->num_elements == 0) {
835 talloc_free(ac->down_req);
837 return password_hash_mod_search_self(h);
840 ac->down_req->context = NULL;
841 ac->down_req->callback = NULL;
843 ac->step = PH_MOD_DO_REQ;
845 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
847 return ldb_next_request(module, ac->down_req);
850 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
852 struct ph_context *ac;
854 if (!context || !ares) {
855 ldb_set_errstring(ldb, "NULL Context or Result in callback");
856 return LDB_ERR_OPERATIONS_ERROR;
859 ac = talloc_get_type(context, struct ph_context);
861 /* we are interested only in the single reply (base search) we receive here */
862 if (ares->type == LDB_REPLY_ENTRY) {
863 if (ac->search_res != NULL) {
864 ldb_set_errstring(ldb, "Too many results");
866 return LDB_ERR_OPERATIONS_ERROR;
869 /* if it is not an entry of type person this is an error */
870 /* TODO: remove this when sambaPassword will be in schema */
871 if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
872 ldb_set_errstring(ldb, "Object class violation");
874 return LDB_ERR_OBJECT_CLASS_VIOLATION;
877 ac->search_res = talloc_steal(ac, ares);
885 static int password_hash_mod_search_self(struct ldb_handle *h) {
887 struct ph_context *ac;
888 static const char * const attrs[] = { "userAccountControl", "sambaLMPwdHistory",
890 "objectSid", "msDS-KeyVersionNumber",
891 "objectClass", "userPrincipalName",
893 "lmPwdHash", "ntPwdHash",
896 ac = talloc_get_type(h->private_data, struct ph_context);
898 /* prepare the search operation */
899 ac->search_req = talloc_zero(ac, struct ldb_request);
900 if (ac->search_req == NULL) {
901 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
902 return LDB_ERR_OPERATIONS_ERROR;
905 ac->search_req->operation = LDB_SEARCH;
906 ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
907 ac->search_req->op.search.scope = LDB_SCOPE_BASE;
908 ac->search_req->op.search.tree = ldb_parse_tree(ac->module->ldb, NULL);
909 if (ac->search_req->op.search.tree == NULL) {
910 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
911 return LDB_ERR_OPERATIONS_ERROR;
913 ac->search_req->op.search.attrs = attrs;
914 ac->search_req->controls = NULL;
915 ac->search_req->context = ac;
916 ac->search_req->callback = get_self_callback;
917 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
919 ac->step = PH_MOD_SEARCH_SELF;
921 return ldb_next_request(ac->module, ac->search_req);
924 static int password_hash_mod_search_dom(struct ldb_handle *h) {
926 struct ph_context *ac;
929 ac = talloc_get_type(h->private_data, struct ph_context);
931 /* get object domain sid */
932 ac->domain_sid = samdb_result_sid_prefix(ac, ac->search_res->message, "objectSid");
933 if (ac->domain_sid == NULL) {
934 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
935 return LDB_ERR_OPERATIONS_ERROR;
938 /* get user domain data */
939 ret = build_domain_data_request(ac);
940 if (ret != LDB_SUCCESS) {
944 ac->step = PH_MOD_SEARCH_DOM;
946 return ldb_next_request(ac->module, ac->dom_req);
949 static int password_hash_mod_do_mod(struct ldb_handle *h) {
951 struct ph_context *ac;
952 struct domain_data *domain;
953 struct smb_krb5_context *smb_krb5_context;
954 struct ldb_message_element *sambaAttr;
955 struct ldb_message *msg;
958 BOOL added_hashes = False;
960 ac = talloc_get_type(h->private_data, struct ph_context);
962 domain = get_domain_data(ac->module, ac, ac->dom_res);
963 if (domain == NULL) {
964 return LDB_ERR_OPERATIONS_ERROR;
967 ac->mod_req = talloc(ac, struct ldb_request);
968 if (ac->mod_req == NULL) {
969 return LDB_ERR_OPERATIONS_ERROR;
972 *(ac->mod_req) = *(ac->orig_req);
974 /* use a new message structure so that we can modify it */
975 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
977 return LDB_ERR_OPERATIONS_ERROR;
981 msg->dn = ac->orig_req->op.mod.message->dn;
983 /* Some operations below require kerberos contexts */
984 if (smb_krb5_init_context(ac->mod_req, &smb_krb5_context) != 0) {
985 return LDB_ERR_OPERATIONS_ERROR;
988 /* we are going to replace the existing krb5key or delete it */
989 if (ldb_msg_add_empty(msg, "krb5key", LDB_FLAG_MOD_REPLACE) != 0) {
990 return LDB_ERR_OPERATIONS_ERROR;
993 /* if we have sambaPassword in the original message add the operation on it here */
994 sambaAttr = ldb_msg_find_element(ac->orig_req->op.mod.message, "sambaPassword");
997 if (ldb_msg_add(msg, sambaAttr, sambaAttr->flags) != 0) {
998 return LDB_ERR_OPERATIONS_ERROR;
1001 /* if we are actually settting a new unicode password,
1002 * use it to generate the password hashes */
1003 if (((sambaAttr->flags & LDB_FLAG_MOD_MASK) != LDB_FLAG_MOD_DELETE)
1004 && (sambaAttr->num_values == 1)) {
1005 /* we can compute new password hashes from the unicode password */
1006 ret = add_password_hashes(ac->module, msg, 1);
1007 if (ret != LDB_SUCCESS) {
1011 added_hashes = True;
1013 /* now add krb5 keys based on unicode password */
1014 ret = add_krb5_keys_from_password(ac->module, msg, smb_krb5_context, domain,
1015 ldb_msg_find_attr_as_string(ac->search_res->message, "samAccountName", NULL),
1016 ldb_msg_find_attr_as_string(ac->search_res->message, "userPrincipalName", NULL),
1017 ldb_msg_check_string_attribute(ac->search_res->message, "objectClass", "computer"));
1019 if (ret != LDB_SUCCESS) {
1023 /* if the domain properties or the user account controls do not permit
1024 * clear text passwords then wipe out the sambaPassword */
1025 if ((!(domain->pwdProperties & DOMAIN_PASSWORD_STORE_CLEARTEXT)) ||
1026 (!(ldb_msg_find_attr_as_uint(ac->search_res->message, "userAccountControl", 0) & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED))) {
1027 ldb_msg_remove_attr(msg, "sambaPassword");
1033 /* if we didn't create the hashes above, try using values supplied directly */
1034 if (!added_hashes) {
1035 struct ldb_message_element *el;
1037 el = ldb_msg_find_element(ac->orig_req->op.mod.message, "ntPwdHash");
1038 if (ldb_msg_add(msg, el, el->flags) != 0) {
1039 return LDB_ERR_OPERATIONS_ERROR;
1042 el = ldb_msg_find_element(ac->orig_req->op.mod.message, "lmPwdHash");
1043 if (ldb_msg_add(msg, el, el->flags) != 0) {
1044 return LDB_ERR_OPERATIONS_ERROR;
1048 /* add also krb5 keys based on NT the hash */
1049 if (add_krb5_keys_from_NThash(ac->module, msg, smb_krb5_context) != LDB_SUCCESS) {
1050 return LDB_ERR_OPERATIONS_ERROR;
1053 /* set change time */
1054 if (set_pwdLastSet(ac->module, msg, 1) != LDB_SUCCESS) {
1055 return LDB_ERR_OPERATIONS_ERROR;
1058 /* don't touch it if a value is set. It could be an incoming samsync */
1059 if (!ldb_msg_find_element(ac->orig_req->op.mod.message,
1060 "msDS-KeyVersionNumber")) {
1061 if (add_keyVersionNumber(ac->module, msg,
1062 ldb_msg_find_attr_as_uint(ac->search_res->message,
1063 "msDS-KeyVersionNumber", 0)
1065 return LDB_ERR_OPERATIONS_ERROR;
1069 if ((phlen = samdb_result_uint(ac->dom_res->message, "pwdHistoryLength", 0)) > 0) {
1070 if (setPwdHistory(ac->module, msg, ac->search_res->message, phlen) != LDB_SUCCESS) {
1071 return LDB_ERR_OPERATIONS_ERROR;
1075 h->state = LDB_ASYNC_INIT;
1076 h->status = LDB_SUCCESS;
1078 ac->step = PH_MOD_DO_MOD;
1080 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
1082 /* perform the search */
1083 return ldb_next_request(ac->module, ac->mod_req);
1086 static int ph_wait(struct ldb_handle *handle) {
1087 struct ph_context *ac;
1090 if (!handle || !handle->private_data) {
1091 return LDB_ERR_OPERATIONS_ERROR;
1094 if (handle->state == LDB_ASYNC_DONE) {
1095 return handle->status;
1098 handle->state = LDB_ASYNC_PENDING;
1099 handle->status = LDB_SUCCESS;
1101 ac = talloc_get_type(handle->private_data, struct ph_context);
1104 case PH_ADD_SEARCH_DOM:
1105 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1107 if (ret != LDB_SUCCESS) {
1108 handle->status = ret;
1111 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1112 handle->status = ac->dom_req->handle->status;
1116 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1120 /* domain search done, go on */
1121 return password_hash_add_do_add(handle);
1124 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1126 if (ret != LDB_SUCCESS) {
1127 handle->status = ret;
1130 if (ac->down_req->handle->status != LDB_SUCCESS) {
1131 handle->status = ac->down_req->handle->status;
1135 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1142 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1144 if (ret != LDB_SUCCESS) {
1145 handle->status = ret;
1148 if (ac->down_req->handle->status != LDB_SUCCESS) {
1149 handle->status = ac->down_req->handle->status;
1153 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1157 /* non-password mods done, go on */
1158 return password_hash_mod_search_self(handle);
1160 case PH_MOD_SEARCH_SELF:
1161 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1163 if (ret != LDB_SUCCESS) {
1164 handle->status = ret;
1167 if (ac->search_req->handle->status != LDB_SUCCESS) {
1168 handle->status = ac->search_req->handle->status;
1172 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1176 /* self search done, go on */
1177 return password_hash_mod_search_dom(handle);
1179 case PH_MOD_SEARCH_DOM:
1180 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1182 if (ret != LDB_SUCCESS) {
1183 handle->status = ret;
1186 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1187 handle->status = ac->dom_req->handle->status;
1191 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1195 /* domain search done, go on */
1196 return password_hash_mod_do_mod(handle);
1199 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
1201 if (ret != LDB_SUCCESS) {
1202 handle->status = ret;
1205 if (ac->mod_req->handle->status != LDB_SUCCESS) {
1206 handle->status = ac->mod_req->handle->status;
1210 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
1217 ret = LDB_ERR_OPERATIONS_ERROR;
1224 handle->state = LDB_ASYNC_DONE;
1228 static int ph_wait_all(struct ldb_handle *handle) {
1232 while (handle->state != LDB_ASYNC_DONE) {
1233 ret = ph_wait(handle);
1234 if (ret != LDB_SUCCESS) {
1239 return handle->status;
1242 static int password_hash_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1244 if (type == LDB_WAIT_ALL) {
1245 return ph_wait_all(handle);
1247 return ph_wait(handle);
1251 static const struct ldb_module_ops password_hash_ops = {
1252 .name = "password_hash",
1253 .add = password_hash_add,
1254 .modify = password_hash_modify,
1255 .wait = password_hash_wait
1259 int password_hash_module_init(void)
1261 return ldb_register_module(&password_hash_ops);