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