selftest: Add dfq_cache share with 'dfree cache time' set
[kai/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_pthread]
774         path = $prefix_abs/share
775         read only = no
776         vfs objects = aio_pthread
777         aio_pthread:aio open = yes
778
779 [vfs_aio_fork]
780         path = $prefix_abs/share
781         vfs objects = aio_fork
782         read only = no
783         vfs_aio_fork:erratic_testing_mode=yes
784
785 [dosmode]
786         path = $prefix_abs/share
787         vfs objects =
788         store dos attributes = yes
789         hide files = /hidefile/
790         hide dot files = yes
791
792 [enc_desired]
793         path = $prefix_abs/share
794         vfs objects =
795         smb encrypt = desired
796 ";
797
798         my $vars = $self->provision($path, "WORKGROUP",
799                                     "LOCALSHARE4",
800                                     "local4pass",
801                                     $simpleserver_options);
802
803         $vars or return undef;
804
805         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
806                return undef;
807         }
808
809         return $vars;
810 }
811
812 sub setup_fileserver
813 {
814         my ($self, $path) = @_;
815         my $prefix_abs = abs_path($path);
816         my $srcdir_abs = abs_path($self->{srcdir});
817
818         print "PROVISIONING file server ...\n";
819
820         my @dirs = ();
821
822         mkdir($prefix_abs, 0777);
823
824         my $usershare_dir="$prefix_abs/lib/usershare";
825
826         mkdir("$prefix_abs/lib", 0755);
827         remove_tree($usershare_dir);
828         mkdir($usershare_dir, 01770);
829
830         my $share_dir="$prefix_abs/share";
831
832         # Create share directory structure
833         my $lower_case_share_dir="$share_dir/lower-case";
834         push(@dirs, $lower_case_share_dir);
835
836         my $lower_case_share_dir_30000="$share_dir/lower-case-30000";
837         push(@dirs, $lower_case_share_dir_30000);
838
839         my $dfree_share_dir="$share_dir/dfree";
840         push(@dirs, $dfree_share_dir);
841         push(@dirs, "$dfree_share_dir/subdir1");
842         push(@dirs, "$dfree_share_dir/subdir2");
843         push(@dirs, "$dfree_share_dir/subdir3");
844
845         my $valid_users_sharedir="$share_dir/valid_users";
846         push(@dirs,$valid_users_sharedir);
847
848         my $offline_sharedir="$share_dir/offline";
849         push(@dirs,$offline_sharedir);
850
851         my $force_user_valid_users_dir = "$share_dir/force_user_valid_users";
852         push(@dirs, $force_user_valid_users_dir);
853
854         my $smbget_sharedir="$share_dir/smbget";
855         push(@dirs,$smbget_sharedir);
856
857         my $tarmode_sharedir="$share_dir/tarmode";
858         push(@dirs,$tarmode_sharedir);
859
860         my $usershare_sharedir="$share_dir/usershares";
861         push(@dirs,$usershare_sharedir);
862
863         my $fileserver_options = "
864         kernel change notify = yes
865
866         usershare path = $usershare_dir
867         usershare max shares = 10
868         usershare allow guests = yes
869         usershare prefix allow list = $usershare_sharedir
870
871 [lowercase]
872         path = $lower_case_share_dir
873         comment = smb username is [%U]
874         case sensitive = True
875         default case = lower
876         preserve case = no
877         short preserve case = no
878 [lowercase-30000]
879         path = $lower_case_share_dir_30000
880         comment = smb username is [%U]
881         case sensitive = True
882         default case = lower
883         preserve case = no
884         short preserve case = no
885 [dfree]
886         path = $dfree_share_dir
887         comment = smb username is [%U]
888         dfree command = $srcdir_abs/testprogs/blackbox/dfree.sh
889 [valid-users-access]
890         path = $valid_users_sharedir
891         valid users = +userdup
892 [offline]
893         path = $offline_sharedir
894         vfs objects = offline
895
896 # BUG: https://bugzilla.samba.org/show_bug.cgi?id=9878
897 # RH BUG: https://bugzilla.redhat.com/show_bug.cgi?id=1077651
898 [force_user_valid_users]
899         path = $force_user_valid_users_dir
900         comment = force user with valid users combination test share
901         valid users = +force_user
902         force user = force_user
903         force group = everyone
904         write list = force_user
905
906 [smbget]
907         path = $smbget_sharedir
908         comment = smb username is [%U]
909         guest ok = yes
910 [ign_sysacls]
911         path = $share_dir
912         comment = ignore system acls
913         acl_xattr:ignore system acls = yes
914 [inherit_owner]
915         path = $share_dir
916         comment = inherit owner
917         inherit owner = yes
918 [inherit_owner_u]
919         path = $share_dir
920         comment = inherit only unix owner
921         inherit owner = unix only
922         acl_xattr:ignore system acls = yes
923 ";
924
925         my $vars = $self->provision($path, "WORKGROUP",
926                                     "FILESERVER",
927                                     "fileserver",
928                                     $fileserver_options,
929                                     undef,
930                                     undef,
931                                     1);
932
933         $vars or return undef;
934
935         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
936                return undef;
937         }
938
939
940         mkdir($_, 0777) foreach(@dirs);
941
942         ## Create case sensitive lower case share dir
943         foreach my $file ('a'..'z') {
944                 my $full_path = $lower_case_share_dir . '/' . $file;
945                 open my $fh, '>', $full_path;
946                 # Add some content to file
947                 print $fh $full_path;
948                 close $fh;
949         }
950
951         for (my $file = 1; $file < 51; ++$file) {
952                 my $full_path = $lower_case_share_dir . '/' . $file;
953                 open my $fh, '>', $full_path;
954                 # Add some content to file
955                 print $fh $full_path;
956                 close $fh;
957         }
958
959         # Create content for 30000 share
960         foreach my $file ('a'..'z') {
961                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
962                 open my $fh, '>', $full_path;
963                 # Add some content to file
964                 print $fh $full_path;
965                 close $fh;
966         }
967
968         for (my $file = 1; $file < 30001; ++$file) {
969                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
970                 open my $fh, '>', $full_path;
971                 # Add some content to file
972                 print $fh $full_path;
973                 close $fh;
974         }
975
976         ##
977         ## create a listable file in valid_users_share
978         ##
979         my $valid_users_target = "$valid_users_sharedir/foo";
980         unless (open(VALID_USERS_TARGET, ">$valid_users_target")) {
981                 warn("Unable to open $valid_users_target");
982                 return undef;
983         }
984         close(VALID_USERS_TARGET);
985         chmod 0644, $valid_users_target;
986
987         return $vars;
988 }
989
990 sub setup_ktest
991 {
992         my ($self, $prefix) = @_;
993
994         # If we didn't build with ADS, pretend this env was never available
995         if (not $self->have_ads()) {
996                 return "UNKNOWN";
997         }
998
999         print "PROVISIONING server with security=ads...";
1000
1001         my $ktest_options = "
1002         workgroup = KTEST
1003         realm = ktest.samba.example.com
1004         security = ads
1005         username map = $prefix/lib/username.map
1006         server signing = required
1007         server min protocol = SMB3_00
1008         client max protocol = SMB3
1009
1010         # This disables NTLM auth against the local SAM, which
1011         # we use can then test this setting by.
1012         ntlm auth = disabled
1013 ";
1014
1015         my $ret = $self->provision($prefix, "KTEST",
1016                                    "LOCALKTEST6",
1017                                    "localktest6pass",
1018                                    $ktest_options);
1019
1020         $ret or return undef;
1021
1022         my $ctx;
1023         my $prefix_abs = abs_path($prefix);
1024         $ctx = {};
1025         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
1026         $ctx->{domain} = "KTEST";
1027         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
1028         $ctx->{dnsname} = lc($ctx->{realm});
1029         $ctx->{kdc_ipv4} = "0.0.0.0";
1030         $ctx->{kdc_ipv6} = "::";
1031         $ctx->{krb5_ccname} = "$prefix_abs/krb5cc_%{uid}";
1032         Samba::mk_krb5_conf($ctx, "");
1033
1034         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
1035
1036         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
1037         print USERMAP "
1038 $ret->{USERNAME} = KTEST\\Administrator
1039 ";
1040         close(USERMAP);
1041
1042 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
1043 #Samba4 DC with the same parameters as are being used here.  The
1044 #domain SID is S-1-5-21-1071277805-689288055-3486227160
1045         $ret->{SAMSID} = "S-1-5-21-1911091480-1468226576-2729736297";
1046         $ret->{DOMSID} = "S-1-5-21-1071277805-689288055-3486227160";
1047
1048         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
1049         chmod 0600, "$prefix/private/secrets.tdb";
1050
1051 #Make sure there's no old ntdb file.
1052         system("rm -f $prefix/private/secrets.ntdb");
1053
1054 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
1055 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
1056 #
1057 #and having in krb5.conf:
1058 # ticket_lifetime = 799718400
1059 # renew_lifetime = 799718400
1060 #
1061 # The commands for the -2 keytab where were:
1062 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
1063 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
1064 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
1065 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
1066 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
1067 #
1068 # and then for the -3 keytab, I did
1069 #
1070 # net changetrustpw; kdestroy and the same again.
1071 #
1072 # This creates a credential cache with a very long lifetime (2036 at
1073 # at 2011-04), and shows that running 'net changetrustpw' does not
1074 # break existing logins (for the secrets.tdb method at least).
1075 #
1076
1077         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
1078
1079         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
1080         chmod 0600, "$prefix/krb5_ccache-2";
1081
1082         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
1083         chmod 0600, "$prefix/krb5_ccache-3";
1084
1085         # We need world access to this share, as otherwise the domain
1086         # administrator from the AD domain provided by ktest can't
1087         # access the share for tests.
1088         chmod 0777, "$prefix/share";
1089
1090         if (not $self->check_or_start($ret, "yes", "no", "yes")) {
1091                return undef;
1092         }
1093         return $ret;
1094 }
1095
1096 sub setup_maptoguest
1097 {
1098         my ($self, $path) = @_;
1099
1100         print "PROVISIONING maptoguest...";
1101
1102         my $options = "
1103 map to guest = bad user
1104 ntlm auth = yes
1105 ";
1106
1107         my $vars = $self->provision($path, "WORKGROUP",
1108                                     "maptoguest",
1109                                     "maptoguestpass",
1110                                     $options);
1111
1112         $vars or return undef;
1113
1114         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
1115                return undef;
1116         }
1117
1118         return $vars;
1119 }
1120
1121 sub stop_sig_term($$) {
1122         my ($self, $pid) = @_;
1123         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
1124 }
1125
1126 sub stop_sig_kill($$) {
1127         my ($self, $pid) = @_;
1128         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
1129 }
1130
1131 sub write_pid($$$)
1132 {
1133         my ($env_vars, $app, $pid) = @_;
1134
1135         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
1136         print PID $pid;
1137         close(PID);
1138 }
1139
1140 sub read_pid($$)
1141 {
1142         my ($env_vars, $app) = @_;
1143
1144         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
1145         my $pid = <PID>;
1146         close(PID);
1147         return $pid;
1148 }
1149
1150 sub check_or_start($$$$$) {
1151         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
1152
1153         # use a pipe for stdin in the child processes. This allows
1154         # those processes to monitor the pipe for EOF to ensure they
1155         # exit when the test script exits
1156         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
1157
1158         unlink($env_vars->{NMBD_TEST_LOG});
1159         print "STARTING NMBD...";
1160         my $pid = fork();
1161         if ($pid == 0) {
1162                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
1163                 open STDERR, '>&STDOUT';
1164
1165                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1166
1167                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1168                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.nmbd";
1169                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1170                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1171
1172                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1173                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1174                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1175                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1176                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1177                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1178                 $ENV{UID_WRAPPER_ROOT} = "1";
1179
1180                 $ENV{ENVNAME} = "$ENV{ENVNAME}.nmbd";
1181
1182                 if ($nmbd ne "yes") {
1183                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1184                                 my $signame = shift;
1185                                 print("Skip nmbd received signal $signame");
1186                                 exit 0;
1187                         };
1188                         sleep($self->{server_maxtime});
1189                         exit 0;
1190                 }
1191
1192                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
1193                 my @optargs = ("-d0");
1194                 if (defined($ENV{NMBD_OPTIONS})) {
1195                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
1196                 }
1197                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1198                 if(defined($ENV{NMBD_VALGRIND})) { 
1199                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
1200                 }
1201                 my @args = ("-F", "--no-process-group",
1202                             "-s", $env_vars->{SERVERCONFFILE},
1203                             "-l", $env_vars->{LOGDIR});
1204                 if (not defined($ENV{NMBD_DONT_LOG_STDOUT})) {
1205                         push(@args, "--log-stdout");
1206                 }
1207
1208                 close($env_vars->{STDIN_PIPE});
1209                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1210
1211                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1212                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1213         }
1214         $env_vars->{NMBD_TL_PID} = $pid;
1215         write_pid($env_vars, "nmbd", $pid);
1216         print "DONE\n";
1217
1218         unlink($env_vars->{WINBINDD_TEST_LOG});
1219         print "STARTING WINBINDD...";
1220         $pid = fork();
1221         if ($pid == 0) {
1222                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
1223                 open STDERR, '>&STDOUT';
1224
1225                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1226
1227                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1228                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.winbindd";
1229                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1230                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1231
1232                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1233                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1234                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1235                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1236                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1237                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1238                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
1239                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
1240                 } else {
1241                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
1242                 }
1243                 $ENV{UID_WRAPPER_ROOT} = "1";
1244
1245                 $ENV{ENVNAME} = "$ENV{ENVNAME}.winbindd";
1246
1247                 if ($winbindd ne "yes") {
1248                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1249                                 my $signame = shift;
1250                                 print("Skip winbindd received signal $signame");
1251                                 exit 0;
1252                         };
1253                         sleep($self->{server_maxtime});
1254                         exit 0;
1255                 }
1256
1257                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
1258                 my @optargs = ("-d0");
1259                 if (defined($ENV{WINBINDD_OPTIONS})) {
1260                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
1261                 }
1262                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1263                 if(defined($ENV{WINBINDD_VALGRIND})) {
1264                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
1265                 }
1266                 my @args = ("-F", "--no-process-group",
1267                             "-s", $env_vars->{SERVERCONFFILE},
1268                             "-l", $env_vars->{LOGDIR});
1269                 if (not defined($ENV{WINBINDD_DONT_LOG_STDOUT})) {
1270                         push(@args, "--stdout");
1271                 }
1272
1273                 close($env_vars->{STDIN_PIPE});
1274                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1275
1276                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1277                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1278         }
1279         $env_vars->{WINBINDD_TL_PID} = $pid;
1280         write_pid($env_vars, "winbindd", $pid);
1281         print "DONE\n";
1282
1283         unlink($env_vars->{SMBD_TEST_LOG});
1284         print "STARTING SMBD...";
1285         $pid = fork();
1286         if ($pid == 0) {
1287                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
1288                 open STDERR, '>&STDOUT';
1289
1290                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1291
1292                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1293                 $ENV{KRB5CCNAME} = "$env_vars->{KRB5_CCACHE}.smbd";
1294                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1295                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1296
1297                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1298                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1299                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1300                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1301                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1302                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1303                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
1304                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
1305                 } else {
1306                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
1307                 }
1308                 $ENV{UID_WRAPPER_ROOT} = "1";
1309
1310                 $ENV{ENVNAME} = "$ENV{ENVNAME}.smbd";
1311
1312                 if ($smbd ne "yes") {
1313                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1314                                 my $signame = shift;
1315                                 print("Skip smbd received signal $signame");
1316                                 exit 0;
1317                         };
1318                         sleep($self->{server_maxtime});
1319                         exit 0;
1320                 }
1321
1322                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
1323                 my @optargs = ("-d0");
1324                 if (defined($ENV{SMBD_OPTIONS})) {
1325                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
1326                 }
1327                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1328                 if(defined($ENV{SMBD_VALGRIND})) {
1329                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
1330                 }
1331                 my @args = ("-F", "--no-process-group",
1332                             "-s", $env_vars->{SERVERCONFFILE},
1333                             "-l", $env_vars->{LOGDIR});
1334                 if (not defined($ENV{SMBD_DONT_LOG_STDOUT})) {
1335                         push(@args, "--log-stdout");
1336                 }
1337
1338                 close($env_vars->{STDIN_PIPE});
1339                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1340
1341                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1342                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1343         }
1344         $env_vars->{SMBD_TL_PID} = $pid;
1345         write_pid($env_vars, "smbd", $pid);
1346         print "DONE\n";
1347
1348         close(STDIN_READER);
1349
1350         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
1351 }
1352
1353 sub createuser($$$$$)
1354 {
1355         my ($self, $username, $password, $conffile, $env) = @_;
1356         my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $username > /dev/null";
1357
1358         keys %$env;
1359         while(my($var, $val) = each %$env) {
1360                 $cmd = "$var=\"$val\" $cmd";
1361         }
1362
1363         unless (open(PWD, "|$cmd")) {
1364             warn("Unable to set password for $username account\n$cmd");
1365             return undef;
1366         }
1367         print PWD "$password\n$password\n";
1368         unless (close(PWD)) {
1369             warn("Unable to set password for $username account\n$cmd");
1370             return undef;
1371         }
1372 }
1373
1374 sub provision($$$$$$$$$)
1375 {
1376         my ($self, $prefix, $domain, $server, $password, $extra_options, $dc_server_ip, $dc_server_ipv6, $no_delete_prefix) = @_;
1377
1378         ##
1379         ## setup the various environment variables we need
1380         ##
1381
1382         my $samsid = Samba::random_domain_sid();
1383         my $swiface = Samba::get_interface($server);
1384         my %ret = ();
1385         my %createuser_env = ();
1386         my $server_ip = "127.0.0.$swiface";
1387         my $server_ipv6 = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
1388
1389         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
1390         chomp $unix_name;
1391         my $unix_uid = $>;
1392         my $unix_gids_str = $);
1393         my @unix_gids = split(" ", $unix_gids_str);
1394
1395         my $prefix_abs = abs_path($prefix);
1396         my $bindir_abs = abs_path($self->{bindir});
1397
1398         my @dirs = ();
1399
1400         my $shrdir="$prefix_abs/share";
1401         push(@dirs,$shrdir);
1402
1403         my $libdir="$prefix_abs/lib";
1404         push(@dirs,$libdir);
1405
1406         my $piddir="$prefix_abs/pid";
1407         push(@dirs,$piddir);
1408
1409         my $privatedir="$prefix_abs/private";
1410         push(@dirs,$privatedir);
1411
1412         my $binddnsdir = "$prefix_abs/bind-dns";
1413         push(@dirs, $binddnsdir);
1414
1415         my $lockdir="$prefix_abs/lockdir";
1416         push(@dirs,$lockdir);
1417
1418         my $eventlogdir="$prefix_abs/lockdir/eventlog";
1419         push(@dirs,$eventlogdir);
1420
1421         my $logdir="$prefix_abs/logs";
1422         push(@dirs,$logdir);
1423
1424         my $driver32dir="$shrdir/W32X86";
1425         push(@dirs,$driver32dir);
1426
1427         my $driver64dir="$shrdir/x64";
1428         push(@dirs,$driver64dir);
1429
1430         my $driver40dir="$shrdir/WIN40";
1431         push(@dirs,$driver40dir);
1432
1433         my $ro_shrdir="$shrdir/root-tmp";
1434         push(@dirs,$ro_shrdir);
1435
1436         my $msdfs_shrdir="$shrdir/msdfsshare";
1437         push(@dirs,$msdfs_shrdir);
1438
1439         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
1440         push(@dirs,$msdfs_deeppath);
1441
1442         my $badnames_shrdir="$shrdir/badnames";
1443         push(@dirs,$badnames_shrdir);
1444
1445         my $lease1_shrdir="$shrdir/SMB2_10";
1446         push(@dirs,$lease1_shrdir);
1447
1448         my $lease2_shrdir="$shrdir/SMB3_00";
1449         push(@dirs,$lease2_shrdir);
1450
1451         my $manglenames_shrdir="$shrdir/manglenames";
1452         push(@dirs,$manglenames_shrdir);
1453
1454         my $widelinks_shrdir="$shrdir/widelinks";
1455         push(@dirs,$widelinks_shrdir);
1456
1457         my $widelinks_linkdir="$shrdir/widelinks_foo";
1458         push(@dirs,$widelinks_linkdir);
1459
1460         my $shadow_tstdir="$shrdir/shadow";
1461         push(@dirs,$shadow_tstdir);
1462         my $shadow_mntdir="$shadow_tstdir/mount";
1463         push(@dirs,$shadow_mntdir);
1464         my $shadow_basedir="$shadow_mntdir/base";
1465         push(@dirs,$shadow_basedir);
1466         my $shadow_shrdir="$shadow_basedir/share";
1467         push(@dirs,$shadow_shrdir);
1468
1469         my $nosymlinks_shrdir="$shrdir/nosymlinks";
1470         push(@dirs,$nosymlinks_shrdir);
1471
1472         my $local_symlinks_shrdir="$shrdir/local_symlinks";
1473         push(@dirs,$local_symlinks_shrdir);
1474
1475         # this gets autocreated by winbindd
1476         my $wbsockdir="$prefix_abs/winbindd";
1477
1478         my $nmbdsockdir="$prefix_abs/nmbd";
1479         unlink($nmbdsockdir);
1480
1481         ## 
1482         ## create the test directory layout
1483         ##
1484         die ("prefix_abs = ''") if $prefix_abs eq "";
1485         die ("prefix_abs = '/'") if $prefix_abs eq "/";
1486
1487         mkdir($prefix_abs, 0777);
1488         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
1489         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
1490             system("rm -rf $prefix_abs/*");
1491         }
1492         mkdir($_, 0777) foreach(@dirs);
1493
1494         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
1495
1496         ##
1497         ## lockdir and piddir must be 0755
1498         ##
1499         chmod 0755, $lockdir;
1500         chmod 0755, $piddir;
1501
1502
1503         ##
1504         ## create ro and msdfs share layout
1505         ##
1506
1507         chmod 0755, $ro_shrdir;
1508         my $unreadable_file = "$ro_shrdir/unreadable_file";
1509         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
1510                 warn("Unable to open $unreadable_file");
1511                 return undef;
1512         }
1513         close(UNREADABLE_FILE);
1514         chmod 0600, $unreadable_file;
1515
1516         my $msdfs_target = "$ro_shrdir/msdfs-target";
1517         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
1518                 warn("Unable to open $msdfs_target");
1519                 return undef;
1520         }
1521         close(MSDFS_TARGET);
1522         chmod 0666, $msdfs_target;
1523         symlink "msdfs:$server_ip\\ro-tmp,$server_ipv6\\ro-tmp",
1524                 "$msdfs_shrdir/msdfs-src1";
1525         symlink "msdfs:$server_ipv6\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
1526
1527         ##
1528         ## create bad names in $badnames_shrdir
1529         ##
1530         ## (An invalid name, would be mangled to 8.3).
1531         my $badname_target = "$badnames_shrdir/\340|\231\216\377\177";
1532         unless (open(BADNAME_TARGET, ">$badname_target")) {
1533                 warn("Unable to open $badname_target");
1534                 return undef;
1535         }
1536         close(BADNAME_TARGET);
1537         chmod 0666, $badname_target;
1538
1539         ## (A bad name, would not be mangled to 8.3).
1540         my $badname_target = "$badnames_shrdir/\240\276\346\327\377\177";
1541         unless (open(BADNAME_TARGET, ">$badname_target")) {
1542                 warn("Unable to open $badname_target");
1543                 return undef;
1544         }
1545         close(BADNAME_TARGET);
1546         chmod 0666, $badname_target;
1547
1548         ## (A bad good name).
1549         my $badname_target = "$badnames_shrdir/blank.txt";
1550         unless (open(BADNAME_TARGET, ">$badname_target")) {
1551                 warn("Unable to open $badname_target");
1552                 return undef;
1553         }
1554         close(BADNAME_TARGET);
1555         chmod 0666, $badname_target;
1556
1557         ##
1558         ## create mangleable directory names in $manglenames_shrdir
1559         ##
1560         my $manglename_target = "$manglenames_shrdir/foo:bar";
1561         mkdir($manglename_target, 0777);
1562
1563         ##
1564         ## create symlinks for widelinks tests.
1565         ##
1566         my $widelinks_target = "$widelinks_linkdir/target";
1567         unless (open(WIDELINKS_TARGET, ">$widelinks_target")) {
1568                 warn("Unable to open $widelinks_target");
1569                 return undef;
1570         }
1571         close(WIDELINKS_TARGET);
1572         chmod 0666, $widelinks_target;
1573         ##
1574         ## This link should get ACCESS_DENIED
1575         ##
1576         symlink "$widelinks_target", "$widelinks_shrdir/source";
1577         ##
1578         ## This link should be allowed
1579         ##
1580         symlink "$widelinks_shrdir", "$widelinks_shrdir/dot";
1581
1582         my $conffile="$libdir/server.conf";
1583         my $dfqconffile="$libdir/dfq.conf";
1584
1585         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/third_party/nss_wrapper/nss_wrapper.pl";
1586         my $nss_wrapper_passwd = "$privatedir/passwd";
1587         my $nss_wrapper_group = "$privatedir/group";
1588         my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
1589         my $resolv_conf = "$privatedir/resolv.conf";
1590         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
1591
1592         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
1593
1594         my $fake_snap_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/fake_snap.pl";
1595
1596         my @eventlog_list = ("dns server", "application");
1597
1598         ##
1599         ## calculate uids and gids
1600         ##
1601
1602         my ($max_uid, $max_gid);
1603         my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2, $uid_userdup);
1604         my ($uid_pdbtest_wkn);
1605         my ($uid_smbget);
1606         my ($uid_force_user);
1607         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
1608         my ($gid_userdup, $gid_everyone);
1609         my ($gid_force_user);
1610         my ($uid_user1);
1611         my ($uid_user2);
1612
1613         if ($unix_uid < 0xffff - 10) {
1614                 $max_uid = 0xffff;
1615         } else {
1616                 $max_uid = $unix_uid;
1617         }
1618
1619         $uid_root = $max_uid - 1;
1620         $uid_nobody = $max_uid - 2;
1621         $uid_pdbtest = $max_uid - 3;
1622         $uid_pdbtest2 = $max_uid - 4;
1623         $uid_userdup = $max_uid - 5;
1624         $uid_pdbtest_wkn = $max_uid - 6;
1625         $uid_force_user = $max_uid - 7;
1626         $uid_smbget = $max_uid - 8;
1627         $uid_user1 = $max_uid - 9;
1628         $uid_user2 = $max_uid - 10;
1629
1630         if ($unix_gids[0] < 0xffff - 8) {
1631                 $max_gid = 0xffff;
1632         } else {
1633                 $max_gid = $unix_gids[0];
1634         }
1635
1636         $gid_nobody = $max_gid - 1;
1637         $gid_nogroup = $max_gid - 2;
1638         $gid_root = $max_gid - 3;
1639         $gid_domusers = $max_gid - 4;
1640         $gid_domadmins = $max_gid - 5;
1641         $gid_userdup = $max_gid - 6;
1642         $gid_everyone = $max_gid - 7;
1643         $gid_force_user = $max_gid - 8;
1644
1645         ##
1646         ## create conffile
1647         ##
1648
1649         unless (open(CONF, ">$conffile")) {
1650                 warn("Unable to open $conffile");
1651                 return undef;
1652         }
1653         print CONF "
1654 [global]
1655         netbios name = $server
1656         interfaces = $server_ip/8 $server_ipv6/64
1657         bind interfaces only = yes
1658         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
1659         smbd:suicide mode = yes
1660
1661         workgroup = $domain
1662
1663         private dir = $privatedir
1664         binddns dir = $binddnsdir
1665         pid directory = $piddir
1666         lock directory = $lockdir
1667         log file = $logdir/log.\%m
1668         log level = 1
1669         debug pid = yes
1670         max log size = 0
1671
1672         state directory = $lockdir
1673         cache directory = $lockdir
1674
1675         passdb backend = tdbsam
1676
1677         time server = yes
1678
1679         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1680         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
1681         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1682         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
1683         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
1684         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
1685         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
1686
1687         addprinter command =            $mod_printer_pl -a -s $conffile --
1688         deleteprinter command =         $mod_printer_pl -d -s $conffile --
1689
1690         eventlog list = application \"dns server\"
1691
1692         kernel oplocks = no
1693         kernel change notify = no
1694
1695         logging = file
1696         printing = bsd
1697         printcap name = /dev/null
1698
1699         winbindd socket directory = $wbsockdir
1700         nmbd:socket dir = $nmbdsockdir
1701         idmap config * : range = 100000-200000
1702         winbind enum users = yes
1703         winbind enum groups = yes
1704         winbind separator = /
1705         include system krb5 conf = no
1706
1707 #       min receivefile size = 4000
1708
1709         read only = no
1710
1711         smbd:sharedelay = 100000
1712         smbd:writetimeupdatedelay = 500000
1713         map hidden = no
1714         map system = no
1715         map readonly = no
1716         store dos attributes = yes
1717         create mask = 755
1718         dos filemode = yes
1719         strict rename = yes
1720         strict sync = yes
1721         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
1722
1723         printing = vlp
1724         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1725         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1726         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1727         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1728         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1729         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1730         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1731         lpq cache time = 0
1732         print notify backchannel = yes
1733
1734         ncalrpc dir = $prefix_abs/ncalrpc
1735
1736         # The samba3.blackbox.smbclient_s3 test uses this to test that
1737         # sending messages works, and that the %m sub works.
1738         message command = mv %s $shrdir/message.%m
1739
1740         # fsrvp server requires registry shares
1741         registry shares = yes
1742
1743         # Used by RPC SRVSVC tests
1744         add share command = $bindir_abs/smbaddshare
1745         change share command = $bindir_abs/smbchangeshare
1746         delete share command = $bindir_abs/smbdeleteshare
1747
1748         # fruit:copyfile is a global option
1749         fruit:copyfile = yes
1750
1751         #this does not mean that we use non-secure test env,
1752         #it just means we ALLOW one to be configured.
1753         allow insecure wide links = yes
1754
1755         # Begin extra options
1756         $extra_options
1757         # End extra options
1758
1759         #Include user defined custom parameters if set
1760 ";
1761
1762         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1763                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1764         }
1765
1766         print CONF "
1767 [tmp]
1768         path = $shrdir
1769         comment = smb username is [%U]
1770 [tmpsort]
1771         path = $shrdir
1772         comment = Load dirsort module
1773         vfs objects = dirsort acl_xattr fake_acls xattr_tdb streams_depot
1774 [tmpenc]
1775         path = $shrdir
1776         comment = encrypt smb username is [%U]
1777         smb encrypt = required
1778         vfs objects = dirsort
1779 [tmpguest]
1780         path = $shrdir
1781         guest ok = yes
1782 [guestonly]
1783         path = $shrdir
1784         guest only = yes
1785         guest ok = yes
1786 [forceuser]
1787         path = $shrdir
1788         force user = $unix_name
1789         guest ok = yes
1790 [forceuser_unixonly]
1791         comment = force a user with unix user SID and group SID
1792         path = $shrdir
1793         force user = pdbtest
1794         guest ok = yes
1795 [forceuser_wkngroup]
1796         comment = force a user with well-known group SID
1797         path = $shrdir
1798         force user = pdbtest_wkn
1799         guest ok = yes
1800 [forcegroup]
1801         path = $shrdir
1802         force group = nogroup
1803         guest ok = yes
1804 [ro-tmp]
1805         path = $ro_shrdir
1806         guest ok = yes
1807 [write-list-tmp]
1808         path = $shrdir
1809         read only = yes
1810         write list = $unix_name
1811 [valid-users-tmp]
1812         path = $shrdir
1813         valid users = $unix_name
1814         access based share enum = yes
1815 [msdfs-share]
1816         path = $msdfs_shrdir
1817         msdfs root = yes
1818         msdfs shuffle referrals = yes
1819         guest ok = yes
1820 [hideunread]
1821         copy = tmp
1822         hide unreadable = yes
1823 [tmpcase]
1824         copy = tmp
1825         case sensitive = yes
1826 [hideunwrite]
1827         copy = tmp
1828         hide unwriteable files = yes
1829 [durable]
1830         copy = tmp
1831         kernel share modes = no
1832         kernel oplocks = no
1833         posix locking = no
1834 [fs_specific]
1835         copy = tmp
1836         $fs_specific_conf
1837 [print1]
1838         copy = tmp
1839         printable = yes
1840
1841 [print2]
1842         copy = print1
1843 [print3]
1844         copy = print1
1845         default devmode = no
1846 [lp]
1847         copy = print1
1848
1849 [nfs4acl_simple_40]
1850         path = $shrdir
1851         comment = smb username is [%U]
1852         nfs4:mode = simple
1853         nfs4acl_xattr:version = 40
1854         vfs objects = nfs4acl_xattr xattr_tdb
1855
1856 [nfs4acl_special_40]
1857         path = $shrdir
1858         comment = smb username is [%U]
1859         nfs4:mode = special
1860         nfs4acl_xattr:version = 40
1861         vfs objects = nfs4acl_xattr xattr_tdb
1862
1863 [nfs4acl_simple_41]
1864         path = $shrdir
1865         comment = smb username is [%U]
1866         nfs4:mode = simple
1867         vfs objects = nfs4acl_xattr xattr_tdb
1868
1869 [nfs4acl_xdr_40]
1870         path = $shrdir
1871         comment = smb username is [%U]
1872         vfs objects = nfs4acl_xattr xattr_tdb
1873         nfs4:mode = simple
1874         nfs4acl_xattr:encoding = xdr
1875         nfs4acl_xattr:version = 40
1876
1877 [nfs4acl_xdr_41]
1878         path = $shrdir
1879         comment = smb username is [%U]
1880         vfs objects = nfs4acl_xattr xattr_tdb
1881         nfs4:mode = simple
1882         nfs4acl_xattr:encoding = xdr
1883         nfs4acl_xattr:version = 41
1884
1885 [xcopy_share]
1886         path = $shrdir
1887         comment = smb username is [%U]
1888         create mask = 777
1889         force create mode = 777
1890 [posix_share]
1891         path = $shrdir
1892         comment = smb username is [%U]
1893         create mask = 0777
1894         force create mode = 0
1895         directory mask = 0777
1896         force directory mode = 0
1897         vfs objects = xattr_tdb streams_depot
1898 [aio]
1899         copy = tmp
1900         aio read size = 1
1901         aio write size = 1
1902
1903 [print\$]
1904         copy = tmp
1905
1906 [vfs_fruit]
1907         path = $shrdir
1908         vfs objects = catia fruit streams_xattr acl_xattr xattr_tdb
1909         fruit:resource = file
1910         fruit:metadata = netatalk
1911         fruit:locking = netatalk
1912         fruit:encoding = native
1913         fruit:veto_appledouble = no
1914
1915 [vfs_fruit_xattr]
1916         path = $shrdir
1917         # This is used by vfs.fruit tests that require real fs xattr
1918         vfs objects = catia fruit streams_xattr acl_xattr
1919         fruit:resource = file
1920         fruit:metadata = netatalk
1921         fruit:locking = netatalk
1922         fruit:encoding = native
1923         fruit:veto_appledouble = no
1924
1925 [vfs_fruit_metadata_stream]
1926         path = $shrdir
1927         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
1928         fruit:resource = file
1929         fruit:metadata = stream
1930         fruit:veto_appledouble = no
1931
1932 [vfs_fruit_stream_depot]
1933         path = $shrdir
1934         vfs objects = fruit streams_depot acl_xattr xattr_tdb
1935         fruit:resource = stream
1936         fruit:metadata = stream
1937         fruit:veto_appledouble = no
1938
1939 [vfs_wo_fruit]
1940         path = $shrdir
1941         vfs objects = streams_xattr acl_xattr xattr_tdb
1942
1943 [vfs_wo_fruit_stream_depot]
1944         path = $shrdir
1945         vfs objects = streams_depot acl_xattr xattr_tdb
1946
1947 [vfs_fruit_timemachine]
1948         path = $shrdir
1949         vfs objects = fruit streams_xattr acl_xattr xattr_tdb
1950         fruit:resource = file
1951         fruit:metadata = stream
1952         fruit:time machine = yes
1953         fruit:time machine max size = 32K
1954
1955 [badname-tmp]
1956         path = $badnames_shrdir
1957         guest ok = yes
1958
1959 [manglenames_share]
1960         path = $manglenames_shrdir
1961         guest ok = yes
1962
1963 [dynamic_share]
1964         path = $shrdir/%R
1965         guest ok = yes
1966
1967 [widelinks_share]
1968         path = $widelinks_shrdir
1969         wide links = no
1970         guest ok = yes
1971
1972 [fsrvp_share]
1973         path = $shrdir
1974         comment = fake shapshots using rsync
1975         vfs objects = shell_snap shadow_copy2
1976         shell_snap:check path command = $fake_snap_pl --check
1977         shell_snap:create command = $fake_snap_pl --create
1978         shell_snap:delete command = $fake_snap_pl --delete
1979         # a relative path here fails, the snapshot dir is no longer found
1980         shadow:snapdir = $shrdir/.snapshots
1981
1982 [shadow1]
1983         path = $shadow_shrdir
1984         comment = previous versions snapshots under mount point
1985         vfs objects = shadow_copy2
1986         shadow:mountpoint = $shadow_mntdir
1987
1988 [shadow2]
1989         path = $shadow_shrdir
1990         comment = previous versions snapshots outside mount point
1991         vfs objects = shadow_copy2
1992         shadow:mountpoint = $shadow_mntdir
1993         shadow:snapdir = $shadow_tstdir/.snapshots
1994
1995 [shadow3]
1996         path = $shadow_shrdir
1997         comment = previous versions with subvolume snapshots, snapshots under base dir
1998         vfs objects = shadow_copy2
1999         shadow:mountpoint = $shadow_mntdir
2000         shadow:basedir = $shadow_basedir
2001         shadow:snapdir = $shadow_basedir/.snapshots
2002
2003 [shadow4]
2004         path = $shadow_shrdir
2005         comment = previous versions with subvolume snapshots, snapshots outside mount point
2006         vfs objects = shadow_copy2
2007         shadow:mountpoint = $shadow_mntdir
2008         shadow:basedir = $shadow_basedir
2009         shadow:snapdir = $shadow_tstdir/.snapshots
2010
2011 [shadow5]
2012         path = $shadow_shrdir
2013         comment = previous versions at volume root snapshots under mount point
2014         vfs objects = shadow_copy2
2015         shadow:mountpoint = $shadow_shrdir
2016
2017 [shadow6]
2018         path = $shadow_shrdir
2019         comment = previous versions at volume root snapshots outside mount point
2020         vfs objects = shadow_copy2
2021         shadow:mountpoint = $shadow_shrdir
2022         shadow:snapdir = $shadow_tstdir/.snapshots
2023
2024 [shadow7]
2025         path = $shadow_shrdir
2026         comment = previous versions snapshots everywhere
2027         vfs objects = shadow_copy2
2028         shadow:mountpoint = $shadow_mntdir
2029         shadow:snapdirseverywhere = yes
2030
2031 [shadow8]
2032         path = $shadow_shrdir
2033         comment = previous versions using snapsharepath
2034         vfs objects = shadow_copy2
2035         shadow:mountpoint = $shadow_mntdir
2036         shadow:snapdir = $shadow_tstdir/.snapshots
2037         shadow:snapsharepath = share
2038
2039 [shadow_fmt0]
2040         comment = Testing shadow:format with default option
2041         vfs object = shadow_copy2
2042         path = $shadow_shrdir
2043         read only = no
2044         guest ok = yes
2045         shadow:mountpoint = $shadow_mntdir
2046         shadow:basedir = $shadow_basedir
2047         shadow:snapdir = $shadow_basedir/.snapshots
2048         shadow:format = \@GMT-%Y.%m.%d-%H.%M.%S
2049
2050 [shadow_fmt1]
2051         comment = Testing shadow:format with only date component
2052         vfs object = shadow_copy2
2053         path = $shadow_shrdir
2054         read only = no
2055         guest ok = yes
2056         shadow:mountpoint = $shadow_mntdir
2057         shadow:basedir = $shadow_basedir
2058         shadow:snapdir = $shadow_basedir/.snapshots
2059         shadow:format = \@GMT-%Y-%m-%d
2060
2061 [shadow_fmt2]
2062         comment = Testing shadow:format with some hardcoded prefix
2063         vfs object = shadow_copy2
2064         path = $shadow_shrdir
2065         read only = no
2066         guest ok = yes
2067         shadow:mountpoint = $shadow_mntdir
2068         shadow:basedir = $shadow_basedir
2069         shadow:snapdir = $shadow_basedir/.snapshots
2070         shadow:format = snap\@GMT-%Y.%m.%d-%H.%M.%S
2071
2072 [shadow_fmt3]
2073         comment = Testing shadow:format with modified format
2074         vfs object = shadow_copy2
2075         path = $shadow_shrdir
2076         read only = no
2077         guest ok = yes
2078         shadow:mountpoint = $shadow_mntdir
2079         shadow:basedir = $shadow_basedir
2080         shadow:snapdir = $shadow_basedir/.snapshots
2081         shadow:format = \@GMT-%Y.%m.%d-%H_%M_%S-snap
2082
2083 [shadow_fmt4]
2084         comment = Testing shadow:snapprefix regex
2085         vfs object = shadow_copy2
2086         path = $shadow_shrdir
2087         read only = no
2088         guest ok = yes
2089         shadow:mountpoint = $shadow_mntdir
2090         shadow:basedir = $shadow_basedir
2091         shadow:snapdir = $shadow_basedir/.snapshots
2092         shadow:snapprefix = \^s[a-z]*p\$
2093         shadow:format = _GMT-%Y.%m.%d-%H.%M.%S
2094
2095 [shadow_fmt5]
2096         comment = Testing shadow:snapprefix with delim regex
2097         vfs object = shadow_copy2
2098         path = $shadow_shrdir
2099         read only = no
2100         guest ok = yes
2101         shadow:mountpoint = $shadow_mntdir
2102         shadow:basedir = $shadow_basedir
2103         shadow:snapdir = $shadow_basedir/.snapshots
2104         shadow:delimiter = \@GMT
2105         shadow:snapprefix = [a-z]*
2106         shadow:format = \@GMT-%Y.%m.%d-%H.%M.%S
2107
2108 [shadow_wl]
2109         path = $shadow_shrdir
2110         comment = previous versions with wide links allowed
2111         vfs objects = shadow_copy2
2112         shadow:mountpoint = $shadow_mntdir
2113         wide links = yes
2114 [dfq]
2115         path = $shrdir/dfree
2116         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2117         admin users = $unix_name
2118         include = $dfqconffile
2119 [dfq_cache]
2120         path = $shrdir/dfree
2121         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2122         admin users = $unix_name
2123         include = $dfqconffile
2124         dfree cache time = 60
2125 [dfq_owner]
2126         path = $shrdir/dfree
2127         vfs objects = acl_xattr fake_acls xattr_tdb fake_dfq
2128         inherit owner = yes
2129         include = $dfqconffile
2130 [acl_xattr_ign_sysacl_posix]
2131         copy = tmp
2132         acl_xattr:ignore system acls = yes
2133         acl_xattr:default acl style = posix
2134 [acl_xattr_ign_sysacl_windows]
2135         copy = tmp
2136         acl_xattr:ignore system acls = yes
2137         acl_xattr:default acl style = windows
2138
2139 [mangle_illegal]
2140         copy = tmp
2141         mangled names = illegal
2142
2143 [nosymlinks]
2144         copy = tmp
2145         path = $nosymlinks_shrdir
2146         follow symlinks = no
2147
2148 [local_symlinks]
2149         copy = tmp
2150         path = $local_symlinks_shrdir
2151         follow symlinks = yes
2152
2153 [kernel_oplocks]
2154         copy = tmp
2155         kernel oplocks = yes
2156         vfs objects = streams_xattr xattr_tdb
2157
2158 [streams_xattr]
2159         copy = tmp
2160         vfs objects = streams_xattr xattr_tdb
2161
2162 [compound_find]
2163         copy = tmp
2164         smbd:find async delay usec = 10000
2165 [error_inject]
2166         copy = tmp
2167         vfs objects = error_inject
2168         include = $libdir/error_inject.conf
2169         ";
2170         close(CONF);
2171
2172         my $net = Samba::bindir_path($self, "net");
2173         my $cmd = "";
2174         $cmd .= "SMB_CONF_PATH=\"$conffile\" ";
2175         $cmd .= "$net setlocalsid $samsid";
2176
2177         if (system($cmd) != 0) {
2178             warn("Join failed\n$cmd");
2179             return undef;
2180         }
2181
2182         unless (open(DFQCONF, ">$dfqconffile")) {
2183                 warn("Unable to open $dfqconffile");
2184                 return undef;
2185         }
2186         close(DFQCONF);
2187
2188         ##
2189         ## create a test account
2190         ##
2191
2192         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
2193            warn("Unable to open $nss_wrapper_passwd");
2194            return undef;
2195         } 
2196         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
2197 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
2198 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
2199 pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
2200 userdup:x:$uid_userdup:$gid_userdup:userdup gecos:$prefix_abs:/bin/false
2201 pdbtest_wkn:x:$uid_pdbtest_wkn:$gid_everyone:pdbtest_wkn gecos:$prefix_abs:/bin/false
2202 force_user:x:$uid_force_user:$gid_force_user:force user gecos:$prefix_abs:/bin/false
2203 smbget_user:x:$uid_smbget:$gid_domusers:smbget_user gecos:$prefix_abs:/bin/false
2204 user1:x:$uid_user1:$gid_nogroup:user1 gecos:$prefix_abs:/bin/false
2205 user2:x:$uid_user2:$gid_nogroup:user2 gecos:$prefix_abs:/bin/false
2206 ";
2207         if ($unix_uid != 0) {
2208                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
2209 ";
2210         }
2211         close(PASSWD);
2212
2213         unless (open(GROUP, ">$nss_wrapper_group")) {
2214              warn("Unable to open $nss_wrapper_group");
2215              return undef;
2216         }
2217         print GROUP "nobody:x:$gid_nobody:
2218 nogroup:x:$gid_nogroup:nobody
2219 $unix_name-group:x:$unix_gids[0]:
2220 domusers:X:$gid_domusers:
2221 domadmins:X:$gid_domadmins:
2222 userdup:x:$gid_userdup:$unix_name
2223 everyone:x:$gid_everyone:
2224 force_user:x:$gid_force_user:
2225 ";
2226         if ($unix_gids[0] != 0) {
2227                 print GROUP "root:x:$gid_root:
2228 ";
2229         }
2230
2231         close(GROUP);
2232
2233         ## hosts
2234         my $hostname = lc($server);
2235         unless (open(HOSTS, ">>$nss_wrapper_hosts")) {
2236                 warn("Unable to open $nss_wrapper_hosts");
2237                 return undef;
2238         }
2239         print HOSTS "${server_ip} ${hostname}.samba.example.com ${hostname}\n";
2240         print HOSTS "${server_ipv6} ${hostname}.samba.example.com ${hostname}\n";
2241         close(HOSTS);
2242
2243         ## hosts
2244         unless (open(RESOLV_CONF, ">$resolv_conf")) {
2245                 warn("Unable to open $resolv_conf");
2246                 return undef;
2247         }
2248         if (defined($dc_server_ip) or defined($dc_server_ipv6)) {
2249                 if (defined($dc_server_ip)) {
2250                         print RESOLV_CONF "nameserver $dc_server_ip\n";
2251                 }
2252                 if (defined($dc_server_ipv6)) {
2253                         print RESOLV_CONF "nameserver $dc_server_ipv6\n";
2254                 }
2255         } else {
2256                 print RESOLV_CONF "nameserver ${server_ip}\n";
2257                 print RESOLV_CONF "nameserver ${server_ipv6}\n";
2258         }
2259         close(RESOLV_CONF);
2260
2261         foreach my $evlog (@eventlog_list) {
2262                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
2263                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
2264                 close(EVENTLOG);
2265         }
2266
2267         $createuser_env{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
2268         $createuser_env{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
2269         $createuser_env{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
2270         $createuser_env{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
2271         if ($ENV{SAMBA_DNS_FAKING}) {
2272                 $createuser_env{RESOLV_WRAPPER_CONF} = $resolv_conf;
2273         } else {
2274                 $createuser_env{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
2275         }
2276
2277         createuser($self, $unix_name, $password, $conffile, \%createuser_env) || die("Unable to create user");
2278         createuser($self, "force_user", $password, $conffile, \%createuser_env) || die("Unable to create force_user");
2279         createuser($self, "smbget_user", $password, $conffile, \%createuser_env) || die("Unable to create smbget_user");
2280         createuser($self, "user1", $password, $conffile, \%createuser_env) || die("Unable to create user1");
2281         createuser($self, "user2", $password, $conffile, \%createuser_env) || die("Unable to create user2");
2282
2283         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
2284         print DNS_UPDATE_LIST "A $server. $server_ip\n";
2285         print DNS_UPDATE_LIST "AAAA $server. $server_ipv6\n";
2286         close(DNS_UPDATE_LIST);
2287
2288         print "DONE\n";
2289
2290         $ret{SERVER_IP} = $server_ip;
2291         $ret{SERVER_IPV6} = $server_ipv6;
2292         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
2293         $ret{NMBD_TEST_LOG_POS} = 0;
2294         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
2295         $ret{WINBINDD_TEST_LOG_POS} = 0;
2296         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
2297         $ret{SMBD_TEST_LOG_POS} = 0;
2298         $ret{SERVERCONFFILE} = $conffile;
2299         $ret{CONFIGURATION} ="-s $conffile";
2300         $ret{LOCK_DIR} = $lockdir;
2301         $ret{SERVER} = $server;
2302         $ret{USERNAME} = $unix_name;
2303         $ret{USERID} = $unix_uid;
2304         $ret{DOMAIN} = $domain;
2305         $ret{SAMSID} = $samsid;
2306         $ret{NETBIOSNAME} = $server;
2307         $ret{PASSWORD} = $password;
2308         $ret{PIDDIR} = $piddir;
2309         $ret{SELFTEST_WINBINDD_SOCKET_DIR} = $wbsockdir;
2310         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
2311         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
2312         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
2313         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
2314         $ret{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
2315         $ret{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
2316         $ret{NSS_WRAPPER_MODULE_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
2317         $ret{NSS_WRAPPER_MODULE_FN_PREFIX} = "winbind";
2318         if ($ENV{SAMBA_DNS_FAKING}) {
2319                 $ret{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
2320         } else {
2321                 $ret{RESOLV_WRAPPER_CONF} = $resolv_conf;
2322         }
2323         $ret{LOCAL_PATH} = "$shrdir";
2324         $ret{LOGDIR} = $logdir;
2325
2326         #
2327         # Avoid hitting system krb5.conf -
2328         # An env that needs Kerberos will reset this to the real
2329         # value.
2330         #
2331         $ret{KRB5_CONFIG} = abs_path($prefix) . "/no_krb5.conf";
2332
2333         # Define KRB5CCNAME for each environment we set up
2334         $ret{KRB5_CCACHE} = abs_path($prefix) . "/krb5ccache";
2335         $ENV{KRB5CCNAME} = $ret{KRB5_CCACHE};
2336
2337         return \%ret;
2338 }
2339
2340 sub wait_for_start($$$$$)
2341 {
2342         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
2343         my $cmd;
2344         my $netcmd;
2345         my $ret;
2346
2347         if ($nmbd eq "yes") {
2348                 my $count = 0;
2349
2350                 # give time for nbt server to register its names
2351                 print "checking for nmbd\n";
2352
2353                 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
2354                 my $nmblookup = Samba::bindir_path($self, "nmblookup");
2355
2356                 do {
2357                         $ret = system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
2358                         if ($ret != 0) {
2359                                 sleep(1);
2360                         } else {
2361                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
2362                                 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
2363                                 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
2364                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
2365                         }
2366                         $count++;
2367                 } while ($ret != 0 && $count < 10);
2368                 if ($count == 10) {
2369                         print "NMBD not reachable after 10 retries\n";
2370                         teardown_env($self, $envvars);
2371                         return 0;
2372                 }
2373         }
2374
2375         if ($winbindd eq "yes") {
2376             print "checking for winbindd\n";
2377             my $count = 0;
2378             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2379             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2380             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2381             $cmd .= Samba::bindir_path($self, "wbinfo") . " --ping-dc";
2382
2383             do {
2384                 if ($ret != 0) {
2385                     $ret = system($cmd);
2386                     sleep(1);
2387                 }
2388                 $count++;
2389             } while ($ret != 0 && $count < 20);
2390             if ($count == 20) {
2391                 print "WINBINDD not reachable after 20 seconds\n";
2392                 teardown_env($self, $envvars);
2393                 return 0;
2394             }
2395         }
2396
2397         if ($smbd eq "yes") {
2398             # make sure smbd is also up set
2399             print "wait for smbd\n";
2400
2401             my $count = 0;
2402             do {
2403                 $ret = system(Samba::bindir_path($self, "smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
2404                 if ($ret != 0) {
2405                     sleep(1);
2406                 }
2407                 $count++
2408             } while ($ret != 0 && $count < 20);
2409             if ($count == 20) {
2410                 print "SMBD failed to start up in a reasonable time (20sec)\n";
2411                 teardown_env($self, $envvars);
2412                 return 0;
2413             }
2414         }
2415
2416         # Ensure we have domain users mapped.
2417         $netcmd = "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2418         $netcmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2419         $netcmd .= Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} ";
2420
2421         $cmd = $netcmd . "groupmap delete ntgroup=domusers";
2422         $ret = system($cmd);
2423
2424         $cmd = $netcmd . "groupmap add rid=513 unixgroup=domusers type=domain";
2425         $ret = system($cmd);
2426         if ($ret != 0) {
2427                 print("\"$cmd\" failed\n");
2428                 return 1;
2429         }
2430
2431         $cmd = $netcmd . "groupmap delete ntgroup=domadmins";
2432         $ret = system($cmd);
2433
2434         $cmd = $netcmd . "groupmap add rid=512 unixgroup=domadmins type=domain";
2435         $ret = system($cmd);
2436         if ($ret != 0) {
2437                 print("\"$cmd\" failed\n");
2438                 return 1;
2439         }
2440
2441         $cmd = $netcmd . "groupmap delete ntgroup=everyone";
2442         $ret = system($cmd);
2443
2444         $cmd = $netcmd . "groupmap add sid=S-1-1-0 unixgroup=everyone type=builtin";
2445         $ret = system($cmd);
2446         if ($ret != 0) {
2447                 print("\"$cmd\" failed\n");
2448                 return 1;
2449         }
2450
2451         # note: creating builtin groups requires winbindd for the
2452         # unix id allocator
2453         my $create_builtin_users = "no";
2454         if ($winbindd eq "yes") {
2455                 $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2456                 $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2457                 $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2458                 $cmd .= Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545";
2459                 my $wbinfo_out = qx($cmd 2>&1);
2460                 if ($? != 0) {
2461                         # wbinfo doesn't give us a better error code then
2462                         # WBC_ERR_DOMAIN_NOT_FOUND, but at least that's
2463                         # different then WBC_ERR_WINBIND_NOT_AVAILABLE
2464                         if ($wbinfo_out !~ /WBC_ERR_DOMAIN_NOT_FOUND/) {
2465                                 print("Failed to run \"wbinfo --sid-to-gid=S-1-5-32-545\": $wbinfo_out");
2466                                 teardown_env($self, $envvars);
2467                                 return 0;
2468                         }
2469                         $create_builtin_users = "yes";
2470                 }
2471         }
2472         if ($create_builtin_users eq "yes") {
2473             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2474             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2475             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2476             $cmd .= Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} ";
2477             $cmd .= "sam createbuiltingroup Users";
2478             $ret = system($cmd);
2479             if ($ret != 0) {
2480                 print "Failed to create BUILTIN\\Users group\n";
2481                 teardown_env($self, $envvars);
2482                 return 0;
2483             }
2484
2485             $cmd = Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} ";
2486             $cmd .= "cache del IDMAP/SID2XID/S-1-5-32-545";
2487             system($cmd);
2488
2489             $cmd = "SELFTEST_WINBINDD_SOCKET_DIR='$envvars->{SELFTEST_WINBINDD_SOCKET_DIR}' ";
2490             $cmd .= "NSS_WRAPPER_PASSWD='$envvars->{NSS_WRAPPER_PASSWD}' ";
2491             $cmd .= "NSS_WRAPPER_GROUP='$envvars->{NSS_WRAPPER_GROUP}' ";
2492             $cmd .= Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545";
2493             $ret = system($cmd);
2494             if ($ret != 0) {
2495                 print "Missing \"BUILTIN\\Users\", did net sam createbuiltingroup Users fail?\n";
2496                 teardown_env($self, $envvars);
2497                 return 0;
2498             }
2499         }
2500
2501         print $self->getlog_env($envvars);
2502
2503         return 1;
2504 }
2505
2506 1;