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