This file is indexed.

/etc/network/ifupdown-scripts-zg2.d/common-functions is in ifupdown-scripts-zg2 0.6-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/bin/bash
# $Header$

export LC_ALL=C

# set global vars, make statefile existent, and do basic sanity checks

VERBOSE=${IF_VERBOSE:-}

if [ -n "${IF_DEBUG:-}" ]; then
  set -x
fi
set -u

me="$0"
[ ${IF_DEBUG:-} ] && echo "$me $IFACE $MODE debug"

[ "$METHOD" = "manual" ] || exit 0

if [ "$ADDRFAM" != "inet" ] && [ "$ADDRFAM" != "inet6" ]; then
  exit 0
fi

# common functions for interface scripts

#
# misc helper functions
#

error()
{
  echo "$me: ERR: $1" >&2
}

warn()
{
  echo "$me: WARN: $1" >&2
}

abort()
{
  error "$1"
  add_down "abort" "1"
  exit 1
}

msg()
{
  echo "$me: $1" >&2
}

verbose()
{
  [ $VERBOSE ] && echo "$me: $*"
}

cmd()
{
  local RET
  [ $VERBOSE ] && echo "$me: $*"
  SHOW_ERRORS=1
  if [ "$1" = "--no-errors" ]; then
    shift
    SHOW_ERRORS=0
  fi
  eval $1
  RET=$?
  if [ "$RET" -ne 0 -a "${NO_ERRORS:-}" = "1" ]; then
    echo >&2 "$me: $1 returned return value $RET"
  fi
  return $RET
}

# set_bool_value key{0/1} value, where key is a path in
# /proc/sys/net/ipv4/conf/$IF_DEVICE
set_bool_value()
{
  KEY="$1"
  VAL="$2"

  [ -z "${VAL:-}" ] && exit 0

  IFACEDIR="/proc/sys/net/ipv4/conf/$IF_DEVICE"

  if ! echo $VAL | grep -s -q '\(0\|1\)'; then
    abort "no valid value $VAL for key $KEY"
  fi
  
  if [ -e "$IFACEDIR/$KEY" ]; then
    echo $VAL > $IFACEDIR/$KEY
  else
    abort "No such key $KEY in $IFACEDIR for $IF_DEVICE"
  fi
}

#
# functions for handling the state
#

# add_down KEY VAL
add_down()
{
  KEY="$1"
  VAL="$2"

  # add ip commands to down script
  verbose "add_down $1 $2"
  echo "$1: $2" >> $STATEFILE
}

get_down()
{
  KEY="$1"
  
  # execute and remove commands from state file
  if ! ls -n "$STATEFILE" | grep -q -- '^-....-..-. [[:digit:]]\+ 0 '; then
    abort "$STATEFILE isn't a plain file, or is writeable by non-root user"
  fi

  # read state file and return commands

  < $STATEFILE sed -n "/^$KEY:/s/^[^\:]*\:[[:space:]]\(.*\)/\1/p"
}

usrlesstac()
{
  sed '1!G;h;$!d;'
}

exec_down()
{
  KEY="$1"
  CMD="$2"
  verbose "exec_down $KEY $CMD"
  
  # execute and remove commands from state file
  if ! ls -n "$STATEFILE" | grep -q -- '^-....-..-. [[:digit:]]\+ 0 '; then
    abort "$STATEFILE isn't a plain file, or is writeable by non-root user"
  fi
  
  # read state file and execute commands _in_ _reverse_ _order_

  COMMAND="$CMD"
  if [ "$CMD" = "exec" ]; then
    COMMAND=""
  fi
  if [ -n "$CMD" ]; then
    < $STATEFILE sed -n "/^$KEY:/s/^[^\:]*\:[[:space:]]\(.*\)/\1/p" \
    | usrlesstac | while read line; do
      verbose "exec_down $COMMAND $line"
      eval "cmd \"$COMMAND $line\""
    done
  fi
  
  # remove commands from STATEFILE
  
  sed -i "/^${KEY}:/d" $STATEFILE
}

remove_state()
{
  rm -f "$STATEFILE"
}

# dev_state_entry DEVICE KEY
dev_state_entry()
{
  local dev key
  dev="$1"; key="$2"

  # check parameter
  local state
  state="$STATEDIR/$dev.state"
  [ ! -f "$state" ] && return 1
  
  < $state sed -n "/^$key: /{s/^[^:]\+:[[:space:]]*\(.*\)/\1/;p;q;}"
}

# state_entry KEY
state_entry()
{
  dev_state_entry "$IFACE" "$1"
  return $?
}

# find_if_by_state KEY PATTERN
list_if_by_state()
{
  local key pat
  key="$1"; pat="$2"; shift 2

  [ -z "${key:-}" -o -z "${pat:-}" ] && return 1

  local sfile dev
  if [ $# -eq 0 ]; then
    for sfile in $STATEDIR/*.state; do
      dev="${sfile%.state}"
      dev="${dev##*/}"

      if dev_state_entry "$dev" "$key" | grep -q "$pat"; then
        echo "$dev"
      fi
    done
  else
    for dev; do
      if dev_state_entry "$dev" "$key" | grep -q "$pat"; then
        echo "$dev"
      fi
    done
  fi
}

#
# helper functions for network interfaces
#

# list_ifaces
list_ifaces()
{
  < /proc/net/dev sed -n '/:/{s/^\([^:]\+\):.*/\1/;p;}'
}

# if_exists IFACE
if_exists()
{
  for iface in $(list_ifaces); do
    [ "$iface" = "$1" ] && return 0
  done
  return 1
}

# is_if_up IFACE
is_if_up()
{
  local state

  if if_exists "$1"; then
    if [ "$(ip link show "$1" | sed -n '1{s/.*[,<]\(UP\)[,>].*/\1/;p;q}')" = \
         "UP" ]; then
      return 0
    else
      return 1
    fi
  else
    return 0
  fi
}

# is_bonding_master IFACE
is_bonding_master()
{
  local state

  if if_exists "$1"; then
    if [ "$(ip link show "$1" | sed -n '1{s/.*[,<]\(MASTER\)[,>].*/\1/;p;q}')" = \
         "MASTER" ]; then
      return 0
    else
      return 1
    fi
  else
    return 0
  fi
}

# has_ip_address IFACE
has_ip_address()
{
  ip addr show dev "$1" | grep -s -q inet[^6]
}

# get_if_mac IFACE
get_if_mac()
{
  ip --oneline link show "$1" 2>/dev/null | \
     sed 's/.*[[:space:]]\+link\/ether[[:space:]]\+\([0-9a-fA-F:]\+\).*/\1/;'
}

# returns v4 or v6 for appropriate address
addrtype()
{
  ADDR="$1"
  if echo $ADDR | grep -q '^[0-9\./]\+$'; then
    echo "v4"
    return 0
  elif echo $ADDR | grep -q '^[0-9a-fA-F\:/]\+$'; then
    echo "v6"
    return 0
  else
    echo "unknown address format"
    exit 1
  fi
}

#
# helper functions for vlan management
#

modprobe 8021q 2> /dev/null || :

VLAN_CFG=/proc/net/vlan/config

if [ ! -r "$VLAN_CFG" ]; then
  VLAN_SUPPORT=no
  # provide dummy functions
  find_vlan() { return 0; }
  get_vlan_id() { echo 0; }
  is_active_vlan_master() { return 0; }
else
VLAN_SUPPORT=yes

# the real functions
read_vlan_config()
{
  < "$VLAN_CFG" sed -n '1d;2d;{s/|/ /g;s/[[:space:]]\+/ /g;p;}'
}

#find_vlan MASTER VLANID
find_vlan()
{
  local iface vlanid master
  read_vlan_config \
  | while read iface vlanid master; do
    if [ "$master" = "$1" -a "$vlanid" = "$2" ]; then
      echo "$iface"
      break
    fi
  done
}

# get_vlan_id IFACE
get_vlan_id()
{
  local iface vlanid master
  read_vlan_config \
  | while read iface vlanid master; do
    if [ "$iface" = "$1" ]; then
      echo "$vlanid"
      exit 42
    fi
  done
  [ $? -eq 42 ] || echo 0
  return 0
}

# is_active_vlan_master IFACE
is_active_vlan_master()
{
  local iface vlanid master
  read_vlan_config | (
    while read iface vlanid master; do
      if [ "$master" = "$1" ]; then
        exit 0
      fi
    done
    exit 1
  )
  return $?
}

# is_vlan_slave IFACE
is_vlan_slave()
{
  read_vlan_config | \
     sed 's/^\([^[:space:]]\+\)[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+\([^[:space:]]\+\)$/\1/' | \
     grep -q "$1"
  return $?
}

fi

# init code that should be executed for every script

# physical device defaults to logical
if [ -z "${IF_DEVICE:-}" ]; then
  IF_DEVICE="$IFACE"
  export IF_DEVICE
fi

# it is possible that /usr is not yet mounted, we try to get by without /usr
PATH="/sbin:/bin"
NWSTATE="/etc/network/run"
STATEDIR="$NWSTATE/ifupdown-scripts-zg2"
STATEFILE="$STATEDIR/$IFACE.state"

# check that noone but root can write to $NWSTATE

if ! ls -nd $(readlink --canonicalize "$NWSTATE") | grep -q -- '^d....-..-. [[:digit:]]\+ 0 0 '; then
  abort "$NWSTATE is not (or does not point to) a directory, or is writeable by non-root user"
fi

if ! [ -e "$STATEFILE" ]; then
  umask 022
  if ! mkdir -p $STATEDIR; then
    abort "Unable to create $STATEDIR"
  fi
  touch $STATEFILE
fi
chown root $STATEFILE
chmod 644 $STATEFILE

# check for abort condition
if [ -f "$STATEFILE" -a "$MODE" = 'start' ]; then
  if grep -q '^abort:' $STATEFILE; then
    verbose "$IFACE $MODE abort"
    exit 0
  fi
fi


# Sanitize environment variables
IF_VLAN_ID=$[${IF_VLAN_ID:-0}+0]
IF_TYPE=${IF_TYPE:-}
IF_MASTER=${IF_MASTER:-}
IF_SCOPE=${IF_SCOPE:-}
IF_PROXY_ARP=${IF_PROXY_ARP:-}
IF_ACCEPT_REDIRECTS=${IF_ACCEPT_REDIRECTS:-}
IF_FLAGS=${IF_FLAGS:-}

verbose "$IFACE $MODE"

# on bonding interfaces, ifupdown doesn't set IF_TYPE
if [ -z "$IF_TYPE" ] && is_bonding_master $IF_DEVICE; then
  IF_TYPE="bonding-master"
fi


# vi:sw=2

# end of file