Merge branch 'v4-0-trivial' into v4-0-python
[nivanova/samba-autobuild/.git] / source4 / 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) = @_;
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 moduleload      refint
248 ";
249                 close(CONF);
250         }
251         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
252                 open(CONF, ">$modconf"); 
253                 # enable slapd modules
254                 print CONF "
255 modulepath      $olroot/libexec/openldap
256 moduleload      back_hdb
257 moduleload      syncprov
258 moduleload      memberof
259 moduleload      refint
260 ";
261                 close(CONF);
262         }
263
264         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
265                 open(CONF, ">$modconf"); 
266                 # enable slapd modules
267                 print CONF "
268 moduleload      back_hdb
269 moduleload      syncprov
270 moduleload      memberof
271 moduleload      refint
272 ";
273                 close(CONF);
274         }
275
276         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
277                 open(CONF, ">$modconf"); 
278                 # enable slapd modules
279                 print CONF "
280 modulepath      /usr/lib/ldap
281 moduleload      back_hdb
282 moduleload      syncprov
283 moduleload      memberof
284 moduleload      refint
285 ";
286                 close(CONF);
287         }
288
289         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
290                 open(CONF, ">$modconf"); 
291                 # enable slapd modules (Fedora layout)
292                 print CONF "
293 modulepath      /usr/lib/openldap
294 moduleload      syncprov
295 moduleload      memberof
296 moduleload      refint
297 ";
298                 close(CONF);
299         }
300
301         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
302                 open(CONF, ">$modconf"); 
303                 # enable slapd modules (Fedora x86_64 layout)
304                 print CONF "
305 modulepath      /usr/lib64/openldap
306 moduleload      syncprov
307 moduleload      memberof
308 moduleload      refint
309 ";
310                 close(CONF);
311         }
312
313         system("slaptest -u -f $slapd_conf") == 0 or die("slaptest still fails after adding modules");
314
315     
316         $ENV{PATH} = $oldpath;
317
318         return ($slapd_conf, $pidfile);
319 }
320
321 sub mk_keyblobs($$)
322 {
323         my ($self, $tlsdir) = @_;
324
325         #TLS and PKINIT crypto blobs
326         my $dhfile = "$tlsdir/dhparms.pem";
327         my $cafile = "$tlsdir/ca.pem";
328         my $certfile = "$tlsdir/cert.pem";
329         my $reqkdc = "$tlsdir/req-kdc.der";
330         my $kdccertfile = "$tlsdir/kdc.pem";
331         my $keyfile = "$tlsdir/key.pem";
332         my $adminkeyfile = "$tlsdir/adminkey.pem";
333         my $reqadmin = "$tlsdir/req-admin.der";
334         my $admincertfile = "$tlsdir/admincert.pem";
335
336         mkdir($tlsdir, 0777);
337
338         #This is specified here to avoid draining entropy on every run
339         open(DHFILE, ">$dhfile");
340         print DHFILE <<EOF;
341 -----BEGIN DH PARAMETERS-----
342 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
343 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
344 YX70obsCAQI=
345 -----END DH PARAMETERS-----
346 EOF
347         close(DHFILE);
348
349         #Likewise, we pregenerate the key material.  This allows the 
350         #other certificates to be pre-generated
351         open(KEYFILE, ">$keyfile");
352         print KEYFILE <<EOF;
353 -----BEGIN RSA PRIVATE KEY-----
354 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
355 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
356 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
357 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
358 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
359 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
360 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
361 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
362 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
363 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
364 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
365 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
366 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
367 -----END RSA PRIVATE KEY-----
368 EOF
369         close(KEYFILE);
370
371         open(ADMINKEYFILE, ">$adminkeyfile");
372
373         print ADMINKEYFILE <<EOF;
374 -----BEGIN RSA PRIVATE KEY-----
375 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
376 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
377 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
378 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
379 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
380 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
381 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
382 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
383 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
384 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
385 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
386 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
387 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
388 -----END RSA PRIVATE KEY-----
389 EOF
390
391         #generated with 
392         #hxtool issue-certificate --self-signed --issue-ca --ca-private-key=FILE:$KEYFILE \
393         #          --subject="CN=CA,$BASEDN" --certificate="FILE:$CAFILE"
394
395         open(CAFILE, ">$cafile");
396         print CAFILE <<EOF;
397 -----BEGIN CERTIFICATE-----
398 MIIChTCCAe6gAwIBAgIUFZoF6jt0R+hQBdF7cWPy0tT3fGwwCwYJKoZIhvcNAQEFMFIxEzAR
399 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
400 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDcwMTIzMDU1MzA5WhgPMjAwODAxMjQw
401 NTUzMDlaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
402 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
403 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
404 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
405 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo1YwVDAOBgNVHQ8BAf8EBAMC
406 AqQwEgYDVR0lBAswCQYHKwYBBQIDBTAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIw
407 DwYDVR0TBAgwBgEB/wIBADANBgkqhkiG9w0BAQUFAAOBgQBgzh5uLDmESGYv60iUdEfuk/T9
408 VCpzb1z3VJVWt3uJoQYbcpR00SKeyMdlfTTLzO6tSPMmlk4hwqfvLkPzGCSObR4DRRYa0BtY
409 2laBVlg9X59bGpMUvpFQfpvxjvFWNJDL+377ELCVpLNdoR23I9TKXlalj0bY5Ks46CVIrm6W
410 EA==
411 -----END CERTIFICATE-----
412 EOF
413
414         #generated with GNUTLS internally in Samba.  
415
416         open(CERTFILE, ">$certfile");
417         print CERTFILE <<EOF;
418 -----BEGIN CERTIFICATE-----
419 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
420 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
421 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
422 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
423 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
424 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
425 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
426 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
427 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
428 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
429 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
430 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
431 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
432 -----END CERTIFICATE-----
433 EOF
434         close(CERTFILE);
435
436         #KDC certificate
437         # hxtool request-create --subject="CN=krbtgt,cn=users,$basedn" --key=FILE:$KEYFILE $KDCREQ
438
439         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE --type="pkinit-kdc" --pk-init-principal="krbtgt/$RELAM@$REALM" --req="$KDCREQ" --certificate="FILE:$KDCCERTFILE"
440
441         open(KDCCERTFILE, ">$kdccertfile");
442         print KDCCERTFILE <<EOF;
443 -----BEGIN CERTIFICATE-----
444 MIIDDDCCAnWgAwIBAgIUDEhjaOT1ZjHjHHEn+l5eYO05oK8wCwYJKoZIhvcNAQEFMFIxEzAR
445 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
446 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDcwMTIzMDcwNzA4WhgPMjAwODAxMjQw
447 NzA3MDhaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
448 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
449 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
450 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
451 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
452 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
453 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
454 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
455 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
456 AQUFAAOBgQCMSgLkIv9RobE0a95H2ECA+5YABBwKXIt4AyN/HpV7iJdRx7B9PE6vM+nboVKY
457 E7i7ECUc3bu6NgrLu7CKHelNclHWWMiZzSUwhkXyvG/LE9qtr/onNu9NfLt1OV+dwQwyLdEP
458 n63FxSmsKg3dfi3ryQI/DIKeisvipwDtLqOn9g==
459 -----END CERTIFICATE-----
460 EOF
461
462         #hxtool request-create --subject="CN=Administrator,cn=users,$basedn" --key=FILE:$ADMINKEYFILE $ADMINREQFILE
463         #hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE --type="pkinit-client" --pk-init-principal="administrator@$REALM" --req="$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE"
464         
465         open(ADMINCERTFILE, ">$admincertfile");
466         print ADMINCERTFILE <<EOF;
467 -----BEGIN CERTIFICATE-----
468 MIICwjCCAiugAwIBAgIUXyECoq4im33ByZDWZMGhtpvHYWEwCwYJKoZIhvcNAQEFMFIxEzAR
469 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
470 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDcwMTIzMDcyMzE2WhgPMjAwODAxMjQw
471 NzIzMTZaMCgxDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1pbmlzdHJhdG9yMIGfMA0G
472 CSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHE
473 si5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
474 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQABo4G8MIG5
475 MA4GA1UdDwEB/wQEAwIFoDASBgNVHSUECzAJBgcrBgEFAgMEMEgGA1UdEQRBMD+gPQYGKwYB
476 BQICoDMwMaATGxFTQU1CQS5FWEFNUExFLkNPTaEaMBigAwIBAaERMA8bDWFkbWluaXN0cmF0
477 b3IwHwYDVR0jBBgwFoAUwtm596AMotmzRU7IVdgrUvozyjIwHQYDVR0OBBYEFCDzVsvJ8IDz
478 wLYH8EONeUa5oVrGMAkGA1UdEwQCMAAwDQYJKoZIhvcNAQEFBQADgYEAbTCnaPTieVZPV3bH
479 UmAMbnF9+YN1mCbe2xZJ0xzve+Yw1XO82iv/9kZaZkcRkaQt2qcwsBK/aSPOgfqGx+mJ7hXQ
480 AGWvAJhnWi25PawNaRysCN8WC6+nWKR4d2O2m5rpj3T9kH5WE7QbG0bCu92dGaS29FvWDCP3
481 q9pRtDOoAZc=
482 -----END CERTIFICATE-----
483 EOF
484         close(ADMINCERTFILE);
485 }
486
487 sub provision($$$$$$)
488 {
489         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password) = @_;
490
491         my $smbd_loglevel = 1;
492         my $username = "administrator";
493         my $domain = "SAMBADOMAIN";
494         my $realm = "SAMBA.EXAMPLE.COM";
495         my $dnsname = "samba.example.com";
496         my $basedn = "dc=samba,dc=example,dc=com";
497         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
498         chomp $unix_name;
499         my $unix_uid = $>;
500         my $unix_gids_str = $);
501         my @unix_gids = split(" ", $unix_gids_str);
502         my $srcdir="$RealBin/..";
503         -d $prefix or mkdir($prefix, 0777) or die("Unable to create $prefix");
504         my $prefix_abs = abs_path($prefix);
505         my $tmpdir = "$prefix_abs/tmp";
506         my $etcdir = "$prefix_abs/etc";
507         my $piddir = "$prefix_abs/pid";
508         my $conffile = "$etcdir/smb.conf";
509         my $krb5_config = "$etcdir/krb5.conf";
510         my $privatedir = "$prefix_abs/private";
511         my $ncalrpcdir = "$prefix_abs/ncalrpc";
512         my $lockdir = "$prefix_abs/lockdir";
513         my $winbindd_socket_dir = "$prefix_abs/winbind_socket";
514         my $winbindd_priv_pipe_dir = "$piddir/smbd.tmp/winbind_pipe";
515         my $nsswrap_passwd = "$etcdir/passwd";
516         my $nsswrap_group = "$etcdir/group";
517
518         my $configuration = "--configfile=$conffile";
519         my $ldapdir = "$privatedir/ldap";
520
521         my $tlsdir = "$privatedir/tls";
522
523         my $ifaceipv4 = "127.0.0.$swiface";
524         my $interfaces = "$ifaceipv4/8";
525
526         (system("rm -rf $prefix/*") == 0) or die("Unable to clean up");
527         mkdir($_, 0777) foreach ($privatedir, $etcdir, $piddir, $ncalrpcdir, $lockdir, 
528                 $tmpdir);
529
530
531         my $localbasedn = $basedn;
532         $localbasedn = "DC=$netbiosname" if $server_role eq "member server";
533
534         open(CONFFILE, ">$conffile");
535         print CONFFILE "
536 [global]
537         netbios name = $netbiosname
538         netbios aliases = $netbiosalias
539         workgroup = $domain
540         realm = $realm
541         private dir = $privatedir
542         pid directory = $piddir
543         ncalrpc dir = $ncalrpcdir
544         lock dir = $lockdir
545         setup directory = $self->{setupdir}
546         modules dir = $self->{bindir}/modules
547         js include = $srcdir/scripting/libjs
548         winbindd socket directory = $winbindd_socket_dir
549         winbind separator = /
550         name resolve order = bcast
551         interfaces = $interfaces
552         tls dh params file = $tlsdir/dhparms.pem
553         panic action = $srcdir/script/gdb_backtrace \%PID% \%PROG%
554         wins support = yes
555         server role = $server_role
556         max xmit = 32K
557         server max protocol = SMB2
558         notify:inotify = false
559         ldb:nosync = true
560         system:anonymous = true
561 #We don't want to pass our self-tests if the PAC code is wrong
562         gensec:require_pac = true
563         log level = $smbd_loglevel
564
565 [tmp]
566         path = $tmpdir
567         read only = no
568         ntvfs handler = posix
569         posix:sharedelay = 100000
570         posix:eadb = $lockdir/eadb.tdb
571
572 [cifs]
573         read only = no
574         ntvfs handler = cifs
575         cifs:server = $netbiosname
576         cifs:share = tmp
577 #There is no username specified here, instead the client is expected
578 #to log in with kerberos, and smbd will used delegated credentials.
579
580 [simple]
581         path = $tmpdir
582         read only = no
583         ntvfs handler = simple
584
585 [cifsposix]
586         copy = simple
587         ntvfs handler = cifsposix   
588 ";
589         close(CONFFILE);
590
591         $self->mk_keyblobs($tlsdir);
592
593         open(KRB5CONF, ">$krb5_config");
594         print KRB5CONF "
595 #Generated krb5.conf for $realm
596
597 [libdefaults]
598  default_realm = $realm
599  dns_lookup_realm = false
600  dns_lookup_kdc = false
601  ticket_lifetime = 24h
602  forwardable = yes
603
604 [realms]
605  $realm = {
606   kdc = 127.0.0.1:88
607   admin_server = 127.0.0.1:88
608   default_domain = $dnsname
609  }
610  $dnsname = {
611   kdc = 127.0.0.1:88
612   admin_server = 127.0.0.1:88
613   default_domain = $dnsname
614  }
615  $domain = {
616   kdc = 127.0.0.1:88
617   admin_server = 127.0.0.1:88
618   default_domain = $dnsname
619  }
620
621 [appdefaults]
622         pkinit_anchors = FILE:$tlsdir/ca.pem
623
624 [kdc]
625         enable-pkinit = true
626         pkinit_identity = FILE:$tlsdir/kdc.pem,$tlsdir/key.pem
627         pkinit_anchors = FILE:$tlsdir/ca.pem
628
629 [domain_realm]
630  .$dnsname = $realm
631 ";
632         close(KRB5CONF);
633
634         open(PWD, ">$nsswrap_passwd");
635         print PWD "
636 root:x:0:0:root gecos:$prefix_abs:/bin/false
637 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
638 nobody:x:65534:65533:nobody gecos:$prefix_abs:/bin/false
639 ";
640         close(PWD);
641
642         open(GRP, ">$nsswrap_group");
643         print GRP "
644 root:x:0:
645 wheel:x:10:
646 users:x:100:
647 nobody:x:65533:
648 nogroup:x:65534:nobody
649 ";
650         close(GRP);
651
652 #Ensure the config file is valid before we start
653         if (system("$self->{bindir}/testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
654                 system("$self->{bindir}/testparm -v --suppress-prompt $configuration >&2");
655                 die("Failed to create a valid smb.conf configuration!");
656         }
657
658         (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");
659
660         my @provision_options = ();
661         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$nsswrap_passwd\"");
662         push (@provision_options, "NSS_WRAPPER_GROUP=\"$nsswrap_group\"");
663         if (defined($ENV{PROVISION_EJS})) {
664                 push (@provision_options, "$self->{bindir}/smbscript");
665                 push (@provision_options, "$self->{setupdir}/provision");
666         } else {
667                 push (@provision_options, "$self->{bindir}/smbpython");
668                 push (@provision_options, "$self->{setupdir}/provision.py");
669         }
670         push (@provision_options, split(' ', $configuration));
671         push (@provision_options, "--host-name=$netbiosname");
672         push (@provision_options, "--host-ip=$ifaceipv4");
673         push (@provision_options, "--quiet");
674         push (@provision_options, "--domain=$domain");
675         push (@provision_options, "--realm=$realm");
676         push (@provision_options, "--adminpass=$password");
677         push (@provision_options, "--krbtgtpass=krbtgt$password");
678         push (@provision_options, "--machinepass=machine$password");
679         push (@provision_options, "--root=$unix_name");
680         push (@provision_options, "--simple-bind-dn=cn=Manager,$localbasedn");
681         push (@provision_options, "--password=$password");
682         push (@provision_options, "--server-role=\"$server_role\"");
683
684         my $ldap_uri= "$ldapdir/ldapi";
685         $ldap_uri =~ s|/|%2F|g;
686         $ldap_uri = "ldapi://$ldap_uri";
687
688         my $ret = {
689                 KRB5_CONFIG => $krb5_config,
690                 PIDDIR => $piddir,
691                 SERVER => $netbiosname,
692                 SERVER_IP => $ifaceipv4,
693                 NETBIOSNAME => $netbiosname,
694                 NETBIOSALIAS => $netbiosalias,
695                 LDAP_URI => $ldap_uri,
696                 DOMAIN => $domain,
697                 USERNAME => $username,
698                 REALM => $realm,
699                 PASSWORD => $password,
700                 LDAPDIR => $ldapdir,
701                 WINBINDD_SOCKET_DIR => $winbindd_socket_dir,
702                 WINBINDD_PRIV_PIPE_DIR => $winbindd_priv_pipe_dir,
703                 NCALRPCDIR => $ncalrpcdir,
704                 LOCKDIR => $lockdir,
705                 CONFIGURATION => $configuration,
706                 SOCKET_WRAPPER_DEFAULT_IFACE => $swiface,
707                 NSS_WRAPPER_PASSWD => $nsswrap_passwd,
708                 NSS_WRAPPER_GROUP => $nsswrap_group,
709         };
710
711         if (defined($self->{ldap})) {
712
713                 push (@provision_options, "--ldap-backend=$ldap_uri");
714                 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");
715
716                 if ($self->{ldap} eq "openldap") {
717                        ($ret->{SLAPD_CONF}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ldapdir, $configuration) or die("Unable to create openldap directories");
718                 } elsif ($self->{ldap} eq "fedora-ds") {
719                        ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ldapdir, $configuration) or die("Unable to create fedora ds directories");
720                        push (@provision_options, "--ldap-module=nsuniqueid");
721                        push (@provision_options, "'--aci=aci:: KHRhcmdldGF0dHIgPSAiKiIpICh2ZXJzaW9uIDMuMDthY2wgImZ1bGwgYWNjZXNzIHRvIGFsbCBieSBhbGwiO2FsbG93IChhbGwpKHVzZXJkbiA9ICJsZGFwOi8vL2FueW9uZSIpOykK'");
722                  }
723
724                 $self->slapd_start($ret) or 
725                         die("couldn't start slapd");
726         }
727
728         my $provision_cmd = join(" ", @provision_options);
729         (system($provision_cmd) == 0) or die("Unable to provision: \n$provision_cmd\n");
730
731         if (defined($self->{ldap})) {
732                 $self->slapd_stop($ret) or 
733                         die("couldn't stop slapd");
734         }
735
736         return $ret; 
737 }
738
739 sub provision_member($$$)
740 {
741         my ($self, $prefix, $dcvars) = @_;
742         print "PROVISIONING MEMBER...";
743
744         my $ret = $self->provision($prefix,
745                                    "member server",
746                                    "localmember3",
747                                    "localmember",
748                                    3,
749                                    "localmemberpass");
750
751         $ret or die("Unable to provision");
752
753         my $cmd = "";
754         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
755         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
756         $cmd .= "$self->{bindir}/net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
757         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
758
759         system($cmd) == 0 or die("Join failed\n$cmd");
760
761         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
762         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
763         $ret->{SMBD_TEST_LOG_POS} = 0;
764
765         $ret->{DC_SERVER} = $dcvars->{SERVER};
766         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
767         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
768         $ret->{DC_NETBIOSALIAS} = $dcvars->{NETBIOSALIAS};
769         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
770         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
771
772         return $ret;
773 }
774
775 sub provision_dc($$)
776 {
777         my ($self, $prefix) = @_;
778
779         print "PROVISIONING DC...";
780         my $ret = $self->provision($prefix,
781                                    "domain controller",
782                                    "localdc1",
783                                    "localdc",
784                                    1,
785                                    "localdcpass");
786
787         $self->add_wins_config("$prefix/private") or 
788                 die("Unable to add wins configuration");
789
790         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
791         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
792         $ret->{SMBD_TEST_LOG_POS} = 0;
793         return $ret;
794 }
795
796 sub teardown_env($$)
797 {
798         my ($self, $envvars) = @_;
799         my $pid;
800
801         close(DATA);
802
803         if (-f "$envvars->{PIDDIR}/smbd.pid" ) {
804                 open(IN, "<$envvars->{PIDDIR}/smbd.pid") or die("unable to open smbd pid file");
805                 $pid = <IN>;
806                 close(IN);
807
808                 # Give the process 20 seconds to exit.  gcov needs
809                 # this time to write out the covarge data
810                 my $count = 0;
811                 until (kill(0, $pid) == 0) {
812                     # if no process sucessfully signalled, then we are done
813                     sleep(1);
814                     $count++;
815                     last if $count > 20;
816                 }
817                 
818                 # If it is still around, kill it
819                 if ($count > 20) {
820                     print "smbd process $pid took more than $count seconds to exit, killing\n";
821                     kill 9, $pid;
822                 }
823         }
824
825         my $failed = $? >> 8;
826
827         $self->slapd_stop($envvars) if ($self->{ldap});
828
829         print $self->getlog_env($envvars);
830
831         return $failed;
832 }
833
834 sub getlog_env($$)
835 {
836         my ($self, $envvars) = @_;
837         my $title = "SMBD LOG of: $envvars->{NETBIOSNAME}\n";
838         my $out = $title;
839
840         open(LOG, "<$envvars->{SMBD_TEST_LOG}");
841
842         seek(LOG, $envvars->{SMBD_TEST_LOG_POS}, SEEK_SET);
843         while (<LOG>) {
844                 $out .= $_;
845         }
846         $envvars->{SMBD_TEST_LOG_POS} = tell(LOG);
847         close(LOG);
848
849         return "" if $out eq $title;
850  
851         return $out;
852 }
853
854 sub check_env($$)
855 {
856         my ($self, $envvars) = @_;
857
858         return 1 if (-p $envvars->{SMBD_TEST_FIFO});
859
860         print $self->getlog_env($envvars);
861
862         return 0;
863 }
864
865 sub setup_env($$$)
866 {
867         my ($self, $envname, $path) = @_;
868
869         if ($envname eq "dc") {
870                 return $self->setup_dc("$path/dc");
871         } elsif ($envname eq "member") {
872                 if (not defined($self->{vars}->{dc})) {
873                         $self->setup_dc("$path/dc");
874                 }
875                 return $self->setup_member("$path/member", $self->{vars}->{dc});
876         } else {
877                 die("Samba4 can't provide environment '$envname'");
878         }
879 }
880
881 sub setup_member($$$$)
882 {
883         my ($self, $path, $dc_vars) = @_;
884
885         my $env = $self->provision_member($path, $dc_vars);
886
887         $self->check_or_start($env, ($ENV{SMBD_MAXTIME} or 7500));
888
889         $self->wait_for_start($env);
890
891         return $env;
892 }
893
894 sub setup_dc($$)
895 {
896         my ($self, $path) = @_;
897
898         my $env = $self->provision_dc($path);
899
900         $self->check_or_start($env, 
901                 ($ENV{SMBD_MAXTIME} or 7500));
902
903         $self->wait_for_start($env);
904
905         $self->{vars}->{dc} = $env;
906
907         return $env;
908 }
909
910 sub stop($)
911 {
912         my ($self) = @_;
913 }
914
915 1;