s4-selftest: don't run rndc reload in selftest
[ira/wip.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
13 sub new($$$$$) {
14         my ($classname, $bindir, $ldap, $setupdir, $exeext) = @_;
15         $exeext = "" unless defined($exeext);
16         my $self = { 
17                 vars => {}, 
18                 ldap => $ldap, 
19                 bindir => $bindir, 
20                 setupdir => $setupdir,
21                 exeext => $exeext
22         };
23         bless $self;
24         return $self;
25 }
26
27 sub bindir_path($$) {
28         my ($self, $path) = @_;
29
30         return "$self->{bindir}/$path$self->{exeext}";
31 }
32
33 sub openldap_start($$$) {
34 }
35
36 sub slapd_start($$)
37 {
38         my $count = 0;
39         my ($self, $env_vars) = @_;
40         my $ldbsearch = $self->bindir_path("ldbsearch");
41
42         my $uri = $env_vars->{LDAP_URI};
43
44         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
45             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
46             return 1;
47         }
48         # running slapd in the background means it stays in the same process group, so it can be
49         # killed by timelimit
50         if ($self->{ldap} eq "fedora-ds") {
51                 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 &");
52         } elsif ($self->{ldap} eq "openldap") {
53                 system("$ENV{OPENLDAP_SLAPD} -d0 -F $env_vars->{SLAPD_CONF_D} -h $uri > $env_vars->{LDAPDIR}/logs 2>&1 &");
54         }
55         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
56                 $count++;
57                 if ($count > 40) {
58                     $self->slapd_stop($env_vars);
59                     return 0;
60                 }
61                 sleep(1);
62         }
63         return 1;
64 }
65
66 sub slapd_stop($$)
67 {
68         my ($self, $envvars) = @_;
69         if ($self->{ldap} eq "fedora-ds") {
70                 system("$envvars->{LDAPDIR}/slapd-samba4/stop-slapd");
71         } elsif ($self->{ldap} eq "openldap") {
72                 open(IN, "<$envvars->{OPENLDAP_PIDFILE}") or 
73                         die("unable to open slapd pid file: $envvars->{OPENLDAP_PIDFILE}");
74                 kill 9, <IN>;
75                 close(IN);
76         }
77         return 1;
78 }
79
80 sub check_or_start($$$) 
81 {
82         my ($self, $env_vars, $max_time) = @_;
83         return 0 if ( -p $env_vars->{SAMBA_TEST_FIFO});
84
85         unlink($env_vars->{SAMBA_TEST_FIFO});
86         POSIX::mkfifo($env_vars->{SAMBA_TEST_FIFO}, 0700);
87         unlink($env_vars->{SAMBA_TEST_LOG});
88         
89         print "STARTING SAMBA... ";
90         my $pid = fork();
91         if ($pid == 0) {
92                 open STDIN, $env_vars->{SAMBA_TEST_FIFO};
93                 # we want out from samba to go to the log file, but also
94                 # to the users terminal when running 'make test' on the command
95                 # line. This puts it on stderr on the terminal
96                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
97                 open STDERR, '>&STDOUT';
98
99                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
100
101                 my $valgrind = "";
102                 if (defined($ENV{SAMBA_VALGRIND})) {
103                     $valgrind = $ENV{SAMBA_VALGRIND};
104                 } 
105
106                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG}; 
107                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
108
109                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
110                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
111
112                 $ENV{UID_WRAPPER} = "1";
113
114                 # Start slapd before samba, but with the fifo on stdin
115                 if (defined($self->{ldap})) {
116                     $self->slapd_start($env_vars) or 
117                         die("couldn't start slapd (main run)");
118                 }
119
120                 my $optarg = "";
121                 if (defined($max_time)) {
122                         $optarg = "--maximum-runtime=$max_time ";
123                 }
124                 if (defined($ENV{SAMBA_OPTIONS})) {
125                         $optarg.= " $ENV{SAMBA_OPTIONS}";
126                 }
127                 my $samba = $self->bindir_path("samba");
128
129                 # allow selection of the process model using
130                 # the environment varibale SAMBA_PROCESS_MODEL
131                 # that allows us to change the process model for 
132                 # individual machines in the build farm
133                 my $model = "single";
134                 if (defined($ENV{SAMBA_PROCESS_MODEL})) {
135                         $model = $ENV{SAMBA_PROCESS_MODEL};
136                 }
137                 my $ret = system("$valgrind $samba $optarg $env_vars->{CONFIGURATION} -M $model -i");
138                 if ($? == -1) {
139                         print "Unable to start $samba: $ret: $!\n";
140                         exit 1;
141                 }
142                 unlink($env_vars->{SAMBA_TEST_FIFO});
143                 my $exit = $? >> 8;
144                 if ( $ret == 0 ) {
145                         print "$samba exits with status $exit\n";
146                 } elsif ( $ret & 127 ) {
147                         print "$samba got signal ".($ret & 127)." and exits with $exit!\n";
148                 } else {
149                         $ret = $? >> 8;
150                         print "$samba failed with status $exit!\n";
151                 }
152                 exit $exit;
153         }
154         print "DONE\n";
155
156         open(DATA, ">$env_vars->{SAMBA_TEST_FIFO}");
157
158         return $pid;
159 }
160
161 sub wait_for_start($$)
162 {
163         my ($self, $testenv_vars) = @_;
164         # give time for nbt server to register its names
165         print "delaying for nbt name registration\n";
166         sleep 2;
167
168         # This will return quickly when things are up, but be slow if we 
169         # need to wait for (eg) SSL init 
170         my $nmblookup = $self->bindir_path("nmblookup");
171         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
172         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
173         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
174         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
175         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSALIAS}");
176         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSALIAS}");
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->{NETBIOSALIAS}");
182         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSALIAS}");
183
184         print $self->getlog_env($testenv_vars);
185 }
186
187 sub write_ldb_file($$$)
188 {
189         my ($self, $file, $ldif) = @_;
190
191         my $ldbadd = $self->bindir_path("ldbadd");
192         open(LDIF, "|$ldbadd -H $file >/dev/null");
193         print LDIF $ldif;
194         return close(LDIF);
195 }
196
197 sub add_wins_config($$)
198 {
199         my ($self, $privatedir) = @_;
200
201         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
202 dn: name=TORTURE_6,CN=PARTNERS
203 objectClass: wreplPartner
204 name: TORTURE_6
205 address: 127.0.0.6
206 pullInterval: 0
207 pushChangeCount: 0
208 type: 0x3
209 ");
210 }
211
212 sub mk_fedora_ds($$)
213 {
214         my ($self, $ldapdir) = @_;
215
216         #Make the subdirectory be as fedora DS would expect
217         my $fedora_ds_dir = "$ldapdir/slapd-samba4";
218
219         my $pidfile = "$fedora_ds_dir/logs/slapd-samba4.pid";
220
221         return ($fedora_ds_dir, $pidfile);
222 }
223
224 sub mk_openldap($$)
225 {
226         my ($self, $ldapdir) = @_;
227
228         my $slapd_conf_d = "$ldapdir/slapd.d";
229         my $pidfile = "$ldapdir/slapd.pid";
230
231         return ($slapd_conf_d, $pidfile);
232 }
233
234 sub mk_keyblobs($$)
235 {
236         my ($self, $tlsdir) = @_;
237
238         #TLS and PKINIT crypto blobs
239         my $dhfile = "$tlsdir/dhparms.pem";
240         my $cafile = "$tlsdir/ca.pem";
241         my $certfile = "$tlsdir/cert.pem";
242         my $reqkdc = "$tlsdir/req-kdc.der";
243         my $kdccertfile = "$tlsdir/kdc.pem";
244         my $keyfile = "$tlsdir/key.pem";
245         my $adminkeyfile = "$tlsdir/adminkey.pem";
246         my $reqadmin = "$tlsdir/req-admin.der";
247         my $admincertfile = "$tlsdir/admincert.pem";
248         my $admincertupnfile = "$tlsdir/admincertupn.pem";
249
250         mkdir($tlsdir, 0777);
251
252         #This is specified here to avoid draining entropy on every run
253         open(DHFILE, ">$dhfile");
254         print DHFILE <<EOF;
255 -----BEGIN DH PARAMETERS-----
256 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
257 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
258 YX70obsCAQI=
259 -----END DH PARAMETERS-----
260 EOF
261         close(DHFILE);
262
263         #Likewise, we pregenerate the key material.  This allows the 
264         #other certificates to be pre-generated
265         open(KEYFILE, ">$keyfile");
266         print KEYFILE <<EOF;
267 -----BEGIN RSA PRIVATE KEY-----
268 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
269 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
270 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
271 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
272 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
273 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
274 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
275 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
276 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
277 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
278 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
279 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
280 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
281 -----END RSA PRIVATE KEY-----
282 EOF
283         close(KEYFILE);
284
285         open(ADMINKEYFILE, ">$adminkeyfile");
286
287         print ADMINKEYFILE <<EOF;
288 -----BEGIN RSA PRIVATE KEY-----
289 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
290 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
291 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
292 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
293 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
294 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
295 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
296 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
297 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
298 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
299 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
300 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
301 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
302 -----END RSA PRIVATE KEY-----
303 EOF
304
305         #generated with 
306         # hxtool issue-certificate --self-signed --issue-ca \
307         # --ca-private-key="FILE:$KEYFILE" \
308         # --subject="CN=CA,DC=samba,DC=example,DC=com" \
309         # --certificate="FILE:$CAFILE" --lifetime="25 years"
310
311         open(CAFILE, ">$cafile");
312         print CAFILE <<EOF;
313 -----BEGIN CERTIFICATE-----
314 MIICcTCCAdqgAwIBAgIUaBPmjnPVqyFqR5foICmLmikJTzgwCwYJKoZIhvcNAQEFMFIxEzAR
315 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
316 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTIyMzEyWhgPMjAzMzAyMjQx
317 MjIzMTJaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
318 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
319 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
320 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
321 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo0IwQDAOBgNVHQ8BAf8EBAMC
322 AaYwHQYDVR0OBBYEFMLZufegDKLZs0VOyFXYK1L6M8oyMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
323 KoZIhvcNAQEFBQADgYEAAZJbCAAkaqgFJ0xgNovn8Ydd0KswQPjicwiODPgw9ZPoD2HiOUVO
324 yYDRg/dhFF9y656OpcHk4N7qZ2sl3RlHkzDu+dseETW+CnKvQIoXNyeARRJSsSlwrwcoD4JR
325 HTLk2sGigsWwrJ2N99sG/cqSJLJ1MFwLrs6koweBnYU0f/g=
326 -----END CERTIFICATE-----
327 EOF
328
329         #generated with GNUTLS internally in Samba.  
330
331         open(CERTFILE, ">$certfile");
332         print CERTFILE <<EOF;
333 -----BEGIN CERTIFICATE-----
334 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
335 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
336 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
337 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
338 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
339 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
340 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
341 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
342 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
343 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
344 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
345 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
346 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
347 -----END CERTIFICATE-----
348 EOF
349         close(CERTFILE);
350
351         #KDC certificate
352         # hxtool request-create \
353         # --subject="CN=krbtgt,CN=users,DC=samba,DC=example,DC=com" \
354         # --key="FILE:$KEYFILE" $KDCREQ
355
356         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
357         # --type="pkinit-kdc" \
358         # --pk-init-principal="krbtgt/SAMBA.EXAMPLE.COM@SAMBA.EXAMPLE.COM" \
359         # --req="PKCS10:$KDCREQ" --certificate="FILE:$KDCCERTFILE" \
360         # --lifetime="25 years"
361
362         open(KDCCERTFILE, ">$kdccertfile");
363         print KDCCERTFILE <<EOF;
364 -----BEGIN CERTIFICATE-----
365 MIIDDDCCAnWgAwIBAgIUI2Tzj+JnMzMcdeabcNo30rovzFAwCwYJKoZIhvcNAQEFMFIxEzAR
366 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
367 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTMxOTIzWhgPMjAzMzAyMjQx
368 MzE5MjNaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
369 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
370 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
371 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
372 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
373 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
374 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
375 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
376 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
377 AQUFAAOBgQBmrVD5MCmZjfHp1nEnHqTIh8r7lSmVtDx4s9MMjxm9oNrzbKXynvdhwQYFVarc
378 ge4yRRDXtSebErOl71zVJI9CVeQQpwcH+tA85oGA7oeFtO/S7ls581RUU6tGgyxV4veD+lJv
379 KPH5LevUtgD+q9H4LU4Sq5N3iFwBaeryB0g2wg==
380 -----END CERTIFICATE-----
381 EOF
382
383         # hxtool request-create \
384         # --subject="CN=Administrator,CN=users,DC=samba,DC=example,DC=com" \
385         # --key="FILE:$ADMINKEYFILE" $ADMINREQFILE
386
387         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
388         # --type="pkinit-client" \
389         # --pk-init-principal="administrator@SAMBA.EXAMPLE.COM" \
390         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE" \
391         # --lifetime="25 years"
392         
393         open(ADMINCERTFILE, ">$admincertfile");
394         print ADMINCERTFILE <<EOF;
395 -----BEGIN CERTIFICATE-----
396 MIIDHTCCAoagAwIBAgIUUggzW4lLRkMKe1DAR2NKatkMDYwwCwYJKoZIhvcNAQELMFIxEzAR
397 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
398 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMjE1WhgPMjAzNDA3MjIw
399 MzMyMTVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
400 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
401 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
402 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
403 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
404 l3JRlwIDAQABo4HSMIHPMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
405 BgEFBQcDAgYKKwYBBAGCNxQCAjBIBgNVHREEQTA/oD0GBisGAQUCAqAzMDGgExsRU0FNQkEu
406 RVhBTVBMRS5DT02hGjAYoAMCAQGhETAPGw1BZG1pbmlzdHJhdG9yMB8GA1UdIwQYMBaAFMLZ
407 ufegDKLZs0VOyFXYK1L6M8oyMB0GA1UdDgQWBBQg81bLyfCA88C2B/BDjXlGuaFaxjAJBgNV
408 HRMEAjAAMA0GCSqGSIb3DQEBCwUAA4GBAEf/OSHUDJaGdtWGNuJeqcVYVMwrfBAc0OSwVhz1
409 7/xqKHWo8wIMPkYRtaRHKLNDsF8GkhQPCpVsa6mX/Nt7YQnNvwd+1SBP5E8GvwWw9ZzLJvma
410 nk2n89emuayLpVtp00PymrDLRBcNaRjFReQU8f0o509kiVPHduAp3jOiy13l
411 -----END CERTIFICATE-----
412 EOF
413         close(ADMINCERTFILE);
414
415         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
416         # --type="pkinit-client" \
417         # --ms-upn="administrator@samba.example.com" \
418         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTUPNFILE" \
419         # --lifetime="25 years"
420         
421         open(ADMINCERTUPNFILE, ">$admincertupnfile");
422         print ADMINCERTUPNFILE <<EOF;
423 -----BEGIN CERTIFICATE-----
424 MIIDDzCCAnigAwIBAgIUUp3CJMuNaEaAdPKp3QdNIwG7a4wwCwYJKoZIhvcNAQELMFIxEzAR
425 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
426 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMzA1WhgPMjAzNDA3MjIw
427 MzMzMDVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
428 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
429 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
430 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
431 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
432 l3JRlwIDAQABo4HEMIHBMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
433 BgEFBQcDAgYKKwYBBAGCNxQCAjA6BgNVHREEMzAxoC8GCisGAQQBgjcUAgOgIQwfYWRtaW5p
434 c3RyYXRvckBzYW1iYS5leGFtcGxlLmNvbTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS
435 +jPKMjAdBgNVHQ4EFgQUIPNWy8nwgPPAtgfwQ415RrmhWsYwCQYDVR0TBAIwADANBgkqhkiG
436 9w0BAQsFAAOBgQBk42+egeUB3Ji2PC55fbt3FNKxvmm2xUUFkV9POK/YR9rajKOwk5jtYSeS
437 Zd7J9s//rNFNa7waklFkDaY56+QWTFtdvxfE+KoHaqt6X8u6pqi7p3M4wDKQox+9Dx8yWFyq
438 Wfz/8alZ5aMezCQzXJyIaJsCLeKABosSwHcpAFmxlQ==
439 -----END CERTIFICATE-----
440 EOF
441 }
442
443 #
444 # provision_raw_prepare() is also used by Samba34.pm!
445 #
446 sub provision_raw_prepare($$$$$$$)
447 {
448         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password, $kdc_ipv4) = @_;
449         my $ctx;
450
451         -d $prefix or mkdir($prefix, 0777) or die("Unable to create $prefix");
452         my $prefix_abs = abs_path($prefix);
453
454         die ("prefix=''") if $prefix_abs eq "";
455         die ("prefix='/'") if $prefix_abs eq "/";
456
457         (system("rm -rf $prefix_abs/*") == 0) or die("Unable to clean up");
458
459         $ctx->{prefix} = $prefix;
460         $ctx->{prefix_abs} = $prefix_abs;
461         $ctx->{server_role} = $server_role;
462         $ctx->{netbiosname} = $netbiosname;
463         $ctx->{netbiosalias} = $netbiosalias;
464         $ctx->{swiface} = $swiface;
465         $ctx->{password} = $password;
466         $ctx->{kdc_ipv4} = $kdc_ipv4;
467
468         $ctx->{server_loglevel} = 1;
469         $ctx->{username} = "Administrator";
470         $ctx->{domain} = "SAMBADOMAIN";
471         $ctx->{realm} = "SAMBA.EXAMPLE.COM";
472         $ctx->{dnsname} = "samba.example.com";
473         $ctx->{basedn} = "dc=samba,dc=example,dc=com";
474         $ctx->{sid_generator} = "internal";
475
476         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
477         chomp $unix_name;
478         $ctx->{unix_name} = $unix_name;
479         $ctx->{unix_uid} = $>;
480         $ctx->{unix_gids_str} = $);
481         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
482
483         $ctx->{etcdir} = "$prefix_abs/etc";
484         $ctx->{piddir} = "$prefix_abs/pid";
485         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
486         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
487         $ctx->{privatedir} = "$prefix_abs/private";
488         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
489         $ctx->{lockdir} = "$prefix_abs/lockdir";
490         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
491         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
492         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
493         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
494         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
495
496         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
497
498         $ctx->{ipv4} = "127.0.0.$swiface";
499         $ctx->{interfaces} = "$ctx->{ipv4}/8";
500
501         $ctx->{localbasedn} = $ctx->{basedn};
502         $ctx->{localbasedn} = "CN=$netbiosname" if $server_role eq "member server";
503
504         push(@{$ctx->{directories}}, $ctx->{privatedir});
505         push(@{$ctx->{directories}}, $ctx->{etcdir});
506         push(@{$ctx->{directories}}, $ctx->{piddir});
507         push(@{$ctx->{directories}}, $ctx->{ncalrpcdir});
508         push(@{$ctx->{directories}}, $ctx->{lockdir});
509
510         $ctx->{smb_conf_extra_options} = "";
511
512         my @provision_options = ();
513         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
514         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
515         if (defined($ENV{GDB_PROVISION})) {
516                 push (@provision_options, "gdb --args");
517         }
518         if (defined($ENV{VALGRIND_PROVISION})) {
519                 push (@provision_options, "valgrind");
520         }
521         if (defined($ENV{PYTHON})) {
522                 push (@provision_options, $ENV{PYTHON});
523         }
524         push (@provision_options, "$self->{setupdir}/provision");
525         push (@provision_options, "--configfile=$ctx->{smb_conf}");
526         push (@provision_options, "--host-name=$ctx->{netbiosname}");
527         push (@provision_options, "--host-ip=$ctx->{ipv4}");
528         push (@provision_options, "--quiet");
529         push (@provision_options, "--domain=$ctx->{domain}");
530         push (@provision_options, "--realm=$ctx->{realm}");
531         push (@provision_options, "--adminpass=$ctx->{password}");
532         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
533         push (@provision_options, "--machinepass=machine$ctx->{password}");
534         push (@provision_options, "--root=$ctx->{unix_name}");
535         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
536
537         @{$ctx->{provision_options}} = @provision_options;
538
539         return $ctx;
540 }
541
542 #
543 # provision_raw_step1() is also used by Samba34.pm!
544 #
545 # Step1 creates the basic configuration
546 #
547 sub provision_raw_step1($$)
548 {
549         my ($self, $ctx) = @_;
550
551         mkdir($_, 0777) foreach (@{$ctx->{directories}});
552
553         open(CONFFILE, ">$ctx->{smb_conf}")
554                 or die("can't open $ctx->{smb_conf}$?");
555         print CONFFILE "
556 [global]
557         netbios name = $ctx->{netbiosname}
558         netbios aliases = $ctx->{netbiosalias}
559         posix:eadb = $ctx->{lockdir}/eadb.tdb
560         workgroup = $ctx->{domain}
561         realm = $ctx->{realm}
562         private dir = $ctx->{privatedir}
563         pid directory = $ctx->{piddir}
564         ncalrpc dir = $ctx->{ncalrpcdir}
565         lock dir = $ctx->{lockdir}
566         setup directory = $self->{setupdir}
567         modules dir = $self->{bindir}/modules
568         winbindd socket directory = $ctx->{winbindd_socket_dir}
569         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
570         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
571         winbind separator = /
572         name resolve order = bcast
573         interfaces = $ctx->{interfaces}
574         tls dh params file = $ctx->{tlsdir}/dhparms.pem
575         panic action = $RealBin/gdb_backtrace \%PID% \%PROG%
576         wins support = yes
577         server role = $ctx->{server_role}
578         notify:inotify = false
579         ldb:nosync = true
580 #We don't want to pass our self-tests if the PAC code is wrong
581         gensec:require_pac = true
582         log level = $ctx->{server_loglevel}
583         lanman auth = Yes
584         dnsupdate:rndc reload command = /bin/true
585 ";
586
587         if (defined($ctx->{sid_generator}) && $ctx->{sid_generator} ne "internal") {
588                 print CONFFILE "
589         sid generator = $ctx->{sid_generator}";
590         }
591
592         print CONFFILE "
593
594         # Begin extra options
595         $ctx->{smb_conf_extra_options}
596         # End extra options
597 ";
598         close(CONFFILE);
599
600         $self->mk_keyblobs($ctx->{tlsdir});
601
602         open(KRB5CONF, ">$ctx->{krb5_conf}")
603                 or die("can't open $ctx->{krb5_conf}$?");
604         print KRB5CONF "
605 #Generated krb5.conf for $ctx->{realm}
606
607 [libdefaults]
608  default_realm = $ctx->{realm}
609  dns_lookup_realm = false
610  dns_lookup_kdc = false
611  ticket_lifetime = 24h
612  forwardable = yes
613
614 [realms]
615  $ctx->{realm} = {
616   kdc = $ctx->{kdc_ipv4}:88
617   admin_server = $ctx->{kdc_ipv4}:88
618   default_domain = $ctx->{dnsname}
619  }
620  $ctx->{dnsname} = {
621   kdc = $ctx->{kdc_ipv4}:88
622   admin_server = $ctx->{kdc_ipv4}:88
623   default_domain = $ctx->{dnsname}
624  }
625  $ctx->{domain} = {
626   kdc = $ctx->{kdc_ipv4}:88
627   admin_server = $ctx->{kdc_ipv4}:88
628   default_domain = $ctx->{dnsname}
629  }
630
631 [appdefaults]
632         pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
633
634 [kdc]
635         enable-pkinit = true
636         pkinit_identity = FILE:$ctx->{tlsdir}/kdc.pem,$ctx->{tlsdir}/key.pem
637         pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
638
639 [domain_realm]
640  .$ctx->{dnsname} = $ctx->{realm}
641 ";
642         close(KRB5CONF);
643
644         open(PWD, ">$ctx->{nsswrap_passwd}");
645         print PWD "
646 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
647 $ctx->{unix_name}:x:$ctx->{unix_uid}:@{$ctx->{unix_gids}}[0]:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false
648 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
649 ";
650         close(PWD);
651
652         open(GRP, ">$ctx->{nsswrap_group}");
653         print GRP "
654 root:x:0:
655 wheel:x:10:
656 users:x:100:
657 nobody:x:65533:
658 nogroup:x:65534:nobody
659 ";
660         close(GRP);
661
662         my $configuration = "--configfile=$ctx->{smb_conf}";
663
664 #Ensure the config file is valid before we start
665         my $testparm = $self->bindir_path("testparm");
666         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
667                 system("$testparm -v --suppress-prompt $configuration >&2");
668                 die("Failed to create a valid smb.conf configuration $testparm!");
669         }
670
671         (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) or die("Failed to create a valid smb.conf configuration! $self->{bindir}/testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
672
673         my $ret = {
674                 KRB5_CONFIG => $ctx->{krb5_conf},
675                 PIDDIR => $ctx->{piddir},
676                 SERVER => $ctx->{netbiosname},
677                 SERVER_IP => $ctx->{ipv4},
678                 NETBIOSNAME => $ctx->{netbiosname},
679                 NETBIOSALIAS => $ctx->{netbiosalias},
680                 DOMAIN => $ctx->{domain},
681                 USERNAME => $ctx->{username},
682                 REALM => $ctx->{realm},
683                 PASSWORD => $ctx->{password},
684                 LDAPDIR => $ctx->{ldapdir},
685                 WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
686                 NCALRPCDIR => $ctx->{ncalrpcdir},
687                 LOCKDIR => $ctx->{lockdir},
688                 SERVERCONFFILE => $ctx->{smb_conf},
689                 CONFIGURATION => $configuration,
690                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
691                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
692                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
693                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
694                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
695                 SAMBA_TEST_LOG_POS => 0,
696         };
697
698         return $ret;
699 }
700
701 #
702 # provision_raw_step2() is also used by Samba34.pm!
703 #
704 # Step2 runs the provision script
705 #
706 sub provision_raw_step2($$$)
707 {
708         my ($self, $ctx, $ret) = @_;
709
710         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
711         (system($provision_cmd) == 0) or die("Unable to provision: \n$provision_cmd\n");
712
713         return $ret;
714 }
715
716 sub provision($$$$$$$)
717 {
718         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password, $kdc_ipv4) = @_;
719
720         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
721                                                $netbiosname, $netbiosalias,
722                                                $swiface, $password, $kdc_ipv4);
723
724         $ctx->{tmpdir} = "$ctx->{prefix_abs}/tmp";
725         push(@{$ctx->{directories}}, "$ctx->{tmpdir}");
726         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test1");
727         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test2");
728
729         $ctx->{smb_conf_extra_options} = "
730
731         max xmit = 32K
732         server max protocol = SMB2
733
734 [tmp]
735         path = $ctx->{tmpdir}
736         read only = no
737         posix:sharedelay = 10000
738         posix:oplocktimeout = 3
739         posix:writetimeupdatedelay = 500000
740
741 [test1]
742         path = $ctx->{tmpdir}/test1
743         read only = no
744         posix:sharedelay = 10000
745         posix:oplocktimeout = 3
746         posix:writetimeupdatedelay = 50000
747
748 [test2]
749         path = $ctx->{tmpdir}/test2
750         read only = no
751         posix:sharedelay = 10000
752         posix:oplocktimeout = 3
753         posix:writetimeupdatedelay = 50000
754
755 [cifs]
756         read only = no
757         ntvfs handler = cifs
758         cifs:server = $ctx->{netbiosname}
759         cifs:share = tmp
760 #There is no username specified here, instead the client is expected
761 #to log in with kerberos, and the serverwill use delegated credentials.
762
763 [simple]
764         path = $ctx->{tmpdir}
765         read only = no
766         ntvfs handler = simple
767
768 [sysvol]
769         path = $ctx->{lockdir}/sysvol
770         read only = yes
771
772 [netlogon]
773         path = $ctx->{lockdir}/sysvol/$ctx->{dnsname}/scripts
774         read only = no
775
776 [cifsposix]
777         copy = simple
778         ntvfs handler = cifsposix
779 ";
780
781         if (defined($self->{ldap})) {
782                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
783                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
784
785                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
786                 $ldap_uri =~ s|/|%2F|g;
787                 $ldap_uri = "ldapi://$ldap_uri";
788                 $ctx->{ldap_uri} = $ldap_uri;
789
790                 if ($self->{ldap} eq "fedora-ds") {
791                         $ctx->{sid_generator} = "backend";
792                 }
793         }
794
795         my $ret = $self->provision_raw_step1($ctx);
796
797         if (defined($self->{ldap})) {
798                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
799                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
800                 if ($self->{ldap} eq "openldap") {
801                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
802                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx->{ldapdir}) or die("Unable to create openldap directories");
803
804                 } elsif ($self->{ldap} eq "fedora-ds") {
805                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
806                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
807                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx->{ldapdir}) or die("Unable to create fedora ds directories");
808                 }
809
810         }
811
812         $ret = $self->provision_raw_step2($ctx, $ret);
813
814         return $ret;
815 }
816
817 sub provision_member($$$)
818 {
819         my ($self, $prefix, $dcvars) = @_;
820         print "PROVISIONING MEMBER...";
821
822         my $ret = $self->provision($prefix,
823                                    "member server",
824                                    "localmember3",
825                                    "localmember",
826                                    3,
827                                    "localmemberpass",
828                                    $dcvars->{SERVER_IP});
829
830         $ret or die("Unable to provision");
831
832         my $net = $self->bindir_path("net");
833         my $cmd = "";
834         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
835         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
836         $cmd .= "$net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
837         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
838
839         system($cmd) == 0 or die("Join failed\n$cmd");
840
841         $ret->{DC_SERVER} = $dcvars->{SERVER};
842         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
843         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
844         $ret->{DC_NETBIOSALIAS} = $dcvars->{NETBIOSALIAS};
845         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
846         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
847
848         return $ret;
849 }
850
851 sub provision_dc($$)
852 {
853         my ($self, $prefix) = @_;
854
855         print "PROVISIONING DC...";
856         my $ret = $self->provision($prefix,
857                                    "domain controller",
858                                    "localdc1",
859                                    "localdc",
860                                    1,
861                                    "localdcpass",
862                                    "127.0.0.1");
863
864         $self->add_wins_config("$prefix/private") or 
865                 die("Unable to add wins configuration");
866
867         return $ret;
868 }
869
870 sub teardown_env($$)
871 {
872         my ($self, $envvars) = @_;
873         my $pid;
874
875         close(DATA);
876
877         if (-f "$envvars->{PIDDIR}/samba.pid" ) {
878                 open(IN, "<$envvars->{PIDDIR}/samba.pid") or die("unable to open server pid file");
879                 $pid = <IN>;
880                 close(IN);
881
882                 # Give the process 20 seconds to exit.  gcov needs
883                 # this time to write out the covarge data
884                 my $count = 0;
885                 until (kill(0, $pid) == 0) {
886                     # if no process sucessfully signalled, then we are done
887                     sleep(1);
888                     $count++;
889                     last if $count > 20;
890                 }
891                 
892                 # If it is still around, kill it
893                 if ($count > 20) {
894                     print "server process $pid took more than $count seconds to exit, killing\n";
895                     kill 9, $pid;
896                 }
897         }
898
899         my $failed = $? >> 8;
900
901         $self->slapd_stop($envvars) if ($self->{ldap});
902
903         print $self->getlog_env($envvars);
904
905         return $failed;
906 }
907
908 sub getlog_env($$)
909 {
910         my ($self, $envvars) = @_;
911         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
912         my $out = $title;
913
914         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
915
916         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
917         while (<LOG>) {
918                 $out .= $_;
919         }
920         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
921         close(LOG);
922
923         return "" if $out eq $title;
924  
925         return $out;
926 }
927
928 sub check_env($$)
929 {
930         my ($self, $envvars) = @_;
931
932         return 1 if (-p $envvars->{SAMBA_TEST_FIFO});
933
934         print $self->getlog_env($envvars);
935
936         return 0;
937 }
938
939 sub setup_env($$$)
940 {
941         my ($self, $envname, $path) = @_;
942
943         if ($envname eq "dc") {
944                 return $self->setup_dc("$path/dc");
945         } elsif ($envname eq "member") {
946                 if (not defined($self->{vars}->{dc})) {
947                         $self->setup_dc("$path/dc");
948                 }
949                 return $self->setup_member("$path/member", $self->{vars}->{dc});
950         } else {
951                 die("Samba4 can't provide environment '$envname'");
952         }
953 }
954
955 sub setup_member($$$$)
956 {
957         my ($self, $path, $dc_vars) = @_;
958
959         my $env = $self->provision_member($path, $dc_vars);
960
961         $self->check_or_start($env, ($ENV{SMBD_MAXTIME} or 7500));
962
963         $self->wait_for_start($env);
964
965         return $env;
966 }
967
968 sub setup_dc($$)
969 {
970         my ($self, $path) = @_;
971
972         my $env = $self->provision_dc($path);
973
974         $self->check_or_start($env, 
975                 ($ENV{SMBD_MAXTIME} or 7500));
976
977         $self->wait_for_start($env);
978
979         $self->{vars}->{dc} = $env;
980
981         return $env;
982 }
983
984 sub stop($)
985 {
986         my ($self) = @_;
987 }
988
989 1;