selftest: Use default 'prefork children' smb.conf setting
[kai/samba-autobuild/.git] / selftest / target / Samba4.pm
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
5
6 # NOTE: Refer to the README for more details about the various testenvs,
7 # and tips about adding new testenvs.
8
9 package Samba4;
10
11 use strict;
12 use Cwd qw(abs_path);
13 use FindBin qw($RealBin);
14 use POSIX;
15 use SocketWrapper;
16 use target::Samba;
17 use target::Samba3;
18 use Archive::Tar;
19 use File::Path 'make_path';
20
21 sub new($$$$$) {
22         my ($classname, $bindir, $ldap, $srcdir, $server_maxtime) = @_;
23
24         my $self = {
25                 vars => {},
26                 ldap => $ldap,
27                 bindir => $bindir,
28                 srcdir => $srcdir,
29                 server_maxtime => $server_maxtime,
30                 target3 => new Samba3($bindir, $srcdir, $server_maxtime)
31         };
32         bless $self;
33         return $self;
34 }
35
36 sub scriptdir_path($$) {
37         my ($self, $path) = @_;
38         return "$self->{srcdir}/source4/scripting/$path";
39 }
40
41 sub openldap_start($$$) {
42 }
43
44 sub slapd_start($$)
45 {
46         my $count = 0;
47         my ($self, $env_vars, $STDIN_READER) = @_;
48         my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
49
50         my $uri = $env_vars->{LDAP_URI};
51
52         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
53             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
54             return 1;
55         }
56         # running slapd in the background means it stays in the same process group, so it can be
57         # killed by timelimit
58         my $pid = fork();
59         if ($pid == 0) {
60                 open STDOUT, ">$env_vars->{LDAPDIR}/logs";
61                 open STDERR, '>&STDOUT';
62                 close($env_vars->{STDIN_PIPE});
63                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
64
65                 if ($self->{ldap} eq "fedora-ds") {
66                         exec("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd", "-D", $env_vars->{FEDORA_DS_DIR}, "-d0", "-i", $env_vars->{FEDORA_DS_PIDFILE});
67                 } elsif ($self->{ldap} eq "openldap") {
68                         exec($ENV{OPENLDAP_SLAPD}, "-dnone", "-F", $env_vars->{SLAPD_CONF_D}, "-h", $uri);
69                 }
70                 die("Unable to start slapd: $!");
71         }
72         $env_vars->{SLAPD_PID} = $pid;
73         sleep(1);
74         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
75                 $count++;
76                 if ($count > 40) {
77                         $self->slapd_stop($env_vars);
78                         return 0;
79                 }
80                 sleep(1);
81         }
82         return 1;
83 }
84
85 sub slapd_stop($$)
86 {
87         my ($self, $envvars) = @_;
88         kill 9, $envvars->{SLAPD_PID};
89         return 1;
90 }
91
92 sub check_or_start($$$)
93 {
94         my ($self, $env_vars, $process_model) = @_;
95         my $STDIN_READER;
96
97         my $env_ok = $self->check_env($env_vars);
98         if ($env_ok) {
99                 return $env_vars->{SAMBA_PID};
100         } elsif (defined($env_vars->{SAMBA_PID})) {
101                 warn("SAMBA PID $env_vars->{SAMBA_PID} is not running (died)");
102                 return undef;
103         }
104
105         # use a pipe for stdin in the child processes. This allows
106         # those processes to monitor the pipe for EOF to ensure they
107         # exit when the test script exits
108         pipe($STDIN_READER, $env_vars->{STDIN_PIPE});
109
110         # Start slapd before samba, but with the fifo on stdin
111         if (defined($self->{ldap})) {
112                 unless($self->slapd_start($env_vars, $STDIN_READER)) {
113                         warn("couldn't start slapd (main run)");
114                         return undef;
115                 }
116         }
117
118         print "STARTING SAMBA...\n";
119         my $pid = fork();
120         if ($pid == 0) {
121                 # we want out from samba to go to the log file, but also
122                 # to the users terminal when running 'make test' on the command
123                 # line. This puts it on stderr on the terminal
124                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
125                 open STDERR, '>&STDOUT';
126
127                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
128
129                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
130                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.samba";
131                 if (defined($ENV{MITKRB5})) {
132                         $ENV{KRB5_KDC_PROFILE} = $env_vars->{MITKDC_CONFIG};
133                 }
134                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
135                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
136
137                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
138                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
139                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
140                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
141                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
142                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
143
144                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
145                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
146                 } else {
147                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
148                 }
149                 $ENV{RESOLV_CONF} = $env_vars->{RESOLV_CONF};
150
151                 $ENV{UID_WRAPPER} = "1";
152                 $ENV{UID_WRAPPER_ROOT} = "1";
153
154                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "samba");
155                 my @preargs = ();
156                 my @optargs = ();
157                 if (defined($ENV{SAMBA_OPTIONS})) {
158                         @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
159                 }
160                 if(defined($ENV{SAMBA_VALGRIND})) {
161                         @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
162                 }
163
164                 if (defined($process_model)) {
165                         push @optargs, ("-M", $process_model);
166                 }
167                 close($env_vars->{STDIN_PIPE});
168                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
169
170                 exec(@preargs, Samba::bindir_path($self, "samba"), "-i", "--no-process-group", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
171         }
172         $env_vars->{SAMBA_PID} = $pid;
173         print "DONE ($pid)\n";
174
175         close($STDIN_READER);
176
177         if ($self->wait_for_start($env_vars) != 0) {
178             warn("Samba $pid failed to start up");
179             return undef;
180         }
181
182         return $pid;
183 }
184
185 sub wait_for_start($$)
186 {
187         my ($self, $testenv_vars) = @_;
188         my $count = 0;
189         my $ret = 0;
190
191         if (not $self->check_env($testenv_vars)) {
192             warn("unable to confirm Samba $testenv_vars->{SAMBA_PID} is running");
193             return -1;
194         }
195
196         # This will return quickly when things are up, but be slow if we
197         # need to wait for (eg) SSL init
198         my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
199
200         do {
201                 $ret = system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
202                 if ($ret != 0) {
203                         sleep(1);
204                 } else {
205                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
206                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
207                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
208                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
209                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
210                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
211                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
212                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
213                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
214                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
215                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
216                 }
217                 $count++;
218         } while ($ret != 0 && $count < 20);
219         if ($count == 20) {
220                 warn("nbt not reachable after 20 retries\n");
221                 teardown_env($self, $testenv_vars);
222                 return 0;
223         }
224
225         # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
226         if ($testenv_vars->{SERVER_ROLE} eq "domain controller") {
227                 print "waiting for working LDAP and a RID Set to be allocated\n";
228                 my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
229                 my $count = 0;
230                 my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
231
232                 my $search_dn = $base_dn;
233                 if ($testenv_vars->{NETBIOSNAME} ne "RODC") {
234                         # TODO currently no check for actual rIDAllocationPool
235                         $search_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
236                 }
237                 my $max_wait = 60;
238
239                 # Add hosts file for name lookups
240                 my $cmd = "NSS_WRAPPER_HOSTS='$testenv_vars->{NSS_WRAPPER_HOSTS}' ";
241                 if (defined($testenv_vars->{RESOLV_WRAPPER_CONF})) {
242                         $cmd .= "RESOLV_WRAPPER_CONF='$testenv_vars->{RESOLV_WRAPPER_CONF}' ";
243                 } else {
244                         $cmd .= "RESOLV_WRAPPER_HOSTS='$testenv_vars->{RESOLV_WRAPPER_HOSTS}' ";
245                 }
246                 $cmd .= "RESOLV_CONF='$testenv_vars->{RESOLV_CONF}' ";
247
248                 $cmd .= "$ldbsearch ";
249                 $cmd .= "$testenv_vars->{CONFIGURATION} ";
250                 $cmd .= "-H ldap://$testenv_vars->{SERVER} ";
251                 $cmd .= "-U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} ";
252                 $cmd .= "-s base ";
253                 $cmd .= "-b '$search_dn' ";
254                 while (system("$cmd >/dev/null") != 0) {
255                         $count++;
256                         if ($count > $max_wait) {
257                                 warn("Timed out ($max_wait sec) waiting for working LDAP and a RID Set to be allocated by $testenv_vars->{NETBIOSNAME} PID $testenv_vars->{SAMBA_PID}");
258                                 $ret = -1;
259                                 last;
260                         }
261                         sleep(1);
262                 }
263         }
264
265         my $wbinfo =  Samba::bindir_path($self, "wbinfo");
266
267         $count = 0;
268         do {
269                 my $cmd = "NSS_WRAPPER_PASSWD=$testenv_vars->{NSS_WRAPPER_PASSWD} ";
270                 $cmd .= "NSS_WRAPPER_GROUP=$testenv_vars->{NSS_WRAPPER_GROUP} ";
271                 $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=$testenv_vars->{SELFTEST_WINBINDD_SOCKET_DIR} ";
272                 $cmd .= "$wbinfo -P";
273                 $ret = system($cmd);
274
275                 if ($ret != 0) {
276                         sleep(1);
277                 }
278                 $count++;
279         } while ($ret != 0 && $count < 20);
280         if ($count == 20) {
281                 warn("winbind not reachable after 20 retries\n");
282                 teardown_env($self, $testenv_vars);
283                 return 0;
284         }
285
286         print $self->getlog_env($testenv_vars);
287
288         return $ret
289 }
290
291 sub write_ldb_file($$$)
292 {
293         my ($self, $file, $ldif) = @_;
294
295         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
296         open(LDIF, "|$ldbadd -H $file >/dev/null");
297         print LDIF $ldif;
298         return(close(LDIF));
299 }
300
301 sub add_wins_config($$)
302 {
303         my ($self, $privatedir) = @_;
304
305         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
306 dn: name=TORTURE_11,CN=PARTNERS
307 objectClass: wreplPartner
308 name: TORTURE_11
309 address: 127.0.0.11
310 pullInterval: 0
311 pushChangeCount: 0
312 type: 0x3
313 ");
314 }
315
316 sub mk_fedora_ds($$)
317 {
318         my ($self, $ctx) = @_;
319
320         #Make the subdirectory be as fedora DS would expect
321         my $fedora_ds_dir = "$ctx->{ldapdir}/slapd-$ctx->{ldap_instance}";
322
323         my $pidfile = "$fedora_ds_dir/logs/slapd-$ctx->{ldap_instance}.pid";
324
325         return ($fedora_ds_dir, $pidfile);
326 }
327
328 sub mk_openldap($$)
329 {
330         my ($self, $ctx) = @_;
331
332         my $slapd_conf_d = "$ctx->{ldapdir}/slapd.d";
333         my $pidfile = "$ctx->{ldapdir}/slapd.pid";
334
335         return ($slapd_conf_d, $pidfile);
336 }
337
338 sub setup_dns_hub_internal($$$)
339 {
340         my ($self, $hostname, $prefix) = @_;
341         my $STDIN_READER;
342
343         unless(-d $prefix or make_path($prefix, 0777)) {
344                 warn("Unable to create $prefix");
345                 return undef;
346         }
347         my $prefix_abs = abs_path($prefix);
348
349         die ("prefix=''") if $prefix_abs eq "";
350         die ("prefix='/'") if $prefix_abs eq "/";
351
352         unless (system("rm -rf $prefix_abs/*") == 0) {
353                 warn("Unable to clean up");
354         }
355
356         my $swiface = Samba::get_interface($hostname);
357
358         my $env = undef;
359         $env->{prefix} = $prefix;
360         $env->{prefix_abs} = $prefix_abs;
361
362         $env->{hostname} = $hostname;
363         $env->{swiface} = $swiface;
364
365         $env->{ipv4} = "127.0.0.$swiface";
366         $env->{ipv6} = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
367
368         $env->{DNS_HUB_LOG} = "$prefix_abs/dns_hub.log";
369
370         $env->{RESOLV_CONF} = "$prefix_abs/resolv.conf";
371
372         open(RESOLV_CONF, ">$env->{RESOLV_CONF}");
373         print RESOLV_CONF "nameserver $env->{ipv4}\n";
374         print RESOLV_CONF "nameserver $env->{ipv6}\n";
375         close(RESOLV_CONF);
376
377         # use a pipe for stdin in the child processes. This allows
378         # those processes to monitor the pipe for EOF to ensure they
379         # exit when the test script exits
380         pipe($STDIN_READER, $env->{STDIN_PIPE});
381
382         print "STARTING rootdnsforwarder...\n";
383         my $pid = fork();
384         if ($pid == 0) {
385                 # we want out from samba to go to the log file, but also
386                 # to the users terminal when running 'make test' on the command
387                 # line. This puts it on stderr on the terminal
388                 open STDOUT, "| tee $env->{DNS_HUB_LOG} 1>&2";
389                 open STDERR, '>&STDOUT';
390
391                 SocketWrapper::set_default_iface($swiface);
392                 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/env-$hostname$.pcap";
393                 SocketWrapper::setup_pcap($pcap_file);
394
395                 my @preargs = ();
396                 my @args = ();
397                 my @optargs = ();
398                 if (!defined($ENV{PYTHON})) {
399                     push (@preargs, "env");
400                     push (@preargs, "python");
401                 } else {
402                     push (@preargs, $ENV{PYTHON});
403                 }
404                 $ENV{MAKE_TEST_BINARY} = "$self->{srcdir}/selftest/target/dns_hub.py";
405                 push (@args, "$self->{server_maxtime}");
406                 push (@args, "$env->{ipv4}");
407                 close($env->{STDIN_PIPE});
408                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
409
410                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
411                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
412         }
413         $env->{SAMBA_PID} = $pid;
414         $env->{KRB5_CONFIG} = "${prefix_abs}/no_krb5.conf";
415         close($STDIN_READER);
416
417         print "DONE\n";
418         return $env;
419 }
420
421 sub setup_dns_hub
422 {
423         my ($self, $prefix) = @_;
424
425         my $hostname = "rootdnsforwarder";
426
427         my $env = $self->setup_dns_hub_internal("$hostname", "$prefix/$hostname");
428
429         $self->{dns_hub_env} = $env;
430
431         return $env;
432 }
433
434 sub get_dns_hub_env($)
435 {
436         my ($self, $prefix) = @_;
437
438         if (defined($self->{dns_hub_env})) {
439                 return $self->{dns_hub_env};
440         }
441
442         die("get_dns_hub_env() not setup 'dns_hub_env'");
443         return undef;
444 }
445
446 sub setup_namespaces($$:$$)
447 {
448         my ($self, $localenv, $upn_array, $spn_array) = @_;
449
450         @{$upn_array} = [] unless defined($upn_array);
451         my $upn_args = "";
452         foreach my $upn (@{$upn_array}) {
453                 $upn_args .= " --add-upn-suffix=$upn";
454         }
455
456         @{$spn_array} = [] unless defined($spn_array);
457         my $spn_args = "";
458         foreach my $spn (@{$spn_array}) {
459                 $spn_args .= " --add-spn-suffix=$spn";
460         }
461
462         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
463
464         my $cmd_env = "NSS_WRAPPER_HOSTS='$localenv->{NSS_WRAPPER_HOSTS}' ";
465         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
466         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
467                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
468         } else {
469                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
470         }
471         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\" ";
472         $cmd_env .= "KRB5CCNAME=\"$localenv->{KRB5_CCACHE}\" ";
473         $cmd_env .= "RESOLV_CONF=\"$localenv->{RESOLV_CONF}\" ";
474
475         my $cmd_config = " $localenv->{CONFIGURATION}";
476
477         my $namespaces = $cmd_env;
478         $namespaces .= " $samba_tool domain trust namespaces $upn_args $spn_args";
479         $namespaces .= $cmd_config;
480         unless (system($namespaces) == 0) {
481                 warn("Failed to add namespaces \n$namespaces");
482                 return;
483         }
484
485         return;
486 }
487
488 sub setup_trust($$$$$)
489 {
490         my ($self, $localenv, $remoteenv, $type, $extra_args) = @_;
491
492         $localenv->{TRUST_SERVER} = $remoteenv->{SERVER};
493         $localenv->{TRUST_SERVER_IP} = $remoteenv->{SERVER_IP};
494         $localenv->{TRUST_SERVER_IPV6} = $remoteenv->{SERVER_IPV6};
495         $localenv->{TRUST_NETBIOSNAME} = $remoteenv->{NETBIOSNAME};
496         $localenv->{TRUST_USERNAME} = $remoteenv->{USERNAME};
497         $localenv->{TRUST_PASSWORD} = $remoteenv->{PASSWORD};
498         $localenv->{TRUST_DOMAIN} = $remoteenv->{DOMAIN};
499         $localenv->{TRUST_REALM} = $remoteenv->{REALM};
500         $localenv->{TRUST_DOMSID} = $remoteenv->{DOMSID};
501
502         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
503
504         # setup the trust
505         my $cmd_env = "NSS_WRAPPER_HOSTS='$localenv->{NSS_WRAPPER_HOSTS}' ";
506         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
507         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
508                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
509         } else {
510                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
511         }
512         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\" ";
513         $cmd_env .= "KRB5CCNAME=\"$localenv->{KRB5_CCACHE}\" ";
514         $cmd_env .= "RESOLV_CONF=\"$localenv->{RESOLV_CONF}\" ";
515
516         my $cmd_config = " $localenv->{CONFIGURATION}";
517         my $cmd_creds = $cmd_config;
518         $cmd_creds .= " -U$localenv->{TRUST_DOMAIN}\\\\$localenv->{TRUST_USERNAME}\%$localenv->{TRUST_PASSWORD}";
519
520         my $create = $cmd_env;
521         $create .= " $samba_tool domain trust create --type=${type} $localenv->{TRUST_REALM}";
522         $create .= " $extra_args";
523         $create .= $cmd_creds;
524         unless (system($create) == 0) {
525                 warn("Failed to create trust \n$create");
526                 return undef;
527         }
528
529         my $groupname = "g_$localenv->{TRUST_DOMAIN}";
530         my $groupadd = $cmd_env;
531         $groupadd .= " $samba_tool group add '$groupname' --group-scope=Domain $cmd_config";
532         unless (system($groupadd) == 0) {
533                 warn("Failed to create group \n$groupadd");
534                 return undef;
535         }
536         my $groupmem = $cmd_env;
537         $groupmem .= " $samba_tool group addmembers '$groupname' '$localenv->{TRUST_DOMSID}-513' $cmd_config";
538         unless (system($groupmem) == 0) {
539                 warn("Failed to add group member \n$groupmem");
540                 return undef;
541         }
542
543         return $localenv
544 }
545
546 sub provision_raw_prepare($$$$$$$$$$$$)
547 {
548         my ($self, $prefix, $server_role, $hostname,
549             $domain, $realm, $samsid, $functional_level,
550             $password, $kdc_ipv4, $kdc_ipv6) = @_;
551         my $ctx;
552         my $python_cmd = "";
553         if (defined $ENV{PYTHON}) {
554                 $python_cmd = $ENV{PYTHON} . " ";
555         }
556         $ctx->{python} = $python_cmd;
557         my $netbiosname = uc($hostname);
558
559         unless(-d $prefix or mkdir($prefix, 0777)) {
560                 warn("Unable to create $prefix");
561                 return undef;
562         }
563         my $prefix_abs = abs_path($prefix);
564
565         die ("prefix=''") if $prefix_abs eq "";
566         die ("prefix='/'") if $prefix_abs eq "/";
567
568         unless (system("rm -rf $prefix_abs/*") == 0) {
569                 warn("Unable to clean up");
570         }
571
572         
573         my $swiface = Samba::get_interface($hostname);
574
575         $ctx->{prefix} = $prefix;
576         $ctx->{prefix_abs} = $prefix_abs;
577
578         $ctx->{server_role} = $server_role;
579         $ctx->{hostname} = $hostname;
580         $ctx->{netbiosname} = $netbiosname;
581         $ctx->{swiface} = $swiface;
582         $ctx->{password} = $password;
583         $ctx->{kdc_ipv4} = $kdc_ipv4;
584         $ctx->{kdc_ipv6} = $kdc_ipv6;
585         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
586         if ($functional_level eq "2000") {
587                 $ctx->{supported_enctypes} = "arcfour-hmac-md5 des-cbc-md5 des-cbc-crc"
588         }
589
590 #
591 # Set smbd log level here.
592 #
593         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
594         $ctx->{username} = "Administrator";
595         $ctx->{domain} = $domain;
596         $ctx->{realm} = uc($realm);
597         $ctx->{dnsname} = lc($realm);
598         $ctx->{samsid} = $samsid;
599
600         $ctx->{functional_level} = $functional_level;
601
602         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
603         chomp $unix_name;
604         $ctx->{unix_name} = $unix_name;
605         $ctx->{unix_uid} = $>;
606         my @mygid = split(" ", $();
607         $ctx->{unix_gid} = $mygid[0];
608         $ctx->{unix_gids_str} = $);
609         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
610
611         $ctx->{etcdir} = "$prefix_abs/etc";
612         $ctx->{piddir} = "$prefix_abs/pid";
613         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
614         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
615         $ctx->{krb5_ccache} = "$prefix_abs/krb5_ccache";
616         $ctx->{mitkdc_conf} = "$ctx->{etcdir}/mitkdc.conf";
617         $ctx->{privatedir} = "$prefix_abs/private";
618         $ctx->{binddnsdir} = "$prefix_abs/bind-dns";
619         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
620         $ctx->{lockdir} = "$prefix_abs/lockdir";
621         $ctx->{logdir} = "$prefix_abs/logs";
622         $ctx->{statedir} = "$prefix_abs/statedir";
623         $ctx->{cachedir} = "$prefix_abs/cachedir";
624         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
625         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
626         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
627         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
628         $ctx->{nsswrap_hosts} = "$ENV{SELFTEST_PREFIX}/hosts";
629         $ctx->{nsswrap_hostname} = "$ctx->{hostname}.$ctx->{dnsname}";
630         if ($ENV{SAMBA_DNS_FAKING}) {
631                 $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
632                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf} --all-interfaces --use-file=$ctx->{dns_host_file}";
633                 $ctx->{samba_dnsupdate} = $python_cmd .  $ctx->{samba_dnsupdate};
634         } else {
635                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf} --all-interfaces";
636                 $ctx->{samba_dnsupdate} = $python_cmd .  $ctx->{samba_dnsupdate};
637                 $ctx->{use_resolv_wrapper} = 1;
638         }
639
640         my $dns_hub = $self->get_dns_hub_env();
641         $ctx->{resolv_conf} = $dns_hub->{RESOLV_CONF};
642
643         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
644
645         $ctx->{ipv4} = "127.0.0.$swiface";
646         $ctx->{ipv6} = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
647         $ctx->{interfaces} = "$ctx->{ipv4}/8 $ctx->{ipv6}/64";
648
649         push(@{$ctx->{directories}}, $ctx->{privatedir});
650         push(@{$ctx->{directories}}, $ctx->{binddnsdir});
651         push(@{$ctx->{directories}}, $ctx->{etcdir});
652         push(@{$ctx->{directories}}, $ctx->{piddir});
653         push(@{$ctx->{directories}}, $ctx->{lockdir});
654         push(@{$ctx->{directories}}, $ctx->{logdir});
655         push(@{$ctx->{directories}}, $ctx->{statedir});
656         push(@{$ctx->{directories}}, $ctx->{cachedir});
657
658         $ctx->{smb_conf_extra_options} = "";
659
660         my @provision_options = ();
661         push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_conf}\"");
662         push (@provision_options, "KRB5_CCACHE=\"$ctx->{krb5_ccache}\"");
663         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
664         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
665         push (@provision_options, "NSS_WRAPPER_HOSTS=\"$ctx->{nsswrap_hosts}\"");
666         push (@provision_options, "NSS_WRAPPER_HOSTNAME=\"$ctx->{nsswrap_hostname}\"");
667         if (defined($ctx->{use_resolv_wrapper})) {
668                 push (@provision_options, "RESOLV_WRAPPER_CONF=\"$ctx->{resolv_conf}\"");
669                 push (@provision_options, "RESOLV_CONF=\"$ctx->{resolv_conf}\"");
670         } else {
671                 push (@provision_options, "RESOLV_WRAPPER_HOSTS=\"$ctx->{dns_host_file}\"");
672         }
673         if (defined($ENV{GDB_PROVISION})) {
674                 push (@provision_options, "gdb --args");
675                 if (!defined($ENV{PYTHON})) {
676                     push (@provision_options, "env");
677                     push (@provision_options, "python");
678                 }
679         }
680         if (defined($ENV{VALGRIND_PROVISION})) {
681                 push (@provision_options, "valgrind");
682                 if (!defined($ENV{PYTHON})) {
683                     push (@provision_options, "env");
684                     push (@provision_options, "python");
685                 }
686         }
687
688         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
689
690         push (@provision_options, $samba_tool);
691         push (@provision_options, "domain");
692         push (@provision_options, "provision");
693         push (@provision_options, "--configfile=$ctx->{smb_conf}");
694         push (@provision_options, "--host-name=$ctx->{hostname}");
695         push (@provision_options, "--host-ip=$ctx->{ipv4}");
696         push (@provision_options, "--quiet");
697         push (@provision_options, "--domain=$ctx->{domain}");
698         push (@provision_options, "--realm=$ctx->{realm}");
699         if (defined($ctx->{samsid})) {
700                 push (@provision_options, "--domain-sid=$ctx->{samsid}");
701         }
702         push (@provision_options, "--adminpass=$ctx->{password}");
703         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
704         push (@provision_options, "--machinepass=machine$ctx->{password}");
705         push (@provision_options, "--root=$ctx->{unix_name}");
706         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
707         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
708
709         @{$ctx->{provision_options}} = @provision_options;
710
711         return $ctx;
712 }
713
714 sub has_option
715 {
716         my ($self, $keyword, @options_list) = @_;
717
718         # convert the options-list to a hash-map for easy keyword lookup
719         my %options_dict = map { $_ => 1 } @options_list;
720
721         return exists $options_dict{$keyword};
722 }
723
724 #
725 # Step1 creates the basic configuration
726 #
727 sub provision_raw_step1($$)
728 {
729         my ($self, $ctx) = @_;
730
731         mkdir($_, 0777) foreach (@{$ctx->{directories}});
732
733         ##
734         ## lockdir and piddir must be 0755
735         ##
736         chmod 0755, $ctx->{lockdir};
737         chmod 0755, $ctx->{piddir};
738
739         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
740                 warn("can't open $ctx->{smb_conf}$?");
741                 return undef;
742         }
743
744         Samba::prepare_keyblobs($ctx);
745         my $crlfile = "$ctx->{tlsdir}/crl.pem";
746         $crlfile = "" unless -e ${crlfile};
747
748         # work out which file server to use. Default to source3 smbd (s3fs),
749         # unless the source4 NTVFS (smb) file server has been specified
750         my $services = "-smb +s3fs";
751         if ($self->has_option("--use-ntvfs", @{$ctx->{provision_options}})) {
752                 $services = "+smb -s3fs";
753         }
754
755         print CONFFILE "
756 [global]
757         netbios name = $ctx->{netbiosname}
758         posix:eadb = $ctx->{statedir}/eadb.tdb
759         workgroup = $ctx->{domain}
760         realm = $ctx->{realm}
761         private dir = $ctx->{privatedir}
762         binddns dir = $ctx->{binddnsdir}
763         pid directory = $ctx->{piddir}
764         ncalrpc dir = $ctx->{ncalrpcdir}
765         lock dir = $ctx->{lockdir}
766         state directory = $ctx->{statedir}
767         cache directory = $ctx->{cachedir}
768         winbindd socket directory = $ctx->{winbindd_socket_dir}
769         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
770         winbind separator = /
771         interfaces = $ctx->{interfaces}
772         tls dh params file = $ctx->{tlsdir}/dhparms.pem
773         tls crlfile = ${crlfile}
774         tls verify peer = no_check
775         panic action = $RealBin/gdb_backtrace \%d
776         wins support = yes
777         server role = $ctx->{server_role}
778         server services = +echo $services
779         dcerpc endpoint servers = +winreg +srvsvc
780         notify:inotify = false
781         ldb:nosync = true
782         ldap server require strong auth = yes
783 #We don't want to pass our self-tests if the PAC code is wrong
784         gensec:require_pac = true
785         log file = $ctx->{logdir}/log.\%m
786         log level = $ctx->{server_loglevel}
787         lanman auth = Yes
788         ntlm auth = Yes
789         rndc command = true
790         dns update command = $ctx->{samba_dnsupdate}
791         spn update command = $ctx->{python} $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
792         gpo update command = $ctx->{python} $ENV{SRCDIR_ABS}/source4/scripting/bin/samba-gpupdate -s $ctx->{smb_conf} --target=Computer
793         samba kcc command = $ctx->{python} $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_kcc
794         dreplsrv:periodic_startup_interval = 0
795         dsdb:schema update allowed = yes
796
797         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
798
799         idmap_ldb:use rfc2307=yes
800         winbind enum users = yes
801         winbind enum groups = yes
802
803         rpc server port:netlogon = 1026
804
805 ";
806
807         print CONFFILE "
808
809         # Begin extra options
810         $ctx->{smb_conf_extra_options}
811         # End extra options
812 ";
813         close(CONFFILE);
814
815         #Default the KDC IP to the server's IP
816         if (not defined($ctx->{kdc_ipv4})) {
817                 $ctx->{kdc_ipv4} = $ctx->{ipv4};
818         }
819         if (not defined($ctx->{kdc_ipv6})) {
820                 $ctx->{kdc_ipv6} = $ctx->{ipv6};
821         }
822
823         Samba::mk_krb5_conf($ctx);
824         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
825
826         open(PWD, ">$ctx->{nsswrap_passwd}");
827         if ($ctx->{unix_uid} != 0) {
828                 print PWD "root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false\n";
829         }
830         print PWD "$ctx->{unix_name}:x:$ctx->{unix_uid}:65531:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false\n";
831         print PWD "nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
832 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
833 pdbtest2:x:65532:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
834 pdbtest3:x:65531:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
835 pdbtest4:x:65530:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
836 ";
837         close(PWD);
838         my $uid_rfc2307test = 65533;
839
840         open(GRP, ">$ctx->{nsswrap_group}");
841         if ($ctx->{unix_gid} != 0) {
842                 print GRP "root:x:0:\n";
843         }
844         print GRP "$ctx->{unix_name}:x:$ctx->{unix_gid}:\n";
845         print GRP "wheel:x:10:
846 users:x:65531:
847 nobody:x:65533:
848 nogroup:x:65534:nobody
849 ";
850         close(GRP);
851         my $gid_rfc2307test = 65532;
852
853         my $hostname = lc($ctx->{hostname});
854         open(HOSTS, ">>$ctx->{nsswrap_hosts}");
855         if ($hostname eq "localdc") {
856                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
857                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
858         } else {
859                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} ${hostname}\n";
860                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} ${hostname}\n";
861         }
862         close(HOSTS);
863
864         my $configuration = "--configfile=$ctx->{smb_conf}";
865
866 #Ensure the config file is valid before we start
867         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
868         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
869                 system("$testparm -v --suppress-prompt $configuration >&2");
870                 warn("Failed to create a valid smb.conf configuration $testparm!");
871                 return undef;
872         }
873         unless (system("($testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global 2> /dev/null | grep -i \"^$ctx->{netbiosname}\" ) >/dev/null 2>&1") == 0) {
874                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
875                 return undef;
876         }
877
878         my $ret = {
879                 KRB5_CONFIG => $ctx->{krb5_conf},
880                 KRB5_CCACHE => $ctx->{krb5_ccache},
881                 MITKDC_CONFIG => $ctx->{mitkdc_conf},
882                 PIDDIR => $ctx->{piddir},
883                 SERVER => $ctx->{hostname},
884                 SERVER_IP => $ctx->{ipv4},
885                 SERVER_IPV6 => $ctx->{ipv6},
886                 NETBIOSNAME => $ctx->{netbiosname},
887                 DOMAIN => $ctx->{domain},
888                 USERNAME => $ctx->{username},
889                 REALM => $ctx->{realm},
890                 DNSNAME => $ctx->{dnsname},
891                 SAMSID => $ctx->{samsid},
892                 PASSWORD => $ctx->{password},
893                 LDAPDIR => $ctx->{ldapdir},
894                 LDAP_INSTANCE => $ctx->{ldap_instance},
895                 SELFTEST_WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
896                 NCALRPCDIR => $ctx->{ncalrpcdir},
897                 LOCKDIR => $ctx->{lockdir},
898                 STATEDIR => $ctx->{statedir},
899                 CACHEDIR => $ctx->{cachedir},
900                 PRIVATEDIR => $ctx->{privatedir},
901                 BINDDNSDIR => $ctx->{binddnsdir},
902                 SERVERCONFFILE => $ctx->{smb_conf},
903                 CONFIGURATION => $configuration,
904                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
905                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
906                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
907                 NSS_WRAPPER_HOSTS => $ctx->{nsswrap_hosts},
908                 NSS_WRAPPER_HOSTNAME => $ctx->{nsswrap_hostname},
909                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
910                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
911                 SAMBA_TEST_LOG_POS => 0,
912                 NSS_WRAPPER_MODULE_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
913                 NSS_WRAPPER_MODULE_FN_PREFIX => "winbind",
914                 LOCAL_PATH => $ctx->{share},
915                 UID_RFC2307TEST => $uid_rfc2307test,
916                 GID_RFC2307TEST => $gid_rfc2307test,
917                 SERVER_ROLE => $ctx->{server_role},
918                 RESOLV_CONF => $ctx->{resolv_conf}
919         };
920
921         if (defined($ctx->{use_resolv_wrapper})) {
922                 $ret->{RESOLV_WRAPPER_CONF} = $ctx->{resolv_conf};
923         } else {
924                 $ret->{RESOLV_WRAPPER_HOSTS} = $ctx->{dns_host_file};
925         }
926
927         if ($ctx->{server_role} eq "domain controller") {
928                 $ret->{DOMSID} = $ret->{SAMSID};
929         }
930
931         return $ret;
932 }
933
934 #
935 # Step2 runs the provision script
936 #
937 sub provision_raw_step2($$$)
938 {
939         my ($self, $ctx, $ret) = @_;
940
941         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
942         unless (system($provision_cmd) == 0) {
943                 warn("Unable to provision: \n$provision_cmd\n");
944                 return undef;
945         }
946
947         my $testallowed_account = "testallowed";
948         my $samba_tool_cmd = "";
949         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
950         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
951         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
952             . " user create --configfile=$ctx->{smb_conf} $testallowed_account $ctx->{password}";
953         unless (system($samba_tool_cmd) == 0) {
954                 warn("Unable to add testallowed user: \n$samba_tool_cmd\n");
955                 return undef;
956         }
957
958         my $ldbmodify = "";
959         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
960         $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
961         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
962         my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
963
964         if ($ctx->{server_role} ne "domain controller") {
965                 $base_dn = "DC=$ctx->{netbiosname}";
966         }
967
968         my $user_dn = "cn=$testallowed_account,cn=users,$base_dn";
969         $testallowed_account = "testallowed account";
970         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
971         print LDIF "dn: $user_dn
972 changetype: modify
973 replace: samAccountName
974 samAccountName: $testallowed_account
975 -
976 ";
977         close(LDIF);
978
979         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
980         print LDIF "dn: $user_dn
981 changetype: modify
982 replace: userPrincipalName
983 userPrincipalName: testallowed upn\@$ctx->{realm}
984 replace: servicePrincipalName
985 servicePrincipalName: host/testallowed
986 -           
987 ";
988         close(LDIF);
989
990         $samba_tool_cmd = "";
991         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
992         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
993         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
994             . " user create --configfile=$ctx->{smb_conf} testdenied $ctx->{password}";
995         unless (system($samba_tool_cmd) == 0) {
996                 warn("Unable to add testdenied user: \n$samba_tool_cmd\n");
997                 return undef;
998         }
999
1000         my $user_dn = "cn=testdenied,cn=users,$base_dn";
1001         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
1002         print LDIF "dn: $user_dn
1003 changetype: modify
1004 replace: userPrincipalName
1005 userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
1006 -           
1007 ";
1008         close(LDIF);
1009
1010         $samba_tool_cmd = "";
1011         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1012         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1013         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
1014             . " user create --configfile=$ctx->{smb_conf} testupnspn $ctx->{password}";
1015         unless (system($samba_tool_cmd) == 0) {
1016                 warn("Unable to add testupnspn user: \n$samba_tool_cmd\n");
1017                 return undef;
1018         }
1019
1020         my $user_dn = "cn=testupnspn,cn=users,$base_dn";
1021         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
1022         print LDIF "dn: $user_dn
1023 changetype: modify
1024 replace: userPrincipalName
1025 userPrincipalName: http/testupnspn.$ctx->{dnsname}\@$ctx->{realm}
1026 replace: servicePrincipalName
1027 servicePrincipalName: http/testupnspn.$ctx->{dnsname}
1028 -
1029 ";
1030         close(LDIF);
1031
1032         $samba_tool_cmd = "";
1033         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1034         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1035         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
1036             . " group addmembers --configfile=$ctx->{smb_conf} 'Allowed RODC Password Replication Group' '$testallowed_account'";
1037         unless (system($samba_tool_cmd) == 0) {
1038                 warn("Unable to add '$testallowed_account' user to 'Allowed RODC Password Replication Group': \n$samba_tool_cmd\n");
1039                 return undef;
1040         }
1041
1042         # Create to users alice and bob!
1043         my $user_account_array = ["alice", "bob", "jane"];
1044
1045         foreach my $user_account (@{$user_account_array}) {
1046                 my $samba_tool_cmd = "";
1047
1048                 $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1049                 $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1050                 $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
1051                     . " user create --configfile=$ctx->{smb_conf} $user_account Secret007";
1052                 unless (system($samba_tool_cmd) == 0) {
1053                         warn("Unable to create user: $user_account\n$samba_tool_cmd\n");
1054                         return undef;
1055                 }
1056         }
1057
1058         my $ldbmodify = "";
1059         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1060         $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1061         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
1062
1063         my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
1064         my $user_dn = "cn=jane,cn=users,$base_dn";
1065
1066         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
1067         print LDIF "dn: $user_dn
1068 changetype: modify
1069 replace: userPrincipalName
1070 userPrincipalName: jane.doe\@$ctx->{realm}
1071 -
1072 ";
1073         close(LDIF);
1074
1075         return $ret;
1076 }
1077
1078 sub provision($$$$$$$$$$)
1079 {
1080         my ($self, $prefix, $server_role, $hostname,
1081             $domain, $realm, $functional_level,
1082             $password, $kdc_ipv4, $kdc_ipv6, $extra_smbconf_options, $extra_smbconf_shares,
1083             $extra_provision_options) = @_;
1084
1085         my $samsid = Samba::random_domain_sid();
1086
1087         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
1088                                                $hostname,
1089                                                $domain, $realm,
1090                                                $samsid,
1091                                                $functional_level,
1092                                                $password, $kdc_ipv4, $kdc_ipv6);
1093
1094         if (defined($extra_provision_options)) {
1095                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
1096         }
1097
1098         $ctx->{share} = "$ctx->{prefix_abs}/share";
1099         push(@{$ctx->{directories}}, "$ctx->{share}");
1100         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
1101         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
1102
1103         # precreate directories for printer drivers
1104         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
1105         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
1106         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
1107
1108         my $msdfs = "no";
1109         $msdfs = "yes" if ($server_role eq "domain controller");
1110         $ctx->{smb_conf_extra_options} = "
1111
1112         max xmit = 32K
1113         server max protocol = SMB2
1114         host msdfs = $msdfs
1115         lanman auth = yes
1116
1117         # fruit:copyfile is a global option
1118         fruit:copyfile = yes
1119
1120         $extra_smbconf_options
1121
1122 [tmp]
1123         path = $ctx->{share}
1124         read only = no
1125         posix:sharedelay = 100000
1126         posix:oplocktimeout = 3
1127         posix:writetimeupdatedelay = 500000
1128
1129 [xcopy_share]
1130         path = $ctx->{share}
1131         read only = no
1132         posix:sharedelay = 100000
1133         posix:oplocktimeout = 3
1134         posix:writetimeupdatedelay = 500000
1135         create mask = 777
1136         force create mode = 777
1137
1138 [posix_share]
1139         path = $ctx->{share}
1140         read only = no
1141         create mask = 0777
1142         force create mode = 0
1143         directory mask = 0777
1144         force directory mode = 0
1145
1146 [test1]
1147         path = $ctx->{share}/test1
1148         read only = no
1149         posix:sharedelay = 100000
1150         posix:oplocktimeout = 3
1151         posix:writetimeupdatedelay = 500000
1152
1153 [test2]
1154         path = $ctx->{share}/test2
1155         read only = no
1156         posix:sharedelay = 100000
1157         posix:oplocktimeout = 3
1158         posix:writetimeupdatedelay = 500000
1159
1160 [cifs]
1161         path = $ctx->{share}/_ignore_cifs_
1162         read only = no
1163         ntvfs handler = cifs
1164         cifs:server = $ctx->{netbiosname}
1165         cifs:share = tmp
1166         cifs:use-s4u2proxy = yes
1167         # There is no username specified here, instead the client is expected
1168         # to log in with kerberos, and the serverwill use delegated credentials.
1169         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1170
1171 [simple]
1172         path = $ctx->{share}
1173         read only = no
1174         ntvfs handler = simple
1175
1176 [sysvol]
1177         path = $ctx->{statedir}/sysvol
1178         read only = no
1179
1180 [netlogon]
1181         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1182         read only = no
1183
1184 [cifsposix]
1185         copy = simple
1186         ntvfs handler = cifsposix
1187
1188 [vfs_fruit]
1189         path = $ctx->{share}
1190         vfs objects = catia fruit streams_xattr acl_xattr
1191         ea support = yes
1192         fruit:resource = file
1193         fruit:metadata = netatalk
1194         fruit:locking = netatalk
1195         fruit:encoding = native
1196
1197 $extra_smbconf_shares
1198 ";
1199
1200         if (defined($self->{ldap})) {
1201                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
1202                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
1203
1204                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
1205                 $ldap_uri =~ s|/|%2F|g;
1206                 $ldap_uri = "ldapi://$ldap_uri";
1207                 $ctx->{ldap_uri} = $ldap_uri;
1208
1209                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
1210         }
1211
1212         my $ret = $self->provision_raw_step1($ctx);
1213         unless (defined $ret) {
1214                 return undef;
1215         }
1216
1217         if (defined($self->{ldap})) {
1218                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
1219                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
1220                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
1221                 if ($self->{ldap} eq "openldap") {
1222                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
1223                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
1224
1225                 } elsif ($self->{ldap} eq "fedora-ds") {
1226                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
1227                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
1228                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
1229                 }
1230
1231         }
1232
1233         return $self->provision_raw_step2($ctx, $ret);
1234 }
1235
1236 sub provision_s4member($$$$$)
1237 {
1238         my ($self, $prefix, $dcvars, $hostname, $more_conf) = @_;
1239         print "PROVISIONING MEMBER...\n";
1240         my $extra_smb_conf = "
1241         passdb backend = samba_dsdb
1242 winbindd:use external pipes = true
1243
1244 # the source4 smb server doesn't allow signing by default
1245 server signing = enabled
1246 raw NTLMv2 auth = yes
1247
1248 rpc_server:default = external
1249 rpc_server:svcctl = embedded
1250 rpc_server:srvsvc = embedded
1251 rpc_server:eventlog = embedded
1252 rpc_server:ntsvcs = embedded
1253 rpc_server:winreg = embedded
1254 rpc_server:spoolss = embedded
1255 rpc_daemon:spoolssd = embedded
1256 rpc_server:tcpip = no
1257 ";
1258         if ($more_conf) {
1259                 $extra_smb_conf = $extra_smb_conf . $more_conf . "\n";
1260         }
1261         my $extra_provision_options = ["--use-ntvfs"];
1262         my $ret = $self->provision($prefix,
1263                                    "member server",
1264                                    $hostname,
1265                                    $dcvars->{DOMAIN},
1266                                    $dcvars->{REALM},
1267                                    "2008",
1268                                    "locMEMpass3",
1269                                    $dcvars->{SERVER_IP},
1270                                    $dcvars->{SERVER_IPV6},
1271                                    $extra_smb_conf, "",
1272                                    $extra_provision_options);
1273         unless ($ret) {
1274                 return undef;
1275         }
1276
1277         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1278         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1279         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1280         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1281                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1282         } else {
1283                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1284         }
1285         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1286         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1287         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1288         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1289         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1290         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1291
1292         unless (system($cmd) == 0) {
1293                 warn("Join failed\n$cmd");
1294                 return undef;
1295         }
1296
1297         $ret->{MEMBER_SERVER} = $ret->{SERVER};
1298         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
1299         $ret->{MEMBER_SERVER_IPV6} = $ret->{SERVER_IPV6};
1300         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
1301         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
1302         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
1303
1304         $ret->{DOMSID} = $dcvars->{DOMSID};
1305         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1306         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1307         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1308         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1309         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1310         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1311
1312         return $ret;
1313 }
1314
1315 sub provision_rpc_proxy($$$)
1316 {
1317         my ($self, $prefix, $dcvars) = @_;
1318         print "PROVISIONING RPC PROXY...\n";
1319
1320         my $extra_smbconf_options = "
1321         passdb backend = samba_dsdb
1322
1323         # rpc_proxy
1324         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
1325         dcerpc endpoint servers = epmapper, remote
1326         dcerpc_remote:interfaces = rpcecho
1327         dcerpc_remote:allow_anonymous_fallback = yes
1328
1329 [cifs_to_dc]
1330         path = /tmp/_ignore_cifs_to_dc_/_none_
1331         read only = no
1332         ntvfs handler = cifs
1333         cifs:server = $dcvars->{SERVER}
1334         cifs:share = cifs
1335         cifs:use-s4u2proxy = yes
1336         # There is no username specified here, instead the client is expected
1337         # to log in with kerberos, and the serverwill use delegated credentials.
1338         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1339
1340 ";
1341
1342         my $extra_provision_options = ["--use-ntvfs"];
1343         my $ret = $self->provision($prefix,
1344                                    "member server",
1345                                    "localrpcproxy",
1346                                    $dcvars->{DOMAIN},
1347                                    $dcvars->{REALM},
1348                                    "2008",
1349                                    "locRPCproxypass4",
1350                                    $dcvars->{SERVER_IP},
1351                                    $dcvars->{SERVER_IPV6},
1352                                    $extra_smbconf_options, "",
1353                                    $extra_provision_options);
1354         unless ($ret) {
1355                 return undef;
1356         }
1357
1358         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1359
1360         # The joind runs in the context of the rpc_proxy/member for now
1361         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1362         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1363         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1364                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1365         } else {
1366                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1367         }
1368         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1369         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1370         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1371         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1372         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1373         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1374
1375         unless (system($cmd) == 0) {
1376                 warn("Join failed\n$cmd");
1377                 return undef;
1378         }
1379
1380         # Setting up delegation runs in the context of the DC for now
1381         $cmd = "";
1382         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1383         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1384         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1385         $cmd .= "RESOLV_CONF=\"$dcvars->{RESOLV_CONF}\" ";
1386         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
1387         $cmd .= " $dcvars->{CONFIGURATION}";
1388         print $cmd;
1389
1390         unless (system($cmd) == 0) {
1391                 warn("Delegation failed\n$cmd");
1392                 return undef;
1393         }
1394
1395         # Setting up delegation runs in the context of the DC for now
1396         $cmd = "";
1397         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1398         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1399         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1400         $cmd .= "RESOLV_CONF=\"$dcvars->{RESOLV_CONF}\" ";
1401         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
1402         $cmd .= " $dcvars->{CONFIGURATION}";
1403
1404         unless (system($cmd) == 0) {
1405                 warn("Delegation failed\n$cmd");
1406                 return undef;
1407         }
1408
1409         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
1410         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
1411         $ret->{RPC_PROXY_SERVER_IPV6} = $ret->{SERVER_IPV6};
1412         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
1413         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
1414         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
1415
1416         $ret->{DOMSID} = $dcvars->{DOMSID};
1417         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1418         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1419         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1420         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1421         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1422         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1423
1424         return $ret;
1425 }
1426
1427 sub provision_promoted_dc($$$)
1428 {
1429         my ($self, $prefix, $dcvars) = @_;
1430         print "PROVISIONING PROMOTED DC...\n";
1431
1432         # We do this so that we don't run the provision.  That's the job of 'samba-tool domain dcpromo'.
1433         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1434                                                "promotedvdc",
1435                                                $dcvars->{DOMAIN},
1436                                                $dcvars->{REALM},
1437                                                $dcvars->{SAMSID},
1438                                                "2008",
1439                                                $dcvars->{PASSWORD},
1440                                                $dcvars->{SERVER_IP},
1441                                                $dcvars->{SERVER_IPV6});
1442
1443         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1444
1445         $ctx->{smb_conf_extra_options} = "
1446         max xmit = 32K
1447         server max protocol = SMB2
1448
1449         ntlm auth = ntlmv2-only
1450
1451 [sysvol]
1452         path = $ctx->{statedir}/sysvol
1453         read only = yes
1454
1455 [netlogon]
1456         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1457         read only = no
1458
1459 ";
1460
1461         my $ret = $self->provision_raw_step1($ctx);
1462         unless ($ret) {
1463                 return undef;
1464         }
1465
1466         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1467         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1468         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1469         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1470                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1471         } else {
1472                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1473         }
1474         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1475         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1476         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1477         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
1478         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1479         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1480
1481         unless (system($cmd) == 0) {
1482                 warn("Join failed\n$cmd");
1483                 return undef;
1484         }
1485
1486         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1487         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1488         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1489         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1490                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1491         } else {
1492                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1493         }
1494         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1495         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1496         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1497         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1498         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1499         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
1500
1501         unless (system($cmd) == 0) {
1502                 warn("Join failed\n$cmd");
1503                 return undef;
1504         }
1505
1506         $ret->{PROMOTED_DC_SERVER} = $ret->{SERVER};
1507         $ret->{PROMOTED_DC_SERVER_IP} = $ret->{SERVER_IP};
1508         $ret->{PROMOTED_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1509         $ret->{PROMOTED_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1510
1511         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1512         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1513         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1514         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1515         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1516         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1517
1518         return $ret;
1519 }
1520
1521 sub provision_vampire_dc($$$)
1522 {
1523         my ($self, $prefix, $dcvars, $fl) = @_;
1524         print "PROVISIONING VAMPIRE DC @ FL $fl...\n";
1525         my $name = "localvampiredc";
1526         my $extra_conf = "";
1527
1528         if ($fl == "2000") {
1529                 $name = "vampire2000dc";
1530         } else {
1531                 $extra_conf = "drs: immediate link sync = yes
1532                        drs: max link sync = 250";
1533         }
1534
1535         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1536         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1537                                                $name,
1538                                                $dcvars->{DOMAIN},
1539                                                $dcvars->{REALM},
1540                                                $dcvars->{DOMSID},
1541                                                $fl,
1542                                                $dcvars->{PASSWORD},
1543                                                $dcvars->{SERVER_IP},
1544                                                $dcvars->{SERVER_IPV6});
1545
1546         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1547
1548         $ctx->{smb_conf_extra_options} = "
1549         max xmit = 32K
1550         server max protocol = SMB2
1551
1552         ntlm auth = mschapv2-and-ntlmv2-only
1553         $extra_conf
1554
1555 [sysvol]
1556         path = $ctx->{statedir}/sysvol
1557         read only = yes
1558
1559 [netlogon]
1560         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1561         read only = no
1562
1563 ";
1564
1565         my $ret = $self->provision_raw_step1($ctx);
1566         unless ($ret) {
1567                 return undef;
1568         }
1569
1570         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1571         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1572         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1573         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1574                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1575         } else {
1576                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1577         }
1578         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1579         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1580         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1581         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1582         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1583         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1584         $cmd .= " --backend-store=mdb";
1585
1586         unless (system($cmd) == 0) {
1587                 warn("Join failed\n$cmd");
1588                 return undef;
1589         }
1590
1591         if ($fl == "2000") {
1592                 $ret->{VAMPIRE_2000_DC_SERVER} = $ret->{SERVER};
1593                 $ret->{VAMPIRE_2000_DC_SERVER_IP} = $ret->{SERVER_IP};
1594                 $ret->{VAMPIRE_2000_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1595                 $ret->{VAMPIRE_2000_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1596         } else {
1597                 $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1598                 $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1599                 $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1600                 $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1601         }
1602         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1603         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1604         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1605         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1606         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1607         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1608         $ret->{DC_REALM} = $dcvars->{DC_REALM};
1609
1610         return $ret;
1611 }
1612
1613 sub provision_subdom_dc($$$)
1614 {
1615         my ($self, $prefix, $dcvars) = @_;
1616         print "PROVISIONING SUBDOMAIN DC...\n";
1617
1618         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1619         my $samsid = undef; # TODO pass the domain sid all the way down
1620         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1621                                                "localsubdc",
1622                                                "SAMBASUBDOM",
1623                                                "sub.samba.example.com",
1624                                                $samsid,
1625                                                "2008",
1626                                                $dcvars->{PASSWORD},
1627                                                undef);
1628
1629         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1630
1631         $ctx->{smb_conf_extra_options} = "
1632         max xmit = 32K
1633         server max protocol = SMB2
1634
1635 [sysvol]
1636         path = $ctx->{statedir}/sysvol
1637         read only = yes
1638
1639 [netlogon]
1640         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1641         read only = no
1642
1643 ";
1644
1645         my $ret = $self->provision_raw_step1($ctx);
1646         unless ($ret) {
1647                 return undef;
1648         }
1649
1650         Samba::mk_krb5_conf($ctx);
1651         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
1652
1653         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1654         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1655         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1656         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1657                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1658         } else {
1659                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1660         }
1661         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1662         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1663         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1664         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{dnsname} subdomain ";
1665         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1666         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1667         $cmd .= " --adminpass=$ret->{PASSWORD}";
1668
1669         unless (system($cmd) == 0) {
1670                 warn("Join failed\n$cmd");
1671                 return undef;
1672         }
1673
1674         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1675         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1676         $ret->{SUBDOM_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1677         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1678
1679         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1680         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1681         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1682         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1683         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1684         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1685
1686         return $ret;
1687 }
1688
1689 sub provision_ad_dc_ntvfs($$)
1690 {
1691         my ($self, $prefix) = @_;
1692
1693         # We keep the old 'winbind' name here in server services to
1694         # ensure upgrades which used that name still work with the now
1695         # alias.
1696
1697         print "PROVISIONING AD DC (NTVFS)...\n";
1698         my $extra_conf_options = "netbios aliases = localDC1-a
1699         server services = +winbind -winbindd
1700         ldap server require strong auth = allow_sasl_over_tls
1701         allow nt4 crypto = yes
1702         raw NTLMv2 auth = yes
1703         lsa over netlogon = yes
1704         rpc server port = 1027
1705         auth event notification = true
1706         dsdb event notification = true
1707         dsdb password event notification = true
1708         dsdb group change notification = true
1709         server schannel = auto
1710         ";
1711         my $extra_provision_options = ["--use-ntvfs"];
1712         my $ret = $self->provision($prefix,
1713                                    "domain controller",
1714                                    "localdc",
1715                                    "SAMBADOMAIN",
1716                                    "samba.example.com",
1717                                    "2008",
1718                                    "locDCpass1",
1719                                    undef,
1720                                    undef,
1721                                    $extra_conf_options,
1722                                    "",
1723                                    $extra_provision_options);
1724         unless ($ret) {
1725                 return undef;
1726         }
1727
1728         unless($self->add_wins_config("$prefix/private")) {
1729                 warn("Unable to add wins configuration");
1730                 return undef;
1731         }
1732         $ret->{NETBIOSALIAS} = "localdc1-a";
1733         $ret->{DC_SERVER} = $ret->{SERVER};
1734         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1735         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1736         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1737         $ret->{DC_USERNAME} = $ret->{USERNAME};
1738         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1739         $ret->{DC_REALM} = $ret->{REALM};
1740
1741         return $ret;
1742 }
1743
1744 sub provision_fl2000dc($$)
1745 {
1746         my ($self, $prefix) = @_;
1747
1748         print "PROVISIONING DC WITH FOREST LEVEL 2000...\n";
1749         my $extra_conf_options = "
1750         spnego:simulate_w2k=yes
1751         ntlmssp_server:force_old_spnego=yes
1752 ";
1753         my $extra_provision_options = ["--use-ntvfs"];
1754         # This environment uses plain text secrets
1755         # i.e. secret attributes are not encrypted on disk.
1756         # This allows testing of the --plaintext-secrets option for
1757         # provision
1758         push (@{$extra_provision_options}, "--plaintext-secrets");
1759         my $ret = $self->provision($prefix,
1760                                    "domain controller",
1761                                    "dc5",
1762                                    "SAMBA2000",
1763                                    "samba2000.example.com",
1764                                    "2000",
1765                                    "locDCpass5",
1766                                    undef,
1767                                    undef,
1768                                    $extra_conf_options,
1769                                    "",
1770                                    $extra_provision_options);
1771         unless ($ret) {
1772                 return undef;
1773         }
1774
1775         unless($self->add_wins_config("$prefix/private")) {
1776                 warn("Unable to add wins configuration");
1777                 return undef;
1778         }
1779         $ret->{DC_SERVER} = $ret->{SERVER};
1780         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1781         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1782         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1783         $ret->{DC_USERNAME} = $ret->{USERNAME};
1784         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1785         $ret->{DC_REALM} = $ret->{REALM};
1786
1787         return $ret;
1788 }
1789
1790 sub provision_fl2003dc($$$)
1791 {
1792         my ($self, $prefix, $dcvars) = @_;
1793         my $swiface1 = Samba::get_interface("fakednsforwarder1");
1794         my $swiface2 = Samba::get_interface("fakednsforwarder2");
1795
1796         print "PROVISIONING DC WITH FOREST LEVEL 2003...\n";
1797         my $extra_conf_options = "allow dns updates = nonsecure and secure
1798         dcesrv:header signing = no
1799         dcesrv:max auth states = 0
1800         dns forwarder = 127.0.0.$swiface1 127.0.0.$swiface2";
1801         my $extra_provision_options = ["--use-ntvfs"];
1802         my $ret = $self->provision($prefix,
1803                                    "domain controller",
1804                                    "dc6",
1805                                    "SAMBA2003",
1806                                    "samba2003.example.com",
1807                                    "2003",
1808                                    "locDCpass6",
1809                                    undef,
1810                                    undef,
1811                                    $extra_conf_options,
1812                                    "",
1813                                    $extra_provision_options);
1814         unless (defined $ret) {
1815                 return undef;
1816         }
1817
1818         $ret->{DC_SERVER} = $ret->{SERVER};
1819         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1820         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1821         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1822         $ret->{DC_USERNAME} = $ret->{USERNAME};
1823         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1824         $ret->{DNS_FORWARDER1} = "127.0.0.$swiface1";
1825         $ret->{DNS_FORWARDER2} = "127.0.0.$swiface2";
1826
1827         my @samba_tool_options;
1828         push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
1829         push (@samba_tool_options, "domain");
1830         push (@samba_tool_options, "passwordsettings");
1831         push (@samba_tool_options, "set");
1832         push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
1833         push (@samba_tool_options, "--min-pwd-age=0");
1834         push (@samba_tool_options, "--history-length=1");
1835
1836         my $samba_tool_cmd = join(" ", @samba_tool_options);
1837
1838         unless (system($samba_tool_cmd) == 0) {
1839                 warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
1840                 return undef;
1841         }
1842
1843         unless($self->add_wins_config("$prefix/private")) {
1844                 warn("Unable to add wins configuration");
1845                 return undef;
1846         }
1847
1848         return $ret;
1849 }
1850
1851 sub provision_fl2008r2dc($$$)
1852 {
1853         my ($self, $prefix, $dcvars) = @_;
1854
1855         print "PROVISIONING DC WITH FOREST LEVEL 2008r2...\n";
1856         my $extra_conf_options = "ldap server require strong auth = no";
1857         my $extra_provision_options = ["--use-ntvfs"];
1858         my $ret = $self->provision($prefix,
1859                                    "domain controller",
1860                                    "dc7",
1861                                    "SAMBA2008R2",
1862                                    "samba2008R2.example.com",
1863                                    "2008_R2",
1864                                    "locDCpass7",
1865                                    undef,
1866                                    undef,
1867                                    $extra_conf_options,
1868                                    "",
1869                                    $extra_provision_options);
1870         unless (defined $ret) {
1871                 return undef;
1872         }
1873
1874         unless ($self->add_wins_config("$prefix/private")) {
1875                 warn("Unable to add wins configuration");
1876                 return undef;
1877         }
1878         $ret->{DC_SERVER} = $ret->{SERVER};
1879         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1880         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1881         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1882         $ret->{DC_USERNAME} = $ret->{USERNAME};
1883         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1884         $ret->{DC_REALM} = $ret->{REALM};
1885
1886         return $ret;
1887 }
1888
1889
1890 sub provision_rodc($$$)
1891 {
1892         my ($self, $prefix, $dcvars) = @_;
1893         print "PROVISIONING RODC...\n";
1894
1895         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1896         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1897                                                "rodc",
1898                                                $dcvars->{DOMAIN},
1899                                                $dcvars->{REALM},
1900                                                $dcvars->{DOMSID},
1901                                                "2008",
1902                                                $dcvars->{PASSWORD},
1903                                                $dcvars->{SERVER_IP},
1904                                                $dcvars->{SERVER_IPV6});
1905         unless ($ctx) {
1906                 return undef;
1907         }
1908
1909         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1910
1911         $ctx->{share} = "$ctx->{prefix_abs}/share";
1912         push(@{$ctx->{directories}}, "$ctx->{share}");
1913
1914         $ctx->{smb_conf_extra_options} = "
1915         max xmit = 32K
1916         server max protocol = SMB2
1917         password server = $dcvars->{DC_SERVER}
1918
1919 [sysvol]
1920         path = $ctx->{statedir}/sysvol
1921         read only = yes
1922
1923 [netlogon]
1924         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1925         read only = yes
1926
1927 [tmp]
1928         path = $ctx->{share}
1929         read only = no
1930         posix:sharedelay = 10000
1931         posix:oplocktimeout = 3
1932         posix:writetimeupdatedelay = 50000
1933
1934 ";
1935
1936         my $ret = $self->provision_raw_step1($ctx);
1937         unless ($ret) {
1938                 return undef;
1939         }
1940
1941         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1942         my $cmd = "NSS_WRAPPER_HOSTS='$ret->{NSS_WRAPPER_HOSTS}' ";
1943         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1944         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1945                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1946         } else {
1947                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1948         }
1949         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1950         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1951         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1952         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1953         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1954         $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
1955
1956         unless (system($cmd) == 0) {
1957                 warn("RODC join failed\n$cmd");
1958                 return undef;
1959         }
1960
1961         # This ensures deterministic behaviour for tests that want to have the 'testallowed account'
1962         # user password verified on the RODC
1963         my $testallowed_account = "testallowed account";
1964         $cmd = "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1965         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1966         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1967         $cmd .= "$samba_tool rodc preload '$testallowed_account' $ret->{CONFIGURATION}";
1968         $cmd .= " --server=$dcvars->{DC_SERVER}";
1969
1970         unless (system($cmd) == 0) {
1971                 warn("RODC join failed\n$cmd");
1972                 return undef;
1973         }
1974
1975         # we overwrite the kdc after the RODC join
1976         # so that use the RODC as kdc and test
1977         # the proxy code
1978         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1979         $ctx->{kdc_ipv6} = $ret->{SERVER_IPV6};
1980         Samba::mk_krb5_conf($ctx);
1981         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
1982
1983         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1984         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1985         $ret->{RODC_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1986         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1987
1988         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1989         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1990         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1991         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1992         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1993         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1994
1995         return $ret;
1996 }
1997
1998 sub read_config_h($)
1999 {
2000         my ($name) = @_;
2001         my %ret = {};
2002         open(LF, "<$name") or die("unable to read $name: $!");
2003         while (<LF>) {
2004                 chomp;
2005                 next if not (/^#define /);
2006                 if (/^#define (.*?)[ \t]+(.*?)$/) {
2007                         $ret{$1} = $2;
2008                         next;
2009                 }
2010                 if (/^#define (.*?)[ \t]+$/) {
2011                         $ret{$1} = 1;;
2012                         next;
2013                 }
2014         }
2015         close(LF);
2016         return \%ret;
2017 }
2018
2019 sub provision_ad_dc($$$$$$)
2020 {
2021         my ($self, $prefix, $hostname, $domain, $realm, $smbconf_args,
2022                 $extra_provision_options) = @_;
2023
2024         my $prefix_abs = abs_path($prefix);
2025
2026         my $bindir_abs = abs_path($self->{bindir});
2027         my $lockdir="$prefix_abs/lockdir";
2028         my $conffile="$prefix_abs/etc/smb.conf";
2029
2030         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
2031         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
2032
2033         my $config_h = {};
2034
2035         if (defined($ENV{CONFIG_H})) {
2036                 $config_h = read_config_h($ENV{CONFIG_H});
2037         }
2038
2039         my $password_hash_gpg_key_ids = "password hash gpg key ids = 4952E40301FAB41A";
2040         $password_hash_gpg_key_ids = "" unless defined($config_h->{HAVE_GPGME});
2041
2042         my $extra_smbconf_options = "
2043         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
2044
2045         dbwrap_tdb_mutexes:* = yes
2046         ${require_mutexes}
2047
2048         ${password_hash_gpg_key_ids}
2049
2050         kernel oplocks = no
2051         kernel change notify = no
2052         smb2 leases = no
2053
2054         logging = file
2055         printing = bsd
2056         printcap name = /dev/null
2057
2058         max protocol = SMB3
2059         read only = no
2060
2061         smbd:sharedelay = 100000
2062         smbd:writetimeupdatedelay = 500000
2063         create mask = 755
2064         dos filemode = yes
2065         check parent directory delete on close = yes
2066
2067         dcerpc endpoint servers = -winreg -srvsvc
2068
2069         printcap name = /dev/null
2070
2071         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
2072         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
2073
2074         printing = vlp
2075         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
2076         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
2077         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
2078         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
2079         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
2080         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
2081         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
2082         lpq cache time = 0
2083         print notify backchannel = yes
2084
2085         server schannel = auto
2086         auth event notification = true
2087         dsdb event notification = true
2088         dsdb password event notification = true
2089         dsdb group change notification = true
2090         $smbconf_args
2091 ";
2092
2093         my $extra_smbconf_shares = "
2094
2095 [tmpenc]
2096         copy = tmp
2097         smb encrypt = required
2098
2099 [tmpcase]
2100         copy = tmp
2101         case sensitive = yes
2102
2103 [tmpguest]
2104         copy = tmp
2105         guest ok = yes
2106
2107 [hideunread]
2108         copy = tmp
2109         hide unreadable = yes
2110
2111 [durable]
2112         copy = tmp
2113         kernel share modes = no
2114         kernel oplocks = no
2115         posix locking = no
2116
2117 [print\$]
2118         copy = tmp
2119
2120 [print1]
2121         copy = tmp
2122         printable = yes
2123
2124 [print2]
2125         copy = print1
2126 [print3]
2127         copy = print1
2128 [lp]
2129         copy = print1
2130 ";
2131
2132         push (@{$extra_provision_options}, "--backend-store=mdb");
2133         print "PROVISIONING AD DC...\n";
2134         my $ret = $self->provision($prefix,
2135                                    "domain controller",
2136                                    $hostname,
2137                                    $domain,
2138                                    $realm,
2139                                    "2008",
2140                                    "locDCpass1",
2141                                    undef,
2142                                    undef,
2143                                    $extra_smbconf_options,
2144                                    $extra_smbconf_shares,
2145                                    $extra_provision_options);
2146         unless (defined $ret) {
2147                 return undef;
2148         }
2149
2150         unless($self->add_wins_config("$prefix/private")) {
2151                 warn("Unable to add wins configuration");
2152                 return undef;
2153         }
2154
2155         $ret->{DC_SERVER} = $ret->{SERVER};
2156         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
2157         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
2158         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
2159         $ret->{DC_USERNAME} = $ret->{USERNAME};
2160         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
2161
2162         return $ret;
2163 }
2164
2165 sub provision_chgdcpass($$)
2166 {
2167         my ($self, $prefix) = @_;
2168
2169         print "PROVISIONING CHGDCPASS...\n";
2170         # This environment disallows the use of this password
2171         # (and also removes the default AD complexity checks)
2172         my $unacceptable_password = "widk3Dsle32jxdBdskldsk55klASKQ";
2173         my $extra_smb_conf = "
2174         check password script = $self->{srcdir}/selftest/checkpassword_arg1.sh ${unacceptable_password}
2175         allow dcerpc auth level connect:lsarpc = yes
2176         dcesrv:max auth states = 8
2177 ";
2178         my $extra_provision_options = ["--use-ntvfs"];
2179         push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
2180         my $ret = $self->provision($prefix,
2181                                    "domain controller",
2182                                    "chgdcpass",
2183                                    "CHDCDOMAIN",
2184                                    "chgdcpassword.samba.example.com",
2185                                    "2008",
2186                                    "chgDCpass1",
2187                                    undef,
2188                                    undef,
2189                                    $extra_smb_conf,
2190                                    "",
2191                                    $extra_provision_options);
2192         unless (defined $ret) {
2193                 return undef;
2194         }
2195
2196         unless($self->add_wins_config("$prefix/private")) {
2197                 warn("Unable to add wins configuration");
2198                 return undef;
2199         }
2200         
2201         # Remove secrets.tdb from this environment to test that we
2202         # still start up on systems without the new matching
2203         # secrets.tdb records.
2204         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
2205                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
2206                 return undef;
2207         }
2208             
2209         $ret->{DC_SERVER} = $ret->{SERVER};
2210         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
2211         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
2212         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
2213         $ret->{DC_USERNAME} = $ret->{USERNAME};
2214         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
2215         $ret->{UNACCEPTABLE_PASSWORD} = $unacceptable_password;
2216
2217         return $ret;
2218 }
2219
2220 sub teardown_env_terminate($$)
2221 {
2222         my ($self, $envvars) = @_;
2223         my $pid;
2224
2225         # This should cause samba to terminate gracefully
2226         my $smbcontrol = Samba::bindir_path($self, "smbcontrol");
2227         my $cmd = "";
2228         $cmd .= "$smbcontrol samba shutdown $envvars->{CONFIGURATION}";
2229         my $ret = system($cmd);
2230         if ($ret != 0) {
2231                 warn "'$cmd' failed with '$ret'\n";
2232         }
2233
2234         # This should cause samba to terminate gracefully
2235         close($envvars->{STDIN_PIPE});
2236
2237         $pid = $envvars->{SAMBA_PID};
2238         my $count = 0;
2239         my $childpid;
2240
2241         # This should give it time to write out the gcov data
2242         until ($count > 15) {
2243             if (Samba::cleanup_child($pid, "samba") != 0) {
2244                 return;
2245             }
2246             sleep(1);
2247             $count++;
2248         }
2249
2250         # After 15 Seconds, work out why this thing is still alive
2251         warn "server process $pid took more than $count seconds to exit, showing backtrace:\n";
2252         system("$self->{srcdir}/selftest/gdb_backtrace $pid");
2253
2254         until ($count > 30) {
2255             if (Samba::cleanup_child($pid, "samba") != 0) {
2256                 return;
2257             }
2258             sleep(1);
2259             $count++;
2260         }
2261
2262         if (kill(0, $pid)) {
2263             warn "server process $pid took more than $count seconds to exit, sending SIGTERM\n";
2264             kill "TERM", $pid;
2265         }
2266
2267         until ($count > 40) {
2268             if (Samba::cleanup_child($pid, "samba") != 0) {
2269                 return;
2270             }
2271             sleep(1);
2272             $count++;
2273         }
2274         # If it is still around, kill it
2275         if (kill(0, $pid)) {
2276             warn "server process $pid took more than $count seconds to exit, killing\n with SIGKILL\n";
2277             kill 9, $pid;
2278         }
2279         return;
2280 }
2281
2282 sub teardown_env($$)
2283 {
2284         my ($self, $envvars) = @_;
2285         teardown_env_terminate($self, $envvars);
2286
2287         $self->slapd_stop($envvars) if ($self->{ldap});
2288
2289         print $self->getlog_env($envvars);
2290
2291         return;
2292 }
2293
2294 sub getlog_env($$)
2295 {
2296         my ($self, $envvars) = @_;
2297         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME} pid $envvars->{SAMBA_PID}\n";
2298         my $out = $title;
2299
2300         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
2301
2302         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
2303         while (<LOG>) {
2304                 $out .= $_;
2305         }
2306         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
2307         close(LOG);
2308
2309         return "" if $out eq $title;
2310
2311         return $out;
2312 }
2313
2314 sub check_env($$)
2315 {
2316         my ($self, $envvars) = @_;
2317         my $samba_pid = $envvars->{SAMBA_PID};
2318
2319         if (not defined($samba_pid)) {
2320             return 0;
2321         } elsif ($samba_pid > 0) {
2322             my $childpid = Samba::cleanup_child($samba_pid, "samba");
2323
2324             if ($childpid == 0) {
2325                 return 1;
2326             }
2327             return 0;
2328         } else {
2329             return 1;
2330         }
2331 }
2332
2333 # Declare the environments Samba4 makes available.
2334 # To be set up, they will be called as
2335 #   samba4->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
2336 # The interdependencies between the testenvs are declared below. Some testenvs
2337 # are dependent on another testenv running first, e.g. vampire_dc is dependent
2338 # on ad_dc_ntvfs because vampire_dc joins ad_dc_ntvfs's domain. All DCs are
2339 # dependent on dns_hub, which handles resolving DNS queries for the realm.
2340 %Samba4::ENV_DEPS = (
2341         # name               => [dep_1, dep_2, ...],
2342         dns_hub              => [],
2343         ad_dc_ntvfs          => ["dns_hub"],
2344         ad_dc                => ["dns_hub"],
2345         ad_dc_no_nss         => ["dns_hub"],
2346         ad_dc_no_ntlm        => ["dns_hub"],
2347
2348         fl2008r2dc           => ["ad_dc"],
2349         fl2003dc             => ["ad_dc"],
2350         fl2000dc             => ["dns_hub"],
2351
2352         vampire_2000_dc      => ["fl2000dc"],
2353         vampire_dc           => ["ad_dc_ntvfs"],
2354         promoted_dc          => ["ad_dc_ntvfs"],
2355         subdom_dc            => ["ad_dc_ntvfs"],
2356
2357         rodc                 => ["ad_dc_ntvfs"],
2358         rpc_proxy            => ["ad_dc_ntvfs"],
2359         chgdcpass            => ["dns_hub"],
2360
2361         s4member_dflt_domain => ["ad_dc_ntvfs"],
2362         s4member             => ["ad_dc_ntvfs"],
2363
2364         # envs that test the server process model
2365         proclimitdc          => ["dns_hub"],
2366         preforkrestartdc     => ["dns_hub"],
2367
2368         # backup/restore testenvs
2369         backupfromdc         => ["dns_hub"],
2370         customdc             => ["dns_hub"],
2371         restoredc            => ["backupfromdc"],
2372         renamedc             => ["backupfromdc"],
2373         offlinebackupdc      => ["backupfromdc"],
2374         labdc                => ["backupfromdc"],
2375
2376         none                 => [],
2377 );
2378
2379 sub setup_s4member
2380 {
2381         my ($self, $path, $dc_vars) = @_;
2382
2383         my $env = $self->provision_s4member($path, $dc_vars, "s4member");
2384
2385         if (defined $env) {
2386                 if (not defined($self->check_or_start($env, "standard"))) {
2387                         return undef;
2388                 }
2389         }
2390
2391         return $env;
2392 }
2393
2394 sub setup_s4member_dflt_domain
2395 {
2396         my ($self, $path, $dc_vars) = @_;
2397
2398         my $env = $self->provision_s4member($path, $dc_vars, "s4member_dflt",
2399                                             "winbind use default domain = yes");
2400
2401         if (defined $env) {
2402                 if (not defined($self->check_or_start($env, "standard"))) {
2403                         return undef;
2404                 }
2405         }
2406
2407         return $env;
2408 }
2409
2410 sub setup_rpc_proxy
2411 {
2412         my ($self, $path, $dc_vars) = @_;
2413
2414         my $env = $self->provision_rpc_proxy($path, $dc_vars);
2415
2416         if (defined $env) {
2417                 if (not defined($self->check_or_start($env, "standard"))) {
2418                         return undef;
2419                 }
2420         }
2421         return $env;
2422 }
2423
2424 sub setup_ad_dc_ntvfs
2425 {
2426         my ($self, $path) = @_;
2427
2428         my $env = $self->provision_ad_dc_ntvfs($path);
2429         if (defined $env) {
2430                 if (not defined($self->check_or_start($env, "standard"))) {
2431                     warn("Failed to start ad_dc_ntvfs");
2432                         return undef;
2433                 }
2434         }
2435         return $env;
2436 }
2437
2438 sub setup_chgdcpass
2439 {
2440         my ($self, $path) = @_;
2441
2442         my $env = $self->provision_chgdcpass($path);
2443         if (defined $env) {
2444                 if (not defined($self->check_or_start($env, "standard"))) {
2445                         return undef;
2446                 }
2447         }
2448         return $env;
2449 }
2450
2451 sub setup_fl2000dc
2452 {
2453         my ($self, $path) = @_;
2454
2455         my $env = $self->provision_fl2000dc($path);
2456         if (defined $env) {
2457                 if (not defined($self->check_or_start($env, "standard"))) {
2458                         return undef;
2459                 }
2460         }
2461
2462         return $env;
2463 }
2464
2465 sub setup_fl2003dc
2466 {
2467         my ($self, $path, $dc_vars) = @_;
2468
2469         my $env = $self->provision_fl2003dc($path);
2470
2471         if (defined $env) {
2472                 if (not defined($self->check_or_start($env, "standard"))) {
2473                         return undef;
2474                 }
2475
2476                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2477         }
2478         return $env;
2479 }
2480
2481 sub setup_fl2008r2dc
2482 {
2483         my ($self, $path, $dc_vars) = @_;
2484
2485         my $env = $self->provision_fl2008r2dc($path);
2486
2487         if (defined $env) {
2488                 if (not defined($self->check_or_start($env, "standard"))) {
2489                         return undef;
2490                 }
2491
2492                 my $upn_array = ["$env->{REALM}.upn"];
2493                 my $spn_array = ["$env->{REALM}.spn"];
2494
2495                 $self->setup_namespaces($env, $upn_array, $spn_array);
2496
2497                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2498         }
2499
2500         return $env;
2501 }
2502
2503 sub setup_vampire_dc
2504 {
2505         return setup_generic_vampire_dc(@_, "2008");
2506 }
2507
2508 sub setup_vampire_2000_dc
2509 {
2510         return setup_generic_vampire_dc(@_, "2000");
2511 }
2512
2513 sub setup_generic_vampire_dc
2514 {
2515         my ($self, $path, $dc_vars, $fl) = @_;
2516
2517         my $env = $self->provision_vampire_dc($path, $dc_vars, $fl);
2518
2519         if (defined $env) {
2520                 if (not defined($self->check_or_start($env, "single"))) {
2521                         return undef;
2522                 }
2523
2524                 # force replicated DC to update repsTo/repsFrom
2525                 # for vampired partitions
2526                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2527
2528                 # as 'vampired' dc may add data in its local replica
2529                 # we need to synchronize data between DCs
2530                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2531                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2532                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2533                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2534                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2535                 } else {
2536                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2537                 }
2538                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2539                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2540                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2541                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2542                 $cmd .= " $dc_vars->{CONFIGURATION}";
2543                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2544                 # replicate Configuration NC
2545                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2546                 unless(system($cmd_repl) == 0) {
2547                         warn("Failed to replicate\n$cmd_repl");
2548                         return undef;
2549                 }
2550                 # replicate Default NC
2551                 $cmd_repl = "$cmd \"$base_dn\"";
2552                 unless(system($cmd_repl) == 0) {
2553                         warn("Failed to replicate\n$cmd_repl");
2554                         return undef;
2555                 }
2556
2557                 # Pull in a full set of changes from the main DC
2558                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2559                 $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2560                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2561                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2562                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2563                 } else {
2564                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2565                 }
2566                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2567                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2568                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2569                 $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2570                 $cmd .= " $dc_vars->{CONFIGURATION}";
2571                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2572                 # replicate Configuration NC
2573                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2574                 unless(system($cmd_repl) == 0) {
2575                         warn("Failed to replicate\n$cmd_repl");
2576                         return undef;
2577                 }
2578                 # replicate Default NC
2579                 $cmd_repl = "$cmd \"$base_dn\"";
2580                 unless(system($cmd_repl) == 0) {
2581                         warn("Failed to replicate\n$cmd_repl");
2582                         return undef;
2583                 }
2584         }
2585
2586         return $env;
2587 }
2588
2589 sub setup_promoted_dc
2590 {
2591         my ($self, $path, $dc_vars) = @_;
2592
2593         my $env = $self->provision_promoted_dc($path, $dc_vars);
2594
2595         if (defined $env) {
2596                 if (not defined($self->check_or_start($env, "single"))) {
2597                         return undef;
2598                 }
2599
2600                 # force source and replicated DC to update repsTo/repsFrom
2601                 # for vampired partitions
2602                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2603                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2604                 # as 'vampired' dc may add data in its local replica
2605                 # we need to synchronize data between DCs
2606                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2607                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2608                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2609                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2610                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2611                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2612                 $cmd .= " $dc_vars->{CONFIGURATION}";
2613                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2614                 # replicate Configuration NC
2615                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2616                 unless(system($cmd_repl) == 0) {
2617                         warn("Failed to replicate\n$cmd_repl");
2618                         return undef;
2619                 }
2620                 # replicate Default NC
2621                 $cmd_repl = "$cmd \"$base_dn\"";
2622                 unless(system($cmd_repl) == 0) {
2623                         warn("Failed to replicate\n$cmd_repl");
2624                         return undef;
2625                 }
2626         }
2627
2628         return $env;
2629 }
2630
2631 sub setup_subdom_dc
2632 {
2633         my ($self, $path, $dc_vars) = @_;
2634
2635         my $env = $self->provision_subdom_dc($path, $dc_vars);
2636
2637         if (defined $env) {
2638                 if (not defined($self->check_or_start($env, "single"))) {
2639                         return undef;
2640                 }
2641
2642                 # force replicated DC to update repsTo/repsFrom
2643                 # for primary domain partitions
2644                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2645                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2646                 # as 'subdomain' dc may add data in its local replica
2647                 # we need to synchronize data between DCs
2648                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
2649                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2650                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2651                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2652                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2653                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2654                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
2655                 $cmd .= " $dc_vars->{CONFIGURATION}";
2656                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2657                 # replicate Configuration NC
2658                 my $cmd_repl = "$cmd \"$config_dn\"";
2659                 unless(system($cmd_repl) == 0) {
2660                         warn("Failed to replicate\n$cmd_repl");
2661                         return undef;
2662                 }
2663                 # replicate Default NC
2664                 $cmd_repl = "$cmd \"$base_dn\"";
2665                 unless(system($cmd_repl) == 0) {
2666                         warn("Failed to replicate\n$cmd_repl");
2667                         return undef;
2668                 }
2669         }
2670
2671         return $env;
2672 }
2673
2674 sub setup_rodc
2675 {
2676         my ($self, $path, $dc_vars) = @_;
2677
2678         my $env = $self->provision_rodc($path, $dc_vars);
2679
2680         unless ($env) {
2681                 return undef;
2682         }
2683
2684         if (not defined($self->check_or_start($env, "standard"))) {
2685             return undef;
2686         }
2687
2688         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2689         my $cmd = "";
2690
2691         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2692         $cmd .= "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2693         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2694         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2695         $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2696         $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2697         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2698         $cmd .= " $dc_vars->{CONFIGURATION}";
2699         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2700         # replicate Configuration NC
2701         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2702         unless(system($cmd_repl) == 0) {
2703             warn("Failed to replicate\n$cmd_repl");
2704             return undef;
2705         }
2706         # replicate Default NC
2707         $cmd_repl = "$cmd \"$base_dn\"";
2708         unless(system($cmd_repl) == 0) {
2709             warn("Failed to replicate\n$cmd_repl");
2710             return undef;
2711         }
2712
2713         return $env;
2714 }
2715
2716 sub setup_ad_dc
2717 {
2718         my ($self, $path) = @_;
2719
2720         # If we didn't build with ADS, pretend this env was never available
2721         if (not $self->{target3}->have_ads()) {
2722                return "UNKNOWN";
2723         }
2724
2725         my $env = $self->provision_ad_dc($path, "addc", "ADDOMAIN",
2726                                          "addom.samba.example.com", "", undef);
2727         unless ($env) {
2728                 return undef;
2729         }
2730
2731         if (not defined($self->check_or_start($env, "prefork"))) {
2732             return undef;
2733         }
2734
2735         my $upn_array = ["$env->{REALM}.upn"];
2736         my $spn_array = ["$env->{REALM}.spn"];
2737
2738         $self->setup_namespaces($env, $upn_array, $spn_array);
2739
2740         return $env;
2741 }
2742
2743 sub setup_ad_dc_no_nss
2744 {
2745         my ($self, $path) = @_;
2746
2747         # If we didn't build with ADS, pretend this env was never available
2748         if (not $self->{target3}->have_ads()) {
2749                return "UNKNOWN";
2750         }
2751
2752         my $env = $self->provision_ad_dc($path, "addc_no_nss", "ADNONSSDOMAIN",
2753                                          "adnonssdom.samba.example.com", "", undef);
2754         unless ($env) {
2755                 return undef;
2756         }
2757
2758         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2759         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2760
2761         if (not defined($self->check_or_start($env, "single"))) {
2762             return undef;
2763         }
2764
2765         my $upn_array = ["$env->{REALM}.upn"];
2766         my $spn_array = ["$env->{REALM}.spn"];
2767
2768         $self->setup_namespaces($env, $upn_array, $spn_array);
2769
2770         return $env;
2771 }
2772
2773 sub setup_ad_dc_no_ntlm
2774 {
2775         my ($self, $path) = @_;
2776
2777         # If we didn't build with ADS, pretend this env was never available
2778         if (not $self->{target3}->have_ads()) {
2779                return "UNKNOWN";
2780         }
2781
2782         my $env = $self->provision_ad_dc($path, "addc_no_ntlm", "ADNONTLMDOMAIN",
2783                                          "adnontlmdom.samba.example.com",
2784                                          "ntlm auth = disabled", undef);
2785         unless ($env) {
2786                 return undef;
2787         }
2788
2789         if (not defined($self->check_or_start($env, "prefork"))) {
2790             return undef;
2791         }
2792
2793         my $upn_array = ["$env->{REALM}.upn"];
2794         my $spn_array = ["$env->{REALM}.spn"];
2795
2796         $self->setup_namespaces($env, $upn_array, $spn_array);
2797
2798         return $env;
2799 }
2800
2801 #
2802 # AD DC test environment used solely to test pre-fork process restarts.
2803 # As processes get killed off and restarted it should not be used for other
2804 sub setup_preforkrestartdc
2805 {
2806         my ($self, $path) = @_;
2807
2808         # If we didn't build with ADS, pretend this env was never available
2809         if (not $self->{target3}->have_ads()) {
2810                return "UNKNOWN";
2811         }
2812
2813         # note DC name must be <= 15 chars so we use 'prockill' instead of
2814         # 'preforkrestart'
2815         my $env = $self->provision_ad_dc(
2816                 $path,
2817                 "prockilldc",
2818                 "PROCKILLDOMAIN",
2819                 "prockilldom.samba.example.com",
2820                 "prefork backoff increment = 5\nprefork maximum backoff=10");
2821         unless ($env) {
2822                 return undef;
2823         }
2824
2825         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2826         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2827
2828         if (not defined($self->check_or_start($env, "prefork"))) {
2829             return undef;
2830         }
2831
2832         my $upn_array = ["$env->{REALM}.upn"];
2833         my $spn_array = ["$env->{REALM}.spn"];
2834
2835         $self->setup_namespaces($env, $upn_array, $spn_array);
2836
2837         return $env;
2838 }
2839
2840 #
2841 # ad_dc test environment used solely to test standard process model connection
2842 # process limits. As the limit is set artificially low it should not be used
2843 # for other tests.
2844 sub setup_proclimitdc
2845 {
2846         my ($self, $path) = @_;
2847
2848         # If we didn't build with ADS, pretend this env was never available
2849         if (not $self->{target3}->have_ads()) {
2850                return "UNKNOWN";
2851         }
2852
2853         my $env = $self->provision_ad_dc(
2854                 $path,
2855                 "proclimitdc",
2856                 "PROCLIMITDOM",
2857                 "proclimit.samba.example.com",
2858                 "max smbd processes = 20");
2859         unless ($env) {
2860                 return undef;
2861         }
2862
2863         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2864         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2865
2866         if (not defined($self->check_or_start($env, "standard"))) {
2867             return undef;
2868         }
2869
2870         my $upn_array = ["$env->{REALM}.upn"];
2871         my $spn_array = ["$env->{REALM}.spn"];
2872
2873         $self->setup_namespaces($env, $upn_array, $spn_array);
2874
2875         return $env;
2876 }
2877
2878 # Sets up a DC that's solely used to do a domain backup from. We then use the
2879 # backupfrom-DC to create the restore-DC - this proves that the backup/restore
2880 # process will create a Samba DC that will actually start up.
2881 # We don't use the backup-DC for anything else because its domain will conflict
2882 # with the restore DC.
2883 sub setup_backupfromdc
2884 {
2885         my ($self, $path) = @_;
2886
2887         # If we didn't build with ADS, pretend this env was never available
2888         if (not $self->{target3}->have_ads()) {
2889                return "UNKNOWN";
2890         }
2891
2892         my $provision_args = ["--site=Backup-Site"];
2893
2894         my $env = $self->provision_ad_dc($path, "backupfromdc", "BACKUPDOMAIN",
2895                                          "backupdom.samba.example.com",
2896                                          "samba kcc command = /bin/true",
2897                                          $provision_args);
2898         unless ($env) {
2899                 return undef;
2900         }
2901
2902         if (not defined($self->check_or_start($env))) {
2903             return undef;
2904         }
2905
2906         my $upn_array = ["$env->{REALM}.upn"];
2907         my $spn_array = ["$env->{REALM}.spn"];
2908
2909         $self->setup_namespaces($env, $upn_array, $spn_array);
2910
2911         return $env;
2912 }
2913
2914 # returns the server/user-auth params needed to run an online backup cmd
2915 sub get_backup_server_args
2916 {
2917         # dcvars contains the env info for the backup DC testenv
2918         my ($self, $dcvars) = @_;
2919         my $server = $dcvars->{DC_SERVER_IP};
2920         my $server_args = "--server=$server ";
2921         $server_args .= "-U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
2922         $server_args .= " $dcvars->{CONFIGURATION}";
2923
2924         return $server_args;
2925 }
2926
2927 # Creates a backup of a running testenv DC
2928 sub create_backup
2929 {
2930         # note: dcvars contains the env info for the backup DC testenv
2931         my ($self, $env, $dcvars, $backupdir, $backup_cmd) = @_;
2932
2933         # get all the env variables we pass in with the samba-tool command
2934         my $cmd_env = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2935         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2936         if (defined($env->{RESOLV_WRAPPER_CONF})) {
2937                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2938         } else {
2939                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2940         }
2941         # Note: use the backupfrom-DC's krb5.conf to do the backup
2942         $cmd_env .= " KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
2943         $cmd_env .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2944
2945         # use samba-tool to create a backup from the 'backupfromdc' DC
2946         my $cmd = "";
2947         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2948
2949         $cmd .= "$cmd_env $samba_tool domain backup $backup_cmd";
2950         $cmd .= " --targetdir=$backupdir";
2951
2952         print "Executing: $cmd\n";
2953         unless(system($cmd) == 0) {
2954                 warn("Failed to create backup using: \n$cmd");
2955                 return undef;
2956         }
2957
2958         # get the name of the backup file created
2959         opendir(DIR, $backupdir);
2960         my @files = grep(/\.tar/, readdir(DIR));
2961         closedir(DIR);
2962
2963         if(scalar @files != 1) {
2964                 warn("Backup file not found in directory $backupdir\n");
2965                 return undef;
2966         }
2967         my $backup_file = "$backupdir/$files[0]";
2968         print "Using backup file $backup_file...\n";
2969
2970         return $backup_file;
2971 }
2972
2973 # Restores a backup-file to populate a testenv for a new DC
2974 sub restore_backup_file
2975 {
2976         my ($self, $backup_file, $restore_opts, $restoredir, $smbconf) = @_;
2977
2978         # pass the restore command the testenv's smb.conf that we've already
2979         # generated. But move it to a temp-dir first, so that the restore doesn't
2980         # overwrite it
2981         my $tmpdir = File::Temp->newdir();
2982         my $tmpconf = "$tmpdir/smb.conf";
2983         my $cmd = "cp $smbconf $tmpconf";
2984         unless(system($cmd) == 0) {
2985                 warn("Failed to backup smb.conf using: \n$cmd");
2986                 return -1;
2987         }
2988
2989         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2990         $cmd = "$samba_tool domain backup restore --backup-file=$backup_file";
2991         $cmd .= " --targetdir=$restoredir $restore_opts --configfile=$tmpconf";
2992
2993         print "Executing: $cmd\n";
2994         unless(system($cmd) == 0) {
2995                 warn("Failed to restore backup using: \n$cmd");
2996                 return -1;
2997         }
2998
2999         print "Restore complete\n";
3000         return 0
3001 }
3002
3003 # sets up the initial directory and returns the new testenv's env info
3004 # (without actually doing a 'domain join')
3005 sub prepare_dc_testenv
3006 {
3007         my ($self, $prefix, $dcname, $domain, $realm,
3008                 $password, $conf_options) = @_;
3009
3010         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
3011                                                $dcname,
3012                                                $domain,
3013                                                $realm,
3014                                                undef,
3015                                                "2008",
3016                                                $password,
3017                                                undef,
3018                                                undef);
3019
3020         # the restore uses a slightly different state-dir location to other testenvs
3021         $ctx->{statedir} = "$ctx->{prefix_abs}/state";
3022         push(@{$ctx->{directories}}, "$ctx->{statedir}");
3023
3024         # add support for sysvol/netlogon/tmp shares
3025         $ctx->{share} = "$ctx->{prefix_abs}/share";
3026         push(@{$ctx->{directories}}, "$ctx->{share}");
3027         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
3028
3029         $ctx->{smb_conf_extra_options} = "
3030         $conf_options
3031         max xmit = 32K
3032         server max protocol = SMB2
3033         samba kcc command = /bin/true
3034         xattr_tdb:file = $ctx->{statedir}/xattr.tdb
3035
3036 [sysvol]
3037         path = $ctx->{statedir}/sysvol
3038         read only = no
3039
3040 [netlogon]
3041         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
3042         read only = no
3043
3044 [tmp]
3045         path = $ctx->{share}
3046         read only = no
3047         posix:sharedelay = 10000
3048         posix:oplocktimeout = 3
3049         posix:writetimeupdatedelay = 50000
3050
3051 [test1]
3052         path = $ctx->{share}/test1
3053         read only = no
3054         posix:sharedelay = 100000
3055         posix:oplocktimeout = 3
3056         posix:writetimeupdatedelay = 500000
3057 ";
3058
3059         my $env = $self->provision_raw_step1($ctx);
3060
3061         $env->{DC_SERVER} = $env->{SERVER};
3062         $env->{DC_SERVER_IP} = $env->{SERVER_IP};
3063         $env->{DC_SERVER_IPV6} = $env->{SERVER_IPV6};
3064         $env->{DC_NETBIOSNAME} = $env->{NETBIOSNAME};
3065         $env->{DC_USERNAME} = $env->{USERNAME};
3066         $env->{DC_PASSWORD} = $env->{PASSWORD};
3067
3068     return ($env, $ctx);
3069 }
3070
3071
3072 # Set up a DC testenv solely by using the samba-tool domain backup/restore
3073 # commands. This proves that we can backup an online DC ('backupfromdc') and
3074 # use the backup file to create a valid, working samba DC.
3075 sub setup_restoredc
3076 {
3077         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3078         my ($self, $prefix, $dcvars) = @_;
3079         print "Preparing RESTORE DC...\n";
3080
3081         # we arbitrarily designate the restored DC as having SMBv1 disabled
3082         my $extra_conf = "
3083         server min protocol = SMB2
3084         client min protocol = SMB2";
3085
3086         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "restoredc",
3087                                                     $dcvars->{DOMAIN},
3088                                                     $dcvars->{REALM},
3089                                                     $dcvars->{PASSWORD},
3090                                                     $extra_conf);
3091
3092         # create a backup of the 'backupfromdc'
3093         my $backupdir = File::Temp->newdir();
3094         my $server_args = $self->get_backup_server_args($dcvars);
3095         my $backup_args = "online $server_args";
3096         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3097                                                $backup_args);
3098         unless($backup_file) {
3099                 return undef;
3100         }
3101
3102         # restore the backup file to populate the restore-DC testenv
3103         my $restore_dir = abs_path($prefix);
3104         my $ret = $self->restore_backup_file($backup_file,
3105                                              "--newservername=$env->{SERVER}",
3106                                              $restore_dir, $env->{SERVERCONFFILE});
3107         unless ($ret == 0) {
3108                 return undef;
3109         }
3110
3111         # start samba for the restored DC
3112         if (not defined($self->check_or_start($env))) {
3113             return undef;
3114         }
3115
3116         return $env;
3117 }
3118
3119 # Set up a DC testenv solely by using the 'samba-tool domain backup rename' and
3120 # restore commands. This proves that we can backup and rename an online DC
3121 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
3122 sub setup_renamedc
3123 {
3124         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3125         my ($self, $prefix, $dcvars) = @_;
3126         print "Preparing RENAME DC...\n";
3127
3128         my $realm = "renamedom.samba.example.com";
3129         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "renamedc",
3130                                                     "RENAMEDOMAIN", $realm,
3131                                                     $dcvars->{PASSWORD}, "");
3132
3133         # create a backup of the 'backupfromdc' which renames the domain
3134         my $backupdir = File::Temp->newdir();
3135         my $server_args = $self->get_backup_server_args($dcvars);
3136         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
3137         $backup_args .= " --backend-store=tdb";
3138         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3139