selftest: systematize formatting of if/elseif/else indentation in cleanup_child
[metze/samba/wip.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...";
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{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
126                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
127
128                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
129                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
130                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
131                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
132                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
133
134                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
135                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
136                 } else {
137                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
138                 }
139
140                 $ENV{UID_WRAPPER} = "1";
141                 $ENV{UID_WRAPPER_ROOT} = "1";
142
143                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "samba");
144                 my @preargs = ();
145                 my @optargs = ();
146                 if (defined($ENV{SAMBA_OPTIONS})) {
147                         @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
148                 }
149                 if(defined($ENV{SAMBA_VALGRIND})) {
150                         @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
151                 }
152
153                 close($env_vars->{STDIN_PIPE});
154                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
155
156                 exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
157         }
158         $env_vars->{SAMBA_PID} = $pid;
159         print "DONE ($pid)\n";
160
161         close($STDIN_READER);
162
163         if ($self->wait_for_start($env_vars) != 0) {
164             warn("Samba $pid failed to start up");
165             return undef;
166         }
167
168         return $pid;
169 }
170
171 sub wait_for_start($$)
172 {
173         my ($self, $testenv_vars) = @_;
174         my $ret = 0;
175
176         if (not $self->check_env($testenv_vars)) {
177             warn("unable to confirm Samba $testenv_vars->{SAMBA_PID} is running");
178             return -1;
179         }
180
181         # give time for nbt server to register its names
182         print "delaying for nbt name registration\n";
183         sleep 2;
184
185         # This will return quickly when things are up, but be slow if we
186         # need to wait for (eg) SSL init
187         my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
188         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
189         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
190         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
191         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
192         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
193         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
194         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
195         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
196         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
197         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
198         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
199         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
200
201         # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
202         if ($testenv_vars->{SERVER_ROLE} eq "domain controller" and not ($testenv_vars->{NETBIOSNAME} eq "RODC")) {
203             # Add hosts file for name lookups
204             $ENV{NSS_WRAPPER_HOSTS} = $testenv_vars->{NSS_WRAPPER_HOSTS};
205                 if (defined($testenv_vars->{RESOLV_WRAPPER_CONF})) {
206                         $ENV{RESOLV_WRAPPER_CONF} = $testenv_vars->{RESOLV_WRAPPER_CONF};
207                 } else {
208                         $ENV{RESOLV_WRAPPER_HOSTS} = $testenv_vars->{RESOLV_WRAPPER_HOSTS};
209                 }
210
211             print "waiting for working LDAP and a RID Set to be allocated\n";
212             my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
213             my $count = 0;
214             my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
215             my $rid_set_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
216             my $max_wait = 60;
217             sleep(1);
218             while (system("$ldbsearch -H ldap://$testenv_vars->{SERVER} -U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} -s base -b \"$rid_set_dn\" rIDAllocationPool > /dev/null") != 0) {
219                 $count++;
220                 if ($count > $max_wait) {
221                     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}");
222                     $ret = -1;
223                     last;
224                 }
225                 sleep(1);
226             }
227         }
228         print $self->getlog_env($testenv_vars);
229
230         return $ret
231 }
232
233 sub write_ldb_file($$$)
234 {
235         my ($self, $file, $ldif) = @_;
236
237         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
238         open(LDIF, "|$ldbadd -H $file >/dev/null");
239         print LDIF $ldif;
240         return(close(LDIF));
241 }
242
243 sub add_wins_config($$)
244 {
245         my ($self, $privatedir) = @_;
246
247         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
248 dn: name=TORTURE_11,CN=PARTNERS
249 objectClass: wreplPartner
250 name: TORTURE_11
251 address: 127.0.0.11
252 pullInterval: 0
253 pushChangeCount: 0
254 type: 0x3
255 ");
256 }
257
258 sub mk_fedora_ds($$)
259 {
260         my ($self, $ctx) = @_;
261
262         #Make the subdirectory be as fedora DS would expect
263         my $fedora_ds_dir = "$ctx->{ldapdir}/slapd-$ctx->{ldap_instance}";
264
265         my $pidfile = "$fedora_ds_dir/logs/slapd-$ctx->{ldap_instance}.pid";
266
267         return ($fedora_ds_dir, $pidfile);
268 }
269
270 sub mk_openldap($$)
271 {
272         my ($self, $ctx) = @_;
273
274         my $slapd_conf_d = "$ctx->{ldapdir}/slapd.d";
275         my $pidfile = "$ctx->{ldapdir}/slapd.pid";
276
277         return ($slapd_conf_d, $pidfile);
278 }
279
280 sub setup_namespaces($$:$$)
281 {
282         my ($self, $localenv, $upn_array, $spn_array) = @_;
283
284         @{$upn_array} = [] unless defined($upn_array);
285         my $upn_args = "";
286         foreach my $upn (@{$upn_array}) {
287                 $upn_args .= " --add-upn-suffix=$upn";
288         }
289
290         @{$spn_array} = [] unless defined($spn_array);
291         my $spn_args = "";
292         foreach my $spn (@{$spn_array}) {
293                 $spn_args .= " --add-spn-suffix=$spn";
294         }
295
296         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
297
298         my $cmd_env = "";
299         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
300         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
301                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
302         } else {
303                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
304         }
305         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\"";
306
307         my $cmd_config = " $localenv->{CONFIGURATION}";
308
309         my $namespaces = $cmd_env;
310         $namespaces .= " $samba_tool domain trust namespaces $upn_args $spn_args";
311         $namespaces .= $cmd_config;
312         unless (system($namespaces) == 0) {
313                 warn("Failed to add namespaces \n$namespaces");
314                 return;
315         }
316
317         return;
318 }
319
320 sub setup_trust($$$$$)
321 {
322         my ($self, $localenv, $remoteenv, $type, $extra_args) = @_;
323
324         $localenv->{TRUST_SERVER} = $remoteenv->{SERVER};
325         $localenv->{TRUST_SERVER_IP} = $remoteenv->{SERVER_IP};
326         $localenv->{TRUST_SERVER_IPV6} = $remoteenv->{SERVER_IPV6};
327         $localenv->{TRUST_NETBIOSNAME} = $remoteenv->{NETBIOSNAME};
328         $localenv->{TRUST_USERNAME} = $remoteenv->{USERNAME};
329         $localenv->{TRUST_PASSWORD} = $remoteenv->{PASSWORD};
330         $localenv->{TRUST_DOMAIN} = $remoteenv->{DOMAIN};
331         $localenv->{TRUST_REALM} = $remoteenv->{REALM};
332
333         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
334         # setup the trust
335         my $cmd_env = "";
336         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
337         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
338                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
339         } else {
340                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
341         }
342         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\"";
343
344         my $cmd_config = " $localenv->{CONFIGURATION}";
345         my $cmd_creds = $cmd_config;
346         $cmd_creds .= " -U$localenv->{TRUST_DOMAIN}\\\\$localenv->{TRUST_USERNAME}\%$localenv->{TRUST_PASSWORD}";
347
348         my $create = $cmd_env;
349         $create .= " $samba_tool domain trust create --type=${type} $localenv->{TRUST_REALM}";
350         $create .= " $extra_args";
351         $create .= $cmd_creds;
352         unless (system($create) == 0) {
353                 warn("Failed to create trust \n$create");
354                 return undef;
355         }
356
357         return $localenv
358 }
359
360 sub provision_raw_prepare($$$$$$$$$$$)
361 {
362         my ($self, $prefix, $server_role, $hostname,
363             $domain, $realm, $functional_level,
364             $password, $kdc_ipv4, $kdc_ipv6) = @_;
365         my $ctx;
366         my $netbiosname = uc($hostname);
367
368         unless(-d $prefix or mkdir($prefix, 0777)) {
369                 warn("Unable to create $prefix");
370                 return undef;
371         }
372         my $prefix_abs = abs_path($prefix);
373
374         die ("prefix=''") if $prefix_abs eq "";
375         die ("prefix='/'") if $prefix_abs eq "/";
376
377         unless (system("rm -rf $prefix_abs/*") == 0) {
378                 warn("Unable to clean up");
379         }
380
381         
382         my $swiface = Samba::get_interface($hostname);
383
384         $ctx->{prefix} = $prefix;
385         $ctx->{prefix_abs} = $prefix_abs;
386
387         $ctx->{server_role} = $server_role;
388         $ctx->{hostname} = $hostname;
389         $ctx->{netbiosname} = $netbiosname;
390         $ctx->{swiface} = $swiface;
391         $ctx->{password} = $password;
392         $ctx->{kdc_ipv4} = $kdc_ipv4;
393         $ctx->{kdc_ipv6} = $kdc_ipv6;
394         if ($functional_level eq "2000") {
395                 $ctx->{supported_enctypes} = "arcfour-hmac-md5 des-cbc-md5 des-cbc-crc"
396         }
397
398 #
399 # Set smbd log level here.
400 #
401         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
402         $ctx->{username} = "Administrator";
403         $ctx->{domain} = $domain;
404         $ctx->{realm} = uc($realm);
405         $ctx->{dnsname} = lc($realm);
406
407         $ctx->{functional_level} = $functional_level;
408
409         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
410         chomp $unix_name;
411         $ctx->{unix_name} = $unix_name;
412         $ctx->{unix_uid} = $>;
413         my @mygid = split(" ", $();
414         $ctx->{unix_gid} = $mygid[0];
415         $ctx->{unix_gids_str} = $);
416         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
417
418         $ctx->{etcdir} = "$prefix_abs/etc";
419         $ctx->{piddir} = "$prefix_abs/pid";
420         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
421         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
422         $ctx->{privatedir} = "$prefix_abs/private";
423         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
424         $ctx->{lockdir} = "$prefix_abs/lockdir";
425         $ctx->{logdir} = "$prefix_abs/logs";
426         $ctx->{statedir} = "$prefix_abs/statedir";
427         $ctx->{cachedir} = "$prefix_abs/cachedir";
428         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
429         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
430         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
431         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
432         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
433         $ctx->{nsswrap_hosts} = "$ENV{SELFTEST_PREFIX}/hosts";
434         if ($ENV{SAMBA_DNS_FAKING}) {
435                 $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
436                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf} --all-interfaces --use-file=$ctx->{dns_host_file}";
437         } else {
438                 $ctx->{resolv_conf} = "$ctx->{etcdir}/resolv.conf";
439                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf}";
440         }
441
442         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
443
444         $ctx->{ipv4} = "127.0.0.$swiface";
445         $ctx->{ipv6} = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
446         $ctx->{interfaces} = "$ctx->{ipv4}/8 $ctx->{ipv6}/64";
447
448         push(@{$ctx->{directories}}, $ctx->{privatedir});
449         push(@{$ctx->{directories}}, $ctx->{etcdir});
450         push(@{$ctx->{directories}}, $ctx->{piddir});
451         push(@{$ctx->{directories}}, $ctx->{lockdir});
452         push(@{$ctx->{directories}}, $ctx->{logdir});
453         push(@{$ctx->{directories}}, $ctx->{statedir});
454         push(@{$ctx->{directories}}, $ctx->{cachedir});
455
456         $ctx->{smb_conf_extra_options} = "";
457
458         my @provision_options = ();
459         push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_config}\"");
460         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
461         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
462         push (@provision_options, "NSS_WRAPPER_HOSTS=\"$ctx->{nsswrap_hosts}\"");
463         if (defined($ctx->{resolv_conf})) {
464                 push (@provision_options, "RESOLV_WRAPPER_CONF=\"$ctx->{resolv_conf}\"");
465         } else {
466                 push (@provision_options, "RESOLV_WRAPPER_HOSTS=\"$ctx->{dns_host_file}\"");
467         }
468         if (defined($ENV{GDB_PROVISION})) {
469                 push (@provision_options, "gdb --args");
470                 if (!defined($ENV{PYTHON})) {
471                     push (@provision_options, "env");
472                     push (@provision_options, "python");
473                 }
474         }
475         if (defined($ENV{VALGRIND_PROVISION})) {
476                 push (@provision_options, "valgrind");
477                 if (!defined($ENV{PYTHON})) {
478                     push (@provision_options, "env");
479                     push (@provision_options, "python");
480                 }
481         }
482         if (defined($ENV{PYTHON})) {
483                 push (@provision_options, $ENV{PYTHON});
484         }
485         push (@provision_options, Samba::bindir_path($self, "samba-tool"));
486         push (@provision_options, "domain");
487         push (@provision_options, "provision");
488         push (@provision_options, "--configfile=$ctx->{smb_conf}");
489         push (@provision_options, "--host-name=$ctx->{hostname}");
490         push (@provision_options, "--host-ip=$ctx->{ipv4}");
491         push (@provision_options, "--quiet");
492         push (@provision_options, "--domain=$ctx->{domain}");
493         push (@provision_options, "--realm=$ctx->{realm}");
494         push (@provision_options, "--adminpass=$ctx->{password}");
495         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
496         push (@provision_options, "--machinepass=machine$ctx->{password}");
497         push (@provision_options, "--root=$ctx->{unix_name}");
498         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
499         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
500
501         @{$ctx->{provision_options}} = @provision_options;
502
503         return $ctx;
504 }
505
506 #
507 # Step1 creates the basic configuration
508 #
509 sub provision_raw_step1($$)
510 {
511         my ($self, $ctx) = @_;
512
513         mkdir($_, 0777) foreach (@{$ctx->{directories}});
514
515         ##
516         ## lockdir and piddir must be 0755
517         ##
518         chmod 0755, $ctx->{lockdir};
519         chmod 0755, $ctx->{piddir};
520
521         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
522                 warn("can't open $ctx->{smb_conf}$?");
523                 return undef;
524         }
525
526         Samba::prepare_keyblobs($ctx);
527         my $crlfile = "$ctx->{tlsdir}/crl.pem";
528         $crlfile = "" unless -e ${crlfile};
529
530         print CONFFILE "
531 [global]
532         netbios name = $ctx->{netbiosname}
533         posix:eadb = $ctx->{statedir}/eadb.tdb
534         workgroup = $ctx->{domain}
535         realm = $ctx->{realm}
536         private dir = $ctx->{privatedir}
537         pid directory = $ctx->{piddir}
538         ncalrpc dir = $ctx->{ncalrpcdir}
539         lock dir = $ctx->{lockdir}
540         state directory = $ctx->{statedir}
541         cache directory = $ctx->{cachedir}
542         winbindd socket directory = $ctx->{winbindd_socket_dir}
543         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
544         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
545         winbind separator = /
546         interfaces = $ctx->{interfaces}
547         tls dh params file = $ctx->{tlsdir}/dhparms.pem
548         tls crlfile = ${crlfile}
549         tls verify peer = no_check
550         panic action = $RealBin/gdb_backtrace \%d
551         wins support = yes
552         server role = $ctx->{server_role}
553         server services = +echo +smb -s3fs
554         dcerpc endpoint servers = +winreg +srvsvc
555         notify:inotify = false
556         ldb:nosync = true
557         ldap server require strong auth = yes
558 #We don't want to pass our self-tests if the PAC code is wrong
559         gensec:require_pac = true
560         log file = $ctx->{logdir}/log.\%m
561         log level = $ctx->{server_loglevel}
562         lanman auth = Yes
563         rndc command = true
564         dns update command = $ctx->{samba_dnsupdate}
565         spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
566         dreplsrv:periodic_startup_interval = 0
567         dsdb:schema update allowed = yes
568
569         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
570
571         # remove this again, when our smb2 client library
572         # supports signin on compound related requests
573         server signing = on
574
575         idmap_ldb:use rfc2307=yes
576         winbind enum users = yes
577         winbind enum groups = yes
578 ";
579
580         print CONFFILE "
581
582         # Begin extra options
583         $ctx->{smb_conf_extra_options}
584         # End extra options
585 ";
586         close(CONFFILE);
587
588         #Default the KDC IP to the server's IP
589         if (not defined($ctx->{kdc_ipv4})) {
590                 $ctx->{kdc_ipv4} = $ctx->{ipv4};
591         }
592         if (not defined($ctx->{kdc_ipv6})) {
593                 $ctx->{kdc_ipv6} = $ctx->{ipv6};
594         }
595
596         Samba::mk_krb5_conf($ctx);
597
598         open(PWD, ">$ctx->{nsswrap_passwd}");
599         if ($ctx->{unix_uid} != 0) {
600                 print PWD "root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false\n";
601         }
602         print PWD "$ctx->{unix_name}:x:$ctx->{unix_uid}:65531:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false\n";
603         print PWD "nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
604 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
605 pdbtest2:x:65532:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
606 pdbtest3:x:65531:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
607 pdbtest4:x:65530:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
608 ";
609         close(PWD);
610         my $uid_rfc2307test = 65533;
611
612         open(GRP, ">$ctx->{nsswrap_group}");
613         if ($ctx->{unix_gid} != 0) {
614                 print GRP "root:x:0:\n";
615         }
616         print GRP "$ctx->{unix_name}:x:$ctx->{unix_gid}:\n";
617         print GRP "wheel:x:10:
618 users:x:65531:
619 nobody:x:65533:
620 nogroup:x:65534:nobody
621 ";
622         close(GRP);
623         my $gid_rfc2307test = 65532;
624
625         my $hostname = lc($ctx->{hostname});
626         open(HOSTS, ">>$ctx->{nsswrap_hosts}");
627         if ($hostname eq "localdc") {
628                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
629                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
630         } else {
631                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} ${hostname}\n";
632                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} ${hostname}\n";
633         }
634         close(HOSTS);
635
636         if (defined($ctx->{resolv_conf})) {
637                 open(RESOLV_CONF, ">$ctx->{resolv_conf}");
638                 print RESOLV_CONF "nameserver $ctx->{kdc_ipv4}\n";
639                 print RESOLV_CONF "nameserver $ctx->{kdc_ipv6}\n";
640                 close(RESOLV_CONF);
641         }
642
643         my $configuration = "--configfile=$ctx->{smb_conf}";
644
645 #Ensure the config file is valid before we start
646         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
647         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
648                 system("$testparm -v --suppress-prompt $configuration >&2");
649                 warn("Failed to create a valid smb.conf configuration $testparm!");
650                 return undef;
651         }
652         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) {
653                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
654                 return undef;
655         }
656
657         my $ret = {
658                 KRB5_CONFIG => $ctx->{krb5_conf},
659                 PIDDIR => $ctx->{piddir},
660                 SERVER => $ctx->{hostname},
661                 SERVER_IP => $ctx->{ipv4},
662                 SERVER_IPV6 => $ctx->{ipv6},
663                 NETBIOSNAME => $ctx->{netbiosname},
664                 DOMAIN => $ctx->{domain},
665                 USERNAME => $ctx->{username},
666                 REALM => $ctx->{realm},
667                 PASSWORD => $ctx->{password},
668                 LDAPDIR => $ctx->{ldapdir},
669                 LDAP_INSTANCE => $ctx->{ldap_instance},
670                 SELFTEST_WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
671                 NCALRPCDIR => $ctx->{ncalrpcdir},
672                 LOCKDIR => $ctx->{lockdir},
673                 STATEDIR => $ctx->{statedir},
674                 CACHEDIR => $ctx->{cachedir},
675                 PRIVATEDIR => $ctx->{privatedir},
676                 SERVERCONFFILE => $ctx->{smb_conf},
677                 CONFIGURATION => $configuration,
678                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
679                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
680                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
681                 NSS_WRAPPER_HOSTS => $ctx->{nsswrap_hosts},
682                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
683                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
684                 SAMBA_TEST_LOG_POS => 0,
685                 NSS_WRAPPER_MODULE_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
686                 NSS_WRAPPER_MODULE_FN_PREFIX => "winbind",
687                 LOCAL_PATH => $ctx->{share},
688                 UID_RFC2307TEST => $uid_rfc2307test,
689                 GID_RFC2307TEST => $gid_rfc2307test,
690                 SERVER_ROLE => $ctx->{server_role}
691         };
692
693         if (defined($ctx->{resolv_conf})) {
694                 $ret->{RESOLV_WRAPPER_CONF} = $ctx->{resolv_conf};
695         } else {
696                 $ret->{RESOLV_WRAPPER_HOSTS} = $ctx->{dns_host_file};
697         }
698
699         return $ret;
700 }
701
702 #
703 # Step2 runs the provision script
704 #
705 sub provision_raw_step2($$$)
706 {
707         my ($self, $ctx, $ret) = @_;
708
709         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
710         unless (system($provision_cmd) == 0) {
711                 warn("Unable to provision: \n$provision_cmd\n");
712                 return undef;
713         }
714
715         my $testallowed_account = "testallowed";
716         my $samba_tool_cmd = "";
717         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
718         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
719             . " user add --configfile=$ctx->{smb_conf} $testallowed_account $ctx->{password}";
720         unless (system($samba_tool_cmd) == 0) {
721                 warn("Unable to add testallowed user: \n$samba_tool_cmd\n");
722                 return undef;
723         }
724
725         my $ldbmodify = "";
726         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
727         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
728         my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
729
730         if ($ctx->{server_role} ne "domain controller") {
731                 $base_dn = "DC=$ctx->{netbiosname}";
732         }
733
734         my $user_dn = "cn=$testallowed_account,cn=users,$base_dn";
735         $testallowed_account = "testallowed account";
736         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
737         print LDIF "dn: $user_dn
738 changetype: modify
739 replace: samAccountName
740 samAccountName: $testallowed_account
741 -
742 ";
743         close(LDIF);
744
745         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
746         print LDIF "dn: $user_dn
747 changetype: modify
748 replace: userPrincipalName
749 userPrincipalName: testallowed upn\@$ctx->{realm}
750 replace: servicePrincipalName
751 servicePrincipalName: host/testallowed
752 -           
753 ";
754         close(LDIF);
755
756         $samba_tool_cmd = "";
757         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
758         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
759             . " user add --configfile=$ctx->{smb_conf} testdenied $ctx->{password}";
760         unless (system($samba_tool_cmd) == 0) {
761                 warn("Unable to add testdenied user: \n$samba_tool_cmd\n");
762                 return undef;
763         }
764
765         my $user_dn = "cn=testdenied,cn=users,$base_dn";
766         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
767         print LDIF "dn: $user_dn
768 changetype: modify
769 replace: userPrincipalName
770 userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
771 -           
772 ";
773         close(LDIF);
774
775         $samba_tool_cmd = "";
776         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
777         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
778             . " group addmembers --configfile=$ctx->{smb_conf} 'Allowed RODC Password Replication Group' '$testallowed_account'";
779         unless (system($samba_tool_cmd) == 0) {
780                 warn("Unable to add '$testallowed_account' user to 'Allowed RODC Password Replication Group': \n$samba_tool_cmd\n");
781                 return undef;
782         }
783
784         return $ret;
785 }
786
787 sub provision($$$$$$$$$$)
788 {
789         my ($self, $prefix, $server_role, $hostname,
790             $domain, $realm, $functional_level,
791             $password, $kdc_ipv4, $kdc_ipv6, $extra_smbconf_options, $extra_smbconf_shares,
792             $extra_provision_options) = @_;
793
794         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
795                                                $hostname,
796                                                $domain, $realm, $functional_level,
797                                                $password, $kdc_ipv4, $kdc_ipv6);
798
799         if (defined($extra_provision_options)) {
800                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
801         } else {
802                 push (@{$ctx->{provision_options}}, "--use-ntvfs");
803         }
804
805         $ctx->{share} = "$ctx->{prefix_abs}/share";
806         push(@{$ctx->{directories}}, "$ctx->{share}");
807         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
808         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
809
810         # precreate directories for printer drivers
811         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
812         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
813         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
814
815         my $msdfs = "no";
816         $msdfs = "yes" if ($server_role eq "domain controller");
817         $ctx->{smb_conf_extra_options} = "
818
819         max xmit = 32K
820         server max protocol = SMB2
821         host msdfs = $msdfs
822         lanman auth = yes
823         allow nt4 crypto = yes
824
825         # fruit:copyfile is a global option
826         fruit:copyfile = yes
827
828         $extra_smbconf_options
829
830 [tmp]
831         path = $ctx->{share}
832         read only = no
833         posix:sharedelay = 100000
834         posix:oplocktimeout = 3
835         posix:writetimeupdatedelay = 500000
836
837 [xcopy_share]
838         path = $ctx->{share}
839         read only = no
840         posix:sharedelay = 100000
841         posix:oplocktimeout = 3
842         posix:writetimeupdatedelay = 500000
843         create mask = 777
844         force create mode = 777
845
846 [posix_share]
847         path = $ctx->{share}
848         read only = no
849         create mask = 0777
850         force create mode = 0
851         directory mask = 0777
852         force directory mode = 0
853
854 [test1]
855         path = $ctx->{share}/test1
856         read only = no
857         posix:sharedelay = 100000
858         posix:oplocktimeout = 3
859         posix:writetimeupdatedelay = 500000
860
861 [test2]
862         path = $ctx->{share}/test2
863         read only = no
864         posix:sharedelay = 100000
865         posix:oplocktimeout = 3
866         posix:writetimeupdatedelay = 500000
867
868 [cifs]
869         path = $ctx->{share}/_ignore_cifs_
870         read only = no
871         ntvfs handler = cifs
872         cifs:server = $ctx->{netbiosname}
873         cifs:share = tmp
874         cifs:use-s4u2proxy = yes
875         # There is no username specified here, instead the client is expected
876         # to log in with kerberos, and the serverwill use delegated credentials.
877         # Or the server tries s4u2self/s4u2proxy to impersonate the client
878
879 [simple]
880         path = $ctx->{share}
881         read only = no
882         ntvfs handler = simple
883
884 [sysvol]
885         path = $ctx->{statedir}/sysvol
886         read only = no
887
888 [netlogon]
889         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
890         read only = no
891
892 [cifsposix]
893         copy = simple
894         ntvfs handler = cifsposix
895
896 [vfs_fruit]
897         path = $ctx->{share}
898         vfs objects = catia fruit streams_xattr acl_xattr
899         ea support = yes
900         fruit:ressource = file
901         fruit:metadata = netatalk
902         fruit:locking = netatalk
903         fruit:encoding = native
904
905 $extra_smbconf_shares
906 ";
907
908         if (defined($self->{ldap})) {
909                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
910                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
911
912                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
913                 $ldap_uri =~ s|/|%2F|g;
914                 $ldap_uri = "ldapi://$ldap_uri";
915                 $ctx->{ldap_uri} = $ldap_uri;
916
917                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
918         }
919
920         my $ret = $self->provision_raw_step1($ctx);
921         unless (defined $ret) {
922                 return undef;
923         }
924
925         if (defined($self->{ldap})) {
926                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
927                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
928                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
929                 if ($self->{ldap} eq "openldap") {
930                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
931                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
932
933                 } elsif ($self->{ldap} eq "fedora-ds") {
934                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
935                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
936                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
937                 }
938
939         }
940
941         return $self->provision_raw_step2($ctx, $ret);
942 }
943
944 sub provision_s4member($$$)
945 {
946         my ($self, $prefix, $dcvars) = @_;
947         print "PROVISIONING MEMBER...";
948         my $extra_smb_conf = "
949         passdb backend = samba_dsdb
950 winbindd:use external pipes = true
951
952 rpc_server:default = external
953 rpc_server:svcctl = embedded
954 rpc_server:srvsvc = embedded
955 rpc_server:eventlog = embedded
956 rpc_server:ntsvcs = embedded
957 rpc_server:winreg = embedded
958 rpc_server:spoolss = embedded
959 rpc_daemon:spoolssd = embedded
960 rpc_server:tcpip = no
961 ";
962         my $ret = $self->provision($prefix,
963                                    "member server",
964                                    "s4member",
965                                    "SAMBADOMAIN",
966                                    "samba.example.com",
967                                    "2008",
968                                    "locMEMpass3",
969                                    $dcvars->{SERVER_IP},
970                                    $dcvars->{SERVER_IPV6},
971                                    $extra_smb_conf, "", undef);
972         unless ($ret) {
973                 return undef;
974         }
975
976         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
977         my $cmd = "";
978         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
979         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
980                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
981         } else {
982                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
983         }
984         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
985         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
986         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
987         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
988
989         unless (system($cmd) == 0) {
990                 warn("Join failed\n$cmd");
991                 return undef;
992         }
993
994         $ret->{MEMBER_SERVER} = $ret->{SERVER};
995         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
996         $ret->{MEMBER_SERVER_IPV6} = $ret->{SERVER_IPV6};
997         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
998         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
999         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
1000
1001         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1002         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1003         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1004         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1005         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1006         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1007
1008         return $ret;
1009 }
1010
1011 sub provision_rpc_proxy($$$)
1012 {
1013         my ($self, $prefix, $dcvars) = @_;
1014         print "PROVISIONING RPC PROXY...";
1015
1016         my $extra_smbconf_options = "
1017         passdb backend = samba_dsdb
1018
1019         # rpc_proxy
1020         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
1021         dcerpc endpoint servers = epmapper, remote
1022         dcerpc_remote:interfaces = rpcecho
1023
1024 [cifs_to_dc]
1025         path = /tmp/_ignore_cifs_to_dc_/_none_
1026         read only = no
1027         ntvfs handler = cifs
1028         cifs:server = $dcvars->{SERVER}
1029         cifs:share = cifs
1030         cifs:use-s4u2proxy = yes
1031         # There is no username specified here, instead the client is expected
1032         # to log in with kerberos, and the serverwill use delegated credentials.
1033         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1034
1035 ";
1036
1037         my $ret = $self->provision($prefix,
1038                                    "member server",
1039                                    "localrpcproxy",
1040                                    "SAMBADOMAIN",
1041                                    "samba.example.com",
1042                                    "2008",
1043                                    "locRPCproxypass4",
1044                                    $dcvars->{SERVER_IP},
1045                                    $dcvars->{SERVER_IPV6},
1046                                    $extra_smbconf_options, "", undef);
1047
1048         unless ($ret) {
1049                 return undef;
1050         }
1051
1052         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1053
1054         # The joind runs in the context of the rpc_proxy/member for now
1055         my $cmd = "";
1056         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1057         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1058                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1059         } else {
1060                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1061         }
1062         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1063         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1064         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1065         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1066
1067         unless (system($cmd) == 0) {
1068                 warn("Join failed\n$cmd");
1069                 return undef;
1070         }
1071
1072         # Setting up delegation runs in the context of the DC for now
1073         $cmd = "";
1074         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1075         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1076         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
1077         $cmd .= " $dcvars->{CONFIGURATION}";
1078         print $cmd;
1079
1080         unless (system($cmd) == 0) {
1081                 warn("Delegation failed\n$cmd");
1082                 return undef;
1083         }
1084
1085         # Setting up delegation runs in the context of the DC for now
1086         $cmd = "";
1087         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1088         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1089         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
1090         $cmd .= " $dcvars->{CONFIGURATION}";
1091
1092         unless (system($cmd) == 0) {
1093                 warn("Delegation failed\n$cmd");
1094                 return undef;
1095         }
1096
1097         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
1098         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
1099         $ret->{RPC_PROXY_SERVER_IPV6} = $ret->{SERVER_IPV6};
1100         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
1101         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
1102         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
1103
1104         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1105         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1106         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1107         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1108         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1109         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1110
1111         return $ret;
1112 }
1113
1114 sub provision_promoted_dc($$$)
1115 {
1116         my ($self, $prefix, $dcvars) = @_;
1117         print "PROVISIONING PROMOTED DC...";
1118
1119         # We do this so that we don't run the provision.  That's the job of 'samba-tool domain dcpromo'.
1120         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1121                                                "promotedvdc",
1122                                                "SAMBADOMAIN",
1123                                                "samba.example.com",
1124                                                "2008",
1125                                                $dcvars->{PASSWORD},
1126                                                $dcvars->{SERVER_IP},
1127                                                $dcvars->{SERVER_IPV6});
1128
1129         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1130
1131         $ctx->{smb_conf_extra_options} = "
1132         max xmit = 32K
1133         server max protocol = SMB2
1134
1135 [sysvol]
1136         path = $ctx->{statedir}/sysvol
1137         read only = yes
1138
1139 [netlogon]
1140         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1141         read only = no
1142
1143 ";
1144
1145         my $ret = $self->provision_raw_step1($ctx);
1146         unless ($ret) {
1147                 return undef;
1148         }
1149
1150         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1151         my $cmd = "";
1152         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1153         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1154                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1155         } else {
1156                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1157         }
1158         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1159         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
1160         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1161         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1162
1163         unless (system($cmd) == 0) {
1164                 warn("Join failed\n$cmd");
1165                 return undef;
1166         }
1167
1168         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1169         my $cmd = "";
1170         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1171         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1172         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1173         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1174         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
1175
1176         unless (system($cmd) == 0) {
1177                 warn("Join failed\n$cmd");
1178                 return undef;
1179         }
1180
1181         $ret->{PROMOTED_DC_SERVER} = $ret->{SERVER};
1182         $ret->{PROMOTED_DC_SERVER_IP} = $ret->{SERVER_IP};
1183         $ret->{PROMOTED_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1184         $ret->{PROMOTED_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1185
1186         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1187         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1188         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1189         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1190         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1191         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1192
1193         return $ret;
1194 }
1195
1196 sub provision_vampire_dc($$$)
1197 {
1198         my ($self, $prefix, $dcvars) = @_;
1199         print "PROVISIONING VAMPIRE DC...";
1200
1201         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1202         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1203                                                "localvampiredc",
1204                                                "SAMBADOMAIN",
1205                                                "samba.example.com",
1206                                                "2008",
1207                                                $dcvars->{PASSWORD},
1208                                                $dcvars->{SERVER_IP},
1209                                                $dcvars->{SERVER_IPV6});
1210
1211         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1212
1213         $ctx->{smb_conf_extra_options} = "
1214         max xmit = 32K
1215         server max protocol = SMB2
1216
1217 [sysvol]
1218         path = $ctx->{statedir}/sysvol
1219         read only = yes
1220
1221 [netlogon]
1222         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1223         read only = no
1224
1225 ";
1226
1227         my $ret = $self->provision_raw_step1($ctx);
1228         unless ($ret) {
1229                 return undef;
1230         }
1231
1232         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1233         my $cmd = "";
1234         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1235         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1236                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1237         } else {
1238                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1239         }
1240         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1241         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1242         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1243         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1244
1245         unless (system($cmd) == 0) {
1246                 warn("Join failed\n$cmd");
1247                 return undef;
1248         }
1249
1250         $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1251         $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1252         $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1253         $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1254
1255         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1256         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1257         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1258         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1259         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1260         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1261         $ret->{DC_REALM} = $dcvars->{DC_REALM};
1262
1263         return $ret;
1264 }
1265
1266 sub provision_subdom_dc($$$)
1267 {
1268         my ($self, $prefix, $dcvars) = @_;
1269         print "PROVISIONING SUBDOMAIN DC...";
1270
1271         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1272         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1273                                                "localsubdc",
1274                                                "SAMBASUBDOM",
1275                                                "sub.samba.example.com",
1276                                                "2008",
1277                                                $dcvars->{PASSWORD},
1278                                                undef);
1279
1280         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1281
1282         $ctx->{smb_conf_extra_options} = "
1283         max xmit = 32K
1284         server max protocol = SMB2
1285
1286 [sysvol]
1287         path = $ctx->{statedir}/sysvol
1288         read only = yes
1289
1290 [netlogon]
1291         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1292         read only = no
1293
1294 ";
1295
1296         my $ret = $self->provision_raw_step1($ctx);
1297         unless ($ret) {
1298                 return undef;
1299         }
1300
1301         Samba::mk_krb5_conf($ctx);
1302
1303         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1304         my $cmd = "";
1305         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1306         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1307                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1308         } else {
1309                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1310         }
1311         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1312         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{dnsname} subdomain ";
1313         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1314         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1315         $cmd .= " --adminpass=$ret->{PASSWORD}";
1316
1317         unless (system($cmd) == 0) {
1318                 warn("Join failed\n$cmd");
1319                 return undef;
1320         }
1321
1322         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1323         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1324         $ret->{SUBDOM_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1325         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1326
1327         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1328         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1329         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1330         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1331         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1332         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1333
1334         return $ret;
1335 }
1336
1337 sub provision_ad_dc_ntvfs($$)
1338 {
1339         my ($self, $prefix) = @_;
1340
1341         # We keep the old 'winbind' name here in server services to
1342         # ensure upgrades which used that name still work with the now
1343         # alias.
1344
1345         print "PROVISIONING AD DC (NTVFS)...";
1346         my $extra_conf_options = "netbios aliases = localDC1-a
1347         server services = +winbind -winbindd
1348         ldap server require strong auth = allow_sasl_over_tls
1349         ";
1350         my $ret = $self->provision($prefix,
1351                                    "domain controller",
1352                                    "localdc",
1353                                    "SAMBADOMAIN",
1354                                    "samba.example.com",
1355                                    "2008",
1356                                    "locDCpass1",
1357                                    undef,
1358                                    undef,
1359                                    $extra_conf_options,
1360                                    "",
1361                                    undef);
1362
1363         return undef unless(defined $ret);
1364         unless($self->add_wins_config("$prefix/private")) {
1365                 warn("Unable to add wins configuration");
1366                 return undef;
1367         }
1368         $ret->{NETBIOSALIAS} = "localdc1-a";
1369         $ret->{DC_SERVER} = $ret->{SERVER};
1370         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1371         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1372         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1373         $ret->{DC_USERNAME} = $ret->{USERNAME};
1374         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1375         $ret->{DC_REALM} = $ret->{REALM};
1376
1377         return $ret;
1378 }
1379
1380 sub provision_fl2000dc($$)
1381 {
1382         my ($self, $prefix) = @_;
1383
1384         print "PROVISIONING DC WITH FOREST LEVEL 2000...";
1385         my $extra_conf_options = "
1386         spnego:simulate_w2k=yes
1387         ntlmssp_server:force_old_spnego=yes
1388 ";
1389         my $ret = $self->provision($prefix,
1390                                    "domain controller",
1391                                    "dc5",
1392                                    "SAMBA2000",
1393                                    "samba2000.example.com",
1394                                    "2000",
1395                                    "locDCpass5",
1396                                    undef,
1397                                    undef,
1398                                    $extra_conf_options,
1399                                    "",
1400                                    undef);
1401
1402         unless($self->add_wins_config("$prefix/private")) {
1403                 warn("Unable to add wins configuration");
1404                 return undef;
1405         }
1406         $ret->{DC_SERVER} = $ret->{SERVER};
1407         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1408         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1409         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1410         $ret->{DC_USERNAME} = $ret->{USERNAME};
1411         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1412         $ret->{DC_REALM} = $ret->{REALM};
1413
1414         return $ret;
1415 }
1416
1417 sub provision_fl2003dc($$$)
1418 {
1419         my ($self, $prefix, $dcvars) = @_;
1420         my $swiface1 = Samba::get_interface("fakednsforwarder1");
1421         my $swiface2 = Samba::get_interface("fakednsforwarder2");
1422
1423         print "PROVISIONING DC WITH FOREST LEVEL 2003...";
1424         my $extra_conf_options = "allow dns updates = nonsecure and secure
1425         dns forwarder = 127.0.0.$swiface1 127.0.0.$swiface2";
1426         my $ret = $self->provision($prefix,
1427                                    "domain controller",
1428                                    "dc6",
1429                                    "SAMBA2003",
1430                                    "samba2003.example.com",
1431                                    "2003",
1432                                    "locDCpass6",
1433                                    undef,
1434                                    undef,
1435                                    $extra_conf_options,
1436                                    "",
1437                                    undef);
1438
1439         unless (defined $ret) {
1440                 return undef;
1441         }
1442
1443         $ret->{DC_SERVER} = $ret->{SERVER};
1444         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1445         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1446         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1447         $ret->{DC_USERNAME} = $ret->{USERNAME};
1448         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1449         $ret->{DNS_FORWARDER1} = "127.0.0.$swiface1";
1450         $ret->{DNS_FORWARDER2} = "127.0.0.$swiface2";
1451
1452         my @samba_tool_options;
1453         push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
1454         push (@samba_tool_options, "domain");
1455         push (@samba_tool_options, "passwordsettings");
1456         push (@samba_tool_options, "set");
1457         push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
1458         push (@samba_tool_options, "--min-pwd-age=0");
1459         push (@samba_tool_options, "--history-length=1");
1460
1461         my $samba_tool_cmd = join(" ", @samba_tool_options);
1462
1463         unless (system($samba_tool_cmd) == 0) {
1464                 warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
1465                 return undef;
1466         }
1467
1468         unless($self->add_wins_config("$prefix/private")) {
1469                 warn("Unable to add wins configuration");
1470                 return undef;
1471         }
1472
1473         return $ret;
1474 }
1475
1476 sub provision_fl2008r2dc($$$)
1477 {
1478         my ($self, $prefix, $dcvars) = @_;
1479
1480         print "PROVISIONING DC WITH FOREST LEVEL 2008r2...";
1481         my $extra_conf_options = "ldap server require strong auth = no";
1482         my $ret = $self->provision($prefix,
1483                                    "domain controller",
1484                                    "dc7",
1485                                    "SAMBA2008R2",
1486                                    "samba2008R2.example.com",
1487                                    "2008_R2",
1488                                    "locDCpass7",
1489                                    undef,
1490                                    undef,
1491                                    $extra_conf_options,
1492                                    "",
1493                                    undef);
1494
1495         unless ($self->add_wins_config("$prefix/private")) {
1496                 warn("Unable to add wins configuration");
1497                 return undef;
1498         }
1499         $ret->{DC_SERVER} = $ret->{SERVER};
1500         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1501         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1502         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1503         $ret->{DC_USERNAME} = $ret->{USERNAME};
1504         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1505         $ret->{DC_REALM} = $ret->{REALM};
1506
1507         return $ret;
1508 }
1509
1510
1511 sub provision_rodc($$$)
1512 {
1513         my ($self, $prefix, $dcvars) = @_;
1514         print "PROVISIONING RODC...";
1515
1516         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1517         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1518                                                "rodc",
1519                                                "SAMBADOMAIN",
1520                                                "samba.example.com",
1521                                                "2008",
1522                                                $dcvars->{PASSWORD},
1523                                                $dcvars->{SERVER_IP},
1524                                                $dcvars->{SERVER_IPV6});
1525         unless ($ctx) {
1526                 return undef;
1527         }
1528
1529         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1530
1531         $ctx->{share} = "$ctx->{prefix_abs}/share";
1532         push(@{$ctx->{directories}}, "$ctx->{share}");
1533
1534         $ctx->{smb_conf_extra_options} = "
1535         max xmit = 32K
1536         server max protocol = SMB2
1537
1538 [sysvol]
1539         path = $ctx->{statedir}/sysvol
1540         read only = yes
1541
1542 [netlogon]
1543         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1544         read only = yes
1545
1546 [tmp]
1547         path = $ctx->{share}
1548         read only = no
1549         posix:sharedelay = 10000
1550         posix:oplocktimeout = 3
1551         posix:writetimeupdatedelay = 50000
1552
1553 ";
1554
1555         my $ret = $self->provision_raw_step1($ctx);
1556         unless ($ret) {
1557                 return undef;
1558         }
1559
1560         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1561         my $cmd = "";
1562         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1563         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1564                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1565         } else {
1566                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1567         }
1568         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1569         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1570         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1571         $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
1572
1573         unless (system($cmd) == 0) {
1574                 warn("RODC join failed\n$cmd");
1575                 return undef;
1576         }
1577
1578         # This ensures deterministic behaviour for tests that want to have the 'testallowed account'
1579         # user password verified on the RODC
1580         my $testallowed_account = "testallowed account";
1581         $cmd = "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1582         $cmd .= "$samba_tool rodc preload '$testallowed_account' $ret->{CONFIGURATION}";
1583         $cmd .= " --server=$dcvars->{DC_SERVER}";
1584
1585         unless (system($cmd) == 0) {
1586                 warn("RODC join failed\n$cmd");
1587                 return undef;
1588         }
1589
1590         # we overwrite the kdc after the RODC join
1591         # so that use the RODC as kdc and test
1592         # the proxy code
1593         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1594         $ctx->{kdc_ipv6} = $ret->{SERVER_IPV6};
1595         Samba::mk_krb5_conf($ctx);
1596
1597         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1598         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1599         $ret->{RODC_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1600         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1601
1602         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1603         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1604         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1605         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1606         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1607         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1608
1609         return $ret;
1610 }
1611
1612 sub provision_ad_dc($$)
1613 {
1614         my ($self, $prefix) = @_;
1615
1616         my $prefix_abs = abs_path($prefix);
1617
1618         my $bindir_abs = abs_path($self->{bindir});
1619         my $lockdir="$prefix_abs/lockdir";
1620         my $conffile="$prefix_abs/etc/smb.conf";
1621
1622         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
1623         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
1624
1625         my $extra_smbconf_options = "
1626         server services = -smb +s3fs
1627         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
1628
1629         dbwrap_tdb_mutexes:* = yes
1630         ${require_mutexes}
1631
1632         kernel oplocks = no
1633         kernel change notify = no
1634
1635         logging = file
1636         printing = bsd
1637         printcap name = /dev/null
1638
1639         max protocol = SMB3
1640         read only = no
1641         server signing = auto
1642
1643         smbd:sharedelay = 100000
1644         smbd:writetimeupdatedelay = 500000
1645         create mask = 755
1646         dos filemode = yes
1647
1648         dcerpc endpoint servers = -winreg -srvsvc
1649
1650         printcap name = /dev/null
1651
1652         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
1653         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
1654
1655         printing = vlp
1656         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1657         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1658         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1659         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1660         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1661         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1662         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1663         lpq cache time = 0
1664         print notify backchannel = yes
1665 ";
1666
1667         my $extra_smbconf_shares = "
1668
1669 [tmpenc]
1670         copy = tmp
1671         smb encrypt = required
1672
1673 [tmpcase]
1674         copy = tmp
1675         case sensitive = yes
1676
1677 [tmpguest]
1678         copy = tmp
1679         guest ok = yes
1680
1681 [hideunread]
1682         copy = tmp
1683         hide unreadable = yes
1684
1685 [durable]
1686         copy = tmp
1687         kernel share modes = no
1688         kernel oplocks = no
1689         posix locking = no
1690
1691 [print\$]
1692         copy = tmp
1693
1694 [print1]
1695         copy = tmp
1696         printable = yes
1697
1698 [print2]
1699         copy = print1
1700 [print3]
1701         copy = print1
1702 [lp]
1703         copy = print1
1704 ";
1705
1706         print "PROVISIONING AD DC...";
1707         my $ret = $self->provision($prefix,
1708                                    "domain controller",
1709                                    "addc",
1710                                    "ADDOMAIN",
1711                                    "addom.samba.example.com",
1712                                    "2008",
1713                                    "locDCpass1",
1714                                    undef,
1715                                    undef,
1716                                    $extra_smbconf_options,
1717                                    $extra_smbconf_shares,
1718                                    undef);
1719
1720         return undef unless(defined $ret);
1721         unless($self->add_wins_config("$prefix/private")) {
1722                 warn("Unable to add wins configuration");
1723                 return undef;
1724         }
1725
1726         $ret->{DC_SERVER} = $ret->{SERVER};
1727         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1728         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1729         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1730         $ret->{DC_USERNAME} = $ret->{USERNAME};
1731         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1732
1733         return $ret;
1734 }
1735
1736 sub provision_chgdcpass($$)
1737 {
1738         my ($self, $prefix) = @_;
1739
1740         print "PROVISIONING CHGDCPASS...";
1741         my $extra_provision_options = undef;
1742         push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
1743         my $ret = $self->provision($prefix,
1744                                    "domain controller",
1745                                    "chgdcpass",
1746                                    "CHDCDOMAIN",
1747                                    "chgdcpassword.samba.example.com",
1748                                    "2008",
1749                                    "chgDCpass1",
1750                                    undef,
1751                                    undef,
1752                                    "",
1753                                    "",
1754                                    $extra_provision_options);
1755
1756         return undef unless(defined $ret);
1757         unless($self->add_wins_config("$prefix/private")) {
1758                 warn("Unable to add wins configuration");
1759                 return undef;
1760         }
1761         
1762         # Remove secrets.tdb from this environment to test that we
1763         # still start up on systems without the new matching
1764         # secrets.tdb records.
1765         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
1766                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
1767                 return undef;
1768         }
1769             
1770         $ret->{DC_SERVER} = $ret->{SERVER};
1771         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1772         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1773         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1774         $ret->{DC_USERNAME} = $ret->{USERNAME};
1775         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1776
1777         return $ret;
1778 }
1779
1780 sub teardown_env($$)
1781 {
1782         my ($self, $envvars) = @_;
1783         my $pid;
1784
1785         # This should cause samba to terminate gracefully
1786         close($envvars->{STDIN_PIPE});
1787
1788         $pid = $envvars->{SAMBA_PID};
1789         my $count = 0;
1790         my $childpid;
1791
1792         # This should give it time to write out the gcov data
1793         until ($count > 30) {
1794             if (Samba::cleanup_child($pid, "samba") == -1) {
1795                 last;
1796             }
1797             sleep(1);
1798             $count++;
1799         }
1800
1801         if ($count > 30 || kill(0, $pid)) {
1802             kill "TERM", $pid;
1803
1804             until ($count > 40) {
1805                 if (Samba::cleanup_child($pid, "samba") == -1) {
1806                     last;
1807                 }
1808                 sleep(1);
1809                 $count++;
1810             }
1811             # If it is still around, kill it
1812             warn "server process $pid took more than $count seconds to exit, killing\n";
1813             kill 9, $pid;
1814         }
1815
1816         $self->slapd_stop($envvars) if ($self->{ldap});
1817
1818         print $self->getlog_env($envvars);
1819
1820         return;
1821 }
1822
1823 sub getlog_env($$)
1824 {
1825         my ($self, $envvars) = @_;
1826         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME} pid $envvars->{SAMBA_PID}\n";
1827         my $out = $title;
1828
1829         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
1830
1831         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
1832         while (<LOG>) {
1833                 $out .= $_;
1834         }
1835         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
1836         close(LOG);
1837
1838         return "" if $out eq $title;
1839
1840         return $out;
1841 }
1842
1843 sub check_env($$)
1844 {
1845         my ($self, $envvars) = @_;
1846         my $samba_pid = $envvars->{SAMBA_PID};
1847
1848         if (not defined($samba_pid)) {
1849             return 0;
1850         } elsif ($samba_pid > 0) {
1851             my $childpid = Samba::cleanup_child($samba_pid, "samba");
1852
1853             if ($childpid == 0) {
1854                 return 1;
1855             }
1856             return 0;
1857         } else {
1858             return 1;
1859         }
1860
1861 }
1862
1863 sub setup_env($$$)
1864 {
1865         my ($self, $envname, $path) = @_;
1866         my $target3 = $self->{target3};
1867
1868         $ENV{ENVNAME} = $envname;
1869
1870         if (defined($self->{vars}->{$envname})) {
1871                 return $self->{vars}->{$envname};
1872         }
1873
1874         if ($envname eq "ad_dc_ntvfs") {
1875                 return $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1876         } elsif ($envname eq "fl2000dc") {
1877                 return $self->setup_fl2000dc("$path/fl2000dc");
1878         } elsif ($envname eq "fl2003dc") {
1879                 if (not defined($self->{vars}->{ad_dc})) {
1880                         $self->setup_ad_dc("$path/ad_dc");
1881                 }
1882                 return $self->setup_fl2003dc("$path/fl2003dc", $self->{vars}->{ad_dc});
1883         } elsif ($envname eq "fl2008r2dc") {
1884                 if (not defined($self->{vars}->{ad_dc})) {
1885                         $self->setup_ad_dc("$path/ad_dc");
1886                 }
1887                 return $self->setup_fl2008r2dc("$path/fl2008r2dc", $self->{vars}->{ad_dc});
1888         } elsif ($envname eq "rpc_proxy") {
1889                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1890                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1891                 }
1892                 return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{ad_dc_ntvfs});
1893         } elsif ($envname eq "vampire_dc") {
1894                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1895                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1896                 }
1897                 return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{ad_dc_ntvfs});
1898         } elsif ($envname eq "promoted_dc") {
1899                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1900                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1901                 }
1902                 return $self->setup_promoted_dc("$path/promoted_dc", $self->{vars}->{ad_dc_ntvfs});
1903         } elsif ($envname eq "subdom_dc") {
1904                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1905                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1906                 }
1907                 return $self->setup_subdom_dc("$path/subdom_dc", $self->{vars}->{ad_dc_ntvfs});
1908         } elsif ($envname eq "s4member") {
1909                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1910                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1911                 }
1912                 return $self->setup_s4member("$path/s4member", $self->{vars}->{ad_dc_ntvfs});
1913         } elsif ($envname eq "rodc") {
1914                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1915                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1916                 }
1917                 return $self->setup_rodc("$path/rodc", $self->{vars}->{ad_dc_ntvfs});
1918         } elsif ($envname eq "chgdcpass") {
1919                 return $self->setup_chgdcpass("$path/chgdcpass", $self->{vars}->{chgdcpass});
1920         } elsif ($envname eq "ad_member") {
1921                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1922                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1923                 }
1924                 return $target3->setup_admember("$path/ad_member", $self->{vars}->{ad_dc_ntvfs}, 29);
1925         } elsif ($envname eq "ad_dc") {
1926                 return $self->setup_ad_dc("$path/ad_dc");
1927         } elsif ($envname eq "ad_dc_no_nss") {
1928                 return $self->setup_ad_dc("$path/ad_dc_no_nss", "no_nss");
1929         } elsif ($envname eq "ad_member_rfc2307") {
1930                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
1931                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
1932                 }
1933                 return $target3->setup_admember_rfc2307("$path/ad_member_rfc2307",
1934                                                         $self->{vars}->{ad_dc_ntvfs}, 34);
1935         } elsif ($envname eq "none") {
1936                 return $self->setup_none("$path/none");
1937         } else {
1938                 return "UNKNOWN";
1939         }
1940 }
1941
1942 sub setup_s4member($$$)
1943 {
1944         my ($self, $path, $dc_vars) = @_;
1945
1946         my $env = $self->provision_s4member($path, $dc_vars);
1947
1948         if (defined $env) {
1949                 if (not defined($self->check_or_start($env, "standard"))) {
1950                         return undef;
1951                 }
1952
1953                 $self->{vars}->{s4member} = $env;
1954         }
1955
1956         return $env;
1957 }
1958
1959 sub setup_rpc_proxy($$$)
1960 {
1961         my ($self, $path, $dc_vars) = @_;
1962
1963         my $env = $self->provision_rpc_proxy($path, $dc_vars);
1964
1965         if (defined $env) {
1966                 if (not defined($self->check_or_start($env, "standard"))) {
1967                         return undef;
1968                 }
1969
1970                 $self->{vars}->{rpc_proxy} = $env;
1971         }
1972         return $env;
1973 }
1974
1975 sub setup_ad_dc_ntvfs($$)
1976 {
1977         my ($self, $path) = @_;
1978
1979         my $env = $self->provision_ad_dc_ntvfs($path);
1980         if (defined $env) {
1981                 if (not defined($self->check_or_start($env, "standard"))) {
1982                     warn("Failed to start ad_dc_ntvfs");
1983                         return undef;
1984                 }
1985
1986                 $self->{vars}->{ad_dc_ntvfs} = $env;
1987         }
1988         return $env;
1989 }
1990
1991 sub setup_chgdcpass($$)
1992 {
1993         my ($self, $path) = @_;
1994
1995         my $env = $self->provision_chgdcpass($path);
1996         if (defined $env) {
1997                 if (not defined($self->check_or_start($env, "standard"))) {
1998                         return undef;
1999                 }
2000
2001                 $self->{vars}->{chgdcpass} = $env;
2002         }
2003         return $env;
2004 }
2005
2006 sub setup_fl2000dc($$)
2007 {
2008         my ($self, $path) = @_;
2009
2010         my $env = $self->provision_fl2000dc($path);
2011         if (defined $env) {
2012                 if (not defined($self->check_or_start($env, "standard"))) {
2013                         return undef;
2014                 }
2015
2016                 $self->{vars}->{fl2000dc} = $env;
2017         }
2018
2019         return $env;
2020 }
2021
2022 sub setup_fl2003dc($$$)
2023 {
2024         my ($self, $path, $dc_vars) = @_;
2025
2026         my $env = $self->provision_fl2003dc($path);
2027
2028         if (defined $env) {
2029                 if (not defined($self->check_or_start($env, "standard"))) {
2030                         return undef;
2031                 }
2032
2033                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2034
2035                 $self->{vars}->{fl2003dc} = $env;
2036         }
2037         return $env;
2038 }
2039
2040 sub setup_fl2008r2dc($$$)
2041 {
2042         my ($self, $path, $dc_vars) = @_;
2043
2044         my $env = $self->provision_fl2008r2dc($path);
2045
2046         if (defined $env) {
2047                 if (not defined($self->check_or_start($env, "standard"))) {
2048                         return undef;
2049                 }
2050
2051                 my $upn_array = ["$env->{REALM}.upn"];
2052                 my $spn_array = ["$env->{REALM}.spn"];
2053
2054                 $self->setup_namespaces($env, $upn_array, $spn_array);
2055
2056                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2057
2058                 $self->{vars}->{fl2008r2dc} = $env;
2059         }
2060
2061         return $env;
2062 }
2063
2064 sub setup_vampire_dc($$$)
2065 {
2066         my ($self, $path, $dc_vars) = @_;
2067
2068         my $env = $self->provision_vampire_dc($path, $dc_vars);
2069
2070         if (defined $env) {
2071                 if (not defined($self->check_or_start($env, "single"))) {
2072                         return undef;
2073                 }
2074
2075                 $self->{vars}->{vampire_dc} = $env;
2076
2077                 # force replicated DC to update repsTo/repsFrom
2078                 # for vampired partitions
2079                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2080                 my $cmd = "";
2081                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2082                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2083                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2084                 } else {
2085                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2086                 }
2087                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2088                 $cmd .= " $samba_tool drs kcc -k no $env->{DC_SERVER}";
2089                 $cmd .= " $env->{CONFIGURATION}";
2090                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2091                 unless (system($cmd) == 0) {
2092                         warn("Failed to exec kcc\n$cmd");
2093                         return undef;
2094                 }
2095
2096                 # as 'vampired' dc may add data in its local replica
2097                 # we need to synchronize data between DCs
2098                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2099                 $cmd = "";
2100                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2101                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2102                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2103                 } else {
2104                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2105                 }
2106                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2107                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2108                 $cmd .= " $dc_vars->{CONFIGURATION}";
2109                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2110                 # replicate Configuration NC
2111                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2112                 unless(system($cmd_repl) == 0) {
2113                         warn("Failed to replicate\n$cmd_repl");
2114                         return undef;
2115                 }
2116                 # replicate Default NC
2117                 $cmd_repl = "$cmd \"$base_dn\"";
2118                 unless(system($cmd_repl) == 0) {
2119                         warn("Failed to replicate\n$cmd_repl");
2120                         return undef;
2121                 }
2122         }
2123
2124         return $env;
2125 }
2126
2127 sub setup_promoted_dc($$$)
2128 {
2129         my ($self, $path, $dc_vars) = @_;
2130
2131         my $env = $self->provision_promoted_dc($path, $dc_vars);
2132
2133         if (defined $env) {
2134                 if (not defined($self->check_or_start($env, "single"))) {
2135                         return undef;
2136                 }
2137
2138                 $self->{vars}->{promoted_dc} = $env;
2139
2140                 # force source and replicated DC to update repsTo/repsFrom
2141                 # for vampired partitions
2142                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2143                 my $cmd = "";
2144                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2145                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2146                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
2147                 $cmd .= " $env->{CONFIGURATION}";
2148                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2149                 unless (system($cmd) == 0) {
2150                         warn("Failed to exec kcc\n$cmd");
2151                         return undef;
2152                 }
2153
2154                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2155                 my $cmd = "";
2156                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2157                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2158                 $cmd .= " $samba_tool drs kcc $env->{SERVER}";
2159                 $cmd .= " $env->{CONFIGURATION}";
2160                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2161                 unless (system($cmd) == 0) {
2162                         warn("Failed to exec kcc\n$cmd");
2163                         return undef;
2164                 }
2165
2166                 # as 'vampired' dc may add data in its local replica
2167                 # we need to synchronize data between DCs
2168                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2169                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2170                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2171                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2172                 $cmd .= " $dc_vars->{CONFIGURATION}";
2173                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2174                 # replicate Configuration NC
2175                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2176                 unless(system($cmd_repl) == 0) {
2177                         warn("Failed to replicate\n$cmd_repl");
2178                         return undef;
2179                 }
2180                 # replicate Default NC
2181                 $cmd_repl = "$cmd \"$base_dn\"";
2182                 unless(system($cmd_repl) == 0) {
2183                         warn("Failed to replicate\n$cmd_repl");
2184                         return undef;
2185                 }
2186         }
2187
2188         return $env;
2189 }
2190
2191 sub setup_subdom_dc($$$)
2192 {
2193         my ($self, $path, $dc_vars) = @_;
2194
2195         my $env = $self->provision_subdom_dc($path, $dc_vars);
2196
2197         if (defined $env) {
2198                 if (not defined($self->check_or_start($env, "single"))) {
2199                         return undef;
2200                 }
2201
2202                 $self->{vars}->{subdom_dc} = $env;
2203
2204                 # force replicated DC to update repsTo/repsFrom
2205                 # for primary domain partitions
2206                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2207                 my $cmd = "";
2208                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2209                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2210                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
2211                 $cmd .= " $env->{CONFIGURATION}";
2212                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2213                 unless (system($cmd) == 0) {
2214                         warn("Failed to exec kcc\n$cmd");
2215                         return undef;
2216                 }
2217
2218                 # as 'subdomain' dc may add data in its local replica
2219                 # we need to synchronize data between DCs
2220                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
2221                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2222                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2223                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2224                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
2225                 $cmd .= " $dc_vars->{CONFIGURATION}";
2226                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2227                 # replicate Configuration NC
2228                 my $cmd_repl = "$cmd \"$config_dn\"";
2229                 unless(system($cmd_repl) == 0) {
2230                         warn("Failed to replicate\n$cmd_repl");
2231                         return undef;
2232                 }
2233                 # replicate Default NC
2234                 $cmd_repl = "$cmd \"$base_dn\"";
2235                 unless(system($cmd_repl) == 0) {
2236                         warn("Failed to replicate\n$cmd_repl");
2237                         return undef;
2238                 }
2239         }
2240
2241         return $env;
2242 }
2243
2244 sub setup_rodc($$$)
2245 {
2246         my ($self, $path, $dc_vars) = @_;
2247
2248         my $env = $self->provision_rodc($path, $dc_vars);
2249
2250         unless ($env) {
2251                 return undef;
2252         }
2253
2254         if (not defined($self->check_or_start($env, "single"))) {
2255             return undef;
2256         }
2257
2258         # force source and replicated DC to update repsTo/repsFrom
2259         # for vampired partitions
2260         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2261         my $cmd = "";
2262         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2263         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2264         $cmd .= " $samba_tool drs kcc -k no $env->{DC_SERVER}";
2265         $cmd .= " $env->{CONFIGURATION}";
2266         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2267         unless (system($cmd) == 0) {
2268             warn("Failed to exec kcc\n$cmd");
2269             return undef;
2270         }
2271
2272         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2273         my $cmd = "";
2274         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2275         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2276         $cmd .= " $samba_tool drs kcc -k no $env->{SERVER}";
2277         $cmd .= " $env->{CONFIGURATION}";
2278         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2279         unless (system($cmd) == 0) {
2280             warn("Failed to exec kcc\n$cmd");
2281             return undef;
2282         }
2283
2284         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2285         $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2286         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2287         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2288         $cmd .= " $dc_vars->{CONFIGURATION}";
2289         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2290         # replicate Configuration NC
2291         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2292         unless(system($cmd_repl) == 0) {
2293             warn("Failed to replicate\n$cmd_repl");
2294             return undef;
2295         }
2296         # replicate Default NC
2297         $cmd_repl = "$cmd \"$base_dn\"";
2298         unless(system($cmd_repl) == 0) {
2299             warn("Failed to replicate\n$cmd_repl");
2300             return undef;
2301         }
2302
2303         $self->{vars}->{rodc} = $env;
2304
2305         return $env;
2306 }
2307
2308 sub setup_ad_dc($$)
2309 {
2310         my ($self, $path, $no_nss) = @_;
2311
2312         # If we didn't build with ADS, pretend this env was never available
2313         if (not $self->{target3}->have_ads()) {
2314                return "UNKNOWN";
2315         }
2316
2317         my $env = $self->provision_ad_dc($path);
2318         unless ($env) {
2319                 return undef;
2320         }
2321
2322         if (defined($no_nss) and $no_nss) {
2323                 $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2324                 $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2325         }
2326
2327         if (not defined($self->check_or_start($env, "single"))) {
2328             return undef;
2329         }
2330
2331         my $upn_array = ["$env->{REALM}.upn"];
2332         my $spn_array = ["$env->{REALM}.spn"];
2333
2334         $self->setup_namespaces($env, $upn_array, $spn_array);
2335
2336         $self->{vars}->{ad_dc} = $env;
2337         return $env;
2338 }
2339
2340 sub setup_none($$)
2341 {
2342         my ($self, $path) = @_;
2343
2344         my $ret = {
2345                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
2346                 SAMBA_PID => -1,
2347         }
2348 }
2349
2350 1;