selftest: Allow a krb5.conf to be generated that covers multiple realms
[amitay/samba.git] / selftest / target / Samba.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 Samba;
7
8 use strict;
9 use target::Samba3;
10 use target::Samba4;
11
12 sub new($$$$$) {
13         my ($classname, $bindir, $binary_mapping,$ldap, $srcdir, $exeext, $server_maxtime) = @_;
14
15         my $self = {
16             samba3 => new Samba3($bindir,$binary_mapping, $srcdir, $exeext, $server_maxtime),
17             samba4 => new Samba4($bindir,$binary_mapping, $ldap, $srcdir, $exeext, $server_maxtime),
18         };
19         bless $self;
20         return $self;
21 }
22
23 sub setup_env($$$)
24 {
25         my ($self, $envname, $path) = @_;
26
27         $ENV{ENVNAME} = $envname;
28
29         my $env = $self->{samba4}->setup_env($envname, $path);
30         if (defined($env)) {
31             if (not defined($env->{target})) {
32                 $env->{target} = $self->{samba4};
33             }
34         } else {
35                 $env = $self->{samba3}->setup_env($envname, $path);
36                 if (defined($env)) {
37                     if (not defined($env->{target})) {
38                         $env->{target} = $self->{samba3};
39                     }
40                 }
41         }
42         if (not defined $env) {
43                 warn("Samba can't provide environment '$envname'");
44                 return undef;
45         }
46         return $env;
47 }
48
49 sub bindir_path($$) {
50         my ($object, $path) = @_;
51
52         if (defined($object->{binary_mapping}->{$path})) {
53             $path = $object->{binary_mapping}->{$path};
54         }
55
56         my $valpath = "$object->{bindir}/$path$object->{exeext}";
57
58         return $valpath if (-f $valpath);
59         return $path;
60 }
61
62 sub mk_krb5_conf($$)
63 {
64         my ($ctx, $other_realms_stanza) = @_;
65
66         unless (open(KRB5CONF, ">$ctx->{krb5_conf}")) {
67                 warn("can't open $ctx->{krb5_conf}$?");
68                 return undef;
69         }
70
71         my $our_realms_stanza = mk_realms_stanza($ctx->{realm},
72                                                  $ctx->{dnsname},
73                                                  $ctx->{domain},
74                                                  $ctx->{kdc_ipv4});
75         print KRB5CONF "
76 #Generated krb5.conf for $ctx->{realm}
77
78 [libdefaults]
79  default_realm = $ctx->{realm}
80  dns_lookup_realm = false
81  dns_lookup_kdc = false
82  ticket_lifetime = 24h
83  forwardable = yes
84  allow_weak_crypto = yes
85
86 [realms]
87  $our_realms_stanza
88  $other_realms_stanza
89 ";
90
91
92         if (defined($ctx->{tlsdir})) {
93                print KRB5CONF "
94
95 [appdefaults]
96         pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
97
98 [kdc]
99         enable-pkinit = true
100         pkinit_identity = FILE:$ctx->{tlsdir}/kdc.pem,$ctx->{tlsdir}/key.pem
101         pkinit_anchors = FILE:$ctx->{tlsdir}/ca.pem
102
103 ";
104         }
105         close(KRB5CONF);
106 }
107
108 sub mk_realms_stanza($$$$)
109 {
110         my ($realm, $dnsname, $domain, $kdc_ipv4) = @_;
111
112         my $realms_stanza = "
113  $realm = {
114   kdc = $kdc_ipv4:88
115   admin_server = $kdc_ipv4:88
116   default_domain = $dnsname
117  }
118  $dnsname = {
119   kdc = $kdc_ipv4:88
120   admin_server = $kdc_ipv4:88
121   default_domain = $dnsname
122  }
123  $domain = {
124   kdc = $kdc_ipv4:88
125   admin_server = $kdc_ipv4:88
126   default_domain = $dnsname
127  }
128
129 ";
130         return $realms_stanza;
131 }
132
133 1;