selftest: Load time_audit and full_audit
[amitay/samba.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
14 sub have_ads($) {
15         my ($self) = @_;
16         my $found_ads = 0;
17         my $smbd_build_options = Samba::bindir_path($self, "smbd") . " -b|";
18         open(IN, $smbd_build_options) or die("Unable to run $smbd_build_options: $!");
19
20         while (<IN>) {
21                 if (/WITH_ADS/) {
22                        $found_ads = 1;
23                 }
24         }
25         close IN;
26
27         # If we were not built with ADS support, pretend we were never even available
28         print "smbd does not have ADS support\n" unless $found_ads;
29         return $found_ads;
30 }
31
32 # return smb.conf parameters applicable to @path, based on the underlying
33 # filesystem type
34 sub get_fs_specific_conf($$)
35 {
36         my ($self, $path) = @_;
37         my $mods = "";
38         my $stat_out = `stat --file-system $path` or return "";
39
40         if ($stat_out =~ m/Type:\s+btrfs/) {
41                 $mods .= "btrfs ";
42         }
43
44         if ($mods) {
45                 return "vfs objects = $mods";
46         }
47
48         return undef;
49 }
50
51 sub new($$) {
52         my ($classname, $bindir, $srcdir, $server_maxtime) = @_;
53         my $self = { vars => {},
54                      bindir => $bindir,
55                      srcdir => $srcdir,
56                      server_maxtime => $server_maxtime
57         };
58         bless $self;
59         return $self;
60 }
61
62 sub teardown_env($$)
63 {
64         my ($self, $envvars) = @_;
65         my $count = 0;
66
67         # This should cause smbd to terminate gracefully
68         close($envvars->{STDIN_PIPE});
69
70         my $smbdpid = $envvars->{SMBD_TL_PID};
71         my $nmbdpid = $envvars->{NMBD_TL_PID};
72         my $winbinddpid = $envvars->{WINBINDD_TL_PID};
73
74         # This should give it time to write out the gcov data
75         until ($count > 20) {
76             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
77             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
78             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
79             if ($smbdchild == -1
80                 && $nmbdchild == -1
81                 && $winbinddchild == -1) {
82                 last;
83             }
84             sleep(1);
85             $count++;
86         }
87
88         if ($count <= 20 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
89             return;
90         }
91
92         $self->stop_sig_term($smbdpid);
93         $self->stop_sig_term($nmbdpid);
94         $self->stop_sig_term($winbinddpid);
95
96         $count = 0;
97         until ($count > 10) {
98             my $smbdchild = Samba::cleanup_child($smbdpid, "smbd");
99             my $nmbdchild = Samba::cleanup_child($nmbdpid, "nmbd");
100             my $winbinddchild = Samba::cleanup_child($winbinddpid, "winbindd");
101             if ($smbdchild == -1
102                 && $nmbdchild == -1
103                 && $winbinddchild == -1) {
104                 last;
105             }
106             sleep(1);
107             $count++;
108         }
109
110         if ($count <= 10 && kill(0, $smbdpid, $nmbdpid, $winbinddpid) == 0) {
111             return;
112         }
113
114         warn("timelimit process did not quit on SIGTERM, sending SIGKILL");
115         $self->stop_sig_kill($smbdpid);
116         $self->stop_sig_kill($nmbdpid);
117         $self->stop_sig_kill($winbinddpid);
118
119         return 0;
120 }
121
122 sub getlog_env_app($$$)
123 {
124         my ($self, $envvars, $name) = @_;
125
126         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
127         my $out = $title;
128
129         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
130
131         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
132         while (<LOG>) {
133                 $out .= $_;
134         }
135         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
136         close(LOG);
137
138         return "" if $out eq $title;
139  
140         return $out;
141 }
142
143 sub getlog_env($$)
144 {
145         my ($self, $envvars) = @_;
146         my $ret = "";
147
148         $ret .= $self->getlog_env_app($envvars, "SMBD");
149         $ret .= $self->getlog_env_app($envvars, "NMBD");
150         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
151
152         return $ret;
153 }
154
155 sub check_env($$)
156 {
157         my ($self, $envvars) = @_;
158
159         my $childpid = waitpid(-1, WNOHANG);
160
161         # TODO ...
162         return 1;
163 }
164
165 sub setup_env($$$)
166 {
167         my ($self, $envname, $path) = @_;
168
169         $ENV{ENVNAME} = $envname;
170
171         if (defined($self->{vars}->{$envname})) {
172                 return $self->{vars}->{$envname};
173         }
174
175         #
176         # Avoid hitting system krb5.conf -
177         # An env that needs Kerberos will reset this to the real
178         # value.
179         #
180         $ENV{KRB5_CONFIG} = "$path/no_krb5.conf";
181
182         if ($envname eq "nt4_dc") {
183                 return $self->setup_nt4_dc("$path/nt4_dc");
184         } elsif ($envname eq "nt4_dc_schannel") {
185                 return $self->setup_nt4_dc_schannel("$path/nt4_dc_schannel");
186         } elsif ($envname eq "simpleserver") {
187                 return $self->setup_simpleserver("$path/simpleserver");
188         } elsif ($envname eq "fileserver") {
189                 return $self->setup_fileserver("$path/fileserver");
190         } elsif ($envname eq "maptoguest") {
191                 return $self->setup_maptoguest("$path/maptoguest");
192         } elsif ($envname eq "ktest") {
193                 return $self->setup_ktest("$path/ktest");
194         } elsif ($envname eq "nt4_member") {
195                 if (not defined($self->{vars}->{nt4_dc})) {
196                         if (not defined($self->setup_nt4_dc("$path/nt4_dc"))) {
197                                 return undef;
198                         }
199                 }
200                 return $self->setup_nt4_member("$path/nt4_member", $self->{vars}->{nt4_dc});
201         } else {
202                 return "UNKNOWN";
203         }
204 }
205
206 sub setup_nt4_dc($$)
207 {
208         my ($self, $path) = @_;
209
210         print "PROVISIONING NT4 DC...";
211
212         my $nt4_dc_options = "
213         domain master = yes
214         domain logons = yes
215         lanman auth = yes
216
217         rpc_server:epmapper = external
218         rpc_server:spoolss = external
219         rpc_server:lsarpc = external
220         rpc_server:samr = external
221         rpc_server:netlogon = external
222         rpc_server:register_embedded_np = yes
223         rpc_server:FssagentRpc = external
224
225         rpc_daemon:epmd = fork
226         rpc_daemon:spoolssd = fork
227         rpc_daemon:lsasd = fork
228         rpc_daemon:fssd = fork
229         fss: sequence timeout = 1
230 ";
231
232         my $vars = $self->provision($path,
233                                     "LOCALNT4DC2",
234                                     "localntdc2pass",
235                                     $nt4_dc_options);
236
237         $vars or return undef;
238
239         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
240                return undef;
241         }
242
243         $vars->{DC_SERVER} = $vars->{SERVER};
244         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
245         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
246         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
247         $vars->{DC_USERNAME} = $vars->{USERNAME};
248         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
249
250         $self->{vars}->{nt4_dc} = $vars;
251
252         return $vars;
253 }
254
255 sub setup_nt4_dc_schannel($$)
256 {
257         my ($self, $path) = @_;
258
259         print "PROVISIONING NT4 DC WITH SERVER SCHANNEL ...";
260
261         my $pdc_options = "
262         domain master = yes
263         domain logons = yes
264         lanman auth = yes
265
266         rpc_server:epmapper = external
267         rpc_server:spoolss = external
268         rpc_server:lsarpc = external
269         rpc_server:samr = external
270         rpc_server:netlogon = external
271         rpc_server:register_embedded_np = yes
272
273         rpc_daemon:epmd = fork
274         rpc_daemon:spoolssd = fork
275         rpc_daemon:lsasd = fork
276
277         server schannel = yes
278 ";
279
280         my $vars = $self->provision($path,
281                                     "LOCALNT4DC9",
282                                     "localntdc9pass",
283                                     $pdc_options);
284
285         $vars or return undef;
286
287         if (not $self->check_or_start($vars, "yes", "yes", "yes")) {
288                return undef;
289         }
290
291         $vars->{DC_SERVER} = $vars->{SERVER};
292         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
293         $vars->{DC_SERVER_IPV6} = $vars->{SERVER_IPV6};
294         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
295         $vars->{DC_USERNAME} = $vars->{USERNAME};
296         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
297
298         $self->{vars}->{nt4_dc_schannel} = $vars;
299
300         return $vars;
301 }
302
303 sub setup_nt4_member($$$)
304 {
305         my ($self, $prefix, $nt4_dc_vars) = @_;
306         my $count = 0;
307         my $rc;
308
309         print "PROVISIONING MEMBER...";
310
311         my $require_mutexes = "dbwrap_tdb_require_mutexes:* = yes";
312         $require_mutexes = "" if ($ENV{SELFTEST_DONT_REQUIRE_TDB_MUTEX_SUPPORT} eq "1");
313
314         my $member_options = "
315         security = domain
316         server signing = on
317         dbwrap_tdb_mutexes:* = yes
318         ${require_mutexes}
319 ";
320         my $ret = $self->provision($prefix,
321                                    "LOCALNT4MEMBER3",
322                                    "localnt4member3pass",
323                                    $member_options);
324
325         $ret or return undef;
326
327         my $nmblookup = Samba::bindir_path($self, "nmblookup");
328         do {
329                 print "Waiting for the LOGON SERVER registration ...\n";
330                 $rc = system("$nmblookup $ret->{CONFIGURATION} $ret->{DOMAIN}\#1c");
331                 if ($rc != 0) {
332                         sleep(1);
333                 }
334                 $count++;
335         } while ($rc != 0 && $count < 10);
336         if ($count == 10) {
337                 print "NMBD not reachable after 10 retries\n";
338                 teardown_env($self, $ret);
339                 return 0;
340         }
341
342         my $net = Samba::bindir_path($self, "net");
343         my $cmd = "";
344         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
345         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
346         $cmd .= "$net join $ret->{CONFIGURATION} $nt4_dc_vars->{DOMAIN} member";
347         $cmd .= " -U$nt4_dc_vars->{USERNAME}\%$nt4_dc_vars->{PASSWORD}";
348
349         if (system($cmd) != 0) {
350             warn("Join failed\n$cmd");
351             return undef;
352         }
353
354         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
355                return undef;
356         }
357
358         $ret->{DC_SERVER} = $nt4_dc_vars->{SERVER};
359         $ret->{DC_SERVER_IP} = $nt4_dc_vars->{SERVER_IP};
360         $ret->{DC_SERVER_IPV6} = $nt4_dc_vars->{SERVER_IPV6};
361         $ret->{DC_NETBIOSNAME} = $nt4_dc_vars->{NETBIOSNAME};
362         $ret->{DC_USERNAME} = $nt4_dc_vars->{USERNAME};
363         $ret->{DC_PASSWORD} = $nt4_dc_vars->{PASSWORD};
364
365         return $ret;
366 }
367
368 sub setup_admember($$$$)
369 {
370         my ($self, $prefix, $dcvars) = @_;
371
372         # If we didn't build with ADS, pretend this env was never available
373         if (not $self->have_ads()) {
374                 return "UNKNOWN";
375         }
376
377         print "PROVISIONING S3 AD MEMBER...";
378
379         my $member_options = "
380         security = ads
381         server signing = on
382         workgroup = $dcvars->{DOMAIN}
383         realm = $dcvars->{REALM}
384 ";
385
386         my $ret = $self->provision($prefix,
387                                    "LOCALADMEMBER",
388                                    "loCalMemberPass",
389                                    $member_options,
390                                    $dcvars->{SERVER_IP},
391                                    $dcvars->{SERVER_IPV6});
392
393         $ret or return undef;
394
395         close(USERMAP);
396         $ret->{DOMAIN} = $dcvars->{DOMAIN};
397         $ret->{REALM} = $dcvars->{REALM};
398
399         my $ctx;
400         my $prefix_abs = abs_path($prefix);
401         $ctx = {};
402         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
403         $ctx->{domain} = $dcvars->{DOMAIN};
404         $ctx->{realm} = $dcvars->{REALM};
405         $ctx->{dnsname} = lc($dcvars->{REALM});
406         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
407         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
408         Samba::mk_krb5_conf($ctx, "");
409
410         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
411
412         my $net = Samba::bindir_path($self, "net");
413         my $cmd = "";
414         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
415         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
416                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
417         } else {
418                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
419         }
420         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
421         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
422         $cmd .= "$net join $ret->{CONFIGURATION}";
423         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
424
425         if (system($cmd) != 0) {
426             warn("Join failed\n$cmd");
427             return undef;
428         }
429
430         # We need world access to this share, as otherwise the domain
431         # administrator from the AD domain provided by Samba4 can't
432         # access the share for tests.
433         chmod 0777, "$prefix/share";
434
435         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
436                 return undef;
437         }
438
439         $ret->{DC_SERVER} = $dcvars->{SERVER};
440         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
441         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
442         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
443         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
444         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
445
446         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
447         $ret->{target} = $self;
448
449         return $ret;
450 }
451
452 sub setup_admember_rfc2307($$$$)
453 {
454         my ($self, $prefix, $dcvars) = @_;
455
456         # If we didn't build with ADS, pretend this env was never available
457         if (not $self->have_ads()) {
458                 return "UNKNOWN";
459         }
460
461         print "PROVISIONING S3 AD MEMBER WITH idmap_rfc2307 config...";
462
463         my $member_options = "
464         security = ads
465         server signing = on
466         workgroup = $dcvars->{DOMAIN}
467         realm = $dcvars->{REALM}
468         idmap config $dcvars->{DOMAIN} : backend = rfc2307
469         idmap config $dcvars->{DOMAIN} : range = 2000000-2999999
470         idmap config $dcvars->{DOMAIN} : ldap_server = ad
471         idmap config $dcvars->{DOMAIN} : bind_path_user = ou=idmap,dc=samba,dc=example,dc=com
472         idmap config $dcvars->{DOMAIN} : bind_path_group = ou=idmap,dc=samba,dc=example,dc=com
473 ";
474
475         my $ret = $self->provision($prefix,
476                                    "RFC2307MEMBER",
477                                    "loCalMemberPass",
478                                    $member_options,
479                                    $dcvars->{SERVER_IP},
480                                    $dcvars->{SERVER_IPV6});
481
482         $ret or return undef;
483
484         close(USERMAP);
485         $ret->{DOMAIN} = $dcvars->{DOMAIN};
486         $ret->{REALM} = $dcvars->{REALM};
487
488         my $ctx;
489         my $prefix_abs = abs_path($prefix);
490         $ctx = {};
491         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
492         $ctx->{domain} = $dcvars->{DOMAIN};
493         $ctx->{realm} = $dcvars->{REALM};
494         $ctx->{dnsname} = lc($dcvars->{REALM});
495         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
496         $ctx->{kdc_ipv6} = $dcvars->{SERVER_IPV6};
497         Samba::mk_krb5_conf($ctx, "");
498
499         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
500
501         my $net = Samba::bindir_path($self, "net");
502         my $cmd = "";
503         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
504         if (defined($ret->{RESOLV_WRAPPER_CONF})) {
505                 $cmd .= "RESOLV_WRAPPER_CONF=\"$ret->{RESOLV_WRAPPER_CONF}\" ";
506         } else {
507                 $cmd .= "RESOLV_WRAPPER_HOSTS=\"$ret->{RESOLV_WRAPPER_HOSTS}\" ";
508         }
509         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
510         $cmd .= "SELFTEST_WINBINDD_SOCKET_DIR=\"$ret->{SELFTEST_WINBINDD_SOCKET_DIR}\" ";
511         $cmd .= "$net join $ret->{CONFIGURATION}";
512         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
513
514         if (system($cmd) != 0) {
515             warn("Join failed\n$cmd");
516             return undef;
517         }
518
519         # We need world access to this share, as otherwise the domain
520         # administrator from the AD domain provided by Samba4 can't
521         # access the share for tests.
522         chmod 0777, "$prefix/share";
523
524         if (not $self->check_or_start($ret, "yes", "yes", "yes")) {
525                 return undef;
526         }
527
528         $ret->{DC_SERVER} = $dcvars->{SERVER};
529         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
530         $ret->{DC_SERVER_IPV6} = $dcvars->{SERVER_IPV6};
531         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
532         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
533         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
534
535         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
536         $ret->{target} = $self;
537
538         return $ret;
539 }
540
541 sub setup_simpleserver($$)
542 {
543         my ($self, $path) = @_;
544
545         print "PROVISIONING simple server...";
546
547         my $prefix_abs = abs_path($path);
548
549         my $simpleserver_options = "
550         lanman auth = yes
551         vfs objects = xattr_tdb streams_depot time_audit full_audit
552         change notify = no
553
554 [vfs_aio_fork]
555         path = $prefix_abs/share
556         vfs objects = aio_fork
557         read only = no
558         vfs_aio_fork:erratic_testing_mode=yes
559 ";
560
561         my $vars = $self->provision($path,
562                                     "LOCALSHARE4",
563                                     "local4pass",
564                                     $simpleserver_options);
565
566         $vars or return undef;
567
568         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
569                return undef;
570         }
571
572         $self->{vars}->{simpleserver} = $vars;
573
574         return $vars;
575 }
576
577 sub setup_fileserver($$)
578 {
579         my ($self, $path) = @_;
580         my $prefix_abs = abs_path($path);
581         my $srcdir_abs = abs_path($self->{srcdir});
582
583         print "PROVISIONING file server ...\n";
584
585         my @dirs = ();
586
587         mkdir($prefix_abs, 0777);
588
589         my $share_dir="$prefix_abs/share";
590
591         # Create share directory structure
592         my $lower_case_share_dir="$share_dir/lower-case";
593         push(@dirs, $lower_case_share_dir);
594
595         my $lower_case_share_dir_30000="$share_dir/lower-case-30000";
596         push(@dirs, $lower_case_share_dir_30000);
597
598         my $dfree_share_dir="$share_dir/dfree";
599         push(@dirs, $dfree_share_dir);
600         push(@dirs, "$dfree_share_dir/subdir1");
601         push(@dirs, "$dfree_share_dir/subdir2");
602
603         my $valid_users_sharedir="$share_dir/valid_users";
604         push(@dirs,$valid_users_sharedir);
605
606         my $offline_sharedir="$share_dir/offline";
607         push(@dirs,$offline_sharedir);
608
609         my $force_user_valid_users_dir = "$share_dir/force_user_valid_users";
610         push(@dirs, $force_user_valid_users_dir);
611
612         my $smbget_sharedir="$share_dir/smbget";
613         push(@dirs,$smbget_sharedir);
614
615         my $fileserver_options = "
616 [lowercase]
617         path = $lower_case_share_dir
618         comment = smb username is [%U]
619         case sensitive = True
620         default case = lower
621         preserve case = no
622         short preserve case = no
623 [lowercase-30000]
624         path = $lower_case_share_dir_30000
625         comment = smb username is [%U]
626         case sensitive = True
627         default case = lower
628         preserve case = no
629         short preserve case = no
630 [dfree]
631         path = $dfree_share_dir
632         comment = smb username is [%U]
633         dfree command = $srcdir_abs/testprogs/blackbox/dfree.sh
634 [valid-users-access]
635         path = $valid_users_sharedir
636         valid users = +userdup
637 [offline]
638         path = $offline_sharedir
639         vfs objects = offline
640
641 # BUG: https://bugzilla.samba.org/show_bug.cgi?id=9878
642 # RH BUG: https://bugzilla.redhat.com/show_bug.cgi?id=1077651
643 [force_user_valid_users]
644         path = $force_user_valid_users_dir
645         comment = force user with valid users combination test share
646         valid users = +force_user
647         force user = force_user
648         force group = everyone
649         write list = force_user
650
651 [smbget]
652         path = $smbget_sharedir
653         comment = smb username is [%U]
654         guest ok = yes
655 [ign_sysacls]
656         path = $share_dir
657         comment = ignore system acls
658         acl_xattr:ignore system acls = yes
659 ";
660
661         my $vars = $self->provision($path,
662                                     "FILESERVER",
663                                     "fileserver",
664                                     $fileserver_options,
665                                     undef,
666                                     undef,
667                                     1);
668
669         $vars or return undef;
670
671         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
672                return undef;
673         }
674
675         $self->{vars}->{fileserver} = $vars;
676
677         mkdir($_, 0777) foreach(@dirs);
678
679         ## Create case sensitive lower case share dir
680         foreach my $file ('a'..'z') {
681                 my $full_path = $lower_case_share_dir . '/' . $file;
682                 open my $fh, '>', $full_path;
683                 # Add some content to file
684                 print $fh $full_path;
685                 close $fh;
686         }
687
688         for (my $file = 1; $file < 51; ++$file) {
689                 my $full_path = $lower_case_share_dir . '/' . $file;
690                 open my $fh, '>', $full_path;
691                 # Add some content to file
692                 print $fh $full_path;
693                 close $fh;
694         }
695
696         # Create content for 30000 share
697         foreach my $file ('a'..'z') {
698                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
699                 open my $fh, '>', $full_path;
700                 # Add some content to file
701                 print $fh $full_path;
702                 close $fh;
703         }
704
705         for (my $file = 1; $file < 30001; ++$file) {
706                 my $full_path = $lower_case_share_dir_30000 . '/' . $file;
707                 open my $fh, '>', $full_path;
708                 # Add some content to file
709                 print $fh $full_path;
710                 close $fh;
711         }
712
713         ##
714         ## create a listable file in valid_users_share
715         ##
716         my $valid_users_target = "$valid_users_sharedir/foo";
717         unless (open(VALID_USERS_TARGET, ">$valid_users_target")) {
718                 warn("Unable to open $valid_users_target");
719                 return undef;
720         }
721         close(VALID_USERS_TARGET);
722         chmod 0644, $valid_users_target;
723
724         return $vars;
725 }
726
727 sub setup_ktest($$$)
728 {
729         my ($self, $prefix) = @_;
730
731         # If we didn't build with ADS, pretend this env was never available
732         if (not $self->have_ads()) {
733                 return "UNKNOWN";
734         }
735
736         print "PROVISIONING server with security=ads...";
737
738         my $ktest_options = "
739         workgroup = KTEST
740         realm = ktest.samba.example.com
741         security = ads
742         username map = $prefix/lib/username.map
743         server signing = required
744 ";
745
746         my $ret = $self->provision($prefix,
747                                    "LOCALKTEST6",
748                                    "localktest6pass",
749                                    $ktest_options);
750
751         $ret or return undef;
752
753         my $ctx;
754         my $prefix_abs = abs_path($prefix);
755         $ctx = {};
756         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
757         $ctx->{domain} = "KTEST";
758         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
759         $ctx->{dnsname} = lc($ctx->{realm});
760         $ctx->{kdc_ipv4} = "0.0.0.0";
761         $ctx->{kdc_ipv6} = "::";
762         Samba::mk_krb5_conf($ctx, "");
763
764         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
765
766         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
767         print USERMAP "
768 $ret->{USERNAME} = KTEST\\Administrator
769 ";
770         close(USERMAP);
771
772 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
773 #Samba4 DC with the same parameters as are being used here.  The
774 #domain SID is S-1-5-21-1071277805-689288055-3486227160
775
776         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
777         chmod 0600, "$prefix/private/secrets.tdb";
778
779 #Make sure there's no old ntdb file.
780         system("rm -f $prefix/private/secrets.ntdb");
781
782 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
783 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
784 #
785 #and having in krb5.conf:
786 # ticket_lifetime = 799718400
787 # renew_lifetime = 799718400
788 #
789 # The commands for the -2 keytab where were:
790 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
791 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
792 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
793 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
794 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
795 #
796 # and then for the -3 keytab, I did
797 #
798 # net changetrustpw; kdestroy and the same again.
799 #
800 # This creates a credential cache with a very long lifetime (2036 at
801 # at 2011-04), and shows that running 'net changetrustpw' does not
802 # break existing logins (for the secrets.tdb method at least).
803 #
804
805         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
806
807         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
808         chmod 0600, "$prefix/krb5_ccache-2";
809
810         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
811         chmod 0600, "$prefix/krb5_ccache-3";
812
813         # We need world access to this share, as otherwise the domain
814         # administrator from the AD domain provided by ktest can't
815         # access the share for tests.
816         chmod 0777, "$prefix/share";
817
818         if (not $self->check_or_start($ret, "yes", "no", "yes")) {
819                return undef;
820         }
821         return $ret;
822 }
823
824 sub setup_maptoguest($$)
825 {
826         my ($self, $path) = @_;
827
828         print "PROVISIONING maptoguest...";
829
830         my $options = "
831 map to guest = bad user
832 ";
833
834         my $vars = $self->provision($path,
835                                     "maptoguest",
836                                     "maptoguestpass",
837                                     $options);
838
839         $vars or return undef;
840
841         if (not $self->check_or_start($vars, "yes", "no", "yes")) {
842                return undef;
843         }
844
845         $self->{vars}->{s3maptoguest} = $vars;
846
847         return $vars;
848 }
849
850 sub stop_sig_term($$) {
851         my ($self, $pid) = @_;
852         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
853 }
854
855 sub stop_sig_kill($$) {
856         my ($self, $pid) = @_;
857         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
858 }
859
860 sub write_pid($$$)
861 {
862         my ($env_vars, $app, $pid) = @_;
863
864         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
865         print PID $pid;
866         close(PID);
867 }
868
869 sub read_pid($$)
870 {
871         my ($env_vars, $app) = @_;
872
873         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
874         my $pid = <PID>;
875         close(PID);
876         return $pid;
877 }
878
879 sub check_or_start($$$$$) {
880         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
881
882         # use a pipe for stdin in the child processes. This allows
883         # those processes to monitor the pipe for EOF to ensure they
884         # exit when the test script exits
885         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
886
887         unlink($env_vars->{NMBD_TEST_LOG});
888         print "STARTING NMBD...";
889         my $pid = fork();
890         if ($pid == 0) {
891                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
892                 open STDERR, '>&STDOUT';
893
894                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
895
896                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
897                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
898                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
899
900                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
901                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
902                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
903                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
904                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
905                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
906                 $ENV{UID_WRAPPER_ROOT} = "1";
907
908                 $ENV{ENVNAME} = "$ENV{ENVNAME}.nmbd";
909
910                 if ($nmbd ne "yes") {
911                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
912                                 my $signame = shift;
913                                 print("Skip nmbd received signal $signame");
914                                 exit 0;
915                         };
916                         sleep($self->{server_maxtime});
917                         exit 0;
918                 }
919
920                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
921                 my @optargs = ("-d0");
922                 if (defined($ENV{NMBD_OPTIONS})) {
923                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
924                 }
925                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
926                 if(defined($ENV{NMBD_VALGRIND})) { 
927                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
928                 }
929                 my @args = ("-F", "--no-process-group",
930                             "-s", $env_vars->{SERVERCONFFILE},
931                             "-l", $env_vars->{LOGDIR});
932                 if (not defined($ENV{NMBD_DONT_LOG_STDOUT})) {
933                         push(@args, "--log-stdout");
934                 }
935
936                 close($env_vars->{STDIN_PIPE});
937                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
938
939                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
940                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
941         }
942         $env_vars->{NMBD_TL_PID} = $pid;
943         write_pid($env_vars, "nmbd", $pid);
944         print "DONE\n";
945
946         unlink($env_vars->{WINBINDD_TEST_LOG});
947         print "STARTING WINBINDD...";
948         $pid = fork();
949         if ($pid == 0) {
950                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
951                 open STDERR, '>&STDOUT';
952
953                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
954
955                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
956                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
957                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
958
959                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
960                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
961                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
962                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
963                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
964                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
965                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
966                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
967                 } else {
968                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
969                 }
970                 $ENV{UID_WRAPPER_ROOT} = "1";
971
972                 $ENV{ENVNAME} = "$ENV{ENVNAME}.winbindd";
973
974                 if ($winbindd ne "yes") {
975                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
976                                 my $signame = shift;
977                                 print("Skip winbindd received signal $signame");
978                                 exit 0;
979                         };
980                         sleep($self->{server_maxtime});
981                         exit 0;
982                 }
983
984                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
985                 my @optargs = ("-d0");
986                 if (defined($ENV{WINBINDD_OPTIONS})) {
987                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
988                 }
989                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
990                 if(defined($ENV{WINBINDD_VALGRIND})) {
991                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
992                 }
993                 my @args = ("-F", "--no-process-group",
994                             "-s", $env_vars->{SERVERCONFFILE},
995                             "-l", $env_vars->{LOGDIR});
996                 if (not defined($ENV{WINBINDD_DONT_LOG_STDOUT})) {
997                         push(@args, "--stdout");
998                 }
999
1000                 close($env_vars->{STDIN_PIPE});
1001                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1002
1003                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1004                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1005         }
1006         $env_vars->{WINBINDD_TL_PID} = $pid;
1007         write_pid($env_vars, "winbindd", $pid);
1008         print "DONE\n";
1009
1010         unlink($env_vars->{SMBD_TEST_LOG});
1011         print "STARTING SMBD...";
1012         $pid = fork();
1013         if ($pid == 0) {
1014                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
1015                 open STDERR, '>&STDOUT';
1016
1017                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
1018
1019                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
1020                 $ENV{SELFTEST_WINBINDD_SOCKET_DIR} = $env_vars->{SELFTEST_WINBINDD_SOCKET_DIR};
1021                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
1022
1023                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
1024                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
1025                 $ENV{NSS_WRAPPER_HOSTS} = $env_vars->{NSS_WRAPPER_HOSTS};
1026                 $ENV{NSS_WRAPPER_HOSTNAME} = $env_vars->{NSS_WRAPPER_HOSTNAME};
1027                 $ENV{NSS_WRAPPER_MODULE_SO_PATH} = $env_vars->{NSS_WRAPPER_MODULE_SO_PATH};
1028                 $ENV{NSS_WRAPPER_MODULE_FN_PREFIX} = $env_vars->{NSS_WRAPPER_MODULE_FN_PREFIX};
1029                 if (defined($env_vars->{RESOLV_WRAPPER_CONF})) {
1030                         $ENV{RESOLV_WRAPPER_CONF} = $env_vars->{RESOLV_WRAPPER_CONF};
1031                 } else {
1032                         $ENV{RESOLV_WRAPPER_HOSTS} = $env_vars->{RESOLV_WRAPPER_HOSTS};
1033                 }
1034                 $ENV{UID_WRAPPER_ROOT} = "1";
1035
1036                 $ENV{ENVNAME} = "$ENV{ENVNAME}.smbd";
1037
1038                 if ($smbd ne "yes") {
1039                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
1040                                 my $signame = shift;
1041                                 print("Skip smbd received signal $signame");
1042                                 exit 0;
1043                         };
1044                         sleep($self->{server_maxtime});
1045                         exit 0;
1046                 }
1047
1048                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
1049                 my @optargs = ("-d0");
1050                 if (defined($ENV{SMBD_OPTIONS})) {
1051                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
1052                 }
1053                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
1054                 if(defined($ENV{SMBD_VALGRIND})) {
1055                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
1056                 }
1057                 my @args = ("-F", "--no-process-group",
1058                             "-s", $env_vars->{SERVERCONFFILE},
1059                             "-l", $env_vars->{LOGDIR});
1060                 if (not defined($ENV{SMBD_DONT_LOG_STDOUT})) {
1061                         push(@args, "--log-stdout");
1062                 }
1063
1064                 close($env_vars->{STDIN_PIPE});
1065                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
1066
1067                 exec(@preargs, $ENV{MAKE_TEST_BINARY}, @args, @optargs)
1068                         or die("Unable to start $ENV{MAKE_TEST_BINARY}: $!");
1069         }
1070         $env_vars->{SMBD_TL_PID} = $pid;
1071         write_pid($env_vars, "smbd", $pid);
1072         print "DONE\n";
1073
1074         close(STDIN_READER);
1075
1076         return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
1077 }
1078
1079 sub createuser($$$$)
1080 {
1081         my ($self, $username, $password, $conffile) = @_;
1082         my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $username > /dev/null";
1083         unless (open(PWD, "|$cmd")) {
1084             warn("Unable to set password for $username account\n$cmd");
1085             return undef;
1086         }
1087         print PWD "$password\n$password\n";
1088         unless (close(PWD)) {
1089             warn("Unable to set password for $username account\n$cmd");
1090             return undef;
1091         }
1092         print "DONE\n";
1093 }
1094
1095 sub provision($$$$$$$$)
1096 {
1097         my ($self, $prefix, $server, $password, $extra_options, $dc_server_ip, $dc_server_ipv6, $no_delete_prefix) = @_;
1098
1099         ##
1100         ## setup the various environment variables we need
1101         ##
1102
1103         my $swiface = Samba::get_interface($server);
1104         my %ret = ();
1105         my $server_ip = "127.0.0.$swiface";
1106         my $server_ipv6 = sprintf("fd00:0000:0000:0000:0000:0000:5357:5f%02x", $swiface);
1107         my $domain = "SAMBA-TEST";
1108
1109         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
1110         chomp $unix_name;
1111         my $unix_uid = $>;
1112         my $unix_gids_str = $);
1113         my @unix_gids = split(" ", $unix_gids_str);
1114
1115         my $prefix_abs = abs_path($prefix);
1116         my $bindir_abs = abs_path($self->{bindir});
1117
1118         my @dirs = ();
1119
1120         my $shrdir="$prefix_abs/share";
1121         push(@dirs,$shrdir);
1122
1123         my $libdir="$prefix_abs/lib";
1124         push(@dirs,$libdir);
1125
1126         my $piddir="$prefix_abs/pid";
1127         push(@dirs,$piddir);
1128
1129         my $privatedir="$prefix_abs/private";
1130         push(@dirs,$privatedir);
1131
1132         my $lockdir="$prefix_abs/lockdir";
1133         push(@dirs,$lockdir);
1134
1135         my $eventlogdir="$prefix_abs/lockdir/eventlog";
1136         push(@dirs,$eventlogdir);
1137
1138         my $logdir="$prefix_abs/logs";
1139         push(@dirs,$logdir);
1140
1141         my $driver32dir="$shrdir/W32X86";
1142         push(@dirs,$driver32dir);
1143
1144         my $driver64dir="$shrdir/x64";
1145         push(@dirs,$driver64dir);
1146
1147         my $driver40dir="$shrdir/WIN40";
1148         push(@dirs,$driver40dir);
1149
1150         my $ro_shrdir="$shrdir/root-tmp";
1151         push(@dirs,$ro_shrdir);
1152
1153         my $msdfs_shrdir="$shrdir/msdfsshare";
1154         push(@dirs,$msdfs_shrdir);
1155
1156         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
1157         push(@dirs,$msdfs_deeppath);
1158
1159         my $badnames_shrdir="$shrdir/badnames";
1160         push(@dirs,$badnames_shrdir);
1161
1162         my $lease1_shrdir="$shrdir/SMB2_10";
1163         push(@dirs,$lease1_shrdir);
1164
1165         my $lease2_shrdir="$shrdir/SMB3_00";
1166         push(@dirs,$lease2_shrdir);
1167
1168         my $manglenames_shrdir="$shrdir/manglenames";
1169         push(@dirs,$manglenames_shrdir);
1170
1171         my $widelinks_shrdir="$shrdir/widelinks";
1172         push(@dirs,$widelinks_shrdir);
1173
1174         my $widelinks_linkdir="$shrdir/widelinks_foo";
1175         push(@dirs,$widelinks_linkdir);
1176
1177         my $shadow_tstdir="$shrdir/shadow";
1178         push(@dirs,$shadow_tstdir);
1179         my $shadow_mntdir="$shadow_tstdir/mount";
1180         push(@dirs,$shadow_mntdir);
1181         my $shadow_basedir="$shadow_mntdir/base";
1182         push(@dirs,$shadow_basedir);
1183         my $shadow_shrdir="$shadow_basedir/share";
1184         push(@dirs,$shadow_shrdir);
1185
1186         # this gets autocreated by winbindd
1187         my $wbsockdir="$prefix_abs/winbindd";
1188         my $wbsockprivdir="$lockdir/winbindd_privileged";
1189
1190         my $nmbdsockdir="$prefix_abs/nmbd";
1191         unlink($nmbdsockdir);
1192
1193         ## 
1194         ## create the test directory layout
1195         ##
1196         die ("prefix_abs = ''") if $prefix_abs eq "";
1197         die ("prefix_abs = '/'") if $prefix_abs eq "/";
1198
1199         mkdir($prefix_abs, 0777);
1200         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
1201         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
1202             system("rm -rf $prefix_abs/*");
1203         }
1204         mkdir($_, 0777) foreach(@dirs);
1205
1206         my $fs_specific_conf = $self->get_fs_specific_conf($shrdir);
1207
1208         ##
1209         ## lockdir and piddir must be 0755
1210         ##
1211         chmod 0755, $lockdir;
1212         chmod 0755, $piddir;
1213
1214
1215         ##
1216         ## create ro and msdfs share layout
1217         ##
1218
1219         chmod 0755, $ro_shrdir;
1220         my $unreadable_file = "$ro_shrdir/unreadable_file";
1221         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
1222                 warn("Unable to open $unreadable_file");
1223                 return undef;
1224         }
1225         close(UNREADABLE_FILE);
1226         chmod 0600, $unreadable_file;
1227
1228         my $msdfs_target = "$ro_shrdir/msdfs-target";
1229         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
1230                 warn("Unable to open $msdfs_target");
1231                 return undef;
1232         }
1233         close(MSDFS_TARGET);
1234         chmod 0666, $msdfs_target;
1235         symlink "msdfs:$server_ip\\ro-tmp,$server_ipv6\\ro-tmp",
1236                 "$msdfs_shrdir/msdfs-src1";
1237         symlink "msdfs:$server_ipv6\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
1238
1239         ##
1240         ## create bad names in $badnames_shrdir
1241         ##
1242         ## (An invalid name, would be mangled to 8.3).
1243         my $badname_target = "$badnames_shrdir/\340|\231\216\377\177";
1244         unless (open(BADNAME_TARGET, ">$badname_target")) {
1245                 warn("Unable to open $badname_target");
1246                 return undef;
1247         }
1248         close(BADNAME_TARGET);
1249         chmod 0666, $badname_target;
1250
1251         ## (A bad name, would not be mangled to 8.3).
1252         my $badname_target = "$badnames_shrdir/\240\276\346\327\377\177";
1253         unless (open(BADNAME_TARGET, ">$badname_target")) {
1254                 warn("Unable to open $badname_target");
1255                 return undef;
1256         }
1257         close(BADNAME_TARGET);
1258         chmod 0666, $badname_target;
1259
1260         ## (A bad good name).
1261         my $badname_target = "$badnames_shrdir/blank.txt";
1262         unless (open(BADNAME_TARGET, ">$badname_target")) {
1263                 warn("Unable to open $badname_target");
1264                 return undef;
1265         }
1266         close(BADNAME_TARGET);
1267         chmod 0666, $badname_target;
1268
1269         ##
1270         ## create mangleable directory names in $manglenames_shrdir
1271         ##
1272         my $manglename_target = "$manglenames_shrdir/foo:bar";
1273         mkdir($manglename_target, 0777);
1274
1275         ##
1276         ## create symlinks for widelinks tests.
1277         ##
1278         my $widelinks_target = "$widelinks_linkdir/target";
1279         unless (open(WIDELINKS_TARGET, ">$widelinks_target")) {
1280                 warn("Unable to open $widelinks_target");
1281                 return undef;
1282         }
1283         close(WIDELINKS_TARGET);
1284         chmod 0666, $widelinks_target;
1285         ##
1286         ## This link should get ACCESS_DENIED
1287         ##
1288         symlink "$widelinks_target", "$widelinks_shrdir/source";
1289         ##
1290         ## This link should be allowed
1291         ##
1292         symlink "$widelinks_shrdir", "$widelinks_shrdir/dot";
1293
1294         my $conffile="$libdir/server.conf";
1295         my $dfqconffile="$libdir/dfq.conf";
1296
1297         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
1298         my $nss_wrapper_passwd = "$privatedir/passwd";
1299         my $nss_wrapper_group = "$privatedir/group";
1300         my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
1301         my $resolv_conf = "$privatedir/resolv.conf";
1302         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
1303
1304         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
1305
1306         my $fake_snap_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/fake_snap.pl";
1307
1308         my @eventlog_list = ("dns server", "application");
1309
1310         ##
1311         ## calculate uids and gids
1312         ##
1313
1314         my ($max_uid, $max_gid);
1315         my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2, $uid_userdup);
1316         my ($uid_pdbtest_wkn);
1317         my ($uid_smbget);
1318         my ($uid_force_user);
1319         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
1320         my ($gid_userdup, $gid_everyone);
1321         my ($gid_force_user);
1322
1323         if ($unix_uid < 0xffff - 7) {
1324                 $max_uid = 0xffff;
1325         } else {
1326                 $max_uid = $unix_uid;
1327         }
1328
1329         $uid_root = $max_uid - 1;
1330         $uid_nobody = $max_uid - 2;
1331         $uid_pdbtest = $max_uid - 3;
1332         $uid_pdbtest2 = $max_uid - 4;
1333         $uid_userdup = $max_uid - 5;
1334         $uid_pdbtest_wkn = $max_uid - 6;
1335         $uid_force_user = $max_uid - 7;
1336         $uid_smbget = $max_uid - 8;
1337
1338         if ($unix_gids[0] < 0xffff - 8) {
1339                 $max_gid = 0xffff;
1340         } else {
1341                 $max_gid = $unix_gids[0];
1342         }
1343
1344         $gid_nobody = $max_gid - 1;
1345         $gid_nogroup = $max_gid - 2;
1346         $gid_root = $max_gid - 3;
1347         $gid_domusers = $max_gid - 4;
1348         $gid_domadmins = $max_gid - 5;
1349         $gid_userdup = $max_gid - 6;
1350         $gid_everyone = $max_gid - 7;
1351         $gid_force_user = $max_gid - 8;
1352
1353         ##
1354         ## create conffile
1355         ##
1356
1357         unless (open(CONF, ">$conffile")) {
1358                 warn("Unable to open $conffile");
1359                 return undef;
1360         }
1361         print CONF "
1362 [global]
1363         netbios name = $server
1364         interfaces = $server_ip/8 $server_ipv6/64
1365         bind interfaces only = yes
1366         panic action = cd $self->{srcdir} && $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
1367         smbd:suicide mode = yes
1368
1369         workgroup = $domain
1370
1371         private dir = $privatedir
1372         pid directory = $piddir
1373         lock directory = $lockdir
1374         log file = $logdir/log.\%m
1375         log level = 1
1376         debug pid = yes
1377         max log size = 0
1378
1379         state directory = $lockdir
1380         cache directory = $lockdir
1381
1382         passdb backend = tdbsam
1383
1384         time server = yes
1385
1386         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1387         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
1388         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
1389         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
1390         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
1391         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
1392         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
1393
1394         addprinter command =            $mod_printer_pl -a -s $conffile --
1395         deleteprinter command =         $mod_printer_pl -d -s $conffile --
1396
1397         eventlog list = application \"dns server\"
1398
1399         kernel oplocks = no
1400         kernel change notify = no
1401         smb2 leases = yes
1402
1403         logging = file
1404         printing = bsd
1405         printcap name = /dev/null
1406
1407         winbindd socket directory = $wbsockdir
1408         nmbd:socket dir = $nmbdsockdir
1409         idmap config * : range = 100000-200000
1410         winbind enum users = yes
1411         winbind enum groups = yes
1412         winbind separator = /
1413
1414 #       min receivefile size = 4000
1415
1416         read only = no
1417         server signing = auto
1418
1419         smbd:sharedelay = 100000
1420         smbd:writetimeupdatedelay = 500000
1421         map hidden = no
1422         map system = no
1423         map readonly = no
1424         store dos attributes = yes
1425         create mask = 755
1426         dos filemode = yes
1427         strict rename = yes
1428         strict sync = yes
1429         vfs objects = acl_xattr fake_acls xattr_tdb streams_depot
1430
1431         printing = vlp
1432         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
1433         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
1434         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
1435         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
1436         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
1437         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
1438         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
1439         lpq cache time = 0
1440         print notify backchannel = yes
1441
1442         ncalrpc dir = $prefix_abs/ncalrpc
1443
1444         # The samba3.blackbox.smbclient_s3 test uses this to test that
1445         # sending messages works, and that the %m sub works.
1446         message command = mv %s $shrdir/message.%m
1447
1448         # fsrvp server requires registry shares
1449         registry shares = yes
1450
1451         # Used by RPC SRVSVC tests
1452         add share command = $bindir_abs/smbaddshare
1453         change share command = $bindir_abs/smbchangeshare
1454         delete share command = $bindir_abs/smbdeleteshare
1455
1456         # fruit:copyfile is a global option
1457         fruit:copyfile = yes
1458
1459         #this does not mean that we use non-secure test env,
1460         #it just means we ALLOW one to be configured.
1461         allow insecure wide links = yes
1462
1463         # Begin extra options
1464         $extra_options
1465         # End extra options
1466
1467         #Include user defined custom parameters if set
1468 ";
1469
1470         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
1471                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
1472         }
1473
1474         print CONF "
1475 [tmp]
1476         path = $shrdir
1477         comment = smb username is [%U]
1478 [tmpsort]
1479         path = $shrdir
1480         comment = Load dirsort module
1481         vfs objects = dirsort acl_xattr fake_acls xattr_tdb streams_depot
1482 [tmpenc]
1483         path = $shrdir
1484         comment = encrypt smb username is [%U]
1485         smb encrypt = required
1486         vfs objects = dirsort
1487 [tmpguest]
1488         path = $shrdir
1489         guest ok = yes
1490 [guestonly]
1491         path = $shrdir
1492         guest only = yes
1493         guest ok = yes
1494 [forceuser]
1495         path = $shrdir
1496         force user = $unix_name
1497         guest ok = yes
1498 [forceuser_unixonly]
1499         comment = force a user with unix user SID and group SID
1500         path = $shrdir
1501         force user = pdbtest
1502         guest ok = yes
1503 [forceuser_wkngroup]
1504         comment = force a user with well-known group SID
1505         path = $shrdir
1506         force user = pdbtest_wkn
1507         guest ok = yes
1508 [forcegroup]
1509         path = $shrdir
1510         force group = nogroup
1511         guest ok = yes
1512 [ro-tmp]
1513         path = $ro_shrdir
1514         guest ok = yes
1515 [write-list-tmp]
1516         path = $shrdir
1517         read only = yes
1518         write list = $unix_name
1519 [valid-users-tmp]
1520         path = $shrdir
1521         valid users = $unix_name
1522         access based share enum = yes
1523 [msdfs-share]
1524         path = $msdfs_shrdir
1525         msdfs root = yes
1526         msdfs shuffle referrals = yes
1527         guest ok = yes
1528 [hideunread]
1529         copy = tmp
1530         hide unreadable = yes
1531 [tmpcase]
1532         copy = tmp
1533         case sensitive = yes
1534 [hideunwrite]
1535         copy = tmp
1536         hide unwriteable files = yes
1537 [durable]
1538         copy = tmp
1539         kernel share modes = no
1540         kernel oplocks = no
1541         posix locking = no
1542 [fs_specific]
1543         copy = tmp
1544         $fs_specific_conf
1545 [print1]
1546         copy = tmp
1547         printable = yes
1548
1549 [print2]
1550         copy = print1
1551 [print3]
1552         copy = print1
1553         default devmode = no
1554 [lp]
1555         copy = print1
1556
1557 [nfs4acl_simple]
1558         path = $shrdir
1559         comment = smb username is [%U]
1560         nfs4:mode = simple
1561         vfs objects = nfs4acl_xattr xattr_tdb
1562
1563 [nfs4acl_special]
1564         path = $shrdir
1565         comment = smb username is [%U]
1566         nfs4:mode = special
1567         vfs objects = nfs4acl_xattr xattr_tdb
1568
1569 [xcopy_share]
1570         path = $shrdir
1571         comment = smb username is [%U]
1572         create mask = 777
1573         force create mode = 777
1574 [posix_share]
1575         path = $shrdir
1576         comment = smb username is [%U]
1577         create mask = 0777
1578         force create mode = 0
1579         directory mask = 0777
1580         force directory mode = 0
1581         vfs objects = xattr_tdb
1582 [aio]
1583         copy = tmp
1584         aio read size = 1
1585         aio write size = 1
1586
1587 [print\$]
1588         copy = tmp
1589
1590 [vfs_fruit]
1591         path = $shrdir
1592         vfs objects = catia fruit streams_xattr acl_xattr
1593         ea support = yes
1594         fruit:ressource = file
1595         fruit:metadata = netatalk
1596         fruit:locking = netatalk
1597         fruit:encoding = native
1598
1599 [badname-tmp]
1600         path = $badnames_shrdir
1601         guest ok = yes
1602
1603 [manglenames_share]
1604         path = $manglenames_shrdir
1605         guest ok = yes
1606
1607 [dynamic_share]
1608         path = $shrdir/%R
1609         guest ok = yes
1610
1611 [widelinks_share]
1612         path = $widelinks_shrdir
1613         wide links = no
1614         guest ok = yes
1615
1616 [fsrvp_share]
1617         path = $shrdir
1618         comment = fake shapshots using rsync
1619         vfs objects = shell_snap shadow_copy2
1620         shell_snap:check path command = $fake_snap_pl --check
1621         shell_snap:create command = $fake_snap_pl --create
1622         shell_snap:delete command = $fake_snap_pl --delete
1623         # a relative path here fails, the snapshot dir is no longer found
1624         shadow:snapdir = $shrdir/.snapshots
1625
1626 [shadow1]
1627         path = $shadow_shrdir
1628         comment = previous versions snapshots under mount point
1629         vfs objects = shadow_copy2
1630         shadow:mountpoint = $shadow_mntdir
1631
1632 [shadow2]
1633         path = $shadow_shrdir
1634         comment = previous versions snapshots outside mount point
1635         vfs objects = shadow_copy2
1636         shadow:mountpoint = $shadow_mntdir
1637         shadow:snapdir = $shadow_tstdir/.snapshots
1638
1639 [shadow3]
1640         path = $shadow_shrdir
1641         comment = previous versions with subvolume snapshots, snapshots under base dir
1642         vfs objects = shadow_copy2
1643         shadow:mountpoint = $shadow_mntdir
1644         shadow:basedir = $shadow_basedir
1645         shadow:snapdir = $shadow_basedir/.snapshots
1646
1647 [shadow4]
1648         path = $shadow_shrdir
1649         comment = previous versions with subvolume snapshots, snapshots outside mount point
1650         vfs objects = shadow_copy2
1651         shadow:mountpoint = $shadow_mntdir
1652         shadow:basedir = $shadow_basedir
1653         shadow:snapdir = $shadow_tstdir/.snapshots
1654
1655 [shadow5]
1656         path = $shadow_shrdir
1657         comment = previous versions at volume root snapshots under mount point
1658         vfs objects = shadow_copy2
1659         shadow:mountpoint = $shadow_shrdir
1660
1661 [shadow6]
1662         path = $shadow_shrdir
1663         comment = previous versions at volume root snapshots outside mount point
1664         vfs objects = shadow_copy2
1665         shadow:mountpoint = $shadow_shrdir
1666         shadow:snapdir = $shadow_tstdir/.snapshots
1667
1668 [shadow7]
1669         path = $shadow_shrdir
1670         comment = previous versions snapshots everywhere
1671         vfs objects = shadow_copy2
1672         shadow:mountpoint = $shadow_mntdir
1673         shadow:snapdirseverywhere = yes
1674
1675 [shadow8]
1676         path = $shadow_shrdir
1677         comment = previous versions using snapsharepath
1678         vfs objects = shadow_copy2
1679         shadow:mountpoint = $shadow_mntdir
1680         shadow:snapdir = $shadow_tstdir/.snapshots
1681         shadow:snapsharepath = share
1682
1683 [shadow_wl]
1684         path = $shadow_shrdir
1685         comment = previous versions with wide links allowed
1686         vfs objects = shadow_copy2
1687         shadow:mountpoint = $shadow_mntdir
1688         wide links = yes
1689 [dfq]
1690         path = $shrdir/dfree
1691         vfs objects = fake_dfq
1692         admin users = $unix_name
1693         include = $dfqconffile
1694         ";
1695         close(CONF);
1696
1697         unless (open(DFQCONF, ">$dfqconffile")) {
1698                 warn("Unable to open $dfqconffile");
1699                 return undef;
1700         }
1701         close(DFQCONF);
1702
1703         ##
1704         ## create a test account
1705         ##
1706
1707         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
1708            warn("Unable to open $nss_wrapper_passwd");
1709            return undef;
1710         } 
1711         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1712 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1713 pdbtest:x:$uid_pdbtest:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1714 pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
1715 userdup:x:$uid_userdup:$gid_userdup:userdup gecos:$prefix_abs:/bin/false
1716 pdbtest_wkn:x:$uid_pdbtest_wkn:$gid_everyone:pdbtest_wkn gecos:$prefix_abs:/bin/false
1717 force_user:x:$uid_force_user:$gid_force_user:force user gecos:$prefix_abs:/bin/false
1718 smbget_user:x:$uid_smbget:$gid_domusers:smbget_user gecos:$prefix_abs:/bin/false
1719 ";
1720         if ($unix_uid != 0) {
1721                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
1722 ";
1723         }
1724         close(PASSWD);
1725
1726         unless (open(GROUP, ">$nss_wrapper_group")) {
1727              warn("Unable to open $nss_wrapper_group");
1728              return undef;
1729         }
1730         print GROUP "nobody:x:$gid_nobody:
1731 nogroup:x:$gid_nogroup:nobody
1732 $unix_name-group:x:$unix_gids[0]:
1733 domusers:X:$gid_domusers:
1734 domadmins:X:$gid_domadmins:
1735 userdup:x:$gid_userdup:$unix_name
1736 everyone:x:$gid_everyone:
1737 force_user:x:$gid_force_user:
1738 ";
1739         if ($unix_gids[0] != 0) {
1740                 print GROUP "root:x:$gid_root:
1741 ";
1742         }
1743
1744         close(GROUP);
1745
1746         ## hosts
1747         my $hostname = lc($server);
1748         unless (open(HOSTS, ">>$nss_wrapper_hosts")) {
1749                 warn("Unable to open $nss_wrapper_hosts");
1750                 return undef;
1751         }
1752         print HOSTS "${server_ip} ${hostname}.samba.example.com ${hostname}\n";
1753         print HOSTS "${server_ipv6} ${hostname}.samba.example.com ${hostname}\n";
1754         close(HOSTS);
1755
1756         ## hosts
1757         unless (open(RESOLV_CONF, ">$resolv_conf")) {
1758                 warn("Unable to open $resolv_conf");
1759                 return undef;
1760         }
1761         if (defined($dc_server_ip) or defined($dc_server_ipv6)) {
1762                 if (defined($dc_server_ip)) {
1763                         print RESOLV_CONF "nameserver $dc_server_ip\n";
1764                 }
1765                 if (defined($dc_server_ipv6)) {
1766                         print RESOLV_CONF "nameserver $dc_server_ipv6\n";
1767                 }
1768         } else {
1769                 print RESOLV_CONF "nameserver ${server_ip}\n";
1770                 print RESOLV_CONF "nameserver ${server_ipv6}\n";
1771         }
1772         close(RESOLV_CONF);
1773
1774         foreach my $evlog (@eventlog_list) {
1775                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1776                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1777                 close(EVENTLOG);
1778         }
1779
1780         $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1781         $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1782         $ENV{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
1783         $ENV{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
1784         if ($ENV{SAMBA_DNS_FAKING}) {
1785                 $ENV{RESOLV_WRAPPER_CONF} = $resolv_conf;
1786         } else {
1787                 $ENV{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
1788         }
1789
1790         createuser($self, $unix_name, $password, $conffile) || die("Unable to create user");
1791         createuser($self, "force_user", $password, $conffile) || die("Unable to create force_user");
1792         createuser($self, "smbget_user", $password, $conffile) || die("Unable to create smbget_user");
1793
1794         open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
1795         print DNS_UPDATE_LIST "A $server. $server_ip\n";
1796         print DNS_UPDATE_LIST "AAAA $server. $server_ipv6\n";
1797         close(DNS_UPDATE_LIST);
1798
1799         $ret{SERVER_IP} = $server_ip;
1800         $ret{SERVER_IPV6} = $server_ipv6;
1801         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1802         $ret{NMBD_TEST_LOG_POS} = 0;
1803         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1804         $ret{WINBINDD_TEST_LOG_POS} = 0;
1805         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1806         $ret{SMBD_TEST_LOG_POS} = 0;
1807         $ret{SERVERCONFFILE} = $conffile;
1808         $ret{CONFIGURATION} ="-s $conffile";
1809         $ret{SERVER} = $server;
1810         $ret{USERNAME} = $unix_name;
1811         $ret{USERID} = $unix_uid;
1812         $ret{DOMAIN} = $domain;
1813         $ret{NETBIOSNAME} = $server;
1814         $ret{PASSWORD} = $password;
1815         $ret{PIDDIR} = $piddir;
1816         $ret{SELFTEST_WINBINDD_SOCKET_DIR} = $wbsockdir;
1817         $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1818         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1819         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1820         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1821         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1822         $ret{NSS_WRAPPER_HOSTS} = $nss_wrapper_hosts;
1823         $ret{NSS_WRAPPER_HOSTNAME} = "${hostname}.samba.example.com";
1824         $ret{NSS_WRAPPER_MODULE_SO_PATH} = Samba::nss_wrapper_winbind_so_path($self);
1825         $ret{NSS_WRAPPER_MODULE_FN_PREFIX} = "winbind";
1826         if ($ENV{SAMBA_DNS_FAKING}) {
1827                 $ret{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
1828         } else {
1829                 $ret{RESOLV_WRAPPER_CONF} = $resolv_conf;
1830         }
1831         $ret{LOCAL_PATH} = "$shrdir";
1832         $ret{LOGDIR} = $logdir;
1833
1834         #
1835         # Avoid hitting system krb5.conf -
1836         # An env that needs Kerberos will reset this to the real
1837         # value.
1838         #
1839         $ret{KRB5_CONFIG} = abs_path($prefix) . "/no_krb5.conf";
1840
1841         return \%ret;
1842 }
1843
1844 sub wait_for_start($$$$$)
1845 {
1846         my ($self, $envvars, $nmbd, $winbindd, $smbd) = @_;
1847         my $ret;
1848
1849         if ($nmbd eq "yes") {
1850                 my $count = 0;
1851
1852                 # give time for nbt server to register its names
1853                 print "checking for nmbd\n";
1854
1855                 # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init
1856                 my $nmblookup = Samba::bindir_path($self, "nmblookup");
1857
1858                 do {
1859                         $ret = system("$nmblookup $envvars->{CONFIGURATION} $envvars->{SERVER}");
1860                         if ($ret != 0) {
1861                                 sleep(1);
1862                         } else {
1863                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1864                                 system("$nmblookup $envvars->{CONFIGURATION} __SAMBA__");
1865                                 system("$nmblookup $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1866                                 system("$nmblookup $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1867                         }
1868                         $count++;
1869                 } while ($ret != 0 && $count < 10);
1870                 if ($count == 10) {
1871                         print "NMBD not reachable after 10 retries\n";
1872                         teardown_env($self, $envvars);
1873                         return 0;
1874                 }
1875         }
1876
1877         if ($winbindd eq "yes") {
1878             print "checking for winbindd\n";
1879             my $count = 0;
1880             do {
1881                 $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --ping-dc");
1882                 if ($ret != 0) {
1883                     sleep(2);
1884                 }
1885                 $count++;
1886             } while ($ret != 0 && $count < 10);
1887             if ($count == 10) {
1888                 print "WINBINDD not reachable after 20 seconds\n";
1889                 teardown_env($self, $envvars);
1890                 return 0;
1891             }
1892         }
1893
1894         if ($smbd eq "yes") {
1895             # make sure smbd is also up set
1896             print "wait for smbd\n";
1897
1898             my $count = 0;
1899             do {
1900                 $ret = system(Samba::bindir_path($self, "smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1901                 if ($ret != 0) {
1902                     sleep(2);
1903                 }
1904                 $count++
1905             } while ($ret != 0 && $count < 10);
1906             if ($count == 10) {
1907                 print "SMBD failed to start up in a reasonable time (20sec)\n";
1908                 teardown_env($self, $envvars);
1909                 return 0;
1910             }
1911         }
1912
1913         # Ensure we have domain users mapped.
1914         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1915         if ($ret != 0) {
1916             return 1;
1917         }
1918         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=512 unixgroup=domadmins type=domain");
1919         if ($ret != 0) {
1920             return 1;
1921         }
1922         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add sid=S-1-1-0 unixgroup=everyone type=builtin");
1923         if ($ret != 0) {
1924             return 1;
1925         }
1926
1927         if ($winbindd eq "yes") {
1928             # note: creating builtin groups requires winbindd for the
1929             # unix id allocator
1930             $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} sam createbuiltingroup Users");
1931             if ($ret != 0) {
1932                 print "Failed to create BUILTIN\\Users group\n";
1933                 return 0;
1934             }
1935             my $count = 0;
1936             do {
1937                 system(Samba::bindir_path($self, "net") . " $envvars->{CONFIGURATION} cache flush");
1938                 $ret = system("SELFTEST_WINBINDD_SOCKET_DIR=" . $envvars->{SELFTEST_WINBINDD_SOCKET_DIR} . " " . Samba::bindir_path($self, "wbinfo") . " --sid-to-gid=S-1-5-32-545");
1939                 if ($ret != 0) {
1940                     sleep(2);
1941                 }
1942                 $count++;
1943             } while ($ret != 0 && $count < 10);
1944             if ($count == 10) {
1945                 print "WINBINDD not reachable after 20 seconds\n";
1946                 teardown_env($self, $envvars);
1947                 return 0;
1948             }
1949         }
1950
1951         print $self->getlog_env($envvars);
1952
1953         return 1;
1954 }
1955
1956 1;