e8d591560fdf33dfb4c19aff8e589cb7a042f7e0
[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, $binary_mapping, $ldap, $srcdir, $server_maxtime) = @_;
18
19         my $self = {
20                 vars => {},
21                 ldap => $ldap,
22                 bindir => $bindir,
23                 binary_mapping => $binary_mapping,
24                 srcdir => $srcdir,
25                 server_maxtime => $server_maxtime,
26                 target3 => new Samba3($bindir, $binary_mapping, $srcdir, $server_maxtime)
27         };
28         bless $self;
29         return $self;
30 }
31
32 sub scriptdir_path($$) {
33         my ($self, $path) = @_;
34         return "$self->{srcdir}/source4/scripting/$path";
35 }
36
37 sub openldap_start($$$) {
38 }
39
40 sub slapd_start($$)
41 {
42         my $count = 0;
43         my ($self, $env_vars, $STDIN_READER) = @_;
44         my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
45
46         my $uri = $env_vars->{LDAP_URI};
47
48         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
49             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
50             return 1;
51         }
52         # running slapd in the background means it stays in the same process group, so it can be
53         # killed by timelimit
54         my $pid = fork();
55         if ($pid == 0) {
56                 open STDOUT, ">$env_vars->{LDAPDIR}/logs";
57                 open STDERR, '>&STDOUT';
58                 close($env_vars->{STDIN_PIPE});
59                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
60
61                 if ($self->{ldap} eq "fedora-ds") {
62                         exec("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd", "-D", $env_vars->{FEDORA_DS_DIR}, "-d0", "-i", $env_vars->{FEDORA_DS_PIDFILE});
63                 } elsif ($self->{ldap} eq "openldap") {
64                         exec($ENV{OPENLDAP_SLAPD}, "-dnone", "-F", $env_vars->{SLAPD_CONF_D}, "-h", $uri);
65                 }
66                 die("Unable to start slapd: $!");
67         }
68         $env_vars->{SLAPD_PID} = $pid;
69         sleep(1);
70         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
71                 $count++;
72                 if ($count > 40) {
73                         $self->slapd_stop($env_vars);
74                         return 0;
75                 }
76                 sleep(1);
77         }
78         return 1;
79 }
80
81 sub slapd_stop($$)
82 {
83         my ($self, $envvars) = @_;
84         kill 9, $envvars->{SLAPD_PID};
85         return 1;
86 }
87
88 sub check_or_start($$$)
89 {
90         my ($self, $env_vars, $process_model) = @_;
91         my $STDIN_READER;
92
93         return 0 if $self->check_env($env_vars);
94
95         # use a pipe for stdin in the child processes. This allows
96         # those processes to monitor the pipe for EOF to ensure they
97         # exit when the test script exits
98         pipe($STDIN_READER, $env_vars->{STDIN_PIPE});
99
100         # Start slapd before samba, but with the fifo on stdin
101         if (defined($self->{ldap})) {
102                 unless($self->slapd_start($env_vars, $STDIN_READER)) {
103                         warn("couldn't start slapd (main run)");
104                         return undef;
105                 }
106         }
107
108         print "STARTING SAMBA...";
109         my $pid = fork();
110         if ($pid == 0) {
111                 # we want out from samba to go to the log file, but also
112                 # to the users terminal when running 'make test' on the command
113                 # line. This puts it on stderr on the terminal
114                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
115                 open STDERR, '>&STDOUT';
116
117                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
118
119                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
120                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
121                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
122
123                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
124                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
125                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
126                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
127                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
128
129                 $ENV{UID_WRAPPER} = "1";
130
131                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "samba");
132                 my @preargs = ();
133                 my @optargs = ();
134                 if (defined($ENV{SAMBA_OPTIONS})) {
135                         @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
136                 }
137                 if(defined($ENV{SAMBA_VALGRIND})) {
138                         @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
139                 }
140
141                 close($env_vars->{STDIN_PIPE});
142                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
143
144                 exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
145         }
146         $env_vars->{SAMBA_PID} = $pid;
147         print "DONE\n";
148
149         close($STDIN_READER);
150
151         return $pid;
152 }
153
154 sub wait_for_start($$)
155 {
156         my ($self, $testenv_vars) = @_;
157         my $ret;
158         # give time for nbt server to register its names
159         print "delaying for nbt name registration\n";
160         sleep 2;
161
162         # This will return quickly when things are up, but be slow if we
163         # need to wait for (eg) SSL init
164         my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
165         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
166         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
167         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
168         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
169         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
170         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
171         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
172         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
173         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
174         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
175         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
176         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
177
178         # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
179         if ($testenv_vars->{SERVER_ROLE} eq "domain controller" and not ($testenv_vars->{NETBIOS_NAME} eq "rodc")) {
180             # Add hosts file for name lookups
181             $ENV{NSS_WRAPPER_HOSTS} = $testenv_vars->{NSS_WRAPPER_HOSTS};
182
183             print "waiting for working LDAP and a RID Set to be allocated\n";
184             my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
185             my $count = 0;
186             my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
187             my $rid_set_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
188             sleep(1);
189             while (system("$ldbsearch -H ldap://$testenv_vars->{SERVER} -U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} -s base -b \"$rid_set_dn\" rIDAllocationPool > /dev/null") != 0) {
190                 $count++;
191                 if ($count > 40) {
192                     $ret = 1;
193                     last;
194                 }
195                 sleep(1);
196             }
197         }
198         print $self->getlog_env($testenv_vars);
199
200         return $ret
201 }
202
203 sub write_ldb_file($$$)
204 {
205         my ($self, $file, $ldif) = @_;
206
207         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
208         open(LDIF, "|$ldbadd -H $file >/dev/null");
209         print LDIF $ldif;
210         return(close(LDIF));
211 }
212
213 sub add_wins_config($$)
214 {
215         my ($self, $privatedir) = @_;
216
217         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
218 dn: name=TORTURE_11,CN=PARTNERS
219 objectClass: wreplPartner
220 name: TORTURE_11
221 address: 127.0.0.11
222 pullInterval: 0
223 pushChangeCount: 0
224 type: 0x3
225 ");
226 }
227
228 sub mk_fedora_ds($$)
229 {
230         my ($self, $ctx) = @_;
231
232         #Make the subdirectory be as fedora DS would expect
233         my $fedora_ds_dir = "$ctx->{ldapdir}/slapd-$ctx->{ldap_instance}";
234
235         my $pidfile = "$fedora_ds_dir/logs/slapd-$ctx->{ldap_instance}.pid";
236
237         return ($fedora_ds_dir, $pidfile);
238 }
239
240 sub mk_openldap($$)
241 {
242         my ($self, $ctx) = @_;
243
244         my $slapd_conf_d = "$ctx->{ldapdir}/slapd.d";
245         my $pidfile = "$ctx->{ldapdir}/slapd.pid";
246
247         return ($slapd_conf_d, $pidfile);
248 }
249
250 sub provision_raw_prepare($$$$$$$$$$)
251 {
252         my ($self, $prefix, $server_role, $hostname,
253             $domain, $realm, $functional_level,
254             $password, $kdc_ipv4) = @_;
255         my $ctx;
256         my $netbiosname = uc($hostname);
257
258         unless(-d $prefix or mkdir($prefix, 0777)) {
259                 warn("Unable to create $prefix");
260                 return undef;
261         }
262         my $prefix_abs = abs_path($prefix);
263
264         die ("prefix=''") if $prefix_abs eq "";
265         die ("prefix='/'") if $prefix_abs eq "/";
266
267         unless (system("rm -rf $prefix_abs/*") == 0) {
268                 warn("Unable to clean up");
269         }
270
271         
272         my $swiface = Samba::get_interface($hostname);
273
274         $ctx->{prefix} = $prefix;
275         $ctx->{prefix_abs} = $prefix_abs;
276         
277         $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
278
279         $ctx->{server_role} = $server_role;
280         $ctx->{hostname} = $hostname;
281         $ctx->{netbiosname} = $netbiosname;
282         $ctx->{swiface} = $swiface;
283         $ctx->{password} = $password;
284         $ctx->{kdc_ipv4} = $kdc_ipv4;
285
286 #
287 # Set smbd log level here.
288 #
289         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
290         $ctx->{username} = "Administrator";
291         $ctx->{domain} = $domain;
292         $ctx->{realm} = uc($realm);
293         $ctx->{dnsname} = lc($realm);
294
295         $ctx->{functional_level} = $functional_level;
296
297         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
298         chomp $unix_name;
299         $ctx->{unix_name} = $unix_name;
300         $ctx->{unix_uid} = $>;
301         my @mygid = split(" ", $();
302         $ctx->{unix_gid} = $mygid[0];
303         $ctx->{unix_gids_str} = $);
304         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
305
306         $ctx->{etcdir} = "$prefix_abs/etc";
307         $ctx->{piddir} = "$prefix_abs/pid";
308         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
309         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
310         $ctx->{privatedir} = "$prefix_abs/private";
311         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
312         $ctx->{lockdir} = "$prefix_abs/lockdir";
313         $ctx->{logdir} = "$prefix_abs/logs";
314         $ctx->{statedir} = "$prefix_abs/statedir";
315         $ctx->{cachedir} = "$prefix_abs/cachedir";
316         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
317         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
318         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
319         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
320         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
321         $ctx->{nsswrap_hosts} = "$ENV{SELFTEST_PREFIX}/hosts";
322
323         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
324
325         $ctx->{ipv4} = "127.0.0.$swiface";
326         $ctx->{ipv6} = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
327         $ctx->{interfaces} = "$ctx->{ipv4}/8 $ctx->{ipv6}/64";
328
329         push(@{$ctx->{directories}}, $ctx->{privatedir});
330         push(@{$ctx->{directories}}, $ctx->{etcdir});
331         push(@{$ctx->{directories}}, $ctx->{piddir});
332         push(@{$ctx->{directories}}, $ctx->{lockdir});
333         push(@{$ctx->{directories}}, $ctx->{logdir});
334         push(@{$ctx->{directories}}, $ctx->{statedir});
335         push(@{$ctx->{directories}}, $ctx->{cachedir});
336
337         $ctx->{smb_conf_extra_options} = "";
338
339         my @provision_options = ();
340         push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_config}\"");
341         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
342         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
343         push (@provision_options, "NSS_WRAPPER_HOSTS=\"$ctx->{nsswrap_hosts}\"");
344         if (defined($ENV{GDB_PROVISION})) {
345                 push (@provision_options, "gdb --args");
346                 if (!defined($ENV{PYTHON})) {
347                     push (@provision_options, "env");
348                     push (@provision_options, "python");
349                 }
350         }
351         if (defined($ENV{VALGRIND_PROVISION})) {
352                 push (@provision_options, "valgrind");
353                 if (!defined($ENV{PYTHON})) {
354                     push (@provision_options, "env");
355                     push (@provision_options, "python");
356                 }
357         }
358         if (defined($ENV{PYTHON})) {
359                 push (@provision_options, $ENV{PYTHON});
360         }
361         push (@provision_options, Samba::bindir_path($self, "samba-tool"));
362         push (@provision_options, "domain");
363         push (@provision_options, "provision");
364         push (@provision_options, "--configfile=$ctx->{smb_conf}");
365         push (@provision_options, "--host-name=$ctx->{hostname}");
366         push (@provision_options, "--host-ip=$ctx->{ipv4}");
367         push (@provision_options, "--quiet");
368         push (@provision_options, "--domain=$ctx->{domain}");
369         push (@provision_options, "--realm=$ctx->{realm}");
370         push (@provision_options, "--adminpass=$ctx->{password}");
371         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
372         push (@provision_options, "--machinepass=machine$ctx->{password}");
373         push (@provision_options, "--root=$ctx->{unix_name}");
374         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
375         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
376
377         @{$ctx->{provision_options}} = @provision_options;
378
379         return $ctx;
380 }
381
382 #
383 # Step1 creates the basic configuration
384 #
385 sub provision_raw_step1($$)
386 {
387         my ($self, $ctx) = @_;
388
389         mkdir($_, 0777) foreach (@{$ctx->{directories}});
390
391         ##
392         ## lockdir and piddir must be 0755
393         ##
394         chmod 0755, $ctx->{lockdir};
395         chmod 0755, $ctx->{piddir};
396
397         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
398                 warn("can't open $ctx->{smb_conf}$?");
399                 return undef;
400         }
401
402         Samba::prepare_keyblobs($ctx);
403         my $crlfile = "$ctx->{tlsdir}/crl.pem";
404         $crlfile = "" unless -e ${crlfile};
405
406         print CONFFILE "
407 [global]
408         netbios name = $ctx->{netbiosname}
409         posix:eadb = $ctx->{statedir}/eadb.tdb
410         workgroup = $ctx->{domain}
411         realm = $ctx->{realm}
412         private dir = $ctx->{privatedir}
413         pid directory = $ctx->{piddir}
414         ncalrpc dir = $ctx->{ncalrpcdir}
415         lock dir = $ctx->{lockdir}
416         state directory = $ctx->{statedir}
417         cache directory = $ctx->{cachedir}
418         winbindd socket directory = $ctx->{winbindd_socket_dir}
419         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
420         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
421         winbind separator = /
422         name resolve order = file bcast
423         interfaces = $ctx->{interfaces}
424         tls dh params file = $ctx->{tlsdir}/dhparms.pem
425         tls crlfile = ${crlfile}
426         tls verify peer = no_check
427         panic action = $RealBin/gdb_backtrace \%d
428         wins support = yes
429         server role = $ctx->{server_role}
430         server services = +echo +smb -s3fs
431         dcerpc endpoint servers = +winreg +srvsvc
432         notify:inotify = false
433         ldb:nosync = true
434         ldap server require strong auth = yes
435 #We don't want to pass our self-tests if the PAC code is wrong
436         gensec:require_pac = true
437         log file = $ctx->{logdir}/log.\%m
438         log level = $ctx->{server_loglevel}
439         lanman auth = Yes
440         rndc command = true
441         dns update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$ctx->{dns_host_file} -s $ctx->{smb_conf}
442         spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
443         resolv:host file = $ctx->{dns_host_file}
444         dreplsrv:periodic_startup_interval = 0
445         dsdb:schema update allowed = yes
446
447         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
448
449         # remove this again, when our smb2 client library
450         # supports signin on compound related requests
451         server signing = on
452
453         idmap_ldb:use rfc2307=yes
454         winbind enum users = yes
455         winbind enum groups = yes
456 ";
457
458         print CONFFILE "
459
460         # Begin extra options
461         $ctx->{smb_conf_extra_options}
462         # End extra options
463 ";
464         close(CONFFILE);
465
466         #Default the KDC IP to the server's IP
467         if (not defined($ctx->{kdc_ipv4})) {
468              $ctx->{kdc_ipv4} = $ctx->{ipv4};
469         }
470
471         Samba::mk_krb5_conf($ctx, "");
472
473         open(PWD, ">$ctx->{nsswrap_passwd}");
474         print PWD "
475 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
476 $ctx->{unix_name}:x:$ctx->{unix_uid}:100:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false
477 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
478 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
479 pdbtest2:x:65532:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
480 pdbtest3:x:65531:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
481 pdbtest4:x:65530:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
482 ";
483         close(PWD);
484         my $uid_rfc2307test = 65533;
485
486         open(GRP, ">$ctx->{nsswrap_group}");
487         print GRP "
488 root:x:0:
489 wheel:x:10:
490 users:x:100:
491 nobody:x:65533:
492 nogroup:x:65534:nobody
493 $ctx->{unix_name}:x:$ctx->{unix_gid}:
494 ";
495         close(GRP);
496         my $gid_rfc2307test = 65532;
497
498         my $hostname = lc($ctx->{hostname});
499         open(HOSTS, ">>$ctx->{nsswrap_hosts}");
500         print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} ${hostname}\n";
501         print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} ${hostname}\n";
502         close(HOSTS);
503
504         my $configuration = "--configfile=$ctx->{smb_conf}";
505
506 #Ensure the config file is valid before we start
507         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
508         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
509                 system("$testparm -v --suppress-prompt $configuration >&2");
510                 warn("Failed to create a valid smb.conf configuration $testparm!");
511                 return undef;
512         }
513         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) {
514                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
515                 return undef;
516         }
517
518         my $ret = {
519                 KRB5_CONFIG => $ctx->{krb5_conf},
520                 PIDDIR => $ctx->{piddir},
521                 SERVER => $ctx->{hostname},
522                 SERVER_IP => $ctx->{ipv4},
523                 SERVER_IPV6 => $ctx->{ipv6},
524                 NETBIOSNAME => $ctx->{netbiosname},
525                 DOMAIN => $ctx->{domain},
526                 USERNAME => $ctx->{username},
527                 REALM => $ctx->{realm},
528                 PASSWORD => $ctx->{password},
529                 LDAPDIR => $ctx->{ldapdir},
530                 LDAP_INSTANCE => $ctx->{ldap_instance},
531                 SELFTEST_WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
532                 NCALRPCDIR => $ctx->{ncalrpcdir},
533                 LOCKDIR => $ctx->{lockdir},
534                 STATEDIR => $ctx->{statedir},
535                 CACHEDIR => $ctx->{cachedir},
536                 PRIVATEDIR => $ctx->{privatedir},
537                 SERVERCONFFILE => $ctx->{smb_conf},
538                 CONFIGURATION => $configuration,
539                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
540                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
541                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
542                 NSS_WRAPPER_HOSTS => $ctx->{nsswrap_hosts},
543                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
544                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
545                 SAMBA_TEST_LOG_POS => 0,
546                 NSS_WRAPPER_MODULE_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
547                 NSS_WRAPPER_MODULE_FN_PREFIX => "winbind",
548                 LOCAL_PATH => $ctx->{share},
549                 UID_RFC2307TEST => $uid_rfc2307test,
550                 GID_RFC2307TEST => $gid_rfc2307test,
551                 SERVER_ROLE => $ctx->{server_role}
552         };
553
554         return $ret;
555 }
556
557 #
558 # Step2 runs the provision script
559 #
560 sub provision_raw_step2($$$)
561 {
562         my ($self, $ctx, $ret) = @_;
563
564         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
565         unless (system($provision_cmd) == 0) {
566                 warn("Unable to provision: \n$provision_cmd\n");
567                 return undef;
568         }
569
570         return $ret;
571 }
572
573 sub provision($$$$$$$$$)
574 {
575         my ($self, $prefix, $server_role, $hostname,
576             $domain, $realm, $functional_level,
577             $password, $kdc_ipv4, $extra_smbconf_options, $extra_smbconf_shares,
578             $extra_provision_options) = @_;
579
580         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
581                                                $hostname,
582                                                $domain, $realm, $functional_level,
583                                                $password, $kdc_ipv4);
584
585         if (defined($extra_provision_options)) {
586                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
587         } else {
588                 push (@{$ctx->{provision_options}}, "--use-ntvfs");
589         }
590
591         $ctx->{share} = "$ctx->{prefix_abs}/share";
592         push(@{$ctx->{directories}}, "$ctx->{share}");
593         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
594         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
595
596         # precreate directories for printer drivers
597         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
598         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
599         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
600
601         my $msdfs = "no";
602         $msdfs = "yes" if ($server_role eq "domain controller");
603         $ctx->{smb_conf_extra_options} = "
604
605         max xmit = 32K
606         server max protocol = SMB2
607         host msdfs = $msdfs
608         lanman auth = yes
609         allow nt4 crypto = yes
610
611         # fruit:copyfile is a global option
612         fruit:copyfile = yes
613
614         $extra_smbconf_options
615
616 [tmp]
617         path = $ctx->{share}
618         read only = no
619         posix:sharedelay = 10000
620         posix:oplocktimeout = 3
621         posix:writetimeupdatedelay = 50000
622
623 [xcopy_share]
624         path = $ctx->{share}
625         read only = no
626         posix:sharedelay = 10000
627         posix:oplocktimeout = 3
628         posix:writetimeupdatedelay = 50000
629         create mask = 777
630         force create mode = 777
631
632 [posix_share]
633         path = $ctx->{share}
634         read only = no
635         create mask = 0777
636         force create mode = 0
637         directory mask = 0777
638         force directory mode = 0
639
640 [test1]
641         path = $ctx->{share}/test1
642         read only = no
643         posix:sharedelay = 10000
644         posix:oplocktimeout = 3
645         posix:writetimeupdatedelay = 50000
646
647 [test2]
648         path = $ctx->{share}/test2
649         read only = no
650         posix:sharedelay = 10000
651         posix:oplocktimeout = 3
652         posix:writetimeupdatedelay = 50000
653
654 [cifs]
655         path = $ctx->{share}/_ignore_cifs_
656         read only = no
657         ntvfs handler = cifs
658         cifs:server = $ctx->{netbiosname}
659         cifs:share = tmp
660         cifs:use-s4u2proxy = yes
661         # There is no username specified here, instead the client is expected
662         # to log in with kerberos, and the serverwill use delegated credentials.
663         # Or the server tries s4u2self/s4u2proxy to impersonate the client
664
665 [simple]
666         path = $ctx->{share}
667         read only = no
668         ntvfs handler = simple
669
670 [sysvol]
671         path = $ctx->{statedir}/sysvol
672         read only = no
673
674 [netlogon]
675         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
676         read only = no
677
678 [cifsposix]
679         copy = simple
680         ntvfs handler = cifsposix
681
682 [vfs_fruit]
683         path = $ctx->{share}
684         vfs objects = catia fruit streams_xattr acl_xattr
685         ea support = yes
686         fruit:ressource = file
687         fruit:metadata = netatalk
688         fruit:locking = netatalk
689         fruit:encoding = native
690
691 $extra_smbconf_shares
692 ";
693
694         if (defined($self->{ldap})) {
695                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
696                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
697
698                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
699                 $ldap_uri =~ s|/|%2F|g;
700                 $ldap_uri = "ldapi://$ldap_uri";
701                 $ctx->{ldap_uri} = $ldap_uri;
702
703                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
704         }
705
706         my $ret = $self->provision_raw_step1($ctx);
707         unless (defined $ret) {
708                 return undef;
709         }
710
711         if (defined($self->{ldap})) {
712                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
713                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
714                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
715                 if ($self->{ldap} eq "openldap") {
716                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
717                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
718
719                 } elsif ($self->{ldap} eq "fedora-ds") {
720                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
721                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
722                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
723                 }
724
725         }
726
727         return $self->provision_raw_step2($ctx, $ret);
728 }
729
730 sub provision_s4member($$$)
731 {
732         my ($self, $prefix, $dcvars) = @_;
733         print "PROVISIONING MEMBER...";
734         my $extra_smb_conf = "
735         passdb backend = samba_dsdb
736 winbindd:use external pipes = true
737
738 rpc_server:default = external
739 rpc_server:svcctl = embedded
740 rpc_server:srvsvc = embedded
741 rpc_server:eventlog = embedded
742 rpc_server:ntsvcs = embedded
743 rpc_server:winreg = embedded
744 rpc_server:spoolss = embedded
745 rpc_daemon:spoolssd = embedded
746 rpc_server:tcpip = no
747 ";
748         my $ret = $self->provision($prefix,
749                                    "member server",
750                                    "s4member",
751                                    "SAMBADOMAIN",
752                                    "samba.example.com",
753                                    "2008",
754                                    "locMEMpass3",
755                                    $dcvars->{SERVER_IP},
756                                    $extra_smb_conf, "", undef);
757         unless ($ret) {
758                 return undef;
759         }
760
761         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
762         my $cmd = "";
763         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
764         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
765         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
766         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
767         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
768
769         unless (system($cmd) == 0) {
770                 warn("Join failed\n$cmd");
771                 return undef;
772         }
773
774         $ret->{MEMBER_SERVER} = $ret->{SERVER};
775         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
776         $ret->{MEMBER_SERVER_IPV6} = $ret->{SERVER_IPV6};
777         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
778         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
779         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
780
781         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
782         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
783         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
784         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
785         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
786         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
787
788         return $ret;
789 }
790
791 sub provision_rpc_proxy($$$)
792 {
793         my ($self, $prefix, $dcvars) = @_;
794         print "PROVISIONING RPC PROXY...";
795
796         my $extra_smbconf_options = "
797         passdb backend = samba_dsdb
798
799         # rpc_proxy
800         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
801         dcerpc endpoint servers = epmapper, remote
802         dcerpc_remote:interfaces = rpcecho
803
804 [cifs_to_dc]
805         path = /tmp/_ignore_cifs_to_dc_/_none_
806         read only = no
807         ntvfs handler = cifs
808         cifs:server = $dcvars->{SERVER}
809         cifs:share = cifs
810         cifs:use-s4u2proxy = yes
811         # There is no username specified here, instead the client is expected
812         # to log in with kerberos, and the serverwill use delegated credentials.
813         # Or the server tries s4u2self/s4u2proxy to impersonate the client
814
815 ";
816
817         my $ret = $self->provision($prefix,
818                                    "member server",
819                                    "localrpcproxy",
820                                    "SAMBADOMAIN",
821                                    "samba.example.com",
822                                    "2008",
823                                    "locRPCproxypass4",
824                                    $dcvars->{SERVER_IP},
825                                    $extra_smbconf_options, "", undef);
826
827         unless ($ret) {
828                 return undef;
829         }
830
831         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
832
833         # The joind runs in the context of the rpc_proxy/member for now
834         my $cmd = "";
835         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
836         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
837         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
838         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
839         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
840
841         unless (system($cmd) == 0) {
842                 warn("Join failed\n$cmd");
843                 return undef;
844         }
845
846         # Setting up delegation runs in the context of the DC for now
847         $cmd = "";
848         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
849         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
850         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
851         $cmd .= " $dcvars->{CONFIGURATION}";
852         print $cmd;
853
854         unless (system($cmd) == 0) {
855                 warn("Delegation failed\n$cmd");
856                 return undef;
857         }
858
859         # Setting up delegation runs in the context of the DC for now
860         $cmd = "";
861         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
862         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
863         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
864         $cmd .= " $dcvars->{CONFIGURATION}";
865
866         unless (system($cmd) == 0) {
867                 warn("Delegation failed\n$cmd");
868                 return undef;
869         }
870
871         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
872         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
873         $ret->{RPC_PROXY_SERVER_IPV6} = $ret->{SERVER_IPV6};
874         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
875         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
876         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
877
878         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
879         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
880         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
881         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
882         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
883         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
884
885         return $ret;
886 }
887
888 sub provision_promoted_dc($$$)
889 {
890         my ($self, $prefix, $dcvars) = @_;
891         print "PROVISIONING VAMPIRE DC...";
892
893         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
894         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
895                                                "promotedvdc",
896                                                "SAMBADOMAIN",
897                                                "samba.example.com",
898                                                "2008",
899                                                $dcvars->{PASSWORD},
900                                                $dcvars->{SERVER_IP});
901
902         push (@{$ctx->{provision_options}}, "--use-ntvfs");
903
904         $ctx->{smb_conf_extra_options} = "
905         max xmit = 32K
906         server max protocol = SMB2
907
908 [sysvol]
909         path = $ctx->{statedir}/sysvol
910         read only = yes
911
912 [netlogon]
913         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
914         read only = no
915
916 ";
917
918         my $ret = $self->provision_raw_step1($ctx);
919         unless ($ret) {
920                 return undef;
921         }
922
923         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
924         my $cmd = "";
925         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
926         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
927         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
928         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
929         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
930
931         unless (system($cmd) == 0) {
932                 warn("Join failed\n$cmd");
933                 return undef;
934         }
935
936         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
937         my $cmd = "";
938         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
939         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
940         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
941         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
942         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
943
944         unless (system($cmd) == 0) {
945                 warn("Join failed\n$cmd");
946                 return undef;
947         }
948
949         $ret->{PROMOTED_DC_SERVER} = $ret->{SERVER};
950         $ret->{PROMOTED_DC_SERVER_IP} = $ret->{SERVER_IP};
951         $ret->{PROMOTED_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
952         $ret->{PROMOTED_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
953
954         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
955         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
956         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
957         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
958         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
959         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
960
961         return $ret;
962 }
963
964 sub provision_vampire_dc($$$)
965 {
966         my ($self, $prefix, $dcvars) = @_;
967         print "PROVISIONING VAMPIRE DC...";
968
969         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
970         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
971                                                "localvampiredc",
972                                                "SAMBADOMAIN",
973                                                "samba.example.com",
974                                                "2008",
975                                                $dcvars->{PASSWORD},
976                                                $dcvars->{SERVER_IP});
977
978         push (@{$ctx->{provision_options}}, "--use-ntvfs");
979
980         $ctx->{smb_conf_extra_options} = "
981         max xmit = 32K
982         server max protocol = SMB2
983
984 [sysvol]
985         path = $ctx->{statedir}/sysvol
986         read only = yes
987
988 [netlogon]
989         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
990         read only = no
991
992 ";
993
994         my $ret = $self->provision_raw_step1($ctx);
995         unless ($ret) {
996                 return undef;
997         }
998
999         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1000         my $cmd = "";
1001         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1002         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1003         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1004         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1005         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1006
1007         unless (system($cmd) == 0) {
1008                 warn("Join failed\n$cmd");
1009                 return undef;
1010         }
1011
1012         $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1013         $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1014         $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1015         $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1016
1017         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1018         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1019         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1020         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1021         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1022         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1023         $ret->{DC_REALM} = $dcvars->{DC_REALM};
1024
1025         return $ret;
1026 }
1027
1028 sub provision_subdom_dc($$$)
1029 {
1030         my ($self, $prefix, $dcvars) = @_;
1031         print "PROVISIONING SUBDOMAIN DC...";
1032
1033         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1034         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1035                                                "localsubdc",
1036                                                "SAMBASUBDOM",
1037                                                "sub.samba.example.com",
1038                                                "2008",
1039                                                $dcvars->{PASSWORD},
1040                                                undef);
1041
1042         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1043
1044         $ctx->{smb_conf_extra_options} = "
1045         max xmit = 32K
1046         server max protocol = SMB2
1047
1048 [sysvol]
1049         path = $ctx->{statedir}/sysvol
1050         read only = yes
1051
1052 [netlogon]
1053         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1054         read only = no
1055
1056 ";
1057
1058         my $ret = $self->provision_raw_step1($ctx);
1059         unless ($ret) {
1060                 return undef;
1061         }
1062
1063         # This ensures we share the krb5.conf with the main DC, so
1064         # they can find each other.  Sadly only works between 'dc' and
1065         # 'subdom_dc', the other DCs won't see it
1066
1067         my $dc_realms = Samba::mk_realms_stanza($dcvars->{REALM}, lc($dcvars->{REALM}),
1068                                                 $dcvars->{DOMAIN}, $dcvars->{SERVER_IP});
1069
1070         $ret->{KRB5_CONFIG} = $dcvars->{KRB5_CONFIG};
1071         $ctx->{krb5_conf} = $dcvars->{KRB5_CONFIG};
1072
1073         Samba::mk_krb5_conf($ctx, $dc_realms);
1074
1075         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1076         my $cmd = "";
1077         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1078         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1079         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{dnsname} subdomain ";
1080         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1081         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1082         $cmd .= " --adminpass=$ret->{PASSWORD}";
1083
1084         unless (system($cmd) == 0) {
1085                 warn("Join failed\n$cmd");
1086                 return undef;
1087         }
1088
1089         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1090         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1091         $ret->{SUBDOM_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1092         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1093
1094         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1095         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1096         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1097         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1098         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1099         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1100
1101         return $ret;
1102 }
1103
1104 sub provision_dc($$)
1105 {
1106         my ($self, $prefix) = @_;
1107
1108         print "PROVISIONING DC...";
1109         my $extra_conf_options = "netbios aliases = localDC1-a
1110         server services = +winbind -winbindd
1111         ldap server require strong auth = allow_sasl_over_tls
1112         ";
1113         my $ret = $self->provision($prefix,
1114                                    "domain controller",
1115                                    "localdc",
1116                                    "SAMBADOMAIN",
1117                                    "samba.example.com",
1118                                    "2008",
1119                                    "locDCpass1",
1120                                    undef, $extra_conf_options, "", undef);
1121
1122         return undef unless(defined $ret);
1123         unless($self->add_wins_config("$prefix/private")) {
1124                 warn("Unable to add wins configuration");
1125                 return undef;
1126         }
1127         $ret->{NETBIOSALIAS} = "localdc1-a";
1128         $ret->{DC_SERVER} = $ret->{SERVER};
1129         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1130         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1131         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1132         $ret->{DC_USERNAME} = $ret->{USERNAME};
1133         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1134         $ret->{DC_REALM} = $ret->{REALM};
1135
1136         return $ret;
1137 }
1138
1139 sub provision_fl2000dc($$)
1140 {
1141         my ($self, $prefix) = @_;
1142
1143         print "PROVISIONING DC...";
1144         my $ret = $self->provision($prefix,
1145                                    "domain controller",
1146                                    "dc5",
1147                                    "SAMBA2000",
1148                                    "samba2000.example.com",
1149                                    "2000",
1150                                    "locDCpass5",
1151                                    undef, "", "", undef);
1152
1153         unless($self->add_wins_config("$prefix/private")) {
1154                 warn("Unable to add wins configuration");
1155                 return undef;
1156         }
1157         $ret->{DC_SERVER} = $ret->{SERVER};
1158         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1159         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1160         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1161         $ret->{DC_USERNAME} = $ret->{USERNAME};
1162         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1163         $ret->{DC_REALM} = $ret->{REALM};
1164
1165         return $ret;
1166 }
1167
1168 sub provision_fl2003dc($$)
1169 {
1170         my ($self, $prefix) = @_;
1171
1172         print "PROVISIONING DC...";
1173         my $extra_conf_options = "allow dns updates = nonsecure and secure";
1174         my $ret = $self->provision($prefix,
1175                                    "domain controller",
1176                                    "dc6",
1177                                    "SAMBA2003",
1178                                    "samba2003.example.com",
1179                                    "2003",
1180                                    "locDCpass6",
1181                                    undef, $extra_conf_options, "", undef);
1182
1183         unless (defined $ret) {
1184                 return undef;
1185         }
1186
1187         $ret->{DC_SERVER} = $ret->{SERVER};
1188         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1189         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1190         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1191         $ret->{DC_USERNAME} = $ret->{USERNAME};
1192         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1193
1194         my @samba_tool_options;
1195         push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
1196         push (@samba_tool_options, "domain");
1197         push (@samba_tool_options, "passwordsettings");
1198         push (@samba_tool_options, "set");
1199         push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
1200         push (@samba_tool_options, "--min-pwd-age=0");
1201         push (@samba_tool_options, "--history-length=1");
1202
1203         my $samba_tool_cmd = join(" ", @samba_tool_options);
1204
1205         unless (system($samba_tool_cmd) == 0) {
1206                 warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
1207                 return undef;
1208         }
1209
1210         return $ret;
1211
1212         unless($self->add_wins_config("$prefix/private")) {
1213                 warn("Unable to add wins configuration");
1214                 return undef;
1215         }
1216
1217         return $ret;
1218 }
1219
1220 sub provision_fl2008r2dc($$)
1221 {
1222         my ($self, $prefix) = @_;
1223
1224         print "PROVISIONING DC...";
1225         my $extra_conf_options = "ldap server require strong auth = no";
1226         my $ret = $self->provision($prefix,
1227                                    "domain controller",
1228                                    "dc7",
1229                                    "SAMBA2008R2",
1230                                    "samba2008R2.example.com",
1231                                    "2008_R2",
1232                                    "locDCpass7",
1233                                    undef, $extra_conf_options,
1234                                    "", undef);
1235
1236         unless ($self->add_wins_config("$prefix/private")) {
1237                 warn("Unable to add wins configuration");
1238                 return undef;
1239         }
1240         $ret->{DC_SERVER} = $ret->{SERVER};
1241         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1242         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1243         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1244         $ret->{DC_USERNAME} = $ret->{USERNAME};
1245         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1246         $ret->{DC_REALM} = $ret->{REALM};
1247
1248         return $ret;
1249 }
1250
1251
1252 sub provision_rodc($$$)
1253 {
1254         my ($self, $prefix, $dcvars) = @_;
1255         print "PROVISIONING RODC...";
1256
1257         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1258         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1259                                                "rodc",
1260                                                "SAMBADOMAIN",
1261                                                "samba.example.com",
1262                                                "2008",
1263                                                $dcvars->{PASSWORD},
1264                                                $dcvars->{SERVER_IP});
1265         unless ($ctx) {
1266                 return undef;
1267         }
1268
1269         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1270
1271         $ctx->{share} = "$ctx->{prefix_abs}/share";
1272         push(@{$ctx->{directories}}, "$ctx->{share}");
1273
1274         $ctx->{smb_conf_extra_options} = "
1275         max xmit = 32K
1276         server max protocol = SMB2
1277
1278 [sysvol]
1279         path = $ctx->{statedir}/sysvol
1280         read only = yes
1281
1282 [netlogon]
1283         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1284         read only = yes
1285
1286 [tmp]
1287         path = $ctx->{share}
1288         read only = no
1289         posix:sharedelay = 10000
1290         posix:oplocktimeout = 3
1291         posix:writetimeupdatedelay = 50000
1292
1293 ";
1294
1295         my $ret = $self->provision_raw_step1($ctx);
1296         unless ($ret) {
1297                 return undef;
1298         }
1299
1300         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1301         my $cmd = "";
1302         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1303         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1304         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1305         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1306         $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
1307
1308         unless (system($cmd) == 0) {
1309                 warn("RODC join failed\n$cmd");
1310                 return undef;
1311         }
1312
1313         # we overwrite the kdc after the RODC join
1314         # so that use the RODC as kdc and test
1315         # the proxy code
1316         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1317         Samba::mk_krb5_conf($ctx);
1318
1319         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1320         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1321         $ret->{RODC_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1322         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1323
1324         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1325         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1326         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1327         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1328         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1329         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1330
1331         return $ret;
1332 }
1333
1334 sub provision_plugin_s4_dc($$)
1335 {
1336         my ($self, $prefix) = @_;
1337
1338         my $prefix_abs = abs_path($prefix);
1339
1340         my $bindir_abs = abs_path($self->{bindir});
1341         my $lockdir="$prefix_abs/lockdir";
1342         my $conffile="$prefix_abs/etc/smb.conf";
1343
1344         my $extra_smbconf_options = "
1345         server services = -smb +s3fs
1346         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
1347
1348         dbwrap_tdb_mutexes:* = yes
1349
1350         kernel oplocks = no
1351         kernel change notify = no
1352
1353         syslog = no
1354         printing = bsd
1355         printcap name = /dev/null
1356
1357         max protocol = SMB3
1358         read only = no
1359         server signing = auto
1360
1361         smbd:sharedelay = 100000
1362         smbd:writetimeupdatedelay = 500000
1363         create mask = 755
1364         dos filemode = yes
1365
1366         dcerpc endpoint servers = -winreg -srvsvc
1367
1368         printcap name = /dev/null
1369
1370         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
1371         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
1372
1373         printing = vlp
1374         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1375         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1376         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1377         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1378         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1379         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1380         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1381         lpq cache time = 0
1382         print notify backchannel = yes
1383 ";
1384
1385         my $extra_smbconf_shares = "
1386
1387 [tmpenc]
1388         copy = tmp
1389         smb encrypt = required
1390
1391 [tmpcase]
1392         copy = tmp
1393         case sensitive = yes
1394
1395 [tmpguest]
1396         copy = tmp
1397         guest ok = yes
1398
1399 [hideunread]
1400         copy = tmp
1401         hide unreadable = yes
1402
1403 [durable]
1404         copy = tmp
1405         kernel share modes = no
1406         kernel oplocks = no
1407         posix locking = no
1408
1409 [print\$]
1410         copy = tmp
1411
1412 [print1]
1413         copy = tmp
1414         printable = yes
1415
1416 [print2]
1417         copy = print1
1418 [print3]
1419         copy = print1
1420 [lp]
1421         copy = print1
1422 ";
1423
1424         print "PROVISIONING PLUGIN S4 DC...";
1425         my $ret = $self->provision($prefix,
1426                                    "domain controller",
1427                                    "plugindc",
1428                                    "PLUGINDOMAIN",
1429                                    "plugindom.samba.example.com",
1430                                    "2008",
1431                                    "locDCpass1",
1432                                    undef, $extra_smbconf_options,
1433                                    $extra_smbconf_shares, undef);
1434
1435         return undef unless(defined $ret);
1436         unless($self->add_wins_config("$prefix/private")) {
1437                 warn("Unable to add wins configuration");
1438                 return undef;
1439         }
1440
1441         $ret->{DC_SERVER} = $ret->{SERVER};
1442         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1443         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1444         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1445         $ret->{DC_USERNAME} = $ret->{USERNAME};
1446         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1447
1448         return $ret;
1449 }
1450
1451 sub provision_chgdcpass($$)
1452 {
1453         my ($self, $prefix) = @_;
1454
1455         print "PROVISIONING CHGDCPASS...";
1456         my $extra_provision_options = undef;
1457         push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
1458         my $ret = $self->provision($prefix,
1459                                    "domain controller",
1460                                    "chgdcpass",
1461                                    "CHDCDOMAIN",
1462                                    "chgdcpassword.samba.example.com",
1463                                    "2008",
1464                                    "chgDCpass1",
1465                                    undef, "", "", $extra_provision_options);
1466
1467         return undef unless(defined $ret);
1468         unless($self->add_wins_config("$prefix/private")) {
1469                 warn("Unable to add wins configuration");
1470                 return undef;
1471         }
1472         
1473         # Remove secrets.tdb from this environment to test that we
1474         # still start up on systems without the new matching
1475         # secrets.tdb records.  
1476         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
1477                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
1478                 return undef;
1479         }
1480             
1481         $ret->{DC_SERVER} = $ret->{SERVER};
1482         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1483         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1484         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1485         $ret->{DC_USERNAME} = $ret->{USERNAME};
1486         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1487
1488         return $ret;
1489 }
1490
1491 sub teardown_env($$)
1492 {
1493         my ($self, $envvars) = @_;
1494         my $pid;
1495
1496         # This should cause samba to terminate gracefully
1497         close($envvars->{STDIN_PIPE});
1498
1499         $pid = $envvars->{SAMBA_PID};
1500         my $count = 0;
1501         my $childpid;
1502
1503         # This should give it time to write out the gcov data
1504         until ($count > 30) {
1505             if (Samba::cleanup_child($pid, "samba") == -1) {
1506                 last;
1507             }
1508             sleep(1);
1509             $count++;
1510         }
1511
1512         if ($count > 30 || kill(0, $pid)) {
1513             kill "TERM", $pid;
1514
1515             until ($count > 40) {
1516                 if (Samba::cleanup_child($pid, "samba") == -1) {
1517                     last;
1518                 }
1519                 sleep(1);
1520                 $count++;
1521             }
1522             # If it is still around, kill it
1523             warn "server process $pid took more than $count seconds to exit, killing\n";
1524             kill 9, $pid;
1525         }
1526
1527         $self->slapd_stop($envvars) if ($self->{ldap});
1528
1529         print $self->getlog_env($envvars);
1530
1531         return;
1532 }
1533
1534 sub getlog_env($$)
1535 {
1536         my ($self, $envvars) = @_;
1537         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
1538         my $out = $title;
1539
1540         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
1541
1542         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
1543         while (<LOG>) {
1544                 $out .= $_;
1545         }
1546         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
1547         close(LOG);
1548
1549         return "" if $out eq $title;
1550
1551         return $out;
1552 }
1553
1554 sub check_env($$)
1555 {
1556         my ($self, $envvars) = @_;
1557         my $samba_pid = $envvars->{SAMBA_PID};
1558
1559         return 1 if $samba_pid == -1;
1560
1561         my $childpid = Samba::cleanup_child($samba_pid, "samba");
1562
1563         return ($childpid == 0);
1564 }
1565
1566 sub setup_env($$$)
1567 {
1568         my ($self, $envname, $path) = @_;
1569         my $target3 = $self->{target3};
1570
1571         $ENV{ENVNAME} = $envname;
1572
1573         if (defined($self->{vars}->{$envname})) {
1574                 return $self->{vars}->{$envname};
1575         }
1576
1577         if ($envname eq "dc") {
1578                 return $self->setup_dc("$path/dc");
1579         } elsif ($envname eq "fl2000dc") {
1580                 return $self->setup_fl2000dc("$path/fl2000dc");
1581         } elsif ($envname eq "fl2003dc") {
1582                 return $self->setup_fl2003dc("$path/fl2003dc");
1583         } elsif ($envname eq "fl2008r2dc") {
1584                 return $self->setup_fl2008r2dc("$path/fl2008r2dc");
1585         } elsif ($envname eq "rpc_proxy") {
1586                 if (not defined($self->{vars}->{dc})) {
1587                         $self->setup_dc("$path/dc");
1588                 }
1589                 return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{dc});
1590         } elsif ($envname eq "vampire_dc") {
1591                 if (not defined($self->{vars}->{dc})) {
1592                         $self->setup_dc("$path/dc");
1593                 }
1594                 return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{dc});
1595         } elsif ($envname eq "promoted_dc") {
1596                 if (not defined($self->{vars}->{dc})) {
1597                         $self->setup_dc("$path/dc");
1598                 }
1599                 return $self->setup_promoted_dc("$path/promoted_dc", $self->{vars}->{dc});
1600         } elsif ($envname eq "subdom_dc") {
1601                 if (not defined($self->{vars}->{dc})) {
1602                         $self->setup_dc("$path/dc");
1603                 }
1604                 return $self->setup_subdom_dc("$path/subdom_dc", $self->{vars}->{dc});
1605         } elsif ($envname eq "s4member") {
1606                 if (not defined($self->{vars}->{dc})) {
1607                         $self->setup_dc("$path/dc");
1608                 }
1609                 return $self->setup_s4member("$path/s4member", $self->{vars}->{dc});
1610         } elsif ($envname eq "rodc") {
1611                 if (not defined($self->{vars}->{dc})) {
1612                         $self->setup_dc("$path/dc");
1613                 }
1614                 return $self->setup_rodc("$path/rodc", $self->{vars}->{dc});
1615         } elsif ($envname eq "chgdcpass") {
1616                 return $self->setup_chgdcpass("$path/chgdcpass", $self->{vars}->{chgdcpass});
1617         } elsif ($envname eq "s3member") {
1618                 if (not defined($self->{vars}->{dc})) {
1619                         $self->setup_dc("$path/dc");
1620                 }
1621                 return $target3->setup_admember("$path/s3member", $self->{vars}->{dc}, 29);
1622         } elsif ($envname eq "plugin_s4_dc") {
1623                 return $self->setup_plugin_s4_dc("$path/plugin_s4_dc");
1624         } elsif ($envname eq "s3member_rfc2307") {
1625                 if (not defined($self->{vars}->{dc})) {
1626                         $self->setup_dc("$path/dc");
1627                 }
1628                 return $target3->setup_admember_rfc2307("$path/s3member_rfc2307",
1629                                                         $self->{vars}->{dc}, 34);
1630         } elsif ($envname eq "none") {
1631                 return $self->setup_none("$path/none");
1632         } else {
1633                 return "UNKNOWN";
1634         }
1635 }
1636
1637 sub setup_s4member($$$)
1638 {
1639         my ($self, $path, $dc_vars) = @_;
1640
1641         my $env = $self->provision_s4member($path, $dc_vars);
1642
1643         if (defined $env) {
1644                 $self->check_or_start($env, "single");
1645
1646                 $self->wait_for_start($env);
1647
1648                 $self->{vars}->{s4member} = $env;
1649         }
1650
1651         return $env;
1652 }
1653
1654 sub setup_rpc_proxy($$$)
1655 {
1656         my ($self, $path, $dc_vars) = @_;
1657
1658         my $env = $self->provision_rpc_proxy($path, $dc_vars);
1659
1660         if (defined $env) {
1661                 $self->check_or_start($env, "single");
1662
1663                 $self->wait_for_start($env);
1664
1665                 $self->{vars}->{rpc_proxy} = $env;
1666         }
1667         return $env;
1668 }
1669
1670 sub setup_dc($$)
1671 {
1672         my ($self, $path) = @_;
1673
1674         my $env = $self->provision_dc($path);
1675         if (defined $env) {
1676                 $self->check_or_start($env, "standard");
1677
1678                 $self->wait_for_start($env);
1679
1680                 $self->{vars}->{dc} = $env;
1681         }
1682         return $env;
1683 }
1684
1685 sub setup_chgdcpass($$)
1686 {
1687         my ($self, $path) = @_;
1688
1689         my $env = $self->provision_chgdcpass($path);
1690         if (defined $env) {
1691                 $self->check_or_start($env, "single");
1692
1693                 $self->wait_for_start($env);
1694
1695                 $self->{vars}->{chgdcpass} = $env;
1696         }
1697         return $env;
1698 }
1699
1700 sub setup_fl2000dc($$)
1701 {
1702         my ($self, $path) = @_;
1703
1704         my $env = $self->provision_fl2000dc($path);
1705         if (defined $env) {
1706                 $self->check_or_start($env, "single");
1707
1708                 $self->wait_for_start($env);
1709
1710                 $self->{vars}->{fl2000dc} = $env;
1711         }
1712
1713         return $env;
1714 }
1715
1716 sub setup_fl2003dc($$)
1717 {
1718         my ($self, $path) = @_;
1719
1720         my $env = $self->provision_fl2003dc($path);
1721
1722         if (defined $env) {
1723                 $self->check_or_start($env, "single");
1724
1725                 $self->wait_for_start($env);
1726
1727                 $self->{vars}->{fl2003dc} = $env;
1728         }
1729         return $env;
1730 }
1731
1732 sub setup_fl2008r2dc($$)
1733 {
1734         my ($self, $path) = @_;
1735
1736         my $env = $self->provision_fl2008r2dc($path);
1737
1738         if (defined $env) {
1739                 $self->check_or_start($env, "single");
1740
1741                 $self->wait_for_start($env);
1742
1743                 $self->{vars}->{fl2008r2dc} = $env;
1744         }
1745
1746         return $env;
1747 }
1748
1749 sub setup_vampire_dc($$$)
1750 {
1751         my ($self, $path, $dc_vars) = @_;
1752
1753         my $env = $self->provision_vampire_dc($path, $dc_vars);
1754
1755         if (defined $env) {
1756                 $self->check_or_start($env, "single");
1757
1758                 $self->wait_for_start($env);
1759
1760                 $self->{vars}->{vampire_dc} = $env;
1761
1762                 # force replicated DC to update repsTo/repsFrom
1763                 # for vampired partitions
1764                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1765                 my $cmd = "";
1766                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1767                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1768                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1769                 $cmd .= " $env->{CONFIGURATION}";
1770                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1771                 unless (system($cmd) == 0) {
1772                         warn("Failed to exec kcc\n$cmd");
1773                         return undef;
1774                 }
1775
1776                 # as 'vampired' dc may add data in its local replica
1777                 # we need to synchronize data between DCs
1778                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1779                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1780                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1781                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
1782                 $cmd .= " $dc_vars->{CONFIGURATION}";
1783                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1784                 # replicate Configuration NC
1785                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
1786                 unless(system($cmd_repl) == 0) {
1787                         warn("Failed to replicate\n$cmd_repl");
1788                         return undef;
1789                 }
1790                 # replicate Default NC
1791                 $cmd_repl = "$cmd \"$base_dn\"";
1792                 unless(system($cmd_repl) == 0) {
1793                         warn("Failed to replicate\n$cmd_repl");
1794                         return undef;
1795                 }
1796         }
1797
1798         return $env;
1799 }
1800
1801 sub setup_promoted_dc($$$)
1802 {
1803         my ($self, $path, $dc_vars) = @_;
1804
1805         my $env = $self->provision_promoted_dc($path, $dc_vars);
1806
1807         if (defined $env) {
1808                 $self->check_or_start($env, "single");
1809
1810                 $self->wait_for_start($env);
1811
1812                 $self->{vars}->{promoted_dc} = $env;
1813
1814                 # force replicated DC to update repsTo/repsFrom
1815                 # for vampired partitions
1816                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1817                 my $cmd = "";
1818                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1819                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1820                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1821                 $cmd .= " $env->{CONFIGURATION}";
1822                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1823                 unless (system($cmd) == 0) {
1824                         warn("Failed to exec kcc\n$cmd");
1825                         return undef;
1826                 }
1827
1828                 # as 'vampired' dc may add data in its local replica
1829                 # we need to synchronize data between DCs
1830                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1831                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1832                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1833                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
1834                 $cmd .= " $dc_vars->{CONFIGURATION}";
1835                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1836                 # replicate Configuration NC
1837                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
1838                 unless(system($cmd_repl) == 0) {
1839                         warn("Failed to replicate\n$cmd_repl");
1840                         return undef;
1841                 }
1842                 # replicate Default NC
1843                 $cmd_repl = "$cmd \"$base_dn\"";
1844                 unless(system($cmd_repl) == 0) {
1845                         warn("Failed to replicate\n$cmd_repl");
1846                         return undef;
1847                 }
1848         }
1849
1850         return $env;
1851 }
1852
1853 sub setup_subdom_dc($$$)
1854 {
1855         my ($self, $path, $dc_vars) = @_;
1856
1857         my $env = $self->provision_subdom_dc($path, $dc_vars);
1858
1859         if (defined $env) {
1860                 $self->check_or_start($env, "single");
1861
1862                 $self->wait_for_start($env);
1863
1864                 $self->{vars}->{subdom_dc} = $env;
1865
1866                 # force replicated DC to update repsTo/repsFrom
1867                 # for primary domain partitions
1868                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1869                 my $cmd = "";
1870                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1871                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1872                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1873                 $cmd .= " $env->{CONFIGURATION}";
1874                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
1875                 unless (system($cmd) == 0) {
1876                         warn("Failed to exec kcc\n$cmd");
1877                         return undef;
1878                 }
1879
1880                 # as 'subdomain' dc may add data in its local replica
1881                 # we need to synchronize data between DCs
1882                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
1883                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1884                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1885                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1886                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
1887                 $cmd .= " $dc_vars->{CONFIGURATION}";
1888                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
1889                 # replicate Configuration NC
1890                 my $cmd_repl = "$cmd \"$config_dn\"";
1891                 unless(system($cmd_repl) == 0) {
1892                         warn("Failed to replicate\n$cmd_repl");
1893                         return undef;
1894                 }
1895                 # replicate Default NC
1896                 $cmd_repl = "$cmd \"$base_dn\"";
1897                 unless(system($cmd_repl) == 0) {
1898                         warn("Failed to replicate\n$cmd_repl");
1899                         return undef;
1900                 }
1901         }
1902
1903         return $env;
1904 }
1905
1906 sub setup_rodc($$$)
1907 {
1908         my ($self, $path, $dc_vars) = @_;
1909
1910         my $env = $self->provision_rodc($path, $dc_vars);
1911
1912         unless ($env) {
1913                 return undef;
1914         }
1915
1916         $self->check_or_start($env, "single");
1917
1918         $self->wait_for_start($env);
1919
1920         $self->{vars}->{rodc} = $env;
1921
1922         return $env;
1923 }
1924
1925 sub setup_plugin_s4_dc($$)
1926 {
1927         my ($self, $path) = @_;
1928
1929         # If we didn't build with ADS, pretend this env was never available
1930         if (not $self->{target3}->have_ads()) {
1931                return "UNKNOWN";
1932         }
1933
1934         my $env = $self->provision_plugin_s4_dc($path);
1935         unless ($env) {
1936                 return undef;
1937         }
1938
1939         $self->check_or_start($env, "single");
1940         
1941         $self->wait_for_start($env);
1942         
1943         $self->{vars}->{plugin_s4_dc} = $env;
1944         return $env;
1945 }
1946
1947 sub setup_none($$)
1948 {
1949         my ($self, $path) = @_;
1950
1951         my $ret = {
1952                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
1953                 SAMBA_PID => -1,
1954         }
1955 }
1956
1957 1;