aboutsummaryrefslogtreecommitdiffstats
path: root/lsb-bootscripts/sbin/ifdown
blob: e6d49c87499285e257301926d32b028ad0b06004 (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
#!/bin/sh
########################################################################
# Begin /sbin/ifdown
#
# Description : Interface Down
#
# Authors     : DJ Lucas - dj@linuxfromscratch.org
#
# Version     : 00.02
#
########################################################################

. /lib/lsb/init-functions

function get_args()
    {
        if test -z "${1}" ; then
            showhelp
            exit 1
        fi

        while test -n "${1}" ; do
            case "${1}" in
                -c | --configfile)
                    check_arg $1 $2
                    CONFIGFILE="${2}"
                    shift 2
                ;;
                -f | --force)
                    FORCE="1"
                    shift 1
                ;;
                eth* | iw* | wlan*)
                     INTERFACE="${1}"
                     shift 1
                ;;
                -h | --help)
                     showhelp
                     exit 0
                ;;
                *)
                   showhelp
                   echo "ERROR: '${1}' unknown argument"
                   echo ""
                   exit 2
                ;;
            esac
        done
    }

function check_arg()
    {
        echo "${2}" | grep -v "^-" > /dev/null
        if [ -z "${?}" -o ! -n "${2}" ]; then
            echo "Error:  ${1} requires a valid argument."
            exit 2
        fi
    }

function showhelp()
    {
        echo ""
        echo "`/usr/bin/basename ${0}` brings down a valid network interface."
        echo ""
        echo "Options:"
        echo "          -c  --configfile    The path to an interface configuration file"
        echo "                              If no configuration file is given, all files"
        echo "                              listed in /etc/network/ifconfig.<int> will"
        echo "                              be processed, regarless of the value of ONBOOT"
        echo "          -f  --force         Flush all IPs and force the interface down."
        echo "          -h  --help          Show this help message and exit."
        echo ""
        echo "Examples:"
        echo "          `/usr/bin/basename ${0}` eth0 -c /run/network/ifconfig.eth0/ipv4"
        echo "          `/usr/bin/basename ${0}` eth0 --force -c /run/network/ifconfig.eth0/ipv4"
        echo "          `/usr/bin/basename ${0}` eth0 --force"
        echo "          `/usr/bin/basename ${0}` eth0"
        echo ""
        echo ""
    }

# Intialize empty variables so that the shell does not polute the script
CONFIGFILE=""
CONFIGDIR=""
INTERFACE=""
FORCE=""
failed=0

# Process command line arguments
get_args ${@}

# Handle common errors - No need to account for bootscripts, this should not
# happen during boot or shutdown.
if [ "${CONFIGFILE}x" != "x" -a ! -f "${CONFIGFILE}" ]; then
    echo "ERROR: ${CONFIGFILE} is not a valid network configuration file."
    echo ""
    exit 2
fi

if [ "${INTERFACE}x" == "x" ]; then
    echo "ERROR: No interface was given"
    echo ""
    exit 2
else
    if ! grep "${INTERFACE}" /proc/net/dev 2>&1 > /dev/null; then
        echo "ERROR: ${INTERFACE} is not a valid network interface."
        echo ""
        exit 2
    fi
fi

# If a configuration file is present, use it
if [ "${CONFIGFILE}x" != "x" ]; then
    . "${CONFIGFILE}"
    if [ -x "/lib/network-services/${SERVICE}" ]; then
        # do the work
        if IFCONFIG=${CONFIGFILE} \
            /lib/network-services/${SERVICE} ${INTERFACE} down; then
            rm "${CONFIGFILE}"
        fi
    else
        echo "ERROR: Service '${SERVICE}' is not a valid service."
        echo ""
        exit 2
    fi
# No interface configuration file was given
else
    # Process all running interface configuration files
    CONFIGDIR="/run/network/ifconfig.${INTERFACE}"
    if [ -d "${CONFIGDIR}" ]; then
        FILES=`ls "${CONFIGDIR}"`
        for CONFIGFILE in ${FILES}
        do
            (
                . "${CONFIGDIR}/${CONFIGFILE}"
                # No error checking necessary if they are in /run
                if IFCONFIG="${CONFIGDIR}/${CONFIGFILE}" \
                    /lib/network-services/${SERVICE} ${INTERFACE} down; then
                    rm "${CONFIGDIR}/${CONFIGFILE}"
                fi
            )
        done
        # all running config files processes, set the link down
        message="Setting interface ${INTERFACE} down..."
        /sbin/ip link set "${INTERFACE}" down
        evaluate_retval standard
    else
        if [ "${FORCE}" != "1" ]; then
            echo "ERROR: No configuration files found for ${INTERFACE}."
            echo ""
            exit 2
        fi
    fi
fi
    
if [ "${FORCE}" == "1" ]; then
    /sbin/ip addr flush dev "${INTERFACE}" 2>&1 > /dev/null || failed=1
    if [ "${failed}" == "1" ]; then
        log_failure_msg "Flushing IP addresses from interface ${INTERFACE}..."
        echo ""
        exit 1
    else
        log_success_msg "Flushing IP addresses from interface ${INTERFACE}..."
    fi
    /sbin/ip link set dev "${INTERFACE}" down 2>&1 > /dev/null || failed=1
    if [ "${failed}" == "1" ]; then
        log_failure_msg "Setting link down for interface ${INTERFACE}..."
        echo ""
        exit 1
    else
        log_success_msg "Setting link down for interface ${INTERFACE}..."
    fi
fi

exit "${failed}"