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
|
#!/usr/bin/perl -w
use strict;
use Socket;
use Getopt::Long;
my $opt_hostname = `hostname`;
chomp $opt_hostname;
my $opt_hostip;
my $opt_realm;
my $opt_domain;
my $opt_adminpass;
my $opt_nobody;
my $opt_nogroup;
my $opt_wheel;
my $opt_users;
my $dnsdomain;
my $netbiosname;
my $dnsname;
my $basedn;
my $defaultsite = "Default-First-Site-Name";
# return the current NTTIME as an integer
sub nttime()
{
my $t = time();
$t += (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60));
$t *= 1.0e7;
return sprintf("%lld", $t);
}
# generate a random guid. Not a good algorithm.
sub randguid()
{
my $r1 = int(rand(2**32));
my $r2 = int(rand(2**16));
my $r3 = int(rand(2**16));
my $r4 = int(rand(2**16));
my $r5 = int(rand(2**32));
my $r6 = int(rand(2**16));
return sprintf("%08x-%04x-%04x-%04x-%08x%04x", $r1, $r2, $r3, $r4, $r5, $r6);
}
my $opt_domainguid = randguid();
my $hostguid = randguid();
sub randsid()
{
return sprintf("S-1-5-21-%d-%d-%d",
int(rand(10**8)), int(rand(10**8)), int(rand(10**8)));
}
my $opt_domainsid = randsid();
# generate a random password. Poor algorithm :(
sub randpass()
{
my $pass = "";
my $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%\$!~";
for (my $i=0;$i<8;$i++) {
my $c = int(rand(length($chars)));
$pass .= substr($chars, $c, 1);
}
return $pass;
}
sub ldaptime()
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
return sprintf "%04u%02u%02u%02u%02u%02u.0Z",
$year+1900, $mon+1, $mday, $hour, $min, $sec;
}
#######################
# substitute a single variable
sub substitute($)
{
my $var = shift;
if ($var eq "BASEDN") {
return $basedn;
}
if ($var eq "DOMAINSID") {
return $opt_domainsid;
}
if ($var eq "DOMAIN") {
return $opt_domain;
}
if ($var eq "REALM") {
return $opt_realm;
}
if ($var eq "DNSDOMAIN") {
return $dnsdomain;
}
if ($var eq "HOSTNAME") {
return $opt_hostname;
}
if ($var eq "NETBIOSNAME") {
return $netbiosname;
}
if ($var eq "DNSNAME") {
return $dnsname;
}
if ($var eq "HOSTIP") {
return $opt_hostip;
}
if ($var eq "LDAPTIME") {
return ldaptime();
}
if ($var eq "NEWGUID") {
return randguid();
}
if ($var eq "DOMAINGUID") {
return $opt_domainguid;
}
if ($var eq "HOSTGUID") {
return $hostguid;
}
if ($var eq "DEFAULTSITE") {
return $defaultsite;
}
if ($var eq "ADMINPASS") {
return $opt_adminpass;
}
if ($var eq "RANDPASS") {
return randpass();
}
if ($var eq "NTTIME") {
return "" . nttime();
}
if ($var eq "WHEEL") {
return $opt_wheel;
}
if ($var eq "NOBODY") {
return $opt_nobody;
}
if ($var eq "NOGROUP") {
return $opt_nogroup;
}
if ($var eq "USERS") {
return $opt_users;
}
die "ERROR: Uknown substitution variable $var\n";
}
#####################################################################
# write a string into a file
sub FileSave($$)
{
my($filename) = shift;
my($v) = shift;
local(*FILE);
open(FILE, ">$filename") || die "can't open $filename";
print FILE $v;
close(FILE);
}
#####################################################################
# read a file into a string
sub FileLoad($)
{
my($filename) = shift;
local(*INPUTFILE);
open(INPUTFILE, $filename) || return undef;
my($saved_delim) = $/;
undef $/;
my($data) = <INPUTFILE>;
close(INPUTFILE);
$/ = $saved_delim;
return $data;
}
#######################################################################
# add a foreign security principle
sub add_foreign($$$)
{
my $sid = shift;
my $desc = shift;
my $unixname = shift;
return "
dn: CN=$sid,CN=ForeignSecurityPrincipals,\${BASEDN}
objectClass: top
objectClass: foreignSecurityPrincipal
cn: $sid
description: $desc
distinguishedName: CN=$sid,CN=ForeignSecurityPrincipals,\${BASEDN}
instanceType: 4
whenCreated: \${LDAPTIME}
whenChanged: \${LDAPTIME}
uSNCreated: 1
uSNChanged: 1
showInAdvancedViewOnly: TRUE
name: $sid
objectGUID: \${NEWGUID}
objectSid: $sid
objectCategory: CN=Foreign-Security-Principal,CN=Schema,CN=Configuration,\${BASEDN}
unixName: $unixname
";
}
############################################
# show some help
sub ShowHelp()
{
print "
Samba4 provisioning
provision.pl [options]
--realm REALM set realm
--domain DOMAIN set domain
--hostname HOSTNAME set hostname
--hostip IPADDRESS set ipaddress
--adminpass PASSWORD choose admin password (otherwise random)
--nobody USERNAME choose 'nobody' user
--nogroup GROUPNAME choose 'nogroup' group
--wheel GROUPNAME choose 'wheel' privileged group
--users GROUPNAME choose 'users' group
You must provide at least a realm and domain
";
exit(1);
}
my $opt_help;
GetOptions(
'help|h|?' => \$opt_help,
'realm=s' => \$opt_realm,
'domain=s' => \$opt_domain,
'domain-guid=s' => \$opt_domainguid,
'domain-sid=s' => \$opt_domainsid,
'hostname=s' => \$opt_hostname,
'hostip=s' => \$opt_hostip,
'adminpass=s' => \$opt_adminpass,
'nobody=s' => \$opt_nobody,
'nogroup=s' => \$opt_nogroup,
'wheel=s' => \$opt_wheel,
'users=s' => \$opt_users,
);
if ($opt_help ||
!$opt_realm ||
!$opt_domain ||
!$opt_hostname) {
ShowHelp();
}
$opt_realm=uc($opt_realm);
$opt_domain=uc($opt_domain);
$opt_hostname=lc($opt_hostname);
$netbiosname=uc($opt_hostname);
if (!$opt_hostip) {
my $hip = gethostbyname($opt_hostname);
if (defined $hip) {
$opt_hostip = inet_ntoa($hip);
} else {
$opt_hostip = "<0.0.0.0>";
}
}
print "Provisioning host '$opt_hostname'[$opt_hostip] for domain '$opt_domain' in realm '$opt_realm'\n";
if (!$opt_nobody) {
if (defined getpwnam("nobody")) {
$opt_nobody = "nobody";
}
}
if (!$opt_nogroup) {
if (defined getgrnam("nogroup")) {
$opt_nogroup = "nogroup";
} elsif (defined getgrnam("nobody")) {
$opt_nogroup = "nobody";
}
}
if (!$opt_wheel) {
if (defined getgrnam("wheel")) {
$opt_wheel = "wheel";
} elsif (defined getgrnam("root")) {
$opt_wheel = "root";
}
}
if (!$opt_users) {
if (defined getgrnam("users")) {
$opt_users = "users";
}
}
$opt_nobody || die "Unable to determine a user for 'nobody'\n";
$opt_nogroup || die "Unable to determine a group for 'nogroup'\n";
$opt_users || die "Unable to determine a group for 'user'\n";
$opt_wheel || die "Unable to determine a group for 'wheel'\n";
print "Using nobody='$opt_nobody' nogroup='$opt_nogroup' wheel='$opt_wheel' users='$opt_users'\n";
print "generating ldif ...\n";
$dnsdomain = lc($opt_realm);
$dnsname = lc($opt_hostname).".".$dnsdomain;
$basedn = "DC=" . join(",DC=", split(/\./, $opt_realm));
my $data = FileLoad("provision.ldif") || die "Unable to load provision.ldif\n";
$data .= add_foreign("S-1-5-7", "Anonymous", "\${NOBODY}");
$data .= add_foreign("S-1-1-0", "World", "\${NOGROUP}");
$data .= add_foreign("S-1-5-2", "Network", "\${NOGROUP}");
$data .= add_foreign("S-1-5-18", "System", "root");
$data .= add_foreign("S-1-5-11", "Authenticated Users", "\${USERS}");
if (!$opt_adminpass) {
$opt_adminpass = randpass();
print "chose random Administrator password '$opt_adminpass'\n";
}
my $res = "";
print "applying substitutions ...\n";
while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
my $sub = substitute($2);
$res .= "$1$sub";
$data = $3;
}
$res .= $data;
print "saving ldif to newsam.ldif ...\n";
FileSave("newsam.ldif", $res);
unlink("newsam.ldb");
print "creating newsam.ldb ...\n";
# allow provisioning to be run from the source directory
$ENV{"PATH"} .= ":bin";
system("ldbadd -H newsam.ldb newsam.ldif");
print "done\n";
unlink("newrootdse.ldb");
print "creating newrootdse.ldb ...\n";
system("ldbadd -H newrootdse.ldb rootdse.ldif");
print "done\n";
print "generating dns zone file ...\n";
$data = FileLoad("provision.zone") || die "Unable to load provision.zone\n";
$res = "";
print "applying substitutions ...\n";
while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
my $sub = substitute($2);
$res .= "$1$sub";
$data = $3;
}
$res .= $data;
print "saving dns zone to newdns.zone ...\n";
FileSave("$dnsdomain.zone", $res);
print "done\n";
unlink("newhklm.ldb");
print "creating newhklm.ldb ... \n";
system("ldbadd -H newhklm.ldb hklm.ldif");
print "done\n";
print "
Installation:
- Please move newsam.ldb to sam.ldb in the private/ directory of your
Samba4 installation
- Please move newrootdse.ldb to rootdse.ldb in the private/ directory
of your Samba4 installation
- Please use $dnsdomain.zone to in BIND dns server
";
|