r21362: rename:
[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
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24  *  Name: ldb
25  *
26  *  Component: ldb password_hash module
27  *
28  *  Description: correctly update hash values based on changes to sambaPassword and friends
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include "libcli/ldap/ldap.h"
35 #include "ldb/include/ldb_errors.h"
36 #include "ldb/include/ldb_private.h"
37 #include "librpc/gen_ndr/misc.h"
38 #include "librpc/gen_ndr/samr.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "libcli/security/security.h"
41 #include "system/kerberos.h"
42 #include "auth/kerberos/kerberos.h"
43 #include "system/time.h"
44 #include "dsdb/samdb/samdb.h"
45 #include "dsdb/common/flags.h"
46 #include "hdb.h"
47 #include "dsdb/samdb/ldb_modules/password_modules.h"
48
49 /* If we have decided there is reason to work on this request, then
50  * setup all the password hash types correctly.
51  *
52  * If the administrator doesn't want the sambaPassword stored (set in the
53  * domain and per-account policies) then we must strip that out before
54  * we do the first operation.
55  *
56  * Once this is done (which could update anything at all), we
57  * calculate the password hashes.
58  *
59  * This function must not only update the ntPwdHash, lmPwdHash and
60  * krb5Key fields, it must also atomicly increment the
61  * msDS-KeyVersionNumber.  We should be in a transaction, so all this
62  * should be quite safe...
63  *
64  * Finally, if the administrator has requested that a password history
65  * be maintained, then this should also be written out.
66  *
67  */
68
69 struct ph_context {
70
71         enum ph_type {PH_ADD, PH_MOD} type;
72         enum ph_step {PH_ADD_SEARCH_DOM, PH_ADD_DO_ADD, PH_MOD_DO_REQ, PH_MOD_SEARCH_SELF, PH_MOD_SEARCH_DOM, PH_MOD_DO_MOD} step;
73
74         struct ldb_module *module;
75         struct ldb_request *orig_req;
76
77         struct ldb_request *dom_req;
78         struct ldb_reply *dom_res;
79
80         struct ldb_request *down_req;
81
82         struct ldb_request *search_req;
83         struct ldb_reply *search_res;
84
85         struct ldb_request *mod_req;
86
87         struct dom_sid *domain_sid;
88 };
89
90 struct domain_data {
91         BOOL store_cleartext;
92         uint_t pwdProperties;
93         uint_t pwdHistoryLength;
94         char *dns_domain;
95         char *realm;
96 };
97
98 static int add_password_hashes(struct ldb_module *module, struct ldb_message *msg, int is_mod)
99 {
100         const char *sambaPassword;
101         struct samr_Password tmp_hash;
102         
103         sambaPassword = ldb_msg_find_attr_as_string(msg, "sambaPassword", NULL);
104         if (sambaPassword == NULL) { /* impossible, what happened ?! */
105                 return LDB_ERR_CONSTRAINT_VIOLATION;
106         }
107
108         if (is_mod) {
109                 if (ldb_msg_add_empty(msg, "unicodePwd", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
110                         return LDB_ERR_OPERATIONS_ERROR;
111                 }
112                 if (ldb_msg_add_empty(msg, "dBCSPwd", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
113                         return LDB_ERR_OPERATIONS_ERROR;
114                 }
115         }       
116
117         /* compute the new nt and lm hashes */
118         E_md4hash(sambaPassword, tmp_hash.hash);
119         if (samdb_msg_add_hash(module->ldb, msg, msg, "unicodePwd", &tmp_hash) != 0) {
120                 return LDB_ERR_OPERATIONS_ERROR;
121         }
122
123         if (E_deshash(sambaPassword, tmp_hash.hash)) {
124                 if (samdb_msg_add_hash(module->ldb, msg, msg, "dBCSPwd", &tmp_hash) != 0) {
125                         return LDB_ERR_OPERATIONS_ERROR;
126                 }
127         }
128
129         return LDB_SUCCESS;
130 }
131
132 static int add_krb5_keys_from_password(struct ldb_module *module, struct ldb_message *msg,
133                                         struct smb_krb5_context *smb_krb5_context,
134                                         struct domain_data *domain,
135                                         const char *samAccountName,
136                                         const char *user_principal_name,
137                                         int is_computer)
138 {
139         const char *sambaPassword;
140         Principal *salt_principal;
141         krb5_error_code krb5_ret;
142         size_t num_keys;
143         Key *keys;
144         int i;
145
146         /* Many, many thanks to lukeh@padl.com for this
147          * algorithm, described in his Nov 10 2004 mail to
148          * samba-technical@samba.org */
149
150         sambaPassword = ldb_msg_find_attr_as_string(msg, "sambaPassword", NULL);
151         if (sambaPassword == NULL) { /* impossible, what happened ?! */
152                 return LDB_ERR_OPERATIONS_ERROR;
153         }
154
155         if (is_computer) {
156                 /* Determine a salting principal */
157                 char *name = talloc_strdup(msg, samAccountName);
158                 char *saltbody;
159                 if (name == NULL) {
160                         ldb_asprintf_errstring(module->ldb,
161                                                 "password_hash_handle: "
162                                                 "generation of new kerberos keys failed: %s is a computer without a samAccountName",
163                                                 ldb_dn_get_linearized(msg->dn));
164                         return LDB_ERR_OPERATIONS_ERROR;
165                 }
166                 if (name[strlen(name)-1] == '$') {
167                         name[strlen(name)-1] = '\0';
168                 }
169                 saltbody = talloc_asprintf(msg, "%s.%s", name, domain->dns_domain);
170                 
171                 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
172                                                 &salt_principal,
173                                                 domain->realm, "host",
174                                                 saltbody, NULL);
175         } else if (user_principal_name) {
176                 char *p;
177                 user_principal_name = talloc_strdup(msg, user_principal_name);
178                 if (user_principal_name == NULL) {
179                         return LDB_ERR_OPERATIONS_ERROR;
180                 } else {
181                         p = strchr(user_principal_name, '@');
182                         if (p) {
183                                 p[0] = '\0';
184                         }
185                         krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
186                                                         &salt_principal,
187                                                         domain->realm, user_principal_name, NULL);
188                 } 
189         } else {
190                 if (!samAccountName) {
191                         ldb_asprintf_errstring(module->ldb,
192                                                 "password_hash_handle: "
193                                                 "generation of new kerberos keys failed: %s has no samAccountName",
194                                                 ldb_dn_get_linearized(msg->dn));
195                         return LDB_ERR_OPERATIONS_ERROR;
196                 }
197                 krb5_ret = krb5_make_principal(smb_krb5_context->krb5_context,
198                                                 &salt_principal,
199                                                 domain->realm, samAccountName,
200                                                 NULL);
201         }
202
203         if (krb5_ret) {
204                 ldb_asprintf_errstring(module->ldb,
205                                         "password_hash_handle: "
206                                         "generation of a saltking principal failed: %s",
207                                         smb_get_krb5_error_message(smb_krb5_context->krb5_context, krb5_ret, msg));
208                 return LDB_ERR_OPERATIONS_ERROR;
209         }
210
211         /* TODO: We may wish to control the encryption types chosen in future */
212         krb5_ret = hdb_generate_key_set_password(smb_krb5_context->krb5_context,
213                                                  salt_principal, sambaPassword, &keys, &num_keys);
214         krb5_free_principal(smb_krb5_context->krb5_context, salt_principal);
215
216         if (krb5_ret) {
217                 ldb_asprintf_errstring(module->ldb,
218                                         "password_hash_handle: "
219                                         "generation of new kerberos keys failed: %s",
220                                         smb_get_krb5_error_message(smb_krb5_context->krb5_context, krb5_ret, msg));
221                 return LDB_ERR_OPERATIONS_ERROR;
222         }
223
224         /* Walking all the key types generated, transform each
225          * key into an ASN.1 blob
226          */
227         for (i=0; i < num_keys; i++) {
228                 unsigned char *buf;
229                 size_t buf_size;
230                 size_t len;
231                 struct ldb_val val;
232                 int ret;
233                 
234                 if (keys[i].key.keytype == ETYPE_ARCFOUR_HMAC_MD5) {
235                         /* We might end up doing this below:
236                          * This ensures we get the unicode
237                          * conversion right.  This should also
238                          * be fixed in the Heimdal libs */
239                         continue;
240                 }
241                 ASN1_MALLOC_ENCODE(Key, buf, buf_size, &keys[i], &len, krb5_ret);
242                 if (krb5_ret) {
243                         return LDB_ERR_OPERATIONS_ERROR;
244                 }
245                 
246                 val.data = talloc_memdup(msg, buf, len);
247                 val.length = len;
248                 free(buf);
249                 if (!val.data || krb5_ret) {
250                         hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
251                         return LDB_ERR_OPERATIONS_ERROR;
252                 }
253                 ret = ldb_msg_add_value(msg, "krb5Key", &val, NULL);
254                 if (ret != LDB_SUCCESS) {
255                         hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
256                         return ret;
257                 }
258         }
259         
260         hdb_free_keys (smb_krb5_context->krb5_context, num_keys, keys);
261
262         return LDB_SUCCESS;
263 }
264
265 static int add_krb5_keys_from_NThash(struct ldb_module *module, struct ldb_message *msg,
266                                         struct smb_krb5_context *smb_krb5_context)
267 {
268         struct samr_Password *ntPwdHash;
269         krb5_error_code krb5_ret;
270         unsigned char *buf;
271         size_t buf_size;
272         size_t len;
273         struct ldb_val val;
274         Key key;
275         
276         key.mkvno = 0;
277         key.salt = NULL; /* No salt for this enc type */
278
279         ntPwdHash = samdb_result_hash(msg, msg, "unicodePwd");
280         if (ntPwdHash == NULL) { /* what happened ?! */
281                 return LDB_ERR_OPERATIONS_ERROR;
282         }
283
284         krb5_ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
285                                       ETYPE_ARCFOUR_HMAC_MD5,
286                                       ntPwdHash->hash, sizeof(ntPwdHash->hash), 
287                                       &key.key);
288         if (krb5_ret) {
289                 return LDB_ERR_OPERATIONS_ERROR;
290         }
291         ASN1_MALLOC_ENCODE(Key, buf, buf_size, &key, &len, krb5_ret);
292         if (krb5_ret) {
293                 return LDB_ERR_OPERATIONS_ERROR;
294         }
295         krb5_free_keyblock_contents(smb_krb5_context->krb5_context,
296                                     &key.key);
297         
298         val.data = talloc_memdup(msg, buf, len);
299         val.length = len;
300         free(buf);
301         if (!val.data) {
302                 return LDB_ERR_OPERATIONS_ERROR;
303         }
304         if (ldb_msg_add_value(msg, "krb5Key", &val, NULL) != 0) {
305                 return LDB_ERR_OPERATIONS_ERROR;
306         }
307
308         return LDB_SUCCESS;
309 }
310
311 static int set_pwdLastSet(struct ldb_module *module, struct ldb_message *msg, int is_mod)
312 {
313         NTTIME now_nt;
314
315         /* set it as now */
316         unix_to_nt_time(&now_nt, time(NULL));
317
318         if (!is_mod) {
319                 /* be sure there isn't a 0 value set (eg. coming from the template) */
320                 ldb_msg_remove_attr(msg, "pwdLastSet");
321                 /* add */
322                 if (ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_ADD, NULL) != 0) {
323                         return LDB_ERR_OPERATIONS_ERROR;
324                 }
325         } else {
326                 /* replace */
327                 if (ldb_msg_add_empty(msg, "pwdLastSet", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
328                         return LDB_ERR_OPERATIONS_ERROR;
329                 }
330         }
331
332         if (samdb_msg_add_uint64(module->ldb, msg, msg, "pwdLastSet", now_nt) != 0) {
333                 return LDB_ERR_OPERATIONS_ERROR;
334         }
335
336         return LDB_SUCCESS;
337 }
338
339 static int add_keyVersionNumber(struct ldb_module *module, struct ldb_message *msg, int previous)
340 {
341         /* replace or add */
342         if (ldb_msg_add_empty(msg, "msDS-KeyVersionNumber", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
343                 return LDB_ERR_OPERATIONS_ERROR;
344         }
345
346         if (samdb_msg_add_uint(module->ldb, msg, msg, "msDS-KeyVersionNumber", previous+1) != 0) {
347                 return LDB_ERR_OPERATIONS_ERROR;
348         }
349
350         return LDB_SUCCESS;
351 }
352
353 static int setPwdHistory(struct ldb_module *module, struct ldb_message *msg, struct ldb_message *old_msg, int hlen)
354 {
355         struct samr_Password *nt_hash;
356         struct samr_Password *lm_hash;
357         struct samr_Password *nt_history;
358         struct samr_Password *lm_history;
359         struct samr_Password *new_nt_history;
360         struct samr_Password *new_lm_history;
361         int nt_hist_len;
362         int lm_hist_len;
363         int i;
364
365         nt_hash = samdb_result_hash(msg, old_msg, "unicodePwd");
366         lm_hash = samdb_result_hash(msg, old_msg, "dBCSPwd");
367
368         /* if no previous passwords just return */
369         if (nt_hash == NULL && lm_hash == NULL) return LDB_SUCCESS;
370
371         nt_hist_len = samdb_result_hashes(msg, old_msg, "ntPwdHistory", &nt_history);
372         lm_hist_len = samdb_result_hashes(msg, old_msg, "lmPwdHistory", &lm_history);
373
374         /* We might not have an old NT password */
375         new_nt_history = talloc_array(msg, struct samr_Password, hlen);
376         if (new_nt_history == NULL) {
377                 return LDB_ERR_OPERATIONS_ERROR;
378         }
379         for (i = 0; i < MIN(hlen-1, nt_hist_len); i++) {
380                 new_nt_history[i+1] = nt_history[i];
381         }
382         nt_hist_len = i + 1;
383         if (nt_hash) {
384                 new_nt_history[0] = *nt_hash;
385         } else {
386                 ZERO_STRUCT(new_nt_history[0]);
387         }
388         if (ldb_msg_add_empty(msg, "ntPwdHistory", LDB_FLAG_MOD_REPLACE, NULL) != LDB_SUCCESS) {
389                 return LDB_ERR_OPERATIONS_ERROR;
390         }
391         if (samdb_msg_add_hashes(msg, msg, "ntPwdHistory", new_nt_history, nt_hist_len) != LDB_SUCCESS) {
392                 return LDB_ERR_OPERATIONS_ERROR;
393         }
394                 
395
396         /* Don't store 'long' passwords in the LM history, 
397            but make sure to 'expire' one password off the other end */
398         new_lm_history = talloc_array(msg, struct samr_Password, hlen);
399         if (new_lm_history == NULL) {
400                 return LDB_ERR_OPERATIONS_ERROR;
401         }
402         for (i = 0; i < MIN(hlen-1, lm_hist_len); i++) {
403                 new_lm_history[i+1] = lm_history[i];
404         }
405         lm_hist_len = i + 1;
406         if (lm_hash) {
407                 new_lm_history[0] = *lm_hash;
408         } else {
409                 ZERO_STRUCT(new_lm_history[0]);
410         }
411         if (ldb_msg_add_empty(msg, "lmPwdHistory", LDB_FLAG_MOD_REPLACE, NULL) != LDB_SUCCESS) {
412                 return LDB_ERR_OPERATIONS_ERROR;
413         }
414         if (samdb_msg_add_hashes(msg, msg, "lmPwdHistory", new_lm_history, lm_hist_len) != LDB_SUCCESS) {
415                 return LDB_ERR_OPERATIONS_ERROR;
416         }
417
418         return LDB_SUCCESS;
419 }
420
421 static struct ldb_handle *ph_init_handle(struct ldb_request *req, struct ldb_module *module, enum ph_type type)
422 {
423         struct ph_context *ac;
424         struct ldb_handle *h;
425
426         h = talloc_zero(req, struct ldb_handle);
427         if (h == NULL) {
428                 ldb_set_errstring(module->ldb, "Out of Memory");
429                 return NULL;
430         }
431
432         h->module = module;
433
434         ac = talloc_zero(h, struct ph_context);
435         if (ac == NULL) {
436                 ldb_set_errstring(module->ldb, "Out of Memory");
437                 talloc_free(h);
438                 return NULL;
439         }
440
441         h->private_data = (void *)ac;
442
443         h->state = LDB_ASYNC_INIT;
444         h->status = LDB_SUCCESS;
445
446         ac->type = type;
447         ac->module = module;
448         ac->orig_req = req;
449
450         return h;
451 }
452
453 static int get_domain_data_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
454 {
455         struct ph_context *ac;
456
457         if (!context || !ares) {
458                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
459                 return LDB_ERR_OPERATIONS_ERROR;
460         }
461
462         ac = talloc_get_type(context, struct ph_context);
463
464         /* we are interested only in the single reply (base search) we receive here */
465         if (ares->type == LDB_REPLY_ENTRY) {
466                 if (ac->dom_res != NULL) {
467                         ldb_set_errstring(ldb, "Too many results");
468                         talloc_free(ares);
469                         return LDB_ERR_OPERATIONS_ERROR;
470                 }
471                 ac->dom_res = talloc_steal(ac, ares);
472         } else {
473                 talloc_free(ares);
474         }
475
476         return LDB_SUCCESS;
477 }
478
479 static int build_domain_data_request(struct ph_context *ac)
480 {
481         /* attrs[] is returned from this function in
482            ac->dom_req->op.search.attrs, so it must be static, as
483            otherwise the compiler can put it on the stack */
484         static const char * const attrs[] = { "pwdProperties", "pwdHistoryLength", NULL };
485         char *filter;
486
487         ac->dom_req = talloc_zero(ac, struct ldb_request);
488         if (ac->dom_req == NULL) {
489                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
490                 return LDB_ERR_OPERATIONS_ERROR;
491         }
492         ac->dom_req->operation = LDB_SEARCH;
493         ac->dom_req->op.search.base = ldb_get_default_basedn(ac->module->ldb);
494         ac->dom_req->op.search.scope = LDB_SCOPE_SUBTREE;
495
496         filter = talloc_asprintf(ac->dom_req, "(&(objectSid=%s)(|(objectClass=domain)(objectClass=builtinDomain)))", 
497                                  ldap_encode_ndr_dom_sid(ac->dom_req, ac->domain_sid));
498         if (filter == NULL) {
499                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
500                 talloc_free(ac->dom_req);
501                 return LDB_ERR_OPERATIONS_ERROR;
502         }
503
504         ac->dom_req->op.search.tree = ldb_parse_tree(ac->dom_req, filter);
505         if (ac->dom_req->op.search.tree == NULL) {
506                 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
507                 talloc_free(ac->dom_req);
508                 return LDB_ERR_OPERATIONS_ERROR;
509         }
510         ac->dom_req->op.search.attrs = attrs;
511         ac->dom_req->controls = NULL;
512         ac->dom_req->context = ac;
513         ac->dom_req->callback = get_domain_data_callback;
514         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->dom_req);
515
516         return LDB_SUCCESS;
517 }
518
519 static struct domain_data *get_domain_data(struct ldb_module *module, void *ctx, struct ldb_reply *res)
520 {
521         struct domain_data *data;
522         const char *tmp;
523         struct ph_context *ac;
524         char *p;
525
526         ac = talloc_get_type(ctx, struct ph_context);
527
528         data = talloc_zero(ac, struct domain_data);
529         if (data == NULL) {
530                 return NULL;
531         }
532
533         if (res == NULL) {
534                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Could not find this user's domain: %s!\n", dom_sid_string(data, ac->domain_sid));
535                 talloc_free(data);
536                 return NULL;
537         }
538
539         data->pwdProperties= samdb_result_uint(res->message, "pwdProperties", 0);
540         data->store_cleartext = data->pwdProperties & DOMAIN_PASSWORD_STORE_CLEARTEXT;
541         data->pwdHistoryLength = samdb_result_uint(res->message, "pwdHistoryLength", 0);
542
543         /* For a domain DN, this puts things in dotted notation */
544         /* For builtin domains, this will give details for the host,
545          * but that doesn't really matter, as it's just used for salt
546          * and kerberos principals, which don't exist here */
547
548         tmp = ldb_dn_canonical_string(ctx, res->message->dn);
549         if (!tmp) {
550                 return NULL;
551         }
552         
553         /* But it puts a trailing (or just before 'builtin') / on things, so kill that */
554         p = strchr(tmp, '/');
555         if (p) {
556                 p[0] = '\0';
557         }
558
559         if (tmp != NULL) {
560                 data->dns_domain = strlower_talloc(data, tmp);
561                 if (data->dns_domain == NULL) {
562                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
563                         return NULL;
564                 }
565                 data->realm = strupper_talloc(data, tmp);
566                 if (data->realm == NULL) {
567                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Out of memory!\n");
568                         return NULL;
569                 }
570         }
571
572         return data;
573 }
574
575 static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
576 {
577         struct ldb_handle *h;
578         struct ph_context *ac;
579         struct ldb_message_element *sambaAttr;
580         struct ldb_message_element *ntAttr;
581         struct ldb_message_element *lmAttr;
582         int ret;
583
584         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
585
586         if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
587                 return ldb_next_request(module, req);
588         }
589
590         /* If the caller is manipulating the local passwords directly, let them pass */
591         if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
592                                 req->op.add.message->dn) == 0) {
593                 return ldb_next_request(module, req);
594         }
595
596         /* nobody must touch password Histories */
597         if (ldb_msg_find_element(req->op.add.message, "ntPwdHistory") ||
598             ldb_msg_find_element(req->op.add.message, "lmPwdHistory")) {
599                 return LDB_ERR_UNWILLING_TO_PERFORM;
600         }
601
602         /* If no part of this ADD touches the sambaPassword, or the NT
603          * or LM hashes, then we don't need to make any changes.  */
604
605         sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
606         ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
607         lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
608
609         if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
610                 return ldb_next_request(module, req);
611         }
612
613         /* if it is not an entry of type person its an error */
614         /* TODO: remove this when sambaPassword will be in schema */
615         if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
616                 ldb_set_errstring(module->ldb, "Cannot set a password on entry that does not have objectClass 'person'");
617                 return LDB_ERR_OBJECT_CLASS_VIOLATION;
618         }
619
620         /* check sambaPassword is single valued here */
621         /* TODO: remove this when sambaPassword will be single valued in schema */
622         if (sambaAttr && sambaAttr->num_values > 1) {
623                 ldb_set_errstring(module->ldb, "mupltiple values for sambaPassword not allowed!\n");
624                 return LDB_ERR_CONSTRAINT_VIOLATION;
625         }
626
627         if (ntAttr && (ntAttr->num_values > 1)) {
628                 ldb_set_errstring(module->ldb, "mupltiple values for lmPwdHash not allowed!\n");
629                 return LDB_ERR_CONSTRAINT_VIOLATION;
630         }
631         if (lmAttr && (lmAttr->num_values > 1)) {
632                 ldb_set_errstring(module->ldb, "mupltiple values for lmPwdHash not allowed!\n");
633                 return LDB_ERR_CONSTRAINT_VIOLATION;
634         }
635
636         if (sambaAttr && sambaAttr->num_values == 0) {
637                 ldb_set_errstring(module->ldb, "sambaPassword must have a value!\n");
638                 return LDB_ERR_CONSTRAINT_VIOLATION;
639         }
640
641         if (ntAttr && (ntAttr->num_values == 0)) {
642                 ldb_set_errstring(module->ldb, "lmPwdHash must have a value!\n");
643                 return LDB_ERR_CONSTRAINT_VIOLATION;
644         }
645         if (lmAttr && (lmAttr->num_values == 0)) {
646                 ldb_set_errstring(module->ldb, "lmPwdHash must have a value!\n");
647                 return LDB_ERR_CONSTRAINT_VIOLATION;
648         }
649
650         h = ph_init_handle(req, module, PH_ADD);
651         if (!h) {
652                 return LDB_ERR_OPERATIONS_ERROR;
653         }
654         ac = talloc_get_type(h->private_data, struct ph_context);
655
656         /* get user domain data */
657         ac->domain_sid = samdb_result_sid_prefix(ac, req->op.add.message, "objectSid");
658         if (ac->domain_sid == NULL) {
659                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
660                 return LDB_ERR_OPERATIONS_ERROR;
661         }
662
663         ret = build_domain_data_request(ac);
664         if (ret != LDB_SUCCESS) {
665                 return ret;
666         }
667
668         ac->step = PH_ADD_SEARCH_DOM;
669
670         req->handle = h;
671
672         return ldb_next_request(module, ac->dom_req);
673 }
674
675 static int password_hash_add_do_add(struct ldb_handle *h) {
676
677         struct ph_context *ac;
678         struct domain_data *domain;
679         struct smb_krb5_context *smb_krb5_context;
680         struct ldb_message_element *sambaAttr;
681         struct ldb_message *msg;
682         int ret;
683
684         ac = talloc_get_type(h->private_data, struct ph_context);
685
686         domain = get_domain_data(ac->module, ac, ac->dom_res);
687         if (domain == NULL) {
688                 return LDB_ERR_OPERATIONS_ERROR;
689         }
690
691         ac->down_req = talloc(ac, struct ldb_request);
692         if (ac->down_req == NULL) {
693                 return LDB_ERR_OPERATIONS_ERROR;
694         }
695
696         *(ac->down_req) = *(ac->orig_req);
697         ac->down_req->op.add.message = msg = ldb_msg_copy_shallow(ac->down_req, ac->orig_req->op.add.message);
698         if (ac->down_req->op.add.message == NULL) {
699                 return LDB_ERR_OPERATIONS_ERROR;
700         }
701
702         /* Some operations below require kerberos contexts */
703         if (smb_krb5_init_context(ac->down_req, &smb_krb5_context) != 0) {
704                 return LDB_ERR_OPERATIONS_ERROR;
705         }
706
707         /* if we have sambaPassword in the original message add the operatio on it here */
708         sambaAttr = ldb_msg_find_element(msg, "sambaPassword");
709         if (sambaAttr) {
710                 unsigned int user_account_control;
711                 ret = add_password_hashes(ac->module, msg, 0);
712                 /* we can compute new password hashes from the unicode password */
713                 if (ret != LDB_SUCCESS) {
714                         return ret;
715                 }
716                 
717                 /* now add krb5 keys based on unicode password */
718                 ret = add_krb5_keys_from_password(ac->module, msg, smb_krb5_context, domain,
719                                                   ldb_msg_find_attr_as_string(msg, "samAccountName", NULL),
720                                                   ldb_msg_find_attr_as_string(msg, "userPrincipalName", NULL),
721                                                   ldb_msg_check_string_attribute(msg, "objectClass", "computer"));
722                 if (ret != LDB_SUCCESS) {
723                         return ret;
724                 }
725                 
726                 /* if both the domain properties and the user account controls do not permit
727                  * clear text passwords then wipe out the sambaPassword */
728                 user_account_control = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
729                 if (domain->store_cleartext && (user_account_control & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
730                         /* Keep sambaPassword attribute */
731                 } else {
732                         ldb_msg_remove_attr(msg, "sambaPassword");
733                 }
734         }
735
736         /* add also krb5 keys based on NT the hash (we might have ntPwdHash, but not the cleartext */
737         ret = add_krb5_keys_from_NThash(ac->module, msg, smb_krb5_context);
738         if (ret != LDB_SUCCESS) {
739                 return ret;
740         }
741                 
742         /* don't touch it if a value is set. It could be an incoming samsync */
743         if (ldb_msg_find_attr_as_uint64(msg, "pwdLastSet", 0) == 0) {
744                 if (set_pwdLastSet(ac->module, msg, 0) != LDB_SUCCESS) {
745                         return LDB_ERR_OPERATIONS_ERROR;
746                 }
747         }
748
749         /* don't touch it if a value is set. It could be an incoming samsync */
750         if (!ldb_msg_find_element(msg, "msDS-KeyVersionNumber")) {
751                 if (add_keyVersionNumber(ac->module, msg, 0) != LDB_SUCCESS) {
752                         return LDB_ERR_OPERATIONS_ERROR;
753                 }
754         }
755
756         h->state = LDB_ASYNC_INIT;
757         h->status = LDB_SUCCESS;
758
759         ac->step = PH_ADD_DO_ADD;
760
761         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->down_req);
762
763         /* perform the operation */
764         return ldb_next_request(ac->module, ac->down_req);
765 }
766
767 static int password_hash_mod_search_self(struct ldb_handle *h);
768
769 static int password_hash_modify(struct ldb_module *module, struct ldb_request *req)
770 {
771         struct ldb_handle *h;
772         struct ph_context *ac;
773         struct ldb_message_element *sambaAttr;
774         struct ldb_message_element *ntAttr;
775         struct ldb_message_element *lmAttr;
776         struct ldb_message *msg;
777
778         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
779
780         if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
781                 return ldb_next_request(module, req);
782         }
783         
784         /* If the caller is manipulating the local passwords directly, let them pass */
785         if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
786                                 req->op.mod.message->dn) == 0) {
787                 return ldb_next_request(module, req);
788         }
789
790         /* nobody must touch password Histories */
791         if (ldb_msg_find_element(req->op.mod.message, "ntPwdHistory") ||
792             ldb_msg_find_element(req->op.mod.message, "lmPwdHistory")) {
793                 return LDB_ERR_UNWILLING_TO_PERFORM;
794         }
795
796         sambaAttr = ldb_msg_find_element(req->op.mod.message, "sambaPassword");
797         ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
798         lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
799
800         /* check passwords are single valued here */
801         /* TODO: remove this when passwords will be single valued in schema */
802         if (sambaAttr && (sambaAttr->num_values > 1)) {
803                 return LDB_ERR_CONSTRAINT_VIOLATION;
804         }
805         if (ntAttr && (ntAttr->num_values > 1)) {
806                 return LDB_ERR_CONSTRAINT_VIOLATION;
807         }
808         if (lmAttr && (lmAttr->num_values > 1)) {
809                 return LDB_ERR_CONSTRAINT_VIOLATION;
810         }
811
812         /* If no part of this touches the sambaPassword OR ntPwdHash and/or lmPwdHash, then we don't
813          * need to make any changes.  For password changes/set there should
814          * be a 'delete' or a 'modify' on this attribute. */
815         /* If the only operation is the deletion of the passwords then go on */
816         if (       ((!sambaAttr) || ((sambaAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))
817                 && ((!ntAttr) || ((ntAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))
818                 && ((!lmAttr) || ((lmAttr->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE))  ) {
819
820                 return ldb_next_request(module, req);
821         }
822
823         h = ph_init_handle(req, module, PH_MOD);
824         if (!h) {
825                 return LDB_ERR_OPERATIONS_ERROR;
826         }
827         ac = talloc_get_type(h->private_data, struct ph_context);
828
829         /* return or own handle to deal with this call */
830         req->handle = h;
831
832         /* prepare the first operation */
833         ac->down_req = talloc_zero(ac, struct ldb_request);
834         if (ac->down_req == NULL) {
835                 ldb_set_errstring(module->ldb, "Out of memory!");
836                 return LDB_ERR_OPERATIONS_ERROR;
837         }
838
839         *(ac->down_req) = *req; /* copy the request */
840
841         /* use a new message structure so that we can modify it */
842         ac->down_req->op.mod.message = msg = ldb_msg_copy_shallow(ac->down_req, req->op.mod.message);
843
844         /* - remove any imodification to the password from the first commit
845          *   we will make the real modification later */
846         if (sambaAttr) ldb_msg_remove_attr(msg, "sambaPassword");
847         if (ntAttr) ldb_msg_remove_attr(msg, "unicodePwd");
848         if (lmAttr) ldb_msg_remove_attr(msg, "dBCSPwd");
849
850         /* if there was nothing else to be modify skip to next step */
851         if (msg->num_elements == 0) {
852                 talloc_free(ac->down_req);
853                 ac->down_req = NULL;
854                 return password_hash_mod_search_self(h);
855         }
856         
857         ac->down_req->context = NULL;
858         ac->down_req->callback = NULL;
859
860         ac->step = PH_MOD_DO_REQ;
861
862         ldb_set_timeout_from_prev_req(module->ldb, req, ac->down_req);
863
864         return ldb_next_request(module, ac->down_req);
865 }
866
867 static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
868 {
869         struct ph_context *ac;
870
871         if (!context || !ares) {
872                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
873                 return LDB_ERR_OPERATIONS_ERROR;
874         }
875
876         ac = talloc_get_type(context, struct ph_context);
877
878         /* we are interested only in the single reply (base search) we receive here */
879         if (ares->type == LDB_REPLY_ENTRY) {
880                 if (ac->search_res != NULL) {
881                         ldb_set_errstring(ldb, "Too many results");
882                         talloc_free(ares);
883                         return LDB_ERR_OPERATIONS_ERROR;
884                 }
885
886                 /* if it is not an entry of type person this is an error */
887                 /* TODO: remove this when sambaPassword will be in schema */
888                 if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
889                         ldb_set_errstring(ldb, "Object class violation");
890                         talloc_free(ares);
891                         return LDB_ERR_OBJECT_CLASS_VIOLATION;
892                 }
893
894                 ac->search_res = talloc_steal(ac, ares);
895         } else {
896                 talloc_free(ares);
897         }
898
899         return LDB_SUCCESS;
900 }
901
902 static int password_hash_mod_search_self(struct ldb_handle *h) {
903
904         struct ph_context *ac;
905         static const char * const attrs[] = { "userAccountControl", "lmPwdHistory", 
906                                               "ntPwdHistory", 
907                                               "objectSid", "msDS-KeyVersionNumber", 
908                                               "objectClass", "userPrincipalName",
909                                               "samAccountName", 
910                                               "dBCSPwd", "unicodePwd",
911                                               NULL };
912
913         ac = talloc_get_type(h->private_data, struct ph_context);
914
915         /* prepare the search operation */
916         ac->search_req = talloc_zero(ac, struct ldb_request);
917         if (ac->search_req == NULL) {
918                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
919                 return LDB_ERR_OPERATIONS_ERROR;
920         }
921
922         ac->search_req->operation = LDB_SEARCH;
923         ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
924         ac->search_req->op.search.scope = LDB_SCOPE_BASE;
925         ac->search_req->op.search.tree = ldb_parse_tree(ac->search_req, NULL);
926         if (ac->search_req->op.search.tree == NULL) {
927                 ldb_set_errstring(ac->module->ldb, "Invalid search filter");
928                 return LDB_ERR_OPERATIONS_ERROR;
929         }
930         ac->search_req->op.search.attrs = attrs;
931         ac->search_req->controls = NULL;
932         ac->search_req->context = ac;
933         ac->search_req->callback = get_self_callback;
934         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
935
936         ac->step = PH_MOD_SEARCH_SELF;
937
938         return ldb_next_request(ac->module, ac->search_req);
939 }
940
941 static int password_hash_mod_search_dom(struct ldb_handle *h) {
942
943         struct ph_context *ac;
944         int ret;
945
946         ac = talloc_get_type(h->private_data, struct ph_context);
947
948         /* get object domain sid */
949         ac->domain_sid = samdb_result_sid_prefix(ac, ac->search_res->message, "objectSid");
950         if (ac->domain_sid == NULL) {
951                 ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "can't handle entry with missing objectSid!\n");
952                 return LDB_ERR_OPERATIONS_ERROR;
953         }
954
955         /* get user domain data */
956         ret = build_domain_data_request(ac);
957         if (ret != LDB_SUCCESS) {
958                 return ret;
959         }
960
961         ac->step = PH_MOD_SEARCH_DOM;
962
963         return ldb_next_request(ac->module, ac->dom_req);
964 }
965
966 static int password_hash_mod_do_mod(struct ldb_handle *h) {
967
968         struct ph_context *ac;
969         struct domain_data *domain;
970         struct smb_krb5_context *smb_krb5_context;
971         struct ldb_message_element *sambaAttr;
972         struct ldb_message *msg;
973         int phlen;
974         int ret;
975         BOOL added_hashes = False;
976
977         ac = talloc_get_type(h->private_data, struct ph_context);
978
979         domain = get_domain_data(ac->module, ac, ac->dom_res);
980         if (domain == NULL) {
981                 return LDB_ERR_OPERATIONS_ERROR;
982         }
983
984         ac->mod_req = talloc(ac, struct ldb_request);
985         if (ac->mod_req == NULL) {
986                 return LDB_ERR_OPERATIONS_ERROR;
987         }
988
989         *(ac->mod_req) = *(ac->orig_req);
990         
991         /* use a new message structure so that we can modify it */
992         ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
993         if (msg == NULL) {
994                 return LDB_ERR_OPERATIONS_ERROR;
995         }
996
997         /* modify dn */
998         msg->dn = ac->orig_req->op.mod.message->dn;
999
1000         /* Some operations below require kerberos contexts */
1001         if (smb_krb5_init_context(ac->mod_req, &smb_krb5_context) != 0) {
1002                 return LDB_ERR_OPERATIONS_ERROR;
1003         }
1004
1005         /* we are going to replace the existing krb5key or delete it */
1006         if (ldb_msg_add_empty(msg, "krb5key", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
1007                 return LDB_ERR_OPERATIONS_ERROR;
1008         }
1009
1010         /* if we have sambaPassword in the original message add the operation on it here */
1011         sambaAttr = ldb_msg_find_element(ac->orig_req->op.mod.message, "sambaPassword");
1012         if (sambaAttr) {
1013
1014                 if (ldb_msg_add(msg, sambaAttr, sambaAttr->flags) != 0) {
1015                         return LDB_ERR_OPERATIONS_ERROR;
1016                 }
1017
1018                 /* if we are actually settting a new unicode password,
1019                  * use it to generate the password hashes */
1020                 if (((sambaAttr->flags & LDB_FLAG_MOD_MASK) != LDB_FLAG_MOD_DELETE)
1021                     && (sambaAttr->num_values == 1)) {
1022                         /* we can compute new password hashes from the unicode password */
1023                         ret = add_password_hashes(ac->module, msg, 1);
1024                         if (ret != LDB_SUCCESS) {
1025                                 return ret;
1026                         }
1027
1028                         added_hashes = True;
1029
1030                         /* now add krb5 keys based on unicode password */
1031                         ret = add_krb5_keys_from_password(ac->module, msg, smb_krb5_context, domain,
1032                                                           ldb_msg_find_attr_as_string(ac->search_res->message, "samAccountName", NULL),
1033                                                           ldb_msg_find_attr_as_string(ac->search_res->message, "userPrincipalName", NULL),
1034                                                           ldb_msg_check_string_attribute(ac->search_res->message, "objectClass", "computer"));
1035
1036                         if (ret != LDB_SUCCESS) {
1037                                 return ret;
1038                         }
1039
1040                         /* if the domain properties or the user account controls do not permit
1041                          * clear text passwords then wipe out the sambaPassword */
1042                         if (domain->store_cleartext &&
1043                             (ldb_msg_find_attr_as_uint(ac->search_res->message, "userAccountControl", 0) & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
1044                                 /* Keep sambaPassword attribute */
1045                         } else {
1046                                 ldb_msg_remove_attr(msg, "sambaPassword");
1047                         }
1048
1049                 }
1050         }
1051
1052         /* if we didn't create the hashes above, try using values supplied directly */
1053         if (!added_hashes) {
1054                 struct ldb_message_element *el;
1055                 
1056                 el = ldb_msg_find_element(ac->orig_req->op.mod.message, "unicodePwd");
1057                 if (ldb_msg_add(msg, el, el->flags) != 0) {
1058                         return LDB_ERR_OPERATIONS_ERROR;
1059                 }
1060                 
1061                 el = ldb_msg_find_element(ac->orig_req->op.mod.message, "dBCSPwd");
1062                 if (ldb_msg_add(msg, el, el->flags) != 0) {
1063                         return LDB_ERR_OPERATIONS_ERROR;
1064                 }
1065         }
1066
1067         /* add also krb5 keys based on NT the hash */
1068         if (add_krb5_keys_from_NThash(ac->module, msg, smb_krb5_context) != LDB_SUCCESS) {
1069                 return LDB_ERR_OPERATIONS_ERROR;
1070         }
1071
1072         /* set change time */
1073         if (set_pwdLastSet(ac->module, msg, 1) != LDB_SUCCESS) {
1074                 return LDB_ERR_OPERATIONS_ERROR;
1075         }
1076
1077         /* don't touch it if a value is set. It could be an incoming samsync */
1078         if (!ldb_msg_find_element(ac->orig_req->op.mod.message, 
1079                                  "msDS-KeyVersionNumber")) {
1080                 if (add_keyVersionNumber(ac->module, msg,
1081                                          ldb_msg_find_attr_as_uint(ac->search_res->message, 
1082                                                            "msDS-KeyVersionNumber", 0)
1083                             ) != LDB_SUCCESS) {
1084                         return LDB_ERR_OPERATIONS_ERROR;
1085                 }
1086         }
1087
1088         if ((phlen = samdb_result_uint(ac->dom_res->message, "pwdHistoryLength", 0)) > 0) {
1089                 if (setPwdHistory(ac->module, msg, ac->search_res->message, phlen) != LDB_SUCCESS) {
1090                         return LDB_ERR_OPERATIONS_ERROR;
1091                 }
1092         }
1093
1094         h->state = LDB_ASYNC_INIT;
1095         h->status = LDB_SUCCESS;
1096
1097         ac->step = PH_MOD_DO_MOD;
1098
1099         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->mod_req);
1100
1101         /* perform the search */
1102         return ldb_next_request(ac->module, ac->mod_req);
1103 }
1104
1105 static int ph_wait(struct ldb_handle *handle) {
1106         struct ph_context *ac;
1107         int ret;
1108     
1109         if (!handle || !handle->private_data) {
1110                 return LDB_ERR_OPERATIONS_ERROR;
1111         }
1112
1113         if (handle->state == LDB_ASYNC_DONE) {
1114                 return handle->status;
1115         }
1116
1117         handle->state = LDB_ASYNC_PENDING;
1118         handle->status = LDB_SUCCESS;
1119
1120         ac = talloc_get_type(handle->private_data, struct ph_context);
1121
1122         switch (ac->step) {
1123         case PH_ADD_SEARCH_DOM:
1124                 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1125
1126                 if (ret != LDB_SUCCESS) {
1127                         handle->status = ret;
1128                         goto done;
1129                 }
1130                 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1131                         handle->status = ac->dom_req->handle->status;
1132                         goto done;
1133                 }
1134
1135                 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1136                         return LDB_SUCCESS;
1137                 }
1138
1139                 /* domain search done, go on */
1140                 return password_hash_add_do_add(handle);
1141
1142         case PH_ADD_DO_ADD:
1143                 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1144
1145                 if (ret != LDB_SUCCESS) {
1146                         handle->status = ret;
1147                         goto done;
1148                 }
1149                 if (ac->down_req->handle->status != LDB_SUCCESS) {
1150                         handle->status = ac->down_req->handle->status;
1151                         goto done;
1152                 }
1153
1154                 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1155                         return LDB_SUCCESS;
1156                 }
1157
1158                 break;
1159                 
1160         case PH_MOD_DO_REQ:
1161                 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
1162
1163                 if (ret != LDB_SUCCESS) {
1164                         handle->status = ret;
1165                         goto done;
1166                 }
1167                 if (ac->down_req->handle->status != LDB_SUCCESS) {
1168                         handle->status = ac->down_req->handle->status;
1169                         goto done;
1170                 }
1171
1172                 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
1173                         return LDB_SUCCESS;
1174                 }
1175
1176                 /* non-password mods done, go on */
1177                 return password_hash_mod_search_self(handle);
1178                 
1179         case PH_MOD_SEARCH_SELF:
1180                 ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
1181
1182                 if (ret != LDB_SUCCESS) {
1183                         handle->status = ret;
1184                         goto done;
1185                 }
1186                 if (ac->search_req->handle->status != LDB_SUCCESS) {
1187                         handle->status = ac->search_req->handle->status;
1188                         goto done;
1189                 }
1190
1191                 if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
1192                         return LDB_SUCCESS;
1193                 }
1194
1195                 if (ac->search_res == NULL) {
1196                         return LDB_ERR_NO_SUCH_OBJECT;
1197                 }
1198
1199                 /* self search done, go on */
1200                 return password_hash_mod_search_dom(handle);
1201                 
1202         case PH_MOD_SEARCH_DOM:
1203                 ret = ldb_wait(ac->dom_req->handle, LDB_WAIT_NONE);
1204
1205                 if (ret != LDB_SUCCESS) {
1206                         handle->status = ret;
1207                         goto done;
1208                 }
1209                 if (ac->dom_req->handle->status != LDB_SUCCESS) {
1210                         handle->status = ac->dom_req->handle->status;
1211                         goto done;
1212                 }
1213
1214                 if (ac->dom_req->handle->state != LDB_ASYNC_DONE) {
1215                         return LDB_SUCCESS;
1216                 }
1217
1218                 /* domain search done, go on */
1219                 return password_hash_mod_do_mod(handle);
1220
1221         case PH_MOD_DO_MOD:
1222                 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
1223
1224                 if (ret != LDB_SUCCESS) {
1225                         handle->status = ret;
1226                         goto done;
1227                 }
1228                 if (ac->mod_req->handle->status != LDB_SUCCESS) {
1229                         handle->status = ac->mod_req->handle->status;
1230                         goto done;
1231                 }
1232
1233                 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
1234                         return LDB_SUCCESS;
1235                 }
1236
1237                 break;
1238                 
1239         default:
1240                 ret = LDB_ERR_OPERATIONS_ERROR;
1241                 goto done;
1242         }
1243
1244         ret = LDB_SUCCESS;
1245
1246 done:
1247         handle->state = LDB_ASYNC_DONE;
1248         return ret;
1249 }
1250
1251 static int ph_wait_all(struct ldb_handle *handle) {
1252
1253         int ret;
1254
1255         while (handle->state != LDB_ASYNC_DONE) {
1256                 ret = ph_wait(handle);
1257                 if (ret != LDB_SUCCESS) {
1258                         return ret;
1259                 }
1260         }
1261
1262         return handle->status;
1263 }
1264
1265 static int password_hash_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1266 {
1267         if (type == LDB_WAIT_ALL) {
1268                 return ph_wait_all(handle);
1269         } else {
1270                 return ph_wait(handle);
1271         }
1272 }
1273
1274 static const struct ldb_module_ops password_hash_ops = {
1275         .name          = "password_hash",
1276         .add           = password_hash_add,
1277         .modify        = password_hash_modify,
1278         .wait          = password_hash_wait
1279 };
1280
1281
1282 int password_hash_module_init(void)
1283 {
1284         return ldb_register_module(&password_hash_ops);
1285 }