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