| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# this shell script is designed to add new SNMPv3 users |
| 4 |
# to Net-SNMP config file. |
| 5 |
|
| 6 |
if @PSCMD@ | @EGREP@ ' snmpd *$' > /dev/null 2>&1 ; then |
| 7 |
echo "Apparently at least one snmpd daemon is already running." |
| 8 |
echo "You must stop them in order to use this command." |
| 9 |
exit 1 |
| 10 |
fi |
| 11 |
|
| 12 |
Aalgorithm="MD5" |
| 13 |
Xalgorithm="DES" |
| 14 |
token=rwuser |
| 15 |
|
| 16 |
while test "x$done" = "x" -a "x$1" != "x" -a "x$usage" != "xyes"; do |
| 17 |
|
| 18 |
unset shifted |
| 19 |
case $1 in |
| 20 |
--version|--ver*) |
| 21 |
echo @VERSION@ |
| 22 |
exit 0 |
| 23 |
;; |
| 24 |
--help) |
| 25 |
usage="yes" |
| 26 |
;; |
| 27 |
|
| 28 |
-A|-a) |
| 29 |
shift |
| 30 |
if test "x$1" = "x" ; then |
| 31 |
echo "You must specify an authentication algorithm or pass phrase" |
| 32 |
exit 1 |
| 33 |
fi |
| 34 |
case $1 in |
| 35 |
MD5|SHA|SHA-512|SHA-384|SHA-256|SHA-224) |
| 36 |
Aalgorithm=$1 |
| 37 |
shift |
| 38 |
;; |
| 39 |
md5|sha) |
| 40 |
Aalgorithm=$(echo "$1" | tr a-z A-Z) |
| 41 |
shift |
| 42 |
;; |
| 43 |
*) |
| 44 |
apassphrase=$1 |
| 45 |
shift |
| 46 |
;; |
| 47 |
esac |
| 48 |
;; |
| 49 |
-X|-x) |
| 50 |
shift |
| 51 |
if test "x$1" = "x" ; then |
| 52 |
echo "You must specify an encryption algorithm or pass phrase" |
| 53 |
exit 1 |
| 54 |
fi |
| 55 |
case $1 in |
| 56 |
DES|AES|AES128) |
| 57 |
Xalgorithm=$1 |
| 58 |
shift |
| 59 |
;; |
| 60 |
des|aes|aes128) |
| 61 |
Xalgorithm=$(echo "$1" | tr a-z A-Z) |
| 62 |
shift |
| 63 |
;; |
| 64 |
*) |
| 65 |
xpassphrase=$1 |
| 66 |
shift |
| 67 |
;; |
| 68 |
esac |
| 69 |
;; |
| 70 |
-ro) |
| 71 |
token="rouser" |
| 72 |
shift |
| 73 |
;; |
| 74 |
-*) |
| 75 |
echo "unknown suboption to $0: $1" |
| 76 |
usage=yes |
| 77 |
done=1 |
| 78 |
;; |
| 79 |
*) |
| 80 |
done=1 |
| 81 |
;; |
| 82 |
esac |
| 83 |
done |
| 84 |
|
| 85 |
if test "x$usage" = "xyes"; then |
| 86 |
echo "" |
| 87 |
echo "Usage:" |
| 88 |
echo " net-snmp-create-v3-user [-ro] [-A authpass] [-X privpass]" |
| 89 |
echo " [-a MD5|SHA|SHA-512|SHA-384|SHA-256|SHA-224] [-x DES|AES|AES128] [username]" |
| 90 |
echo "" |
| 91 |
exit |
| 92 |
fi |
| 93 |
|
| 94 |
if test "x$1" = "x" ; then |
| 95 |
prompt=yes |
| 96 |
echo "Enter a SNMPv3 user name to create: " |
| 97 |
read user |
| 98 |
else |
| 99 |
user=$1 |
| 100 |
shift |
| 101 |
fi |
| 102 |
if test "x$user" = "x" ; then |
| 103 |
echo "You must specify a user name" |
| 104 |
exit 1 |
| 105 |
fi |
| 106 |
if test "x$apassphrase" = "x" ; then |
| 107 |
prompt=yes |
| 108 |
echo "Enter authentication pass-phrase: " |
| 109 |
read apassphrase |
| 110 |
fi |
| 111 |
if test "x$apassphrase" = "x" ; then |
| 112 |
echo "You must specify an authentication pass-phrase" |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
if test "x$prompt" = "xyes" -a "x$xpassphrase" = "x" ; then |
| 116 |
echo "Enter encryption pass-phrase: " |
| 117 |
echo " [press return to reuse the authentication pass-phrase]" |
| 118 |
read xpassphrase |
| 119 |
fi |
| 120 |
outdir="@PERSISTENT_DIRECTORY@" |
| 121 |
outfile="$outdir/snmpd.conf" |
| 122 |
if test "x$xpassphrase" = "x" ; then |
| 123 |
line="createUser $user $Aalgorithm \"$apassphrase\" $Xalgorithm" |
| 124 |
else |
| 125 |
line="createUser $user $Aalgorithm \"$apassphrase\" $Xalgorithm \"$xpassphrase\"" |
| 126 |
fi |
| 127 |
echo "adding the following line to $outfile:" |
| 128 |
echo " $line" |
| 129 |
# in case it hasn't ever been started yet, start it. |
| 130 |
if test ! -d "$outdir"; then |
| 131 |
mkdir "$outdir" |
| 132 |
fi |
| 133 |
if test ! -d "$outfile"; then |
| 134 |
touch "$outfile" |
| 135 |
fi |
| 136 |
echo "$line" >> "$outfile" |
| 137 |
prefix=@prefix@ |
| 138 |
datarootdir=@datarootdir@ |
| 139 |
# To suppress shellcheck complaints about $prefix and $datarootdir. |
| 140 |
: "$prefix" "$datarootdir" |
| 141 |
outfile="@datadir@/snmp/snmpd.conf" |
| 142 |
line="$token $user" |
| 143 |
echo "adding the following line to $outfile:" |
| 144 |
echo " $line" |
| 145 |
if test ! -d "$outfile"; then |
| 146 |
touch "$outfile" |
| 147 |
fi |
| 148 |
echo "$line" >> $outfile |