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