forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdb_ldap.c
6859 lines (5751 loc) · 181 KB
/
pdb_ldap.c
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Unix SMB/CIFS implementation.
LDAP protocol helper functions for SAMBA
Copyright (C) Jean François Micouleau 1998
Copyright (C) Gerald Carter 2001-2003
Copyright (C) Shahms King 2001
Copyright (C) Andrew Bartlett 2002-2003
Copyright (C) Stefan (metze) Metzmacher 2002-2003
Copyright (C) Simo Sorce 2006
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/>.
*/
/* TODO:
* persistent connections: if using NSS LDAP, many connections are made
* however, using only one within Samba would be nice
*
* Clean up SSL stuff, compile on OpenLDAP 1.x, 2.x, and Netscape SDK
*
* Other LDAP based login attributes: accountExpires, etc.
* (should be the domain of Samba proper, but the sam_password/struct samu
* structures don't have fields for some of these attributes)
*
* SSL is done, but can't get the certificate based authentication to work
* against on my test platform (Linux 2.4, OpenLDAP 2.x)
*/
/* NOTE: this will NOT work against an Active Directory server
* due to the fact that the two password fields cannot be retrieved
* from a server; recommend using security = domain in this situation
* and/or winbind
*/
#include "includes.h"
#include "passdb.h"
#include "../libcli/auth/libcli_auth.h"
#include "secrets.h"
#include "idmap_cache.h"
#include "../libcli/security/security.h"
#include "../lib/util/util_pw.h"
#include "lib/winbind_util.h"
#include "librpc/gen_ndr/idmap.h"
#include "lib/param/loadparm.h"
#include "lib/util_sid_passdb.h"
#include "lib/util/smb_strtox.h"
#include "lib/util/string_wrappers.h"
#include "source3/lib/substitute.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_PASSDB
#include <lber.h>
#include <ldap.h>
#include "smbldap.h"
#include "passdb/pdb_ldap.h"
#include "passdb/pdb_nds.h"
#include "passdb/pdb_ldap_util.h"
#include "passdb/pdb_ldap_schema.h"
/**********************************************************************
Simple helper function to make stuff better readable
**********************************************************************/
LDAP *priv2ld(struct ldapsam_privates *priv)
{
return smbldap_get_ldap(priv->smbldap_state);
}
/**********************************************************************
Get the attribute name given a user schame version.
**********************************************************************/
static const char* get_userattr_key2string( int schema_ver, int key )
{
switch ( schema_ver ) {
case SCHEMAVER_SAMBASAMACCOUNT:
return get_attr_key2string( attrib_map_v30, key );
default:
DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
break;
}
return NULL;
}
/**********************************************************************
Return the list of attribute names given a user schema version.
**********************************************************************/
const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
{
switch ( schema_ver ) {
case SCHEMAVER_SAMBASAMACCOUNT:
return get_attr_list( mem_ctx, attrib_map_v30 );
default:
DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
break;
}
return NULL;
}
/**************************************************************************
Return the list of attribute names to delete given a user schema version.
**************************************************************************/
static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
int schema_ver )
{
switch ( schema_ver ) {
case SCHEMAVER_SAMBASAMACCOUNT:
return get_attr_list( mem_ctx,
attrib_map_to_delete_v30 );
default:
DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
break;
}
return NULL;
}
/*******************************************************************
Generate the LDAP search filter for the objectclass based on the
version of the schema we are using.
******************************************************************/
static const char* get_objclass_filter( int schema_ver )
{
fstring objclass_filter;
char *result;
switch( schema_ver ) {
case SCHEMAVER_SAMBASAMACCOUNT:
fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
break;
default:
DEBUG(0,("get_objclass_filter: Invalid schema version specified!\n"));
objclass_filter[0] = '\0';
break;
}
result = talloc_strdup(talloc_tos(), objclass_filter);
SMB_ASSERT(result != NULL);
return result;
}
/*****************************************************************
Scan a sequence number off OpenLDAP's syncrepl contextCSN
******************************************************************/
static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_num)
{
struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
LDAPMessage *msg = NULL;
LDAPMessage *entry = NULL;
TALLOC_CTX *mem_ctx;
char **values = NULL;
int rc, num_result, num_values, rid;
char *suffix = NULL;
char *tok;
const char *p;
const char **attrs;
/* Unfortunatly there is no proper way to detect syncrepl-support in
* smbldap_connect_system(). The syncrepl OIDs are submitted for publication
* but do not show up in the root-DSE yet. Neither we can query the
* subschema-context for the syncProviderSubentry or syncConsumerSubentry
* objectclass. Currently we require lp_ldap_suffix() to show up as
* namingContext. - Guenther
*/
if (!lp_parm_bool(-1, "ldapsam", "syncrepl_seqnum", False)) {
return ntstatus;
}
if (!seq_num) {
DEBUG(3,("ldapsam_get_seq_num: no sequence_number\n"));
return ntstatus;
}
if (!smbldap_has_naming_context(
smbldap_get_ldap(ldap_state->smbldap_state),
lp_ldap_suffix())) {
DEBUG(3,("ldapsam_get_seq_num: DIT not configured to hold %s "
"as top-level namingContext\n", lp_ldap_suffix()));
return ntstatus;
}
mem_ctx = talloc_init("ldapsam_get_seq_num");
if (mem_ctx == NULL)
return NT_STATUS_NO_MEMORY;
if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
ntstatus = NT_STATUS_NO_MEMORY;
goto done;
}
/* if we got a syncrepl-rid (up to three digits long) we speak with a consumer */
rid = lp_parm_int(-1, "ldapsam", "syncrepl_rid", -1);
if (rid > 0) {
/* consumer syncreplCookie: */
/* csn=20050126161620Z#0000001#00#00000 */
attrs[0] = talloc_strdup(mem_ctx, "syncreplCookie");
attrs[1] = NULL;
suffix = talloc_asprintf(mem_ctx,
"cn=syncrepl%d,%s", rid, lp_ldap_suffix());
if (!suffix) {
ntstatus = NT_STATUS_NO_MEMORY;
goto done;
}
} else {
/* provider contextCSN */
/* 20050126161620Z#000009#00#000000 */
attrs[0] = talloc_strdup(mem_ctx, "contextCSN");
attrs[1] = NULL;
suffix = talloc_asprintf(mem_ctx,
"cn=ldapsync,%s", lp_ldap_suffix());
if (!suffix) {
ntstatus = NT_STATUS_NO_MEMORY;
goto done;
}
}
rc = smbldap_search(ldap_state->smbldap_state, suffix,
LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0, &msg);
if (rc != LDAP_SUCCESS) {
goto done;
}
num_result = ldap_count_entries(
smbldap_get_ldap(ldap_state->smbldap_state), msg);
if (num_result != 1) {
DEBUG(3,("ldapsam_get_seq_num: Expected one entry, got %d\n", num_result));
goto done;
}
entry = ldap_first_entry(
smbldap_get_ldap(ldap_state->smbldap_state), msg);
if (entry == NULL) {
DEBUG(3,("ldapsam_get_seq_num: Could not retrieve entry\n"));
goto done;
}
values = ldap_get_values(
smbldap_get_ldap(ldap_state->smbldap_state), entry, attrs[0]);
if (values == NULL) {
DEBUG(3,("ldapsam_get_seq_num: no values\n"));
goto done;
}
num_values = ldap_count_values(values);
if (num_values == 0) {
DEBUG(3,("ldapsam_get_seq_num: not a single value\n"));
goto done;
}
p = values[0];
if (!next_token_talloc(mem_ctx, &p, &tok, "#")) {
DEBUG(0,("ldapsam_get_seq_num: failed to parse sequence number\n"));
goto done;
}
p = tok;
if (!strncmp(p, "csn=", strlen("csn=")))
p += strlen("csn=");
DEBUG(10,("ldapsam_get_seq_num: got %s: %s\n", attrs[0], p));
*seq_num = generalized_to_unix_time(p);
/* very basic sanity check */
if (*seq_num <= 0) {
DEBUG(3,("ldapsam_get_seq_num: invalid sequence number: %d\n",
(int)*seq_num));
goto done;
}
ntstatus = NT_STATUS_OK;
done:
if (values != NULL)
ldap_value_free(values);
if (msg != NULL)
ldap_msgfree(msg);
if (mem_ctx)
talloc_destroy(mem_ctx);
return ntstatus;
}
/*******************************************************************
Run the search by name.
******************************************************************/
int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
const char *user,
LDAPMessage ** result,
const char **attr)
{
char *filter = NULL;
char *escape_user = escape_ldap_string(talloc_tos(), user);
int ret = -1;
if (!escape_user) {
return LDAP_NO_MEMORY;
}
/*
* in the filter expression, replace %u with the real name
* so in ldap filter, %u MUST exist :-)
*/
filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
get_objclass_filter(ldap_state->schema_ver));
if (!filter) {
TALLOC_FREE(escape_user);
return LDAP_NO_MEMORY;
}
/*
* have to use this here because $ is filtered out
* in string_sub
*/
filter = talloc_all_string_sub(talloc_tos(),
filter, "%u", escape_user);
TALLOC_FREE(escape_user);
if (!filter) {
return LDAP_NO_MEMORY;
}
ret = smbldap_search_suffix(ldap_state->smbldap_state,
filter, attr, result);
TALLOC_FREE(filter);
return ret;
}
/*******************************************************************
Run the search by SID.
******************************************************************/
static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
const struct dom_sid *sid, LDAPMessage ** result,
const char **attr)
{
char *filter = NULL;
int rc;
struct dom_sid_buf sid_string;
filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_USER_SID),
dom_sid_str_buf(sid, &sid_string),
get_objclass_filter(ldap_state->schema_ver));
if (!filter) {
return LDAP_NO_MEMORY;
}
rc = smbldap_search_suffix(ldap_state->smbldap_state,
filter, attr, result);
TALLOC_FREE(filter);
return rc;
}
/*******************************************************************
Delete complete object or objectclass and attrs from
object found in search_result depending on lp_ldap_delete_dn
******************************************************************/
static int ldapsam_delete_entry(struct ldapsam_privates *priv,
TALLOC_CTX *mem_ctx,
LDAPMessage *entry,
const char *objectclass,
const char **attrs)
{
LDAPMod **mods = NULL;
char *name;
const char *dn;
BerElement *ptr = NULL;
dn = smbldap_talloc_dn(mem_ctx, priv2ld(priv), entry);
if (dn == NULL) {
return LDAP_NO_MEMORY;
}
if (lp_ldap_delete_dn()) {
return smbldap_delete(priv->smbldap_state, dn);
}
/* Ok, delete only the SAM attributes */
for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
name != NULL;
name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
const char **attrib;
/* We are only allowed to delete the attributes that
really exist. */
for (attrib = attrs; *attrib != NULL; attrib++) {
if (strequal(*attrib, name)) {
DEBUG(10, ("ldapsam_delete_entry: deleting "
"attribute %s\n", name));
smbldap_set_mod(&mods, LDAP_MOD_DELETE, name,
NULL);
}
}
ldap_memfree(name);
}
if (ptr != NULL) {
ber_free(ptr, 0);
}
smbldap_set_mod(&mods, LDAP_MOD_DELETE, "objectClass", objectclass);
smbldap_talloc_autofree_ldapmod(mem_ctx, mods);
return smbldap_modify(priv->smbldap_state, dn, mods);
}
static time_t ldapsam_get_entry_timestamp( struct ldapsam_privates *ldap_state, LDAPMessage * entry)
{
char *temp;
struct tm tm;
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state), entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_MOD_TIMESTAMP),
talloc_tos());
if (!temp) {
return (time_t) 0;
}
if ( !strptime(temp, "%Y%m%d%H%M%SZ", &tm)) {
DEBUG(2,("ldapsam_get_entry_timestamp: strptime failed on: %s\n",
(char*)temp));
TALLOC_FREE(temp);
return (time_t) 0;
}
TALLOC_FREE(temp);
tzset();
return timegm(&tm);
}
/**********************************************************************
Initialize struct samu from an LDAP query.
(Based on init_sam_from_buffer in pdb_tdb.c)
*********************************************************************/
static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
struct samu * sampass,
LDAPMessage * entry)
{
time_t logon_time,
logoff_time,
kickoff_time,
pass_last_set_time,
pass_can_change_time,
ldap_entry_time,
bad_password_time;
char *username = NULL,
*domain = NULL,
*nt_username = NULL,
*fullname = NULL,
*homedir = NULL,
*dir_drive = NULL,
*logon_script = NULL,
*profile_path = NULL,
*acct_desc = NULL,
*workstations = NULL,
*munged_dial = NULL;
uint32_t user_rid;
uint8_t smblmpwd[LM_HASH_LEN],
smbntpwd[NT_HASH_LEN];
bool use_samba_attrs = True;
uint16_t logon_divs;
uint16_t bad_password_count = 0,
logon_count = 0;
uint32_t hours_len;
uint8_t hours[MAX_HOURS_LEN];
char *temp = NULL;
struct login_cache cache_entry;
uint32_t pwHistLen;
bool expand_explicit = lp_passdb_expand_explicit();
bool ret = false;
TALLOC_CTX *ctx = talloc_init("init_sam_from_ldap");
if (!ctx) {
return false;
}
if (sampass == NULL || ldap_state == NULL || entry == NULL) {
DEBUG(0, ("init_sam_from_ldap: NULL parameters found!\n"));
goto fn_exit;
}
if (priv2ld(ldap_state) == NULL) {
DEBUG(0, ("init_sam_from_ldap: ldap_state->smbldap_state->"
"ldap_struct is NULL!\n"));
goto fn_exit;
}
if (!(username = smbldap_talloc_first_attribute(priv2ld(ldap_state),
entry,
"uid",
ctx))) {
DEBUG(1, ("init_sam_from_ldap: No uid attribute found for "
"this user!\n"));
goto fn_exit;
}
DEBUG(2, ("init_sam_from_ldap: Entry found for user: %s\n", username));
nt_username = talloc_strdup(ctx, username);
if (!nt_username) {
goto fn_exit;
}
domain = talloc_strdup(ctx, ldap_state->domain_name);
if (!domain) {
goto fn_exit;
}
pdb_set_username(sampass, username, PDB_SET);
pdb_set_domain(sampass, domain, PDB_DEFAULT);
pdb_set_nt_username(sampass, nt_username, PDB_SET);
/* deal with different attributes between the schema first */
if ( ldap_state->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ) {
if ((temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_USER_SID),
ctx))!=NULL) {
pdb_set_user_sid_from_string(sampass, temp, PDB_SET);
}
} else {
if ((temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_USER_RID),
ctx))!=NULL) {
user_rid = (uint32_t)atol(temp);
pdb_set_user_sid_from_rid(sampass, user_rid, PDB_SET);
}
}
if (IS_SAM_DEFAULT(sampass, PDB_USERSID)) {
DEBUG(1, ("init_sam_from_ldap: no %s or %s attribute found for this user %s\n",
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_USER_SID),
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_USER_RID),
username));
return False;
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_PWD_LAST_SET),
ctx);
if (temp) {
pass_last_set_time = (time_t) atol(temp);
pdb_set_pass_last_set_time(sampass,
pass_last_set_time, PDB_SET);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_LOGON_TIME),
ctx);
if (temp) {
logon_time = (time_t) atol(temp);
pdb_set_logon_time(sampass, logon_time, PDB_SET);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_LOGOFF_TIME),
ctx);
if (temp) {
logoff_time = (time_t) atol(temp);
pdb_set_logoff_time(sampass, logoff_time, PDB_SET);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_KICKOFF_TIME),
ctx);
if (temp) {
kickoff_time = (time_t) atol(temp);
pdb_set_kickoff_time(sampass, kickoff_time, PDB_SET);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_PWD_CAN_CHANGE),
ctx);
if (temp) {
pass_can_change_time = (time_t) atol(temp);
pdb_set_pass_can_change_time(sampass,
pass_can_change_time, PDB_SET);
}
/* recommend that 'gecos' and 'displayName' should refer to the same
* attribute OID. userFullName depreciated, only used by Samba
* primary rules of LDAP: don't make a new attribute when one is already defined
* that fits your needs; using cn then displayName rather than 'userFullName'
*/
fullname = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_DISPLAY_NAME),
ctx);
if (fullname) {
pdb_set_fullname(sampass, fullname, PDB_SET);
} else {
fullname = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_CN),
ctx);
if (fullname) {
pdb_set_fullname(sampass, fullname, PDB_SET);
}
}
dir_drive = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_HOME_DRIVE),
ctx);
if (dir_drive) {
pdb_set_dir_drive(sampass, dir_drive, PDB_SET);
} else {
pdb_set_dir_drive( sampass, lp_logon_drive(), PDB_DEFAULT );
}
homedir = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_HOME_PATH),
ctx);
if (homedir) {
if (expand_explicit) {
homedir = talloc_sub_basic(ctx,
username,
domain,
homedir);
if (!homedir) {
goto fn_exit;
}
}
pdb_set_homedir(sampass, homedir, PDB_SET);
} else {
pdb_set_homedir(sampass,
talloc_sub_basic(ctx, username, domain,
lp_logon_home()),
PDB_DEFAULT);
}
logon_script = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_LOGON_SCRIPT),
ctx);
if (logon_script) {
if (expand_explicit) {
logon_script = talloc_sub_basic(ctx,
username,
domain,
logon_script);
if (!logon_script) {
goto fn_exit;
}
}
pdb_set_logon_script(sampass, logon_script, PDB_SET);
} else {
pdb_set_logon_script(sampass,
talloc_sub_basic(ctx, username, domain,
lp_logon_script()),
PDB_DEFAULT );
}
profile_path = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_PROFILE_PATH),
ctx);
if (profile_path) {
if (expand_explicit) {
profile_path = talloc_sub_basic(ctx,
username,
domain,
profile_path);
if (!profile_path) {
goto fn_exit;
}
}
pdb_set_profile_path(sampass, profile_path, PDB_SET);
} else {
pdb_set_profile_path(sampass,
talloc_sub_basic(ctx, username, domain,
lp_logon_path()),
PDB_DEFAULT );
}
acct_desc = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_DESC),
ctx);
if (acct_desc) {
pdb_set_acct_desc(sampass, acct_desc, PDB_SET);
}
workstations = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_USER_WKS),
ctx);
if (workstations) {
pdb_set_workstations(sampass, workstations, PDB_SET);
}
munged_dial = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_MUNGED_DIAL),
ctx);
if (munged_dial) {
pdb_set_munged_dial(sampass, munged_dial, PDB_SET);
}
/* FIXME: hours stuff should be cleaner */
logon_divs = 168;
hours_len = 21;
memset(hours, 0xff, hours_len);
if (ldap_state->is_nds_ldap) {
char *user_dn;
size_t pwd_len;
char clear_text_pw[512];
/* Make call to Novell eDirectory ldap extension to get clear text password.
NOTE: This will only work if we have an SSL connection to eDirectory. */
user_dn = smbldap_talloc_dn(
ctx, smbldap_get_ldap(ldap_state->smbldap_state),
entry);
if (user_dn != NULL) {
DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
pwd_len = sizeof(clear_text_pw);
if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
TALLOC_FREE(user_dn);
return False;
}
ZERO_STRUCT(smblmpwd);
if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
TALLOC_FREE(user_dn);
return False;
}
ZERO_STRUCT(smbntpwd);
use_samba_attrs = False;
}
TALLOC_FREE(user_dn);
} else {
DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
}
}
if (use_samba_attrs) {
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_LMPW),
ctx);
if (temp) {
pdb_gethexpwd(temp, smblmpwd);
memset((char *)temp, '\0', strlen(temp)+1);
if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
goto fn_exit;
}
ZERO_STRUCT(smblmpwd);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_NTPW),
ctx);
if (temp) {
pdb_gethexpwd(temp, smbntpwd);
memset((char *)temp, '\0', strlen(temp)+1);
if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
goto fn_exit;
}
ZERO_STRUCT(smbntpwd);
}
}
pwHistLen = 0;
pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
if (pwHistLen > 0){
uint8_t *pwhist = NULL;
int i;
char *history_string = talloc_array(ctx, char,
MAX_PW_HISTORY_LEN*64);
if (!history_string) {
goto fn_exit;
}
pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
pwhist = talloc_zero_array(ctx, uint8_t,
pwHistLen * PW_HISTORY_ENTRY_LEN);
if (pwhist == NULL) {
DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
goto fn_exit;
}
if (smbldap_get_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_PWD_HISTORY),
history_string,
MAX_PW_HISTORY_LEN*64)) {
bool hex_failed = false;
for (i = 0; i < pwHistLen; i++){
/* Get the 16 byte salt. */
if (!pdb_gethexpwd(&history_string[i*64],
&pwhist[i*PW_HISTORY_ENTRY_LEN])) {
hex_failed = true;
break;
}
/* Get the 16 byte MD5 hash of salt+passwd. */
if (!pdb_gethexpwd(&history_string[(i*64)+32],
&pwhist[(i*PW_HISTORY_ENTRY_LEN)+
PW_HISTORY_SALT_LEN])) {
hex_failed = True;
break;
}
}
if (hex_failed) {
DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
username));
memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
}
}
if (!pdb_set_pw_history(sampass, pwhist, pwHistLen, PDB_SET)){
goto fn_exit;
}
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_ACB_INFO),
ctx);
if (temp) {
uint32_t acct_ctrl = 0;
acct_ctrl = pdb_decode_acct_ctrl(temp);
if (acct_ctrl == 0) {
acct_ctrl |= ACB_NORMAL;
}
pdb_set_acct_ctrl(sampass, acct_ctrl, PDB_SET);
}
pdb_set_hours_len(sampass, hours_len, PDB_SET);
pdb_set_logon_divs(sampass, logon_divs, PDB_SET);
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_BAD_PASSWORD_COUNT),
ctx);
if (temp) {
bad_password_count = (uint32_t) atol(temp);
pdb_set_bad_password_count(sampass,
bad_password_count, PDB_SET);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_BAD_PASSWORD_TIME),
ctx);
if (temp) {
bad_password_time = (time_t) atol(temp);
pdb_set_bad_password_time(sampass, bad_password_time, PDB_SET);
}
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_LOGON_COUNT),
ctx);
if (temp) {
logon_count = (uint32_t) atol(temp);
pdb_set_logon_count(sampass, logon_count, PDB_SET);
}
/* pdb_set_unknown_6(sampass, unknown6, PDB_SET); */
temp = smbldap_talloc_single_attribute(
smbldap_get_ldap(ldap_state->smbldap_state),
entry,
get_userattr_key2string(ldap_state->schema_ver,
LDAP_ATTR_LOGON_HOURS),
ctx);
if (temp) {
pdb_gethexhours(temp, hours);
memset((char *)temp, '\0', strlen(temp) +1);
pdb_set_hours(sampass, hours, hours_len, PDB_SET);
ZERO_STRUCT(hours);
}
if (lp_parm_bool(-1, "ldapsam", "trusted", False)) {
struct passwd unix_pw;
bool have_uid = false;
bool have_gid = false;
struct dom_sid mapped_gsid;
const struct dom_sid *primary_gsid;
struct unixid id;
int error = 0;
ZERO_STRUCT(unix_pw);
unix_pw.pw_name = username;
unix_pw.pw_passwd = discard_const_p(char, "x");
temp = smbldap_talloc_single_attribute(
priv2ld(ldap_state),
entry,
"uidNumber",
ctx);
if (temp) {
/* We've got a uid, feed the cache */
unix_pw.pw_uid = smb_strtoul(temp,