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