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