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