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