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