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