Skip to content

Instantly share code, notes, and snippets.

@goertzenator
Created March 4, 2014 14:35
Show Gist options
  • Save goertzenator/9347573 to your computer and use it in GitHub Desktop.
Save goertzenator/9347573 to your computer and use it in GitHub Desktop.
snmp_user({create, Record0}) ->
Table = snmp_user,
% merge defaults
Record = maps:merge(#{engine_id=>local}, Record0),
case Record of
#{security_model := SecurityModel,
security_name := SecurityName,
access_type := AccessType}
when is_list(SecurityName),
(AccessType==readonly orelse AccessType==readwrite) ->
map_table:maybe_transaction(
fun() ->
%% make sure SecurityName is not already present in table
case lists:member(SecurityName, map_table:all_keys(Table)) of
true -> erlang:error(badarg, "security name exists");
false -> ok
end,
EngineId = case Record of
#{engine_id := E} when is_list(E)-> E;
#{engine_id := local} -> snmp_agent_controller:get_engine_id()
end,
case Record of
#{security_model:=v2c, community_name:=CommunityName} when is_list(CommunityName) ->
map_table:write(Table, SecurityModel, #{
engine_id => EngineId,
security_model => SecurityModel,
security_name => SecurityName,
community_name => CommunityName,
access_type => AccessType});
#{security_model:=usm, user_name:=UserName} when is_list(UserName) ->
{AuthP, LocalizationHash} = case maps:get(authp, Record) of
md5 -> {usmHMACMD5AuthProtocol, md5};
sha -> {usmHMACSHAAuthProtocol, sha};
usmHMACMD5AuthProtocol -> {usmHMACMD5AuthProtocol, md5};
usmHMACSHAAuthProtocol -> {usmHMACSHAAuthProtocol, sha}
end,
%% Validate auth. Convert password to key if present
AuthKey = case Record of
#{authkey:=AuthK} when is_list(AuthK) -> AuthK;
#{authpassword:=AuthPass} when is_list(AuthPass) ->
snmp:passwd2localized_key(LocalizationHash, AuthPass, EngineId)
end,
PrivP = case maps:get(privp, Record) of
des -> usmDESPrivProtocol;
aes -> usmAesCfb128Protocol;
PP when PP==usmDESPrivProtocol; PP==usmAesCfb128Protocol ->
PP
end,
%% Validate priv. Convert password to key if present
PrivKey = case Record of
#{privkey:=PrivK} when is_list(PrivK) -> PrivK;
#{privpassword:=PrivPass} when is_list(PrivPass) ->
lists:sublist(snmp:passwd2localized_key(LocalizationHash, PrivPass, EngineId), 16)
end,
map_table:write(Table, SecurityModel, #{
security_model => SecurityModel,
user_name => UserName,
security_name => SecurityName,
engine_id => EngineId,
authp => AuthP,
authkey => AuthKey,
privp => PrivP,
privkey => PrivKey,
access_type => AccessType})
end
end)
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment