4 Copyright (C) Simo Sorce 2004-2006
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2006
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Stefan Metzmacher 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * Component: ldb password_hash module
28 * Description: correctly update hash values based on changes to sambaPassword and friends
30 * Author: Andrew Bartlett
31 * Author: Stefan Metzmacher
35 #include "libcli/ldap/ldap_ndr.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "librpc/gen_ndr/misc.h"
40 #include "librpc/gen_ndr/samr.h"
41 #include "libcli/auth/libcli_auth.h"
42 #include "libcli/security/security.h"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
45 #include "system/time.h"
46 #include "dsdb/samdb/samdb.h"
47 #include "dsdb/common/flags.h"
48 #include "dsdb/samdb/ldb_modules/password_modules.h"
49 #include "librpc/ndr/libndr.h"
50 #include "librpc/gen_ndr/ndr_drsblobs.h"
51 #include "lib/crypto/crypto.h"
52 #include "param/param.h"
54 /* If we have decided there is reason to work on this request, then
55 * setup all the password hash types correctly.
57 * If the administrator doesn't want the sambaPassword stored (set in the
58 * domain and per-account policies) then we must strip that out before
59 * we do the first operation.
61 * Once this is done (which could update anything at all), we
62 * calculate the password hashes.
64 * This function must not only update the unicodePwd, dBCSPwd and
65 * supplementalCredentials fields, it must also atomicly increment the
66 * msDS-KeyVersionNumber. We should be in a transaction, so all this
67 * should be quite safe...
69 * Finally, if the administrator has requested that a password history
70 * be maintained, then this should also be written out.
76 enum ph_type {PH_ADD, PH_MOD} type;
77 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;
79 struct ldb_module *module;
80 struct ldb_request *orig_req;
82 struct ldb_request *dom_req;
83 struct ldb_reply *dom_res;
85 struct ldb_request *down_req;
87 struct ldb_request *search_req;
88 struct ldb_reply *search_res;
90 struct ldb_request *mod_req;
92 struct dom_sid *domain_sid;
98 uint_t pwdHistoryLength;
104 struct setup_password_fields_io {
105 struct ph_context *ac;
106 struct domain_data *domain;
107 struct smb_krb5_context *smb_krb5_context;
109 /* infos about the user account */
111 uint32_t user_account_control;
112 const char *sAMAccountName;
113 const char *user_principal_name;
117 /* new credentials */
119 const char *cleartext;
120 struct samr_Password *nt_hash;
121 struct samr_Password *lm_hash;
124 /* old credentials */
126 uint32_t nt_history_len;
127 struct samr_Password *nt_history;
128 uint32_t lm_history_len;
129 struct samr_Password *lm_history;
130 const struct ldb_val *supplemental;
131 struct supplementalCredentialsBlob scb;
135 /* generated credentials */
137 struct samr_Password *nt_hash;
138 struct samr_Password *lm_hash;
139 uint32_t nt_history_len;
140 struct samr_Password *nt_history;
141 uint32_t lm_history_len;
142 struct samr_Password *lm_history;
143 struct ldb_val supplemental;
149 static int setup_nt_fields(struct setup_password_fields_io *io)
153 io->g.nt_hash = io->n.nt_hash;
155 if (io->domain->pwdHistoryLength == 0) {
159 /* We might not have an old NT password */
160 io->g.nt_history = talloc_array(io->ac,
161 struct samr_Password,
162 io->domain->pwdHistoryLength);
163 if (!io->g.nt_history) {
164 ldb_oom(io->ac->module->ldb);
165 return LDB_ERR_OPERATIONS_ERROR;
168 for (i = 0; i < MIN(io->domain->pwdHistoryLength-1, io->o.nt_history_len); i++) {
169 io->g.nt_history[i+1] = io->o.nt_history[i];
171 io->g.nt_history_len = i + 1;
174 io->g.nt_history[0] = *io->g.nt_hash;
177 * TODO: is this correct?
178 * the simular behavior is correct for the lm history case
180 E_md4hash("", io->g.nt_history[0].hash);
186 static int setup_lm_fields(struct setup_password_fields_io *io)
190 io->g.lm_hash = io->n.lm_hash;
192 if (io->domain->pwdHistoryLength == 0) {
196 /* We might not have an old NT password */
197 io->g.lm_history = talloc_array(io->ac,
198 struct samr_Password,
199 io->domain->pwdHistoryLength);
200 if (!io->g.lm_history) {
201 ldb_oom(io->ac->module->ldb);
202 return LDB_ERR_OPERATIONS_ERROR;
205 for (i = 0; i < MIN(io->domain->pwdHistoryLength-1, io->o.lm_history_len); i++) {
206 io->g.lm_history[i+1] = io->o.lm_history[i];
208 io->g.lm_history_len = i + 1;
211 io->g.lm_history[0] = *io->g.lm_hash;
213 E_deshash("", io->g.lm_history[0].hash);
219 static int setup_primary_kerberos(struct setup_password_fields_io *io,
220 const struct supplementalCredentialsBlob *old_scb,
221 struct package_PrimaryKerberosBlob *pkb)
223 krb5_error_code krb5_ret;
224 Principal *salt_principal;
228 struct package_PrimaryKerberosCtr3 *pkb3 = &pkb->ctr.ctr3;
229 struct supplementalCredentialsPackage *old_scp = NULL;
230 struct package_PrimaryKerberosBlob _old_pkb;
231 struct package_PrimaryKerberosCtr3 *old_pkb3 = NULL;
233 enum ndr_err_code ndr_err;
235 /* Many, many thanks to lukeh@padl.com for this
236 * algorithm, described in his Nov 10 2004 mail to
237 * samba-technical@samba.org */
240 * Determine a salting principal
242 if (io->u.is_computer) {
246 name = talloc_strdup(io->ac, io->u.sAMAccountName);
248 ldb_oom(io->ac->module->ldb);
249 return LDB_ERR_OPERATIONS_ERROR;
252 if (name[strlen(name)-1] == '$') {
253 name[strlen(name)-1] = '\0';
256 saltbody = talloc_asprintf(io->ac, "%s.%s", name, io->domain->dns_domain);
258 ldb_oom(io->ac->module->ldb);
259 return LDB_ERR_OPERATIONS_ERROR;
262 krb5_ret = krb5_make_principal(io->smb_krb5_context->krb5_context,
264 io->domain->realm, "host",
266 } else if (io->u.user_principal_name) {
267 char *user_principal_name;
270 user_principal_name = talloc_strdup(io->ac, io->u.user_principal_name);
271 if (!user_principal_name) {
272 ldb_oom(io->ac->module->ldb);
273 return LDB_ERR_OPERATIONS_ERROR;
276 p = strchr(user_principal_name, '@');
281 krb5_ret = krb5_make_principal(io->smb_krb5_context->krb5_context,
283 io->domain->realm, user_principal_name,
286 krb5_ret = krb5_make_principal(io->smb_krb5_context->krb5_context,
288 io->domain->realm, io->u.sAMAccountName,
292 ldb_asprintf_errstring(io->ac->module->ldb,
293 "setup_primary_kerberos: "
294 "generation of a salting principal failed: %s",
295 smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
296 return LDB_ERR_OPERATIONS_ERROR;
300 * create salt from salt_principal
302 krb5_ret = krb5_get_pw_salt(io->smb_krb5_context->krb5_context,
303 salt_principal, &salt);
304 krb5_free_principal(io->smb_krb5_context->krb5_context, salt_principal);
306 ldb_asprintf_errstring(io->ac->module->ldb,
307 "setup_primary_kerberos: "
308 "generation of krb5_salt failed: %s",
309 smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
310 return LDB_ERR_OPERATIONS_ERROR;
312 /* create a talloc copy */
313 pkb3->salt.string = talloc_strndup(io->ac,
315 salt.saltvalue.length);
316 krb5_free_salt(io->smb_krb5_context->krb5_context, salt);
317 if (!pkb3->salt.string) {
318 ldb_oom(io->ac->module->ldb);
319 return LDB_ERR_OPERATIONS_ERROR;
321 salt.saltvalue.data = discard_const(pkb3->salt.string);
322 salt.saltvalue.length = strlen(pkb3->salt.string);
325 * prepare generation of keys
327 * ENCTYPE_AES256_CTS_HMAC_SHA1_96 (disabled by default)
328 * ENCTYPE_DES_CBC_MD5
329 * ENCTYPE_DES_CBC_CRC
331 * NOTE: update num_keys when you add another enctype!
334 pkb3->keys = talloc_array(io->ac, struct package_PrimaryKerberosKey, pkb3->num_keys);
336 ldb_oom(io->ac->module->ldb);
337 return LDB_ERR_OPERATIONS_ERROR;
339 pkb3->unknown3 = talloc_zero_array(io->ac, uint64_t, pkb3->num_keys);
340 if (!pkb3->unknown3) {
341 ldb_oom(io->ac->module->ldb);
342 return LDB_ERR_OPERATIONS_ERROR;
345 if (lp_parm_bool(ldb_get_opaque(io->ac->module->ldb, "loadparm"), NULL, "password_hash", "create_aes_key", false)) {
349 * w2k and w2k3 doesn't support AES, so we'll not include
350 * the AES key here yet.
352 * Also we don't have an example supplementalCredentials blob
353 * from Windows Longhorn Server with AES support
357 * create ENCTYPE_AES256_CTS_HMAC_SHA1_96 key out of
358 * the salt and the cleartext password
360 krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
361 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
365 pkb3->keys[k].keytype = ENCTYPE_AES256_CTS_HMAC_SHA1_96;
366 pkb3->keys[k].value = talloc(pkb3->keys, DATA_BLOB);
367 if (!pkb3->keys[k].value) {
368 krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
369 ldb_oom(io->ac->module->ldb);
370 return LDB_ERR_OPERATIONS_ERROR;
372 *pkb3->keys[k].value = data_blob_talloc(pkb3->keys[k].value,
374 key.keyvalue.length);
375 krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
376 if (!pkb3->keys[k].value->data) {
377 ldb_oom(io->ac->module->ldb);
378 return LDB_ERR_OPERATIONS_ERROR;
384 * create ENCTYPE_DES_CBC_MD5 key out of
385 * the salt and the cleartext password
387 krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
392 pkb3->keys[k].keytype = ENCTYPE_DES_CBC_MD5;
393 pkb3->keys[k].value = talloc(pkb3->keys, DATA_BLOB);
394 if (!pkb3->keys[k].value) {
395 krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
396 ldb_oom(io->ac->module->ldb);
397 return LDB_ERR_OPERATIONS_ERROR;
399 *pkb3->keys[k].value = data_blob_talloc(pkb3->keys[k].value,
401 key.keyvalue.length);
402 krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
403 if (!pkb3->keys[k].value->data) {
404 ldb_oom(io->ac->module->ldb);
405 return LDB_ERR_OPERATIONS_ERROR;
410 * create ENCTYPE_DES_CBC_CRC key out of
411 * the salt and the cleartext password
413 krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
418 pkb3->keys[k].keytype = ENCTYPE_DES_CBC_CRC;
419 pkb3->keys[k].value = talloc(pkb3->keys, DATA_BLOB);
420 if (!pkb3->keys[k].value) {
421 krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
422 ldb_oom(io->ac->module->ldb);
423 return LDB_ERR_OPERATIONS_ERROR;
425 *pkb3->keys[k].value = data_blob_talloc(pkb3->keys[k].value,
427 key.keyvalue.length);
428 krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
429 if (!pkb3->keys[k].value->data) {
430 ldb_oom(io->ac->module->ldb);
431 return LDB_ERR_OPERATIONS_ERROR;
435 /* fix up key number */
438 /* initialize the old keys to zero */
439 pkb3->num_old_keys = 0;
440 pkb3->old_keys = NULL;
441 pkb3->unknown3_old = NULL;
443 /* if there're no old keys, then we're done */
448 for (i=0; i < old_scb->sub.num_packages; i++) {
449 if (old_scb->sub.packages[i].unknown1 != 0x00000001) {
453 if (strcmp("Primary:Kerberos", old_scb->sub.packages[i].name) != 0) {
457 if (!old_scb->sub.packages[i].data || !old_scb->sub.packages[i].data[0]) {
461 old_scp = &old_scb->sub.packages[i];
464 /* Primary:Kerberos element of supplementalCredentials */
468 blob = strhex_to_data_blob(old_scp->data);
470 ldb_oom(io->ac->module->ldb);
471 return LDB_ERR_OPERATIONS_ERROR;
473 talloc_steal(io->ac, blob.data);
475 /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
476 ndr_err = ndr_pull_struct_blob(&blob, io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), &_old_pkb,
477 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
478 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
479 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
480 ldb_asprintf_errstring(io->ac->module->ldb,
481 "setup_primary_kerberos: "
482 "failed to pull old package_PrimaryKerberosBlob: %s",
484 return LDB_ERR_OPERATIONS_ERROR;
487 if (_old_pkb.version != 3) {
488 ldb_asprintf_errstring(io->ac->module->ldb,
489 "setup_primary_kerberos: "
490 "package_PrimaryKerberosBlob version[%u] expected[3]",
492 return LDB_ERR_OPERATIONS_ERROR;
495 old_pkb3 = &_old_pkb.ctr.ctr3;
498 /* if we didn't found the old keys we're done */
503 /* fill in the old keys */
504 pkb3->num_old_keys = old_pkb3->num_keys;
505 pkb3->old_keys = old_pkb3->keys;
506 pkb3->unknown3_old = old_pkb3->unknown3;
511 static int setup_primary_wdigest(struct setup_password_fields_io *io,
512 const struct supplementalCredentialsBlob *old_scb,
513 struct package_PrimaryWDigestBlob *pdb)
515 DATA_BLOB sAMAccountName;
516 DATA_BLOB sAMAccountName_l;
517 DATA_BLOB sAMAccountName_u;
518 const char *user_principal_name = io->u.user_principal_name;
519 DATA_BLOB userPrincipalName;
520 DATA_BLOB userPrincipalName_l;
521 DATA_BLOB userPrincipalName_u;
522 DATA_BLOB netbios_domain;
523 DATA_BLOB netbios_domain_l;
524 DATA_BLOB netbios_domain_u;
525 DATA_BLOB dns_domain;
526 DATA_BLOB dns_domain_l;
527 DATA_BLOB dns_domain_u;
540 * http://technet2.microsoft.com/WindowsServer/en/library/717b450c-f4a0-4cc9-86f4-cc0633aae5f91033.mspx?mfr=true
541 * for what precalculated hashes are supposed to be stored...
543 * I can't reproduce all values which should contain "Digest" as realm,
544 * am I doing something wrong or is w2k3 just broken...?
546 * W2K3 fills in following for a user:
548 * dn: CN=NewUser,OU=newtop,DC=sub1,DC=w2k3,DC=vmnet1,DC=vm,DC=base
549 * sAMAccountName: NewUser2Sam
550 * userPrincipalName: NewUser2Princ@sub1.w2k3.vmnet1.vm.base
552 * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
553 * b7ec9da91062199aee7d121e6710fe23 => newuser2sam:sub1:TestPwd2007
554 * 17d290bc5c9f463fac54c37a8cea134d => NEWUSER2SAM:SUB1:TestPwd2007
555 * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
556 * 5d57e7823938348127322e08cd81bcb5 => NewUser2Sam:sub1:TestPwd2007
557 * 07dd701bf8a011ece585de3d47237140 => NEWUSER2SAM:sub1:TestPwd2007
558 * e14fb0eb401498d2cb33c9aae1cc7f37 => newuser2sam:SUB1:TestPwd2007
559 * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
560 * f52da1266a6bdd290ffd48b2c823dda7 => newuser2sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
561 * d2b42f171248cec37a3c5c6b55404062 => NEWUSER2SAM:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
562 * fff8d790ff6c152aaeb6ebe17b4021de => NewUser2Sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
563 * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
564 * 2a7563c3715bc418d626dabef378c008 => NEWUSER2SAM:sub1.w2k3.vmnet1.vm.base:TestPwd2007
565 * c8e9557a87cd4200fda0c11d2fa03f96 => newuser2sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
566 * 221c55284451ae9b3aacaa2a3c86f10f => NewUser2Princ@sub1.w2k3.vmnet1.vm.base::TestPwd2007
567 * 74e1be668853d4324d38c07e2acfb8ea => (w2k3 has a bug here!) newuser2princ@sub1.w2k3.vmnet1.vm.base::TestPwd2007
568 * e1e244ab7f098e3ae1761be7f9229bbb => NEWUSER2PRINC@SUB1.W2K3.VMNET1.VM.BASE::TestPwd2007
569 * 86db637df42513039920e605499c3af6 => SUB1\NewUser2Sam::TestPwd2007
570 * f5e43474dfaf067fee8197a253debaa2 => sub1\newuser2sam::TestPwd2007
571 * 2ecaa8382e2518e4b77a52422b279467 => SUB1\NEWUSER2SAM::TestPwd2007
572 * 31dc704d3640335b2123d4ee28aa1f11 => ??? changes with NewUser2Sam => NewUser1Sam
573 * 36349f5cecd07320fb3bb0e119230c43 => ??? changes with NewUser2Sam => NewUser1Sam
574 * 12adf019d037fb535c01fd0608e78d9d => ??? changes with NewUser2Sam => NewUser1Sam
575 * 6feecf8e724906f3ee1105819c5105a1 => ??? changes with NewUser2Princ => NewUser1Princ
576 * 6c6911f3de6333422640221b9c51ff1f => ??? changes with NewUser2Princ => NewUser1Princ
577 * 4b279877e742895f9348ac67a8de2f69 => ??? changes with NewUser2Princ => NewUser1Princ
578 * db0c6bff069513e3ebb9870d29b57490 => ??? changes with NewUser2Sam => NewUser1Sam
579 * 45072621e56b1c113a4e04a8ff68cd0e => ??? changes with NewUser2Sam => NewUser1Sam
580 * 11d1220abc44a9c10cf91ef4a9c1de02 => ??? changes with NewUser2Sam => NewUser1Sam
582 * dn: CN=NewUser,OU=newtop,DC=sub1,DC=w2k3,DC=vmnet1,DC=vm,DC=base
583 * sAMAccountName: NewUser2Sam
585 * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
586 * b7ec9da91062199aee7d121e6710fe23 => newuser2sam:sub1:TestPwd2007
587 * 17d290bc5c9f463fac54c37a8cea134d => NEWUSER2SAM:SUB1:TestPwd2007
588 * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
589 * 5d57e7823938348127322e08cd81bcb5 => NewUser2Sam:sub1:TestPwd2007
590 * 07dd701bf8a011ece585de3d47237140 => NEWUSER2SAM:sub1:TestPwd2007
591 * e14fb0eb401498d2cb33c9aae1cc7f37 => newuser2sam:SUB1:TestPwd2007
592 * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
593 * f52da1266a6bdd290ffd48b2c823dda7 => newuser2sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
594 * d2b42f171248cec37a3c5c6b55404062 => NEWUSER2SAM:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
595 * fff8d790ff6c152aaeb6ebe17b4021de => NewUser2Sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
596 * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
597 * 2a7563c3715bc418d626dabef378c008 => NEWUSER2SAM:sub1.w2k3.vmnet1.vm.base:TestPwd2007
598 * c8e9557a87cd4200fda0c11d2fa03f96 => newuser2sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
599 * 8a140d30b6f0a5912735dc1e3bc993b4 => NewUser2Sam@sub1.w2k3.vmnet1.vm.base::TestPwd2007
600 * 86d95b2faae6cae4ec261e7fbaccf093 => (here w2k3 is correct) newuser2sam@sub1.w2k3.vmnet1.vm.base::TestPwd2007
601 * dfeff1493110220efcdfc6362e5f5450 => NEWUSER2SAM@SUB1.W2K3.VMNET1.VM.BASE::TestPwd2007
602 * 86db637df42513039920e605499c3af6 => SUB1\NewUser2Sam::TestPwd2007
603 * f5e43474dfaf067fee8197a253debaa2 => sub1\newuser2sam::TestPwd2007
604 * 2ecaa8382e2518e4b77a52422b279467 => SUB1\NEWUSER2SAM::TestPwd2007
605 * 31dc704d3640335b2123d4ee28aa1f11 => ???M1 changes with NewUser2Sam => NewUser1Sam
606 * 36349f5cecd07320fb3bb0e119230c43 => ???M1.L changes with newuser2sam => newuser1sam
607 * 12adf019d037fb535c01fd0608e78d9d => ???M1.U changes with NEWUSER2SAM => NEWUSER1SAM
608 * 569b4533f2d9e580211dd040e5e360a8 => ???M2 changes with NewUser2Princ => NewUser1Princ
609 * 52528bddf310a587c5d7e6a9ae2cbb20 => ???M2.L changes with newuser2princ => newuser1princ
610 * 4f629a4f0361289ca4255ab0f658fcd5 => ???M3 changes with NewUser2Princ => NewUser1Princ (doesn't depend on case of userPrincipal )
611 * db0c6bff069513e3ebb9870d29b57490 => ???M4 changes with NewUser2Sam => NewUser1Sam
612 * 45072621e56b1c113a4e04a8ff68cd0e => ???M5 changes with NewUser2Sam => NewUser1Sam (doesn't depend on case of sAMAccountName)
613 * 11d1220abc44a9c10cf91ef4a9c1de02 => ???M4.U changes with NEWUSER2SAM => NEWUSER1SAM
617 * sAMAccountName, netbios_domain
620 .user = &sAMAccountName,
621 .realm = &netbios_domain,
624 .user = &sAMAccountName_l,
625 .realm = &netbios_domain_l,
628 .user = &sAMAccountName_u,
629 .realm = &netbios_domain_u,
632 .user = &sAMAccountName,
633 .realm = &netbios_domain_u,
636 .user = &sAMAccountName,
637 .realm = &netbios_domain_l,
640 .user = &sAMAccountName_u,
641 .realm = &netbios_domain_l,
644 .user = &sAMAccountName_l,
645 .realm = &netbios_domain_u,
648 * sAMAccountName, dns_domain
651 .user = &sAMAccountName,
652 .realm = &dns_domain,
655 .user = &sAMAccountName_l,
656 .realm = &dns_domain_l,
659 .user = &sAMAccountName_u,
660 .realm = &dns_domain_u,
663 .user = &sAMAccountName,
664 .realm = &dns_domain_u,
667 .user = &sAMAccountName,
668 .realm = &dns_domain_l,
671 .user = &sAMAccountName_u,
672 .realm = &dns_domain_l,
675 .user = &sAMAccountName_l,
676 .realm = &dns_domain_u,
679 * userPrincipalName, no realm
682 .user = &userPrincipalName,
686 * NOTE: w2k3 messes this up, if the user has a real userPrincipalName,
687 * the fallback to the sAMAccountName based userPrincipalName is correct
689 .user = &userPrincipalName_l,
692 .user = &userPrincipalName_u,
695 * nt4dom\sAMAccountName, no realm
698 .user = &sAMAccountName,
699 .nt4dom = &netbios_domain
702 .user = &sAMAccountName_l,
703 .nt4dom = &netbios_domain_l
706 .user = &sAMAccountName_u,
707 .nt4dom = &netbios_domain_u
711 * the following ones are guessed depending on the technet2 article
712 * but not reproducable on a w2k3 server
714 /* sAMAccountName with "Digest" realm */
716 .user = &sAMAccountName,
720 .user = &sAMAccountName_l,
724 .user = &sAMAccountName_u,
727 /* userPrincipalName with "Digest" realm */
729 .user = &userPrincipalName,
733 .user = &userPrincipalName_l,
737 .user = &userPrincipalName_u,
740 /* nt4dom\\sAMAccountName with "Digest" realm */
742 .user = &sAMAccountName,
743 .nt4dom = &netbios_domain,
747 .user = &sAMAccountName_l,
748 .nt4dom = &netbios_domain_l,
752 .user = &sAMAccountName_u,
753 .nt4dom = &netbios_domain_u,
758 /* prepare DATA_BLOB's used in the combinations array */
759 sAMAccountName = data_blob_string_const(io->u.sAMAccountName);
760 sAMAccountName_l = data_blob_string_const(strlower_talloc(io->ac, io->u.sAMAccountName));
761 if (!sAMAccountName_l.data) {
762 ldb_oom(io->ac->module->ldb);
763 return LDB_ERR_OPERATIONS_ERROR;
765 sAMAccountName_u = data_blob_string_const(strupper_talloc(io->ac, io->u.sAMAccountName));
766 if (!sAMAccountName_u.data) {
767 ldb_oom(io->ac->module->ldb);
768 return LDB_ERR_OPERATIONS_ERROR;
771 /* if the user doesn't have a userPrincipalName, create one (with lower case realm) */
772 if (!user_principal_name) {
773 user_principal_name = talloc_asprintf(io->ac, "%s@%s",
774 io->u.sAMAccountName,
775 io->domain->dns_domain);
776 if (!user_principal_name) {
777 ldb_oom(io->ac->module->ldb);
778 return LDB_ERR_OPERATIONS_ERROR;
781 userPrincipalName = data_blob_string_const(user_principal_name);
782 userPrincipalName_l = data_blob_string_const(strlower_talloc(io->ac, user_principal_name));
783 if (!userPrincipalName_l.data) {
784 ldb_oom(io->ac->module->ldb);
785 return LDB_ERR_OPERATIONS_ERROR;
787 userPrincipalName_u = data_blob_string_const(strupper_talloc(io->ac, user_principal_name));
788 if (!userPrincipalName_u.data) {
789 ldb_oom(io->ac->module->ldb);
790 return LDB_ERR_OPERATIONS_ERROR;
793 netbios_domain = data_blob_string_const(io->domain->netbios_domain);
794 netbios_domain_l = data_blob_string_const(strlower_talloc(io->ac, io->domain->netbios_domain));
795 if (!netbios_domain_l.data) {
796 ldb_oom(io->ac->module->ldb);
797 return LDB_ERR_OPERATIONS_ERROR;
799 netbios_domain_u = data_blob_string_const(strupper_talloc(io->ac, io->domain->netbios_domain));
800 if (!netbios_domain_u.data) {
801 ldb_oom(io->ac->module->ldb);
802 return LDB_ERR_OPERATIONS_ERROR;
805 dns_domain = data_blob_string_const(io->domain->dns_domain);
806 dns_domain_l = data_blob_string_const(io->domain->dns_domain);
807 dns_domain_u = data_blob_string_const(io->domain->realm);
809 cleartext = data_blob_string_const(io->n.cleartext);
811 digest = data_blob_string_const("Digest");
813 delim = data_blob_string_const(":");
814 backslash = data_blob_string_const("\\");
816 pdb->num_hashes = ARRAY_SIZE(wdigest);
817 pdb->hashes = talloc_array(io->ac, struct package_PrimaryWDigestHash, pdb->num_hashes);
819 ldb_oom(io->ac->module->ldb);
820 return LDB_ERR_OPERATIONS_ERROR;
823 for (i=0; i < ARRAY_SIZE(wdigest); i++) {
824 struct MD5Context md5;
826 if (wdigest[i].nt4dom) {
827 MD5Update(&md5, wdigest[i].nt4dom->data, wdigest[i].nt4dom->length);
828 MD5Update(&md5, backslash.data, backslash.length);
830 MD5Update(&md5, wdigest[i].user->data, wdigest[i].user->length);
831 MD5Update(&md5, delim.data, delim.length);
832 if (wdigest[i].realm) {
833 MD5Update(&md5, wdigest[i].realm->data, wdigest[i].realm->length);
835 MD5Update(&md5, delim.data, delim.length);
836 MD5Update(&md5, cleartext.data, cleartext.length);
837 MD5Final(pdb->hashes[i].hash, &md5);
843 static int setup_supplemental_field(struct setup_password_fields_io *io)
845 struct supplementalCredentialsBlob scb;
846 struct supplementalCredentialsBlob _old_scb;
847 struct supplementalCredentialsBlob *old_scb = NULL;
848 /* Packages + (Kerberos, WDigest and maybe CLEARTEXT) */
849 uint32_t num_packages = 1 + 2;
850 struct supplementalCredentialsPackage packages[1+3];
851 struct supplementalCredentialsPackage *pp = &packages[0];
852 struct supplementalCredentialsPackage *pk = &packages[1];
853 struct supplementalCredentialsPackage *pd = &packages[2];
854 struct supplementalCredentialsPackage *pc = NULL;
855 struct package_PackagesBlob pb;
858 struct package_PrimaryKerberosBlob pkb;
861 struct package_PrimaryWDigestBlob pdb;
864 struct package_PrimaryCLEARTEXTBlob pcb;
868 enum ndr_err_code ndr_err;
873 if (!io->n.cleartext) {
875 * when we don't have a cleartext password
876 * we can't setup a supplementalCredential value
881 /* if there's an old supplementaCredentials blob then parse it */
882 if (io->o.supplemental) {
883 ndr_err = ndr_pull_struct_blob_all(io->o.supplemental, io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), &_old_scb,
884 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
885 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
886 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
887 ldb_asprintf_errstring(io->ac->module->ldb,
888 "setup_supplemental_field: "
889 "failed to pull old supplementalCredentialsBlob: %s",
891 return LDB_ERR_OPERATIONS_ERROR;
897 if (io->domain->store_cleartext &&
898 (io->u.user_account_control & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
903 /* Kerberos, WDigest, CLEARTEXT and termination(counted by the Packages element) */
904 pb.names = talloc_zero_array(io->ac, const char *, num_packages);
907 * setup 'Primary:Kerberos' element
909 pb.names[0] = "Kerberos";
911 ret = setup_primary_kerberos(io, old_scb, &pkb);
912 if (ret != LDB_SUCCESS) {
916 ndr_err = ndr_push_struct_blob(&pkb_blob, io->ac,
917 lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
919 (ndr_push_flags_fn_t)ndr_push_package_PrimaryKerberosBlob);
920 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
921 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
922 ldb_asprintf_errstring(io->ac->module->ldb,
923 "setup_supplemental_field: "
924 "failed to push package_PrimaryKerberosBlob: %s",
926 return LDB_ERR_OPERATIONS_ERROR;
931 * This is ugly, but we want to generate the same blob as
932 * w2k and w2k3...we should handle this in the idl
934 if (!data_blob_append(io->ac, &pkb_blob, zero16, sizeof(zero16))) {
935 ldb_oom(io->ac->module->ldb);
936 return LDB_ERR_OPERATIONS_ERROR;
938 pkb_hexstr = data_blob_hex_string(io->ac, &pkb_blob);
940 ldb_oom(io->ac->module->ldb);
941 return LDB_ERR_OPERATIONS_ERROR;
943 pk->name = "Primary:Kerberos";
945 pk->data = pkb_hexstr;
948 * setup 'Primary:WDigest' element
950 pb.names[1] = "WDigest";
952 ret = setup_primary_wdigest(io, old_scb, &pdb);
953 if (ret != LDB_SUCCESS) {
957 ndr_err = ndr_push_struct_blob(&pdb_blob, io->ac,
958 lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
960 (ndr_push_flags_fn_t)ndr_push_package_PrimaryWDigestBlob);
961 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
962 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
963 ldb_asprintf_errstring(io->ac->module->ldb,
964 "setup_supplemental_field: "
965 "failed to push package_PrimaryWDigestBlob: %s",
967 return LDB_ERR_OPERATIONS_ERROR;
969 pdb_hexstr = data_blob_hex_string(io->ac, &pdb_blob);
971 ldb_oom(io->ac->module->ldb);
972 return LDB_ERR_OPERATIONS_ERROR;
974 pd->name = "Primary:WDigest";
976 pd->data = pdb_hexstr;
979 * setup 'Primary:CLEARTEXT' element
982 pb.names[2] = "CLEARTEXT";
984 pcb.cleartext = io->n.cleartext;
986 ndr_err = ndr_push_struct_blob(&pcb_blob, io->ac,
987 lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
989 (ndr_push_flags_fn_t)ndr_push_package_PrimaryCLEARTEXTBlob);
990 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
991 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
992 ldb_asprintf_errstring(io->ac->module->ldb,
993 "setup_supplemental_field: "
994 "failed to push package_PrimaryCLEARTEXTBlob: %s",
996 return LDB_ERR_OPERATIONS_ERROR;
998 pcb_hexstr = data_blob_hex_string(io->ac, &pcb_blob);
1000 ldb_oom(io->ac->module->ldb);
1001 return LDB_ERR_OPERATIONS_ERROR;
1003 pc->name = "Primary:CLEARTEXT";
1005 pc->data = pcb_hexstr;
1009 * setup 'Packages' element
1011 ndr_err = ndr_push_struct_blob(&pb_blob, io->ac,
1012 lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1014 (ndr_push_flags_fn_t)ndr_push_package_PackagesBlob);
1015 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1016 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1017 ldb_asprintf_errstring(io->ac->module->ldb,
1018 "setup_supplemental_field: "
1019 "failed to push package_PackagesBlob: %s",
1021 return LDB_ERR_OPERATIONS_ERROR;
1023 pb_hexstr = data_blob_hex_string(io->ac, &pb_blob);
1025 ldb_oom(io->ac->module->ldb);
1026 return LDB_ERR_OPERATIONS_ERROR;
1028 pp->name = "Packages";
1030 pp->data = pb_hexstr;
1033 * setup 'supplementalCredentials' value
1035 scb.sub.num_packages = num_packages;
1036 scb.sub.packages = packages;
1038 ndr_err = ndr_push_struct_blob(&io->g.supplemental, io->ac,
1039 lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1041 (ndr_push_flags_fn_t)ndr_push_supplementalCredentialsBlob);
1042 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1043 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1044 ldb_asprintf_errstring(io->ac->module->ldb,
1045 "setup_supplemental_field: "
1046 "failed to push supplementalCredentialsBlob: %s",
1048 return LDB_ERR_OPERATIONS_ERROR;
1054 static int setup_last_set_field(struct setup_password_fields_io *io)
1057 unix_to_nt_time(&io->g.last_set, time(NULL));
1062 static int setup_kvno_field(struct setup_password_fields_io *io)
1064 /* increment by one */
1065 io->g.kvno = io->o.kvno + 1;
1070 static int setup_password_fields(struct setup_password_fields_io *io)
1076 * refuse the change if someone want to change the cleartext
1077 * and supply his own hashes at the same time...
1079 if (io->n.cleartext && (io->n.nt_hash || io->n.lm_hash)) {
1080 ldb_asprintf_errstring(io->ac->module->ldb,
1081 "setup_password_fields: "
1082 "it's only allowed to set the cleartext password or the password hashes");
1083 return LDB_ERR_UNWILLING_TO_PERFORM;
1086 if (io->n.cleartext && !io->n.nt_hash) {
1087 struct samr_Password *hash;
1089 hash = talloc(io->ac, struct samr_Password);
1091 ldb_oom(io->ac->module->ldb);
1092 return LDB_ERR_OPERATIONS_ERROR;
1095 /* compute the new nt hash */
1096 ok = E_md4hash(io->n.cleartext, hash->hash);
1098 io->n.nt_hash = hash;
1100 ldb_asprintf_errstring(io->ac->module->ldb,
1101 "setup_password_fields: "
1102 "failed to generate nthash from cleartext password");
1103 return LDB_ERR_OPERATIONS_ERROR;
1107 if (io->n.cleartext && !io->n.lm_hash) {
1108 struct samr_Password *hash;
1110 hash = talloc(io->ac, struct samr_Password);
1112 ldb_oom(io->ac->module->ldb);
1113 return LDB_ERR_OPERATIONS_ERROR;
1116 /* compute the new lm hash */
1117 ok = E_deshash(io->n.cleartext, hash->hash);
1119 io->n.lm_hash = hash;
1121 talloc_free(hash->hash);
1125 ret = setup_nt_fields(io);
1130 ret = setup_lm_fields(io);
1135 ret = setup_supplemental_field(io);
1140 ret = setup_last_set_field(io);
1145 ret = setup_kvno_field(io);
1153 static struct ldb_handle *ph_init_handle(struct ldb_request *req, struct ldb_module *module, enum ph_type type)
1155 struct ph_context *ac;
1156 struct ldb_handle *h;
1158 h = talloc_zero(req, struct ldb_handle);
1160 ldb_set_errstring(module->ldb, "Out of Memory");
1166 ac = talloc_zero(h, struct ph_context);
1168 ldb_set_errstring(module->ldb, "Out of Memory");
1173 h->private_data = (void *)ac;
1175 h->state = LDB_ASYNC_INIT;
1176 h->status = LDB_SUCCESS;
1179 ac->module = module;
1185 static int get_domain_data_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1187 struct ph_context *ac;
1189 ac = talloc_get_type(context, struct ph_context);
1191 /* we are interested only in the single reply (base search) we receive here */
1192 if (ares->type == LDB_REPLY_ENTRY) {
1193 if (ac->dom_res != NULL) {
1194 ldb_set_errstring(ldb, "Too many results");
1196 return LDB_ERR_OPERATIONS_ERROR;
1198 ac->dom_res = talloc_steal(ac, ares);
1206 static int build_domain_data_request(struct ph_context *ac)
1208 /* attrs[] is returned from this function in
1209 ac->dom_req->op.search.attrs, so it must be static, as
1210 otherwise the compiler can put it on the stack */
1211 static const char * const attrs[] = { "pwdProperties", "pwdHistoryLength", NULL };
1214 ac->dom_req = talloc_zero(ac, struct ldb_request);
1215 if (ac->dom_req == NULL) {
1216 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
1217 return LDB_ERR_OPERATIONS_ERROR;
1219 ac->dom_req->operation = LDB_SEARCH;
1220 ac->dom_req->op.search.base = ldb_get_default_basedn(ac->module->ldb);
1221 ac->dom_req->op.search.scope = LDB_SCOPE_SUBTREE;
1223 filter = talloc_asprintf(ac->dom_req, "(&(objectSid=%s)(|(objectClass=domain)(objectClass=builtinDomain)))",
1224 ldap_encode_ndr_dom_sid(ac->dom_req, ac->domain_sid));
1225 if (filter == NULL) {
1226 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
1227 talloc_free(ac->dom_req);
1228 return LDB_ERR_OPERATIONS_ERROR;
1231 ac->dom_req->op.search.tree = ldb_parse_tree(ac->dom_req, filter);
1232 if (ac->dom_req->op.search.tree == NULL) {
1233 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
1234 talloc_free(ac->dom_req);
1235 return LDB_ERR_OPERATIONS_ERROR;
1237 ac->dom_req->op.search.attrs = attrs;
1238 ac->dom_req->controls = NULL;
1239 ac->dom_req->context = ac;
1240 ac->dom_req->callback = get_domain_data_callback;
1241 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->dom_req);
1246 static struct domain_data *get_domain_data(struct ldb_module *module, void *ctx, struct ldb_reply *res)
1248 struct domain_data *data;
1250 struct ph_context *ac;
1253 ac = talloc_get_type(ctx, struct ph_context);
1255 data = talloc_zero(ac, struct domain_data);
1261 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Could not find this user's domain: %s!\n", dom_sid_string(data, ac->domain_sid));
1266 data->pwdProperties= samdb_result_uint(res->message, "pwdProperties", 0);
1267 data->store_cleartext = data->pwdProperties & DOMAIN_PASSWORD_STORE_CLEARTEXT;
1268 data->pwdHistoryLength = samdb_result_uint(res->message, "pwdHistoryLength", 0);
1270 /* For a domain DN, this puts things in dotted notation */
1271 /* For builtin domains, this will give details for the host,
1272 * but that doesn't really matter, as it's just used for salt
1273 * and kerberos principals, which don't exist here */
1275 tmp = ldb_dn_canonical_string(ctx, res->message->dn);
1280 /* But it puts a trailing (or just before 'builtin') / on things, so kill that */
1281 p = strchr(tmp, '/');
1287 data->dns_domain = strlower_talloc(data, tmp);
1288 if (data->dns_domain == NULL) {
1289 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
1292 data->realm = strupper_talloc(data, tmp);
1293 if (data->realm == NULL) {
1294 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
1297 p = strchr(tmp, '.');
1301 data->netbios_domain = strupper_talloc(data, tmp);
1302 if (data->netbios_domain == NULL) {
1303 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
1311 static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
1313 struct ldb_handle *h;
1314 struct ph_context *ac;
1315 struct ldb_message_element *sambaAttr;
1316 struct ldb_message_element *ntAttr;
1317 struct ldb_message_element *lmAttr;
1320 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
1322 if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
1323 return ldb_next_request(module, req);
1326 /* If the caller is manipulating the local passwords directly, let them pass */
1327 if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
1328 req->op.add.message->dn) == 0) {
1329 return ldb_next_request(module, req);
1332 /* nobody must touch this fields */
1333 if (ldb_msg_find_element(req->op.add.message, "ntPwdHistory")) {
1334 return LDB_ERR_UNWILLING_TO_PERFORM;
1336 if (ldb_msg_find_element(req->op.add.message, "lmPwdHistory")) {
1337 return LDB_ERR_UNWILLING_TO_PERFORM;
1339 if (ldb_msg_find_element(req->op.add.message, "supplementalCredentials")) {
1340 return LDB_ERR_UNWILLING_TO_PERFORM;
1343 /* If no part of this ADD touches the sambaPassword, or the NT
1344 * or LM hashes, then we don't need to make any changes. */
1346 sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
1347 ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
1348 lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
1350 if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
1351 return ldb_next_request(module, req);
1354 /* if it is not an entry of type person its an error */
1355 /* TODO: remove this when sambaPassword will be in schema */
1356 if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
1357 ldb_set_errstring(module->ldb, "Cannot set a password on entry that does not have objectClass 'person'");
1358 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1361 /* check sambaPassword is single valued here */
1362 /* TODO: remove this when sambaPassword will be single valued in schema */
1363 if (sambaAttr && sambaAttr->num_values > 1) {
1364 ldb_set_errstring(module->ldb, "mupltiple values for sambaPassword not allowed!\n");
1365 return LDB_ERR_CONSTRAINT_VIOLATION;
1368 if (ntAttr && (ntAttr->num_values > 1)) {
1369 ldb_set_errstring(module->ldb, "mupltiple values for unicodePwd not allowed!\n");
1370 return LDB_ERR_CONSTRAINT_VIOLATION;
1372 if (lmAttr && (lmAttr->num_values > 1)) {
1373 ldb_set_errstring(module->ldb, "mupltiple values for dBCSPwd not allowed!\n");
1374 return LDB_ERR_CONSTRAINT_VIOLATION;
1377 if (sambaAttr && sambaAttr->num_values == 0) {
1378 ldb_set_errstring(module->ldb, "sambaPassword must have a value!\n");
1379 return LDB_ERR_CONSTRAINT_VIOLATION;
1382 if (ntAttr && (ntAttr->num_values == 0)) {
1383 ldb_set_errstring(module->ldb, "unicodePwd must have a value!\n");
1384 return LDB_ERR_CONSTRAINT_VIOLATION;
1386 if (lmAttr && (lmAttr->num_values == 0)) {
1387 ldb_set_errstring(module->ldb, "dBCSPwd must have a value!\n");
1388 return LDB_ERR_CONSTRAINT_VIOLATION;
1391 h = ph_init_handle(req, module, PH_ADD);
1393 return LDB_ERR_OPERATIONS_ERROR;
1395 ac = talloc_get_type(h->private_data, struct ph_context);
1397 /* get user domain data */
1398 ac->domain_sid = samdb_result_sid_prefix(ac, req->op.add.message, "objectSid");
1399 if (ac->domain_sid == NULL) {
1400 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
1401 return LDB_ERR_OPERATIONS_ERROR;
1404 ret = build_domain_data_request(ac);
1405 if (ret != LDB_SUCCESS) {
1409 ac->step = PH_ADD_SEARCH_DOM;
1413 return ldb_next_request(module, ac->dom_req);
1416 static int password_hash_add_do_add(struct ldb_handle *h) {
1418 struct ph_context *ac;
1419 struct domain_data *domain;
1420 struct smb_krb5_context *smb_krb5_context;
1421 struct ldb_message *msg;
1422 struct setup_password_fields_io io;
1425 ac = talloc_get_type(h->private_data, struct ph_context);
1427 domain = get_domain_data(ac->module, ac, ac->dom_res);
1428 if (domain == NULL) {
1429 return LDB_ERR_OPERATIONS_ERROR;
1432 ac->down_req = talloc(ac, struct ldb_request);
1433 if (ac->down_req == NULL) {
1434 return LDB_ERR_OPERATIONS_ERROR;
1437 *(ac->down_req) = *(ac->orig_req);
1438 ac->down_req->op.add.message = msg = ldb_msg_copy_shallow(ac->down_req, ac->orig_req->op.add.message);
1439 if (ac->down_req->op.add.message == NULL) {
1440 return LDB_ERR_OPERATIONS_ERROR;
1443 /* Some operations below require kerberos contexts */
1444 if (smb_krb5_init_context(ac->down_req,
1445 ldb_get_opaque(h->module->ldb, "EventContext"),
1446 (struct loadparm_context *)ldb_get_opaque(h->module->ldb, "loadparm"),
1447 &smb_krb5_context) != 0) {
1448 return LDB_ERR_OPERATIONS_ERROR;
1454 io.smb_krb5_context = smb_krb5_context;
1456 io.u.user_account_control = samdb_result_uint(msg, "userAccountControl", 0);
1457 io.u.sAMAccountName = samdb_result_string(msg, "samAccountName", NULL);
1458 io.u.user_principal_name = samdb_result_string(msg, "userPrincipalName", NULL);
1459 io.u.is_computer = ldb_msg_check_string_attribute(msg, "objectClass", "computer");
1461 io.n.cleartext = samdb_result_string(msg, "sambaPassword", NULL);
1462 io.n.nt_hash = samdb_result_hash(io.ac, msg, "unicodePwd");
1463 io.n.lm_hash = samdb_result_hash(io.ac, msg, "dBCSPwd");
1465 /* remove attributes */
1466 if (io.n.cleartext) ldb_msg_remove_attr(msg, "sambaPassword");
1467 if (io.n.nt_hash) ldb_msg_remove_attr(msg, "unicodePwd");
1468 if (io.n.lm_hash) ldb_msg_remove_attr(msg, "dBCSPwd");
1469 ldb_msg_remove_attr(msg, "pwdLastSet");
1470 io.o.kvno = samdb_result_uint(msg, "msDs-KeyVersionNumber", 1) - 1;
1471 ldb_msg_remove_attr(msg, "msDs-KeyVersionNumber");
1473 ret = setup_password_fields(&io);
1474 if (ret != LDB_SUCCESS) {
1479 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
1480 "unicodePwd", io.g.nt_hash);
1481 if (ret != LDB_SUCCESS) {
1486 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
1487 "dBCSPwd", io.g.lm_hash);
1488 if (ret != LDB_SUCCESS) {
1492 if (io.g.nt_history_len > 0) {
1493 ret = samdb_msg_add_hashes(ac, msg,
1496 io.g.nt_history_len);
1497 if (ret != LDB_SUCCESS) {
1501 if (io.g.lm_history_len > 0) {
1502 ret = samdb_msg_add_hashes(ac, msg,
1505 io.g.lm_history_len);
1506 if (ret != LDB_SUCCESS) {
1510 if (io.g.supplemental.length > 0) {
1511 ret = ldb_msg_add_value(msg, "supplementalCredentials",
1512 &io.g.supplemental, NULL);
1513 if (ret != LDB_SUCCESS) {
1517 ret = samdb_msg_add_uint64(ac->module->ldb, ac, msg,
1520 if (ret != LDB_SUCCESS) {
1523 ret = samdb_msg_add_uint(ac->module->ldb, ac, msg,
1524 "msDs-KeyVersionNumber",
1526 if (ret != LDB_SUCCESS) {
1530 h->state = LDB_ASYNC_INIT;
1531 h->status = LDB_SUCCESS;
1533 ac->step = PH_ADD_DO_ADD;
1535 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->down_req);
1537 /* perform the operation */
1538 return ldb_next_request(ac->module, ac->down_req);
1541 static int password_hash_mod_search_self(struct ldb_handle *h);
1543 static int password_hash_modify(struct ldb_module *module, struct ldb_request *req)
1545 struct ldb_handle *h;
1546 struct ph_context *ac;
1547 struct ldb_message_element *sambaAttr;
1548 struct ldb_message_element *ntAttr;
1549 struct ldb_message_element *lmAttr;
1550 struct ldb_message *msg;
1552 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
1554 if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
1555 return ldb_next_request(module, req);
1558 /* If the caller is manipulating the local passwords directly, let them pass */
1559 if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
1560 req->op.mod.message->dn) == 0) {
1561 return ldb_next_request(module, req);
1564 /* nobody must touch password Histories */
1565 if (ldb_msg_find_element(req->op.add.message, "ntPwdHistory")) {
1566 return LDB_ERR_UNWILLING_TO_PERFORM;
1568 if (ldb_msg_find_element(req->op.add.message, "lmPwdHistory")) {
1569 return LDB_ERR_UNWILLING_TO_PERFORM;
1571 if (ldb_msg_find_element(req->op.add.message, "supplementalCredentials")) {
1572 return LDB_ERR_UNWILLING_TO_PERFORM;
1575 sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
1576 ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
1577 lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
1579 /* If no part of this touches the sambaPassword OR unicodePwd and/or dBCSPwd, then we don't
1580 * need to make any changes. For password changes/set there should
1581 * be a 'delete' or a 'modify' on this attribute. */
1582 if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
1583 return ldb_next_request(module, req);
1586 /* check passwords are single valued here */
1587 /* TODO: remove this when passwords will be single valued in schema */
1588 if (sambaAttr && (sambaAttr->num_values > 1)) {
1589 return LDB_ERR_CONSTRAINT_VIOLATION;
1591 if (ntAttr && (ntAttr->num_values > 1)) {
1592 return LDB_ERR_CONSTRAINT_VIOLATION;
1594 if (lmAttr && (lmAttr->num_values > 1)) {
1595 return LDB_ERR_CONSTRAINT_VIOLATION;
1598 h = ph_init_handle(req, module, PH_MOD);
1600 return LDB_ERR_OPERATIONS_ERROR;
1602 ac = talloc_get_type(h->private_data, struct ph_context);
1604 /* return or own handle to deal with this call */
1607 /* prepare the first operation */
1608 ac->down_req = talloc_zero(ac, struct ldb_request);
1609 if (ac->down_req == NULL) {
1610 ldb_set_errstring(module->ldb, "Out of memory!");
1611 return LDB_ERR_OPERATIONS_ERROR;
1614 *(ac->down_req) = *req; /* copy the request */
1616 /* use a new message structure so that we can modify it */
1617 ac->down_req->op.mod.message = msg = ldb_msg_copy_shallow(ac->down_req, req->op.mod.message);
1619 /* - remove any imodification to the password from the first commit
1620 * we will make the real modification later */
1621 if (sambaAttr) ldb_msg_remove_attr(msg, "sambaPassword");
1622 if (ntAttr) ldb_msg_remove_attr(msg, "unicodePwd");
1623 if (lmAttr) ldb_msg_remove_attr(msg, "dBCSPwd");
1625 /* if there was nothing else to be modify skip to next step */
1626 if (msg->num_elements == 0) {
1627 talloc_free(ac->down_req);
1628 ac->down_req = NULL;
1629 return password_hash_mod_search_self(h);
1632 ac->down_req->context = NULL;
1633 ac->down_req->callback = NULL;
1635 ac->step = PH_MOD_DO_REQ;
1637 ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
1639 return ldb_next_request(module, ac->down_req);
1642 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1644 struct ph_context *ac;
1646 ac = talloc_get_type(context, struct ph_context);
1648 /* we are interested only in the single reply (base search) we receive here */
1649 if (ares->type == LDB_REPLY_ENTRY) {
1650 if (ac->search_res != NULL) {
1651 ldb_set_errstring(ldb, "Too many results");
1653 return LDB_ERR_OPERATIONS_ERROR;
1656 /* if it is not an entry of type person this is an error */
1657 /* TODO: remove this when sambaPassword will be in schema */
1658 if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
1659 ldb_set_errstring(ldb, "Object class violation");
1661 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1664 ac->search_res = talloc_steal(ac, ares);
1672 static int password_hash_mod_search_self(struct ldb_handle *h) {
1674 struct ph_context *ac;
1675 static const char * const attrs[] = { "userAccountControl", "lmPwdHistory",
1677 "objectSid", "msDS-KeyVersionNumber",
1678 "objectClass", "userPrincipalName",
1680 "dBCSPwd", "unicodePwd",
1681 "supplementalCredentials",
1684 ac = talloc_get_type(h->private_data, struct ph_context);
1686 /* prepare the search operation */
1687 ac->search_req = talloc_zero(ac, struct ldb_request);
1688 if (ac->search_req == NULL) {
1689 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
1690 return LDB_ERR_OPERATIONS_ERROR;
1693 ac->search_req->operation = LDB_SEARCH;
1694 ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
1695 ac->search_req->op.search.scope = LDB_SCOPE_BASE;
1696 ac->search_req->op.search.tree = ldb_parse_tree(ac->search_req, NULL);
1697 if (ac->search_req->op.search.tree == NULL) {
1698 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
1699 return LDB_ERR_OPERATIONS_ERROR;
1701 ac->search_req->op.search.attrs = attrs;
1702 ac->search_req->controls = NULL;
1703 ac->search_req->context = ac;
1704 ac->search_req->callback = get_self_callback;
1705 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
1707 ac->step = PH_MOD_SEARCH_SELF;
1709 return ldb_next_request(ac->module, ac->search_req);
1712 static int password_hash_mod_search_dom(struct ldb_handle *h) {
1714 struct ph_context *ac;
1717 ac = talloc_get_type(h->private_data, struct ph_context);
1719 /* get object domain sid */
1720 ac->domain_sid = samdb_result_sid_prefix(ac, ac->search_res->message, "objectSid");
1721 if (ac->domain_sid == NULL) {
1722 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
1723 return LDB_ERR_OPERATIONS_ERROR;
1726 /* get user domain data */
1727 ret = build_domain_data_request(ac);
1728 if (ret != LDB_SUCCESS) {
1732 ac->step = PH_MOD_SEARCH_DOM;
1734 return ldb_next_request(ac->module, ac->dom_req);
1737 static int password_hash_mod_do_mod(struct ldb_handle *h) {
1739 struct ph_context *ac;
1740 struct domain_data *domain;
1741 struct smb_krb5_context *smb_krb5_context;
1742 struct ldb_message *msg;
1743 struct ldb_message *orig_msg;
1744 struct ldb_message *searched_msg;
1745 struct setup_password_fields_io io;
1748 ac = talloc_get_type(h->private_data, struct ph_context);
1750 domain = get_domain_data(ac->module, ac, ac->dom_res);
1751 if (domain == NULL) {
1752 return LDB_ERR_OPERATIONS_ERROR;
1755 ac->mod_req = talloc(ac, struct ldb_request);
1756 if (ac->mod_req == NULL) {
1757 return LDB_ERR_OPERATIONS_ERROR;
1760 *(ac->mod_req) = *(ac->orig_req);
1762 /* use a new message structure so that we can modify it */
1763 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
1765 return LDB_ERR_OPERATIONS_ERROR;
1769 msg->dn = ac->orig_req->op.mod.message->dn;
1771 /* Some operations below require kerberos contexts */
1772 if (smb_krb5_init_context(ac->mod_req,
1773 ldb_get_opaque(h->module->ldb, "EventContext"),
1774 (struct loadparm_context *)ldb_get_opaque(h->module->ldb, "loadparm"),
1775 &smb_krb5_context) != 0) {
1776 return LDB_ERR_OPERATIONS_ERROR;
1779 orig_msg = discard_const(ac->orig_req->op.mod.message);
1780 searched_msg = ac->search_res->message;
1785 io.smb_krb5_context = smb_krb5_context;
1787 io.u.user_account_control = samdb_result_uint(searched_msg, "userAccountControl", 0);
1788 io.u.sAMAccountName = samdb_result_string(searched_msg, "samAccountName", NULL);
1789 io.u.user_principal_name = samdb_result_string(searched_msg, "userPrincipalName", NULL);
1790 io.u.is_computer = ldb_msg_check_string_attribute(searched_msg, "objectClass", "computer");
1792 io.n.cleartext = samdb_result_string(orig_msg, "sambaPassword", NULL);
1793 io.n.nt_hash = samdb_result_hash(io.ac, orig_msg, "unicodePwd");
1794 io.n.lm_hash = samdb_result_hash(io.ac, orig_msg, "dBCSPwd");
1796 io.o.kvno = samdb_result_uint(searched_msg, "msDs-KeyVersionNumber", 0);
1797 io.o.nt_history_len = samdb_result_hashes(io.ac, searched_msg, "ntPwdHistory", &io.o.nt_history);
1798 io.o.lm_history_len = samdb_result_hashes(io.ac, searched_msg, "lmPwdHistory", &io.o.lm_history);
1799 io.o.supplemental = ldb_msg_find_ldb_val(searched_msg, "supplementalCredentials");
1801 ret = setup_password_fields(&io);
1802 if (ret != LDB_SUCCESS) {
1806 /* make sure we replace all the old attributes */
1807 ret = ldb_msg_add_empty(msg, "unicodePwd", LDB_FLAG_MOD_REPLACE, NULL);
1808 ret = ldb_msg_add_empty(msg, "dBCSPwd", LDB_FLAG_MOD_REPLACE, NULL);
1809 ret = ldb_msg_add_empty(msg, "ntPwdHistory", LDB_FLAG_MOD_REPLACE, NULL);
1810 ret = ldb_msg_add_empty(msg, "lmPwdHistory", LDB_FLAG_MOD_REPLACE, NULL);
1811 ret = ldb_msg_add_empty(msg, "supplementalCredentials", LDB_FLAG_MOD_REPLACE, NULL);
1812 ret = ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_REPLACE, NULL);
1813 ret = ldb_msg_add_empty(msg, "msDs-KeyVersionNumber", LDB_FLAG_MOD_REPLACE, NULL);
1816 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
1817 "unicodePwd", io.g.nt_hash);
1818 if (ret != LDB_SUCCESS) {
1823 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
1824 "dBCSPwd", io.g.lm_hash);
1825 if (ret != LDB_SUCCESS) {
1829 if (io.g.nt_history_len > 0) {
1830 ret = samdb_msg_add_hashes(ac, msg,
1833 io.g.nt_history_len);
1834 if (ret != LDB_SUCCESS) {
1838 if (io.g.lm_history_len > 0) {
1839 ret = samdb_msg_add_hashes(ac, msg,
1842 io.g.lm_history_len);
1843 if (ret != LDB_SUCCESS) {
1847 if (io.g.supplemental.length > 0) {
1848 ret = ldb_msg_add_value(msg, "supplementalCredentials",
1849 &io.g.supplemental, NULL);
1850 if (ret != LDB_SUCCESS) {
1854 ret = samdb_msg_add_uint64(ac->module->ldb, ac, msg,
1857 if (ret != LDB_SUCCESS) {
1860 ret = samdb_msg_add_uint(ac->module->ldb, ac, msg,
1861 "msDs-KeyVersionNumber",
1863 if (ret != LDB_SUCCESS) {
1867 h->state = LDB_ASYNC_INIT;
1868 h->status = LDB_SUCCESS;
1870 ac->step = PH_MOD_DO_MOD;
1872 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
1874 /* perform the search */
1875 return ldb_next_request(ac->module, ac->mod_req);
1878 static int ph_wait(struct ldb_handle *handle) {
1879 struct ph_context *ac;
1882 if (!handle || !handle->private_data) {
1883 return LDB_ERR_OPERATIONS_ERROR;
1886 if (handle->state == LDB_ASYNC_DONE) {
1887 return handle->status;
1890 handle->state = LDB_ASYNC_PENDING;
1891 handle->status = LDB_SUCCESS;
1893 ac = talloc_get_type(handle->private_data, struct ph_context);
1896 case PH_ADD_SEARCH_DOM:
1897 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1899 if (ret != LDB_SUCCESS) {
1900 handle->status = ret;
1903 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1904 handle->status = ac->dom_req->handle->status;
1908 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1912 /* domain search done, go on */
1913 return password_hash_add_do_add(handle);
1916 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1918 if (ret != LDB_SUCCESS) {
1919 handle->status = ret;
1922 if (ac->down_req->handle->status != LDB_SUCCESS) {
1923 handle->status = ac->down_req->handle->status;
1927 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1934 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1936 if (ret != LDB_SUCCESS) {
1937 handle->status = ret;
1940 if (ac->down_req->handle->status != LDB_SUCCESS) {
1941 handle->status = ac->down_req->handle->status;
1945 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1949 /* non-password mods done, go on */
1950 return password_hash_mod_search_self(handle);
1952 case PH_MOD_SEARCH_SELF:
1953 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1955 if (ret != LDB_SUCCESS) {
1956 handle->status = ret;
1959 if (ac->search_req->handle->status != LDB_SUCCESS) {
1960 handle->status = ac->search_req->handle->status;
1964 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1968 if (ac->search_res == NULL) {
1969 return LDB_ERR_NO_SUCH_OBJECT;
1972 /* self search done, go on */
1973 return password_hash_mod_search_dom(handle);
1975 case PH_MOD_SEARCH_DOM:
1976 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1978 if (ret != LDB_SUCCESS) {
1979 handle->status = ret;
1982 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1983 handle->status = ac->dom_req->handle->status;
1987 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1991 /* domain search done, go on */
1992 return password_hash_mod_do_mod(handle);
1995 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
1997 if (ret != LDB_SUCCESS) {
1998 handle->status = ret;
2001 if (ac->mod_req->handle->status != LDB_SUCCESS) {
2002 handle->status = ac->mod_req->handle->status;
2006 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
2013 ret = LDB_ERR_OPERATIONS_ERROR;
2020 handle->state = LDB_ASYNC_DONE;
2024 static int ph_wait_all(struct ldb_handle *handle) {
2028 while (handle->state != LDB_ASYNC_DONE) {
2029 ret = ph_wait(handle);
2030 if (ret != LDB_SUCCESS) {
2035 return handle->status;
2038 static int password_hash_wait(struct ldb_handle *handle, enum ldb_wait_type type)
2040 if (type == LDB_WAIT_ALL) {
2041 return ph_wait_all(handle);
2043 return ph_wait(handle);
2047 _PUBLIC_ const struct ldb_module_ops ldb_password_hash_module_ops = {
2048 .name = "password_hash",
2049 .add = password_hash_add,
2050 .modify = password_hash_modify,
2051 .wait = password_hash_wait