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