selftest: s!addc.samba.example.com!addom.samba.example.com!
[samba.git] / selftest / target / Samba4.pm
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
5
6 package Samba4;
7
8 use strict;
9 use Cwd qw(abs_path);
10 use FindBin qw($RealBin);
11 use POSIX;
12 use SocketWrapper;
13 use target::Samba;
14 use target::Samba3;
15
16 sub new($$$$$) {
17         my ($classname, $bindir, $ldap, $srcdir, $server_maxtime) = @_;
18
19         my $self = {
20                 vars => {},
21                 ldap => $ldap,
22                 bindir => $bindir,
23                 srcdir => $srcdir,
24                 server_maxtime => $server_maxtime,
25                 target3 => new Samba3($bindir, $srcdir, $server_maxtime)
26         };
27         bless $self;
28         return $self;
29 }
30
31 sub scriptdir_path($$) {
32         my ($self, $path) = @_;
33         return "$self->{srcdir}/source4/scripting/$path";
34 }
35
36 sub openldap_start($$$) {
37 }
38
39 sub slapd_start($$)
40 {
41         my $count = 0;
42         my ($self, $env_vars, $STDIN_READER) = @_;
43         my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
44
45         my $uri = $env_vars->{LDAP_URI};
46
47         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
48             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
49             return 1;
50         }
51         # running slapd in the background means it stays in the same process group, so it can be
52         # killed by timelimit
53         my $pid = fork();
54         if ($pid == 0) {
55                 open STDOUT, ">$env_vars->{LDAPDIR}/logs";
56                 open STDERR, '>&STDOUT';
57                 close($env_vars->{STDIN_PIPE});
58                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
59
60                 if ($self->{ldap} eq "fedora-ds") {
61                         exec("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd", "-D", $env_vars->{FEDORA_DS_DIR}, "-d0", "-i", $env_vars->{FEDORA_DS_PIDFILE});
62                 } elsif ($self->{ldap} eq "openldap") {
63                         exec($ENV{OPENLDAP_SLAPD}, "-dnone", "-F", $env_vars->{SLAPD_CONF_D}, "-h", $uri);
64                 }
65                 die("Unable to start slapd: $!");
66         }
67         $env_vars->{SLAPD_PID} = $pid;
68         sleep(1);
69         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
70                 $count++;
71                 if ($count > 40) {
72                         $self->slapd_stop($env_vars);
73                         return 0;
74                 }
75                 sleep(1);
76         }
77         return 1;
78 }
79
80 sub slapd_stop($$)
81 {
82         my ($self, $envvars) = @_;
83         kill 9, $envvars->{SLAPD_PID};
84         return 1;
85 }
86
87 sub check_or_start($$$)
88 {
89         my ($self, $env_vars, $process_model) = @_;
90         my $STDIN_READER;
91
92         my $env_ok = $self->check_env($env_vars);
93         if ($env_ok) {
94             return $env_vars->{SAMBA_PID};
95         }
96
97         # use a pipe for stdin in the child processes. This allows
98         # those processes to monitor the pipe for EOF to ensure they
99         # exit when the test script exits
100         pipe($STDIN_READER, $env_vars->{STDIN_PIPE});
101
102         # Start slapd before samba, but with the fifo on stdin
103         if (defined($self->{ldap})) {
104                 unless($self->slapd_start($env_vars, $STDIN_READER)) {
105                         warn("couldn't start slapd (main run)");
106                         return undef;
107                 }
108         }
109
110         print "STARTING SAMBA...";
111         my $pid = fork();
112         if ($pid == 0) {
113                 # we want out from samba to go to the log file, but also
114                 # to the users terminal when running 'make test' on the command
115                 # line. This puts it on stderr on the terminal
116                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
117                 open STDERR, '>&STDOUT';
118
119                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
120
121                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
122                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
123                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
124
125                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
126                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
127                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
128                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
129                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
130
131                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
132                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
133                 } else {
134                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
135                 }
136
137                 $ENV{UID_WRAPPER} = "1";
138                 $ENV{UID_WRAPPER_ROOT} = "1";
139
140                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "samba");
141                 my @preargs = ();
142                 my @optargs = ();
143                 if (defined($ENV{SAMBA_OPTIONS})) {
144                         @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
145                 }
146                 if(defined($ENV{SAMBA_VALGRIND})) {
147                         @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
148                 }
149
150                 close($env_vars->{STDIN_PIPE});
151                 open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
152
153                 exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
154         }
155         $env_vars->{SAMBA_PID} = $pid;
156         print "DONE ($pid)\n";
157
158         close($STDIN_READER);
159
160         if ($self->wait_for_start($env_vars) != 0) {
161             warn("Samba $pid failed to start up");
162             return undef;
163         }
164
165         return $pid;
166 }
167
168 sub wait_for_start($$)
169 {
170         my ($self, $testenv_vars) = @_;
171         my $ret = 0;
172
173         if (not $self->check_env($testenv_vars)) {
174             warn("unable to confirm Samba $testenv_vars->{SAMBA_PID} is running");
175             return -1;
176         }
177
178         # give time for nbt server to register its names
179         print "delaying for nbt name registration\n";
180         sleep 2;
181
182         # This will return quickly when things are up, but be slow if we
183         # need to wait for (eg) SSL init
184         my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
185         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
186         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
187         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
188         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
189         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
190         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
191         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
192         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
193         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
194         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
195         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
196         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
197
198         # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
199         if ($testenv_vars->{SERVER_ROLE} eq "domain controller" and not ($testenv_vars->{NETBIOSNAME} eq "RODC")) {
200             # Add hosts file for name lookups
201             $ENV{NSS_WRAPPER_HOSTS} = $testenv_vars->{NSS_WRAPPER_HOSTS};
202                 if (defined($testenv_vars->{RESOLV_WRAPPER_CONF})) {
203                         $ENV{RESOLV_WRAPPER_CONF} = $testenv_vars->{RESOLV_WRAPPER_CONF};
204                 } else {
205                         $ENV{RESOLV_WRAPPER_HOSTS} = $testenv_vars->{RESOLV_WRAPPER_HOSTS};
206                 }
207
208             print "waiting for working LDAP and a RID Set to be allocated\n";
209             my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
210             my $count = 0;
211             my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
212             my $rid_set_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
213             sleep(1);
214             while (system("$ldbsearch -H ldap://$testenv_vars->{SERVER} -U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} -s base -b \"$rid_set_dn\" rIDAllocationPool > /dev/null") != 0) {
215                 $count++;
216                 if ($count > 40) {
217                     $ret = -1;
218                     last;
219                 }
220                 sleep(1);
221             }
222         }
223         print $self->getlog_env($testenv_vars);
224
225         return $ret
226 }
227
228 sub write_ldb_file($$$)
229 {
230         my ($self, $file, $ldif) = @_;
231
232         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
233         open(LDIF, "|$ldbadd -H $file >/dev/null");
234         print LDIF $ldif;
235         return(close(LDIF));
236 }
237
238 sub add_wins_config($$)
239 {
240         my ($self, $privatedir) = @_;
241
242         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
243 dn: name=TORTURE_11,CN=PARTNERS
244 objectClass: wreplPartner
245 name: TORTURE_11
246 address: 127.0.0.11
247 pullInterval: 0
248 pushChangeCount: 0
249 type: 0x3
250 ");
251 }
252
253 sub mk_fedora_ds($$)
254 {
255         my ($self, $ctx) = @_;
256
257         #Make the subdirectory be as fedora DS would expect
258         my $fedora_ds_dir = "$ctx->{ldapdir}/slapd-$ctx->{ldap_instance}";
259
260         my $pidfile = "$fedora_ds_dir/logs/slapd-$ctx->{ldap_instance}.pid";
261
262         return ($fedora_ds_dir, $pidfile);
263 }
264
265 sub mk_openldap($$)
266 {
267         my ($self, $ctx) = @_;
268
269         my $slapd_conf_d = "$ctx->{ldapdir}/slapd.d";
270         my $pidfile = "$ctx->{ldapdir}/slapd.pid";
271
272         return ($slapd_conf_d, $pidfile);
273 }
274
275 sub mk_keyblobs($$)
276 {
277         my ($self, $tlsdir) = @_;
278
279         #TLS and PKINIT crypto blobs
280         my $dhfile = "$tlsdir/dhparms.pem";
281         my $cafile = "$tlsdir/ca.pem";
282         my $certfile = "$tlsdir/cert.pem";
283         my $reqkdc = "$tlsdir/req-kdc.der";
284         my $kdccertfile = "$tlsdir/kdc.pem";
285         my $keyfile = "$tlsdir/key.pem";
286         my $adminkeyfile = "$tlsdir/adminkey.pem";
287         my $reqadmin = "$tlsdir/req-admin.der";
288         my $admincertfile = "$tlsdir/admincert.pem";
289         my $admincertupnfile = "$tlsdir/admincertupn.pem";
290
291         mkdir($tlsdir, 0700);
292         my $oldumask = umask;
293         umask 0077;
294
295         #This is specified here to avoid draining entropy on every run
296         open(DHFILE, ">$dhfile");
297         print DHFILE <<EOF;
298 -----BEGIN DH PARAMETERS-----
299 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
300 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
301 YX70obsCAQI=
302 -----END DH PARAMETERS-----
303 EOF
304         close(DHFILE);
305
306         #Likewise, we pregenerate the key material.  This allows the
307         #other certificates to be pre-generated
308         open(KEYFILE, ">$keyfile");
309         print KEYFILE <<EOF;
310 -----BEGIN RSA PRIVATE KEY-----
311 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
312 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
313 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
314 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
315 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
316 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
317 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
318 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
319 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
320 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
321 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
322 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
323 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
324 -----END RSA PRIVATE KEY-----
325 EOF
326         close(KEYFILE);
327
328         open(ADMINKEYFILE, ">$adminkeyfile");
329
330         print ADMINKEYFILE <<EOF;
331 -----BEGIN RSA PRIVATE KEY-----
332 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
333 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
334 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
335 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
336 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
337 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
338 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
339 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
340 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
341 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
342 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
343 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
344 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
345 -----END RSA PRIVATE KEY-----
346 EOF
347
348         #generated with
349         # hxtool issue-certificate --self-signed --issue-ca \
350         # --ca-private-key="FILE:$KEYFILE" \
351         # --subject="CN=CA,DC=samba,DC=example,DC=com" \
352         # --certificate="FILE:$CAFILE" --lifetime="25 years"
353
354         open(CAFILE, ">$cafile");
355         print CAFILE <<EOF;
356 -----BEGIN CERTIFICATE-----
357 MIICcTCCAdqgAwIBAgIUaBPmjnPVqyFqR5foICmLmikJTzgwCwYJKoZIhvcNAQEFMFIxEzAR
358 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
359 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTIyMzEyWhgPMjAzMzAyMjQx
360 MjIzMTJaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
361 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
362 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
363 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
364 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo0IwQDAOBgNVHQ8BAf8EBAMC
365 AaYwHQYDVR0OBBYEFMLZufegDKLZs0VOyFXYK1L6M8oyMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
366 KoZIhvcNAQEFBQADgYEAAZJbCAAkaqgFJ0xgNovn8Ydd0KswQPjicwiODPgw9ZPoD2HiOUVO
367 yYDRg/dhFF9y656OpcHk4N7qZ2sl3RlHkzDu+dseETW+CnKvQIoXNyeARRJSsSlwrwcoD4JR
368 HTLk2sGigsWwrJ2N99sG/cqSJLJ1MFwLrs6koweBnYU0f/g=
369 -----END CERTIFICATE-----
370 EOF
371
372         #generated with GNUTLS internally in Samba.
373
374         open(CERTFILE, ">$certfile");
375         print CERTFILE <<EOF;
376 -----BEGIN CERTIFICATE-----
377 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
378 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
379 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
380 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
381 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
382 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
383 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
384 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
385 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
386 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
387 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
388 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
389 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
390 -----END CERTIFICATE-----
391 EOF
392         close(CERTFILE);
393
394         #KDC certificate
395         # hxtool request-create \
396         # --subject="CN=krbtgt,CN=users,DC=samba,DC=example,DC=com" \
397         # --key="FILE:$KEYFILE" $KDCREQ
398
399         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
400         # --type="pkinit-kdc" \
401         # --pk-init-principal="krbtgt/SAMBA.EXAMPLE.COM@SAMBA.EXAMPLE.COM" \
402         # --req="PKCS10:$KDCREQ" --certificate="FILE:$KDCCERTFILE" \
403         # --lifetime="25 years"
404
405         open(KDCCERTFILE, ">$kdccertfile");
406         print KDCCERTFILE <<EOF;
407 -----BEGIN CERTIFICATE-----
408 MIIDDDCCAnWgAwIBAgIUI2Tzj+JnMzMcdeabcNo30rovzFAwCwYJKoZIhvcNAQEFMFIxEzAR
409 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
410 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTMxOTIzWhgPMjAzMzAyMjQx
411 MzE5MjNaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
412 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
413 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
414 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
415 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
416 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
417 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
418 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
419 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
420 AQUFAAOBgQBmrVD5MCmZjfHp1nEnHqTIh8r7lSmVtDx4s9MMjxm9oNrzbKXynvdhwQYFVarc
421 ge4yRRDXtSebErOl71zVJI9CVeQQpwcH+tA85oGA7oeFtO/S7ls581RUU6tGgyxV4veD+lJv
422 KPH5LevUtgD+q9H4LU4Sq5N3iFwBaeryB0g2wg==
423 -----END CERTIFICATE-----
424 EOF
425
426         # hxtool request-create \
427         # --subject="CN=Administrator,CN=users,DC=samba,DC=example,DC=com" \
428         # --key="FILE:$ADMINKEYFILE" $ADMINREQFILE
429
430         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
431         # --type="pkinit-client" \
432         # --pk-init-principal="administrator@SAMBA.EXAMPLE.COM" \
433         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE" \
434         # --lifetime="25 years"
435         
436         open(ADMINCERTFILE, ">$admincertfile");
437         print ADMINCERTFILE <<EOF;
438 -----BEGIN CERTIFICATE-----
439 MIIDHTCCAoagAwIBAgIUUggzW4lLRkMKe1DAR2NKatkMDYwwCwYJKoZIhvcNAQELMFIxEzAR
440 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
441 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMjE1WhgPMjAzNDA3MjIw
442 MzMyMTVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
443 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
444 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
445 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
446 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
447 l3JRlwIDAQABo4HSMIHPMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
448 BgEFBQcDAgYKKwYBBAGCNxQCAjBIBgNVHREEQTA/oD0GBisGAQUCAqAzMDGgExsRU0FNQkEu
449 RVhBTVBMRS5DT02hGjAYoAMCAQGhETAPGw1BZG1pbmlzdHJhdG9yMB8GA1UdIwQYMBaAFMLZ
450 ufegDKLZs0VOyFXYK1L6M8oyMB0GA1UdDgQWBBQg81bLyfCA88C2B/BDjXlGuaFaxjAJBgNV
451 HRMEAjAAMA0GCSqGSIb3DQEBCwUAA4GBAEf/OSHUDJaGdtWGNuJeqcVYVMwrfBAc0OSwVhz1
452 7/xqKHWo8wIMPkYRtaRHKLNDsF8GkhQPCpVsa6mX/Nt7YQnNvwd+1SBP5E8GvwWw9ZzLJvma
453 nk2n89emuayLpVtp00PymrDLRBcNaRjFReQU8f0o509kiVPHduAp3jOiy13l
454 -----END CERTIFICATE-----
455 EOF
456         close(ADMINCERTFILE);
457
458         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
459         # --type="pkinit-client" \
460         # --ms-upn="administrator@samba.example.com" \
461         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTUPNFILE" \
462         # --lifetime="25 years"
463         
464         open(ADMINCERTUPNFILE, ">$admincertupnfile");
465         print ADMINCERTUPNFILE <<EOF;
466 -----BEGIN CERTIFICATE-----
467 MIIDDzCCAnigAwIBAgIUUp3CJMuNaEaAdPKp3QdNIwG7a4wwCwYJKoZIhvcNAQELMFIxEzAR
468 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
469 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMzA1WhgPMjAzNDA3MjIw
470 MzMzMDVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
471 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
472 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
473 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
474 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
475 l3JRlwIDAQABo4HEMIHBMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
476 BgEFBQcDAgYKKwYBBAGCNxQCAjA6BgNVHREEMzAxoC8GCisGAQQBgjcUAgOgIQwfYWRtaW5p
477 c3RyYXRvckBzYW1iYS5leGFtcGxlLmNvbTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS
478 +jPKMjAdBgNVHQ4EFgQUIPNWy8nwgPPAtgfwQ415RrmhWsYwCQYDVR0TBAIwADANBgkqhkiG
479 9w0BAQsFAAOBgQBk42+egeUB3Ji2PC55fbt3FNKxvmm2xUUFkV9POK/YR9rajKOwk5jtYSeS
480 Zd7J9s//rNFNa7waklFkDaY56+QWTFtdvxfE+KoHaqt6X8u6pqi7p3M4wDKQox+9Dx8yWFyq
481 Wfz/8alZ5aMezCQzXJyIaJsCLeKABosSwHcpAFmxlQ==
482 -----END CERTIFICATE-----
483 EOF
484
485         umask $oldumask;
486 }
487
488 sub setup_namespaces($$:$$)
489 {
490         my ($self, $localenv, $upn_array, $spn_array) = @_;
491
492         @{$upn_array} = [] unless defined($upn_array);
493         my $upn_args = "";
494         foreach my $upn (@{$upn_array}) {
495                 $upn_args .= " --add-upn-suffix=$upn";
496         }
497
498         @{$spn_array} = [] unless defined($spn_array);
499         my $spn_args = "";
500         foreach my $spn (@{$spn_array}) {
501                 $spn_args .= " --add-spn-suffix=$spn";
502         }
503
504         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
505
506         my $cmd_env = "";
507         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
508         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
509                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
510         } else {
511                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
512         }
513         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\"";
514
515         my $cmd_config = " $localenv->{CONFIGURATION}";
516
517         my $namespaces = $cmd_env;
518         $namespaces .= " $samba_tool domain trust namespaces $upn_args $spn_args";
519         $namespaces .= $cmd_config;
520         unless (system($namespaces) == 0) {
521                 warn("Failed to add namespaces \n$namespaces");
522                 return;
523         }
524
525         return;
526 }
527
528 sub setup_trust($$$$$)
529 {
530         my ($self, $localenv, $remoteenv, $type, $extra_args) = @_;
531
532         $localenv->{TRUST_SERVER} = $remoteenv->{SERVER};
533         $localenv->{TRUST_SERVER_IP} = $remoteenv->{SERVER_IP};
534         $localenv->{TRUST_SERVER_IPV6} = $remoteenv->{SERVER_IPV6};
535         $localenv->{TRUST_NETBIOSNAME} = $remoteenv->{NETBIOSNAME};
536         $localenv->{TRUST_USERNAME} = $remoteenv->{USERNAME};
537         $localenv->{TRUST_PASSWORD} = $remoteenv->{PASSWORD};
538         $localenv->{TRUST_DOMAIN} = $remoteenv->{DOMAIN};
539         $localenv->{TRUST_REALM} = $remoteenv->{REALM};
540
541         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
542         # setup the trust
543         my $cmd_env = "";
544         $cmd_env .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$localenv->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
545         if (defined($localenv->{RESOLV_WRAPPER_CONF})) {
546                 $cmd_env .= "RESOLV_WRAPPER_CONF=\"$localenv->{RESOLV_WRAPPER_CONF}\" ";
547         } else {
548                 $cmd_env .= "RESOLV_WRAPPER_HOSTS=\"$localenv->{RESOLV_WRAPPER_HOSTS}\" ";
549         }
550         $cmd_env .= " KRB5_CONFIG=\"$localenv->{KRB5_CONFIG}\"";
551
552         my $cmd_config = " $localenv->{CONFIGURATION}";
553         my $cmd_creds = $cmd_config;
554         $cmd_creds .= " -U$localenv->{TRUST_DOMAIN}\\\\$localenv->{TRUST_USERNAME}\%$localenv->{TRUST_PASSWORD}";
555
556         my $create = $cmd_env;
557         $create .= " $samba_tool domain trust create --type=${type} $localenv->{TRUST_REALM}";
558         $create .= " $extra_args";
559         $create .= $cmd_creds;
560         unless (system($create) == 0) {
561                 warn("Failed to create trust \n$create");
562                 return undef;
563         }
564
565         return $localenv
566 }
567
568 sub provision_raw_prepare($$$$$$$$$$$)
569 {
570         my ($self, $prefix, $server_role, $hostname,
571             $domain, $realm, $functional_level,
572             $password, $kdc_ipv4, $kdc_ipv6) = @_;
573         my $ctx;
574         my $netbiosname = uc($hostname);
575
576         unless(-d $prefix or mkdir($prefix, 0777)) {
577                 warn("Unable to create $prefix");
578                 return undef;
579         }
580         my $prefix_abs = abs_path($prefix);
581
582         die ("prefix=''") if $prefix_abs eq "";
583         die ("prefix='/'") if $prefix_abs eq "/";
584
585         unless (system("rm -rf $prefix_abs/*") == 0) {
586                 warn("Unable to clean up");
587         }
588
589         
590         my $swiface = Samba::get_interface($hostname);
591
592         $ctx->{prefix} = $prefix;
593         $ctx->{prefix_abs} = $prefix_abs;
594
595         $ctx->{server_role} = $server_role;
596         $ctx->{hostname} = $hostname;
597         $ctx->{netbiosname} = $netbiosname;
598         $ctx->{swiface} = $swiface;
599         $ctx->{password} = $password;
600         $ctx->{kdc_ipv4} = $kdc_ipv4;
601         $ctx->{kdc_ipv6} = $kdc_ipv6;
602
603 #
604 # Set smbd log level here.
605 #
606         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
607         $ctx->{username} = "Administrator";
608         $ctx->{domain} = $domain;
609         $ctx->{realm} = uc($realm);
610         $ctx->{dnsname} = lc($realm);
611
612         $ctx->{functional_level} = $functional_level;
613
614         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
615         chomp $unix_name;
616         $ctx->{unix_name} = $unix_name;
617         $ctx->{unix_uid} = $>;
618         my @mygid = split(" ", $();
619         $ctx->{unix_gid} = $mygid[0];
620         $ctx->{unix_gids_str} = $);
621         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
622
623         $ctx->{etcdir} = "$prefix_abs/etc";
624         $ctx->{piddir} = "$prefix_abs/pid";
625         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
626         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
627         $ctx->{privatedir} = "$prefix_abs/private";
628         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
629         $ctx->{lockdir} = "$prefix_abs/lockdir";
630         $ctx->{logdir} = "$prefix_abs/logs";
631         $ctx->{statedir} = "$prefix_abs/statedir";
632         $ctx->{cachedir} = "$prefix_abs/cachedir";
633         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
634         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
635         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
636         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
637         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
638         $ctx->{nsswrap_hosts} = "$ENV{SELFTEST_PREFIX}/hosts";
639         if ($ENV{SAMBA_DNS_FAKING}) {
640                 $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
641                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf} --all-interfaces --use-file=$ctx->{dns_host_file}";
642         } else {
643                 $ctx->{resolv_conf} = "$ctx->{etcdir}/resolv.conf";
644                 $ctx->{samba_dnsupdate} = "$ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate -s $ctx->{smb_conf}";
645         }
646
647         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
648
649         $ctx->{ipv4} = "127.0.0.$swiface";
650         $ctx->{ipv6} = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
651         $ctx->{interfaces} = "$ctx->{ipv4}/8 $ctx->{ipv6}/64";
652
653         push(@{$ctx->{directories}}, $ctx->{privatedir});
654         push(@{$ctx->{directories}}, $ctx->{etcdir});
655         push(@{$ctx->{directories}}, $ctx->{piddir});
656         push(@{$ctx->{directories}}, $ctx->{lockdir});
657         push(@{$ctx->{directories}}, $ctx->{logdir});
658         push(@{$ctx->{directories}}, $ctx->{statedir});
659         push(@{$ctx->{directories}}, $ctx->{cachedir});
660
661         $ctx->{smb_conf_extra_options} = "";
662
663         my @provision_options = ();
664         push (@provision_options, "KRB5_CONFIG=\"$ctx->{krb5_config}\"");
665         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
666         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
667         push (@provision_options, "NSS_WRAPPER_HOSTS=\"$ctx->{nsswrap_hosts}\"");
668         if (defined($ctx->{resolv_conf})) {
669                 push (@provision_options, "RESOLV_WRAPPER_CONF=\"$ctx->{resolv_conf}\"");
670         } else {
671                 push (@provision_options, "RESOLV_WRAPPER_HOSTS=\"$ctx->{dns_host_file}\"");
672         }
673         if (defined($ENV{GDB_PROVISION})) {
674                 push (@provision_options, "gdb --args");
675                 if (!defined($ENV{PYTHON})) {
676                     push (@provision_options, "env");
677                     push (@provision_options, "python");
678                 }
679         }
680         if (defined($ENV{VALGRIND_PROVISION})) {
681                 push (@provision_options, "valgrind");
682                 if (!defined($ENV{PYTHON})) {
683                     push (@provision_options, "env");
684                     push (@provision_options, "python");
685                 }
686         }
687         if (defined($ENV{PYTHON})) {
688                 push (@provision_options, $ENV{PYTHON});
689         }
690         push (@provision_options, Samba::bindir_path($self, "samba-tool"));
691         push (@provision_options, "domain");
692         push (@provision_options, "provision");
693         push (@provision_options, "--configfile=$ctx->{smb_conf}");
694         push (@provision_options, "--host-name=$ctx->{hostname}");
695         push (@provision_options, "--host-ip=$ctx->{ipv4}");
696         push (@provision_options, "--quiet");
697         push (@provision_options, "--domain=$ctx->{domain}");
698         push (@provision_options, "--realm=$ctx->{realm}");
699         push (@provision_options, "--adminpass=$ctx->{password}");
700         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
701         push (@provision_options, "--machinepass=machine$ctx->{password}");
702         push (@provision_options, "--root=$ctx->{unix_name}");
703         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
704         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
705
706         @{$ctx->{provision_options}} = @provision_options;
707
708         return $ctx;
709 }
710
711 #
712 # Step1 creates the basic configuration
713 #
714 sub provision_raw_step1($$)
715 {
716         my ($self, $ctx) = @_;
717
718         mkdir($_, 0777) foreach (@{$ctx->{directories}});
719
720         ##
721         ## lockdir and piddir must be 0755
722         ##
723         chmod 0755, $ctx->{lockdir};
724         chmod 0755, $ctx->{piddir};
725
726         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
727                 warn("can't open $ctx->{smb_conf}$?");
728                 return undef;
729         }
730         print CONFFILE "
731 [global]
732         netbios name = $ctx->{netbiosname}
733         posix:eadb = $ctx->{statedir}/eadb.tdb
734         workgroup = $ctx->{domain}
735         realm = $ctx->{realm}
736         private dir = $ctx->{privatedir}
737         pid directory = $ctx->{piddir}
738         ncalrpc dir = $ctx->{ncalrpcdir}
739         lock dir = $ctx->{lockdir}
740         state directory = $ctx->{statedir}
741         cache directory = $ctx->{cachedir}
742         winbindd socket directory = $ctx->{winbindd_socket_dir}
743         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
744         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
745         winbind separator = /
746         interfaces = $ctx->{interfaces}
747         tls dh params file = $ctx->{tlsdir}/dhparms.pem
748         panic action = $RealBin/gdb_backtrace \%d
749         wins support = yes
750         server role = $ctx->{server_role}
751         server services = +echo +smb -s3fs
752         dcerpc endpoint servers = +winreg +srvsvc
753         notify:inotify = false
754         ldb:nosync = true
755 #We don't want to pass our self-tests if the PAC code is wrong
756         gensec:require_pac = true
757         log file = $ctx->{logdir}/log.\%m
758         log level = $ctx->{server_loglevel}
759         lanman auth = Yes
760         rndc command = true
761         dns update command = $ctx->{samba_dnsupdate}
762         spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
763         dreplsrv:periodic_startup_interval = 0
764         dsdb:schema update allowed = yes
765
766         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
767
768         # remove this again, when our smb2 client library
769         # supports signin on compound related requests
770         server signing = on
771
772         idmap_ldb:use rfc2307=yes
773         winbind enum users = yes
774         winbind enum groups = yes
775 ";
776
777         print CONFFILE "
778
779         # Begin extra options
780         $ctx->{smb_conf_extra_options}
781         # End extra options
782 ";
783         close(CONFFILE);
784
785         $self->mk_keyblobs($ctx->{tlsdir});
786
787         #Default the KDC IP to the server's IP
788         if (not defined($ctx->{kdc_ipv4})) {
789                 $ctx->{kdc_ipv4} = $ctx->{ipv4};
790         }
791         if (not defined($ctx->{kdc_ipv6})) {
792                 $ctx->{kdc_ipv6} = $ctx->{ipv6};
793         }
794
795         Samba::mk_krb5_conf($ctx);
796
797         open(PWD, ">$ctx->{nsswrap_passwd}");
798         if ($ctx->{unix_uid} != 0) {
799                 print PWD "root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false\n";
800         }
801         print PWD "$ctx->{unix_name}:x:$ctx->{unix_uid}:65531:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false\n";
802         print PWD "nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
803 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
804 pdbtest2:x:65532:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
805 pdbtest3:x:65531:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
806 pdbtest4:x:65530:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
807 ";
808         close(PWD);
809         my $uid_rfc2307test = 65533;
810
811         open(GRP, ">$ctx->{nsswrap_group}");
812         if ($ctx->{unix_gid} != 0) {
813                 print GRP "root:x:0:\n";
814         }
815         print GRP "$ctx->{unix_name}:x:$ctx->{unix_gid}:\n";
816         print GRP "wheel:x:10:
817 users:x:65531:
818 nobody:x:65533:
819 nogroup:x:65534:nobody
820 ";
821         close(GRP);
822         my $gid_rfc2307test = 65532;
823
824         my $hostname = lc($ctx->{hostname});
825         open(HOSTS, ">>$ctx->{nsswrap_hosts}");
826         if ($hostname eq "localdc") {
827                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
828                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} $ctx->{dnsname} ${hostname}\n";
829         } else {
830                 print HOSTS "$ctx->{ipv4} ${hostname}.$ctx->{dnsname} ${hostname}\n";
831                 print HOSTS "$ctx->{ipv6} ${hostname}.$ctx->{dnsname} ${hostname}\n";
832         }
833         close(HOSTS);
834
835         if (defined($ctx->{resolv_conf})) {
836                 open(RESOLV_CONF, ">$ctx->{resolv_conf}");
837                 print RESOLV_CONF "nameserver $ctx->{kdc_ipv4}\n";
838                 print RESOLV_CONF "nameserver $ctx->{kdc_ipv6}\n";
839                 close(RESOLV_CONF);
840         }
841
842         my $configuration = "--configfile=$ctx->{smb_conf}";
843
844 #Ensure the config file is valid before we start
845         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
846         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
847                 system("$testparm -v --suppress-prompt $configuration >&2");
848                 warn("Failed to create a valid smb.conf configuration $testparm!");
849                 return undef;
850         }
851         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) {
852                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
853                 return undef;
854         }
855
856         my $ret = {
857                 KRB5_CONFIG => $ctx->{krb5_conf},
858                 PIDDIR => $ctx->{piddir},
859                 SERVER => $ctx->{hostname},
860                 SERVER_IP => $ctx->{ipv4},
861                 SERVER_IPV6 => $ctx->{ipv6},
862                 NETBIOSNAME => $ctx->{netbiosname},
863                 DOMAIN => $ctx->{domain},
864                 USERNAME => $ctx->{username},
865                 REALM => $ctx->{realm},
866                 PASSWORD => $ctx->{password},
867                 LDAPDIR => $ctx->{ldapdir},
868                 LDAP_INSTANCE => $ctx->{ldap_instance},
869                 SELFTEST_WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
870                 NCALRPCDIR => $ctx->{ncalrpcdir},
871                 LOCKDIR => $ctx->{lockdir},
872                 STATEDIR => $ctx->{statedir},
873                 CACHEDIR => $ctx->{cachedir},
874                 PRIVATEDIR => $ctx->{privatedir},
875                 SERVERCONFFILE => $ctx->{smb_conf},
876                 CONFIGURATION => $configuration,
877                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
878                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
879                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
880                 NSS_WRAPPER_HOSTS => $ctx->{nsswrap_hosts},
881                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
882                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
883                 SAMBA_TEST_LOG_POS => 0,
884                 NSS_WRAPPER_MODULE_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
885                 NSS_WRAPPER_MODULE_FN_PREFIX => "winbind",
886                 LOCAL_PATH => $ctx->{share},
887                 UID_RFC2307TEST => $uid_rfc2307test,
888                 GID_RFC2307TEST => $gid_rfc2307test,
889                 SERVER_ROLE => $ctx->{server_role}
890         };
891
892         if (defined($ctx->{resolv_conf})) {
893                 $ret->{RESOLV_WRAPPER_CONF} = $ctx->{resolv_conf};
894         } else {
895                 $ret->{RESOLV_WRAPPER_HOSTS} = $ctx->{dns_host_file};
896         }
897
898         return $ret;
899 }
900
901 #
902 # Step2 runs the provision script
903 #
904 sub provision_raw_step2($$$)
905 {
906         my ($self, $ctx, $ret) = @_;
907
908         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
909         unless (system($provision_cmd) == 0) {
910                 warn("Unable to provision: \n$provision_cmd\n");
911                 return undef;
912         }
913
914         my $testallowed_account = "testallowed";
915         my $samba_tool_cmd = "";
916         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
917         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
918             . " user add --configfile=$ctx->{smb_conf} $testallowed_account $ctx->{password}";
919         unless (system($samba_tool_cmd) == 0) {
920                 warn("Unable to add testallowed user: \n$samba_tool_cmd\n");
921                 return undef;
922         }
923
924         my $ldbmodify = "";
925         $ldbmodify .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
926         $ldbmodify .= Samba::bindir_path($self, "ldbmodify");
927         my $base_dn = "DC=".join(",DC=", split(/\./, $ctx->{realm}));
928
929         if ($ctx->{server_role} ne "domain controller") {
930                 $base_dn = "DC=$ctx->{netbiosname}";
931         }
932
933         my $user_dn = "cn=$testallowed_account,cn=users,$base_dn";
934         $testallowed_account = "testallowed account";
935         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
936         print LDIF "dn: $user_dn
937 changetype: modify
938 replace: samAccountName
939 samAccountName: $testallowed_account
940 -
941 ";
942         close(LDIF);
943
944         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
945         print LDIF "dn: $user_dn
946 changetype: modify
947 replace: userPrincipalName
948 userPrincipalName: testallowed upn\@$ctx->{realm}
949 replace: servicePrincipalName
950 servicePrincipalName: host/testallowed
951 -           
952 ";
953         close(LDIF);
954
955         $samba_tool_cmd = "";
956         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
957         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
958             . " user add --configfile=$ctx->{smb_conf} testdenied $ctx->{password}";
959         unless (system($samba_tool_cmd) == 0) {
960                 warn("Unable to add testdenied user: \n$samba_tool_cmd\n");
961                 return undef;
962         }
963
964         my $user_dn = "cn=testdenied,cn=users,$base_dn";
965         open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
966         print LDIF "dn: $user_dn
967 changetype: modify
968 replace: userPrincipalName
969 userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
970 -           
971 ";
972         close(LDIF);
973
974         $samba_tool_cmd = "";
975         $samba_tool_cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
976         $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
977             . " group addmembers --configfile=$ctx->{smb_conf} 'Allowed RODC Password Replication Group' '$testallowed_account'";
978         unless (system($samba_tool_cmd) == 0) {
979                 warn("Unable to add '$testallowed_account' user to 'Allowed RODC Password Replication Group': \n$samba_tool_cmd\n");
980                 return undef;
981         }
982
983         return $ret;
984 }
985
986 sub provision($$$$$$$$$$)
987 {
988         my ($self, $prefix, $server_role, $hostname,
989             $domain, $realm, $functional_level,
990             $password, $kdc_ipv4, $kdc_ipv6, $extra_smbconf_options, $extra_smbconf_shares,
991             $extra_provision_options) = @_;
992
993         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
994                                                $hostname,
995                                                $domain, $realm, $functional_level,
996                                                $password, $kdc_ipv4, $kdc_ipv6);
997
998         if (defined($extra_provision_options)) {
999                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
1000         } else {
1001                 push (@{$ctx->{provision_options}}, "--use-ntvfs");
1002         }
1003
1004         $ctx->{share} = "$ctx->{prefix_abs}/share";
1005         push(@{$ctx->{directories}}, "$ctx->{share}");
1006         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
1007         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
1008
1009         # precreate directories for printer drivers
1010         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
1011         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
1012         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
1013
1014         my $msdfs = "no";
1015         $msdfs = "yes" if ($server_role eq "domain controller");
1016         $ctx->{smb_conf_extra_options} = "
1017
1018         max xmit = 32K
1019         server max protocol = SMB2
1020         host msdfs = $msdfs
1021         lanman auth = yes
1022         allow nt4 crypto = yes
1023
1024         # fruit:copyfile is a global option
1025         fruit:copyfile = yes
1026
1027         $extra_smbconf_options
1028
1029 [tmp]
1030         path = $ctx->{share}
1031         read only = no
1032         posix:sharedelay = 100000
1033         posix:oplocktimeout = 3
1034         posix:writetimeupdatedelay = 500000
1035
1036 [xcopy_share]
1037         path = $ctx->{share}
1038         read only = no
1039         posix:sharedelay = 100000
1040         posix:oplocktimeout = 3
1041         posix:writetimeupdatedelay = 500000
1042         create mask = 777
1043         force create mode = 777
1044
1045 [posix_share]
1046         path = $ctx->{share}
1047         read only = no
1048         create mask = 0777
1049         force create mode = 0
1050         directory mask = 0777
1051         force directory mode = 0
1052
1053 [test1]
1054         path = $ctx->{share}/test1
1055         read only = no
1056         posix:sharedelay = 100000
1057         posix:oplocktimeout = 3
1058         posix:writetimeupdatedelay = 500000
1059
1060 [test2]
1061         path = $ctx->{share}/test2
1062         read only = no
1063         posix:sharedelay = 100000
1064         posix:oplocktimeout = 3
1065         posix:writetimeupdatedelay = 500000
1066
1067 [cifs]
1068         path = $ctx->{share}/_ignore_cifs_
1069         read only = no
1070         ntvfs handler = cifs
1071         cifs:server = $ctx->{netbiosname}
1072         cifs:share = tmp
1073         cifs:use-s4u2proxy = yes
1074         # There is no username specified here, instead the client is expected
1075         # to log in with kerberos, and the serverwill use delegated credentials.
1076         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1077
1078 [simple]
1079         path = $ctx->{share}
1080         read only = no
1081         ntvfs handler = simple
1082
1083 [sysvol]
1084         path = $ctx->{statedir}/sysvol
1085         read only = no
1086
1087 [netlogon]
1088         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1089         read only = no
1090
1091 [cifsposix]
1092         copy = simple
1093         ntvfs handler = cifsposix
1094
1095 [vfs_fruit]
1096         path = $ctx->{share}
1097         vfs objects = catia fruit streams_xattr acl_xattr
1098         ea support = yes
1099         fruit:ressource = file
1100         fruit:metadata = netatalk
1101         fruit:locking = netatalk
1102         fruit:encoding = native
1103
1104 $extra_smbconf_shares
1105 ";
1106
1107         if (defined($self->{ldap})) {
1108                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
1109                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
1110
1111                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
1112                 $ldap_uri =~ s|/|%2F|g;
1113                 $ldap_uri = "ldapi://$ldap_uri";
1114                 $ctx->{ldap_uri} = $ldap_uri;
1115
1116                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
1117         }
1118
1119         my $ret = $self->provision_raw_step1($ctx);
1120         unless (defined $ret) {
1121                 return undef;
1122         }
1123
1124         if (defined($self->{ldap})) {
1125                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
1126                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
1127                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
1128                 if ($self->{ldap} eq "openldap") {
1129                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
1130                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
1131
1132                 } elsif ($self->{ldap} eq "fedora-ds") {
1133                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
1134                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
1135                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
1136                 }
1137
1138         }
1139
1140         return $self->provision_raw_step2($ctx, $ret);
1141 }
1142
1143 sub provision_s4member($$$)
1144 {
1145         my ($self, $prefix, $dcvars) = @_;
1146         print "PROVISIONING MEMBER...";
1147         my $extra_smb_conf = "
1148         passdb backend = samba_dsdb
1149 winbindd:use external pipes = true
1150
1151 rpc_server:default = external
1152 rpc_server:svcctl = embedded
1153 rpc_server:srvsvc = embedded
1154 rpc_server:eventlog = embedded
1155 rpc_server:ntsvcs = embedded
1156 rpc_server:winreg = embedded
1157 rpc_server:spoolss = embedded
1158 rpc_daemon:spoolssd = embedded
1159 rpc_server:tcpip = no
1160 ";
1161         my $ret = $self->provision($prefix,
1162                                    "member server",
1163                                    "s4member",
1164                                    "SAMBADOMAIN",
1165                                    "samba.example.com",
1166                                    "2008",
1167                                    "locMEMpass3",
1168                                    $dcvars->{SERVER_IP},
1169                                    $dcvars->{SERVER_IPV6},
1170                                    $extra_smb_conf, "", undef);
1171         unless ($ret) {
1172                 return undef;
1173         }
1174
1175         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1176         my $cmd = "";
1177         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1178         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1179                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1180         } else {
1181                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1182         }
1183         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1184         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1185         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1186         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1187
1188         unless (system($cmd) == 0) {
1189                 warn("Join failed\n$cmd");
1190                 return undef;
1191         }
1192
1193         $ret->{MEMBER_SERVER} = $ret->{SERVER};
1194         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
1195         $ret->{MEMBER_SERVER_IPV6} = $ret->{SERVER_IPV6};
1196         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
1197         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
1198         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
1199
1200         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1201         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1202         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1203         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1204         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1205         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1206
1207         return $ret;
1208 }
1209
1210 sub provision_rpc_proxy($$$)
1211 {
1212         my ($self, $prefix, $dcvars) = @_;
1213         print "PROVISIONING RPC PROXY...";
1214
1215         my $extra_smbconf_options = "
1216         passdb backend = samba_dsdb
1217
1218         # rpc_proxy
1219         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
1220         dcerpc endpoint servers = epmapper, remote
1221         dcerpc_remote:interfaces = rpcecho
1222
1223 [cifs_to_dc]
1224         path = /tmp/_ignore_cifs_to_dc_/_none_
1225         read only = no
1226         ntvfs handler = cifs
1227         cifs:server = $dcvars->{SERVER}
1228         cifs:share = cifs
1229         cifs:use-s4u2proxy = yes
1230         # There is no username specified here, instead the client is expected
1231         # to log in with kerberos, and the serverwill use delegated credentials.
1232         # Or the server tries s4u2self/s4u2proxy to impersonate the client
1233
1234 ";
1235
1236         my $ret = $self->provision($prefix,
1237                                    "member server",
1238                                    "localrpcproxy",
1239                                    "SAMBADOMAIN",
1240                                    "samba.example.com",
1241                                    "2008",
1242                                    "locRPCproxypass4",
1243                                    $dcvars->{SERVER_IP},
1244                                    $dcvars->{SERVER_IPV6},
1245                                    $extra_smbconf_options, "", undef);
1246
1247         unless ($ret) {
1248                 return undef;
1249         }
1250
1251         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1252
1253         # The joind runs in the context of the rpc_proxy/member for now
1254         my $cmd = "";
1255         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1256         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1257                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1258         } else {
1259                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1260         }
1261         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1262         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
1263         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1264         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1265
1266         unless (system($cmd) == 0) {
1267                 warn("Join failed\n$cmd");
1268                 return undef;
1269         }
1270
1271         # Setting up delegation runs in the context of the DC for now
1272         $cmd = "";
1273         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1274         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1275         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
1276         $cmd .= " $dcvars->{CONFIGURATION}";
1277         print $cmd;
1278
1279         unless (system($cmd) == 0) {
1280                 warn("Delegation failed\n$cmd");
1281                 return undef;
1282         }
1283
1284         # Setting up delegation runs in the context of the DC for now
1285         $cmd = "";
1286         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1287         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
1288         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
1289         $cmd .= " $dcvars->{CONFIGURATION}";
1290
1291         unless (system($cmd) == 0) {
1292                 warn("Delegation failed\n$cmd");
1293                 return undef;
1294         }
1295
1296         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
1297         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
1298         $ret->{RPC_PROXY_SERVER_IPV6} = $ret->{SERVER_IPV6};
1299         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
1300         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
1301         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
1302
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_promoted_dc($$$)
1314 {
1315         my ($self, $prefix, $dcvars) = @_;
1316         print "PROVISIONING PROMOTED DC...";
1317
1318         # We do this so that we don't run the provision.  That's the job of 'samba-tool domain dcpromo'.
1319         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1320                                                "promotedvdc",
1321                                                "SAMBADOMAIN",
1322                                                "samba.example.com",
1323                                                "2008",
1324                                                $dcvars->{PASSWORD},
1325                                                $dcvars->{SERVER_IP},
1326                                                $dcvars->{SERVER_IPV6});
1327
1328         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1329
1330         $ctx->{smb_conf_extra_options} = "
1331         max xmit = 32K
1332         server max protocol = SMB2
1333
1334 [sysvol]
1335         path = $ctx->{statedir}/sysvol
1336         read only = yes
1337
1338 [netlogon]
1339         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1340         read only = no
1341
1342 ";
1343
1344         my $ret = $self->provision_raw_step1($ctx);
1345         unless ($ret) {
1346                 return undef;
1347         }
1348
1349         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1350         my $cmd = "";
1351         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1352         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1353                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1354         } else {
1355                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1356         }
1357         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1358         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
1359         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1360         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1361
1362         unless (system($cmd) == 0) {
1363                 warn("Join failed\n$cmd");
1364                 return undef;
1365         }
1366
1367         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1368         my $cmd = "";
1369         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1370         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1371         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1372         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1373         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
1374
1375         unless (system($cmd) == 0) {
1376                 warn("Join failed\n$cmd");
1377                 return undef;
1378         }
1379
1380         $ret->{PROMOTED_DC_SERVER} = $ret->{SERVER};
1381         $ret->{PROMOTED_DC_SERVER_IP} = $ret->{SERVER_IP};
1382         $ret->{PROMOTED_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1383         $ret->{PROMOTED_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1384
1385         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1386         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1387         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1388         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1389         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1390         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1391
1392         return $ret;
1393 }
1394
1395 sub provision_vampire_dc($$$)
1396 {
1397         my ($self, $prefix, $dcvars) = @_;
1398         print "PROVISIONING VAMPIRE DC...";
1399
1400         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1401         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1402                                                "localvampiredc",
1403                                                "SAMBADOMAIN",
1404                                                "samba.example.com",
1405                                                "2008",
1406                                                $dcvars->{PASSWORD},
1407                                                $dcvars->{SERVER_IP},
1408                                                $dcvars->{SERVER_IPV6});
1409
1410         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1411
1412         $ctx->{smb_conf_extra_options} = "
1413         max xmit = 32K
1414         server max protocol = SMB2
1415
1416 [sysvol]
1417         path = $ctx->{statedir}/sysvol
1418         read only = yes
1419
1420 [netlogon]
1421         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1422         read only = no
1423
1424 ";
1425
1426         my $ret = $self->provision_raw_step1($ctx);
1427         unless ($ret) {
1428                 return undef;
1429         }
1430
1431         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1432         my $cmd = "";
1433         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1434         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1435                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1436         } else {
1437                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1438         }
1439         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1440         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1441         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1442         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1443
1444         unless (system($cmd) == 0) {
1445                 warn("Join failed\n$cmd");
1446                 return undef;
1447         }
1448
1449         $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1450         $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1451         $ret->{VAMPIRE_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1452         $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1453
1454         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1455         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1456         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1457         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1458         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1459         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1460         $ret->{DC_REALM} = $dcvars->{DC_REALM};
1461
1462         return $ret;
1463 }
1464
1465 sub provision_subdom_dc($$$)
1466 {
1467         my ($self, $prefix, $dcvars) = @_;
1468         print "PROVISIONING SUBDOMAIN DC...";
1469
1470         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1471         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1472                                                "localsubdc",
1473                                                "SAMBASUBDOM",
1474                                                "sub.samba.example.com",
1475                                                "2008",
1476                                                $dcvars->{PASSWORD},
1477                                                undef);
1478
1479         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1480
1481         $ctx->{smb_conf_extra_options} = "
1482         max xmit = 32K
1483         server max protocol = SMB2
1484
1485 [sysvol]
1486         path = $ctx->{statedir}/sysvol
1487         read only = yes
1488
1489 [netlogon]
1490         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1491         read only = no
1492
1493 ";
1494
1495         my $ret = $self->provision_raw_step1($ctx);
1496         unless ($ret) {
1497                 return undef;
1498         }
1499
1500         Samba::mk_krb5_conf($ctx);
1501
1502         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1503         my $cmd = "";
1504         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1505         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1506                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1507         } else {
1508                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1509         }
1510         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1511         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{dnsname} subdomain ";
1512         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1513         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1514         $cmd .= " --adminpass=$ret->{PASSWORD}";
1515
1516         unless (system($cmd) == 0) {
1517                 warn("Join failed\n$cmd");
1518                 return undef;
1519         }
1520
1521         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1522         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1523         $ret->{SUBDOM_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1524         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1525
1526         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1527         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1528         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1529         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1530         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1531         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1532
1533         return $ret;
1534 }
1535
1536 sub provision_ad_dc_ntvfs($$)
1537 {
1538         my ($self, $prefix) = @_;
1539
1540         # We keep the old 'winbind' name here in server services to
1541         # ensure upgrades which used that name still work with the now
1542         # alias.
1543
1544         print "PROVISIONING AD DC (NTVFS)...";
1545         my $extra_conf_options = "netbios aliases = localDC1-a
1546         server services = +winbind -winbindd";
1547         my $ret = $self->provision($prefix,
1548                                    "domain controller",
1549                                    "localdc",
1550                                    "SAMBADOMAIN",
1551                                    "samba.example.com",
1552                                    "2008",
1553                                    "locDCpass1",
1554                                    undef,
1555                                    undef,
1556                                    $extra_conf_options,
1557                                    "",
1558                                    undef);
1559
1560         return undef unless(defined $ret);
1561         unless($self->add_wins_config("$prefix/private")) {
1562                 warn("Unable to add wins configuration");
1563                 return undef;
1564         }
1565         $ret->{NETBIOSALIAS} = "localdc1-a";
1566         $ret->{DC_SERVER} = $ret->{SERVER};
1567         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1568         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1569         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1570         $ret->{DC_USERNAME} = $ret->{USERNAME};
1571         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1572         $ret->{DC_REALM} = $ret->{REALM};
1573
1574         return $ret;
1575 }
1576
1577 sub provision_fl2000dc($$)
1578 {
1579         my ($self, $prefix) = @_;
1580
1581         print "PROVISIONING DC WITH FOREST LEVEL 2000...";
1582         my $ret = $self->provision($prefix,
1583                                    "domain controller",
1584                                    "dc5",
1585                                    "SAMBA2000",
1586                                    "samba2000.example.com",
1587                                    "2000",
1588                                    "locDCpass5",
1589                                    undef,
1590                                    undef,
1591                                    "",
1592                                    "",
1593                                    undef);
1594
1595         unless($self->add_wins_config("$prefix/private")) {
1596                 warn("Unable to add wins configuration");
1597                 return undef;
1598         }
1599
1600         return $ret;
1601 }
1602
1603 sub provision_fl2003dc($$$)
1604 {
1605         my ($self, $prefix, $dcvars) = @_;
1606
1607         print "PROVISIONING DC WITH FOREST LEVEL 2003...";
1608         my $extra_conf_options = "allow dns updates = nonsecure and secure";
1609         my $ret = $self->provision($prefix,
1610                                    "domain controller",
1611                                    "dc6",
1612                                    "SAMBA2003",
1613                                    "samba2003.example.com",
1614                                    "2003",
1615                                    "locDCpass6",
1616                                    undef,
1617                                    undef,
1618                                    $extra_conf_options,
1619                                    "",
1620                                    undef);
1621
1622         unless (defined $ret) {
1623                 return undef;
1624         }
1625
1626         $ret->{DC_SERVER} = $ret->{SERVER};
1627         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1628         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1629         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1630         $ret->{DC_USERNAME} = $ret->{USERNAME};
1631         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1632
1633         my @samba_tool_options;
1634         push (@samba_tool_options, Samba::bindir_path($self, "samba-tool"));
1635         push (@samba_tool_options, "domain");
1636         push (@samba_tool_options, "passwordsettings");
1637         push (@samba_tool_options, "set");
1638         push (@samba_tool_options, "--configfile=$ret->{SERVERCONFFILE}");
1639         push (@samba_tool_options, "--min-pwd-age=0");
1640         push (@samba_tool_options, "--history-length=1");
1641
1642         my $samba_tool_cmd = join(" ", @samba_tool_options);
1643
1644         unless (system($samba_tool_cmd) == 0) {
1645                 warn("Unable to set min password age to 0: \n$samba_tool_cmd\n");
1646                 return undef;
1647         }
1648
1649         return $ret;
1650
1651         unless($self->add_wins_config("$prefix/private")) {
1652                 warn("Unable to add wins configuration");
1653                 return undef;
1654         }
1655
1656         return $ret;
1657 }
1658
1659 sub provision_fl2008r2dc($$$)
1660 {
1661         my ($self, $prefix, $dcvars) = @_;
1662
1663         print "PROVISIONING DC WITH FOREST LEVEL 2008r2...";
1664         my $ret = $self->provision($prefix,
1665                                    "domain controller",
1666                                    "dc7",
1667                                    "SAMBA2008R2",
1668                                    "samba2008R2.example.com",
1669                                    "2008_R2",
1670                                    "locDCpass7",
1671                                    undef,
1672                                    undef,
1673                                    "",
1674                                    "",
1675                                    undef);
1676
1677         unless ($self->add_wins_config("$prefix/private")) {
1678                 warn("Unable to add wins configuration");
1679                 return undef;
1680         }
1681
1682         return $ret;
1683 }
1684
1685
1686 sub provision_rodc($$$)
1687 {
1688         my ($self, $prefix, $dcvars) = @_;
1689         print "PROVISIONING RODC...";
1690
1691         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1692         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1693                                                "rodc",
1694                                                "SAMBADOMAIN",
1695                                                "samba.example.com",
1696                                                "2008",
1697                                                $dcvars->{PASSWORD},
1698                                                $dcvars->{SERVER_IP},
1699                                                $dcvars->{SERVER_IPV6});
1700         unless ($ctx) {
1701                 return undef;
1702         }
1703
1704         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1705
1706         $ctx->{share} = "$ctx->{prefix_abs}/share";
1707         push(@{$ctx->{directories}}, "$ctx->{share}");
1708
1709         $ctx->{smb_conf_extra_options} = "
1710         max xmit = 32K
1711         server max protocol = SMB2
1712
1713 [sysvol]
1714         path = $ctx->{statedir}/sysvol
1715         read only = yes
1716
1717 [netlogon]
1718         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1719         read only = yes
1720
1721 [tmp]
1722         path = $ctx->{share}
1723         read only = no
1724         posix:sharedelay = 10000
1725         posix:oplocktimeout = 3
1726         posix:writetimeupdatedelay = 50000
1727
1728 ";
1729
1730         my $ret = $self->provision_raw_step1($ctx);
1731         unless ($ret) {
1732                 return undef;
1733         }
1734
1735         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1736         my $cmd = "";
1737         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1738         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
1739                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
1740         } else {
1741                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
1742         }
1743         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1744         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1745         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1746         $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
1747
1748         unless (system($cmd) == 0) {
1749                 warn("RODC join failed\n$cmd");
1750                 return undef;
1751         }
1752
1753         # This ensures deterministic behaviour for tests that want to have the 'testallowed account'
1754         # user password verified on the RODC
1755         my $testallowed_account = "testallowed account";
1756         $cmd = "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1757         $cmd .= "$samba_tool rodc preload '$testallowed_account' $ret->{CONFIGURATION}";
1758         $cmd .= " --server=$dcvars->{DC_SERVER}";
1759
1760         unless (system($cmd) == 0) {
1761                 warn("RODC join failed\n$cmd");
1762                 return undef;
1763         }
1764
1765         # we overwrite the kdc after the RODC join
1766         # so that use the RODC as kdc and test
1767         # the proxy code
1768         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1769         $ctx->{kdc_ipv6} = $ret->{SERVER_IPV6};
1770         Samba::mk_krb5_conf($ctx);
1771
1772         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1773         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1774         $ret->{RODC_DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1775         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1776
1777         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1778         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1779         $ret->{DC_SERVER_IPV6} = $dcvars->{DC_SERVER_IPV6};
1780         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1781         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1782         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1783
1784         return $ret;
1785 }
1786
1787 sub provision_ad_dc($$)
1788 {
1789         my ($self, $prefix) = @_;
1790
1791         my $prefix_abs = abs_path($prefix);
1792
1793         my $bindir_abs = abs_path($self->{bindir});
1794         my $lockdir="$prefix_abs/lockdir";
1795         my $conffile="$prefix_abs/etc/smb.conf";
1796
1797         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
1798         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
1799
1800         my $extra_smbconf_options = "
1801         server services = -smb +s3fs
1802         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
1803
1804         dbwrap_tdb_mutexes:* = yes
1805         ${require_mutexes}
1806
1807         kernel oplocks = no
1808         kernel change notify = no
1809
1810         logging = file
1811         printing = bsd
1812         printcap name = /dev/null
1813
1814         max protocol = SMB3
1815         read only = no
1816         server signing = auto
1817
1818         smbd:sharedelay = 100000
1819         smbd:writetimeupdatedelay = 500000
1820         create mask = 755
1821         dos filemode = yes
1822
1823         dcerpc endpoint servers = -winreg -srvsvc
1824
1825         printcap name = /dev/null
1826
1827         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
1828         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
1829
1830         printing = vlp
1831         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1832         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1833         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1834         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1835         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1836         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1837         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1838         lpq cache time = 0
1839         print notify backchannel = yes
1840 ";
1841
1842         my $extra_smbconf_shares = "
1843
1844 [tmpenc]
1845         copy = tmp
1846         smb encrypt = required
1847
1848 [tmpcase]
1849         copy = tmp
1850         case sensitive = yes
1851
1852 [tmpguest]
1853         copy = tmp
1854         guest ok = yes
1855
1856 [hideunread]
1857         copy = tmp
1858         hide unreadable = yes
1859
1860 [durable]
1861         copy = tmp
1862         kernel share modes = no
1863         kernel oplocks = no
1864         posix locking = no
1865
1866 [print\$]
1867         copy = tmp
1868
1869 [print1]
1870         copy = tmp
1871         printable = yes
1872
1873 [print2]
1874         copy = print1
1875 [print3]
1876         copy = print1
1877 [lp]
1878         copy = print1
1879 ";
1880
1881         print "PROVISIONING AD DC...";
1882         my $ret = $self->provision($prefix,
1883                                    "domain controller",
1884                                    "addc",
1885                                    "ADDOMAIN",
1886                                    "addom.samba.example.com",
1887                                    "2008",
1888                                    "locDCpass1",
1889                                    undef,
1890                                    undef,
1891                                    $extra_smbconf_options,
1892                                    $extra_smbconf_shares,
1893                                    undef);
1894
1895         return undef unless(defined $ret);
1896         unless($self->add_wins_config("$prefix/private")) {
1897                 warn("Unable to add wins configuration");
1898                 return undef;
1899         }
1900
1901         $ret->{DC_SERVER} = $ret->{SERVER};
1902         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1903         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1904         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1905         $ret->{DC_USERNAME} = $ret->{USERNAME};
1906         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1907
1908         return $ret;
1909 }
1910
1911 sub provision_chgdcpass($$)
1912 {
1913         my ($self, $prefix) = @_;
1914
1915         print "PROVISIONING CHGDCPASS...";
1916         my $extra_provision_options = undef;
1917         push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
1918         my $ret = $self->provision($prefix,
1919                                    "domain controller",
1920                                    "chgdcpass",
1921                                    "CHDCDOMAIN",
1922                                    "chgdcpassword.samba.example.com",
1923                                    "2008",
1924                                    "chgDCpass1",
1925                                    undef,
1926                                    undef,
1927                                    "",
1928                                    "",
1929                                    $extra_provision_options);
1930
1931         return undef unless(defined $ret);
1932         unless($self->add_wins_config("$prefix/private")) {
1933                 warn("Unable to add wins configuration");
1934                 return undef;
1935         }
1936         
1937         # Remove secrets.tdb from this environment to test that we
1938         # still start up on systems without the new matching
1939         # secrets.tdb records.
1940         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
1941                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
1942                 return undef;
1943         }
1944             
1945         $ret->{DC_SERVER} = $ret->{SERVER};
1946         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1947         $ret->{DC_SERVER_IPV6} = $ret->{SERVER_IPV6};
1948         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1949         $ret->{DC_USERNAME} = $ret->{USERNAME};
1950         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1951
1952         return $ret;
1953 }
1954
1955 sub teardown_env($$)
1956 {
1957         my ($self, $envvars) = @_;
1958         my $pid;
1959
1960         # This should cause samba to terminate gracefully
1961         close($envvars->{STDIN_PIPE});
1962
1963         $pid = $envvars->{SAMBA_PID};
1964         my $count = 0;
1965         my $childpid;
1966
1967         # This should give it time to write out the gcov data
1968         until ($count > 30) {
1969             if (Samba::cleanup_child($pid, "samba") == -1) {
1970                 last;
1971             }
1972             sleep(1);
1973             $count++;
1974         }
1975
1976         if ($count > 30 || kill(0, $pid)) {
1977             kill "TERM", $pid;
1978
1979             until ($count > 40) {
1980                 if (Samba::cleanup_child($pid, "samba") == -1) {
1981                     last;
1982                 }
1983                 sleep(1);
1984                 $count++;
1985             }
1986             # If it is still around, kill it
1987             warn "server process $pid took more than $count seconds to exit, killing\n";
1988             kill 9, $pid;
1989         }
1990
1991         $self->slapd_stop($envvars) if ($self->{ldap});
1992
1993         print $self->getlog_env($envvars);
1994
1995         return;
1996 }
1997
1998 sub getlog_env($$)
1999 {
2000         my ($self, $envvars) = @_;
2001         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
2002         my $out = $title;
2003
2004         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
2005
2006         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
2007         while (<LOG>) {
2008                 $out .= $_;
2009         }
2010         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
2011         close(LOG);
2012
2013         return "" if $out eq $title;
2014
2015         return $out;
2016 }
2017
2018 sub check_env($$)
2019 {
2020         my ($self, $envvars) = @_;
2021         my $samba_pid = $envvars->{SAMBA_PID};
2022
2023         if (not defined($samba_pid)) {
2024             return 0;
2025         } elsif ($samba_pid > 0) {
2026             my $childpid = Samba::cleanup_child($samba_pid, "samba");
2027
2028             if ($childpid == 0) {
2029                 return 1;
2030             }
2031             return 0;
2032         } else {
2033             return 1;
2034         }
2035
2036 }
2037
2038 sub setup_env($$$)
2039 {
2040         my ($self, $envname, $path) = @_;
2041         my $target3 = $self->{target3};
2042
2043         $ENV{ENVNAME} = $envname;
2044
2045         if (defined($self->{vars}->{$envname})) {
2046                 return $self->{vars}->{$envname};
2047         }
2048
2049         if ($envname eq "ad_dc_ntvfs") {
2050                 return $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2051         } elsif ($envname eq "fl2000dc") {
2052                 return $self->setup_fl2000dc("$path/fl2000dc");
2053         } elsif ($envname eq "fl2003dc") {
2054                 if (not defined($self->{vars}->{ad_dc})) {
2055                         $self->setup_ad_dc("$path/ad_dc");
2056                 }
2057                 return $self->setup_fl2003dc("$path/fl2003dc", $self->{vars}->{ad_dc});
2058         } elsif ($envname eq "fl2008r2dc") {
2059                 if (not defined($self->{vars}->{ad_dc})) {
2060                         $self->setup_ad_dc("$path/ad_dc");
2061                 }
2062                 return $self->setup_fl2008r2dc("$path/fl2008r2dc", $self->{vars}->{ad_dc});
2063         } elsif ($envname eq "rpc_proxy") {
2064                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2065                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2066                 }
2067                 return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{ad_dc_ntvfs});
2068         } elsif ($envname eq "vampire_dc") {
2069                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2070                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2071                 }
2072                 return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{ad_dc_ntvfs});
2073         } elsif ($envname eq "promoted_dc") {
2074                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2075                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2076                 }
2077                 return $self->setup_promoted_dc("$path/promoted_dc", $self->{vars}->{ad_dc_ntvfs});
2078         } elsif ($envname eq "subdom_dc") {
2079                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2080                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2081                 }
2082                 return $self->setup_subdom_dc("$path/subdom_dc", $self->{vars}->{ad_dc_ntvfs});
2083         } elsif ($envname eq "s4member") {
2084                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2085                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2086                 }
2087                 return $self->setup_s4member("$path/s4member", $self->{vars}->{ad_dc_ntvfs});
2088         } elsif ($envname eq "rodc") {
2089                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2090                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2091                 }
2092                 return $self->setup_rodc("$path/rodc", $self->{vars}->{ad_dc_ntvfs});
2093         } elsif ($envname eq "chgdcpass") {
2094                 return $self->setup_chgdcpass("$path/chgdcpass", $self->{vars}->{chgdcpass});
2095         } elsif ($envname eq "ad_member") {
2096                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2097                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2098                 }
2099                 return $target3->setup_admember("$path/ad_member", $self->{vars}->{ad_dc_ntvfs}, 29);
2100         } elsif ($envname eq "ad_dc") {
2101                 return $self->setup_ad_dc("$path/ad_dc");
2102         } elsif ($envname eq "ad_dc_no_nss") {
2103                 return $self->setup_ad_dc("$path/ad_dc_no_nss", "no_nss");
2104         } elsif ($envname eq "ad_member_rfc2307") {
2105                 if (not defined($self->{vars}->{ad_dc_ntvfs})) {
2106                         $self->setup_ad_dc_ntvfs("$path/ad_dc_ntvfs");
2107                 }
2108                 return $target3->setup_admember_rfc2307("$path/ad_member_rfc2307",
2109                                                         $self->{vars}->{ad_dc_ntvfs}, 34);
2110         } elsif ($envname eq "none") {
2111                 return $self->setup_none("$path/none");
2112         } else {
2113                 return "UNKNOWN";
2114         }
2115 }
2116
2117 sub setup_s4member($$$)
2118 {
2119         my ($self, $path, $dc_vars) = @_;
2120
2121         my $env = $self->provision_s4member($path, $dc_vars);
2122
2123         if (defined $env) {
2124                 if (not defined($self->check_or_start($env, "standard"))) {
2125                         return undef;
2126                 }
2127
2128                 $self->{vars}->{s4member} = $env;
2129         }
2130
2131         return $env;
2132 }
2133
2134 sub setup_rpc_proxy($$$)
2135 {
2136         my ($self, $path, $dc_vars) = @_;
2137
2138         my $env = $self->provision_rpc_proxy($path, $dc_vars);
2139
2140         if (defined $env) {
2141                 if (not defined($self->check_or_start($env, "standard"))) {
2142                         return undef;
2143                 }
2144
2145                 $self->{vars}->{rpc_proxy} = $env;
2146         }
2147         return $env;
2148 }
2149
2150 sub setup_ad_dc_ntvfs($$)
2151 {
2152         my ($self, $path) = @_;
2153
2154         my $env = $self->provision_ad_dc_ntvfs($path);
2155         if (defined $env) {
2156                 if (not defined($self->check_or_start($env, "standard"))) {
2157                     warn("Failed to start ad_dc_ntvfs");
2158                         return undef;
2159                 }
2160
2161                 $self->{vars}->{ad_dc_ntvfs} = $env;
2162         }
2163         return $env;
2164 }
2165
2166 sub setup_chgdcpass($$)
2167 {
2168         my ($self, $path) = @_;
2169
2170         my $env = $self->provision_chgdcpass($path);
2171         if (defined $env) {
2172                 if (not defined($self->check_or_start($env, "standard"))) {
2173                         return undef;
2174                 }
2175
2176                 $self->{vars}->{chgdcpass} = $env;
2177         }
2178         return $env;
2179 }
2180
2181 sub setup_fl2000dc($$)
2182 {
2183         my ($self, $path) = @_;
2184
2185         my $env = $self->provision_fl2000dc($path);
2186         if (defined $env) {
2187                 if (not defined($self->check_or_start($env, "standard"))) {
2188                         return undef;
2189                 }
2190
2191                 $self->{vars}->{fl2000dc} = $env;
2192         }
2193
2194         return $env;
2195 }
2196
2197 sub setup_fl2003dc($$$)
2198 {
2199         my ($self, $path, $dc_vars) = @_;
2200
2201         my $env = $self->provision_fl2003dc($path);
2202
2203         if (defined $env) {
2204                 if (not defined($self->check_or_start($env, "standard"))) {
2205                         return undef;
2206                 }
2207
2208                 $env = $self->setup_trust($env, $dc_vars, "external", "--no-aes-keys");
2209
2210                 $self->{vars}->{fl2003dc} = $env;
2211         }
2212         return $env;
2213 }
2214
2215 sub setup_fl2008r2dc($$$)
2216 {
2217         my ($self, $path, $dc_vars) = @_;
2218
2219         my $env = $self->provision_fl2008r2dc($path);
2220
2221         if (defined $env) {
2222                 if (not defined($self->check_or_start($env, "standard"))) {
2223                         return undef;
2224                 }
2225
2226                 my $upn_array = ["$env->{REALM}.upn"];
2227                 my $spn_array = ["$env->{REALM}.spn"];
2228
2229                 $self->setup_namespaces($env, $upn_array, $spn_array);
2230
2231                 $env = $self->setup_trust($env, $dc_vars, "forest", "");
2232
2233                 $self->{vars}->{fl2008r2dc} = $env;
2234         }
2235
2236         return $env;
2237 }
2238
2239 sub setup_vampire_dc($$$)
2240 {
2241         my ($self, $path, $dc_vars) = @_;
2242
2243         my $env = $self->provision_vampire_dc($path, $dc_vars);
2244
2245         if (defined $env) {
2246                 if (not defined($self->check_or_start($env, "single"))) {
2247                         return undef;
2248                 }
2249
2250                 $self->{vars}->{vampire_dc} = $env;
2251
2252                 # force replicated DC to update repsTo/repsFrom
2253                 # for vampired partitions
2254                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2255                 my $cmd = "";
2256                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2257                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2258                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2259                 } else {
2260                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2261                 }
2262                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2263                 $cmd .= " $samba_tool drs kcc -k no $env->{DC_SERVER}";
2264                 $cmd .= " $env->{CONFIGURATION}";
2265                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2266                 unless (system($cmd) == 0) {
2267                         warn("Failed to exec kcc\n$cmd");
2268                         return undef;
2269                 }
2270
2271                 # as 'vampired' dc may add data in its local replica
2272                 # we need to synchronize data between DCs
2273                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2274                 $cmd = "";
2275                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
2276                 if (defined($env->{RESOLV_WRAPPER_CONF})) {
2277                         $cmd .= "RESOLV_WRAPPER_CONF=\"$env->{RESOLV_WRAPPER_CONF}\" ";
2278                 } else {
2279                         $cmd .= "RESOLV_WRAPPER_HOSTS=\"$env->{RESOLV_WRAPPER_HOSTS}\" ";
2280                 }
2281                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2282                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2283                 $cmd .= " $dc_vars->{CONFIGURATION}";
2284                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2285                 # replicate Configuration NC
2286                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2287                 unless(system($cmd_repl) == 0) {
2288                         warn("Failed to replicate\n$cmd_repl");
2289                         return undef;
2290                 }
2291                 # replicate Default NC
2292                 $cmd_repl = "$cmd \"$base_dn\"";
2293                 unless(system($cmd_repl) == 0) {
2294                         warn("Failed to replicate\n$cmd_repl");
2295                         return undef;
2296                 }
2297         }
2298
2299         return $env;
2300 }
2301
2302 sub setup_promoted_dc($$$)
2303 {
2304         my ($self, $path, $dc_vars) = @_;
2305
2306         my $env = $self->provision_promoted_dc($path, $dc_vars);
2307
2308         if (defined $env) {
2309                 if (not defined($self->check_or_start($env, "single"))) {
2310                         return undef;
2311                 }
2312
2313                 $self->{vars}->{promoted_dc} = $env;
2314
2315                 # force source and replicated DC to update repsTo/repsFrom
2316                 # for vampired partitions
2317                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2318                 my $cmd = "";
2319                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2320                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2321                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
2322                 $cmd .= " $env->{CONFIGURATION}";
2323                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2324                 unless (system($cmd) == 0) {
2325                         warn("Failed to exec kcc\n$cmd");
2326                         return undef;
2327                 }
2328
2329                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2330                 my $cmd = "";
2331                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2332                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2333                 $cmd .= " $samba_tool drs kcc $env->{SERVER}";
2334                 $cmd .= " $env->{CONFIGURATION}";
2335                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2336                 unless (system($cmd) == 0) {
2337                         warn("Failed to exec kcc\n$cmd");
2338                         return undef;
2339                 }
2340
2341                 # as 'vampired' dc may add data in its local replica
2342                 # we need to synchronize data between DCs
2343                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2344                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2345                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2346                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
2347                 $cmd .= " $dc_vars->{CONFIGURATION}";
2348                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2349                 # replicate Configuration NC
2350                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2351                 unless(system($cmd_repl) == 0) {
2352                         warn("Failed to replicate\n$cmd_repl");
2353                         return undef;
2354                 }
2355                 # replicate Default NC
2356                 $cmd_repl = "$cmd \"$base_dn\"";
2357                 unless(system($cmd_repl) == 0) {
2358                         warn("Failed to replicate\n$cmd_repl");
2359                         return undef;
2360                 }
2361         }
2362
2363         return $env;
2364 }
2365
2366 sub setup_subdom_dc($$$)
2367 {
2368         my ($self, $path, $dc_vars) = @_;
2369
2370         my $env = $self->provision_subdom_dc($path, $dc_vars);
2371
2372         if (defined $env) {
2373                 if (not defined($self->check_or_start($env, "single"))) {
2374                         return undef;
2375                 }
2376
2377                 $self->{vars}->{subdom_dc} = $env;
2378
2379                 # force replicated DC to update repsTo/repsFrom
2380                 # for primary domain partitions
2381                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2382                 my $cmd = "";
2383                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2384                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2385                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
2386                 $cmd .= " $env->{CONFIGURATION}";
2387                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2388                 unless (system($cmd) == 0) {
2389                         warn("Failed to exec kcc\n$cmd");
2390                         return undef;
2391                 }
2392
2393                 # as 'subdomain' dc may add data in its local replica
2394                 # we need to synchronize data between DCs
2395                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
2396                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2397                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2398                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2399                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
2400                 $cmd .= " $dc_vars->{CONFIGURATION}";
2401                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
2402                 # replicate Configuration NC
2403                 my $cmd_repl = "$cmd \"$config_dn\"";
2404                 unless(system($cmd_repl) == 0) {
2405                         warn("Failed to replicate\n$cmd_repl");
2406                         return undef;
2407                 }
2408                 # replicate Default NC
2409                 $cmd_repl = "$cmd \"$base_dn\"";
2410                 unless(system($cmd_repl) == 0) {
2411                         warn("Failed to replicate\n$cmd_repl");
2412                         return undef;
2413                 }
2414         }
2415
2416         return $env;
2417 }
2418
2419 sub setup_rodc($$$)
2420 {
2421         my ($self, $path, $dc_vars) = @_;
2422
2423         my $env = $self->provision_rodc($path, $dc_vars);
2424
2425         unless ($env) {
2426                 return undef;
2427         }
2428
2429         if (not defined($self->check_or_start($env, "single"))) {
2430             return undef;
2431         }
2432
2433         # force source and replicated DC to update repsTo/repsFrom
2434         # for vampired partitions
2435         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2436         my $cmd = "";
2437         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2438         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2439         $cmd .= " $samba_tool drs kcc -k no $env->{DC_SERVER}";
2440         $cmd .= " $env->{CONFIGURATION}";
2441         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2442         unless (system($cmd) == 0) {
2443             warn("Failed to exec kcc\n$cmd");
2444             return undef;
2445         }
2446
2447         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
2448         my $cmd = "";
2449         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2450         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2451         $cmd .= " $samba_tool drs kcc -k no $env->{SERVER}";
2452         $cmd .= " $env->{CONFIGURATION}";
2453         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2454         unless (system($cmd) == 0) {
2455             warn("Failed to exec kcc\n$cmd");
2456             return undef;
2457         }
2458
2459         my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
2460         $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
2461         $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
2462         $cmd .= " $samba_tool drs replicate $env->{SERVER} $env->{DC_SERVER}";
2463         $cmd .= " $dc_vars->{CONFIGURATION}";
2464         $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
2465         # replicate Configuration NC
2466         my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
2467         unless(system($cmd_repl) == 0) {
2468             warn("Failed to replicate\n$cmd_repl");
2469             return undef;
2470         }
2471         # replicate Default NC
2472         $cmd_repl = "$cmd \"$base_dn\"";
2473         unless(system($cmd_repl) == 0) {
2474             warn("Failed to replicate\n$cmd_repl");
2475             return undef;
2476         }
2477
2478         $self->{vars}->{rodc} = $env;
2479
2480         return $env;
2481 }
2482
2483 sub setup_ad_dc($$)
2484 {
2485         my ($self, $path, $no_nss) = @_;
2486
2487         # If we didn't build with ADS, pretend this env was never available
2488         if (not $self->{target3}->have_ads()) {
2489                return "UNKNOWN";
2490         }
2491
2492         my $env = $self->provision_ad_dc($path);
2493         unless ($env) {
2494                 return undef;
2495         }
2496
2497         if (defined($no_nss) and $no_nss) {
2498                 $env->{NSS_WRAPPER_MODULE_SO_PATH} = undef;
2499                 $env->{NSS_WRAPPER_MODULE_FN_PREFIX} = undef;
2500         }
2501
2502         if (not defined($self->check_or_start($env, "single"))) {
2503             return undef;
2504         }
2505
2506         my $upn_array = ["$env->{REALM}.upn"];
2507         my $spn_array = ["$env->{REALM}.spn"];
2508
2509         $self->setup_namespaces($env, $upn_array, $spn_array);
2510
2511         $self->{vars}->{ad_dc} = $env;
2512         return $env;
2513 }
2514
2515 sub setup_none($$)
2516 {
2517         my ($self, $path) = @_;
2518
2519         my $ret = {
2520                 KRB5_CONFIG => abs_path($path) . "/no_krb5.conf",
2521                 SAMBA_PID => -1,
2522         }
2523 }
2524
2525 1;