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