Merge branch 'master' of ssh://git.samba.org/data/git/samba into selftest
[tprouty/samba.git] / selftest / target / Samba4.pm
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU GPL, v3 or later.
5
6 package Samba4;
7
8 use strict;
9 use Cwd qw(abs_path);
10 use FindBin qw($RealBin);
11 use POSIX;
12
13 sub new($$$$) {
14         my ($classname, $bindir, $ldap, $setupdir) = @_;
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}/samba $optarg $env_vars->{CONFIGURATION} -M single -i --leak-report-full");
120                 if ($? == -1) {
121                         print "Unable to start samba: $ret: $!\n";
122                         exit 1;
123                 }
124                 unlink($env_vars->{SMBD_TEST_FIFO});
125                 my $exit = $? >> 8;
126                 if ( $ret == 0 ) {
127                         print "samba exits with status $exit\n";
128                 } elsif ( $ret & 127 ) {
129                         print "samba got signal ".($ret & 127)." and exits with $exit!\n";
130                 } else {
131                         $ret = $? >> 8;
132                         print "samba 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 my $dir = getcwd();
205 chdir "$ENV{FEDORA_DS_ROOT}/bin" || die;
206         if (system("perl $ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl --silent --file=$fedora_ds_inf >&2") != 0) {
207             chdir $dir;
208             die("perl $ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl --silent --file=$fedora_ds_inf FAILED: $?");
209         }
210         chdir $dir || die;
211
212         return ($fedora_ds_dir, $pidfile);
213 }
214
215 sub mk_openldap($$$)
216 {
217         my ($self, $ldapdir, $configuration) = @_;
218
219         my $slapd_conf = "$ldapdir/slapd.conf";
220         my $pidfile = "$ldapdir/slapd.pid";
221         my $modconf = "$ldapdir/modules.conf";
222
223         my $oldpath = $ENV{PATH};
224         my $olpath = "";
225         my $olroot = "";
226         if (defined $ENV{OPENLDAP_ROOT}) {
227                $olroot = "$ENV{OPENLDAP_ROOT}";
228                $olpath = "$olroot/libexec:$olroot/sbin:";
229         }
230         $ENV{PATH} = "$olpath/usr/local/sbin:/usr/sbin:/sbin:$ENV{PATH}";
231
232         unlink($modconf);
233         open(CONF, ">$modconf"); close(CONF);
234
235         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
236                 open(CONF, ">$modconf"); 
237                 # enable slapd modules
238                 print CONF "
239 modulepath      $olroot/libexec/openldap
240 moduleload      syncprov
241 moduleload      memberof
242 moduleload      refint
243 ";
244                 close(CONF);
245         }
246         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
247                 open(CONF, ">$modconf"); 
248                 # enable slapd modules
249                 print CONF "
250 modulepath      $olroot/libexec/openldap
251 moduleload      back_hdb
252 moduleload      syncprov
253 moduleload      memberof
254 moduleload      refint
255 ";
256                 close(CONF);
257         }
258
259         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
260                 open(CONF, ">$modconf"); 
261                 # enable slapd modules
262                 print CONF "
263 moduleload      back_hdb
264 moduleload      syncprov
265 moduleload      memberof
266 moduleload      refint
267 ";
268                 close(CONF);
269         }
270
271         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
272                 open(CONF, ">$modconf"); 
273                 # enable slapd modules
274                 print CONF "
275 modulepath      /usr/lib/ldap
276 moduleload      back_hdb
277 moduleload      syncprov
278 moduleload      memberof
279 moduleload      refint
280 ";
281                 close(CONF);
282         }
283
284         if (system("slaptest -u -f $slapd_conf >&2") != 0) {
285                 open(CONF, ">$modconf"); 
286                 # enable slapd modules (Fedora layout)
287                 print CONF "
288 modulepath      /usr/lib/openldap
289 moduleload      syncprov
290 moduleload      memberof
291 moduleload      refint
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 moduleload      refint
304 ";
305                 close(CONF);
306         }
307
308         system("slaptest -u -f $slapd_conf") == 0 or die("slaptest still fails after adding modules");
309
310     
311         $ENV{PATH} = $oldpath;
312
313         return ($slapd_conf, $pidfile);
314 }
315
316 sub mk_keyblobs($$)
317 {
318         my ($self, $tlsdir) = @_;
319
320         #TLS and PKINIT crypto blobs
321         my $dhfile = "$tlsdir/dhparms.pem";
322         my $cafile = "$tlsdir/ca.pem";
323         my $certfile = "$tlsdir/cert.pem";
324         my $reqkdc = "$tlsdir/req-kdc.der";
325         my $kdccertfile = "$tlsdir/kdc.pem";
326         my $keyfile = "$tlsdir/key.pem";
327         my $adminkeyfile = "$tlsdir/adminkey.pem";
328         my $reqadmin = "$tlsdir/req-admin.der";
329         my $admincertfile = "$tlsdir/admincert.pem";
330
331         mkdir($tlsdir, 0777);
332
333         #This is specified here to avoid draining entropy on every run
334         open(DHFILE, ">$dhfile");
335         print DHFILE <<EOF;
336 -----BEGIN DH PARAMETERS-----
337 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
338 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
339 YX70obsCAQI=
340 -----END DH PARAMETERS-----
341 EOF
342         close(DHFILE);
343
344         #Likewise, we pregenerate the key material.  This allows the 
345         #other certificates to be pre-generated
346         open(KEYFILE, ">$keyfile");
347         print KEYFILE <<EOF;
348 -----BEGIN RSA PRIVATE KEY-----
349 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
350 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
351 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
352 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
353 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
354 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
355 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
356 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
357 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
358 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
359 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
360 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
361 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
362 -----END RSA PRIVATE KEY-----
363 EOF
364         close(KEYFILE);
365
366         open(ADMINKEYFILE, ">$adminkeyfile");
367
368         print ADMINKEYFILE <<EOF;
369 -----BEGIN RSA PRIVATE KEY-----
370 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
371 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
372 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
373 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
374 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
375 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
376 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
377 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
378 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
379 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
380 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
381 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
382 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
383 -----END RSA PRIVATE KEY-----
384 EOF
385
386         #generated with 
387         # hxtool issue-certificate --self-signed --issue-ca \
388         # --ca-private-key="FILE:$KEYFILE" \
389         # --subject="CN=CA,DC=samba,DC=example,DC=com" \
390         # --certificate="FILE:$CAFILE" --lifetime="25 years"
391
392         open(CAFILE, ">$cafile");
393         print CAFILE <<EOF;
394 -----BEGIN CERTIFICATE-----
395 MIICcTCCAdqgAwIBAgIUaBPmjnPVqyFqR5foICmLmikJTzgwCwYJKoZIhvcNAQEFMFIxEzAR
396 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
397 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTIyMzEyWhgPMjAzMzAyMjQx
398 MjIzMTJaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
399 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
400 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
401 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
402 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo0IwQDAOBgNVHQ8BAf8EBAMC
403 AaYwHQYDVR0OBBYEFMLZufegDKLZs0VOyFXYK1L6M8oyMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
404 KoZIhvcNAQEFBQADgYEAAZJbCAAkaqgFJ0xgNovn8Ydd0KswQPjicwiODPgw9ZPoD2HiOUVO
405 yYDRg/dhFF9y656OpcHk4N7qZ2sl3RlHkzDu+dseETW+CnKvQIoXNyeARRJSsSlwrwcoD4JR
406 HTLk2sGigsWwrJ2N99sG/cqSJLJ1MFwLrs6koweBnYU0f/g=
407 -----END CERTIFICATE-----
408 EOF
409
410         #generated with GNUTLS internally in Samba.  
411
412         open(CERTFILE, ">$certfile");
413         print CERTFILE <<EOF;
414 -----BEGIN CERTIFICATE-----
415 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
416 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
417 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
418 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
419 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
420 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
421 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
422 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
423 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
424 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
425 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
426 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
427 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
428 -----END CERTIFICATE-----
429 EOF
430         close(CERTFILE);
431
432         #KDC certificate
433         # hxtool request-create \
434         # --subject="CN=krbtgt,CN=users,DC=samba,DC=example,DC=com" \
435         # --key="FILE:$KEYFILE" $KDCREQ
436
437         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
438         # --type="pkinit-kdc" \
439         # --pk-init-principal="krbtgt/SAMBA.EXAMPLE.COM@SAMBA.EXAMPLE.COM" \
440         # --req="PKCS10:$KDCREQ" --certificate="FILE:$KDCCERTFILE" \
441         # --lifetime="25 years"
442
443         open(KDCCERTFILE, ">$kdccertfile");
444         print KDCCERTFILE <<EOF;
445 -----BEGIN CERTIFICATE-----
446 MIIDDDCCAnWgAwIBAgIUI2Tzj+JnMzMcdeabcNo30rovzFAwCwYJKoZIhvcNAQEFMFIxEzAR
447 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
448 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTMxOTIzWhgPMjAzMzAyMjQx
449 MzE5MjNaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
450 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
451 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
452 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
453 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
454 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
455 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
456 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
457 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
458 AQUFAAOBgQBmrVD5MCmZjfHp1nEnHqTIh8r7lSmVtDx4s9MMjxm9oNrzbKXynvdhwQYFVarc
459 ge4yRRDXtSebErOl71zVJI9CVeQQpwcH+tA85oGA7oeFtO/S7ls581RUU6tGgyxV4veD+lJv
460 KPH5LevUtgD+q9H4LU4Sq5N3iFwBaeryB0g2wg==
461 -----END CERTIFICATE-----
462 EOF
463
464         # hxtool request-create \
465         # --subject="CN=Administrator,CN=users,DC=samba,DC=example,DC=com" \
466         # --key="FILE:$ADMINKEYFILE" $ADMINREQFILE
467
468         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
469         # --type="pkinit-client" \
470         # --pk-init-principal="administrator@SAMBA.EXAMPLE.COM" \
471         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE" \
472         # --lifetime="25 years"
473         
474         open(ADMINCERTFILE, ">$admincertfile");
475         print ADMINCERTFILE <<EOF;
476 -----BEGIN CERTIFICATE-----
477 MIIDHTCCAoagAwIBAgIUC0W5dW/N9kE+NgD0mKK34YgyqQ0wCwYJKoZIhvcNAQEFMFIxEzAR
478 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
479 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTMyMzAwWhgPMjAzMzAyMjQx
480 MzIzMDBaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
481 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
482 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
483 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
484 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
485 l3JRlwIDAQABo4HSMIHPMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
486 BgEFBQcDAgYKKwYBBAGCNxQCAjBIBgNVHREEQTA/oD0GBisGAQUCAqAzMDGgExsRU0FNQkEu
487 RVhBTVBMRS5DT02hGjAYoAMCAQGhETAPGw1hZG1pbmlzdHJhdG9yMB8GA1UdIwQYMBaAFMLZ
488 ufegDKLZs0VOyFXYK1L6M8oyMB0GA1UdDgQWBBQg81bLyfCA88C2B/BDjXlGuaFaxjAJBgNV
489 HRMEAjAAMA0GCSqGSIb3DQEBBQUAA4GBAHsqSqul0hZCXn4t8Kfp3v/JLMiUMJihR1XOgzoa
490 ufLOQ1HNzFUHKuo1JEQ1+i5gHT/arLu/ZBF4BfQol7vW27gKIEt0fkRV8EvoPxXvSokHq0Ku
491 HCuPOhYNEP3wYiwB3g93NMCinWVlz0mh5aijEU7y/XrjlZxBKFFrTE+BJi1o
492 -----END CERTIFICATE-----
493 EOF
494         close(ADMINCERTFILE);
495 }
496
497 sub provision($$$$$$)
498 {
499         my ($self, $prefix, $server_role, $netbiosname, $netbiosalias, $swiface, $password) = @_;
500
501         my $server_loglevel = 1;
502         my $username = "administrator";
503         my $domain = "SAMBADOMAIN";
504         my $realm = "SAMBA.EXAMPLE.COM";
505         my $dnsname = "samba.example.com";
506         my $basedn = "dc=samba,dc=example,dc=com";
507         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
508         chomp $unix_name;
509         my $unix_uid = $>;
510         my $unix_gids_str = $);
511         my @unix_gids = split(" ", $unix_gids_str);
512         my $srcdir="$RealBin/..";
513         -d $prefix or mkdir($prefix, 0777) or die("Unable to create $prefix");
514         my $prefix_abs = abs_path($prefix);
515         my $tmpdir = "$prefix_abs/tmp";
516         my $etcdir = "$prefix_abs/etc";
517         my $piddir = "$prefix_abs/pid";
518         my $conffile = "$etcdir/smb.conf";
519         my $krb5_config = "$etcdir/krb5.conf";
520         my $privatedir = "$prefix_abs/private";
521         my $ncalrpcdir = "$prefix_abs/ncalrpc";
522         my $lockdir = "$prefix_abs/lockdir";
523         my $winbindd_socket_dir = "$prefix_abs/winbindd_socket";
524         my $winbindd_privileged_socket_dir = "$prefix_abs/winbindd_privileged_socket";
525         my $ntp_signd_socket_dir = "$prefix_abs/ntp_signd_socket";
526         my $nsswrap_passwd = "$etcdir/passwd";
527         my $nsswrap_group = "$etcdir/group";
528
529         my $configuration = "--configfile=$conffile";
530         my $ldapdir = "$privatedir/ldap";
531
532         my $tlsdir = "$privatedir/tls";
533
534         my $ifaceipv4 = "127.0.0.$swiface";
535         my $interfaces = "$ifaceipv4/8";
536
537         (system("rm -rf $prefix/*") == 0) or die("Unable to clean up");
538         mkdir($_, 0777) foreach ($privatedir, $etcdir, $piddir, $ncalrpcdir, $lockdir, 
539                 $tmpdir, "$tmpdir/test1", "$tmpdir/test2");
540
541
542         my $localbasedn = $basedn;
543         $localbasedn = "CN=$netbiosname" if $server_role eq "member server";
544
545         open(CONFFILE, ">$conffile");
546         print CONFFILE "
547 [global]
548         netbios name = $netbiosname
549         netbios aliases = $netbiosalias
550         workgroup = $domain
551         realm = $realm
552         private dir = $privatedir
553         pid directory = $piddir
554         ncalrpc dir = $ncalrpcdir
555         lock dir = $lockdir
556         setup directory = $self->{setupdir}
557         modules dir = $self->{bindir}/modules
558         winbindd socket directory = $winbindd_socket_dir
559         winbindd privileged socket directory = $winbindd_privileged_socket_dir
560         ntp signd socket directory = $ntp_signd_socket_dir
561         winbind separator = /
562         name resolve order = bcast
563         interfaces = $interfaces
564         tls dh params file = $tlsdir/dhparms.pem
565         panic action = $srcdir/script/gdb_backtrace \%PID% \%PROG%
566         wins support = yes
567         server role = $server_role
568         max xmit = 32K
569         server max protocol = SMB2
570         notify:inotify = false
571         ldb:nosync = true
572 #We don't want to pass our self-tests if the PAC code is wrong
573         gensec:require_pac = true
574         log level = $server_loglevel
575
576 [tmp]
577         path = $tmpdir
578         read only = no
579         ntvfs handler = posix
580         posix:sharedelay = 100000
581         posix:eadb = $lockdir/eadb.tdb
582         posix:oplocktimeout = 3
583         posix:writetimeupdatedelay = 500000
584
585 [test1]
586         path = $tmpdir/test1
587         read only = no
588         ntvfs handler = posix
589         posix:sharedelay = 100000
590         posix:eadb = $lockdir/eadb.tdb
591         posix:oplocktimeout = 3
592         posix:writetimeupdatedelay = 500000
593
594 [test2]
595         path = $tmpdir/test2
596         read only = no
597         ntvfs handler = posix
598         posix:sharedelay = 100000
599         posix:eadb = $lockdir/eadb.tdb
600         posix:oplocktimeout = 3
601         posix:writetimeupdatedelay = 500000
602
603 [cifs]
604         read only = no
605         ntvfs handler = cifs
606         cifs:server = $netbiosname
607         cifs:share = tmp
608 #There is no username specified here, instead the client is expected
609 #to log in with kerberos, and the serverwill use delegated credentials.
610
611 [simple]
612         path = $tmpdir
613         read only = no
614         ntvfs handler = simple
615
616 [sysvol]
617         path = $lockdir/sysvol
618         read only = yes
619
620 [netlogon]
621         path = $lockdir/sysvol/$dnsname/scripts
622         read only = no
623
624 [cifsposix]
625         copy = simple
626         ntvfs handler = cifsposix   
627 ";
628         close(CONFFILE);
629
630         $self->mk_keyblobs($tlsdir);
631
632         open(KRB5CONF, ">$krb5_config");
633         print KRB5CONF "
634 #Generated krb5.conf for $realm
635
636 [libdefaults]
637  default_realm = $realm
638  dns_lookup_realm = false
639  dns_lookup_kdc = false
640  ticket_lifetime = 24h
641  forwardable = yes
642
643 [realms]
644  $realm = {
645   kdc = 127.0.0.1:88
646   admin_server = 127.0.0.1:88
647   default_domain = $dnsname
648  }
649  $dnsname = {
650   kdc = 127.0.0.1:88
651   admin_server = 127.0.0.1:88
652   default_domain = $dnsname
653  }
654  $domain = {
655   kdc = 127.0.0.1:88
656   admin_server = 127.0.0.1:88
657   default_domain = $dnsname
658  }
659
660 [appdefaults]
661         pkinit_anchors = FILE:$tlsdir/ca.pem
662
663 [kdc]
664         enable-pkinit = true
665         pkinit_identity = FILE:$tlsdir/kdc.pem,$tlsdir/key.pem
666         pkinit_anchors = FILE:$tlsdir/ca.pem
667
668 [domain_realm]
669  .$dnsname = $realm
670 ";
671         close(KRB5CONF);
672
673         open(PWD, ">$nsswrap_passwd");
674         print PWD "
675 root:x:0:0:root gecos:$prefix_abs:/bin/false
676 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
677 nobody:x:65534:65533:nobody gecos:$prefix_abs:/bin/false
678 ";
679         close(PWD);
680
681         open(GRP, ">$nsswrap_group");
682         print GRP "
683 root:x:0:
684 wheel:x:10:
685 users:x:100:
686 nobody:x:65533:
687 nogroup:x:65534:nobody
688 ";
689         close(GRP);
690
691 #Ensure the config file is valid before we start
692         if (system("$self->{bindir}/testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
693                 system("$self->{bindir}/testparm -v --suppress-prompt $configuration >&2");
694                 die("Failed to create a valid smb.conf configuration!");
695         }
696
697         (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");
698
699         my @provision_options = ();
700         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$nsswrap_passwd\"");
701         push (@provision_options, "NSS_WRAPPER_GROUP=\"$nsswrap_group\"");
702         if (defined($ENV{GDB_PROVISION})) {
703                 push (@provision_options, "gdb --args python");
704         }
705         if (defined($ENV{VALGRIND_PROVISION})) {
706                 push (@provision_options, "valgrind");
707         }
708         push (@provision_options, "$self->{setupdir}/provision");
709         push (@provision_options, split(' ', $configuration));
710         push (@provision_options, "--host-name=$netbiosname");
711         push (@provision_options, "--host-ip=$ifaceipv4");
712         push (@provision_options, "--quiet");
713         push (@provision_options, "--domain=$domain");
714         push (@provision_options, "--realm=$realm");
715         push (@provision_options, "--adminpass=$password");
716         push (@provision_options, "--krbtgtpass=krbtgt$password");
717         push (@provision_options, "--machinepass=machine$password");
718         push (@provision_options, "--root=$unix_name");
719
720         push (@provision_options, "--server-role=\"$server_role\"");
721
722         my $ldap_uri= "$ldapdir/ldapi";
723         $ldap_uri =~ s|/|%2F|g;
724         $ldap_uri = "ldapi://$ldap_uri";
725
726         my $ret = {
727                 KRB5_CONFIG => $krb5_config,
728                 PIDDIR => $piddir,
729                 SERVER => $netbiosname,
730                 SERVER_IP => $ifaceipv4,
731                 NETBIOSNAME => $netbiosname,
732                 NETBIOSALIAS => $netbiosalias,
733                 LDAP_URI => $ldap_uri,
734                 DOMAIN => $domain,
735                 USERNAME => $username,
736                 REALM => $realm,
737                 PASSWORD => $password,
738                 LDAPDIR => $ldapdir,
739                 WINBINDD_SOCKET_DIR => $winbindd_socket_dir,
740                 NCALRPCDIR => $ncalrpcdir,
741                 LOCKDIR => $lockdir,
742                 CONFIGURATION => $configuration,
743                 SOCKET_WRAPPER_DEFAULT_IFACE => $swiface,
744                 NSS_WRAPPER_PASSWD => $nsswrap_passwd,
745                 NSS_WRAPPER_GROUP => $nsswrap_group,
746         };
747
748         if (defined($self->{ldap})) {
749
750                 push (@provision_options, "--ldap-backend=$ldap_uri");
751                 system("$self->{setupdir}/provision-backend $configuration --ldap-admin-pass=$password --root=$unix_name --realm=$realm --domain=$domain --host-name=$netbiosname --ldap-backend-type=$self->{ldap}>&2") == 0 or die("backend provision failed");
752
753                 push (@provision_options, "--password=$password");
754
755                 if ($self->{ldap} eq "openldap") {
756                        push (@provision_options, "--username=samba-admin");
757                        ($ret->{SLAPD_CONF}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ldapdir, $configuration) or die("Unable to create openldap directories");
758                        push (@provision_options, "--ldap-backend-type=openldap");
759                 } elsif ($self->{ldap} eq "fedora-ds") {
760                        push (@provision_options, "--simple-bind-dn=cn=Manager,$localbasedn");
761                        ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ldapdir, $configuration) or die("Unable to create fedora ds directories");
762                        push (@provision_options, "--ldap-backend-type=fedora-ds");
763                  }
764
765                 $self->slapd_start($ret) or 
766                         die("couldn't start slapd");
767         }
768
769         my $provision_cmd = join(" ", @provision_options);
770         (system($provision_cmd) == 0) or die("Unable to provision: \n$provision_cmd\n");
771
772         if (defined($self->{ldap})) {
773                 $self->slapd_stop($ret) or 
774                         die("couldn't stop slapd");
775         }
776
777         return $ret; 
778 }
779
780 sub provision_member($$$)
781 {
782         my ($self, $prefix, $dcvars) = @_;
783         print "PROVISIONING MEMBER...";
784
785         my $ret = $self->provision($prefix,
786                                    "member server",
787                                    "localmember3",
788                                    "localmember",
789                                    3,
790                                    "localmemberpass");
791
792         $ret or die("Unable to provision");
793
794         my $cmd = "";
795         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
796         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
797         $cmd .= "$self->{bindir}/net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
798         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
799
800         system($cmd) == 0 or die("Join failed\n$cmd");
801
802         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
803         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
804         $ret->{SMBD_TEST_LOG_POS} = 0;
805
806         $ret->{DC_SERVER} = $dcvars->{SERVER};
807         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
808         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
809         $ret->{DC_NETBIOSALIAS} = $dcvars->{NETBIOSALIAS};
810         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
811         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
812
813         return $ret;
814 }
815
816 sub provision_dc($$)
817 {
818         my ($self, $prefix) = @_;
819
820         print "PROVISIONING DC...";
821         my $ret = $self->provision($prefix,
822                                    "domain controller",
823                                    "localdc1",
824                                    "localdc",
825                                    1,
826                                    "localdcpass");
827
828         $self->add_wins_config("$prefix/private") or 
829                 die("Unable to add wins configuration");
830
831         $ret->{SMBD_TEST_FIFO} = "$prefix/server_test.fifo";
832         $ret->{SMBD_TEST_LOG} = "$prefix/server_test.log";
833         $ret->{SMBD_TEST_LOG_POS} = 0;
834         return $ret;
835 }
836
837 sub teardown_env($$)
838 {
839         my ($self, $envvars) = @_;
840         my $pid;
841
842         close(DATA);
843
844         if (-f "$envvars->{PIDDIR}/samba.pid" ) {
845                 open(IN, "<$envvars->{PIDDIR}/samba.pid") or die("unable to open server pid file");
846                 $pid = <IN>;
847                 close(IN);
848
849                 # Give the process 20 seconds to exit.  gcov needs
850                 # this time to write out the covarge data
851                 my $count = 0;
852                 until (kill(0, $pid) == 0) {
853                     # if no process sucessfully signalled, then we are done
854                     sleep(1);
855                     $count++;
856                     last if $count > 20;
857                 }
858                 
859                 # If it is still around, kill it
860                 if ($count > 20) {
861                     print "server process $pid took more than $count seconds to exit, killing\n";
862                     kill 9, $pid;
863                 }
864         }
865
866         my $failed = $? >> 8;
867
868         $self->slapd_stop($envvars) if ($self->{ldap});
869
870         print $self->getlog_env($envvars);
871
872         return $failed;
873 }
874
875 sub getlog_env($$)
876 {
877         my ($self, $envvars) = @_;
878         my $title = "SMBD LOG of: $envvars->{NETBIOSNAME}\n";
879         my $out = $title;
880
881         open(LOG, "<$envvars->{SMBD_TEST_LOG}");
882
883         seek(LOG, $envvars->{SMBD_TEST_LOG_POS}, SEEK_SET);
884         while (<LOG>) {
885                 $out .= $_;
886         }
887         $envvars->{SMBD_TEST_LOG_POS} = tell(LOG);
888         close(LOG);
889
890         return "" if $out eq $title;
891  
892         return $out;
893 }
894
895 sub check_env($$)
896 {
897         my ($self, $envvars) = @_;
898
899         return 1 if (-p $envvars->{SMBD_TEST_FIFO});
900
901         print $self->getlog_env($envvars);
902
903         return 0;
904 }
905
906 sub setup_env($$$)
907 {
908         my ($self, $envname, $path) = @_;
909
910         if ($envname eq "dc") {
911                 return $self->setup_dc("$path/dc");
912         } elsif ($envname eq "member") {
913                 if (not defined($self->{vars}->{dc})) {
914                         $self->setup_dc("$path/dc");
915                 }
916                 return $self->setup_member("$path/member", $self->{vars}->{dc});
917         } else {
918                 die("Samba4 can't provide environment '$envname'");
919         }
920 }
921
922 sub setup_member($$$$)
923 {
924         my ($self, $path, $dc_vars) = @_;
925
926         my $env = $self->provision_member($path, $dc_vars);
927
928         $self->check_or_start($env, ($ENV{SMBD_MAXTIME} or 7500));
929
930         $self->wait_for_start($env);
931
932         return $env;
933 }
934
935 sub setup_dc($$)
936 {
937         my ($self, $path) = @_;
938
939         my $env = $self->provision_dc($path);
940
941         $self->check_or_start($env, 
942                 ($ENV{SMBD_MAXTIME} or 7500));
943
944         $self->wait_for_start($env);
945
946         $self->{vars}->{dc} = $env;
947
948         return $env;
949 }
950
951 sub stop($)
952 {
953         my ($self) = @_;
954 }
955
956 1;