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