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