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