552f6535af2288d720cc9d1866ee32587f54af2e
[samba.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 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 $smbd_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/winbind_socket";
524         my $winbindd_priv_pipe_dir = "$piddir/smbd.tmp/winbind_pipe";
525         my $nsswrap_passwd = "$etcdir/passwd";
526         my $nsswrap_group = "$etcdir/group";
527
528         my $configuration = "--configfile=$conffile";
529         my $ldapdir = "$privatedir/ldap";
530
531         my $tlsdir = "$privatedir/tls";
532
533         my $ifaceipv4 = "127.0.0.$swiface";
534         my $interfaces = "$ifaceipv4/8";
535
536         (system("rm -rf $prefix/*") == 0) or die("Unable to clean up");
537         mkdir($_, 0777) foreach ($privatedir, $etcdir, $piddir, $ncalrpcdir, $lockdir, 
538                 $tmpdir, "$tmpdir/test1", "$tmpdir/test2");
539
540
541         my $localbasedn = $basedn;
542         $localbasedn = "CN=$netbiosname" if $server_role eq "member server";
543
544         open(CONFFILE, ">$conffile");
545         print CONFFILE "
546 [global]
547         netbios name = $netbiosname
548         netbios aliases = $netbiosalias
549         workgroup = $domain
550         realm = $realm
551         private dir = $privatedir
552         pid directory = $piddir
553         ncalrpc dir = $ncalrpcdir
554         lock dir = $lockdir
555         setup directory = $self->{setupdir}
556         modules dir = $self->{bindir}/modules
557         js include = $srcdir/scripting/libjs
558         winbindd socket directory = $winbindd_socket_dir
559         winbind separator = /
560         name resolve order = bcast
561         interfaces = $interfaces
562         tls dh params file = $tlsdir/dhparms.pem
563         panic action = $srcdir/script/gdb_backtrace \%PID% \%PROG%
564         wins support = yes
565         server role = $server_role
566         max xmit = 32K
567         server max protocol = SMB2
568         notify:inotify = false
569         ldb:nosync = true
570         system:anonymous = true
571 #We don't want to pass our self-tests if the PAC code is wrong
572         gensec:require_pac = true
573         log level = $smbd_loglevel
574
575 [tmp]
576         path = $tmpdir
577         read only = no
578         ntvfs handler = posix
579         posix:sharedelay = 100000
580         posix:eadb = $lockdir/eadb.tdb
581         posix:oplocktimeout = 3
582
583 [test1]
584         path = $tmpdir/test1
585         read only = no
586         ntvfs handler = posix
587         posix:sharedelay = 100000
588         posix:eadb = $lockdir/eadb.tdb
589         posix:oplocktimeout = 3
590
591 [test2]
592         path = $tmpdir/test2
593         read only = no
594         ntvfs handler = posix
595         posix:sharedelay = 100000
596         posix:eadb = $lockdir/eadb.tdb
597         posix:oplocktimeout = 3
598
599 [cifs]
600         read only = no
601         ntvfs handler = cifs
602         cifs:server = $netbiosname
603         cifs:share = tmp
604 #There is no username specified here, instead the client is expected
605 #to log in with kerberos, and smbd will used delegated credentials.
606
607 [simple]
608         path = $tmpdir
609         read only = no
610         ntvfs handler = simple
611
612 [sysvol]
613         path = $lockdir/sysvol
614         read only = yes
615
616 [netlogon]
617         path = $lockdir/sysvol/$dnsname/scripts
618         read only = no
619
620 [cifsposix]
621         copy = simple
622         ntvfs handler = cifsposix   
623 ";
624         close(CONFFILE);
625
626         $self->mk_keyblobs($tlsdir);
627
628         open(KRB5CONF, ">$krb5_config");
629         print KRB5CONF "
630 #Generated krb5.conf for $realm
631
632 [libdefaults]
633  default_realm = $realm
634  dns_lookup_realm = false
635  dns_lookup_kdc = false
636  ticket_lifetime = 24h
637  forwardable = yes
638
639 [realms]
640  $realm = {
641   kdc = 127.0.0.1:88
642   admin_server = 127.0.0.1:88
643   default_domain = $dnsname
644  }
645  $dnsname = {
646   kdc = 127.0.0.1:88
647   admin_server = 127.0.0.1:88
648   default_domain = $dnsname
649  }
650  $domain = {
651   kdc = 127.0.0.1:88
652   admin_server = 127.0.0.1:88
653   default_domain = $dnsname
654  }
655
656 [appdefaults]
657         pkinit_anchors = FILE:$tlsdir/ca.pem
658
659 [kdc]
660         enable-pkinit = true
661         pkinit_identity = FILE:$tlsdir/kdc.pem,$tlsdir/key.pem
662         pkinit_anchors = FILE:$tlsdir/ca.pem
663
664 [domain_realm]
665  .$dnsname = $realm
666 ";
667         close(KRB5CONF);
668
669         open(PWD, ">$nsswrap_passwd");
670         print PWD "
671 root:x:0:0:root gecos:$prefix_abs:/bin/false
672 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
673 nobody:x:65534:65533:nobody gecos:$prefix_abs:/bin/false
674 ";
675         close(PWD);
676
677         open(GRP, ">$nsswrap_group");
678         print GRP "
679 root:x:0:
680 wheel:x:10:
681 users:x:100:
682 nobody:x:65533:
683 nogroup:x:65534:nobody
684 ";
685         close(GRP);
686
687 #Ensure the config file is valid before we start
688         if (system("$self->{bindir}/testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
689                 system("$self->{bindir}/testparm -v --suppress-prompt $configuration >&2");
690                 die("Failed to create a valid smb.conf configuration!");
691         }
692
693         (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");
694
695         my @provision_options = ();
696         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$nsswrap_passwd\"");
697         push (@provision_options, "NSS_WRAPPER_GROUP=\"$nsswrap_group\"");
698         if (defined($ENV{PROVISION_EJS})) {
699                 push (@provision_options, "$self->{bindir}/smbscript");
700                 push (@provision_options, "$self->{setupdir}/provision.js");
701         } else {
702                 push (@provision_options, "$self->{bindir}/smbpython");
703                 push (@provision_options, "$self->{setupdir}/provision");
704         }
705         push (@provision_options, split(' ', $configuration));
706         push (@provision_options, "--host-name=$netbiosname");
707         push (@provision_options, "--host-ip=$ifaceipv4");
708         push (@provision_options, "--quiet");
709         push (@provision_options, "--domain=$domain");
710         push (@provision_options, "--realm=$realm");
711         push (@provision_options, "--adminpass=$password");
712         push (@provision_options, "--krbtgtpass=krbtgt$password");
713         push (@provision_options, "--machinepass=machine$password");
714         push (@provision_options, "--root=$unix_name");
715         push (@provision_options, "--simple-bind-dn=cn=Manager,$localbasedn");
716         push (@provision_options, "--password=$password");
717         push (@provision_options, "--server-role=\"$server_role\"");
718
719         my $ldap_uri= "$ldapdir/ldapi";
720         $ldap_uri =~ s|/|%2F|g;
721         $ldap_uri = "ldapi://$ldap_uri";
722
723         my $ret = {
724                 KRB5_CONFIG => $krb5_config,
725                 PIDDIR => $piddir,
726                 SERVER => $netbiosname,
727                 SERVER_IP => $ifaceipv4,
728                 NETBIOSNAME => $netbiosname,
729                 NETBIOSALIAS => $netbiosalias,
730                 LDAP_URI => $ldap_uri,
731                 DOMAIN => $domain,
732                 USERNAME => $username,
733                 REALM => $realm,
734                 PASSWORD => $password,
735                 LDAPDIR => $ldapdir,
736                 WINBINDD_SOCKET_DIR => $winbindd_socket_dir,
737                 WINBINDD_PRIV_PIPE_DIR => $winbindd_priv_pipe_dir,
738                 NCALRPCDIR => $ncalrpcdir,
739                 LOCKDIR => $lockdir,
740                 CONFIGURATION => $configuration,
741                 SOCKET_WRAPPER_DEFAULT_IFACE => $swiface,
742                 NSS_WRAPPER_PASSWD => $nsswrap_passwd,
743                 NSS_WRAPPER_GROUP => $nsswrap_group,
744         };
745
746         if (defined($self->{ldap})) {
747
748                 push (@provision_options, "--ldap-backend=$ldap_uri");
749                 system("$self->{bindir}/smbpython $self->{setupdir}/provision-backend $configuration --ldap-manager-pass=$password --root=$unix_name --realm=$realm --domain=$domain --host-name=$netbiosname --ldap-backend-type=$self->{ldap}>&2") == 0 or die("backend provision failed");
750
751                 if ($self->{ldap} eq "openldap") {
752                        ($ret->{SLAPD_CONF}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ldapdir, $configuration) or die("Unable to create openldap directories");
753                        push (@provision_options, "--ldap-backend-type=openldap");
754                 } elsif ($self->{ldap} eq "fedora-ds") {
755                        ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ldapdir, $configuration) or die("Unable to create fedora ds directories");
756                        push (@provision_options, "--ldap-backend-type=fedora-ds");
757                        push (@provision_options, "'--aci=aci:: KHRhcmdldGF0dHIgPSAiKiIpICh2ZXJzaW9uIDMuMDthY2wgImZ1bGwgYWNjZXNzIHRvIGFsbCBieSBhbGwiO2FsbG93IChhbGwpKHVzZXJkbiA9ICJsZGFwOi8vL2FueW9uZSIpOykK'");
758                  }
759
760                 $self->slapd_start($ret) or 
761                         die("couldn't start slapd");
762         }
763
764         my $provision_cmd = join(" ", @provision_options);
765         (system($provision_cmd) == 0) or die("Unable to provision: \n$provision_cmd\n");
766
767         if (defined($self->{ldap})) {
768                 $self->slapd_stop($ret) or 
769                         die("couldn't stop slapd");
770         }
771
772         return $ret; 
773 }
774
775 sub provision_member($$$)
776 {
777         my ($self, $prefix, $dcvars) = @_;
778         print "PROVISIONING MEMBER...";
779
780         my $ret = $self->provision($prefix,
781                                    "member server",
782                                    "localmember3",
783                                    "localmember",
784                                    3,
785                                    "localmemberpass");
786
787         $ret or die("Unable to provision");
788
789         my $cmd = "";
790         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
791         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
792         $cmd .= "$self->{bindir}/net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
793         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
794
795         system($cmd) == 0 or die("Join failed\n$cmd");
796
797         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
798         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
799         $ret->{SMBD_TEST_LOG_POS} = 0;
800
801         $ret->{DC_SERVER} = $dcvars->{SERVER};
802         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
803         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
804         $ret->{DC_NETBIOSALIAS} = $dcvars->{NETBIOSALIAS};
805         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
806         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
807
808         return $ret;
809 }
810
811 sub provision_dc($$)
812 {
813         my ($self, $prefix) = @_;
814
815         print "PROVISIONING DC...";
816         my $ret = $self->provision($prefix,
817                                    "domain controller",
818                                    "localdc1",
819                                    "localdc",
820                                    1,
821                                    "localdcpass");
822
823         $self->add_wins_config("$prefix/private") or 
824                 die("Unable to add wins configuration");
825
826         $ret->{SMBD_TEST_FIFO} = "$prefix/smbd_test.fifo";
827         $ret->{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
828         $ret->{SMBD_TEST_LOG_POS} = 0;
829         return $ret;
830 }
831
832 sub teardown_env($$)
833 {
834         my ($self, $envvars) = @_;
835         my $pid;
836
837         close(DATA);
838
839         if (-f "$envvars->{PIDDIR}/smbd.pid" ) {
840                 open(IN, "<$envvars->{PIDDIR}/smbd.pid") or die("unable to open smbd pid file");
841                 $pid = <IN>;
842                 close(IN);
843
844                 # Give the process 20 seconds to exit.  gcov needs
845                 # this time to write out the covarge data
846                 my $count = 0;
847                 until (kill(0, $pid) == 0) {
848                     # if no process sucessfully signalled, then we are done
849                     sleep(1);
850                     $count++;
851                     last if $count > 20;
852                 }
853                 
854                 # If it is still around, kill it
855                 if ($count > 20) {
856                     print "smbd process $pid took more than $count seconds to exit, killing\n";
857                     kill 9, $pid;
858                 }
859         }
860
861         my $failed = $? >> 8;
862
863         $self->slapd_stop($envvars) if ($self->{ldap});
864
865         print $self->getlog_env($envvars);
866
867         return $failed;
868 }
869
870 sub getlog_env($$)
871 {
872         my ($self, $envvars) = @_;
873         my $title = "SMBD LOG of: $envvars->{NETBIOSNAME}\n";
874         my $out = $title;
875
876         open(LOG, "<$envvars->{SMBD_TEST_LOG}");
877
878         seek(LOG, $envvars->{SMBD_TEST_LOG_POS}, SEEK_SET);
879         while (<LOG>) {
880                 $out .= $_;
881         }
882         $envvars->{SMBD_TEST_LOG_POS} = tell(LOG);
883         close(LOG);
884
885         return "" if $out eq $title;
886  
887         return $out;
888 }
889
890 sub check_env($$)
891 {
892         my ($self, $envvars) = @_;
893
894         return 1 if (-p $envvars->{SMBD_TEST_FIFO});
895
896         print $self->getlog_env($envvars);
897
898         return 0;
899 }
900
901 sub setup_env($$$)
902 {
903         my ($self, $envname, $path) = @_;
904
905         if ($envname eq "dc") {
906                 return $self->setup_dc("$path/dc");
907         } elsif ($envname eq "member") {
908                 if (not defined($self->{vars}->{dc})) {
909                         $self->setup_dc("$path/dc");
910                 }
911                 return $self->setup_member("$path/member", $self->{vars}->{dc});
912         } else {
913                 die("Samba4 can't provide environment '$envname'");
914         }
915 }
916
917 sub setup_member($$$$)
918 {
919         my ($self, $path, $dc_vars) = @_;
920
921         my $env = $self->provision_member($path, $dc_vars);
922
923         $self->check_or_start($env, ($ENV{SMBD_MAXTIME} or 7500));
924
925         $self->wait_for_start($env);
926
927         return $env;
928 }
929
930 sub setup_dc($$)
931 {
932         my ($self, $path) = @_;
933
934         my $env = $self->provision_dc($path);
935
936         $self->check_or_start($env, 
937                 ($ENV{SMBD_MAXTIME} or 7500));
938
939         $self->wait_for_start($env);
940
941         $self->{vars}->{dc} = $env;
942
943         return $env;
944 }
945
946 sub stop($)
947 {
948         my ($self) = @_;
949 }
950
951 1;