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