Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:9370847
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
snmp_user2({create, Record0}) ->
map_table:maybe_transaction(
fun() ->
Record = maps:merge(#{engine_id=>local}, Record0),
snmp_user_create(Record)
end).
 
snmp_user_create(Record=#{ security_model := SecurityModel,
security_name := SecurityName,
access_type := AccessType})
when is_list(SecurityName),
(AccessType==readonly orelse AccessType==readwrite) ->
 
%% make sure SecurityName is not already present in table
case lists:member(SecurityName, map_table:all_keys(snmp_user)) of
true -> erlang:error(badarg, "security name exists");
false -> ok
end,
 
EngineId = engine_id(Record),
 
User = case Record of
#{security_model:=v2c, community_name:=CommunityName} when is_list(CommunityName) ->
#{ engine_id => EngineId,
security_model => SecurityModel,
security_name => SecurityName,
community_name => CommunityName,
access_type => AccessType};
 
#{security_model:=usm, user_name:=UserName, authp:=AuthP, privp:=PrivP} when is_list(UserName) ->
LocalizationHash = localization_hash(AuthP),
#{ security_model => SecurityModel,
user_name => UserName,
security_name => SecurityName,
engine_id => EngineId,
authp => authp(AuthP),
authkey => authkey(Record, LocalizationHash, EngineId),
privp => privp(PrivP),
privkey => privkey(Record, LocalizationHash, EngineId),
access_type => AccessType}
end,
map_table:write(snmp_user, SecurityModel, User).
 
engine_id(#{engine_id := E}) when is_list(E)->
E;
engine_id(#{engine_id := local}) ->
snmp_agent_controller:get_engine_id().
 
localization_hash(md5) -> md5;
localization_hash(sha) -> sha;
localization_hash(usmHMACMD5AuthProtocol) -> md5;
localization_hash(usmHMACSHAAuthProtocol) -> sha
 
authkey(#{authkey:=AuthK}, _, _) when is_list(AuthK) ->
AuthK;
authkey(#{authpassword:=AuthPass}, LocalizationHash, EngineId) when is_list(AuthPass) ->
snmp:passwd2localized_key(LocalizationHash, AuthPass, EngineId).
 
authp(md5) -> usmHMACMD5AuthProtocol;
authp(sha) -> usmHMACSHAAuthProtocol.
 
privp(des) -> usmDESPrivProtocol;
privp(aes) -> usmAesCfb128Protocol;
privp(PP) when PP==usmDESPrivProtocol; PP==usmAesCfb128Protocol ->
PP.
 
privkey(#{privkey:=PrivK}, _, _) when is_list(PrivK) -> PrivK;
privkey(#{privpassword:=PrivPass}, LocalizationHash, EngineId) when is_list(PrivPass) ->
lists:sublist(snmp:passwd2localized_key(LocalizationHash, PrivPass, EngineId), 16).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.