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