summaryrefslogtreecommitdiff
path: root/scripts/rl-system.init
blob: cc78700b9148400141ce7858d544bb2a1204a66f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Copyright (C) 2007-2010 Vyatta, Inc.
# All Rights Reserved.
#
# Description:	Vyatta Router system setup
#		Indirect init sub-script of vyatta-router.init
#
# **** End License ****

progname=${0##*/}
ACTION=$1

source /etc/default/vyatta

: ${vyatta_prefix:=/opt/vyatta}
: ${vyatta_bindir:=${vyatta_prefix}/bin}
: ${vyatta_sbindir:=${vyatta_prefix}/sbin}
: ${vyatta_sysconfdir:=${vyatta_prefix}/etc}

[[ $PATH == *${vyatta_bindir}* ]] || PATH+=:${vyatta_bindir}
[[ $PATH == *${vyatta_sbindir}* ]] || PATH+=:${vyatta_sbindir}

export PATH
export DEBIAN_FRONTEND=noninteractive

. /lib/lsb/init-functions

## BOOTFILE is provided by ofr.init
: ${BOOTFILE:=$prefix/etc/config/config.boot}

syslog ()
{
    logger -p user.warning -t "$progname" "$*"
}

# easy way to make empty file without any command
empty()
{
    >$1
}

shopt -s extglob nullglob

search_config_if_wan () {
    grep -q "\<serial\>.*\<$1\>" $BOOTFILE
}

add_new_serial_if () {
    __config_additions=/tmp/__config_additions
    rm -f $__config_additions

    ip link show |
        sed -n '/^[0-9]*: wan[0-9]*:/ s/.* \([^:]*\):.*$/\1/p' |
        while read wan ; do
	    if ! search_config_if_wan $wan ; then
	        echo "    serial $wan" >> $__config_additions
	    fi
        done

    if [ -e $__config_additions ]; then
	rm -f /tmp/__bootfile
	sed '/^[! ]*interfaces {$/ r '$__config_additions \
	    $BOOTFILE > /tmp/__bootfile
	mv /tmp/__bootfile $BOOTFILE
	rm -f $__config_additions
    fi
}

## Update the version information
update_version_info () {
    if [ -f ${vyatta_sysconfdir}/version.master ]; then
	cp -u ${vyatta_sysconfdir}/version.master ${vyatta_sysconfdir}/version
    fi
}

clear_or_override_config_files ()
{
    for conf in snmp/snmpd.conf snmp/snmptrapd.conf snmp/snmp.conf \
        keepalived/keepalived.conf cron.d/vyos-crontab \
        ipvsadm.rules default/ipvsadm resolv.conf
    do
	if [ -s /etc/$conf ] ; then
	    empty /etc/$conf
	    chmod 0644 /etc/$conf
	fi
    done
}

update_interface_config () {
    if [ -d /run/udev/vyatta ]; then
        $vyatta_sbindir/vyatta_interface_rescan /run/udev/vyatta $BOOTFILE
    fi
}

create_ssh_host_keys () {
    if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
        syslog "Creating ssh v2 rsa host key."
        ssh-keygen -q -N '' -t rsa -f /etc/ssh/ssh_host_rsa_key
    fi;
    if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
        syslog "Creating ssh v2 dsa host key."
        ssh-keygen -q -N '' -t dsa -f /etc/ssh/ssh_host_dsa_key
    fi;
    if [ ! -f "/etc/ssh/ssh_host_ed25519_key" ]; then
        syslog "Creating ssh ed25519 host key."
        ssh-keygen -q -N '' -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
    fi;
}

# These are all the default security setting which are later
# overridden when configuration is read. These are the values the
# system defaults.
security_reset () {
   # restore PAM back to virgin state (no radius other services)
   rm -f /etc/pam_radius_auth.conf
   if grep -q radius /etc/pam.d/common-auth; then
      pam-auth-update --package --remove radius
   fi

   # The PPPOE config files are re-generated by the configuration subsystem
   # on every boot, so we delete the old ones here.
   rm -f /etc/ppp/peers/pppoe*
}

start () {
    mkdir -p /var/run/vyatta /var/log/vyatta
    chgrp vyattacfg /var/run/vyatta /var/log/vyatta
    chmod 775 /var/run/vyatta /var/log/vyatta

    # mountpoint for bpf maps required by xdp
    mount -t bpf none /sys/fs/bpf

    update_interface_config
    create_ssh_host_keys || \
	log_failure_msg "can't initialize ssh host keys"
    clear_or_override_config_files || \
	log_failure_msg "can\'t reset config files"

    add_new_serial_if || \
	log_failure_msg "can\'t add serial interfaces"

    security_reset

    update_version_info

    # Remove links from the post-commit hooks directory.
    # note that this approach only supports hooks that are "configured",
    # i.e., it does not support hooks that need to always be present.
    cpostdir=$(cli-shell-api getPostCommitHookDir)

    # exclude commits hooks from vyatta-cfg
    excluded="10vyatta-log-commit.pl 99vyos-user-postcommit-hooks"

    if [ -d "$cpostdir" ]; then
	for f in $cpostdir/*
	do
	    if [[ ! $excluded =~ $(basename $f) ]]; then
		rm -f $cpostdir/$(basename $f)
	    fi
	done
    fi

    ## Clear out apt config file--it will be filled in by config load
    empty /etc/apt/sources.list

    # remove any interface stat clears
    rm -f /var/run/vyatta/*.stats
}

case "$ACTION" in
    start) start ;;
    stop|restart|force-reload) true ;; # nothing to stop/restart
    *)	log_failure_msg "action unknown: $ACTION" ;
	false ;;
esac

exit $?