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