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