selftest: generate a ramdon domain sid during provision and export as SAMSID/[TRUST_...
[nivanova/samba-autobuild/.git] / selftest / target / Samba3.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 Samba3;
7
8 use strict;
9 use Cwd qw(abs_path);
10 use FindBin qw($RealBin);
11 use POSIX;
12 use target::Samba;
13 use File::Path 'remove_tree';
14
15 sub have_ads($) {
16         my ($self) = @_;
17         my $found_ads = 0;
18         my $smbd_build_options = Samba::bindir_path($self, "smbd") . " -b|";
19         open(IN, $smbd_build_options) or die("Unable to run $smbd_build_options: $!");
20
21         while (<IN>) {
22                 if (/WITH_ADS/) {
23                        $found_ads = 1;
24                 }
25         }
26         close IN;
27
28         # If we were not built with ADS support, pretend we were never even available
29         print "smbd does not have ADS support\n" unless $found_ads;
30         return $found_ads;
31 }
32
33 # return smb.conf parameters applicable to @path, based on the underlying
34 # filesystem type
35 sub get_fs_specific_conf($$)
36 {
37         my ($self, $path) = @_;
38         my $mods = "";
39         my $stat_out = `stat --file-system $path` or return "";
40
41         if ($stat_out =~ m/Type:\s+btrfs/) {
42                 $mods .= "streams_xattr btrfs";
43         }
44
45         if ($mods) {
46                 return "vfs objects = $mods";
47         }
48
49         return undef;
50 }
51
52 sub new($$) {
53         my ($classname, $bindir, $srcdir, $server_maxtime) = @_;
54         my $self = { vars => {},
55                      bindir => $bindir,
56                      srcdir => $srcdir,
57                      server_maxtime => $server_maxtime
58         };
59         bless $self;
60         return $self;
61 }
62
63 sub teardown_env($$)
64 {
65         my ($self, $envvars) = @_;
66         my $count = 0;
67
68         # This should cause smbd to terminate gracefully
69         close($envvars->{STDIN_PIPE});
70
71         my $smbdpid = $envvars->{SMBD_TL_PID};
72         my $nmbdpid = $envvars->{NMBD_TL_PID};
73         my $winbinddpid = $envvars->{WINBINDD_TL_PID};
74
75         # This should give it time to write out the gcov data
76         until ($count > 20) {
77             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
78             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
79             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
80             if ($smbdchild == -1
81                 && $nmbdchild == -1
82                 && $winbinddchild == -1) {
83                 last;
84             }
85             sleep(1);
86             $count++;
87         }
88
89         if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
90             return;
91         }
92
93         $self->stop_sig_term($smbdpid);
94         $self->stop_sig_term($nmbdpid);
95         $self->stop_sig_term($winbinddpid);
96
97         $count = 0;
98         until ($count > 10) {
99             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
100             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
101             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
102             if ($smbdchild == -1
103                 && $nmbdchild == -1
104                 && $winbinddchild == -1) {
105                 last;
106             }
107             sleep(1);
108             $count++;
109         }
110
111         if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
112             return;
113         }
114
115         warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
116         $self->stop_sig_kill($smbdpid);
117         $self->stop_sig_kill($nmbdpid);
118         $self->stop_sig_kill($winbinddpid);
119
120         return 0;
121 }
122
123 sub getlog_env_app($$$)
124 {
125         my ($self, $envvars, $name) = @_;
126
127         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
128         my $out = $title;
129
130         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
131
132         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
133         while (<LOG>) {
134                 $out .= $_;
135         }
136         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
137         close(LOG);
138
139         return "" if $out eq $title;
140  
141         return $out;
142 }
143
144 sub getlog_env($$)
145 {
146         my ($self, $envvars) = @_;
147         my $ret = "";
148
149         $ret .= $self->getlog_env_app($envvars, "SMBD");
150         $ret .= $self->getlog_env_app($envvars, "NMBD");
151         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
152
153         return $ret;
154 }
155
156 sub check_env($$)
157 {
158         my ($self, $envvars) = @_;
159
160         my $childpid = waitpid(-1, WNOHANG);
161
162         # TODO ...
163         return 1;
164 }
165
166 # Declare the environments Samba3 makes available.
167 # To be set up, they will be called as
168 #   samba3->setup_$envname($self, $path, $dep_1_vars, $dep_2_vars, ...)
169 %Samba3::ENV_DEPS = (
170         # name              => [dep_1, dep_2, ...],
171         nt4_dc              => [],
172         nt4_dc_schannel     => [],
173
174         simpleserver        => [],
175         fileserver          => [],
176         maptoguest          => [],
177         ktest               => [],
178
179         nt4_member          => ["nt4_dc"],
180
181         ad_member           => ["ad_dc"],
182         ad_member_rfc2307   => ["ad_dc_ntvfs"],
183         ad_member_idmap_rid => ["ad_dc"],
184         ad_member_idmap_ad  => ["ad_dc"],
185 );
186
187 sub setup_nt4_dc
188 {
189         my ($self, $path) = @_;
190
191         print "PROVISIONING NT4 DC...";
192
193         my $nt4_dc_options = "
194         domain master = yes
195         domain logons = yes
196         lanman auth = yes
197         ntlm auth = yes
198         raw NTLMv2 auth = yes
199         server schannel = auto
200
201         rpc_server:epmapper = external
202         rpc_server:spoolss = external
203         rpc_server:lsarpc = external
204         rpc_server:samr = external
205         rpc_server:netlogon = external
206         rpc_server:register_embedded_np = yes
207         rpc_server:FssagentRpc = external
208
209         rpc_daemon:epmd = fork
210         rpc_daemon:spoolssd = fork
211         rpc_daemon:lsasd = fork
212         rpc_daemon:fssd = fork
213         fss: sequence timeout = 1
214         check parent directory delete on close = yes
215 ";
216
217         my $vars = $self->provision($path, "SAMBA-TEST",
218                                     "LOCALNT4DC2",
219                                     "localntdc2pass",
220                                     $nt4_dc_options);
221
222         $vars or return undef;
223
224         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
225                return undef;
226         }
227
228         $vars->{DOMSID} = $vars->{SAMSID};
229         $vars->{DC_SERVER} = $vars->{SERVER};
230         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
231         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
232         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
233         $vars->{DC_USERNAME} = $vars->{USERNAME};
234         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
235
236         return $vars;
237 }
238
239 sub setup_nt4_dc_schannel
240 {
241         my ($self, $path) = @_;
242
243         print "PROVISIONING NT4 DC WITH SERVER SCHANNEL ...";
244
245         my $pdc_options = "
246         domain master = yes
247         domain logons = yes
248         lanman auth = yes
249
250         rpc_server:epmapper = external
251         rpc_server:spoolss = external
252         rpc_server:lsarpc = external
253         rpc_server:samr = external
254         rpc_server:netlogon = external
255         rpc_server:register_embedded_np = yes
256
257         rpc_daemon:epmd = fork
258         rpc_daemon:spoolssd = fork
259         rpc_daemon:lsasd = fork
260
261         server schannel = yes
262         # used to reproduce bug #12772
263         server max protocol = SMB2_02
264 ";
265
266         my $vars = $self->provision($path, "NT4SCHANNEL",
267                                     "LOCALNT4DC9",
268                                     "localntdc9pass",
269                                     $pdc_options);
270
271         $vars or return undef;
272
273         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
274                return undef;
275         }
276
277         $vars->{DOMSID} = $vars->{SAMSID};
278         $vars->{DC_SERVER} = $vars->{SERVER};
279         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
280         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
281         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
282         $vars->{DC_USERNAME} = $vars->{USERNAME};
283         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
284
285         return $vars;
286 }
287
288 sub setup_nt4_member
289 {
290         my ($self, $prefix, $nt4_dc_vars) = @_;
291         my $count = 0;
292         my $rc;
293
294         print "PROVISIONING MEMBER...";
295
296         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
297         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
298
299         my $member_options = "
300         security = domain
301         dbwrap_tdb_mutexes:* = yes
302         ${require_mutexes}
303 ";
304         my $ret = $self->provision($prefix, $nt4_dc_vars->{DOMAIN},
305                                    "LOCALNT4MEMBER3",
306                                    "localnt4member3pass",
307                                    $member_options);
308
309         $ret or return undef;
310
311         my $nmblookup = Samba::bindir_path($self, "nmblookup");
312         do {
313                 print "Waiting for the LOGON SERVER registration ...\n";
314                 $rc = system("$nmblookup $ret->{CONFIGURATION} $ret->{DOMAIN}\#1c");
315                 if ($rc != 0) {
316                         sleep(1);
317                 }
318                 $count++;
319         } while ($rc != 0 && $count < 10);
320         if ($count == 10) {
321                 print "NMBD not reachable after 10 retries\n";
322                 teardown_env($self, $ret);
323                 return 0;
324         }
325
326         my $net = Samba::bindir_path($self, "net");
327         my $cmd = "";
328         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
329         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
330         $cmd .= "$net rpc join $ret->{CONFIGURATION} $nt4_dc_vars->{DOMAIN} member";
331         $cmd .= " -U$nt4_dc_vars->{USERNAME}\%$nt4_dc_vars->{PASSWORD}";
332
333         if (system($cmd) != 0) {
334             warn("Join failed\n$cmd");
335             return undef;
336         }
337
338         my $cmd = "";
339         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
340         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
341         $cmd .= "$net $ret->{CONFIGURATION} primarytrust dumpinfo | grep -q 'REDACTED SECRET VALUES'";
342
343         if (system($cmd) != 0) {
344             warn("check failed\n$cmd");
345             return undef;
346         }
347
348         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
349                return undef;
350         }
351
352         $ret->{DOMSID} = $nt4_dc_vars->{DOMSID};
353         $ret->{DC_SERVER} = $nt4_dc_vars->{SERVER};
354         $ret->{DC_SERVER_IP} = $nt4_dc_vars->{SERVER_IP};
355         $ret->{DC_SERVER_IPV6} = $nt4_dc_vars->{SERVER_IPV6};
356         $ret->{DC_NETBIOSNAME} = $nt4_dc_vars->{NETBIOSNAME};
357         $ret->{DC_USERNAME} = $nt4_dc_vars->{USERNAME};
358         $ret->{DC_PASSWORD} = $nt4_dc_vars->{PASSWORD};
359
360         return $ret;
361 }
362
363 sub setup_ad_member
364 {
365         my ($self, $prefix, $dcvars) = @_;
366
367         my $prefix_abs = abs_path($prefix);
368         my @dirs = ();
369
370         # If we didn't build with ADS, pretend this env was never available
371         if (not $self->have_ads()) {
372                 return "UNKNOWN";
373         }
374
375         print "PROVISIONING S3 AD MEMBER...";
376
377         mkdir($prefix_abs, 0777);
378
379         my $share_dir="$prefix_abs/share";
380         push(@dirs, $share_dir);
381
382         my $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}";
383         push(@dirs, $substitution_path);
384
385         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/U_alice";
386         push(@dirs, $substitution_path);
387
388         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/U_alice/G_domain users";
389         push(@dirs, $substitution_path);
390
391         # Using '/' as the winbind separator is a bad idea ...
392         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}";
393         push(@dirs, $substitution_path);
394
395         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}/alice";
396         push(@dirs, $substitution_path);
397
398         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}/alice/g_$dcvars->{DOMAIN}";
399         push(@dirs, $substitution_path);
400
401         $substitution_path = "$share_dir/D_$dcvars->{DOMAIN}/u_$dcvars->{DOMAIN}/alice/g_$dcvars->{DOMAIN}/domain users";
402         push(@dirs, $substitution_path);
403
404         my $member_options = "
405         security = ads
406         workgroup = $dcvars->{DOMAIN}
407         realm = $dcvars->{REALM}
408         netbios aliases = foo bar
409         template homedir = /home/%D/%G/%U
410
411 [sub_dug]
412         path = $share_dir/D_%D/U_%U/G_%G
413         writeable = yes
414
415 [sub_dug2]
416         path = $share_dir/D_%D/u_%u/g_%g
417         writeable = yes
418
419 ";
420
421         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
422                                    "LOCALADMEMBER",
423                                    "loCalMemberPass",
424                                    $member_options,
425                                    $dcvars->{SERVER_IP},
426                                    $dcvars->{SERVER_IPV6});
427
428         $ret or return undef;
429
430         mkdir($_, 0777) foreach(@dirs);
431
432         close(USERMAP);
433         $ret->{DOMAIN} = $dcvars->{DOMAIN};
434         $ret->{REALM} = $dcvars->{REALM};
435         $ret->{DOMSID} = $dcvars->{DOMSID};
436
437         my $ctx;
438         $ctx = {};
439         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
440         $ctx->{domain} = $dcvars->{DOMAIN};
441         $ctx->{realm} = $dcvars->{REALM};
442         $ctx->{dnsname} = lc($dcvars->{REALM});
443         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
444         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
445         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
446         Samba::mk_krb5_conf($ctx, "");
447
448         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
449
450         my $net = Samba::bindir_path($self, "net");
451         my $cmd = "";
452         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
453         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
454                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
455         } else {
456                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
457         }
458         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
459         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
460         $cmd .= "$net join $ret->{CONFIGURATION}";
461         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
462
463         if (system($cmd) != 0) {
464             warn("Join failed\n$cmd");
465             return undef;
466         }
467
468         # We need world access to this share, as otherwise the domain
469         # administrator from the AD domain provided by Samba4 can't
470         # access the share for tests.
471         chmod 0777, "$prefix/share";
472
473         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
474                 return undef;
475         }
476
477         $ret->{DC_SERVER} = $dcvars->{SERVER};
478         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
479         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
480         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
481         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
482         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
483
484         return $ret;
485 }
486
487 sub setup_ad_member_rfc2307
488 {
489         my ($self, $prefix, $dcvars) = @_;
490
491         # If we didn't build with ADS, pretend this env was never available
492         if (not $self->have_ads()) {
493                 return "UNKNOWN";
494         }
495
496         print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
497
498         my $member_options = "
499         security = ads
500         workgroup = $dcvars->{DOMAIN}
501         realm = $dcvars->{REALM}
502         idmap cache time = 0
503         idmap negative cache time = 0
504         idmap config * : backend = autorid
505         idmap config * : range = 1000000-1999999
506         idmap config * : rangesize = 100000
507         idmap config $dcvars->{DOMAIN} : backend = rfc2307
508         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
509         idmap config $dcvars->{DOMAIN} : ldap_server = ad
510         idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
511         idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
512
513         password server = $dcvars->{SERVER}
514 ";
515
516         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
517                                    "RFC2307MEMBER",
518                                    "loCalMemberPass",
519                                    $member_options,
520                                    $dcvars->{SERVER_IP},
521                                    $dcvars->{SERVER_IPV6});
522
523         $ret or return undef;
524
525         close(USERMAP);
526         $ret->{DOMAIN} = $dcvars->{DOMAIN};
527         $ret->{REALM} = $dcvars->{REALM};
528         $ret->{DOMSID} = $dcvars->{DOMSID};
529
530         my $ctx;
531         my $prefix_abs = abs_path($prefix);
532         $ctx = {};
533         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
534         $ctx->{domain} = $dcvars->{DOMAIN};
535         $ctx->{realm} = $dcvars->{REALM};
536         $ctx->{dnsname} = lc($dcvars->{REALM});
537         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
538         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
539         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
540         Samba::mk_krb5_conf($ctx, "");
541
542         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
543
544         my $net = Samba::bindir_path($self, "net");
545         my $cmd = "";
546         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
547         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
548                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
549         } else {
550                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
551         }
552         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
553         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
554         $cmd .= "$net join $ret->{CONFIGURATION}";
555         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
556
557         if (system($cmd) != 0) {
558             warn("Join failed\n$cmd");
559             return undef;
560         }
561
562         # We need world access to this share, as otherwise the domain
563         # administrator from the AD domain provided by Samba4 can't
564         # access the share for tests.
565         chmod 0777, "$prefix/share";
566
567         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
568                 return undef;
569         }
570
571         $ret->{DC_SERVER} = $dcvars->{SERVER};
572         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
573         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
574         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
575         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
576         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
577
578         return $ret;
579 }
580
581 sub setup_ad_member_idmap_rid
582 {
583         my ($self, $prefix, $dcvars) = @_;
584
585         # If we didn't build with ADS, pretend this env was never available
586         if (not $self->have_ads()) {
587                 return "UNKNOWN";
588         }
589
590         print "PROVISIONING S3 AD MEMBER WITH idmap_rid config...";
591
592         my $member_options = "
593         security = ads
594         workgroup = $dcvars->{DOMAIN}
595         realm = $dcvars->{REALM}
596         idmap config * : backend = tdb
597         idmap config * : range = 1000000-1999999
598         idmap config $dcvars->{DOMAIN} : backend = rid
599         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
600 ";
601
602         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
603                                    "IDMAPRIDMEMBER",
604                                    "loCalMemberPass",
605                                    $member_options,
606                                    $dcvars->{SERVER_IP},
607                                    $dcvars->{SERVER_IPV6});
608
609         $ret or return undef;
610
611         close(USERMAP);
612         $ret->{DOMAIN} = $dcvars->{DOMAIN};
613         $ret->{REALM} = $dcvars->{REALM};
614         $ret->{DOMSID} = $dcvars->{DOMSID};
615
616         my $ctx;
617         my $prefix_abs = abs_path($prefix);
618         $ctx = {};
619         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
620         $ctx->{domain} = $dcvars->{DOMAIN};
621         $ctx->{realm} = $dcvars->{REALM};
622         $ctx->{dnsname} = lc($dcvars->{REALM});
623         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
624         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
625         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
626         Samba::mk_krb5_conf($ctx, "");
627
628         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
629
630         my $net = Samba::bindir_path($self, "net");
631         my $cmd = "";
632         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
633         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
634                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
635         } else {
636                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
637         }
638         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
639         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
640         $cmd .= "$net join $ret->{CONFIGURATION}";
641         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
642
643         if (system($cmd) != 0) {
644             warn("Join failed\n$cmd");
645             return undef;
646         }
647
648         # We need world access to this share, as otherwise the domain
649         # administrator from the AD domain provided by Samba4 can't
650         # access the share for tests.
651         chmod 0777, "$prefix/share";
652
653         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
654                 return undef;
655         }
656
657         $ret->{DC_SERVER} = $dcvars->{SERVER};
658         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
659         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
660         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
661         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
662         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
663
664         return $ret;
665 }
666
667 sub setup_ad_member_idmap_ad
668 {
669         my ($self, $prefix, $dcvars) = @_;
670
671         # If we didn't build with ADS, pretend this env was never available
672         if (not $self->have_ads()) {
673                 return "UNKNOWN";
674         }
675
676         print "PROVISIONING S3 AD MEMBER WITH idmap_ad config...";
677
678         my $member_options = "
679         security = ads
680         workgroup = $dcvars->{DOMAIN}
681         realm = $dcvars->{REALM}
682         password server = $dcvars->{SERVER}
683         idmap config * : backend = tdb
684         idmap config * : range = 1000000-1999999
685         idmap config $dcvars->{DOMAIN} : backend = ad
686         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
687 ";
688
689         my $ret = $self->provision($prefix, $dcvars->{DOMAIN},
690                                    "IDMAPADMEMBER",
691                                    "loCalMemberPass",
692                                    $member_options,
693                                    $dcvars->{SERVER_IP},
694                                    $dcvars->{SERVER_IPV6});
695
696         $ret or return undef;
697
698         close(USERMAP);
699         $ret->{DOMAIN} = $dcvars->{DOMAIN};
700         $ret->{REALM} = $dcvars->{REALM};
701         $ret->{DOMSID} = $dcvars->{DOMSID};
702
703         my $ctx;
704         my $prefix_abs = abs_path($prefix);
705         $ctx = {};
706         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
707         $ctx->{domain} = $dcvars->{DOMAIN};
708         $ctx->{realm} = $dcvars->{REALM};
709         $ctx->{dnsname} = lc($dcvars->{REALM});
710         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
711         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
712         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
713         Samba::mk_krb5_conf($ctx, "");
714
715         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
716
717         my $net = Samba::bindir_path($self, "net");
718         my $cmd = "";
719         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
720         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
721                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
722         } else {
723                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
724         }
725         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
726         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
727         $cmd .= "$net join $ret->{CONFIGURATION}";
728         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
729
730         if (system($cmd) != 0) {
731             warn("Join failed\n$cmd");
732             return undef;
733         }
734
735         # We need world access to this share, as otherwise the domain
736         # administrator from the AD domain provided by Samba4 can't
737         # access the share for tests.
738         chmod 0777, "$prefix/share";
739
740         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
741                 return undef;
742         }
743
744         $ret->{DC_SERVER} = $dcvars->{SERVER};
745         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
746         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
747         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
748         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
749         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
750
751         return $ret;
752 }
753
754 sub setup_simpleserver
755 {
756         my ($self, $path) = @_;
757
758         print "PROVISIONING simple server...";
759
760         my $prefix_abs = abs_path($path);
761
762         my $simpleserver_options = "
763         lanman auth = yes
764         ntlm auth = yes
765         vfs objects = xattr_tdb streams_depot time_audit full_audit
766         change notify = no
767         smb encrypt = off
768
769         full_audit:syslog = no
770         full_audit:success = none
771         full_audit:failure = none
772
773 [vfs_aio_fork]
774         path = $prefix_abs/share
775         vfs objects = aio_fork
776         read only = no
777         vfs_aio_fork:erratic_testing_mode=yes
778
779 [dosmode]
780         path = $prefix_abs/share
781         vfs objects =
782         store dos attributes = yes
783         hide files = /hidefile/
784         hide dot files = yes
785
786 [enc_desired]
787         path = $prefix_abs/share
788         vfs objects =
789         smb encrypt = desired
790 ";
791
792         my $vars = $self->provision($path, "WORKGROUP",
793                                     "LOCALSHARE4",
794                                     "local4pass",
795                                     $simpleserver_options);
796
797         $vars or return undef;
798
799         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
800                return undef;
801         }
802
803         return $vars;
804 }
805
806 sub setup_fileserver
807 {
808         my ($self, $path) = @_;
809         my $prefix_abs = abs_path($path);
810         my $srcdir_abs = abs_path($self->{srcdir});
811
812         print "PROVISIONING file server ...\n";
813
814         my @dirs = ();
815
816         mkdir($prefix_abs, 0777);
817
818         my $usershare_dir="$prefix_abs/lib/usershare";
819
820         mkdir("$prefix_abs/lib", 0755);
821         remove_tree($usershare_dir);
822         mkdir($usershare_dir, 01770);
823
824         my $share_dir="$prefix_abs/share";
825
826         # Create share directory structure
827         my $lower_case_share_dir="$share_dir/lower-case";
828         push(@dirs, $lower_case_share_dir);
829
830         my $lower_case_share_dir_30000="$share_dir/lower-case-30000";
831         push(@dirs, $lower_case_share_dir_30000);
832
833         my $dfree_share_dir="$share_dir/dfree";
834         push(@dirs, $dfree_share_dir);
835         push(@dirs, "$dfree_share_dir/subdir1");
836         push(@dirs, "$dfree_share_dir/subdir2");
837         push(@dirs, "$dfree_share_dir/subdir3");
838
839         my $valid_users_sharedir="$share_dir/valid_users";
840         push(@dirs,$valid_users_sharedir);
841
842         my $offline_sharedir="$share_dir/offline";
843         push(@dirs,$offline_sharedir);
844
845         my $force_user_valid_users_dir = "$share_dir/force_user_valid_users";
846         push(@dirs, $force_user_valid_users_dir);
847
848         my $smbget_sharedir="$share_dir/smbget";
849         push(@dirs,$smbget_sharedir);
850
851         my $tarmode_sharedir="$share_dir/tarmode";
852         push(@dirs,$tarmode_sharedir);
853
854         my $usershare_sharedir="$share_dir/usershares";
855         push(@dirs,$usershare_sharedir);
856
857         my $fileserver_options = "
858         kernel change notify = yes
859
860         usershare path = $usershare_dir
861         usershare max shares = 10
862         usershare allow guests = yes
863         usershare prefix allow list = $usershare_sharedir
864
865 [lowercase]
866         path = $lower_case_share_dir
867         comment = smb username is [%U]
868         case sensitive = True
869         default case = lower
870         preserve case = no
871         short preserve case = no
872 [lowercase-30000]
873         path = $lower_case_share_dir_30000
874         comment = smb username is [%U]
875         case sensitive = True
876         default case = lower
877         preserve case = no
878         short preserve case = no
879 [dfree]
880         path = $dfree_share_dir
881         comment = smb username is [%U]
882         dfree command = $srcdir_abs/testprogs/blackbox/dfree.sh
883 [valid-users-access]
884         path = $valid_users_sharedir
885         valid users = +userdup
886 [offline]
887         path = $offline_sharedir
888         vfs objects = offline
889
890 # BUG: https://bugzilla.samba.org/show_bug.cgi?id=9878
891 # RH BUG: https://bugzilla.redhat.com/show_bug.cgi?id=1077651
892 [force_user_valid_users]
893         path = $force_user_valid_users_dir
894         comment = force user with valid users combination test share
895         valid users = +force_user
896         force user = force_user
897         force group = everyone
898         write list = force_user
899
900 [smbget]
901         path = $smbget_sharedir
902         comment = smb username is [%U]
903         guest ok = yes
904 [ign_sysacls]
905         path = $share_dir
906         comment = ignore system acls
907         acl_xattr:ignore system acls = yes
908 [inherit_owner]
909         path = $share_dir
910         comment = inherit owner
911         inherit owner = yes
912 [inherit_owner_u]
913         path = $share_dir
914         comment = inherit only unix owner
915         inherit owner = unix only
916         acl_xattr:ignore system acls = yes
917 ";
918
919         my $vars = $self->provision($path, "WORKGROUP",
920                                     "FILESERVER",
921                                     "fileserver",
922                                     $fileserver_options,
923                                     undef,
924                                     undef,
925                                     1);
926
927         $vars or return undef;
928
929         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
930                return undef;
931         }
932
933
934         mkdir($_, 0777) foreach(@dirs);
935
936         ## Create case sensitive lower case share dir
937         foreach my $file ('a'..'z') {
938                 my $full_path = $lower_case_share_dir . '/' . $file;
939                 open my $fh, '>', $full_path;
940                 # Add some content to file
941                 print $fh $full_path;
942                 close $fh;
943         }
944
945         for (my $file = 1; $file < 51; ++$file) {
946                 my $full_path = $lower_case_share_dir . '/' . $file;
947                 open my $fh, '>', $full_path;
948                 # Add some content to file
949                 print $fh $full_path;
950                 close $fh;
951         }
952
953         # Create content for 30000 share
954         foreach my $file ('a'..'z') {
955                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
956                 open my $fh, '>', $full_path;
957                 # Add some content to file
958                 print $fh $full_path;
959                 close $fh;
960         }
961
962         for (my $file = 1; $file < 30001; ++$file) {
963                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
964                 open my $fh, '>', $full_path;
965                 # Add some content to file
966                 print $fh $full_path;
967                 close $fh;
968         }
969
970         ##
971         ## create a listable file in valid_users_share
972         ##
973         my $valid_users_target = "$valid_users_sharedir/foo";
974         unless (open(VALID_USERS_TARGET, ">$valid_users_target")) {
975                 warn("Unable to open $valid_users_target");
976                 return undef;
977         }
978         close(VALID_USERS_TARGET);
979         chmod 0644, $valid_users_target;
980
981         return $vars;
982 }
983
984 sub setup_ktest
985 {
986         my ($self, $prefix) = @_;
987
988         # If we didn't build with ADS, pretend this env was never available
989         if (not $self->have_ads()) {
990                 return "UNKNOWN";
991         }
992
993         print "PROVISIONING server with security=ads...";
994
995         my $ktest_options = "
996         workgroup = KTEST
997         realm = ktest.samba.example.com
998         security = ads
999         username map = $prefix/lib/username.map
1000         server signing = required
1001         server min protocol = SMB3_00
1002         client max protocol = SMB3
1003
1004         # This disables NTLM auth against the local SAM, which
1005         # we use can then test this setting by.
1006         ntlm auth = disabled
1007 ";
1008
1009         my $ret = $self->provision($prefix, "KTEST",
1010                                    "LOCALKTEST6",
1011                                    "localktest6pass",
1012                                    $ktest_options);
1013
1014         $ret or return undef;
1015
1016         my $ctx;
1017         my $prefix_abs = abs_path($prefix);
1018         $ctx = {};
1019         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
1020         $ctx->{domain} = "KTEST";
1021         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
1022         $ctx->{dnsname} = lc($ctx->{realm});
1023         $ctx->{kdc_ipv4} = "0.0.0.0";
1024         $ctx->{kdc_ipv6} = "::";
1025         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
1026         Samba::mk_krb5_conf($ctx, "");
1027
1028         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
1029
1030         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
1031         print USERMAP "
1032 $ret->{USERNAME} = KTEST\\Administrator
1033 ";
1034         close(USERMAP);
1035
1036 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
1037 #Samba4 DC with the same parameters as are being used here.  The
1038 #domain SID is S-1-5-21-1071277805-689288055-3486227160
1039         $ret->{SAMSID} = "S-1-5-21-1911091480-1468226576-2729736297";
1040         $ret->{DOMSID} = "S-1-5-21-1071277805-689288055-3486227160";
1041
1042         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
1043         chmod 0600, "$prefix/private/secrets.tdb";
1044
1045 #Make sure there's no old ntdb file.
1046         system("rm -f $prefix/private/secrets.ntdb");
1047
1048 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
1049 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
1050 #
1051 #and having in krb5.conf:
1052 # ticket_lifetime = 799718400
1053 # renew_lifetime = 799718400
1054 #
1055 # The commands for the -2 keytab where were:
1056 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
1057 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
1058 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
1059 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
1060 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
1061 #
1062 # and then for the -3 keytab, I did
1063 #
1064 # net changetrustpw; kdestroy and the same again.
1065 #
1066 # This creates a credential cache with a very long lifetime (2036 at
1067 # at 2011-04), and shows that running 'net changetrustpw' does not
1068 # break existing logins (for the secrets.tdb method at least).
1069 #
1070
1071         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
1072
1073         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
1074         chmod 0600, "$prefix/krb5_ccache-2";
1075
1076         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
1077         chmod 0600, "$prefix/krb5_ccache-3";
1078
1079         # We need world access to this share, as otherwise the domain
1080         # administrator from the AD domain provided by ktest can't
1081         # access the share for tests.
1082         chmod 0777, "$prefix/share";
1083
1084         if (not $self->check_or_start($ret, "yes", "no", "yes")) {
1085                return undef;
1086         }
1087         return $ret;
1088 }
1089
1090 sub setup_maptoguest
1091 {
1092         my ($self, $path) = @_;
1093
1094         print "PROVISIONING maptoguest...";
1095
1096         my $options = "
1097 map to guest = bad user
1098 ntlm auth = yes
1099 ";
1100
1101         my $vars = $self->provision($path, "WORKGROUP",
1102                                     "maptoguest",
1103                                     "maptoguestpass",
1104                                     $options);
1105
1106         $vars or return undef;
1107
1108         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
1109                return undef;
1110         }
1111
1112         return $vars;
1113 }
1114
1115 sub stop_sig_term($$) {
1116         my ($self, $pid) = @_;
1117         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
1118 }
1119
1120 sub stop_sig_kill($$) {
1121         my ($self, $pid) = @_;
1122         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
1123 }
1124
1125 sub write_pid($$$)
1126 {
1127         my ($env_vars, $app, $pid) = @_;
1128
1129         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
1130         print PID $pid;
1131         close(PID);
1132 }
1133
1134 sub read_pid($$)
1135 {
1136         my ($env_vars, $app) = @_;
1137
1138         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
1139         my $pid = <PID>;
1140         close(PID);
1141         return $pid;
1142 }
1143
1144 sub check_or_start($$$$$) {
1145         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
1146
1147         # use a pipe for stdin in the child processes. This allows
1148         # those processes to monitor the pipe for EOF to ensure they
1149         # exit when the test script exits
1150         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
1151
1152         unlink($env_vars->{NMBD_TEST_LOG});
1153         print "STARTING NMBD...";
1154         my $pid = fork();
1155         if ($pid == 0) {
1156                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
1157                 open STDERR, '>&STDOUT';
1158
1159                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1160
1161                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1162                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.nmbd";
1163                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1164                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1165
1166                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1167                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1168                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1169                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1170                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1171                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1172                 $ENV{UID_WRAPPER_ROOT} = "1";
1173
1174                 $ENV{ENVNAME} = "$ENV{ENVNAME}.nmbd";
1175
1176                 if ($nmbd ne "yes") {
1177                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1178                                 my $signame = shift;
1179                                 print("Skip nmbd received signal $signame");
1180                                 exit 0;
1181                         };
1182                         sleep($self->{server_maxtime});
1183                         exit 0;
1184                 }
1185
1186                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
1187                 my @optargs = ("-d0");
1188                 if (defined($ENV{NMBD_OPTIONS})) {
1189                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
1190                 }
1191                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1192                 if(defined($ENV{NMBD_VALGRIND})) { 
1193                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
1194                 }
1195                 my @args = ("-F", "--no-process-group",
1196                             "-s", $env_vars->{SERVERCONFFILE},
1197                             "-l", $env_vars->{LOGDIR});
1198                 if (not defined($ENV{NMBD_DONT_LOG_STDOUT})) {
1199                         push(@args, "--log-stdout");
1200                 }
1201
1202                 close($env_vars->{STDIN_PIPE});
1203                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1204
1205                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1206                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1207         }
1208         $env_vars->{NMBD_TL_PID} = $pid;
1209         write_pid($env_vars, "nmbd", $pid);
1210         print "DONE\n";
1211
1212         unlink($env_vars->{WINBINDD_TEST_LOG});
1213         print "STARTING WINBINDD...";
1214         $pid = fork();
1215         if ($pid == 0) {
1216                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
1217                 open STDERR, '>&STDOUT';
1218
1219                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1220
1221                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1222                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.winbindd";
1223                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1224                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1225
1226                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1227                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1228                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1229                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1230                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1231                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1232                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
1233                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
1234                 } else {
1235                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
1236                 }
1237                 $ENV{UID_WRAPPER_ROOT} = "1";
1238
1239                 $ENV{ENVNAME} = "$ENV{ENVNAME}.winbindd";
1240
1241                 if ($winbindd ne "yes") {
1242                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1243                                 my $signame = shift;
1244                                 print("Skip winbindd received signal $signame");
1245                                 exit 0;
1246                         };
1247                         sleep($self->{server_maxtime});
1248                         exit 0;
1249                 }
1250
1251                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
1252                 my @optargs = ("-d0");
1253                 if (defined($ENV{WINBINDD_OPTIONS})) {
1254                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
1255                 }
1256                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1257                 if(defined($ENV{WINBINDD_VALGRIND})) {
1258                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
1259                 }
1260                 my @args = ("-F", "--no-process-group",
1261                             "-s", $env_vars->{SERVERCONFFILE},
1262                             "-l", $env_vars->{LOGDIR});
1263                 if (not defined($ENV{WINBINDD_DONT_LOG_STDOUT})) {
1264                         push(@args, "--stdout");
1265                 }
1266
1267                 close($env_vars->{STDIN_PIPE});
1268                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1269
1270                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1271                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1272         }
1273         $env_vars->{WINBINDD_TL_PID} = $pid;
1274         write_pid($env_vars, "winbindd", $pid);
1275         print "DONE\n";
1276
1277         unlink($env_vars->{SMBD_TEST_LOG});
1278         print "STARTING SMBD...";
1279         $pid = fork();
1280         if ($pid == 0) {
1281                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
1282                 open STDERR, '>&STDOUT';
1283
1284                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1285
1286                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1287                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.smbd";
1288                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1289                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1290
1291                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1292                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1293                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1294                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1295                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1296                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1297                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
1298                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
1299                 } else {
1300                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
1301                 }
1302                 $ENV{UID_WRAPPER_ROOT} = "1";
1303
1304                 $ENV{ENVNAME} = "$ENV{ENVNAME}.smbd";
1305
1306                 if ($smbd ne "yes") {
1307                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1308                                 my $signame = shift;
1309                                 print("Skip smbd received signal $signame");
1310                                 exit 0;
1311                         };
1312                         sleep($self->{server_maxtime});
1313                         exit 0;
1314                 }
1315
1316                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
1317                 my @optargs = ("-d0");
1318                 if (defined($ENV{SMBD_OPTIONS})) {
1319                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
1320                 }
1321                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1322                 if(defined($ENV{SMBD_VALGRIND})) {
1323                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
1324                 }
1325                 my @args = ("-F", "--no-process-group",
1326                             "-s", $env_vars->{SERVERCONFFILE},
1327                             "-l", $env_vars->{LOGDIR});
1328                 if (not defined($ENV{SMBD_DONT_LOG_STDOUT})) {
1329                         push(@args, "--log-stdout");
1330                 }
1331
1332                 close($env_vars->{STDIN_PIPE});
1333                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1334
1335                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1336                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1337         }
1338         $env_vars->{SMBD_TL_PID} = $pid;
1339         write_pid($env_vars, "smbd", $pid);
1340         print "DONE\n";
1341
1342         close(STDIN_READER);
1343
1344         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
1345 }
1346
1347 sub createuser($$$$$)
1348 {
1349         my ($self, $username, $password, $conffile, $env) = @_;
1350         my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $username > /dev/null";
1351
1352         keys %$env;
1353         while(my($var, $val) = each %$env) {
1354                 $cmd = "$var=\"$val\" $cmd";
1355         }
1356
1357         unless (open(PWD, "|$cmd")) {
1358             warn("Unable to set password for $username account\n$cmd");
1359             return undef;
1360         }
1361         print PWD "$password\n$password\n";
1362         unless (close(PWD)) {
1363             warn("Unable to set password for $username account\n$cmd");
1364             return undef;
1365         }
1366 }
1367
1368 sub provision($$$$$$$$$)
1369 {
1370         my ($self, $prefix, $domain, $server, $password, $extra_options, $dc_server_ip, $dc_server_ipv6, $no_delete_prefix) = @_;
1371
1372         ##
1373         ## setup the various environment variables we need
1374         ##
1375
1376         my $samsid = Samba::random_domain_sid();
1377         my $swiface = Samba::get_interface($server);
1378         my %ret = ();
1379         my %createuser_env = ();
1380         my $server_ip = "127.0.0.$swiface";
1381         my $server_ipv6 = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
1382
1383         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
1384         chomp $unix_name;
1385         my $unix_uid = $>;
1386         my $unix_gids_str = $);
1387         my @unix_gids = split(" ", $unix_gids_str);
1388
1389         my $prefix_abs = abs_path($prefix);
1390         my $bindir_abs = abs_path($self->{bindir});
1391
1392         my @dirs = ();
1393
1394         my $shrdir="$prefix_abs/share";
1395         push(@dirs,$shrdir);
1396
1397         my $libdir="$prefix_abs/lib";
1398         push(@dirs,$libdir);
1399
1400         my $piddir="$prefix_abs/pid";
1401         push(@dirs,$piddir);
1402
1403         my $privatedir="$prefix_abs/private";
1404         push(@dirs,$privatedir);
1405
1406         my $binddnsdir = "$prefix_abs/bind-dns";
1407         push(@dirs, $binddnsdir);
1408
1409         my $lockdir="$prefix_abs/lockdir";
1410         push(@dirs,$lockdir);
1411
1412         my $eventlogdir="$prefix_abs/lockdir/eventlog";
1413         push(@dirs,$eventlogdir);
1414
1415         my $logdir="$prefix_abs/logs";
1416         push(@dirs,$logdir);
1417
1418         my $driver32dir="$shrdir/W32X86";
1419         push(@dirs,$driver32dir);
1420
1421         my $driver64dir="$shrdir/x64";
1422         push(@dirs,$driver64dir);
1423
1424         my $driver40dir="$shrdir/WIN40";
1425         push(@dirs,$driver40dir);
1426
1427         my $ro_shrdir="$shrdir/root-tmp";
1428         push(@dirs,$ro_shrdir);
1429
1430         my $msdfs_shrdir="$shrdir/msdfsshare";
1431         push(@dirs,$msdfs_shrdir);
1432
1433         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
1434         push(@dirs,$msdfs_deeppath);
1435
1436         my $badnames_shrdir="$shrdir/badnames";
1437         push(@dirs,$badnames_shrdir);
1438
1439         my $lease1_shrdir="$shrdir/SMB2_10";
1440         push(@dirs,$lease1_shrdir);
1441
1442         my $lease2_shrdir="$shrdir/SMB3_00";
1443         push(@dirs,$lease2_shrdir);
1444
1445         my $manglenames_shrdir="$shrdir/manglenames";
1446         push(@dirs,$manglenames_shrdir);
1447
1448         my $widelinks_shrdir="$shrdir/widelinks";
1449         push(@dirs,$widelinks_shrdir);
1450
1451         my $widelinks_linkdir="$shrdir/widelinks_foo";
1452         push(@dirs,$widelinks_linkdir);
1453
1454         my $shadow_tstdir="$shrdir/shadow";
1455         push(@dirs,$shadow_tstdir);
1456         my $shadow_mntdir="$shadow_tstdir/mount";
1457         push(@dirs,$shadow_mntdir);
1458         my $shadow_basedir="$shadow_mntdir/base";
1459         push(@dirs,$shadow_basedir);
1460         my $shadow_shrdir="$shadow_basedir/share";
1461         push(@dirs,$shadow_shrdir);
1462
1463         my $nosymlinks_shrdir="$shrdir/nosymlinks";
1464         push(@dirs,$nosymlinks_shrdir);
1465
1466         my $local_symlinks_shrdir="$shrdir/local_symlinks";
1467         push(@dirs,$local_symlinks_shrdir);
1468
1469         # this gets autocreated by winbindd
1470         my $wbsockdir="$prefix_abs/winbindd";
1471
1472         my $nmbdsockdir="$prefix_abs/nmbd";
1473         unlink($nmbdsockdir);
1474
1475         ## 
1476         ## create the test directory layout
1477         ##
1478         die ("prefix_abs = ''") if $prefix_abs eq "";
1479         die ("prefix_abs = '/'") if $prefix_abs eq "/";
1480
1481         mkdir($prefix_abs, 0777);
1482         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
1483         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
1484             system("rm -rf $prefix_abs/*");
1485         }
1486         mkdir($_, 0777) foreach(@dirs);
1487
1488         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
1489
1490         ##
1491         ## lockdir and piddir must be 0755
1492         ##
1493         chmod 0755, $lockdir;
1494         chmod 0755, $piddir;
1495
1496
1497         ##
1498         ## create ro and msdfs share layout
1499         ##
1500
1501         chmod 0755, $ro_shrdir;
1502         my $unreadable_file = "$ro_shrdir/unreadable_file";
1503         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
1504                 warn("Unable to open $unreadable_file");
1505                 return undef;
1506         }
1507         close(UNREADABLE_FILE);
1508         chmod 0600, $unreadable_file;
1509
1510         my $msdfs_target = "$ro_shrdir/msdfs-target";
1511         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
1512                 warn("Unable to open $msdfs_target");
1513                 return undef;
1514         }
1515         close(MSDFS_TARGET);
1516         chmod 0666, $msdfs_target;
1517         symlink "msdfs:$server_ip\\ro-tmp,$server_ipv6\\ro-tmp",
1518                 "$msdfs_shrdir/msdfs-src1";
1519         symlink "msdfs:$server_ipv6\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
1520
1521         ##
1522         ## create bad names in $badnames_shrdir
1523         ##
1524         ## (An invalid name, would be mangled to 8.3).
1525         my $badname_target = "$badnames_shrdir/\340|\231\216\377\177";
1526         unless (open(BADNAME_TARGET, ">$badname_target")) {
1527                 warn("Unable to open $badname_target");
1528                 return undef;
1529         }
1530         close(BADNAME_TARGET);
1531         chmod 0666, $badname_target;
1532
1533         ## (A bad name, would not be mangled to 8.3).
1534         my $badname_target = "$badnames_shrdir/\240\276\346\327\377\177";
1535         unless (open(BADNAME_TARGET, ">$badname_target")) {
1536                 warn("Unable to open $badname_target");
1537                 return undef;
1538         }
1539         close(BADNAME_TARGET);
1540         chmod 0666, $badname_target;
1541
1542         ## (A bad good name).
1543         my $badname_target = "$badnames_shrdir/blank.txt";
1544         unless (open(BADNAME_TARGET, ">$badname_target")) {
1545                 warn("Unable to open $badname_target");
1546                 return undef;
1547         }
1548         close(BADNAME_TARGET);
1549         chmod 0666, $badname_target;
1550
1551         ##
1552         ## create mangleable directory names in $manglenames_shrdir
1553         ##
1554         my $manglename_target = "$manglenames_shrdir/foo:bar";
1555         mkdir($manglename_target, 0777);
1556
1557         ##
1558         ## create symlinks for widelinks tests.
1559         ##
1560         my $widelinks_target = "$widelinks_linkdir/target";
1561         unless (open(WIDELINKS_TARGET, ">$widelinks_target")) {
1562                 warn("Unable to open $widelinks_target");
1563                 return undef;
1564         }
1565         close(WIDELINKS_TARGET);
1566         chmod 0666, $widelinks_target;
1567         ##
1568         ## This link should get ACCESS_DENIED
1569         ##
1570         symlink "$widelinks_target", "$widelinks_shrdir/source";
1571         ##
1572         ## This link should be allowed
1573         ##
1574         symlink "$widelinks_shrdir", "$widelinks_shrdir/dot";
1575
1576         my $conffile="$libdir/server.conf";
1577         my $dfqconffile="$libdir/dfq.conf";
1578
1579         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/third_party/nss_wrapper/nss_wrapper.pl";
1580         my $nss_wrapper_passwd = "$privatedir/passwd";
1581         my $nss_wrapper_group = "$privatedir/group";
1582         my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
1583         my $resolv_conf = "$privatedir/resolv.conf";
1584         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
1585
1586         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
1587
1588         my $fake_snap_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/fake_snap.pl";
1589
1590         my @eventlog_list = ("dns server", "application");
1591
1592         ##
1593         ## calculate uids and gids
1594         ##
1595
1596         my ($max_uid, $max_gid);
1597         my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2, $uid_userdup);
1598         my ($uid_pdbtest_wkn);
1599         my ($uid_smbget);
1600         my ($uid_force_user);
1601         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
1602         my ($gid_userdup, $gid_everyone);
1603         my ($gid_force_user);
1604         my ($uid_user1);
1605         my ($uid_user2);
1606
1607         if ($unix_uid < 0xffff - 10) {
1608                 $max_uid = 0xffff;
1609         } else {
1610                 $max_uid = $unix_uid;
1611         }
1612
1613         $uid_root = $max_uid - 1;
1614         $uid_nobody = $max_uid - 2;
1615         $uid_pdbtest = $max_uid - 3;
1616         $uid_pdbtest2 = $max_uid - 4;
1617         $uid_userdup = $max_uid - 5;
1618         $uid_pdbtest_wkn = $max_uid - 6;
1619         $uid_force_user = $max_uid - 7;
1620         $uid_smbget = $max_uid - 8;
1621         $uid_user1 = $max_uid - 9;
1622         $uid_user2 = $max_uid - 10;
1623
1624         if ($unix_gids[0] < 0xffff - 8) {
1625                 $max_gid = 0xffff;
1626         } else {
1627                 $max_gid = $unix_gids[0];
1628         }
1629
1630         $gid_nobody = $max_gid - 1;
1631         $gid_nogroup = $max_gid - 2;
1632         $gid_root = $max_gid - 3;
1633         $gid_domusers = $max_gid - 4;
1634         $gid_domadmins = $max_gid - 5;
1635         $gid_userdup = $max_gid - 6;
1636         $gid_everyone = $max_gid - 7;
1637         $gid_force_user = $max_gid - 8;
1638
1639         ##
1640         ## create conffile
1641         ##
1642
1643         unless (open(CONF, ">$conffile")) {
1644                 warn("Unable to open $conffile");
1645                 return undef;
1646         }
1647         print CONF "
1648 [global]
1649         netbios name = $server
1650         interfaces = $server_ip/8 $server_ipv6/64
1651         bind interfaces only = yes
1652         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
1653         smbd:suicide mode = yes
1654
1655         workgroup = $domain
1656
1657         private dir = $privatedir
1658         binddns dir = $binddnsdir
1659         pid directory = $piddir
1660         lock directory = $lockdir
1661         log file = $logdir/log.\%m
1662         log level = 1
1663         debug pid = yes
1664         max log size = 0
1665
1666         state directory = $lockdir
1667         cache directory = $lockdir
1668
1669         passdb backend = tdbsam
1670
1671         time server = yes
1672
1673         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1674         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
1675         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1676         add user to group script =      $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type member --action add --member %u --name %g --group_path $nss_wrapper_group
1677         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
1678         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
1679         delete user from group script = $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type member --action delete --member %u --name %g --group_path $nss_wrapper_group
1680
1681         addprinter command =            $mod_printer_pl -a -s $conffile --
1682         deleteprinter command =         $mod_printer_pl -d -s $conffile --
1683
1684         eventlog list = application \"dns server\"
1685
1686         kernel oplocks = no
1687         kernel change notify = no
1688
1689         logging = file
1690         printing = bsd
1691         printcap name = /dev/null
1692
1693         winbindd socket directory = $wbsockdir
1694         nmbd:socket dir = $nmbdsockdir
1695         idmap config * : range = 100000-200000
1696         winbind enum users = yes
1697         winbind enum groups = yes
1698         winbind separator = /
1699         include system krb5 conf = no
1700
1701 #       min receivefile size = 4000
1702
1703         read only = no
1704
1705         smbd:sharedelay = 100000
1706         smbd:writetimeupdatedelay = 500000
1707         map hidden = no
1708         map system = no
1709         map readonly = no
1710         store dos attributes = yes
1711         create mask = 755
1712         dos filemode = yes
1713         strict rename = yes
1714         strict sync = yes
1715         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
1716
1717         printing = vlp
1718         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1719         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1720         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1721         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1722         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1723         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1724         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1725         lpq cache time = 0
1726         print notify backchannel = yes
1727
1728         ncalrpc dir = $prefix_abs/ncalrpc
1729
1730         # The samba3.blackbox.smbclient_s3 test uses this to test that
1731         # sending messages works, and that the %m sub works.
1732         message command = mv %s $shrdir/message.%m
1733
1734         # fsrvp server requires registry shares
1735         registry shares = yes
1736
1737         # Used by RPC SRVSVC tests
1738         add share command = $bindir_abs/smbaddshare
1739         change share command = $bindir_abs/smbchangeshare
1740         delete share command = $bindir_abs/smbdeleteshare
1741
1742         # fruit:copyfile is a global option
1743         fruit:copyfile = yes
1744
1745         #this does not mean that we use non-secure test env,
1746         #it just means we ALLOW one to be configured.
1747         allow insecure wide links = yes
1748
1749         # Begin extra options
1750         $extra_options
1751         # End extra options
1752
1753         #Include user defined custom parameters if set
1754 ";
1755
1756         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1757                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1758         }
1759
1760         print CONF "
1761 [tmp]
1762         path = $shrdir
1763         comment = smb username is [%U]
1764 [tmpsort]
1765         path = $shrdir
1766         comment = Load dirsort module
1767         vfs objects = dirsort acl_xattr fake_acls xattr_tdb streams_depot
1768 [tmpenc]
1769         path = $shrdir
1770         comment = encrypt smb username is [%U]
1771         smb encrypt = required
1772         vfs objects = dirsort
1773 [tmpguest]
1774         path = $shrdir
1775         guest ok = yes
1776 [guestonly]
1777         path = $shrdir
1778         guest only = yes
1779         guest ok = yes
1780 [forceuser]
1781         path = $shrdir
1782         force user = $unix_name
1783         guest ok = yes
1784 [forceuser_unixonly]
1785         comment = force a user with unix user SID and group SID
1786         path = $shrdir
1787         force user = pdbtest
1788         guest ok = yes
1789 [forceuser_wkngroup]
1790         comment = force a user with well-known group SID
1791         path = $shrdir
1792         force user = pdbtest_wkn
1793         guest ok = yes
1794 [forcegroup]
1795         path = $shrdir
1796         force group = nogroup
1797         guest ok = yes
1798 [ro-tmp]
1799         path = $ro_shrdir
1800         guest ok = yes
1801 [write-list-tmp]
1802         path = $shrdir
1803         read only = yes
1804         write list = $unix_name
1805 [valid-users-tmp]
1806         path = $shrdir
1807         valid users = $unix_name
1808         access based share enum = yes
1809 [msdfs-share]
1810         path = $msdfs_shrdir
1811         msdfs root = yes
1812         msdfs shuffle referrals = yes
1813         guest ok = yes
1814 [hideunread]
1815         copy = tmp
1816         hide unreadable = yes
1817 [tmpcase]
1818         copy = tmp
1819         case sensitive = yes
1820 [hideunwrite]
1821         copy = tmp
1822         hide unwriteable files = yes
1823 [durable]
1824         copy = tmp
1825         kernel share modes = no
1826         kernel oplocks = no
1827         posix locking = no
1828 [fs_specific]
1829         copy = tmp
1830         $fs_specific_conf
1831 [print1]
1832         copy = tmp
1833         printable = yes
1834
1835 [print2]
1836         copy = print1
1837 [print3]
1838         copy = print1
1839         default devmode = no
1840 [lp]
1841         copy = print1
1842
1843 [nfs4acl_simple_40]
1844         path = $shrdir
1845         comment = smb username is [%U]
1846         nfs4:mode = simple
1847         nfs4acl_xattr:version = 40
1848         vfs objects = nfs4acl_xattr xattr_tdb
1849
1850 [nfs4acl_special_40]
1851         path = $shrdir
1852         comment = smb username is [%U]
1853         nfs4:mode = special
1854         nfs4acl_xattr:version = 40
1855         vfs objects = nfs4acl_xattr xattr_tdb
1856
1857 [nfs4acl_simple_41]
1858         path = $shrdir
1859         comment = smb username is [%U]
1860         nfs4:mode = simple
1861         vfs objects = nfs4acl_xattr xattr_tdb
1862
1863 [nfs4acl_xdr_40]
1864         path = $shrdir
1865         comment = smb username is [%U]
1866         vfs objects = nfs4acl_xattr xattr_tdb
1867         nfs4:mode = simple
1868         nfs4acl_xattr:encoding = xdr
1869         nfs4acl_xattr:version = 40
1870
1871 [nfs4acl_xdr_41]
1872         path = $shrdir
1873         comment = smb username is [%U]
1874         vfs objects = nfs4acl_xattr xattr_tdb
1875         nfs4:mode = simple
1876         nfs4acl_xattr:encoding = xdr
1877         nfs4acl_xattr:version = 41
1878
1879 [xcopy_share]
1880         path = $shrdir
1881         comment = smb username is [%U]
1882         create mask = 777
1883         force create mode = 777
1884 [posix_share]
1885         path = $shrdir
1886         comment = smb username is [%U]
1887         create mask = 0777
1888         force create mode = 0
1889         directory mask = 0777
1890         force directory mode = 0
1891         vfs objects = xattr_tdb streams_depot
1892 [aio]
1893         copy = tmp
1894         aio read size = 1
1895         aio write size = 1
1896
1897 [print\$]
1898         copy = tmp
1899
1900 [vfs_fruit]
1901         path = $shrdir
1902         vfs objects = catia fruit streams_xattr acl_xattr xattr_tdb
1903         fruit:resource = file
1904         fruit:metadata = netatalk
1905         fruit:locking = netatalk
1906         fruit:encoding = native
1907         fruit:veto_appledouble = no
1908
1909 [vfs_fruit_xattr]
1910         path = $shrdir
1911         # This is used by vfs.fruit tests that require real fs xattr
1912         vfs objects = catia fruit streams_xattr acl_xattr
1913         fruit:resource = file
1914         fruit:metadata = netatalk
1915         fruit:locking = netatalk
1916         fruit:encoding = native
1917         fruit:veto_appledouble = no
1918
1919 [vfs_fruit_metadata_stream]
1920         path = $shrdir
1921         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
1922         fruit:resource = file
1923         fruit:metadata = stream
1924         fruit:veto_appledouble = no
1925
1926 [vfs_fruit_stream_depot]
1927         path = $shrdir
1928         vfs objects = fruit streams_depot acl_xattr xattr_tdb
1929         fruit:resource = stream
1930         fruit:metadata = stream
1931         fruit:veto_appledouble = no
1932
1933 [vfs_wo_fruit]
1934         path = $shrdir
1935         vfs objects = streams_xattr acl_xattr xattr_tdb
1936
1937 [vfs_wo_fruit_stream_depot]
1938         path = $shrdir
1939         vfs objects = streams_depot acl_xattr xattr_tdb
1940
1941 [vfs_fruit_timemachine]
1942         path = $shrdir
1943         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
1944         fruit:resource = file
1945         fruit:metadata = stream
1946         fruit:time machine = yes
1947         fruit:time machine max size = 32K
1948
1949 [badname-tmp]
1950         path = $badnames_shrdir
1951         guest ok = yes
1952
1953 [manglenames_share]
1954         path = $manglenames_shrdir
1955         guest ok = yes
1956
1957 [dynamic_share]
1958         path = $shrdir/%R
1959         guest ok = yes
1960
1961 [widelinks_share]
1962         path = $widelinks_shrdir
1963         wide links = no
1964         guest ok = yes
1965
1966 [fsrvp_share]
1967         path = $shrdir
1968         comment = fake shapshots using rsync
1969         vfs objects = shell_snap shadow_copy2
1970         shell_snap:check path command = $fake_snap_pl --check
1971         shell_snap:create command = $fake_snap_pl --create
1972         shell_snap:delete command = $fake_snap_pl --delete
1973         # a relative path here fails, the snapshot dir is no longer found
1974         shadow:snapdir = $shrdir/.snapshots
1975
1976 [shadow1]
1977         path = $shadow_shrdir
1978         comment = previous versions snapshots under mount point
1979         vfs objects = shadow_copy2
1980         shadow:mountpoint = $shadow_mntdir
1981
1982 [shadow2]
1983         path = $shadow_shrdir
1984         comment = previous versions snapshots outside mount point
1985         vfs objects = shadow_copy2
1986         shadow:mountpoint = $shadow_mntdir
1987         shadow:snapdir = $shadow_tstdir/.snapshots
1988
1989 [shadow3]
1990         path = $shadow_shrdir
1991         comment = previous versions with subvolume snapshots, snapshots under base dir
1992         vfs objects = shadow_copy2
1993         shadow:mountpoint = $shadow_mntdir
1994         shadow:basedir = $shadow_basedir
1995         shadow:snapdir = $shadow_basedir/.snapshots
1996
1997 [shadow4]
1998         path = $shadow_shrdir
1999         comment = previous versions with subvolume snapshots, snapshots outside mount point
2000         vfs objects = shadow_copy2
2001         shadow:mountpoint = $shadow_mntdir
2002         shadow:basedir = $shadow_basedir
2003         shadow:snapdir = $shadow_tstdir/.snapshots
2004
2005 [shadow5]
2006         path = $shadow_shrdir
2007         comment = previous versions at volume root snapshots under mount point
2008         vfs objects = shadow_copy2
2009         shadow:mountpoint = $shadow_shrdir
2010
2011 [shadow6]
2012         path = $shadow_shrdir
2013         comment = previous versions at volume root snapshots outside mount point
2014         vfs objects = shadow_copy2
2015         shadow:mountpoint = $shadow_shrdir
2016         shadow:snapdir = $shadow_tstdir/.snapshots
2017
2018 [shadow7]
2019         path = $shadow_shrdir
2020         comment = previous versions snapshots everywhere
2021         vfs objects = shadow_copy2
2022         shadow:mountpoint = $shadow_mntdir
2023         shadow:snapdirseverywhere = yes
2024
2025 [shadow8]
2026         path = $shadow_shrdir
2027         comment = previous versions using snapsharepath
2028         vfs objects = shadow_copy2
2029         shadow:mountpoint = $shadow_mntdir
2030         shadow:snapdir = $shadow_tstdir/.snapshots
2031         shadow:snapsharepath = share
2032
2033 [shadow_fmt0]
2034         comment = Testing shadow:format with default option
2035         vfs object = shadow_copy2
2036         path = $shadow_shrdir
2037         read only = no
2038         guest ok = yes
2039         shadow:mountpoint = $shadow_mntdir
2040         shadow:basedir = $shadow_basedir
2041         shadow:snapdir = $shadow_basedir/.snapshots
2042         shadow:format = \@GMT-%Y.%m.%d-%H.%M.%S
2043
2044 [shadow_fmt1]
2045         comment = Testing shadow:format with only date component
2046         vfs object = shadow_copy2
2047         path = $shadow_shrdir
2048         read only = no
2049         guest ok = yes
2050         shadow:mountpoint = $shadow_mntdir
2051         shadow:basedir = $shadow_basedir
2052         shadow:snapdir = $shadow_basedir/.snapshots
2053         shadow:format = \@GMT-%Y-%m-%d
2054
2055 [shadow_fmt2]
2056         comment = Testing shadow:format with some hardcoded prefix
2057         vfs object = shadow_copy2
2058         path = $shadow_shrdir
2059         read only = no
2060         guest ok = yes
2061         shadow:mountpoint = $shadow_mntdir
2062         shadow:basedir = $shadow_basedir
2063         shadow:snapdir = $shadow_basedir/.snapshots
2064         shadow:format = snap\@GMT-%Y.%m.%d-%H.%M.%S
2065
2066 [shadow_fmt3]
2067         comment = Testing shadow:format with modified format
2068         vfs object = shadow_copy2
2069         path = $shadow_shrdir
2070         read only = no
2071         guest ok = yes
2072         shadow:mountpoint = $shadow_mntdir
2073         shadow:basedir = $shadow_basedir
2074         shadow:snapdir = $shadow_basedir/.snapshots
2075         shadow:format = \@GMT-%Y.%m.%d-%H_%M_%S-snap
2076
2077 [shadow_fmt4]
2078         comment = Testing shadow:snapprefix regex
2079         vfs object = shadow_copy2
2080         path = $shadow_shrdir
2081         read only = no
2082         guest ok = yes
2083         shadow:mountpoint = $shadow_mntdir
2084         shadow:basedir = $shadow_basedir
2085         shadow:snapdir = $shadow_basedir/.snapshots
2086         shadow:snapprefix = \^s[a-z]*p\$
2087         shadow:format = _GMT-%Y.%m.%d-%H.%M.%S
2088
2089 [shadow_fmt5]
2090         comment = Testing shadow:snapprefix with delim regex
2091         vfs object = shadow_copy2
2092         path = $shadow_shrdir
2093         read only = no
2094         guest ok = yes
2095         shadow:mountpoint = $shadow_mntdir
2096         shadow:basedir = $shadow_basedir
2097         shadow:snapdir = $shadow_basedir/.snapshots
2098         shadow:delimiter = \@GMT
2099         shadow:snapprefix = [a-z]*
2100         shadow:format = \@GMT-%Y.%m.%d-%H.%M.%S
2101
2102 [shadow_wl]
2103         path = $shadow_shrdir
2104         comment = previous versions with wide links allowed
2105         vfs objects = shadow_copy2
2106         shadow:mountpoint = $shadow_mntdir
2107         wide links = yes
2108 [dfq]
2109         path = $shrdir/dfree
2110         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2111         admin users = $unix_name
2112         include = $dfqconffile
2113 [dfq_owner]
2114         path = $shrdir/dfree
2115         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2116         inherit owner = yes
2117         include = $dfqconffile
2118 [acl_xattr_ign_sysacl_posix]
2119         copy = tmp
2120         acl_xattr:ignore system acls = yes
2121         acl_xattr:default acl style = posix
2122 [acl_xattr_ign_sysacl_windows]
2123         copy = tmp
2124         acl_xattr:ignore system acls = yes
2125         acl_xattr:default acl style = windows
2126
2127 [mangle_illegal]
2128         copy = tmp
2129         mangled names = illegal
2130
2131 [nosymlinks]
2132         copy = tmp
2133         path = $nosymlinks_shrdir
2134         follow symlinks = no
2135
2136 [local_symlinks]
2137         copy = tmp
2138         path = $local_symlinks_shrdir
2139         follow symlinks = yes
2140
2141 [kernel_oplocks]
2142         copy = tmp
2143         kernel oplocks = yes
2144         vfs objects = streams_xattr xattr_tdb
2145
2146 [compound_find]
2147         copy = tmp
2148         smbd:find async delay usec = 10000
2149 [error_inject]
2150         copy = tmp
2151         vfs objects = error_inject
2152         include = $libdir/error_inject.conf
2153         ";
2154         close(CONF);
2155
2156         my $net = Samba::bindir_path($self, "net");
2157         my $cmd = "";
2158         $cmd .= "SMB_CONF_PATH=\"$conffile\" ";
2159         $cmd .= "$net setlocalsid $samsid";
2160
2161         if (system($cmd) != 0) {
2162             warn("Join failed\n$cmd");
2163             return undef;
2164         }
2165
2166         unless (open(DFQCONF, ">$dfqconffile")) {
2167                 warn("Unable to open $dfqconffile");
2168                 return undef;
2169         }
2170         close(DFQCONF);
2171
2172         ##
2173         ## create a test account
2174         ##
2175
2176         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
2177            warn("Unable to open $nss_wrapper_passwd");
2178            return undef;
2179         } 
2180         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
2181 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
2182 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
2183 pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
2184 userdup:x:$uid_userdup:$gid_userdup:userdup gecos:$prefix_abs:/bin/false
2185 pdbtest_wkn:x:$uid_pdbtest_wkn:$gid_everyone:pdbtest_wkn gecos:$prefix_abs:/bin/false
2186 force_user:x:$uid_force_user:$gid_force_user:force user gecos:$prefix_abs:/bin/false
2187 smbget_user:x:$uid_smbget:$gid_domusers:smbget_user gecos:$prefix_abs:/bin/false
2188 user1:x:$uid_user1:$gid_nogroup:user1 gecos:$prefix_abs:/bin/false
2189 user2:x:$uid_user2:$gid_nogroup:user2 gecos:$prefix_abs:/bin/false
2190 ";
2191         if ($unix_uid != 0) {
2192                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
2193 ";
2194         }
2195         close(PASSWD);
2196
2197         unless (open(GROUP, ">$nss_wrapper_group")) {
2198              warn("Unable to open $nss_wrapper_group");
2199              return undef;
2200         }
2201         print GROUP "nobody:x:$gid_nobody:
2202 nogroup:x:$gid_nogroup:nobody
2203 $unix_name-group:x:$unix_gids[0]:
2204 domusers:X:$gid_domusers:
2205 domadmins:X:$gid_domadmins:
2206 userdup:x:$gid_userdup:$unix_name
2207 everyone:x:$gid_everyone:
2208 force_user:x:$gid_force_user:
2209 ";
2210         if ($unix_gids[0] != 0) {
2211                 print GROUP "root:x:$gid_root:
2212 ";
2213         }
2214
2215         close(GROUP);
2216
2217         ## hosts
2218         my $hostname = lc($server);
2219         unless (open(HOSTS, ">>$nss_wrapper_hosts")) {
2220                 warn("Unable to open $nss_wrapper_hosts");
2221                 return undef;
2222         }
2223         print HOSTS "${server_ip} ${hostname}.samba.example.com ${hostname}\n";
2224         print HOSTS "${server_ipv6} ${hostname}.samba.example.com ${hostname}\n";
2225         close(HOSTS);
2226
2227         ## hosts
2228         unless (open(RESOLV_CONF, ">$resolv_conf")) {
2229                 warn("Unable to open $resolv_conf");
2230                 return undef;
2231         }
2232         if (defined($dc_server_ip) or defined($dc_server_ipv6)) {
2233                 if (defined($dc_server_ip)) {
2234                         print RESOLV_CONF "nameserver $dc_server_ip\n";
2235                 }
2236                 if (defined($dc_server_ipv6)) {
2237                         print RESOLV_CONF "nameserver $dc_server_ipv6\n";
2238                 }
2239         } else {
2240                 print RESOLV_CONF "nameserver ${server_ip}\n";
2241                 print RESOLV_CONF "nameserver ${server_ipv6}\n";
2242         }
2243         close(RESOLV_CONF);
2244
2245         foreach my $evlog (@eventlog_list) {
2246                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
2247                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
2248                 close(EVENTLOG);
2249         }
2250
2251         $createuser_env{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
2252         $createuser_env{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
2253         $createuser_env{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
2254         $createuser_env{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
2255         if ($ENV{SAMBA_DNS_FAKING}) {
2256                 $createuser_env{RESOLV_WRAPPER_CONF} = $resolv_conf;
2257         } else {
2258                 $createuser_env{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
2259         }
2260
2261         createuser($self, $unix_name, $password, $conffile, \%createuser_env) || die("Unable to create user");
2262         createuser($self, "force_user", $password, $conffile, \%createuser_env) || die("Unable to create force_user");
2263         createuser($self, "smbget_user", $password, $conffile, \%createuser_env) || die("Unable to create smbget_user");
2264         createuser($self, "user1", $password, $conffile, \%createuser_env) || die("Unable to create user1");
2265         createuser($self, "user2", $password, $conffile, \%createuser_env) || die("Unable to create user2");
2266
2267         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
2268         print DNS_UPDATE_LIST "A $server. $server_ip\n";
2269         print DNS_UPDATE_LIST "AAAA $server. $server_ipv6\n";
2270         close(DNS_UPDATE_LIST);
2271
2272         print "DONE\n";
2273
2274         $ret{SERVER_IP} = $server_ip;
2275         $ret{SERVER_IPV6} = $server_ipv6;
2276         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
2277         $ret{NMBD_TEST_LOG_POS} = 0;
2278         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
2279         $ret{WINBINDD_TEST_LOG_POS} = 0;
2280         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
2281         $ret{SMBD_TEST_LOG_POS} = 0;
2282         $ret{SERVERCONFFILE} = $conffile;
2283         $ret{CONFIGURATION} ="-s $conffile";
2284         $ret{LOCK_DIR} = $lockdir;
2285         $ret{SERVER} = $server;
2286         $ret{USERNAME} = $unix_name;
2287         $ret{USERID} = $unix_uid;
2288         $ret{DOMAIN} = $domain;
2289         $ret{SAMSID} = $samsid;
2290         $ret{NETBIOSNAME} = $server;
2291         $ret{PASSWORD} = $password;
2292         $ret{PIDDIR} = $piddir;
2293         $ret{SELFTEST_WINBINDD_SOCKET_DIR} = $wbsockdir;
2294         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
2295         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
2296         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
2297         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
2298         $ret{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
2299         $ret{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
2300         $ret{NSS_WRAPPER_MODULE_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
2301         $ret{NSS_WRAPPER_MODULE_FN_PREFIX} = "winbind";
2302         if ($ENV{SAMBA_DNS_FAKING}) {
2303                 $ret{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
2304         } else {
2305                 $ret{RESOLV_WRAPPER_CONF} = $resolv_conf;
2306         }
2307         $ret{LOCAL_PATH} = "$shrdir";
2308         $ret{LOGDIR} = $logdir;
2309
2310         #
2311         # Avoid hitting system krb5.conf -
2312         # An env that needs Kerberos will reset this to the real
2313         # value.
2314         #
2315         $ret{KRB5_CONFIG} = abs_path($prefix) . "/no_krb5.conf";
2316
2317         # Define KRB5CCNAME for each environment we set up
2318         $ret{KRB5_CCACHE} = abs_path($prefix) . "/krb5ccache";
2319         $ENV{KRB5CCNAME} = $ret{KRB5_CCACHE};
2320
2321         return \%ret;
2322 }
2323
2324 sub wait_for_start($$$$$)
2325 {
2326         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
2327         my $cmd;
2328         my $netcmd;
2329         my $ret;
2330
2331         if ($nmbd eq "yes") {
2332                 my $count = 0;
2333
2334                 # give time for nbt server to register its names
2335                 print "checking for nmbd\n";
2336
2337                 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
2338                 my $nmblookup = Samba::bindir_path($self, "nmblookup");
2339
2340                 do {
2341                         $ret = system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
2342                         if ($ret != 0) {
2343                                 sleep(1);
2344                         } else {
2345                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
2346                                 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
2347                                 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
2348                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
2349                         }
2350                         $count++;
2351                 } while ($ret != 0 && $count < 10);
2352                 if ($count == 10) {
2353                         print "NMBD not reachable after 10 retries\n";
2354                         teardown_env($self, $envvars);
2355                         return 0;
2356                 }
2357         }
2358
2359         if ($winbindd eq "yes") {
2360             print "checking for winbindd\n";
2361             my $count = 0;
2362             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2363             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2364             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2365             $cmd .= Samba::bindir_path($self, "wbinfo") . " --ping-dc";
2366
2367             do {
2368                 if ($ret != 0) {
2369                     $ret = system($cmd);
2370                     sleep(1);
2371                 }
2372                 $count++;
2373             } while ($ret != 0 && $count < 20);
2374             if ($count == 20) {
2375                 print "WINBINDD not reachable after 20 seconds\n";
2376                 teardown_env($self, $envvars);
2377                 return 0;
2378             }
2379         }
2380
2381         if ($smbd eq "yes") {
2382             # make sure smbd is also up set
2383             print "wait for smbd\n";
2384
2385             my $count = 0;
2386             do {
2387                 $ret = system(Samba::bindir_path($self, "smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
2388                 if ($ret != 0) {
2389                     sleep(1);
2390                 }
2391                 $count++
2392             } while ($ret != 0 && $count < 20);
2393             if ($count == 20) {
2394                 print "SMBD failed to start up in a reasonable time (20sec)\n";
2395                 teardown_env($self, $envvars);
2396                 return 0;
2397             }
2398         }
2399
2400         # Ensure we have domain users mapped.
2401         $netcmd = "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2402         $netcmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2403         $netcmd .= Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} ";
2404
2405         $cmd = $netcmd . "groupmap add rid=513 unixgroup=domusers type=domain";
2406         $ret = system($cmd);
2407         if ($ret != 0) {
2408                 print("\"$cmd\" failed\n");
2409                 return 1;
2410         }
2411
2412         $cmd = $netcmd . "groupmap add rid=512 unixgroup=domadmins type=domain";
2413         $ret = system($cmd);
2414         if ($ret != 0) {
2415                 print("\"$cmd\" failed\n");
2416                 return 1;
2417         }
2418
2419         $cmd = $netcmd . "groupmap add sid=S-1-1-0 unixgroup=everyone type=builtin";
2420         $ret = system($cmd);
2421         if ($ret != 0) {
2422                 print("\"$cmd\" failed\n");
2423                 return 1;
2424         }
2425
2426         # note: creating builtin groups requires winbindd for the
2427         # unix id allocator
2428         my $create_builtin_users = "no";
2429         if ($winbindd eq "yes") {
2430                 $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2431                 $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2432                 $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2433                 $cmd .= Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545";
2434                 my $wbinfo_out = qx($cmd 2>&1);
2435                 if ($? != 0) {
2436                         # wbinfo doesn't give us a better error code then
2437                         # WBC_ERR_DOMAIN_NOT_FOUND, but at least that's
2438                         # different then WBC_ERR_WINBIND_NOT_AVAILABLE
2439                         if ($wbinfo_out !~ /WBC_ERR_DOMAIN_NOT_FOUND/) {
2440                                 print("Failed to run \"wbinfo --sid-to-gid=S-1-5-32-545\": $wbinfo_out");
2441                                 teardown_env($self, $envvars);
2442                                 return 0;
2443                         }
2444                         $create_builtin_users = "yes";
2445                 }
2446         }
2447         if ($create_builtin_users eq "yes") {
2448             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2449             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2450             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2451             $cmd .= Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} ";
2452             $cmd .= "sam createbuiltingroup Users";
2453             $ret = system($cmd);
2454             if ($ret != 0) {
2455                 print "Failed to create BUILTIN\\Users group\n";
2456                 teardown_env($self, $envvars);
2457                 return 0;
2458             }
2459
2460             $cmd = Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} ";
2461             $cmd .= "cache del IDMAP/SID2XID/S-1-5-32-545";
2462             system($cmd);
2463
2464             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2465             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2466             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2467             $cmd .= Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545";
2468             $ret = system($cmd);
2469             if ($ret != 0) {
2470                 print "Missing \"BUILTIN\\Users\", did net sam createbuiltingroup Users fail?\n";
2471                 teardown_env($self, $envvars);
2472                 return 0;
2473             }
2474         }
2475
2476         print $self->getlog_env($envvars);
2477
2478         return 1;
2479 }
2480
2481 1;