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