replace: Fix checking for config.h #define in readline.h
[Samba.git] / selftest / target / Samba.pm
blob4820f987fd73c096ef24cd1251134fac4608a0ce
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.
6 package Samba;
8 use strict;
9 use target::Samba3;
10 use target::Samba4;
11 use POSIX;
12 use Cwd qw(abs_path);
14 sub new($$$$$) {
15 my ($classname, $bindir, $ldap, $srcdir, $server_maxtime) = @_;
17 my $self = {
18 samba3 => new Samba3($bindir, $srcdir, $server_maxtime),
19 samba4 => new Samba4($bindir, $ldap, $srcdir, $server_maxtime),
21 bless $self;
22 return $self;
25 %Samba::ENV_DEPS = (%Samba3::ENV_DEPS, %Samba4::ENV_DEPS);
26 our %ENV_DEPS;
28 %Samba::ENV_TARGETS = (
29 (map { $_ => "Samba3" } keys %Samba3::ENV_DEPS),
30 (map { $_ => "Samba4" } keys %Samba4::ENV_DEPS),
32 our %ENV_TARGETS;
34 %Samba::ENV_NEEDS_AD_DC = (
35 (map { $_ => 1 } keys %Samba4::ENV_DEPS)
37 our %ENV_NEEDS_AD_DC;
38 foreach my $env (keys %Samba3::ENV_DEPS) {
39 $ENV_NEEDS_AD_DC{$env} = ($env =~ /^ad_/);
42 sub setup_env($$$)
44 my ($self, $envname, $path) = @_;
46 my $targetname = $ENV_TARGETS{$envname};
47 if (not defined($targetname)) {
48 warn("Samba can't provide environment '$envname'");
49 return "UNKNOWN";
52 my %targetlookup = (
53 "Samba3" => $self->{samba3},
54 "Samba4" => $self->{samba4}
56 my $target = $targetlookup{$targetname};
58 if (defined($target->{vars}->{$envname})) {
59 return $target->{vars}->{$envname};
62 my @dep_vars;
63 foreach(@{$ENV_DEPS{$envname}}) {
64 my $vars = $self->setup_env($_, $path);
65 if (defined($vars)) {
66 push(@dep_vars, $vars);
67 } else {
68 warn("Failed setting up $_ as a dependency of $envname");
69 return undef;
73 $ENV{ENVNAME} = $envname;
74 # Avoid hitting system krb5.conf -
75 # An env that needs Kerberos will reset this to the real value.
76 $ENV{KRB5_CONFIG} = "$path/no_krb5.conf";
78 my $setup_name = $ENV_TARGETS{$envname}."::setup_".$envname;
79 my $setup_sub = \&$setup_name;
80 my $env = &$setup_sub($target, "$path/$envname", @dep_vars);
82 if (not defined($env)) {
83 warn("failed to start up environment '$envname'");
84 return undef;
87 $target->{vars}->{$envname} = $env;
88 $target->{vars}->{$envname}->{target} = $target;
90 return $env;
93 sub bindir_path($$) {
94 my ($object, $path) = @_;
96 my $valpath = "$object->{bindir}/$path";
97 my $python_cmd = "";
98 my $result = $path;
99 if (defined $ENV{'PYTHON'}) {
100 $python_cmd = $ENV{'PYTHON'} . " ";
103 if (-f $valpath or -d $valpath) {
104 $result = $valpath;
106 # make sure we prepend samba-tool with calling $PYTHON python version
107 if ($path eq "samba-tool") {
108 $result = $python_cmd . $result;
110 return $result;
113 sub nss_wrapper_winbind_so_path($) {
114 my ($object) = @_;
115 my $ret = $ENV{NSS_WRAPPER_WINBIND_SO_PATH};
116 if (not defined($ret)) {
117 $ret = bindir_path($object, "shared/libnss_wrapper_winbind.so.2");
118 $ret = abs_path($ret);
120 return $ret;
123 sub copy_file_content($$)
125 my ($in, $out) = @_;
126 open(IN, "${in}") or die("failed to open in[${in}] for reading: $!");
127 open(OUT, ">${out}") or die("failed to open out[${out}] for writing: $!");
128 while(<IN>) {
129 print OUT $_;
131 close(OUT);
132 close(IN);
135 sub prepare_keyblobs($)
137 my ($ctx) = @_;
139 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
140 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
141 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
142 my $dcdnsname = "$ctx->{hostname}.$ctx->{dnsname}";
143 my $dcdir = "$cadir/DCs/$dcdnsname";
144 my $dccert = "$dcdir/DC-$dcdnsname-cert.pem";
145 my $dckey_private = "$dcdir/DC-$dcdnsname-private-key.pem";
146 my $adminprincipalname = "administrator\@$ctx->{dnsname}";
147 my $admindir = "$cadir/Users/$adminprincipalname";
148 my $admincert = "$admindir/USER-$adminprincipalname-cert.pem";
149 my $adminkey_private = "$admindir/USER-$adminprincipalname-private-key.pem";
150 my $pkinitprincipalname = "pkinit\@$ctx->{dnsname}";
151 my $pkinitdir = "$cadir/Users/$pkinitprincipalname";
152 my $pkinitcert = "$pkinitdir/USER-$pkinitprincipalname-cert.pem";
153 my $pkinitkey_private = "$pkinitdir/USER-$pkinitprincipalname-private-key.pem";
155 my $tlsdir = "$ctx->{tlsdir}";
156 my $pkinitdir = "$ctx->{prefix_abs}/pkinit";
157 #TLS and PKINIT crypto blobs
158 my $dhfile = "$tlsdir/dhparms.pem";
159 my $cafile = "$tlsdir/ca.pem";
160 my $crlfile = "$tlsdir/crl.pem";
161 my $certfile = "$tlsdir/cert.pem";
162 my $keyfile = "$tlsdir/key.pem";
163 my $admincertfile = "$pkinitdir/USER-$adminprincipalname-cert.pem";
164 my $adminkeyfile = "$pkinitdir/USER-$adminprincipalname-private-key.pem";
165 my $pkinitcertfile = "$pkinitdir/USER-$pkinitprincipalname-cert.pem";
166 my $pkinitkeyfile = "$pkinitdir/USER-$pkinitprincipalname-private-key.pem";
168 mkdir($tlsdir, 0700);
169 mkdir($pkinitdir, 0700);
170 my $oldumask = umask;
171 umask 0077;
173 # This is specified here to avoid draining entropy on every run
174 # generate by
175 # openssl dhparam -out dhparms.pem -text -2 8192
176 open(DHFILE, ">$dhfile");
177 print DHFILE <<EOF;
178 -----BEGIN DH PARAMETERS-----
179 MIIECAKCBAEAlcpjuJptCzC2bIIApLuyFLw2nODQUztqs/peysY9e3LgWh/xrc87
180 SWJNSUrqFJFh2m357WH0XGcTdTk0b/8aIYIWjbwEhWR/5hZ+1x2TDrX1awkYayAe
181 pr0arycmWHaAmhw+m+dBdj2O2jRMe7gn0ha85JALNl+Z3wv2q2eys8TIiQ2dbHPx
182 XvpMmlA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031jr0EisX8QtFR7+/0Nfoni6ydFGs5fNH/L1ckq6FEO4OhgucJw9H
199 YRmiFlsQBQNny78vNchwZne3ZixkShtGW0hWDdi2n+h7St1peNJCNJjMbEhRsPRx
200 RmNGWh4AL8rho4RO9OBao0MnUdjbbffD+wIBAg==
201 -----END DH PARAMETERS-----
203 close(DHFILE);
205 if (! -e ${dckey_private}) {
206 umask $oldumask;
207 return;
210 copy_file_content(${cacert}, ${cafile});
211 copy_file_content(${cacrl_pem}, ${crlfile});
212 copy_file_content(${dccert}, ${certfile});
213 copy_file_content(${dckey_private}, ${keyfile});
214 if (-e ${adminkey_private}) {
215 copy_file_content(${admincert}, ${admincertfile});
216 copy_file_content(${adminkey_private}, ${adminkeyfile});
218 if (-e ${pkinitkey_private}) {
219 copy_file_content(${pkinitcert}, ${pkinitcertfile});
220 copy_file_content(${pkinitkey_private}, ${pkinitkeyfile});
223 # COMPAT stuff to be removed in a later commit
224 my $kdccertfile = "$tlsdir/kdc.pem";
225 copy_file_content(${dccert}, ${kdccertfile});
227 umask $oldumask;
230 sub mk_krb5_conf($$)
232 my ($ctx) = @_;
234 unless (open(KRB5CONF, ">$ctx->{krb5_conf}")) {
235 warn("can't open $ctx->{krb5_conf}$?");
236 return undef;
239 my $our_realms_stanza = mk_realms_stanza($ctx->{realm},
240 $ctx->{dnsname},
241 $ctx->{domain},
242 $ctx->{kdc_ipv4});
243 print KRB5CONF "
244 #Generated krb5.conf for $ctx->{realm}
246 [libdefaults]
247 default_realm = $ctx->{realm}
248 dns_lookup_realm = false
249 dns_lookup_kdc = true
250 ticket_lifetime = 24h
251 forwardable = yes
252 allow_weak_crypto = yes
253 # Set the grace clocskew to 5 seconds
254 # This is especially required by samba3.raw.session krb5 and
255 # reauth tests
256 clockskew = 5
257 # We are running on the same machine, do not correct
258 # system clock differences
259 kdc_timesync = 0
263 if (defined($ctx->{krb5_ccname})) {
264 print KRB5CONF "
265 default_ccache_name = $ctx->{krb5_ccname}
270 if (defined($ctx->{supported_enctypes})) {
271 print KRB5CONF "
272 default_etypes = $ctx->{supported_enctypes}
273 default_as_etypes = $ctx->{supported_enctypes}
274 default_tgs_enctypes = $ctx->{supported_enctypes}
275 default_tkt_enctypes = $ctx->{supported_enctypes}
276 permitted_enctypes = $ctx->{supported_enctypes}
280 print KRB5CONF "
281 [realms]
282 $our_realms_stanza
286 if (defined($ctx->{tlsdir})) {
287 print KRB5CONF "
289 [appdefaults]
290 pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
292 [kdc]
293 enable-pkinit = true
294 pkinit_identity = FILE:$ctx->{tlsdir}/kdc.pem,$ctx->{tlsdir}/key.pem
295 pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
299 close(KRB5CONF);
302 sub mk_realms_stanza($$$$)
304 my ($realm, $dnsname, $domain, $kdc_ipv4) = @_;
305 my $lc_domain = lc($domain);
307 my $realms_stanza = "
308 $realm = {
309 kdc = $kdc_ipv4:88
310 admin_server = $kdc_ipv4:88
311 default_domain = $dnsname
313 $dnsname = {
314 kdc = $kdc_ipv4:88
315 admin_server = $kdc_ipv4:88
316 default_domain = $dnsname
318 $domain = {
319 kdc = $kdc_ipv4:88
320 admin_server = $kdc_ipv4:88
321 default_domain = $dnsname
323 $lc_domain = {
324 kdc = $kdc_ipv4:88
325 admin_server = $kdc_ipv4:88
326 default_domain = $dnsname
330 return $realms_stanza;
333 sub mk_mitkdc_conf($$)
335 # samba_kdb_dir is the path to mit_samba.so
336 my ($ctx, $samba_kdb_dir) = @_;
338 unless (open(KDCCONF, ">$ctx->{mitkdc_conf}")) {
339 warn("can't open $ctx->{mitkdc_conf}$?");
340 return undef;
343 print KDCCONF "
344 # Generated kdc.conf for $ctx->{realm}
346 [kdcdefaults]
347 kdc_ports = 88
348 kdc_tcp_ports = 88
350 [realms]
351 $ctx->{realm} = {
354 $ctx->{dnsname} = {
357 $ctx->{domain} = {
360 [dbmodules]
361 db_module_dir = $samba_kdb_dir
363 $ctx->{realm} = {
364 db_library = samba
367 $ctx->{dnsname} = {
368 db_library = samba
371 $ctx->{domain} = {
372 db_library = samba
375 [logging]
376 kdc = FILE:$ctx->{logdir}/mit_kdc.log
379 close(KDCCONF);
382 sub get_interface($)
384 my ($netbiosname) = @_;
385 $netbiosname = lc($netbiosname);
387 my %interfaces = ();
388 $interfaces{"localnt4dc2"} = 3;
389 $interfaces{"localnt4member3"} = 4;
390 $interfaces{"localshare4"} = 5;
392 $interfaces{"localktest6"} = 7;
393 $interfaces{"maptoguest"} = 8;
394 $interfaces{"localnt4dc9"} = 9;
396 # 11-16 used by selftest.pl for client interfaces
398 $interfaces{"addc_no_nss"} = 17;
399 $interfaces{"addc_no_ntlm"} = 18;
400 $interfaces{"idmapadmember"} = 19;
401 $interfaces{"idmapridmember"} = 20;
402 $interfaces{"localdc"} = 21;
403 $interfaces{"localvampiredc"} = 22;
404 $interfaces{"s4member"} = 23;
405 $interfaces{"localrpcproxy"} = 24;
406 $interfaces{"dc5"} = 25;
407 $interfaces{"dc6"} = 26;
408 $interfaces{"dc7"} = 27;
409 $interfaces{"rodc"} = 28;
410 $interfaces{"localadmember"} = 29;
411 $interfaces{"addc"} = 30;
412 $interfaces{"localsubdc"} = 31;
413 $interfaces{"chgdcpass"} = 32;
414 $interfaces{"promotedvdc"} = 33;
415 $interfaces{"rfc2307member"} = 34;
416 $interfaces{"fileserver"} = 35;
417 $interfaces{"fakednsforwarder1"} = 36;
418 $interfaces{"fakednsforwarder2"} = 37;
419 $interfaces{"s4member_dflt"} = 38;
420 $interfaces{"vampire2000dc"} = 39;
421 $interfaces{"backupfromdc"} = 40;
422 $interfaces{"restoredc"} = 41;
423 $interfaces{"renamedc"} = 42;
424 $interfaces{"labdc"} = 43;
425 $interfaces{"offlinebackupdc"} = 44;
426 $interfaces{"customdc"} = 45;
427 $interfaces{"prockilldc"} = 46;
428 $interfaces{"proclimitdc"} = 47;
430 # update lib/socket_wrapper/socket_wrapper.c
431 # #define MAX_WRAPPED_INTERFACES 64
432 # if you wish to have more than 64 interfaces
434 if (not defined($interfaces{$netbiosname})) {
435 die();
438 return $interfaces{$netbiosname};
441 sub cleanup_child($$)
443 my ($pid, $name) = @_;
445 if (!defined($pid)) {
446 print STDERR "cleanup_child: pid not defined ... not calling waitpid\n";
447 return -1;
450 my $childpid = waitpid($pid, WNOHANG);
452 if ($childpid == 0) {
453 } elsif ($childpid < 0) {
454 printf STDERR "%s child process %d isn't here any more\n", $name, $pid;
455 return $childpid;
456 } elsif ($? & 127) {
457 printf STDERR "%s child process %d, died with signal %d, %s coredump\n",
458 $name, $childpid, ($? & 127), ($? & 128) ? 'with' : 'without';
459 } else {
460 printf STDERR "%s child process %d exited with value %d\n", $name, $childpid, $? >> 8;
462 return $childpid;
465 sub random_domain_sid()
467 my $domain_sid = "S-1-5-21-". int(rand(4294967295)) . "-" . int(rand(4294967295)) . "-" . int(rand(4294967295));
468 return $domain_sid;