File: DNS.pm

package info (click to toggle)
bastille 1%3A1.3.0-2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,576 kB
  • ctags: 397
  • sloc: perl: 8,407; sh: 1,879; ansic: 638; makefile: 203; csh: 17
file content (331 lines) | stat: -rw-r--r-- 12,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Copyright (C) 1999, 2000 Jay Beale
# Licensed under the GNU General Public License

package Bastille::DNS;
use lib "/usr/lib";

use Bastille::API;
@ENV="";
$ENV{PATH}="";
$ENV{CDPATH}=".";
$ENV{BASH_ENV}="";

######## TO DO: Convert the chown line to API functions, so we don't
#########       have to use GLOBAL_PREFIX

#######################################################################
##                             DNS/BIND/NAMED                        ##
#######################################################################

&chrootBIND;
&DeactivateNamed;


sub chrootBIND {

    if ( &getGlobalConfig("DNS","chrootbind") eq "Y" ) {
	&ActionLog("# sub chrootBIND\n");

	# Only chroot bind if we are running on a Red Hat / Mandrake / HP-UX 
        # system and we haven't already chroot'ed it...
	if($GLOBAL_DISTRO =~ "^HP-UX"){
            my $extrasteps=0;

            # create non-privileged user and group named
            my $groupadd = 'PATH="/usr/bin"; ' . &getGlobal('BIN',"groupadd") . " named";
            my $groupdel = 'PATH="/usr/bin"; ' . &getGlobal('BIN',"groupdel") . " named";

            if (! &B_System($groupadd,$groupdel)) {
               $extrasteps = 1;
            }

            my $jail = &getGlobal('BDIR',"jail");

            my $useradd = 'PATH="/usr/bin"; ' . &getGlobal('BIN',"useradd") . 
                            " -g named -d $jail/bind -s /usr/bin/false named"; 
            my $userdel = 'PATH="/usr/bin"; ' . &getGlobal('BIN',"userdel") . " named";

            if (! &B_System($useradd, $userdel)) {
               $extrasteps = 1;
            }

	    # install generic chroot tree
	    if(&B_install_jail("bind",&getGlobal('BFILE',"jail.generic.hpux"))) {
                my $success=0;
		# add dev/null and dev/log to chroot tree
		&B_mknod("", "$jail/bind/dev/null","c 3 0x000002");
		&B_chmod(0666,"$jail/bind/dev/null");
		&B_System(&getGlobal('BIN','mkfifo') . " -p -m 0666 " . $jail . "/bind/dev/log",
			  &getGlobal('BIN','rm') . " -f " . $jail . "/bind/dev/log\n" . 
			  &getGlobal('BIN',"echo") . " \"Your DNS is not running outside of it's\\n" . 
			  "chroot jail.  You must manually migrate your named server\\n" .
			  "back to your Named server's default defined location(s).\\n".
			  "After you have completed this, feel free to remove the jail directories\\n" .
			  "from your machine.  Be sure to reset NAMED_ARGS to equal \\\"-u daemon\\\"\\n" . 
			  "in /etc/rc.config.d/namesvrs and then restart the daemon using the init\\n" . 
			  "scripts.  Your bind jail directory is located in\\n" .
			  &getGlobal('BDIR',"jail") . "\\n\" >> " . &getGlobal('BFILE',"TOUNDO"));

		if(-e &getGlobal('BIN',"named-xfer")){
		    # install bind 8 specific files and directories.
		    &ActionLog("Bind 8 chroot directory setup\n");
                    if (&B_install_jail("bind", &getGlobal('BFILE',"jail.bind.hpux"))){
                       $success=1;
		    }
                } elsif(-e &getGlobal('FILE',"named")){
		    # install bind 9 specific files and directories.
		    &ActionLog("Bind 9 chroot directory setup\n");
		    if(&B_install_jail("bind",&getGlobal('BFILE',"jail.bind9.hpux"))){
                      $success=1;
		    }
		} else {
                    &ErrorLog("Bind does not appear to be on this system; no action was taken.\n");
                }

                if ($success) {
                   &B_System(&getGlobal('BIN',"cat") . " " .&getGlobal('FILE',"group") . " | " .
                             &getGlobal('BIN',"grep") . " named > $jail/bind/etc/group", "");

                   &B_chmod(0444, "$jail/bind/etc/group");
                   &B_chown("bin", "$jail/bind/etc/group");
                   &B_chgrp("bin", "$jail/bind/etc/group");

                   my $ch_rc = &getGlobal('BIN',"ch_rc");
                   my $initscript = &getGlobal('FILE',"chkconfig_named");
                   my $syslogd = &getGlobal('BIN',"syslogd");
                           #--80 chars wide----------------------------------------------------------------
                   &B_TODO("Chroot'ed Bind:\n" .
                           "Bastille has created a simple \"named\" directory structure in\n" .
                           "$jail/bind/\n\n" .

                           "You need to take the following steps to configure your name server\n" .
                           "to run without root privileges in a chroot jail:\n\n");

                   if ($extrasteps==1) {
                      &B_TODO("First, there were errors recorded in the error log which you will \n" .
                              "need to resolve.  Depending on the severity of the errors, you may \n" .
                              "or may not be able to continue with the following steps.  The most\n" .
                              "likely error is that a \"named\" user or group already exists, in which\n" .
                              "case the following steps should work just fine and \"named\" will run as\n" .
                              "that user.\n\n");
                   }

                   my $pidfile = &getGlobal('FILE', "named.pid");

                   &B_TODO("1: Stop the currently running \"named\" process using the system\'s\n" .
                           "   init script for \"named\" \n\n" .

                           "   type_this> $initscript stop\n\n" .

                           "   and remove all \"/var/run/named.*\" files if they exist.\n\n" .

                           "   type_this> rm -r /var/run/named.*\n\n" .

                           "2: Next, move all of the configuration files that the current \n" .
                           "   \"named\" implementation requires from their current locations into\n" .
                           "   the equivalent place in the jail  structure.  This is at least a\n" .
                           "   \"named.conf\" file. If there is a directory path specified in the\n" .
                           "   options section of the \"named.conf\", that path must be made as well\n" .
                           "   as all of the files that are referenced from that directory.  Remember\n" .
                           "   that any files owned by the named user can be modified if an attacker\n" .
                           "   breaks into bind as the \"named\" user.\n\n" . 

                           "3: Instruct the \"named\" init script to chroot into the new environment:\n\n" .

                           "   type_this> $ch_rc" . " -a -p NAMED_ARGS=\"-u named -t $jail/bind\"\n\n".

                           "4: Start named in its chroot jail using the named init script\n\n" .

                           "   type_this> $initscript start\n");
                } else {
                   &ErrorLog("Error trying to install the chroot bind jail.\n");
                }
           }
	} else {
	    my $home_dns="/home/dns";
	
	    if (( not ( -d "${GLOBAL_PREFIX}$home_dns" ) ) and (($GLOBAL_DISTRO =~ /^RH/)  or ($GLOBAL_DISTRO =~ /^MN/) )) {
		
		# We place BIND in a chroot'd environment, following the 
		# instructions in SANS Step by Step 4.3.3
		
		my $user ="";
		my $group = "";
		my $gid = "";

		#
		# Create the dns user, if one doesn't yet exist
		#

		# Look for a DNS user already on the operating system
		if (open PASSWD,"/etc/passwd") {
		    my @lines = <PASSWD>;
		    close PASSWD;
		    chomp @lines;

		    foreach $name ( 'dns','named' ) {
			if (grep /^$name:/,@lines) {
			    $user = $name;
			}
		    }
		}
		if ($user) {
		    $home_dns = (getpwnam($user))[7];
		}
		else {
		    $home_dns = '/home/dns';	 
		}

                # Make the dns chroot directory
		&B_create_dir ( $home_dns );  		

		# Look for a DNS group already on the operating system
		if (open PASSWD,"/etc/group") {
		    my @lines = <PASSWD>;
		    close PASSWD;
		    chomp @lines;

		    foreach $name ( 'dns','named' ) {
			if (grep /^$name:/,@lines) {
			    $group = $name;
			    $gid = getgrnam($name);
			}
		    }
		}

		# Create whatever users or groups are necessary
		unless ($group) {
		    $gid = '53';
		    &B_append_line ("/etc/group",":53:","dns:x:53:\n");
		    $group = 'dns';
		}
		unless ($user) {		    
		    &B_append_line ("/etc/passwd",":$gid:","dns:x:53:$gid::$home_dns:/bin/false\n");
		    &B_append_line("/etc/shadow","^dns:","dns:*:11089:0:99999:7:::\n");
		    $user = 'dns';
		}
		
		# Populate the dns chroot directory
		&B_create_dir ("$home_dns");
		&B_create_dir ("$home_dns/etc");
		&B_chmod (0755,"$home_dns/etc");
		&B_create_dir ("$home_dns/lib");
		&B_chmod (0755,"$home_dns/lib");
		&B_create_dir ("$home_dns/dev");
		&B_chmod (0755,"$home_dns/dev");
		&B_create_dir ("$home_dns/usr");
		&B_chmod (0755,"$home_dns/usr");
		&B_create_dir ("$home_dns/usr/sbin");
		&B_chmod (0755,"$home_dns/usr/sbin");
		&B_create_dir ("$home_dns/var");
		&B_chmod (0755,"$home_dns/var");
		&B_create_dir ("$home_dns/var/named");
		&B_chmod (0755,"$home_dns/var/named");
		&B_create_dir ("$home_dns/var/run");
		&B_chmod (0755,"$home_dns/var/run");
		&B_create_dir ("$home_dns/var/run/named");
                &B_chmod (0755,"$home_dns/var/run/named");

		
		unless ($GLOBAL_LOGONLY) {
		    &B_mknod(" -m 666 ","$home_dns/dev/null"," c 1 3");
		    
		    &B_cp("/etc/named.conf","$home_dns/etc/named.conf");
		    
		    # Should we tell them to move their name logs over?
		    
		    &B_cp("/var/named/named.ca","$home_dns/var/named/named.ca");
		    &B_cp("/var/named/named.local","$home_dns/var/named/named.local");
		    
		    my $command=&getGlobal('BIN',"chown");
		    `$command -R $user.$user ${GLOBAL_PREFIX}$home_dns/var/named $home_dns/var/run $home_dns/var/run/named`;
		    &B_cp("/usr/sbin/named","$home_dns/usr/sbin/named");
		    &B_chmod(0755,"$home_dns/usr/sbin/named");
		    # Don't delete named -- let it get loaded from /usr/sbin, but
		    # find data files in the chroot directory.  We'll still put 
		    # a copy of the binary in the chroot directory so the named
		    # binary can be reloaded from there if necessary.
		    #
		    #&B_delete_file("/usr/sbin/named");
		    my $namedxfer = &getGlobal('BIN',"named-xfer");
		    if (-e $namedxfer ) {
			&B_cp($namedxfer,"$home_dns" . $namedxfer);
			&B_chmod(0755,"$home_dns" . $namedxfer);
		    }
		    &B_cp("/lib/libc.so.6","$home_dns/lib/libc.so.6");
		    &B_cp("/lib/ld-linux.so.2","$home_dns/lib/ld-linux.so.2");
		}
		
		# named communicates normally with syslog via the device /dev/log, 
		# which isn't accessible from the chroot'd environment.  We set 
		# syslog to create and listen to a specific device just for bind.
		
		&B_replace_line("/etc/rc.d/init.d/syslog",'daemon syslogd -m 0\s*\$',"daemon syslogd -m 0 -a $home_dns/dev/log\n");
                &B_replace_line("/etc/rc.d/init.d/syslog",'daemon syslogd $SYSLOGD_OPTIONS\s*\$',"daemon syslogd \$SYSLOGD_OPTIONS -a $home_dns/dev/log\n");
	
	
		# Modify named's init script to use the chroot environment

		my $bind8=1;	
		if ($GLOBAL_DISTRO =~ /MN(\d+\.\d+)/) {
			if ($1 >= 8.0) {
				$bind8=0;
			}
		}
		elsif ($GLOBAL_DISTRO =~ /RH(\d+\.\d+)/) {
                        if ($1 >= 7.1) {
                                $bind8=0;
			}	 
		}
	
		if ( $bind8 ) {
		    &B_replace_line("/etc/rc.d/init.d/named",'^\s*daemon\s+named',"daemon named -u $user -g $group -t $home_dns\n");
		}
		else {
		    # Mandrake 8.0 and later, along with Red Hat 7.1 and later, use BIND 9, which deprecated the -g option.
		    # Nicely, all of these run as an alternate user already.

		    if ($GLOBAL_DISTRO =~ /MN/) {
		        &B_replace_line("/etc/rc.d/init.d/named",'^\s*daemon\s+named',"daemon named -u $user -t $home_dns\n");  
		    }
		    elsif ($GLOBAL_DISTRO =~ /RH/) {
			&B_append_line('/etc/sysconfig/named','^\s*ROOTDIR\s*\=\s*',"ROOTDIR=$home_dns\n");
		    }
		}
		
	    }
	    else {
		# We only do this to Red Hat and Mandrake boxes for now, because
		# we'd have to find all the libraries and modify startup files
		# and this can just be totally difficult to automate when you
		# don't know the target...
		
		&ErrorLog("Couldn't chroot named! Not on a Red Hat / Mandrake box or $home_dns already exists.\n");
	    }
	    
	} 
    }
}


sub DeactivateNamed { 


    if (&getGlobalConfig("DNS","namedoff") eq "Y") {

	&ActionLog("# sub DeactivateNamed\n");

	# Deactivate BIND unless it is being used.

	&B_chkconfig_off ("named");
    }

} 


1;