b4e155ad7ffb7baad033e9061adb96bfb444849f
[samba.git] / selftest / target / Samba4.pm
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
5
6 # NOTE: Refer to the README for more details about the various testenvs,
7 # and tips about adding new testenvs.
8
9 package Samba4;
10
11 use strict;
12 use 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         dns_hub              => [],
2319         ad_dc_ntvfs          => ["dns_hub"],
2320         ad_dc                => ["dns_hub"],
2321         ad_dc_no_nss         => ["dns_hub"],
2322         ad_dc_no_ntlm        => ["dns_hub"],
2323         backupfromdc         => ["dns_hub"],
2324         customdc             => ["dns_hub"],
2325         preforkrestartdc     => ["dns_hub"],
2326
2327         fl2008r2dc           => ["ad_dc"],
2328         fl2003dc             => ["ad_dc"],
2329         fl2000dc             => ["dns_hub"],
2330
2331         vampire_2000_dc      => ["fl2000dc"],
2332         vampire_dc           => ["ad_dc_ntvfs"],
2333         promoted_dc          => ["ad_dc_ntvfs"],
2334         subdom_dc            => ["ad_dc_ntvfs"],
2335
2336         rodc                 => ["ad_dc_ntvfs"],
2337         rpc_proxy            => ["ad_dc_ntvfs"],
2338         chgdcpass            => ["dns_hub"],
2339
2340         s4member_dflt_domain => ["ad_dc_ntvfs"],
2341         s4member             => ["ad_dc_ntvfs"],
2342
2343         restoredc            => ["backupfromdc"],
2344         renamedc             => ["backupfromdc"],
2345         offlinebackupdc      => ["backupfromdc"],
2346         labdc                => ["backupfromdc"],
2347         proclimitdc          => [],
2348
2349         none                 => [],
2350 );
2351
2352 sub setup_s4member
2353 {
2354         my ($self, $path, $dc_vars) = @_;
2355
2356         my $env = $self->provision_s4member($path, $dc_vars, "s4member");
2357
2358         if (defined $env) {
2359                 if (not defined($self->check_or_start($env, "standard"))) {
2360                         return undef;
2361                 }
2362         }
2363
2364         return $env;
2365 }
2366
2367 sub setup_s4member_dflt_domain
2368 {
2369         my ($self, $path, $dc_vars) = @_;
2370
2371         my $env = $self->provision_s4member($path, $dc_vars, "s4member_dflt",
2372                                             "winbind use default domain = yes");
2373
2374         if (defined $env) {
2375                 if (not defined($self->check_or_start($env, "standard"))) {
2376                         return undef;
2377                 }
2378         }
2379
2380         return $env;
2381 }
2382
2383 sub setup_rpc_proxy
2384 {
2385         my ($self, $path, $dc_vars) = @_;
2386
2387         my $env = $self->provision_rpc_proxy($path, $dc_vars);
2388
2389         if (defined $env) {
2390                 if (not defined($self->check_or_start($env, "standard"))) {
2391                         return undef;
2392                 }
2393         }
2394         return $env;
2395 }
2396
2397 sub setup_ad_dc_ntvfs
2398 {
2399         my ($self, $path) = @_;
2400
2401         my $env = $self->provision_ad_dc_ntvfs($path);
2402         if (defined $env) {
2403                 if (not defined($self->check_or_start($env, "standard"))) {
2404                     warn("Failed to start ad_dc_ntvfs");
2405                         return undef;
2406                 }
2407         }
2408         return $env;
2409 }
2410
2411 sub setup_chgdcpass
2412 {
2413         my ($self, $path) = @_;
2414
2415         my $env = $self->provision_chgdcpass($path);
2416         if (defined $env) {
2417                 if (not defined($self->check_or_start($env, "standard"))) {
2418                         return undef;
2419                 }
2420         }
2421         return $env;
2422 }
2423
2424 sub setup_fl2000dc
2425 {
2426         my ($self, $path) = @_;
2427
2428         my $env = $self->provision_fl2000dc($path);
2429         if (defined $env) {
2430                 if (not defined($self->check_or_start($env, "standard"))) {
2431                         return undef;
2432                 }
2433         }
2434
2435         return $env;
2436 }
2437
2438 sub setup_fl2003dc
2439 {
2440         my ($self, $path, $dc_vars) = @_;
2441
2442         my $env = $self->provision_fl2003dc($path);
2443
2444         if (defined $env) {
2445                 if (not defined($self->check_or_start($env, "standard"))) {
2446                         return undef;
2447                 }
2448
2449                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2450         }
2451         return $env;
2452 }
2453
2454 sub setup_fl2008r2dc
2455 {
2456         my ($self, $path, $dc_vars) = @_;
2457
2458         my $env = $self->provision_fl2008r2dc($path);
2459
2460         if (defined $env) {
2461                 if (not defined($self->check_or_start($env, "standard"))) {
2462                         return undef;
2463                 }
2464
2465                 my $upn_array = ["$env->{REALM}.upn"];
2466                 my $spn_array = ["$env->{REALM}.spn"];
2467
2468                 $self->setup_namespaces($env, $upn_array, $spn_array);
2469
2470                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2471         }
2472
2473         return $env;
2474 }
2475
2476 sub setup_vampire_dc
2477 {
2478         return setup_generic_vampire_dc(@_, "2008");
2479 }
2480
2481 sub setup_vampire_2000_dc
2482 {
2483         return setup_generic_vampire_dc(@_, "2000");
2484 }
2485
2486 sub setup_generic_vampire_dc
2487 {
2488         my ($self, $path, $dc_vars, $fl) = @_;
2489
2490         my $env = $self->provision_vampire_dc($path, $dc_vars, $fl);
2491
2492         if (defined $env) {
2493                 if (not defined($self->check_or_start($env, "single"))) {
2494                         return undef;
2495                 }
2496
2497                 # force replicated DC to update repsTo/repsFrom
2498                 # for vampired partitions
2499                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2500
2501                 # as 'vampired' dc may add data in its local replica
2502                 # we need to synchronize data between DCs
2503                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2504                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2505                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2506                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2507                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2508                 } else {
2509                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2510                 }
2511                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2512                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2513                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2514                 $cmd .= " $dc_vars->{CONFIGURATION}";
2515                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2516                 # replicate Configuration NC
2517                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2518                 unless(system($cmd_repl) == 0) {
2519                         warn("Failed to replicate\n$cmd_repl");
2520                         return undef;
2521                 }
2522                 # replicate Default NC
2523                 $cmd_repl = "$cmd \"$base_dn\"";
2524                 unless(system($cmd_repl) == 0) {
2525                         warn("Failed to replicate\n$cmd_repl");
2526                         return undef;
2527                 }
2528
2529                 # Pull in a full set of changes from the main DC
2530                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2531                 $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2532                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2533                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2534                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2535                 } else {
2536                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2537                 }
2538                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2539                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2540                 $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2541                 $cmd .= " $dc_vars->{CONFIGURATION}";
2542                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2543                 # replicate Configuration NC
2544                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2545                 unless(system($cmd_repl) == 0) {
2546                         warn("Failed to replicate\n$cmd_repl");
2547                         return undef;
2548                 }
2549                 # replicate Default NC
2550                 $cmd_repl = "$cmd \"$base_dn\"";
2551                 unless(system($cmd_repl) == 0) {
2552                         warn("Failed to replicate\n$cmd_repl");
2553                         return undef;
2554                 }
2555         }
2556
2557         return $env;
2558 }
2559
2560 sub setup_promoted_dc
2561 {
2562         my ($self, $path, $dc_vars) = @_;
2563
2564         my $env = $self->provision_promoted_dc($path, $dc_vars);
2565
2566         if (defined $env) {
2567                 if (not defined($self->check_or_start($env, "single"))) {
2568                         return undef;
2569                 }
2570
2571                 # force source and replicated DC to update repsTo/repsFrom
2572                 # for vampired partitions
2573                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2574                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2575                 # as 'vampired' dc may add data in its local replica
2576                 # we need to synchronize data between DCs
2577                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2578                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2579                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2580                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2581                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2582                 $cmd .= " $dc_vars->{CONFIGURATION}";
2583                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2584                 # replicate Configuration NC
2585                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2586                 unless(system($cmd_repl) == 0) {
2587                         warn("Failed to replicate\n$cmd_repl");
2588                         return undef;
2589                 }
2590                 # replicate Default NC
2591                 $cmd_repl = "$cmd \"$base_dn\"";
2592                 unless(system($cmd_repl) == 0) {
2593                         warn("Failed to replicate\n$cmd_repl");
2594                         return undef;
2595                 }
2596         }
2597
2598         return $env;
2599 }
2600
2601 sub setup_subdom_dc
2602 {
2603         my ($self, $path, $dc_vars) = @_;
2604
2605         my $env = $self->provision_subdom_dc($path, $dc_vars);
2606
2607         if (defined $env) {
2608                 if (not defined($self->check_or_start($env, "single"))) {
2609                         return undef;
2610                 }
2611
2612                 # force replicated DC to update repsTo/repsFrom
2613                 # for primary domain partitions
2614                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2615                 my $cmd = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2616                 # as 'subdomain' dc may add data in its local replica
2617                 # we need to synchronize data between DCs
2618                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
2619                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2620                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2621                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2622                 $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2623                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
2624                 $cmd .= " $dc_vars->{CONFIGURATION}";
2625                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2626                 # replicate Configuration NC
2627                 my $cmd_repl = "$cmd \"$config_dn\"";
2628                 unless(system($cmd_repl) == 0) {
2629                         warn("Failed to replicate\n$cmd_repl");
2630                         return undef;
2631                 }
2632                 # replicate Default NC
2633                 $cmd_repl = "$cmd \"$base_dn\"";
2634                 unless(system($cmd_repl) == 0) {
2635                         warn("Failed to replicate\n$cmd_repl");
2636                         return undef;
2637                 }
2638         }
2639
2640         return $env;
2641 }
2642
2643 sub setup_rodc
2644 {
2645         my ($self, $path, $dc_vars) = @_;
2646
2647         my $env = $self->provision_rodc($path, $dc_vars);
2648
2649         unless ($env) {
2650                 return undef;
2651         }
2652
2653         if (not defined($self->check_or_start($env, "standard"))) {
2654             return undef;
2655         }
2656
2657         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2658         my $cmd = "";
2659
2660         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2661         $cmd .= "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2662         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2663         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2664         $cmd .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2665         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2666         $cmd .= " $dc_vars->{CONFIGURATION}";
2667         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2668         # replicate Configuration NC
2669         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2670         unless(system($cmd_repl) == 0) {
2671             warn("Failed to replicate\n$cmd_repl");
2672             return undef;
2673         }
2674         # replicate Default NC
2675         $cmd_repl = "$cmd \"$base_dn\"";
2676         unless(system($cmd_repl) == 0) {
2677             warn("Failed to replicate\n$cmd_repl");
2678             return undef;
2679         }
2680
2681         return $env;
2682 }
2683
2684 sub setup_ad_dc
2685 {
2686         my ($self, $path) = @_;
2687
2688         # If we didn't build with ADS, pretend this env was never available
2689         if (not $self->{target3}->have_ads()) {
2690                return "UNKNOWN";
2691         }
2692
2693         my $env = $self->provision_ad_dc($path, "addc", "ADDOMAIN",
2694                                          "addom.samba.example.com", "", undef);
2695         unless ($env) {
2696                 return undef;
2697         }
2698
2699         if (not defined($self->check_or_start($env, "prefork"))) {
2700             return undef;
2701         }
2702
2703         my $upn_array = ["$env->{REALM}.upn"];
2704         my $spn_array = ["$env->{REALM}.spn"];
2705
2706         $self->setup_namespaces($env, $upn_array, $spn_array);
2707
2708         return $env;
2709 }
2710
2711 sub setup_ad_dc_no_nss
2712 {
2713         my ($self, $path) = @_;
2714
2715         # If we didn't build with ADS, pretend this env was never available
2716         if (not $self->{target3}->have_ads()) {
2717                return "UNKNOWN";
2718         }
2719
2720         my $env = $self->provision_ad_dc($path, "addc_no_nss", "ADNONSSDOMAIN",
2721                                          "adnonssdom.samba.example.com", "", undef);
2722         unless ($env) {
2723                 return undef;
2724         }
2725
2726         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2727         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2728
2729         if (not defined($self->check_or_start($env, "single"))) {
2730             return undef;
2731         }
2732
2733         my $upn_array = ["$env->{REALM}.upn"];
2734         my $spn_array = ["$env->{REALM}.spn"];
2735
2736         $self->setup_namespaces($env, $upn_array, $spn_array);
2737
2738         return $env;
2739 }
2740
2741 sub setup_ad_dc_no_ntlm
2742 {
2743         my ($self, $path) = @_;
2744
2745         # If we didn't build with ADS, pretend this env was never available
2746         if (not $self->{target3}->have_ads()) {
2747                return "UNKNOWN";
2748         }
2749
2750         my $env = $self->provision_ad_dc($path, "addc_no_ntlm", "ADNONTLMDOMAIN",
2751                                          "adnontlmdom.samba.example.com",
2752                                          "ntlm auth = disabled", undef);
2753         unless ($env) {
2754                 return undef;
2755         }
2756
2757         if (not defined($self->check_or_start($env, "prefork"))) {
2758             return undef;
2759         }
2760
2761         my $upn_array = ["$env->{REALM}.upn"];
2762         my $spn_array = ["$env->{REALM}.spn"];
2763
2764         $self->setup_namespaces($env, $upn_array, $spn_array);
2765
2766         return $env;
2767 }
2768
2769 #
2770 # AD DC test environment used solely to test pre-fork process restarts.
2771 # As processes get killed off and restarted it should not be used for other
2772 sub setup_preforkrestartdc
2773 {
2774         my ($self, $path) = @_;
2775
2776         # If we didn't build with ADS, pretend this env was never available
2777         if (not $self->{target3}->have_ads()) {
2778                return "UNKNOWN";
2779         }
2780
2781         # note DC name must be <= 15 chars so we use 'prockill' instead of
2782         # 'preforkrestart'
2783         my $env = $self->provision_ad_dc(
2784                 $path,
2785                 "prockilldc",
2786                 "PROCKILLDOMAIN",
2787                 "prockilldom.samba.example.com",
2788                 "prefork backoff increment = 5\nprefork maximum backoff=10");
2789         unless ($env) {
2790                 return undef;
2791         }
2792
2793         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2794         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2795
2796         if (not defined($self->check_or_start($env, "prefork"))) {
2797             return undef;
2798         }
2799
2800         my $upn_array = ["$env->{REALM}.upn"];
2801         my $spn_array = ["$env->{REALM}.spn"];
2802
2803         $self->setup_namespaces($env, $upn_array, $spn_array);
2804
2805         return $env;
2806 }
2807
2808 #
2809 # ad_dc test environment used solely to test standard process model connection
2810 # process limits. As the limit is set artificially low it should not be used
2811 # for other tests.
2812 sub setup_proclimitdc
2813 {
2814         my ($self, $path) = @_;
2815
2816         # If we didn't build with ADS, pretend this env was never available
2817         if (not $self->{target3}->have_ads()) {
2818                return "UNKNOWN";
2819         }
2820
2821         my $env = $self->provision_ad_dc(
2822                 $path,
2823                 "proclimitdc",
2824                 "PROCLIMITDOM",
2825                 "proclimit.samba.example.com",
2826                 "max smbd processes = 20");
2827         unless ($env) {
2828                 return undef;
2829         }
2830
2831         $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2832         $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2833
2834         if (not defined($self->check_or_start($env, "standard"))) {
2835             return undef;
2836         }
2837
2838         my $upn_array = ["$env->{REALM}.upn"];
2839         my $spn_array = ["$env->{REALM}.spn"];
2840
2841         $self->setup_namespaces($env, $upn_array, $spn_array);
2842
2843         return $env;
2844 }
2845
2846 # Sets up a DC that's solely used to do a domain backup from. We then use the
2847 # backupfrom-DC to create the restore-DC - this proves that the backup/restore
2848 # process will create a Samba DC that will actually start up.
2849 # We don't use the backup-DC for anything else because its domain will conflict
2850 # with the restore DC.
2851 sub setup_backupfromdc
2852 {
2853         my ($self, $path) = @_;
2854
2855         # If we didn't build with ADS, pretend this env was never available
2856         if (not $self->{target3}->have_ads()) {
2857                return "UNKNOWN";
2858         }
2859
2860         my $provision_args = ["--site=Backup-Site"];
2861
2862         my $env = $self->provision_ad_dc($path, "backupfromdc", "BACKUPDOMAIN",
2863                                          "backupdom.samba.example.com",
2864                                          "samba kcc command = /bin/true",
2865                                          $provision_args);
2866         unless ($env) {
2867                 return undef;
2868         }
2869
2870         if (not defined($self->check_or_start($env, "standard"))) {
2871             return undef;
2872         }
2873
2874         my $upn_array = ["$env->{REALM}.upn"];
2875         my $spn_array = ["$env->{REALM}.spn"];
2876
2877         $self->setup_namespaces($env, $upn_array, $spn_array);
2878
2879         return $env;
2880 }
2881
2882 # returns the server/user-auth params needed to run an online backup cmd
2883 sub get_backup_server_args
2884 {
2885         # dcvars contains the env info for the backup DC testenv
2886         my ($self, $dcvars) = @_;
2887         my $server = $dcvars->{DC_SERVER_IP};
2888         my $server_args = "--server=$server ";
2889         $server_args .= "-U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
2890         $server_args .= " $dcvars->{CONFIGURATION}";
2891
2892         return $server_args;
2893 }
2894
2895 # Creates a backup of a running testenv DC
2896 sub create_backup
2897 {
2898         # note: dcvars contains the env info for the backup DC testenv
2899         my ($self, $env, $dcvars, $backupdir, $backup_cmd) = @_;
2900
2901         # get all the env variables we pass in with the samba-tool command
2902         my $cmd_env = "NSS_WRAPPER_HOSTS='$env->{NSS_WRAPPER_HOSTS}' ";
2903         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2904         if (defined($env->{RESOLV_WRAPPER_CONF})) {
2905                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2906         } else {
2907                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2908         }
2909         # Note: use the backupfrom-DC's krb5.conf to do the backup
2910         $cmd_env .= " KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
2911         $cmd_env .= "KRB5CCNAME=\"$env->{KRB5_CCACHE}\" ";
2912
2913         # use samba-tool to create a backup from the 'backupfromdc' DC
2914         my $cmd = "";
2915         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2916
2917         $cmd .= "$cmd_env $samba_tool domain backup $backup_cmd";
2918         $cmd .= " --targetdir=$backupdir";
2919
2920         print "Executing: $cmd\n";
2921         unless(system($cmd) == 0) {
2922                 warn("Failed to create backup using: \n$cmd");
2923                 return undef;
2924         }
2925
2926         # get the name of the backup file created
2927         opendir(DIR, $backupdir);
2928         my @files = grep(/\.tar/, readdir(DIR));
2929         closedir(DIR);
2930
2931         if(scalar @files != 1) {
2932                 warn("Backup file not found in directory $backupdir\n");
2933                 return undef;
2934         }
2935         my $backup_file = "$backupdir/$files[0]";
2936         print "Using backup file $backup_file...\n";
2937
2938         return $backup_file;
2939 }
2940
2941 # Restores a backup-file to populate a testenv for a new DC
2942 sub restore_backup_file
2943 {
2944         my ($self, $backup_file, $restore_opts, $restoredir, $smbconf) = @_;
2945
2946         # pass the restore command the testenv's smb.conf that we've already
2947         # generated. But move it to a temp-dir first, so that the restore doesn't
2948         # overwrite it
2949         my $tmpdir = File::Temp->newdir();
2950         my $tmpconf = "$tmpdir/smb.conf";
2951         my $cmd = "cp $smbconf $tmpconf";
2952         unless(system($cmd) == 0) {
2953                 warn("Failed to backup smb.conf using: \n$cmd");
2954                 return -1;
2955         }
2956
2957         my $samba_tool = Samba::bindir_path($self, "samba-tool");
2958         $cmd = "$samba_tool domain backup restore --backup-file=$backup_file";
2959         $cmd .= " --targetdir=$restoredir $restore_opts --configfile=$tmpconf";
2960
2961         print "Executing: $cmd\n";
2962         unless(system($cmd) == 0) {
2963                 warn("Failed to restore backup using: \n$cmd");
2964                 return -1;
2965         }
2966
2967         print "Restore complete\n";
2968         return 0
2969 }
2970
2971 # sets up the initial directory and returns the new testenv's env info
2972 # (without actually doing a 'domain join')
2973 sub prepare_dc_testenv
2974 {
2975         my ($self, $prefix, $dcname, $domain, $realm,
2976                 $password, $conf_options) = @_;
2977
2978         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
2979                                                $dcname,
2980                                                $domain,
2981                                                $realm,
2982                                                undef,
2983                                                "2008",
2984                                                $password,
2985                                                undef,
2986                                                undef);
2987
2988         # the restore uses a slightly different state-dir location to other testenvs
2989         $ctx->{statedir} = "$ctx->{prefix_abs}/state";
2990         push(@{$ctx->{directories}}, "$ctx->{statedir}");
2991
2992         # add support for sysvol/netlogon/tmp shares
2993         $ctx->{share} = "$ctx->{prefix_abs}/share";
2994         push(@{$ctx->{directories}}, "$ctx->{share}");
2995
2996         $ctx->{smb_conf_extra_options} = "
2997         $conf_options
2998         max xmit = 32K
2999         server max protocol = SMB2
3000         samba kcc command = /bin/true
3001
3002 [sysvol]
3003         path = $ctx->{statedir}/sysvol
3004         read only = no
3005
3006 [netlogon]
3007         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
3008         read only = no
3009
3010 [tmp]
3011         path = $ctx->{share}
3012         read only = no
3013         posix:sharedelay = 10000
3014         posix:oplocktimeout = 3
3015         posix:writetimeupdatedelay = 50000
3016
3017 ";
3018
3019         my $env = $self->provision_raw_step1($ctx);
3020
3021         $env->{DC_SERVER} = $env->{SERVER};
3022         $env->{DC_SERVER_IP} = $env->{SERVER_IP};
3023         $env->{DC_SERVER_IPV6} = $env->{SERVER_IPV6};
3024         $env->{DC_NETBIOSNAME} = $env->{NETBIOSNAME};
3025         $env->{DC_USERNAME} = $env->{USERNAME};
3026         $env->{DC_PASSWORD} = $env->{PASSWORD};
3027
3028     return ($env, $ctx);
3029 }
3030
3031
3032 # Set up a DC testenv solely by using the samba-tool domain backup/restore
3033 # commands. This proves that we can backup an online DC ('backupfromdc') and
3034 # use the backup file to create a valid, working samba DC.
3035 sub setup_restoredc
3036 {
3037         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3038         my ($self, $prefix, $dcvars) = @_;
3039         print "Preparing RESTORE DC...\n";
3040
3041         # we arbitrarily designate the restored DC as having SMBv1 disabled
3042         my $extra_conf = "
3043         server min protocol = SMB2
3044         client min protocol = SMB2";
3045
3046         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "restoredc",
3047                                                     $dcvars->{DOMAIN},
3048                                                     $dcvars->{REALM},
3049                                                     $dcvars->{PASSWORD},
3050                                                     $extra_conf);
3051
3052         # create a backup of the 'backupfromdc'
3053         my $backupdir = File::Temp->newdir();
3054         my $server_args = $self->get_backup_server_args($dcvars);
3055         my $backup_args = "online $server_args";
3056         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3057                                                $backup_args);
3058         unless($backup_file) {
3059                 return undef;
3060         }
3061
3062         # restore the backup file to populate the restore-DC testenv
3063         my $restore_dir = abs_path($prefix);
3064         my $ret = $self->restore_backup_file($backup_file,
3065                                              "--newservername=$env->{SERVER}",
3066                                              $restore_dir, $env->{SERVERCONFFILE});
3067         unless ($ret == 0) {
3068                 return undef;
3069         }
3070
3071         # start samba for the restored DC
3072         if (not defined($self->check_or_start($env, "standard"))) {
3073             return undef;
3074         }
3075
3076         return $env;
3077 }
3078
3079 # Set up a DC testenv solely by using the 'samba-tool domain backup rename' and
3080 # restore commands. This proves that we can backup and rename an online DC
3081 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
3082 sub setup_renamedc
3083 {
3084         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3085         my ($self, $prefix, $dcvars) = @_;
3086         print "Preparing RENAME DC...\n";
3087
3088         my $realm = "renamedom.samba.example.com";
3089         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "renamedc",
3090                                                     "RENAMEDOMAIN", $realm,
3091                                                     $dcvars->{PASSWORD}, "");
3092
3093         # create a backup of the 'backupfromdc' which renames the domain
3094         my $backupdir = File::Temp->newdir();
3095         my $server_args = $self->get_backup_server_args($dcvars);
3096         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
3097         $backup_args .= " --backend-store=tdb";
3098         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3099                                                $backup_args);
3100         unless($backup_file) {
3101                 return undef;
3102         }
3103
3104         # restore the backup file to populate the rename-DC testenv
3105         my $restore_dir = abs_path($prefix);
3106         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3107         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3108                                              $restore_dir, $env->{SERVERCONFFILE});
3109         unless ($ret == 0) {
3110                 return undef;
3111         }
3112
3113         # start samba for the restored DC
3114         if (not defined($self->check_or_start($env, "standard"))) {
3115             return undef;
3116         }
3117
3118         my $upn_array = ["$env->{REALM}.upn"];
3119         my $spn_array = ["$env->{REALM}.spn"];
3120
3121         $self->setup_namespaces($env, $upn_array, $spn_array);
3122
3123         return $env;
3124 }
3125
3126 # Set up a DC testenv solely by using the 'samba-tool domain backup offline' and
3127 # restore commands. This proves that we do an offline backup of a local DC
3128 # ('backupfromdc') and use the backup file to create a valid, working samba DC.
3129 sub setup_offlinebackupdc
3130 {
3131         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3132         my ($self, $prefix, $dcvars) = @_;
3133         print "Preparing OFFLINE BACKUP DC...\n";
3134
3135         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "offlinebackupdc",
3136                                                     $dcvars->{DOMAIN},
3137                                                     $dcvars->{REALM},
3138                                                     $dcvars->{PASSWORD}, "");
3139
3140         # create an offline backup of the 'backupfromdc' target
3141         my $backupdir = File::Temp->newdir();
3142         my $cmd = "offline -s $dcvars->{SERVERCONFFILE}";
3143         my $backup_file = $self->create_backup($env, $dcvars,
3144                                                $backupdir, $cmd);
3145
3146         unless($backup_file) {
3147                 return undef;
3148         }
3149
3150         # restore the backup file to populate the rename-DC testenv
3151         my $restore_dir = abs_path($prefix);
3152         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3153         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3154                                              $restore_dir, $env->{SERVERCONFFILE});
3155         unless ($ret == 0) {
3156                 return undef;
3157         }
3158
3159         # re-create the testenv's krb5.conf (the restore may have overwritten it)
3160         Samba::mk_krb5_conf($ctx);
3161
3162         # start samba for the restored DC
3163         if (not defined($self->check_or_start($env, "standard"))) {
3164             return undef;
3165         }
3166
3167         return $env;
3168 }
3169
3170 # Set up a DC testenv solely by using the samba-tool 'domain backup rename' and
3171 # restore commands, using the --no-secrets option. This proves that we can
3172 # create a realistic lab environment from an online DC ('backupfromdc').
3173 sub setup_labdc
3174 {
3175         # note: dcvars contains the env info for the dependent testenv ('backupfromdc')
3176         my ($self, $prefix, $dcvars) = @_;
3177         print "Preparing LAB-DOMAIN DC...\n";
3178
3179         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, "labdc",
3180                                                     "LABDOMAIN",
3181                                                     "labdom.samba.example.com",
3182                                                     $dcvars->{PASSWORD}, "");
3183
3184         # create a backup of the 'backupfromdc' which renames the domain and uses
3185         # the --no-secrets option to scrub any sensitive info
3186         my $backupdir = File::Temp->newdir();
3187         my $server_args = $self->get_backup_server_args($dcvars);
3188         my $backup_args = "rename $env->{DOMAIN} $env->{REALM} $server_args";
3189         $backup_args .= " --no-secrets --backend-store=mdb";
3190         my $backup_file = $self->create_backup($env, $dcvars, $backupdir,
3191                                                $backup_args);
3192         unless($backup_file) {
3193                 return undef;
3194         }
3195
3196         # restore the backup file to populate the lab-DC testenv
3197         my $restore_dir = abs_path($prefix);
3198         my $restore_opts =  "--newservername=$env->{SERVER} --host-ip=$env->{SERVER_IP}";
3199         my $ret = $self->restore_backup_file($backup_file, $restore_opts,
3200                                              $restore_dir, $env->{SERVERCONFFILE});
3201         unless ($ret == 0) {
3202                 return undef;
3203         }
3204
3205         # because we don't include any secrets in the backup, we need to reset the
3206         # admin user's password back to what the testenv expects
3207         my $samba_tool = Samba::bindir_path($self, "samba-tool");
3208         my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
3209         $cmd .= "--newpassword=$env->{PASSWORD} -H $restore_dir/private/sam.ldb";
3210         $cmd .= " $env->{CONFIGURATION}";
3211
3212         unless(system($cmd) == 0) {
3213                 warn("Failed to reset admin's password: \n$cmd");
3214                 return undef;
3215         }
3216
3217         # start samba for the restored DC
3218         if (not defined($self->check_or_start($env, "standard"))) {
3219             return undef;
3220         }
3221
3222         my $upn_array = ["$env->{REALM}.upn"];
3223         my $spn_array = ["$env->{REALM}.spn"];
3224
3225         $self->setup_namespaces($env, $upn_array, $spn_array);
3226
3227         return $env;
3228 }
3229
3230 # Inspects a backup *.tar.bz2 file and determines the realm/domain it contains
3231 sub get_backup_domain_realm
3232 {
3233         my ($self, $backup_file) = @_;
3234
3235         print "Determining REALM/DOMAIN values in backup...\n";
3236
3237         # The backup will have the correct domain/realm values in the smb.conf.
3238         # So we can work out the env variables the testenv should use based on
3239         # that. Let's start by extracting the smb.conf
3240         my $tar = Archive::Tar->new($backup_file);
3241         my $tmpdir = File::Temp->newdir();
3242         my $smbconf = "$tmpdir/smb.conf";
3243
3244         # note that the filepaths within the tar-file differ slightly for online
3245         # and offline backups
3246         if ($tar->contains_file("etc/smb.conf")) {
3247                 $tar->extract_file("etc/smb.conf", $smbconf);
3248         } elsif ($tar->contains_file("./etc/smb.conf")) {
3249                 $tar->extract_file("./etc/smb.conf", $smbconf);
3250         } else {
3251                 warn("Could not find smb.conf in $backup_file");
3252                 return undef, undef;
3253         }
3254
3255         # now use testparm to read the values we're interested in
3256         my $testparm = Samba::bindir_path($self, "testparm");
3257         my $domain = `$testparm $smbconf -sl --parameter-name=WORKGROUP`;
3258         my $realm = `$testparm $smbconf -sl --parameter-name=REALM`;
3259         chomp $realm;
3260         chomp $domain;
3261         print "Backup-file REALM is $realm, DOMAIN is $domain\n";
3262
3263         return ($domain, $realm);
3264 }
3265
3266 # This spins up a custom testenv that can be based on any backup-file you want.
3267 # This is just intended for manual testing (rather than automated test-cases)
3268 sub setup_customdc
3269 {
3270         my ($self, $prefix) = @_;
3271         print "Preparing CUSTOM RESTORE DC...\n";
3272         my $dc_name = "customdc";
3273         my $password = "locDCpass1";
3274         my $backup_file = $ENV{'BACKUP_FILE'};
3275
3276         # user must specify a backup file to restore via an ENV variable, i.e.
3277         # BACKUP_FILE=backup-blah.tar.bz2 SELFTEST_TESTENV=customdc make testenv
3278         if (not defined($backup_file)) {
3279                 warn("Please specify BACKUP_FILE");
3280                 return undef;
3281         }
3282
3283         # work out the correct domain/realm env values from the backup-file
3284         my ($domain, $realm) = $self->get_backup_domain_realm($backup_file);
3285
3286         # create a placeholder directory and smb.conf, as well as the env vars.
3287         my ($env, $ctx) = $self->prepare_dc_testenv($prefix, $dc_name,
3288                                                     $domain, $realm, $password, "");
3289
3290         # restore the specified backup file to populate the testenv
3291         my $restore_dir = abs_path($prefix);
3292         my $ret = $self->restore_backup_file($backup_file,
3293                                              "--newservername=$env->{SERVER}",
3294                                              $restore_dir, $env->{SERVERCONFFILE});
3295         unless ($ret == 0) {
3296                 return undef;
3297         }
3298
3299         # Change the admin password to the testenv default, just in case it's
3300         # different, or in case this was a --no-secrets backup
3301         my $samba_tool = Samba::bindir_path($self, "samba-tool");
3302         my $cmd = "$samba_tool user setpassword $env->{USERNAME} ";
3303         $cmd .= "--newpassword=$password -H $restore_dir/private/sam.ldb";
3304         $cmd .= " $env->{CONFIGURATION}";
3305
3306         unless(system($cmd) == 0) {
3307                 warn("Failed to reset admin's password: \n$cmd");
3308                 return undef;
3309         }
3310
3311         # re-create the testenv's krb5.conf (the restore may have overwritten it,
3312         # if the backup-file was an offline backup)
3313         Samba::mk_krb5_conf($ctx);
3314
3315         # start samba for the restored DC
3316         if (not defined($self->check_or_start($env, "standard"))) {
3317             return undef;
3318         }
3319
3320         # if this was a backup-rename, then we may need to setup namespaces
3321         my $upn_array = ["$env->{REALM}.upn"];
3322         my $spn_array = ["$env->{REALM}.spn"];
3323
3324         $self->setup_namespaces($env, $upn_array, $spn_array);
3325
3326         return $env;
3327 }
3328
3329 sub setup_none
3330 {
3331         my ($self, $path) = @_;
3332
3333         my $ret = {
3334                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
3335                 SAMBA_PID => -1,
3336         }
3337 }
3338
3339 1;