1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
/*
ldb database module
Copyright (C) Simo Sorce 2004-2006
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2006
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Name: ldb
*
* Component: ldb local_password module
*
* Description: correctly update hash values based on changes to sambaPassword and friends
*
* Author: Andrew Bartlett
*/
#include "includes.h"
#include "libcli/ldap/ldap.h"
#include "ldb/include/ldb_errors.h"
#include "ldb/include/ldb_private.h"
#include "dsdb/samdb/samdb.h"
#include "librpc/ndr/libndr.h"
#include "dsdb/samdb/ldb_modules/password_modules.h"
#define PASSWORD_GUID_ATTR "masterGUID"
/* This module maintains a local password database, seperate from the main LDAP server.
This allows the password database to be syncronised in a multi-master
fashion, seperate to the more difficult concerns of the main
database. (With passwords, the last writer always wins)
Each incoming add/modify is split into a remote, and a local request, done in that order.
We maintain a list of attributes that are kept locally:
*/
static const char * const password_attrs[] = {
"supplementalCredentials",
"unicodePwd",
"dBCSPwd",
"lmPwdHistory",
"ntPwdHistory",
"msDS-KeyVersionNumber",
"pwdLastSet"
};
/* And we merge them back into search requests when asked to do so */
struct lpdb_context {
enum lpdb_type {LPDB_ADD, LPDB_MOD, LPDB_SEARCH} type;
enum lpdb_step {LPDB_ADD_REMOTE, LPDB_MOD_REMOTE, LPDB_MOD_SEARCH_SELF, LPDB_LOCAL, LPDB_SEARCH_REMOTE} step;
struct ldb_module *module;
struct ldb_request *orig_req;
struct ldb_request *remote_req;
struct ldb_request *search_req;
struct ldb_request *local_req;
struct ldb_message *local_message;
bool added_objectGUID;
bool added_objectClass;
struct ldb_reply *search_res;
};
struct lpdb_local_search_context {
struct lpdb_context *ac;
struct ldb_reply *remote_res;
struct ldb_reply *local_res;
};
static struct ldb_handle *lpdb_init_handle(struct ldb_request *req, struct ldb_module *module, enum lpdb_type type)
{
struct lpdb_context *ac;
struct ldb_handle *h;
h = talloc_zero(req, struct ldb_handle);
if (h == NULL) {
ldb_set_errstring(module->ldb, "Out of Memory");
return NULL;
}
h->module = module;
ac = talloc_zero(h, struct lpdb_context);
if (ac == NULL) {
ldb_set_errstring(module->ldb, "Out of Memory");
talloc_free(h);
return NULL;
}
h->private_data = (void *)ac;
h->state = LDB_ASYNC_INIT;
h->status = LDB_SUCCESS;
ac->type = type;
ac->module = module;
ac->orig_req = req;
return h;
}
/* Add a record, splitting password attributes from the user's main
* record */
static int local_password_add(struct ldb_module *module, struct ldb_request *req)
{
struct ldb_handle *h;
struct lpdb_context *ac;
struct ldb_message *remote_message;
struct ldb_message *local_message;
struct GUID objectGUID;
int i;
ldb_debug(module->ldb, LDB_DEBUG_TRACE, "local_password_add\n");
if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
return ldb_next_request(module, req);
}
/* If the caller is manipulating the local passwords directly, let them pass */
if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
req->op.add.message->dn) == 0) {
return ldb_next_request(module, req);
}
for (i=0; i < ARRAY_SIZE(password_attrs); i++) {
if (ldb_msg_find_element(req->op.add.message, password_attrs[i])) {
break;
}
}
/* It didn't match any of our password attributes, go on */
if (i == ARRAY_SIZE(password_attrs)) {
return ldb_next_request(module, req);
}
/* TODO: remove this when sambaPassword will be in schema */
if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
ldb_asprintf_errstring(module->ldb,
"Cannot relocate a password on entry: %s, does not have objectClass 'person'",
ldb_dn_get_linearized(req->op.add.message->dn));
return LDB_ERR_OBJECT_CLASS_VIOLATION;
}
/* From here, we assume we have password attributes to split off */
h = lpdb_init_handle(req, module, LPDB_ADD);
if (!h) {
return LDB_ERR_OPERATIONS_ERROR;
}
ac = talloc_get_type(h->private_data, struct lpdb_context);
ac->orig_req = req;
ac->remote_req = talloc(ac, struct ldb_request);
if (ac->remote_req == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
*(ac->remote_req) = *(ac->orig_req);
remote_message = ldb_msg_copy_shallow(ac->remote_req, ac->orig_req->op.add.message);
if (remote_message == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
/* Remove any password attributes from the remote message */
for (i=0; i < ARRAY_SIZE(password_attrs); i++) {
ldb_msg_remove_attr(remote_message, password_attrs[i]);
}
ac->remote_req->op.add.message = remote_message;
ac->remote_req->context = NULL;
ac->remote_req->callback = NULL;
ac->local_req = talloc(ac, struct ldb_request);
if (ac->local_req == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
*(ac->local_req) = *(ac->orig_req);
local_message = ldb_msg_copy_shallow(ac->local_req, ac->orig_req->op.add.message);
if (local_message == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
/* Remove anything seen in the remote message from the local
* message (leaving only password attributes) */
for (i=0;i<ac->remote_req->op.add.message->num_elements;i++) {
ldb_msg_remove_attr(local_message, ac->remote_req->op.add.message->elements[i].name);
}
/* We must have an objectGUID already, or we don't know where
* to add the password. This may be changed to an 'add and
* search', to allow the directory to create the objectGUID */
if (ldb_msg_find_ldb_val(ac->orig_req->op.add.message, "objectGUID") == NULL) {
ldb_set_errstring(module->ldb,
"no objectGUID found in search: local_password module must be configured below objectGUID module!\n");
return LDB_ERR_CONSTRAINT_VIOLATION;
}
/* Find the objectGUID to use as the key */
objectGUID = samdb_result_guid(ac->orig_req->op.add.message, "objectGUID");
local_message->dn = ldb_dn_new(local_message, module->ldb, LOCAL_BASE);
ldb_dn_add_child_fmt(local_message->dn, PASSWORD_GUID_ATTR "=%s", GUID_string(local_message, &objectGUID));
ac->local_req->op.add.message = local_message;
ac->local_req->context = NULL;
ac->local_req->callback = NULL;
ac->step = LPDB_ADD_REMOTE;
/* Return our own handle do deal with this call */
req->handle = h;
return ldb_next_request(module, ac->remote_req);
}
/* After adding the remote entry, add the local one */
static int local_password_add_local(struct ldb_handle *h) {
struct lpdb_context *ac;
ac = talloc_get_type(h->private_data, struct lpdb_context);
h->state = LDB_ASYNC_INIT;
h->status = LDB_SUCCESS;
ac->step = LPDB_LOCAL;
ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->local_req);
/* perform the local add */
return ldb_next_request(ac->module, ac->local_req);
}
static int local_password_mod_search_self(struct ldb_handle *h);
static int local_password_modify(struct ldb_module *module, struct ldb_request *req)
{
struct ldb_handle *h;
struct lpdb_context *ac;
struct ldb_message *remote_message;
struct ldb_message *local_message;
int i;
ldb_debug(module->ldb, LDB_DEBUG_TRACE, "local_password_modify\n");
if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
return ldb_next_request(module, req);
}
/* If the caller is manipulating the local passwords directly, let them pass */
if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
req->op.mod.message->dn) == 0) {
return ldb_next_request(module, req);
}
for (i=0; i < ARRAY_SIZE(password_attrs); i++) {
if (ldb_msg_find_element(req->op.add.message, password_attrs[i])) {
break;
}
}
/* It didn't match any of our password attributes, then we have nothing to do here */
if (i == ARRAY_SIZE(password_attrs)) {
return ldb_next_request(module, req);
}
/* From here, we assume we have password attributes to split off */
h = lpdb_init_handle(req, module, LPDB_MOD);
if (!h) {
return LDB_ERR_OPERATIONS_ERROR;
}
ac = talloc_get_type(h->private_data, struct lpdb_context);
ac->orig_req = req;
ac->remote_req = talloc(ac, struct ldb_request);
if (ac->remote_req == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
*(ac->remote_req) = *(ac->orig_req);
remote_message = ldb_msg_copy_shallow(ac->remote_req, ac->orig_req->op.mod.message);
if (remote_message == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
/* Remove any password attributes from the remote message */
for (i=0; i < ARRAY_SIZE(password_attrs); i++) {
ldb_msg_remove_attr(remote_message, password_attrs[i]);
}
ac->remote_req->op.mod.message = remote_message;
ac->remote_req->context = NULL;
ac->remote_req->callback = NULL;
ac->local_req = talloc(ac, struct ldb_request);
if (ac->local_req == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
*(ac->local_req) = *(ac->orig_req);
local_message = ldb_msg_copy_shallow(ac->local_req, ac->orig_req->op.mod.message);
if (local_message == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
/* Remove anything seen in the remote message from the local
* message (leaving only password attributes) */
for (i=0;i<ac->remote_req->op.mod.message->num_elements;i++) {
ldb_msg_remove_attr(local_message, ac->remote_req->op.mod.message->elements[i].name);
}
ac->local_req->op.mod.message = local_message;
ac->local_message = local_message;
ac->local_req->context = NULL;
ac->local_req->callback = NULL;
ac->step = LPDB_MOD_REMOTE;
/* Return our own handle do deal with this call */
req->handle = h;
return ldb_next_request(module, ac->remote_req);
}
/* Called when we search for our oen entry. Stores the one entry we
* expect (as it is a base search) on the context pointer */
static int get_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
{
struct lpdb_context *ac;
ac = talloc_get_type(context, struct lpdb_context);
/* we are interested only in the single reply (base search) we receive here */
if (ares->type == LDB_REPLY_ENTRY) {
if (ac->search_res != NULL) {
ldb_set_errstring(ldb, "Too many results");
talloc_free(ares);
return LDB_ERR_OPERATIONS_ERROR;
}
ac->search_res = talloc_steal(ac, ares);
} else {
talloc_free(ares);
}
return LDB_SUCCESS;
}
/* On a modify, we don't have the objectGUID handy, so we need to
* search our DN for it */
static int local_password_mod_search_self(struct ldb_handle *h) {
struct lpdb_context *ac;
static const char * const attrs[] = { "objectGUID", "objectClass", NULL };
ac = talloc_get_type(h->private_data, struct lpdb_context);
/* prepare the search operation */
ac->search_req = talloc_zero(ac, struct ldb_request);
if (ac->search_req == NULL) {
ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Out of Memory!\n");
return LDB_ERR_OPERATIONS_ERROR;
}
ac->search_req->operation = LDB_SEARCH;
ac->search_req->op.search.base = ac->orig_req->op.mod.message->dn;
ac->search_req->op.search.scope = LDB_SCOPE_BASE;
ac->search_req->op.search.tree = ldb_parse_tree(ac->orig_req, NULL);
if (ac->search_req->op.search.tree == NULL) {
ldb_set_errstring(ac->module->ldb, "Invalid search filter");
return LDB_ERR_OPERATIONS_ERROR;
}
ac->search_req->op.search.attrs = attrs;
ac->search_req->controls = NULL;
ac->search_req->context = ac;
ac->search_req->callback = get_self_callback;
ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->search_req);
ac->step = LPDB_MOD_SEARCH_SELF;
return ldb_next_request(ac->module, ac->search_req);
}
/* After we find out the objectGUID for the entry, modify the local
* password database as required */
static int local_password_mod_local(struct ldb_handle *h) {
struct lpdb_context *ac;
struct GUID objectGUID;
ac = talloc_get_type(h->private_data, struct lpdb_context);
/* if it is not an entry of type person this is an error */
/* TODO: remove this when sambaPassword will be in schema */
if (!ac->search_res) {
ldb_asprintf_errstring(ac->module->ldb,
"entry just modified (%s) not found!",
ldb_dn_get_linearized(ac->remote_req->op.mod.message->dn));
return LDB_ERR_OPERATIONS_ERROR;
}
if (!ldb_msg_check_string_attribute(ac->search_res->message, "objectClass", "person")) {
/* Not relevent to us */
return LDB_SUCCESS;
}
if (ldb_msg_find_ldb_val(ac->search_res->message, "objectGUID") == NULL) {
ldb_set_errstring(ac->module->ldb,
"no objectGUID found in search: local_password module must be configured below objectGUID module!\n");
return LDB_ERR_OBJECT_CLASS_VIOLATION;
}
objectGUID = samdb_result_guid(ac->search_res->message, "objectGUID");
ac->local_message->dn = ldb_dn_new(ac, ac->module->ldb, LOCAL_BASE);
ldb_dn_add_child_fmt(ac->local_message->dn, PASSWORD_GUID_ATTR "=%s", GUID_string(ac, &objectGUID));
h->state = LDB_ASYNC_INIT;
h->status = LDB_SUCCESS;
ac->step = LPDB_LOCAL;
ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->local_req);
/* perform the local update */
return ldb_next_request(ac->module, ac->local_req);
}
static int lpdb_local_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
{
struct lpdb_local_search_context *local_context;
local_context = talloc_get_type(context, struct lpdb_local_search_context);
/* we are interested only in the single reply (base search) we receive here */
switch (ares->type) {
case LDB_REPLY_ENTRY:
{
int i;
if (local_context->local_res != NULL) {
ldb_set_errstring(ldb, "Too many results to base search for password entry!");
talloc_free(ares);
return LDB_ERR_OPERATIONS_ERROR;
}
local_context->local_res = ares;
/* Make sure never to return the internal key attribute to the caller */
ldb_msg_remove_attr(ares->message, PASSWORD_GUID_ATTR);
talloc_steal(local_context->remote_res->message->elements, ares->message->elements);
for (i=0; i < ares->message->num_elements; i++) {
struct ldb_message_element *el;
el = ldb_msg_find_element(local_context->remote_res->message,
ares->message->elements[i].name);
if (!el) {
if (ldb_msg_add_empty(local_context->remote_res->message,
ares->message->elements[i].name, 0, &el) != LDB_SUCCESS) {
talloc_free(ares);
return LDB_ERR_OPERATIONS_ERROR;
}
*el = ares->message->elements[i];
}
}
return local_context->ac->orig_req->callback(ldb,
local_context->ac->orig_req->context,
local_context->remote_res);
}
case LDB_REPLY_DONE:
{
/* Fire off the callback if there was no local entry, so we get the rest returned */
if (local_context->local_res == NULL) {
return local_context->ac->orig_req->callback(ldb,
local_context->ac->orig_req->context,
local_context->remote_res);
}
return LDB_SUCCESS;
break;
}
default:
{
talloc_free(ares);
ldb_set_errstring(ldb, "Unexpected result type in base search for password entry!");
return LDB_ERR_OPERATIONS_ERROR;
}
}
}
/* For each entry returned in a remote search, do a local base search,
* based on the objectGUID we asked for as an additional attribute */
static int lpdb_remote_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
{
struct lpdb_context *ac;
ac = talloc_get_type(context, struct lpdb_context);
if (ares->type == LDB_REPLY_ENTRY) {
struct ldb_request *req;
struct lpdb_local_search_context *local_context;
struct GUID objectGUID;
/* No point searching further if it's not a 'person' entry */
if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
/* Make sure to remove anything we added */
if (ac->added_objectGUID) {
ldb_msg_remove_attr(ares->message, "objectGUID");
}
if (ac->added_objectClass) {
ldb_msg_remove_attr(ares->message, "objectClass");
}
return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
}
if (ldb_msg_find_ldb_val(ares->message, "objectGUID") == NULL) {
ldb_set_errstring(ac->module->ldb,
"no objectGUID found in search: local_password module must be configured below objectGUID module!\n");
return LDB_ERR_OPERATIONS_ERROR;
}
objectGUID = samdb_result_guid(ares->message, "objectGUID");
if (ac->added_objectGUID) {
ldb_msg_remove_attr(ares->message, "objectGUID");
}
if (ac->added_objectClass) {
ldb_msg_remove_attr(ares->message, "objectClass");
}
req = talloc_zero(ac, struct ldb_request);
if (!req) {
return LDB_ERR_OPERATIONS_ERROR;
}
local_context = talloc(ac, struct lpdb_local_search_context);
if (!local_context) {
return LDB_ERR_OPERATIONS_ERROR;
}
local_context->ac = ac;
local_context->remote_res = ares;
local_context->local_res = NULL;
req->op.search.base = ldb_dn_new(ac, ac->module->ldb, LOCAL_BASE);
if ( ! ldb_dn_add_child_fmt(req->op.search.base, PASSWORD_GUID_ATTR "=%s", GUID_string(ac, &objectGUID))) {
return LDB_ERR_OPERATIONS_ERROR;
}
req->operation = LDB_SEARCH;
req->op.search.scope = LDB_SCOPE_BASE;
req->op.search.tree = ldb_parse_tree(req, NULL);
if (req->op.search.tree == NULL) {
ldb_set_errstring(ac->module->ldb, "Out of Memory");
return LDB_ERR_OPERATIONS_ERROR;
}
req->op.search.attrs = ac->orig_req->op.search.attrs;
req->controls = NULL;
req->context = ac;
req->callback = get_self_callback;
ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, req);
req->context = local_context;
req->callback = lpdb_local_search_callback;
return ldb_next_request(ac->module, req);
} else {
return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
}
}
/* Search for passwords and other attributes. The passwords are
* local, but the other attributes are remote, and we need to glue the
* two search spaces back togeather */
static int local_password_search(struct ldb_module *module, struct ldb_request *req)
{
struct ldb_handle *h;
struct lpdb_context *ac;
int i;
int ret;
const char * const *search_attrs = NULL;
ldb_debug(module->ldb, LDB_DEBUG_TRACE, "local_password_search\n");
if (ldb_dn_is_special(req->op.search.base)) { /* do not manipulate our control entries */
return ldb_next_request(module, req);
}
/* If the caller is searching for the local passwords directly, let them pass */
if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
req->op.search.base) == 0) {
return ldb_next_request(module, req);
}
if (req->op.search.attrs && (!ldb_attr_in_list(req->op.search.attrs, "*"))) {
for (i=0; i < ARRAY_SIZE(password_attrs); i++) {
if (ldb_attr_in_list(req->op.search.attrs, password_attrs[i])) {
break;
}
}
/* It didn't match any of our password attributes, go on */
if (i == ARRAY_SIZE(password_attrs)) {
return ldb_next_request(module, req);
}
}
h = lpdb_init_handle(req, module, LPDB_SEARCH);
if (!h) {
return LDB_ERR_OPERATIONS_ERROR;
}
ac = talloc_get_type(h->private_data, struct lpdb_context);
ac->orig_req = req;
ac->remote_req = talloc(ac, struct ldb_request);
if (ac->remote_req == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
/* Remote search is for all attributes: if the remote LDAP server has these attributes, then it overrides the local database */
*(ac->remote_req) = *(ac->orig_req);
/* Return our own handle do deal with this call */
ac->remote_req->handle = h;
ac->remote_req->context = ac;
ac->remote_req->callback = lpdb_remote_search_callback;
if (req->op.search.attrs && !ldb_attr_in_list(req->op.search.attrs, "*")) {
if (!ldb_attr_in_list(req->op.search.attrs, "objectGUID")) {
search_attrs = ldb_attr_list_copy_add(req, req->op.search.attrs, "objectGUID");
ac->added_objectGUID = true;
if (!search_attrs) {
return LDB_ERR_OPERATIONS_ERROR;
}
} else {
search_attrs = req->op.search.attrs;
}
if (!ldb_attr_in_list(search_attrs, "objectClass")) {
search_attrs = ldb_attr_list_copy_add(req, search_attrs, "objectClass");
ac->added_objectClass = true;
if (!search_attrs) {
return LDB_ERR_OPERATIONS_ERROR;
}
}
} else {
search_attrs = req->op.search.attrs;
}
ac->remote_req->op.search.attrs = search_attrs;
ldb_set_timeout_from_prev_req(module->ldb, ac->orig_req, ac->remote_req);
h->state = LDB_ASYNC_INIT;
h->status = LDB_SUCCESS;
ac->step = LPDB_SEARCH_REMOTE;
/* perform the search */
ret = ldb_next_request(module, ac->remote_req);
if (ret == LDB_SUCCESS) {
req->handle = ac->remote_req->handle;
}
return ret;
}
static int lpdb_wait(struct ldb_handle *handle) {
struct lpdb_context *ac;
int ret;
if (!handle || !handle->private_data) {
return LDB_ERR_OPERATIONS_ERROR;
}
if (handle->state == LDB_ASYNC_DONE) {
return handle->status;
}
handle->state = LDB_ASYNC_PENDING;
handle->status = LDB_SUCCESS;
ac = talloc_get_type(handle->private_data, struct lpdb_context);
switch (ac->step) {
case LPDB_ADD_REMOTE:
ret = ldb_wait(ac->remote_req->handle, LDB_WAIT_NONE);
if (ret != LDB_SUCCESS) {
handle->status = ret;
goto done;
}
if (ac->remote_req->handle->status != LDB_SUCCESS) {
handle->status = ac->remote_req->handle->status;
goto done;
}
if (ac->remote_req->handle->state != LDB_ASYNC_DONE) {
return LDB_SUCCESS;
}
/* original request done, go on */
return local_password_add_local(handle);
case LPDB_MOD_REMOTE:
ret = ldb_wait(ac->remote_req->handle, LDB_WAIT_NONE);
if (ret != LDB_SUCCESS) {
handle->status = ret;
goto done;
}
if (ac->remote_req->handle->status != LDB_SUCCESS) {
handle->status = ac->remote_req->handle->status;
goto done;
}
if (ac->remote_req->handle->state != LDB_ASYNC_DONE) {
return LDB_SUCCESS;
}
/* original request done, go on */
return local_password_mod_search_self(handle);
case LPDB_MOD_SEARCH_SELF:
ret = ldb_wait(ac->search_req->handle, LDB_WAIT_NONE);
if (ret != LDB_SUCCESS) {
handle->status = ret;
goto done;
}
if (ac->search_req->handle->status != LDB_SUCCESS) {
handle->status = ac->search_req->handle->status;
goto done;
}
if (ac->search_req->handle->state != LDB_ASYNC_DONE) {
return LDB_SUCCESS;
}
/* original request done, go on */
return local_password_mod_local(handle);
case LPDB_LOCAL:
ret = ldb_wait(ac->local_req->handle, LDB_WAIT_NONE);
if (ret != LDB_SUCCESS) {
handle->status = ret;
goto done;
}
if (ac->local_req->handle->status != LDB_SUCCESS) {
handle->status = ac->local_req->handle->status;
goto done;
}
if (ac->local_req->handle->state != LDB_ASYNC_DONE) {
return LDB_SUCCESS;
}
break;
case LPDB_SEARCH_REMOTE:
ret = ldb_wait(ac->remote_req->handle, LDB_WAIT_NONE);
if (ret != LDB_SUCCESS) {
handle->status = ret;
goto done;
}
if (ac->remote_req->handle->status != LDB_SUCCESS) {
handle->status = ac->remote_req->handle->status;
goto done;
}
if (ac->remote_req->handle->state != LDB_ASYNC_DONE) {
return LDB_SUCCESS;
}
break;
default:
ret = LDB_ERR_OPERATIONS_ERROR;
goto done;
}
ret = LDB_SUCCESS;
done:
handle->state = LDB_ASYNC_DONE;
return ret;
}
static int lpdb_wait_all(struct ldb_handle *handle) {
int ret;
while (handle->state != LDB_ASYNC_DONE) {
ret = lpdb_wait(handle);
if (ret != LDB_SUCCESS) {
return ret;
}
}
return handle->status;
}
static int local_password_wait(struct ldb_handle *handle, enum ldb_wait_type type)
{
if (type == LDB_WAIT_ALL) {
return lpdb_wait_all(handle);
} else {
return lpdb_wait(handle);
}
}
static const struct ldb_module_ops local_password_ops = {
.name = "local_password",
.add = local_password_add,
.modify = local_password_modify,
.search = local_password_search,
.wait = local_password_wait
};
int local_password_module_init(void)
{
return ldb_register_module(&local_password_ops);
}
|