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
|
/*
backend code for provisioning a Samba4 server
Copyright Andrew Tridgell 2005
Released under the GNU GPL v2 or later
*/
/* used to generate sequence numbers for records */
provision_next_usn = 1;
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.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
uSNCreated: 1
uSNChanged: 1
",
sid, subobj.BASEDN, 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),
NULL, ldb.SCOPE_DEFAULT, attrs);
if (res.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[0].dn, unixname);
var ok = ldb.modify(mod);
if (!ok) {
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.gmtime(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 next USN in the sequence
*/
function nextusn()
{
provision_next_usn = provision_next_usn+1;
return provision_next_usn;
}
/*
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(ldb)
{
println("Deleting " + ldb.filename);
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(ldb)
{
var attrs = new Array("dn");
/* delete the specials */
ldb.del("@INDEXLIST");
ldb.del("@ATTRIBUTES");
ldb.del("@SUBCLASSES");
ldb.del("@MODULES");
/* and the rest */
var res = ldb.search("(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", attrs);
var i;
if (typeof(res) == "undefined") {
ldb_delete(ldb);
return;
}
for (i=0;i<res.length;i++) {
ldb.del(res[i].dn);
}
/* extra hack to ensure it's gone on remote ldap */
ldb.del("cn=ROOTDSE");
var res = ldb.search("(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", attrs);
if (res.length != 0) {
ldb_delete(ldb);
return;
}
assert(res.length == 0);
}
/*
setup a ldb in the private dir
*/
function setup_ldb(ldif, info, dbname)
{
var erase = true;
var extra = "";
var ldb = ldb_init();
var lp = loadparm_init();
ldb.session_info = info.session_info;
ldb.credentials = info.credentials;
if (arguments.length >= 4) {
extra = arguments[3];
}
if (arguments.length == 5) {
erase = arguments[4];
}
var src = lp.get("setup directory") + "/" + ldif;
var data = sys.file_load(src);
data = data + extra;
data = substitute_var(data, info.subobj);
ldb.filename = dbname;
var connect_ok = ldb.connect(dbname);
assert(connect_ok);
ldb.transaction_start();
if (erase) {
ldb_erase(ldb);
}
var add_ok = ldb.add(data);
if (!add_ok) {
info.message("ldb load failed: " + ldb.errstring() + "\n");
assert(add_ok);
}
var commit_ok = ldb.transaction_commit();
if (!commit_ok) {
info.message("ldb commit failed: " + ldb.errstring() + "\n");
assert(add_ok);
}
}
/*
setup a file in the private dir
*/
function setup_file(template, fname, subobj)
{
var lp = loadparm_init();
var f = fname;
var src = lp.get("setup directory") + "/" + template;
sys.unlink(f);
var data = sys.file_load(src);
data = substitute_var(data, subobj);
ok = sys.file_save(f, data);
assert(ok);
}
function provision_default_paths(subobj)
{
var lp = loadparm_init();
var paths = new Object();
paths.smbconf = lp.get("config file");
paths.hklm = "hklm.ldb";
paths.hkcu = "hkcu.ldb";
paths.hkcr = "hkcr.ldb";
paths.hku = "hku.ldb";
paths.hkpd = "hkpd.ldb";
paths.hkpt = "hkpt.ldb";
paths.samdb = lp.get("sam database");
paths.secrets = "secrets.ldb";
paths.dns = lp.get("private dir") + "/" + subobj.DNSDOMAIN + ".zone";
paths.winsdb = "wins.ldb";
return paths;
}
/*
setup reasonable name mappings for sam names to unix names
*/
function setup_name_mappings(info, subobj, session_info, credentials)
{
var lp = loadparm_init();
var ldb = ldb_init();
ldb.session_info = session_info;
ldb.credentials = credentials;
var ok = ldb.connect(lp.get("sam database"));
if (!ok) {
return false;
}
var attrs = new Array("objectSid");
var res = ldb.search("dnsDomain=" + subobj.REALM,
NULL, ldb.SCOPE_DEFAULT, attrs);
if (res.length != 1) {
info.message("Failed to find dnsDomain %s\n", subobj.REALM);
return false;
}
var sid = res[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-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;
}
/*
provision samba4 - caution, this wipes all existing data!
*/
function provision(subobj, message, blank, paths, session_info, credentials)
{
var data = "";
var lp = loadparm_init();
var sys = sys_init();
var info = new Object();
/*
some options need to be upper/lower case
*/
subobj.REALM = strupper(subobj.REALM);
subobj.HOSTNAME = strlower(subobj.HOSTNAME);
subobj.DOMAIN = strupper(subobj.DOMAIN);
assert(valid_netbios_name(subobj.DOMAIN));
subobj.NETBIOSNAME = strupper(subobj.HOSTNAME);
assert(valid_netbios_name(subobj.NETBIOSNAME));
var rdns = split(",", subobj.BASEDN);
subobj.RDN_DC = substr(rdns[0], strlen("DC="));
provision_next_usn = 1;
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) {
message("Setting up smb.conf\n");
setup_file("provision.smb.conf", paths.smbconf, subobj);
lp.reload();
}
message("Setting up secrets.ldb\n");
setup_ldb("secrets.ldif", info, paths.secrets);
message("Setting up keytabs\n");
var keytab_ok = credentials_update_all_keytabs();
assert(keytab_ok);
message("Setting up hklm.ldb\n");
setup_ldb("hklm.ldif", info, paths.hklm);
message("Setting up sam.ldb attributes\n");
setup_ldb("provision_init.ldif", info, paths.samdb);
message("Setting up sam.ldb schema\n");
setup_ldb("schema.ldif", info, paths.samdb, NULL, false);
message("Setting up display specifiers\n");
setup_ldb("display_specifiers.ldif", info, paths.samdb, NULL, false);
message("Setting up sam.ldb templates\n");
setup_ldb("provision_templates.ldif", info, paths.samdb, NULL, false);
message("Setting up sam.ldb data\n");
setup_ldb("provision.ldif", info, paths.samdb, NULL, false);
if (blank != false) {
return true;
}
message("Setting up sam.ldb users and groups\n");
setup_ldb("provision_users.ldif", info, paths.samdb, data, false);
if (setup_name_mappings(info, subobj, session_info, credentials) == false) {
return false;
}
return true;
}
/* Write out a DNS zone file, from the info in the current database */
function provision_dns(subobj, message, paths, session_info, credentials)
{
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, so fetch them from the database */
subobj.DOMAINGUID = searchone(ldb, "(&(objectClass=domainDNS)(dnsDomain=" + subobj.DNSDOMAIN + "))", "objectGUID");
assert(subobj.DOMAINGUID != undefined);
subobj.HOSTGUID = searchone(ldb, "(&(objectClass=computer)(cn=" + subobj.NETBIOSNAME + "))", "objectGUID");
assert(subobj.HOSTGUID != undefined);
setup_file("provision.zone",
paths.dns,
subobj);
message("Please install the zone located in " + paths.dns + " into your DNS server\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.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.DOMAINGUID = randguid();
subobj.DOMAINSID = randsid();
subobj.HOSTGUID = randguid();
subobj.INVOCATIONID = randguid();
subobj.KRBTGTPASS = randpass(12);
subobj.MACHINEPASS = randpass(12);
subobj.ADMINPASS = randpass(12);
subobj.DEFAULTSITE = "Default-First-Site-Name";
subobj.NEWGUID = randguid;
subobj.NTTIME = nttime;
subobj.LDAPTIME = ldaptime;
subobj.DATESTRING = datestring;
subobj.USN = nextusn;
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");
subobj.BACKUP = findnss(nss.getgrnam, "backup", "wheel", "root", "staff");
subobj.USERS = findnss(nss.getgrnam, "users", "guest", "other");
subobj.DNSDOMAIN = strlower(subobj.REALM);
subobj.DNSNAME = sprintf("%s.%s",
strlower(subobj.HOSTNAME),
subobj.DNSDOMAIN);
rdn_list = split(".", subobj.DNSDOMAIN);
subobj.BASEDN = "DC=" + join(",DC=", rdn_list);
return subobj;
}
/*
search for one attribute as a string
*/
function searchone(ldb, expression, attribute)
{
var attrs = new Array(attribute);
res = ldb.search(expression, attrs);
if (res.length != 1 ||
res[0][attribute] == undefined) {
return undefined;
}
return res[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.length == 1);
var userAccountControl = res[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;
}
/*
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 domain_dn = searchone(ldb, "objectClass=domainDNS", "dn");
assert(domain_dn != undefined);
var dom_users = searchone(ldb, "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
memberOf: %s
unixName: %s
sambaPassword: %s
objectClass: user
",
user_dn, username, dom_users,
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 != true) {
message("Failed to add %s - %s\n", user_dn, ldb.errstring());
return false;
}
message("Modifying group %s\n", dom_users);
ok = ldb.modify(modgroup);
if (ok != true) {
message("Failed to modify %s - %s\n", dom_users, ldb.errstring());
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)) {
message("workgroup '%s' in smb.conf must match chosen domain '%s'\n",
lp.get("workgroup"), subobj.DOMAIN);
return false;
}
if (strupper(lp.get("realm")) != strupper(subobj.REALM)) {
message("realm '%s' in smb.conf must match chosen realm '%s'\n",
lp.get("realm"), subobj.REALM);
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;
|