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