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