password_hash: add generation of the Primary:Kerberos-Newer-Keys blob
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / password_hash.c
1 /* 
2    ldb database module
3
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
8
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.
13    
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.
18    
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/>.
21 */
22
23 /*
24  *  Name: ldb
25  *
26  *  Component: ldb password_hash module
27  *
28  *  Description: correctly update hash values based on changes to userPassword and friends
29  *
30  *  Author: Andrew Bartlett
31  *  Author: Stefan Metzmacher
32  */
33
34 #include "includes.h"
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"
53
54 /* If we have decided there is reason to work on this request, then
55  * setup all the password hash types correctly.
56  *
57  * If the administrator doesn't want the userPassword stored (set in the
58  * domain and per-account policies) then we must strip that out before
59  * we do the first operation.
60  *
61  * Once this is done (which could update anything at all), we
62  * calculate the password hashes.
63  *
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...
68  *
69  * Finally, if the administrator has requested that a password history
70  * be maintained, then this should also be written out.
71  *
72  */
73
74 struct ph_context {
75
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;
78
79         struct ldb_module *module;
80         struct ldb_request *orig_req;
81
82         struct ldb_request *dom_req;
83         struct ldb_reply *dom_res;
84
85         struct ldb_request *down_req;
86
87         struct ldb_request *search_req;
88         struct ldb_reply *search_res;
89
90         struct ldb_request *mod_req;
91
92         struct dom_sid *domain_sid;
93 };
94
95 struct domain_data {
96         bool store_cleartext;
97         uint_t pwdProperties;
98         uint_t pwdHistoryLength;
99         char *netbios_domain;
100         char *dns_domain;
101         char *realm;
102 };
103
104 struct setup_password_fields_io {
105         struct ph_context *ac;
106         struct domain_data *domain;
107         struct smb_krb5_context *smb_krb5_context;
108
109         /* infos about the user account */
110         struct {
111                 uint32_t user_account_control;
112                 const char *sAMAccountName;
113                 const char *user_principal_name;
114                 bool is_computer;
115         } u;
116
117         /* new credentials */
118         struct {
119                 const char *cleartext;
120                 struct samr_Password *nt_hash;
121                 struct samr_Password *lm_hash;
122         } n;
123
124         /* old credentials */
125         struct {
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;
132                 uint32_t kvno;
133         } o;
134
135         /* generated credentials */
136         struct {
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                 const char *salt;
144                 DATA_BLOB aes_256;
145                 DATA_BLOB aes_128;
146                 DATA_BLOB des_md5;
147                 DATA_BLOB des_crc;
148                 struct ldb_val supplemental;
149                 NTTIME last_set;
150                 uint32_t kvno;
151         } g;
152 };
153
154 static int setup_nt_fields(struct setup_password_fields_io *io)
155 {
156         uint32_t i;
157
158         io->g.nt_hash = io->n.nt_hash;
159
160         if (io->domain->pwdHistoryLength == 0) {
161                 return LDB_SUCCESS;
162         }
163
164         /* We might not have an old NT password */
165         io->g.nt_history = talloc_array(io->ac,
166                                         struct samr_Password,
167                                         io->domain->pwdHistoryLength);
168         if (!io->g.nt_history) {
169                 ldb_oom(io->ac->module->ldb);
170                 return LDB_ERR_OPERATIONS_ERROR;
171         }
172
173         for (i = 0; i < MIN(io->domain->pwdHistoryLength-1, io->o.nt_history_len); i++) {
174                 io->g.nt_history[i+1] = io->o.nt_history[i];
175         }
176         io->g.nt_history_len = i + 1;
177
178         if (io->g.nt_hash) {
179                 io->g.nt_history[0] = *io->g.nt_hash;
180         } else {
181                 /* 
182                  * TODO: is this correct?
183                  * the simular behavior is correct for the lm history case
184                  */
185                 E_md4hash("", io->g.nt_history[0].hash);
186         }
187
188         return LDB_SUCCESS;
189 }
190
191 static int setup_lm_fields(struct setup_password_fields_io *io)
192 {
193         uint32_t i;
194
195         io->g.lm_hash = io->n.lm_hash;
196
197         if (io->domain->pwdHistoryLength == 0) {
198                 return LDB_SUCCESS;
199         }
200
201         /* We might not have an old NT password */
202         io->g.lm_history = talloc_array(io->ac,
203                                         struct samr_Password,
204                                         io->domain->pwdHistoryLength);
205         if (!io->g.lm_history) {
206                 ldb_oom(io->ac->module->ldb);
207                 return LDB_ERR_OPERATIONS_ERROR;
208         }
209
210         for (i = 0; i < MIN(io->domain->pwdHistoryLength-1, io->o.lm_history_len); i++) {
211                 io->g.lm_history[i+1] = io->o.lm_history[i];
212         }
213         io->g.lm_history_len = i + 1;
214
215         if (io->g.lm_hash) {
216                 io->g.lm_history[0] = *io->g.lm_hash;
217         } else {
218                 E_deshash("", io->g.lm_history[0].hash);
219         }
220
221         return LDB_SUCCESS;
222 }
223
224 static int setup_kerberos_keys(struct setup_password_fields_io *io)
225 {
226         krb5_error_code krb5_ret;
227         Principal *salt_principal;
228         krb5_salt salt;
229         krb5_keyblock key;
230
231         /* Many, many thanks to lukeh@padl.com for this
232          * algorithm, described in his Nov 10 2004 mail to
233          * samba-technical@samba.org */
234
235         /*
236          * Determine a salting principal
237          */
238         if (io->u.is_computer) {
239                 char *name;
240                 char *saltbody;
241
242                 name = talloc_strdup(io->ac, io->u.sAMAccountName);
243                 if (!name) {
244                         ldb_oom(io->ac->module->ldb);
245                         return LDB_ERR_OPERATIONS_ERROR;
246                 }
247
248                 if (name[strlen(name)-1] == '$') {
249                         name[strlen(name)-1] = '\0';
250                 }
251
252                 saltbody = talloc_asprintf(io->ac, "%s.%s", name, io->domain->dns_domain);
253                 if (!saltbody) {
254                         ldb_oom(io->ac->module->ldb);
255                         return LDB_ERR_OPERATIONS_ERROR;
256                 }
257                 
258                 krb5_ret = krb5_make_principal(io->smb_krb5_context->krb5_context,
259                                                &salt_principal,
260                                                io->domain->realm, "host",
261                                                saltbody, NULL);
262         } else if (io->u.user_principal_name) {
263                 char *user_principal_name;
264                 char *p;
265
266                 user_principal_name = talloc_strdup(io->ac, io->u.user_principal_name);
267                 if (!user_principal_name) {
268                         ldb_oom(io->ac->module->ldb);
269                         return LDB_ERR_OPERATIONS_ERROR;
270                 }
271
272                 p = strchr(user_principal_name, '@');
273                 if (p) {
274                         p[0] = '\0';
275                 }
276
277                 krb5_ret = krb5_make_principal(io->smb_krb5_context->krb5_context,
278                                                &salt_principal,
279                                                io->domain->realm, user_principal_name,
280                                                NULL);
281         } else {
282                 krb5_ret = krb5_make_principal(io->smb_krb5_context->krb5_context,
283                                                &salt_principal,
284                                                io->domain->realm, io->u.sAMAccountName,
285                                                NULL);
286         }
287         if (krb5_ret) {
288                 ldb_asprintf_errstring(io->ac->module->ldb,
289                                        "setup_kerberos_keys: "
290                                        "generation of a salting principal failed: %s",
291                                        smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
292                 return LDB_ERR_OPERATIONS_ERROR;
293         }
294
295         /*
296          * create salt from salt_principal
297          */
298         krb5_ret = krb5_get_pw_salt(io->smb_krb5_context->krb5_context,
299                                     salt_principal, &salt);
300         krb5_free_principal(io->smb_krb5_context->krb5_context, salt_principal);
301         if (krb5_ret) {
302                 ldb_asprintf_errstring(io->ac->module->ldb,
303                                        "setup_kerberos_keys: "
304                                        "generation of krb5_salt failed: %s",
305                                        smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
306                 return LDB_ERR_OPERATIONS_ERROR;
307         }
308         /* create a talloc copy */
309         io->g.salt = talloc_strndup(io->ac,
310                                     salt.saltvalue.data,
311                                     salt.saltvalue.length);
312         krb5_free_salt(io->smb_krb5_context->krb5_context, salt);
313         if (!io->g.salt) {
314                 ldb_oom(io->ac->module->ldb);
315                 return LDB_ERR_OPERATIONS_ERROR;
316         }
317         salt.saltvalue.data     = discard_const(io->g.salt);
318         salt.saltvalue.length   = strlen(io->g.salt);
319
320         /*
321          * create ENCTYPE_AES256_CTS_HMAC_SHA1_96 key out of
322          * the salt and the cleartext password
323          */
324         krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
325                                            ENCTYPE_AES256_CTS_HMAC_SHA1_96,
326                                            io->n.cleartext,
327                                            salt,
328                                            &key);
329         if (krb5_ret) {
330                 ldb_asprintf_errstring(io->ac->module->ldb,
331                                        "setup_kerberos_keys: "
332                                        "generation of a aes256-cts-hmac-sha1-96 key failed: %s",
333                                        smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
334                 return LDB_ERR_OPERATIONS_ERROR;
335         }
336         io->g.aes_256 = data_blob_talloc(io->ac,
337                                          key.keyvalue.data,
338                                          key.keyvalue.length);
339         krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
340         if (!io->g.aes_256.data) {
341                 ldb_oom(io->ac->module->ldb);
342                 return LDB_ERR_OPERATIONS_ERROR;
343         }
344
345         /*
346          * create ENCTYPE_AES128_CTS_HMAC_SHA1_96 key out of
347          * the salt and the cleartext password
348          */
349         krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
350                                            ENCTYPE_AES128_CTS_HMAC_SHA1_96,
351                                            io->n.cleartext,
352                                            salt,
353                                            &key);
354         if (krb5_ret) {
355                 ldb_asprintf_errstring(io->ac->module->ldb,
356                                        "setup_kerberos_keys: "
357                                        "generation of a aes128-cts-hmac-sha1-96 key failed: %s",
358                                        smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
359                 return LDB_ERR_OPERATIONS_ERROR;
360         }
361         io->g.aes_128 = data_blob_talloc(io->ac,
362                                          key.keyvalue.data,
363                                          key.keyvalue.length);
364         krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
365         if (!io->g.aes_128.data) {
366                 ldb_oom(io->ac->module->ldb);
367                 return LDB_ERR_OPERATIONS_ERROR;
368         }
369
370         /*
371          * create ENCTYPE_DES_CBC_MD5 key out of
372          * the salt and the cleartext password
373          */
374         krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
375                                            ENCTYPE_DES_CBC_MD5,
376                                            io->n.cleartext,
377                                            salt,
378                                            &key);
379         if (krb5_ret) {
380                 ldb_asprintf_errstring(io->ac->module->ldb,
381                                        "setup_kerberos_keys: "
382                                        "generation of a des-cbc-md5 key failed: %s",
383                                        smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
384                 return LDB_ERR_OPERATIONS_ERROR;
385         }
386         io->g.des_md5 = data_blob_talloc(io->ac,
387                                          key.keyvalue.data,
388                                          key.keyvalue.length);
389         krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
390         if (!io->g.des_md5.data) {
391                 ldb_oom(io->ac->module->ldb);
392                 return LDB_ERR_OPERATIONS_ERROR;
393         }
394
395         /*
396          * create ENCTYPE_DES_CBC_CRC key out of
397          * the salt and the cleartext password
398          */
399         krb5_ret = krb5_string_to_key_salt(io->smb_krb5_context->krb5_context,
400                                            ENCTYPE_DES_CBC_CRC,
401                                            io->n.cleartext,
402                                            salt,
403                                            &key);
404         if (krb5_ret) {
405                 ldb_asprintf_errstring(io->ac->module->ldb,
406                                        "setup_kerberos_keys: "
407                                        "generation of a des-cbc-crc key failed: %s",
408                                        smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
409                 return LDB_ERR_OPERATIONS_ERROR;
410         }
411         io->g.des_crc = data_blob_talloc(io->ac,
412                                          key.keyvalue.data,
413                                          key.keyvalue.length);
414         krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
415         if (!io->g.des_crc.data) {
416                 ldb_oom(io->ac->module->ldb);
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419
420         return LDB_SUCCESS;
421 }
422
423 static int setup_primary_kerberos(struct setup_password_fields_io *io,
424                                   const struct supplementalCredentialsBlob *old_scb,
425                                   struct package_PrimaryKerberosBlob *pkb)
426 {
427         struct package_PrimaryKerberosCtr3 *pkb3 = &pkb->ctr.ctr3;
428         struct supplementalCredentialsPackage *old_scp = NULL;
429         struct package_PrimaryKerberosBlob _old_pkb;
430         struct package_PrimaryKerberosCtr3 *old_pkb3 = NULL;
431         uint32_t i;
432         enum ndr_err_code ndr_err;
433
434         /*
435          * prepare generation of keys
436          *
437          * ENCTYPE_DES_CBC_MD5
438          * ENCTYPE_DES_CBC_CRC
439          */
440         pkb3->salt.string       = io->g.salt;
441         pkb3->num_keys          = 2;
442         pkb3->keys              = talloc_array(io->ac,
443                                                struct package_PrimaryKerberosKey,
444                                                pkb3->num_keys);
445         if (!pkb3->keys) {
446                 ldb_oom(io->ac->module->ldb);
447                 return LDB_ERR_OPERATIONS_ERROR;
448         }
449
450         pkb3->keys[0].keytype   = ENCTYPE_DES_CBC_MD5;
451         pkb3->keys[0].value     = &io->g.des_md5;
452         pkb3->keys[1].keytype   = ENCTYPE_DES_CBC_CRC;
453         pkb3->keys[1].value     = &io->g.des_crc;
454
455         /* initialize the old keys to zero */
456         pkb3->num_old_keys      = 0;
457         pkb3->old_keys          = NULL;
458
459         /* if there're no old keys, then we're done */
460         if (!old_scb) {
461                 return LDB_SUCCESS;
462         }
463
464         for (i=0; i < old_scb->sub.num_packages; i++) {
465                 if (strcmp("Primary:Kerberos", old_scb->sub.packages[i].name) != 0) {
466                         continue;
467                 }
468
469                 if (!old_scb->sub.packages[i].data || !old_scb->sub.packages[i].data[0]) {
470                         continue;
471                 }
472
473                 old_scp = &old_scb->sub.packages[i];
474                 break;
475         }
476         /* Primary:Kerberos element of supplementalCredentials */
477         if (old_scp) {
478                 DATA_BLOB blob;
479
480                 blob = strhex_to_data_blob(old_scp->data);
481                 if (!blob.data) {
482                         ldb_oom(io->ac->module->ldb);
483                         return LDB_ERR_OPERATIONS_ERROR;
484                 }
485                 talloc_steal(io->ac, blob.data);
486
487                 /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
488                 ndr_err = ndr_pull_struct_blob(&blob, io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), &_old_pkb,
489                                                (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
490                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
491                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
492                         ldb_asprintf_errstring(io->ac->module->ldb,
493                                                "setup_primary_kerberos: "
494                                                "failed to pull old package_PrimaryKerberosBlob: %s",
495                                                nt_errstr(status));
496                         return LDB_ERR_OPERATIONS_ERROR;
497                 }
498
499                 if (_old_pkb.version != 3) {
500                         ldb_asprintf_errstring(io->ac->module->ldb,
501                                                "setup_primary_kerberos: "
502                                                "package_PrimaryKerberosBlob version[%u] expected[3]",
503                                                _old_pkb.version);
504                         return LDB_ERR_OPERATIONS_ERROR;
505                 }
506
507                 old_pkb3 = &_old_pkb.ctr.ctr3;
508         }
509
510         /* if we didn't found the old keys we're done */
511         if (!old_pkb3) {
512                 return LDB_SUCCESS;
513         }
514
515         /* fill in the old keys */
516         pkb3->num_old_keys      = old_pkb3->num_keys;
517         pkb3->old_keys          = old_pkb3->keys;
518
519         return LDB_SUCCESS;
520 }
521
522 static int setup_primary_kerberos_newer(struct setup_password_fields_io *io,
523                                         const struct supplementalCredentialsBlob *old_scb,
524                                         struct package_PrimaryKerberosNewerBlob *pkb)
525 {
526         struct package_PrimaryKerberosNewerCtr4 *pkb4 = &pkb->ctr.ctr4;
527         struct supplementalCredentialsPackage *old_scp = NULL;
528         struct package_PrimaryKerberosNewerBlob _old_pkb;
529         struct package_PrimaryKerberosNewerCtr4 *old_pkb4 = NULL;
530         uint32_t i;
531         enum ndr_err_code ndr_err;
532
533         /*
534          * prepare generation of keys
535          *
536          * ENCTYPE_AES256_CTS_HMAC_SHA1_96
537          * ENCTYPE_AES128_CTS_HMAC_SHA1_96
538          * ENCTYPE_DES_CBC_MD5
539          * ENCTYPE_DES_CBC_CRC
540          */
541         pkb4->salt.string       = io->g.salt;
542         pkb4->num_keys          = 4;
543         pkb4->keys              = talloc_array(io->ac,
544                                                struct package_PrimaryKerberosNewerKey,
545                                                pkb4->num_keys);
546         if (!pkb4->keys) {
547                 ldb_oom(io->ac->module->ldb);
548                 return LDB_ERR_OPERATIONS_ERROR;
549         }
550
551         pkb4->keys[0].keytype   = ENCTYPE_AES256_CTS_HMAC_SHA1_96;
552         pkb4->keys[0].value     = &io->g.aes_256;
553         pkb4->keys[1].keytype   = ENCTYPE_AES128_CTS_HMAC_SHA1_96;
554         pkb4->keys[1].value     = &io->g.aes_128;
555         pkb4->keys[2].keytype   = ENCTYPE_DES_CBC_MD5;
556         pkb4->keys[2].value     = &io->g.des_md5;
557         pkb4->keys[3].keytype   = ENCTYPE_DES_CBC_CRC;
558         pkb4->keys[3].value     = &io->g.des_crc;
559
560         /* initialize the old keys to zero */
561         pkb4->num_old_keys1     = 0;
562         pkb4->old_keys1         = NULL;
563         pkb4->num_old_keys2     = 0;
564         pkb4->old_keys2         = NULL;
565
566         /* if there're no old keys, then we're done */
567         if (!old_scb) {
568                 return LDB_SUCCESS;
569         }
570
571         for (i=0; i < old_scb->sub.num_packages; i++) {
572                 if (strcmp("Primary:Kerberos-Newer-Keys", old_scb->sub.packages[i].name) != 0) {
573                         continue;
574                 }
575
576                 if (!old_scb->sub.packages[i].data || !old_scb->sub.packages[i].data[0]) {
577                         continue;
578                 }
579
580                 old_scp = &old_scb->sub.packages[i];
581                 break;
582         }
583         /* Primary:Kerberos element of supplementalCredentials */
584         if (old_scp) {
585                 DATA_BLOB blob;
586
587                 blob = strhex_to_data_blob(old_scp->data);
588                 if (!blob.data) {
589                         ldb_oom(io->ac->module->ldb);
590                         return LDB_ERR_OPERATIONS_ERROR;
591                 }
592                 talloc_steal(io->ac, blob.data);
593
594                 /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
595                 ndr_err = ndr_pull_struct_blob(&blob, io->ac,
596                                                lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
597                                                &_old_pkb,
598                                                (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosNewerBlob);
599                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
600                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
601                         ldb_asprintf_errstring(io->ac->module->ldb,
602                                                "setup_primary_kerberos_newer: "
603                                                "failed to pull old package_PrimaryKerberosNewerBlob: %s",
604                                                nt_errstr(status));
605                         return LDB_ERR_OPERATIONS_ERROR;
606                 }
607
608                 if (_old_pkb.version != 4) {
609                         ldb_asprintf_errstring(io->ac->module->ldb,
610                                                "setup_primary_kerberos: "
611                                                "package_PrimaryKerberosNewerBlob version[%u] expected[4]",
612                                                _old_pkb.version);
613                         return LDB_ERR_OPERATIONS_ERROR;
614                 }
615
616                 old_pkb4 = &_old_pkb.ctr.ctr4;
617         }
618
619         /* if we didn't found the old keys we're done */
620         if (!old_pkb4) {
621                 return LDB_SUCCESS;
622         }
623
624         /* fill in the old keys */
625         pkb4->num_old_keys1     = old_pkb4->num_keys;
626         pkb4->old_keys1         = old_pkb4->keys;
627         pkb4->num_old_keys2     = old_pkb4->num_old_keys1;
628         pkb4->old_keys2         = old_pkb4->old_keys1;
629
630         return LDB_SUCCESS;
631 }
632
633 static int setup_primary_wdigest(struct setup_password_fields_io *io,
634                                  const struct supplementalCredentialsBlob *old_scb,
635                                  struct package_PrimaryWDigestBlob *pdb)
636 {
637         DATA_BLOB sAMAccountName;
638         DATA_BLOB sAMAccountName_l;
639         DATA_BLOB sAMAccountName_u;
640         const char *user_principal_name = io->u.user_principal_name;
641         DATA_BLOB userPrincipalName;
642         DATA_BLOB userPrincipalName_l;
643         DATA_BLOB userPrincipalName_u;
644         DATA_BLOB netbios_domain;
645         DATA_BLOB netbios_domain_l;
646         DATA_BLOB netbios_domain_u;
647         DATA_BLOB dns_domain;
648         DATA_BLOB dns_domain_l;
649         DATA_BLOB dns_domain_u;
650         DATA_BLOB cleartext;
651         DATA_BLOB digest;
652         DATA_BLOB delim;
653         DATA_BLOB backslash;
654         uint8_t i;
655         struct {
656                 DATA_BLOB *user;
657                 DATA_BLOB *realm;
658                 DATA_BLOB *nt4dom;
659         } wdigest[] = {
660         /*
661          * See
662          * http://technet2.microsoft.com/WindowsServer/en/library/717b450c-f4a0-4cc9-86f4-cc0633aae5f91033.mspx?mfr=true
663          * for what precalculated hashes are supposed to be stored...
664          *
665          * I can't reproduce all values which should contain "Digest" as realm,
666          * am I doing something wrong or is w2k3 just broken...?
667          *
668          * W2K3 fills in following for a user:
669          *
670          * dn: CN=NewUser,OU=newtop,DC=sub1,DC=w2k3,DC=vmnet1,DC=vm,DC=base
671          * sAMAccountName: NewUser2Sam
672          * userPrincipalName: NewUser2Princ@sub1.w2k3.vmnet1.vm.base
673          *
674          * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
675          * b7ec9da91062199aee7d121e6710fe23 => newuser2sam:sub1:TestPwd2007
676          * 17d290bc5c9f463fac54c37a8cea134d => NEWUSER2SAM:SUB1:TestPwd2007
677          * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
678          * 5d57e7823938348127322e08cd81bcb5 => NewUser2Sam:sub1:TestPwd2007
679          * 07dd701bf8a011ece585de3d47237140 => NEWUSER2SAM:sub1:TestPwd2007
680          * e14fb0eb401498d2cb33c9aae1cc7f37 => newuser2sam:SUB1:TestPwd2007
681          * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
682          * f52da1266a6bdd290ffd48b2c823dda7 => newuser2sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
683          * d2b42f171248cec37a3c5c6b55404062 => NEWUSER2SAM:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
684          * fff8d790ff6c152aaeb6ebe17b4021de => NewUser2Sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
685          * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
686          * 2a7563c3715bc418d626dabef378c008 => NEWUSER2SAM:sub1.w2k3.vmnet1.vm.base:TestPwd2007
687          * c8e9557a87cd4200fda0c11d2fa03f96 => newuser2sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
688          * 221c55284451ae9b3aacaa2a3c86f10f => NewUser2Princ@sub1.w2k3.vmnet1.vm.base::TestPwd2007
689          * 74e1be668853d4324d38c07e2acfb8ea => (w2k3 has a bug here!) newuser2princ@sub1.w2k3.vmnet1.vm.base::TestPwd2007
690          * e1e244ab7f098e3ae1761be7f9229bbb => NEWUSER2PRINC@SUB1.W2K3.VMNET1.VM.BASE::TestPwd2007
691          * 86db637df42513039920e605499c3af6 => SUB1\NewUser2Sam::TestPwd2007
692          * f5e43474dfaf067fee8197a253debaa2 => sub1\newuser2sam::TestPwd2007
693          * 2ecaa8382e2518e4b77a52422b279467 => SUB1\NEWUSER2SAM::TestPwd2007
694          * 31dc704d3640335b2123d4ee28aa1f11 => ??? changes with NewUser2Sam => NewUser1Sam
695          * 36349f5cecd07320fb3bb0e119230c43 => ??? changes with NewUser2Sam => NewUser1Sam
696          * 12adf019d037fb535c01fd0608e78d9d => ??? changes with NewUser2Sam => NewUser1Sam
697          * 6feecf8e724906f3ee1105819c5105a1 => ??? changes with NewUser2Princ => NewUser1Princ
698          * 6c6911f3de6333422640221b9c51ff1f => ??? changes with NewUser2Princ => NewUser1Princ
699          * 4b279877e742895f9348ac67a8de2f69 => ??? changes with NewUser2Princ => NewUser1Princ
700          * db0c6bff069513e3ebb9870d29b57490 => ??? changes with NewUser2Sam => NewUser1Sam
701          * 45072621e56b1c113a4e04a8ff68cd0e => ??? changes with NewUser2Sam => NewUser1Sam
702          * 11d1220abc44a9c10cf91ef4a9c1de02 => ??? changes with NewUser2Sam => NewUser1Sam
703          *
704          * dn: CN=NewUser,OU=newtop,DC=sub1,DC=w2k3,DC=vmnet1,DC=vm,DC=base
705          * sAMAccountName: NewUser2Sam
706          *
707          * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
708          * b7ec9da91062199aee7d121e6710fe23 => newuser2sam:sub1:TestPwd2007
709          * 17d290bc5c9f463fac54c37a8cea134d => NEWUSER2SAM:SUB1:TestPwd2007
710          * 4279815024bda54fc074a5f8bd0a6e6f => NewUser2Sam:SUB1:TestPwd2007
711          * 5d57e7823938348127322e08cd81bcb5 => NewUser2Sam:sub1:TestPwd2007
712          * 07dd701bf8a011ece585de3d47237140 => NEWUSER2SAM:sub1:TestPwd2007
713          * e14fb0eb401498d2cb33c9aae1cc7f37 => newuser2sam:SUB1:TestPwd2007
714          * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
715          * f52da1266a6bdd290ffd48b2c823dda7 => newuser2sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
716          * d2b42f171248cec37a3c5c6b55404062 => NEWUSER2SAM:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
717          * fff8d790ff6c152aaeb6ebe17b4021de => NewUser2Sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
718          * 8dadc90250f873d8b883f79d890bef82 => NewUser2Sam:sub1.w2k3.vmnet1.vm.base:TestPwd2007
719          * 2a7563c3715bc418d626dabef378c008 => NEWUSER2SAM:sub1.w2k3.vmnet1.vm.base:TestPwd2007
720          * c8e9557a87cd4200fda0c11d2fa03f96 => newuser2sam:SUB1.W2K3.VMNET1.VM.BASE:TestPwd2007
721          * 8a140d30b6f0a5912735dc1e3bc993b4 => NewUser2Sam@sub1.w2k3.vmnet1.vm.base::TestPwd2007
722          * 86d95b2faae6cae4ec261e7fbaccf093 => (here w2k3 is correct) newuser2sam@sub1.w2k3.vmnet1.vm.base::TestPwd2007
723          * dfeff1493110220efcdfc6362e5f5450 => NEWUSER2SAM@SUB1.W2K3.VMNET1.VM.BASE::TestPwd2007
724          * 86db637df42513039920e605499c3af6 => SUB1\NewUser2Sam::TestPwd2007
725          * f5e43474dfaf067fee8197a253debaa2 => sub1\newuser2sam::TestPwd2007
726          * 2ecaa8382e2518e4b77a52422b279467 => SUB1\NEWUSER2SAM::TestPwd2007
727          * 31dc704d3640335b2123d4ee28aa1f11 => ???M1   changes with NewUser2Sam => NewUser1Sam
728          * 36349f5cecd07320fb3bb0e119230c43 => ???M1.L changes with newuser2sam => newuser1sam
729          * 12adf019d037fb535c01fd0608e78d9d => ???M1.U changes with NEWUSER2SAM => NEWUSER1SAM
730          * 569b4533f2d9e580211dd040e5e360a8 => ???M2   changes with NewUser2Princ => NewUser1Princ
731          * 52528bddf310a587c5d7e6a9ae2cbb20 => ???M2.L changes with newuser2princ => newuser1princ
732          * 4f629a4f0361289ca4255ab0f658fcd5 => ???M3 changes with NewUser2Princ => NewUser1Princ (doesn't depend on case of userPrincipal )
733          * db0c6bff069513e3ebb9870d29b57490 => ???M4 changes with NewUser2Sam => NewUser1Sam
734          * 45072621e56b1c113a4e04a8ff68cd0e => ???M5 changes with NewUser2Sam => NewUser1Sam (doesn't depend on case of sAMAccountName)
735          * 11d1220abc44a9c10cf91ef4a9c1de02 => ???M4.U changes with NEWUSER2SAM => NEWUSER1SAM
736          */
737
738         /*
739          * sAMAccountName, netbios_domain
740          */
741                 {
742                 .user   = &sAMAccountName,
743                 .realm  = &netbios_domain,
744                 },
745                 {
746                 .user   = &sAMAccountName_l,
747                 .realm  = &netbios_domain_l,
748                 },
749                 {
750                 .user   = &sAMAccountName_u,
751                 .realm  = &netbios_domain_u,
752                 },
753                 {
754                 .user   = &sAMAccountName,
755                 .realm  = &netbios_domain_u,
756                 },
757                 {
758                 .user   = &sAMAccountName,
759                 .realm  = &netbios_domain_l,
760                 },
761                 {
762                 .user   = &sAMAccountName_u,
763                 .realm  = &netbios_domain_l,
764                 },
765                 {
766                 .user   = &sAMAccountName_l,
767                 .realm  = &netbios_domain_u,
768                 },
769         /* 
770          * sAMAccountName, dns_domain
771          */
772                 {
773                 .user   = &sAMAccountName,
774                 .realm  = &dns_domain,
775                 },
776                 {
777                 .user   = &sAMAccountName_l,
778                 .realm  = &dns_domain_l,
779                 },
780                 {
781                 .user   = &sAMAccountName_u,
782                 .realm  = &dns_domain_u,
783                 },
784                 {
785                 .user   = &sAMAccountName,
786                 .realm  = &dns_domain_u,
787                 },
788                 {
789                 .user   = &sAMAccountName,
790                 .realm  = &dns_domain_l,
791                 },
792                 {
793                 .user   = &sAMAccountName_u,
794                 .realm  = &dns_domain_l,
795                 },
796                 {
797                 .user   = &sAMAccountName_l,
798                 .realm  = &dns_domain_u,
799                 },
800         /* 
801          * userPrincipalName, no realm
802          */
803                 {
804                 .user   = &userPrincipalName,
805                 },
806                 {
807                 /* 
808                  * NOTE: w2k3 messes this up, if the user has a real userPrincipalName,
809                  *       the fallback to the sAMAccountName based userPrincipalName is correct
810                  */
811                 .user   = &userPrincipalName_l,
812                 },
813                 {
814                 .user   = &userPrincipalName_u,
815                 },
816         /* 
817          * nt4dom\sAMAccountName, no realm
818          */
819                 {
820                 .user   = &sAMAccountName,
821                 .nt4dom = &netbios_domain
822                 },
823                 {
824                 .user   = &sAMAccountName_l,
825                 .nt4dom = &netbios_domain_l
826                 },
827                 {
828                 .user   = &sAMAccountName_u,
829                 .nt4dom = &netbios_domain_u
830                 },
831
832         /*
833          * the following ones are guessed depending on the technet2 article
834          * but not reproducable on a w2k3 server
835          */
836         /* sAMAccountName with "Digest" realm */
837                 {
838                 .user   = &sAMAccountName,
839                 .realm  = &digest
840                 },
841                 {
842                 .user   = &sAMAccountName_l,
843                 .realm  = &digest
844                 },
845                 {
846                 .user   = &sAMAccountName_u,
847                 .realm  = &digest
848                 },
849         /* userPrincipalName with "Digest" realm */
850                 {
851                 .user   = &userPrincipalName,
852                 .realm  = &digest
853                 },
854                 {
855                 .user   = &userPrincipalName_l,
856                 .realm  = &digest
857                 },
858                 {
859                 .user   = &userPrincipalName_u,
860                 .realm  = &digest
861                 },
862         /* nt4dom\\sAMAccountName with "Digest" realm */
863                 {
864                 .user   = &sAMAccountName,
865                 .nt4dom = &netbios_domain,
866                 .realm  = &digest
867                 },
868                 {
869                 .user   = &sAMAccountName_l,
870                 .nt4dom = &netbios_domain_l,
871                 .realm  = &digest
872                 },
873                 {
874                 .user   = &sAMAccountName_u,
875                 .nt4dom = &netbios_domain_u,
876                 .realm  = &digest
877                 },
878         };
879
880         /* prepare DATA_BLOB's used in the combinations array */
881         sAMAccountName          = data_blob_string_const(io->u.sAMAccountName);
882         sAMAccountName_l        = data_blob_string_const(strlower_talloc(io->ac, io->u.sAMAccountName));
883         if (!sAMAccountName_l.data) {
884                 ldb_oom(io->ac->module->ldb);
885                 return LDB_ERR_OPERATIONS_ERROR;
886         }
887         sAMAccountName_u        = data_blob_string_const(strupper_talloc(io->ac, io->u.sAMAccountName));
888         if (!sAMAccountName_u.data) {
889                 ldb_oom(io->ac->module->ldb);
890                 return LDB_ERR_OPERATIONS_ERROR;
891         }
892
893         /* if the user doesn't have a userPrincipalName, create one (with lower case realm) */
894         if (!user_principal_name) {
895                 user_principal_name = talloc_asprintf(io->ac, "%s@%s",
896                                                       io->u.sAMAccountName,
897                                                       io->domain->dns_domain);
898                 if (!user_principal_name) {
899                         ldb_oom(io->ac->module->ldb);
900                         return LDB_ERR_OPERATIONS_ERROR;
901                 }       
902         }
903         userPrincipalName       = data_blob_string_const(user_principal_name);
904         userPrincipalName_l     = data_blob_string_const(strlower_talloc(io->ac, user_principal_name));
905         if (!userPrincipalName_l.data) {
906                 ldb_oom(io->ac->module->ldb);
907                 return LDB_ERR_OPERATIONS_ERROR;
908         }
909         userPrincipalName_u     = data_blob_string_const(strupper_talloc(io->ac, user_principal_name));
910         if (!userPrincipalName_u.data) {
911                 ldb_oom(io->ac->module->ldb);
912                 return LDB_ERR_OPERATIONS_ERROR;
913         }
914
915         netbios_domain          = data_blob_string_const(io->domain->netbios_domain);
916         netbios_domain_l        = data_blob_string_const(strlower_talloc(io->ac, io->domain->netbios_domain));
917         if (!netbios_domain_l.data) {
918                 ldb_oom(io->ac->module->ldb);
919                 return LDB_ERR_OPERATIONS_ERROR;
920         }
921         netbios_domain_u        = data_blob_string_const(strupper_talloc(io->ac, io->domain->netbios_domain));
922         if (!netbios_domain_u.data) {
923                 ldb_oom(io->ac->module->ldb);
924                 return LDB_ERR_OPERATIONS_ERROR;
925         }
926
927         dns_domain              = data_blob_string_const(io->domain->dns_domain);
928         dns_domain_l            = data_blob_string_const(io->domain->dns_domain);
929         dns_domain_u            = data_blob_string_const(io->domain->realm);
930
931         cleartext               = data_blob_string_const(io->n.cleartext);
932
933         digest                  = data_blob_string_const("Digest");
934
935         delim                   = data_blob_string_const(":");
936         backslash               = data_blob_string_const("\\");
937
938         pdb->num_hashes = ARRAY_SIZE(wdigest);
939         pdb->hashes     = talloc_array(io->ac, struct package_PrimaryWDigestHash, pdb->num_hashes);
940         if (!pdb->hashes) {
941                 ldb_oom(io->ac->module->ldb);
942                 return LDB_ERR_OPERATIONS_ERROR;
943         }
944
945         for (i=0; i < ARRAY_SIZE(wdigest); i++) {
946                 struct MD5Context md5;
947                 MD5Init(&md5);
948                 if (wdigest[i].nt4dom) {
949                         MD5Update(&md5, wdigest[i].nt4dom->data, wdigest[i].nt4dom->length);
950                         MD5Update(&md5, backslash.data, backslash.length);
951                 }
952                 MD5Update(&md5, wdigest[i].user->data, wdigest[i].user->length);
953                 MD5Update(&md5, delim.data, delim.length);
954                 if (wdigest[i].realm) {
955                         MD5Update(&md5, wdigest[i].realm->data, wdigest[i].realm->length);
956                 }
957                 MD5Update(&md5, delim.data, delim.length);
958                 MD5Update(&md5, cleartext.data, cleartext.length);
959                 MD5Final(pdb->hashes[i].hash, &md5);
960         }
961
962         return LDB_SUCCESS;
963 }
964
965 static int setup_supplemental_field(struct setup_password_fields_io *io)
966 {
967         struct supplementalCredentialsBlob scb;
968         struct supplementalCredentialsBlob _old_scb;
969         struct supplementalCredentialsBlob *old_scb = NULL;
970         /* Packages + (Kerberos-Newer-Keys, Kerberos, WDigest and CLEARTEXT) */
971         uint32_t num_names = 0;
972         const char *names[1+4];
973         uint32_t num_packages = 0;
974         struct supplementalCredentialsPackage packages[1+4];
975         /* Packages */
976         struct supplementalCredentialsPackage *pp = NULL;
977         struct package_PackagesBlob pb;
978         DATA_BLOB pb_blob;
979         char *pb_hexstr;
980         /* Primary:Kerberos-Newer-Keys */
981         const char **nkn = NULL;
982         struct supplementalCredentialsPackage *pkn = NULL;
983         struct package_PrimaryKerberosNewerBlob pknb;
984         DATA_BLOB pknb_blob;
985         char *pknb_hexstr;
986         /* Primary:Kerberos */
987         const char **nk = NULL;
988         struct supplementalCredentialsPackage *pk = NULL;
989         struct package_PrimaryKerberosBlob pkb;
990         DATA_BLOB pkb_blob;
991         char *pkb_hexstr;
992         /* Primary:WDigest */
993         const char **nd = NULL;
994         struct supplementalCredentialsPackage *pd = NULL;
995         struct package_PrimaryWDigestBlob pdb;
996         DATA_BLOB pdb_blob;
997         char *pdb_hexstr;
998         /* Primary:CLEARTEXT */
999         const char **nc = NULL;
1000         struct supplementalCredentialsPackage *pc = NULL;
1001         struct package_PrimaryCLEARTEXTBlob pcb;
1002         DATA_BLOB pcb_blob;
1003         char *pcb_hexstr;
1004         int ret;
1005         enum ndr_err_code ndr_err;
1006         uint8_t zero16[16];
1007         bool do_newer_keys = false;
1008         bool do_cleartext = false;
1009
1010         ZERO_STRUCT(zero16);
1011         ZERO_STRUCT(names);
1012
1013         if (!io->n.cleartext) {
1014                 /* 
1015                  * when we don't have a cleartext password
1016                  * we can't setup a supplementalCredential value
1017                  */
1018                 return LDB_SUCCESS;
1019         }
1020
1021         /* if there's an old supplementaCredentials blob then parse it */
1022         if (io->o.supplemental) {
1023                 ndr_err = ndr_pull_struct_blob_all(io->o.supplemental, io->ac,
1024                                                    lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1025                                                    &_old_scb,
1026                                                    (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
1027                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1028                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1029                         ldb_asprintf_errstring(io->ac->module->ldb,
1030                                                "setup_supplemental_field: "
1031                                                "failed to pull old supplementalCredentialsBlob: %s",
1032                                                nt_errstr(status));
1033                         return LDB_ERR_OPERATIONS_ERROR;
1034                 }
1035
1036                 if (_old_scb.sub.signature == SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
1037                         old_scb = &_old_scb;
1038                 } else {
1039                         ldb_debug(io->ac->module->ldb, LDB_DEBUG_ERROR,
1040                                                "setup_supplemental_field: "
1041                                                "supplementalCredentialsBlob signature[0x%04X] expected[0x%04X]",
1042                                                _old_scb.sub.signature, SUPPLEMENTAL_CREDENTIALS_SIGNATURE);
1043                 }
1044         }
1045
1046         /* TODO: do the correct check for this, it maybe depends on the functional level? */
1047         do_newer_keys = lp_parm_bool(ldb_get_opaque(io->ac->module->ldb, "loadparm"),
1048                                      NULL, "password_hash", "create_aes_key", false);
1049
1050         if (io->domain->store_cleartext &&
1051             (io->u.user_account_control & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
1052                 do_cleartext = true;
1053         }
1054
1055         /*
1056          * The ordering is this
1057          *
1058          * Primary:Kerberos-Newer-Keys (optional)
1059          * Primary:Kerberos
1060          * Primary:WDigest
1061          * Primary:CLEARTEXT (optional)
1062          *
1063          * And the 'Packages' package is insert before the last
1064          * other package.
1065          */
1066         if (do_newer_keys) {
1067                 /* Primary:Kerberos-Newer-Keys */
1068                 nkn = &names[num_names++];
1069                 pkn = &packages[num_packages++];
1070         }
1071
1072         /* Primary:Kerberos */
1073         nk = &names[num_names++];
1074         pk = &packages[num_packages++];
1075
1076         if (!do_cleartext) {
1077                 /* Packages */
1078                 pp = &packages[num_packages++];
1079         }
1080
1081         /* Primary:WDigest */
1082         nd = &names[num_names++];
1083         pd = &packages[num_packages++];
1084
1085         if (do_cleartext) {
1086                 /* Packages */
1087                 pp = &packages[num_packages++];
1088
1089                 /* Primary:CLEARTEXT */
1090                 nc = &names[num_names++];
1091                 pc = &packages[num_packages++];
1092         }
1093
1094         if (pkn) {
1095                 /*
1096                  * setup 'Primary:Kerberos-Newer-Keys' element
1097                  */
1098                 *nkn = "Kerberos-Newer-Keys";
1099
1100                 ret = setup_primary_kerberos_newer(io, old_scb, &pknb);
1101                 if (ret != LDB_SUCCESS) {
1102                         return ret;
1103                 }
1104
1105                 ndr_err = ndr_push_struct_blob(&pknb_blob, io->ac,
1106                                                lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1107                                                &pknb,
1108                                                (ndr_push_flags_fn_t)ndr_push_package_PrimaryKerberosNewerBlob);
1109                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1110                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1111                         ldb_asprintf_errstring(io->ac->module->ldb,
1112                                                "setup_supplemental_field: "
1113                                                "failed to push package_PrimaryKerberosNeverBlob: %s",
1114                                                nt_errstr(status));
1115                         return LDB_ERR_OPERATIONS_ERROR;
1116                 }
1117                 pknb_hexstr = data_blob_hex_string(io->ac, &pknb_blob);
1118                 if (!pknb_hexstr) {
1119                         ldb_oom(io->ac->module->ldb);
1120                         return LDB_ERR_OPERATIONS_ERROR;
1121                 }
1122                 pkn->name       = "Primary:Kerberos-Newer-Keys";
1123                 pkn->reserved   = 1;
1124                 pkn->data       = pknb_hexstr;
1125         }
1126
1127         /*
1128          * setup 'Primary:Kerberos' element
1129          */
1130         *nk = "Kerberos";
1131
1132         ret = setup_primary_kerberos(io, old_scb, &pkb);
1133         if (ret != LDB_SUCCESS) {
1134                 return ret;
1135         }
1136
1137         ndr_err = ndr_push_struct_blob(&pkb_blob, io->ac, 
1138                                        lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1139                                        &pkb,
1140                                        (ndr_push_flags_fn_t)ndr_push_package_PrimaryKerberosBlob);
1141         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1142                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1143                 ldb_asprintf_errstring(io->ac->module->ldb,
1144                                        "setup_supplemental_field: "
1145                                        "failed to push package_PrimaryKerberosBlob: %s",
1146                                        nt_errstr(status));
1147                 return LDB_ERR_OPERATIONS_ERROR;
1148         }
1149         pkb_hexstr = data_blob_hex_string(io->ac, &pkb_blob);
1150         if (!pkb_hexstr) {
1151                 ldb_oom(io->ac->module->ldb);
1152                 return LDB_ERR_OPERATIONS_ERROR;
1153         }
1154         pk->name        = "Primary:Kerberos";
1155         pk->reserved    = 1;
1156         pk->data        = pkb_hexstr;
1157
1158         /*
1159          * setup 'Primary:WDigest' element
1160          */
1161         *nd = "WDigest";
1162
1163         ret = setup_primary_wdigest(io, old_scb, &pdb);
1164         if (ret != LDB_SUCCESS) {
1165                 return ret;
1166         }
1167
1168         ndr_err = ndr_push_struct_blob(&pdb_blob, io->ac, 
1169                                        lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1170                                        &pdb,
1171                                        (ndr_push_flags_fn_t)ndr_push_package_PrimaryWDigestBlob);
1172         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1173                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1174                 ldb_asprintf_errstring(io->ac->module->ldb,
1175                                        "setup_supplemental_field: "
1176                                        "failed to push package_PrimaryWDigestBlob: %s",
1177                                        nt_errstr(status));
1178                 return LDB_ERR_OPERATIONS_ERROR;
1179         }
1180         pdb_hexstr = data_blob_hex_string(io->ac, &pdb_blob);
1181         if (!pdb_hexstr) {
1182                 ldb_oom(io->ac->module->ldb);
1183                 return LDB_ERR_OPERATIONS_ERROR;
1184         }
1185         pd->name        = "Primary:WDigest";
1186         pd->reserved    = 1;
1187         pd->data        = pdb_hexstr;
1188
1189         /*
1190          * setup 'Primary:CLEARTEXT' element
1191          */
1192         if (pc) {
1193                 *nc             = "CLEARTEXT";
1194
1195                 pcb.cleartext   = io->n.cleartext;
1196
1197                 ndr_err = ndr_push_struct_blob(&pcb_blob, io->ac, 
1198                                                lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1199                                                &pcb,
1200                                                (ndr_push_flags_fn_t)ndr_push_package_PrimaryCLEARTEXTBlob);
1201                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1202                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1203                         ldb_asprintf_errstring(io->ac->module->ldb,
1204                                                "setup_supplemental_field: "
1205                                                "failed to push package_PrimaryCLEARTEXTBlob: %s",
1206                                                nt_errstr(status));
1207                         return LDB_ERR_OPERATIONS_ERROR;
1208                 }
1209                 pcb_hexstr = data_blob_hex_string(io->ac, &pcb_blob);
1210                 if (!pcb_hexstr) {
1211                         ldb_oom(io->ac->module->ldb);
1212                         return LDB_ERR_OPERATIONS_ERROR;
1213                 }
1214                 pc->name        = "Primary:CLEARTEXT";
1215                 pc->reserved    = 1;
1216                 pc->data        = pcb_hexstr;
1217         }
1218
1219         /*
1220          * setup 'Packages' element
1221          */
1222         pb.names = names;
1223         ndr_err = ndr_push_struct_blob(&pb_blob, io->ac, 
1224                                        lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), 
1225                                        &pb,
1226                                        (ndr_push_flags_fn_t)ndr_push_package_PackagesBlob);
1227         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1228                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1229                 ldb_asprintf_errstring(io->ac->module->ldb,
1230                                        "setup_supplemental_field: "
1231                                        "failed to push package_PackagesBlob: %s",
1232                                        nt_errstr(status));
1233                 return LDB_ERR_OPERATIONS_ERROR;
1234         }
1235         pb_hexstr = data_blob_hex_string(io->ac, &pb_blob);
1236         if (!pb_hexstr) {
1237                 ldb_oom(io->ac->module->ldb);
1238                 return LDB_ERR_OPERATIONS_ERROR;
1239         }
1240         pp->name        = "Packages";
1241         pp->reserved    = 2;
1242         pp->data        = pb_hexstr;
1243
1244         /*
1245          * setup 'supplementalCredentials' value
1246          */
1247         ZERO_STRUCT(scb);
1248         scb.sub.num_packages    = num_packages;
1249         scb.sub.packages        = packages;
1250
1251         ndr_err = ndr_push_struct_blob(&io->g.supplemental, io->ac, 
1252                                        lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
1253                                        &scb,
1254                                        (ndr_push_flags_fn_t)ndr_push_supplementalCredentialsBlob);
1255         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1256                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1257                 ldb_asprintf_errstring(io->ac->module->ldb,
1258                                        "setup_supplemental_field: "
1259                                        "failed to push supplementalCredentialsBlob: %s",
1260                                        nt_errstr(status));
1261                 return LDB_ERR_OPERATIONS_ERROR;
1262         }
1263
1264         return LDB_SUCCESS;
1265 }
1266
1267 static int setup_last_set_field(struct setup_password_fields_io *io)
1268 {
1269         /* set it as now */
1270         unix_to_nt_time(&io->g.last_set, time(NULL));
1271
1272         return LDB_SUCCESS;
1273 }
1274
1275 static int setup_kvno_field(struct setup_password_fields_io *io)
1276 {
1277         /* increment by one */
1278         io->g.kvno = io->o.kvno + 1;
1279
1280         return LDB_SUCCESS;
1281 }
1282
1283 static int setup_password_fields(struct setup_password_fields_io *io)
1284 {
1285         bool ok;
1286         int ret;
1287
1288         /*
1289          * refuse the change if someone want to change the cleartext
1290          * and supply his own hashes at the same time...
1291          */
1292         if (io->n.cleartext && (io->n.nt_hash || io->n.lm_hash)) {
1293                 ldb_asprintf_errstring(io->ac->module->ldb,
1294                                        "setup_password_fields: "
1295                                        "it's only allowed to set the cleartext password or the password hashes");
1296                 return LDB_ERR_UNWILLING_TO_PERFORM;
1297         }
1298
1299         if (io->n.cleartext) {
1300                 struct samr_Password *hash;
1301
1302                 hash = talloc(io->ac, struct samr_Password);
1303                 if (!hash) {
1304                         ldb_oom(io->ac->module->ldb);
1305                         return LDB_ERR_OPERATIONS_ERROR;
1306                 }
1307
1308                 /* compute the new nt hash */
1309                 ok = E_md4hash(io->n.cleartext, hash->hash);
1310                 if (ok) {
1311                         io->n.nt_hash = hash;
1312                 } else {
1313                         ldb_asprintf_errstring(io->ac->module->ldb,
1314                                                "setup_password_fields: "
1315                                                "failed to generate nthash from cleartext password");
1316                         return LDB_ERR_OPERATIONS_ERROR;
1317                 }
1318         }
1319
1320         if (io->n.cleartext) {
1321                 struct samr_Password *hash;
1322
1323                 hash = talloc(io->ac, struct samr_Password);
1324                 if (!hash) {
1325                         ldb_oom(io->ac->module->ldb);
1326                         return LDB_ERR_OPERATIONS_ERROR;
1327                 }
1328
1329                 /* compute the new lm hash */
1330                 ok = E_deshash(io->n.cleartext, hash->hash);
1331                 if (ok) {
1332                         io->n.lm_hash = hash;
1333                 } else {
1334                         talloc_free(hash->hash);
1335                 }
1336         }
1337
1338         if (io->n.cleartext) {
1339                 ret = setup_kerberos_keys(io);
1340                 if (ret != 0) {
1341                         return ret;
1342                 }
1343         }
1344
1345         ret = setup_nt_fields(io);
1346         if (ret != 0) {
1347                 return ret;
1348         }
1349
1350         ret = setup_lm_fields(io);
1351         if (ret != 0) {
1352                 return ret;
1353         }
1354
1355         ret = setup_supplemental_field(io);
1356         if (ret != 0) {
1357                 return ret;
1358         }
1359
1360         ret = setup_last_set_field(io);
1361         if (ret != 0) {
1362                 return ret;
1363         }
1364
1365         ret = setup_kvno_field(io);
1366         if (ret != 0) {
1367                 return ret;
1368         }
1369
1370         return LDB_SUCCESS;
1371 }
1372
1373 static struct ldb_handle *ph_init_handle(struct ldb_request *req, struct ldb_module *module, enum ph_type type)
1374 {
1375         struct ph_context *ac;
1376         struct ldb_handle *h;
1377
1378         h = talloc_zero(req, struct ldb_handle);
1379         if (h == NULL) {
1380                 ldb_set_errstring(module->ldb, "Out of Memory");
1381                 return NULL;
1382         }
1383
1384         h->module = module;
1385
1386         ac = talloc_zero(h, struct ph_context);
1387         if (ac == NULL) {
1388                 ldb_set_errstring(module->ldb, "Out of Memory");
1389                 talloc_free(h);
1390                 return NULL;
1391         }
1392
1393         h->private_data = (void *)ac;
1394
1395         h->state = LDB_ASYNC_INIT;
1396         h->status = LDB_SUCCESS;
1397
1398         ac->type = type;
1399         ac->module = module;
1400         ac->orig_req = req;
1401
1402         return h;
1403 }
1404
1405 static int get_domain_data_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1406 {
1407         struct ph_context *ac;
1408
1409         ac = talloc_get_type(context, struct ph_context);
1410
1411         /* we are interested only in the single reply (base search) we receive here */
1412         if (ares->type == LDB_REPLY_ENTRY) {
1413                 if (ac->dom_res != NULL) {
1414                         ldb_set_errstring(ldb, "Too many results");
1415                         talloc_free(ares);
1416                         return LDB_ERR_OPERATIONS_ERROR;
1417                 }
1418                 ac->dom_res = talloc_steal(ac, ares);
1419         } else {
1420                 talloc_free(ares);
1421         }
1422
1423         return LDB_SUCCESS;
1424 }
1425
1426 static int build_domain_data_request(struct ph_context *ac)
1427 {
1428         /* attrs[] is returned from this function in
1429            ac->dom_req->op.search.attrs, so it must be static, as
1430            otherwise the compiler can put it on the stack */
1431         static const char * const attrs[] = { "pwdProperties", "pwdHistoryLength", NULL };
1432         char *filter;
1433
1434         ac->dom_req = talloc_zero(ac, struct ldb_request);
1435         if (ac->dom_req == NULL) {
1436                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
1437                 return LDB_ERR_OPERATIONS_ERROR;
1438         }
1439         ac->dom_req->operation = LDB_SEARCH;
1440         ac->dom_req->op.search.base = ldb_get_default_basedn(ac->module->ldb);
1441         ac->dom_req->op.search.scope = LDB_SCOPE_SUBTREE;
1442
1443         filter = talloc_asprintf(ac->dom_req,
1444                                  "(&(objectSid=%s)(|(|(objectClass=domain)(objectClass=builtinDomain))(objectClass=samba4LocalDomain)))", 
1445                                  ldap_encode_ndr_dom_sid(ac->dom_req, ac->domain_sid));
1446         if (filter == NULL) {
1447                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
1448                 talloc_free(ac->dom_req);
1449                 return LDB_ERR_OPERATIONS_ERROR;
1450         }
1451
1452         ac->dom_req->op.search.tree = ldb_parse_tree(ac->dom_req, filter);
1453         if (ac->dom_req->op.search.tree == NULL) {
1454                 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
1455                 talloc_free(ac->dom_req);
1456                 return LDB_ERR_OPERATIONS_ERROR;
1457         }
1458         ac->dom_req->op.search.attrs = attrs;
1459         ac->dom_req->controls = NULL;
1460         ac->dom_req->context = ac;
1461         ac->dom_req->callback = get_domain_data_callback;
1462         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->dom_req);
1463
1464         return LDB_SUCCESS;
1465 }
1466
1467 static struct domain_data *get_domain_data(struct ldb_module *module, void *ctx, struct ldb_reply *res)
1468 {
1469         struct domain_data *data;
1470         const char *tmp;
1471         struct ph_context *ac;
1472         char *p;
1473
1474         ac = talloc_get_type(ctx, struct ph_context);
1475
1476         data = talloc_zero(ac, struct domain_data);
1477         if (data == NULL) {
1478                 return NULL;
1479         }
1480
1481         if (res == NULL) {
1482                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Could not find this user's domain: %s!\n", dom_sid_string(data, ac->domain_sid));
1483                 talloc_free(data);
1484                 return NULL;
1485         }
1486
1487         data->pwdProperties= samdb_result_uint(res->message, "pwdProperties", 0);
1488         data->store_cleartext = data->pwdProperties & DOMAIN_PASSWORD_STORE_CLEARTEXT;
1489         data->pwdHistoryLength = samdb_result_uint(res->message, "pwdHistoryLength", 0);
1490
1491         /* For a domain DN, this puts things in dotted notation */
1492         /* For builtin domains, this will give details for the host,
1493          * but that doesn't really matter, as it's just used for salt
1494          * and kerberos principals, which don't exist here */
1495
1496         tmp = ldb_dn_canonical_string(ctx, res->message->dn);
1497         if (!tmp) {
1498                 return NULL;
1499         }
1500         
1501         /* But it puts a trailing (or just before 'builtin') / on things, so kill that */
1502         p = strchr(tmp, '/');
1503         if (p) {
1504                 p[0] = '\0';
1505         }
1506
1507         if (tmp != NULL) {
1508                 data->dns_domain = strlower_talloc(data, tmp);
1509                 if (data->dns_domain == NULL) {
1510                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
1511                         return NULL;
1512                 }
1513                 data->realm = strupper_talloc(data, tmp);
1514                 if (data->realm == NULL) {
1515                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
1516                         return NULL;
1517                 }
1518                 p = strchr(tmp, '.');
1519                 if (p) {
1520                         p[0] = '\0';
1521                 }
1522                 data->netbios_domain = strupper_talloc(data, tmp);
1523                 if (data->netbios_domain == NULL) {
1524                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
1525                         return NULL;
1526                 }
1527         }
1528
1529         return data;
1530 }
1531
1532 static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
1533 {
1534         struct ldb_handle *h;
1535         struct ph_context *ac;
1536         struct ldb_message_element *sambaAttr;
1537         struct ldb_message_element *ntAttr;
1538         struct ldb_message_element *lmAttr;
1539         int ret;
1540
1541         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
1542
1543         if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
1544                 return ldb_next_request(module, req);
1545         }
1546
1547         /* If the caller is manipulating the local passwords directly, let them pass */
1548         if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
1549                                 req->op.add.message->dn) == 0) {
1550                 return ldb_next_request(module, req);
1551         }
1552
1553         /* nobody must touch this fields */
1554         if (ldb_msg_find_element(req->op.add.message, "ntPwdHistory")) {
1555                 return LDB_ERR_UNWILLING_TO_PERFORM;
1556         }
1557         if (ldb_msg_find_element(req->op.add.message, "lmPwdHistory")) {
1558                 return LDB_ERR_UNWILLING_TO_PERFORM;
1559         }
1560         if (ldb_msg_find_element(req->op.add.message, "supplementalCredentials")) {
1561                 return LDB_ERR_UNWILLING_TO_PERFORM;
1562         }
1563
1564         /* If no part of this ADD touches the userPassword, or the NT
1565          * or LM hashes, then we don't need to make any changes.  */
1566
1567         sambaAttr = ldb_msg_find_element(req->op.mod.message, "userPassword");
1568         ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
1569         lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
1570
1571         if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
1572                 return ldb_next_request(module, req);
1573         }
1574
1575         /* if it is not an entry of type person its an error */
1576         /* TODO: remove this when userPassword will be in schema */
1577         if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
1578                 ldb_set_errstring(module->ldb, "Cannot set a password on entry that does not have objectClass 'person'");
1579                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1580         }
1581
1582         /* check userPassword is single valued here */
1583         /* TODO: remove this when userPassword will be single valued in schema */
1584         if (sambaAttr && sambaAttr->num_values > 1) {
1585                 ldb_set_errstring(module->ldb, "mupltiple values for userPassword not allowed!\n");
1586                 return LDB_ERR_CONSTRAINT_VIOLATION;
1587         }
1588
1589         if (ntAttr && (ntAttr->num_values > 1)) {
1590                 ldb_set_errstring(module->ldb, "mupltiple values for unicodePwd not allowed!\n");
1591                 return LDB_ERR_CONSTRAINT_VIOLATION;
1592         }
1593         if (lmAttr && (lmAttr->num_values > 1)) {
1594                 ldb_set_errstring(module->ldb, "mupltiple values for dBCSPwd not allowed!\n");
1595                 return LDB_ERR_CONSTRAINT_VIOLATION;
1596         }
1597
1598         if (sambaAttr && sambaAttr->num_values == 0) {
1599                 ldb_set_errstring(module->ldb, "userPassword must have a value!\n");
1600                 return LDB_ERR_CONSTRAINT_VIOLATION;
1601         }
1602
1603         if (ntAttr && (ntAttr->num_values == 0)) {
1604                 ldb_set_errstring(module->ldb, "unicodePwd must have a value!\n");
1605                 return LDB_ERR_CONSTRAINT_VIOLATION;
1606         }
1607         if (lmAttr && (lmAttr->num_values == 0)) {
1608                 ldb_set_errstring(module->ldb, "dBCSPwd must have a value!\n");
1609                 return LDB_ERR_CONSTRAINT_VIOLATION;
1610         }
1611
1612         h = ph_init_handle(req, module, PH_ADD);
1613         if (!h) {
1614                 return LDB_ERR_OPERATIONS_ERROR;
1615         }
1616         ac = talloc_get_type(h->private_data, struct ph_context);
1617
1618         /* get user domain data */
1619         ac->domain_sid = samdb_result_sid_prefix(ac, req->op.add.message, "objectSid");
1620         if (ac->domain_sid == NULL) {
1621                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
1622                 return LDB_ERR_OPERATIONS_ERROR;
1623         }
1624
1625         ret = build_domain_data_request(ac);
1626         if (ret != LDB_SUCCESS) {
1627                 return ret;
1628         }
1629
1630         ac->step = PH_ADD_SEARCH_DOM;
1631
1632         req->handle = h;
1633
1634         return ldb_next_request(module, ac->dom_req);
1635 }
1636
1637 static int password_hash_add_do_add(struct ldb_handle *h) {
1638
1639         struct ph_context *ac;
1640         struct domain_data *domain;
1641         struct smb_krb5_context *smb_krb5_context;
1642         struct ldb_message *msg;
1643         struct setup_password_fields_io io;
1644         int ret;
1645
1646         ac = talloc_get_type(h->private_data, struct ph_context);
1647
1648         domain = get_domain_data(ac->module, ac, ac->dom_res);
1649         if (domain == NULL) {
1650                 return LDB_ERR_OPERATIONS_ERROR;
1651         }
1652
1653         ac->down_req = talloc(ac, struct ldb_request);
1654         if (ac->down_req == NULL) {
1655                 return LDB_ERR_OPERATIONS_ERROR;
1656         }
1657
1658         *(ac->down_req) = *(ac->orig_req);
1659         ac->down_req->op.add.message = msg = ldb_msg_copy_shallow(ac->down_req, ac->orig_req->op.add.message);
1660         if (ac->down_req->op.add.message == NULL) {
1661                 return LDB_ERR_OPERATIONS_ERROR;
1662         }
1663
1664         /* Some operations below require kerberos contexts */
1665         if (smb_krb5_init_context(ac->down_req, 
1666                                   ldb_get_opaque(h->module->ldb, "EventContext"), 
1667                                   (struct loadparm_context *)ldb_get_opaque(h->module->ldb, "loadparm"), 
1668                                   &smb_krb5_context) != 0) {
1669                 return LDB_ERR_OPERATIONS_ERROR;
1670         }
1671
1672         ZERO_STRUCT(io);
1673         io.ac                           = ac;
1674         io.domain                       = domain;
1675         io.smb_krb5_context             = smb_krb5_context;
1676
1677         io.u.user_account_control       = samdb_result_uint(msg, "userAccountControl", 0);
1678         io.u.sAMAccountName             = samdb_result_string(msg, "samAccountName", NULL);
1679         io.u.user_principal_name        = samdb_result_string(msg, "userPrincipalName", NULL);
1680         io.u.is_computer                = ldb_msg_check_string_attribute(msg, "objectClass", "computer");
1681
1682         io.n.cleartext                  = samdb_result_string(msg, "userPassword", NULL);
1683         io.n.nt_hash                    = samdb_result_hash(io.ac, msg, "unicodePwd");
1684         io.n.lm_hash                    = samdb_result_hash(io.ac, msg, "dBCSPwd");
1685
1686         /* remove attributes */
1687         if (io.n.cleartext) ldb_msg_remove_attr(msg, "userPassword");
1688         if (io.n.nt_hash) ldb_msg_remove_attr(msg, "unicodePwd");
1689         if (io.n.lm_hash) ldb_msg_remove_attr(msg, "dBCSPwd");
1690         ldb_msg_remove_attr(msg, "pwdLastSet");
1691         io.o.kvno = samdb_result_uint(msg, "msDs-KeyVersionNumber", 1) - 1;
1692         ldb_msg_remove_attr(msg, "msDs-KeyVersionNumber");
1693
1694         ret = setup_password_fields(&io);
1695         if (ret != LDB_SUCCESS) {
1696                 return ret;
1697         }
1698
1699         if (io.g.nt_hash) {
1700                 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
1701                                          "unicodePwd", io.g.nt_hash);
1702                 if (ret != LDB_SUCCESS) {
1703                         return ret;
1704                 }
1705         }
1706         if (io.g.lm_hash) {
1707                 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
1708                                          "dBCSPwd", io.g.lm_hash);
1709                 if (ret != LDB_SUCCESS) {
1710                         return ret;
1711                 }
1712         }
1713         if (io.g.nt_history_len > 0) {
1714                 ret = samdb_msg_add_hashes(ac, msg,
1715                                            "ntPwdHistory",
1716                                            io.g.nt_history,
1717                                            io.g.nt_history_len);
1718                 if (ret != LDB_SUCCESS) {
1719                         return ret;
1720                 }
1721         }
1722         if (io.g.lm_history_len > 0) {
1723                 ret = samdb_msg_add_hashes(ac, msg,
1724                                            "lmPwdHistory",
1725                                            io.g.lm_history,
1726                                            io.g.lm_history_len);
1727                 if (ret != LDB_SUCCESS) {
1728                         return ret;
1729                 }
1730         }
1731         if (io.g.supplemental.length > 0) {
1732                 ret = ldb_msg_add_value(msg, "supplementalCredentials",
1733                                         &io.g.supplemental, NULL);
1734                 if (ret != LDB_SUCCESS) {
1735                         return ret;
1736                 }
1737         }
1738         ret = samdb_msg_add_uint64(ac->module->ldb, ac, msg,
1739                                    "pwdLastSet",
1740                                    io.g.last_set);
1741         if (ret != LDB_SUCCESS) {
1742                 return ret;
1743         }
1744         ret = samdb_msg_add_uint(ac->module->ldb, ac, msg,
1745                                  "msDs-KeyVersionNumber",
1746                                  io.g.kvno);
1747         if (ret != LDB_SUCCESS) {
1748                 return ret;
1749         }
1750
1751         h->state = LDB_ASYNC_INIT;
1752         h->status = LDB_SUCCESS;
1753
1754         ac->step = PH_ADD_DO_ADD;
1755
1756         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->down_req);
1757
1758         /* perform the operation */
1759         return ldb_next_request(ac->module, ac->down_req);
1760 }
1761
1762 static int password_hash_mod_search_self(struct ldb_handle *h);
1763
1764 static int password_hash_modify(struct ldb_module *module, struct ldb_request *req)
1765 {
1766         struct ldb_handle *h;
1767         struct ph_context *ac;
1768         struct ldb_message_element *sambaAttr;
1769         struct ldb_message_element *ntAttr;
1770         struct ldb_message_element *lmAttr;
1771         struct ldb_message *msg;
1772
1773         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
1774
1775         if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
1776                 return ldb_next_request(module, req);
1777         }
1778         
1779         /* If the caller is manipulating the local passwords directly, let them pass */
1780         if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
1781                                 req->op.mod.message->dn) == 0) {
1782                 return ldb_next_request(module, req);
1783         }
1784
1785         /* nobody must touch password Histories */
1786         if (ldb_msg_find_element(req->op.add.message, "ntPwdHistory")) {
1787                 return LDB_ERR_UNWILLING_TO_PERFORM;
1788         }
1789         if (ldb_msg_find_element(req->op.add.message, "lmPwdHistory")) {
1790                 return LDB_ERR_UNWILLING_TO_PERFORM;
1791         }
1792         if (ldb_msg_find_element(req->op.add.message, "supplementalCredentials")) {
1793                 return LDB_ERR_UNWILLING_TO_PERFORM;
1794         }
1795
1796         sambaAttr = ldb_msg_find_element(req->op.mod.message, "userPassword");
1797         ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
1798         lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
1799
1800         /* If no part of this touches the userPassword OR unicodePwd and/or dBCSPwd, then we don't
1801          * need to make any changes.  For password changes/set there should
1802          * be a 'delete' or a 'modify' on this attribute. */
1803         if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
1804                 return ldb_next_request(module, req);
1805         }
1806
1807         /* check passwords are single valued here */
1808         /* TODO: remove this when passwords will be single valued in schema */
1809         if (sambaAttr && (sambaAttr->num_values > 1)) {
1810                 return LDB_ERR_CONSTRAINT_VIOLATION;
1811         }
1812         if (ntAttr && (ntAttr->num_values > 1)) {
1813                 return LDB_ERR_CONSTRAINT_VIOLATION;
1814         }
1815         if (lmAttr && (lmAttr->num_values > 1)) {
1816                 return LDB_ERR_CONSTRAINT_VIOLATION;
1817         }
1818
1819         h = ph_init_handle(req, module, PH_MOD);
1820         if (!h) {
1821                 return LDB_ERR_OPERATIONS_ERROR;
1822         }
1823         ac = talloc_get_type(h->private_data, struct ph_context);
1824
1825         /* return or own handle to deal with this call */
1826         req->handle = h;
1827
1828         /* prepare the first operation */
1829         ac->down_req = talloc_zero(ac, struct ldb_request);
1830         if (ac->down_req == NULL) {
1831                 ldb_set_errstring(module->ldb, "Out of memory!");
1832                 return LDB_ERR_OPERATIONS_ERROR;
1833         }
1834
1835         *(ac->down_req) = *req; /* copy the request */
1836
1837         /* use a new message structure so that we can modify it */
1838         ac->down_req->op.mod.message = msg = ldb_msg_copy_shallow(ac->down_req, req->op.mod.message);
1839
1840         /* - remove any imodification to the password from the first commit
1841          *   we will make the real modification later */
1842         if (sambaAttr) ldb_msg_remove_attr(msg, "userPassword");
1843         if (ntAttr) ldb_msg_remove_attr(msg, "unicodePwd");
1844         if (lmAttr) ldb_msg_remove_attr(msg, "dBCSPwd");
1845
1846         /* if there was nothing else to be modify skip to next step */
1847         if (msg->num_elements == 0) {
1848                 talloc_free(ac->down_req);
1849                 ac->down_req = NULL;
1850                 return password_hash_mod_search_self(h);
1851         }
1852         
1853         ac->down_req->context = NULL;
1854         ac->down_req->callback = NULL;
1855
1856         ac->step = PH_MOD_DO_REQ;
1857
1858         ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
1859
1860         return ldb_next_request(module, ac->down_req);
1861 }
1862
1863 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1864 {
1865         struct ph_context *ac;
1866
1867         ac = talloc_get_type(context, struct ph_context);
1868
1869         /* we are interested only in the single reply (base search) we receive here */
1870         if (ares->type == LDB_REPLY_ENTRY) {
1871                 if (ac->search_res != NULL) {
1872                         ldb_set_errstring(ldb, "Too many results");
1873                         talloc_free(ares);
1874                         return LDB_ERR_OPERATIONS_ERROR;
1875                 }
1876
1877                 /* if it is not an entry of type person this is an error */
1878                 /* TODO: remove this when userPassword will be in schema */
1879                 if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
1880                         ldb_set_errstring(ldb, "Object class violation");
1881                         talloc_free(ares);
1882                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
1883                 }
1884
1885                 ac->search_res = talloc_steal(ac, ares);
1886         } else {
1887                 talloc_free(ares);
1888         }
1889
1890         return LDB_SUCCESS;
1891 }
1892
1893 static int password_hash_mod_search_self(struct ldb_handle *h) {
1894
1895         struct ph_context *ac;
1896         static const char * const attrs[] = { "userAccountControl", "lmPwdHistory", 
1897                                               "ntPwdHistory", 
1898                                               "objectSid", "msDS-KeyVersionNumber", 
1899                                               "objectClass", "userPrincipalName",
1900                                               "sAMAccountName", 
1901                                               "dBCSPwd", "unicodePwd",
1902                                               "supplementalCredentials",
1903                                               NULL };
1904
1905         ac = talloc_get_type(h->private_data, struct ph_context);
1906
1907         /* prepare the search operation */
1908         ac->search_req = talloc_zero(ac, struct ldb_request);
1909         if (ac->search_req == NULL) {
1910                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
1911                 return LDB_ERR_OPERATIONS_ERROR;
1912         }
1913
1914         ac->search_req->operation = LDB_SEARCH;
1915         ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
1916         ac->search_req->op.search.scope = LDB_SCOPE_BASE;
1917         ac->search_req->op.search.tree = ldb_parse_tree(ac->search_req, NULL);
1918         if (ac->search_req->op.search.tree == NULL) {
1919                 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
1920                 return LDB_ERR_OPERATIONS_ERROR;
1921         }
1922         ac->search_req->op.search.attrs = attrs;
1923         ac->search_req->controls = NULL;
1924         ac->search_req->context = ac;
1925         ac->search_req->callback = get_self_callback;
1926         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
1927
1928         ac->step = PH_MOD_SEARCH_SELF;
1929
1930         return ldb_next_request(ac->module, ac->search_req);
1931 }
1932
1933 static int password_hash_mod_search_dom(struct ldb_handle *h) {
1934
1935         struct ph_context *ac;
1936         int ret;
1937
1938         ac = talloc_get_type(h->private_data, struct ph_context);
1939
1940         /* get object domain sid */
1941         ac->domain_sid = samdb_result_sid_prefix(ac, ac->search_res->message, "objectSid");
1942         if (ac->domain_sid == NULL) {
1943                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
1944                 return LDB_ERR_OPERATIONS_ERROR;
1945         }
1946
1947         /* get user domain data */
1948         ret = build_domain_data_request(ac);
1949         if (ret != LDB_SUCCESS) {
1950                 return ret;
1951         }
1952
1953         ac->step = PH_MOD_SEARCH_DOM;
1954
1955         return ldb_next_request(ac->module, ac->dom_req);
1956 }
1957
1958 static int password_hash_mod_do_mod(struct ldb_handle *h) {
1959
1960         struct ph_context *ac;
1961         struct domain_data *domain;
1962         struct smb_krb5_context *smb_krb5_context;
1963         struct ldb_message *msg;
1964         struct ldb_message *orig_msg;
1965         struct ldb_message *searched_msg;
1966         struct setup_password_fields_io io;
1967         int ret;
1968
1969         ac = talloc_get_type(h->private_data, struct ph_context);
1970
1971         domain = get_domain_data(ac->module, ac, ac->dom_res);
1972         if (domain == NULL) {
1973                 return LDB_ERR_OPERATIONS_ERROR;
1974         }
1975
1976         ac->mod_req = talloc(ac, struct ldb_request);
1977         if (ac->mod_req == NULL) {
1978                 return LDB_ERR_OPERATIONS_ERROR;
1979         }
1980
1981         *(ac->mod_req) = *(ac->orig_req);
1982         
1983         /* use a new message structure so that we can modify it */
1984         ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
1985         if (msg == NULL) {
1986                 return LDB_ERR_OPERATIONS_ERROR;
1987         }
1988
1989         /* modify dn */
1990         msg->dn = ac->orig_req->op.mod.message->dn;
1991
1992         /* Some operations below require kerberos contexts */
1993         if (smb_krb5_init_context(ac->mod_req, 
1994                                   ldb_get_opaque(h->module->ldb, "EventContext"), 
1995                                   (struct loadparm_context *)ldb_get_opaque(h->module->ldb, "loadparm"), 
1996                                   &smb_krb5_context) != 0) {
1997                 return LDB_ERR_OPERATIONS_ERROR;
1998         }
1999
2000         orig_msg        = discard_const(ac->orig_req->op.mod.message);
2001         searched_msg    = ac->search_res->message;
2002
2003         ZERO_STRUCT(io);
2004         io.ac                           = ac;
2005         io.domain                       = domain;
2006         io.smb_krb5_context             = smb_krb5_context;
2007
2008         io.u.user_account_control       = samdb_result_uint(searched_msg, "userAccountControl", 0);
2009         io.u.sAMAccountName             = samdb_result_string(searched_msg, "samAccountName", NULL);
2010         io.u.user_principal_name        = samdb_result_string(searched_msg, "userPrincipalName", NULL);
2011         io.u.is_computer                = ldb_msg_check_string_attribute(searched_msg, "objectClass", "computer");
2012
2013         io.n.cleartext                  = samdb_result_string(orig_msg, "userPassword", NULL);
2014         io.n.nt_hash                    = samdb_result_hash(io.ac, orig_msg, "unicodePwd");
2015         io.n.lm_hash                    = samdb_result_hash(io.ac, orig_msg, "dBCSPwd");
2016
2017         io.o.kvno                       = samdb_result_uint(searched_msg, "msDs-KeyVersionNumber", 0);
2018         io.o.nt_history_len             = samdb_result_hashes(io.ac, searched_msg, "ntPwdHistory", &io.o.nt_history);
2019         io.o.lm_history_len             = samdb_result_hashes(io.ac, searched_msg, "lmPwdHistory", &io.o.lm_history);
2020         io.o.supplemental               = ldb_msg_find_ldb_val(searched_msg, "supplementalCredentials");
2021
2022         ret = setup_password_fields(&io);
2023         if (ret != LDB_SUCCESS) {
2024                 return ret;
2025         }
2026
2027         /* make sure we replace all the old attributes */
2028         ret = ldb_msg_add_empty(msg, "unicodePwd", LDB_FLAG_MOD_REPLACE, NULL);
2029         ret = ldb_msg_add_empty(msg, "dBCSPwd", LDB_FLAG_MOD_REPLACE, NULL);
2030         ret = ldb_msg_add_empty(msg, "ntPwdHistory", LDB_FLAG_MOD_REPLACE, NULL);
2031         ret = ldb_msg_add_empty(msg, "lmPwdHistory", LDB_FLAG_MOD_REPLACE, NULL);
2032         ret = ldb_msg_add_empty(msg, "supplementalCredentials", LDB_FLAG_MOD_REPLACE, NULL);
2033         ret = ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_REPLACE, NULL);
2034         ret = ldb_msg_add_empty(msg, "msDs-KeyVersionNumber", LDB_FLAG_MOD_REPLACE, NULL);
2035
2036         if (io.g.nt_hash) {
2037                 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
2038                                          "unicodePwd", io.g.nt_hash);
2039                 if (ret != LDB_SUCCESS) {
2040                         return ret;
2041                 }
2042         }
2043         if (io.g.lm_hash) {
2044                 ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
2045                                          "dBCSPwd", io.g.lm_hash);
2046                 if (ret != LDB_SUCCESS) {
2047                         return ret;
2048                 }
2049         }
2050         if (io.g.nt_history_len > 0) {
2051                 ret = samdb_msg_add_hashes(ac, msg,
2052                                            "ntPwdHistory",
2053                                            io.g.nt_history,
2054                                            io.g.nt_history_len);
2055                 if (ret != LDB_SUCCESS) {
2056                         return ret;
2057                 }
2058         }
2059         if (io.g.lm_history_len > 0) {
2060                 ret = samdb_msg_add_hashes(ac, msg,
2061                                            "lmPwdHistory",
2062                                            io.g.lm_history,
2063                                            io.g.lm_history_len);
2064                 if (ret != LDB_SUCCESS) {
2065                         return ret;
2066                 }
2067         }
2068         if (io.g.supplemental.length > 0) {
2069                 ret = ldb_msg_add_value(msg, "supplementalCredentials",
2070                                         &io.g.supplemental, NULL);
2071                 if (ret != LDB_SUCCESS) {
2072                         return ret;
2073                 }
2074         }
2075         ret = samdb_msg_add_uint64(ac->module->ldb, ac, msg,
2076                                    "pwdLastSet",
2077                                    io.g.last_set);
2078         if (ret != LDB_SUCCESS) {
2079                 return ret;
2080         }
2081         ret = samdb_msg_add_uint(ac->module->ldb, ac, msg,
2082                                  "msDs-KeyVersionNumber",
2083                                  io.g.kvno);
2084         if (ret != LDB_SUCCESS) {
2085                 return ret;
2086         }
2087
2088         h->state = LDB_ASYNC_INIT;
2089         h->status = LDB_SUCCESS;
2090
2091         ac->step = PH_MOD_DO_MOD;
2092
2093         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
2094
2095         /* perform the search */
2096         return ldb_next_request(ac->module, ac->mod_req);
2097 }
2098
2099 static int ph_wait(struct ldb_handle *handle) {
2100         struct ph_context *ac;
2101         int ret;
2102     
2103         if (!handle || !handle->private_data) {
2104                 return LDB_ERR_OPERATIONS_ERROR;
2105         }
2106
2107         if (handle->state == LDB_ASYNC_DONE) {
2108                 return handle->status;
2109         }
2110
2111         handle->state = LDB_ASYNC_PENDING;
2112         handle->status = LDB_SUCCESS;
2113
2114         ac = talloc_get_type(handle->private_data, struct ph_context);
2115
2116         switch (ac->step) {
2117         case PH_ADD_SEARCH_DOM:
2118                 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
2119
2120                 if (ret != LDB_SUCCESS) {
2121                         handle->status = ret;
2122                         goto done;
2123                 }
2124                 if (ac->dom_req->handle->status != LDB_SUCCESS) {
2125                         handle->status = ac->dom_req->handle->status;
2126                         goto done;
2127                 }
2128
2129                 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
2130                         return LDB_SUCCESS;
2131                 }
2132
2133                 /* domain search done, go on */
2134                 return password_hash_add_do_add(handle);
2135
2136         case PH_ADD_DO_ADD:
2137                 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
2138
2139                 if (ret != LDB_SUCCESS) {
2140                         handle->status = ret;
2141                         goto done;
2142                 }
2143                 if (ac->down_req->handle->status != LDB_SUCCESS) {
2144                         handle->status = ac->down_req->handle->status;
2145                         goto done;
2146                 }
2147
2148                 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
2149                         return LDB_SUCCESS;
2150                 }
2151
2152                 break;
2153                 
2154         case PH_MOD_DO_REQ:
2155                 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
2156
2157                 if (ret != LDB_SUCCESS) {
2158                         handle->status = ret;
2159                         goto done;
2160                 }
2161                 if (ac->down_req->handle->status != LDB_SUCCESS) {
2162                         handle->status = ac->down_req->handle->status;
2163                         goto done;
2164                 }
2165
2166                 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
2167                         return LDB_SUCCESS;
2168                 }
2169
2170                 /* non-password mods done, go on */
2171                 return password_hash_mod_search_self(handle);
2172                 
2173         case PH_MOD_SEARCH_SELF:
2174                 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
2175
2176                 if (ret != LDB_SUCCESS) {
2177                         handle->status = ret;
2178                         goto done;
2179                 }
2180                 if (ac->search_req->handle->status != LDB_SUCCESS) {
2181                         handle->status = ac->search_req->handle->status;
2182                         goto done;
2183                 }
2184
2185                 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
2186                         return LDB_SUCCESS;
2187                 }
2188
2189                 if (ac->search_res == NULL) {
2190                         return LDB_ERR_NO_SUCH_OBJECT;
2191                 }
2192
2193                 /* self search done, go on */
2194                 return password_hash_mod_search_dom(handle);
2195                 
2196         case PH_MOD_SEARCH_DOM:
2197                 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
2198
2199                 if (ret != LDB_SUCCESS) {
2200                         handle->status = ret;
2201                         goto done;
2202                 }
2203                 if (ac->dom_req->handle->status != LDB_SUCCESS) {
2204                         handle->status = ac->dom_req->handle->status;
2205                         goto done;
2206                 }
2207
2208                 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
2209                         return LDB_SUCCESS;
2210                 }
2211
2212                 /* domain search done, go on */
2213                 return password_hash_mod_do_mod(handle);
2214
2215         case PH_MOD_DO_MOD:
2216                 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
2217
2218                 if (ret != LDB_SUCCESS) {
2219                         handle->status = ret;
2220                         goto done;
2221                 }
2222                 if (ac->mod_req->handle->status != LDB_SUCCESS) {
2223                         handle->status = ac->mod_req->handle->status;
2224                         goto done;
2225                 }
2226
2227                 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
2228                         return LDB_SUCCESS;
2229                 }
2230
2231                 break;
2232                 
2233         default:
2234                 ret = LDB_ERR_OPERATIONS_ERROR;
2235                 goto done;
2236         }
2237
2238         ret = LDB_SUCCESS;
2239
2240 done:
2241         handle->state = LDB_ASYNC_DONE;
2242         return ret;
2243 }
2244
2245 static int ph_wait_all(struct ldb_handle *handle) {
2246
2247         int ret;
2248
2249         while (handle->state != LDB_ASYNC_DONE) {
2250                 ret = ph_wait(handle);
2251                 if (ret != LDB_SUCCESS) {
2252                         return ret;
2253                 }
2254         }
2255
2256         return handle->status;
2257 }
2258
2259 static int password_hash_wait(struct ldb_handle *handle, enum ldb_wait_type type)
2260 {
2261         if (type == LDB_WAIT_ALL) {
2262                 return ph_wait_all(handle);
2263         } else {
2264                 return ph_wait(handle);
2265         }
2266 }
2267
2268 _PUBLIC_ const struct ldb_module_ops ldb_password_hash_module_ops = {
2269         .name          = "password_hash",
2270         .add           = password_hash_add,
2271         .modify        = password_hash_modify,
2272         .wait          = password_hash_wait
2273 };