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