37f7102c39e965798c843a42e08967582443d741
[nivanova/samba-autobuild/.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 use SocketWrapper;
13 use target::Samba;
14 use target::Samba3;
15
16 sub new($$$$$) {
17         my ($classname, $bindir, $binary_mapping, $ldap, $srcdir, $server_maxtime) = @_;
18
19         my $self = {
20                 vars => {},
21                 ldap => $ldap,
22                 bindir => $bindir,
23                 binary_mapping => $binary_mapping,
24                 srcdir => $srcdir,
25                 server_maxtime => $server_maxtime,
26                 target3 => new Samba3($bindir, $binary_mapping, $srcdir, $server_maxtime)
27         };
28         bless $self;
29         return $self;
30 }
31
32 sub scriptdir_path($$) {
33         my ($self, $path) = @_;
34         return "$self->{srcdir}/source4/scripting/$path";
35 }
36
37 sub openldap_start($$$) {
38 }
39
40 sub slapd_start($$)
41 {
42         my $count = 0;
43         my ($self, $env_vars) = @_;
44         my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
45
46         my $uri = $env_vars->{LDAP_URI};
47
48         if (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") == 0) {
49             print "A SLAPD is still listening to $uri before we started the LDAP backend.  Aborting!";
50             return 1;
51         }
52         # running slapd in the background means it stays in the same process group, so it can be
53         # killed by timelimit
54         if ($self->{ldap} eq "fedora-ds") {
55                 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 &");
56         } elsif ($self->{ldap} eq "openldap") {
57                 system("$ENV{OPENLDAP_SLAPD} -d0 -F $env_vars->{SLAPD_CONF_D} -h $uri > $env_vars->{LDAPDIR}/logs 2>&1 &");
58         }
59         while (system("$ldbsearch -H $uri -s base -b \"\" supportedLDAPVersion > /dev/null") != 0) {
60                 $count++;
61                 if ($count > 40) {
62                         $self->slapd_stop($env_vars);
63                         return 0;
64                 }
65                 sleep(1);
66         }
67         return 1;
68 }
69
70 sub slapd_stop($$)
71 {
72         my ($self, $envvars) = @_;
73         if ($self->{ldap} eq "fedora-ds") {
74                 system("$envvars->{LDAPDIR}/slapd-$envvars->{LDAP_INSTANCE}/stop-slapd");
75         } elsif ($self->{ldap} eq "openldap") {
76                 unless (open(IN, "<$envvars->{OPENLDAP_PIDFILE}")) {
77                         warn("unable to open slapd pid file: $envvars->{OPENLDAP_PIDFILE}");
78                         return 0;
79                 }
80                 kill 9, <IN>;
81                 close(IN);
82         }
83         return 1;
84 }
85
86 sub check_or_start($$$)
87 {
88         my ($self, $env_vars, $process_model) = @_;
89
90         return 0 if $self->check_env($env_vars);
91
92         # use a pipe for stdin in the child processes. This allows
93         # those processes to monitor the pipe for EOF to ensure they
94         # exit when the test script exits
95         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
96
97         print "STARTING SAMBA...";
98         my $pid = fork();
99         if ($pid == 0) {
100                 # we want out from samba to go to the log file, but also
101                 # to the users terminal when running 'make test' on the command
102                 # line. This puts it on stderr on the terminal
103                 open STDOUT, "| tee $env_vars->{SAMBA_TEST_LOG} 1>&2";
104                 open STDERR, '>&STDOUT';
105
106                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
107
108                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
109                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
110                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
111
112                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
113                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
114                 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
115
116                 $ENV{UID_WRAPPER} = "1";
117
118                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "samba");
119                 my @preargs = ();
120                 my @optargs = ();
121                 if (defined($ENV{SAMBA_OPTIONS})) {
122                         @optargs = split(/ /, $ENV{SAMBA_OPTIONS});
123                 }
124                 if(defined($ENV{SAMBA_VALGRIND})) {
125                         @preargs = split(/ /,$ENV{SAMBA_VALGRIND});
126                 }
127
128                 close($env_vars->{STDIN_PIPE});
129                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
130
131                 exec(@preargs, Samba::bindir_path($self, "samba"), "-M", $process_model, "-i", "--maximum-runtime=$self->{server_maxtime}", $env_vars->{CONFIGURATION}, @optargs) or die("Unable to start samba: $!");
132         }
133         $env_vars->{SAMBA_PID} = $pid;
134         print "DONE\n";
135
136         close(STDIN_READER);
137
138         return $pid;
139 }
140
141 sub wait_for_start($$)
142 {
143         my ($self, $testenv_vars) = @_;
144         my $ret;
145         # give time for nbt server to register its names
146         print "delaying for nbt name registration\n";
147         sleep 2;
148
149         # This will return quickly when things are up, but be slow if we
150         # need to wait for (eg) SSL init
151         my $nmblookup =  Samba::bindir_path($self, "nmblookup4");
152         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
153         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
154         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
155         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
156         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
157         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
158         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{SERVER}");
159         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{SERVER}");
160         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
161         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
162         system("$nmblookup $testenv_vars->{CONFIGURATION} $testenv_vars->{NETBIOSNAME}");
163         system("$nmblookup $testenv_vars->{CONFIGURATION} -U $testenv_vars->{SERVER_IP} $testenv_vars->{NETBIOSNAME}");
164
165         # Ensure we have the first RID Set before we start tests.  This makes the tests more reliable.
166         if ($testenv_vars->{SERVER_ROLE} eq "domain controller" and not ($testenv_vars->{NETBIOS_NAME} eq "rodc")) {
167             print "waiting for working LDAP and a RID Set to be allocated\n";
168             my $ldbsearch = Samba::bindir_path($self, "ldbsearch");
169             my $count = 0;
170             my $base_dn = "DC=".join(",DC=", split(/\./, $testenv_vars->{REALM}));
171             my $rid_set_dn = "cn=RID Set,cn=$testenv_vars->{NETBIOSNAME},ou=domain controllers,$base_dn";
172             while (system("$ldbsearch -H ldap://$testenv_vars->{SERVER} -U$testenv_vars->{USERNAME}%$testenv_vars->{PASSWORD} -s base -b \"$rid_set_dn\" rIDAllocationPool > /dev/null") != 0) {
173                 $count++;
174                 if ($count > 40) {
175                     $ret = 1;
176                     last;
177                 }
178                 sleep(1);
179             }
180         }
181         print $self->getlog_env($testenv_vars);
182
183         return $ret
184 }
185
186 sub write_ldb_file($$$)
187 {
188         my ($self, $file, $ldif) = @_;
189
190         my $ldbadd =  Samba::bindir_path($self, "ldbadd");
191         open(LDIF, "|$ldbadd -H $file >/dev/null");
192         print LDIF $ldif;
193         return(close(LDIF));
194 }
195
196 sub add_wins_config($$)
197 {
198         my ($self, $privatedir) = @_;
199
200         return $self->write_ldb_file("$privatedir/wins_config.ldb", "
201 dn: name=TORTURE_11,CN=PARTNERS
202 objectClass: wreplPartner
203 name: TORTURE_11
204 address: 127.0.0.11
205 pullInterval: 0
206 pushChangeCount: 0
207 type: 0x3
208 ");
209 }
210
211 sub mk_fedora_ds($$)
212 {
213         my ($self, $ctx) = @_;
214
215         #Make the subdirectory be as fedora DS would expect
216         my $fedora_ds_dir = "$ctx->{ldapdir}/slapd-$ctx->{ldap_instance}";
217
218         my $pidfile = "$fedora_ds_dir/logs/slapd-$ctx->{ldap_instance}.pid";
219
220         return ($fedora_ds_dir, $pidfile);
221 }
222
223 sub mk_openldap($$)
224 {
225         my ($self, $ctx) = @_;
226
227         my $slapd_conf_d = "$ctx->{ldapdir}/slapd.d";
228         my $pidfile = "$ctx->{ldapdir}/slapd.pid";
229
230         return ($slapd_conf_d, $pidfile);
231 }
232
233 sub mk_keyblobs($$)
234 {
235         my ($self, $tlsdir) = @_;
236
237         #TLS and PKINIT crypto blobs
238         my $dhfile = "$tlsdir/dhparms.pem";
239         my $cafile = "$tlsdir/ca.pem";
240         my $certfile = "$tlsdir/cert.pem";
241         my $reqkdc = "$tlsdir/req-kdc.der";
242         my $kdccertfile = "$tlsdir/kdc.pem";
243         my $keyfile = "$tlsdir/key.pem";
244         my $adminkeyfile = "$tlsdir/adminkey.pem";
245         my $reqadmin = "$tlsdir/req-admin.der";
246         my $admincertfile = "$tlsdir/admincert.pem";
247         my $admincertupnfile = "$tlsdir/admincertupn.pem";
248
249         mkdir($tlsdir, 0777);
250
251         #This is specified here to avoid draining entropy on every run
252         open(DHFILE, ">$dhfile");
253         print DHFILE <<EOF;
254 -----BEGIN DH PARAMETERS-----
255 MGYCYQC/eWD2xkb7uELmqLi+ygPMKyVcpHUo2yCluwnbPutEueuxrG/Cys8j8wLO
256 svCN/jYNyR2NszOmg7ZWcOC/4z/4pWDVPUZr8qrkhj5MRKJc52MncfaDglvEdJrv
257 YX70obsCAQI=
258 -----END DH PARAMETERS-----
259 EOF
260         close(DHFILE);
261
262         #Likewise, we pregenerate the key material.  This allows the
263         #other certificates to be pre-generated
264         open(KEYFILE, ">$keyfile");
265         print KEYFILE <<EOF;
266 -----BEGIN RSA PRIVATE KEY-----
267 MIICXQIBAAKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpc
268 ol3+S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H
269 6H+pPqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQAB
270 AoGAAqDLzFRR/BF1kpsiUfL4WFvTarCe9duhwj7ORc6fs785qAXuwUYAJ0Uvzmy6
271 HqoGv3t3RfmeHDmjcpPHsbOKnsOQn2MgmthidQlPBMWtQMff5zdoYNUFiPS0XQBq
272 szNW4PRjaA9KkLQVTwnzdXGkBSkn/nGxkaVu7OR3vJOBoo0CQQDO4upypesnbe6p
273 9/xqfZ2uim8IwV1fLlFClV7WlCaER8tsQF4lEi0XSzRdXGUD/dilpY88Nb+xok/X
274 8Z8OvgAXAkEA+pcLsx1gN7kxnARxv54jdzQjC31uesJgMKQXjJ0h75aUZwTNHmZQ
275 vPxi6u62YiObrN5oivkixwFNncT9MxTxVQJBAMaWUm2SjlLe10UX4Zdm1MEB6OsC
276 kVoX37CGKO7YbtBzCfTzJGt5Mwc1DSLA2cYnGJqIfSFShptALlwedot0HikCQAJu
277 jNKEKnbf+TdGY8Q0SKvTebOW2Aeg80YFkaTvsXCdyXrmdQcifw4WdO9KucJiDhSz
278 Y9hVapz7ykEJtFtWjLECQQDIlfc63I5ZpXfg4/nN4IJXUW6AmPVOYIA5215itgki
279 cSlMYli1H9MEXH0pQMGv5Qyd0OYIx2DDg96mZ+aFvqSG
280 -----END RSA PRIVATE KEY-----
281 EOF
282         close(KEYFILE);
283
284         open(ADMINKEYFILE, ">$adminkeyfile");
285
286         print ADMINKEYFILE <<EOF;
287 -----BEGIN RSA PRIVATE KEY-----
288 MIICXQIBAAKBgQD0+OL7TQBj0RejbIH1+g5GeRaWaM9xF43uE5y7jUHEsi5owhZF
289 5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMFxB6esnXhl0Jpip1JkUMM
290 XLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xdl3JRlwIDAQAB
291 AoGAP8mjCP628Ebc2eACQzOWjgEvwYCPK4qPmYOf1zJkArzG2t5XAGJ5WGrENRuB
292 cm3XFh1lpmaADl982UdW3gul4gXUy6w4XjKK4vVfhyHj0kZ/LgaXUK9BAGhroJ2L
293 osIOUsaC6jdx9EwSRctwdlF3wWJ8NK0g28AkvIk+FlolW4ECQQD7w5ouCDnf58CN
294 u4nARx4xv5XJXekBvOomkCQAmuOsdOb6b9wn3mm2E3au9fueITjb3soMR31AF6O4
295 eAY126rXAkEA+RgHzybzZEP8jCuznMqoN2fq/Vrs6+W3M8/G9mzGEMgLLpaf2Jiz
296 I9tLZ0+OFk9tkRaoCHPfUOCrVWJZ7Y53QQJBAMhoA6rw0WDyUcyApD5yXg6rusf4
297 ASpo/tqDkqUIpoL464Qe1tjFqtBM3gSXuhs9xsz+o0bzATirmJ+WqxrkKTECQHt2
298 OLCpKqwAspU7N+w32kaUADoRLisCEdrhWklbwpQgwsIVsCaoEOpt0CLloJRYTANE
299 yoZeAErTALjyZYZEPcECQQDlUi0N8DFxQ/lOwWyR3Hailft+mPqoPCa8QHlQZnlG
300 +cfgNl57YHMTZFwgUVFRdJNpjH/WdZ5QxDcIVli0q+Ko
301 -----END RSA PRIVATE KEY-----
302 EOF
303
304         #generated with
305         # hxtool issue-certificate --self-signed --issue-ca \
306         # --ca-private-key="FILE:$KEYFILE" \
307         # --subject="CN=CA,DC=samba,DC=example,DC=com" \
308         # --certificate="FILE:$CAFILE" --lifetime="25 years"
309
310         open(CAFILE, ">$cafile");
311         print CAFILE <<EOF;
312 -----BEGIN CERTIFICATE-----
313 MIICcTCCAdqgAwIBAgIUaBPmjnPVqyFqR5foICmLmikJTzgwCwYJKoZIhvcNAQEFMFIxEzAR
314 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
315 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTIyMzEyWhgPMjAzMzAyMjQx
316 MjIzMTJaMFIxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
317 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMIGfMA0GCSqGSIb3DQEBAQUA
318 A4GNADCBiQKBgQDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+S9/6
319 I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+pPqVIRLOmrWIm
320 ai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABo0IwQDAOBgNVHQ8BAf8EBAMC
321 AaYwHQYDVR0OBBYEFMLZufegDKLZs0VOyFXYK1L6M8oyMA8GA1UdEwEB/wQFMAMBAf8wDQYJ
322 KoZIhvcNAQEFBQADgYEAAZJbCAAkaqgFJ0xgNovn8Ydd0KswQPjicwiODPgw9ZPoD2HiOUVO
323 yYDRg/dhFF9y656OpcHk4N7qZ2sl3RlHkzDu+dseETW+CnKvQIoXNyeARRJSsSlwrwcoD4JR
324 HTLk2sGigsWwrJ2N99sG/cqSJLJ1MFwLrs6koweBnYU0f/g=
325 -----END CERTIFICATE-----
326 EOF
327
328         #generated with GNUTLS internally in Samba.
329
330         open(CERTFILE, ">$certfile");
331         print CERTFILE <<EOF;
332 -----BEGIN CERTIFICATE-----
333 MIICYTCCAcygAwIBAgIE5M7SRDALBgkqhkiG9w0BAQUwZTEdMBsGA1UEChMUU2Ft
334 YmEgQWRtaW5pc3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1
335 dG9nZW5lcmF0ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMB4XDTA2MDgw
336 NDA0MzY1MloXDTA4MDcwNDA0MzY1MlowZTEdMBsGA1UEChMUU2FtYmEgQWRtaW5p
337 c3RyYXRpb24xNDAyBgNVBAsTK1NhbWJhIC0gdGVtcG9yYXJ5IGF1dG9nZW5lcmF0
338 ZWQgY2VydGlmaWNhdGUxDjAMBgNVBAMTBVNhbWJhMIGcMAsGCSqGSIb3DQEBAQOB
339 jAAwgYgCgYDKg6pAwCHUMA1DfHDmWhZfd+F0C+9Jxcqvpw9ii9En3E1uflpcol3+
340 S9/6I/uaTmJHZre+DF3dTzb/UOZo0Zem8N+IzzkgoGkFafjXuT3BL5UPY2/H6H+p
341 PqVIRLOmrWImai359YyoKhFyo37Y6HPeU8QcZ+u2rS9geapIWfeuowIDAQABoyUw
342 IzAMBgNVHRMBAf8EAjAAMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAsGCSqGSIb3DQEB
343 BQOBgQAmkN6XxvDnoMkGcWLCTwzxGfNNSVcYr7TtL2aJh285Xw9zaxcm/SAZBFyG
344 LYOChvh6hPU7joMdDwGfbiLrBnMag+BtGlmPLWwp/Kt1wNmrRhduyTQFhN3PP6fz
345 nBr9vVny2FewB2gHmelaPS//tXdxivSXKz3NFqqXLDJjq7P8wA==
346 -----END CERTIFICATE-----
347 EOF
348         close(CERTFILE);
349
350         #KDC certificate
351         # hxtool request-create \
352         # --subject="CN=krbtgt,CN=users,DC=samba,DC=example,DC=com" \
353         # --key="FILE:$KEYFILE" $KDCREQ
354
355         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
356         # --type="pkinit-kdc" \
357         # --pk-init-principal="krbtgt/SAMBA.EXAMPLE.COM@SAMBA.EXAMPLE.COM" \
358         # --req="PKCS10:$KDCREQ" --certificate="FILE:$KDCCERTFILE" \
359         # --lifetime="25 years"
360
361         open(KDCCERTFILE, ">$kdccertfile");
362         print KDCCERTFILE <<EOF;
363 -----BEGIN CERTIFICATE-----
364 MIIDDDCCAnWgAwIBAgIUI2Tzj+JnMzMcdeabcNo30rovzFAwCwYJKoZIhvcNAQEFMFIxEzAR
365 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
366 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDgwMzAxMTMxOTIzWhgPMjAzMzAyMjQx
367 MzE5MjNaMGYxEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
368 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMQ8wDQYDVQQDDAZrcmJ0
369 Z3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMqDqkDAIdQwDUN8cOZaFl934XQL70nF
370 yq+nD2KL0SfcTW5+WlyiXf5L3/oj+5pOYkdmt74MXd1PNv9Q5mjRl6bw34jPOSCgaQVp+Ne5
371 PcEvlQ9jb8fof6k+pUhEs6atYiZqLfn1jKgqEXKjftjoc95TxBxn67atL2B5qkhZ966jAgMB
372 AAGjgcgwgcUwDgYDVR0PAQH/BAQDAgWgMBIGA1UdJQQLMAkGBysGAQUCAwUwVAYDVR0RBE0w
373 S6BJBgYrBgEFAgKgPzA9oBMbEVNBTUJBLkVYQU1QTEUuQ09NoSYwJKADAgEBoR0wGxsGa3Ji
374 dGd0GxFTQU1CQS5FWEFNUExFLkNPTTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS+jPK
375 MjAdBgNVHQ4EFgQUwtm596AMotmzRU7IVdgrUvozyjIwCQYDVR0TBAIwADANBgkqhkiG9w0B
376 AQUFAAOBgQBmrVD5MCmZjfHp1nEnHqTIh8r7lSmVtDx4s9MMjxm9oNrzbKXynvdhwQYFVarc
377 ge4yRRDXtSebErOl71zVJI9CVeQQpwcH+tA85oGA7oeFtO/S7ls581RUU6tGgyxV4veD+lJv
378 KPH5LevUtgD+q9H4LU4Sq5N3iFwBaeryB0g2wg==
379 -----END CERTIFICATE-----
380 EOF
381
382         # hxtool request-create \
383         # --subject="CN=Administrator,CN=users,DC=samba,DC=example,DC=com" \
384         # --key="FILE:$ADMINKEYFILE" $ADMINREQFILE
385
386         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
387         # --type="pkinit-client" \
388         # --pk-init-principal="administrator@SAMBA.EXAMPLE.COM" \
389         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTFILE" \
390         # --lifetime="25 years"
391         
392         open(ADMINCERTFILE, ">$admincertfile");
393         print ADMINCERTFILE <<EOF;
394 -----BEGIN CERTIFICATE-----
395 MIIDHTCCAoagAwIBAgIUUggzW4lLRkMKe1DAR2NKatkMDYwwCwYJKoZIhvcNAQELMFIxEzAR
396 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
397 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMjE1WhgPMjAzNDA3MjIw
398 MzMyMTVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
399 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
400 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
401 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
402 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
403 l3JRlwIDAQABo4HSMIHPMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
404 BgEFBQcDAgYKKwYBBAGCNxQCAjBIBgNVHREEQTA/oD0GBisGAQUCAqAzMDGgExsRU0FNQkEu
405 RVhBTVBMRS5DT02hGjAYoAMCAQGhETAPGw1BZG1pbmlzdHJhdG9yMB8GA1UdIwQYMBaAFMLZ
406 ufegDKLZs0VOyFXYK1L6M8oyMB0GA1UdDgQWBBQg81bLyfCA88C2B/BDjXlGuaFaxjAJBgNV
407 HRMEAjAAMA0GCSqGSIb3DQEBCwUAA4GBAEf/OSHUDJaGdtWGNuJeqcVYVMwrfBAc0OSwVhz1
408 7/xqKHWo8wIMPkYRtaRHKLNDsF8GkhQPCpVsa6mX/Nt7YQnNvwd+1SBP5E8GvwWw9ZzLJvma
409 nk2n89emuayLpVtp00PymrDLRBcNaRjFReQU8f0o509kiVPHduAp3jOiy13l
410 -----END CERTIFICATE-----
411 EOF
412         close(ADMINCERTFILE);
413
414         # hxtool issue-certificate --ca-certificate=FILE:$CAFILE,$KEYFILE \
415         # --type="pkinit-client" \
416         # --ms-upn="administrator@samba.example.com" \
417         # --req="PKCS10:$ADMINREQFILE" --certificate="FILE:$ADMINCERTUPNFILE" \
418         # --lifetime="25 years"
419         
420         open(ADMINCERTUPNFILE, ">$admincertupnfile");
421         print ADMINCERTUPNFILE <<EOF;
422 -----BEGIN CERTIFICATE-----
423 MIIDDzCCAnigAwIBAgIUUp3CJMuNaEaAdPKp3QdNIwG7a4wwCwYJKoZIhvcNAQELMFIxEzAR
424 BgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxlMRUwEwYKCZImiZPy
425 LGQBGQwFc2FtYmExCzAJBgNVBAMMAkNBMCIYDzIwMDkwNzI3MDMzMzA1WhgPMjAzNDA3MjIw
426 MzMzMDVaMG0xEzARBgoJkiaJk/IsZAEZDANjb20xFzAVBgoJkiaJk/IsZAEZDAdleGFtcGxl
427 MRUwEwYKCZImiZPyLGQBGQwFc2FtYmExDjAMBgNVBAMMBXVzZXJzMRYwFAYDVQQDDA1BZG1p
428 bmlzdHJhdG9yMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0+OL7TQBj0RejbIH1+g5G
429 eRaWaM9xF43uE5y7jUHEsi5owhZF5iIoHZeeL6cpDF5y1BZRs0JlA1VqMry1jjKlzFYVEMMF
430 xB6esnXhl0Jpip1JkUMMXLOP1m/0dqayuHBWozj9f/cdyCJr0wJIX1Z8Pr+EjYRGPn/MF0xd
431 l3JRlwIDAQABo4HEMIHBMA4GA1UdDwEB/wQEAwIFoDAoBgNVHSUEITAfBgcrBgEFAgMEBggr
432 BgEFBQcDAgYKKwYBBAGCNxQCAjA6BgNVHREEMzAxoC8GCisGAQQBgjcUAgOgIQwfYWRtaW5p
433 c3RyYXRvckBzYW1iYS5leGFtcGxlLmNvbTAfBgNVHSMEGDAWgBTC2bn3oAyi2bNFTshV2CtS
434 +jPKMjAdBgNVHQ4EFgQUIPNWy8nwgPPAtgfwQ415RrmhWsYwCQYDVR0TBAIwADANBgkqhkiG
435 9w0BAQsFAAOBgQBk42+egeUB3Ji2PC55fbt3FNKxvmm2xUUFkV9POK/YR9rajKOwk5jtYSeS
436 Zd7J9s//rNFNa7waklFkDaY56+QWTFtdvxfE+KoHaqt6X8u6pqi7p3M4wDKQox+9Dx8yWFyq
437 Wfz/8alZ5aMezCQzXJyIaJsCLeKABosSwHcpAFmxlQ==
438 -----END CERTIFICATE-----
439 EOF
440 }
441
442 sub provision_raw_prepare($$$$$$$$$$)
443 {
444         my ($self, $prefix, $server_role, $hostname,
445             $domain, $realm, $functional_level,
446             $password, $kdc_ipv4) = @_;
447         my $ctx;
448         my $netbiosname = uc($hostname);
449
450         unless(-d $prefix or mkdir($prefix, 0777)) {
451                 warn("Unable to create $prefix");
452                 return undef;
453         }
454         my $prefix_abs = abs_path($prefix);
455
456         die ("prefix=''") if $prefix_abs eq "";
457         die ("prefix='/'") if $prefix_abs eq "/";
458
459         unless (system("rm -rf $prefix_abs/*") == 0) {
460                 warn("Unable to clean up");
461         }
462
463         
464         my $swiface = Samba::get_interface($hostname);
465
466         $ctx->{prefix} = $prefix;
467         $ctx->{prefix_abs} = $prefix_abs;
468         
469         $ctx->{dns_host_file} = "$ENV{SELFTEST_PREFIX}/dns_host_file";
470
471         $ctx->{server_role} = $server_role;
472         $ctx->{hostname} = $hostname;
473         $ctx->{netbiosname} = $netbiosname;
474         $ctx->{swiface} = $swiface;
475         $ctx->{password} = $password;
476         $ctx->{kdc_ipv4} = $kdc_ipv4;
477
478 #
479 # Set smbd log level here.
480 #
481         $ctx->{server_loglevel} =$ENV{SERVER_LOG_LEVEL} || 1;
482         $ctx->{username} = "Administrator";
483         $ctx->{domain} = $domain;
484         $ctx->{realm} = uc($realm);
485         $ctx->{dnsname} = lc($realm);
486
487         $ctx->{functional_level} = $functional_level;
488
489         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `whoami`);
490         chomp $unix_name;
491         $ctx->{unix_name} = $unix_name;
492         $ctx->{unix_uid} = $>;
493         $ctx->{unix_gids_str} = $);
494         @{$ctx->{unix_gids}} = split(" ", $ctx->{unix_gids_str});
495
496         $ctx->{etcdir} = "$prefix_abs/etc";
497         $ctx->{piddir} = "$prefix_abs/pid";
498         $ctx->{smb_conf} = "$ctx->{etcdir}/smb.conf";
499         $ctx->{krb5_conf} = "$ctx->{etcdir}/krb5.conf";
500         $ctx->{privatedir} = "$prefix_abs/private";
501         $ctx->{ncalrpcdir} = "$prefix_abs/ncalrpc";
502         $ctx->{lockdir} = "$prefix_abs/lockdir";
503         $ctx->{logdir} = "$prefix_abs/logs";
504         $ctx->{statedir} = "$prefix_abs/statedir";
505         $ctx->{cachedir} = "$prefix_abs/cachedir";
506         $ctx->{winbindd_socket_dir} = "$prefix_abs/winbindd_socket";
507         $ctx->{winbindd_privileged_socket_dir} = "$prefix_abs/winbindd_privileged_socket";
508         $ctx->{ntp_signd_socket_dir} = "$prefix_abs/ntp_signd_socket";
509         $ctx->{nsswrap_passwd} = "$ctx->{etcdir}/passwd";
510         $ctx->{nsswrap_group} = "$ctx->{etcdir}/group";
511
512         $ctx->{tlsdir} = "$ctx->{privatedir}/tls";
513
514         $ctx->{ipv4} = "127.0.0.$swiface";
515         $ctx->{interfaces} = "$ctx->{ipv4}/8";
516
517         push(@{$ctx->{directories}}, $ctx->{privatedir});
518         push(@{$ctx->{directories}}, $ctx->{etcdir});
519         push(@{$ctx->{directories}}, $ctx->{piddir});
520         push(@{$ctx->{directories}}, $ctx->{lockdir});
521         push(@{$ctx->{directories}}, $ctx->{logdir});
522         push(@{$ctx->{directories}}, $ctx->{statedir});
523         push(@{$ctx->{directories}}, $ctx->{cachedir});
524
525         $ctx->{smb_conf_extra_options} = "";
526
527         my @provision_options = ();
528         push (@provision_options, "NSS_WRAPPER_PASSWD=\"$ctx->{nsswrap_passwd}\"");
529         push (@provision_options, "NSS_WRAPPER_GROUP=\"$ctx->{nsswrap_group}\"");
530         if (defined($ENV{GDB_PROVISION})) {
531                 push (@provision_options, "gdb --args");
532                 if (!defined($ENV{PYTHON})) {
533                     push (@provision_options, "env");
534                     push (@provision_options, "python");
535                 }
536         }
537         if (defined($ENV{VALGRIND_PROVISION})) {
538                 push (@provision_options, "valgrind");
539                 if (!defined($ENV{PYTHON})) {
540                     push (@provision_options, "env");
541                     push (@provision_options, "python");
542                 }
543         }
544         if (defined($ENV{PYTHON})) {
545                 push (@provision_options, $ENV{PYTHON});
546         }
547         push (@provision_options, Samba::bindir_path($self, "samba-tool"));
548         push (@provision_options, "domain");
549         push (@provision_options, "provision");
550         push (@provision_options, "--configfile=$ctx->{smb_conf}");
551         push (@provision_options, "--host-name=$ctx->{hostname}");
552         push (@provision_options, "--host-ip=$ctx->{ipv4}");
553         push (@provision_options, "--quiet");
554         push (@provision_options, "--domain=$ctx->{domain}");
555         push (@provision_options, "--realm=$ctx->{realm}");
556         push (@provision_options, "--adminpass=$ctx->{password}");
557         push (@provision_options, "--krbtgtpass=krbtgt$ctx->{password}");
558         push (@provision_options, "--machinepass=machine$ctx->{password}");
559         push (@provision_options, "--root=$ctx->{unix_name}");
560         push (@provision_options, "--server-role=\"$ctx->{server_role}\"");
561         push (@provision_options, "--function-level=\"$ctx->{functional_level}\"");
562
563         @{$ctx->{provision_options}} = @provision_options;
564
565         return $ctx;
566 }
567
568 #
569 # Step1 creates the basic configuration
570 #
571 sub provision_raw_step1($$)
572 {
573         my ($self, $ctx) = @_;
574
575         mkdir($_, 0777) foreach (@{$ctx->{directories}});
576
577         ##
578         ## lockdir and piddir must be 0755
579         ##
580         chmod 0755, $ctx->{lockdir};
581         chmod 0755, $ctx->{piddir};
582
583         unless (open(CONFFILE, ">$ctx->{smb_conf}")) {
584                 warn("can't open $ctx->{smb_conf}$?");
585                 return undef;
586         }
587         print CONFFILE "
588 [global]
589         netbios name = $ctx->{netbiosname}
590         posix:eadb = $ctx->{statedir}/eadb.tdb
591         workgroup = $ctx->{domain}
592         realm = $ctx->{realm}
593         private dir = $ctx->{privatedir}
594         pid directory = $ctx->{piddir}
595         ncalrpc dir = $ctx->{ncalrpcdir}
596         lock dir = $ctx->{lockdir}
597         state directory = $ctx->{statedir}
598         cache directory = $ctx->{cachedir}
599         winbindd socket directory = $ctx->{winbindd_socket_dir}
600         winbindd privileged socket directory = $ctx->{winbindd_privileged_socket_dir}
601         ntp signd socket directory = $ctx->{ntp_signd_socket_dir}
602         winbind separator = /
603         name resolve order = file bcast
604         interfaces = $ctx->{interfaces}
605         tls dh params file = $ctx->{tlsdir}/dhparms.pem
606         panic action = $RealBin/gdb_backtrace \%d
607         wins support = yes
608         server role = $ctx->{server_role}
609         server services = +echo +smb -s3fs
610         dcerpc endpoint servers = +winreg +srvsvc
611         notify:inotify = false
612         ldb:nosync = true
613 #We don't want to pass our self-tests if the PAC code is wrong
614         gensec:require_pac = true
615         log file = $ctx->{logdir}/log.\%m
616         log level = $ctx->{server_loglevel}
617         lanman auth = Yes
618         rndc command = true
619         dns update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_dnsupdate --all-interfaces --use-file=$ctx->{dns_host_file} -s $ctx->{smb_conf}
620         spn update command = $ENV{SRCDIR_ABS}/source4/scripting/bin/samba_spnupdate -s $ctx->{smb_conf}
621         resolv:host file = $ctx->{dns_host_file}
622         dreplsrv:periodic_startup_interval = 0
623         dsdb:schema update allowed = yes
624
625         vfs objects = dfs_samba4 acl_xattr fake_acls xattr_tdb streams_depot
626
627         # remove this again, when our smb2 client library
628         # supports signin on compound related requests
629         server signing = on
630
631         idmap_ldb:use rfc2307=yes
632 ";
633
634         print CONFFILE "
635
636         # Begin extra options
637         $ctx->{smb_conf_extra_options}
638         # End extra options
639 ";
640         close(CONFFILE);
641
642         $self->mk_keyblobs($ctx->{tlsdir});
643
644         #Default the KDC IP to the server's IP
645         if (not defined($ctx->{kdc_ipv4})) {
646              $ctx->{kdc_ipv4} = $ctx->{ipv4};
647         }
648
649         Samba::mk_krb5_conf($ctx, "");
650
651         open(PWD, ">$ctx->{nsswrap_passwd}");
652         print PWD "
653 root:x:0:0:root gecos:$ctx->{prefix_abs}:/bin/false
654 nobody:x:65534:65533:nobody gecos:$ctx->{prefix_abs}:/bin/false
655 pdbtest:x:65533:65533:pdbtest gecos:$ctx->{prefix_abs}:/bin/false
656 ";
657         close(PWD);
658         my $uid_rfc2307test = 65533;
659
660         open(GRP, ">$ctx->{nsswrap_group}");
661         print GRP "
662 root:x:0:
663 wheel:x:10:
664 users:x:100:
665 nobody:x:65533:
666 nogroup:x:65534:nobody
667 ";
668         close(GRP);
669         my $gid_rfc2307test = 65532;
670
671         my $configuration = "--configfile=$ctx->{smb_conf}";
672
673 #Ensure the config file is valid before we start
674         my $testparm = Samba::bindir_path($self, "samba-tool") . " testparm";
675         if (system("$testparm $configuration -v --suppress-prompt >/dev/null 2>&1") != 0) {
676                 system("$testparm -v --suppress-prompt $configuration >&2");
677                 warn("Failed to create a valid smb.conf configuration $testparm!");
678                 return undef;
679         }
680         unless (system("($testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global 2> /dev/null | grep -i \"^$ctx->{netbiosname}\" ) >/dev/null 2>&1") == 0) {
681                 warn("Failed to create a valid smb.conf configuration! $testparm $configuration -v --suppress-prompt --parameter-name=\"netbios name\" --section-name=global");
682                 return undef;
683         }
684
685         my $ret = {
686                 KRB5_CONFIG => $ctx->{krb5_conf},
687                 PIDDIR => $ctx->{piddir},
688                 SERVER => $ctx->{hostname},
689                 SERVER_IP => $ctx->{ipv4},
690                 NETBIOSNAME => $ctx->{netbiosname},
691                 DOMAIN => $ctx->{domain},
692                 USERNAME => $ctx->{username},
693                 REALM => $ctx->{realm},
694                 PASSWORD => $ctx->{password},
695                 LDAPDIR => $ctx->{ldapdir},
696                 LDAP_INSTANCE => $ctx->{ldap_instance},
697                 WINBINDD_SOCKET_DIR => $ctx->{winbindd_socket_dir},
698                 NCALRPCDIR => $ctx->{ncalrpcdir},
699                 LOCKDIR => $ctx->{lockdir},
700                 STATEDIR => $ctx->{statedir},
701                 CACHEDIR => $ctx->{cachedir},
702                 PRIVATEDIR => $ctx->{privatedir},
703                 SERVERCONFFILE => $ctx->{smb_conf},
704                 CONFIGURATION => $configuration,
705                 SOCKET_WRAPPER_DEFAULT_IFACE => $ctx->{swiface},
706                 NSS_WRAPPER_PASSWD => $ctx->{nsswrap_passwd},
707                 NSS_WRAPPER_GROUP => $ctx->{nsswrap_group},
708                 SAMBA_TEST_FIFO => "$ctx->{prefix}/samba_test.fifo",
709                 SAMBA_TEST_LOG => "$ctx->{prefix}/samba_test.log",
710                 SAMBA_TEST_LOG_POS => 0,
711                 NSS_WRAPPER_WINBIND_SO_PATH => Samba::nss_wrapper_winbind_so_path($self),
712                 LOCAL_PATH => $ctx->{share},
713                 UID_RFC2307TEST => $uid_rfc2307test,
714                 GID_RFC2307TEST => $gid_rfc2307test,
715                 SERVER_ROLE => $ctx->{server_role}
716         };
717
718         return $ret;
719 }
720
721 #
722 # Step2 runs the provision script
723 #
724 sub provision_raw_step2($$$)
725 {
726         my ($self, $ctx, $ret) = @_;
727
728         my $provision_cmd = join(" ", @{$ctx->{provision_options}});
729         unless (system($provision_cmd) == 0) {
730                 warn("Unable to provision: \n$provision_cmd\n");
731                 return undef;
732         }
733
734         return $ret;
735 }
736
737 sub provision($$$$$$$$$)
738 {
739         my ($self, $prefix, $server_role, $hostname,
740             $domain, $realm, $functional_level,
741             $password, $kdc_ipv4, $extra_smbconf_options, $extra_smbconf_shares,
742             $extra_provision_options) = @_;
743
744         my $ctx = $self->provision_raw_prepare($prefix, $server_role,
745                                                $hostname,
746                                                $domain, $realm, $functional_level,
747                                                $password, $kdc_ipv4);
748
749         if (defined($extra_provision_options)) {
750                 push (@{$ctx->{provision_options}}, @{$extra_provision_options});
751         } else {
752                 push (@{$ctx->{provision_options}}, "--use-ntvfs");
753         }
754
755         $ctx->{share} = "$ctx->{prefix_abs}/share";
756         push(@{$ctx->{directories}}, "$ctx->{share}");
757         push(@{$ctx->{directories}}, "$ctx->{share}/test1");
758         push(@{$ctx->{directories}}, "$ctx->{share}/test2");
759
760         # precreate directories for printer drivers
761         push(@{$ctx->{directories}}, "$ctx->{share}/W32X86");
762         push(@{$ctx->{directories}}, "$ctx->{share}/x64");
763         push(@{$ctx->{directories}}, "$ctx->{share}/WIN40");
764
765         my $msdfs = "no";
766         $msdfs = "yes" if ($server_role eq "domain controller");
767         $ctx->{smb_conf_extra_options} = "
768
769         max xmit = 32K
770         server max protocol = SMB2
771         host msdfs = $msdfs
772         lanman auth = yes
773
774         $extra_smbconf_options
775
776 [tmp]
777         path = $ctx->{share}
778         read only = no
779         posix:sharedelay = 10000
780         posix:oplocktimeout = 3
781         posix:writetimeupdatedelay = 50000
782
783 [xcopy_share]
784         path = $ctx->{share}
785         read only = no
786         posix:sharedelay = 10000
787         posix:oplocktimeout = 3
788         posix:writetimeupdatedelay = 50000
789         create mask = 777
790         force create mode = 777
791
792 [posix_share]
793         path = $ctx->{share}
794         read only = no
795         create mask = 0777
796         force create mode = 0
797         directory mask = 0777
798         force directory mode = 0
799
800 [test1]
801         path = $ctx->{share}/test1
802         read only = no
803         posix:sharedelay = 10000
804         posix:oplocktimeout = 3
805         posix:writetimeupdatedelay = 50000
806
807 [test2]
808         path = $ctx->{share}/test2
809         read only = no
810         posix:sharedelay = 10000
811         posix:oplocktimeout = 3
812         posix:writetimeupdatedelay = 50000
813
814 [cifs]
815         read only = no
816         ntvfs handler = cifs
817         cifs:server = $ctx->{netbiosname}
818         cifs:share = tmp
819         cifs:use-s4u2proxy = yes
820         # There is no username specified here, instead the client is expected
821         # to log in with kerberos, and the serverwill use delegated credentials.
822         # Or the server tries s4u2self/s4u2proxy to impersonate the client
823
824 [simple]
825         path = $ctx->{share}
826         read only = no
827         ntvfs handler = simple
828
829 [sysvol]
830         path = $ctx->{statedir}/sysvol
831         read only = no
832
833 [netlogon]
834         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
835         read only = no
836
837 [cifsposix]
838         copy = simple
839         ntvfs handler = cifsposix
840
841 $extra_smbconf_shares
842 ";
843
844         if (defined($self->{ldap})) {
845                 $ctx->{ldapdir} = "$ctx->{privatedir}/ldap";
846                 push(@{$ctx->{directories}}, "$ctx->{ldapdir}");
847
848                 my $ldap_uri= "$ctx->{ldapdir}/ldapi";
849                 $ldap_uri =~ s|/|%2F|g;
850                 $ldap_uri = "ldapi://$ldap_uri";
851                 $ctx->{ldap_uri} = $ldap_uri;
852
853                 $ctx->{ldap_instance} = lc($ctx->{netbiosname});
854         }
855
856         my $ret = $self->provision_raw_step1($ctx);
857         unless (defined $ret) {
858                 return undef;
859         }
860
861         if (defined($self->{ldap})) {
862                 $ret->{LDAP_URI} = $ctx->{ldap_uri};
863                 push (@{$ctx->{provision_options}}, "--ldap-backend-type=" . $self->{ldap});
864                 push (@{$ctx->{provision_options}}, "--ldap-backend-nosync");
865                 if ($self->{ldap} eq "openldap") {
866                         push (@{$ctx->{provision_options}}, "--slapd-path=" . $ENV{OPENLDAP_SLAPD});
867                         ($ret->{SLAPD_CONF_D}, $ret->{OPENLDAP_PIDFILE}) = $self->mk_openldap($ctx) or die("Unable to create openldap directories");
868
869                 } elsif ($self->{ldap} eq "fedora-ds") {
870                         push (@{$ctx->{provision_options}}, "--slapd-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/ns-slapd");
871                         push (@{$ctx->{provision_options}}, "--setup-ds-path=" . "$ENV{FEDORA_DS_ROOT}/sbin/setup-ds.pl");
872                         ($ret->{FEDORA_DS_DIR}, $ret->{FEDORA_DS_PIDFILE}) = $self->mk_fedora_ds($ctx) or die("Unable to create fedora ds directories");
873                 }
874
875         }
876
877         return $self->provision_raw_step2($ctx, $ret);
878 }
879
880 sub provision_member($$$)
881 {
882         my ($self, $prefix, $dcvars) = @_;
883         print "PROVISIONING MEMBER...";
884
885         my $ret = $self->provision($prefix,
886                                    "member server",
887                                    "s4member",
888                                    "SAMBADOMAIN",
889                                    "samba.example.com",
890                                    "2008",
891                                    "locMEMpass3",
892                                    $dcvars->{SERVER_IP},
893                                    "", "", undef);
894         unless ($ret) {
895                 return undef;
896         }
897
898         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
899         my $cmd = "";
900         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
901         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
902         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
903         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
904         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
905
906         unless (system($cmd) == 0) {
907                 warn("Join failed\n$cmd");
908                 return undef;
909         }
910
911         $ret->{MEMBER_SERVER} = $ret->{SERVER};
912         $ret->{MEMBER_SERVER_IP} = $ret->{SERVER_IP};
913         $ret->{MEMBER_NETBIOSNAME} = $ret->{NETBIOSNAME};
914         $ret->{MEMBER_USERNAME} = $ret->{USERNAME};
915         $ret->{MEMBER_PASSWORD} = $ret->{PASSWORD};
916
917         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
918         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
919         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
920         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
921         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
922
923         return $ret;
924 }
925
926 sub provision_rpc_proxy($$$)
927 {
928         my ($self, $prefix, $dcvars) = @_;
929         print "PROVISIONING RPC PROXY...";
930
931         my $extra_smbconf_options = "
932
933         # rpc_proxy
934         dcerpc_remote:binding = ncacn_ip_tcp:$dcvars->{SERVER}
935         dcerpc endpoint servers = epmapper, remote
936         dcerpc_remote:interfaces = rpcecho
937
938 [cifs_to_dc]
939         read only = no
940         ntvfs handler = cifs
941         cifs:server = $dcvars->{SERVER}
942         cifs:share = cifs
943         cifs:use-s4u2proxy = yes
944         # There is no username specified here, instead the client is expected
945         # to log in with kerberos, and the serverwill use delegated credentials.
946         # Or the server tries s4u2self/s4u2proxy to impersonate the client
947
948 ";
949
950         my $ret = $self->provision($prefix,
951                                    "member server",
952                                    "localrpcproxy",
953                                    "SAMBADOMAIN",
954                                    "samba.example.com",
955                                    "2008",
956                                    "locRPCproxypass4",
957                                    $dcvars->{SERVER_IP},
958                                    $extra_smbconf_options, "", undef);
959
960         unless ($ret) {
961                 return undef;
962         }
963
964         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
965
966         # The joind runs in the context of the rpc_proxy/member for now
967         my $cmd = "";
968         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
969         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
970         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} member";
971         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
972         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
973
974         unless (system($cmd) == 0) {
975                 warn("Join failed\n$cmd");
976                 return undef;
977         }
978
979         # Setting up delegation runs in the context of the DC for now
980         $cmd = "";
981         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
982         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
983         $cmd .= "$samba_tool delegation for-any-protocol '$ret->{NETBIOSNAME}\$' on";
984         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
985
986         unless (system($cmd) == 0) {
987                 warn("Delegation failed\n$cmd");
988                 return undef;
989         }
990
991         # Setting up delegation runs in the context of the DC for now
992         $cmd = "";
993         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$dcvars->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
994         $cmd .= "KRB5_CONFIG=\"$dcvars->{KRB5_CONFIG}\" ";
995         $cmd .= "$samba_tool delegation add-service '$ret->{NETBIOSNAME}\$' cifs/$dcvars->{SERVER}";
996         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} $dcvars->{CONFIGURATION}";
997
998         unless (system($cmd) == 0) {
999                 warn("Delegation failed\n$cmd");
1000                 return undef;
1001         }
1002
1003         $ret->{RPC_PROXY_SERVER} = $ret->{SERVER};
1004         $ret->{RPC_PROXY_SERVER_IP} = $ret->{SERVER_IP};
1005         $ret->{RPC_PROXY_NETBIOSNAME} = $ret->{NETBIOSNAME};
1006         $ret->{RPC_PROXY_USERNAME} = $ret->{USERNAME};
1007         $ret->{RPC_PROXY_PASSWORD} = $ret->{PASSWORD};
1008
1009         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1010         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1011         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1012         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1013         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1014
1015         return $ret;
1016 }
1017
1018 sub provision_promoted_dc($$$)
1019 {
1020         my ($self, $prefix, $dcvars) = @_;
1021         print "PROVISIONING VAMPIRE DC...";
1022
1023         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1024         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1025                                                "promotedvdc",
1026                                                "SAMBADOMAIN",
1027                                                "samba.example.com",
1028                                                "2008",
1029                                                $dcvars->{PASSWORD},
1030                                                $dcvars->{SERVER_IP});
1031
1032         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1033
1034         $ctx->{smb_conf_extra_options} = "
1035         max xmit = 32K
1036         server max protocol = SMB2
1037
1038 [sysvol]
1039         path = $ctx->{statedir}/sysvol
1040         read only = yes
1041
1042 [netlogon]
1043         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1044         read only = no
1045
1046 ";
1047
1048         my $ret = $self->provision_raw_step1($ctx);
1049         unless ($ret) {
1050                 return undef;
1051         }
1052
1053         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1054         my $cmd = "";
1055         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1056         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1057         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} MEMBER --realm=$dcvars->{REALM}";
1058         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1059         $cmd .= " --machinepass=machine$ret->{PASSWORD}";
1060
1061         unless (system($cmd) == 0) {
1062                 warn("Join failed\n$cmd");
1063                 return undef;
1064         }
1065
1066         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1067         my $cmd = "";
1068         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1069         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1070         $cmd .= "$samba_tool domain dcpromo $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1071         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1072         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs --dns-backend=BIND9_DLZ";
1073
1074         unless (system($cmd) == 0) {
1075                 warn("Join failed\n$cmd");
1076                 return undef;
1077         }
1078
1079         $ret->{PROMOTED_DC_SERVER} = $ret->{SERVER};
1080         $ret->{PROMOTED_DC_SERVER_IP} = $ret->{SERVER_IP};
1081         $ret->{PROMOTED_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1082
1083         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1084         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1085         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1086         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1087         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1088
1089         return $ret;
1090 }
1091
1092 sub provision_vampire_dc($$$)
1093 {
1094         my ($self, $prefix, $dcvars) = @_;
1095         print "PROVISIONING VAMPIRE DC...";
1096
1097         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1098         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1099                                                "localvampiredc",
1100                                                "SAMBADOMAIN",
1101                                                "samba.example.com",
1102                                                "2008",
1103                                                $dcvars->{PASSWORD},
1104                                                $dcvars->{SERVER_IP});
1105
1106         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1107
1108         $ctx->{smb_conf_extra_options} = "
1109         max xmit = 32K
1110         server max protocol = SMB2
1111
1112 [sysvol]
1113         path = $ctx->{statedir}/sysvol
1114         read only = yes
1115
1116 [netlogon]
1117         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1118         read only = no
1119
1120 ";
1121
1122         my $ret = $self->provision_raw_step1($ctx);
1123         unless ($ret) {
1124                 return undef;
1125         }
1126
1127         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1128         my $cmd = "";
1129         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1130         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1131         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} DC --realm=$dcvars->{REALM}";
1132         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD} --domain-critical-only";
1133         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1134
1135         unless (system($cmd) == 0) {
1136                 warn("Join failed\n$cmd");
1137                 return undef;
1138         }
1139
1140         $ret->{VAMPIRE_DC_SERVER} = $ret->{SERVER};
1141         $ret->{VAMPIRE_DC_SERVER_IP} = $ret->{SERVER_IP};
1142         $ret->{VAMPIRE_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1143
1144         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1145         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1146         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1147         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1148         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1149
1150         return $ret;
1151 }
1152
1153 sub provision_subdom_dc($$$)
1154 {
1155         my ($self, $prefix, $dcvars) = @_;
1156         print "PROVISIONING SUBDOMAIN DC...";
1157
1158         # We do this so that we don't run the provision.  That's the job of 'net vampire'.
1159         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1160                                                "localsubdc",
1161                                                "SAMBASUBDOM",
1162                                                "sub.samba.example.com",
1163                                                "2008",
1164                                                $dcvars->{PASSWORD},
1165                                                undef);
1166
1167         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1168
1169         $ctx->{smb_conf_extra_options} = "
1170         max xmit = 32K
1171         server max protocol = SMB2
1172
1173 [sysvol]
1174         path = $ctx->{statedir}/sysvol
1175         read only = yes
1176
1177 [netlogon]
1178         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1179         read only = no
1180
1181 ";
1182
1183         my $ret = $self->provision_raw_step1($ctx);
1184         unless ($ret) {
1185                 return undef;
1186         }
1187
1188         my $dc_realms = Samba::mk_realms_stanza($dcvars->{REALM}, lc($dcvars->{REALM}),
1189                                                 $dcvars->{DOMAIN}, $dcvars->{SERVER_IP});
1190         Samba::mk_krb5_conf($ctx, $dc_realms);
1191
1192         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1193         my $cmd = "";
1194         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1195         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1196         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $ctx->{realm} subdomain ";
1197         $cmd .= "--parent-domain=$dcvars->{REALM} -U$dcvars->{DC_USERNAME}\@$dcvars->{REALM}\%$dcvars->{DC_PASSWORD}";
1198         $cmd .= " --machinepass=machine$ret->{PASSWORD} --use-ntvfs";
1199
1200         unless (system($cmd) == 0) {
1201                 warn("Join failed\n$cmd");
1202                 return undef;
1203         }
1204
1205         $ret->{SUBDOM_DC_SERVER} = $ret->{SERVER};
1206         $ret->{SUBDOM_DC_SERVER_IP} = $ret->{SERVER_IP};
1207         $ret->{SUBDOM_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1208
1209         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1210         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1211         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1212         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1213         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1214
1215         return $ret;
1216 }
1217
1218 sub provision_dc($$)
1219 {
1220         my ($self, $prefix) = @_;
1221
1222         print "PROVISIONING DC...";
1223         my $extra_conf_options = "netbios aliases = localDC1-a";
1224         my $ret = $self->provision($prefix,
1225                                    "domain controller",
1226                                    "localdc",
1227                                    "SAMBADOMAIN",
1228                                    "samba.example.com",
1229                                    "2008",
1230                                    "locDCpass1",
1231                                    undef, $extra_conf_options, "", undef);
1232
1233         return undef unless(defined $ret);
1234         unless($self->add_wins_config("$prefix/private")) {
1235                 warn("Unable to add wins configuration");
1236                 return undef;
1237         }
1238         $ret->{NETBIOSALIAS} = "localdc1-a";
1239         $ret->{DC_SERVER} = $ret->{SERVER};
1240         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1241         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1242         $ret->{DC_USERNAME} = $ret->{USERNAME};
1243         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1244
1245         return $ret;
1246 }
1247
1248 sub provision_fl2000dc($$)
1249 {
1250         my ($self, $prefix) = @_;
1251
1252         print "PROVISIONING DC...";
1253         my $ret = $self->provision($prefix,
1254                                    "domain controller",
1255                                    "dc5",
1256                                    "SAMBA2000",
1257                                    "samba2000.example.com",
1258                                    "2000",
1259                                    "locDCpass5",
1260                                    undef, "", "", undef);
1261
1262         unless($self->add_wins_config("$prefix/private")) {
1263                 warn("Unable to add wins configuration");
1264                 return undef;
1265         }
1266
1267         return $ret;
1268 }
1269
1270 sub provision_fl2003dc($$)
1271 {
1272         my ($self, $prefix) = @_;
1273
1274         print "PROVISIONING DC...";
1275         my $ret = $self->provision($prefix,
1276                                    "domain controller",
1277                                    "dc6",
1278                                    "SAMBA2003",
1279                                    "samba2003.example.com",
1280                                    "2003",
1281                                    "locDCpass6",
1282                                    undef, "allow dns updates = nonsecure and secure", "", undef);
1283
1284         unless($self->add_wins_config("$prefix/private")) {
1285                 warn("Unable to add wins configuration");
1286                 return undef;
1287         }
1288
1289         return $ret;
1290 }
1291
1292 sub provision_fl2008r2dc($$)
1293 {
1294         my ($self, $prefix) = @_;
1295
1296         print "PROVISIONING DC...";
1297         my $ret = $self->provision($prefix,
1298                                    "domain controller",
1299                                    "dc7",
1300                                    "SAMBA2008R2",
1301                                    "samba2008R2.example.com",
1302                                    "2008_R2",
1303                                    "locDCpass7",
1304                                    undef, "", "", undef);
1305
1306         unless ($self->add_wins_config("$prefix/private")) {
1307                 warn("Unable to add wins configuration");
1308                 return undef;
1309         }
1310
1311         return $ret;
1312 }
1313
1314
1315 sub provision_rodc($$$)
1316 {
1317         my ($self, $prefix, $dcvars) = @_;
1318         print "PROVISIONING RODC...";
1319
1320         # We do this so that we don't run the provision.  That's the job of 'net join RODC'.
1321         my $ctx = $self->provision_raw_prepare($prefix, "domain controller",
1322                                                "rodc",
1323                                                "SAMBADOMAIN",
1324                                                "samba.example.com",
1325                                                "2008",
1326                                                $dcvars->{PASSWORD},
1327                                                $dcvars->{SERVER_IP});
1328         unless ($ctx) {
1329                 return undef;
1330         }
1331
1332         push (@{$ctx->{provision_options}}, "--use-ntvfs");
1333
1334         $ctx->{share} = "$ctx->{prefix_abs}/share";
1335         push(@{$ctx->{directories}}, "$ctx->{share}");
1336
1337         $ctx->{smb_conf_extra_options} = "
1338         max xmit = 32K
1339         server max protocol = SMB2
1340
1341 [sysvol]
1342         path = $ctx->{statedir}/sysvol
1343         read only = yes
1344
1345 [netlogon]
1346         path = $ctx->{statedir}/sysvol/$ctx->{dnsname}/scripts
1347         read only = yes
1348
1349 [tmp]
1350         path = $ctx->{share}
1351         read only = no
1352         posix:sharedelay = 10000
1353         posix:oplocktimeout = 3
1354         posix:writetimeupdatedelay = 50000
1355
1356 ";
1357
1358         my $ret = $self->provision_raw_step1($ctx);
1359         unless ($ret) {
1360                 return undef;
1361         }
1362
1363         my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1364         my $cmd = "";
1365         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
1366         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
1367         $cmd .= "$samba_tool domain join $ret->{CONFIGURATION} $dcvars->{REALM} RODC";
1368         $cmd .= " -U$dcvars->{DC_USERNAME}\%$dcvars->{DC_PASSWORD}";
1369         $cmd .= " --server=$dcvars->{DC_SERVER} --use-ntvfs";
1370
1371         unless (system($cmd) == 0) {
1372                 warn("RODC join failed\n$cmd");
1373                 return undef;
1374         }
1375
1376         # we overwrite the kdc after the RODC join
1377         # so that use the RODC as kdc and test
1378         # the proxy code
1379         $ctx->{kdc_ipv4} = $ret->{SERVER_IP};
1380         Samba::mk_krb5_conf($ctx);
1381
1382         $ret->{RODC_DC_SERVER} = $ret->{SERVER};
1383         $ret->{RODC_DC_SERVER_IP} = $ret->{SERVER_IP};
1384         $ret->{RODC_DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1385
1386         $ret->{DC_SERVER} = $dcvars->{DC_SERVER};
1387         $ret->{DC_SERVER_IP} = $dcvars->{DC_SERVER_IP};
1388         $ret->{DC_NETBIOSNAME} = $dcvars->{DC_NETBIOSNAME};
1389         $ret->{DC_USERNAME} = $dcvars->{DC_USERNAME};
1390         $ret->{DC_PASSWORD} = $dcvars->{DC_PASSWORD};
1391
1392         return $ret;
1393 }
1394
1395 sub provision_plugin_s4_dc($$)
1396 {
1397         my ($self, $prefix) = @_;
1398
1399         my $prefix_abs = abs_path($prefix);
1400
1401         my $bindir_abs = abs_path($self->{bindir});
1402         my $lockdir="$prefix_abs/lockdir";
1403         my $conffile="$prefix_abs/etc/smb.conf";
1404
1405         my $extra_smbconf_options = "
1406         server services = -smb +s3fs
1407         xattr_tdb:file = $prefix_abs/statedir/xattr.tdb
1408
1409         kernel oplocks = no
1410         kernel change notify = no
1411
1412         syslog = no
1413         printing = bsd
1414         printcap name = /dev/null
1415
1416         max protocol = SMB3
1417         read only = no
1418         server signing = auto
1419
1420         smbd:sharedelay = 100000
1421         smbd:writetimeupdatedelay = 500000
1422         create mask = 755
1423         dos filemode = yes
1424
1425         dcerpc endpoint servers = -winreg -srvsvc
1426
1427         printcap name = /dev/null
1428
1429         addprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -a -s $conffile --
1430         deleteprinter command = $ENV{SRCDIR_ABS}/source3/script/tests/printing/modprinter.pl -d -s $conffile --
1431
1432         printing = vlp
1433         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1434         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1435         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1436         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1437         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1438         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1439         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1440         lpq cache time = 0
1441
1442 ";
1443
1444         my $extra_smbconf_shares = "
1445
1446 [tmpenc]
1447         copy = tmp
1448         smb encrypt = required
1449
1450 [tmpcase]
1451         copy = tmp
1452         case sensitive = yes
1453
1454 [tmpguest]
1455         copy = tmp
1456         guest ok = yes
1457
1458 [hideunread]
1459         copy = tmp
1460         hide unreadable = yes
1461
1462 [durable]
1463         copy = tmp
1464         kernel share modes = no
1465         kernel oplocks = no
1466         posix locking = no
1467
1468 [print\$]
1469         copy = tmp
1470
1471 [print1]
1472         copy = tmp
1473         printable = yes
1474
1475 [print2]
1476         copy = print1
1477 [print3]
1478         copy = print1
1479 [lp]
1480         copy = print1
1481 ";
1482
1483         print "PROVISIONING PLUGIN S4 DC...";
1484         my $ret = $self->provision($prefix,
1485                                    "domain controller",
1486                                    "plugindc",
1487                                    "PLUGINDOMAIN",
1488                                    "plugindc.samba.example.com",
1489                                    "2008",
1490                                    "locDCpass1",
1491                                    undef, $extra_smbconf_options,
1492                                    $extra_smbconf_shares, undef);
1493
1494         return undef unless(defined $ret);
1495         unless($self->add_wins_config("$prefix/private")) {
1496                 warn("Unable to add wins configuration");
1497                 return undef;
1498         }
1499
1500         $ret->{DC_SERVER} = $ret->{SERVER};
1501         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1502         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1503         $ret->{DC_USERNAME} = $ret->{USERNAME};
1504         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1505
1506         return $ret;
1507 }
1508
1509 sub provision_chgdcpass($$)
1510 {
1511         my ($self, $prefix) = @_;
1512
1513         print "PROVISIONING CHGDCPASS...";
1514         my $extra_provision_options = undef;
1515         push (@{$extra_provision_options}, "--dns-backend=BIND9_DLZ");
1516         my $ret = $self->provision($prefix,
1517                                    "domain controller",
1518                                    "chgdcpass",
1519                                    "CHDCDOMAIN",
1520                                    "chgdcpassword.samba.example.com",
1521                                    "2008",
1522                                    "chgDCpass1",
1523                                    undef, "", "",
1524                                    $extra_provision_options);
1525
1526         return undef unless(defined $ret);
1527         unless($self->add_wins_config("$prefix/private")) {
1528                 warn("Unable to add wins configuration");
1529                 return undef;
1530         }
1531         
1532         # Remove secrets.tdb from this environment to test that we still start up
1533         # on systems without the new matching secrets.tdb records
1534         unless (unlink("$ret->{PRIVATEDIR}/secrets.tdb") || unlink("$ret->{PRIVATEDIR}/secrets.ntdb")) {
1535                 warn("Unable to remove $ret->{PRIVATEDIR}/secrets.tdb added during provision");
1536                 return undef;
1537         }
1538             
1539         $ret->{DC_SERVER} = $ret->{SERVER};
1540         $ret->{DC_SERVER_IP} = $ret->{SERVER_IP};
1541         $ret->{DC_NETBIOSNAME} = $ret->{NETBIOSNAME};
1542         $ret->{DC_USERNAME} = $ret->{USERNAME};
1543         $ret->{DC_PASSWORD} = $ret->{PASSWORD};
1544
1545         return $ret;
1546 }
1547
1548 sub teardown_env($$)
1549 {
1550         my ($self, $envvars) = @_;
1551         my $pid;
1552
1553         # This should cause samba to terminate gracefully
1554         close($envvars->{STDIN_PIPE});
1555
1556         $pid = $envvars->{SAMBA_PID};
1557         my $count = 0;
1558         my $childpid;
1559
1560         # This should give it time to write out the gcov data
1561         until ($count > 30) {
1562             if (Samba::cleanup_child($pid, "samba") == -1) {
1563                 last;
1564             }
1565             sleep(1);
1566             $count++;
1567         }
1568
1569         if ($count <= 20 && kill(0, $pid) == 0) {
1570             return;
1571         }
1572
1573         kill "TERM", $pid;
1574
1575         until ($count > 20) {
1576             if (Samba::cleanup_child($pid, "samba") == -1) {
1577                 last;
1578             }
1579             sleep(1);
1580             $count++;
1581         }
1582
1583         # If it is still around, kill it
1584         if ($count > 20 && kill(0, $pid) == 0) {
1585             warn "server process $pid took more than $count seconds to exit, killing\n";
1586             kill 9, $pid;
1587         }
1588
1589         $self->slapd_stop($envvars) if ($self->{ldap});
1590
1591         print $self->getlog_env($envvars);
1592
1593         return;
1594 }
1595
1596 sub getlog_env($$)
1597 {
1598         my ($self, $envvars) = @_;
1599         my $title = "SAMBA LOG of: $envvars->{NETBIOSNAME}\n";
1600         my $out = $title;
1601
1602         open(LOG, "<$envvars->{SAMBA_TEST_LOG}");
1603
1604         seek(LOG, $envvars->{SAMBA_TEST_LOG_POS}, SEEK_SET);
1605         while (<LOG>) {
1606                 $out .= $_;
1607         }
1608         $envvars->{SAMBA_TEST_LOG_POS} = tell(LOG);
1609         close(LOG);
1610
1611         return "" if $out eq $title;
1612
1613         return $out;
1614 }
1615
1616 sub check_env($$)
1617 {
1618         my ($self, $envvars) = @_;
1619
1620         my $childpid = Samba::cleanup_child($envvars->{SAMBA_PID}, "samba");
1621
1622         return ($childpid == 0);
1623 }
1624
1625 sub setup_env($$$)
1626 {
1627         my ($self, $envname, $path) = @_;
1628         my $target3 = $self->{target3};
1629
1630         $ENV{ENVNAME} = $envname;
1631
1632         if (defined($self->{vars}->{$envname})) {
1633                 return $self->{vars}->{$envname};
1634         }
1635
1636         if ($envname eq "dc") {
1637                 return $self->setup_dc("$path/dc");
1638         } elsif ($envname eq "fl2000dc") {
1639                 return $self->setup_fl2000dc("$path/fl2000dc");
1640         } elsif ($envname eq "fl2003dc") {
1641                 return $self->setup_fl2003dc("$path/fl2003dc");
1642         } elsif ($envname eq "fl2008r2dc") {
1643                 return $self->setup_fl2008r2dc("$path/fl2008r2dc");
1644         } elsif ($envname eq "rpc_proxy") {
1645                 if (not defined($self->{vars}->{dc})) {
1646                         $self->setup_dc("$path/dc");
1647                 }
1648                 return $self->setup_rpc_proxy("$path/rpc_proxy", $self->{vars}->{dc});
1649         } elsif ($envname eq "vampire_dc") {
1650                 if (not defined($self->{vars}->{dc})) {
1651                         $self->setup_dc("$path/dc");
1652                 }
1653                 return $self->setup_vampire_dc("$path/vampire_dc", $self->{vars}->{dc});
1654         } elsif ($envname eq "promoted_dc") {
1655                 if (not defined($self->{vars}->{dc})) {
1656                         $self->setup_dc("$path/dc");
1657                 }
1658                 return $self->setup_promoted_dc("$path/promoted_dc", $self->{vars}->{dc});
1659         } elsif ($envname eq "subdom_dc") {
1660                 if (not defined($self->{vars}->{dc})) {
1661                         $self->setup_dc("$path/dc");
1662                 }
1663                 return $self->setup_subdom_dc("$path/subdom_dc", $self->{vars}->{dc});
1664         } elsif ($envname eq "s4member") {
1665                 if (not defined($self->{vars}->{dc})) {
1666                         $self->setup_dc("$path/dc");
1667                 }
1668                 return $self->setup_member("$path/s4member", $self->{vars}->{dc});
1669         } elsif ($envname eq "rodc") {
1670                 if (not defined($self->{vars}->{dc})) {
1671                         $self->setup_dc("$path/dc");
1672                 }
1673                 return $self->setup_rodc("$path/rodc", $self->{vars}->{dc});
1674         } elsif ($envname eq "chgdcpass") {
1675                 return $self->setup_chgdcpass("$path/chgdcpass", $self->{vars}->{chgdcpass});
1676         } elsif ($envname eq "s3member") {
1677                 if (not defined($self->{vars}->{dc})) {
1678                         $self->setup_dc("$path/dc");
1679                 }
1680                 return $target3->setup_admember("$path/s3member", $self->{vars}->{dc}, 29);
1681         } elsif ($envname eq "plugin_s4_dc") {
1682                 return $self->setup_plugin_s4_dc("$path/plugin_s4_dc");
1683         } elsif ($envname eq "s3member_rfc2307") {
1684                 if (not defined($self->{vars}->{dc})) {
1685                         $self->setup_dc("$path/dc");
1686                 }
1687                 return $target3->setup_admember_rfc2307("$path/s3member_rfc2307",
1688                                                         $self->{vars}->{dc}, 34);
1689         } else {
1690                 return "UNKNOWN";
1691         }
1692 }
1693
1694 sub setup_member($$$)
1695 {
1696         my ($self, $path, $dc_vars) = @_;
1697
1698         my $env = $self->provision_member($path, $dc_vars);
1699
1700         if (defined $env) {
1701                 $self->check_or_start($env, "single");
1702
1703                 $self->wait_for_start($env);
1704
1705                 $self->{vars}->{member} = $env;
1706         }
1707
1708         return $env;
1709 }
1710
1711 sub setup_rpc_proxy($$$)
1712 {
1713         my ($self, $path, $dc_vars) = @_;
1714
1715         my $env = $self->provision_rpc_proxy($path, $dc_vars);
1716
1717         if (defined $env) {
1718                 $self->check_or_start($env, "single");
1719
1720                 $self->wait_for_start($env);
1721
1722                 $self->{vars}->{rpc_proxy} = $env;
1723         }
1724         return $env;
1725 }
1726
1727 sub setup_dc($$)
1728 {
1729         my ($self, $path) = @_;
1730
1731         my $env = $self->provision_dc($path);
1732         if (defined $env) {
1733                 $self->check_or_start($env, "standard");
1734
1735                 $self->wait_for_start($env);
1736
1737                 $self->{vars}->{dc} = $env;
1738         }
1739         return $env;
1740 }
1741
1742 sub setup_chgdcpass($$)
1743 {
1744         my ($self, $path) = @_;
1745
1746         my $env = $self->provision_chgdcpass($path);
1747         if (defined $env) {
1748                 $self->check_or_start($env, "single");
1749
1750                 $self->wait_for_start($env);
1751
1752                 $self->{vars}->{chgdcpass} = $env;
1753         }
1754         return $env;
1755 }
1756
1757 sub setup_fl2000dc($$)
1758 {
1759         my ($self, $path) = @_;
1760
1761         my $env = $self->provision_fl2000dc($path);
1762         if (defined $env) {
1763                 $self->check_or_start($env, "single");
1764
1765                 $self->wait_for_start($env);
1766
1767                 $self->{vars}->{fl2000dc} = $env;
1768         }
1769
1770         return $env;
1771 }
1772
1773 sub setup_fl2003dc($$)
1774 {
1775         my ($self, $path) = @_;
1776
1777         my $env = $self->provision_fl2003dc($path);
1778
1779         if (defined $env) {
1780                 $self->check_or_start($env, "single");
1781
1782                 $self->wait_for_start($env);
1783
1784                 $self->{vars}->{fl2003dc} = $env;
1785         }
1786         return $env;
1787 }
1788
1789 sub setup_fl2008r2dc($$)
1790 {
1791         my ($self, $path) = @_;
1792
1793         my $env = $self->provision_fl2008r2dc($path);
1794
1795         if (defined $env) {
1796                 $self->check_or_start($env, "single");
1797
1798                 $self->wait_for_start($env);
1799
1800                 $self->{vars}->{fl2008r2dc} = $env;
1801         }
1802
1803         return $env;
1804 }
1805
1806 sub setup_vampire_dc($$$)
1807 {
1808         my ($self, $path, $dc_vars) = @_;
1809
1810         my $env = $self->provision_vampire_dc($path, $dc_vars);
1811
1812         if (defined $env) {
1813                 $self->check_or_start($env, "single");
1814
1815                 $self->wait_for_start($env);
1816
1817                 $self->{vars}->{vampire_dc} = $env;
1818
1819                 # force replicated DC to update repsTo/repsFrom
1820                 # for vampired partitions
1821                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1822                 my $cmd = "";
1823                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1824                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1825                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1826                 $cmd .= " $env->{CONFIGURATION}";
1827                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1828                 unless (system($cmd) == 0) {
1829                         warn("Failed to exec kcc\n$cmd");
1830                         return undef;
1831                 }
1832
1833                 # as 'vampired' dc may add data in its local replica
1834                 # we need to synchronize data between DCs
1835                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1836                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1837                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1838                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
1839                 $cmd .= " $dc_vars->{CONFIGURATION}";
1840                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1841                 # replicate Configuration NC
1842                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
1843                 unless(system($cmd_repl) == 0) {
1844                         warn("Failed to replicate\n$cmd_repl");
1845                         return undef;
1846                 }
1847                 # replicate Default NC
1848                 $cmd_repl = "$cmd \"$base_dn\"";
1849                 unless(system($cmd_repl) == 0) {
1850                         warn("Failed to replicate\n$cmd_repl");
1851                         return undef;
1852                 }
1853         }
1854
1855         return $env;
1856 }
1857
1858 sub setup_promoted_dc($$$)
1859 {
1860         my ($self, $path, $dc_vars) = @_;
1861
1862         my $env = $self->provision_promoted_dc($path, $dc_vars);
1863
1864         if (defined $env) {
1865                 $self->check_or_start($env, "single");
1866
1867                 $self->wait_for_start($env);
1868
1869                 $self->{vars}->{promoted_dc} = $env;
1870
1871                 # force replicated DC to update repsTo/repsFrom
1872                 # for vampired partitions
1873                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1874                 my $cmd = "";
1875                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1876                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1877                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1878                 $cmd .= " $env->{CONFIGURATION}";
1879                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1880                 unless (system($cmd) == 0) {
1881                         warn("Failed to exec kcc\n$cmd");
1882                         return undef;
1883                 }
1884
1885                 # as 'vampired' dc may add data in its local replica
1886                 # we need to synchronize data between DCs
1887                 my $base_dn = "DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1888                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1889                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1890                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SERVER}";
1891                 $cmd .= " $dc_vars->{CONFIGURATION}";
1892                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD}";
1893                 # replicate Configuration NC
1894                 my $cmd_repl = "$cmd \"CN=Configuration,$base_dn\"";
1895                 unless(system($cmd_repl) == 0) {
1896                         warn("Failed to replicate\n$cmd_repl");
1897                         return undef;
1898                 }
1899                 # replicate Default NC
1900                 $cmd_repl = "$cmd \"$base_dn\"";
1901                 unless(system($cmd_repl) == 0) {
1902                         warn("Failed to replicate\n$cmd_repl");
1903                         return undef;
1904                 }
1905         }
1906
1907         return $env;
1908 }
1909
1910 sub setup_subdom_dc($$$)
1911 {
1912         my ($self, $path, $dc_vars) = @_;
1913
1914         my $env = $self->provision_subdom_dc($path, $dc_vars);
1915
1916         if (defined $env) {
1917                 $self->check_or_start($env, "single");
1918
1919                 $self->wait_for_start($env);
1920
1921                 $self->{vars}->{subdom_dc} = $env;
1922
1923                 # force replicated DC to update repsTo/repsFrom
1924                 # for primary domain partitions
1925                 my $samba_tool =  Samba::bindir_path($self, "samba-tool");
1926                 my $cmd = "";
1927                 $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1928                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1929                 $cmd .= " $samba_tool drs kcc $env->{DC_SERVER}";
1930                 $cmd .= " $env->{CONFIGURATION}";
1931                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
1932                 unless (system($cmd) == 0) {
1933                         warn("Failed to exec kcc\n$cmd");
1934                         return undef;
1935                 }
1936
1937                 # as 'subdomain' dc may add data in its local replica
1938                 # we need to synchronize data between DCs
1939                 my $base_dn = "DC=".join(",DC=", split(/\./, $env->{REALM}));
1940                 my $config_dn = "CN=Configuration,DC=".join(",DC=", split(/\./, $dc_vars->{REALM}));
1941                 $cmd = "SOCKET_WRAPPER_DEFAULT_IFACE=\"$env->{SOCKET_WRAPPER_DEFAULT_IFACE}\"";
1942                 $cmd .= " KRB5_CONFIG=\"$env->{KRB5_CONFIG}\"";
1943                 $cmd .= " $samba_tool drs replicate $env->{DC_SERVER} $env->{SUBDOM_DC_SERVER}";
1944                 $cmd .= " $dc_vars->{CONFIGURATION}";
1945                 $cmd .= " -U$dc_vars->{DC_USERNAME}\%$dc_vars->{DC_PASSWORD} --realm=$dc_vars->{DC_REALM}";
1946                 # replicate Configuration NC
1947                 my $cmd_repl = "$cmd \"$config_dn\"";
1948                 unless(system($cmd_repl) == 0) {
1949                         warn("Failed to replicate\n$cmd_repl");
1950                         return undef;
1951                 }
1952                 # replicate Default NC
1953                 $cmd_repl = "$cmd \"$base_dn\"";
1954                 unless(system($cmd_repl) == 0) {
1955                         warn("Failed to replicate\n$cmd_repl");
1956                         return undef;
1957                 }
1958         }
1959
1960         return $env;
1961 }
1962
1963 sub setup_rodc($$$)
1964 {
1965         my ($self, $path, $dc_vars) = @_;
1966
1967         my $env = $self->provision_rodc($path, $dc_vars);
1968
1969         unless ($env) {
1970                 return undef;
1971         }
1972
1973         $self->check_or_start($env, "single");
1974
1975         $self->wait_for_start($env);
1976
1977         $self->{vars}->{rodc} = $env;
1978
1979         return $env;
1980 }
1981
1982 sub setup_plugin_s4_dc($$)
1983 {
1984         my ($self, $path) = @_;
1985
1986         # If we didn't build with ADS, pretend this env was never available
1987         if (not $self->{target3}->have_ads()) {
1988                return "UNKNOWN";
1989         }
1990
1991         my $env = $self->provision_plugin_s4_dc($path);
1992         unless ($env) {
1993                 return undef;
1994         }
1995
1996         $self->check_or_start($env, "single");
1997         
1998         $self->wait_for_start($env);
1999         
2000         $self->{vars}->{plugin_s4_dc} = $env;
2001         return $env;
2002 }
2003
2004 1;