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