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