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