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
|
#
# This file is part of pysnmp-apps software.
#
# Copyright (c) 2005-2016, Ilya Etingof <ilya@glas.net>
# License: http://pysnmp.sf.net/license.html
#
from pysnmp_apps.cli import base
from pysnmp.entity import config
from pysnmp import error
# Usage
def getUsage():
return "\
SNMPv1/v2c security options:\n\
-c COMMUNITY community name\n\
SNMPv3 security options:\n\
-u SECURITY-NAME USM user security name\n\
-l SECURITY-LEVEL \"noAuthNoPriv\"|\"authNoPriv\"|\"authPriv\"\n\
-a AUTH-PROTOCOL \"MD5\"|\"SHA\"\n\
-A AUTH-KEY user authentication key\n\
-x PRIV-PROTOCOL \"DES\"|\"AES\"\n\
-X PRIV-KEY user privacy key\n\
-E CONTEXT-ENGINE-ID authoritative context engine ID\n\
-e ENGINE-ID authoritative SNMP engine ID (will discover)\n\
-n CONTEXT-NAME authoritative context name\n\
-Z BOOTS,TIME destination SNMP engine boots/uptime\n\
"
# Scanner
class SMScannerMixIn:
# SNMPv1/v2
def t_community(self, s):
r' -c '
self.rv.append(base.ConfigToken('community'))
# SNMPv3
def t_authProtocol(self, s):
r' -a '
self.rv.append(base.ConfigToken('authProtocol'))
def t_authKey(self, s):
r' -A '
self.rv.append(base.ConfigToken('authKey'))
def t_privProtocol(self, s):
r' -x '
self.rv.append(base.ConfigToken('privProtocol'))
def t_privKey(self, s):
r' -X '
self.rv.append(base.ConfigToken('privKey'))
def t_securityName(self, s):
r' -u '
self.rv.append(base.ConfigToken('securityName'))
def t_securityLevel(self, s):
r' -l '
self.rv.append(base.ConfigToken('securityLevel'))
def t_engineID(self, s):
r' -e '
self.rv.append(base.ConfigToken('engineID'))
def t_contextEngineId(self, s):
r' -E '
self.rv.append(base.ConfigToken('contextEngineId'))
def t_contextName(self, s):
r' -n '
self.rv.append(base.ConfigToken('contextName'))
def t_engineBoots(self, s):
r' -Z '
self.rv.append(base.ConfigToken('engineBoots'))
# Parser
class SMParserMixIn:
def p_smSpec(self, args):
'''
Option ::= SnmpV1Option
Option ::= SnmpV3Option
SnmpV1Option ::= Community
Community ::= community string
Community ::= community whitespace string
SnmpV3Option ::= AuthProtocol
SnmpV3Option ::= AuthKey
SnmpV3Option ::= PrivProtocol
SnmpV3Option ::= PrivKey
SnmpV3Option ::= SecurityName
SnmpV3Option ::= SecurityLevel
SnmpV3Option ::= EngineID
SnmpV3Option ::= ContextEngineId
SnmpV3Option ::= ContextName
SnmpV3Option ::= EngineBoots
AuthProtocol ::= authProtocol string
AuthProtocol ::= authProtocol whitespace string
AuthKey ::= authKey string
AuthKey ::= authKey whitespace string
PrivProtocol ::= privProtocol string
PrivProtocol ::= privProtocol whitespace string
PrivKey ::= privKey string
PrivKey ::= privKey whitespace string
SecurityName ::= securityName string
SecurityName ::= securityName whitespace string
SecurityLevel ::= securityLevel string
SecurityLevel ::= securityLevel whitespace string
EngineID ::= engineID string
EngineID ::= engineID whitespace string
ContextEngineId ::= contextEngineId string
ContextEngineId ::= contextEngineId whitespace string
ContextName ::= contextName string
ContextName ::= contextName whitespace string
EngineBoots ::= engineBoots string
EngineBoots ::= engineBoots whitespace string
'''
# Generator
class __SMGenerator(base.GeneratorTemplate):
# SNMPv1/v2
def n_Community(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['communityName'] = node[2].attr
else:
ctx['communityName'] = node[1].attr
# SNMPv3
def n_AuthProtocol(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
p = node[2].attr.upper()
else:
p = node[1].attr.upper()
if p.find('MD5') != -1:
ctx['authProtocol'] = config.usmHMACMD5AuthProtocol
elif p.find('SHA') != -1:
ctx['authProtocol'] = config.usmHMACSHAAuthProtocol
else:
raise error.PySnmpError('Unknown auth protocol \"%s\"' % p)
def n_AuthKey(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['authKey'] = node[2].attr
else:
ctx['authKey'] = node[1].attr
def n_PrivProtocol(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
p = node[2].attr.upper()
else:
p = node[1].attr.upper()
if p.find('DES') != -1:
ctx['privProtocol'] = config.usmDESPrivProtocol
elif p.find('AES') != -1:
ctx['privProtocol'] = config.usmAesCfb128Protocol
else:
raise error.PySnmpError('Unknown priv protocol \"%s\"' % p)
def n_PrivKey(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['privKey'] = node[2].attr
else:
ctx['privKey'] = node[1].attr
def n_SecurityName(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['securityName'] = node[2].attr
else:
ctx['securityName'] = node[1].attr
def n_SecurityLevel(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['securityLevel'] = node[2].attr
else:
ctx['securityLevel'] = node[1].attr
def n_EngineID(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['engineID'] = node[2].attr
else:
ctx['engineID'] = node[1].attr
def n_ContextEngineId(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['contextEngineId'] = node[2].attr
else:
ctx['contextEngineId'] = node[1].attr
def n_ContextName(self, cbCtx, node):
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['contextName'] = node[2].attr
else:
ctx['contextName'] = node[1].attr
def n_EngineBoots(self, cbCtx, node): # XXX
snmpEngine, ctx = cbCtx
if len(node) > 2:
ctx['engineBoots'] = node[2].attr
else:
ctx['engineBoots'] = node[1].attr
if ',' in ctx['engineBoots']:
ctx['engineBoots'], ctx['engineTime'] = ctx['engineBoots'].split(',', 1)
else:
ctx['engineTime'] = 0
def generator(cbCtx, ast):
snmpEngine, ctx = cbCtx
__SMGenerator().preorder(cbCtx, ast)
# Commit collected data
if ctx['versionId'] == 3:
if 'securityName' not in ctx:
raise error.PySnmpError('Security name not specified')
if 'securityLevel' not in ctx:
raise error.PySnmpError('Security level not specified')
if ctx['securityLevel'] == 'noAuthNoPriv':
if 'authKey' in ctx: del ctx['authKey']
if 'privKey' in ctx: del ctx['privKey']
elif ctx['securityLevel'] == 'authNoPriv':
if 'privKey' in ctx: del ctx['privKey']
if 'authKey' in ctx:
if 'authProtocol' not in ctx:
ctx['authProtocol'] = config.usmHMACMD5AuthProtocol
else:
ctx['authProtocol'] = config.usmNoAuthProtocol
ctx['authKey'] = None
if 'privKey' in ctx:
if 'privProtocol' not in ctx:
ctx['privProtocol'] = config.usmDESPrivProtocol
else:
ctx['privProtocol'] = config.usmNoPrivProtocol
ctx['privKey'] = None
config.addV3User(
snmpEngine,
ctx['securityName'],
ctx['authProtocol'],
ctx['authKey'],
ctx['privProtocol'],
ctx['privKey']
)
# edit SNMP engine boots/uptime
if 'engineBoots' in ctx:
snmpEngineBoots, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineBoots')
snmpEngineBoots.setSyntax(
snmpEngineBoots.getSyntax().clone(ctx['engineBoots'])
)
if 'engineTime' in ctx:
snmpEngineTime, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineTime')
snmpEngineTime.setSyntax(
snmpEngineTime.getSyntax().clone(ctx['engineTime'])
)
else: # SNMPv1/v2c
if 'communityName' not in ctx:
raise error.PySnmpError('Community name not specified')
ctx['securityName'] = 'my-agent'
ctx['securityLevel'] = 'noAuthNoPriv'
config.addV1System(
snmpEngine,
ctx['securityName'],
ctx['communityName']
)
ctx['paramsName'] = '%s-params' % ctx['securityName']
config.addTargetParams(
snmpEngine, ctx['paramsName'],ctx['securityName'],
ctx['securityLevel'], ctx['versionId']
)
|