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