Annotation of src/etc/rc, Revision 1.388
1.388 ! deraadt 1: # $OpenBSD: rc,v 1.387 2011/07/18 21:01:23 deraadt Exp $
1.1 deraadt 2:
3: # System startup script run by init on autoboot
4: # or after single-user.
5: # Output and error are redirected to console by init,
6: # and the console is the controlling terminal.
7:
1.131 millert 8: # Subroutines (have to come first).
9:
10: # Strip comments (and leading/trailing whitespace if IFS is set)
11: # from a file and spew to stdout
12: stripcom() {
13: local _file="$1"
14: local _line
15:
16: {
17: while read _line ; do
18: _line=${_line%%#*} # strip comments
19: test -z "$_line" && continue
20: echo $_line
21: done
22: } < $_file
23: }
24:
1.265 millert 25: # Update resource limits when sysctl changes
26: # Usage: update_limit -X loginconf_name
27: update_limit() {
28: local _fl="$1" # ulimit flag
29: local _lc="$2" # login.conf name
30: local _new _suf
31:
32: for _suf in "" -cur -max; do
33: _new=`getcap -f /etc/login.conf -s ${_lc}${_suf} daemon 2>/dev/null`
34: if [ X"$_new" != X"" ]; then
35: if [ X"$_new" = X"infinity" ]; then
36: _new=unlimited
37: fi
38: case "$_suf" in
39: -cur)
40: ulimit -S $_fl $_new
41: ;;
42: -max)
43: ulimit -H $_fl $_new
44: ;;
45: *)
46: ulimit $_fl $_new
47: return
48: ;;
49: esac
50: fi
51: done
52: }
53:
54: sysctl_conf() {
1.267 millert 55: test -s /etc/sysctl.conf || return
56:
1.265 millert 57: # delete comments and blank lines
58: set -- `stripcom /etc/sysctl.conf`
59: while [ $# -ge 1 ] ; do
60: sysctl $1
61: # update limits if needed
62: case $1 in
63: kern.maxproc=*)
64: update_limit -p maxproc
65: ;;
66: kern.maxfiles=*)
67: update_limit -n openfiles
68: ;;
69: esac
70: shift
71: done
72: }
73:
74: mixerctl_conf()
75: {
1.267 millert 76: test -s /etc/mixerctl.conf || return
77:
1.265 millert 78: # delete comments and blank lines
79: set -- `stripcom /etc/mixerctl.conf`
80: while [ $# -ge 1 ] ; do
81: mixerctl -q $1 > /dev/null 2>&1
82: shift
83: done
84: }
85:
1.267 millert 86: wsconsctl_conf()
87: {
88: local save_IFS="$IFS"
89:
90: test -x /sbin/wsconsctl -a -s /etc/wsconsctl.conf || return
91: # delete comments and blank lines
92: IFS="
93: "
94: set -- `stripcom /etc/wsconsctl.conf`
95: IFS="$save_IFS"
96: while [ $# -ge 1 ] ; do
1.356 deraadt 97: eval wsconsctl $1
1.267 millert 98: shift
99: done
100: }
101:
1.312 djm 102: random_seed()
103: {
104: if [ -f /var/db/host.random -a "X$random_seed_done" = "X" ]; then
1.347 deraadt 105: dd if=/var/db/host.random of=/dev/arandom bs=65536 count=1 \
1.312 djm 106: > /dev/null 2>&1
1.328 deraadt 107:
1.312 djm 108: # reset seed file, so that if a shutdown-less reboot occurs,
109: # the next seed is not a repeat
1.347 deraadt 110: dd if=/dev/arandom of=/var/db/host.random bs=65536 count=1 \
1.312 djm 111: > /dev/null 2>&1
112:
113: random_seed_done=1
114: fi
115: }
116:
1.318 djm 117: fill_baddynamic()
118: {
119: local _service="$1"
120: local _sysctl="net.inet.${_service}.baddynamic"
121: local _name _port _srv _junk _ban
122: local _i=0
1.388 ! deraadt 123: local _ifs="${IFS}"
! 124: IFS=" /"
! 125: while read _name _port _srv _junk; do
! 126: [ "x${_srv}" = "x${_service}" ] && \
! 127: [ "x${_name}" = "x${_name#\#}" ] || continue;
! 128: if [ "x${_ban}" = "x" ]; then
! 129: _ban="+${_port}"
! 130: else
! 131: _ban="${_ban},+${_port}"
! 132: fi
! 133: # Flush before argv gets too long
! 134: if [ $((++_i)) -gt 128 ]; then
1.318 djm 135: sysctl ${_sysctl}=${_ban} >/dev/null
1.388 ! deraadt 136: _ban=""
! 137: _i=0
1.318 djm 138: fi
1.388 ! deraadt 139: done < /etc/services;
! 140: if [ "x${_ban}" != "x" ]; then
! 141: sysctl ${_sysctl}=${_ban} >/dev/null
! 142: fi
! 143: IFS="${_ifs}"
1.318 djm 144: }
145:
1.353 robert 146: start_daemon()
147: {
1.354 robert 148: local _n
1.353 robert 149: for _n; do
150: eval _do=\${${_n}_flags}
151: if [ X"${_do}" != X"NO" ]; then
152: /etc/rc.d/${_n} start
153: fi
154: done
155: }
156:
1.373 deraadt 157: make_keys()
158: {
159: if [ X"${named_flags}" != X"NO" ]; then
160: if ! cmp -s /etc/rndc.key /var/named/etc/rndc.key ; then
161: echo -n "rndc-confgen: generating shared secret... "
162: if rndc-confgen -a -t /var/named >/dev/null 2>&1; then
163: chmod 0640 /var/named/etc/rndc.key \
164: >/dev/null 2>&1
165: echo done.
166: else
167: echo failed.
168: fi
169: fi
170: fi
171:
172: if [ ! -f /etc/isakmpd/private/local.key ]; then
173: echo -n "openssl: generating isakmpd/iked RSA key... "
174: if openssl genrsa -out /etc/isakmpd/private/local.key 2048 \
175: >/dev/null 2>&1; then
176: chmod 600 /etc/isakmpd/private/local.key
177: openssl rsa -out /etc/isakmpd/local.pub -in \
178: /etc/isakmpd/private/local.key -pubout \
179: >/dev/null 2>&1
180: echo done.
181: else
182: echo failed.
183: fi
184: fi
185:
186: if [ ! -f /etc/iked/private/local.key ]; then
187: # Just copy the generated isakmpd key
188: cp /etc/isakmpd/private/local.key /etc/iked/private/local.key
189: chmod 600 /etc/iked/private/local.key
190: cp /etc/isakmpd/local.pub /etc/iked/local.pub
191: fi
192:
193: ssh-keygen -A
194: }
195:
196: # create Unix sockets directories for X if needed and make sure they have
197: # correct permissions
198: setup_X_sockets()
199: {
200: if [ -d /usr/X11R6/lib ]; then
201: for d in /tmp/.X11-unix /tmp/.ICE-unix ; do
202: if [ -d $d ]; then
203: if [ `ls -ld $d | cut -d' ' -f4` \
204: != root ]; then
205: chown root $d
206: fi
207: if [ `ls -ld $d | cut -d' ' -f1` \
208: != drwxrwxrwt ]; then
209: chmod 1777 $d
210: fi
211: elif [ -e $d ]; then
212: echo "Error: $d exists and isn't a directory."
213: else
214: mkdir -m 1777 $d
215: fi
216: done
217: fi
218: }
219:
1.131 millert 220: # End subroutines
221:
1.1 deraadt 222: stty status '^T'
223:
224: # Set shell to ignore SIGINT (2), but not children;
225: # shell catches SIGQUIT (3) and returns to single user after fsck.
226: trap : 2
227: trap : 3 # shouldn't be needed
228:
229: HOME=/; export HOME
1.349 robert 230: INRC=1; export INRC
1.1 deraadt 231: PATH=/sbin:/bin:/usr/sbin:/usr/bin
232: export PATH
1.108 deraadt 233:
1.343 robert 234: # pick up option configuration
235: . /etc/rc.conf
236:
1.266 millert 237: if [ X"$1" = X"shutdown" ]; then
1.347 deraadt 238: dd if=/dev/arandom of=/var/db/host.random bs=65536 count=1 >/dev/null 2>&1
1.108 deraadt 239: chmod 600 /var/db/host.random >/dev/null 2>&1
1.352 ajacouto 240: local _c=$?
1.380 ajacouto 241: if [ ${_c} -eq 0 -a -n "${pkg_scripts}" ]; then
1.352 ajacouto 242: echo -n 'stopping package daemons:'
1.380 ajacouto 243: while [ -n "${pkg_scripts}" ]; do
244: _r=${pkg_scripts##* }
245: pkg_scripts=${pkg_scripts%%*( )${_r}}
1.352 ajacouto 246: [ -x /etc/rc.d/${_r} ] && /etc/rc.d/${_r} stop
247: done
248: echo '.'
249: fi
250: if [ ${_c} -eq 0 -a -f /etc/rc.shutdown ]; then
1.108 deraadt 251: echo /etc/rc.shutdown in progress...
252: . /etc/rc.shutdown
253: echo /etc/rc.shutdown complete.
1.240 mcbride 254:
255: # bring carp interfaces down gracefully
1.331 sthen 256: ifconfig | while read a b; do
257: case $a in
258: carp+([0-9]):) ifconfig ${a%:} down ;;
1.329 sthen 259: esac
1.240 mcbride 260: done
1.246 mcbride 261:
1.266 millert 262: if [ X"${powerdown}" = X"YES" ]; then
1.246 mcbride 263: exit 2
264: fi
265:
1.108 deraadt 266: else
267: echo single user: not running /etc/rc.shutdown
268: fi
269: exit 0
270: fi
1.1 deraadt 271:
272: # Configure ccd devices.
1.17 deraadt 273: if [ -f /etc/ccd.conf ]; then
1.1 deraadt 274: ccdconfig -C
275: fi
1.98 jakob 276:
277: # Configure raid devices.
278: for dev in 0 1 2 3; do
279: if [ -f /etc/raid$dev.conf ]; then
280: raidctl -c /etc/raid$dev.conf raid$dev
281: fi
282: done
1.190 tdeval 283:
284: # Check parity on raid devices.
1.191 deraadt 285: raidctl -P all
1.1 deraadt 286:
1.172 miod 287: swapctl -A -t blk
1.170 deraadt 288:
1.17 deraadt 289: if [ -e /fastboot ]; then
1.1 deraadt 290: echo "Fast boot: skipping disk checks."
1.266 millert 291: elif [ X"$1" = X"autoboot" ]; then
1.1 deraadt 292: echo "Automatic boot in progress: starting file system checks."
1.31 millert 293: fsck -p
1.1 deraadt 294: case $? in
295: 0)
296: ;;
297: 2)
298: exit 1
299: ;;
300: 4)
301: echo "Rebooting..."
302: reboot
303: echo "Reboot failed; help!"
304: exit 1
305: ;;
306: 8)
307: echo "Automatic file system check failed; help!"
308: exit 1
309: ;;
310: 12)
311: echo "Boot interrupted."
312: exit 1
313: ;;
314: 130)
315: # interrupt before catcher installed
316: exit 1
317: ;;
318: *)
319: echo "Unknown error; help!"
320: exit 1
321: ;;
322: esac
323: fi
324:
325: trap "echo 'Boot interrupted.'; exit 1" 3
326:
327: umount -a >/dev/null 2>&1
1.303 grunk 328: mount -a -t nonfs,vnd
1.57 niklas 329: mount -uw / # root on nfs requires this, others aren't hurt
1.1 deraadt 330: rm -f /fastboot # XXX (root now writeable)
1.177 deraadt 331:
1.312 djm 332: random_seed
1.1 deraadt 333:
1.255 henning 334: # set flags on ttys. (do early, in case they use tty for SLIP in netstart)
1.1 deraadt 335: echo 'setting tty flags'
336: ttyflags -a
1.77 angelos 337:
1.252 mcbride 338: if [ -f /sbin/kbd -a -f /etc/kbdtype ]; then
339: kbd `cat /etc/kbdtype`
340: fi
341:
1.279 deraadt 342: wsconsctl_conf
343:
1.266 millert 344: if [ X"${pf}" != X"NO" ]; then
1.211 mcbride 345: RULES="block all"
1.228 henning 346: RULES="$RULES\npass on lo0"
1.195 dhartmei 347: RULES="$RULES\npass in proto tcp from any to any port 22 keep state"
1.208 camield 348: RULES="$RULES\npass out proto { tcp, udp } from any to any port 53 keep state"
1.215 camield 349: RULES="$RULES\npass out inet proto icmp all icmp-type echoreq keep state"
1.257 grange 350: if ifconfig lo0 inet6 >/dev/null 2>&1; then
1.258 itojun 351: RULES="$RULES\npass out inet6 proto icmp6 all icmp6-type neighbrsol"
352: RULES="$RULES\npass in inet6 proto icmp6 all icmp6-type neighbradv"
1.257 grange 353: RULES="$RULES\npass out inet6 proto icmp6 all icmp6-type routersol"
354: RULES="$RULES\npass in inet6 proto icmp6 all icmp6-type routeradv"
355: fi
1.322 mcbride 356: RULES="$RULES\npass proto carp keep state (no-sync)"
1.193 deraadt 357: case `sysctl vfs.mounts.nfs 2>/dev/null` in
1.184 deraadt 358: *[1-9]*)
359: # don't kill NFS
1.324 henning 360: RULES="set reassemble yes no-df\n$RULES"
1.306 deraadt 361: RULES="$RULES\npass in proto { tcp, udp } from any port { 111, 2049 } to any"
362: RULES="$RULES\npass out proto { tcp, udp } from any to any port { 111, 2049 }"
1.184 deraadt 363: ;;
364: esac
1.269 dhartmei 365: echo $RULES | pfctl -f -
366: pfctl -e
1.176 kjell 367: fi
1.318 djm 368:
369: # Fill net.inet.(tcp|udp).baddynamic lists from /etc/services
370: fill_baddynamic udp
371: fill_baddynamic tcp
1.176 kjell 372:
1.267 millert 373: sysctl_conf
1.260 jsg 374:
1.1 deraadt 375: # set hostname, turn on network
376: echo 'starting network'
1.289 henning 377: ifconfig -g carp carpdemote 128
1.264 deraadt 378: if [ -f /etc/resolv.conf.save ]; then
1.334 deraadt 379: mv -f /etc/resolv.conf.save /etc/resolv.conf
1.264 deraadt 380: touch /etc/resolv.conf
381: fi
1.1 deraadt 382: . /etc/netstart
1.348 deraadt 383: echo rekey > /dev/arandom # any write triggers an RC4 rekey
1.176 kjell 384:
1.266 millert 385: if [ X"${pf}" != X"NO" ]; then
1.176 kjell 386: if [ -f ${pf_rules} ]; then
1.198 dhartmei 387: pfctl -f ${pf_rules}
1.309 mpf 388: fi
389: # bring up pfsync after the working ruleset has been loaded
1.367 deraadt 390: if [ -f /etc/hostname.pfsync0 ]; then
391: . /etc/netstart pfsync0
392: fi
1.176 kjell 393: fi
1.1 deraadt 394:
1.278 otto 395: mount -s /usr >/dev/null 2>&1
396: mount -s /var >/dev/null 2>&1
1.180 deraadt 397:
1.346 deraadt 398: # if there's no /var/db/host.random, use /dev/arandom to create one
1.149 deraadt 399: if [ ! -f /var/db/host.random ]; then
1.347 deraadt 400: dd if=/dev/arandom of=/var/db/host.random bs=65536 count=1 \
1.149 deraadt 401: >/dev/null 2>&1
402: chmod 600 /var/db/host.random >/dev/null 2>&1
403: else
1.312 djm 404: # Try to read seed if it was not initially present (e.g. /var on NFS)
405: random_seed
1.149 deraadt 406: fi
1.29 deraadt 407:
1.55 deraadt 408: # clean up left-over files
1.376 deraadt 409: rm -f /etc/nologin /var/spool/lock/LCK.* /var/spool/uucp/STST/*
1.247 henning 410: (cd /var/run && { rm -rf -- *; install -c -m 664 -g utmp /dev/null utmp; })
1.196 beck 411: (cd /var/authpf && rm -rf -- *)
1.96 alex 412:
413: # save a copy of the boot messages
414: dmesg >/var/run/dmesg.boot
1.55 deraadt 415:
1.373 deraadt 416: make_keys
417:
1.379 deraadt 418: echo -n 'starting early daemons:'
1.387 deraadt 419: start_daemon syslogd ldattach pflogd named nsd ntpd isakmpd iked sasyncd
1.353 robert 420: echo '.'
1.280 hshoexer 421:
422: if [ X"${ipsec}" != X"NO" ]; then
423: if [ -f ${ipsec_rules} ]; then
424: ipsecctl -f ${ipsec_rules}
425: fi
1.40 provos 426: fi
1.1 deraadt 427:
1.379 deraadt 428: echo -n 'starting RPC daemons:'
1.355 robert 429: start_daemon portmap
1.376 deraadt 430: if [ X"`domainname`" != X"" ]; then
1.385 deraadt 431: start_daemon ypserv ypbind yppasswdd
1.376 deraadt 432: fi
1.385 deraadt 433: start_daemon ypldap mountd nfsd lockd statd amd
1.1 deraadt 434: echo '.'
1.58 deraadt 435:
1.278 otto 436: mount -a
1.172 miod 437: swapctl -A -t noblk
1.1 deraadt 438:
439: # /var/crash should be a directory or a symbolic link
440: # to the crash directory if core dumps are to be saved.
441: if [ -d /var/crash ]; then
1.188 tholo 442: savecore ${savecore_flags} /var/crash
1.90 art 443: fi
444:
1.266 millert 445: if [ X"${check_quotas}" = X"YES" ]; then
1.41 downsj 446: echo -n 'checking quotas:'
447: quotacheck -a
448: echo ' done.'
449: quotaon -a
450: fi
1.1 deraadt 451:
1.376 deraadt 452: kvm_mkdb # build kvm(3) databases
1.1 deraadt 453: dev_mkdb
1.101 deraadt 454: chmod 666 /dev/tty[pqrstuvwxyzPQRST]*
1.226 millert 455: chown root:wheel /dev/tty[pqrstuvwxyzPQRST]*
1.1 deraadt 456:
457: # check the password temp/lock file
1.17 deraadt 458: if [ -f /etc/ptmp ]; then
1.1 deraadt 459: logger -s -p auth.err \
1.376 deraadt 460: 'password file may be incorrect -- /etc/ptmp exists'
1.32 deraadt 461: fi
462:
1.49 millert 463: echo clearing /tmp
464:
465: # prune quickly with one rm, then use find to clean up /tmp/[lq]*
466: # (not needed with mfs /tmp, but doesn't hurt there...)
1.339 sthen 467: (cd /tmp && rm -rf [a-km-pr-zA-Z]*)
468: (cd /tmp &&
1.49 millert 469: find . ! -name . ! -name lost+found ! -name quota.user \
1.103 millert 470: ! -name quota.group -execdir rm -rf -- {} \; -type d -prune)
1.373 deraadt 471:
472: setup_X_sockets
1.203 hugh 473:
1.72 deraadt 474: [ -f /etc/rc.securelevel ] && . /etc/rc.securelevel
1.266 millert 475: if [ X"${securelevel}" != X"" ]; then
1.33 millert 476: echo -n 'setting kernel security level: '
1.234 jmc 477: sysctl kern.securelevel=${securelevel}
1.1 deraadt 478: fi
479:
1.34 deraadt 480: # patch /etc/motd
481: if [ ! -f /etc/motd ]; then
482: install -c -o root -g wheel -m 664 /dev/null /etc/motd
483: fi
1.364 guenther 484: if T=`mktemp /tmp/_motd.XXXXXXXXXX`; then
1.102 millert 485: sysctl -n kern.version | sed 1q > $T
486: echo "" >> $T
487: sed '1,/^$/d' < /etc/motd >> $T
488: cmp -s $T /etc/motd || cp $T /etc/motd
489: rm -f $T
490: fi
1.34 deraadt 491:
1.298 ajacouto 492: if [ X"${accounting}" = X"YES" ]; then
493: if [ ! -f /var/account/acct ]; then
494: touch /var/account/acct
495: fi
1.1 deraadt 496: echo 'turning on accounting'; accton /var/account/acct
497: fi
498:
1.363 deraadt 499: if [ -f /sbin/ldconfig ]; then
1.115 deraadt 500: echo 'creating runtime link editor directory cache.'
501: if [ -d /usr/local/lib ]; then
1.183 todd 502: shlib_dirs="/usr/local/lib $shlib_dirs"
1.115 deraadt 503: fi
504: if [ -d /usr/X11R6/lib ]; then
1.183 todd 505: shlib_dirs="/usr/X11R6/lib $shlib_dirs"
1.115 deraadt 506: fi
507: ldconfig $shlib_dirs
1.231 millert 508: fi
509:
1.376 deraadt 510: echo 'preserving editor files.'; /usr/libexec/vi.recover
1.244 markus 511:
1.353 robert 512: echo -n 'starting network daemons:'
1.383 deraadt 513: start_daemon sshd snmpd ldpd ripd ospfd ospf6d bgpd ifstated
514: start_daemon relayd dhcpd dhcrelay mrouted dvmrpd
1.127 itojun 515:
516: if ifconfig lo0 inet6 >/dev/null 2>&1; then
517: fw=`sysctl -n net.inet6.ip6.forwarding`
1.266 millert 518: if [ X"${fw}" = X"0" ]; then
1.353 robert 519: start_daemon rtsold
1.127 itojun 520: else
1.376 deraadt 521: start_daemon route6d rtadvd
1.127 itojun 522: fi
1.281 reyk 523: fi
524:
1.371 deraadt 525: start_daemon hostapd rwhod lpd ldapd sendmail smtpd httpd ftpd
1.377 robert 526: start_daemon ftpproxy identd inetd rarpd bootparamd rbootd mopd
1.387 deraadt 527: start_daemon spamd spamlogd kdc kadmind kpasswdd aucat
1.377 robert 528: echo '.'
1.365 robert 529:
1.379 deraadt 530: if [ X"${spamd_flags}" != X"NO" ]; then
1.319 deraadt 531: /usr/libexec/spamd-setup -D
1.17 deraadt 532: fi
1.335 deraadt 533:
534: # If rc.firstime exists, run it just once, and make sure it is deleted
535: if [ -f /etc/rc.firsttime ]; then
536: mv /etc/rc.firsttime /etc/rc.firsttime.run
1.384 halex 537: . /etc/rc.firsttime.run 2>&1 | tee /dev/tty |
538: mail -s 'rc.firsttime output' root >/dev/null
1.335 deraadt 539: fi
540: rm -f /etc/rc.firsttime.run
1.352 ajacouto 541:
542: # Run rc.d(8) scripts from packages
1.380 ajacouto 543: if [ -n "${pkg_scripts}" ]; then
1.352 ajacouto 544: echo -n 'starting package daemons:'
1.380 ajacouto 545: for _r in $pkg_scripts; do
1.368 robert 546: [ -x /etc/rc.d/${_r} ] && start_daemon ${_r}
1.352 ajacouto 547: done
548: echo '.'
549: fi
1.17 deraadt 550:
1.72 deraadt 551: [ -f /etc/rc.local ] && . /etc/rc.local
1.73 millert 552:
1.379 deraadt 553: ifconfig -g carp -carpdemote 128 # disable carp interlock
1.286 mcbride 554:
1.379 deraadt 555: mixerctl_conf
556: echo -n 'starting local daemons:'
1.386 deraadt 557: start_daemon apmd sensorsd hotplugd watchdogd cron wsmoused xdm
1.73 millert 558: echo '.'
1.1 deraadt 559:
560: date
561: exit 0