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