6d6722928973e00d2636422827f514f5c962829e
[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         push (@provision_options, "--dns-backend=BIND9_DLZ");
561
562         @{$ctx->{provision_options}} = @provision_options;
563
564         return $ctx;
565 }
566
567 #
568 # Step1 creates the basic configuration
569 #
570 sub provision_raw_step1($$)
571 {
572         my ($self, $ctx) = @_;
573
574         mkdir($_, 0777) foreach (@{$ctx->{directories}});
575
576         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
577                 warn("can't open $ctx->{smb_conf}$?");
578                 return undef;
579         }
580         my $acl = "false";
581         $acl = "true" if (defined $ENV{WITH_ACL});
582         print CONFFILE "
583 [global]
584         acl:search = $acl
585         netbios name = $ctx->{netbiosname}
586         posix:eadb = $ctx->{statedir}/eadb.tdb
587         workgroup = $ctx->{domain}
588         realm = $ctx->{realm}
589         private dir = $ctx->{privatedir}
590         pid directory = $ctx->{piddir}
591         ncalrpc dir = $ctx->{ncalrpcdir}
592         lock dir = $ctx->{lockdir}
593         state directory = $ctx->{statedir}
594         cache directory = $ctx->{cachedir}
595         winbindd socket directory = $ctx->{winbindd_socket_dir}
596         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
597         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
598         winbind separator = /
599         name resolve order = file bcast
600         interfaces = $ctx->{interfaces}
601         tls dh params file = $ctx->{tlsdir}/dhparms.pem
602         panic action = $RealBin/gdb_backtrace \%d
603         wins support = yes
604         server role = $ctx->{server_role}
605         server services = +echo
606         notify:inotify = false
607         ldb:nosync = true
608 #We don't want to pass our self-tests if the PAC code is wrong
609         gensec:require_pac = true
610         log level = $ctx->{server_loglevel}
611         lanman auth = Yes
612         rndc command = true
613         dns update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$ctx->{dns_host_file}
614         spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate
615         resolv:host file = $ctx->{dns_host_file}
616         dreplsrv:periodic_startup_interval = 0
617
618         passdb backend = samba4
619 ";
620
621         if (defined($ctx->{sid_generator}) && $ctx->{sid_generator} ne "internal") {
622                 print CONFFILE "
623         sid generator = $ctx->{sid_generator}";
624         }
625
626         print CONFFILE "
627
628         # Begin extra options
629         $ctx->{smb_conf_extra_options}
630         # End extra options
631 ";
632         close(CONFFILE);
633
634         $self->mk_keyblobs($ctx->{tlsdir});
635
636         #Default the KDC IP to the server's IP
637         if (not defined($ctx->{kdc_ipv4})) {
638              $ctx->{kdc_ipv4} = $ctx->{ipv4};
639         }
640
641         Samba::mk_krb5_conf($ctx, "");
642
643         open(PWD, ">$ctx->{nsswrap_passwd}");
644         print PWD "
645 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
646 $ctx->{unix_name}:x:$ctx->{unix_uid}:@{$ctx->{unix_gids}}[0]:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false
647 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
648 ";
649         close(PWD);
650
651         open(GRP, ">$ctx->{nsswrap_group}");
652         print GRP "
653 root:x:0:
654 wheel:x:10:
655 users:x:100:
656 nobody:x:65533:
657 nogroup:x:65534:nobody
658 ";
659         close(GRP);
660
661         my $configuration = "--configfile=$ctx->{smb_conf}";
662
663 #Ensure the config file is valid before we start
664         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
665         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
666                 system("$testparm -v --suppress-prompt $configuration >&2");
667                 warn("Failed to create a valid smb.conf configuration $testparm!");
668                 return undef;
669         }
670         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) {
671                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
672                 return undef;
673         }
674
675         my $ret = {
676                 KRB5_CONFIG => $ctx->{krb5_conf},
677                 PIDDIR => $ctx->{piddir},
678                 SERVER => $ctx->{hostname},
679                 SERVER_IP => $ctx->{ipv4},
680                 NETBIOSNAME => $ctx->{netbiosname},
681                 DOMAIN => $ctx->{domain},
682                 USERNAME => $ctx->{username},
683                 REALM => $ctx->{realm},
684                 PASSWORD => $ctx->{password},
685                 LDAPDIR => $ctx->{ldapdir},
686                 LDAP_INSTANCE => $ctx->{ldap_instance},
687                 WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
688                 NCALRPCDIR => $ctx->{ncalrpcdir},
689                 LOCKDIR => $ctx->{lockdir},
690                 STATEDIR => $ctx->{statedir},
691                 CACHEDIR => $ctx->{cachedir},
692                 SERVERCONFFILE => $ctx->{smb_conf},
693                 CONFIGURATION => $configuration,
694                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
695                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
696                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
697                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
698                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
699                 SAMBA_TEST_LOG_POS => 0,
700         };
701
702         return $ret;
703 }
704
705 #
706 # Step2 runs the provision script
707 #
708 sub provision_raw_step2($$$)
709 {
710         my ($self, $ctx, $ret) = @_;
711
712         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
713         unless (system($provision_cmd) == 0) {
714                 warn("Unable to provision: \n$provision_cmd\n");
715                 return undef;
716         }
717
718         return $ret;
719 }
720
721 sub provision($$$$$$$$$)
722 {
723         my ($self, $prefix, $server_role, $hostname,
724             $domain, $realm, $functional_level,
725             $swiface, $password, $kdc_ipv4, $extra_smbconf_options) = @_;
726
727         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
728                                                $hostname,
729                                                $domain, $realm, $functional_level,
730                                                $swiface, $password, $kdc_ipv4);
731
732         $ctx->{tmpdir} = "$ctx->{prefix_abs}/tmp";
733         push(@{$ctx->{directories}}, "$ctx->{tmpdir}");
734         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test1");
735         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test2");
736         my $msdfs = "no";
737         $msdfs = "yes" if ($server_role eq "domain controller");
738         $ctx->{smb_conf_extra_options} = "
739
740         max xmit = 32K
741         server max protocol = SMB2
742         host msdfs = $msdfs
743         lanman auth = yes
744
745         $extra_smbconf_options
746
747 [tmp]
748         path = $ctx->{tmpdir}
749         read only = no
750         posix:sharedelay = 10000
751         posix:oplocktimeout = 3
752         posix:writetimeupdatedelay = 500000
753
754 [test1]
755         path = $ctx->{tmpdir}/test1
756         read only = no
757         posix:sharedelay = 10000
758         posix:oplocktimeout = 3
759         posix:writetimeupdatedelay = 50000
760
761 [test2]
762         path = $ctx->{tmpdir}/test2
763         read only = no
764         posix:sharedelay = 10000
765         posix:oplocktimeout = 3
766         posix:writetimeupdatedelay = 50000
767
768 [cifs]
769         read only = no
770         ntvfs handler = cifs
771         cifs:server = $ctx->{netbiosname}
772         cifs:share = tmp
773         cifs:use-s4u2proxy = yes
774         # There is no username specified here, instead the client is expected
775         # to log in with kerberos, and the serverwill use delegated credentials.
776         # Or the server tries s4u2self/s4u2proxy to impersonate the client
777
778 [simple]
779         path = $ctx->{tmpdir}
780         read only = no
781         ntvfs handler = simple
782
783 [sysvol]
784         path = $ctx->{statedir}/sysvol
785         read only = yes
786
787 [netlogon]
788         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
789         read only = no
790
791 [cifsposix]
792         copy = simple
793         ntvfs handler = cifsposix
794 ";
795
796         if (defined($self->{ldap})) {
797                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
798                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
799
800                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
801                 $ldap_uri =~ s|/|%2F|g;
802                 $ldap_uri = "ldapi://$ldap_uri";
803                 $ctx->{ldap_uri} = $ldap_uri;
804                 if ($self->{ldap} eq "fedora-ds") {
805                         $ctx->{sid_generator} = "backend";
806                 }
807
808                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
809         }
810
811         my $ret = $self->provision_raw_step1($ctx);
812         unless (defined $ret) {
813                 return undef;
814         }
815
816         if (defined($self->{ldap})) {
817                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
818                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
819                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
820                 if ($self->{ldap} eq "openldap") {
821                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
822                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
823
824                 } elsif ($self->{ldap} eq "fedora-ds") {
825                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
826                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
827                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
828                 }
829
830         }
831
832         return $self->provision_raw_step2($ctx, $ret);
833 }
834
835 sub provision_member($$$)
836 {
837         my ($self, $prefix, $dcvars) = @_;
838         print "PROVISIONING MEMBER...";
839
840         my $ret = $self->provision($prefix,
841                                    "member server",
842                                    "s4member",
843                                    "SAMBADOMAIN",
844                                    "samba.example.com",
845                                    "2008",
846                                    23,
847                                    "locMEMpass3",
848                                    $dcvars->{SERVER_IP},
849                                    "");
850         unless ($ret) {
851                 return undef;
852         }
853
854         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
855         my $cmd = "";
856         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
857         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
858         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
859         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
860
861         unless (system($cmd) == 0) {
862                 warn("Join failed\n$cmd");
863                 return undef;
864         }
865
866         $ret->{MEMBER_SERVER} = $ret->{SERVER};
867         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
868         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
869         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
870         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
871
872         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
873         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
874         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
875         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
876         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
877
878         return $ret;
879 }
880
881 sub provision_rpc_proxy($$$)
882 {
883         my ($self, $prefix, $dcvars) = @_;
884         print "PROVISIONING RPC PROXY...";
885
886         my $extra_smbconf_options = "
887
888         # rpc_proxy
889         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
890         dcerpc endpoint servers = epmapper, remote
891         dcerpc_remote:interfaces = rpcecho
892
893 [cifs_to_dc]
894         read only = no
895         ntvfs handler = cifs
896         cifs:server = $dcvars->{SERVER}
897         cifs:share = cifs
898         cifs:use-s4u2proxy = yes
899         # There is no username specified here, instead the client is expected
900         # to log in with kerberos, and the serverwill use delegated credentials.
901         # Or the server tries s4u2self/s4u2proxy to impersonate the client
902
903 ";
904
905         my $ret = $self->provision($prefix,
906                                    "member server",
907                                    "localrpcproxy",
908                                    "SAMBADOMAIN",
909                                    "samba.example.com",
910                                    "2008",
911                                    24,
912                                    "locRPCproxypass4",
913                                    $dcvars->{SERVER_IP},
914                                    $extra_smbconf_options);
915
916         unless ($ret) {
917                 return undef;
918         }
919
920         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
921
922         # The joind runs in the context of the rpc_proxy/member for now
923         my $cmd = "";
924         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
925         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
926         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
927         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
928
929         unless (system($cmd) == 0) {
930                 warn("Join failed\n$cmd");
931                 return undef;
932         }
933
934         # Setting up delegation runs in the context of the DC for now
935         $cmd = "";
936         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
937         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
938         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
939         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
940
941         unless (system($cmd) == 0) {
942                 warn("Delegation failed\n$cmd");
943                 return undef;
944         }
945
946         # Setting up delegation runs in the context of the DC for now
947         $cmd = "";
948         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
949         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
950         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
951         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
952
953         unless (system($cmd) == 0) {
954                 warn("Delegation failed\n$cmd");
955                 return undef;
956         }
957
958         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
959         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
960         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
961         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
962         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
963
964         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
965         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
966         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
967         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
968         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
969
970         return $ret;
971 }
972
973 sub provision_vampire_dc($$$)
974 {
975         my ($self, $prefix, $dcvars) = @_;
976         print "PROVISIONING VAMPIRE DC...";
977
978         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
979         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
980                                                "localvampiredc",
981                                                "SAMBADOMAIN",
982                                                "samba.example.com",
983                                                "2008",
984                                                22, $dcvars->{PASSWORD},
985                                                $dcvars->{SERVER_IP});
986
987         $ctx->{smb_conf_extra_options} = "
988         max xmit = 32K
989         server max protocol = SMB2
990
991 [sysvol]
992         path = $ctx->{statedir}/sysvol
993         read only = yes
994
995 [netlogon]
996         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
997         read only = no
998
999 ";
1000
1001         my $ret = $self->provision_raw_step1($ctx);
1002         unless ($ret) {
1003                 return undef;
1004         }
1005
1006         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1007         my $cmd = "";
1008         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1009         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1010         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1011         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1012
1013         unless (system($cmd) == 0) {
1014                 warn("Join failed\n$cmd");
1015                 return undef;
1016         }
1017
1018         $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1019         $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1020         $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1021
1022         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1023         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1024         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1025         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1026         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1027
1028         return $ret;
1029 }
1030
1031 sub provision_subdom_dc($$$)
1032 {
1033         my ($self, $prefix, $dcvars) = @_;
1034         print "PROVISIONING SUBDOMAIN DC...";
1035
1036         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1037         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1038                                                "localsubdc",
1039                                                "SAMBASUBDOM",
1040                                                "sub.samba.example.com",
1041                                                "2008",
1042                                                31, $dcvars->{PASSWORD},
1043                                                undef);
1044
1045         $ctx->{smb_conf_extra_options} = "
1046         max xmit = 32K
1047         server max protocol = SMB2
1048
1049 [sysvol]
1050         path = $ctx->{statedir}/sysvol
1051         read only = yes
1052
1053 [netlogon]
1054         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1055         read only = no
1056
1057 ";
1058
1059         my $ret = $self->provision_raw_step1($ctx);
1060         unless ($ret) {
1061                 return undef;
1062         }
1063
1064         my $dc_realms = Samba::mk_realms_stanza($dcvars->{REALM}, lc($dcvars->{REALM}),
1065                                                 $dcvars->{DOMAIN}, $dcvars->{SERVER_IP});
1066         Samba::mk_krb5_conf($ctx, $dc_realms);
1067
1068         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1069         my $cmd = "";
1070         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1071         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1072         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{realm} subdomain ";
1073         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1074
1075         unless (system($cmd) == 0) {
1076                 warn("Join failed\n$cmd");
1077                 return undef;
1078         }
1079
1080         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1081         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1082         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1083
1084         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1085         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1086         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1087         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1088         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1089
1090         return $ret;
1091 }
1092
1093 sub provision_dc($$)
1094 {
1095         my ($self, $prefix) = @_;
1096
1097         print "PROVISIONING DC...";
1098         my $ret = $self->provision($prefix,
1099                                    "domain controller",
1100                                    "localdc",
1101                                    "SAMBADOMAIN",
1102                                    "samba.example.com",
1103                                    "2008",
1104                                    21,
1105                                    "locDCpass1",
1106                                    undef, "netbios aliases = DC1");
1107
1108         return undef unless(defined $ret);
1109         unless($self->add_wins_config("$prefix/private")) {
1110                 warn("Unable to add wins configuration");
1111                 return undef;
1112         }
1113
1114         $ret->{NETBIOSALIAS} = "DC1";
1115         $ret->{DC_SERVER} = $ret->{SERVER};
1116         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1117         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1118         $ret->{DC_USERNAME} = $ret->{USERNAME};
1119         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1120
1121         return $ret;
1122 }
1123
1124 sub provision_fl2000dc($$)
1125 {
1126         my ($self, $prefix) = @_;
1127
1128         print "PROVISIONING DC...";
1129         my $ret = $self->provision($prefix,
1130                                    "domain controller",
1131                                    "dc5",
1132                                    "SAMBA2000",
1133                                    "samba2000.example.com",
1134                                    "2000",
1135                                    25,
1136                                    "locDCpass5",
1137                                    undef, "");
1138
1139         unless($self->add_wins_config("$prefix/private")) {
1140                 warn("Unable to add wins configuration");
1141                 return undef;
1142         }
1143
1144         return $ret;
1145 }
1146
1147 sub provision_fl2003dc($$)
1148 {
1149         my ($self, $prefix) = @_;
1150
1151         print "PROVISIONING DC...";
1152         my $ret = $self->provision($prefix,
1153                                    "domain controller",
1154                                    "dc6",
1155                                    "SAMBA2003",
1156                                    "samba2003.example.com",
1157                                    "2003",
1158                                    26,
1159                                    "locDCpass6",
1160                                    undef, "");
1161
1162         unless($self->add_wins_config("$prefix/private")) {
1163                 warn("Unable to add wins configuration");
1164                 return undef;
1165         }
1166
1167         return $ret;
1168 }
1169
1170 sub provision_fl2008r2dc($$)
1171 {
1172         my ($self, $prefix) = @_;
1173
1174         print "PROVISIONING DC...";
1175         my $ret = $self->provision($prefix,
1176                                    "domain controller",
1177                                    "dc7",
1178                                    "SAMBA2008R2",
1179                                    "samba2008R2.example.com",
1180                                    "2008_R2",
1181                                    27,
1182                                    "locDCpass7",
1183                                    undef, "");
1184
1185         unless ($self->add_wins_config("$prefix/private")) {
1186                 warn("Unable to add wins configuration");
1187                 return undef;
1188         }
1189
1190         return $ret;
1191 }
1192
1193
1194 sub provision_rodc($$$)
1195 {
1196         my ($self, $prefix, $dcvars) = @_;
1197         print "PROVISIONING RODC...";
1198
1199         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1200         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1201                                                "rodc",
1202                                                "SAMBADOMAIN",
1203                                                "samba.example.com",
1204                                                "2008",
1205                                                28, $dcvars->{PASSWORD},
1206                                                $dcvars->{SERVER_IP});
1207         unless ($ctx) {
1208                 return undef;
1209         }
1210
1211         $ctx->{tmpdir} = "$ctx->{prefix_abs}/tmp";
1212         push(@{$ctx->{directories}}, "$ctx->{tmpdir}");
1213
1214         $ctx->{smb_conf_extra_options} = "
1215         max xmit = 32K
1216         server max protocol = SMB2
1217
1218 [sysvol]
1219         path = $ctx->{statedir}/sysvol
1220         read only = yes
1221
1222 [netlogon]
1223         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1224         read only = yes
1225
1226 [tmp]
1227         path = $ctx->{tmpdir}
1228         read only = no
1229         posix:sharedelay = 10000
1230         posix:oplocktimeout = 3
1231         posix:writetimeupdatedelay = 500000
1232
1233 ";
1234
1235         my $ret = $self->provision_raw_step1($ctx);
1236         unless ($ret) {
1237                 return undef;
1238         }
1239
1240         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1241         my $cmd = "";
1242         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1243         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1244         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1245         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1246         $cmd .= " --server=$dcvars->{DC_SERVER}";
1247
1248         unless (system($cmd) == 0) {
1249                 warn("RODC join failed\n$cmd");
1250                 return undef;
1251         }
1252
1253         # we overwrite the kdc after the RODC join
1254         # so that use the RODC as kdc and test
1255         # the proxy code
1256         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1257         Samba::mk_krb5_conf($ctx);
1258
1259         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1260         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1261         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1262
1263         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1264         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1265         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1266         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1267         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1268
1269         return $ret;
1270 }
1271
1272 sub provision_plugin_s4_dc($$)
1273 {
1274         my ($self, $prefix) = @_;
1275
1276         my $extra_smbconf_options = "
1277 server services = -smb
1278 dcerpc endpoint servers = -unixinfo -rpcecho -spoolss -winreg -wkssvc -srvsvc
1279 ";
1280
1281         print "PROVISIONING PLUGIN S4 DC...";
1282         my $ret = $self->provision($prefix,
1283                                    "domain controller",
1284                                    "plugindc",
1285                                    "PLUGINDOMAIN",
1286                                    "plugin.samba.example.com",
1287                                    "2008",
1288                                    30,
1289                                    "locDCpass1",
1290                                    undef, $extra_smbconf_options);
1291
1292         return undef unless(defined $ret);
1293         unless($self->add_wins_config("$prefix/private")) {
1294                 warn("Unable to add wins configuration");
1295                 return undef;
1296         }
1297
1298         $ret->{DC_SERVER} = $ret->{SERVER};
1299         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1300         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1301         $ret->{DC_USERNAME} = $ret->{USERNAME};
1302         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1303
1304         return $ret;
1305 }
1306
1307 sub teardown_env($$)
1308 {
1309         my ($self, $envvars) = @_;
1310         my $pid;
1311
1312         close(DATA);
1313
1314         if (open(IN, "<$envvars->{PIDDIR}/samba.pid")) {
1315                 $pid = <IN>;
1316                 close(IN);
1317
1318                 # Give the process 20 seconds to exit.  gcov needs
1319                 # this time to write out the covarge data
1320                 my $count = 0;
1321                 until (kill(0, $pid) == 0) {
1322                         # if no process sucessfully signalled, then we are done
1323                         sleep(1);
1324                         $count++;
1325                         last if $count > 20;
1326                 }
1327
1328                 # If it is still around, kill it
1329                 if ($count > 20) {
1330                         print "server process $pid took more than $count seconds to exit, killing\n";
1331                         kill 9, $pid;
1332                 }
1333         }
1334
1335         my $failed = $? >> 8;
1336
1337         $self->slapd_stop($envvars) if ($self->{ldap});
1338
1339         print $self->getlog_env($envvars);
1340
1341         return $failed;
1342 }
1343
1344 sub getlog_env($$)
1345 {
1346         my ($self, $envvars) = @_;
1347         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
1348         my $out = $title;
1349
1350         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
1351
1352         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
1353         while (<LOG>) {
1354                 $out .= $_;
1355         }
1356         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
1357         close(LOG);
1358
1359         return "" if $out eq $title;
1360
1361         return $out;
1362 }
1363
1364 sub check_env($$)
1365 {
1366         my ($self, $envvars) = @_;
1367
1368         return (-p $envvars->{SAMBA_TEST_FIFO});
1369 }
1370
1371 sub setup_env($$$)
1372 {
1373         my ($self, $envname, $path) = @_;
1374         my $target3 = $self->{target3};
1375
1376         $ENV{ENVNAME} = $envname;
1377
1378         if ($envname eq "dc") {
1379                 return $self->setup_dc("$path/dc");
1380         } elsif ($envname eq "fl2000dc") {
1381                 return $self->setup_fl2000dc("$path/fl2000dc");
1382         } elsif ($envname eq "fl2003dc") {
1383                 return $self->setup_fl2003dc("$path/fl2003dc");
1384         } elsif ($envname eq "fl2008r2dc") {
1385                 return $self->setup_fl2008r2dc("$path/fl2008r2dc");
1386         } elsif ($envname eq "rpc_proxy") {
1387                 if (not defined($self->{vars}->{dc})) {
1388                         $self->setup_dc("$path/dc");
1389                 }
1390                 return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{dc});
1391         } elsif ($envname eq "vampire_dc") {
1392                 if (not defined($self->{vars}->{dc})) {
1393                         $self->setup_dc("$path/dc");
1394                 }
1395                 return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{dc});
1396         } elsif ($envname eq "subdom_dc") {
1397                 if (not defined($self->{vars}->{dc})) {
1398                         $self->setup_dc("$path/dc");
1399                 }
1400                 return $self->setup_subdom_dc("$path/subdom_dc", $self->{vars}->{dc});
1401         } elsif ($envname eq "s4member") {
1402                 if (not defined($self->{vars}->{dc})) {
1403                         $self->setup_dc("$path/dc");
1404                 }
1405                 return $self->setup_member("$path/s4member", $self->{vars}->{dc});
1406         } elsif ($envname eq "rodc") {
1407                 if (not defined($self->{vars}->{dc})) {
1408                         $self->setup_dc("$path/dc");
1409                 }
1410                 return $self->setup_rodc("$path/rodc", $self->{vars}->{dc});
1411         } elsif ($envname eq "s3member") {
1412                 if (not defined($self->{vars}->{dc})) {
1413                         $self->setup_dc("$path/dc");
1414                 }
1415                 return $target3->setup_admember("$path/s3member", $self->{vars}->{dc}, 29);
1416         } elsif ($envname eq "plugin_s4_dc") {
1417                 return $self->setup_plugin_s4_dc("$path/plugin_s4_dc");
1418         } elsif ($envname eq "all") {
1419                 if (not defined($self->{vars}->{dc})) {
1420                         $ENV{ENVNAME} = "dc";
1421                         $self->setup_dc("$path/dc");
1422                 }
1423                 my $ret = $self->setup_member("$path/s4member", $self->{vars}->{dc});
1424                 if (not defined($self->{vars}->{rpc_proxy})) {
1425                         $ENV{ENVNAME} = "rpc_proxy";
1426                         my $rpc_proxy_ret = $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{dc});
1427                         
1428                         $ret->{RPC_PROXY_SERVER} = $rpc_proxy_ret->{SERVER};
1429                         $ret->{RPC_PROXY_SERVER_IP} = $rpc_proxy_ret->{SERVER_IP};
1430                         $ret->{RPC_PROXY_NETBIOSNAME} = $rpc_proxy_ret->{NETBIOSNAME};
1431                         $ret->{RPC_PROXY_USERNAME} = $rpc_proxy_ret->{USERNAME};
1432                         $ret->{RPC_PROXY_PASSWORD} = $rpc_proxy_ret->{PASSWORD};
1433                 }
1434                 if (not defined($self->{vars}->{fl2000dc})) {
1435                         $ENV{ENVNAME} = "fl2000dc";
1436                         my $fl2000dc_ret = $self->setup_fl2000dc("$path/fl2000dc", $self->{vars}->{dc});
1437                         
1438                         $ret->{FL2000DC_SERVER} = $fl2000dc_ret->{SERVER};
1439                         $ret->{FL2000DC_SERVER_IP} = $fl2000dc_ret->{SERVER_IP};
1440                         $ret->{FL2000DC_NETBIOSNAME} = $fl2000dc_ret->{NETBIOSNAME};
1441                         $ret->{FL2000DC_USERNAME} = $fl2000dc_ret->{USERNAME};
1442                         $ret->{FL2000DC_PASSWORD} = $fl2000dc_ret->{PASSWORD};
1443                 }
1444                 if (not defined($self->{vars}->{fl2003dc})) {
1445                         $ENV{ENVNAME} = "fl2003dc";
1446                         my $fl2003dc_ret = $self->setup_fl2003dc("$path/fl2003dc", $self->{vars}->{dc});
1447
1448                         $ret->{FL2003DC_SERVER} = $fl2003dc_ret->{SERVER};
1449                         $ret->{FL2003DC_SERVER_IP} = $fl2003dc_ret->{SERVER_IP};
1450                         $ret->{FL2003DC_NETBIOSNAME} = $fl2003dc_ret->{NETBIOSNAME};
1451                         $ret->{FL2003DC_USERNAME} = $fl2003dc_ret->{USERNAME};
1452                         $ret->{FL2003DC_PASSWORD} = $fl2003dc_ret->{PASSWORD};
1453                 }
1454                 if (not defined($self->{vars}->{fl2008r2dc})) {
1455                         $ENV{ENVNAME} = "fl2008r2dc";
1456                         my $fl2008r2dc_ret = $self->setup_fl2008r2dc("$path/fl2008r2dc", $self->{vars}->{dc});
1457
1458                         $ret->{FL2008R2DC_SERVER} = $fl2008r2dc_ret->{SERVER};
1459                         $ret->{FL2008R2DC_SERVER_IP} = $fl2008r2dc_ret->{SERVER_IP};
1460                         $ret->{FL2008R2DC_NETBIOSNAME} = $fl2008r2dc_ret->{NETBIOSNAME};
1461                         $ret->{FL2008R2DC_USERNAME} = $fl2008r2dc_ret->{USERNAME};
1462                         $ret->{FL2008R2DC_PASSWORD} = $fl2008r2dc_ret->{PASSWORD};
1463                 }
1464                 if (not defined($self->{vars}->{s3member})) {
1465                         $ENV{ENVNAME} = "s3member";
1466                         my $s3member_ret = $target3->setup_admember("$path/s3member", $self->{vars}->{dc}, 29);
1467                         $self->{vars}->{s3member} = $s3member_ret;
1468
1469                         $ret->{S3MEMBER_SERVER} = $s3member_ret->{SERVER};
1470                         $ret->{S3MEMBER_SERVER_IP} = $s3member_ret->{SERVER_IP};
1471                         $ret->{S3MEMBER_NETBIOSNAME} = $s3member_ret->{NETBIOSNAME};
1472                         $ret->{S3MEMBER_NETBIOSALIAS} = $s3member_ret->{NETBIOSALIAS};
1473                         $ret->{S3MEMBER_USERNAME} = $s3member_ret->{USERNAME};
1474                         $ret->{S3MEMBER_PASSWORD} = $s3member_ret->{PASSWORD};
1475                 }
1476                 return $ret;
1477         } else {
1478                 return undef;
1479         }
1480 }
1481
1482 sub setup_member($$$)
1483 {
1484         my ($self, $path, $dc_vars) = @_;
1485
1486         my $env = $self->provision_member($path, $dc_vars);
1487
1488         if (defined $env) {
1489                 $self->check_or_start($env, "single");
1490
1491                 $self->wait_for_start($env);
1492
1493                 $self->{vars}->{member} = $env;
1494         }
1495
1496         return $env;
1497 }
1498
1499 sub setup_rpc_proxy($$$)
1500 {
1501         my ($self, $path, $dc_vars) = @_;
1502
1503         my $env = $self->provision_rpc_proxy($path, $dc_vars);
1504
1505         if (defined $env) {
1506                 $self->check_or_start($env, "single");
1507
1508                 $self->wait_for_start($env);
1509
1510                 $self->{vars}->{rpc_proxy} = $env;
1511         }
1512         return $env;
1513 }
1514
1515 sub setup_dc($$)
1516 {
1517         my ($self, $path) = @_;
1518
1519         my $env = $self->provision_dc($path);
1520         if (defined $env) {
1521                 $self->check_or_start($env, "standard");
1522
1523                 $self->wait_for_start($env);
1524
1525                 $self->{vars}->{dc} = $env;
1526         }
1527         return $env;
1528 }
1529
1530 sub setup_fl2000dc($$)
1531 {
1532         my ($self, $path) = @_;
1533
1534         my $env = $self->provision_fl2000dc($path);
1535         if (defined $env) {
1536                 $self->check_or_start($env, "single");
1537
1538                 $self->wait_for_start($env);
1539
1540                 $self->{vars}->{fl2000dc} = $env;
1541         }
1542
1543         return $env;
1544 }
1545
1546 sub setup_fl2003dc($$)
1547 {
1548         my ($self, $path) = @_;
1549
1550         my $env = $self->provision_fl2003dc($path);
1551
1552         if (defined $env) {
1553                 $self->check_or_start($env, "single");
1554
1555                 $self->wait_for_start($env);
1556
1557                 $self->{vars}->{fl2003dc} = $env;
1558         }
1559         return $env;
1560 }
1561
1562 sub setup_fl2008r2dc($$)
1563 {
1564         my ($self, $path) = @_;
1565
1566         my $env = $self->provision_fl2008r2dc($path);
1567
1568         if (defined $env) {
1569                 $self->check_or_start($env, "single");
1570
1571                 $self->wait_for_start($env);
1572
1573                 $self->{vars}->{fl2008r2dc} = $env;
1574         }
1575
1576         return $env;
1577 }
1578
1579 sub setup_vampire_dc($$$)
1580 {
1581         my ($self, $path, $dc_vars) = @_;
1582
1583         my $env = $self->provision_vampire_dc($path, $dc_vars);
1584
1585         if (defined $env) {
1586                 $self->check_or_start($env, "single");
1587
1588                 $self->wait_for_start($env);
1589
1590                 $self->{vars}->{vampire_dc} = $env;
1591
1592                 # force replicated DC to update repsTo/repsFrom
1593                 # for vampired partitions
1594                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1595                 my $cmd = "";
1596                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1597                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1598                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1599                 $cmd .= " $env->{CONFIGURATION}";
1600                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1601                 unless (system($cmd) == 0) {
1602                         warn("Failed to exec kcc\n$cmd");
1603                         return undef;
1604                 }
1605
1606                 # as 'vampired' dc may add data in its local replica
1607                 # we need to synchronize data between DCs
1608                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1609                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1610                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1611                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{VAMPIRE_DC_SERVER}";
1612                 $cmd .= " $dc_vars->{CONFIGURATION}";
1613                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1614                 # replicate Configuration NC
1615                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
1616                 unless(system($cmd_repl) == 0) {
1617                         warn("Failed to replicate\n$cmd_repl");
1618                         return undef;
1619                 }
1620                 # replicate Default NC
1621                 $cmd_repl = "$cmd \"$base_dn\"";
1622                 unless(system($cmd_repl) == 0) {
1623                         warn("Failed to replicate\n$cmd_repl");
1624                         return undef;
1625                 }
1626         }
1627
1628         return $env;
1629 }
1630
1631 sub setup_subdom_dc($$$)
1632 {
1633         my ($self, $path, $dc_vars) = @_;
1634
1635         my $env = $self->provision_subdom_dc($path, $dc_vars);
1636
1637         if (defined $env) {
1638                 $self->check_or_start($env, "single");
1639
1640                 $self->wait_for_start($env);
1641
1642                 $self->{vars}->{subdom_dc} = $env;
1643
1644                 # force replicated DC to update repsTo/repsFrom
1645                 # for primary domain partitions
1646                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1647                 my $cmd = "";
1648                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1649                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1650                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1651                 $cmd .= " $env->{CONFIGURATION}";
1652                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
1653                 unless (system($cmd) == 0) {
1654                         warn("Failed to exec kcc\n$cmd");
1655                         return undef;
1656                 }
1657
1658                 # as 'subdomain' dc may add data in its local replica
1659                 # we need to synchronize data between DCs
1660                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
1661                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1662                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1663                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1664                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
1665                 $cmd .= " $dc_vars->{CONFIGURATION}";
1666                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
1667                 # replicate Configuration NC
1668                 my $cmd_repl = "$cmd \"$config_dn\"";
1669                 unless(system($cmd_repl) == 0) {
1670                         warn("Failed to replicate\n$cmd_repl");
1671                         return undef;
1672                 }
1673                 # replicate Default NC
1674                 $cmd_repl = "$cmd \"$base_dn\"";
1675                 unless(system($cmd_repl) == 0) {
1676                         warn("Failed to replicate\n$cmd_repl");
1677                         return undef;
1678                 }
1679         }
1680
1681         return $env;
1682 }
1683
1684 sub setup_rodc($$$)
1685 {
1686         my ($self, $path, $dc_vars) = @_;
1687
1688         my $env = $self->provision_rodc($path, $dc_vars);
1689
1690         unless ($env) {
1691                 return undef;
1692         }
1693
1694         $self->check_or_start($env, "single");
1695
1696         $self->wait_for_start($env);
1697
1698         $self->{vars}->{rodc} = $env;
1699
1700         return $env;
1701 }
1702
1703 sub setup_plugin_s4_dc($$)
1704 {
1705         my ($self, $path) = @_;
1706
1707         my $env = $self->provision_plugin_s4_dc($path);
1708         if (defined $env) {
1709                 $self->check_or_start($env, "single");
1710
1711                 $self->wait_for_start($env);
1712
1713                 my $s3_part_env = $self->{target3}->setup_plugin_s4_dc($path, $env, 30);
1714                 if (not defined($s3_part_env)) {
1715                     return undef;
1716                 }
1717
1718                 $self->{vars}->{plugin_s4_dc} = $s3_part_env;
1719         }
1720         return $env;
1721 }
1722
1723 1;