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