78aafa7533acdcc5f810c73dffbc9cdd92f33e92
[ira/wip.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
13 sub binpath($$)
14 {
15         my ($self, $binary) = @_;
16
17         if (defined($self->{bindir})) {
18                 my $path = "$self->{bindir}/$binary";
19                 -f $path or die("File $path doesn't exist");
20                 return $path;
21         }
22
23         return $binary;
24 }
25
26 sub new($$) {
27         my ($classname, $bindir) = @_;
28         my $self = { bindir => $bindir };
29         bless $self;
30         return $self;
31 }
32
33 sub teardown_env($$)
34 {
35         my ($self, $envvars) = @_;
36
37         my $smbdpid = read_pid($envvars, "smbd");
38         my $nmbdpid = read_pid($envvars, "nmbd");
39         my $winbinddpid = read_pid($envvars, "winbindd");
40
41         $self->stop_sig_term($smbdpid);
42         $self->stop_sig_term($nmbdpid);
43         $self->stop_sig_term($winbinddpid);
44
45         sleep(2);
46
47         $self->stop_sig_kill($smbdpid);
48         $self->stop_sig_kill($nmbdpid);
49         $self->stop_sig_kill($winbinddpid);
50
51         return 0;
52 }
53
54 sub getlog_env_app($$$)
55 {
56         my ($self, $envvars, $name) = @_;
57
58         my $title = "$name LOG of: $envvars->{NETBIOSNAME}\n";
59         my $out = $title;
60
61         open(LOG, "<".$envvars->{$name."_TEST_LOG"});
62
63         seek(LOG, $envvars->{$name."_TEST_LOG_POS"}, SEEK_SET);
64         while (<LOG>) {
65                 $out .= $_;
66         }
67         $envvars->{$name."_TEST_LOG_POS"} = tell(LOG);
68         close(LOG);
69
70         return "" if $out eq $title;
71  
72         return $out;
73 }
74
75 sub getlog_env($$)
76 {
77         my ($self, $envvars) = @_;
78         my $ret = "";
79
80         $ret .= $self->getlog_env_app($envvars, "SMBD");
81         $ret .= $self->getlog_env_app($envvars, "NMBD");
82         $ret .= $self->getlog_env_app($envvars, "WINBINDD");
83
84         return $ret;
85 }
86
87 sub check_env($$)
88 {
89         my ($self, $envvars) = @_;
90
91         # TODO ...
92         return 1;
93 }
94
95 sub setup_env($$$)
96 {
97         my ($self, $envname, $path) = @_;
98         
99         if ($envname eq "dc") {
100                 return $self->setup_dc("$path/dc");
101         } elsif ($envname eq "member") {
102                 if (not defined($self->{vars}->{dc})) {
103                         $self->setup_dc("$path/dc");
104                 }
105                 return $self->setup_member("$path/member", $self->{vars}->{dc});
106         } else {
107                 return undef;
108         }
109 }
110
111 sub setup_dc($$)
112 {
113         my ($self, $path) = @_;
114
115         print "PROVISIONING DC...";
116
117         my $dc_options = "
118         domain master = yes
119         domain logons = yes
120 ";
121
122         my $vars = $self->provision($path,
123                                     "LOCALDC2",
124                                     2,
125                                     "localdc2pass",
126                                     $dc_options);
127
128         $self->check_or_start($vars,
129                               ($ENV{SMBD_MAXTIME} or 2700),
130                                "yes", "yes", "yes");
131
132         $self->wait_for_start($vars);
133
134         $self->{vars}->{dc} = $vars;
135
136         return $vars;
137 }
138
139 sub setup_member($$$)
140 {
141         my ($self, $prefix, $dcvars) = @_;
142
143         print "PROVISIONING MEMBER...";
144
145         my $member_options = "
146         security = domain
147 ";
148         my $ret = $self->provision($prefix,
149                                    "LOCALMEMBER3",
150                                    3,
151                                    "localmember3pass",
152                                    $member_options);
153
154         $ret or die("Unable to provision");
155
156         my $net = $self->binpath("net");
157         my $cmd = "";
158         $cmd .= "SOCKET_WRAPPER_DEFAULT_IFACE=\"$ret->{SOCKET_WRAPPER_DEFAULT_IFACE}\" ";
159         $cmd .= "$net join $ret->{CONFIGURATION} $dcvars->{DOMAIN} member";
160         $cmd .= " -U$dcvars->{USERNAME}\%$dcvars->{PASSWORD}";
161
162         system($cmd) == 0 or die("Join failed\n$cmd");
163
164         $self->check_or_start($ret,
165                               ($ENV{SMBD_MAXTIME} or 2700),
166                                "yes", "yes", "yes");
167
168         $self->wait_for_start($ret);
169
170         $ret->{DC_SERVER} = $dcvars->{SERVER};
171         $ret->{DC_SERVER_IP} = $dcvars->{SERVER_IP};
172         $ret->{DC_NETBIOSNAME} = $dcvars->{NETBIOSNAME};
173         $ret->{DC_USERNAME} = $dcvars->{USERNAME};
174         $ret->{DC_PASSWORD} = $dcvars->{PASSWORD};
175
176         return $ret;
177 }
178
179 sub stop($)
180 {
181         my ($self) = @_;
182 }
183
184 sub stop_sig_term($$) {
185         my ($self, $pid) = @_;
186         kill("USR1", $pid) or kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
187 }
188
189 sub stop_sig_kill($$) {
190         my ($self, $pid) = @_;
191         kill("ALRM", $pid) or warn("Unable to kill $pid: $!");
192 }
193
194 sub write_pid($$$)
195 {
196         my ($env_vars, $app, $pid) = @_;
197
198         open(PID, ">$env_vars->{PIDDIR}/timelimit.$app.pid");
199         print PID $pid;
200         close(PID);
201 }
202
203 sub read_pid($$)
204 {
205         my ($env_vars, $app) = @_;
206
207         open(PID, "<$env_vars->{PIDDIR}/timelimit.$app.pid");
208         my $pid = <PID>;
209         close(PID);
210         return $pid;
211 }
212
213 sub check_or_start($$$$$) {
214         my ($self, $env_vars, $maxtime, $nmbd, $winbindd, $smbd) = @_;
215
216         unlink($env_vars->{NMBD_TEST_LOG});
217         print "STARTING NMBD...";
218         my $pid = fork();
219         if ($pid == 0) {
220                 open STDOUT, ">$env_vars->{NMBD_TEST_LOG}";
221                 open STDERR, '>&STDOUT';
222
223                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
224
225                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
226
227                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
228                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
229
230                 if ($nmbd ne "yes") {
231                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
232                                 my $signame = shift;
233                                 print("Skip nmbd received signal $signame");
234                                 exit 0;
235                         };
236                         sleep($maxtime);
237                         exit 0;
238                 }
239
240                 my @optargs = ("-d0");
241                 if (defined($ENV{NMBD_OPTIONS})) {
242                         @optargs = split(/ /, $ENV{NMBD_OPTIONS});
243                 }
244
245                 $ENV{MAKE_TEST_BINARY} = $self->binpath("nmbd");
246
247                 my @preargs = ($self->binpath("timelimit"), $maxtime);
248                 if(defined($ENV{NMBD_VALGRIND})) { 
249                         @preargs = split(/ /, $ENV{NMBD_VALGRIND});
250                 }
251
252                 exec(@preargs, $self->binpath("nmbd"), "-F", "-S", "--no-process-group", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start nmbd: $!");
253         }
254         write_pid($env_vars, "nmbd", $pid);
255         print "DONE\n";
256
257         unlink($env_vars->{WINBINDD_TEST_LOG});
258         print "STARTING WINBINDD...";
259         $pid = fork();
260         if ($pid == 0) {
261                 open STDOUT, ">$env_vars->{WINBINDD_TEST_LOG}";
262                 open STDERR, '>&STDOUT';
263
264                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
265
266                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
267
268                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
269                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
270
271                 if ($winbindd ne "yes") {
272                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
273                                 my $signame = shift;
274                                 print("Skip winbindd received signal $signame");
275                                 exit 0;
276                         };
277                         sleep($maxtime);
278                         exit 0;
279                 }
280
281                 my @optargs = ("-d0");
282                 if (defined($ENV{WINBINDD_OPTIONS})) {
283                         @optargs = split(/ /, $ENV{WINBINDD_OPTIONS});
284                 }
285
286                 $ENV{MAKE_TEST_BINARY} = $self->binpath("winbindd");
287
288                 my @preargs = ($self->binpath("timelimit"), $maxtime);
289                 if(defined($ENV{WINBINDD_VALGRIND})) {
290                         @preargs = split(/ /, $ENV{WINBINDD_VALGRIND});
291                 }
292
293                 exec(@preargs, $self->binpath("winbindd"), "-F", "-S", "--no-process-group", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start winbindd: $!");
294         }
295         write_pid($env_vars, "winbindd", $pid);
296         print "DONE\n";
297
298         unlink($env_vars->{SMBD_TEST_LOG});
299         print "STARTING SMBD...";
300         $pid = fork();
301         if ($pid == 0) {
302                 open STDOUT, ">$env_vars->{SMBD_TEST_LOG}";
303                 open STDERR, '>&STDOUT';
304
305                 SocketWrapper::set_default_iface($env_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
306
307                 $ENV{WINBINDD_SOCKET_DIR} = $env_vars->{WINBINDD_SOCKET_DIR};
308
309                 $ENV{NSS_WRAPPER_PASSWD} = $env_vars->{NSS_WRAPPER_PASSWD};
310                 $ENV{NSS_WRAPPER_GROUP} = $env_vars->{NSS_WRAPPER_GROUP};
311
312                 if ($smbd ne "yes") {
313                         $SIG{USR1} = $SIG{ALRM} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
314                                 my $signame = shift;
315                                 print("Skip smbd received signal $signame");
316                                 exit 0;
317                         };
318                         sleep($maxtime);
319                         exit 0;
320                 }
321
322                 $ENV{MAKE_TEST_BINARY} = $self->binpath("smbd");
323                 my @optargs = ("-d0");
324                 if (defined($ENV{SMBD_OPTIONS})) {
325                         @optargs = split(/ /, $ENV{SMBD_OPTIONS});
326                 }
327                 my @preargs = ($self->binpath("timelimit"), $maxtime);
328                 if(defined($ENV{SMBD_VALGRIND})) {
329                         @preargs = split(/ /,$ENV{SMBD_VALGRIND});
330                 }
331                 exec(@preargs, $self->binpath("smbd"), "-F", "-S", "--no-process-group", "-s", $env_vars->{SERVERCONFFILE}, @optargs) or die("Unable to start smbd: $!");
332         }
333         write_pid($env_vars, "smbd", $pid);
334         print "DONE\n";
335
336         return 0;
337 }
338
339 sub create_clientconf($$$)
340 {
341         my ($self, $prefix, $domain) = @_;
342
343         my $lockdir = "$prefix/locks";
344         my $logdir = "$prefix/logs";
345         my $piddir = "$prefix/pid";
346         my $privatedir = "$prefix/private";
347         my $conffile = "$prefix/smb.conf";
348
349         my $torture_interfaces='127.0.0.6/8,127.0.0.7/8,127.0.0.8/8,127.0.0.9/8,127.0.0.10/8,127.0.0.11/8';
350         open(CONF, ">$conffile");
351         print CONF "
352 [global]
353         workgroup = $domain
354
355         private dir = $privatedir
356         pid directory = $piddir
357         lock directory = $lockdir
358         log file = $logdir/log.\%m
359         log level = 0
360
361         name resolve order = bcast
362
363         netbios name = TORTURE_6
364         interfaces = $torture_interfaces
365         panic action = $RealBin/gdb_backtrace \%d %\$(MAKE_TEST_BINARY)
366
367         passdb backend = tdbsam
368         ";
369         close(CONF);
370 }
371
372 sub provision($$$$$$)
373 {
374         my ($self, $prefix, $server, $swiface, $password, $extra_options) = @_;
375
376         ##
377         ## setup the various environment variables we need
378         ##
379
380         my %ret = ();
381         my $server_ip = "127.0.0.$swiface";
382         my $domain = "SAMBA-TEST";
383
384         my $unix_name = ($ENV{USER} or $ENV{LOGNAME} or `PATH=/usr/ucb:$ENV{PATH} whoami`);
385         chomp $unix_name;
386         my $unix_uid = $>;
387         my $unix_gids_str = $);
388         my @unix_gids = split(" ", $unix_gids_str);
389
390         my $prefix_abs = abs_path($prefix);
391         my $bindir_abs = abs_path($self->{bindir});
392
393         my @dirs = ();
394
395         my $shrdir="$prefix_abs/share";
396         push(@dirs,$shrdir);
397
398         my $libdir="$prefix_abs/lib";
399         push(@dirs,$libdir);
400
401         my $piddir="$prefix_abs/pid";
402         push(@dirs,$piddir);
403
404         my $privatedir="$prefix_abs/private";
405         push(@dirs,$privatedir);
406
407         my $lockdir="$prefix_abs/lockdir";
408         push(@dirs,$lockdir);
409
410         my $logdir="$prefix_abs/logs";
411         push(@dirs,$logdir);
412
413         # this gets autocreated by winbindd
414         my $wbsockdir="$prefix_abs/winbindd";
415         my $wbsockprivdir="$lockdir/winbindd_privileged";
416
417         ## 
418         ## create the test directory layout
419         ##
420         die ("prefix_abs = ''") if $prefix_abs eq "";
421         die ("prefix_abs = '/'") if $prefix_abs eq "/";
422
423         mkdir($prefix_abs, 0777);
424         print "CREATE TEST ENVIRONMENT IN '$prefix'...";
425         system("rm -rf $prefix_abs/*");
426         mkdir($_, 0777) foreach(@dirs);
427
428         my $conffile="$libdir/server.conf";
429
430         my $nss_wrapper_pl = "$ENV{PERL} $RealBin/../lib/nss_wrapper/nss_wrapper.pl";
431         my $nss_wrapper_passwd = "$privatedir/passwd";
432         my $nss_wrapper_group = "$privatedir/group";
433
434         open(CONF, ">$conffile") or die("Unable to open $conffile");
435         print CONF "
436 [global]
437         netbios name = $server
438         interfaces = $server_ip/8
439         bind interfaces only = yes
440         panic action = $RealBin/gdb_backtrace %d %\$(MAKE_TEST_BINARY)
441
442         workgroup = $domain
443
444         private dir = $privatedir
445         pid directory = $piddir
446         lock directory = $lockdir
447         log file = $logdir/log.\%m
448         log level = 0
449
450         name resolve order = bcast
451
452         state directory = $lockdir
453         cache directory = $lockdir
454
455         passdb backend = tdbsam
456
457         time server = yes
458
459         add user script = $nss_wrapper_pl --path $nss_wrapper_passwd --type passwd --action add --name %u
460         add machine script = $nss_wrapper_pl --path $nss_wrapper_passwd --type passwd --action add --name %u
461         delete user script = $nss_wrapper_pl --path $nss_wrapper_passwd --type passwd --action delete --name %u
462
463         kernel oplocks = no
464         kernel change notify = no
465
466         syslog = no
467         printing = bsd
468         printcap name = /dev/null
469
470         winbindd:socket dir = $wbsockdir
471         idmap uid = 100000-200000
472         idmap gid = 100000-200000
473
474 #       min receivefile size = 4000
475
476         read only = no
477         smbd:sharedelay = 100000
478         smbd:writetimeupdatedelay = 500000
479         map hidden = yes
480         map system = yes
481         create mask = 755
482         vfs objects = $bindir_abs/xattr_tdb.so $bindir_abs/streams_depot.so
483
484         # Begin extra options
485         $extra_options
486         # End extra options
487
488         #Include user defined custom parameters if set
489 ";
490
491         if (defined($ENV{INCLUDE_CUSTOM_CONF})) {
492                 print CONF "\t$ENV{INCLUDE_CUSTOM_CONF}\n";
493         }
494
495         print CONF "
496 [tmp]
497         path = $shrdir
498 [hideunread]
499         copy = tmp
500         hide unreadable = yes
501 [hideunwrite]
502         copy = tmp
503         hide unwriteable files = yes
504 [print1]
505         copy = tmp
506         printable = yes
507         printing = test
508 [print2]
509         copy = print1
510 [print3]
511         copy = print1
512 [print4]
513         copy = print1
514         ";
515         close(CONF);
516
517         ##
518         ## create a test account
519         ##
520
521         open(PASSWD, ">$nss_wrapper_passwd") or die("Unable to open $nss_wrapper_passwd");
522         print PASSWD "nobody:x:65534:65533:nobody gecos:$prefix_abs:/bin/false
523 $unix_name:x:$unix_uid:$unix_gids[0]:$unix_name gecos:$prefix_abs:/bin/false
524 ";
525         close(PASSWD);
526
527         open(GROUP, ">$nss_wrapper_group") or die("Unable to open $nss_wrapper_group");
528         print GROUP "nobody:x:65533:
529 nogroup:x:65534:nobody
530 $unix_name-group:x:$unix_gids[0]:
531 ";
532         close(GROUP);
533
534         $ENV{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
535         $ENV{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
536
537         open(PWD, "|".$self->binpath("smbpasswd")." -c $conffile -L -s -a $unix_name >/dev/null");
538         print PWD "$password\n$password\n";
539         close(PWD) or die("Unable to set password for test account");
540
541         delete $ENV{NSS_WRAPPER_PASSWD};
542         delete $ENV{NSS_WRAPPER_GROUP};
543
544         print "DONE\n";
545
546         $ret{SERVER_IP} = $server_ip;
547         $ret{NMBD_TEST_LOG} = "$prefix/nmbd_test.log";
548         $ret{NMBD_TEST_LOG_POS} = 0;
549         $ret{WINBINDD_TEST_LOG} = "$prefix/winbindd_test.log";
550         $ret{WINBINDD_TEST_LOG_POS} = 0;
551         $ret{SMBD_TEST_LOG} = "$prefix/smbd_test.log";
552         $ret{SMBD_TEST_LOG_POS} = 0;
553         $ret{SERVERCONFFILE} = $conffile;
554         $ret{CONFIGURATION} ="-s $conffile";
555         $ret{SERVER} = $server;
556         $ret{USERNAME} = $unix_name;
557         $ret{DOMAIN} = $domain;
558         $ret{NETBIOSNAME} = $server;
559         $ret{PASSWORD} = $password;
560         $ret{PIDDIR} = $piddir;
561         $ret{WINBINDD_SOCKET_DIR} = $wbsockdir;
562         $ret{WINBINDD_PRIV_PIPE_DIR} = $wbsockprivdir;
563         $ret{SOCKET_WRAPPER_DEFAULT_IFACE} = $swiface;
564         $ret{NSS_WRAPPER_PASSWD} = $nss_wrapper_passwd;
565         $ret{NSS_WRAPPER_GROUP} = $nss_wrapper_group;
566
567         return \%ret;
568 }
569
570 sub wait_for_start($$)
571 {
572         my ($self, $envvars) = @_;
573
574         # give time for nbt server to register its names
575         print "delaying for nbt name registration\n";
576         sleep(10);
577         # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init 
578         system($self->binpath("nmblookup") ." $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} __SAMBA__");
579         system($self->binpath("nmblookup") ." $envvars->{CONFIGURATION} __SAMBA__");
580         system($self->binpath("nmblookup") ." $envvars->{CONFIGURATION} -U 127.255.255.255 __SAMBA__");
581         system($self->binpath("nmblookup") ." $envvars->{CONFIGURATION} -U $envvars->{SERVER_IP} $envvars->{SERVER}");
582         system($self->binpath("nmblookup") ." $envvars->{CONFIGURATION} $envvars->{SERVER}");
583         # make sure smbd is also up set
584         print "wait for smbd\n";
585         system($self->binpath("smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER_IP} -U% -p 139 | head -2");
586         system($self->binpath("smbclient") ." $envvars->{CONFIGURATION} -L $envvars->{SERVER_IP} -U% -p 139 | head -2");
587
588         print $self->getlog_env($envvars);
589 }
590
591 1;