14e312f858647369491e16cda93f4e61e798b41a
[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", "jane"];
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         my $ldbmodify = "";
896         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
897         $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
898         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
899
900         my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
901         my $user_dn = "cn=jane,cn=users,$base_dn";
902
903         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
904         print LDIF "dn: $user_dn
905 changetype: modify
906 replace: userPrincipalName
907 userPrincipalName: jane.doe\@$ctx->{realm}
908 -
909 ";
910         close(LDIF);
911
912         return $ret;
913 }
914
915 sub provision($$$$$$$$$$)
916 {
917         my ($self, $prefix, $server_role, $hostname,
918             $domain, $realm, $functional_level,
919             $password, $kdc_ipv4, $kdc_ipv6, $extra_smbconf_options, $extra_smbconf_shares,
920             $extra_provision_options) = @_;
921
922         my $samsid = Samba::random_domain_sid();
923
924         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
925                                                $hostname,
926                                                $domain, $realm,
927                                                $samsid,
928                                                $functional_level,
929                                                $password, $kdc_ipv4, $kdc_ipv6);
930
931         if (defined($extra_provision_options)) {
932                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
933         } else {
934                 push (@{$ctx->{provision_options}}, "--use-ntvfs");
935         }
936
937         $ctx->{share} = "$ctx->{prefix_abs}/share";
938         push(@{$ctx->{directories}}, "$ctx->{share}");
939         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
940         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
941
942         # precreate directories for printer drivers
943         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
944         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
945         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
946
947         my $msdfs = "no";
948         $msdfs = "yes" if ($server_role eq "domain controller");
949         $ctx->{smb_conf_extra_options} = "
950
951         max xmit = 32K
952         server max protocol = SMB2
953         host msdfs = $msdfs
954         lanman auth = yes
955
956         # fruit:copyfile is a global option
957         fruit:copyfile = yes
958
959         $extra_smbconf_options
960
961 [tmp]
962         path = $ctx->{share}
963         read only = no
964         posix:sharedelay = 100000
965         posix:oplocktimeout = 3
966         posix:writetimeupdatedelay = 500000
967
968 [xcopy_share]
969         path = $ctx->{share}
970         read only = no
971         posix:sharedelay = 100000
972         posix:oplocktimeout = 3
973         posix:writetimeupdatedelay = 500000
974         create mask = 777
975         force create mode = 777
976
977 [posix_share]
978         path = $ctx->{share}
979         read only = no
980         create mask = 0777
981         force create mode = 0
982         directory mask = 0777
983         force directory mode = 0
984
985 [test1]
986         path = $ctx->{share}/test1
987         read only = no
988         posix:sharedelay = 100000
989         posix:oplocktimeout = 3
990         posix:writetimeupdatedelay = 500000
991
992 [test2]
993         path = $ctx->{share}/test2
994         read only = no
995         posix:sharedelay = 100000
996         posix:oplocktimeout = 3
997         posix:writetimeupdatedelay = 500000
998
999 [cifs]
1000         path = $ctx->{share}/_ignore_cifs_
1001         read only = no
1002         ntvfs handler = cifs
1003         cifs:server = $ctx->{netbiosname}
1004         cifs:share = tmp
1005         cifs:use-s4u2proxy = yes
1006         # There is no username specified here, instead the client is expected
1007         # to log in with kerberos, and the serverwill use delegated credentials.
1008         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1009
1010 [simple]
1011         path = $ctx->{share}
1012         read only = no
1013         ntvfs handler = simple
1014
1015 [sysvol]
1016         path = $ctx->{statedir}/sysvol
1017         read only = no
1018
1019 [netlogon]
1020         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1021         read only = no
1022
1023 [cifsposix]
1024         copy = simple
1025         ntvfs handler = cifsposix
1026
1027 [vfs_fruit]
1028         path = $ctx->{share}
1029         vfs objects = catia fruit streams_xattr acl_xattr
1030         ea support = yes
1031         fruit:resource = file
1032         fruit:metadata = netatalk
1033         fruit:locking = netatalk
1034         fruit:encoding = native
1035
1036 $extra_smbconf_shares
1037 ";
1038
1039         if (defined($self->{ldap})) {
1040                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
1041                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
1042
1043                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
1044                 $ldap_uri =~ s|/|%2F|g;
1045                 $ldap_uri = "ldapi://$ldap_uri";
1046                 $ctx->{ldap_uri} = $ldap_uri;
1047
1048                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
1049         }
1050
1051         my $ret = $self->provision_raw_step1($ctx);
1052         unless (defined $ret) {
1053                 return undef;
1054         }
1055
1056         if (defined($self->{ldap})) {
1057                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
1058                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
1059                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
1060                 if ($self->{ldap} eq "openldap") {
1061                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
1062                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
1063
1064                 } elsif ($self->{ldap} eq "fedora-ds") {
1065                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
1066                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
1067                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
1068                 }
1069
1070         }
1071
1072         return $self->provision_raw_step2($ctx, $ret);
1073 }
1074
1075 sub provision_s4member($$$$$)
1076 {
1077         my ($self, $prefix, $dcvars, $hostname, $more_conf) = @_;
1078         print "PROVISIONING MEMBER...\n";
1079         my $extra_smb_conf = "
1080         passdb backend = samba_dsdb
1081 winbindd:use external pipes = true
1082
1083 # the source4 smb server doesn't allow signing by default
1084 server signing = enabled
1085 raw NTLMv2 auth = yes
1086
1087 rpc_server:default = external
1088 rpc_server:svcctl = embedded
1089 rpc_server:srvsvc = embedded
1090 rpc_server:eventlog = embedded
1091 rpc_server:ntsvcs = embedded
1092 rpc_server:winreg = embedded
1093 rpc_server:spoolss = embedded
1094 rpc_daemon:spoolssd = embedded
1095 rpc_server:tcpip = no
1096 ";
1097         if ($more_conf) {
1098                 $extra_smb_conf = $extra_smb_conf . $more_conf . "\n";
1099         }
1100         my $ret = $self->provision($prefix,
1101                                    "member server",
1102                                    $hostname,
1103                                    $dcvars->{DOMAIN},
1104                                    $dcvars->{REALM},
1105                                    "2008",
1106                                    "locMEMpass3",
1107                                    $dcvars->{SERVER_IP},
1108                                    $dcvars->{SERVER_IPV6},
1109                                    $extra_smb_conf, "", undef);
1110         unless ($ret) {
1111                 return undef;
1112         }
1113
1114         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1115         my $cmd = "";
1116         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1117         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1118                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1119         } else {
1120                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1121         }
1122         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1123         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1124         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1125         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1126         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1127
1128         unless (system($cmd) == 0) {
1129                 warn("Join failed\n$cmd");
1130                 return undef;
1131         }
1132
1133         $ret->{MEMBER_SERVER} = $ret->{SERVER};
1134         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
1135         $ret->{MEMBER_SERVER_IPV6} = $ret->{SERVER_IPV6};
1136         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
1137         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
1138         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
1139
1140         $ret->{DOMSID} = $dcvars->{DOMSID};
1141         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1142         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1143         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1144         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1145         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1146         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1147
1148         return $ret;
1149 }
1150
1151 sub provision_rpc_proxy($$$)
1152 {
1153         my ($self, $prefix, $dcvars) = @_;
1154         print "PROVISIONING RPC PROXY...\n";
1155
1156         my $extra_smbconf_options = "
1157         passdb backend = samba_dsdb
1158
1159         # rpc_proxy
1160         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
1161         dcerpc endpoint servers = epmapper, remote
1162         dcerpc_remote:interfaces = rpcecho
1163
1164 [cifs_to_dc]
1165         path = /tmp/_ignore_cifs_to_dc_/_none_
1166         read only = no
1167         ntvfs handler = cifs
1168         cifs:server = $dcvars->{SERVER}
1169         cifs:share = cifs
1170         cifs:use-s4u2proxy = yes
1171         # There is no username specified here, instead the client is expected
1172         # to log in with kerberos, and the serverwill use delegated credentials.
1173         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1174
1175 ";
1176
1177         my $ret = $self->provision($prefix,
1178                                    "member server",
1179                                    "localrpcproxy",
1180                                    $dcvars->{DOMAIN},
1181                                    $dcvars->{REALM},
1182                                    "2008",
1183                                    "locRPCproxypass4",
1184                                    $dcvars->{SERVER_IP},
1185                                    $dcvars->{SERVER_IPV6},
1186                                    $extra_smbconf_options, "", undef);
1187         unless ($ret) {
1188                 return undef;
1189         }
1190
1191         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1192
1193         # The joind runs in the context of the rpc_proxy/member for now
1194         my $cmd = "";
1195         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1196         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1197                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1198         } else {
1199                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1200         }
1201         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1202         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1203         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1204         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1205         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1206
1207         unless (system($cmd) == 0) {
1208                 warn("Join failed\n$cmd");
1209                 return undef;
1210         }
1211
1212         # Setting up delegation runs in the context of the DC for now
1213         $cmd = "";
1214         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1215         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1216         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1217         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
1218         $cmd .= " $dcvars->{CONFIGURATION}";
1219         print $cmd;
1220
1221         unless (system($cmd) == 0) {
1222                 warn("Delegation failed\n$cmd");
1223                 return undef;
1224         }
1225
1226         # Setting up delegation runs in the context of the DC for now
1227         $cmd = "";
1228         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1229         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1230         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1231         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
1232         $cmd .= " $dcvars->{CONFIGURATION}";
1233
1234         unless (system($cmd) == 0) {
1235                 warn("Delegation failed\n$cmd");
1236                 return undef;
1237         }
1238
1239         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
1240         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
1241         $ret->{RPC_PROXY_SERVER_IPV6} = $ret->{SERVER_IPV6};
1242         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
1243         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
1244         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
1245
1246         $ret->{DOMSID} = $dcvars->{DOMSID};
1247         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1248         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1249         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1250         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1251         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1252         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1253
1254         return $ret;
1255 }
1256
1257 sub provision_promoted_dc($$$)
1258 {
1259         my ($self, $prefix, $dcvars) = @_;
1260         print "PROVISIONING PROMOTED DC...\n";
1261
1262         # We do this so that we don't run the provision.  That's the job of 'samba-tool domain dcpromo'.
1263         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1264                                                "promotedvdc",
1265                                                $dcvars->{DOMAIN},
1266                                                $dcvars->{REALM},
1267                                                $dcvars->{SAMSID},
1268                                                "2008",
1269                                                $dcvars->{PASSWORD},
1270                                                $dcvars->{SERVER_IP},
1271                                                $dcvars->{SERVER_IPV6});
1272
1273         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1274
1275         $ctx->{smb_conf_extra_options} = "
1276         max xmit = 32K
1277         server max protocol = SMB2
1278
1279         ntlm auth = ntlmv2-only
1280
1281 [sysvol]
1282         path = $ctx->{statedir}/sysvol
1283         read only = yes
1284
1285 [netlogon]
1286         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1287         read only = no
1288
1289 ";
1290
1291         my $ret = $self->provision_raw_step1($ctx);
1292         unless ($ret) {
1293                 return undef;
1294         }
1295
1296         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1297         my $cmd = "";
1298         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1299         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1300                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1301         } else {
1302                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1303         }
1304         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1305         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1306         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
1307         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1308         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1309
1310         unless (system($cmd) == 0) {
1311                 warn("Join failed\n$cmd");
1312                 return undef;
1313         }
1314
1315         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1316         my $cmd = "";
1317         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1318         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1319         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1320         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1321         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1322         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
1323
1324         unless (system($cmd) == 0) {
1325                 warn("Join failed\n$cmd");
1326                 return undef;
1327         }
1328
1329         $ret->{PROMOTED_DC_SERVER} = $ret->{SERVER};
1330         $ret->{PROMOTED_DC_SERVER_IP} = $ret->{SERVER_IP};
1331         $ret->{PROMOTED_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1332         $ret->{PROMOTED_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1333
1334         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1335         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1336         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1337         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1338         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1339         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1340
1341         return $ret;
1342 }
1343
1344 sub provision_vampire_dc($$$)
1345 {
1346         my ($self, $prefix, $dcvars, $fl) = @_;
1347         print "PROVISIONING VAMPIRE DC @ FL $fl...\n";
1348         my $name = "localvampiredc";
1349         my $extra_conf = "";
1350
1351         if ($fl == "2000") {
1352                 $name = "vampire2000dc";
1353         } else {
1354                 $extra_conf = "drs: immediate link sync = yes
1355                        drs: max link sync = 250";
1356         }
1357
1358         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1359         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1360                                                $name,
1361                                                $dcvars->{DOMAIN},
1362                                                $dcvars->{REALM},
1363                                                $dcvars->{DOMSID},
1364                                                $fl,
1365                                                $dcvars->{PASSWORD},
1366                                                $dcvars->{SERVER_IP},
1367                                                $dcvars->{SERVER_IPV6});
1368
1369         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1370
1371         $ctx->{smb_conf_extra_options} = "
1372         max xmit = 32K
1373         server max protocol = SMB2
1374
1375         ntlm auth = mschapv2-and-ntlmv2-only
1376         $extra_conf
1377
1378 [sysvol]
1379         path = $ctx->{statedir}/sysvol
1380         read only = yes
1381
1382 [netlogon]
1383         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1384         read only = no
1385
1386 ";
1387
1388         my $ret = $self->provision_raw_step1($ctx);
1389         unless ($ret) {
1390                 return undef;
1391         }
1392
1393         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1394         my $cmd = "";
1395         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1396         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1397                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1398         } else {
1399                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1400         }
1401         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1402         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1403         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1404         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1405         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1406         $cmd .= " --backend-store=mdb";
1407
1408         unless (system($cmd) == 0) {
1409                 warn("Join failed\n$cmd");
1410                 return undef;
1411         }
1412
1413         if ($fl == "2000") {
1414                 $ret->{VAMPIRE_2000_DC_SERVER} = $ret->{SERVER};
1415                 $ret->{VAMPIRE_2000_DC_SERVER_IP} = $ret->{SERVER_IP};
1416                 $ret->{VAMPIRE_2000_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1417                 $ret->{VAMPIRE_2000_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1418         } else {
1419                 $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1420                 $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1421                 $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1422                 $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1423         }
1424         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1425         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1426         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1427         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1428         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1429         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1430         $ret->{DC_REALM} = $dcvars->{DC_REALM};
1431
1432         return $ret;
1433 }
1434
1435 sub provision_subdom_dc($$$)
1436 {
1437         my ($self, $prefix, $dcvars) = @_;
1438         print "PROVISIONING SUBDOMAIN DC...\n";
1439
1440         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1441         my $samsid = undef; # TODO pass the domain sid all the way down
1442         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1443                                                "localsubdc",
1444                                                "SAMBASUBDOM",
1445                                                "sub.samba.example.com",
1446                                                $samsid,
1447                                                "2008",
1448                                                $dcvars->{PASSWORD},
1449                                                undef);
1450
1451         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1452
1453         $ctx->{smb_conf_extra_options} = "
1454         max xmit = 32K
1455         server max protocol = SMB2
1456
1457 [sysvol]
1458         path = $ctx->{statedir}/sysvol
1459         read only = yes
1460
1461 [netlogon]
1462         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1463         read only = no
1464
1465 ";
1466
1467         my $ret = $self->provision_raw_step1($ctx);
1468         unless ($ret) {
1469                 return undef;
1470         }
1471
1472         Samba::mk_krb5_conf($ctx);
1473         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
1474
1475         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1476         my $cmd = "";
1477         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1478         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1479                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1480         } else {
1481                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1482         }
1483         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1484         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1485         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{dnsname} subdomain ";
1486         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1487         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1488         $cmd .= " --adminpass=$ret->{PASSWORD}";
1489
1490         unless (system($cmd) == 0) {
1491                 warn("Join failed\n$cmd");
1492                 return undef;
1493         }
1494
1495         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1496         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1497         $ret->{SUBDOM_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1498         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1499
1500         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1501         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1502         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1503         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1504         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1505         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1506
1507         return $ret;
1508 }
1509
1510 sub provision_ad_dc_ntvfs($$)
1511 {
1512         my ($self, $prefix) = @_;
1513
1514         # We keep the old 'winbind' name here in server services to
1515         # ensure upgrades which used that name still work with the now
1516         # alias.
1517
1518         print "PROVISIONING AD DC (NTVFS)...\n";
1519         my $extra_conf_options = "netbios aliases = localDC1-a
1520         server services = +winbind -winbindd
1521         ldap server require strong auth = allow_sasl_over_tls
1522         allow nt4 crypto = yes
1523         raw NTLMv2 auth = yes
1524         lsa over netlogon = yes
1525         rpc server port = 1027
1526         auth event notification = true
1527         server schannel = auto
1528         ";
1529         my $ret = $self->provision($prefix,
1530                                    "domain controller",
1531                                    "localdc",
1532                                    "SAMBADOMAIN",
1533                                    "samba.example.com",
1534                                    "2008",
1535                                    "locDCpass1",
1536                                    undef,
1537                                    undef,
1538                                    $extra_conf_options,
1539                                    "",
1540                                    undef);
1541         unless ($ret) {
1542                 return undef;
1543         }
1544
1545         unless($self->add_wins_config("$prefix/private")) {
1546                 warn("Unable to add wins configuration");
1547                 return undef;
1548         }
1549         $ret->{NETBIOSALIAS} = "localdc1-a";
1550         $ret->{DC_SERVER} = $ret->{SERVER};
1551         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1552         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1553         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1554         $ret->{DC_USERNAME} = $ret->{USERNAME};
1555         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1556         $ret->{DC_REALM} = $ret->{REALM};
1557
1558         return $ret;
1559 }
1560
1561 sub provision_fl2000dc($$)
1562 {
1563         my ($self, $prefix) = @_;
1564
1565         print "PROVISIONING DC WITH FOREST LEVEL 2000...\n";
1566         my $extra_conf_options = "
1567         spnego:simulate_w2k=yes
1568         ntlmssp_server:force_old_spnego=yes
1569 ";
1570         my $extra_provision_options = undef;
1571         # This environment uses plain text secrets
1572         # i.e. secret attributes are not encrypted on disk.
1573         # This allows testing of the --plaintext-secrets option for
1574         # provision
1575         push (@{$extra_provision_options}, "--plaintext-secrets");
1576         my $ret = $self->provision($prefix,
1577                                    "domain controller",
1578                                    "dc5",
1579                                    "SAMBA2000",
1580                                    "samba2000.example.com",
1581                                    "2000",
1582                                    "locDCpass5",
1583                                    undef,
1584                                    undef,
1585                                    $extra_conf_options,
1586                                    "",
1587                                    $extra_provision_options);
1588         unless ($ret) {
1589                 return undef;
1590         }
1591
1592         unless($self->add_wins_config("$prefix/private")) {
1593                 warn("Unable to add wins configuration");
1594                 return undef;
1595         }
1596         $ret->{DC_SERVER} = $ret->{SERVER};
1597         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1598         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1599         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1600         $ret->{DC_USERNAME} = $ret->{USERNAME};
1601         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1602         $ret->{DC_REALM} = $ret->{REALM};
1603
1604         return $ret;
1605 }
1606
1607 sub provision_fl2003dc($$$)
1608 {
1609         my ($self, $prefix, $dcvars) = @_;
1610         my $swiface1 = Samba::get_interface("fakednsforwarder1");
1611         my $swiface2 = Samba::get_interface("fakednsforwarder2");
1612
1613         print "PROVISIONING DC WITH FOREST LEVEL 2003...\n";
1614         my $extra_conf_options = "allow dns updates = nonsecure and secure
1615         dcesrv:header signing = no
1616         dns forwarder = 127.0.0.$swiface1 127.0.0.$swiface2";
1617         my $ret = $self->provision($prefix,
1618                                    "domain controller",
1619                                    "dc6",
1620                                    "SAMBA2003",
1621                                    "samba2003.example.com",
1622                                    "2003",
1623                                    "locDCpass6",
1624                                    undef,
1625                                    undef,
1626                                    $extra_conf_options,
1627                                    "",
1628                                    undef);
1629         unless (defined $ret) {
1630                 return undef;
1631         }
1632
1633         $ret->{DC_SERVER} = $ret->{SERVER};
1634         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1635         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1636         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1637         $ret->{DC_USERNAME} = $ret->{USERNAME};
1638         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1639         $ret->{DNS_FORWARDER1} = "127.0.0.$swiface1";
1640         $ret->{DNS_FORWARDER2} = "127.0.0.$swiface2";
1641
1642         my @samba_tool_options;
1643         push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
1644         push (@samba_tool_options, "domain");
1645         push (@samba_tool_options, "passwordsettings");
1646         push (@samba_tool_options, "set");
1647         push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
1648         push (@samba_tool_options, "--min-pwd-age=0");
1649         push (@samba_tool_options, "--history-length=1");
1650
1651         my $samba_tool_cmd = join(" ", @samba_tool_options);
1652
1653         unless (system($samba_tool_cmd) == 0) {
1654                 warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
1655                 return undef;
1656         }
1657
1658         unless($self->add_wins_config("$prefix/private")) {
1659                 warn("Unable to add wins configuration");
1660                 return undef;
1661         }
1662
1663         return $ret;
1664 }
1665
1666 sub provision_fl2008r2dc($$$)
1667 {
1668         my ($self, $prefix, $dcvars) = @_;
1669
1670         print "PROVISIONING DC WITH FOREST LEVEL 2008r2...\n";
1671         my $extra_conf_options = "ldap server require strong auth = no";
1672         my $ret = $self->provision($prefix,
1673                                    "domain controller",
1674                                    "dc7",
1675                                    "SAMBA2008R2",
1676                                    "samba2008R2.example.com",
1677                                    "2008_R2",
1678                                    "locDCpass7",
1679                                    undef,
1680                                    undef,
1681                                    $extra_conf_options,
1682                                    "",
1683                                    undef);
1684         unless (defined $ret) {
1685                 return undef;
1686         }
1687
1688         unless ($self->add_wins_config("$prefix/private")) {
1689                 warn("Unable to add wins configuration");
1690                 return undef;
1691         }
1692         $ret->{DC_SERVER} = $ret->{SERVER};
1693         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1694         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1695         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1696         $ret->{DC_USERNAME} = $ret->{USERNAME};
1697         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1698         $ret->{DC_REALM} = $ret->{REALM};
1699
1700         return $ret;
1701 }
1702
1703
1704 sub provision_rodc($$$)
1705 {
1706         my ($self, $prefix, $dcvars) = @_;
1707         print "PROVISIONING RODC...\n";
1708
1709         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1710         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1711                                                "rodc",
1712                                                $dcvars->{DOMAIN},
1713                                                $dcvars->{REALM},
1714                                                $dcvars->{DOMSID},
1715                                                "2008",
1716                                                $dcvars->{PASSWORD},
1717                                                $dcvars->{SERVER_IP},
1718                                                $dcvars->{SERVER_IPV6});
1719         unless ($ctx) {
1720                 return undef;
1721         }
1722
1723         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1724
1725         $ctx->{share} = "$ctx->{prefix_abs}/share";
1726         push(@{$ctx->{directories}}, "$ctx->{share}");
1727
1728         $ctx->{smb_conf_extra_options} = "
1729         max xmit = 32K
1730         server max protocol = SMB2
1731         password server = $dcvars->{DC_SERVER}
1732
1733 [sysvol]
1734         path = $ctx->{statedir}/sysvol
1735         read only = yes
1736
1737 [netlogon]
1738         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1739         read only = yes
1740
1741 [tmp]
1742         path = $ctx->{share}
1743         read only = no
1744         posix:sharedelay = 10000
1745         posix:oplocktimeout = 3
1746         posix:writetimeupdatedelay = 50000
1747
1748 ";
1749
1750         my $ret = $self->provision_raw_step1($ctx);
1751         unless ($ret) {
1752                 return undef;
1753         }
1754
1755         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1756         my $cmd = "";
1757         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1758         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1759                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1760         } else {
1761                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1762         }
1763         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1764         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1765         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1766         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1767         $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
1768
1769         unless (system($cmd) == 0) {
1770                 warn("RODC join failed\n$cmd");
1771                 return undef;
1772         }
1773
1774         # This ensures deterministic behaviour for tests that want to have the 'testallowed account'
1775         # user password verified on the RODC
1776         my $testallowed_account = "testallowed account";
1777         $cmd = "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1778         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1779         $cmd .= "$samba_tool rodc preload '$testallowed_account' $ret->{CONFIGURATION}";
1780         $cmd .= " --server=$dcvars->{DC_SERVER}";
1781
1782         unless (system($cmd) == 0) {
1783                 warn("RODC join failed\n$cmd");
1784                 return undef;
1785         }
1786
1787         # we overwrite the kdc after the RODC join
1788         # so that use the RODC as kdc and test
1789         # the proxy code
1790         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1791         $ctx->{kdc_ipv6} = $ret->{SERVER_IPV6};
1792         Samba::mk_krb5_conf($ctx);
1793         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
1794
1795         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1796         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1797         $ret->{RODC_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1798         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1799
1800         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1801         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1802         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1803         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1804         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1805         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1806
1807         return $ret;
1808 }
1809
1810 sub read_config_h($)
1811 {
1812         my ($name) = @_;
1813         my %ret = {};
1814         open(LF, "<$name") or die("unable to read $name: $!");
1815         while (<LF>) {
1816                 chomp;
1817                 next if not (/^#define /);
1818                 if (/^#define (.*?)[ \t]+(.*?)$/) {
1819                         $ret{$1} = $2;
1820                         next;
1821                 }
1822                 if (/^#define (.*?)[ \t]+$/) {
1823                         $ret{$1} = 1;;
1824                         next;
1825                 }
1826         }
1827         close(LF);
1828         return \%ret;
1829 }
1830
1831 sub provision_ad_dc($$$$$$)
1832 {
1833         my ($self, $prefix, $hostname, $domain, $realm, $smbconf_args) = @_;
1834
1835         my $prefix_abs = abs_path($prefix);
1836
1837         my $bindir_abs = abs_path($self->{bindir});
1838         my $lockdir="$prefix_abs/lockdir";
1839         my $conffile="$prefix_abs/etc/smb.conf";
1840
1841         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
1842         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
1843
1844         my $config_h = {};
1845
1846         if (defined($ENV{CONFIG_H})) {
1847                 $config_h = read_config_h($ENV{CONFIG_H});
1848         }
1849
1850         my $password_hash_gpg_key_ids = "password hash gpg key ids = 4952E40301FAB41A";
1851         $password_hash_gpg_key_ids = "" unless defined($config_h->{HAVE_GPGME});
1852
1853         my $extra_smbconf_options = "
1854         server services = -smb +s3fs
1855         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
1856
1857         dbwrap_tdb_mutexes:* = yes
1858         ${require_mutexes}
1859
1860         ${password_hash_gpg_key_ids}
1861
1862         kernel oplocks = no
1863         kernel change notify = no
1864         smb2 leases = no
1865
1866         logging = file
1867         printing = bsd
1868         printcap name = /dev/null
1869
1870         max protocol = SMB3
1871         read only = no
1872
1873         smbd:sharedelay = 100000
1874         smbd:writetimeupdatedelay = 500000
1875         create mask = 755
1876         dos filemode = yes
1877         check parent directory delete on close = yes
1878
1879         dcerpc endpoint servers = -winreg -srvsvc
1880
1881         printcap name = /dev/null
1882
1883         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
1884         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
1885
1886         printing = vlp
1887         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1888         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1889         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1890         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1891         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1892         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1893         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1894         lpq cache time = 0
1895         print notify backchannel = yes
1896
1897         server schannel = auto
1898         auth event notification = true
1899         $smbconf_args
1900 ";
1901
1902         my $extra_smbconf_shares = "
1903
1904 [tmpenc]
1905         copy = tmp
1906         smb encrypt = required
1907
1908 [tmpcase]
1909         copy = tmp
1910         case sensitive = yes
1911
1912 [tmpguest]
1913         copy = tmp
1914         guest ok = yes
1915
1916 [hideunread]
1917         copy = tmp
1918         hide unreadable = yes
1919
1920 [durable]
1921         copy = tmp
1922         kernel share modes = no
1923         kernel oplocks = no
1924         posix locking = no
1925
1926 [print\$]
1927         copy = tmp
1928
1929 [print1]
1930         copy = tmp
1931         printable = yes
1932
1933 [print2]
1934         copy = print1
1935 [print3]
1936         copy = print1
1937 [lp]
1938         copy = print1
1939 ";
1940
1941         my $extra_provision_options = undef;
1942         push (@{$extra_provision_options}, "--backend-store=mdb");
1943         print "PROVISIONING AD DC...\n";
1944         my $ret = $self->provision($prefix,
1945                                    "domain controller",
1946                                    $hostname,
1947                                    $domain,
1948                                    $realm,
1949                                    "2008",
1950                                    "locDCpass1",
1951                                    undef,
1952                                    undef,
1953                                    $extra_smbconf_options,
1954                                    $extra_smbconf_shares,
1955                                    $extra_provision_options);
1956         unless (defined $ret) {
1957                 return undef;
1958         }
1959
1960         unless($self->add_wins_config("$prefix/private")) {
1961                 warn("Unable to add wins configuration");
1962                 return undef;
1963         }
1964
1965         $ret->{DC_SERVER} = $ret->{SERVER};
1966         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1967         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1968         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1969         $ret->{DC_USERNAME} = $ret->{USERNAME};
1970         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1971
1972         return $ret;
1973 }
1974
1975 sub provision_chgdcpass($$)
1976 {
1977         my ($self, $prefix) = @_;
1978
1979         print "PROVISIONING CHGDCPASS...\n";
1980         my $extra_provision_options = undef;
1981         # This environment disallows the use of this password
1982         # (and also removes the default AD complexity checks)
1983         my $unacceptable_password = "widk3Dsle32jxdBdskldsk55klASKQ";
1984         push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
1985         my $ret = $self->provision($prefix,
1986                                    "domain controller",
1987                                    "chgdcpass",
1988                                    "CHDCDOMAIN",
1989                                    "chgdcpassword.samba.example.com",
1990                                    "2008",
1991                                    "chgDCpass1",
1992                                    undef,
1993                                    undef,
1994                                    "check password script = sed -e '/$unacceptable_password/{;q1}; /$unacceptable_password/!{q0}'\n",
1995                                    "",
1996                                    $extra_provision_options);
1997         unless (defined $ret) {
1998                 return undef;
1999         }
2000
2001         unless($self->add_wins_config("$prefix/private")) {
2002                 warn("Unable to add wins configuration");
2003                 return undef;
2004         }
2005         
2006         # Remove secrets.tdb from this environment to test that we
2007         # still start up on systems without the new matching
2008         # secrets.tdb records.
2009         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
2010                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
2011                 return undef;
2012         }
2013             
2014         $ret->{DC_SERVER} = $ret->{SERVER};
2015         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
2016         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
2017         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
2018         $ret->{DC_USERNAME} = $ret->{USERNAME};
2019         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
2020         $ret->{UNACCEPTABLE_PASSWORD} = $unacceptable_password;
2021
2022         return $ret;
2023 }
2024
2025 sub teardown_env_terminate($$)
2026 {
2027         my ($self, $envvars) = @_;
2028         my $pid;
2029
2030         # This should cause samba to terminate gracefully
2031         close($envvars->{STDIN_PIPE});
2032
2033         $pid = $envvars->{SAMBA_PID};
2034         my $count = 0;
2035         my $childpid;
2036
2037         # This should give it time to write out the gcov data
2038         until ($count > 15) {
2039             if (Samba::cleanup_child($pid, "samba") != 0) {
2040                 return;
2041             }
2042             sleep(1);
2043             $count++;
2044         }
2045
2046         # After 15 Seconds, work out why this thing is still alive
2047         warn "server process $pid took more than $count seconds to exit, showing backtrace:\n";
2048         system("$self->{srcdir}/selftest/gdb_backtrace $pid");
2049
2050         until ($count > 30) {
2051             if (Samba::cleanup_child($pid, "samba") != 0) {
2052                 return;
2053             }
2054             sleep(1);
2055             $count++;
2056         }
2057
2058         if (kill(0, $pid)) {
2059             warn "server process $pid took more than $count seconds to exit, sending SIGTERM\n";
2060             kill "TERM", $pid;
2061         }
2062
2063         until ($count > 40) {
2064             if (Samba::cleanup_child($pid, "samba") != 0) {
2065                 return;
2066             }
2067             sleep(1);
2068             $count++;
2069         }
2070         # If it is still around, kill it
2071         if (kill(0, $pid)) {
2072             warn "server process $pid took more than $count seconds to exit, killing\n with SIGKILL\n";
2073             kill 9, $pid;
2074         }
2075         return;
2076 }
2077
2078 sub teardown_env($$)
2079 {
2080         my ($self, $envvars) = @_;
2081         teardown_env_terminate($self, $envvars);
2082
2083         $self->slapd_stop($envvars) if ($self->{ldap});
2084
2085         print $self->getlog_env($envvars);
2086
2087         return;
2088 }
2089
2090 sub getlog_env($$)
2091 {
2092         my ($self, $envvars) = @_;
2093         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME} pid $envvars->{SAMBA_PID}\n";
2094         my $out = $title;
2095
2096         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
2097
2098         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
2099         while (<LOG>) {
2100                 $out .= $_;
2101         }
2102         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
2103         close(LOG);
2104
2105         return "" if $out eq $title;
2106
2107         return $out;
2108 }
2109
2110 sub check_env($$)
2111 {
2112         my ($self, $envvars) = @_;
2113         my $samba_pid = $envvars->{SAMBA_PID};
2114
2115         if (not defined($samba_pid)) {
2116             return 0;
2117         } elsif ($samba_pid > 0) {
2118             my $childpid = Samba::cleanup_child($samba_pid, "samba");
2119
2120             if ($childpid == 0) {
2121                 return 1;
2122             }
2123             return 0;
2124         } else {
2125             return 1;
2126         }
2127 }
2128
2129 # Declare the environments Samba4 makes available.
2130 # To be set up, they will be called as
2131 #   samba4->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
2132 %Samba4::ENV_DEPS = (
2133         # name               => [dep_1, dep_2, ...],
2134         ad_dc_ntvfs          => [],
2135         ad_dc                => [],
2136         ad_dc_no_nss         => [],
2137         ad_dc_no_ntlm        => [],
2138         ad_dc_ntvfs          => [],
2139
2140         fl2008r2dc           => ["ad_dc"],
2141         fl2003dc             => ["ad_dc"],
2142         fl2000dc             => [],
2143
2144         vampire_2000_dc      => ["fl2000dc"],
2145         vampire_dc           => ["ad_dc_ntvfs"],
2146         promoted_dc          => ["ad_dc_ntvfs"],
2147         subdom_dc            => ["ad_dc_ntvfs"],
2148
2149         rodc                 => ["ad_dc_ntvfs"],
2150         rpc_proxy            => ["ad_dc_ntvfs"],
2151         chgdcpass            => [],
2152
2153         s4member_dflt_domain => ["ad_dc_ntvfs"],
2154         s4member             => ["ad_dc_ntvfs"],
2155
2156         none                 => [],
2157 );
2158
2159 sub setup_s4member
2160 {
2161         my ($self, $path, $dc_vars) = @_;
2162
2163         my $env = $self->provision_s4member($path, $dc_vars, "s4member");
2164
2165         if (defined $env) {
2166                 if (not defined($self->check_or_start($env, "standard"))) {
2167                         return undef;
2168                 }
2169         }
2170
2171         return $env;
2172 }
2173
2174 sub setup_s4member_dflt_domain
2175 {
2176         my ($self, $path, $dc_vars) = @_;
2177
2178         my $env = $self->provision_s4member($path, $dc_vars, "s4member_dflt",
2179                                             "winbind use default domain = yes");
2180
2181         if (defined $env) {
2182                 if (not defined($self->check_or_start($env, "standard"))) {
2183                         return undef;
2184                 }
2185         }
2186
2187         return $env;
2188 }
2189
2190 sub setup_rpc_proxy
2191 {
2192         my ($self, $path, $dc_vars) = @_;
2193
2194         my $env = $self->provision_rpc_proxy($path, $dc_vars);
2195
2196         if (defined $env) {
2197                 if (not defined($self->check_or_start($env, "standard"))) {
2198                         return undef;
2199                 }
2200         }
2201         return $env;
2202 }
2203
2204 sub setup_ad_dc_ntvfs
2205 {
2206         my ($self, $path) = @_;
2207
2208         my $env = $self->provision_ad_dc_ntvfs($path);
2209         if (defined $env) {
2210                 if (not defined($self->check_or_start($env, "standard"))) {
2211                     warn("Failed to start ad_dc_ntvfs");
2212                         return undef;
2213                 }
2214         }
2215         return $env;
2216 }
2217
2218 sub setup_chgdcpass
2219 {
2220         my ($self, $path) = @_;
2221
2222         my $env = $self->provision_chgdcpass($path);
2223         if (defined $env) {
2224                 if (not defined($self->check_or_start($env, "standard"))) {
2225                         return undef;
2226                 }
2227         }
2228         return $env;
2229 }
2230
2231 sub setup_fl2000dc
2232 {
2233         my ($self, $path) = @_;
2234
2235         my $env = $self->provision_fl2000dc($path);
2236         if (defined $env) {
2237                 if (not defined($self->check_or_start($env, "standard"))) {
2238                         return undef;
2239                 }
2240         }
2241
2242         return $env;
2243 }
2244
2245 sub setup_fl2003dc
2246 {
2247         my ($self, $path, $dc_vars) = @_;
2248
2249         my $env = $self->provision_fl2003dc($path);
2250
2251         if (defined $env) {
2252                 if (not defined($self->check_or_start($env, "standard"))) {
2253                         return undef;
2254                 }
2255
2256                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2257         }
2258         return $env;
2259 }
2260
2261 sub setup_fl2008r2dc
2262 {
2263         my ($self, $path, $dc_vars) = @_;
2264
2265         my $env = $self->provision_fl2008r2dc($path);
2266
2267         if (defined $env) {
2268                 if (not defined($self->check_or_start($env, "standard"))) {
2269                         return undef;
2270                 }
2271
2272                 my $upn_array = ["$env->{REALM}.upn"];
2273                 my $spn_array = ["$env->{REALM}.spn"];
2274
2275                 $self->setup_namespaces($env, $upn_array, $spn_array);
2276
2277                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2278         }
2279
2280         return $env;
2281 }
2282
2283 sub setup_vampire_dc
2284 {
2285         return setup_generic_vampire_dc(@_, "2008");
2286 }
2287
2288 sub setup_vampire_2000_dc
2289 {
2290         return setup_generic_vampire_dc(@_, "2000");
2291 }
2292
2293 sub setup_generic_vampire_dc
2294 {
2295         my ($self, $path, $dc_vars, $fl) = @_;
2296
2297         my $env = $self->provision_vampire_dc($path, $dc_vars, $fl);
2298
2299         if (defined $env) {
2300                 if (not defined($self->check_or_start($env, "single"))) {
2301                         return undef;
2302                 }
2303
2304                 # force replicated DC to update repsTo/repsFrom
2305                 # for vampired partitions
2306                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2307
2308                 # as 'vampired' dc may add data in its local replica
2309                 # we need to synchronize data between DCs
2310                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2311                 my $cmd = "";
2312                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2313                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2314                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2315                 } else {
2316                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2317                 }
2318                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2319                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2320                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2321                 $cmd .= " $dc_vars->{CONFIGURATION}";
2322                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2323                 # replicate Configuration NC
2324                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2325                 unless(system($cmd_repl) == 0) {
2326                         warn("Failed to replicate\n$cmd_repl");
2327                         return undef;
2328                 }
2329                 # replicate Default NC
2330                 $cmd_repl = "$cmd \"$base_dn\"";
2331                 unless(system($cmd_repl) == 0) {
2332                         warn("Failed to replicate\n$cmd_repl");
2333                         return undef;
2334                 }
2335
2336                 # Pull in a full set of changes from the main DC
2337                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2338                 $cmd = "";
2339                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2340                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2341                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2342                 } else {
2343                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2344                 }
2345                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2346                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2347                 $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2348                 $cmd .= " $dc_vars->{CONFIGURATION}";
2349                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2350                 # replicate Configuration NC
2351                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2352                 unless(system($cmd_repl) == 0) {
2353                         warn("Failed to replicate\n$cmd_repl");
2354                         return undef;
2355                 }
2356                 # replicate Default NC
2357                 $cmd_repl = "$cmd \"$base_dn\"";
2358                 unless(system($cmd_repl) == 0) {
2359                         warn("Failed to replicate\n$cmd_repl");
2360                         return undef;
2361                 }
2362         }
2363
2364         return $env;
2365 }
2366
2367 sub setup_promoted_dc
2368 {
2369         my ($self, $path, $dc_vars) = @_;
2370
2371         my $env = $self->provision_promoted_dc($path, $dc_vars);
2372
2373         if (defined $env) {
2374                 if (not defined($self->check_or_start($env, "single"))) {
2375                         return undef;
2376                 }
2377
2378                 # force source and replicated DC to update repsTo/repsFrom
2379                 # for vampired partitions
2380                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2381                 my $cmd = "";
2382                 # as 'vampired' dc may add data in its local replica
2383                 # we need to synchronize data between DCs
2384                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2385                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2386                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2387                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2388                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2389                 $cmd .= " $dc_vars->{CONFIGURATION}";
2390                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2391                 # replicate Configuration NC
2392                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2393                 unless(system($cmd_repl) == 0) {
2394                         warn("Failed to replicate\n$cmd_repl");
2395                         return undef;
2396                 }
2397                 # replicate Default NC
2398                 $cmd_repl = "$cmd \"$base_dn\"";
2399                 unless(system($cmd_repl) == 0) {
2400                         warn("Failed to replicate\n$cmd_repl");
2401                         return undef;
2402                 }
2403         }
2404
2405         return $env;
2406 }
2407
2408 sub setup_subdom_dc
2409 {
2410         my ($self, $path, $dc_vars) = @_;
2411
2412         my $env = $self->provision_subdom_dc($path, $dc_vars);
2413
2414         if (defined $env) {
2415                 if (not defined($self->check_or_start($env, "single"))) {
2416                         return undef;
2417                 }
2418
2419                 # force replicated DC to update repsTo/repsFrom
2420                 # for primary domain partitions
2421                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2422                 my $cmd = "";
2423                 # as 'subdomain' dc may add data in its local replica
2424                 # we need to synchronize data between DCs
2425                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
2426                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2427                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2428                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2429                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2430                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
2431                 $cmd .= " $dc_vars->{CONFIGURATION}";
2432                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2433                 # replicate Configuration NC
2434                 my $cmd_repl = "$cmd \"$config_dn\"";
2435                 unless(system($cmd_repl) == 0) {
2436                         warn("Failed to replicate\n$cmd_repl");
2437                         return undef;
2438                 }
2439                 # replicate Default NC
2440                 $cmd_repl = "$cmd \"$base_dn\"";
2441                 unless(system($cmd_repl) == 0) {
2442                         warn("Failed to replicate\n$cmd_repl");
2443                         return undef;
2444                 }
2445         }
2446
2447         return $env;
2448 }
2449
2450 sub setup_rodc
2451 {
2452         my ($self, $path, $dc_vars) = @_;
2453
2454         my $env = $self->provision_rodc($path, $dc_vars);
2455
2456         unless ($env) {
2457                 return undef;
2458         }
2459
2460         if (not defined($self->check_or_start($env, "standard"))) {
2461             return undef;
2462         }
2463
2464         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2465         my $cmd = "";
2466
2467         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2468         $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2469         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2470         $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2471         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2472         $cmd .= " $dc_vars->{CONFIGURATION}";
2473         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2474         # replicate Configuration NC
2475         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2476         unless(system($cmd_repl) == 0) {
2477             warn("Failed to replicate\n$cmd_repl");
2478             return undef;
2479         }
2480         # replicate Default NC
2481         $cmd_repl = "$cmd \"$base_dn\"";
2482         unless(system($cmd_repl) == 0) {
2483             warn("Failed to replicate\n$cmd_repl");
2484             return undef;
2485         }
2486
2487         return $env;
2488 }
2489
2490 sub setup_ad_dc
2491 {
2492         my ($self, $path) = @_;
2493
2494         # If we didn't build with ADS, pretend this env was never available
2495         if (not $self->{target3}->have_ads()) {
2496                return "UNKNOWN";
2497         }
2498
2499         my $env = $self->provision_ad_dc($path, "addc", "ADDOMAIN",
2500                                          "addom.samba.example.com", "");
2501         unless ($env) {
2502                 return undef;
2503         }
2504
2505         if (not defined($self->check_or_start($env, "single"))) {
2506             return undef;
2507         }
2508
2509         my $upn_array = ["$env->{REALM}.upn"];
2510         my $spn_array = ["$env->{REALM}.spn"];
2511
2512         $self->setup_namespaces($env, $upn_array, $spn_array);
2513
2514         return $env;
2515 }
2516
2517 sub setup_ad_dc_no_nss
2518 {
2519         my ($self, $path) = @_;
2520
2521         # If we didn't build with ADS, pretend this env was never available
2522         if (not $self->{target3}->have_ads()) {
2523                return "UNKNOWN";
2524         }
2525
2526         my $env = $self->provision_ad_dc($path, "addc_no_nss", "ADNONSSDOMAIN",
2527                                          "adnonssdom.samba.example.com", "");
2528         unless ($env) {
2529                 return undef;
2530         }
2531
2532         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2533         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2534
2535         if (not defined($self->check_or_start($env, "single"))) {
2536             return undef;
2537         }
2538
2539         my $upn_array = ["$env->{REALM}.upn"];
2540         my $spn_array = ["$env->{REALM}.spn"];
2541
2542         $self->setup_namespaces($env, $upn_array, $spn_array);
2543
2544         return $env;
2545 }
2546
2547 sub setup_ad_dc_no_ntlm
2548 {
2549         my ($self, $path) = @_;
2550
2551         # If we didn't build with ADS, pretend this env was never available
2552         if (not $self->{target3}->have_ads()) {
2553                return "UNKNOWN";
2554         }
2555
2556         my $env = $self->provision_ad_dc($path, "addc_no_ntlm", "ADNONTLMDOMAIN",
2557                                          "adnontlmdom.samba.example.com",
2558                                          "ntlm auth = disabled");
2559         unless ($env) {
2560                 return undef;
2561         }
2562
2563         if (not defined($self->check_or_start($env, "prefork"))) {
2564             return undef;
2565         }
2566
2567         my $upn_array = ["$env->{REALM}.upn"];
2568         my $spn_array = ["$env->{REALM}.spn"];
2569
2570         $self->setup_namespaces($env, $upn_array, $spn_array);
2571
2572         return $env;
2573 }
2574
2575 sub setup_none
2576 {
2577         my ($self, $path) = @_;
2578
2579         my $ret = {
2580                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
2581                 SAMBA_PID => -1,
2582         }
2583 }
2584
2585 1;