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