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