s4:selftest Make OpenLDAP guess it's own modules from now on
[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         my ($slapd_conf, $uri, $logs) = @_;
35         my $oldpath = $ENV{PATH};
36         my $olroot = "";
37         my $olpath = "";
38         if (defined $ENV{OPENLDAP_ROOT}) {
39             $olroot = "$ENV{OPENLDAP_ROOT}";
40             $olpath = "$olroot/libexec:$olroot/sbin:";
41         }
42         $ENV{PATH} = "$olpath/usr/local/sbin:/usr/sbin:/sbin:$ENV{PATH}";
43         system("slapd -d0 -f $slapd_conf -h $uri > $logs 2>&1 &");
44         $ENV{PATH} = $oldpath;
45 }
46
47 sub slapd_start($$)
48 {
49         my $count = 0;
50         my ($self, $env_vars) = @_;
51
52         my $uri = $env_vars->{LDAP_URI};
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                 openldap_start($env_vars->{SLAPD_CONF}, $uri, "$env_vars->{LDAPDIR}/logs");
60         }
61         my $ldbsearch = $self->bindir_path("ldbsearch");
62         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
63                 $count++;
64                 if ($count > 40) {
65                     $self->slapd_stop($env_vars);
66                     return 0;
67                 }
68                 sleep(1);
69         }
70         return 1;
71 }
72
73 sub slapd_stop($$)
74 {
75         my ($self, $envvars) = @_;
76         if ($self->{ldap} eq "fedora-ds") {
77                 system("$envvars->{LDAPDIR}/slapd-samba4/stop-slapd");
78         } elsif ($self->{ldap} eq "openldap") {
79                 open(IN, "<$envvars->{OPENLDAP_PIDFILE}") or 
80                         die("unable to open slapd pid file: $envvars->{OPENLDAP_PIDFILE}");
81                 kill 9, <IN>;
82                 close(IN);
83         }
84         return 1;
85 }
86
87 sub check_or_start($$$) 
88 {
89         my ($self, $env_vars, $max_time) = @_;
90         return 0 if ( -p $env_vars->{SAMBA_TEST_FIFO});
91
92         unlink($env_vars->{SAMBA_TEST_FIFO});
93         POSIX::mkfifo($env_vars->{SAMBA_TEST_FIFO}, 0700);
94         unlink($env_vars->{SAMBA_TEST_LOG});
95         
96         print "STARTING SAMBA... ";
97         my $pid = fork();
98         if ($pid == 0) {
99                 open STDIN, $env_vars->{SAMBA_TEST_FIFO};
100                 open STDOUT, ">$env_vars->{SAMBA_TEST_LOG}";
101                 open STDERR, '>&STDOUT';
102                 
103                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
104
105                 my $valgrind = "";
106                 if (defined($ENV{SAMBA_VALGRIND})) {
107                     $valgrind = $ENV{SAMBA_VALGRIND};
108                 } 
109
110                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG}; 
111                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
112
113                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
114                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
115
116                 $ENV{UID_WRAPPER} = "1";
117
118                 # Start slapd before samba, but with the fifo on stdin
119                 if (defined($self->{ldap})) {
120                     $self->slapd_start($env_vars) or 
121                         die("couldn't start slapd (2nd time)");
122                 }
123
124                 my $optarg = "";
125                 if (defined($max_time)) {
126                         $optarg = "--maximum-runtime=$max_time ";
127                 }
128                 if (defined($ENV{SAMBA_OPTIONS})) {
129                         $optarg.= " $ENV{SAMBA_OPTIONS}";
130                 }
131                 my $samba = $self->bindir_path("samba");
132                 my $ret = system("$valgrind $samba $optarg $env_vars->{CONFIGURATION} -M single -i --leak-report-full");
133                 if ($? == -1) {
134                         print "Unable to start $samba: $ret: $!\n";
135                         exit 1;
136                 }
137                 unlink($env_vars->{SAMBA_TEST_FIFO});
138                 my $exit = $? >> 8;
139                 if ( $ret == 0 ) {
140                         print "$samba exits with status $exit\n";
141                 } elsif ( $ret & 127 ) {
142                         print "$samba got signal ".($ret & 127)." and exits with $exit!\n";
143                 } else {
144                         $ret = $? >> 8;
145                         print "$samba failed with status $exit!\n";
146                 }
147                 exit $exit;
148         }
149         print "DONE\n";
150
151         open(DATA, ">$env_vars->{SAMBA_TEST_FIFO}");
152
153         return $pid;
154 }
155
156 sub wait_for_start($$)
157 {
158         my ($self, $testenv_vars) = @_;
159         # give time for nbt server to register its names
160         print "delaying for nbt name registration\n";
161         sleep 2;
162
163         # This will return quickly when things are up, but be slow if we 
164         # need to wait for (eg) SSL init 
165         my $nmblookup = $self->bindir_path("nmblookup");
166         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
167         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
168         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
169         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
170         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSALIAS}");
171         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSALIAS}");
172         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
173         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
174         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
175         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
176         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSALIAS}");
177         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSALIAS}");
178
179         print $self->getlog_env($testenv_vars);
180 }
181
182 sub write_ldb_file($$$)
183 {
184         my ($self, $file, $ldif) = @_;
185
186         my $ldbadd = $self->bindir_path("ldbadd");
187         open(LDIF, "|$ldbadd -H $file >/dev/null");
188         print LDIF $ldif;
189         return close(LDIF);
190 }
191
192 sub add_wins_config($$)
193 {
194         my ($self, $privatedir) = @_;
195
196         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
197 dn: name=TORTURE_6,CN=PARTNERS
198 objectClass: wreplPartner
199 name: TORTURE_6
200 address: 127.0.0.6
201 pullInterval: 0
202 pushChangeCount: 0
203 type: 0x3
204 ");
205 }
206
207 sub mk_fedora_ds($$$)
208 {
209         my ($self, $ldapdir, $configuration) = @_;
210
211         my $fedora_ds_inf = "$ldapdir/fedorads.inf";
212         my $fedora_ds_extra_ldif = "$ldapdir/fedorads-partitions.ldif";
213
214         #Make the subdirectory be as fedora DS would expect
215         my $fedora_ds_dir = "$ldapdir/slapd-samba4";
216
217         my $pidfile = "$fedora_ds_dir/logs/slapd-samba4.pid";
218
219 my $dir = getcwd();
220 chdir "$ENV{FEDORA_DS_ROOT}/bin" || die;
221         if (system("perl $ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl --silent --file=$fedora_ds_inf >&2") != 0) {
222             chdir $dir;
223             die("perl $ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl --silent --file=$fedora_ds_inf FAILED: $?");
224         }
225         chdir $dir || die;
226
227         return ($fedora_ds_dir, $pidfile);
228 }
229
230 sub mk_openldap($$$)
231 {
232         my ($self, $ldapdir, $configuration) = @_;
233
234         my $slapd_conf = "$ldapdir/slapd.conf";
235         my $pidfile = "$ldapdir/slapd.pid";
236
237         return ($slapd_conf, $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 #
450 # provision_raw_prepare() is also used by Samba34.pm!
451 #
452 sub provision_raw_prepare($$$$$$$)
453 {
454         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password, $kdc_ipv4) = @_;
455         my $ctx;
456
457         -d $prefix or mkdir($prefix, 0777) or die("Unable to create $prefix");
458         my $prefix_abs = abs_path($prefix);
459
460         die ("prefix=''") if $prefix_abs eq "";
461         die ("prefix='/'") if $prefix_abs eq "/";
462
463         (system("rm -rf $prefix_abs/*") == 0) or die("Unable to clean up");
464
465         $ctx->{prefix} = $prefix;
466         $ctx->{prefix_abs} = $prefix_abs;
467         $ctx->{server_role} = $server_role;
468         $ctx->{netbiosname} = $netbiosname;
469         $ctx->{netbiosalias} = $netbiosalias;
470         $ctx->{swiface} = $swiface;
471         $ctx->{password} = $password;
472         $ctx->{kdc_ipv4} = $kdc_ipv4;
473
474         $ctx->{server_loglevel} = 1;
475         $ctx->{username} = "Administrator";
476         $ctx->{domain} = "SAMBADOMAIN";
477         $ctx->{realm} = "SAMBA.EXAMPLE.COM";
478         $ctx->{dnsname} = "samba.example.com";
479         $ctx->{basedn} = "dc=samba,dc=example,dc=com";
480
481         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
482         chomp $unix_name;
483         $ctx->{unix_name} = $unix_name;
484         $ctx->{unix_uid} = $>;
485         $ctx->{unix_gids_str} = $);
486         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
487
488         $ctx->{etcdir} = "$prefix_abs/etc";
489         $ctx->{piddir} = "$prefix_abs/pid";
490         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
491         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
492         $ctx->{privatedir} = "$prefix_abs/private";
493         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
494         $ctx->{lockdir} = "$prefix_abs/lockdir";
495         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
496         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
497         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
498         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
499         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
500
501         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
502
503         $ctx->{ipv4} = "127.0.0.$swiface";
504         $ctx->{interfaces} = "$ctx->{ipv4}/8";
505
506         $ctx->{localbasedn} = $ctx->{basedn};
507         $ctx->{localbasedn} = "CN=$netbiosname" if $server_role eq "member server";
508
509         push(@{$ctx->{directories}}, $ctx->{privatedir});
510         push(@{$ctx->{directories}}, $ctx->{etcdir});
511         push(@{$ctx->{directories}}, $ctx->{piddir});
512         push(@{$ctx->{directories}}, $ctx->{ncalrpcdir});
513         push(@{$ctx->{directories}}, $ctx->{lockdir});
514
515         $ctx->{smb_conf_extra_options} = "";
516
517         my @provision_options = ();
518         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
519         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
520         if (defined($ENV{GDB_PROVISION})) {
521                 push (@provision_options, "gdb --args");
522         }
523         if (defined($ENV{VALGRIND_PROVISION})) {
524                 push (@provision_options, "valgrind");
525         }
526         if (defined($ENV{PYTHON})) {
527                 push (@provision_options, $ENV{PYTHON});
528         }
529         push (@provision_options, "$self->{setupdir}/provision");
530         push (@provision_options, "--configfile=$ctx->{smb_conf}");
531         push (@provision_options, "--host-name=$ctx->{netbiosname}");
532         push (@provision_options, "--host-ip=$ctx->{ipv4}");
533         push (@provision_options, "--quiet");
534         push (@provision_options, "--domain=$ctx->{domain}");
535         push (@provision_options, "--realm=$ctx->{realm}");
536         push (@provision_options, "--adminpass=$ctx->{password}");
537         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
538         push (@provision_options, "--machinepass=machine$ctx->{password}");
539         push (@provision_options, "--root=$ctx->{unix_name}");
540         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
541
542         @{$ctx->{provision_options}} = @provision_options;
543
544         return $ctx;
545 }
546
547 #
548 # provision_raw_step1() is also used by Samba34.pm!
549 #
550 # Step1 creates the basic configuration
551 #
552 sub provision_raw_step1($$)
553 {
554         my ($self, $ctx) = @_;
555
556         mkdir($_, 0777) foreach (@{$ctx->{directories}});
557
558         open(CONFFILE, ">$ctx->{smb_conf}")
559                 or die("can't open $ctx->{smb_conf}$?");
560         print CONFFILE "
561 [global]
562         netbios name = $ctx->{netbiosname}
563         netbios aliases = $ctx->{netbiosalias}
564         workgroup = $ctx->{domain}
565         realm = $ctx->{realm}
566         private dir = $ctx->{privatedir}
567         pid directory = $ctx->{piddir}
568         ncalrpc dir = $ctx->{ncalrpcdir}
569         lock dir = $ctx->{lockdir}
570         setup directory = $self->{setupdir}
571         modules dir = $self->{bindir}/modules
572         winbindd socket directory = $ctx->{winbindd_socket_dir}
573         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
574         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
575         winbind separator = /
576         name resolve order = bcast
577         interfaces = $ctx->{interfaces}
578         tls dh params file = $ctx->{tlsdir}/dhparms.pem
579         panic action = $RealBin/gdb_backtrace \%PID% \%PROG%
580         wins support = yes
581         server role = $ctx->{server_role}
582         notify:inotify = false
583         ldb:nosync = true
584 #We don't want to pass our self-tests if the PAC code is wrong
585         gensec:require_pac = true
586         log level = $ctx->{server_loglevel}
587         lanman auth = Yes
588
589         # Begin extra options
590         $ctx->{smb_conf_extra_options}
591         # End extra options
592 ";
593         close(CONFFILE);
594
595         $self->mk_keyblobs($ctx->{tlsdir});
596
597         open(KRB5CONF, ">$ctx->{krb5_conf}")
598                 or die("can't open $ctx->{krb5_conf}$?");
599         print KRB5CONF "
600 #Generated krb5.conf for $ctx->{realm}
601
602 [libdefaults]
603  default_realm = $ctx->{realm}
604  dns_lookup_realm = false
605  dns_lookup_kdc = false
606  ticket_lifetime = 24h
607  forwardable = yes
608
609 [realms]
610  $ctx->{realm} = {
611   kdc = $ctx->{kdc_ipv4}:88
612   admin_server = $ctx->{kdc_ipv4}:88
613   default_domain = $ctx->{dnsname}
614  }
615  $ctx->{dnsname} = {
616   kdc = $ctx->{kdc_ipv4}:88
617   admin_server = $ctx->{kdc_ipv4}:88
618   default_domain = $ctx->{dnsname}
619  }
620  $ctx->{domain} = {
621   kdc = $ctx->{kdc_ipv4}:88
622   admin_server = $ctx->{kdc_ipv4}:88
623   default_domain = $ctx->{dnsname}
624  }
625
626 [appdefaults]
627         pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
628
629 [kdc]
630         enable-pkinit = true
631         pkinit_identity = FILE:$ctx->{tlsdir}/kdc.pem,$ctx->{tlsdir}/key.pem
632         pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
633
634 [domain_realm]
635  .$ctx->{dnsname} = $ctx->{realm}
636 ";
637         close(KRB5CONF);
638
639         open(PWD, ">$ctx->{nsswrap_passwd}");
640         print PWD "
641 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
642 $ctx->{unix_name}:x:$ctx->{unix_uid}:@{$ctx->{unix_gids}}[0]:$ctx->{unix_name} gecos:$ctx->{prefix_abs}:/bin/false
643 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
644 ";
645         close(PWD);
646
647         open(GRP, ">$ctx->{nsswrap_group}");
648         print GRP "
649 root:x:0:
650 wheel:x:10:
651 users:x:100:
652 nobody:x:65533:
653 nogroup:x:65534:nobody
654 ";
655         close(GRP);
656
657         my $configuration = "--configfile=$ctx->{smb_conf}";
658
659 #Ensure the config file is valid before we start
660         my $testparm = $self->bindir_path("testparm");
661         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
662                 system("$testparm -v --suppress-prompt $configuration >&2");
663                 die("Failed to create a valid smb.conf configuration $testparm!");
664         }
665
666         (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");
667
668         my $ret = {
669                 KRB5_CONFIG => $ctx->{krb5_conf},
670                 PIDDIR => $ctx->{piddir},
671                 SERVER => $ctx->{netbiosname},
672                 SERVER_IP => $ctx->{ipv4},
673                 NETBIOSNAME => $ctx->{netbiosname},
674                 NETBIOSALIAS => $ctx->{netbiosalias},
675                 DOMAIN => $ctx->{domain},
676                 USERNAME => $ctx->{username},
677                 REALM => $ctx->{realm},
678                 PASSWORD => $ctx->{password},
679                 LDAPDIR => $ctx->{ldapdir},
680                 WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
681                 NCALRPCDIR => $ctx->{ncalrpcdir},
682                 LOCKDIR => $ctx->{lockdir},
683                 SERVERCONFFILE => $ctx->{smb_conf},
684                 CONFIGURATION => $configuration,
685                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
686                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
687                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
688                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
689                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
690                 SAMBA_TEST_LOG_POS => 0,
691         };
692
693         return $ret;
694 }
695
696 #
697 # provision_raw_step2() is also used by Samba34.pm!
698 #
699 # Step2 runs the provision script
700 #
701 sub provision_raw_step2($$$)
702 {
703         my ($self, $ctx, $ret) = @_;
704
705         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
706         (system($provision_cmd) == 0) or die("Unable to provision: \n$provision_cmd\n");
707
708         return $ret;
709 }
710
711 sub provision($$$$$$$)
712 {
713         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password, $kdc_ipv4) = @_;
714
715         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
716                                                $netbiosname, $netbiosalias,
717                                                $swiface, $password, $kdc_ipv4);
718
719         $ctx->{tmpdir} = "$ctx->{prefix_abs}/tmp";
720         push(@{$ctx->{directories}}, "$ctx->{tmpdir}");
721         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test1");
722         push(@{$ctx->{directories}}, "$ctx->{tmpdir}/test2");
723
724         $ctx->{smb_conf_extra_options} = "
725
726         max xmit = 32K
727         server max protocol = SMB2
728
729 [tmp]
730         path = $ctx->{tmpdir}
731         read only = no
732         posix:sharedelay = 100000
733         posix:eadb = $ctx->{lockdir}/eadb.tdb
734         posix:oplocktimeout = 3
735         posix:writetimeupdatedelay = 500000
736
737 [test1]
738         path = $ctx->{tmpdir}/test1
739         read only = no
740         posix:sharedelay = 100000
741         posix:eadb = $ctx->{lockdir}/eadb.tdb
742         posix:oplocktimeout = 3
743         posix:writetimeupdatedelay = 500000
744
745 [test2]
746         path = $ctx->{tmpdir}/test2
747         read only = no
748         posix:sharedelay = 100000
749         posix:eadb = $ctx->{lockdir}/eadb.tdb
750         posix:oplocktimeout = 3
751         posix:writetimeupdatedelay = 500000
752
753 [cifs]
754         read only = no
755         ntvfs handler = cifs
756         cifs:server = $ctx->{netbiosname}
757         cifs:share = tmp
758 #There is no username specified here, instead the client is expected
759 #to log in with kerberos, and the serverwill use delegated credentials.
760
761 [simple]
762         path = $ctx->{tmpdir}
763         read only = no
764         ntvfs handler = simple
765
766 [sysvol]
767         path = $ctx->{lockdir}/sysvol
768         read only = yes
769
770 [netlogon]
771         path = $ctx->{lockdir}/sysvol/$ctx->{dnsname}/scripts
772         read only = no
773
774 [cifsposix]
775         copy = simple
776         ntvfs handler = cifsposix
777 ";
778
779         if (defined($self->{ldap})) {
780                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
781                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
782
783                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
784                 $ldap_uri =~ s|/|%2F|g;
785                 $ldap_uri = "ldapi://$ldap_uri";
786                 $ctx->{ldap_uri} = $ldap_uri;
787         }
788
789         my $ret = $self->provision_raw_step1($ctx);
790
791         if (defined($self->{ldap})) {
792                 my $configuration = "--configfile=$ctx->{smb_conf}";
793
794                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
795                 push (@{$ctx->{provision_options}},"--ldap-backend=$ctx->{ldap_uri}");
796
797                 system("$self->{setupdir}/provision-backend $configuration --ldap-admin-pass=$ctx->{password} --root=$ctx->{unix_name} --realm=$ctx->{realm} --domain=$ctx->{domain} --host-name=$ctx->{netbiosname} --ldap-backend-type=$self->{ldap} --nosync>&2") == 0 or die("backend provision failed");
798
799                 push (@{$ctx->{provision_options}}, "--password=$ctx->{password}");
800
801                 if ($self->{ldap} eq "openldap") {
802                         push (@{$ctx->{provision_options}}, "--username=samba-admin");
803                         push (@{$ctx->{provision_options}}, "--ldap-backend-type=openldap");
804
805                         ($ret->{SLAPD_CONF}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx->{ldapdir}, $configuration) or die("Unable to create openldap directories");
806
807                 } elsif ($self->{ldap} eq "fedora-ds") {
808                         push (@{$ctx->{provision_options}}, "--simple-bind-dn=cn=Manager,$ctx->{localbasedn}");
809                         push (@{$ctx->{provision_options}}, "--ldap-backend-type=fedora-ds");
810
811                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx->{ldapdir}, $configuration) or die("Unable to create fedora ds directories");
812                 }
813
814                 $self->slapd_start($ret) or die("couldn't start slapd");
815         }
816
817         $ret = $self->provision_raw_step2($ctx, $ret);
818
819         if (defined($self->{ldap})) {
820                 $self->slapd_stop($ret) or die("couldn't stop slapd");
821         }
822
823         return $ret;
824 }
825
826 sub provision_member($$$)
827 {
828         my ($self, $prefix, $dcvars) = @_;
829         print "PROVISIONING MEMBER...";
830
831         my $ret = $self->provision($prefix,
832                                    "member server",
833                                    "localmember3",
834                                    "localmember",
835                                    3,
836                                    "localmemberpass",
837                                    $dcvars->{SERVER_IP});
838
839         $ret or die("Unable to provision");
840
841         my $net = $self->bindir_path("net");
842         my $cmd = "";
843         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
844         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
845         $cmd .= "$net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
846         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
847
848         system($cmd) == 0 or die("Join failed\n$cmd");
849
850         $ret->{DC_SERVER} = $dcvars->{SERVER};
851         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
852         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
853         $ret->{DC_NETBIOSALIAS} = $dcvars->{NETBIOSALIAS};
854         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
855         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
856
857         return $ret;
858 }
859
860 sub provision_dc($$)
861 {
862         my ($self, $prefix) = @_;
863
864         print "PROVISIONING DC...";
865         my $ret = $self->provision($prefix,
866                                    "domain controller",
867                                    "localdc1",
868                                    "localdc",
869                                    1,
870                                    "localdcpass",
871                                    "127.0.0.1");
872
873         $self->add_wins_config("$prefix/private") or 
874                 die("Unable to add wins configuration");
875
876         return $ret;
877 }
878
879 sub teardown_env($$)
880 {
881         my ($self, $envvars) = @_;
882         my $pid;
883
884         close(DATA);
885
886         if (-f "$envvars->{PIDDIR}/samba.pid" ) {
887                 open(IN, "<$envvars->{PIDDIR}/samba.pid") or die("unable to open server pid file");
888                 $pid = <IN>;
889                 close(IN);
890
891                 # Give the process 20 seconds to exit.  gcov needs
892                 # this time to write out the covarge data
893                 my $count = 0;
894                 until (kill(0, $pid) == 0) {
895                     # if no process sucessfully signalled, then we are done
896                     sleep(1);
897                     $count++;
898                     last if $count > 20;
899                 }
900                 
901                 # If it is still around, kill it
902                 if ($count > 20) {
903                     print "server process $pid took more than $count seconds to exit, killing\n";
904                     kill 9, $pid;
905                 }
906         }
907
908         my $failed = $? >> 8;
909
910         $self->slapd_stop($envvars) if ($self->{ldap});
911
912         print $self->getlog_env($envvars);
913
914         return $failed;
915 }
916
917 sub getlog_env($$)
918 {
919         my ($self, $envvars) = @_;
920         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
921         my $out = $title;
922
923         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
924
925         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
926         while (<LOG>) {
927                 $out .= $_;
928         }
929         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
930         close(LOG);
931
932         return "" if $out eq $title;
933  
934         return $out;
935 }
936
937 sub check_env($$)
938 {
939         my ($self, $envvars) = @_;
940
941         return 1 if (-p $envvars->{SAMBA_TEST_FIFO});
942
943         print $self->getlog_env($envvars);
944
945         return 0;
946 }
947
948 sub setup_env($$$)
949 {
950         my ($self, $envname, $path) = @_;
951
952         if ($envname eq "dc") {
953                 return $self->setup_dc("$path/dc");
954         } elsif ($envname eq "member") {
955                 if (not defined($self->{vars}->{dc})) {
956                         $self->setup_dc("$path/dc");
957                 }
958                 return $self->setup_member("$path/member", $self->{vars}->{dc});
959         } else {
960                 die("Samba4 can't provide environment '$envname'");
961         }
962 }
963
964 sub setup_member($$$$)
965 {
966         my ($self, $path, $dc_vars) = @_;
967
968         my $env = $self->provision_member($path, $dc_vars);
969
970         $self->check_or_start($env, ($ENV{SMBD_MAXTIME} or 7500));
971
972         $self->wait_for_start($env);
973
974         return $env;
975 }
976
977 sub setup_dc($$)
978 {
979         my ($self, $path) = @_;
980
981         my $env = $self->provision_dc($path);
982
983         $self->check_or_start($env, 
984                 ($ENV{SMBD_MAXTIME} or 7500));
985
986         $self->wait_for_start($env);
987
988         $self->{vars}->{dc} = $env;
989
990         return $env;
991 }
992
993 sub stop($)
994 {
995         my ($self) = @_;
996 }
997
998 1;