summarylogtreecommitdiffstats
path: root/drpadmin
blob: 22c0aaef0516cc5cec75f39faee90f6776d51b63 (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
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
#!/bin/bash

# drpadmin - adminstrative tool for adding and removing portservers,
#    tty devices, and configuration files on servers not running xWindows.
#    From drpadmin written by Digi for SCO, HP UX, and Solaris

set -u

. '/usr/bin/dgrp/config/file_locations'
if [ -z "${DGRP_STORE}" ]; then
  echo "$0: Can't find backing store from file locations."
  exit 2
fi

g_ttyname='tty' # driver prefix
g_procdir="$(dirname "${DGRP_PROCCONFIG}")"
g_distro='Linux' # Originally made for Arch Linux
# Packagers can use sed -i to change these next lines to the appropriate distro name
#g_distro=:::

# return 0 if the the device id $1 is acceptable for use
check_devid() {
  case "$1" in
  [a-o,q-z]|[a-z][a-z]) return 0;;
  p)printf '**** p cannot be used. p is reserved for psuedo devices!';;
  *)printf '**** Please enter 1 or 2 alpha characters';;
  esac
  return 1
}

# return 0 if the kernel is running. We try to start it if possible.
check_kernel() {
  if [ "${EUID}" -eq 0 ] && [ ! -d "${g_procdir}" ]; then
    modprobe 'dgrp'
  fi
  if [ -d "${g_procdir}" ]; then
    local lines
    lines="$(wc -l "${DGRP_PROCCONFIG}" | cut -d ' ' -f 1)"
    if [ "${lines}" -eq 0 ]; then
      # this is not possible because the config file is removed and the kernel module is unloaded when the last device is uninit
      echo 'The kernel module is loaded but no devices are active'
    fi
  else
    echo 'The kernel module is not loaded'
    if [ -s "${DGRP_STORE}" ]; then
      echo 'A configuration exists but will not be active until the kernel module is loaded'
    else
      echo '  systemctl start dgrp_daemon.service'
      echo 'to load the kernel module'
    fi
    return 1
  fi
  return 0
}

#
# add a PortServer entry
#
add_ps() {
  local node val ndev devid secureopt wan ans
  check_kernel

  local line linea available ttyused ipused
  declare -A ttyused
  declare -A ipused
  while IFS='' read -r line || [[ -n "${line}" ]]; do
    read -ra linea <<<"${line}"
    case "${linea[0]:-}" in
    [a-z]|[a-z][a-z])
      ttyused[${linea[0]}]='used'
      ipused[${linea[1]}]='used'
      ;;
    esac
  done < "${DGRP_STORE}"

  declare -A available
  while IFS='' read -r line || [[ -n "${line}" ]]; do
    printf '%s\n' "${line}"
    read -ra linea <<<"${line}"
    case "${linea[0]}" in
    '*'*);;
    '0.0.0.0');;
    [0-9]*'.'[0-9]*'.'[0-9]*'.'[0-9]*)available[${linea[0]}]="${line}";; # crude IP detection
    esac
  done < <(addp.pl -Q --backing-store "${DGRP_STORE}" --timeout=1 --query-short --limit-query=10 | sort)
  unset line linea

  echo 'Answer 0 for any prompt to stop'
  while :; do
    printf '\nEnter the host name or IP address: '
    read node
    if [ -z "${node}" ]; then
      echo 'Enter 0 to abort'
      continue
    fi
    if [ "${node}" = '0' ]; then
      return
    fi
    if [ ! -z "${ipused[${node}]:-}" ]; then
      echo "${node} is already configured"
      continue
    fi

    ping -n -w4 -c3 "${node}"

    ndev=''
    secureopt=''
    if [ ! -z "${available[${node}]:-}" ]; then
      local linea
      read -ra linea <<<"${available[${node}]}"
      ndev="${linea[1]}"
      if [ "${linea[2]}" = 'N' ]; then
        secureopt='never'
      fi
      unset linea
    fi

    while :; do
      if [ -z "${ndev}" ]; then
        printf "Enter the number of ports: "
      else
        printf "Enter the number of ports [${ndev}]: "
      fi
      read val
      if [ ! -z "${val}" ]; then
        ndev="${val}"
      fi
      if [ -z "${ndev}" ]; then
        echo 'Enter 0 to abort'
        continue
      fi
      if [ "${ndev}" = '0' ]; then
        return
      fi
      if [ "${ndev}" -ge 1 ] && [ "${ndev}" -le 64 ]; then
        break
      fi
      printf '\n**** Please enter a number from 1 to 64\n\n'
    done

    while :; do
      printf 'Enter the tty device ID (1 or 2 lower case alpha characters) : '
      read devid
      if [ -z "${devid}" ]; then
        echo 'Enter 0 to abort'
        continue
      fi
      if [ "${devid}" = '0' ]; then
        return
      fi
      if ! check_devid "${devid}"; then
        continue
      fi
      if [ ! -z "${ttyused[${devid}]:-}" ]; then
        echo "${devid} is already used, try another"
        continue
      fi
      break
    done

    if [ -z "${secureopt}" ]; then
      while :; do
        printf '\n\nWould you like this RealPort session to be encrypted?\n\n'
        printf 'NOTE: Not all RealPort products support encrypted RealPort sessions.\n'
        printf "Please check your RealPort product's firmware release notes\n"
        printf 'or product literature before selecting "always".\n'
        printf 'If in doubt, select "never".\n\n'
        printf '(always/never) : (never): '

        read secureopt
        secureopt="${secureopt,,}"
        if [ -z "${secureopt}" ]; then
          secureopt='never'
        fi
        if [ -z "${secureopt}" ] || [ "${secureopt}" = '0' ]; then
          return
        fi
        if [ "${secureopt}" = 'never' ] || [ "${secureopt}" = 'always' ]; then
          break
        fi
        printf '\nPlease answer always or never.\n'
      done
    fi

    while :; do
      printf '\nIf the network connection between your Server system\n'
      printf 'and the remote device includes a WAN link slower than 1 MBit,\n'
      printf 'see the drpd manual page and enter the WAN speed here : '
      read wan
      if [ "${wan}" = '0' ]; then
        return
      fi
      if [ -z "${wan}" ] || [[ "${wan}" =~ [0-9]+ ]]; then
        break
      fi
      printf '\nPlease enter a numeric answer or blank for none\n'
    done

    printf '\nThe following device will be configured,\n'
    printf "${node}\t${ndev}\t${g_ttyname}${devid}\t\tsecure (${secureopt})\t${wan}\n"
    printf '\nIs this correct (y to add, 0 to abort, n to start again) ? '
    read ans
    if [ -z "${ans}" ]; then
      printf '\nEmpty response, not configuring this device.\n'
      return
    fi
    if [ "${ans}" = 'x' ] || [ "${ans}" = '0' ]; then
      printf '\nx pressed, not configuring this device.\n'
      return
    fi
    if [ "${ans}" = 'y' ]; then
      if [ ! -z "${wan}" ]; then
        wan="-s ${wan}"
      fi
      # -m mode and -g group id don't work any more. It's all done with /etc/udev/rules.d/10-dgrp.rules
      echo "$DGRP_CFG" -v -e "${secureopt}" ${wan} init "${devid}" "${node}" "${ndev}"
      if "$DGRP_CFG" -v -e "${secureopt}" ${wan} init "${devid}" "${node}" "${ndev}"; then
        systemctl restart 'dgrp_daemon.service' # can be removed when dgrp gets systemd compatible
      fi
      return
    fi
  done
}

#
# delete a PortServer device
#
delete_ps() {
  if ! check_kernel; then
    return
  fi
  show_ps
  printf '\nEnter the 2 letter id of the device to delete: '
  local devid
  read devid
  if ! check_devid "${devid}"; then
    return
  fi
  echo "$DGRP_CFG" -v uninit "${devid}"
  if "$DGRP_CFG" -v uninit "${devid}"; then
    systemctl restart 'dgrp_daemon.service' # can be removed when dgrp gets systemd compatible
  fi
}

#
# reset by stop/start a PortServer device
#
# $1 = 0 for reset, 1 for move
# $2 = word to display like "Resetting" or "Moving"
# $3 = word to display like "reset" or "move"
# $4 = word to display like "" or "new"
resetmove_ps() {
  if ! check_kernel; then
    return
  fi
  show_ps
  printf "\nWarning: $2 a device will disconnect all active connections!\n"
  printf "Enter the 1 or 2 letter device to $3: "
  local devid
  read devid
  if ! check_devid "${devid}"; then
    return
  fi
  local node
  node="$(grep "^${devid}[^0-9]" "${DGRP_STORE}" | cut -d ' ' -f 2)"
  if [ -z "${node}" ]; then
    printf "Device ${devid} not found"
    return
  fi
  if [ "$1" -eq 1 ]; then # move
    printf "\nEnter the $4 host name or IP address: "
    read node
    if [ -z "${node}" ] || [ "${node}" = "0" ]; then
      return
    fi
    echo ping -w 4 -c 4 "${node}"
    ping -w 4 -c 4 "${node}"
  fi
  echo "$DGRP_CFG" -v stop "${devid}"
  "$DGRP_CFG" -v stop "${devid}"
  echo "$DGRP_CFG" -v start "${devid}" "${node}"
  if "$DGRP_CFG" -v start "${devid}" "${node}"; then
    systemctl restart 'dgrp_daemon.service' # can be removed when dgrp gets systemd compatible
  fi
}

show_ps() {
  check_kernel
  local lines
  if [ -s "${DGRP_STORE}" ]; then
    lines="$(wc -l "${DGRP_STORE}" | cut -d ' ' -f 1)"
  else
    lines=0
  fi
  if [ "${lines}" -eq 0 ]; then
    printf '\nNo devices are configured.\n'
  elif [ "${lines}" -lt 20 ]; then
    echo "$(grep 'PortCount' "${DGRP_STORE}" | cut -d ' ' -f3-)"
    grep "^[a-z]" "${DGRP_STORE}"
  else
    less -S "${DGRP_STORE}"
  fi
}

show_info() {
  if check_kernel; then
    if [ "$(wc -l "${DGRP_PROCCONFIG}" | cut -d ' ' -f 1)" -lt 20 ]; then
      cat "${DGRP_PROCCONFIG}"
    else
      less "${DGRP_PROCCONFIG}"
    fi
  fi
}

show_node() {
  if check_kernel; then
    if [ "$(wc -l "${g_procdir}/nodeinfo" | cut -d ' ' -f 1)" -lt 20 ]; then
      cat "${g_procdir}/nodeinfo"
    else
      less "${g_procdir}/nodeinfo"
    fi
  fi
}

show_version() {
  if check_kernel; then
    if [ "$(wc -l "${g_procdir}/info" | cut -d ' ' -f 1)" -lt 20 ]; then
      cat "${g_procdir}/info"
    else
      less "${g_procdir}/info"
    fi
  fi
}

# main body

echo ''

if [ "${EUID}" -ne 0 ]; then
  printf '\nDigi Realport Current Configuration:\n'
  show_ps
else
  echo "Digi Realport Configuration Menu for ${g_distro}"
  echo ''
  echo 'Add     - add a new device configuration (init)'
  echo 'Delete  - delete a device configuration (uninit)'
  echo 'Reset   - reset a single device by stopping and starting it'
  #echo 'Move    - change IP address for a device without losing configuration'
  echo "Show    - display set configuration ${DGRP_STORE}"
  echo "Kernel  - display active configuration ${DGRP_PROCCONFIG}"
  echo "Node    - display active nodes ${g_procdir}/nodeinfo"
  echo "Version - display kernel version and debugging info ${g_procdir}/info"
  echo ''
fi

check_kernel

echo "Service at boot: $(systemctl is-enabled 'dgrp_daemon.service')"
echo "Service status: $(systemctl is-active 'dgrp_daemon.service')"

if [ -d "${g_procdir}" ]; then
  echo 'Kernel status: running'
  lines="$(grep "^[^#].*" "${g_procdir}/config" | wc -l | cut -d ' ' -f 1)"
  if [ "${lines}" -eq 0 ]; then
    if [ "${EUID}" -eq 0 ]; then
      echo "Device count : ${lines}"
      echo 'systemctl start dgrp_daemon.service'
      systemctl start 'dgrp_daemon.service'
      echo 'systemctl start dgrp_ditty.service'
      systemctl start 'dgrp_ditty.service'
      lines="$(grep "^[^#].*" "${g_procdir}/config" | wc -l | cut -d ' ' -f 1)"
    fi
  elif ! pgrep -c 'drpd' > /dev/null; then
    echo 'Daemon status: NOT RUNNING'
    echo 'to fix:'
    echo '  systemctl restart dgrp_daemon.service'
  else
    echo 'Daemon status: running'
  fi
  echo "Device Count : ${lines}"
  unset lines
else
  echo ''
  echo 'Kernel status: NOT RUNNING'
fi

if [ "${EUID}" -ne 0 ]; then
  printf "\nMust be root to use $0\n"
  exit 2
fi

while :; do
  printf '\nOp: (a)dd (d)elete (r)eset (s)how (k)ernel (n)ode (v)ersion (q)uit:'
  read opt
  case "${opt,,}" in
  'a') add_ps ;;
  'd') delete_ps;;
  'r') resetmove_ps 0 'Resetting' 'reset' '';;
  #'m') resetmove_ps 1 'Moving' 'move' 'new';; # Doesn't work according to the instructions
  's') show_ps;;
  'k') show_info;;
  'n') show_node;;
  'v') show_version;;
  'q') echo "man $(basename "${DGRP_CFG}") for advanced configuration options"; exit 0;;
  esac
done