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