Make the Fedora DS LDAP backend 'aci' actually work, with better quoting.
[ira/wip.git] / source / selftest / env / 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) = @_;
15         my $self = { 
16                 vars => {}, 
17                 ldap => $ldap, 
18                 bindir => $bindir, 
19                 setupdir => $setupdir 
20         };
21         bless $self;
22         return $self;
23 }
24
25 sub openldap_start($$$) {
26         my ($slapd_conf, $uri, $logs) = @_;
27         my $oldpath = $ENV{PATH};
28         my $olroot = "";
29         my $olpath = "";
30         if (defined $ENV{OPENLDAP_ROOT}) {
31             $olroot = "$ENV{OPENLDAP_ROOT}";
32             $olpath = "$olroot/libexec:$olroot/sbin:";
33         }
34         $ENV{PATH} = "$olpath/usr/local/sbin:/usr/sbin:/sbin:$ENV{PATH}";
35         system("slapd -d63 -f $slapd_conf -h $uri > $logs 2>&1 &");
36         $ENV{PATH} = $oldpath;
37 }
38
39 sub slapd_start($$)
40 {
41         my $count = 0;
42         my ($self, $env_vars) = @_;
43
44         my $uri = $env_vars->{LDAP_URI};
45
46         # running slapd in the background means it stays in the same process group, so it can be
47         # killed by timelimit
48         if ($self->{ldap} eq "fedora-ds") {
49                 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 &");
50         } elsif ($self->{ldap} eq "openldap") {
51                 openldap_start($env_vars->{SLAPD_CONF}, $uri, "$env_vars->{LDAPDIR}/logs");
52         }
53         while (system("$self->{bindir}/ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
54                 $count++;
55                 if ($count > 40) {
56                     $self->slapd_stop($env_vars);
57                     return 0;
58                 }
59                 sleep(1);
60         }
61         return 1;
62 }
63
64 sub slapd_stop($$)
65 {
66         my ($self, $envvars) = @_;
67         if ($self->{ldap} eq "fedora-ds") {
68                 system("$envvars->{LDAPDIR}/slapd-samba4/stop-slapd");
69         } elsif ($self->{ldap} eq "openldap") {
70                 open(IN, "<$envvars->{OPENLDAP_PIDFILE}") or 
71                         die("unable to open slapd pid file: $envvars->{OPENLDAP_PIDFILE}");
72                 kill 9, <IN>;
73                 close(IN);
74         }
75         return 1;
76 }
77
78 sub check_or_start($$$) 
79 {
80         my ($self, $env_vars, $max_time) = @_;
81         return 0 if ( -p $env_vars->{SMBD_TEST_FIFO});
82
83         unlink($env_vars->{SMBD_TEST_FIFO});
84         POSIX::mkfifo($env_vars->{SMBD_TEST_FIFO}, 0700);
85         unlink($env_vars->{SMBD_TEST_LOG});
86         
87         print "STARTING SMBD... ";
88         my $pid = fork();
89         if ($pid == 0) {
90                 open STDIN, $env_vars->{SMBD_TEST_FIFO};
91                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
92                 open STDERR, '>&STDOUT';
93                 
94                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
95
96                 my $valgrind = "";
97                 if (defined($ENV{SMBD_VALGRIND})) {
98                     $valgrind = $ENV{SMBD_VALGRIND};
99                 } 
100
101                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG}; 
102
103                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
104                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
105
106                 # Start slapd before smbd, but with the fifo on stdin
107                 if (defined($self->{ldap})) {
108                     $self->slapd_start($env_vars) or 
109                         die("couldn't start slapd (2nd time)");
110                 }
111
112                 my $optarg = "";
113                 if (defined($max_time)) {
114                         $optarg = "--maximum-runtime=$max_time ";
115                 }
116                 if (defined($ENV{SMBD_OPTIONS})) {
117                         $optarg.= " $ENV{SMBD_OPTIONS}";
118                 }
119                 my $ret = system("$valgrind $self->{bindir}/smbd $optarg $env_vars->{CONFIGURATION} -M single -i --leak-report-full");
120                 if ($? == -1) {
121                         print "Unable to start smbd: $ret: $!\n";
122                         exit 1;
123                 }
124                 unlink($env_vars->{SMBD_TEST_FIFO});
125                 my $exit = $? >> 8;
126                 if ( $ret == 0 ) {
127                         print "smbd exits with status $exit\n";
128                 } elsif ( $ret & 127 ) {
129                         print "smbd got signal ".($ret & 127)." and exits with $exit!\n";
130                 } else {
131                         $ret = $? >> 8;
132                         print "smbd failed with status $exit!\n";
133                 }
134                 exit $exit;
135         }
136         print "DONE\n";
137
138         open(DATA, ">$env_vars->{SMBD_TEST_FIFO}");
139
140         return $pid;
141 }
142
143 sub wait_for_start($$)
144 {
145         my ($self, $testenv_vars) = @_;
146         # give time for nbt server to register its names
147         print "delaying for nbt name registration\n";
148         sleep 2;
149
150         # This will return quickly when things are up, but be slow if we 
151         # need to wait for (eg) SSL init 
152         system("bin/nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
153         system("bin/nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
154         system("bin/nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
155         system("bin/nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
156         system("bin/nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSALIAS}");
157         system("bin/nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSALIAS}");
158         system("bin/nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
159         system("bin/nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
160         system("bin/nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
161         system("bin/nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
162         system("bin/nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSALIAS}");
163         system("bin/nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSALIAS}");
164
165         print $self->getlog_env($testenv_vars);
166 }
167
168 sub write_ldb_file($$$)
169 {
170         my ($self, $file, $ldif) = @_;
171
172         open(LDIF, "|$self->{bindir}/ldbadd -H $file >/dev/null");
173         print LDIF $ldif;
174         return close(LDIF);
175 }
176
177 sub add_wins_config($$)
178 {
179         my ($self, $privatedir) = @_;
180
181         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
182 dn: name=TORTURE_6,CN=PARTNERS
183 objectClass: wreplPartner
184 name: TORTURE_6
185 address: 127.0.0.6
186 pullInterval: 0
187 pushChangeCount: 0
188 type: 0x3
189 ");
190 }
191
192 sub mk_fedora_ds($$$)
193 {
194         my ($self, $ldapdir, $configuration) = @_;
195
196         my $fedora_ds_inf = "$ldapdir/fedorads.inf";
197         my $fedora_ds_extra_ldif = "$ldapdir/fedorads-partitions.ldif";
198
199         #Make the subdirectory be as fedora DS would expect
200         my $fedora_ds_dir = "$ldapdir/slapd-samba4";
201
202         my $pidfile = "$fedora_ds_dir/logs/slapd-samba4.pid";
203
204         system("$self->{bindir}/ad2oLschema $configuration -H $ldapdir/schema-tmp.ldb --option=convert:target=fedora-ds -I $self->{setupdir}/schema-map-fedora-ds-1.0 -O $ldapdir/99_ad.ldif >&2") == 0 or die("schema conversion for Fedora DS failed");
205
206 my $dir = getcwd();
207 chdir "$ENV{FEDORA_DS_ROOT}/bin" || die;
208         if (system("perl $ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl --silent --file=$fedora_ds_inf >&2") != 0) {
209             chdir $dir;
210             die("perl $ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl --silent --file=$fedora_ds_inf FAILED: $?");
211         }
212         chdir $dir || die;
213
214         return ($fedora_ds_dir, $pidfile);
215 }
216
217 sub mk_openldap($$$)
218 {
219         my ($self, $ldapdir, $configuration) = @_;
220
221         my $slapd_conf = "$ldapdir/slapd.conf";
222         my $pidfile = "$ldapdir/slapd.pid";
223         my $modconf = "$ldapdir/modules.conf";
224
225         #This uses the backend provision we just did, to read out the schema
226         system("$self->{bindir}/ad2oLschema $configuration --option=convert:target=openldap -H $ldapdir/schema-tmp.ldb -I $self->{setupdir}/schema-map-openldap-2.3 -O $ldapdir/backend-schema.schema >&2") == 0 or die("schema conversion for OpenLDAP failed");
227
228         my $oldpath = $ENV{PATH};
229         my $olpath = "";
230         my $olroot = "";
231         if (defined $ENV{OPENLDAP_ROOT}) {
232                $olroot = "$ENV{OPENLDAP_ROOT}";
233                $olpath = "$olroot/libexec:$olroot/sbin:";
234         }
235         $ENV{PATH} = "$olpath/usr/local/sbin:/usr/sbin:/sbin:$ENV{PATH}";
236
237         unlink($modconf);
238         open(CONF, ">$modconf"); close(CONF);
239
240         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
241                 open(CONF, ">$modconf"); 
242                 # enable slapd modules
243                 print CONF "
244 modulepath      $olroot/libexec/openldap
245 moduleload      syncprov
246 moduleload      memberof
247 ";
248                 close(CONF);
249         }
250         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
251                 open(CONF, ">$modconf"); 
252                 # enable slapd modules
253                 print CONF "
254 modulepath      $olroot/libexec/openldap
255 moduleload      back_hdb
256 moduleload      syncprov
257 moduleload      memberof
258 ";
259                 close(CONF);
260         }
261
262         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
263                 open(CONF, ">$modconf"); 
264                 # enable slapd modules
265                 print CONF "
266 moduleload      back_hdb
267 moduleload      syncprov
268 moduleload      memberof
269 ";
270                 close(CONF);
271         }
272
273         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
274                 open(CONF, ">$modconf"); 
275                 # enable slapd modules
276                 print CONF "
277 modulepath      /usr/lib/ldap
278 moduleload      back_hdb
279 moduleload      syncprov
280 moduleload      memberof
281 ";
282                 close(CONF);
283         }
284
285         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
286                 open(CONF, ">$modconf"); 
287                 # enable slapd modules (Fedora layout)
288                 print CONF "
289 modulepath      /usr/lib/openldap
290 moduleload      syncprov
291 moduleload      memberof
292 ";
293                 close(CONF);
294         }
295
296         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
297                 open(CONF, ">$modconf"); 
298                 # enable slapd modules (Fedora x86_64 layout)
299                 print CONF "
300 modulepath      /usr/lib64/openldap
301 moduleload      syncprov
302 moduleload      memberof
303 ";
304                 close(CONF);
305         }
306
307         system("slaptest -u -f $slapd_conf") == 0 or die("slaptest still fails after adding modules");
308
309     
310         $ENV{PATH} = $oldpath;
311
312         return ($slapd_conf, $pidfile);
313 }
314
315 sub mk_keyblobs($$)
316 {
317         my ($self, $tlsdir) = @_;
318
319         #TLS and PKINIT crypto blobs
320         my $dhfile = "$tlsdir/dhparms.pem";
321         my $cafile = "$tlsdir/ca.pem";
322         my $certfile = "$tlsdir/cert.pem";
323         my $reqkdc = "$tlsdir/req-kdc.der";
324         my $kdccertfile = "$tlsdir/kdc.pem";
325         my $keyfile = "$tlsdir/key.pem";
326         my $adminkeyfile = "$tlsdir/adminkey.pem";
327         my $reqadmin = "$tlsdir/req-admin.der";
328         my $admincertfile = "$tlsdir/admincert.pem";
329
330         mkdir($tlsdir, 0777);
331
332         #This is specified here to avoid draining entropy on every run
333         open(DHFILE, ">$dhfile");
334         print DHFILE <<EOF;
335 -----BEGIN DH PARAMETERS-----
336 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
337 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
338 YX70obsCAQI=
339 -----END DH PARAMETERS-----
340 EOF
341         close(DHFILE);
342
343         #Likewise, we pregenerate the key material.  This allows the 
344         #other certificates to be pre-generated
345         open(KEYFILE, ">$keyfile");
346         print KEYFILE <<EOF;
347 -----BEGIN RSA PRIVATE KEY-----
348 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
349 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
350 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
351 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
352 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
353 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
354 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
355 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
356 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
357 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
358 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
359 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
360 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
361 -----END RSA PRIVATE KEY-----
362 EOF
363         close(KEYFILE);
364
365         open(ADMINKEYFILE, ">$adminkeyfile");
366
367         print ADMINKEYFILE <<EOF;
368 -----BEGIN RSA PRIVATE KEY-----
369 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
370 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
371 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
372 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
373 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
374 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
375 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
376 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
377 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
378 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
379 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
380 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
381 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
382 -----END RSA PRIVATE KEY-----
383 EOF
384
385         #generated with 
386         #hxtool issue-certificate --self-signed --issue-ca --ca-private-key=FILE:$KEYFILE \
387         #          --subject="CN=CA,$BASEDN" --certificate="FILE:$CAFILE"
388
389         open(CAFILE, ">$cafile");
390         print CAFILE <<EOF;
391 -----BEGIN CERTIFICATE-----
392 MIIChTCCAe6gAwIBAgIUFZoF6jt0R+hQBdF7cWPy0tT3fGwwCwYJKoZIhvcNAQEFMFIxEzAR
393 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
394 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDcwMTIzMDU1MzA5WhgPMjAwODAxMjQw
395 NTUzMDlaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
396 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
397 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
398 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
399 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo1YwVDAOBgNVHQ8BAf8EBAMC
400 AqQwEgYDVR0lBAswCQYHKwYBBQIDBTAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIw
401 DwYDVR0TBAgwBgEB/wIBADANBgkqhkiG9w0BAQUFAAOBgQBgzh5uLDmESGYv60iUdEfuk/T9
402 VCpzb1z3VJVWt3uJoQYbcpR00SKeyMdlfTTLzO6tSPMmlk4hwqfvLkPzGCSObR4DRRYa0BtY
403 2laBVlg9X59bGpMUvpFQfpvxjvFWNJDL+377ELCVpLNdoR23I9TKXlalj0bY5Ks46CVIrm6W
404 EA==
405 -----END CERTIFICATE-----
406 EOF
407
408         #generated with GNUTLS internally in Samba.  
409
410         open(CERTFILE, ">$certfile");
411         print CERTFILE <<EOF;
412 -----BEGIN CERTIFICATE-----
413 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
414 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
415 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
416 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
417 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
418 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
419 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
420 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
421 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
422 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
423 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
424 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
425 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
426 -----END CERTIFICATE-----
427 EOF
428         close(CERTFILE);
429
430         #KDC certificate
431         # hxtool request-create --subject="CN=krbtgt,cn=users,$basedn" --key=FILE:$KEYFILE $KDCREQ
432
433         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE --type="pkinit-kdc" --pk-init-principal="krbtgt/$RELAM@$REALM" --req="$KDCREQ" --certificate="FILE:$KDCCERTFILE"
434
435         open(KDCCERTFILE, ">$kdccertfile");
436         print KDCCERTFILE <<EOF;
437 -----BEGIN CERTIFICATE-----
438 MIIDDDCCAnWgAwIBAgIUDEhjaOT1ZjHjHHEn+l5eYO05oK8wCwYJKoZIhvcNAQEFMFIxEzAR
439 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
440 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDcwMTIzMDcwNzA4WhgPMjAwODAxMjQw
441 NzA3MDhaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
442 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
443 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
444 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
445 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
446 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
447 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
448 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
449 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
450 AQUFAAOBgQCMSgLkIv9RobE0a95H2ECA+5YABBwKXIt4AyN/HpV7iJdRx7B9PE6vM+nboVKY
451 E7i7ECUc3bu6NgrLu7CKHelNclHWWMiZzSUwhkXyvG/LE9qtr/onNu9NfLt1OV+dwQwyLdEP
452 n63FxSmsKg3dfi3ryQI/DIKeisvipwDtLqOn9g==
453 -----END CERTIFICATE-----
454 EOF
455
456         #hxtool request-create --subject="CN=Administrator,cn=users,$basedn" --key=FILE:$ADMINKEYFILE $ADMINREQFILE
457         #hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE --type="pkinit-client" --pk-init-principal="administrator@$REALM" --req="$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE"
458         
459         open(ADMINCERTFILE, ">$admincertfile");
460         print ADMINCERTFILE <<EOF;
461 -----BEGIN CERTIFICATE-----
462 MIICwjCCAiugAwIBAgIUXyECoq4im33ByZDWZMGhtpvHYWEwCwYJKoZIhvcNAQEFMFIxEzAR
463 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
464 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDcwMTIzMDcyMzE2WhgPMjAwODAxMjQw
465 NzIzMTZaMCgxDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1pbmlzdHJhdG9yMIGfMA0G
466 CSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHE
467 si5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
468 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQABo4G8MIG5
469 MA4GA1UdDwEB/wQEAwIFoDASBgNVHSUECzAJBgcrBgEFAgMEMEgGA1UdEQRBMD+gPQYGKwYB
470 BQICoDMwMaATGxFTQU1CQS5FWEFNUExFLkNPTaEaMBigAwIBAaERMA8bDWFkbWluaXN0cmF0
471 b3IwHwYDVR0jBBgwFoAUwtm596AMotmzRU7IVdgrUvozyjIwHQYDVR0OBBYEFCDzVsvJ8IDz
472 wLYH8EONeUa5oVrGMAkGA1UdEwQCMAAwDQYJKoZIhvcNAQEFBQADgYEAbTCnaPTieVZPV3bH
473 UmAMbnF9+YN1mCbe2xZJ0xzve+Yw1XO82iv/9kZaZkcRkaQt2qcwsBK/aSPOgfqGx+mJ7hXQ
474 AGWvAJhnWi25PawNaRysCN8WC6+nWKR4d2O2m5rpj3T9kH5WE7QbG0bCu92dGaS29FvWDCP3
475 q9pRtDOoAZc=
476 -----END CERTIFICATE-----
477 EOF
478         close(ADMINCERTFILE);
479 }
480
481 sub provision($$$$$$)
482 {
483         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password) = @_;
484
485         my $smbd_loglevel = 1;
486         my $username = "administrator";
487         my $domain = "SAMBADOMAIN";
488         my $realm = "SAMBA.EXAMPLE.COM";
489         my $dnsname = "samba.example.com";
490         my $basedn = "dc=samba,dc=example,dc=com";
491         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
492         chomp $unix_name;
493         my $unix_uid = $>;
494         my $unix_gids_str = $);
495         my @unix_gids = split(" ", $unix_gids_str);
496         my $srcdir="$RealBin/..";
497         -d $prefix or mkdir($prefix, 0777) or die("Unable to create $prefix");
498         my $prefix_abs = abs_path($prefix);
499         my $tmpdir = "$prefix_abs/tmp";
500         my $etcdir = "$prefix_abs/etc";
501         my $piddir = "$prefix_abs/pid";
502         my $conffile = "$etcdir/smb.conf";
503         my $krb5_config = "$etcdir/krb5.conf";
504         my $privatedir = "$prefix_abs/private";
505         my $ncalrpcdir = "$prefix_abs/ncalrpc";
506         my $lockdir = "$prefix_abs/lockdir";
507         my $winbindd_socket_dir = "$prefix_abs/winbind_socket";
508         my $winbindd_priv_pipe_dir = "$piddir/smbd.tmp/winbind_pipe";
509         my $nsswrap_passwd = "$etcdir/passwd";
510         my $nsswrap_group = "$etcdir/group";
511
512         my $configuration = "--configfile=$conffile";
513         my $ldapdir = "$privatedir/ldap";
514
515         my $tlsdir = "$privatedir/tls";
516
517         my $ifaceipv4 = "127.0.0.$swiface";
518         my $interfaces = "$ifaceipv4/8";
519
520         (system("rm -rf $prefix/*") == 0) or die("Unable to clean up");
521         mkdir($_, 0777) foreach ($privatedir, $etcdir, $piddir, $ncalrpcdir, $lockdir, 
522                 $tmpdir);
523
524
525         my $localbasedn = $basedn;
526         $localbasedn = "DC=$netbiosname" if $server_role eq "member server";
527
528         open(CONFFILE, ">$conffile");
529         print CONFFILE "
530 [global]
531         netbios name = $netbiosname
532         netbios aliases = $netbiosalias
533         workgroup = $domain
534         realm = $realm
535         private dir = $privatedir
536         pid directory = $piddir
537         ncalrpc dir = $ncalrpcdir
538         lock dir = $lockdir
539         setup directory = $self->{setupdir}
540         modules dir = $self->{bindir}/modules
541         js include = $srcdir/scripting/libjs
542         winbindd socket directory = $winbindd_socket_dir
543         winbind separator = /
544         name resolve order = bcast
545         interfaces = $interfaces
546         tls dh params file = $tlsdir/dhparms.pem
547         panic action = $srcdir/script/gdb_backtrace \%PID% \%PROG%
548         wins support = yes
549         server role = $server_role
550         max xmit = 32K
551         server max protocol = SMB2
552         notify:inotify = false
553         ldb:nosync = true
554         system:anonymous = true
555 #We don't want to pass our self-tests if the PAC code is wrong
556         gensec:require_pac = true
557         log level = $smbd_loglevel
558
559 [tmp]
560         path = $tmpdir
561         read only = no
562         ntvfs handler = posix
563         posix:sharedelay = 100000
564         posix:eadb = $lockdir/eadb.tdb
565
566 [cifs]
567         read only = no
568         ntvfs handler = cifs
569         cifs:server = $netbiosname
570         cifs:share = tmp
571 #There is no username specified here, instead the client is expected
572 #to log in with kerberos, and smbd will used delegated credentials.
573
574 [simple]
575         path = $tmpdir
576         read only = no
577         ntvfs handler = simple
578
579 [cifsposix]
580         copy = simple
581         ntvfs handler = cifsposix   
582 ";
583         close(CONFFILE);
584
585         $self->mk_keyblobs($tlsdir);
586
587         open(KRB5CONF, ">$krb5_config");
588         print KRB5CONF "
589 #Generated krb5.conf for $realm
590
591 [libdefaults]
592  default_realm = $realm
593  dns_lookup_realm = false
594  dns_lookup_kdc = false
595  ticket_lifetime = 24h
596  forwardable = yes
597
598 [realms]
599  $realm = {
600   kdc = 127.0.0.1:88
601   admin_server = 127.0.0.1:88
602   default_domain = $dnsname
603  }
604  $dnsname = {
605   kdc = 127.0.0.1:88
606   admin_server = 127.0.0.1:88
607   default_domain = $dnsname
608  }
609  $domain = {
610   kdc = 127.0.0.1:88
611   admin_server = 127.0.0.1:88
612   default_domain = $dnsname
613  }
614
615 [appdefaults]
616         pkinit_anchors = FILE:$tlsdir/ca.pem
617
618 [kdc]
619         enable-pkinit = true
620         pkinit_identity = FILE:$tlsdir/kdc.pem,$tlsdir/key.pem
621         pkinit_anchors = FILE:$tlsdir/ca.pem
622
623 [domain_realm]
624  .$dnsname = $realm
625 ";
626         close(KRB5CONF);
627
628         open(PWD, ">$nsswrap_passwd");
629         print PWD "
630 root:x:0:0:root gecos:$prefix_abs:/bin/false
631 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
632 nobody:x:65534:65533:nobody gecos:$prefix_abs:/bin/false
633 ";
634         close(PWD);
635
636         open(GRP, ">$nsswrap_group");
637         print GRP "
638 root:x:0:
639 wheel:x:10:
640 users:x:100:
641 nobody:x:65533:
642 nogroup:x:65534:nobody
643 ";
644         close(GRP);
645
646 #Ensure the config file is valid before we start
647         if (system("$self->{bindir}/testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
648                 system("$self->{bindir}/testparm -v --suppress-prompt $configuration >&2");
649                 die("Failed to create a valid smb.conf configuration!");
650         }
651
652         (system("($self->{bindir}/testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global 2> /dev/null | grep -i \"^$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");
653
654         my @provision_options = ();
655         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$nsswrap_passwd\"");
656         push (@provision_options, "NSS_WRAPPER_GROUP=\"$nsswrap_group\"");
657         if (defined($ENV{PROVISION_PYTHON})) {
658                 push (@provision_options, "$self->{bindir}/smbpython");
659                 push (@provision_options, "$self->{setupdir}/provision.py");
660         } else {
661                 push (@provision_options, "$self->{bindir}/smbscript");
662                 push (@provision_options, "$self->{setupdir}/provision");
663         }
664         push (@provision_options, split(' ', $configuration));
665         push (@provision_options, "--host-name=$netbiosname");
666         push (@provision_options, "--host-ip=$ifaceipv4");
667         push (@provision_options, "--quiet");
668         push (@provision_options, "--domain=$domain");
669         push (@provision_options, "--realm=$realm");
670         push (@provision_options, "--adminpass=$password");
671         push (@provision_options, "--krbtgtpass=krbtgt$password");
672         push (@provision_options, "--machinepass=machine$password");
673         push (@provision_options, "--root=$unix_name");
674         push (@provision_options, "--simple-bind-dn=cn=Manager,$localbasedn");
675         push (@provision_options, "--password=$password");
676         push (@provision_options, "--server-role=\"$server_role\"");
677
678         my $ldap_uri= "$ldapdir/ldapi";
679         $ldap_uri =~ s|/|%2F|g;
680         $ldap_uri = "ldapi://$ldap_uri";
681
682         my $ret = {
683                 KRB5_CONFIG => $krb5_config,
684                 PIDDIR => $piddir,
685                 SERVER => $netbiosname,
686                 SERVER_IP => $ifaceipv4,
687                 NETBIOSNAME => $netbiosname,
688                 NETBIOSALIAS => $netbiosalias,
689                 LDAP_URI => $ldap_uri,
690                 DOMAIN => $domain,
691                 USERNAME => $username,
692                 REALM => $realm,
693                 PASSWORD => $password,
694                 LDAPDIR => $ldapdir,
695                 WINBINDD_SOCKET_DIR => $winbindd_socket_dir,
696                 WINBINDD_PRIV_PIPE_DIR => $winbindd_priv_pipe_dir,
697                 NCALRPCDIR => $ncalrpcdir,
698                 LOCKDIR => $lockdir,
699                 CONFIGURATION => $configuration,
700                 SOCKET_WRAPPER_DEFAULT_IFACE => $swiface,
701                 NSS_WRAPPER_PASSWD => $nsswrap_passwd,
702                 NSS_WRAPPER_GROUP => $nsswrap_group,
703         };
704
705         if (defined($self->{ldap})) {
706
707                 push (@provision_options, "--ldap-backend=$ldap_uri");
708                 system("$self->{bindir}/smbscript $self->{setupdir}/provision-backend $configuration --ldap-manager-pass=$password --root=$unix_name --realm=$realm --host-name=$netbiosname --ldap-backend-type=$self->{ldap}>&2") == 0 or die("backend provision failed");
709
710                 if ($self->{ldap} eq "openldap") {
711                        ($ret->{SLAPD_CONF}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ldapdir, $configuration) or die("Unable to create openldap directories");
712                 } elsif ($self->{ldap} eq "fedora-ds") {
713                        ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ldapdir, $configuration) or die("Unable to create fedora ds directories");
714                        push (@provision_options, "--ldap-module=nsuniqueid");
715                        push (@provision_options, "'--aci=aci:: KHRhcmdldGF0dHIgPSAiKiIpICh2ZXJzaW9uIDMuMDthY2wgImZ1bGwgYWNjZXNzIHRvIGFsbCBieSBhbGwiO2FsbG93IChhbGwpKHVzZXJkbiA9ICJsZGFwOi8vL2FueW9uZSIpOykK'");
716                  }
717
718                 $self->slapd_start($ret) or 
719                         die("couldn't start slapd");
720         }
721
722         my $provision_cmd = join(" ", @provision_options);
723         (system($provision_cmd) == 0) or die("Unable to provision: \n$provision_cmd\n");
724
725         if (defined($self->{ldap})) {
726                 $self->slapd_stop($ret) or 
727                         die("couldn't stop slapd");
728         }
729
730         return $ret; 
731 }
732
733 sub provision_member($$$)
734 {
735         my ($self, $prefix, $dcvars) = @_;
736         print "PROVISIONING MEMBER...";
737
738         my $ret = $self->provision($prefix,
739                                    "member server",
740                                    "localmember3",
741                                    "localmember",
742                                    3,
743                                    "localmemberpass");
744
745         $ret or die("Unable to provision");
746
747         my $cmd = "";
748         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
749         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
750         $cmd .= "$self->{bindir}/net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
751         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
752
753         system($cmd) == 0 or die("Join failed\n$cmd");
754
755         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
756         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
757         $ret->{SMBD_TEST_LOG_POS} = 0;
758
759         $ret->{DC_SERVER} = $dcvars->{SERVER};
760         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
761         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
762         $ret->{DC_NETBIOSALIAS} = $dcvars->{NETBIOSALIAS};
763         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
764         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
765
766         return $ret;
767 }
768
769 sub provision_dc($$)
770 {
771         my ($self, $prefix) = @_;
772
773         print "PROVISIONING DC...";
774         my $ret = $self->provision($prefix,
775                                    "domain controller",
776                                    "localdc1",
777                                    "localdc",
778                                    1,
779                                    "localdcpass");
780
781         $self->add_wins_config("$prefix/private") or 
782                 die("Unable to add wins configuration");
783
784         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
785         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
786         $ret->{SMBD_TEST_LOG_POS} = 0;
787         return $ret;
788 }
789
790 sub teardown_env($$)
791 {
792         my ($self, $envvars) = @_;
793         my $pid;
794
795         close(DATA);
796
797         if (-f "$envvars->{PIDDIR}/smbd.pid" ) {
798                 open(IN, "<$envvars->{PIDDIR}/smbd.pid") or die("unable to open smbd pid file");
799                 $pid = <IN>;
800                 close(IN);
801
802                 # Give the process 20 seconds to exit.  gcov needs
803                 # this time to write out the covarge data
804                 my $count = 0;
805                 until (kill(0, $pid) == 0) {
806                     # if no process sucessfully signalled, then we are done
807                     sleep(1);
808                     $count++;
809                     last if $count > 20;
810                 }
811                 
812                 # If it is still around, kill it
813                 if ($count > 20) {
814                     print "smbd process $pid took more than $count seconds to exit, killing\n";
815                     kill 9, $pid;
816                 }
817         }
818
819         my $failed = $? >> 8;
820
821         $self->slapd_stop($envvars) if ($self->{ldap});
822
823         print $self->getlog_env($envvars);
824
825         return $failed;
826 }
827
828 sub getlog_env($$)
829 {
830         my ($self, $envvars) = @_;
831         my $title = "SMBD LOG of: $envvars->{NETBIOSNAME}\n";
832         my $out = $title;
833
834         open(LOG, "<$envvars->{SMBD_TEST_LOG}");
835
836         seek(LOG, $envvars->{SMBD_TEST_LOG_POS}, SEEK_SET);
837         while (<LOG>) {
838                 $out .= $_;
839         }
840         $envvars->{SMBD_TEST_LOG_POS} = tell(LOG);
841         close(LOG);
842
843         return "" if $out eq $title;
844  
845         return $out;
846 }
847
848 sub check_env($$)
849 {
850         my ($self, $envvars) = @_;
851
852         return 1 if (-p $envvars->{SMBD_TEST_FIFO});
853
854         print $self->getlog_env($envvars);
855
856         return 0;
857 }
858
859 sub setup_env($$$)
860 {
861         my ($self, $envname, $path) = @_;
862
863         if ($envname eq "dc") {
864                 return $self->setup_dc("$path/dc");
865         } elsif ($envname eq "member") {
866                 if (not defined($self->{vars}->{dc})) {
867                         $self->setup_dc("$path/dc");
868                 }
869                 return $self->setup_member("$path/member", $self->{vars}->{dc});
870         } else {
871                 die("Samba4 can't provide environment '$envname'");
872         }
873 }
874
875 sub setup_member($$$$)
876 {
877         my ($self, $path, $dc_vars) = @_;
878
879         my $env = $self->provision_member($path, $dc_vars);
880
881         $self->check_or_start($env, ($ENV{SMBD_MAXTIME} or 7500));
882
883         $self->wait_for_start($env);
884
885         return $env;
886 }
887
888 sub setup_dc($$)
889 {
890         my ($self, $path) = @_;
891
892         my $env = $self->provision_dc($path);
893
894         $self->check_or_start($env, 
895                 ($ENV{SMBD_MAXTIME} or 7500));
896
897         $self->wait_for_start($env);
898
899         $self->{vars}->{dc} = $env;
900
901         return $env;
902 }
903
904 sub stop($)
905 {
906         my ($self) = @_;
907 }
908
909 1;