9d74e7db48cde3d7ec9860d44adfb7d416a61240
[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         return $found_ads;
29 }
30
31 sub new($$) {
32         my ($classname, $bindir, $binary_mapping, $srcdir, $server_maxtime) = @_;
33         my $self = { vars => {},
34                      bindir => $bindir,
35                      binary_mapping => $binary_mapping,
36                      srcdir => $srcdir,
37                      server_maxtime => $server_maxtime
38         };
39         bless $self;
40         return $self;
41 }
42
43 sub teardown_env($$)
44 {
45         my ($self, $envvars) = @_;
46
47         my $smbdpid = read_pid($envvars, "smbd");
48         my $nmbdpid = read_pid($envvars, "nmbd");
49         my $winbinddpid = read_pid($envvars, "winbindd");
50
51         $self->stop_sig_term($smbdpid);
52         $self->stop_sig_term($nmbdpid);
53         $self->stop_sig_term($winbinddpid);
54
55         sleep(2);
56
57         $self->stop_sig_kill($smbdpid);
58         $self->stop_sig_kill($nmbdpid);
59         $self->stop_sig_kill($winbinddpid);
60
61         return 0;
62 }
63
64 sub getlog_env_app($$$)
65 {
66         my ($self, $envvars, $name) = @_;
67
68         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
69         my $out = $title;
70
71         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
72
73         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
74         while (<LOG>) {
75                 $out .= $_;
76         }
77         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
78         close(LOG);
79
80         return "" if $out eq $title;
81  
82         return $out;
83 }
84
85 sub getlog_env($$)
86 {
87         my ($self, $envvars) = @_;
88         my $ret = "";
89
90         $ret .= $self->getlog_env_app($envvars, "SMBD");
91         $ret .= $self->getlog_env_app($envvars, "NMBD");
92         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
93
94         return $ret;
95 }
96
97 sub check_env($$)
98 {
99         my ($self, $envvars) = @_;
100
101         # TODO ...
102         return 1;
103 }
104
105 sub setup_env($$$)
106 {
107         my ($self, $envname, $path) = @_;
108         
109         if (defined($self->{vars}->{$envname})) {
110                 return $self->{vars}->{$envname};
111         }
112
113         if ($envname eq "s3dc") {
114                 return $self->setup_s3dc("$path/s3dc");
115         } elsif ($envname eq "secshare") {
116                 return $self->setup_secshare("$path/secshare");
117         } elsif ($envname eq "maptoguest") {
118                 return $self->setup_maptoguest("$path/maptoguest");
119         } elsif ($envname eq "ktest") {
120                 return $self->setup_ktest("$path/ktest");
121         } elsif ($envname eq "secserver") {
122                 if (not defined($self->{vars}->{s3dc})) {
123                         if (not defined($self->setup_s3dc("$path/s3dc"))) {
124                                 return undef;
125                         }
126                 }
127                 return $self->setup_secserver("$path/secserver", $self->{vars}->{s3dc});
128         } elsif ($envname eq "member") {
129                 if (not defined($self->{vars}->{s3dc})) {
130                         if (not defined($self->setup_s3dc("$path/s3dc"))) {
131                                 return undef;
132                         }
133                 }
134                 return $self->setup_member("$path/member", $self->{vars}->{s3dc});
135         } else {
136                 return "UNKNOWN";
137         }
138 }
139
140 sub setup_s3dc($$)
141 {
142         my ($self, $path) = @_;
143
144         print "PROVISIONING S3DC...";
145
146         my $s3dc_options = "
147         domain master = yes
148         domain logons = yes
149         lanman auth = yes
150 ";
151
152         my $vars = $self->provision($path,
153                                     "LOCALS3DC2",
154                                     2,
155                                     "locals3dc2pass",
156                                     $s3dc_options);
157
158         $vars or return undef;
159
160         $self->check_or_start($vars,
161                                "yes", "yes", "yes");
162
163         if (not $self->wait_for_start($vars)) {
164                return undef;
165         }
166
167         $vars->{DC_SERVER} = $vars->{SERVER};
168         $vars->{DC_SERVER_IP} = $vars->{SERVER_IP};
169         $vars->{DC_NETBIOSNAME} = $vars->{NETBIOSNAME};
170         $vars->{DC_USERNAME} = $vars->{USERNAME};
171         $vars->{DC_PASSWORD} = $vars->{PASSWORD};
172
173         $self->{vars}->{s3dc} = $vars;
174
175         return $vars;
176 }
177
178 sub setup_member($$$)
179 {
180         my ($self, $prefix, $s3dcvars) = @_;
181
182         print "PROVISIONING MEMBER...";
183
184         my $member_options = "
185         security = domain
186         server signing = on
187 ";
188         my $ret = $self->provision($prefix,
189                                    "LOCALMEMBER3",
190                                    3,
191                                    "localmember3pass",
192                                    $member_options);
193
194         $ret or return undef;
195
196         my $net = Samba::bindir_path($self, "net");
197         my $cmd = "";
198         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
199         $cmd .= "$net join $ret->{CONFIGURATION} $s3dcvars->{DOMAIN} member";
200         $cmd .= " -U$s3dcvars->{USERNAME}\%$s3dcvars->{PASSWORD}";
201
202         if (system($cmd) != 0) {
203             warn("Join failed\n$cmd");
204             return undef;
205         }
206
207         $self->check_or_start($ret, "yes", "yes", "yes");
208
209         if (not $self->wait_for_start($ret)) {
210                return undef;
211         }
212
213         $ret->{DC_SERVER} = $s3dcvars->{SERVER};
214         $ret->{DC_SERVER_IP} = $s3dcvars->{SERVER_IP};
215         $ret->{DC_NETBIOSNAME} = $s3dcvars->{NETBIOSNAME};
216         $ret->{DC_USERNAME} = $s3dcvars->{USERNAME};
217         $ret->{DC_PASSWORD} = $s3dcvars->{PASSWORD};
218
219         return $ret;
220 }
221
222 sub setup_admember($$$$)
223 {
224         my ($self, $prefix, $dcvars, $iface) = @_;
225
226         # If we didn't build with ADS, pretend this env was never available
227         if (not $self->have_ads()) {
228                 return "UNKNOWN";
229         }
230
231         print "PROVISIONING S3 AD MEMBER$iface...";
232
233         my $member_options = "
234         security = ads
235         server signing = on
236         workgroup = $dcvars->{DOMAIN}
237         realm = $dcvars->{REALM}
238 ";
239
240         my $ret = $self->provision($prefix,
241                                    "LOCALADMEMBER$iface",
242                                    $iface,
243                                    "loCalMember${iface}Pass",
244                                    $member_options);
245
246         $ret or return undef;
247
248         close(USERMAP);
249         $ret->{DOMAIN} = $dcvars->{DOMAIN};
250         $ret->{REALM} = $dcvars->{REALM};
251
252         my $ctx;
253         my $prefix_abs = abs_path($prefix);
254         $ctx = {};
255         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
256         $ctx->{domain} = $dcvars->{DOMAIN};
257         $ctx->{realm} = $dcvars->{REALM};
258         $ctx->{dnsname} = lc($dcvars->{REALM});
259         $ctx->{kdc_ipv4} = $dcvars->{SERVER_IP};
260         Samba::mk_krb5_conf($ctx, "");
261
262         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
263
264         my $net = Samba::bindir_path($self, "net");
265         my $cmd = "";
266         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
267         $cmd .= "KRB5_CONFIG=\"$ret->{KRB5_CONFIG}\" ";
268         $cmd .= "$net join $ret->{CONFIGURATION}";
269         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
270
271         if (system($cmd) != 0) {
272             warn("Join failed\n$cmd");
273             return undef;
274         }
275
276         # We need world access to this share, as otherwise the domain
277         # administrator from the AD domain provided by Samba4 can't
278         # access the share for tests.
279         chmod 0777, "$prefix/share";
280
281         $self->check_or_start($ret,
282                               "yes", "yes", "yes");
283
284         $self->wait_for_start($ret);
285
286         $ret->{DC_SERVER} = $dcvars->{SERVER};
287         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
288         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
289         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
290         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
291
292         # Special case, this is called from Samba4.pm but needs to use the Samba3 check_env and get_log_env
293         $ret->{target} = $self;
294
295         return $ret;
296 }
297
298 sub setup_secshare($$)
299 {
300         my ($self, $path) = @_;
301
302         print "PROVISIONING server with security=share...";
303
304         my $secshare_options = "
305         security = share
306         lanman auth = yes
307 ";
308
309         my $vars = $self->provision($path,
310                                     "LOCALSHARE4",
311                                     4,
312                                     "local4pass",
313                                     $secshare_options);
314
315         $vars or return undef;
316
317         $self->check_or_start($vars, "yes", "no", "yes");
318
319         if (not $self->wait_for_start($vars)) {
320                return undef;
321         }
322
323         $self->{vars}->{secshare} = $vars;
324
325         return $vars;
326 }
327
328 sub setup_secserver($$$)
329 {
330         my ($self, $prefix, $s3dcvars) = @_;
331
332         print "PROVISIONING server with security=server...";
333
334         my $secserver_options = "
335         security = server
336         password server = $s3dcvars->{SERVER_IP}
337 ";
338
339         my $ret = $self->provision($prefix,
340                                    "LOCALSERVER5",
341                                    5,
342                                    "localserver5pass",
343                                    $secserver_options);
344
345         $ret or return undef;
346
347         $self->check_or_start($ret, "yes", "no", "yes");
348
349         if (not $self->wait_for_start($ret)) {
350                return undef;
351         }
352
353         $ret->{DC_SERVER} = $s3dcvars->{SERVER};
354         $ret->{DC_SERVER_IP} = $s3dcvars->{SERVER_IP};
355         $ret->{DC_NETBIOSNAME} = $s3dcvars->{NETBIOSNAME};
356         $ret->{DC_USERNAME} = $s3dcvars->{USERNAME};
357         $ret->{DC_PASSWORD} = $s3dcvars->{PASSWORD};
358
359         return $ret;
360 }
361
362 sub setup_ktest($$$)
363 {
364         my ($self, $prefix) = @_;
365
366         # If we didn't build with ADS, pretend this env was never available
367         if (not $self->have_ads()) {
368                 return "UNKNOWN";
369         }
370
371         print "PROVISIONING server with security=ads...";
372
373         my $ktest_options = "
374         workgroup = KTEST
375         realm = ktest.samba.example.com
376         security = ads
377         username map = $prefix/lib/username.map
378         server signing = required
379 ";
380
381         my $ret = $self->provision($prefix,
382                                    "LOCALKTEST6",
383                                    6,
384                                    "localktest6pass",
385                                    $ktest_options);
386
387         $ret or return undef;
388
389         my $ctx;
390         my $prefix_abs = abs_path($prefix);
391         $ctx = {};
392         $ctx->{krb5_conf} = "$prefix_abs/lib/krb5.conf";
393         $ctx->{domain} = "KTEST";
394         $ctx->{realm} = "KTEST.SAMBA.EXAMPLE.COM";
395         $ctx->{dnsname} = lc($ctx->{realm});
396         $ctx->{kdc_ipv4} = "0.0.0.0";
397         Samba::mk_krb5_conf($ctx, "");
398
399         $ret->{KRB5_CONFIG} = $ctx->{krb5_conf};
400
401         open(USERMAP, ">$prefix/lib/username.map") or die("Unable to open $prefix/lib/username.map");
402         print USERMAP "
403 $ret->{USERNAME} = KTEST\\Administrator
404 ";
405         close(USERMAP);
406
407 #This is the secrets.tdb created by 'net ads join' from Samba3 to a
408 #Samba4 DC with the same parameters as are being used here.  The
409 #domain SID is S-1-5-21-1071277805-689288055-3486227160
410
411         system("cp $self->{srcdir}/source3/selftest/ktest-secrets.tdb $prefix/private/secrets.tdb");
412         chmod 0600, "$prefix/private/secrets.tdb";
413
414 #This uses a pre-calculated krb5 credentials cache, obtained by running Samba4 with:
415 # "--option=kdc:service ticket lifetime=239232" "--option=kdc:user ticket lifetime=239232" "--option=kdc:renewal lifetime=239232"
416 #
417 #and having in krb5.conf:
418 # ticket_lifetime = 799718400
419 # renew_lifetime = 799718400
420 #
421 # The commands for the -2 keytab where were:
422 # kinit administrator@KTEST.SAMBA.EXAMPLE.COM
423 # kvno host/localktest6@KTEST.SAMBA.EXAMPLE.COM
424 # kvno cifs/localktest6@KTEST.SAMBA.EXAMPLE.COM
425 # kvno host/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
426 # kvno cifs/LOCALKTEST6@KTEST.SAMBA.EXAMPLE.COM
427 #
428 # and then for the -3 keytab, I did
429 #
430 # net changetrustpw; kdestroy and the same again.
431 #
432 # This creates a credential cache with a very long lifetime (2036 at
433 # at 2011-04), and shows that running 'net changetrustpw' does not
434 # break existing logins (for the secrets.tdb method at least).
435 #
436
437         $ret->{KRB5_CCACHE}="FILE:$prefix/krb5_ccache";
438
439         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-2 $prefix/krb5_ccache-2");
440         chmod 0600, "$prefix/krb5_ccache-2";
441
442         system("cp $self->{srcdir}/source3/selftest/ktest-krb5_ccache-3 $prefix/krb5_ccache-3");
443         chmod 0600, "$prefix/krb5_ccache-3";
444
445         # We need world access to this share, as otherwise the domain
446         # administrator from the AD domain provided by ktest can't
447         # access the share for tests.
448         chmod 0777, "$prefix/share";
449
450         $self->check_or_start($ret, "yes", "no", "yes");
451
452         if (not $self->wait_for_start($ret)) {
453                return undef;
454         }
455         return $ret;
456 }
457
458 sub setup_maptoguest($$)
459 {
460         my ($self, $path) = @_;
461
462         print "PROVISIONING maptoguest...";
463
464         my $options = "
465 map to guest = bad user
466 ";
467
468         my $vars = $self->provision($path,
469                                     "maptoguest",
470                                     7,
471                                     "maptoguestpass",
472                                     $options);
473
474         $vars or return undef;
475
476         $self->check_or_start($vars,
477                                "yes", "no", "yes");
478
479         if (not $self->wait_for_start($vars)) {
480                return undef;
481         }
482
483         $self->{vars}->{s3maptoguest} = $vars;
484
485         return $vars;
486 }
487
488 sub stop_sig_term($$) {
489         my ($self, $pid) = @_;
490         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
491 }
492
493 sub stop_sig_kill($$) {
494         my ($self, $pid) = @_;
495         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
496 }
497
498 sub write_pid($$$)
499 {
500         my ($env_vars, $app, $pid) = @_;
501
502         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
503         print PID $pid;
504         close(PID);
505 }
506
507 sub read_pid($$)
508 {
509         my ($env_vars, $app) = @_;
510
511         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
512         my $pid = <PID>;
513         close(PID);
514         return $pid;
515 }
516
517 sub check_or_start($$$$$) {
518         my ($self, $env_vars, $nmbd, $winbindd, $smbd) = @_;
519
520         # use a pipe for stdin in the child processes. This allows
521         # those processes to monitor the pipe for EOF to ensure they
522         # exit when the test script exits
523         pipe(STDIN_READER, $env_vars->{STDIN_PIPE});
524
525         unlink($env_vars->{NMBD_TEST_LOG});
526         print "STARTING NMBD...";
527         my $pid = fork();
528         if ($pid == 0) {
529                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
530                 open STDERR, '>&STDOUT';
531
532                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
533
534                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
535                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
536                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
537
538                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
539                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
540                 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
541
542                 $ENV{UID_WRAPPER} = "1";
543
544                 if ($nmbd ne "yes") {
545                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
546                                 my $signame = shift;
547                                 print("Skip nmbd received signal $signame");
548                                 exit 0;
549                         };
550                         sleep($self->{server_maxtime});
551                         exit 0;
552                 }
553
554                 my @optargs = ("-d0");
555                 if (defined($ENV{NMBD_OPTIONS})) {
556                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
557                 }
558
559                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "nmbd");
560
561                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
562                 if(defined($ENV{NMBD_VALGRIND})) { 
563                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
564                 }
565
566                 close($env_vars->{STDIN_PIPE});
567                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
568
569                 exec(@preargs, Samba::bindir_path($self, "nmbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start nmbd: $!");
570         }
571         write_pid($env_vars, "nmbd", $pid);
572         print "DONE\n";
573
574         unlink($env_vars->{WINBINDD_TEST_LOG});
575         print "STARTING WINBINDD...";
576         $pid = fork();
577         if ($pid == 0) {
578                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
579                 open STDERR, '>&STDOUT';
580
581                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
582
583                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
584                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
585                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
586
587                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
588                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
589                 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
590
591                 $ENV{UID_WRAPPER} = "1";
592
593                 if ($winbindd ne "yes") {
594                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
595                                 my $signame = shift;
596                                 print("Skip winbindd received signal $signame");
597                                 exit 0;
598                         };
599                         sleep($self->{server_maxtime});
600                         exit 0;
601                 }
602
603                 my @optargs = ("-d0");
604                 if (defined($ENV{WINBINDD_OPTIONS})) {
605                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
606                 }
607
608                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "winbindd");
609
610                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
611                 if(defined($ENV{WINBINDD_VALGRIND})) {
612                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
613                 }
614
615                 print "Starting winbindd with config $env_vars->{SERVERCONFFILE}\n";
616
617                 close($env_vars->{STDIN_PIPE});
618                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
619
620                 exec(@preargs, Samba::bindir_path($self, "winbindd"), "-F", "--no-process-group", "--stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start winbindd: $!");
621         }
622         write_pid($env_vars, "winbindd", $pid);
623         print "DONE\n";
624
625         unlink($env_vars->{SMBD_TEST_LOG});
626         print "STARTING SMBD...";
627         $pid = fork();
628         if ($pid == 0) {
629                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
630                 open STDERR, '>&STDOUT';
631
632                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
633
634                 $ENV{KRB5_CONFIG} = $env_vars->{KRB5_CONFIG};
635                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
636                 $ENV{NMBD_SOCKET_DIR} = $env_vars->{NMBD_SOCKET_DIR};
637
638                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
639                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
640                 $ENV{NSS_WRAPPER_WINBIND_SO_PATH} = $env_vars->{NSS_WRAPPER_WINBIND_SO_PATH};
641
642                 $ENV{UID_WRAPPER} = "1";
643
644                 if ($smbd ne "yes") {
645                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
646                                 my $signame = shift;
647                                 print("Skip smbd received signal $signame");
648                                 exit 0;
649                         };
650                         sleep($self->{server_maxtime});
651                         exit 0;
652                 }
653
654                 $ENV{MAKE_TEST_BINARY} = Samba::bindir_path($self, "smbd");
655                 my @optargs = ("-d0");
656                 if (defined($ENV{SMBD_OPTIONS})) {
657                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
658                 }
659                 my @preargs = (Samba::bindir_path($self, "timelimit"), $self->{server_maxtime});
660                 if(defined($ENV{SMBD_VALGRIND})) {
661                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
662                 }
663
664                 close($env_vars->{STDIN_PIPE});
665                 open STDIN, ">&", \*STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
666
667                 exec(@preargs, Samba::bindir_path($self, "smbd"), "-F", "--no-process-group", "--log-stdout", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start smbd: $!");
668         }
669         write_pid($env_vars, "smbd", $pid);
670         print "DONE\n";
671
672         close(STDIN_READER);
673
674         return 0;
675 }
676
677 sub provision($$$$$$$)
678 {
679         my ($self, $prefix, $server, $swiface, $password, $extra_options, $no_delete_prefix) = @_;
680
681         ##
682         ## setup the various environment variables we need
683         ##
684
685         my %ret = ();
686         my $server_ip = "127.0.0.$swiface";
687         my $domain = "SAMBA-TEST";
688
689         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
690         chomp $unix_name;
691         my $unix_uid = $>;
692         my $unix_gids_str = $);
693         my @unix_gids = split(" ", $unix_gids_str);
694
695         my $prefix_abs = abs_path($prefix);
696         my $bindir_abs = abs_path($self->{bindir});
697         my $vfs_modulesdir_abs = ($ENV{VFSLIBDIR} or $bindir_abs);
698
699         my $dns_host_file = "$ENV{SELFTEST_PREFIX}/dns_host_file";
700
701         my @dirs = ();
702
703         my $shrdir="$prefix_abs/share";
704         push(@dirs,$shrdir);
705
706         my $libdir="$prefix_abs/lib";
707         push(@dirs,$libdir);
708
709         my $piddir="$prefix_abs/pid";
710         push(@dirs,$piddir);
711
712         my $privatedir="$prefix_abs/private";
713         push(@dirs,$privatedir);
714
715         my $lockdir="$prefix_abs/lockdir";
716         push(@dirs,$lockdir);
717
718         my $eventlogdir="$prefix_abs/lockdir/eventlog";
719         push(@dirs,$eventlogdir);
720
721         my $logdir="$prefix_abs/logs";
722         push(@dirs,$logdir);
723
724         my $driver32dir="$shrdir/W32X86";
725         push(@dirs,$driver32dir);
726
727         my $driver64dir="$shrdir/x64";
728         push(@dirs,$driver64dir);
729
730         my $driver40dir="$shrdir/WIN40";
731         push(@dirs,$driver40dir);
732
733         my $ro_shrdir="$shrdir/root-tmp";
734         push(@dirs,$ro_shrdir);
735
736         my $msdfs_shrdir="$shrdir/msdfsshare";
737         push(@dirs,$msdfs_shrdir);
738
739         my $msdfs_deeppath="$msdfs_shrdir/deeppath";
740         push(@dirs,$msdfs_deeppath);
741
742         # this gets autocreated by winbindd
743         my $wbsockdir="$prefix_abs/winbindd";
744         my $wbsockprivdir="$lockdir/winbindd_privileged";
745
746         my $nmbdsockdir="$prefix_abs/nmbd";
747         unlink($nmbdsockdir);
748
749         ## 
750         ## create the test directory layout
751         ##
752         die ("prefix_abs = ''") if $prefix_abs eq "";
753         die ("prefix_abs = '/'") if $prefix_abs eq "/";
754
755         mkdir($prefix_abs, 0777);
756         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
757         if (not defined($no_delete_prefix) or not $no_delete_prefix) {
758             system("rm -rf $prefix_abs/*");
759         }
760         mkdir($_, 0777) foreach(@dirs);
761
762         ##
763         ## create ro and msdfs share layout
764         ##
765
766         chmod 0755, $ro_shrdir;
767         my $unreadable_file = "$ro_shrdir/unreadable_file";
768         unless (open(UNREADABLE_FILE, ">$unreadable_file")) {
769                 warn("Unable to open $unreadable_file");
770                 return undef;
771         }
772         close(UNREADABLE_FILE);
773         chmod 0600, $unreadable_file;
774
775         my $msdfs_target = "$ro_shrdir/msdfs-target";
776         unless (open(MSDFS_TARGET, ">$msdfs_target")) {
777                 warn("Unable to open $msdfs_target");
778                 return undef;
779         }
780         close(MSDFS_TARGET);
781         chmod 0666, $msdfs_target;
782         symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/msdfs-src1";
783         symlink "msdfs:$server_ip\\ro-tmp", "$msdfs_shrdir/deeppath/msdfs-src2";
784
785         my $conffile="$libdir/server.conf";
786
787         my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
788         my $nss_wrapper_passwd = "$privatedir/passwd";
789         my $nss_wrapper_group = "$privatedir/group";
790
791         my $mod_printer_pl = "$ENV{PERL} $self->{srcdir}/source3/script/tests/printing/modprinter.pl";
792
793         my @eventlog_list = ("dns server", "application");
794
795         ##
796         ## calculate uids and gids
797         ##
798
799         my ($max_uid, $max_gid);
800         my ($uid_nobody, $uid_root);
801         my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers);
802
803         if ($unix_uid < 0xffff - 2) {
804                 $max_uid = 0xffff;
805         } else {
806                 $max_uid = $unix_uid;
807         }
808
809         $uid_root = $max_uid - 1;
810         $uid_nobody = $max_uid - 2;
811
812         if ($unix_gids[0] < 0xffff - 3) {
813                 $max_gid = 0xffff;
814         } else {
815                 $max_gid = $unix_gids[0];
816         }
817
818         $gid_nobody = $max_gid - 1;
819         $gid_nogroup = $max_gid - 2;
820         $gid_root = $max_gid - 3;
821         $gid_domusers = $max_gid - 4;
822
823         ##
824         ## create conffile
825         ##
826
827         unless (open(CONF, ">$conffile")) {
828                 warn("Unable to open $conffile");
829                 return undef;
830         }
831         print CONF "
832 [global]
833         netbios name = $server
834         interfaces = $server_ip/8
835         bind interfaces only = yes
836         panic action = $self->{srcdir}/selftest/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
837
838         workgroup = $domain
839
840         private dir = $privatedir
841         pid directory = $piddir
842         lock directory = $lockdir
843         log file = $logdir/log.\%m
844         log level = 1
845         debug pid = yes
846         max log size = 0
847
848         state directory = $lockdir
849         cache directory = $lockdir
850
851         passdb backend = tdbsam
852
853         time server = yes
854
855         add user script =               $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
856         add group script =              $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action add --name %g
857         add machine script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action add --name %u --gid $gid_nogroup
858         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
859         delete user script =            $nss_wrapper_pl --passwd_path $nss_wrapper_passwd --type passwd --action delete --name %u
860         delete group script =           $nss_wrapper_pl --group_path  $nss_wrapper_group  --type group  --action delete --name %g
861         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
862
863         addprinter command =            $mod_printer_pl -a -s $conffile --
864         deleteprinter command =         $mod_printer_pl -d -s $conffile --
865
866         eventlog list = application \"dns server\"
867
868         kernel oplocks = no
869         kernel change notify = no
870
871         syslog = no
872         printing = bsd
873         printcap name = /dev/null
874
875         winbindd:socket dir = $wbsockdir
876         nmbd:socket dir = $nmbdsockdir
877         idmap config * : range = 100000-200000
878         winbind enum users = yes
879         winbind enum groups = yes
880
881 #       min receivefile size = 4000
882
883         max protocol = SMB2
884         read only = no
885         server signing = auto
886
887         smbd:sharedelay = 100000
888 #       smbd:writetimeupdatedelay = 500000
889         map hidden = no
890         map system = no
891         map readonly = no
892         store dos attributes = yes
893         create mask = 755
894         dos filemode = yes
895         vfs objects = $vfs_modulesdir_abs/xattr_tdb.so $vfs_modulesdir_abs/streams_depot.so
896
897         printing = vlp
898         print command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb print %p %s
899         lpq command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpq %p
900         lp rm command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lprm %p %j
901         lp pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lppause %p %j
902         lp resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb lpresume %p %j
903         queue pause command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queuepause %p
904         queue resume command = $bindir_abs/vlp tdbfile=$lockdir/vlp.tdb queueresume %p
905         lpq cache time = 0
906
907         ncalrpc dir = $prefix_abs/ncalrpc
908         rpc_server:epmapper = external
909         rpc_server:spoolss = external
910         rpc_server:lsarpc = external
911         rpc_server:samr = external
912         rpc_server:netlogon = external
913         rpc_server:tcpip = yes
914
915         rpc_daemon:epmd = fork
916         rpc_daemon:spoolssd = fork
917         rpc_daemon:lsasd = fork
918
919         resolv:host file = $dns_host_file
920
921         # The samba3.blackbox.smbclient_s3 test uses this to test that
922         # sending messages works, and that the %m sub works.
923         message command = mv %s $shrdir/message.%m
924
925         # Begin extra options
926         $extra_options
927         # End extra options
928
929         #Include user defined custom parameters if set
930 ";
931
932         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
933                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
934         }
935
936         print CONF "
937 [tmp]
938         path = $shrdir
939         comment = smb username is [%U]
940 [tmpguest]
941         path = $shrdir
942         guest ok = yes
943 [guestonly]
944         path = $shrdir
945         guest only = yes
946         guest ok = yes
947 [forceuser]
948         path = $shrdir
949         force user = $unix_name
950         guest ok = yes
951 [forcegroup]
952         path = $shrdir
953         force group = nogroup
954         guest ok = yes
955 [ro-tmp]
956         path = $ro_shrdir
957         guest ok = yes
958 [msdfs-share]
959         path = $msdfs_shrdir
960         msdfs root = yes
961         guest ok = yes
962 [hideunread]
963         copy = tmp
964         hide unreadable = yes
965 [tmpcase]
966         copy = tmp
967         case sensitive = yes
968 [hideunwrite]
969         copy = tmp
970         hide unwriteable files = yes
971 [print1]
972         copy = tmp
973         printable = yes
974
975 [print2]
976         copy = print1
977 [print3]
978         copy = print1
979 [lp]
980         copy = print1
981 [xcopy_share]
982         path = $shrdir
983         comment = smb username is [%U]
984         create mask = 777
985         force create mode = 777
986 [print\$]
987         copy = tmp
988         ";
989         close(CONF);
990
991         ##
992         ## create a test account
993         ##
994
995         unless (open(PASSWD, ">$nss_wrapper_passwd")) {
996            warn("Unable to open $nss_wrapper_passwd");
997            return undef;
998         } 
999         print PASSWD "nobody:x:$uid_nobody:$gid_nobody:nobody gecos:$prefix_abs:/bin/false
1000 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
1001 ";
1002         if ($unix_uid != 0) {
1003                 print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false";
1004         }
1005         close(PASSWD);
1006
1007         unless (open(GROUP, ">$nss_wrapper_group")) {
1008              warn("Unable to open $nss_wrapper_group");
1009              return undef;
1010         }
1011         print GROUP "nobody:x:$gid_nobody:
1012 nogroup:x:$gid_nogroup:nobody
1013 $unix_name-group:x:$unix_gids[0]:
1014 domusers:X:$gid_domusers:
1015 ";
1016         if ($unix_gids[0] != 0) {
1017                 print GROUP "root:x:$gid_root:";
1018         }
1019
1020         close(GROUP);
1021
1022         foreach my $evlog (@eventlog_list) {
1023                 my $evlogtdb = "$eventlogdir/$evlog.tdb";
1024                 open(EVENTLOG, ">$evlogtdb") or die("Unable to open $evlogtdb");
1025                 close(EVENTLOG);
1026         }
1027
1028         $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1029         $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1030
1031         my $cmd = Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
1032         unless (open(PWD, "|$cmd")) {
1033              warn("Unable to set password for test account\n$cmd");
1034              return undef;
1035         }
1036         print PWD "$password\n$password\n";
1037         unless (close(PWD)) {
1038              warn("Unable to set password for test account\n$cmd");
1039              return undef; 
1040         }
1041         print "DONE\n";
1042
1043         open(HOSTS, ">>$ENV{SELFTEST_PREFIX}/dns_host_file") or die("Unable to open $ENV{SELFTEST_PREFIX}/dns_host_file");
1044         print HOSTS "A $server. $server_ip
1045 ";
1046         close(HOSTS);
1047
1048         $ret{SERVER_IP} = $server_ip;
1049         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
1050         $ret{NMBD_TEST_LOG_POS} = 0;
1051         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
1052         $ret{WINBINDD_TEST_LOG_POS} = 0;
1053         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
1054         $ret{SMBD_TEST_LOG_POS} = 0;
1055         $ret{SERVERCONFFILE} = $conffile;
1056         $ret{CONFIGURATION} ="-s $conffile";
1057         $ret{SERVER} = $server;
1058         $ret{USERNAME} = $unix_name;
1059         $ret{USERID} = $unix_uid;
1060         $ret{DOMAIN} = $domain;
1061         $ret{NETBIOSNAME} = $server;
1062         $ret{PASSWORD} = $password;
1063         $ret{PIDDIR} = $piddir;
1064         $ret{WINBINDD_SOCKET_DIR} = $wbsockdir;
1065         $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
1066         $ret{NMBD_SOCKET_DIR} = $nmbdsockdir;
1067         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
1068         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
1069         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
1070         $ret{NSS_WRAPPER_WINBIND_SO_PATH} = $ENV{NSS_WRAPPER_WINBIND_SO_PATH};
1071         if (not defined($ret{NSS_WRAPPER_WINBIND_SO_PATH})) {
1072                 $ret{NSS_WRAPPER_WINBIND_SO_PATH} = Samba::bindir_path($self, "default/nsswitch/libnss-winbind.so");
1073         }
1074         $ret{LOCAL_PATH} = "$shrdir";
1075
1076         return \%ret;
1077 }
1078
1079 sub wait_for_start($$)
1080 {
1081         my ($self, $envvars) = @_;
1082
1083         # give time for nbt server to register its names
1084         print "delaying for nbt name registration\n";
1085         sleep(10);
1086         # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init 
1087         system(Samba::bindir_path($self, "nmblookup3") ." $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
1088         system(Samba::bindir_path($self, "nmblookup3") ." $envvars->{CONFIGURATION} __SAMBA__");
1089         system(Samba::bindir_path($self, "nmblookup3") ." $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
1090         system(Samba::bindir_path($self, "nmblookup3") ." $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
1091         system(Samba::bindir_path($self, "nmblookup3") ." $envvars->{CONFIGURATION} $envvars->{SERVER}");
1092
1093         # make sure smbd is also up set
1094         print "wait for smbd\n";
1095
1096         my $count = 0;
1097         my $ret;
1098         do {
1099             $ret = system(Samba::bindir_path($self, "smbclient3") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER} -U% -p 139");
1100             if ($ret != 0) {
1101                 sleep(2);
1102             }
1103             $count++
1104         } while ($ret != 0 && $count < 10);
1105         if ($count == 10) {
1106             print "SMBD failed to start up in a reasonable time (20sec)\n";
1107             teardown_env($self, $envvars);
1108             return 0;
1109         }
1110         # Ensure we have domain users mapped.
1111         $ret = system(Samba::bindir_path($self, "net") ." $envvars->{CONFIGURATION} groupmap add rid=513 unixgroup=domusers type=domain");
1112         if ($ret != 0) {
1113             return 1;
1114         }
1115
1116         print $self->getlog_env($envvars);
1117
1118         return 1;
1119 }
1120
1121 1;