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