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