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