selftest: Allow a krb5.conf to be generated that covers multiple realms
[amitay/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, $exeext, $server_maxtime) = @_;
18         $exeext = "" unless defined($exeext);
19
20         my $self = {
21                 vars => {},
22                 ldap => $ldap,
23                 bindir => $bindir,
24                 binary_mapping => $binary_mapping,
25                 srcdir => $srcdir,
26                 exeext => $exeext,
27                 server_maxtime => $server_maxtime,
28                 target3 => new Samba3($bindir, $binary_mapping, $srcdir, $exeext, $server_maxtime)
29         };
30         bless $self;
31         return $self;
32 }
33
34 sub scriptdir_path($$) {
35         my ($self, $path) = @_;
36         return "$self->{srcdir}/source4/scripting/$path";
37 }
38
39 sub openldap_start($$$) {
40 }
41
42 sub slapd_start($$)
43 {
44         my $count = 0;
45         my ($self, $env_vars) = @_;
46         my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
47
48         my $uri = $env_vars->{LDAP_URI};
49
50         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
51             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
52             return 1;
53         }
54         # running slapd in the background means it stays in the same process group, so it can be
55         # killed by timelimit
56         if ($self->{ldap} eq "fedora-ds") {
57                 system("$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd -D $env_vars->{FEDORA_DS_DIR} -d0 -i $env_vars->{FEDORA_DS_PIDFILE}> $env_vars->{LDAPDIR}/logs 2>&1 &");
58         } elsif ($self->{ldap} eq "openldap") {
59                 system("$ENV{OPENLDAP_SLAPD} -d0 -F $env_vars->{SLAPD_CONF_D} -h $uri > $env_vars->{LDAPDIR}/logs 2>&1 &");
60         }
61         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
62                 $count++;
63                 if ($count > 40) {
64                         $self->slapd_stop($env_vars);
65                         return 0;
66                 }
67                 sleep(1);
68         }
69         return 1;
70 }
71
72 sub slapd_stop($$)
73 {
74         my ($self, $envvars) = @_;
75         if ($self->{ldap} eq "fedora-ds") {
76                 system("$envvars->{LDAPDIR}/slapd-$envvars->{LDAP_INSTANCE}/stop-slapd");
77         } elsif ($self->{ldap} eq "openldap") {
78                 unless (open(IN, "<$envvars->{OPENLDAP_PIDFILE}")) {
79                         warn("unable to open slapd pid file: $envvars->{OPENLDAP_PIDFILE}");
80                         return 0;
81                 }
82                 kill 9, <IN>;
83                 close(IN);
84         }
85         return 1;
86 }
87
88 sub check_or_start($$$)
89 {
90         my ($self, $env_vars, $process_model) = @_;
91         return 0 if ( -p $env_vars->{SAMBA_TEST_FIFO});
92
93         unlink($env_vars->{SAMBA_TEST_FIFO});
94         POSIX::mkfifo($env_vars->{SAMBA_TEST_FIFO}, 0700);
95         unlink($env_vars->{SAMBA_TEST_LOG});
96         
97         my $pwd = `pwd`;
98         print "STARTING SAMBA for $ENV{ENVNAME}\n";
99         my $pid = fork();
100         if ($pid == 0) {
101                 open STDIN, $env_vars->{SAMBA_TEST_FIFO};
102                 # we want out from samba to go to the log file, but also
103                 # to the users terminal when running 'make test' on the command
104                 # line. This puts it on stderr on the terminal
105                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
106                 open STDERR, '>&STDOUT';
107
108                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
109
110                 my $valgrind = "";
111                 if (defined($ENV{SAMBA_VALGRIND})) {
112                     $valgrind = $ENV{SAMBA_VALGRIND};
113                 }
114
115                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
116                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
117
118                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
119                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
120
121                 $ENV{UID_WRAPPER} = "1";
122
123                 # Start slapd before samba, but with the fifo on stdin
124                 if (defined($self->{ldap})) {
125                         unless($self->slapd_start($env_vars)) {
126                                 warn("couldn't start slapd (main run)");
127                                 return undef;
128                         }
129                 }
130
131                 my $optarg = "";
132                 $optarg = "--maximum-runtime=$self->{server_maxtime}";
133                 if (defined($ENV{SAMBA_OPTIONS})) {
134                         $optarg.= " $ENV{SAMBA_OPTIONS}";
135                 }
136                 my $samba =  Samba::bindir_path($self, "samba");
137
138                 chomp($pwd);
139                 my $cmdline = "$valgrind ${pwd}/$samba $optarg $env_vars->{CONFIGURATION} -M $process_model -i";
140                 my $ret = system("$cmdline");
141                 if ($ret == -1) {
142                         print "Unable to start $cmdline: $ret: $!\n";
143                         exit 1;
144                 }
145                 my $exit = ($ret >> 8);
146                 unlink($env_vars->{SAMBA_TEST_FIFO});
147                 if ($ret == 0) {
148                         print "$samba exited with no error\n";
149                         exit 0;
150                 } elsif ( $ret & 127 ) {
151                         print "$samba got signal ".($ret & 127)." and exits with $exit!\n";
152                 } else {
153                         print "$samba failed with status $exit!\n";
154                 }
155                 if ($exit == 0) {
156                         $exit = -1;
157                 }
158                 exit $exit;
159         }
160         print "DONE\n";
161
162         open(DATA, ">$env_vars->{SAMBA_TEST_FIFO}");
163
164         return $pid;
165 }
166
167 sub wait_for_start($$)
168 {
169         my ($self, $testenv_vars) = @_;
170         # give time for nbt server to register its names
171         print "delaying for nbt name registration\n";
172         sleep 2;
173
174         # This will return quickly when things are up, but be slow if we
175         # need to wait for (eg) SSL init
176         my $nmblookup =  Samba::bindir_path($self, "nmblookup");
177         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
178         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
179         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
180         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
181         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
182         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
183         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
184         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
185         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
186         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
187         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
188         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
189
190         print $self->getlog_env($testenv_vars);
191 }
192
193 sub write_ldb_file($$$)
194 {
195         my ($self, $file, $ldif) = @_;
196
197         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
198         open(LDIF, "|$ldbadd -H $file >/dev/null");
199         print LDIF $ldif;
200         return(close(LDIF));
201 }
202
203 sub add_wins_config($$)
204 {
205         my ($self, $privatedir) = @_;
206
207         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
208 dn: name=TORTURE_11,CN=PARTNERS
209 objectClass: wreplPartner
210 name: TORTURE_11
211 address: 127.0.0.11
212 pullInterval: 0
213 pushChangeCount: 0
214 type: 0x3
215 ");
216 }
217
218 sub mk_fedora_ds($$)
219 {
220         my ($self, $ctx) = @_;
221
222         #Make the subdirectory be as fedora DS would expect
223         my $fedora_ds_dir = "$ctx->{ldapdir}/slapd-$ctx->{ldap_instance}";
224
225         my $pidfile = "$fedora_ds_dir/logs/slapd-$ctx->{ldap_instance}.pid";
226
227         return ($fedora_ds_dir, $pidfile);
228 }
229
230 sub mk_openldap($$)
231 {
232         my ($self, $ctx) = @_;
233
234         my $slapd_conf_d = "$ctx->{ldapdir}/slapd.d";
235         my $pidfile = "$ctx->{ldapdir}/slapd.pid";
236
237         return ($slapd_conf_d, $pidfile);
238 }
239
240 sub mk_keyblobs($$)
241 {
242         my ($self, $tlsdir) = @_;
243
244         #TLS and PKINIT crypto blobs
245         my $dhfile = "$tlsdir/dhparms.pem";
246         my $cafile = "$tlsdir/ca.pem";
247         my $certfile = "$tlsdir/cert.pem";
248         my $reqkdc = "$tlsdir/req-kdc.der";
249         my $kdccertfile = "$tlsdir/kdc.pem";
250         my $keyfile = "$tlsdir/key.pem";
251         my $adminkeyfile = "$tlsdir/adminkey.pem";
252         my $reqadmin = "$tlsdir/req-admin.der";
253         my $admincertfile = "$tlsdir/admincert.pem";
254         my $admincertupnfile = "$tlsdir/admincertupn.pem";
255
256         mkdir($tlsdir, 0777);
257
258         #This is specified here to avoid draining entropy on every run
259         open(DHFILE, ">$dhfile");
260         print DHFILE <<EOF;
261 -----BEGIN DH PARAMETERS-----
262 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
263 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
264 YX70obsCAQI=
265 -----END DH PARAMETERS-----
266 EOF
267         close(DHFILE);
268
269         #Likewise, we pregenerate the key material.  This allows the
270         #other certificates to be pre-generated
271         open(KEYFILE, ">$keyfile");
272         print KEYFILE <<EOF;
273 -----BEGIN RSA PRIVATE KEY-----
274 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
275 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
276 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
277 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
278 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
279 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
280 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
281 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
282 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
283 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
284 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
285 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
286 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
287 -----END RSA PRIVATE KEY-----
288 EOF
289         close(KEYFILE);
290
291         open(ADMINKEYFILE, ">$adminkeyfile");
292
293         print ADMINKEYFILE <<EOF;
294 -----BEGIN RSA PRIVATE KEY-----
295 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
296 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
297 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
298 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
299 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
300 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
301 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
302 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
303 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
304 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
305 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
306 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
307 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
308 -----END RSA PRIVATE KEY-----
309 EOF
310
311         #generated with
312         # hxtool issue-certificate --self-signed --issue-ca \
313         # --ca-private-key="FILE:$KEYFILE" \
314         # --subject="CN=CA,DC=samba,DC=example,DC=com" \
315         # --certificate="FILE:$CAFILE" --lifetime="25 years"
316
317         open(CAFILE, ">$cafile");
318         print CAFILE <<EOF;
319 -----BEGIN CERTIFICATE-----
320 MIICcTCCAdqgAwIBAgIUaBPmjnPVqyFqR5foICmLmikJTzgwCwYJKoZIhvcNAQEFMFIxEzAR
321 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
322 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTIyMzEyWhgPMjAzMzAyMjQx
323 MjIzMTJaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
324 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
325 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
326 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
327 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo0IwQDAOBgNVHQ8BAf8EBAMC
328 AaYwHQYDVR0OBBYEFMLZufegDKLZs0VOyFXYK1L6M8oyMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
329 KoZIhvcNAQEFBQADgYEAAZJbCAAkaqgFJ0xgNovn8Ydd0KswQPjicwiODPgw9ZPoD2HiOUVO
330 yYDRg/dhFF9y656OpcHk4N7qZ2sl3RlHkzDu+dseETW+CnKvQIoXNyeARRJSsSlwrwcoD4JR
331 HTLk2sGigsWwrJ2N99sG/cqSJLJ1MFwLrs6koweBnYU0f/g=
332 -----END CERTIFICATE-----
333 EOF
334
335         #generated with GNUTLS internally in Samba.
336
337         open(CERTFILE, ">$certfile");
338         print CERTFILE <<EOF;
339 -----BEGIN CERTIFICATE-----
340 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
341 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
342 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
343 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
344 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
345 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
346 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
347 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
348 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
349 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
350 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
351 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
352 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
353 -----END CERTIFICATE-----
354 EOF
355         close(CERTFILE);
356
357         #KDC certificate
358         # hxtool request-create \
359         # --subject="CN=krbtgt,CN=users,DC=samba,DC=example,DC=com" \
360         # --key="FILE:$KEYFILE" $KDCREQ
361
362         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
363         # --type="pkinit-kdc" \
364         # --pk-init-principal="krbtgt/SAMBA.EXAMPLE.COM@SAMBA.EXAMPLE.COM" \
365         # --req="PKCS10:$KDCREQ" --certificate="FILE:$KDCCERTFILE" \
366         # --lifetime="25 years"
367
368         open(KDCCERTFILE, ">$kdccertfile");
369         print KDCCERTFILE <<EOF;
370 -----BEGIN CERTIFICATE-----
371 MIIDDDCCAnWgAwIBAgIUI2Tzj+JnMzMcdeabcNo30rovzFAwCwYJKoZIhvcNAQEFMFIxEzAR
372 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
373 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTMxOTIzWhgPMjAzMzAyMjQx
374 MzE5MjNaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
375 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
376 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
377 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
378 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
379 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
380 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
381 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
382 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
383 AQUFAAOBgQBmrVD5MCmZjfHp1nEnHqTIh8r7lSmVtDx4s9MMjxm9oNrzbKXynvdhwQYFVarc
384 ge4yRRDXtSebErOl71zVJI9CVeQQpwcH+tA85oGA7oeFtO/S7ls581RUU6tGgyxV4veD+lJv
385 KPH5LevUtgD+q9H4LU4Sq5N3iFwBaeryB0g2wg==
386 -----END CERTIFICATE-----
387 EOF
388
389         # hxtool request-create \
390         # --subject="CN=Administrator,CN=users,DC=samba,DC=example,DC=com" \
391         # --key="FILE:$ADMINKEYFILE" $ADMINREQFILE
392
393         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
394         # --type="pkinit-client" \
395         # --pk-init-principal="administrator@SAMBA.EXAMPLE.COM" \
396         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE" \
397         # --lifetime="25 years"
398         
399         open(ADMINCERTFILE, ">$admincertfile");
400         print ADMINCERTFILE <<EOF;
401 -----BEGIN CERTIFICATE-----
402 MIIDHTCCAoagAwIBAgIUUggzW4lLRkMKe1DAR2NKatkMDYwwCwYJKoZIhvcNAQELMFIxEzAR
403 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
404 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMjE1WhgPMjAzNDA3MjIw
405 MzMyMTVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
406 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
407 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
408 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
409 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
410 l3JRlwIDAQABo4HSMIHPMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
411 BgEFBQcDAgYKKwYBBAGCNxQCAjBIBgNVHREEQTA/oD0GBisGAQUCAqAzMDGgExsRU0FNQkEu
412 RVhBTVBMRS5DT02hGjAYoAMCAQGhETAPGw1BZG1pbmlzdHJhdG9yMB8GA1UdIwQYMBaAFMLZ
413 ufegDKLZs0VOyFXYK1L6M8oyMB0GA1UdDgQWBBQg81bLyfCA88C2B/BDjXlGuaFaxjAJBgNV
414 HRMEAjAAMA0GCSqGSIb3DQEBCwUAA4GBAEf/OSHUDJaGdtWGNuJeqcVYVMwrfBAc0OSwVhz1
415 7/xqKHWo8wIMPkYRtaRHKLNDsF8GkhQPCpVsa6mX/Nt7YQnNvwd+1SBP5E8GvwWw9ZzLJvma
416 nk2n89emuayLpVtp00PymrDLRBcNaRjFReQU8f0o509kiVPHduAp3jOiy13l
417 -----END CERTIFICATE-----
418 EOF
419         close(ADMINCERTFILE);
420
421         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
422         # --type="pkinit-client" \
423         # --ms-upn="administrator@samba.example.com" \
424         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTUPNFILE" \
425         # --lifetime="25 years"
426         
427         open(ADMINCERTUPNFILE, ">$admincertupnfile");
428         print ADMINCERTUPNFILE <<EOF;
429 -----BEGIN CERTIFICATE-----
430 MIIDDzCCAnigAwIBAgIUUp3CJMuNaEaAdPKp3QdNIwG7a4wwCwYJKoZIhvcNAQELMFIxEzAR
431 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
432 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMzA1WhgPMjAzNDA3MjIw
433 MzMzMDVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
434 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
435 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
436 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
437 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
438 l3JRlwIDAQABo4HEMIHBMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
439 BgEFBQcDAgYKKwYBBAGCNxQCAjA6BgNVHREEMzAxoC8GCisGAQQBgjcUAgOgIQwfYWRtaW5p
440 c3RyYXRvckBzYW1iYS5leGFtcGxlLmNvbTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS
441 +jPKMjAdBgNVHQ4EFgQUIPNWy8nwgPPAtgfwQ415RrmhWsYwCQYDVR0TBAIwADANBgkqhkiG
442 9w0BAQsFAAOBgQBk42+egeUB3Ji2PC55fbt3FNKxvmm2xUUFkV9POK/YR9rajKOwk5jtYSeS
443 Zd7J9s//rNFNa7waklFkDaY56+QWTFtdvxfE+KoHaqt6X8u6pqi7p3M4wDKQox+9Dx8yWFyq
444 Wfz/8alZ5aMezCQzXJyIaJsCLeKABosSwHcpAFmxlQ==
445 -----END CERTIFICATE-----
446 EOF
447 }
448
449 sub provision_raw_prepare($$$$$$$$$$)
450 {
451         my ($self, $prefix, $server_role, $hostname,
452             $domain, $realm, $functional_level,
453             $swiface, $password, $kdc_ipv4) = @_;
454         my $ctx;
455         my $netbiosname = uc($hostname);
456
457         unless(-d $prefix or mkdir($prefix, 0777)) {
458                 warn("Unable to create $prefix");
459                 return undef;
460         }
461         my $prefix_abs = abs_path($prefix);
462
463         die ("prefix=''") if $prefix_abs eq "";
464         die ("prefix='/'") if $prefix_abs eq "/";
465
466         unless (system("rm -rf $prefix_abs/*") == 0) {
467                 warn("Unable to clean up");
468         }
469
470         $ctx->{prefix} = $prefix;
471         $ctx->{prefix_abs} = $prefix_abs;
472         
473         $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
474
475         $ctx->{server_role} = $server_role;
476         $ctx->{hostname} = $hostname;
477         $ctx->{netbiosname} = $netbiosname;
478         $ctx->{swiface} = $swiface;
479         $ctx->{password} = $password;
480         $ctx->{kdc_ipv4} = $kdc_ipv4;
481
482         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
483         $ctx->{username} = "Administrator";
484         $ctx->{domain} = $domain;
485         $ctx->{realm} = uc($realm);
486         $ctx->{dnsname} = lc($realm);
487         $ctx->{sid_generator} = "internal";
488
489         $ctx->{functional_level} = $functional_level;
490
491         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
492         chomp $unix_name;
493         $ctx->{unix_name} = $unix_name;
494         $ctx->{unix_uid} = $>;
495         $ctx->{unix_gids_str} = $);
496         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
497
498         $ctx->{etcdir} = "$prefix_abs/etc";
499         $ctx->{piddir} = "$prefix_abs/pid";
500         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
501         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
502         $ctx->{privatedir} = "$prefix_abs/private";
503         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
504         $ctx->{lockdir} = "$prefix_abs/lockdir";
505         $ctx->{statedir} = "$prefix_abs/statedir";
506         $ctx->{cachedir} = "$prefix_abs/cachedir";
507         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
508         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
509         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
510         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
511         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
512
513         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
514
515         $ctx->{ipv4} = "127.0.0.$swiface";
516         $ctx->{interfaces} = "$ctx->{ipv4}/8";
517
518         push(@{$ctx->{directories}}, $ctx->{privatedir});
519         push(@{$ctx->{directories}}, $ctx->{etcdir});
520         push(@{$ctx->{directories}}, $ctx->{piddir});
521         push(@{$ctx->{directories}}, $ctx->{lockdir});
522         push(@{$ctx->{directories}}, $ctx->{statedir});
523         push(@{$ctx->{directories}}, $ctx->{cachedir});
524
525         $ctx->{smb_conf_extra_options} = "";
526
527         my @provision_options = ();
528         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
529         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
530         if (defined($ENV{GDB_PROVISION})) {
531                 push (@provision_options, "gdb --args");
532                 if (!defined($ENV{PYTHON})) {
533                     push (@provision_options, "env");
534                     push (@provision_options, "python");
535                 }
536         }
537         if (defined($ENV{VALGRIND_PROVISION})) {
538                 push (@provision_options, "valgrind");
539                 if (!defined($ENV{PYTHON})) {
540                     push (@provision_options, "env");
541                     push (@provision_options, "python");
542                 }
543         }
544         if (defined($ENV{PYTHON})) {
545                 push (@provision_options, $ENV{PYTHON});
546         }
547         push (@provision_options, "$self->{srcdir}/source4/setup/provision");
548         push (@provision_options, "--configfile=$ctx->{smb_conf}");
549         push (@provision_options, "--host-name=$ctx->{hostname}");
550         push (@provision_options, "--host-ip=$ctx->{ipv4}");
551         push (@provision_options, "--quiet");
552         push (@provision_options, "--domain=$ctx->{domain}");
553         push (@provision_options, "--realm=$ctx->{realm}");
554         push (@provision_options, "--adminpass=$ctx->{password}");
555         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
556         push (@provision_options, "--machinepass=machine$ctx->{password}");
557         push (@provision_options, "--root=$ctx->{unix_name}");
558         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
559         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
560
561         @{$ctx->{provision_options}} = @provision_options;
562
563         return $ctx;
564 }
565
566 #
567 # Step1 creates the basic configuration
568 #
569 sub provision_raw_step1($$)
570 {
571         my ($self, $ctx) = @_;
572
573         mkdir($_, 0777) foreach (@{$ctx->{directories}});
574
575         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
576                 warn("can't open $ctx->{smb_conf}$?");
577                 return undef;
578         }
579         my $acl = "false";
580         $acl = "true" if (defined $ENV{WITH_ACL});
581         print CONFFILE "
582 [global]
583         acl:search = $acl
584         netbios name = $ctx->{netbiosname}
585         posix:eadb = $ctx->{statedir}/eadb.tdb
586         workgroup = $ctx->{domain}
587         realm = $ctx->{realm}
588         private dir = $ctx->{privatedir}
589         pid directory = $ctx->{piddir}
590         ncalrpc dir = $ctx->{ncalrpcdir}
591         lock dir = $ctx->{lockdir}
592         state directory = $ctx->{statedir}
593         cache directory = $ctx->{cachedir}
594         winbindd socket directory = $ctx->{winbindd_socket_dir}
595         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
596         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
597         winbind separator = /
598         name resolve order = file bcast
599         interfaces = $ctx->{interfaces}
600         tls dh params file = $ctx->{tlsdir}/dhparms.pem
601         panic action = $RealBin/gdb_backtrace \%d
602         wins support = yes
603         server role = $ctx->{server_role}
604         server services = +echo
605         notify:inotify = false
606         ldb:nosync = true
607 #We don't want to pass our self-tests if the PAC code is wrong
608         gensec:require_pac = true
609         log level = $ctx->{server_loglevel}
610         lanman auth = Yes
611         rndc command = true
612         dns update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$ctx->{dns_host_file}
613         spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate
614         resolv:host file = $ctx->{dns_host_file}
615         dreplsrv:periodic_startup_interval = 0
616
617         passdb backend = samba4
618 ";
619
620         if (defined($ctx->{sid_generator}) && $ctx->{sid_generator} ne "internal") {
621                 print CONFFILE "
622         sid generator = $ctx->{sid_generator}";
623         }
624
625         print CONFFILE "
626
627         # Begin extra options
628         $ctx->{smb_conf_extra_options}
629         # End extra options
630 ";
631         close(CONFFILE);
632
633         $self->mk_keyblobs($ctx->{tlsdir});
634
635         #Default the KDC IP to the server's IP
636         if (not defined($ctx->{kdc_ipv4})) {
637              $ctx->{kdc_ipv4} = $ctx->{ipv4};
638         }
639
640         Samba::mk_krb5_conf($ctx, "");
641
642         open(PWD, ">$ctx->{nsswrap_passwd}");
643         print PWD "
644 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
645 $ctx->{unix_name}:x:$ctx->{unix_uid}:@{$ctx->{unix_gids}}[0]:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false
646 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
647 ";
648         close(PWD);
649
650         open(GRP, ">$ctx->{nsswrap_group}");
651         print GRP "
652 root:x:0:
653 wheel:x:10:
654 users:x:100:
655 nobody:x:65533:
656 nogroup:x:65534:nobody
657 ";
658         close(GRP);
659
660         my $configuration = "--configfile=$ctx->{smb_conf}";
661
662 #Ensure the config file is valid before we start
663         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
664         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
665                 system("$testparm -v --suppress-prompt $configuration >&2");
666                 warn("Failed to create a valid smb.conf configuration $testparm!");
667                 return undef;
668         }
669         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) {
670                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
671                 return undef;
672         }
673
674         my $ret = {
675                 KRB5_CONFIG => $ctx->{krb5_conf},
676                 PIDDIR => $ctx->{piddir},
677                 SERVER => $ctx->{hostname},
678                 SERVER_IP => $ctx->{ipv4},
679                 NETBIOSNAME => $ctx->{netbiosname},
680                 DOMAIN => $ctx->{domain},
681                 USERNAME => $ctx->{username},
682                 REALM => $ctx->{realm},
683                 PASSWORD => $ctx->{password},
684                 LDAPDIR => $ctx->{ldapdir},
685                 LDAP_INSTANCE => $ctx->{ldap_instance},
686                 WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
687                 NCALRPCDIR => $ctx->{ncalrpcdir},
688                 LOCKDIR => $ctx->{lockdir},
689                 STATEDIR => $ctx->{statedir},
690                 CACHEDIR => $ctx->{cachedir},
691                 SERVERCONFFILE => $ctx->{smb_conf},
692                 CONFIGURATION => $configuration,
693                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
694                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
695                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
696                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
697                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
698                 SAMBA_TEST_LOG_POS => 0,
699         };
700
701         return $ret;
702 }
703
704 #
705 # Step2 runs the provision script
706 #
707 sub provision_raw_step2($$$)
708 {
709         my ($self, $ctx, $ret) = @_;
710
711         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
712         unless (system($provision_cmd) == 0) {
713                 warn("Unable to provision: \n$provision_cmd\n");
714                 return undef;
715         }
716
717         return $ret;
718 }
719
720 sub provision($$$$$$$$$)
721 {
722         my ($self, $prefix, $server_role, $hostname,
723             $domain, $realm, $functional_level,
724             $swiface, $password, $kdc_ipv4, $extra_smbconf_options) = @_;
725
726         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
727                                                $hostname,
728                                                $domain, $realm, $functional_level,
729                                                $swiface, $password, $kdc_ipv4);
730
731         $ctx->{tmpdir} = "$ctx->{prefix_abs}/tmp";
732         push(@{$ctx->{directories}}, "$ctx->{tmpdir}");
733         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test1");
734         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test2");
735         my $msdfs = "no";
736         $msdfs = "yes" if ($server_role eq "domain controller");
737         $ctx->{smb_conf_extra_options} = "
738
739         max xmit = 32K
740         server max protocol = SMB2
741         host msdfs = $msdfs
742         lanman auth = yes
743
744         $extra_smbconf_options
745
746 [tmp]
747         path = $ctx->{tmpdir}
748         read only = no
749         posix:sharedelay = 10000
750         posix:oplocktimeout = 3
751         posix:writetimeupdatedelay = 500000
752
753 [test1]
754         path = $ctx->{tmpdir}/test1
755         read only = no
756         posix:sharedelay = 10000
757         posix:oplocktimeout = 3
758         posix:writetimeupdatedelay = 50000
759
760 [test2]
761         path = $ctx->{tmpdir}/test2
762         read only = no
763         posix:sharedelay = 10000
764         posix:oplocktimeout = 3
765         posix:writetimeupdatedelay = 50000
766
767 [cifs]
768         read only = no
769         ntvfs handler = cifs
770         cifs:server = $ctx->{netbiosname}
771         cifs:share = tmp
772         cifs:use-s4u2proxy = yes
773         # There is no username specified here, instead the client is expected
774         # to log in with kerberos, and the serverwill use delegated credentials.
775         # Or the server tries s4u2self/s4u2proxy to impersonate the client
776
777 [simple]
778         path = $ctx->{tmpdir}
779         read only = no
780         ntvfs handler = simple
781
782 [sysvol]
783         path = $ctx->{statedir}/sysvol
784         read only = yes
785
786 [netlogon]
787         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
788         read only = no
789
790 [cifsposix]
791         copy = simple
792         ntvfs handler = cifsposix
793 ";
794
795         if (defined($self->{ldap})) {
796                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
797                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
798
799                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
800                 $ldap_uri =~ s|/|%2F|g;
801                 $ldap_uri = "ldapi://$ldap_uri";
802                 $ctx->{ldap_uri} = $ldap_uri;
803                 if ($self->{ldap} eq "fedora-ds") {
804                         $ctx->{sid_generator} = "backend";
805                 }
806
807                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
808         }
809
810         my $ret = $self->provision_raw_step1($ctx);
811         unless (defined $ret) {
812                 return undef;
813         }
814
815         if (defined($self->{ldap})) {
816                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
817                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
818                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
819                 if ($self->{ldap} eq "openldap") {
820                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
821                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
822
823                 } elsif ($self->{ldap} eq "fedora-ds") {
824                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
825                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
826                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
827                 }
828
829         }
830
831         return $self->provision_raw_step2($ctx, $ret);
832 }
833
834 sub provision_member($$$)
835 {
836         my ($self, $prefix, $dcvars) = @_;
837         print "PROVISIONING MEMBER...";
838
839         my $ret = $self->provision($prefix,
840                                    "member server",
841                                    "s4member",
842                                    "SAMBADOMAIN",
843                                    "samba.example.com",
844                                    "2008",
845                                    23,
846                                    "locMEMpass3",
847                                    $dcvars->{SERVER_IP},
848                                    "");
849         unless ($ret) {
850                 return undef;
851         }
852
853         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
854         my $cmd = "";
855         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
856         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
857         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
858         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
859
860         unless (system($cmd) == 0) {
861                 warn("Join failed\n$cmd");
862                 return undef;
863         }
864
865         $ret->{MEMBER_SERVER} = $ret->{SERVER};
866         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
867         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
868         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
869         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
870
871         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
872         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
873         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
874         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
875         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
876
877         return $ret;
878 }
879
880 sub provision_rpc_proxy($$$)
881 {
882         my ($self, $prefix, $dcvars) = @_;
883         print "PROVISIONING RPC PROXY...";
884
885         my $extra_smbconf_options = "
886
887         # rpc_proxy
888         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
889         dcerpc endpoint servers = epmapper, remote
890         dcerpc_remote:interfaces = rpcecho
891
892 [cifs_to_dc]
893         read only = no
894         ntvfs handler = cifs
895         cifs:server = $dcvars->{SERVER}
896         cifs:share = cifs
897         cifs:use-s4u2proxy = yes
898         # There is no username specified here, instead the client is expected
899         # to log in with kerberos, and the serverwill use delegated credentials.
900         # Or the server tries s4u2self/s4u2proxy to impersonate the client
901
902 ";
903
904         my $ret = $self->provision($prefix,
905                                    "member server",
906                                    "localrpcproxy",
907                                    "SAMBADOMAIN",
908                                    "samba.example.com",
909                                    "2008",
910                                    24,
911                                    "locRPCproxypass4",
912                                    $dcvars->{SERVER_IP},
913                                    $extra_smbconf_options);
914
915         unless ($ret) {
916                 return undef;
917         }
918
919         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
920
921         # The joind runs in the context of the rpc_proxy/member for now
922         my $cmd = "";
923         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
924         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
925         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
926         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
927
928         unless (system($cmd) == 0) {
929                 warn("Join failed\n$cmd");
930                 return undef;
931         }
932
933         # Setting up delegation runs in the context of the DC for now
934         $cmd = "";
935         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
936         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
937         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
938         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
939
940         unless (system($cmd) == 0) {
941                 warn("Delegation failed\n$cmd");
942                 return undef;
943         }
944
945         # Setting up delegation runs in the context of the DC for now
946         $cmd = "";
947         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
948         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
949         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
950         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
951
952         unless (system($cmd) == 0) {
953                 warn("Delegation failed\n$cmd");
954                 return undef;
955         }
956
957         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
958         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
959         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
960         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
961         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
962
963         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
964         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
965         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
966         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
967         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
968
969         return $ret;
970 }
971
972 sub provision_vampire_dc($$$)
973 {
974         my ($self, $prefix, $dcvars) = @_;
975         print "PROVISIONING VAMPIRE DC...";
976
977         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
978         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
979                                                "localvampiredc",
980                                                "SAMBADOMAIN",
981                                                "samba.example.com",
982                                                "2008",
983                                                22, $dcvars->{PASSWORD},
984                                                $dcvars->{SERVER_IP});
985
986         $ctx->{smb_conf_extra_options} = "
987         max xmit = 32K
988         server max protocol = SMB2
989
990 [sysvol]
991         path = $ctx->{statedir}/sysvol
992         read only = yes
993
994 [netlogon]
995         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
996         read only = no
997
998 ";
999
1000         my $ret = $self->provision_raw_step1($ctx);
1001         unless ($ret) {
1002                 return undef;
1003         }
1004
1005         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1006         my $cmd = "";
1007         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1008         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1009         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1010         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1011
1012         unless (system($cmd) == 0) {
1013                 warn("Join failed\n$cmd");
1014                 return undef;
1015         }
1016
1017         $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1018         $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1019         $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1020
1021         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1022         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1023         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1024         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1025         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1026
1027         return $ret;
1028 }
1029
1030 sub provision_dc($$)
1031 {
1032         my ($self, $prefix) = @_;
1033
1034         print "PROVISIONING DC...";
1035         my $ret = $self->provision($prefix,
1036                                    "domain controller",
1037                                    "localdc",
1038                                    "SAMBADOMAIN",
1039                                    "samba.example.com",
1040                                    "2008",
1041                                    21,
1042                                    "locDCpass1",
1043                                    undef, "netbios aliases = DC1");
1044
1045         return undef unless(defined $ret);
1046         unless($self->add_wins_config("$prefix/private")) {
1047                 warn("Unable to add wins configuration");
1048                 return undef;
1049         }
1050
1051         $ret->{NETBIOSALIAS} = "DC1";
1052         $ret->{DC_SERVER} = $ret->{SERVER};
1053         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1054         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1055         $ret->{DC_USERNAME} = $ret->{USERNAME};
1056         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1057
1058         return $ret;
1059 }
1060
1061 sub provision_fl2000dc($$)
1062 {
1063         my ($self, $prefix) = @_;
1064
1065         print "PROVISIONING DC...";
1066         my $ret = $self->provision($prefix,
1067                                    "domain controller",
1068                                    "dc5",
1069                                    "SAMBA2000",
1070                                    "samba2000.example.com",
1071                                    "2000",
1072                                    25,
1073                                    "locDCpass5",
1074                                    undef, "");
1075
1076         unless($self->add_wins_config("$prefix/private")) {
1077                 warn("Unable to add wins configuration");
1078                 return undef;
1079         }
1080
1081         return $ret;
1082 }
1083
1084 sub provision_fl2003dc($$)
1085 {
1086         my ($self, $prefix) = @_;
1087
1088         print "PROVISIONING DC...";
1089         my $ret = $self->provision($prefix,
1090                                    "domain controller",
1091                                    "dc6",
1092                                    "SAMBA2003",
1093                                    "samba2003.example.com",
1094                                    "2003",
1095                                    26,
1096                                    "locDCpass6",
1097                                    undef, "");
1098
1099         unless($self->add_wins_config("$prefix/private")) {
1100                 warn("Unable to add wins configuration");
1101                 return undef;
1102         }
1103
1104         return $ret;
1105 }
1106
1107 sub provision_fl2008r2dc($$)
1108 {
1109         my ($self, $prefix) = @_;
1110
1111         print "PROVISIONING DC...";
1112         my $ret = $self->provision($prefix,
1113                                    "domain controller",
1114                                    "dc7",
1115                                    "SAMBA2008R2",
1116                                    "samba2008R2.example.com",
1117                                    "2008_R2",
1118                                    27,
1119                                    "locDCpass7",
1120                                    undef, "");
1121
1122         unless ($self->add_wins_config("$prefix/private")) {
1123                 warn("Unable to add wins configuration");
1124                 return undef;
1125         }
1126
1127         return $ret;
1128 }
1129
1130
1131 sub provision_rodc($$$)
1132 {
1133         my ($self, $prefix, $dcvars) = @_;
1134         print "PROVISIONING RODC...";
1135
1136         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1137         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1138                                                "rodc",
1139                                                "SAMBADOMAIN",
1140                                                "samba.example.com",
1141                                                "2008",
1142                                                28, $dcvars->{PASSWORD},
1143                                                $dcvars->{SERVER_IP});
1144         unless ($ctx) {
1145                 return undef;
1146         }
1147
1148         $ctx->{tmpdir} = "$ctx->{prefix_abs}/tmp";
1149         push(@{$ctx->{directories}}, "$ctx->{tmpdir}");
1150
1151         $ctx->{smb_conf_extra_options} = "
1152         max xmit = 32K
1153         server max protocol = SMB2
1154
1155 [sysvol]
1156         path = $ctx->{statedir}/sysvol
1157         read only = yes
1158
1159 [netlogon]
1160         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1161         read only = yes
1162
1163 [tmp]
1164         path = $ctx->{tmpdir}
1165         read only = no
1166         posix:sharedelay = 10000
1167         posix:oplocktimeout = 3
1168         posix:writetimeupdatedelay = 500000
1169
1170 ";
1171
1172         my $ret = $self->provision_raw_step1($ctx);
1173         unless ($ret) {
1174                 return undef;
1175         }
1176
1177         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1178         my $cmd = "";
1179         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1180         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1181         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1182         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1183         $cmd .= " --server=$dcvars->{DC_SERVER}";
1184
1185         unless (system($cmd) == 0) {
1186                 warn("RODC join failed\n$cmd");
1187                 return undef;
1188         }
1189
1190         # we overwrite the kdc after the RODC join
1191         # so that use the RODC as kdc and test
1192         # the proxy code
1193         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1194         Samba::mk_krb5_conf($ctx);
1195
1196         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1197         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1198         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1199
1200         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1201         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1202         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1203         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1204         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1205
1206         return $ret;
1207 }
1208
1209 sub provision_plugin_s4_dc($$)
1210 {
1211         my ($self, $prefix) = @_;
1212
1213         my $extra_smbconf_options = "
1214 server services = -smb
1215 ";
1216
1217         print "PROVISIONING PLUGIN S4 DC...";
1218         my $ret = $self->provision($prefix,
1219                                    "domain controller",
1220                                    "plugindc",
1221                                    "PLUGINDOMAIN",
1222                                    "plugin.samba.example.com",
1223                                    "2008",
1224                                    30,
1225                                    "locDCpass1",
1226                                    undef, $extra_smbconf_options);
1227
1228         return undef unless(defined $ret);
1229         unless($self->add_wins_config("$prefix/private")) {
1230                 warn("Unable to add wins configuration");
1231                 return undef;
1232         }
1233
1234         $ret->{DC_SERVER} = $ret->{SERVER};
1235         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1236         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1237         $ret->{DC_USERNAME} = $ret->{USERNAME};
1238         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1239
1240         return $ret;
1241 }
1242
1243 sub teardown_env($$)
1244 {
1245         my ($self, $envvars) = @_;
1246         my $pid;
1247
1248         close(DATA);
1249
1250         if (open(IN, "<$envvars->{PIDDIR}/samba.pid")) {
1251                 $pid = <IN>;
1252                 close(IN);
1253
1254                 # Give the process 20 seconds to exit.  gcov needs
1255                 # this time to write out the covarge data
1256                 my $count = 0;
1257                 until (kill(0, $pid) == 0) {
1258                         # if no process sucessfully signalled, then we are done
1259                         sleep(1);
1260                         $count++;
1261                         last if $count > 20;
1262                 }
1263
1264                 # If it is still around, kill it
1265                 if ($count > 20) {
1266                         print "server process $pid took more than $count seconds to exit, killing\n";
1267                         kill 9, $pid;
1268                 }
1269         }
1270
1271         my $failed = $? >> 8;
1272
1273         $self->slapd_stop($envvars) if ($self->{ldap});
1274
1275         print $self->getlog_env($envvars);
1276
1277         return $failed;
1278 }
1279
1280 sub getlog_env($$)
1281 {
1282         my ($self, $envvars) = @_;
1283         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
1284         my $out = $title;
1285
1286         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
1287
1288         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
1289         while (<LOG>) {
1290                 $out .= $_;
1291         }
1292         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
1293         close(LOG);
1294
1295         return "" if $out eq $title;
1296
1297         return $out;
1298 }
1299
1300 sub check_env($$)
1301 {
1302         my ($self, $envvars) = @_;
1303
1304         return (-p $envvars->{SAMBA_TEST_FIFO});
1305 }
1306
1307 sub setup_env($$$)
1308 {
1309         my ($self, $envname, $path) = @_;
1310         my $target3 = $self->{target3};
1311
1312         $ENV{ENVNAME} = $envname;
1313
1314         if ($envname eq "dc") {
1315                 return $self->setup_dc("$path/dc");
1316         } elsif ($envname eq "fl2000dc") {
1317                 return $self->setup_fl2000dc("$path/fl2000dc");
1318         } elsif ($envname eq "fl2003dc") {
1319                 return $self->setup_fl2003dc("$path/fl2003dc");
1320         } elsif ($envname eq "fl2008r2dc") {
1321                 return $self->setup_fl2008r2dc("$path/fl2008r2dc");
1322         } elsif ($envname eq "rpc_proxy") {
1323                 if (not defined($self->{vars}->{dc})) {
1324                         $self->setup_dc("$path/dc");
1325                 }
1326                 return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{dc});
1327         } elsif ($envname eq "vampire_dc") {
1328                 if (not defined($self->{vars}->{dc})) {
1329                         $self->setup_dc("$path/dc");
1330                 }
1331                 return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{dc});
1332         } elsif ($envname eq "s4member") {
1333                 if (not defined($self->{vars}->{dc})) {
1334                         $self->setup_dc("$path/dc");
1335                 }
1336                 return $self->setup_member("$path/s4member", $self->{vars}->{dc});
1337         } elsif ($envname eq "rodc") {
1338                 if (not defined($self->{vars}->{dc})) {
1339                         $self->setup_dc("$path/dc");
1340                 }
1341                 return $self->setup_rodc("$path/rodc", $self->{vars}->{dc});
1342         } elsif ($envname eq "s3member") {
1343                 if (not defined($self->{vars}->{dc})) {
1344                         $self->setup_dc("$path/dc");
1345                 }
1346                 return $target3->setup_admember("$path/s3member", $self->{vars}->{dc}, 29);
1347         } elsif ($envname eq "plugin_s4_dc") {
1348                 return $self->setup_plugin_s4_dc("$path/plugin_s4_dc");
1349         } elsif ($envname eq "all") {
1350                 if (not defined($self->{vars}->{dc})) {
1351                         $ENV{ENVNAME} = "dc";
1352                         $self->setup_dc("$path/dc");
1353                 }
1354                 my $ret = $self->setup_member("$path/s4member", $self->{vars}->{dc});
1355                 if (not defined($self->{vars}->{rpc_proxy})) {
1356                         $ENV{ENVNAME} = "rpc_proxy";
1357                         my $rpc_proxy_ret = $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{dc});
1358                         
1359                         $ret->{RPC_PROXY_SERVER} = $rpc_proxy_ret->{SERVER};
1360                         $ret->{RPC_PROXY_SERVER_IP} = $rpc_proxy_ret->{SERVER_IP};
1361                         $ret->{RPC_PROXY_NETBIOSNAME} = $rpc_proxy_ret->{NETBIOSNAME};
1362                         $ret->{RPC_PROXY_USERNAME} = $rpc_proxy_ret->{USERNAME};
1363                         $ret->{RPC_PROXY_PASSWORD} = $rpc_proxy_ret->{PASSWORD};
1364                 }
1365                 if (not defined($self->{vars}->{fl2000dc})) {
1366                         $ENV{ENVNAME} = "fl2000dc";
1367                         my $fl2000dc_ret = $self->setup_fl2000dc("$path/fl2000dc", $self->{vars}->{dc});
1368                         
1369                         $ret->{FL2000DC_SERVER} = $fl2000dc_ret->{SERVER};
1370                         $ret->{FL2000DC_SERVER_IP} = $fl2000dc_ret->{SERVER_IP};
1371                         $ret->{FL2000DC_NETBIOSNAME} = $fl2000dc_ret->{NETBIOSNAME};
1372                         $ret->{FL2000DC_USERNAME} = $fl2000dc_ret->{USERNAME};
1373                         $ret->{FL2000DC_PASSWORD} = $fl2000dc_ret->{PASSWORD};
1374                 }
1375                 if (not defined($self->{vars}->{fl2003dc})) {
1376                         $ENV{ENVNAME} = "fl2003dc";
1377                         my $fl2003dc_ret = $self->setup_fl2003dc("$path/fl2003dc", $self->{vars}->{dc});
1378
1379                         $ret->{FL2003DC_SERVER} = $fl2003dc_ret->{SERVER};
1380                         $ret->{FL2003DC_SERVER_IP} = $fl2003dc_ret->{SERVER_IP};
1381                         $ret->{FL2003DC_NETBIOSNAME} = $fl2003dc_ret->{NETBIOSNAME};
1382                         $ret->{FL2003DC_USERNAME} = $fl2003dc_ret->{USERNAME};
1383                         $ret->{FL2003DC_PASSWORD} = $fl2003dc_ret->{PASSWORD};
1384                 }
1385                 if (not defined($self->{vars}->{fl2008r2dc})) {
1386                         $ENV{ENVNAME} = "fl2008r2dc";
1387                         my $fl2008r2dc_ret = $self->setup_fl2008r2dc("$path/fl2008r2dc", $self->{vars}->{dc});
1388
1389                         $ret->{FL2008R2DC_SERVER} = $fl2008r2dc_ret->{SERVER};
1390                         $ret->{FL2008R2DC_SERVER_IP} = $fl2008r2dc_ret->{SERVER_IP};
1391                         $ret->{FL2008R2DC_NETBIOSNAME} = $fl2008r2dc_ret->{NETBIOSNAME};
1392                         $ret->{FL2008R2DC_USERNAME} = $fl2008r2dc_ret->{USERNAME};
1393                         $ret->{FL2008R2DC_PASSWORD} = $fl2008r2dc_ret->{PASSWORD};
1394                 }
1395                 if (not defined($self->{vars}->{s3member})) {
1396                         $ENV{ENVNAME} = "s3member";
1397                         my $s3member_ret = $target3->setup_admember("$path/s3member", $self->{vars}->{dc}, 29);
1398                         $self->{vars}->{s3member} = $s3member_ret;
1399
1400                         $ret->{S3MEMBER_SERVER} = $s3member_ret->{SERVER};
1401                         $ret->{S3MEMBER_SERVER_IP} = $s3member_ret->{SERVER_IP};
1402                         $ret->{S3MEMBER_NETBIOSNAME} = $s3member_ret->{NETBIOSNAME};
1403                         $ret->{S3MEMBER_NETBIOSALIAS} = $s3member_ret->{NETBIOSALIAS};
1404                         $ret->{S3MEMBER_USERNAME} = $s3member_ret->{USERNAME};
1405                         $ret->{S3MEMBER_PASSWORD} = $s3member_ret->{PASSWORD};
1406                 }
1407                 return $ret;
1408         } else {
1409                 return undef;
1410         }
1411 }
1412
1413 sub setup_member($$$)
1414 {
1415         my ($self, $path, $dc_vars) = @_;
1416
1417         my $env = $self->provision_member($path, $dc_vars);
1418
1419         if (defined $env) {
1420                 $self->check_or_start($env, "single");
1421
1422                 $self->wait_for_start($env);
1423
1424                 $self->{vars}->{member} = $env;
1425         }
1426
1427         return $env;
1428 }
1429
1430 sub setup_rpc_proxy($$$)
1431 {
1432         my ($self, $path, $dc_vars) = @_;
1433
1434         my $env = $self->provision_rpc_proxy($path, $dc_vars);
1435
1436         if (defined $env) {
1437                 $self->check_or_start($env, "single");
1438
1439                 $self->wait_for_start($env);
1440
1441                 $self->{vars}->{rpc_proxy} = $env;
1442         }
1443         return $env;
1444 }
1445
1446 sub setup_dc($$)
1447 {
1448         my ($self, $path) = @_;
1449
1450         my $env = $self->provision_dc($path);
1451         if (defined $env) {
1452                 $self->check_or_start($env, "standard");
1453
1454                 $self->wait_for_start($env);
1455
1456                 $self->{vars}->{dc} = $env;
1457         }
1458         return $env;
1459 }
1460
1461 sub setup_fl2000dc($$)
1462 {
1463         my ($self, $path) = @_;
1464
1465         my $env = $self->provision_fl2000dc($path);
1466         if (defined $env) {
1467                 $self->check_or_start($env, "single");
1468
1469                 $self->wait_for_start($env);
1470
1471                 $self->{vars}->{fl2000dc} = $env;
1472         }
1473
1474         return $env;
1475 }
1476
1477 sub setup_fl2003dc($$)
1478 {
1479         my ($self, $path) = @_;
1480
1481         my $env = $self->provision_fl2003dc($path);
1482
1483         if (defined $env) {
1484                 $self->check_or_start($env, "single");
1485
1486                 $self->wait_for_start($env);
1487
1488                 $self->{vars}->{fl2003dc} = $env;
1489         }
1490         return $env;
1491 }
1492
1493 sub setup_fl2008r2dc($$)
1494 {
1495         my ($self, $path) = @_;
1496
1497         my $env = $self->provision_fl2008r2dc($path);
1498
1499         if (defined $env) {
1500                 $self->check_or_start($env, "single");
1501
1502                 $self->wait_for_start($env);
1503
1504                 $self->{vars}->{fl2008r2dc} = $env;
1505         }
1506
1507         return $env;
1508 }
1509
1510 sub setup_vampire_dc($$$)
1511 {
1512         my ($self, $path, $dc_vars) = @_;
1513
1514         my $env = $self->provision_vampire_dc($path, $dc_vars);
1515
1516         if (defined $env) {
1517                 $self->check_or_start($env, "single");
1518
1519                 $self->wait_for_start($env);
1520
1521                 $self->{vars}->{vampire_dc} = $env;
1522
1523                 # force replicated DC to update repsTo/repsFrom
1524                 # for vampired partitions
1525                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1526                 my $cmd = "";
1527                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1528                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1529                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1530                 $cmd .= " $env->{CONFIGURATION}";
1531                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1532                 unless (system($cmd) == 0) {
1533                         warn("Failed to exec kcc\n$cmd");
1534                         return undef;
1535                 }
1536
1537                 # as 'vampired' dc may add data in its local replica
1538                 # we need to synchronize data between DCs
1539                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1540                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1541                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1542                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{VAMPIRE_DC_SERVER}";
1543                 $cmd .= " $dc_vars->{CONFIGURATION}";
1544                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1545                 # replicate Configuration NC
1546                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
1547                 unless(system($cmd_repl) == 0) {
1548                         warn("Failed to replicate\n$cmd_repl");
1549                         return undef;
1550                 }
1551                 # replicate Default NC
1552                 $cmd_repl = "$cmd \"$base_dn\"";
1553                 unless(system($cmd_repl) == 0) {
1554                         warn("Failed to replicate\n$cmd_repl");
1555                         return undef;
1556                 }
1557         }
1558
1559         return $env;
1560 }
1561
1562 sub setup_rodc($$$)
1563 {
1564         my ($self, $path, $dc_vars) = @_;
1565
1566         my $env = $self->provision_rodc($path, $dc_vars);
1567
1568         unless ($env) {
1569                 return undef;
1570         }
1571
1572         $self->check_or_start($env, "single");
1573
1574         $self->wait_for_start($env);
1575
1576         $self->{vars}->{rodc} = $env;
1577
1578         return $env;
1579 }
1580
1581 sub setup_plugin_s4_dc($$)
1582 {
1583         my ($self, $path) = @_;
1584
1585         my $env = $self->provision_plugin_s4_dc($path);
1586         if (defined $env) {
1587                 $self->check_or_start($env, "single");
1588
1589                 $self->wait_for_start($env);
1590
1591                 my $s3_part_env = $self->{target3}->setup_plugin_s4_dc($path, $env, 30);
1592                 if (not defined($s3_part_env)) {
1593                     return undef;
1594                 }
1595
1596                 $self->{vars}->{plugin_s4_dc} = $s3_part_env;
1597         }
1598         return $env;
1599 }
1600
1601 1;