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