summaryrefslogtreecommitdiffstats
path: root/source4/scripting/libjs/provision.js
blob: dc9eae8e7286ddf1de24fe8de2dfa5af8c40c12e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/*
	backend code for provisioning a Samba4 server
	Copyright Andrew Tridgell 2005
	Released under the GNU GPL v2 or later
*/

sys = sys_init();

/*
  return true if the current install seems to be OK
*/
function install_ok(session_info, credentials)
{
	var lp = loadparm_init();
	var ldb = ldb_init();
	ldb.session_info = session_info;
	ldb.credentials = credentials;
	if (lp.get("realm") == "") {
		return false;
	}
	var ok = ldb.connect(lp.get("sam database"));
	if (!ok) {
		return false;
	}
	var res = ldb.search("(cn=Administrator)");
	if (res.error != 0 || res.msgs.length != 1) {
		return false;
	}
	return true;
}

/*
  find a user or group from a list of possibilities
*/
function findnss()
{
	var i;
	assert(arguments.length >= 2);
	var nssfn = arguments[0];
	for (i=1;i<arguments.length;i++) {
		if (nssfn(arguments[i]) != undefined) {
			return arguments[i];
		}
	}
	printf("Unable to find user/group for %s\n", arguments[1]);
	assert(i<arguments.length);
}

/*
   add a foreign security principle
 */
function add_foreign(ldb, subobj, sid, desc)
{
	var add = sprintf("
dn: CN=%s,CN=ForeignSecurityPrincipals,%s
objectClass: top
objectClass: foreignSecurityPrincipal
description: %s
",
			  sid, subobj.DOMAINDN, desc);
	/* deliberately ignore errors from this, as the records may
	   already exist */
	ldb.add(add);
}


/*
  setup a mapping between a sam name and a unix name
 */
function setup_name_mapping(info, ldb, sid, unixname)
{
	var attrs = new Array("dn");
	var res = ldb.search(sprintf("objectSid=%s", sid), 
			     info.subobj.DOMAINDN, ldb.SCOPE_SUBTREE, attrs);
	if (res.error != 0 || res.msgs.length != 1) {
		info.message("Failed to find record for objectSid %s\n", sid);
		return false;
	}
	var mod = sprintf("
dn: %s
changetype: modify
replace: unixName
unixName: %s
",
			  res.msgs[0].dn, unixname);
	var ok = ldb.modify(mod);
	if (ok.error != 0) {
		info.message("name mapping for %s failed - %s\n",
			     sid, ldb.errstring());
		return false;
	}
	return true;
}

/*
  return current time as a nt time string
*/
function nttime()
{
	return "" + sys.nttime();
}

/*
  return current time as a ldap time string
*/
function ldaptime()
{
	return sys.ldaptime(sys.nttime());
}

/*
  return a date string suitable for a dns zone serial number
*/
function datestring()
{
	var t = sys.ntgmtime(sys.nttime());
	return sprintf("%04u%02u%02u%02u",
		       t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour);
}

/*
  return first host IP
*/
function hostip()
{
	var list = sys.interfaces();
	return list[0];
}


/*
  return first part of hostname
*/
function hostname()
{
	var s = split(".", sys.hostname());
	return s[0];
}

/* the ldb is in bad shape, possibly due to being built from an
   incompatible previous version of the code, so delete it
   completely */
function ldb_delete(info, ldb)
{
	info.message("Deleting " + ldb.filename + "\n");
	var lp = loadparm_init();
	sys.unlink(sprintf("%s/%s", lp.get("private dir"), ldb.filename));
	ldb.transaction_cancel();
	ldb.close();
	var ok = ldb.connect(ldb.filename);
	ldb.transaction_start();
	assert(ok);
}

/*
  erase an ldb, removing all records
*/
function ldb_erase(info, ldb)
{
	var res;

	/* delete the specials */
	ldb.del("@INDEXLIST");
	ldb.del("@ATTRIBUTES");
	ldb.del("@OPTIONS");
	ldb.del("@MODULES");
	ldb.del("@PARTITION");
	ldb.del("@KLUDGEACL");

	/* and the rest */
	attrs = new Array("dn");
     	var basedn = "";
     	var res = ldb.search("(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
	var i;
	if (res.error != 0) {
		ldb_delete(info, ldb);
		return;
	}
	for (i=0;i<res.msgs.length;i++) {
		ldb.del(res.msgs[i].dn);
	}

     	var res = ldb.search("(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
	if (res.error != 0 || res.msgs.length != 0) {
		ldb_delete(info, ldb);
		return;
	}
	assert(res.msgs.length == 0);
}

/*
  erase an ldb, removing all records
*/
function ldb_erase_partitions(info, ldb, ldapbackend)
{
	var rootDSE_attrs = new Array("namingContexts");
	var lp = loadparm_init();
	var j;

	var res = ldb.search("(objectClass=*)", "", ldb.SCOPE_BASE, rootDSE_attrs);
	if (res.error != 0) {
		info.message("rootdse search failed: " + res.errstr + "\n");
		assert(res.error == 0);
	}
	assert(res.msgs.length == 1);
	if (typeof(res.msgs[0].namingContexts) == "undefined") {
		return;
	}	
	for (j=0; j<res.msgs[0].namingContexts.length; j++) {
		var anything = "(|(objectclass=*)(distinguishedName=*))";
		var attrs = new Array("distinguishedName");
		var basedn = res.msgs[0].namingContexts[j];
		var k;
		var previous_remaining = 1;
		var current_remaining = 0;

		if (ldapbackend && (basedn == info.subobj.DOMAINDN)) {
			/* Only delete objects that were created by provision */
			anything = "(objectcategory=*)";
		}

		for (k=0; k < 10 && (previous_remaining != current_remaining); k++) {
			/* and the rest */
			var res2 = ldb.search(anything, basedn, ldb.SCOPE_SUBTREE, attrs);
			var i;
			if (res2.error != 0) {
				if (res2.error == 32) {
					break;
				} else {
					info.message("ldb search(2) failed: " + res2.errstr + "\n");
					continue;
				}
			}
			previous_remaining = current_remaining;
			current_remaining = res2.msgs.length;
			for (i=0;i<res2.msgs.length;i++) {
				ldb.del(res2.msgs[i].dn);
			}
			
			var res3 = ldb.search(anything, basedn, ldb.SCOPE_SUBTREE, attrs);
			if (res3.error != 0) {
				info.message("ldb search(3) failed: " + res3.errstr + "\n");
				continue;
			}
			if (res3.msgs.length != 0) {
				info.message("Failed to delete all records under " + basedn + ", " + res3.msgs.length + " records remaining\n");
			}
		}
	}
}

function open_ldb(info, dbname, erase)
{
	var ldb = ldb_init();
	ldb.session_info = info.session_info;
	ldb.credentials = info.credentials;
	ldb.filename = dbname;

	var connect_ok = ldb.connect(dbname);
	if (!connect_ok) {
		var lp = loadparm_init();
		sys.unlink(sprintf("%s/%s", lp.get("private dir"), dbname));
		connect_ok = ldb.connect(dbname);
		assert(connect_ok);
	}

	ldb.transaction_start();

	if (erase) {
		ldb_erase(info, ldb);	
	}
	return ldb;
}


/*
  setup a ldb in the private dir
 */
function setup_add_ldif(ldif, info, ldb, failok)
{
	var lp = loadparm_init();
	var src = lp.get("setup directory") + "/" + ldif;

	var data = sys.file_load(src);
	data = substitute_var(data, info.subobj);

	var add_res = ldb.add(data);
	if (add_res.error != 0) {
		info.message("ldb load failed: " + add_res.errstr + "\n");
		if (!failok) {
			assert(add_res.error == 0);
	        }
	}
	return (add_res.error == 0);
}

function setup_modify_ldif(ldif, info, ldb, failok)
{
	var lp = loadparm_init();
	var src = lp.get("setup directory") + "/" + ldif;

	var data = sys.file_load(src);
	data = substitute_var(data, info.subobj);

	var mod_res = ldb.modify(data);
	if (mod_res.error != 0) {
		info.message("ldb load failed: " + mod_res.errstr + "\n");
		if (!failok) {
			assert(mod_res.error == 0);
	        }
	}
	return (mod_res.error == 0);
}


function setup_ldb(ldif, info, dbname) 
{
	var erase = true;
	var failok = false;

	if (arguments.length >= 4) {
	        erase = arguments[3];
        }
	if (arguments.length == 5) {
	        failok = arguments[4];
        }
	var ldb = open_ldb(info, dbname, erase);
	if (setup_add_ldif(ldif, info, ldb, failok)) {
		var commit_ok = ldb.transaction_commit();
		if (!commit_ok) {
			info.message("ldb commit failed: " + ldb.errstring() + "\n");
			assert(commit_ok);
		}
	}
}

/*
  setup a ldb in the private dir
 */
function setup_ldb_modify(ldif, info, ldb)
{
	var lp = loadparm_init();

	var src = lp.get("setup directory") + "/" + ldif;

	var data = sys.file_load(src);
	data = substitute_var(data, info.subobj);

	var mod_res = ldb.modify(data);
	if (mod_res.error != 0) {
		info.message("ldb load failed: " + mod_res.errstr + "\n");
		return (mod_res.error == 0);
	}
	return (mod_res.error == 0);
}

/*
  setup a file in the private dir
 */
function setup_file(template, message, fname, subobj)
{
	var lp = loadparm_init();
	var f = fname;
	var src = lp.get("setup directory") + "/" + template;

	if (! sys.stat(src)) {
		message("Template file not found: %s\n",src);
		assert(0);
	}

	sys.unlink(f);

	var data = sys.file_load(src);
	data = substitute_var(data, subobj);

	ok = sys.file_save(f, data);
	if (!ok) {
		message("failed to create file: " + f + "\n");
		assert(ok);
	}
}

function provision_default_paths(subobj)
{
	/* subobj.DNSDOMAIN isn't available at this point */
	var dnsdomain = strlower(subobj.REALM);
	var lp = loadparm_init();
	var paths = new Object();
	paths.smbconf = lp.filename()
	paths.shareconf = lp.get("private dir") + "/" + "share.ldb";
	paths.samdb = lp.get("sam database");
	paths.secrets = lp.get("secrets database");
	paths.templates = lp.get("private dir") + "/" + "templates.ldb";
	paths.keytab = "secrets.keytab";
	paths.dns_keytab = "dns.keytab";
	paths.dns_keytab_abs = lp.get("private dir") + "/" + paths.dns_keytab;
	paths.dns = lp.get("private dir") + "/" + dnsdomain + ".zone";
	paths.named_conf = lp.get("private dir") + "/named.conf";
	paths.winsdb = "wins.ldb";
	paths.ldapdir = lp.get("private dir") + "/ldap";

	paths.s4_ldapi_socket = lp.get("private dir") + "/ldapi";
	paths.phpldapadminconfig = lp.get("private dir") + "/phpldapadmin-config.php";

	paths.sysvol = lp.get("sysvol", "path");

	if (paths.sysvol == undefined) {
		paths.sysvol = lp.get("lock dir") + "/sysvol";
	}
	
	paths.netlogon = lp.get("netlogon", "path");
	
	if (paths.netlogon == undefined) {
		paths.netlogon = paths.sysvol + "/" + dnsdomain + "/scripts";
	}

	return paths;
}


/*
  setup reasonable name mappings for sam names to unix names
*/
function setup_name_mappings(info, ldb)
{
	var lp = loadparm_init();
	var attrs = new Array("objectSid");
	var subobj = info.subobj;

	res = ldb.search("objectSid=*", subobj.DOMAINDN, ldb.SCOPE_BASE, attrs);
	assert(res.error == 0);
	assert(res.msgs.length == 1 && res.msgs[0].objectSid != undefined);
	var sid = res.msgs[0].objectSid;

	/* add some foreign sids if they are not present already */
	add_foreign(ldb, subobj, "S-1-5-7",  "Anonymous");
	add_foreign(ldb, subobj, "S-1-1-0",  "World");
	add_foreign(ldb, subobj, "S-1-5-2",  "Network");
	add_foreign(ldb, subobj, "S-1-5-18", "System");
	add_foreign(ldb, subobj, "S-1-5-11", "Authenticated Users");

	/* some well known sids */
	setup_name_mapping(info, ldb, "S-1-5-7",  subobj.NOBODY);
	setup_name_mapping(info, ldb, "S-1-1-0",  subobj.NOGROUP);
	setup_name_mapping(info, ldb, "S-1-5-2",  subobj.NOGROUP);
	setup_name_mapping(info, ldb, "S-1-5-18", subobj.ROOT);
	setup_name_mapping(info, ldb, "S-1-5-11", subobj.USERS);
	setup_name_mapping(info, ldb, "S-1-5-32-544", subobj.WHEEL);
	setup_name_mapping(info, ldb, "S-1-5-32-545", subobj.USERS);
	setup_name_mapping(info, ldb, "S-1-5-32-546", subobj.NOGROUP);
	setup_name_mapping(info, ldb, "S-1-5-32-551", subobj.BACKUP);

	/* and some well known domain rids */
	setup_name_mapping(info, ldb, sid + "-500", subobj.ROOT);
	setup_name_mapping(info, ldb, sid + "-518", subobj.WHEEL);
	setup_name_mapping(info, ldb, sid + "-519", subobj.WHEEL);
	setup_name_mapping(info, ldb, sid + "-512", subobj.WHEEL);
	setup_name_mapping(info, ldb, sid + "-513", subobj.USERS);
	setup_name_mapping(info, ldb, sid + "-520", subobj.WHEEL);

	return true;
}

function provision_fix_subobj(subobj, paths)
{
	var ldb = ldb_init();
	
	subobj.REALM       = strupper(subobj.REALM);
	subobj.HOSTNAME    = strlower(subobj.HOSTNAME);
	subobj.DOMAIN      = strupper(subobj.DOMAIN);
	subobj.NETBIOSNAME = strupper(subobj.HOSTNAME);
	subobj.DNSDOMAIN    = strlower(subobj.REALM);
	subobj.DNSNAME      = sprintf("%s.%s", 
				      strlower(subobj.HOSTNAME), 
				      subobj.DNSDOMAIN);
	var rdn_list = split(".", subobj.DNSDOMAIN);
	subobj.DOMAINDN     = "DC=" + join(",DC=", rdn_list);
	subobj.ROOTDN       = subobj.DOMAINDN;
	subobj.CONFIGDN     = "CN=Configuration," + subobj.ROOTDN;
	subobj.SCHEMADN     = "CN=Schema," + subobj.CONFIGDN;

	subobj.MACHINEPASS_B64 = ldb.encode(subobj.MACHINEPASS);
	subobj.KRBTGTPASS_B64  = ldb.encode(subobj.KRBTGTPASS);
	subobj.ADMINPASS_B64   = ldb.encode(subobj.ADMINPASS);
	subobj.DNSPASS_B64     = ldb.encode(subobj.DNSPASS);

	subobj.SAM_LDB		= "tdb://" + paths.samdb;
	subobj.SECRETS_KEYTAB	= paths.keytab;
	subobj.DNS_KEYTAB	= paths.dns_keytab;
	subobj.DNS_KEYTAB_ABS	= paths.dns_keytab_abs;

	subobj.LDAPDIR = paths.ldapdir;
	var ldap_path_list = split("/", paths.ldapdir);
	subobj.LDAPI_URI = "ldapi://" + join("%2F", ldap_path_list) + "%2Fldapi";

	var s4ldap_path_list = split("/", paths.s4_ldapi_socket);
	subobj.S4_LDAPI_URI = "ldapi://" + join("%2F", s4ldap_path_list);

	subobj.LDAPMANAGERDN = "cn=Manager," + subobj.DOMAINDN;

	subobj.NETLOGONPATH = paths.netlogon;
	subobj.SYSVOLPATH = paths.sysvol;

	if (subobj.DOMAIN_CONF == undefined) {
		subobj.DOMAIN_CONF = subobj.DOMAIN;
	}
	if (subobj.REALM_CONF == undefined) {
		subobj.REALM_CONF = subobj.REALM;
	}
	if (strlower(subobj.SERVERROLE) != strlower("domain controller")) {
		subobj.REALM = subobj.HOSTNAME;
		subobj.DOMAIN = subobj.HOSTNAME;
	}

	return true;
}

function provision_become_dc(subobj, message, erase, paths, session_info)
{
	var lp = loadparm_init();
	var sys = sys_init();
	var info = new Object();

	var ok = provision_fix_subobj(subobj, paths);
	assert(ok);

	if (subobj.BACKEND_MOD == undefined) {
		subobj.BACKEND_MOD = "repl_meta_data";
	}

	info.subobj = subobj;
	info.message = message;
	info.session_info = session_info;

	message("Setting up templates into " + paths.templates + "\n");
	setup_ldb("provision_templates.ldif", info, paths.templates);

	/* Also wipes the database */
	message("Setting up " + paths.samdb + " partitions\n");
	setup_ldb("provision_partitions.ldif", info, paths.samdb);

	var samdb = open_ldb(info, paths.samdb, false);

	message("Setting up " + paths.samdb + " attributes\n");
	setup_add_ldif("provision_init.ldif", info, samdb, false);

	message("Setting up " + paths.samdb + " rootDSE\n");
	setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false);

	if (erase) {
		message("Erasing data from partitions\n");
		ldb_erase_partitions(info, samdb, undefined);
	}

	message("Setting up " + paths.samdb + " indexes\n");
	setup_add_ldif("provision_index.ldif", info, samdb, false);

	ok = samdb.transaction_commit();
	assert(ok);

	message("Setting up " + paths.secrets + "\n");
	setup_ldb("secrets_init.ldif", info, paths.secrets);

	setup_ldb("secrets.ldif", info, paths.secrets, false);

	setup_ldb("secrets_dc.ldif", info, paths.secrets, false);

	return true;
}

function load_schema(subobj, message, samdb)
{
	var lp = loadparm_init();
	var src = lp.get("setup directory") + "/" + "schema.ldif";

	if (! sys.stat(src)) {
		message("Template file not found: %s\n",src);
		assert(0);
	}

	var schema_data = sys.file_load(src);

	src = lp.get("setup directory") + "/" + "schema_samba4.ldif";

	if (! sys.stat(src)) {
		message("Template file not found: %s\n",src);
		assert(0);
	}

	schema_data = schema_data + sys.file_load(src);

	schema_data = substitute_var(schema_data, subobj);

	src = lp.get("setup directory") + "/" + "provision_schema_basedn_modify.ldif";

	if (! sys.stat(src)) {
		message("Template file not found: %s\n",src);
		assert(0);
	}

	var head_data = sys.file_load(src);
	head_data = substitute_var(head_data, subobj);

	var ok = samdb.attach_dsdb_schema_from_ldif(head_data, schema_data);
	return ok;
}


/*
  provision samba4 - caution, this wipes all existing data!
*/
function provision(subobj, message, blank, paths, session_info, credentials, ldapbackend)
{
	var lp = loadparm_init();
	var sys = sys_init();
	var info = new Object();
	random_init(local);

	var ok = provision_fix_subobj(subobj, paths);
	assert(ok);

	if (strlower(subobj.SERVERROLE) == strlower("domain controller")) {
		if (subobj.BACKEND_MOD == undefined) {
			subobj.BACKEND_MOD = "repl_meta_data";
		}
	} else {
		if (subobj.BACKEND_MOD == undefined) {
			subobj.BACKEND_MOD = "objectguid";
		}
	}

	if (subobj.DOMAINGUID != undefined) {
		subobj.DOMAINGUID_MOD = sprintf("replace: objectGUID\nobjectGUID: %s\n-", subobj.DOMAINGUID);
	} else {
		subobj.DOMAINGUID_MOD = "";
	}

	if (subobj.HOSTGUID != undefined) {
		subobj.HOSTGUID_ADD = sprintf("objectGUID: %s", subobj.HOSTGUID);
	} else {
		subobj.HOSTGUID_ADD = "";
	}

	info.subobj = subobj;
	info.message = message;
	info.credentials = credentials;
	info.session_info = session_info;

	/* only install a new smb.conf if there isn't one there already */
	var st = sys.stat(paths.smbconf);
	if (st == undefined) {
		var smbconfsuffix;
		if (strlower(subobj.SERVERROLE) == strlower("domain controller")) {
			smbconfsuffix = "dc";
		} else if (strlower(subobj.SERVERROLE) == strlower("member server")) {
			smbconfsuffix = "member";
		} else {
			smbconfsuffix = subobj.SERVERROLE;
		}
		message("Setting up " + paths.smbconf +"\n");
		setup_file("provision.smb.conf." + smbconfsuffix, info.message, paths.smbconf, subobj);
		lp.reload();
	}
	/* only install a new shares config db if there is none */
	st = sys.stat(paths.shareconf);
	if (st == undefined) {
		message("Setting up share.ldb\n");
		setup_ldb("share.ldif", info, paths.shareconf);
	}

	message("Setting up " + paths.secrets + "\n");
	setup_ldb("secrets_init.ldif", info, paths.secrets);
	setup_ldb("secrets.ldif", info, paths.secrets, false);

	message("Setting up the registry\n");
	var reg = reg_open();
	reg.apply_patchfile(lp.get("setup directory") + "/provision.reg")

	message("Setting up templates into " + paths.templates + "\n");
	setup_ldb("provision_templates.ldif", info, paths.templates);

	message("Setting up sam.ldb partitions\n");
	/* Also wipes the database */
	setup_ldb("provision_partitions.ldif", info, paths.samdb);

	var samdb = open_ldb(info, paths.samdb, false);

	message("Setting up sam.ldb attributes\n");
	setup_add_ldif("provision_init.ldif", info, samdb, false);

	message("Setting up sam.ldb rootDSE\n");
	setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false);

	message("Erasing data from partitions\n");
	ldb_erase_partitions(info, samdb, ldapbackend);
	
	// (hack) Reload, now we have the partitions and rootdse loaded.  
	var commit_ok = samdb.transaction_commit();
	if (!commit_ok) {
		info.message("samdb commit failed: " + samdb.errstring() + "\n");
		assert(commit_ok);
	}
	samdb.close();

	message("Pre-loading the Samba4 and AD schema\n");
	
	samdb = open_ldb(info, paths.samdb, false);

	samdb.set_domain_sid(subobj.DOMAINSID);

	if (strlower(subobj.SERVERROLE) == strlower("domain controller")) {
		if (subobj.INVOCATIONID == undefined) {
			subobj.INVOCATIONID = randguid();
		}
		samdb.set_ntds_invocationId(subobj.INVOCATIONID);
		if (subobj.BACKEND_MOD == undefined) {
			subobj.BACKEND_MOD = "repl_meta_data";
		}
	} else {
		if (subobj.BACKEND_MOD == undefined) {
			subobj.BACKEND_MOD = "objectguid";
		}
	}

	var load_schema_ok = load_schema(subobj, message, samdb);
	assert(load_schema_ok.is_ok);

	message("Adding DomainDN: " + subobj.DOMAINDN + " (permitted to fail)\n");
	var add_ok = setup_add_ldif("provision_basedn.ldif", info, samdb, true);
	message("Modifying DomainDN: " + subobj.DOMAINDN + "\n");
	var modify_basedn_ok = setup_ldb_modify("provision_basedn_modify.ldif", info, samdb);
	if (!modify_basedn_ok) {
		if (!add_ok) {
			message("%s", "Failed to both add and modify " + subobj.DOMAINDN + " in target " + subobj.DOMAINDN_LDB + ": " + samdb.errstring() + "\n");
			message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
		};
		assert(modify_basedn_ok);
	};

	message("Adding configuration container (permitted to fail)\n");
	var add_config_ok = setup_add_ldif("provision_configuration_basedn.ldif", info, samdb, true);
	message("Modifying configuration container\n");
	var modify_config_ok = setup_ldb_modify("provision_configuration_basedn_modify.ldif", info, samdb);
	if (!modify_config_ok) {
		if (!add_config_ok) {
			message("%s", "Failed to both add and modify " + subobj.CONFIGDN + " in target " + subobj.CONFIGDN_LDB + ": " + samdb.errstring() + "\n");
			message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
		}
		assert(modify_config_ok);
	}

	message("Adding schema container (permitted to fail)\n");
	var add_schema_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true);
	message("Modifying schema container\n");
	var modify_schema_ok = setup_ldb_modify("provision_schema_basedn_modify.ldif", info, samdb);
	if (!modify_schema_ok) {
		if (!add_schema_ok) {
			message("%s", "Failed to both add and modify " + subobj.SCHEMADN + " in target " + subobj.SCHEMADN_LDB + ": " + samdb.errstring() + "\n");
			message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
		}
		message("Failed to modify the schema container: " + samdb.errstring() + "\n");
		assert(modify_schema_ok);
	}

	message("Setting up sam.ldb Samba4 schema\n");
	setup_add_ldif("schema_samba4.ldif", info, samdb, false);
	message("Setting up sam.ldb AD schema\n");
	setup_add_ldif("schema.ldif", info, samdb, false);

	message("Setting up sam.ldb configuration data\n");
	setup_add_ldif("provision_configuration.ldif", info, samdb, false);

	message("Setting up display specifiers\n");
	setup_add_ldif("display_specifiers.ldif", info, samdb, false);

	message("Adding users container (permitted to fail)\n");
	var add_users_ok = setup_add_ldif("provision_users_add.ldif", info, samdb, true);
	message("Modifying users container\n");
	var modify_users_ok = setup_ldb_modify("provision_users_modify.ldif", info, samdb);
	if (!modify_users_ok) {
		if (!add_users_ok) {
			message("Failed to both add and modify the users container\n");
		}
		assert(modify_users_ok);
	}
	message("Adding computers container (permitted to fail)\n");
	var add_computers_ok = setup_add_ldif("provision_computers_add.ldif", info, samdb, true);
	message("Modifying computers container\n");
	var modify_computers_ok = setup_ldb_modify("provision_computers_modify.ldif", info, samdb);
	if (!modify_computers_ok) {
		if (!add_computers_ok) {
			message("Failed to both add and modify the computers container\n");
		}
		assert(modify_computers_ok);
	}

	message("Setting up sam.ldb data\n");
	setup_add_ldif("provision.ldif", info, samdb, false);

	if (blank != false) {
		message("Setting up sam.ldb index\n");
		setup_add_ldif("provision_index.ldif", info, samdb, false);

		message("Setting up sam.ldb rootDSE marking as syncronized\n");
		setup_modify_ldif("provision_rootdse_modify.ldif", info, samdb, false);

		var commit_ok = samdb.transaction_commit();
		if (!commit_ok) {
			info.message("ldb commit failed: " + samdb.errstring() + "\n");
			assert(commit_ok);
		}
		return true;
	}

//	message("Activate schema module");
//	setup_modify_ldif("schema_activation.ldif", info, samdb, false);
//
//	// (hack) Reload, now we have the schema loaded.  
//	var commit_ok = samdb.transaction_commit();
//	if (!commit_ok) {
//		info.message("samdb commit failed: " + samdb.errstring() + "\n");
//		assert(commit_ok);
//	}
//	samdb.close();
//
//	samdb = open_ldb(info, paths.samdb, false);
//
	message("Setting up sam.ldb users and groups\n");
	setup_add_ldif("provision_users.ldif", info, samdb, false);

	if (strlower(subobj.SERVERROLE) == strlower("domain controller")) {
		message("Setting up self join\n");
		setup_add_ldif("provision_self_join.ldif", info, samdb, false);
		setup_add_ldif("provision_group_policy.ldif", info, samdb, false);

		sys.mkdir(paths.sysvol, 0755);
		sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN, 0755);
		sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies", 0755);
		sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies/{" + subobj.POLICYGUID + "}", 0755);
		sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies/{" + subobj.POLICYGUID + "}/Machine", 0755);
		sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies/{" + subobj.POLICYGUID + "}/User", 0755);

		sys.mkdir(paths.netlogon, 0755);

		setup_ldb("secrets_dc.ldif", info, paths.secrets, false);

	}

	if (setup_name_mappings(info, samdb) == false) {
		return false;
	}

	message("Setting up sam.ldb index\n");
	setup_add_ldif("provision_index.ldif", info, samdb, false);

	message("Setting up sam.ldb rootDSE marking as syncronized\n");
	setup_modify_ldif("provision_rootdse_modify.ldif", info, samdb, false);

	var commit_ok = samdb.transaction_commit();
	if (!commit_ok) {
		info.message("samdb commit failed: " + samdb.errstring() + "\n");
		assert(commit_ok);
	}

	message("Setting up phpLDAPadmin configuration\n");
	setup_file("phpldapadmin-config.php", info.message, paths.phpldapadminconfig, subobj);
	message("Please install the phpLDAPadmin configuration located at " + paths.phpldapadminconfig + " into /etc/phpldapadmin/config.php\n");

	return true;
}

/*
  provision just the schema into a temporary ldb, so we can run ad2oLschema on it
*/
function provision_schema(subobj, message, tmp_schema_path, paths)
{
	var lp = loadparm_init();
	var sys = sys_init();
	var info = new Object();

	var ok = provision_fix_subobj(subobj, paths);
	assert(ok);

	info.subobj = subobj;
	info.message = message;

	message("Setting up sam.ldb partitions\n");

	/* This will erase anything in the tmp db */
	var samdb = open_ldb(info, tmp_schema_path, true);

	message("Setting up sam.ldb attributes\n");
	setup_add_ldif("provision_init.ldif", info, samdb, false);

	message("Setting up sam.ldb rootDSE\n");
	setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false);

	message("Adding schema container (permitted to fail)\n");
	var add_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true);
	message("Modifying schema container\n");
	var modify_ok = setup_ldb_modify("provision_schema_basedn_modify.ldif", info, samdb);
	if (!modify_ok) {
		if (!add_ok) {
			message("Failed to both add and modify schema dn: " + samdb.errstring() + "\n");
			message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
			assert(modify_ok);
		}
		message("Failed to modify the schema container: " + samdb.errstring() + "\n");
		assert(modify_ok);
	}

	message("Setting up sam.ldb Samba4 schema\n");
	setup_add_ldif("schema_samba4.ldif", info, samdb, false);
	message("Setting up sam.ldb AD schema\n");
	setup_add_ldif("schema.ldif", info, samdb, false);

	var commit_ok = samdb.transaction_commit();
	if (!commit_ok) {
		info.message("samdb commit failed: " + samdb.errstring() + "\n");
		assert(commit_ok);
	}
	samdb.close();
}

/* Write out a DNS zone file, from the info in the current database */
function provision_dns(subobj, message, paths, session_info, credentials)
{
	var lp = loadparm_init();
	if (strlower(subobj.SERVERROLE) != strlower("domain controller")) {
		message("No DNS zone required for role %s\n", subobj.SERVERROLE);
		return;
	}
	message("Setting up DNS zone: " + subobj.DNSDOMAIN + " \n");
	var ldb = ldb_init();
	ldb.session_info = session_info;
	ldb.credentials = credentials;

	/* connect to the sam */
	var ok = ldb.connect(paths.samdb);
	assert(ok);

	/* These values may have changed, due to an incoming SamSync,
           or may not have been specified, so fetch them from the database */

	var attrs = new Array("objectGUID");
	res = ldb.search("objectGUID=*", subobj.DOMAINDN, ldb.SCOPE_BASE, attrs);
	assert(res.error == 0);
	assert(res.msgs.length == 1);
	assert(res.msgs[0].objectGUID != undefined);
	subobj.DOMAINGUID = res.msgs[0].objectGUID;

	subobj.HOSTGUID = searchone(ldb, subobj.DOMAINDN, "(&(objectClass=computer)(cn=" + subobj.NETBIOSNAME + "))", "objectGUID");
	assert(subobj.HOSTGUID != undefined);

	setup_file("provision.zone", 
		   message, paths.dns, 
		   subobj);

	setup_file("named.conf", 
		   message, paths.named_conf, 
		   subobj);

	message("Please install the zone located in " + paths.dns + " into your DNS server.  A sample BIND configuration snippit is at " + paths.named_conf + "\n");
}


/*
  guess reasonably default options for provisioning
*/
function provision_guess()
{
	var subobj = new Object();
	var nss = nss_init();
	var lp = loadparm_init();
	var rdn_list;
	random_init(local);

	subobj.SERVERROLE   = strlower(lp.get("server role"));
	subobj.REALM        = strupper(lp.get("realm"));
	subobj.DOMAIN       = lp.get("workgroup");
	subobj.HOSTNAME     = hostname();

	assert(subobj.REALM);
	assert(subobj.DOMAIN);
	assert(subobj.HOSTNAME);
	
	subobj.VERSION      = version();
	subobj.HOSTIP       = hostip();
	subobj.DOMAINSID    = randsid();
	subobj.POLICYGUID   = randguid();
	subobj.KRBTGTPASS   = randpass(12);
	subobj.MACHINEPASS  = randpass(12);
	subobj.DNSPASS  = randpass(12);
	subobj.ADMINPASS    = randpass(12);
	subobj.LDAPMANAGERPASS     = randpass(12);
	subobj.DEFAULTSITE  = "Default-First-Site-Name";
	subobj.DATESTRING   = datestring;
	subobj.ROOT         = findnss(nss.getpwnam, "root");
	subobj.NOBODY       = findnss(nss.getpwnam, "nobody");
	subobj.NOGROUP      = findnss(nss.getgrnam, "nogroup", "nobody");
	subobj.WHEEL        = findnss(nss.getgrnam, "wheel", "root", "staff", "adm");
	subobj.BACKUP       = findnss(nss.getgrnam, "backup", "wheel", "root", "staff");
	subobj.USERS        = findnss(nss.getgrnam, "users", "guest", "other", "unknown", "usr");

	//Add modules to the list to activate them by default
	//beware often order is important
	//
	// Some Known ordering constraints:
	// - rootdse must be first, as it makes redirects from "" -> cn=rootdse
	// - objectclass must be before password_hash, because password_hash checks
	//   that the objectclass is of type person (filled in by the objectclass 
	//   module when expanding the objectclass list)
	// - partition must be last
	// - each partition has its own module list then
	var modules_list     = new Array("rootdse",
					 "paged_results",
					 "ranged_results",
					 "anr",
					 "server_sort",
					 "extended_dn",
					 "asq",
					 "samldb",
					 "rdn_name",
					 "objectclass",
					 "kludge_acl",
					 "operational");
	var tdb_modules_list = new Array("subtree_rename",
					 "subtree_delete",
					 "linked_attributes");
	var modules_list2    = new Array("show_deleted",
					 "partition");
	subobj.MODULES_LIST = join(",", modules_list);
	subobj.TDB_MODULES_LIST = "," + join(",", tdb_modules_list);
	subobj.MODULES_LIST2 = join(",", modules_list2);
	subobj.DOMAINDN_LDB = "users.ldb";
	subobj.CONFIGDN_LDB = "configuration.ldb";
	subobj.SCHEMADN_LDB = "schema.ldb";
	subobj.DOMAINDN_MOD = "pdc_fsmo,password_hash,instancetype";
	subobj.CONFIGDN_MOD = "naming_fsmo,instancetype";
	subobj.SCHEMADN_MOD = "schema_fsmo,instancetype";

	subobj.ACI		= "# no aci for local ldb";

	return subobj;
}

/*
  search for one attribute as a string
 */
function searchone(ldb, basedn, expression, attribute)
{
	var attrs = new Array(attribute);
	res = ldb.search(expression, basedn, ldb.SCOPE_SUBTREE, attrs);
	if (res.error != 0 ||
	    res.msgs.length != 1 ||
	    res.msgs[0][attribute] == undefined) {
		return undefined;
	}
	return res.msgs[0][attribute];
}

/*
  modify an account to remove the 
*/
function enable_account(ldb, user_dn)
{
	var attrs = new Array("userAccountControl");
	var res = ldb.search(NULL, user_dn, ldb.SCOPE_ONELEVEL, attrs);
	assert(res.error == 0);
	assert(res.msgs.length == 1);
	var userAccountControl = res.msgs[0].userAccountControl;
	userAccountControl = userAccountControl - 2; /* remove disabled bit */
	var mod = sprintf("
dn: %s
changetype: modify
replace: userAccountControl
userAccountControl: %u
", 
			  user_dn, userAccountControl);
	var ok = ldb.modify(mod);
	return (ok.error == 0);	
}


/*
  add a new user record
*/
function newuser(username, unixname, password, message, session_info, credentials)
{
	var lp = loadparm_init();
	var samdb = lp.get("sam database");
	var ldb = ldb_init();
	random_init(local);
	ldb.session_info = session_info;
	ldb.credentials = credentials;

	/* connect to the sam */
	var ok = ldb.connect(samdb);
	assert(ok);

	ldb.transaction_start();

	/* find the DNs for the domain and the domain users group */
	var attrs = new Array("defaultNamingContext");
	res = ldb.search("defaultNamingContext=*", "", ldb.SCOPE_BASE, attrs);
	assert(res.error == 0);
	assert(res.msgs.length == 1 && res.msgs[0].defaultNamingContext != undefined);
	var domain_dn = res.msgs[0].defaultNamingContext;
	assert(domain_dn != undefined);
	var dom_users = searchone(ldb, domain_dn, "name=Domain Users", "dn");
	assert(dom_users != undefined);

	var user_dn = sprintf("CN=%s,CN=Users,%s", username, domain_dn);


	/*
	  the new user record. note the reliance on the samdb module to fill
	  in a sid, guid etc
	*/
	var ldif = sprintf("
dn: %s
sAMAccountName: %s
unixName: %s
sambaPassword: %s
objectClass: user
",
			   user_dn, username,
			   unixname, password);
	/*
	  add the user to the users group as well
	*/
	var modgroup = sprintf("
dn: %s
changetype: modify
add: member
member: %s
",
			       dom_users, user_dn);


	/*
	  now the real work
	*/
	message("Adding user %s\n", user_dn);
	ok = ldb.add(ldif);
	if (ok.error != 0) {
		message("Failed to add %s - %s\n", user_dn, ok.errstr);
		return false;
	}

	message("Modifying group %s\n", dom_users);
	ok = ldb.modify(modgroup);
	if (ok.error != 0) {
		message("Failed to modify %s - %s\n", dom_users, ok.errstr);
		return false;
	}

	/*
	  modify the userAccountControl to remove the disabled bit
	*/
	ok = enable_account(ldb, user_dn);
	if (ok) {
		ldb.transaction_commit();
	}
	return ok;
}

// Check whether a name is valid as a NetBIOS name. 
// FIXME: There are probably more constraints here. 
// crh has a paragraph on this in his book (1.4.1.1)
function valid_netbios_name(name)
{
	if (strlen(name) > 13) return false;
	return true;
}

function provision_validate(subobj, message)
{
	var lp = loadparm_init();

	if (!valid_netbios_name(subobj.DOMAIN)) {
		message("Invalid NetBIOS name for domain\n");
		return false;
	}

	if (!valid_netbios_name(subobj.NETBIOSNAME)) {
		message("Invalid NetBIOS name for host\n");
		return false;
	}


	if (strupper(lp.get("workgroup")) != strupper(subobj.DOMAIN_CONF)) {
		message("workgroup '%s' in smb.conf must match chosen domain '%s'\n",
			lp.get("workgroup"), subobj.DOMAIN_CONF);
		return false;
	}

	if (strupper(lp.get("realm")) != strupper(subobj.REALM_CONF)) {
		message("realm '%s' in smb.conf must match chosen realm '%s'\n",
			lp.get("realm"), subobj.REALM_CONF);
		return false;
	}

	if (strlower(lp.get("server role")) != strlower(subobj.SERVERROLE)) {
		message("server role '%s' in smb.conf must match chosen role '%s'\n",
			lp.get("server role"), subobj.SERVERROLE);
		return false;
	}

	return true;
}

function join_domain(domain, netbios_name, join_type, creds, message) 
{
	var ctx = NetContext(creds);
	var joindom = new Object();
	joindom.domain = domain;
	joindom.join_type = join_type;
	joindom.netbios_name = netbios_name;
	if (!ctx.JoinDomain(joindom)) {
		message("Domain Join failed: " + joindom.error_string);
		return false;
	}
	return true;
}

/* Vampire a remote domain.  Session info and credentials are required for for
 * access to our local database (might be remote ldap)
 */ 

function vampire(domain, session_info, credentials, message) {
	var ctx = NetContext(credentials);
	var vampire_ctx = new Object();
	var machine_creds = credentials_init();
	machine_creds.set_domain(form.DOMAIN);
	if (!machine_creds.set_machine_account()) {
		message("Failed to access domain join information!");
		return false;
	}
	vampire_ctx.machine_creds = machine_creds;
	vampire_ctx.session_info = session_info;
	if (!ctx.SamSyncLdb(vampire_ctx)) {
		message("Migration of remote domain to Samba failed: " + vampire_ctx.error_string);
		return false;
	}

	return true;
}

return 0;