f2635e574cdf55a81cba91824331d3b3d11a668e
[amitay/samba.git] / selftest / target / Samba4.pm
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
5
6 # NOTE: Refer to the README for more details about the various testenvs,
7 # and tips about adding new testenvs.
8
9 package Samba4;
10
11 use strict;
12 use Cwd qw(abs_path);
13 use FindBin qw($RealBin);
14 use POSIX;
15 use SocketWrapper;
16 use target::Samba;
17 use target::Samba3;
18 use Archive::Tar;
19 use File::Path 'make_path';
20
21 sub new($$$$$) {
22         my ($classname, $bindir, $ldap, $srcdir, $server_maxtime) = @_;
23
24         my $self = {
25                 vars => {},
26                 ldap => $ldap,
27                 bindir => $bindir,
28                 srcdir => $srcdir,
29                 server_maxtime => $server_maxtime,
30                 target3 => new Samba3($bindir, $srcdir, $server_maxtime)
31         };
32         bless $self;
33         return $self;
34 }
35
36 sub scriptdir_path($$) {
37         my ($self, $path) = @_;
38         return "$self->{srcdir}/source4/scripting/$path";
39 }
40
41 sub openldap_start($$$) {
42 }
43
44 sub slapd_start($$)
45 {
46         my $count = 0;
47         my ($self, $env_vars, $STDIN_READER) = @_;
48         my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
49
50         my $uri = $env_vars->{LDAP_URI};
51
52         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
53             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
54             return 1;
55         }
56         # running slapd in the background means it stays in the same process group, so it can be
57         # killed by timelimit
58         my $pid = fork();
59         if ($pid == 0) {
60                 open STDOUT, ">$env_vars->{LDAPDIR}/logs";
61                 open STDERR, '>&STDOUT';
62                 close($env_vars->{STDIN_PIPE});
63                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
64
65                 if ($self->{ldap} eq "fedora-ds") {
66                         exec("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd", "-D", $env_vars->{FEDORA_DS_DIR}, "-d0", "-i", $env_vars->{FEDORA_DS_PIDFILE});
67                 } elsif ($self->{ldap} eq "openldap") {
68                         exec($ENV{OPENLDAP_SLAPD}, "-dnone", "-F", $env_vars->{SLAPD_CONF_D}, "-h", $uri);
69                 }
70                 die("Unable to start slapd: $!");
71         }
72         $env_vars->{SLAPD_PID} = $pid;
73         sleep(1);
74         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
75                 $count++;
76                 if ($count > 40) {
77                         $self->slapd_stop($env_vars);
78                         return 0;
79                 }
80                 sleep(1);
81         }
82         return 1;
83 }
84
85 sub slapd_stop($$)
86 {
87         my ($self, $envvars) = @_;
88         kill 9, $envvars->{SLAPD_PID};
89         return 1;
90 }
91
92 sub check_or_start($$$)
93 {
94         my ($self, $env_vars, $process_model) = @_;
95         my $STDIN_READER;
96
97         my $env_ok = $self->check_env($env_vars);
98         if ($env_ok) {
99                 return $env_vars->{SAMBA_PID};
100         } elsif (defined($env_vars->{SAMBA_PID})) {
101                 warn("SAMBA PID $env_vars->{SAMBA_PID} is not running (died)");
102                 return undef;
103         }
104
105         # use a pipe for stdin in the child processes. This allows
106         # those processes to monitor the pipe for EOF to ensure they
107         # exit when the test script exits
108         pipe($STDIN_READER, $env_vars->{STDIN_PIPE});
109
110         # Start slapd before samba, but with the fifo on stdin
111         if (defined($self->{ldap})) {
112                 unless($self->slapd_start($env_vars, $STDIN_READER)) {
113                         warn("couldn't start slapd (main run)");
114                         return undef;
115                 }
116         }
117
118         print "STARTING SAMBA...\n";
119         my $pid = fork();
120         if ($pid == 0) {
121                 # we want out from samba to go to the log file, but also
122                 # to the users terminal when running 'make test' on the command
123                 # line. This puts it on stderr on the terminal
124                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
125                 open STDERR, '>&STDOUT';
126
127                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
128
129                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
130                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.samba";
131                 if (defined($ENV{MITKRB5})) {
132                         $ENV{KRB5_KDC_PROFILE} = $env_vars->{MITKDC_CONFIG};
133                 }
134                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
135                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
136
137                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
138                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
139                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
140                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
141                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
142                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
143
144                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
145                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
146                 } else {
147                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
148                 }
149                 $ENV{RESOLV_CONF} = $env_vars->{RESOLV_CONF};
150
151                 $ENV{UID_WRAPPER} = "1";
152                 $ENV{UID_WRAPPER_ROOT} = "1";
153
154                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "samba");
155                 my @preargs = ();
156                 my @optargs = ();
157                 if (defined($ENV{SAMBA_OPTIONS})) {
158                         @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
159                 }
160                 if(defined($ENV{SAMBA_VALGRIND})) {
161                         @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
162                 }
163
164                 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         close($envvars->{STDIN_PIPE});
2226
2227         $pid = $envvars->{SAMBA_PID};
2228         my $count = 0;
2229         my $childpid;
2230
2231         # This should give it time to write out the gcov data
2232         until ($count > 15) {
2233             if (Samba::cleanup_child($pid, "samba") != 0) {
2234                 return;
2235             }
2236             sleep(1);
2237             $count++;
2238         }
2239
2240         # After 15 Seconds, work out why this thing is still alive
2241         warn "server process $pid took more than $count seconds to exit, showing backtrace:\n";
2242         system("$self->{srcdir}/selftest/gdb_backtrace $pid");
2243
2244         until ($count > 30) {
2245             if (Samba::cleanup_child($pid, "samba") != 0) {
2246                 return;
2247             }
2248             sleep(1);
2249             $count++;
2250         }
2251
2252         if (kill(0, $pid)) {
2253             warn "server process $pid took more than $count seconds to exit, sending SIGTERM\n";
2254             kill "TERM", $pid;
2255         }
2256
2257         until ($count > 40) {
2258             if (Samba::cleanup_child($pid, "samba") != 0) {
2259                 return;
2260             }
2261             sleep(1);
2262             $count++;
2263         }
2264         # If it is still around, kill it
2265         if (kill(0, $pid)) {
2266             warn "server process $pid took more than $count seconds to exit, killing\n with SIGKILL\n";
2267             kill 9, $pid;
2268         }
2269         return;
2270 }
2271
2272 sub teardown_env($$)
2273 {
2274         my ($self, $envvars) = @_;
2275         teardown_env_terminate($self, $envvars);
2276
2277         $self->slapd_stop($envvars) if ($self->{ldap});
2278
2279         print $self->getlog_env($envvars);
2280
2281         return;
2282 }
2283
2284 sub getlog_env($$)
2285 {
2286         my ($self, $envvars) = @_;
2287         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME} pid $envvars->{SAMBA_PID}\n";
2288         my $out = $title;
2289
2290         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
2291
2292         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
2293         while (<LOG>) {
2294                 $out .= $_;
2295         }
2296         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
2297         close(LOG);
2298
2299         return "" if $out eq $title;
2300
2301         return $out;
2302 }
2303
2304 sub check_env($$)
2305 {
2306         my ($self, $envvars) = @_;
2307         my $samba_pid = $envvars->{SAMBA_PID};
2308
2309         if (not defined($samba_pid)) {
2310             return 0;
2311         } elsif ($samba_pid > 0) {
2312             my $childpid = Samba::cleanup_child($samba_pid, "samba");
2313
2314             if ($childpid == 0) {
2315                 return 1;
2316             }
2317             return 0;
2318         } else {
2319             return 1;
2320         }
2321 }
2322
2323 # Declare the environments Samba4 makes available.
2324 # To be set up, they will be called as
2325 #   samba4->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
2326 %Samba4::ENV_DEPS = (
2327         # name               => [dep_1, dep_2, ...],
2328         dns_hub              => [],
2329         ad_dc_ntvfs          => ["dns_hub"],
2330         ad_dc                => ["dns_hub"],
2331         ad_dc_no_nss         => ["dns_hub"],
2332         ad_dc_no_ntlm        => ["dns_hub"],
2333         backupfromdc         => ["dns_hub"],
2334         customdc             => ["dns_hub"],
2335         preforkrestartdc     => ["dns_hub"],
2336
2337         fl2008r2dc           => ["ad_dc"],
2338         fl2003dc             => ["ad_dc"],
2339         fl2000dc             => ["dns_hub"],
2340
2341         vampire_2000_dc      => ["fl2000dc"],
2342         vampire_dc           => ["ad_dc_ntvfs"],
2343         promoted_dc          => ["ad_dc_ntvfs"],
2344         subdom_dc            => ["ad_dc_ntvfs"],
2345
2346         rodc                 => ["ad_dc_ntvfs"],
2347         rpc_proxy            => ["ad_dc_ntvfs"],
2348         chgdcpass            => ["dns_hub"],
2349
2350         s4member_dflt_domain => ["ad_dc_ntvfs"],
2351         s4member             => ["ad_dc_ntvfs"],
2352
2353         restoredc            => ["backupfromdc"],
2354         renamedc             => ["backupfromdc"],
2355         offlinebackupdc      => ["backupfromdc"],
2356         labdc                => ["backupfromdc"],
2357         proclimitdc          => [],
2358
2359         none                 => [],
2360 );
2361
2362 sub setup_s4member
2363 {
2364         my ($self, $path, $dc_vars) = @_;
2365
2366         my $env = $self->provision_s4member($path, $dc_vars, "s4member");
2367
2368         if (defined $env) {
2369                 if (not defined($self->check_or_start($env, "standard"))) {
2370                         return undef;
2371                 }
2372         }
2373
2374         return $env;
2375 }
2376
2377 sub setup_s4member_dflt_domain
2378 {
2379         my ($self, $path, $dc_vars) = @_;
2380
2381         my $env = $self->provision_s4member($path, $dc_vars, "s4member_dflt",
2382                                             "winbind use default domain = yes");
2383
2384         if (defined $env) {
2385                 if (not defined($self->check_or_start($env, "standard"))) {
2386                         return undef;
2387                 }
2388         }
2389
2390         return $env;
2391 }
2392
2393 sub setup_rpc_proxy
2394 {
2395         my ($self, $path, $dc_vars) = @_;
2396
2397         my $env = $self->provision_rpc_proxy($path, $dc_vars);
2398
2399         if (defined $env) {
2400                 if (not defined($self->check_or_start($env, "standard"))) {
2401                         return undef;
2402                 }
2403         }
2404         return $env;
2405 }
2406
2407 sub setup_ad_dc_ntvfs
2408 {
2409         my ($self, $path) = @_;
2410
2411         my $env = $self->provision_ad_dc_ntvfs($path);
2412         if (defined $env) {
2413                 if (not defined($self->check_or_start($env, "standard"))) {
2414                     warn("Failed to start ad_dc_ntvfs");
2415                         return undef;
2416                 }
2417         }
2418         return $env;
2419 }
2420
2421 sub setup_chgdcpass
2422 {
2423         my ($self, $path) = @_;
2424
2425         my $env = $self->provision_chgdcpass($path);
2426         if (defined $env) {
2427                 if (not defined($self->check_or_start($env, "standard"))) {
2428                         return undef;
2429                 }
2430         }
2431         return $env;
2432 }
2433
2434 sub setup_fl2000dc
2435 {
2436         my ($self, $path) = @_;
2437
2438         my $env = $self->provision_fl2000dc($path);
2439         if (defined $env) {
2440                 if (not defined($self->check_or_start($env, "standard"))) {
2441                         return undef;
2442                 }
2443         }
2444
2445         return $env;
2446 }
2447
2448 sub setup_fl2003dc
2449 {
2450         my ($self, $path, $dc_vars) = @_;
2451
2452         my $env = $self->provision_fl2003dc($path);
2453
2454         if (defined $env) {
2455                 if (not defined($self->check_or_start($env, "standard"))) {
2456                         return undef;
2457                 }
2458
2459                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2460         }
2461         return $env;
2462 }
2463
2464 sub setup_fl2008r2dc
2465 {
2466         my ($self, $path, $dc_vars) = @_;
2467
2468         my $env = $self->provision_fl2008r2dc($path);
2469
2470         if (defined $env) {
2471                 if (not defined($self->check_or_start($env, "standard"))) {
2472                         return undef;
2473                 }
2474
2475                 my $upn_array = ["$env->{REALM}.upn"];
2476                 my $spn_array = ["$env->{REALM}.spn"];
2477
2478                 $self->setup_namespaces($env, $upn_array, $spn_array);
2479
2480                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2481         }
2482
2483         return $env;
2484 }
2485
2486 sub setup_vampire_dc
2487 {
2488         return setup_generic_vampire_dc(@_, "2008");
2489 }
2490
2491 sub setup_vampire_2000_dc
2492 {
2493         return setup_generic_vampire_dc(@_, "2000");
2494 }
2495
2496 sub setup_generic_vampire_dc
2497 {
2498         my ($self, $path, $dc_vars, $fl) = @_;
2499
2500         my $env = $self->provision_vampire_dc($path, $dc_vars, $fl);
2501
2502         if (defined $env) {
2503                 if (not defined($self->check_or_start($env, "single"))) {
2504                         return undef;
2505                 }
2506
2507                 # force replicated DC to update repsTo/repsFrom
2508                 # for vampired partitions
2509                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2510
2511                 # as 'vampired' dc may add data in its local replica
2512                 # we need to synchronize data between DCs
2513                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2514                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2515                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2516                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2517                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2518                 } else {
2519                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2520                 }
2521                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2522                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2523                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2524                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2525                 $cmd .= " $dc_vars->{CONFIGURATION}";
2526                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2527                 # replicate Configuration NC
2528                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2529                 unless(system($cmd_repl) == 0) {
2530                         warn("Failed to replicate\n$cmd_repl");
2531                         return undef;
2532                 }
2533                 # replicate Default NC
2534                 $cmd_repl = "$cmd \"$base_dn\"";
2535                 unless(system($cmd_repl) == 0) {
2536                         warn("Failed to replicate\n$cmd_repl");
2537                         return undef;
2538                 }
2539
2540                 # Pull in a full set of changes from the main DC
2541                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2542                 $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2543                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2544                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2545                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2546                 } else {
2547                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2548                 }
2549                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2550                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2551                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2552                 $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2553                 $cmd .= " $dc_vars->{CONFIGURATION}";
2554                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2555                 # replicate Configuration NC
2556                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2557                 unless(system($cmd_repl) == 0) {
2558                         warn("Failed to replicate\n$cmd_repl");
2559                         return undef;
2560                 }
2561                 # replicate Default NC
2562                 $cmd_repl = "$cmd \"$base_dn\"";
2563                 unless(system($cmd_repl) == 0) {
2564                         warn("Failed to replicate\n$cmd_repl");
2565                         return undef;
2566                 }
2567         }
2568
2569         return $env;
2570 }
2571
2572 sub setup_promoted_dc
2573 {
2574         my ($self, $path, $dc_vars) = @_;
2575
2576         my $env = $self->provision_promoted_dc($path, $dc_vars);
2577
2578         if (defined $env) {
2579                 if (not defined($self->check_or_start($env, "single"))) {
2580                         return undef;
2581                 }
2582
2583                 # force source and replicated DC to update repsTo/repsFrom
2584                 # for vampired partitions
2585                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2586                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2587                 # as 'vampired' dc may add data in its local replica
2588                 # we need to synchronize data between DCs
2589                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2590                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2591                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2592                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2593                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2594                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2595                 $cmd .= " $dc_vars->{CONFIGURATION}";
2596                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2597                 # replicate Configuration NC
2598                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2599                 unless(system($cmd_repl) == 0) {
2600                         warn("Failed to replicate\n$cmd_repl");
2601                         return undef;
2602                 }
2603                 # replicate Default NC
2604                 $cmd_repl = "$cmd \"$base_dn\"";
2605                 unless(system($cmd_repl) == 0) {
2606                         warn("Failed to replicate\n$cmd_repl");
2607                         return undef;
2608                 }
2609         }
2610
2611         return $env;
2612 }
2613
2614 sub setup_subdom_dc
2615 {
2616         my ($self, $path, $dc_vars) = @_;
2617
2618         my $env = $self->provision_subdom_dc($path, $dc_vars);
2619
2620         if (defined $env) {
2621                 if (not defined($self->check_or_start($env, "single"))) {
2622                         return undef;
2623                 }
2624
2625                 # force replicated DC to update repsTo/repsFrom
2626                 # for primary domain partitions
2627                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2628                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2629                 # as 'subdomain' dc may add data in its local replica
2630                 # we need to synchronize data between DCs
2631                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
2632                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2633                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2634                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2635                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2636                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2637                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
2638                 $cmd .= " $dc_vars->{CONFIGURATION}";
2639                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2640                 # replicate Configuration NC
2641                 my $cmd_repl = "$cmd \"$config_dn\"";
2642                 unless(system($cmd_repl) == 0) {
2643                         warn("Failed to replicate\n$cmd_repl");
2644                         return undef;
2645                 }
2646                 # replicate Default NC
2647                 $cmd_repl = "$cmd \"$base_dn\"";
2648                 unless(system($cmd_repl) == 0) {
2649                         warn("Failed to replicate\n$cmd_repl");
2650                         return undef;
2651                 }
2652         }
2653
2654         return $env;
2655 }
2656
2657 sub setup_rodc
2658 {
2659         my ($self, $path, $dc_vars) = @_;
2660
2661         my $env = $self->provision_rodc($path, $dc_vars);
2662
2663         unless ($env) {
2664                 return undef;
2665         }
2666
2667         if (not defined($self->check_or_start($env, "standard"))) {
2668             return undef;
2669         }
2670
2671         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2672         my $cmd = "";
2673
2674         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2675         $cmd .= "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2676         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2677         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2678         $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2679         $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2680         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2681         $cmd .= " $dc_vars->{CONFIGURATION}";
2682         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2683         # replicate Configuration NC
2684         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2685         unless(system($cmd_repl) == 0) {
2686             warn("Failed to replicate\n$cmd_repl");
2687             return undef;
2688         }
2689         # replicate Default NC
2690         $cmd_repl = "$cmd \"$base_dn\"";
2691         unless(system($cmd_repl) == 0) {
2692             warn("Failed to replicate\n$cmd_repl");
2693             return undef;
2694         }
2695
2696         return $env;
2697 }
2698
2699 sub setup_ad_dc
2700 {
2701         my ($self, $path) = @_;
2702
2703         # If we didn't build with ADS, pretend this env was never available
2704         if (not $self->{target3}->have_ads()) {
2705                return "UNKNOWN";
2706         }
2707
2708         my $env = $self->provision_ad_dc($path, "addc", "ADDOMAIN",
2709                                          "addom.samba.example.com", "", undef);
2710         unless ($env) {
2711                 return undef;
2712         }
2713
2714         if (not defined($self->check_or_start($env, "prefork"))) {
2715             return undef;
2716         }
2717
2718         my $upn_array = ["$env->{REALM}.upn"];
2719         my $spn_array = ["$env->{REALM}.spn"];
2720
2721         $self->setup_namespaces($env, $upn_array, $spn_array);
2722
2723         return $env;
2724 }
2725
2726 sub setup_ad_dc_no_nss
2727 {
2728         my ($self, $path) = @_;
2729
2730         # If we didn't build with ADS, pretend this env was never available
2731         if (not $self->{target3}->have_ads()) {
2732                return "UNKNOWN";
2733         }
2734
2735         my $env = $self->provision_ad_dc($path, "addc_no_nss", "ADNONSSDOMAIN",
2736                                          "adnonssdom.samba.example.com", "", undef);
2737         unless ($env) {
2738                 return undef;
2739         }
2740
2741         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2742         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2743
2744         if (not defined($self->check_or_start($env, "single"))) {
2745             return undef;
2746         }
2747
2748         my $upn_array = ["$env->{REALM}.upn"];
2749         my $spn_array = ["$env->{REALM}.spn"];
2750
2751         $self->setup_namespaces($env, $upn_array, $spn_array);
2752
2753         return $env;
2754 }
2755
2756 sub setup_ad_dc_no_ntlm
2757 {
2758         my ($self, $path) = @_;
2759
2760         # If we didn't build with ADS, pretend this env was never available
2761         if (not $self->{target3}->have_ads()) {
2762                return "UNKNOWN";
2763         }
2764
2765         my $env = $self->provision_ad_dc($path, "addc_no_ntlm", "ADNONTLMDOMAIN",
2766                                          "adnontlmdom.samba.example.com",
2767                                          "ntlm auth = disabled", undef);
2768         unless ($env) {
2769                 return undef;
2770         }
2771
2772         if (not defined($self->check_or_start($env, "prefork"))) {
2773             return undef;
2774         }
2775
2776         my $upn_array = ["$env->{REALM}.upn"];
2777         my $spn_array = ["$env->{REALM}.spn"];
2778
2779         $self->setup_namespaces($env, $upn_array, $spn_array);
2780
2781         return $env;
2782 }
2783
2784 #
2785 # AD DC test environment used solely to test pre-fork process restarts.
2786 # As processes get killed off and restarted it should not be used for other
2787 sub setup_preforkrestartdc
2788 {
2789         my ($self, $path) = @_;
2790
2791         # If we didn't build with ADS, pretend this env was never available
2792         if (not $self->{target3}->have_ads()) {
2793                return "UNKNOWN";
2794         }
2795
2796         # note DC name must be <= 15 chars so we use 'prockill' instead of
2797         # 'preforkrestart'
2798         my $env = $self->provision_ad_dc(
2799                 $path,
2800                 "prockilldc",
2801                 "PROCKILLDOMAIN",
2802                 "prockilldom.samba.example.com",
2803                 "prefork backoff increment = 5\nprefork maximum backoff=10");
2804         unless ($env) {
2805                 return undef;
2806         }
2807
2808         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2809         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2810
2811         if (not defined($self->check_or_start($env, "prefork"))) {
2812             return undef;
2813         }
2814
2815         my $upn_array = ["$env->{REALM}.upn"];
2816         my $spn_array = ["$env->{REALM}.spn"];
2817
2818         $self->setup_namespaces($env, $upn_array, $spn_array);
2819
2820         return $env;
2821 }
2822
2823 #
2824 # ad_dc test environment used solely to test standard process model connection
2825 # process limits. As the limit is set artificially low it should not be used
2826 # for other tests.
2827 sub setup_proclimitdc
2828 {
2829         my ($self, $path) = @_;
2830
2831         # If we didn't build with ADS, pretend this env was never available
2832         if (not $self->{target3}->have_ads()) {
2833                return "UNKNOWN";
2834         }
2835
2836         my $env = $self->provision_ad_dc(
2837                 $path,
2838                 "proclimitdc",
2839                 "PROCLIMITDOM",
2840                 "proclimit.samba.example.com",
2841                 "max smbd processes = 20");
2842         unless ($env) {
2843                 return undef;
2844         }
2845
2846         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2847         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2848
2849         if (not defined($self->check_or_start($env, "standard"))) {
2850             return undef;
2851         }
2852
2853         my $upn_array = ["$env->{REALM}.upn"];
2854         my $spn_array = ["$env->{REALM}.spn"];
2855
2856         $self->setup_namespaces($env, $upn_array, $spn_array);
2857
2858         return $env;
2859 }
2860
2861 # Sets up a DC that's solely used to do a domain backup from. We then use the
2862 # backupfrom-DC to create the restore-DC - this proves that the backup/restore
2863 # process will create a Samba DC that will actually start up.
2864 # We don't use the backup-DC for anything else because its domain will conflict
2865 # with the restore DC.
2866 sub setup_backupfromdc
2867 {
2868         my ($self, $path) = @_;
2869
2870         # If we didn't build with ADS, pretend this env was never available
2871         if (not $self->{target3}->have_ads()) {
2872                return "UNKNOWN";
2873         }
2874
2875         my $provision_args = ["--site=Backup-Site"];
2876
2877         my $env = $self->provision_ad_dc($path, "backupfromdc", "BACKUPDOMAIN",
2878                                          "backupdom.samba.example.com",
2879                                          "samba kcc command = /bin/true",
2880                                          $provision_args);
2881         unless ($env) {
2882                 return undef;
2883         }
2884
2885         if (not defined($self->check_or_start($env, "standard"))) {
2886             return undef;
2887         }
2888
2889         my $upn_array = ["$env->{REALM}.upn"];
2890         my $spn_array = ["$env->{REALM}.spn"];
2891
2892         $self->setup_namespaces($env, $upn_array, $spn_array);
2893
2894         return $env;
2895 }
2896
2897 # returns the server/user-auth params needed to run an online backup cmd
2898 sub get_backup_server_args
2899 {
2900         # dcvars contains the env info for the backup DC testenv
2901         my ($self, $dcvars) = @_;
2902         my $server = $dcvars->{DC_SERVER_IP};
2903         my $server_args = "--server=$server ";
2904         $server_args .= "-U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
2905         $server_args .= " $dcvars->{CONFIGURATION}";
2906
2907         return $server_args;
2908 }
2909
2910 # Creates a backup of a running testenv DC
2911 sub create_backup
2912 {
2913         # note: dcvars contains the env info for the backup DC testenv
2914         my ($self, $env, $dcvars, $backupdir, $backup_cmd) = @_;
2915
2916         # get all the env variables we pass in with the samba-tool command
2917         my $cmd_env = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2918         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2919         if (defined($env->{RESOLV_WRAPPER_CONF})) {
2920                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2921         } else {
2922                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2923         }
2924         # Note: use the backupfrom-DC's krb5.conf to do the backup
2925         $cmd_env .= " KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
2926         $cmd_env .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2927
2928         # use samba-tool to create a backup from the 'backupfromdc' DC
2929         my $cmd = "";
2930         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2931
2932         $cmd .= "$cmd_env $samba_tool domain backup $backup_cmd";
2933         $cmd .= " --targetdir=$backupdir";
2934
2935         print "Executing: $cmd\n";
2936         unless(system($cmd) == 0) {
2937                 warn("Failed to create backup using: \n$cmd");
2938                 return undef;
2939         }
2940
2941         # get the name of the backup file created
2942         opendir(DIR, $backupdir);
2943         my @files = grep(/\.tar/, readdir(DIR));
2944         closedir(DIR);
2945
2946         if(scalar @files != 1) {
2947                 warn("Backup file not found in directory $backupdir\n");
2948                 return undef;
2949         }
2950         my $backup_file = "$backupdir/$files[0]";
2951         print "Using backup file $backup_file...\n";
2952
2953         return $backup_file;
2954 }
2955
2956 # Restores a backup-file to populate a testenv for a new DC
2957 sub restore_backup_file
2958 {
2959         my ($self, $backup_file, $restore_opts, $restoredir, $smbconf) = @_;
2960
2961         # pass the restore command the testenv's smb.conf that we've already
2962         # generated. But move it to a temp-dir first, so that the restore doesn't
2963         # overwrite it
2964         my $tmpdir = File::Temp->newdir();
2965         my $tmpconf = "$tmpdir/smb.conf";
2966         my $cmd = "cp $smbconf $tmpconf";
2967         unless(system($cmd) == 0) {
2968                 warn("Failed to backup smb.conf using: \n$cmd");
2969                 return -1;
2970         }
2971
2972         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2973         $cmd = "$samba_tool domain backup restore --backup-file=$backup_file";
2974         $cmd .= " --targetdir=$restoredir $restore_opts --configfile=$tmpconf";
2975
2976         print "Executing: $cmd\n";
2977         unless(system($cmd) == 0) {
2978                 warn("Failed to restore backup using: \n$cmd");
2979                 return -1;
2980         }
2981
2982         print "Restore complete\n";
2983         return 0
2984 }
2985
2986 # sets up the initial directory and returns the new testenv's env info
2987 # (without actually doing a 'domain join')
2988 sub prepare_dc_testenv
2989 {
2990         my ($self, $prefix, $dcname, $domain, $realm,
2991                 $password, $conf_options) = @_;
2992
2993         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
2994                                                $dcname,
2995                                                $domain,
2996                                                $realm,
2997                                                undef,
2998                                                "2008",
2999                                                $password,
3000                                                undef,
3001                                                undef);
3002
3003         # the restore uses a slightly different state-dir location to other testenvs
3004         $ctx->{statedir} = "$ctx->{prefix_abs}/state";
3005         push(@{$ctx->{directories}}, "$ctx->{statedir}");
3006
3007         # add support for sysvol/netlogon/tmp shares
3008         $ctx->{share} = "$ctx->{prefix_abs}/share";
3009         push(@{$ctx->{directories}}, "$ctx->{share}");
3010         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
3011
3012         $ctx->{smb_conf_extra_options} = "
3013         $conf_options
3014         max xmit = 32K
3015         server max protocol = SMB2
3016         samba kcc command = /bin/true
3017         xattr_tdb:file = $ctx->{statedir}/xattr.tdb
3018
3019 [sysvol]
3020         path = $ctx->{statedir}/sysvol
3021         read only = no
3022
3023 [netlogon]
3024         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
3025         read only = no
3026
3027 [tmp]
3028         path = $ctx->{share}
3029         read only = no
3030         posix:sharedelay = 10000
3031         posix:oplocktimeout = 3
3032         posix:writetimeupdatedelay = 50000
3033
3034 [test1]
3035         path = $ctx->{share}/test1
3036         read only = no
3037         posix:sharedelay = 100000
3038         posix:oplocktimeout = 3
3039         posix:writetimeupdatedelay = 500000
3040 ";
3041
3042         my $env = $self->provision_raw_step1($ctx);
3043
3044         $env->{DC_SERVER} = $env->{SERVER};
3045         $env->{DC_SERVER_IP} = $env->{SERVER_IP};
3046         $env->{DC_SERVER_IPV6} = $env->{SERVER_IPV6};
3047         $env->{DC_NETBIOSNAME} = $env->{NETBIOSNAME};
3048         $env->{DC_USERNAME} = $env->{USERNAME};
3049         $env->{DC_PASSWORD} = $env->{PASSWORD};
3050
3051     return ($env, $ctx);
3052 }
3053
3054
3055 # Set up a DC testenv solely by using the samba-tool domain backup/restore
3056 # commands. This proves that we can backup an online DC ('backupfromdc') and
3057 # use the backup file to create a valid, working samba DC.
3058 sub setup_restoredc
3059 {
3060         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3061         my ($self, $prefix, $dcvars) = @_;
3062         print "Preparing RESTORE DC...\n";
3063
3064         # we arbitrarily designate the restored DC as having SMBv1 disabled
3065         my $extra_conf = "
3066         server min protocol = SMB2
3067         client min protocol = SMB2";
3068
3069         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "restoredc",
3070                                                     $dcvars->{DOMAIN},
3071                                                     $dcvars->{REALM},
3072                                                     $dcvars->{PASSWORD},
3073                                                     $extra_conf);
3074
3075         # create a backup of the 'backupfromdc'
3076         my $backupdir = File::Temp->newdir();
3077         my $server_args = $self->get_backup_server_args($dcvars);
3078         my $backup_args = "online $server_args";
3079         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3080                                                $backup_args);
3081         unless($backup_file) {
3082                 return undef;
3083         }
3084
3085         # restore the backup file to populate the restore-DC testenv
3086         my $restore_dir = abs_path($prefix);
3087         my $ret = $self->restore_backup_file($backup_file,
3088                                              "--newservername=$env->{SERVER}",
3089                                              $restore_dir, $env->{SERVERCONFFILE});
3090         unless ($ret == 0) {
3091                 return undef;
3092         }
3093
3094         # start samba for the restored DC
3095         if (not defined($self->check_or_start($env, "standard"))) {
3096             return undef;
3097         }
3098
3099         return $env;
3100 }
3101
3102 # Set up a DC testenv solely by using the 'samba-tool domain backup rename' and
3103 # restore commands. This proves that we can backup and rename an online DC
3104 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
3105 sub setup_renamedc
3106 {
3107         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3108         my ($self, $prefix, $dcvars) = @_;
3109         print "Preparing RENAME DC...\n";
3110
3111         my $realm = "renamedom.samba.example.com";
3112         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "renamedc",
3113                                                     "RENAMEDOMAIN", $realm,
3114                                                     $dcvars->{PASSWORD}, "");
3115
3116         # create a backup of the 'backupfromdc' which renames the domain
3117         my $backupdir = File::Temp->newdir();
3118         my $server_args = $self->get_backup_server_args($dcvars);
3119         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
3120         $backup_args .= " --backend-store=tdb";
3121         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3122                                                $backup_args);
3123         unless($backup_file) {
3124                 return undef;
3125         }
3126
3127         # restore the backup file to populate the rename-DC testenv
3128         my $restore_dir = abs_path($prefix);
3129         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3130         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3131                                              $restore_dir, $env->{SERVERCONFFILE});
3132         unless ($ret == 0) {
3133                 return undef;
3134         }
3135
3136         # start samba for the restored DC
3137         if (not defined($self->check_or_start($env, "standard"))) {
3138             return undef;
3139         }
3140
3141         my $upn_array = ["$env->{REALM}.upn"];
3142         my $spn_array = ["$env->{REALM}.spn"];
3143
3144         $self->setup_namespaces($env, $upn_array, $spn_array);
3145
3146         return $env;
3147 }
3148
3149 # Set up a DC testenv solely by using the 'samba-tool domain backup offline' and
3150 # restore commands. This proves that we do an offline backup of a local DC
3151 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
3152 sub setup_offlinebackupdc
3153 {
3154         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3155         my ($self, $prefix, $dcvars) = @_;
3156         print "Preparing OFFLINE BACKUP DC...\n";
3157
3158         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "offlinebackupdc",
3159                                                     $dcvars->{DOMAIN},
3160                                                     $dcvars->{REALM},
3161                                                     $dcvars->{PASSWORD}, "");
3162
3163         # create an offline backup of the 'backupfromdc' target
3164         my $backupdir = File::Temp->newdir();
3165         my $cmd = "offline -s $dcvars->{SERVERCONFFILE}";
3166         my $backup_file = $self->create_backup($env, $dcvars,
3167                                                $backupdir, $cmd);
3168
3169         unless($backup_file) {
3170                 return undef;
3171         }
3172
3173         # restore the backup file to populate the rename-DC testenv
3174         my $restore_dir = abs_path($prefix);
3175         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3176         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3177                                              $restore_dir, $env->{SERVERCONFFILE});
3178         unless ($ret == 0) {
3179                 return undef;
3180         }
3181
3182         # re-create the testenv's krb5.conf (the restore may have overwritten it)
3183         Samba::mk_krb5_conf($ctx);
3184
3185         # start samba for the restored DC
3186         if (not defined($self->check_or_start($env, "standard"))) {
3187             return undef;
3188         }
3189
3190         return $env;
3191 }
3192
3193 # Set up a DC testenv solely by using the samba-tool 'domain backup rename' and
3194 # restore commands, using the --no-secrets option. This proves that we can
3195 # create a realistic lab environment from an online DC ('backupfromdc').
3196 sub setup_labdc
3197 {
3198         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3199         my ($self, $prefix, $dcvars) = @_;
3200         print "Preparing LAB-DOMAIN DC...\n";
3201
3202         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "labdc",
3203                                                     "LABDOMAIN",
3204                                                     "labdom.samba.example.com",
3205                                                     $dcvars->{PASSWORD}, "");
3206
3207         # create a backup of the 'backupfromdc' which renames the domain and uses
3208         # the --no-secrets option to scrub any sensitive info
3209         my $backupdir = File::Temp->newdir();
3210         my $server_args = $self->get_backup_server_args($dcvars);
3211         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
3212         $backup_args .= " --no-secrets --backend-store=mdb";
3213         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3214                                                $backup_args);
3215         unless($backup_file) {
3216                 return undef;
3217         }
3218
3219         # restore the backup file to populate the lab-DC testenv
3220         my $restore_dir = abs_path($prefix);
3221         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3222         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3223                                              $restore_dir, $env->{SERVERCONFFILE});
3224         unless ($ret == 0) {
3225                 return undef;
3226         }
3227
3228         # because we don't include any secrets in the backup, we need to reset the
3229         # admin user's password back to what the testenv expects
3230         my $samba_tool = Samba::bindir_path($self, "samba-tool");
3231         my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
3232         $cmd .= "--newpassword=$env->{PASSWORD} -H $restore_dir/private/sam.ldb";
3233         $cmd .= " $env->{CONFIGURATION}";
3234
3235         unless(system($cmd) == 0) {
3236                 warn("Failed to reset admin's password: \n$cmd");
3237                 return undef;
3238         }
3239
3240         # start samba for the restored DC
3241         if (not defined($self->check_or_start($env, "standard"))) {
3242             return undef;
3243         }
3244
3245         my $upn_array = ["$env->{REALM}.upn"];
3246         my $spn_array = ["$env->{REALM}.spn"];
3247
3248         $self->setup_namespaces($env, $upn_array, $spn_array);
3249
3250         return $env;
3251 }
3252
3253 # Inspects a backup *.tar.bz2 file and determines the realm/domain it contains
3254 sub get_backup_domain_realm
3255 {
3256         my ($self, $backup_file) = @_;
3257
3258         print "Determining REALM/DOMAIN values in backup...\n";
3259
3260         # The backup will have the correct domain/realm values in the smb.conf.
3261         # So we can work out the env variables the testenv should use based on
3262         # that. Let's start by extracting the smb.conf
3263         my $tar = Archive::Tar->new($backup_file);
3264         my $tmpdir = File::Temp->newdir();
3265         my $smbconf = "$tmpdir/smb.conf";
3266
3267         # note that the filepaths within the tar-file differ slightly for online
3268         # and offline backups
3269         if ($tar->contains_file("etc/smb.conf")) {
3270                 $tar->extract_file("etc/smb.conf", $smbconf);
3271         } elsif ($tar->contains_file("./etc/smb.conf")) {
3272                 $tar->extract_file("./etc/smb.conf", $smbconf);
3273         } else {
3274                 warn("Could not find smb.conf in $backup_file");
3275                 return undef, undef;
3276         }
3277
3278         # now use testparm to read the values we're interested in
3279         my $testparm = Samba::bindir_path($self, "testparm");
3280         my $domain = `$testparm $smbconf -sl --parameter-name=WORKGROUP`;
3281         my $realm = `$testparm $smbconf -sl --parameter-name=REALM`;
3282         chomp $realm;
3283         chomp $domain;
3284         print "Backup-file REALM is $realm, DOMAIN is $domain\n";
3285
3286         return ($domain, $realm);
3287 }
3288
3289 # This spins up a custom testenv that can be based on any backup-file you want.
3290 # This is just intended for manual testing (rather than automated test-cases)
3291 sub setup_customdc
3292 {
3293         my ($self, $prefix) = @_;
3294         print "Preparing CUSTOM RESTORE DC...\n";
3295         my $dc_name = "customdc";
3296         my $password = "locDCpass1";
3297         my $backup_file = $ENV{'BACKUP_FILE'};
3298
3299         # user must specify a backup file to restore via an ENV variable, i.e.
3300         # BACKUP_FILE=backup-blah.tar.bz2 SELFTEST_TESTENV=customdc make testenv
3301         if (not defined($backup_file)) {
3302                 warn("Please specify BACKUP_FILE");
3303                 return undef;
3304         }
3305
3306         # work out the correct domain/realm env values from the backup-file
3307         my ($domain, $realm) = $self->get_backup_domain_realm($backup_file);
3308
3309         # create a placeholder directory and smb.conf, as well as the env vars.
3310         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, $dc_name,
3311                                                     $domain, $realm, $password, "");
3312
3313         # restore the specified backup file to populate the testenv
3314         my $restore_dir = abs_path($prefix);
3315         my $ret = $self->restore_backup_file($backup_file,
3316                                              "--newservername=$env->{SERVER}",
3317                                              $restore_dir, $env->{SERVERCONFFILE});
3318         unless ($ret == 0) {
3319                 return undef;
3320         }
3321
3322         # Change the admin password to the testenv default, just in case it's
3323         # different, or in case this was a --no-secrets backup
3324         my $samba_tool = Samba::bindir_path($self, "samba-tool");
3325         my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
3326         $cmd .= "--newpassword=$password -H $restore_dir/private/sam.ldb";
3327         $cmd .= " $env->{CONFIGURATION}";
3328
3329         unless(system($cmd) == 0) {
3330                 warn("Failed to reset admin's password: \n$cmd");
3331                 return undef;
3332         }
3333
3334         # re-create the testenv's krb5.conf (the restore may have overwritten it,
3335         # if the backup-file was an offline backup)
3336         Samba::mk_krb5_conf($ctx);
3337
3338         # start samba for the restored DC
3339         if (not defined($self->check_or_start($env, "standard"))) {
3340             return undef;
3341         }
3342
3343         # if this was a backup-rename, then we may need to setup namespaces
3344         my $upn_array = ["$env->{REALM}.upn"];
3345         my $spn_array = ["$env->{REALM}.spn"];
3346
3347         $self->setup_namespaces($env, $upn_array, $spn_array);
3348
3349         return $env;
3350 }
3351
3352 sub setup_none
3353 {
3354         my ($self, $path) = @_;
3355
3356         my $ret = {
3357                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
3358                 SAMBA_PID => -1,
3359         }
3360 }
3361
3362 1;