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