selftest/s4: remove illegal function signature
[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 # NOTE: Refer to the README for more details about the various testenvs,
7 # and tips about adding new testenvs.
8
9 package Samba4;
10
11 use strict;
12 use warnings;
13 use Cwd qw(abs_path);
14 use FindBin qw($RealBin);
15 use POSIX;
16 use SocketWrapper;
17 use target::Samba;
18 use target::Samba3;
19 use Archive::Tar;
20 use File::Path 'make_path';
21
22 sub new($$$$$) {
23         my ($classname, $bindir, $srcdir, $server_maxtime) = @_;
24
25         my $self = {
26                 vars => {},
27                 bindir => $bindir,
28                 srcdir => $srcdir,
29                 server_maxtime => $server_maxtime,
30                 target3 => new Samba3($bindir, $srcdir, $server_maxtime)
31         };
32         bless $self;
33         return $self;
34 }
35
36 sub scriptdir_path($$) {
37         my ($self, $path) = @_;
38         return "$self->{srcdir}/source4/scripting/$path";
39 }
40
41 sub check_or_start($$$)
42 {
43         my ($self, $env_vars, $process_model) = @_;
44         my $STDIN_READER;
45
46         my $env_ok = $self->check_env($env_vars);
47         if ($env_ok) {
48                 return $env_vars->{SAMBA_PID};
49         } elsif (defined($env_vars->{SAMBA_PID})) {
50                 warn("SAMBA PID $env_vars->{SAMBA_PID} is not running (died)");
51                 return undef;
52         }
53
54         # use a pipe for stdin in the child processes. This allows
55         # those processes to monitor the pipe for EOF to ensure they
56         # exit when the test script exits
57         pipe($STDIN_READER, $env_vars->{STDIN_PIPE});
58
59         # build up the command to run samba
60         my @preargs = ();
61         my @optargs = ();
62         if (defined($ENV{SAMBA_OPTIONS})) {
63                 @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
64         }
65         if(defined($ENV{SAMBA_VALGRIND})) {
66                 @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
67         }
68
69         if (defined($process_model)) {
70                 push @optargs, ("-M", $process_model);
71         }
72         my $binary = Samba::bindir_path($self, "samba");
73         my @full_cmd = (@preargs, $binary, "-i",
74                         "--no-process-group", "--maximum-runtime=$self->{server_maxtime}",
75                         $env_vars->{CONFIGURATION}, @optargs);
76
77         # the samba process takes some additional env variables (compared to s3)
78         my $samba_envs = Samba::get_env_for_process("samba", $env_vars);
79         $samba_envs->{RESOLV_CONF} = $env_vars->{RESOLV_CONF};
80         $samba_envs->{UID_WRAPPER} = "1";
81         if (defined($ENV{MITKRB5})) {
82                 $samba_envs->{KRB5_KDC_PROFILE} = $env_vars->{MITKDC_CONFIG};
83         }
84
85         # fork a child process and exec() samba
86         my $daemon_ctx = {
87                 NAME => "samba",
88                 BINARY_PATH => $binary,
89                 FULL_CMD => [ @full_cmd ],
90                 LOG_FILE => $env_vars->{SAMBA_TEST_LOG},
91                 TEE_STDOUT => 1,
92                 ENV_VARS => $samba_envs,
93         };
94         my $pid = Samba::fork_and_exec($self, $env_vars, $daemon_ctx, $STDIN_READER);
95
96         $env_vars->{SAMBA_PID} = $pid;
97
98         # close the parent's read-end of the pipe
99         close($STDIN_READER);
100
101         if ($self->wait_for_start($env_vars) != 0) {
102             warn("Samba $pid failed to start up");
103             return undef;
104         }
105
106         return $pid;
107 }
108
109 sub wait_for_start($$)
110 {
111         my ($self, $testenv_vars) = @_;
112         my $count = 0;
113         my $ret = 0;
114
115         if (not $self->check_env($testenv_vars)) {
116             warn("unable to confirm Samba $testenv_vars->{SAMBA_PID} is running");
117             return -1;
118         }
119
120         # This will return quickly when things are up, but be slow if we
121         # need to wait for (eg) SSL init
122         my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
123
124         do {
125                 $ret = system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
126                 if ($ret != 0) {
127                         sleep(1);
128                 } else {
129                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
130                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
131                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
132                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
133                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
134                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
135                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
136                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
137                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
138                         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
139                         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
140                 }
141                 $count++;
142         } while ($ret != 0 && $count < 20);
143         if ($count == 20) {
144                 teardown_env($self, $testenv_vars);
145                 warn("nbt not reachable after 20 retries\n");
146                 return -1;
147         }
148
149         # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
150         if ($testenv_vars->{SERVER_ROLE} eq "domain controller") {
151                 print "waiting for working LDAP and a RID Set to be allocated\n";
152                 my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
153                 my $count = 0;
154                 my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
155
156                 my $search_dn = $base_dn;
157                 if ($testenv_vars->{NETBIOSNAME} ne "RODC") {
158                         # TODO currently no check for actual rIDAllocationPool
159                         $search_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
160                 }
161                 my $max_wait = 60;
162
163                 # Add hosts file for name lookups
164                 my $cmd = "NSS_WRAPPER_HOSTS='$testenv_vars->{NSS_WRAPPER_HOSTS}' ";
165                 if (defined($testenv_vars->{RESOLV_WRAPPER_CONF})) {
166                         $cmd .= "RESOLV_WRAPPER_CONF='$testenv_vars->{RESOLV_WRAPPER_CONF}' ";
167                 } else {
168                         $cmd .= "RESOLV_WRAPPER_HOSTS='$testenv_vars->{RESOLV_WRAPPER_HOSTS}' ";
169                 }
170                 $cmd .= "RESOLV_CONF='$testenv_vars->{RESOLV_CONF}' ";
171
172                 $cmd .= "$ldbsearch ";
173                 $cmd .= "$testenv_vars->{CONFIGURATION} ";
174                 $cmd .= "-H ldap://$testenv_vars->{SERVER} ";
175                 $cmd .= "-U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} ";
176                 $cmd .= "-s base ";
177                 $cmd .= "-b '$search_dn' ";
178                 while (system("$cmd >/dev/null") != 0) {
179                         $count++;
180                         if ($count > $max_wait) {
181                                 teardown_env($self, $testenv_vars);
182                                 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}");
183                                 return -1;
184                         }
185                         print "Waiting for working LDAP...\n";
186                         sleep(1);
187                 }
188         }
189
190         my $wbinfo =  Samba::bindir_path($self, "wbinfo");
191
192         $count = 0;
193         do {
194                 my $cmd = "NSS_WRAPPER_PASSWD=$testenv_vars->{NSS_WRAPPER_PASSWD} ";
195                 $cmd .= "NSS_WRAPPER_GROUP=$testenv_vars->{NSS_WRAPPER_GROUP} ";
196                 $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=$testenv_vars->{SELFTEST_WINBINDD_SOCKET_DIR} ";
197                 $cmd .= "$wbinfo -P";
198                 $ret = system($cmd);
199
200                 if ($ret != 0) {
201                         sleep(1);
202                 }
203                 $count++;
204         } while ($ret != 0 && $count < 20);
205         if ($count == 20) {
206                 teardown_env($self, $testenv_vars);
207                 warn("winbind not reachable after 20 retries\n");
208                 return -1;
209         }
210
211         # Ensure we registered all our names
212         if ($testenv_vars->{SERVER_ROLE} eq "domain controller") {
213                 my $max_wait = 120;
214                 print "Waiting for dns_update_cache to be created.\n";
215                 $count = 0;
216                 while (not -e "$testenv_vars->{PRIVATEDIR}/dns_update_cache") {
217                         $count++;
218                         if ($count > $max_wait) {
219                                 teardown_env($self, $testenv_vars);
220                                 warn("Timed out ($max_wait sec) waiting for dns_update_cache PID $testenv_vars->{SAMBA_PID}");
221                                 return -1;
222                         }
223                         print "Waiting for dns_update_cache to be created...\n";
224                         sleep(1);
225                 }
226                 print "Waiting for dns_update_cache to be filled.\n";
227                 $count = 0;
228                 while ((-s "$testenv_vars->{PRIVATEDIR}/dns_update_cache") == 0) {
229                         $count++;
230                         if ($count > $max_wait) {
231                                 teardown_env($self, $testenv_vars);
232                                 warn("Timed out ($max_wait sec) waiting for dns_update_cache PID $testenv_vars->{SAMBA_PID}");
233                                 return -1;
234                         }
235                         print "Waiting for dns_update_cache to be filled...\n";
236                         sleep(1);
237                 }
238         }
239
240         print $self->getlog_env($testenv_vars);
241
242         print "READY ($testenv_vars->{SAMBA_PID})\n";
243
244         return 0
245 }
246
247 sub write_ldb_file($$$)
248 {
249         my ($self, $file, $ldif) = @_;
250
251         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
252         open(LDIF, "|$ldbadd -H $file >/dev/null");
253         print LDIF $ldif;
254         return(close(LDIF));
255 }
256
257 sub add_wins_config($$)
258 {
259         my ($self, $privatedir) = @_;
260         my $client_ip = Samba::get_ipv4_addr("client");
261
262         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
263 dn: name=TORTURE_11,CN=PARTNERS
264 objectClass: wreplPartner
265 name: TORTURE_11
266 address: $client_ip
267 pullInterval: 0
268 pushChangeCount: 0
269 type: 0x3
270 ");
271 }
272
273 sub setup_dns_hub_internal($$$)
274 {
275         my ($self, $hostname, $prefix) = @_;
276         my $STDIN_READER;
277
278         unless(-d $prefix or make_path($prefix, 0777)) {
279                 warn("Unable to create $prefix");
280                 return undef;
281         }
282         my $prefix_abs = abs_path($prefix);
283
284         die ("prefix=''") if $prefix_abs eq "";
285         die ("prefix='/'") if $prefix_abs eq "/";
286
287         unless (system("rm -rf $prefix_abs/*") == 0) {
288                 warn("Unable to clean up");
289         }
290
291         my $env = undef;
292         $env->{NETBIOSNAME} = $hostname;
293
294         $env->{SERVER_IP} = Samba::get_ipv4_addr($hostname);
295         $env->{SERVER_IPV6} = Samba::get_ipv6_addr($hostname);
296         $env->{SOCKET_WRAPPER_DEFAULT_IFACE} = Samba::get_interface($hostname);
297         $env->{DNS_HUB_LOG} = "$prefix_abs/dns_hub.log";
298         $env->{RESOLV_CONF} = "$prefix_abs/resolv.conf";
299         $env->{TESTENV_DIR} = $prefix_abs;
300
301         open(RESOLV_CONF, ">$env->{RESOLV_CONF}");
302         print RESOLV_CONF "nameserver $env->{SERVER_IP}\n";
303         print RESOLV_CONF "nameserver $env->{SERVER_IPV6}\n";
304         close(RESOLV_CONF);
305
306         my @preargs = ();
307         my @args = ();
308         if (!defined($ENV{PYTHON})) {
309             push (@preargs, "env");
310             push (@preargs, "python");
311         } else {
312             push (@preargs, $ENV{PYTHON});
313         }
314         my $binary = "$self->{srcdir}/selftest/target/dns_hub.py";
315         push (@args, "$self->{server_maxtime}");
316         push (@args, "$env->{SERVER_IP}");
317         push (@args, Samba::realm_to_ip_mappings());
318         my @full_cmd = (@preargs, $binary, @args);
319
320         my $daemon_ctx = {
321                 NAME => "dnshub",
322                 BINARY_PATH => $binary,
323                 FULL_CMD => [ @full_cmd ],
324                 LOG_FILE => $env->{DNS_HUB_LOG},
325                 TEE_STDOUT => 1,
326                 ENV_VARS => {},
327         };
328
329         my $pcap_dir = $ENV{SOCKET_WRAPPER_PCAP_DIR};
330         if (defined $pcap_dir) {
331                 $daemon_ctx->{PCAP_FILE} = "$pcap_dir/env-$hostname$.pcap",
332         }
333
334         # use a pipe for stdin in the child processes. This allows
335         # those processes to monitor the pipe for EOF to ensure they
336         # exit when the test script exits
337         pipe($STDIN_READER, $env->{STDIN_PIPE});
338
339         my $pid = Samba::fork_and_exec($self, $env, $daemon_ctx, $STDIN_READER);
340
341         $env->{SAMBA_PID} = $pid;
342         $env->{KRB5_CONFIG} = "$prefix_abs/no_krb5.conf";
343
344         # close the parent's read-end of the pipe
345         close($STDIN_READER);
346
347         return $env;
348 }
349
350 sub setup_dns_hub
351 {
352         my ($self, $prefix) = @_;
353
354         my $hostname = "rootdnsforwarder";
355
356         my $env = $self->setup_dns_hub_internal("$hostname", "$prefix/$hostname");
357
358         $self->{dns_hub_env} = $env;
359
360         return $env;
361 }
362
363 sub get_dns_hub_env($)
364 {
365         my ($self, $prefix) = @_;
366
367         if (defined($self->{dns_hub_env})) {
368                 return $self->{dns_hub_env};
369         }
370
371         die("get_dns_hub_env() not setup 'dns_hub_env'");
372         return undef;
373 }
374
375 # Returns the environmental variables that we pass to samba-tool commands
376 sub get_cmd_env_vars
377 {
378         my ($self, $localenv) = @_;
379
380         my $cmd_env = "NSS_WRAPPER_HOSTS='$localenv->{NSS_WRAPPER_HOSTS}' ";
381         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
382         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
383                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
384         } else {
385                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
386         }
387         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\" ";
388         $cmd_env .= "KRB5CCNAME=\"$localenv->{KRB5_CCACHE}\" ";
389         $cmd_env .= "RESOLV_CONF=\"$localenv->{RESOLV_CONF}\" ";
390
391         return $cmd_env;
392 }
393
394 # Sets up a forest trust namespace.
395 # (Note this is different to kernel namespaces, setup by the
396 # USE_NAMESPACES=1 option)
397 sub setup_namespaces
398 {
399         my ($self, $localenv, $upn_array, $spn_array) = @_;
400
401         @{$upn_array} = [] unless defined($upn_array);
402         my $upn_args = "";
403         foreach my $upn (@{$upn_array}) {
404                 $upn_args .= " --add-upn-suffix=$upn";
405         }
406
407         @{$spn_array} = [] unless defined($spn_array);
408         my $spn_args = "";
409         foreach my $spn (@{$spn_array}) {
410                 $spn_args .= " --add-spn-suffix=$spn";
411         }
412
413         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
414
415         my $cmd_env = $self->get_cmd_env_vars($localenv);
416
417         my $cmd_config = " $localenv->{CONFIGURATION}";
418
419         my $namespaces = $cmd_env;
420         $namespaces .= " $samba_tool domain trust namespaces $upn_args $spn_args";
421         $namespaces .= $cmd_config;
422         unless (system($namespaces) == 0) {
423                 warn("Failed to add namespaces \n$namespaces");
424                 return;
425         }
426
427         return;
428 }
429
430 sub setup_trust($$$$$)
431 {
432         my ($self, $localenv, $remoteenv, $type, $extra_args) = @_;
433
434         $localenv->{TRUST_SERVER} = $remoteenv->{SERVER};
435
436         $localenv->{TRUST_USERNAME} = $remoteenv->{USERNAME};
437         $localenv->{TRUST_PASSWORD} = $remoteenv->{PASSWORD};
438         $localenv->{TRUST_DOMAIN} = $remoteenv->{DOMAIN};
439         $localenv->{TRUST_REALM} = $remoteenv->{REALM};
440         $localenv->{TRUST_DOMSID} = $remoteenv->{DOMSID};
441
442         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
443
444         # setup the trust
445         my $cmd_env = $self->get_cmd_env_vars($localenv);
446
447         my $cmd_config = " $localenv->{CONFIGURATION}";
448         my $cmd_creds = $cmd_config;
449         $cmd_creds .= " -U$localenv->{TRUST_DOMAIN}\\\\$localenv->{TRUST_USERNAME}\%$localenv->{TRUST_PASSWORD}";
450
451         my $create = $cmd_env;
452         $create .= " $samba_tool domain trust create --type=${type} $localenv->{TRUST_REALM}";
453         $create .= " $extra_args";
454         $create .= $cmd_creds;
455         unless (system($create) == 0) {
456                 warn("Failed to create trust \n$create");
457                 return undef;
458         }
459
460         my $groupname = "g_$localenv->{TRUST_DOMAIN}";
461         my $groupadd = $cmd_env;
462         $groupadd .= " $samba_tool group add '$groupname' --group-scope=Domain $cmd_config";
463         unless (system($groupadd) == 0) {
464                 warn("Failed to create group \n$groupadd");
465                 return undef;
466         }
467         my $groupmem = $cmd_env;
468         $groupmem .= " $samba_tool group addmembers '$groupname' '$localenv->{TRUST_DOMSID}-513' $cmd_config";
469         unless (system($groupmem) == 0) {
470                 warn("Failed to add group member \n$groupmem");
471                 return undef;
472         }
473
474         return $localenv
475 }
476
477 sub provision_raw_prepare($$$$$$$$$$$$)
478 {
479         my ($self, $prefix, $server_role, $hostname,
480             $domain, $realm, $samsid, $functional_level,
481             $password, $kdc_ipv4, $kdc_ipv6) = @_;
482         my $ctx;
483         my $python_cmd = "";
484         if (defined $ENV{PYTHON}) {
485                 $python_cmd = $ENV{PYTHON} . " ";
486         }
487         $ctx->{python} = $python_cmd;
488         my $netbiosname = uc($hostname);
489
490         unless(-d $prefix or mkdir($prefix, 0777)) {
491                 warn("Unable to create $prefix");
492                 return undef;
493         }
494         my $prefix_abs = abs_path($prefix);
495
496         die ("prefix=''") if $prefix_abs eq "";
497         die ("prefix='/'") if $prefix_abs eq "/";
498
499         unless (system("rm -rf $prefix_abs/*") == 0) {
500                 warn("Unable to clean up");
501         }
502
503         
504         my $swiface = Samba::get_interface($hostname);
505
506         $ctx->{prefix} = $prefix;
507         $ctx->{prefix_abs} = $prefix_abs;
508
509         $ctx->{server_role} = $server_role;
510         $ctx->{hostname} = $hostname;
511         $ctx->{netbiosname} = $netbiosname;
512         $ctx->{swiface} = $swiface;
513         $ctx->{password} = $password;
514         $ctx->{kdc_ipv4} = $kdc_ipv4;
515         $ctx->{kdc_ipv6} = $kdc_ipv6;
516         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
517         if ($functional_level eq "2000") {
518                 $ctx->{supported_enctypes} = "arcfour-hmac-md5 des-cbc-md5 des-cbc-crc"
519         }
520
521 #
522 # Set smbd log level here.
523 #
524         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
525         $ctx->{username} = "Administrator";
526         $ctx->{domain} = $domain;
527         $ctx->{realm} = uc($realm);
528         $ctx->{dnsname} = lc($realm);
529         $ctx->{samsid} = $samsid;
530
531         $ctx->{functional_level} = $functional_level;
532
533         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
534         chomp $unix_name;
535         $ctx->{unix_name} = $unix_name;
536         $ctx->{unix_uid} = $>;
537         my @mygid = split(" ", $();
538         $ctx->{unix_gid} = $mygid[0];
539         $ctx->{unix_gids_str} = $);
540         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
541
542         $ctx->{etcdir} = "$prefix_abs/etc";
543         $ctx->{piddir} = "$prefix_abs/pid";
544         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
545         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
546         $ctx->{krb5_ccache} = "$prefix_abs/krb5_ccache";
547         $ctx->{mitkdc_conf} = "$ctx->{etcdir}/mitkdc.conf";
548         $ctx->{privatedir} = "$prefix_abs/private";
549         $ctx->{binddnsdir} = "$prefix_abs/bind-dns";
550         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
551         $ctx->{lockdir} = "$prefix_abs/lockdir";
552         $ctx->{logdir} = "$prefix_abs/logs";
553         $ctx->{statedir} = "$prefix_abs/statedir";
554         $ctx->{cachedir} = "$prefix_abs/cachedir";
555         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
556         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
557         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
558         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
559         $ctx->{nsswrap_hosts} = "$ENV{SELFTEST_PREFIX}/hosts";
560         $ctx->{nsswrap_hostname} = "$ctx->{hostname}.$ctx->{dnsname}";
561         if ($ENV{SAMBA_DNS_FAKING}) {
562                 $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
563                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf} --all-interfaces --use-file=$ctx->{dns_host_file}";
564                 $ctx->{samba_dnsupdate} = $python_cmd .  $ctx->{samba_dnsupdate};
565         } else {
566                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf} --all-interfaces";
567                 $ctx->{samba_dnsupdate} = $python_cmd .  $ctx->{samba_dnsupdate};
568                 $ctx->{use_resolv_wrapper} = 1;
569         }
570
571         my $dns_hub = $self->get_dns_hub_env();
572         $ctx->{resolv_conf} = $dns_hub->{RESOLV_CONF};
573
574         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
575
576         $ctx->{ipv4} = Samba::get_ipv4_addr($hostname);
577         $ctx->{ipv6} = Samba::get_ipv6_addr($hostname);
578
579         push(@{$ctx->{directories}}, $ctx->{privatedir});
580         push(@{$ctx->{directories}}, $ctx->{binddnsdir});
581         push(@{$ctx->{directories}}, $ctx->{etcdir});
582         push(@{$ctx->{directories}}, $ctx->{piddir});
583         push(@{$ctx->{directories}}, $ctx->{lockdir});
584         push(@{$ctx->{directories}}, $ctx->{logdir});
585         push(@{$ctx->{directories}}, $ctx->{statedir});
586         push(@{$ctx->{directories}}, $ctx->{cachedir});
587
588         $ctx->{smb_conf_extra_options} = "";
589
590         my @provision_options = ();
591         push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_conf}\"");
592         push (@provision_options, "KRB5_CCACHE=\"$ctx->{krb5_ccache}\"");
593         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
594         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
595         push (@provision_options, "NSS_WRAPPER_HOSTS=\"$ctx->{nsswrap_hosts}\"");
596         push (@provision_options, "NSS_WRAPPER_HOSTNAME=\"$ctx->{nsswrap_hostname}\"");
597         if (defined($ctx->{use_resolv_wrapper})) {
598                 push (@provision_options, "RESOLV_WRAPPER_CONF=\"$ctx->{resolv_conf}\"");
599                 push (@provision_options, "RESOLV_CONF=\"$ctx->{resolv_conf}\"");
600         } else {
601                 push (@provision_options, "RESOLV_WRAPPER_HOSTS=\"$ctx->{dns_host_file}\"");
602         }
603         if (defined($ENV{GDB_PROVISION})) {
604                 push (@provision_options, "gdb --args");
605                 if (!defined($ENV{PYTHON})) {
606                     push (@provision_options, "env");
607                     push (@provision_options, "python");
608                 }
609         }
610         if (defined($ENV{VALGRIND_PROVISION})) {
611                 push (@provision_options, "valgrind");
612                 if (!defined($ENV{PYTHON})) {
613                     push (@provision_options, "env");
614                     push (@provision_options, "python");
615                 }
616         }
617
618         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
619
620         push (@provision_options, $samba_tool);
621         push (@provision_options, "domain");
622         push (@provision_options, "provision");
623         push (@provision_options, "--configfile=$ctx->{smb_conf}");
624         push (@provision_options, "--host-name=$ctx->{hostname}");
625         push (@provision_options, "--host-ip=$ctx->{ipv4}");
626         push (@provision_options, "--quiet");
627         push (@provision_options, "--domain=$ctx->{domain}");
628         push (@provision_options, "--realm=$ctx->{realm}");
629         if (defined($ctx->{samsid})) {
630                 push (@provision_options, "--domain-sid=$ctx->{samsid}");
631         }
632         push (@provision_options, "--adminpass=$ctx->{password}");
633         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
634         push (@provision_options, "--machinepass=machine$ctx->{password}");
635         push (@provision_options, "--root=$ctx->{unix_name}");
636         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
637         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
638
639         @{$ctx->{provision_options}} = @provision_options;
640
641         return $ctx;
642 }
643
644 sub has_option
645 {
646         my ($self, $keyword, @options_list) = @_;
647
648         # convert the options-list to a hash-map for easy keyword lookup
649         my %options_dict = map { $_ => 1 } @options_list;
650
651         return exists $options_dict{$keyword};
652 }
653
654 #
655 # Step1 creates the basic configuration
656 #
657 sub provision_raw_step1($$)
658 {
659         my ($self, $ctx) = @_;
660
661         mkdir($_, 0777) foreach (@{$ctx->{directories}});
662
663         ##
664         ## lockdir and piddir must be 0755
665         ##
666         chmod 0755, $ctx->{lockdir};
667         chmod 0755, $ctx->{piddir};
668
669         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
670                 warn("can't open $ctx->{smb_conf}$?");
671                 return undef;
672         }
673
674         Samba::prepare_keyblobs($ctx);
675         my $crlfile = "$ctx->{tlsdir}/crl.pem";
676         $crlfile = "" unless -e ${crlfile};
677
678         # work out which file server to use. Default to source3 smbd (s3fs),
679         # unless the source4 NTVFS (smb) file server has been specified
680         my $services = "-smb +s3fs";
681         if ($self->has_option("--use-ntvfs", @{$ctx->{provision_options}})) {
682                 $services = "+smb -s3fs";
683         }
684
685         my $interfaces = Samba::get_interfaces_config($ctx->{netbiosname});
686
687         print CONFFILE "
688 [global]
689         netbios name = $ctx->{netbiosname}
690         posix:eadb = $ctx->{statedir}/eadb.tdb
691         workgroup = $ctx->{domain}
692         realm = $ctx->{realm}
693         private dir = $ctx->{privatedir}
694         binddns dir = $ctx->{binddnsdir}
695         pid directory = $ctx->{piddir}
696         ncalrpc dir = $ctx->{ncalrpcdir}
697         lock dir = $ctx->{lockdir}
698         state directory = $ctx->{statedir}
699         cache directory = $ctx->{cachedir}
700         winbindd socket directory = $ctx->{winbindd_socket_dir}
701         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
702         winbind separator = /
703         interfaces = $interfaces
704         tls dh params file = $ctx->{tlsdir}/dhparms.pem
705         tls crlfile = ${crlfile}
706         tls verify peer = no_check
707         panic action = $RealBin/gdb_backtrace \%d
708         wins support = yes
709         server role = $ctx->{server_role}
710         server services = +echo $services
711         dcerpc endpoint servers = +winreg +srvsvc
712         notify:inotify = false
713         ldb:nosync = true
714         ldap server require strong auth = yes
715 #We don't want to pass our self-tests if the PAC code is wrong
716         gensec:require_pac = true
717         log file = $ctx->{logdir}/log.\%m
718         log level = $ctx->{server_loglevel}
719         lanman auth = Yes
720         ntlm auth = Yes
721         client min protocol = CORE
722         server min protocol = LANMAN1
723         mangled names = yes
724         dns update command = $ctx->{samba_dnsupdate}
725         spn update command = $ctx->{python} $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
726         gpo update command = $ctx->{python} $ENV{SRCDIR_ABS}/source4/scripting/bin/samba-gpupdate -s $ctx->{smb_conf} --target=Computer
727         samba kcc command = $ctx->{python} $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_kcc
728         dreplsrv:periodic_startup_interval = 0
729         dsdb:schema update allowed = yes
730
731         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
732
733         idmap_ldb:use rfc2307=yes
734         winbind enum users = yes
735         winbind enum groups = yes
736
737         rpc server port:netlogon = 1026
738         include system krb5 conf = no
739
740 ";
741
742         print CONFFILE "
743
744         # Begin extra options
745         $ctx->{smb_conf_extra_options}
746         # End extra options
747 ";
748         close(CONFFILE);
749
750         #Default the KDC IP to the server's IP
751         if (not defined($ctx->{kdc_ipv4})) {
752                 $ctx->{kdc_ipv4} = $ctx->{ipv4};
753         }
754         if (not defined($ctx->{kdc_ipv6})) {
755                 $ctx->{kdc_ipv6} = $ctx->{ipv6};
756         }
757
758         Samba::mk_krb5_conf($ctx);
759         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
760
761         open(PWD, ">$ctx->{nsswrap_passwd}");
762         if ($ctx->{unix_uid} != 0) {
763                 print PWD "root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false\n";
764         }
765         print PWD "$ctx->{unix_name}:x:$ctx->{unix_uid}:65531:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false\n";
766         print PWD "nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
767 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
768 pdbtest2:x:65532:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
769 pdbtest3:x:65531:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
770 pdbtest4:x:65530:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
771 ";
772         close(PWD);
773         my $uid_rfc2307test = 65533;
774
775         open(GRP, ">$ctx->{nsswrap_group}");
776         if ($ctx->{unix_gid} != 0) {
777                 print GRP "root:x:0:\n";
778         }
779         print GRP "$ctx->{unix_name}:x:$ctx->{unix_gid}:\n";
780         print GRP "wheel:x:10:
781 users:x:65531:
782 nobody:x:65533:
783 nogroup:x:65534:nobody
784 ";
785         close(GRP);
786         my $gid_rfc2307test = 65532;
787
788         my $hostname = lc($ctx->{hostname});
789         open(HOSTS, ">>$ctx->{nsswrap_hosts}");
790         if ($hostname eq "localdc") {
791                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
792                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
793         } else {
794                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} ${hostname}\n";
795                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} ${hostname}\n";
796         }
797         close(HOSTS);
798
799         my $configuration = "--configfile=$ctx->{smb_conf}";
800
801 #Ensure the config file is valid before we start
802         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
803         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
804                 system("$testparm -v --suppress-prompt $configuration >&2");
805                 warn("Failed to create a valid smb.conf configuration $testparm!");
806                 return undef;
807         }
808         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) {
809                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
810                 return undef;
811         }
812
813         # Return the environment variables for the new testenv DC.
814         # Note that we have SERVER_X and DC_SERVER_X variables (which have the same
815         # value initially). In a 2 DC setup, $DC_SERVER_X will always be the PDC.
816         my $ret = {
817                 KRB5_CONFIG => $ctx->{krb5_conf},
818                 KRB5_CCACHE => $ctx->{krb5_ccache},
819                 MITKDC_CONFIG => $ctx->{mitkdc_conf},
820                 PIDDIR => $ctx->{piddir},
821                 SERVER => $ctx->{hostname},
822                 DC_SERVER => $ctx->{hostname},
823                 SERVER_IP => $ctx->{ipv4},
824                 DC_SERVER_IP => $ctx->{ipv4},
825                 SERVER_IPV6 => $ctx->{ipv6},
826                 DC_SERVER_IPV6 => $ctx->{ipv6},
827                 NETBIOSNAME => $ctx->{netbiosname},
828                 DC_NETBIOSNAME => $ctx->{netbiosname},
829                 DOMAIN => $ctx->{domain},
830                 USERNAME => $ctx->{username},
831                 DC_USERNAME => $ctx->{username},
832                 REALM => $ctx->{realm},
833                 DNSNAME => $ctx->{dnsname},
834                 SAMSID => $ctx->{samsid},
835                 PASSWORD => $ctx->{password},
836                 DC_PASSWORD => $ctx->{password},
837                 LDAPDIR => $ctx->{ldapdir},
838                 LDAP_INSTANCE => $ctx->{ldap_instance},
839                 SELFTEST_WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
840                 NCALRPCDIR => $ctx->{ncalrpcdir},
841                 LOCKDIR => $ctx->{lockdir},
842                 STATEDIR => $ctx->{statedir},
843                 CACHEDIR => $ctx->{cachedir},
844                 PRIVATEDIR => $ctx->{privatedir},
845                 BINDDNSDIR => $ctx->{binddnsdir},
846                 SERVERCONFFILE => $ctx->{smb_conf},
847                 TESTENV_DIR => $ctx->{prefix_abs},
848                 CONFIGURATION => $configuration,
849                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
850                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
851                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
852                 NSS_WRAPPER_HOSTS => $ctx->{nsswrap_hosts},
853                 NSS_WRAPPER_HOSTNAME => $ctx->{nsswrap_hostname},
854                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
855                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
856                 SAMBA_TEST_LOG_POS => 0,
857                 NSS_WRAPPER_MODULE_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
858                 NSS_WRAPPER_MODULE_FN_PREFIX => "winbind",
859                 LOCAL_PATH => $ctx->{share},
860                 UID_RFC2307TEST => $uid_rfc2307test,
861                 GID_RFC2307TEST => $gid_rfc2307test,
862                 SERVER_ROLE => $ctx->{server_role},
863                 RESOLV_CONF => $ctx->{resolv_conf}
864         };
865
866         if (defined($ctx->{use_resolv_wrapper})) {
867                 $ret->{RESOLV_WRAPPER_CONF} = $ctx->{resolv_conf};
868         } else {
869                 $ret->{RESOLV_WRAPPER_HOSTS} = $ctx->{dns_host_file};
870         }
871
872         if ($ctx->{server_role} eq "domain controller") {
873                 $ret->{DOMSID} = $ret->{SAMSID};
874         }
875
876         return $ret;
877 }
878
879 #
880 # Step2 runs the provision script
881 #
882 sub provision_raw_step2($$$)
883 {
884         my ($self, $ctx, $ret) = @_;
885
886         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
887         unless (system($provision_cmd) == 0) {
888                 warn("Unable to provision: \n$provision_cmd\n");
889                 return undef;
890         }
891
892         my $testallowed_account = "testallowed";
893         my $samba_tool_cmd = "";
894         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
895         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
896         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
897             . " user create --configfile=$ctx->{smb_conf} $testallowed_account $ctx->{password}";
898         unless (system($samba_tool_cmd) == 0) {
899                 warn("Unable to add testallowed user: \n$samba_tool_cmd\n");
900                 return undef;
901         }
902
903         my $ldbmodify = "";
904         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
905         $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
906         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
907         $ldbmodify .=  " --configfile=$ctx->{smb_conf}";
908         my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
909
910         if ($ctx->{server_role} ne "domain controller") {
911                 $base_dn = "DC=$ctx->{netbiosname}";
912         }
913
914         my $user_dn = "cn=$testallowed_account,cn=users,$base_dn";
915         $testallowed_account = "testallowed account";
916         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
917         print LDIF "dn: $user_dn
918 changetype: modify
919 replace: samAccountName
920 samAccountName: $testallowed_account
921 -
922 ";
923         close(LDIF);
924
925         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
926         print LDIF "dn: $user_dn
927 changetype: modify
928 replace: userPrincipalName
929 userPrincipalName: testallowed upn\@$ctx->{realm}
930 replace: servicePrincipalName
931 servicePrincipalName: host/testallowed
932 -           
933 ";
934         close(LDIF);
935
936         $samba_tool_cmd = "";
937         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
938         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
939         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
940             . " user create --configfile=$ctx->{smb_conf} testdenied $ctx->{password}";
941         unless (system($samba_tool_cmd) == 0) {
942                 warn("Unable to add testdenied user: \n$samba_tool_cmd\n");
943                 return undef;
944         }
945
946         $user_dn = "cn=testdenied,cn=users,$base_dn";
947         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
948         print LDIF "dn: $user_dn
949 changetype: modify
950 replace: userPrincipalName
951 userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
952 -           
953 ";
954         close(LDIF);
955
956         $samba_tool_cmd = "";
957         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
958         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
959         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
960             . " user create --configfile=$ctx->{smb_conf} testupnspn $ctx->{password}";
961         unless (system($samba_tool_cmd) == 0) {
962                 warn("Unable to add testupnspn user: \n$samba_tool_cmd\n");
963                 return undef;
964         }
965
966         $user_dn = "cn=testupnspn,cn=users,$base_dn";
967         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
968         print LDIF "dn: $user_dn
969 changetype: modify
970 replace: userPrincipalName
971 userPrincipalName: http/testupnspn.$ctx->{dnsname}\@$ctx->{realm}
972 replace: servicePrincipalName
973 servicePrincipalName: http/testupnspn.$ctx->{dnsname}
974 -
975 ";
976         close(LDIF);
977
978         $samba_tool_cmd = "";
979         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
980         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
981         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
982             . " group addmembers --configfile=$ctx->{smb_conf} 'Allowed RODC Password Replication Group' '$testallowed_account'";
983         unless (system($samba_tool_cmd) == 0) {
984                 warn("Unable to add '$testallowed_account' user to 'Allowed RODC Password Replication Group': \n$samba_tool_cmd\n");
985                 return undef;
986         }
987
988         # Create to users alice and bob!
989         my $user_account_array = ["alice", "bob", "jane", "joe"];
990
991         foreach my $user_account (@{$user_account_array}) {
992                 my $samba_tool_cmd = "";
993
994                 $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
995                 $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
996                 $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
997                     . " user create --configfile=$ctx->{smb_conf} $user_account Secret007";
998                 unless (system($samba_tool_cmd) == 0) {
999                         warn("Unable to create user: $user_account\n$samba_tool_cmd\n");
1000                         return undef;
1001                 }
1002         }
1003
1004         my $group_array = ["Samba Users"];
1005
1006         foreach my $group (@{$group_array}) {
1007                 my $samba_tool_cmd = "";
1008
1009                 $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1010                 $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1011                 $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
1012                     . " group add --configfile=$ctx->{smb_conf} \"$group\"";
1013                 unless (system($samba_tool_cmd) == 0) {
1014                         warn("Unable to create group: $group\n$samba_tool_cmd\n");
1015                         return undef;
1016                 }
1017         }
1018
1019         # Add user joe to group "Samba Users"
1020         $samba_tool_cmd = "";
1021         my $group = "Samba Users";
1022         my $user_account = "joe";
1023
1024         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1025         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1026         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
1027             . " group addmembers --configfile=$ctx->{smb_conf} \"$group\" $user_account";
1028         unless (system($samba_tool_cmd) == 0) {
1029                 warn("Unable to add " . $user_account . "to group group : $group\n$samba_tool_cmd\n");
1030                 return undef;
1031         }
1032
1033         $samba_tool_cmd = "";
1034         $group = "Samba Users";
1035         $user_account = "joe";
1036
1037         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1038         $samba_tool_cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1039         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
1040             . " user setprimarygroup --configfile=$ctx->{smb_conf} $user_account \"$group\"";
1041         unless (system($samba_tool_cmd) == 0) {
1042                 warn("Unable to set primary group of user: $user_account\n$samba_tool_cmd\n");
1043                 return undef;
1044         }
1045
1046         # Change the userPrincipalName for jane
1047         $ldbmodify = "";
1048         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1049         $ldbmodify .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1050         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
1051         $ldbmodify .=  " --configfile=$ctx->{smb_conf}";
1052         $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
1053         $user_dn = "cn=jane,cn=users,$base_dn";
1054
1055         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
1056         print LDIF "dn: $user_dn
1057 changetype: modify
1058 replace: userPrincipalName
1059 userPrincipalName: jane.doe\@$ctx->{realm}
1060 -
1061 ";
1062         close(LDIF);
1063
1064         return $ret;
1065 }
1066
1067 sub provision($$$$$$$$$$)
1068 {
1069         my ($self, $prefix, $server_role, $hostname,
1070             $domain, $realm, $functional_level,
1071             $password, $kdc_ipv4, $kdc_ipv6, $extra_smbconf_options, $extra_smbconf_shares,
1072             $extra_provision_options) = @_;
1073
1074         my $samsid = Samba::random_domain_sid();
1075
1076         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
1077                                                $hostname,
1078                                                $domain, $realm,
1079                                                $samsid,
1080                                                $functional_level,
1081                                                $password, $kdc_ipv4, $kdc_ipv6);
1082
1083         if (defined($extra_provision_options)) {
1084                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
1085         }
1086
1087         $ctx->{share} = "$ctx->{prefix_abs}/share";
1088         push(@{$ctx->{directories}}, "$ctx->{share}");
1089         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
1090         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
1091
1092         # precreate directories for printer drivers
1093         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
1094         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
1095         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
1096
1097         my $msdfs = "no";
1098         $msdfs = "yes" if ($server_role eq "domain controller");
1099         $ctx->{smb_conf_extra_options} = "
1100
1101         max xmit = 32K
1102         server max protocol = SMB2
1103         host msdfs = $msdfs
1104         lanman auth = yes
1105
1106         # fruit:copyfile is a global option
1107         fruit:copyfile = yes
1108
1109         $extra_smbconf_options
1110
1111 [tmp]
1112         path = $ctx->{share}
1113         read only = no
1114         posix:sharedelay = 100000
1115         posix:oplocktimeout = 3
1116         posix:writetimeupdatedelay = 500000
1117
1118 [xcopy_share]
1119         path = $ctx->{share}
1120         read only = no
1121         posix:sharedelay = 100000
1122         posix:oplocktimeout = 3
1123         posix:writetimeupdatedelay = 500000
1124         create mask = 777
1125         force create mode = 777
1126
1127 [posix_share]
1128         path = $ctx->{share}
1129         read only = no
1130         create mask = 0777
1131         force create mode = 0
1132         directory mask = 0777
1133         force directory mode = 0
1134
1135 [test1]
1136         path = $ctx->{share}/test1
1137         read only = no
1138         posix:sharedelay = 100000
1139         posix:oplocktimeout = 3
1140         posix:writetimeupdatedelay = 500000
1141
1142 [test2]
1143         path = $ctx->{share}/test2
1144         read only = no
1145         posix:sharedelay = 100000
1146         posix:oplocktimeout = 3
1147         posix:writetimeupdatedelay = 500000
1148
1149 [cifs]
1150         path = $ctx->{share}/_ignore_cifs_
1151         read only = no
1152         ntvfs handler = cifs
1153         cifs:server = $ctx->{netbiosname}
1154         cifs:share = tmp
1155         cifs:use-s4u2proxy = yes
1156         # There is no username specified here, instead the client is expected
1157         # to log in with kerberos, and the serverwill use delegated credentials.
1158         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1159
1160 [simple]
1161         path = $ctx->{share}
1162         read only = no
1163         ntvfs handler = simple
1164
1165 [sysvol]
1166         path = $ctx->{statedir}/sysvol
1167         read only = no
1168
1169 [netlogon]
1170         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1171         read only = no
1172
1173 [cifsposix]
1174         copy = simple
1175         ntvfs handler = cifsposix
1176
1177 [vfs_fruit]
1178         path = $ctx->{share}
1179         vfs objects = catia fruit streams_xattr acl_xattr
1180         ea support = yes
1181         fruit:resource = file
1182         fruit:metadata = netatalk
1183         fruit:locking = netatalk
1184         fruit:encoding = native
1185
1186 [xattr]
1187         path = $ctx->{share}
1188         # This can be used for testing real fs xattr stuff
1189         vfs objects = streams_xattr acl_xattr
1190
1191 $extra_smbconf_shares
1192 ";
1193
1194         my $ret = $self->provision_raw_step1($ctx);
1195         unless (defined $ret) {
1196                 return undef;
1197         }
1198
1199         return $self->provision_raw_step2($ctx, $ret);
1200 }
1201
1202 # For multi-DC testenvs, we want $DC_SERVER to always be the PDC (i.e. the
1203 # original DC) in the testenv. $SERVER is always the joined DC that we are
1204 # actually running the test against
1205 sub set_pdc_env_vars
1206 {
1207         my ($self, $env, $dcvars) = @_;
1208
1209         $env->{DC_SERVER} = $dcvars->{DC_SERVER};
1210         $env->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1211         $env->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1212         $env->{DC_SERVERCONFFILE} = $dcvars->{SERVERCONFFILE};
1213         $env->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1214         $env->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1215         $env->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1216 }
1217
1218 sub provision_s4member($$$$$)
1219 {
1220         my ($self, $prefix, $dcvars, $hostname, $more_conf) = @_;
1221         print "PROVISIONING MEMBER...\n";
1222         my $extra_smb_conf = "
1223         passdb backend = samba_dsdb
1224 winbindd:use external pipes = true
1225
1226 # the source4 smb server doesn't allow signing by default
1227 server signing = enabled
1228 raw NTLMv2 auth = yes
1229
1230 rpc_server:default = external
1231 rpc_server:svcctl = embedded
1232 rpc_server:srvsvc = embedded
1233 rpc_server:eventlog = embedded
1234 rpc_server:ntsvcs = embedded
1235 rpc_server:winreg = embedded
1236 rpc_server:spoolss = embedded
1237 rpc_daemon:spoolssd = embedded
1238 rpc_server:tcpip = no
1239 ";
1240         if ($more_conf) {
1241                 $extra_smb_conf = $extra_smb_conf . $more_conf . "\n";
1242         }
1243         my $extra_provision_options = ["--use-ntvfs"];
1244         my $ret = $self->provision($prefix,
1245                                    "member server",
1246                                    $hostname,
1247                                    $dcvars->{DOMAIN},
1248                                    $dcvars->{REALM},
1249                                    "2008",
1250                                    "locMEMpass3",
1251                                    $dcvars->{SERVER_IP},
1252                                    $dcvars->{SERVER_IPV6},
1253                                    $extra_smb_conf, "",
1254                                    $extra_provision_options);
1255         unless ($ret) {
1256                 return undef;
1257         }
1258
1259         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1260         my $cmd = $self->get_cmd_env_vars($ret);
1261         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1262         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1263         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1264
1265         unless (system($cmd) == 0) {
1266                 warn("Join failed\n$cmd");
1267                 return undef;
1268         }
1269
1270         $ret->{DOMSID} = $dcvars->{DOMSID};
1271         $self->set_pdc_env_vars($ret, $dcvars);
1272
1273         return $ret;
1274 }
1275
1276 sub provision_rpc_proxy($$$)
1277 {
1278         my ($self, $prefix, $dcvars) = @_;
1279         print "PROVISIONING RPC PROXY...\n";
1280
1281         my $extra_smbconf_options = "
1282         passdb backend = samba_dsdb
1283
1284         # rpc_proxy
1285         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
1286         dcerpc endpoint servers = epmapper, remote
1287         dcerpc_remote:interfaces = rpcecho
1288         dcerpc_remote:allow_anonymous_fallback = yes
1289
1290 [cifs_to_dc]
1291         path = /tmp/_ignore_cifs_to_dc_/_none_
1292         read only = no
1293         ntvfs handler = cifs
1294         cifs:server = $dcvars->{SERVER}
1295         cifs:share = cifs
1296         cifs:use-s4u2proxy = yes
1297         # There is no username specified here, instead the client is expected
1298         # to log in with kerberos, and the serverwill use delegated credentials.
1299         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1300
1301 ";
1302
1303         my $extra_provision_options = ["--use-ntvfs"];
1304         my $ret = $self->provision($prefix,
1305                                    "member server",
1306                                    "localrpcproxy",
1307                                    $dcvars->{DOMAIN},
1308                                    $dcvars->{REALM},
1309                                    "2008",
1310                                    "locRPCproxypass4",
1311                                    $dcvars->{SERVER_IP},
1312                                    $dcvars->{SERVER_IPV6},
1313                                    $extra_smbconf_options, "",
1314                                    $extra_provision_options);
1315         unless ($ret) {
1316                 return undef;
1317         }
1318
1319         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1320
1321         # The joind runs in the context of the rpc_proxy/member for now
1322         my $cmd = $self->get_cmd_env_vars($ret);
1323         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1324         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1325         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1326
1327         unless (system($cmd) == 0) {
1328                 warn("Join failed\n$cmd");
1329                 return undef;
1330         }
1331
1332         # Setting up delegation runs in the context of the DC for now
1333         $cmd = "";
1334         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1335         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1336         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1337         $cmd .= "RESOLV_CONF=\"$dcvars->{RESOLV_CONF}\" ";
1338         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
1339         $cmd .= " $dcvars->{CONFIGURATION}";
1340         print $cmd;
1341
1342         unless (system($cmd) == 0) {
1343                 warn("Delegation failed\n$cmd");
1344                 return undef;
1345         }
1346
1347         # Setting up delegation runs in the context of the DC for now
1348         $cmd = "";
1349         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1350         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1351         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1352         $cmd .= "RESOLV_CONF=\"$dcvars->{RESOLV_CONF}\" ";
1353         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
1354         $cmd .= " $dcvars->{CONFIGURATION}";
1355
1356         unless (system($cmd) == 0) {
1357                 warn("Delegation failed\n$cmd");
1358                 return undef;
1359         }
1360
1361         $ret->{DOMSID} = $dcvars->{DOMSID};
1362         $self->set_pdc_env_vars($ret, $dcvars);
1363
1364         return $ret;
1365 }
1366
1367 sub provision_promoted_dc($$$)
1368 {
1369         my ($self, $prefix, $dcvars) = @_;
1370         print "PROVISIONING PROMOTED DC...\n";
1371
1372         # We do this so that we don't run the provision.  That's the job of 'samba-tool domain dcpromo'.
1373         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1374                                                "promotedvdc",
1375                                                $dcvars->{DOMAIN},
1376                                                $dcvars->{REALM},
1377                                                $dcvars->{SAMSID},
1378                                                "2008",
1379                                                $dcvars->{PASSWORD},
1380                                                $dcvars->{SERVER_IP},
1381                                                $dcvars->{SERVER_IPV6});
1382
1383         $ctx->{smb_conf_extra_options} = "
1384         max xmit = 32K
1385         server max protocol = SMB2
1386
1387         ntlm auth = ntlmv2-only
1388
1389 [sysvol]
1390         path = $ctx->{statedir}/sysvol
1391         read only = yes
1392
1393 [netlogon]
1394         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1395         read only = no
1396
1397 ";
1398
1399         my $ret = $self->provision_raw_step1($ctx);
1400         unless ($ret) {
1401                 return undef;
1402         }
1403
1404         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1405         my $cmd = $self->get_cmd_env_vars($ret);
1406         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
1407         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1408         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1409
1410         unless (system($cmd) == 0) {
1411                 warn("Join failed\n$cmd");
1412                 return undef;
1413         }
1414
1415         $samba_tool =  Samba::bindir_path($self, "samba-tool");
1416         $cmd = $self->get_cmd_env_vars($ret);
1417         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1418         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1419         $cmd .= " --machinepass=machine$ret->{PASSWORD} --dns-backend=BIND9_DLZ";
1420
1421         unless (system($cmd) == 0) {
1422                 warn("Join failed\n$cmd");
1423                 return undef;
1424         }
1425
1426         $self->set_pdc_env_vars($ret, $dcvars);
1427
1428         return $ret;
1429 }
1430
1431 sub provision_vampire_dc($$$)
1432 {
1433         my ($self, $prefix, $dcvars, $fl) = @_;
1434         print "PROVISIONING VAMPIRE DC @ FL $fl...\n";
1435         my $name = "localvampiredc";
1436         my $extra_conf = "";
1437
1438         if ($fl == "2000") {
1439                 $name = "vampire2000dc";
1440         } else {
1441                 $extra_conf = "drs: immediate link sync = yes
1442                        drs: max link sync = 250";
1443         }
1444
1445         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1446         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1447                                                $name,
1448                                                $dcvars->{DOMAIN},
1449                                                $dcvars->{REALM},
1450                                                $dcvars->{DOMSID},
1451                                                $fl,
1452                                                $dcvars->{PASSWORD},
1453                                                $dcvars->{SERVER_IP},
1454                                                $dcvars->{SERVER_IPV6});
1455
1456         $ctx->{smb_conf_extra_options} = "
1457         max xmit = 32K
1458         server max protocol = SMB2
1459
1460         ntlm auth = mschapv2-and-ntlmv2-only
1461         $extra_conf
1462
1463 [sysvol]
1464         path = $ctx->{statedir}/sysvol
1465         read only = yes
1466
1467 [netlogon]
1468         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1469         read only = no
1470
1471 ";
1472
1473         my $ret = $self->provision_raw_step1($ctx);
1474         unless ($ret) {
1475                 return undef;
1476         }
1477
1478         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1479         my $cmd = $self->get_cmd_env_vars($ret);
1480         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1481         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1482         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1483         $cmd .= " --backend-store=mdb";
1484
1485         unless (system($cmd) == 0) {
1486                 warn("Join failed\n$cmd");
1487                 return undef;
1488         }
1489
1490         $self->set_pdc_env_vars($ret, $dcvars);
1491         $ret->{DC_REALM} = $dcvars->{DC_REALM};
1492
1493         return $ret;
1494 }
1495
1496 sub provision_ad_dc_ntvfs($$$)
1497 {
1498         my ($self, $prefix, $extra_provision_options) = @_;
1499
1500         # We keep the old 'winbind' name here in server services to
1501         # ensure upgrades which used that name still work with the now
1502         # alias.
1503
1504         print "PROVISIONING AD DC (NTVFS)...\n";
1505         my $extra_conf_options = "netbios aliases = localDC1-a
1506         server services = +winbind -winbindd
1507         ldap server require strong auth = allow_sasl_over_tls
1508         allow nt4 crypto = yes
1509         raw NTLMv2 auth = yes
1510         lsa over netlogon = yes
1511         rpc server port = 1027
1512         auth event notification = true
1513         dsdb event notification = true
1514         dsdb password event notification = true
1515         dsdb group change notification = true
1516         server schannel = auto
1517         ";
1518         push (@{$extra_provision_options}, "--use-ntvfs");
1519         my $ret = $self->provision($prefix,
1520                                    "domain controller",
1521                                    "localdc",
1522                                    "SAMBADOMAIN",
1523                                    "samba.example.com",
1524                                    "2008",
1525                                    "locDCpass1",
1526                                    undef,
1527                                    undef,
1528                                    $extra_conf_options,
1529                                    "",
1530                                    $extra_provision_options);
1531         unless ($ret) {
1532                 return undef;
1533         }
1534
1535         unless($self->add_wins_config("$prefix/private")) {
1536                 warn("Unable to add wins configuration");
1537                 return undef;
1538         }
1539         $ret->{NETBIOSALIAS} = "localdc1-a";
1540         $ret->{DC_REALM} = $ret->{REALM};
1541
1542         return $ret;
1543 }
1544
1545 sub provision_fl2000dc($$)
1546 {
1547         my ($self, $prefix) = @_;
1548
1549         print "PROVISIONING DC WITH FOREST LEVEL 2000...\n";
1550         my $extra_conf_options = "
1551         spnego:simulate_w2k=yes
1552         ntlmssp_server:force_old_spnego=yes
1553 ";
1554         my $extra_provision_options = ["--base-schema=2008_R2"];
1555         # This environment uses plain text secrets
1556         # i.e. secret attributes are not encrypted on disk.
1557         # This allows testing of the --plaintext-secrets option for
1558         # provision
1559         push (@{$extra_provision_options}, "--plaintext-secrets");
1560         my $ret = $self->provision($prefix,
1561                                    "domain controller",
1562                                    "dc5",
1563                                    "SAMBA2000",
1564                                    "samba2000.example.com",
1565                                    "2000",
1566                                    "locDCpass5",
1567                                    undef,
1568                                    undef,
1569                                    $extra_conf_options,
1570                                    "",
1571                                    $extra_provision_options);
1572         unless ($ret) {
1573                 return undef;
1574         }
1575
1576         unless($self->add_wins_config("$prefix/private")) {
1577                 warn("Unable to add wins configuration");
1578                 return undef;
1579         }
1580         $ret->{DC_REALM} = $ret->{REALM};
1581
1582         return $ret;
1583 }
1584
1585 sub provision_fl2003dc($$$)
1586 {
1587         my ($self, $prefix, $dcvars) = @_;
1588         my $ip_addr1 = Samba::get_ipv4_addr("fakednsforwarder1");
1589         my $ip_addr2 = Samba::get_ipv4_addr("fakednsforwarder2");
1590
1591         print "PROVISIONING DC WITH FOREST LEVEL 2003...\n";
1592         my $extra_conf_options = "allow dns updates = nonsecure and secure
1593         dcesrv:header signing = no
1594         dcesrv:max auth states = 0
1595         dns forwarder = $ip_addr1 $ip_addr2";
1596         my $extra_provision_options = ["--base-schema=2008_R2"];
1597         my $ret = $self->provision($prefix,
1598                                    "domain controller",
1599                                    "dc6",
1600                                    "SAMBA2003",
1601                                    "samba2003.example.com",
1602                                    "2003",
1603                                    "locDCpass6",
1604                                    undef,
1605                                    undef,
1606                                    $extra_conf_options,
1607                                    "",
1608                                    $extra_provision_options);
1609         unless (defined $ret) {
1610                 return undef;
1611         }
1612
1613         $ret->{DNS_FORWARDER1} = $ip_addr1;
1614         $ret->{DNS_FORWARDER2} = $ip_addr2;
1615
1616         my @samba_tool_options;
1617         push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
1618         push (@samba_tool_options, "domain");
1619         push (@samba_tool_options, "passwordsettings");
1620         push (@samba_tool_options, "set");
1621         push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
1622         push (@samba_tool_options, "--min-pwd-age=0");
1623         push (@samba_tool_options, "--history-length=1");
1624
1625         my $samba_tool_cmd = join(" ", @samba_tool_options);
1626
1627         unless (system($samba_tool_cmd) == 0) {
1628                 warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
1629                 return undef;
1630         }
1631
1632         unless($self->add_wins_config("$prefix/private")) {
1633                 warn("Unable to add wins configuration");
1634                 return undef;
1635         }
1636
1637         return $ret;
1638 }
1639
1640 sub provision_fl2008r2dc($$$)
1641 {
1642         my ($self, $prefix, $dcvars) = @_;
1643
1644         print "PROVISIONING DC WITH FOREST LEVEL 2008r2...\n";
1645         my $extra_conf_options = "ldap server require strong auth = no";
1646         my $extra_provision_options = ["--base-schema=2008_R2"];
1647         my $ret = $self->provision($prefix,
1648                                    "domain controller",
1649                                    "dc7",
1650                                    "SAMBA2008R2",
1651                                    "samba2008R2.example.com",
1652                                    "2008_R2",
1653                                    "locDCpass7",
1654                                    undef,
1655                                    undef,
1656                                    $extra_conf_options,
1657                                    "",
1658                                    $extra_provision_options);
1659         unless (defined $ret) {
1660                 return undef;
1661         }
1662
1663         unless ($self->add_wins_config("$prefix/private")) {
1664                 warn("Unable to add wins configuration");
1665                 return undef;
1666         }
1667         $ret->{DC_REALM} = $ret->{REALM};
1668
1669         return $ret;
1670 }
1671
1672
1673 sub provision_rodc($$$)
1674 {
1675         my ($self, $prefix, $dcvars) = @_;
1676         print "PROVISIONING RODC...\n";
1677
1678         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1679         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1680                                                "rodc",
1681                                                $dcvars->{DOMAIN},
1682                                                $dcvars->{REALM},
1683                                                $dcvars->{DOMSID},
1684                                                "2008",
1685                                                $dcvars->{PASSWORD},
1686                                                $dcvars->{SERVER_IP},
1687                                                $dcvars->{SERVER_IPV6});
1688         unless ($ctx) {
1689                 return undef;
1690         }
1691
1692         $ctx->{share} = "$ctx->{prefix_abs}/share";
1693         push(@{$ctx->{directories}}, "$ctx->{share}");
1694
1695         $ctx->{smb_conf_extra_options} = "
1696         max xmit = 32K
1697         server max protocol = SMB2
1698         password server = $dcvars->{DC_SERVER}
1699
1700 [sysvol]
1701         path = $ctx->{statedir}/sysvol
1702         read only = yes
1703
1704 [netlogon]
1705         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1706         read only = yes
1707
1708 [tmp]
1709         path = $ctx->{share}
1710         read only = no
1711         posix:sharedelay = 10000
1712         posix:oplocktimeout = 3
1713         posix:writetimeupdatedelay = 50000
1714
1715 ";
1716
1717         my $ret = $self->provision_raw_step1($ctx);
1718         unless ($ret) {
1719                 return undef;
1720         }
1721
1722         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1723         my $cmd = $self->get_cmd_env_vars($ret);
1724         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1725         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1726         $cmd .= " --server=$dcvars->{DC_SERVER}";
1727
1728         unless (system($cmd) == 0) {
1729                 warn("RODC join failed\n$cmd");
1730                 return undef;
1731         }
1732
1733         # This ensures deterministic behaviour for tests that want to have the 'testallowed account'
1734         # user password verified on the RODC
1735         my $testallowed_account = "testallowed account";
1736         $cmd = "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1737         $cmd .= "KRB5CCNAME=\"$ret->{KRB5_CCACHE}\" ";
1738         $cmd .= "RESOLV_CONF=\"$ret->{RESOLV_CONF}\" ";
1739         $cmd .= "$samba_tool rodc preload '$testallowed_account' $ret->{CONFIGURATION}";
1740         $cmd .= " --server=$dcvars->{DC_SERVER}";
1741
1742         unless (system($cmd) == 0) {
1743                 warn("RODC join failed\n$cmd");
1744                 return undef;
1745         }
1746
1747         # we overwrite the kdc after the RODC join
1748         # so that use the RODC as kdc and test
1749         # the proxy code
1750         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1751         $ctx->{kdc_ipv6} = $ret->{SERVER_IPV6};
1752         Samba::mk_krb5_conf($ctx);
1753         Samba::mk_mitkdc_conf($ctx, abs_path(Samba::bindir_path($self, "shared")));
1754
1755         $self->set_pdc_env_vars($ret, $dcvars);
1756
1757         return $ret;
1758 }
1759
1760 sub read_config_h($)
1761 {
1762         my ($name) = @_;
1763         my %ret;
1764         open(LF, "<$name") or die("unable to read $name: $!");
1765         while (<LF>) {
1766                 chomp;
1767                 next if not (/^#define /);
1768                 if (/^#define (.*?)[ \t]+(.*?)$/) {
1769                         $ret{$1} = $2;
1770                         next;
1771                 }
1772                 if (/^#define (.*?)[ \t]+$/) {
1773                         $ret{$1} = 1;;
1774                         next;
1775                 }
1776         }
1777         close(LF);
1778         return \%ret;
1779 }
1780
1781 sub provision_ad_dc($$$$$$)
1782 {
1783         my ($self, $prefix, $hostname, $domain, $realm, $smbconf_args,
1784                 $extra_provision_options) = @_;
1785
1786         my $prefix_abs = abs_path($prefix);
1787
1788         my $bindir_abs = abs_path($self->{bindir});
1789         my $lockdir="$prefix_abs/lockdir";
1790         my $conffile="$prefix_abs/etc/smb.conf";
1791
1792         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
1793         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
1794
1795         my $config_h = {};
1796
1797         if (defined($ENV{CONFIG_H})) {
1798                 $config_h = read_config_h($ENV{CONFIG_H});
1799         }
1800
1801         my $password_hash_gpg_key_ids = "password hash gpg key ids = 4952E40301FAB41A";
1802         $password_hash_gpg_key_ids = "" unless defined($config_h->{HAVE_GPGME});
1803
1804         my $extra_smbconf_options = "
1805         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
1806
1807         dbwrap_tdb_mutexes:* = yes
1808         ${require_mutexes}
1809
1810         ${password_hash_gpg_key_ids}
1811
1812         kernel oplocks = no
1813         kernel change notify = no
1814         smb2 leases = no
1815
1816         logging = file
1817         printing = bsd
1818         printcap name = /dev/null
1819
1820         max protocol = SMB3
1821         read only = no
1822
1823         smbd:sharedelay = 100000
1824         smbd:writetimeupdatedelay = 500000
1825         create mask = 755
1826         dos filemode = yes
1827         check parent directory delete on close = yes
1828
1829         dcerpc endpoint servers = -winreg -srvsvc
1830
1831         printcap name = /dev/null
1832
1833         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
1834         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
1835
1836         printing = vlp
1837         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1838         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1839         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1840         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1841         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1842         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1843         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1844         lpq cache time = 0
1845         print notify backchannel = yes
1846
1847         server schannel = auto
1848         auth event notification = true
1849         dsdb event notification = true
1850         dsdb password event notification = true
1851         dsdb group change notification = true
1852         $smbconf_args
1853 ";
1854
1855         my $extra_smbconf_shares = "
1856
1857 [tmpenc]
1858         copy = tmp
1859         smb encrypt = required
1860
1861 [tmpcase]
1862         copy = tmp
1863         case sensitive = yes
1864
1865 [tmpguest]
1866         copy = tmp
1867         guest ok = yes
1868
1869 [hideunread]
1870         copy = tmp
1871         hide unreadable = yes
1872
1873 [durable]
1874         copy = tmp
1875         kernel share modes = no
1876         kernel oplocks = no
1877         posix locking = no
1878
1879 [print\$]
1880         copy = tmp
1881
1882 [print1]
1883         copy = tmp
1884         printable = yes
1885
1886 [print2]
1887         copy = print1
1888 [print3]
1889         copy = print1
1890 [print4]
1891         copy = print1
1892         guest ok = yes
1893 [lp]
1894         copy = print1
1895 ";
1896
1897         push (@{$extra_provision_options}, "--backend-store=mdb");
1898         print "PROVISIONING AD DC...\n";
1899         my $ret = $self->provision($prefix,
1900                                    "domain controller",
1901                                    $hostname,
1902                                    $domain,
1903                                    $realm,
1904                                    "2008",
1905                                    "locDCpass1",
1906                                    undef,
1907                                    undef,
1908                                    $extra_smbconf_options,
1909                                    $extra_smbconf_shares,
1910                                    $extra_provision_options);
1911         unless (defined $ret) {
1912                 return undef;
1913         }
1914
1915         unless($self->add_wins_config("$prefix/private")) {
1916                 warn("Unable to add wins configuration");
1917                 return undef;
1918         }
1919
1920         return $ret;
1921 }
1922
1923 sub provision_chgdcpass($$)
1924 {
1925         my ($self, $prefix) = @_;
1926
1927         print "PROVISIONING CHGDCPASS...\n";
1928         # This environment disallows the use of this password
1929         # (and also removes the default AD complexity checks)
1930         my $unacceptable_password = "Paßßword-widk3Dsle32jxdBdskldsk55klASKQ";
1931         my $extra_smb_conf = "
1932         check password script = $self->{srcdir}/selftest/checkpassword_arg1.sh ${unacceptable_password}
1933         allow dcerpc auth level connect:lsarpc = yes
1934         dcesrv:max auth states = 8
1935 ";
1936         my $extra_provision_options = ["--dns-backend=BIND9_DLZ"];
1937         my $ret = $self->provision($prefix,
1938                                    "domain controller",
1939                                    "chgdcpass",
1940                                    "CHDCDOMAIN",
1941                                    "chgdcpassword.samba.example.com",
1942                                    "2008",
1943                                    "chgDCpass1",
1944                                    undef,
1945                                    undef,
1946                                    $extra_smb_conf,
1947                                    "",
1948                                    $extra_provision_options);
1949         unless (defined $ret) {
1950                 return undef;
1951         }
1952
1953         unless($self->add_wins_config("$prefix/private")) {
1954                 warn("Unable to add wins configuration");
1955                 return undef;
1956         }
1957         
1958         # Remove secrets.tdb from this environment to test that we
1959         # still start up on systems without the new matching
1960         # secrets.tdb records.
1961         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
1962                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
1963                 return undef;
1964         }
1965
1966         $ret->{UNACCEPTABLE_PASSWORD} = $unacceptable_password;
1967
1968         return $ret;
1969 }
1970
1971 sub teardown_env_terminate($$)
1972 {
1973         my ($self, $envvars) = @_;
1974         my $pid;
1975
1976         # This should cause samba to terminate gracefully
1977         my $smbcontrol = Samba::bindir_path($self, "smbcontrol");
1978         my $cmd = "";
1979         $cmd .= "$smbcontrol samba shutdown $envvars->{CONFIGURATION}";
1980         my $ret = system($cmd);
1981         if ($ret != 0) {
1982                 warn "'$cmd' failed with '$ret'\n";
1983         }
1984
1985         # This should cause samba to terminate gracefully
1986         close($envvars->{STDIN_PIPE});
1987
1988         $pid = $envvars->{SAMBA_PID};
1989         my $count = 0;
1990         my $childpid;
1991
1992         # This should give it time to write out the gcov data
1993         until ($count > 15) {
1994             if (Samba::cleanup_child($pid, "samba") != 0) {
1995                 return;
1996             }
1997             sleep(1);
1998             $count++;
1999         }
2000
2001         # After 15 Seconds, work out why this thing is still alive
2002         warn "server process $pid took more than $count seconds to exit, showing backtrace:\n";
2003         system("$self->{srcdir}/selftest/gdb_backtrace $pid");
2004
2005         until ($count > 30) {
2006             if (Samba::cleanup_child($pid, "samba") != 0) {
2007                 return;
2008             }
2009             sleep(1);
2010             $count++;
2011         }
2012
2013         if (kill(0, $pid)) {
2014             warn "server process $pid took more than $count seconds to exit, sending SIGTERM\n";
2015             kill "TERM", $pid;
2016         }
2017
2018         until ($count > 40) {
2019             if (Samba::cleanup_child($pid, "samba") != 0) {
2020                 return;
2021             }
2022             sleep(1);
2023             $count++;
2024         }
2025         # If it is still around, kill it
2026         if (kill(0, $pid)) {
2027             warn "server process $pid took more than $count seconds to exit, killing\n with SIGKILL\n";
2028             kill 9, $pid;
2029         }
2030         return;
2031 }
2032
2033 sub teardown_env($$)
2034 {
2035         my ($self, $envvars) = @_;
2036         teardown_env_terminate($self, $envvars);
2037
2038         print $self->getlog_env($envvars);
2039
2040         return;
2041 }
2042
2043 sub getlog_env($$)
2044 {
2045         my ($self, $envvars) = @_;
2046         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME} pid $envvars->{SAMBA_PID}\n";
2047         my $out = $title;
2048
2049         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
2050
2051         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
2052         while (<LOG>) {
2053                 $out .= $_;
2054         }
2055         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
2056         close(LOG);
2057
2058         return "" if $out eq $title;
2059
2060         return $out;
2061 }
2062
2063 sub check_env($$)
2064 {
2065         my ($self, $envvars) = @_;
2066         my $samba_pid = $envvars->{SAMBA_PID};
2067
2068         if (not defined($samba_pid)) {
2069             return 0;
2070         } elsif ($samba_pid > 0) {
2071             my $childpid = Samba::cleanup_child($samba_pid, "samba");
2072
2073             if ($childpid == 0) {
2074                 return 1;
2075             }
2076             return 0;
2077         } else {
2078             return 1;
2079         }
2080 }
2081
2082 # Declare the environments Samba4 makes available.
2083 # To be set up, they will be called as
2084 #   samba4->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
2085 # The interdependencies between the testenvs are declared below. Some testenvs
2086 # are dependent on another testenv running first, e.g. vampire_dc is dependent
2087 # on ad_dc_ntvfs because vampire_dc joins ad_dc_ntvfs's domain. All DCs are
2088 # dependent on dns_hub, which handles resolving DNS queries for the realm.
2089 %Samba4::ENV_DEPS = (
2090         # name               => [dep_1, dep_2, ...],
2091         dns_hub              => [],
2092         ad_dc_ntvfs          => ["dns_hub"],
2093         ad_dc                => ["dns_hub"],
2094         ad_dc_no_nss         => ["dns_hub"],
2095         ad_dc_no_ntlm        => ["dns_hub"],
2096
2097         fl2008r2dc           => ["ad_dc"],
2098         fl2003dc             => ["ad_dc"],
2099         fl2000dc             => ["dns_hub"],
2100
2101         vampire_2000_dc      => ["fl2000dc"],
2102         vampire_dc           => ["ad_dc_ntvfs"],
2103         promoted_dc          => ["ad_dc_ntvfs"],
2104
2105         rodc                 => ["ad_dc_ntvfs"],
2106         rpc_proxy            => ["ad_dc_ntvfs"],
2107         chgdcpass            => ["dns_hub"],
2108
2109         s4member_dflt_domain => ["ad_dc_ntvfs"],
2110         s4member             => ["ad_dc_ntvfs"],
2111
2112         # envs that test the server process model
2113         proclimitdc          => ["dns_hub"],
2114         preforkrestartdc     => ["dns_hub"],
2115
2116         # backup/restore testenvs
2117         backupfromdc         => ["dns_hub"],
2118         customdc             => ["dns_hub"],
2119         restoredc            => ["backupfromdc"],
2120         renamedc             => ["backupfromdc"],
2121         offlinebackupdc      => ["backupfromdc"],
2122         labdc                => ["backupfromdc"],
2123
2124         # aliases in order to split autbuild tasks
2125         fl2008dc             => ["ad_dc"],
2126         ad_dc_default        => ["ad_dc"],
2127         ad_dc_slowtests      => ["ad_dc"],
2128         ad_dc_backup         => ["ad_dc"],
2129
2130         schema_dc      => ["dns_hub"],
2131         schema_pair_dc => ["schema_dc"],
2132
2133         none                 => [],
2134 );
2135
2136 %Samba4::ENV_DEPS_POST = (
2137         schema_dc => ["schema_pair_dc"],
2138 );
2139
2140 sub return_alias_env
2141 {
2142         my ($self, $path, $env) = @_;
2143
2144         # just an alias
2145         return $env;
2146 }
2147
2148 sub setup_fl2008dc
2149 {
2150         my ($self, $path) = @_;
2151
2152         my $extra_args = ["--base-schema=2008_R2"];
2153         my $env = $self->provision_ad_dc_ntvfs($path, $extra_args);
2154         if (defined $env) {
2155                 if (not defined($self->check_or_start($env, "standard"))) {
2156                     warn("Failed to start fl2008dc");
2157                         return undef;
2158                 }
2159         }
2160         return $env;
2161 }
2162
2163 sub setup_ad_dc_default
2164 {
2165         my ($self, $path, $dep_env) = @_;
2166         return $self->return_alias_env($path, $dep_env)
2167 }
2168
2169 sub setup_ad_dc_slowtests
2170 {
2171         my ($self, $path, $dep_env) = @_;
2172         return $self->return_alias_env($path, $dep_env)
2173 }
2174
2175 sub setup_ad_dc_backup
2176 {
2177         my ($self, $path, $dep_env) = @_;
2178         return $self->return_alias_env($path, $dep_env)
2179 }
2180
2181 sub setup_s4member
2182 {
2183         my ($self, $path, $dc_vars) = @_;
2184
2185         my $env = $self->provision_s4member($path, $dc_vars, "s4member");
2186
2187         if (defined $env) {
2188                 if (not defined($self->check_or_start($env, "standard"))) {
2189                         return undef;
2190                 }
2191         }
2192
2193         return $env;
2194 }
2195
2196 sub setup_s4member_dflt_domain
2197 {
2198         my ($self, $path, $dc_vars) = @_;
2199
2200         my $env = $self->provision_s4member($path, $dc_vars, "s4member_dflt",
2201                                             "winbind use default domain = yes");
2202
2203         if (defined $env) {
2204                 if (not defined($self->check_or_start($env, "standard"))) {
2205                         return undef;
2206                 }
2207         }
2208
2209         return $env;
2210 }
2211
2212 sub setup_rpc_proxy
2213 {
2214         my ($self, $path, $dc_vars) = @_;
2215
2216         my $env = $self->provision_rpc_proxy($path, $dc_vars);
2217
2218         if (defined $env) {
2219                 if (not defined($self->check_or_start($env, "standard"))) {
2220                         return undef;
2221                 }
2222         }
2223         return $env;
2224 }
2225
2226 sub setup_ad_dc_ntvfs
2227 {
2228         my ($self, $path) = @_;
2229
2230         my $env = $self->provision_ad_dc_ntvfs($path, undef);
2231         if (defined $env) {
2232                 if (not defined($self->check_or_start($env, "standard"))) {
2233                     warn("Failed to start ad_dc_ntvfs");
2234                         return undef;
2235                 }
2236         }
2237         return $env;
2238 }
2239
2240 sub setup_chgdcpass
2241 {
2242         my ($self, $path) = @_;
2243
2244         my $env = $self->provision_chgdcpass($path);
2245         if (defined $env) {
2246                 if (not defined($self->check_or_start($env, "standard"))) {
2247                         return undef;
2248                 }
2249         }
2250         return $env;
2251 }
2252
2253 sub setup_fl2000dc
2254 {
2255         my ($self, $path) = @_;
2256
2257         my $env = $self->provision_fl2000dc($path);
2258         if (defined $env) {
2259                 if (not defined($self->check_or_start($env, "standard"))) {
2260                         return undef;
2261                 }
2262         }
2263
2264         return $env;
2265 }
2266
2267 sub setup_fl2003dc
2268 {
2269         my ($self, $path, $dc_vars) = @_;
2270
2271         my $env = $self->provision_fl2003dc($path);
2272
2273         if (defined $env) {
2274                 if (not defined($self->check_or_start($env, "standard"))) {
2275                         return undef;
2276                 }
2277
2278                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2279         }
2280         return $env;
2281 }
2282
2283 sub setup_fl2008r2dc
2284 {
2285         my ($self, $path, $dc_vars) = @_;
2286
2287         my $env = $self->provision_fl2008r2dc($path);
2288
2289         if (defined $env) {
2290                 if (not defined($self->check_or_start($env, "standard"))) {
2291                         return undef;
2292                 }
2293
2294                 my $upn_array = ["$env->{REALM}.upn"];
2295                 my $spn_array = ["$env->{REALM}.spn"];
2296
2297                 $self->setup_namespaces($env, $upn_array, $spn_array);
2298
2299                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2300         }
2301
2302         return $env;
2303 }
2304
2305 sub setup_vampire_dc
2306 {
2307         return setup_generic_vampire_dc(@_, "2008");
2308 }
2309
2310 sub setup_vampire_2000_dc
2311 {
2312         return setup_generic_vampire_dc(@_, "2000");
2313 }
2314
2315 sub setup_generic_vampire_dc
2316 {
2317         my ($self, $path, $dc_vars, $fl) = @_;
2318
2319         my $env = $self->provision_vampire_dc($path, $dc_vars, $fl);
2320
2321         if (defined $env) {
2322                 if (not defined($self->check_or_start($env, "single"))) {
2323                         return undef;
2324                 }
2325
2326                 # force replicated DC to update repsTo/repsFrom
2327                 # for vampired partitions
2328                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2329
2330                 # as 'vampired' dc may add data in its local replica
2331                 # we need to synchronize data between DCs
2332                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2333                 my $cmd = $self->get_cmd_env_vars($env);
2334                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2335                 $cmd .= " $dc_vars->{CONFIGURATION}";
2336                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2337                 # replicate Configuration NC
2338                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2339                 unless(system($cmd_repl) == 0) {
2340                         warn("Failed to replicate\n$cmd_repl");
2341                         return undef;
2342                 }
2343                 # replicate Default NC
2344                 $cmd_repl = "$cmd \"$base_dn\"";
2345                 unless(system($cmd_repl) == 0) {
2346                         warn("Failed to replicate\n$cmd_repl");
2347                         return undef;
2348                 }
2349
2350                 # Pull in a full set of changes from the main DC
2351                 $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2352                 $cmd = $self->get_cmd_env_vars($env);
2353                 $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2354                 $cmd .= " $dc_vars->{CONFIGURATION}";
2355                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2356                 # replicate Configuration NC
2357                 $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2358                 unless(system($cmd_repl) == 0) {
2359                         warn("Failed to replicate\n$cmd_repl");
2360                         return undef;
2361                 }
2362                 # replicate Default NC
2363                 $cmd_repl = "$cmd \"$base_dn\"";
2364                 unless(system($cmd_repl) == 0) {
2365                         warn("Failed to replicate\n$cmd_repl");
2366                         return undef;
2367                 }
2368         }
2369
2370         return $env;
2371 }
2372
2373 sub setup_promoted_dc
2374 {
2375         my ($self, $path, $dc_vars) = @_;
2376
2377         my $env = $self->provision_promoted_dc($path, $dc_vars);
2378
2379         if (defined $env) {
2380                 if (not defined($self->check_or_start($env, "single"))) {
2381                         return undef;
2382                 }
2383
2384                 # force source and replicated DC to update repsTo/repsFrom
2385                 # for vampired partitions
2386                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2387                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2388                 # as 'vampired' dc may add data in its local replica
2389                 # we need to synchronize data between DCs
2390                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2391                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2392                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2393                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2394                 $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2395                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2396                 $cmd .= " $dc_vars->{CONFIGURATION}";
2397                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2398                 # replicate Configuration NC
2399                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2400                 unless(system($cmd_repl) == 0) {
2401                         warn("Failed to replicate\n$cmd_repl");
2402                         return undef;
2403                 }
2404                 # replicate Default NC
2405                 $cmd_repl = "$cmd \"$base_dn\"";
2406                 unless(system($cmd_repl) == 0) {
2407                         warn("Failed to replicate\n$cmd_repl");
2408                         return undef;
2409                 }
2410         }
2411
2412         return $env;
2413 }
2414
2415 sub setup_rodc
2416 {
2417         my ($self, $path, $dc_vars) = @_;
2418
2419         my $env = $self->provision_rodc($path, $dc_vars);
2420
2421         unless ($env) {
2422                 return undef;
2423         }
2424
2425         if (not defined($self->check_or_start($env, "standard"))) {
2426             return undef;
2427         }
2428
2429         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2430         my $cmd = "";
2431
2432         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2433         $cmd .= "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2434         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2435         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2436         $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2437         $cmd .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2438         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2439         $cmd .= " $dc_vars->{CONFIGURATION}";
2440         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2441         # replicate Configuration NC
2442         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2443         unless(system($cmd_repl) == 0) {
2444             warn("Failed to replicate\n$cmd_repl");
2445             return undef;
2446         }
2447         # replicate Default NC
2448         $cmd_repl = "$cmd \"$base_dn\"";
2449         unless(system($cmd_repl) == 0) {
2450             warn("Failed to replicate\n$cmd_repl");
2451             return undef;
2452         }
2453
2454         return $env;
2455 }
2456
2457 sub setup_ad_dc
2458 {
2459         my ($self, $path) = @_;
2460
2461         # If we didn't build with ADS, pretend this env was never available
2462         if (not $self->{target3}->have_ads()) {
2463                return "UNKNOWN";
2464         }
2465
2466         my $env = $self->provision_ad_dc($path, "addc", "ADDOMAIN",
2467                                          "addom.samba.example.com", "", undef);
2468         unless ($env) {
2469                 return undef;
2470         }
2471
2472         if (not defined($self->check_or_start($env, "prefork"))) {
2473             return undef;
2474         }
2475
2476         my $upn_array = ["$env->{REALM}.upn"];
2477         my $spn_array = ["$env->{REALM}.spn"];
2478
2479         $self->setup_namespaces($env, $upn_array, $spn_array);
2480
2481         return $env;
2482 }
2483
2484 sub setup_ad_dc_no_nss
2485 {
2486         my ($self, $path) = @_;
2487
2488         # If we didn't build with ADS, pretend this env was never available
2489         if (not $self->{target3}->have_ads()) {
2490                return "UNKNOWN";
2491         }
2492
2493         my $env = $self->provision_ad_dc($path, "addc_no_nss", "ADNONSSDOMAIN",
2494                                          "adnonssdom.samba.example.com", "", undef);
2495         unless ($env) {
2496                 return undef;
2497         }
2498
2499         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2500         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2501
2502         if (not defined($self->check_or_start($env, "single"))) {
2503             return undef;
2504         }
2505
2506         my $upn_array = ["$env->{REALM}.upn"];
2507         my $spn_array = ["$env->{REALM}.spn"];
2508
2509         $self->setup_namespaces($env, $upn_array, $spn_array);
2510
2511         return $env;
2512 }
2513
2514 sub setup_ad_dc_no_ntlm
2515 {
2516         my ($self, $path) = @_;
2517
2518         # If we didn't build with ADS, pretend this env was never available
2519         if (not $self->{target3}->have_ads()) {
2520                return "UNKNOWN";
2521         }
2522
2523         my $env = $self->provision_ad_dc($path, "addc_no_ntlm", "ADNONTLMDOMAIN",
2524                                          "adnontlmdom.samba.example.com",
2525                                          "ntlm auth = disabled", undef);
2526         unless ($env) {
2527                 return undef;
2528         }
2529
2530         if (not defined($self->check_or_start($env, "prefork"))) {
2531             return undef;
2532         }
2533
2534         my $upn_array = ["$env->{REALM}.upn"];
2535         my $spn_array = ["$env->{REALM}.spn"];
2536
2537         $self->setup_namespaces($env, $upn_array, $spn_array);
2538
2539         return $env;
2540 }
2541
2542 #
2543 # AD DC test environment used solely to test pre-fork process restarts.
2544 # As processes get killed off and restarted it should not be used for other
2545 sub setup_preforkrestartdc
2546 {
2547         my ($self, $path) = @_;
2548
2549         # If we didn't build with ADS, pretend this env was never available
2550         if (not $self->{target3}->have_ads()) {
2551                return "UNKNOWN";
2552         }
2553
2554         # note DC name must be <= 15 chars so we use 'prockill' instead of
2555         # 'preforkrestart'
2556         my $env = $self->provision_ad_dc(
2557                 $path,
2558                 "prockilldc",
2559                 "PROCKILLDOMAIN",
2560                 "prockilldom.samba.example.com",
2561                 "prefork backoff increment = 5\nprefork maximum backoff=10");
2562         unless ($env) {
2563                 return undef;
2564         }
2565
2566         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2567         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2568
2569         if (not defined($self->check_or_start($env, "prefork"))) {
2570             return undef;
2571         }
2572
2573         my $upn_array = ["$env->{REALM}.upn"];
2574         my $spn_array = ["$env->{REALM}.spn"];
2575
2576         $self->setup_namespaces($env, $upn_array, $spn_array);
2577
2578         return $env;
2579 }
2580
2581 #
2582 # ad_dc test environment used solely to test standard process model connection
2583 # process limits. As the limit is set artificially low it should not be used
2584 # for other tests.
2585 sub setup_proclimitdc
2586 {
2587         my ($self, $path) = @_;
2588
2589         # If we didn't build with ADS, pretend this env was never available
2590         if (not $self->{target3}->have_ads()) {
2591                return "UNKNOWN";
2592         }
2593
2594         my $env = $self->provision_ad_dc(
2595                 $path,
2596                 "proclimitdc",
2597                 "PROCLIMITDOM",
2598                 "proclimit.samba.example.com",
2599                 "max smbd processes = 20");
2600         unless ($env) {
2601                 return undef;
2602         }
2603
2604         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2605         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2606
2607         if (not defined($self->check_or_start($env, "standard"))) {
2608             return undef;
2609         }
2610
2611         my $upn_array = ["$env->{REALM}.upn"];
2612         my $spn_array = ["$env->{REALM}.spn"];
2613
2614         $self->setup_namespaces($env, $upn_array, $spn_array);
2615
2616         return $env;
2617 }
2618
2619 # Used to test a live upgrade of the schema on a 2 DC network.
2620 sub setup_schema_dc
2621 {
2622         my ($self, $path) = @_;
2623
2624         # provision the PDC using an older base schema
2625         my $provision_args = ["--base-schema=2008_R2", "--backend-store=mdb"];
2626
2627         my $env = $self->provision_ad_dc($path, "liveupgrade1dc", "SCHEMADOMAIN",
2628                                          "schema.samba.example.com",
2629                                          "drs: max link sync = 2",
2630                                          $provision_args);
2631         unless ($env) {
2632                 return undef;
2633         }
2634
2635         if (not defined($self->check_or_start($env, "prefork"))) {
2636             return undef;
2637         }
2638
2639         my $upn_array = ["$env->{REALM}.upn"];
2640         my $spn_array = ["$env->{REALM}.spn"];
2641
2642         $self->setup_namespaces($env, $upn_array, $spn_array);
2643
2644         return $env;
2645 }
2646
2647 # the second DC in the live schema upgrade pair
2648 sub setup_schema_pair_dc
2649 {
2650         # note: dcvars contains the env info for the dependent testenv ('schema_dc')
2651         my ($self, $prefix, $dcvars) = @_;
2652         print "Preparing SCHEMA UPGRADE PAIR DC...\n";
2653
2654         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "liveupgrade2dc",
2655                                                     $dcvars->{DOMAIN},
2656                                                     $dcvars->{REALM},
2657                                                     $dcvars->{PASSWORD},
2658                                                     "");
2659
2660         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2661         my $cmd_vars = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2662         $cmd_vars .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2663         if (defined($env->{RESOLV_WRAPPER_CONF})) {
2664                 $cmd_vars .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2665         } else {
2666                 $cmd_vars .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2667         }
2668         $cmd_vars .= "KRB5_CONFIG=\"$env->{KRB5_CONFIG}\" ";
2669         $cmd_vars .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2670         $cmd_vars .= "RESOLV_CONF=\"$env->{RESOLV_CONF}\" ";
2671
2672         my $join_cmd = $cmd_vars;
2673         $join_cmd .= "$samba_tool domain join $env->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
2674         $join_cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} ";
2675         $join_cmd .= " --backend-store=mdb";
2676
2677         my $upgrade_cmd = $cmd_vars;
2678         $upgrade_cmd .= "$samba_tool domain schemaupgrade $dcvars->{CONFIGURATION}";
2679         $upgrade_cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
2680
2681         my $repl_cmd = $cmd_vars;
2682         $repl_cmd .= "$samba_tool drs replicate $env->{SERVER} $dcvars->{SERVER}";
2683         $repl_cmd .= " CN=Schema,CN=Configuration,DC=schema,DC=samba,DC=example,DC=com";
2684         $repl_cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
2685
2686         unless (system($join_cmd) == 0) {
2687                 warn("Join failed\n$join_cmd");
2688                 return undef;
2689         }
2690
2691         $env->{DC_SERVER} = $dcvars->{SERVER};
2692         $env->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
2693         $env->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
2694         $env->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
2695
2696         # start samba for the new DC
2697         if (not defined($self->check_or_start($env, "standard"))) {
2698             return undef;
2699         }
2700
2701         unless (system($upgrade_cmd) == 0) {
2702                 warn("Schema upgrade failed\n$upgrade_cmd");
2703                 return undef;
2704         }
2705
2706         unless (system($repl_cmd) == 0) {
2707                 warn("Post-update schema replication failed\n$repl_cmd");
2708                 return undef;
2709         }
2710
2711         return $env;
2712 }
2713
2714 # Sets up a DC that's solely used to do a domain backup from. We then use the
2715 # backupfrom-DC to create the restore-DC - this proves that the backup/restore
2716 # process will create a Samba DC that will actually start up.
2717 # We don't use the backup-DC for anything else because its domain will conflict
2718 # with the restore DC.
2719 sub setup_backupfromdc
2720 {
2721         my ($self, $path) = @_;
2722
2723         # If we didn't build with ADS, pretend this env was never available
2724         if (not $self->{target3}->have_ads()) {
2725                return "UNKNOWN";
2726         }
2727
2728         my $provision_args = ["--site=Backup-Site"];
2729
2730         my $env = $self->provision_ad_dc($path, "backupfromdc", "BACKUPDOMAIN",
2731                                          "backupdom.samba.example.com",
2732                                          "samba kcc command = /bin/true",
2733                                          $provision_args);
2734         unless ($env) {
2735                 return undef;
2736         }
2737
2738         if (not defined($self->check_or_start($env))) {
2739             return undef;
2740         }
2741
2742         my $upn_array = ["$env->{REALM}.upn"];
2743         my $spn_array = ["$env->{REALM}.spn"];
2744
2745         $self->setup_namespaces($env, $upn_array, $spn_array);
2746
2747         return $env;
2748 }
2749
2750 # returns the server/user-auth params needed to run an online backup cmd
2751 sub get_backup_server_args
2752 {
2753         # dcvars contains the env info for the backup DC testenv
2754         my ($self, $dcvars) = @_;
2755         my $server = $dcvars->{DC_SERVER_IP};
2756         my $server_args = "--server=$server ";
2757         $server_args .= "-U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
2758         $server_args .= " $dcvars->{CONFIGURATION}";
2759
2760         return $server_args;
2761 }
2762
2763 # Creates a backup of a running testenv DC
2764 sub create_backup
2765 {
2766         # note: dcvars contains the env info for the backup DC testenv
2767         my ($self, $env, $dcvars, $backupdir, $backup_cmd) = @_;
2768
2769         # get all the env variables we pass in with the samba-tool command
2770         my $cmd_env = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2771         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2772         if (defined($env->{RESOLV_WRAPPER_CONF})) {
2773                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2774         } else {
2775                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2776         }
2777         # Note: use the backupfrom-DC's krb5.conf to do the backup
2778         $cmd_env .= " KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
2779         $cmd_env .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2780
2781         # use samba-tool to create a backup from the 'backupfromdc' DC
2782         my $cmd = "";
2783         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2784
2785         $cmd .= "$cmd_env $samba_tool domain backup $backup_cmd";
2786         $cmd .= " --targetdir=$backupdir";
2787
2788         print "Executing: $cmd\n";
2789         unless(system($cmd) == 0) {
2790                 warn("Failed to create backup using: \n$cmd");
2791                 return undef;
2792         }
2793
2794         # get the name of the backup file created
2795         opendir(DIR, $backupdir);
2796         my @files = grep(/\.tar/, readdir(DIR));
2797         closedir(DIR);
2798
2799         if(scalar @files != 1) {
2800                 warn("Backup file not found in directory $backupdir\n");
2801                 return undef;
2802         }
2803         my $backup_file = "$backupdir/$files[0]";
2804         print "Using backup file $backup_file...\n";
2805
2806         return $backup_file;
2807 }
2808
2809 # Restores a backup-file to populate a testenv for a new DC
2810 sub restore_backup_file
2811 {
2812         my ($self, $backup_file, $restore_opts, $restoredir, $smbconf) = @_;
2813
2814         # pass the restore command the testenv's smb.conf that we've already
2815         # generated. But move it to a temp-dir first, so that the restore doesn't
2816         # overwrite it
2817         my $tmpdir = File::Temp->newdir();
2818         my $tmpconf = "$tmpdir/smb.conf";
2819         my $cmd = "cp $smbconf $tmpconf";
2820         unless(system($cmd) == 0) {
2821                 warn("Failed to backup smb.conf using: \n$cmd");
2822                 return -1;
2823         }
2824
2825         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2826         $cmd = "$samba_tool domain backup restore --backup-file=$backup_file";
2827         $cmd .= " --targetdir=$restoredir $restore_opts --configfile=$tmpconf";
2828
2829         print "Executing: $cmd\n";
2830         unless(system($cmd) == 0) {
2831                 warn("Failed to restore backup using: \n$cmd");
2832                 return -1;
2833         }
2834
2835         print "Restore complete\n";
2836         return 0
2837 }
2838
2839 # sets up the initial directory and returns the new testenv's env info
2840 # (without actually doing a 'domain join')
2841 sub prepare_dc_testenv
2842 {
2843         my ($self, $prefix, $dcname, $domain, $realm,
2844                 $password, $conf_options) = @_;
2845
2846         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
2847                                                $dcname,
2848                                                $domain,
2849                                                $realm,
2850                                                undef,
2851                                                "2008",
2852                                                $password,
2853                                                undef,
2854                                                undef);
2855
2856         # the restore uses a slightly different state-dir location to other testenvs
2857         $ctx->{statedir} = "$ctx->{prefix_abs}/state";
2858         push(@{$ctx->{directories}}, "$ctx->{statedir}");
2859
2860         # add support for sysvol/netlogon/tmp shares
2861         $ctx->{share} = "$ctx->{prefix_abs}/share";
2862         push(@{$ctx->{directories}}, "$ctx->{share}");
2863         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
2864
2865         $ctx->{smb_conf_extra_options} = "
2866         $conf_options
2867         max xmit = 32K
2868         server max protocol = SMB2
2869         samba kcc command = /bin/true
2870         xattr_tdb:file = $ctx->{statedir}/xattr.tdb
2871
2872 [sysvol]
2873         path = $ctx->{statedir}/sysvol
2874         read only = no
2875
2876 [netlogon]
2877         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
2878         read only = no
2879
2880 [tmp]
2881         path = $ctx->{share}
2882         read only = no
2883         posix:sharedelay = 10000
2884         posix:oplocktimeout = 3
2885         posix:writetimeupdatedelay = 50000
2886
2887 [test1]
2888         path = $ctx->{share}/test1
2889         read only = no
2890         posix:sharedelay = 100000
2891         posix:oplocktimeout = 3
2892         posix:writetimeupdatedelay = 500000
2893 ";
2894
2895         my $env = $self->provision_raw_step1($ctx);
2896
2897     return ($env, $ctx);
2898 }
2899
2900
2901 # Set up a DC testenv solely by using the samba-tool domain backup/restore
2902 # commands. This proves that we can backup an online DC ('backupfromdc') and
2903 # use the backup file to create a valid, working samba DC.
2904 sub setup_restoredc
2905 {
2906         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
2907         my ($self, $prefix, $dcvars) = @_;
2908         print "Preparing RESTORE DC...\n";
2909
2910         # we arbitrarily designate the restored DC as having SMBv1 disabled
2911         my $extra_conf = "
2912         server min protocol = SMB2
2913         client min protocol = SMB2
2914         prefork children = 1";
2915
2916         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "restoredc",
2917                                                     $dcvars->{DOMAIN},
2918                                                     $dcvars->{REALM},
2919                                                     $dcvars->{PASSWORD},
2920                                                     $extra_conf);
2921
2922         # create a backup of the 'backupfromdc'
2923         my $backupdir = File::Temp->newdir();
2924         my $server_args = $self->get_backup_server_args($dcvars);
2925         my $backup_args = "online $server_args";
2926         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
2927                                                $backup_args);
2928         unless($backup_file) {
2929                 return undef;
2930         }
2931
2932         # restore the backup file to populate the restore-DC testenv
2933         my $restore_dir = abs_path($prefix);
2934         my $ret = $self->restore_backup_file($backup_file,
2935                                              "--newservername=$env->{SERVER}",
2936                                              $restore_dir, $env->{SERVERCONFFILE});
2937         unless ($ret == 0) {
2938                 return undef;
2939         }
2940
2941         # start samba for the restored DC
2942         if (not defined($self->check_or_start($env))) {
2943             return undef;
2944         }
2945
2946         return $env;
2947 }
2948
2949 # Set up a DC testenv solely by using the 'samba-tool domain backup rename' and
2950 # restore commands. This proves that we can backup and rename an online DC
2951 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
2952 sub setup_renamedc
2953 {
2954         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
2955         my ($self, $prefix, $dcvars) = @_;
2956         print "Preparing RENAME DC...\n";
2957         my $extra_conf = "prefork children = 1";
2958
2959         my $realm = "renamedom.samba.example.com";
2960         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "renamedc",
2961                                                     "RENAMEDOMAIN", $realm,
2962                                                     $dcvars->{PASSWORD}, $extra_conf);
2963
2964         # create a backup of the 'backupfromdc' which renames the domain
2965         my $backupdir = File::Temp->newdir();
2966         my $server_args = $self->get_backup_server_args($dcvars);
2967         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
2968         $backup_args .= " --backend-store=tdb";
2969         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
2970                                                $backup_args);
2971         unless($backup_file) {
2972                 return undef;
2973         }
2974
2975         # restore the backup file to populate the rename-DC testenv
2976         my $restore_dir = abs_path($prefix);
2977         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
2978         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
2979                                              $restore_dir, $env->{SERVERCONFFILE});
2980         unless ($ret == 0) {
2981                 return undef;
2982         }
2983
2984         # start samba for the restored DC
2985         if (not defined($self->check_or_start($env))) {
2986             return undef;
2987         }
2988
2989         my $upn_array = ["$env->{REALM}.upn"];
2990         my $spn_array = ["$env->{REALM}.spn"];
2991
2992         $self->setup_namespaces($env, $upn_array, $spn_array);
2993
2994         return $env;
2995 }
2996
2997 # Set up a DC testenv solely by using the 'samba-tool domain backup offline' and
2998 # restore commands. This proves that we do an offline backup of a local DC
2999 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
3000 sub setup_offlinebackupdc
3001 {
3002         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3003         my ($self, $prefix, $dcvars) = @_;
3004         print "Preparing OFFLINE BACKUP DC...\n";
3005         my $extra_conf = "prefork children = 1";
3006
3007         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "offlinebackupdc",
3008                                                     $dcvars->{DOMAIN},
3009                                                     $dcvars->{REALM},
3010                                                     $dcvars->{PASSWORD}, $extra_conf);
3011
3012         # create an offline backup of the 'backupfromdc' target
3013         my $backupdir = File::Temp->newdir();
3014         my $cmd = "offline -s $dcvars->{SERVERCONFFILE}";
3015         my $backup_file = $self->create_backup($env, $dcvars,
3016                                                $backupdir, $cmd);
3017
3018         unless($backup_file) {
3019                 return undef;
3020         }
3021
3022         # restore the backup file to populate the rename-DC testenv
3023         my $restore_dir = abs_path($prefix);
3024         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3025         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3026                                              $restore_dir, $env->{SERVERCONFFILE});
3027         unless ($ret == 0) {
3028                 return undef;
3029         }
3030
3031         # re-create the testenv's krb5.conf (the restore may have overwritten it)
3032         Samba::mk_krb5_conf($ctx);
3033
3034         # start samba for the restored DC
3035         if (not defined($self->check_or_start($env))) {
3036             return undef;
3037         }
3038
3039         return $env;
3040 }
3041
3042 # Set up a DC testenv solely by using the samba-tool 'domain backup rename' and
3043 # restore commands, using the --no-secrets option. This proves that we can
3044 # create a realistic lab environment from an online DC ('backupfromdc').
3045 sub setup_labdc
3046 {
3047         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3048         my ($self, $prefix, $dcvars) = @_;
3049         print "Preparing LAB-DOMAIN DC...\n";
3050         my $extra_conf = "prefork children = 1";
3051
3052         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "labdc",
3053                                                     "LABDOMAIN",
3054                                                     "labdom.samba.example.com",
3055                                                     $dcvars->{PASSWORD}, $extra_conf);
3056
3057         # create a backup of the 'backupfromdc' which renames the domain and uses
3058         # the --no-secrets option to scrub any sensitive info
3059         my $backupdir = File::Temp->newdir();
3060         my $server_args = $self->get_backup_server_args($dcvars);
3061         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
3062         $backup_args .= " --no-secrets --backend-store=mdb";
3063         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3064                                                $backup_args);
3065         unless($backup_file) {
3066                 return undef;
3067         }
3068
3069         # restore the backup file to populate the lab-DC testenv
3070         my $restore_dir = abs_path($prefix);
3071         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3072         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3073                                              $restore_dir, $env->{SERVERCONFFILE});
3074         unless ($ret == 0) {
3075                 return undef;
3076         }
3077
3078         # because we don't include any secrets in the backup, we need to reset the
3079         # admin user's password back to what the testenv expects
3080         my $samba_tool = Samba::bindir_path($self, "samba-tool");
3081         my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
3082         $cmd .= "--newpassword=$env->{PASSWORD} -H $restore_dir/private/sam.ldb";
3083         $cmd .= " $env->{CONFIGURATION}";
3084
3085         unless(system($cmd) == 0) {
3086                 warn("Failed to reset admin's password: \n$cmd");
3087                 return undef;
3088         }
3089
3090         # start samba for the restored DC
3091         if (not defined($self->check_or_start($env))) {
3092             return undef;
3093         }
3094
3095         my $upn_array = ["$env->{REALM}.upn"];
3096         my $spn_array = ["$env->{REALM}.spn"];
3097
3098         $self->setup_namespaces($env, $upn_array, $spn_array);
3099
3100         return $env;
3101 }
3102
3103 # Inspects a backup *.tar.bz2 file and determines the realm/domain it contains
3104 sub get_backup_domain_realm
3105 {
3106         my ($self, $backup_file) = @_;
3107
3108         print "Determining REALM/DOMAIN values in backup...\n";
3109
3110         # The backup will have the correct domain/realm values in the smb.conf.
3111         # So we can work out the env variables the testenv should use based on
3112         # that. Let's start by extracting the smb.conf
3113         my $tar = Archive::Tar->new($backup_file);
3114         my $tmpdir = File::Temp->newdir();
3115         my $smbconf = "$tmpdir/smb.conf";
3116
3117         # note that the filepaths within the tar-file differ slightly for online
3118         # and offline backups
3119         if ($tar->contains_file("etc/smb.conf")) {
3120                 $tar->extract_file("etc/smb.conf", $smbconf);
3121         } elsif ($tar->contains_file("./etc/smb.conf")) {
3122                 $tar->extract_file("./etc/smb.conf", $smbconf);
3123         } else {
3124                 warn("Could not find smb.conf in $backup_file");
3125                 return undef, undef;
3126         }
3127
3128         # make sure we don't try to create locks/sockets in the default install
3129         # location (i.e. /usr/local/samba/)
3130         my $options = "--option=\"private dir = $tmpdir\"";
3131         $options .=  " --option=\"lock dir = $tmpdir\"";
3132
3133         # now use testparm to read the values we're interested in
3134         my $testparm = Samba::bindir_path($self, "testparm");
3135         my $domain = `$testparm $smbconf -sl --parameter-name=WORKGROUP $options`;
3136         my $realm = `$testparm $smbconf -sl --parameter-name=REALM $options`;
3137         chomp $realm;
3138         chomp $domain;
3139         print "Backup-file REALM is $realm, DOMAIN is $domain\n";
3140
3141         return ($domain, $realm);
3142 }
3143
3144 # This spins up a custom testenv that can be based on any backup-file you want.
3145 # This is just intended for manual testing (rather than automated test-cases)
3146 sub setup_customdc
3147 {
3148         my ($self, $prefix) = @_;
3149         print "Preparing CUSTOM RESTORE DC...\n";
3150         my $dc_name = "customdc";
3151         my $password = "locDCpass1";
3152         my $backup_file = $ENV{'BACKUP_FILE'};
3153
3154         # user must specify a backup file to restore via an ENV variable, i.e.
3155         # BACKUP_FILE=backup-blah.tar.bz2 SELFTEST_TESTENV=customdc make testenv
3156         if (not defined($backup_file)) {
3157                 warn("Please specify BACKUP_FILE");
3158                 return undef;
3159         }
3160
3161         # work out the correct domain/realm env values from the backup-file
3162         my ($domain, $realm) = $self->get_backup_domain_realm($backup_file);
3163         if ($domain eq '' or $realm eq '') {
3164                 warn("Could not determine domain or realm");
3165                 return undef;
3166         }
3167
3168         # create a placeholder directory and smb.conf, as well as the env vars.
3169         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, $dc_name,
3170                                                     $domain, $realm, $password, "");
3171
3172         # restore the specified backup file to populate the testenv
3173         my $restore_dir = abs_path($prefix);
3174         my $ret = $self->restore_backup_file($backup_file,
3175                                              "--newservername=$env->{SERVER}",
3176                                              $restore_dir, $env->{SERVERCONFFILE});
3177         unless ($ret == 0) {
3178                 return undef;
3179         }
3180
3181         # Change the admin password to the testenv default, just in case it's
3182         # different, or in case this was a --no-secrets backup
3183         my $samba_tool = Samba::bindir_path($self, "samba-tool");
3184         my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
3185         $cmd .= "--newpassword=$password -H $restore_dir/private/sam.ldb";
3186         $cmd .= " $env->{CONFIGURATION}";
3187
3188         unless(system($cmd) == 0) {
3189                 warn("Failed to reset admin's password: \n$cmd");
3190                 return undef;
3191         }
3192
3193         # re-create the testenv's krb5.conf (the restore may have overwritten it,
3194         # if the backup-file was an offline backup)
3195         Samba::mk_krb5_conf($ctx);
3196
3197         # start samba for the restored DC
3198         if (not defined($self->check_or_start($env))) {
3199             return undef;
3200         }
3201
3202         # if this was a backup-rename, then we may need to setup namespaces
3203         my $upn_array = ["$env->{REALM}.upn"];
3204         my $spn_array = ["$env->{REALM}.spn"];
3205
3206         $self->setup_namespaces($env, $upn_array, $spn_array);
3207
3208         return $env;
3209 }
3210
3211 sub setup_none
3212 {
3213         my ($self, $path) = @_;
3214
3215         my $ret = {
3216                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
3217                 SAMBA_PID => -1,
3218         }
3219 }
3220
3221 1;