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