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