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