net-snmp.js 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452
  1. // Copyright 2013 Stephen Vickers <stephen.vickers.sv@gmail.com>
  2. // SPDX-License-Identifier: MIT
  3. var ber = require("asn1-ber").Ber;
  4. var dgram = require("dgram");
  5. var events = require("events");
  6. var util = require("util");
  7. var crypto = require("crypto");
  8. var DEBUG = false;
  9. var MAX_INT32 = 2147483647;
  10. function debug(line) {
  11. if (DEBUG) {
  12. console.debug(line);
  13. }
  14. }
  15. /*****************************************************************************
  16. ** Constants
  17. **/
  18. function _expandConstantObject(object) {
  19. var keys = [];
  20. for (var key in object)
  21. keys.push(key);
  22. for (var i = 0; i < keys.length; i++)
  23. object[object[keys[i]]] = parseInt(keys[i]);
  24. }
  25. var ErrorStatus = {
  26. 0: "NoError",
  27. 1: "TooBig",
  28. 2: "NoSuchName",
  29. 3: "BadValue",
  30. 4: "ReadOnly",
  31. 5: "GeneralError",
  32. 6: "NoAccess",
  33. 7: "WrongType",
  34. 8: "WrongLength",
  35. 9: "WrongEncoding",
  36. 10: "WrongValue",
  37. 11: "NoCreation",
  38. 12: "InconsistentValue",
  39. 13: "ResourceUnavailable",
  40. 14: "CommitFailed",
  41. 15: "UndoFailed",
  42. 16: "AuthorizationError",
  43. 17: "NotWritable",
  44. 18: "InconsistentName"
  45. };
  46. _expandConstantObject(ErrorStatus);
  47. var ObjectType = {
  48. 1: "Boolean",
  49. 2: "Integer",
  50. 4: "OctetString",
  51. 5: "Null",
  52. 6: "OID",
  53. 64: "IpAddress",
  54. 65: "Counter",
  55. 66: "Gauge",
  56. 67: "TimeTicks",
  57. 68: "Opaque",
  58. 70: "Counter64",
  59. 128: "NoSuchObject",
  60. 129: "NoSuchInstance",
  61. 130: "EndOfMibView"
  62. };
  63. _expandConstantObject(ObjectType);
  64. ObjectType.Integer32 = ObjectType.Integer;
  65. ObjectType.Counter32 = ObjectType.Counter;
  66. ObjectType.Gauge32 = ObjectType.Gauge;
  67. ObjectType.Unsigned32 = ObjectType.Gauge32;
  68. var PduType = {
  69. 160: "GetRequest",
  70. 161: "GetNextRequest",
  71. 162: "GetResponse",
  72. 163: "SetRequest",
  73. 164: "Trap",
  74. 165: "GetBulkRequest",
  75. 166: "InformRequest",
  76. 167: "TrapV2",
  77. 168: "Report"
  78. };
  79. _expandConstantObject(PduType);
  80. var TrapType = {
  81. 0: "ColdStart",
  82. 1: "WarmStart",
  83. 2: "LinkDown",
  84. 3: "LinkUp",
  85. 4: "AuthenticationFailure",
  86. 5: "EgpNeighborLoss",
  87. 6: "EnterpriseSpecific"
  88. };
  89. _expandConstantObject(TrapType);
  90. var SecurityLevel = {
  91. 1: "noAuthNoPriv",
  92. 2: "authNoPriv",
  93. 3: "authPriv"
  94. };
  95. _expandConstantObject(SecurityLevel);
  96. var AuthProtocols = {
  97. "1": "none",
  98. "2": "md5",
  99. "3": "sha"
  100. };
  101. _expandConstantObject(AuthProtocols);
  102. var PrivProtocols = {
  103. "1": "none",
  104. "2": "des"
  105. };
  106. _expandConstantObject(PrivProtocols);
  107. var MibProviderType = {
  108. "1": "Scalar",
  109. "2": "Table"
  110. };
  111. _expandConstantObject(MibProviderType);
  112. var Version1 = 0;
  113. var Version2c = 1;
  114. var Version3 = 3;
  115. var Version = {
  116. "1": Version1,
  117. "2c": Version2c,
  118. "3": Version3
  119. };
  120. /*****************************************************************************
  121. ** Exception class definitions
  122. **/
  123. function ResponseInvalidError(message) {
  124. this.name = "ResponseInvalidError";
  125. this.message = message;
  126. Error.captureStackTrace(this, ResponseInvalidError);
  127. }
  128. util.inherits(ResponseInvalidError, Error);
  129. function RequestInvalidError(message) {
  130. this.name = "RequestInvalidError";
  131. this.message = message;
  132. Error.captureStackTrace(this, RequestInvalidError);
  133. }
  134. util.inherits(RequestInvalidError, Error);
  135. function RequestFailedError(message, status) {
  136. this.name = "RequestFailedError";
  137. this.message = message;
  138. this.status = status;
  139. Error.captureStackTrace(this, RequestFailedError);
  140. }
  141. util.inherits(RequestFailedError, Error);
  142. function RequestTimedOutError(message) {
  143. this.name = "RequestTimedOutError";
  144. this.message = message;
  145. Error.captureStackTrace(this, RequestTimedOutError);
  146. }
  147. util.inherits(RequestTimedOutError, Error);
  148. /*****************************************************************************
  149. ** OID and varbind helper functions
  150. **/
  151. function isVarbindError(varbind) {
  152. return !!(varbind.type == ObjectType.NoSuchObject
  153. || varbind.type == ObjectType.NoSuchInstance
  154. || varbind.type == ObjectType.EndOfMibView);
  155. }
  156. function varbindError(varbind) {
  157. return (ObjectType[varbind.type] || "NotAnError") + ": " + varbind.oid;
  158. }
  159. function oidFollowsOid(oidString, nextString) {
  160. var oid = {str: oidString, len: oidString.length, idx: 0};
  161. var next = {str: nextString, len: nextString.length, idx: 0};
  162. var dotCharCode = ".".charCodeAt(0);
  163. function getNumber(item) {
  164. var n = 0;
  165. if (item.idx >= item.len)
  166. return null;
  167. while (item.idx < item.len) {
  168. var charCode = item.str.charCodeAt(item.idx++);
  169. if (charCode == dotCharCode)
  170. return n;
  171. n = (n ? (n * 10) : n) + (charCode - 48);
  172. }
  173. return n;
  174. }
  175. while (1) {
  176. var oidNumber = getNumber(oid);
  177. var nextNumber = getNumber(next);
  178. if (oidNumber !== null) {
  179. if (nextNumber !== null) {
  180. if (nextNumber > oidNumber) {
  181. return true;
  182. } else if (nextNumber < oidNumber) {
  183. return false;
  184. }
  185. } else {
  186. return true;
  187. }
  188. } else {
  189. return true;
  190. }
  191. }
  192. }
  193. function oidInSubtree(oidString, nextString) {
  194. var oid = oidString.split(".");
  195. var next = nextString.split(".");
  196. if (oid.length > next.length)
  197. return false;
  198. for (var i = 0; i < oid.length; i++) {
  199. if (next[i] != oid[i])
  200. return false;
  201. }
  202. return true;
  203. }
  204. /**
  205. ** Some SNMP agents produce integers on the wire such as 00 ff ff ff ff.
  206. ** The ASN.1 BER parser we use throws an error when parsing this, which we
  207. ** believe is correct. So, we decided not to bother the "asn1" developer(s)
  208. ** with this, instead opting to work around it here.
  209. **
  210. ** If an integer is 5 bytes in length we check if the first byte is 0, and if so
  211. ** simply drop it and parse it like it was a 4 byte integer, otherwise throw
  212. ** an error since the integer is too large.
  213. **/
  214. function readInt(buffer) {
  215. return readUint(buffer, true);
  216. }
  217. function readIpAddress(buffer) {
  218. var bytes = buffer.readString(ObjectType.IpAddress, true);
  219. if (bytes.length != 4)
  220. throw new ResponseInvalidError("Length '" + bytes.length
  221. + "' of IP address '" + bytes.toString("hex")
  222. + "' is not 4");
  223. var value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3];
  224. return value;
  225. }
  226. function readUint(buffer, isSigned) {
  227. buffer.readByte();
  228. var length = buffer.readByte();
  229. var value = 0;
  230. var signedBitSet = false;
  231. if (length > 5) {
  232. throw new RangeError("Integer too long '" + length + "'");
  233. } else if (length == 5) {
  234. if (buffer.readByte() !== 0)
  235. throw new RangeError("Integer too long '" + length + "'");
  236. length = 4;
  237. }
  238. for (var i = 0; i < length; i++) {
  239. value *= 256;
  240. value += buffer.readByte();
  241. if (isSigned && i <= 0) {
  242. if ((value & 0x80) == 0x80)
  243. signedBitSet = true;
  244. }
  245. }
  246. if (signedBitSet)
  247. value -= (1 << (i * 8));
  248. return value;
  249. }
  250. function readUint64(buffer) {
  251. var value = buffer.readString(ObjectType.Counter64, true);
  252. return value;
  253. }
  254. function readVarbinds(buffer, varbinds) {
  255. buffer.readSequence();
  256. while (1) {
  257. buffer.readSequence();
  258. if (buffer.peek() != ObjectType.OID)
  259. break;
  260. var oid = buffer.readOID();
  261. var type = buffer.peek();
  262. if (type == null)
  263. break;
  264. var value;
  265. if (type == ObjectType.Boolean) {
  266. value = buffer.readBoolean();
  267. } else if (type == ObjectType.Integer) {
  268. value = readInt(buffer);
  269. } else if (type == ObjectType.OctetString) {
  270. value = buffer.readString(null, true);
  271. } else if (type == ObjectType.Null) {
  272. buffer.readByte();
  273. buffer.readByte();
  274. value = null;
  275. } else if (type == ObjectType.OID) {
  276. value = buffer.readOID();
  277. } else if (type == ObjectType.IpAddress) {
  278. var bytes = buffer.readString(ObjectType.IpAddress, true);
  279. if (bytes.length != 4)
  280. throw new ResponseInvalidError("Length '" + bytes.length
  281. + "' of IP address '" + bytes.toString("hex")
  282. + "' is not 4");
  283. value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3];
  284. } else if (type == ObjectType.Counter) {
  285. value = readUint(buffer);
  286. } else if (type == ObjectType.Gauge) {
  287. value = readUint(buffer);
  288. } else if (type == ObjectType.TimeTicks) {
  289. value = readUint(buffer);
  290. } else if (type == ObjectType.Opaque) {
  291. value = buffer.readString(ObjectType.Opaque, true);
  292. } else if (type == ObjectType.Counter64) {
  293. value = readUint64(buffer);
  294. } else if (type == ObjectType.NoSuchObject) {
  295. buffer.readByte();
  296. buffer.readByte();
  297. value = null;
  298. } else if (type == ObjectType.NoSuchInstance) {
  299. buffer.readByte();
  300. buffer.readByte();
  301. value = null;
  302. } else if (type == ObjectType.EndOfMibView) {
  303. buffer.readByte();
  304. buffer.readByte();
  305. value = null;
  306. } else {
  307. throw new ResponseInvalidError("Unknown type '" + type
  308. + "' in response");
  309. }
  310. varbinds.push({
  311. oid: oid,
  312. type: type,
  313. value: value
  314. });
  315. }
  316. }
  317. function writeUint(buffer, type, value) {
  318. var b = Buffer.alloc(4);
  319. b.writeUInt32BE(value, 0);
  320. buffer.writeBuffer(b, type);
  321. }
  322. function writeUint64(buffer, value) {
  323. buffer.writeBuffer(value, ObjectType.Counter64);
  324. }
  325. function writeVarbinds(buffer, varbinds) {
  326. buffer.startSequence();
  327. for (var i = 0; i < varbinds.length; i++) {
  328. buffer.startSequence();
  329. buffer.writeOID(varbinds[i].oid);
  330. if (varbinds[i].type && varbinds[i].hasOwnProperty("value")) {
  331. var type = varbinds[i].type;
  332. var value = varbinds[i].value;
  333. if (type == ObjectType.Boolean) {
  334. buffer.writeBoolean(value ? true : false);
  335. } else if (type == ObjectType.Integer) { // also Integer32
  336. buffer.writeInt(value);
  337. } else if (type == ObjectType.OctetString) {
  338. if (typeof value == "string")
  339. buffer.writeString(value);
  340. else
  341. buffer.writeBuffer(value, ObjectType.OctetString);
  342. } else if (type == ObjectType.Null) {
  343. buffer.writeNull();
  344. } else if (type == ObjectType.OID) {
  345. buffer.writeOID(value);
  346. } else if (type == ObjectType.IpAddress) {
  347. var bytes = value.split(".");
  348. if (bytes.length != 4)
  349. throw new RequestInvalidError("Invalid IP address '"
  350. + value + "'");
  351. buffer.writeBuffer(Buffer.from(bytes), 64);
  352. } else if (type == ObjectType.Counter) { // also Counter32
  353. writeUint(buffer, ObjectType.Counter, value);
  354. } else if (type == ObjectType.Gauge) { // also Gauge32 & Unsigned32
  355. writeUint(buffer, ObjectType.Gauge, value);
  356. } else if (type == ObjectType.TimeTicks) {
  357. writeUint(buffer, ObjectType.TimeTicks, value);
  358. } else if (type == ObjectType.Opaque) {
  359. buffer.writeBuffer(value, ObjectType.Opaque);
  360. } else if (type == ObjectType.Counter64) {
  361. writeUint64(buffer, value);
  362. } else if (type == ObjectType.EndOfMibView) {
  363. buffer.writeByte(130);
  364. buffer.writeByte(0);
  365. } else {
  366. throw new RequestInvalidError("Unknown type '" + type
  367. + "' in request");
  368. }
  369. } else {
  370. buffer.writeNull();
  371. }
  372. buffer.endSequence();
  373. }
  374. buffer.endSequence();
  375. }
  376. /*****************************************************************************
  377. ** PDU class definitions
  378. **/
  379. var SimplePdu = function () {
  380. };
  381. SimplePdu.prototype.toBuffer = function (buffer) {
  382. buffer.startSequence(this.type);
  383. buffer.writeInt(this.id);
  384. buffer.writeInt((this.type == PduType.GetBulkRequest)
  385. ? (this.options.nonRepeaters || 0)
  386. : 0);
  387. buffer.writeInt((this.type == PduType.GetBulkRequest)
  388. ? (this.options.maxRepetitions || 0)
  389. : 0);
  390. writeVarbinds(buffer, this.varbinds);
  391. buffer.endSequence();
  392. };
  393. SimplePdu.prototype.initializeFromVariables = function (id, varbinds, options) {
  394. this.id = id;
  395. this.varbinds = varbinds;
  396. this.options = options || {};
  397. this.contextName = (options && options.context) ? options.context : "";
  398. }
  399. SimplePdu.prototype.initializeFromBuffer = function (reader) {
  400. this.type = reader.peek();
  401. reader.readSequence();
  402. this.id = reader.readInt();
  403. this.nonRepeaters = reader.readInt();
  404. this.maxRepetitions = reader.readInt();
  405. this.varbinds = [];
  406. readVarbinds(reader, this.varbinds);
  407. };
  408. SimplePdu.prototype.getResponsePduForRequest = function () {
  409. var responsePdu = GetResponsePdu.createFromVariables(this.id, [], {});
  410. if (this.contextEngineID) {
  411. responsePdu.contextEngineID = this.contextEngineID;
  412. responsePdu.contextName = this.contextName;
  413. }
  414. return responsePdu;
  415. };
  416. SimplePdu.createFromVariables = function (pduClass, id, varbinds, options) {
  417. var pdu = new pduClass(id, varbinds, options);
  418. pdu.id = id;
  419. pdu.varbinds = varbinds;
  420. pdu.options = options || {};
  421. pdu.contextName = (options && options.context) ? options.context : "";
  422. return pdu;
  423. };
  424. var GetBulkRequestPdu = function () {
  425. this.type = PduType.GetBulkRequest;
  426. GetBulkRequestPdu.super_.apply(this, arguments);
  427. };
  428. util.inherits(GetBulkRequestPdu, SimplePdu);
  429. GetBulkRequestPdu.createFromBuffer = function (reader) {
  430. var pdu = new GetBulkRequestPdu();
  431. pdu.initializeFromBuffer(reader);
  432. return pdu;
  433. };
  434. var GetNextRequestPdu = function () {
  435. this.type = PduType.GetNextRequest;
  436. GetNextRequestPdu.super_.apply(this, arguments);
  437. };
  438. util.inherits(GetNextRequestPdu, SimplePdu);
  439. GetNextRequestPdu.createFromBuffer = function (reader) {
  440. var pdu = new GetNextRequestPdu();
  441. pdu.initializeFromBuffer(reader);
  442. return pdu;
  443. };
  444. var GetRequestPdu = function () {
  445. this.type = PduType.GetRequest;
  446. GetRequestPdu.super_.apply(this, arguments);
  447. };
  448. util.inherits(GetRequestPdu, SimplePdu);
  449. GetRequestPdu.createFromBuffer = function (reader) {
  450. var pdu = new GetRequestPdu();
  451. pdu.initializeFromBuffer(reader);
  452. return pdu;
  453. };
  454. GetRequestPdu.createFromVariables = function (id, varbinds, options) {
  455. var pdu = new GetRequestPdu();
  456. pdu.initializeFromVariables(id, varbinds, options);
  457. return pdu;
  458. };
  459. var InformRequestPdu = function () {
  460. this.type = PduType.InformRequest;
  461. InformRequestPdu.super_.apply(this, arguments);
  462. };
  463. util.inherits(InformRequestPdu, SimplePdu);
  464. InformRequestPdu.createFromBuffer = function (reader) {
  465. var pdu = new InformRequestPdu();
  466. pdu.initializeFromBuffer(reader);
  467. return pdu;
  468. };
  469. var SetRequestPdu = function () {
  470. this.type = PduType.SetRequest;
  471. SetRequestPdu.super_.apply(this, arguments);
  472. };
  473. util.inherits(SetRequestPdu, SimplePdu);
  474. SetRequestPdu.createFromBuffer = function (reader) {
  475. var pdu = new SetRequestPdu();
  476. pdu.initializeFromBuffer(reader);
  477. return pdu;
  478. };
  479. var TrapPdu = function () {
  480. this.type = PduType.Trap;
  481. };
  482. TrapPdu.prototype.toBuffer = function (buffer) {
  483. buffer.startSequence(this.type);
  484. buffer.writeOID(this.enterprise);
  485. buffer.writeBuffer(Buffer.from(this.agentAddr.split(".")),
  486. ObjectType.IpAddress);
  487. buffer.writeInt(this.generic);
  488. buffer.writeInt(this.specific);
  489. writeUint(buffer, ObjectType.TimeTicks,
  490. this.upTime || Math.floor(process.uptime() * 100));
  491. writeVarbinds(buffer, this.varbinds);
  492. buffer.endSequence();
  493. };
  494. TrapPdu.createFromBuffer = function (reader) {
  495. var pdu = new TrapPdu();
  496. reader.readSequence();
  497. pdu.enterprise = reader.readOID();
  498. pdu.agentAddr = readIpAddress(reader);
  499. pdu.generic = reader.readInt();
  500. pdu.specific = reader.readInt();
  501. pdu.upTime = readUint(reader)
  502. pdu.varbinds = [];
  503. readVarbinds(reader, pdu.varbinds);
  504. return pdu;
  505. };
  506. TrapPdu.createFromVariables = function (typeOrOid, varbinds, options) {
  507. var pdu = new TrapPdu();
  508. pdu.agentAddr = options.agentAddr || "127.0.0.1";
  509. pdu.upTime = options.upTime;
  510. if (typeof typeOrOid == "string") {
  511. pdu.generic = TrapType.EnterpriseSpecific;
  512. pdu.specific = parseInt(typeOrOid.match(/\.(\d+)$/)[1]);
  513. pdu.enterprise = typeOrOid.replace(/\.(\d+)$/, "");
  514. } else {
  515. pdu.generic = typeOrOid;
  516. pdu.specific = 0;
  517. pdu.enterprise = "1.3.6.1.4.1";
  518. }
  519. pdu.varbinds = varbinds;
  520. return pdu;
  521. };
  522. var TrapV2Pdu = function () {
  523. this.type = PduType.TrapV2;
  524. TrapV2Pdu.super_.apply(this, arguments);
  525. };
  526. util.inherits(TrapV2Pdu, SimplePdu);
  527. TrapV2Pdu.createFromBuffer = function (reader) {
  528. var pdu = new TrapV2Pdu();
  529. pdu.initializeFromBuffer(reader);
  530. return pdu;
  531. };
  532. TrapV2Pdu.createFromVariables = function (id, varbinds, options) {
  533. var pdu = new TrapV2Pdu();
  534. pdu.initializeFromVariables(id, varbinds, options);
  535. return pdu;
  536. };
  537. var SimpleResponsePdu = function () {
  538. };
  539. SimpleResponsePdu.prototype.toBuffer = function (writer) {
  540. writer.startSequence(this.type);
  541. writer.writeInt(this.id);
  542. writer.writeInt(this.errorStatus || 0);
  543. writer.writeInt(this.errorIndex || 0);
  544. writeVarbinds(writer, this.varbinds);
  545. writer.endSequence();
  546. };
  547. SimpleResponsePdu.prototype.initializeFromBuffer = function (reader) {
  548. reader.readSequence(this.type);
  549. this.id = reader.readInt();
  550. this.errorStatus = reader.readInt();
  551. this.errorIndex = reader.readInt();
  552. this.varbinds = [];
  553. readVarbinds(reader, this.varbinds);
  554. };
  555. SimpleResponsePdu.prototype.initializeFromVariables = function (id, varbinds, options) {
  556. this.id = id;
  557. this.varbinds = varbinds;
  558. this.options = options || {};
  559. };
  560. var GetResponsePdu = function () {
  561. this.type = PduType.GetResponse;
  562. GetResponsePdu.super_.apply(this, arguments);
  563. };
  564. util.inherits(GetResponsePdu, SimpleResponsePdu);
  565. GetResponsePdu.createFromBuffer = function (reader) {
  566. var pdu = new GetResponsePdu();
  567. pdu.initializeFromBuffer(reader);
  568. return pdu;
  569. };
  570. GetResponsePdu.createFromVariables = function (id, varbinds, options) {
  571. var pdu = new GetResponsePdu();
  572. pdu.initializeFromVariables(id, varbinds, options);
  573. return pdu;
  574. };
  575. var ReportPdu = function () {
  576. this.type = PduType.Report;
  577. ReportPdu.super_.apply(this, arguments);
  578. };
  579. util.inherits(ReportPdu, SimpleResponsePdu);
  580. ReportPdu.createFromBuffer = function (reader) {
  581. var pdu = new ReportPdu();
  582. pdu.initializeFromBuffer(reader);
  583. return pdu;
  584. };
  585. ReportPdu.createFromVariables = function (id, varbinds, options) {
  586. var pdu = new ReportPdu();
  587. pdu.initializeFromVariables(id, varbinds, options);
  588. return pdu;
  589. };
  590. var readPdu = function (reader, scoped) {
  591. var pdu;
  592. var contextEngineID;
  593. var contextName;
  594. if (scoped) {
  595. reader.readSequence();
  596. contextEngineID = reader.readString(ber.OctetString, true);
  597. contextName = reader.readString();
  598. }
  599. var type = reader.peek();
  600. if (type == PduType.GetResponse) {
  601. pdu = GetResponsePdu.createFromBuffer(reader);
  602. } else if (type == PduType.Report) {
  603. pdu = ReportPdu.createFromBuffer(reader);
  604. } else if (type == PduType.Trap) {
  605. pdu = TrapPdu.createFromBuffer(reader);
  606. } else if (type == PduType.TrapV2) {
  607. pdu = TrapV2Pdu.createFromBuffer(reader);
  608. } else if (type == PduType.InformRequest) {
  609. pdu = InformRequestPdu.createFromBuffer(reader);
  610. } else if (type == PduType.GetRequest) {
  611. pdu = GetRequestPdu.createFromBuffer(reader);
  612. } else if (type == PduType.SetRequest) {
  613. pdu = SetRequestPdu.createFromBuffer(reader);
  614. } else if (type == PduType.GetNextRequest) {
  615. pdu = GetNextRequestPdu.createFromBuffer(reader);
  616. } else if (type == PduType.GetBulkRequest) {
  617. pdu = GetBulkRequestPdu.createFromBuffer(reader);
  618. } else {
  619. throw new ResponseInvalidError("Unknown PDU type '" + type
  620. + "' in response");
  621. }
  622. if (scoped) {
  623. pdu.contextEngineID = contextEngineID;
  624. pdu.contextName = contextName;
  625. }
  626. pdu.scoped = scoped;
  627. return pdu;
  628. };
  629. var createDiscoveryPdu = function (context) {
  630. return GetRequestPdu.createFromVariables(_generateId(), [], {context: context});
  631. };
  632. var Authentication = {};
  633. Authentication.HMAC_BUFFER_SIZE = 1024 * 1024;
  634. Authentication.HMAC_BLOCK_SIZE = 64;
  635. Authentication.AUTHENTICATION_CODE_LENGTH = 12;
  636. Authentication.AUTH_PARAMETERS_PLACEHOLDER = Buffer.from('8182838485868788898a8b8c', 'hex');
  637. Authentication.algorithms = {};
  638. Authentication.algorithms[AuthProtocols.md5] = {
  639. // KEY_LENGTH: 16,
  640. CRYPTO_ALGORITHM: 'md5'
  641. };
  642. Authentication.algorithms[AuthProtocols.sha] = {
  643. // KEY_LENGTH: 20,
  644. CRYPTO_ALGORITHM: 'sha1'
  645. };
  646. // Adapted from RFC3414 Appendix A.2.1. Password to Key Sample Code for MD5
  647. Authentication.passwordToKey = function (authProtocol, authPasswordString, engineID) {
  648. var hashAlgorithm;
  649. var firstDigest;
  650. var finalDigest;
  651. var buf = Buffer.alloc(Authentication.HMAC_BUFFER_SIZE);
  652. var bufOffset = 0;
  653. var passwordIndex = 0;
  654. var count = 0;
  655. var password = Buffer.from(authPasswordString);
  656. var cryptoAlgorithm = Authentication.algorithms[authProtocol].CRYPTO_ALGORITHM;
  657. while (count < Authentication.HMAC_BUFFER_SIZE) {
  658. for (var i = 0; i < Authentication.HMAC_BLOCK_SIZE; i++) {
  659. buf.writeUInt8(password[passwordIndex++ % password.length], bufOffset++);
  660. }
  661. count += Authentication.HMAC_BLOCK_SIZE;
  662. }
  663. hashAlgorithm = crypto.createHash(cryptoAlgorithm);
  664. hashAlgorithm.update(buf);
  665. firstDigest = hashAlgorithm.digest();
  666. // debug ("First digest: " + firstDigest.toString('hex'));
  667. hashAlgorithm = crypto.createHash(cryptoAlgorithm);
  668. hashAlgorithm.update(firstDigest);
  669. hashAlgorithm.update(engineID);
  670. hashAlgorithm.update(firstDigest);
  671. finalDigest = hashAlgorithm.digest();
  672. debug("Localized key: " + finalDigest.toString('hex'));
  673. return finalDigest;
  674. };
  675. Authentication.addParametersToMessageBuffer = function (messageBuffer, authProtocol, authPassword, engineID) {
  676. var authenticationParametersOffset;
  677. var digestToAdd;
  678. // clear the authenticationParameters field in message
  679. authenticationParametersOffset = messageBuffer.indexOf(Authentication.AUTH_PARAMETERS_PLACEHOLDER);
  680. messageBuffer.fill(0, authenticationParametersOffset, authenticationParametersOffset + Authentication.AUTHENTICATION_CODE_LENGTH);
  681. digestToAdd = Authentication.calculateDigest(messageBuffer, authProtocol, authPassword, engineID);
  682. digestToAdd.copy(messageBuffer, authenticationParametersOffset, 0, Authentication.AUTHENTICATION_CODE_LENGTH);
  683. debug("Added Auth Parameters: " + digestToAdd.toString('hex'));
  684. };
  685. Authentication.isAuthentic = function (messageBuffer, authProtocol, authPassword, engineID, digestInMessage) {
  686. var authenticationParametersOffset;
  687. var calculatedDigest;
  688. // clear the authenticationParameters field in message
  689. authenticationParametersOffset = messageBuffer.indexOf(digestInMessage);
  690. messageBuffer.fill(0, authenticationParametersOffset, authenticationParametersOffset + Authentication.AUTHENTICATION_CODE_LENGTH);
  691. calculatedDigest = Authentication.calculateDigest(messageBuffer, authProtocol, authPassword, engineID);
  692. // replace previously cleared authenticationParameters field in message
  693. digestInMessage.copy(messageBuffer, authenticationParametersOffset, 0, Authentication.AUTHENTICATION_CODE_LENGTH);
  694. debug("Digest in message: " + digestInMessage.toString('hex'));
  695. debug("Calculated digest: " + calculatedDigest.toString('hex'));
  696. return calculatedDigest.equals(digestInMessage, Authentication.AUTHENTICATION_CODE_LENGTH);
  697. };
  698. Authentication.calculateDigest = function (messageBuffer, authProtocol, authPassword, engineID) {
  699. var authKey = Authentication.passwordToKey(authProtocol, authPassword, engineID);
  700. // Adapted from RFC3147 Section 6.3.1. Processing an Outgoing Message
  701. var hashAlgorithm;
  702. var kIpad;
  703. var kOpad;
  704. var firstDigest;
  705. var finalDigest;
  706. var truncatedDigest;
  707. var i;
  708. var cryptoAlgorithm = Authentication.algorithms[authProtocol].CRYPTO_ALGORITHM;
  709. if (authKey.length > Authentication.HMAC_BLOCK_SIZE) {
  710. hashAlgorithm = crypto.createHash(cryptoAlgorithm);
  711. hashAlgorithm.update(authKey);
  712. authKey = hashAlgorithm.digest();
  713. }
  714. // MD(K XOR opad, MD(K XOR ipad, msg))
  715. kIpad = Buffer.alloc(Authentication.HMAC_BLOCK_SIZE);
  716. kOpad = Buffer.alloc(Authentication.HMAC_BLOCK_SIZE);
  717. for (i = 0; i < authKey.length; i++) {
  718. kIpad[i] = authKey[i] ^ 0x36;
  719. kOpad[i] = authKey[i] ^ 0x5c;
  720. }
  721. kIpad.fill(0x36, authKey.length);
  722. kOpad.fill(0x5c, authKey.length);
  723. // inner MD
  724. hashAlgorithm = crypto.createHash(cryptoAlgorithm);
  725. hashAlgorithm.update(kIpad);
  726. hashAlgorithm.update(messageBuffer);
  727. firstDigest = hashAlgorithm.digest();
  728. // outer MD
  729. hashAlgorithm = crypto.createHash(cryptoAlgorithm);
  730. hashAlgorithm.update(kOpad);
  731. hashAlgorithm.update(firstDigest);
  732. finalDigest = hashAlgorithm.digest();
  733. truncatedDigest = Buffer.alloc(Authentication.AUTHENTICATION_CODE_LENGTH);
  734. finalDigest.copy(truncatedDigest, 0, 0, Authentication.AUTHENTICATION_CODE_LENGTH);
  735. return truncatedDigest;
  736. };
  737. var Encryption = {};
  738. Encryption.INPUT_KEY_LENGTH = 16;
  739. Encryption.DES_KEY_LENGTH = 8;
  740. Encryption.DES_BLOCK_LENGTH = 8;
  741. Encryption.CRYPTO_DES_ALGORITHM = 'des-cbc';
  742. Encryption.PRIV_PARAMETERS_PLACEHOLDER = Buffer.from('9192939495969798', 'hex');
  743. Encryption.encryptPdu = function (scopedPdu, privProtocol, privPassword, authProtocol, engineID) {
  744. var privLocalizedKey;
  745. var encryptionKey;
  746. var preIv;
  747. var salt;
  748. var iv;
  749. var i;
  750. var paddedScopedPduLength;
  751. var paddedScopedPdu;
  752. var encryptedPdu;
  753. var cbcProtocol = Encryption.CRYPTO_DES_ALGORITHM;
  754. privLocalizedKey = Authentication.passwordToKey(authProtocol, privPassword, engineID);
  755. encryptionKey = Buffer.alloc(Encryption.DES_KEY_LENGTH);
  756. privLocalizedKey.copy(encryptionKey, 0, 0, Encryption.DES_KEY_LENGTH);
  757. preIv = Buffer.alloc(Encryption.DES_BLOCK_LENGTH);
  758. privLocalizedKey.copy(preIv, 0, Encryption.DES_KEY_LENGTH, Encryption.DES_KEY_LENGTH + Encryption.DES_BLOCK_LENGTH);
  759. salt = Buffer.alloc(Encryption.DES_BLOCK_LENGTH);
  760. // set local SNMP engine boots part of salt to 1, as we have no persistent engine state
  761. salt.fill('00000001', 0, 4, 'hex');
  762. // set local integer part of salt to random
  763. salt.fill(crypto.randomBytes(4), 4, 8);
  764. iv = Buffer.alloc(Encryption.DES_BLOCK_LENGTH);
  765. for (i = 0; i < iv.length; i++) {
  766. iv[i] = preIv[i] ^ salt[i];
  767. }
  768. if (scopedPdu.length % Encryption.DES_BLOCK_LENGTH == 0) {
  769. paddedScopedPdu = scopedPdu;
  770. } else {
  771. paddedScopedPduLength = Encryption.DES_BLOCK_LENGTH * (Math.floor(scopedPdu.length / Encryption.DES_BLOCK_LENGTH) + 1);
  772. paddedScopedPdu = Buffer.alloc(paddedScopedPduLength);
  773. scopedPdu.copy(paddedScopedPdu, 0, 0, scopedPdu.length);
  774. }
  775. cipher = crypto.createCipheriv(cbcProtocol, encryptionKey, iv);
  776. encryptedPdu = cipher.update(paddedScopedPdu);
  777. encryptedPdu = Buffer.concat([encryptedPdu, cipher.final()]);
  778. debug("Key: " + encryptionKey.toString('hex'));
  779. debug("IV: " + iv.toString('hex'));
  780. debug("Plain: " + paddedScopedPdu.toString('hex'));
  781. debug("Encrypted: " + encryptedPdu.toString('hex'));
  782. return {
  783. encryptedPdu: encryptedPdu,
  784. msgPrivacyParameters: salt
  785. };
  786. };
  787. Encryption.decryptPdu = function (encryptedPdu, privProtocol, privParameters, privPassword, authProtocol, engineID, forceAutoPaddingDisable) {
  788. var privLocalizedKey;
  789. var decryptionKey;
  790. var preIv;
  791. var salt;
  792. var iv;
  793. var i;
  794. var decryptedPdu;
  795. var cbcProtocol = Encryption.CRYPTO_DES_ALGORITHM;
  796. ;
  797. privLocalizedKey = Authentication.passwordToKey(authProtocol, privPassword, engineID);
  798. decryptionKey = Buffer.alloc(Encryption.DES_KEY_LENGTH);
  799. privLocalizedKey.copy(decryptionKey, 0, 0, Encryption.DES_KEY_LENGTH);
  800. preIv = Buffer.alloc(Encryption.DES_BLOCK_LENGTH);
  801. privLocalizedKey.copy(preIv, 0, Encryption.DES_KEY_LENGTH, Encryption.DES_KEY_LENGTH + Encryption.DES_BLOCK_LENGTH);
  802. salt = privParameters;
  803. iv = Buffer.alloc(Encryption.DES_BLOCK_LENGTH);
  804. for (i = 0; i < iv.length; i++) {
  805. iv[i] = preIv[i] ^ salt[i];
  806. }
  807. decipher = crypto.createDecipheriv(cbcProtocol, decryptionKey, iv);
  808. if (forceAutoPaddingDisable) {
  809. decipher.setAutoPadding(false);
  810. }
  811. decryptedPdu = decipher.update(encryptedPdu);
  812. // This try-catch is a workaround for a seemingly incorrect error condition
  813. // - where sometimes a decrypt error is thrown with decipher.final()
  814. // It replaces this line which should have been sufficient:
  815. // decryptedPdu = Buffer.concat ([decryptedPdu, decipher.final()]);
  816. try {
  817. decryptedPdu = Buffer.concat([decryptedPdu, decipher.final()]);
  818. } catch (error) {
  819. // debug("Decrypt error: " + error);
  820. decipher = crypto.createDecipheriv(cbcProtocol, decryptionKey, iv);
  821. decipher.setAutoPadding(false);
  822. decryptedPdu = decipher.update(encryptedPdu);
  823. decryptedPdu = Buffer.concat([decryptedPdu, decipher.final()]);
  824. }
  825. debug("Key: " + decryptionKey.toString('hex'));
  826. debug("IV: " + iv.toString('hex'));
  827. debug("Encrypted: " + encryptedPdu.toString('hex'));
  828. debug("Plain: " + decryptedPdu.toString('hex'));
  829. return decryptedPdu;
  830. };
  831. Encryption.addParametersToMessageBuffer = function (messageBuffer, msgPrivacyParameters) {
  832. privacyParametersOffset = messageBuffer.indexOf(Encryption.PRIV_PARAMETERS_PLACEHOLDER);
  833. msgPrivacyParameters.copy(messageBuffer, privacyParametersOffset, 0, Encryption.DES_IV_LENGTH);
  834. };
  835. /*****************************************************************************
  836. ** Message class definition
  837. **/
  838. var Message = function () {
  839. }
  840. Message.prototype.getReqId = function () {
  841. return this.version == Version3 ? this.msgGlobalData.msgID : this.pdu.id;
  842. };
  843. Message.prototype.toBuffer = function () {
  844. if (this.version == Version3) {
  845. return this.toBufferV3();
  846. } else {
  847. return this.toBufferCommunity();
  848. }
  849. }
  850. Message.prototype.toBufferCommunity = function () {
  851. if (this.buffer)
  852. return this.buffer;
  853. var writer = new ber.Writer();
  854. writer.startSequence();
  855. writer.writeInt(this.version);
  856. writer.writeString(this.community);
  857. this.pdu.toBuffer(writer);
  858. writer.endSequence();
  859. this.buffer = writer.buffer;
  860. return this.buffer;
  861. };
  862. Message.prototype.toBufferV3 = function () {
  863. var encryptionResult;
  864. if (this.buffer)
  865. return this.buffer;
  866. var writer = new ber.Writer();
  867. writer.startSequence();
  868. writer.writeInt(this.version);
  869. // HeaderData
  870. writer.startSequence();
  871. writer.writeInt(this.msgGlobalData.msgID);
  872. writer.writeInt(this.msgGlobalData.msgMaxSize);
  873. writer.writeByte(ber.OctetString);
  874. writer.writeByte(1);
  875. writer.writeByte(this.msgGlobalData.msgFlags);
  876. writer.writeInt(this.msgGlobalData.msgSecurityModel);
  877. writer.endSequence();
  878. // msgSecurityParameters
  879. var msgSecurityParametersWriter = new ber.Writer();
  880. msgSecurityParametersWriter.startSequence();
  881. //msgSecurityParametersWriter.writeString (this.msgSecurityParameters.msgAuthoritativeEngineID);
  882. // writing a zero-length buffer fails - should fix asn1-ber for this condition
  883. if (this.msgSecurityParameters.msgAuthoritativeEngineID.length == 0) {
  884. msgSecurityParametersWriter.writeString("");
  885. } else {
  886. msgSecurityParametersWriter.writeBuffer(this.msgSecurityParameters.msgAuthoritativeEngineID, ber.OctetString);
  887. }
  888. msgSecurityParametersWriter.writeInt(this.msgSecurityParameters.msgAuthoritativeEngineBoots);
  889. msgSecurityParametersWriter.writeInt(this.msgSecurityParameters.msgAuthoritativeEngineTime);
  890. msgSecurityParametersWriter.writeString(this.msgSecurityParameters.msgUserName);
  891. if (this.hasAuthentication()) {
  892. msgSecurityParametersWriter.writeBuffer(Authentication.AUTH_PARAMETERS_PLACEHOLDER, ber.OctetString);
  893. // should never happen where msgFlags has no authentication but authentication parameters still present
  894. } else if (this.msgSecurityParameters.msgAuthenticationParameters.length > 0) {
  895. msgSecurityParametersWriter.writeBuffer(this.msgSecurityParameters.msgAuthenticationParameters, ber.OctetString);
  896. } else {
  897. msgSecurityParametersWriter.writeString("");
  898. }
  899. if (this.hasPrivacy()) {
  900. msgSecurityParametersWriter.writeBuffer(Encryption.PRIV_PARAMETERS_PLACEHOLDER, ber.OctetString);
  901. // should never happen where msgFlags has no privacy but privacy parameters still present
  902. } else if (this.msgSecurityParameters.msgPrivacyParameters.length > 0) {
  903. msgSecurityParametersWriter.writeBuffer(this.msgSecurityParameters.msgPrivacyParameters, ber.OctetString);
  904. } else {
  905. msgSecurityParametersWriter.writeString("");
  906. }
  907. msgSecurityParametersWriter.endSequence();
  908. writer.writeBuffer(msgSecurityParametersWriter.buffer, ber.OctetString);
  909. // ScopedPDU
  910. var scopedPduWriter = new ber.Writer();
  911. scopedPduWriter.startSequence();
  912. var contextEngineID = this.pdu.contextEngineID ? this.pdu.contextEngineID : this.msgSecurityParameters.msgAuthoritativeEngineID;
  913. if (contextEngineID.length == 0) {
  914. scopedPduWriter.writeString("");
  915. } else {
  916. scopedPduWriter.writeBuffer(contextEngineID, ber.OctetString);
  917. }
  918. scopedPduWriter.writeString(this.pdu.contextName);
  919. this.pdu.toBuffer(scopedPduWriter);
  920. scopedPduWriter.endSequence();
  921. if (this.hasPrivacy()) {
  922. encryptionResult = Encryption.encryptPdu(scopedPduWriter.buffer, this.user.privProtocol, this.user.privKey, this.user.authProtocol, this.msgSecurityParameters.msgAuthoritativeEngineID);
  923. writer.writeBuffer(encryptionResult.encryptedPdu, ber.OctetString);
  924. } else {
  925. writer.writeBuffer(scopedPduWriter.buffer);
  926. }
  927. writer.endSequence();
  928. this.buffer = writer.buffer;
  929. if (this.hasPrivacy()) {
  930. Encryption.addParametersToMessageBuffer(this.buffer, encryptionResult.msgPrivacyParameters);
  931. }
  932. if (this.hasAuthentication()) {
  933. Authentication.addParametersToMessageBuffer(this.buffer, this.user.authProtocol, this.user.authKey,
  934. this.msgSecurityParameters.msgAuthoritativeEngineID);
  935. }
  936. return this.buffer;
  937. };
  938. Message.prototype.processIncomingSecurity = function (user, responseCb) {
  939. if (this.hasPrivacy()) {
  940. if (!this.decryptPdu(user, responseCb)) {
  941. return false;
  942. }
  943. }
  944. if (this.hasAuthentication() && !this.isAuthenticationDisabled()) {
  945. return this.checkAuthentication(user, responseCb);
  946. } else {
  947. return true;
  948. }
  949. };
  950. Message.prototype.decryptPdu = function (user, responseCb) {
  951. var decryptedPdu;
  952. var decryptedPduReader;
  953. try {
  954. decryptedPdu = Encryption.decryptPdu(this.encryptedPdu, user.privProtocol,
  955. this.msgSecurityParameters.msgPrivacyParameters, user.privKey, user.authProtocol,
  956. this.msgSecurityParameters.msgAuthoritativeEngineID);
  957. decryptedPduReader = new ber.Reader(decryptedPdu);
  958. this.pdu = readPdu(decryptedPduReader, true);
  959. return true;
  960. // really really occasionally the decrypt truncates a single byte
  961. // causing an ASN read failure in readPdu()
  962. // in this case, disabling auto padding decrypts the PDU correctly
  963. // this try-catch provides the workaround for this condition
  964. } catch (possibleTruncationError) {
  965. try {
  966. decryptedPdu = Encryption.decryptPdu(this.encryptedPdu, user.privProtocol,
  967. this.msgSecurityParameters.msgPrivacyParameters, user.privKey, user.authProtocol,
  968. this.msgSecurityParameters.msgAuthoritativeEngineID, true);
  969. decryptedPduReader = new ber.Reader(decryptedPdu);
  970. this.pdu = readPdu(decryptedPduReader, true);
  971. return true;
  972. } catch (error) {
  973. responseCb(new ResponseInvalidError("Failed to decrypt PDU: " + error));
  974. return false;
  975. }
  976. }
  977. };
  978. Message.prototype.checkAuthentication = function (user, responseCb) {
  979. if (Authentication.isAuthentic(this.buffer, user.authProtocol, user.authKey,
  980. this.msgSecurityParameters.msgAuthoritativeEngineID, this.msgSecurityParameters.msgAuthenticationParameters)) {
  981. return true;
  982. } else {
  983. responseCb(new ResponseInvalidError("Authentication digest "
  984. + this.msgSecurityParameters.msgAuthenticationParameters.toString('hex')
  985. + " received in message does not match digest "
  986. + Authentication.calculateDigest(buffer, user.authProtocol, user.authKey,
  987. this.msgSecurityParameters.msgAuthoritativeEngineID).toString('hex')
  988. + " calculated for message"));
  989. return false;
  990. }
  991. };
  992. Message.prototype.hasAuthentication = function () {
  993. return this.msgGlobalData && this.msgGlobalData.msgFlags && this.msgGlobalData.msgFlags & 1;
  994. };
  995. Message.prototype.hasPrivacy = function () {
  996. return this.msgGlobalData && this.msgGlobalData.msgFlags && this.msgGlobalData.msgFlags & 2;
  997. };
  998. Message.prototype.isReportable = function () {
  999. return this.msgGlobalData && this.msgGlobalData.msgFlags && this.msgGlobalData.msgFlags & 4;
  1000. };
  1001. Message.prototype.setReportable = function (flag) {
  1002. if (this.msgGlobalData && this.msgGlobalData.msgFlags) {
  1003. if (flag) {
  1004. this.msgGlobalData.msgFlags = this.msgGlobalData.msgFlags | 4;
  1005. } else {
  1006. this.msgGlobalData.msgFlags = this.msgGlobalData.msgFlags & (255 - 4);
  1007. }
  1008. }
  1009. };
  1010. Message.prototype.isAuthenticationDisabled = function () {
  1011. return this.disableAuthentication;
  1012. };
  1013. Message.prototype.hasAuthoritativeEngineID = function () {
  1014. return this.msgSecurityParameters && this.msgSecurityParameters.msgAuthoritativeEngineID &&
  1015. this.msgSecurityParameters.msgAuthoritativeEngineID != "";
  1016. };
  1017. Message.prototype.createReportResponseMessage = function (engine, context) {
  1018. var user = {
  1019. name: "",
  1020. level: SecurityLevel.noAuthNoPriv
  1021. };
  1022. var responseSecurityParameters = {
  1023. msgAuthoritativeEngineID: engine.engineID,
  1024. msgAuthoritativeEngineBoots: engine.engineBoots,
  1025. msgAuthoritativeEngineTime: engine.engineTime,
  1026. msgUserName: user.name,
  1027. msgAuthenticationParameters: "",
  1028. msgPrivacyParameters: ""
  1029. };
  1030. var reportPdu = ReportPdu.createFromVariables(this.pdu.id, [], {});
  1031. reportPdu.contextName = context;
  1032. var responseMessage = Message.createRequestV3(user, responseSecurityParameters, reportPdu);
  1033. responseMessage.msgGlobalData.msgID = this.msgGlobalData.msgID;
  1034. return responseMessage;
  1035. };
  1036. Message.prototype.createResponseForRequest = function (responsePdu) {
  1037. if (this.version == Version3) {
  1038. return this.createV3ResponseFromRequest(responsePdu);
  1039. } else {
  1040. return this.createCommunityResponseFromRequest(responsePdu);
  1041. }
  1042. };
  1043. Message.prototype.createCommunityResponseFromRequest = function (responsePdu) {
  1044. return Message.createCommunity(this.version, this.community, responsePdu);
  1045. };
  1046. Message.prototype.createV3ResponseFromRequest = function (responsePdu) {
  1047. var responseUser = {
  1048. name: this.user.name,
  1049. level: this.user.name,
  1050. authProtocol: this.user.authProtocol,
  1051. authKey: this.user.authKey,
  1052. privProtocol: this.user.privProtocol,
  1053. privKey: this.user.privKey
  1054. };
  1055. var responseSecurityParameters = {
  1056. msgAuthoritativeEngineID: this.msgSecurityParameters.msgAuthoritativeEngineID,
  1057. msgAuthoritativeEngineBoots: this.msgSecurityParameters.msgAuthoritativeEngineBoots,
  1058. msgAuthoritativeEngineTime: this.msgSecurityParameters.msgAuthoritativeEngineTime,
  1059. msgUserName: this.msgSecurityParameters.msgUserName,
  1060. msgAuthenticationParameters: "",
  1061. msgPrivacyParameters: ""
  1062. };
  1063. var responseGlobalData = {
  1064. msgID: this.msgGlobalData.msgID,
  1065. msgMaxSize: 65507,
  1066. msgFlags: this.msgGlobalData.msgFlags & (255 - 4),
  1067. msgSecurityModel: 3
  1068. };
  1069. return Message.createV3(responseUser, responseGlobalData, responseSecurityParameters, responsePdu);
  1070. };
  1071. Message.createCommunity = function (version, community, pdu) {
  1072. var message = new Message();
  1073. message.version = version;
  1074. message.community = community;
  1075. message.pdu = pdu;
  1076. return message;
  1077. };
  1078. Message.createRequestV3 = function (user, msgSecurityParameters, pdu) {
  1079. var authFlag = user.level == SecurityLevel.authNoPriv || user.level == SecurityLevel.authPriv ? 1 : 0;
  1080. var privFlag = user.level == SecurityLevel.authPriv ? 1 : 0;
  1081. var reportableFlag = (pdu.type == PduType.GetResponse || pdu.type == PduType.TrapV2) ? 0 : 1;
  1082. var msgGlobalData = {
  1083. msgID: _generateId(), // random ID
  1084. msgMaxSize: 65507,
  1085. msgFlags: reportableFlag * 4 | privFlag * 2 | authFlag * 1,
  1086. msgSecurityModel: 3
  1087. };
  1088. return Message.createV3(user, msgGlobalData, msgSecurityParameters, pdu);
  1089. };
  1090. Message.createV3 = function (user, msgGlobalData, msgSecurityParameters, pdu) {
  1091. var message = new Message();
  1092. message.version = 3;
  1093. message.user = user;
  1094. message.msgGlobalData = msgGlobalData;
  1095. message.msgSecurityParameters = {
  1096. msgAuthoritativeEngineID: msgSecurityParameters.msgAuthoritativeEngineID || Buffer.from(""),
  1097. msgAuthoritativeEngineBoots: msgSecurityParameters.msgAuthoritativeEngineBoots || 0,
  1098. msgAuthoritativeEngineTime: msgSecurityParameters.msgAuthoritativeEngineTime || 0,
  1099. msgUserName: user.name || "",
  1100. msgAuthenticationParameters: "",
  1101. msgPrivacyParameters: ""
  1102. };
  1103. message.pdu = pdu;
  1104. return message;
  1105. };
  1106. Message.createDiscoveryV3 = function (pdu) {
  1107. var msgSecurityParameters = {
  1108. msgAuthoritativeEngineID: Buffer.from(""),
  1109. msgAuthoritativeEngineBoots: 0,
  1110. msgAuthoritativeEngineTime: 0
  1111. };
  1112. var emptyUser = {
  1113. name: "",
  1114. level: SecurityLevel.noAuthNoPriv
  1115. };
  1116. return Message.createRequestV3(emptyUser, msgSecurityParameters, pdu);
  1117. }
  1118. Message.createFromBuffer = function (buffer, user) {
  1119. var reader = new ber.Reader(buffer);
  1120. var message = new Message();
  1121. reader.readSequence();
  1122. message.version = reader.readInt();
  1123. if (message.version != 3) {
  1124. message.community = reader.readString();
  1125. message.pdu = readPdu(reader, false);
  1126. } else {
  1127. // HeaderData
  1128. message.msgGlobalData = {};
  1129. reader.readSequence();
  1130. message.msgGlobalData.msgID = reader.readInt();
  1131. message.msgGlobalData.msgMaxSize = reader.readInt();
  1132. message.msgGlobalData.msgFlags = reader.readString(ber.OctetString, true)[0];
  1133. message.msgGlobalData.msgSecurityModel = reader.readInt();
  1134. // msgSecurityParameters
  1135. message.msgSecurityParameters = {};
  1136. var msgSecurityParametersReader = new ber.Reader(reader.readString(ber.OctetString, true));
  1137. msgSecurityParametersReader.readSequence();
  1138. message.msgSecurityParameters.msgAuthoritativeEngineID = msgSecurityParametersReader.readString(ber.OctetString, true);
  1139. message.msgSecurityParameters.msgAuthoritativeEngineBoots = msgSecurityParametersReader.readInt();
  1140. message.msgSecurityParameters.msgAuthoritativeEngineTime = msgSecurityParametersReader.readInt();
  1141. message.msgSecurityParameters.msgUserName = msgSecurityParametersReader.readString();
  1142. message.msgSecurityParameters.msgAuthenticationParameters = Buffer.from(msgSecurityParametersReader.readString(ber.OctetString, true));
  1143. message.msgSecurityParameters.msgPrivacyParameters = Buffer.from(msgSecurityParametersReader.readString(ber.OctetString, true));
  1144. scopedPdu = true;
  1145. if (message.hasPrivacy()) {
  1146. message.encryptedPdu = reader.readString(ber.OctetString, true);
  1147. message.pdu = null;
  1148. } else {
  1149. message.pdu = readPdu(reader, true);
  1150. }
  1151. }
  1152. message.buffer = buffer;
  1153. return message;
  1154. };
  1155. var Req = function (session, message, feedCb, responseCb, options) {
  1156. this.message = message;
  1157. this.responseCb = responseCb;
  1158. this.retries = session.retries;
  1159. this.timeout = session.timeout;
  1160. this.onResponse = session.onSimpleGetResponse;
  1161. this.feedCb = feedCb;
  1162. this.port = (options && options.port) ? options.port : session.port;
  1163. this.context = session.context;
  1164. };
  1165. Req.prototype.getId = function () {
  1166. return this.message.getReqId();
  1167. };
  1168. /*****************************************************************************
  1169. ** Session class definition
  1170. **/
  1171. var Session = function (target, authenticator, options) {
  1172. this.target = target || "127.0.0.1";
  1173. this.version = (options && options.version)
  1174. ? options.version
  1175. : Version1;
  1176. if (this.version == Version3) {
  1177. this.user = authenticator;
  1178. } else {
  1179. this.community = authenticator || "public";
  1180. }
  1181. this.transport = (options && options.transport)
  1182. ? options.transport
  1183. : "udp4";
  1184. this.port = (options && options.port)
  1185. ? options.port
  1186. : 161;
  1187. this.trapPort = (options && options.trapPort)
  1188. ? options.trapPort
  1189. : 162;
  1190. this.retries = (options && (options.retries || options.retries == 0))
  1191. ? options.retries
  1192. : 1;
  1193. this.timeout = (options && options.timeout)
  1194. ? options.timeout
  1195. : 5000;
  1196. this.sourceAddress = (options && options.sourceAddress)
  1197. ? options.sourceAddress
  1198. : undefined;
  1199. this.sourcePort = (options && options.sourcePort)
  1200. ? parseInt(options.sourcePort)
  1201. : undefined;
  1202. this.idBitsSize = (options && options.idBitsSize)
  1203. ? parseInt(options.idBitsSize)
  1204. : 32;
  1205. this.context = (options && options.context) ? options.context : "";
  1206. DEBUG = options.debug;
  1207. this.reqs = {};
  1208. this.reqCount = 0;
  1209. this.dgram = dgram.createSocket(this.transport);
  1210. this.dgram.unref();
  1211. var me = this;
  1212. this.dgram.on("message", me.onMsg.bind(me));
  1213. this.dgram.on("close", me.onClose.bind(me));
  1214. this.dgram.on("error", me.onError.bind(me));
  1215. if (this.sourceAddress || this.sourcePort)
  1216. this.dgram.bind(this.sourcePort, this.sourceAddress);
  1217. };
  1218. util.inherits(Session, events.EventEmitter);
  1219. Session.prototype.close = function () {
  1220. this.dgram.close();
  1221. return this;
  1222. };
  1223. Session.prototype.cancelRequests = function (error) {
  1224. var id;
  1225. for (id in this.reqs) {
  1226. var req = this.reqs[id];
  1227. this.unregisterRequest(req.getId());
  1228. req.responseCb(error);
  1229. }
  1230. };
  1231. function _generateId(bitSize) {
  1232. if (bitSize === 16) {
  1233. return Math.floor(Math.random() * 10000) % 65535;
  1234. }
  1235. return Math.floor(Math.random() * 100000000) % 4294967295;
  1236. }
  1237. Session.prototype.get = function (oids, responseCb) {
  1238. function feedCb(req, message) {
  1239. var pdu = message.pdu;
  1240. var varbinds = [];
  1241. if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
  1242. req.responseCb(new ResponseInvalidError("Requested OIDs do not "
  1243. + "match response OIDs"));
  1244. } else {
  1245. for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
  1246. if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
  1247. req.responseCb(new ResponseInvalidError("OID '"
  1248. + req.message.pdu.varbinds[i].oid
  1249. + "' in request at positiion '" + i + "' does not "
  1250. + "match OID '" + pdu.varbinds[i].oid + "' in response "
  1251. + "at position '" + i + "'"));
  1252. return;
  1253. } else {
  1254. varbinds.push(pdu.varbinds[i]);
  1255. }
  1256. }
  1257. req.responseCb(null, varbinds);
  1258. }
  1259. }
  1260. var pduVarbinds = [];
  1261. for (var i = 0; i < oids.length; i++) {
  1262. var varbind = {
  1263. oid: oids[i]
  1264. };
  1265. pduVarbinds.push(varbind);
  1266. }
  1267. this.simpleGet(GetRequestPdu, feedCb, pduVarbinds, responseCb);
  1268. return this;
  1269. };
  1270. Session.prototype.getBulk = function () {
  1271. var oids, nonRepeaters, maxRepetitions, responseCb;
  1272. if (arguments.length >= 4) {
  1273. oids = arguments[0];
  1274. nonRepeaters = arguments[1];
  1275. maxRepetitions = arguments[2];
  1276. responseCb = arguments[3];
  1277. } else if (arguments.length >= 3) {
  1278. oids = arguments[0];
  1279. nonRepeaters = arguments[1];
  1280. maxRepetitions = 10;
  1281. responseCb = arguments[2];
  1282. } else {
  1283. oids = arguments[0];
  1284. nonRepeaters = 0;
  1285. maxRepetitions = 10;
  1286. responseCb = arguments[1];
  1287. }
  1288. function feedCb(req, message) {
  1289. var pdu = message.pdu;
  1290. var varbinds = [];
  1291. var i = 0;
  1292. // first walk through and grab non-repeaters
  1293. if (pdu.varbinds.length < nonRepeaters) {
  1294. req.responseCb(new ResponseInvalidError("Varbind count in "
  1295. + "response '" + pdu.varbinds.length + "' is less than "
  1296. + "non-repeaters '" + nonRepeaters + "' in request"));
  1297. } else {
  1298. for (; i < nonRepeaters; i++) {
  1299. if (isVarbindError(pdu.varbinds[i])) {
  1300. varbinds.push(pdu.varbinds[i]);
  1301. } else if (!oidFollowsOid(req.message.pdu.varbinds[i].oid,
  1302. pdu.varbinds[i].oid)) {
  1303. req.responseCb(new ResponseInvalidError("OID '"
  1304. + req.message.pdu.varbinds[i].oid + "' in request at "
  1305. + "positiion '" + i + "' does not precede "
  1306. + "OID '" + pdu.varbinds[i].oid + "' in response "
  1307. + "at position '" + i + "'"));
  1308. return;
  1309. } else {
  1310. varbinds.push(pdu.varbinds[i]);
  1311. }
  1312. }
  1313. }
  1314. var repeaters = req.message.pdu.varbinds.length - nonRepeaters;
  1315. // secondly walk through and grab repeaters
  1316. if (pdu.varbinds.length % (repeaters)) {
  1317. req.responseCb(new ResponseInvalidError("Varbind count in "
  1318. + "response '" + pdu.varbinds.length + "' is not a "
  1319. + "multiple of repeaters '" + repeaters
  1320. + "' plus non-repeaters '" + nonRepeaters + "' in request"));
  1321. } else {
  1322. while (i < pdu.varbinds.length) {
  1323. for (var j = 0; j < repeaters; j++, i++) {
  1324. var reqIndex = nonRepeaters + j;
  1325. var respIndex = i;
  1326. if (isVarbindError(pdu.varbinds[respIndex])) {
  1327. if (!varbinds[reqIndex])
  1328. varbinds[reqIndex] = [];
  1329. varbinds[reqIndex].push(pdu.varbinds[respIndex]);
  1330. } else if (!oidFollowsOid(
  1331. req.message.pdu.varbinds[reqIndex].oid,
  1332. pdu.varbinds[respIndex].oid)) {
  1333. req.responseCb(new ResponseInvalidError("OID '"
  1334. + req.message.pdu.varbinds[reqIndex].oid
  1335. + "' in request at positiion '" + (reqIndex)
  1336. + "' does not precede OID '"
  1337. + pdu.varbinds[respIndex].oid
  1338. + "' in response at position '" + (respIndex) + "'"));
  1339. return;
  1340. } else {
  1341. if (!varbinds[reqIndex])
  1342. varbinds[reqIndex] = [];
  1343. varbinds[reqIndex].push(pdu.varbinds[respIndex]);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. req.responseCb(null, varbinds);
  1349. }
  1350. var pduVarbinds = [];
  1351. for (var i = 0; i < oids.length; i++) {
  1352. var varbind = {
  1353. oid: oids[i]
  1354. };
  1355. pduVarbinds.push(varbind);
  1356. }
  1357. var options = {
  1358. nonRepeaters: nonRepeaters,
  1359. maxRepetitions: maxRepetitions
  1360. };
  1361. this.simpleGet(GetBulkRequestPdu, feedCb, pduVarbinds, responseCb,
  1362. options);
  1363. return this;
  1364. };
  1365. Session.prototype.getNext = function (oids, responseCb) {
  1366. function feedCb(req, message) {
  1367. var pdu = message.pdu;
  1368. var varbinds = [];
  1369. if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
  1370. req.responseCb(new ResponseInvalidError("Requested OIDs do not "
  1371. + "match response OIDs"));
  1372. } else {
  1373. for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
  1374. if (isVarbindError(pdu.varbinds[i])) {
  1375. varbinds.push(pdu.varbinds[i]);
  1376. } else if (!oidFollowsOid(req.message.pdu.varbinds[i].oid,
  1377. pdu.varbinds[i].oid)) {
  1378. req.responseCb(new ResponseInvalidError("OID '"
  1379. + req.message.pdu.varbinds[i].oid + "' in request at "
  1380. + "positiion '" + i + "' does not precede "
  1381. + "OID '" + pdu.varbinds[i].oid + "' in response "
  1382. + "at position '" + i + "'"));
  1383. return;
  1384. } else {
  1385. varbinds.push(pdu.varbinds[i]);
  1386. }
  1387. }
  1388. req.responseCb(null, varbinds);
  1389. }
  1390. }
  1391. var pduVarbinds = [];
  1392. for (var i = 0; i < oids.length; i++) {
  1393. var varbind = {
  1394. oid: oids[i]
  1395. };
  1396. pduVarbinds.push(varbind);
  1397. }
  1398. this.simpleGet(GetNextRequestPdu, feedCb, pduVarbinds, responseCb);
  1399. return this;
  1400. };
  1401. Session.prototype.inform = function () {
  1402. var typeOrOid = arguments[0];
  1403. var varbinds, options = {}, responseCb;
  1404. /**
  1405. ** Support the following signatures:
  1406. **
  1407. ** typeOrOid, varbinds, options, callback
  1408. ** typeOrOid, varbinds, callback
  1409. ** typeOrOid, options, callback
  1410. ** typeOrOid, callback
  1411. **/
  1412. if (arguments.length >= 4) {
  1413. varbinds = arguments[1];
  1414. options = arguments[2];
  1415. responseCb = arguments[3];
  1416. } else if (arguments.length >= 3) {
  1417. if (arguments[1].constructor != Array) {
  1418. varbinds = [];
  1419. options = arguments[1];
  1420. responseCb = arguments[2];
  1421. } else {
  1422. varbinds = arguments[1];
  1423. responseCb = arguments[2];
  1424. }
  1425. } else {
  1426. varbinds = [];
  1427. responseCb = arguments[1];
  1428. }
  1429. if (this.version == Version1) {
  1430. responseCb(new RequestInvalidError("Inform not allowed for SNMPv1"));
  1431. return;
  1432. }
  1433. function feedCb(req, message) {
  1434. var pdu = message.pdu;
  1435. var varbinds = [];
  1436. if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
  1437. req.responseCb(new ResponseInvalidError("Inform OIDs do not "
  1438. + "match response OIDs"));
  1439. } else {
  1440. for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
  1441. if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
  1442. req.responseCb(new ResponseInvalidError("OID '"
  1443. + req.message.pdu.varbinds[i].oid
  1444. + "' in inform at positiion '" + i + "' does not "
  1445. + "match OID '" + pdu.varbinds[i].oid + "' in response "
  1446. + "at position '" + i + "'"));
  1447. return;
  1448. } else {
  1449. varbinds.push(pdu.varbinds[i]);
  1450. }
  1451. }
  1452. req.responseCb(null, varbinds);
  1453. }
  1454. }
  1455. if (typeof typeOrOid != "string")
  1456. typeOrOid = "1.3.6.1.6.3.1.1.5." + (typeOrOid + 1);
  1457. var pduVarbinds = [
  1458. {
  1459. oid: "1.3.6.1.2.1.1.3.0",
  1460. type: ObjectType.TimeTicks,
  1461. value: options.upTime || Math.floor(process.uptime() * 100)
  1462. },
  1463. {
  1464. oid: "1.3.6.1.6.3.1.1.4.1.0",
  1465. type: ObjectType.OID,
  1466. value: typeOrOid
  1467. }
  1468. ];
  1469. for (var i = 0; i < varbinds.length; i++) {
  1470. var varbind = {
  1471. oid: varbinds[i].oid,
  1472. type: varbinds[i].type,
  1473. value: varbinds[i].value
  1474. };
  1475. pduVarbinds.push(varbind);
  1476. }
  1477. options.port = this.trapPort;
  1478. this.simpleGet(InformRequestPdu, feedCb, pduVarbinds, responseCb, options);
  1479. return this;
  1480. };
  1481. Session.prototype.onClose = function () {
  1482. this.cancelRequests(new Error("Socket forcibly closed"));
  1483. this.emit("close");
  1484. };
  1485. Session.prototype.onError = function (error) {
  1486. this.emit(error);
  1487. };
  1488. Session.prototype.onMsg = function (buffer) {
  1489. try {
  1490. var message = Message.createFromBuffer(buffer);
  1491. var req = this.unregisterRequest(message.getReqId());
  1492. if (!req)
  1493. return;
  1494. if (!message.processIncomingSecurity(this.user, req.responseCb))
  1495. return;
  1496. try {
  1497. if (message.version != req.message.version) {
  1498. req.responseCb(new ResponseInvalidError("Version in request '"
  1499. + req.message.version + "' does not match version in "
  1500. + "response '" + message.version + "'"));
  1501. } else if (message.community != req.message.community) {
  1502. req.responseCb(new ResponseInvalidError("Community '"
  1503. + req.message.community + "' in request does not match "
  1504. + "community '" + message.community + "' in response"));
  1505. } else if (message.pdu.type == PduType.GetResponse) {
  1506. req.onResponse(req, message);
  1507. } else if (message.pdu.type == PduType.Report) {
  1508. if (!req.originalPdu) {
  1509. req.responseCb(new ResponseInvalidError("Unexpected Report PDU"));
  1510. return;
  1511. }
  1512. this.msgSecurityParameters = {
  1513. msgAuthoritativeEngineID: message.msgSecurityParameters.msgAuthoritativeEngineID,
  1514. msgAuthoritativeEngineBoots: message.msgSecurityParameters.msgAuthoritativeEngineBoots,
  1515. msgAuthoritativeEngineTime: message.msgSecurityParameters.msgAuthoritativeEngineTime
  1516. };
  1517. req.originalPdu.contextName = this.context;
  1518. this.sendV3Req(req.originalPdu, req.feedCb, req.responseCb, req.options, req.port);
  1519. } else {
  1520. req.responseCb(new ResponseInvalidError("Unknown PDU type '"
  1521. + message.pdu.type + "' in response"));
  1522. }
  1523. } catch (error) {
  1524. req.responseCb(error);
  1525. }
  1526. } catch (error) {
  1527. this.emit("error", error);
  1528. }
  1529. };
  1530. Session.prototype.onSimpleGetResponse = function (req, message) {
  1531. var pdu = message.pdu;
  1532. if (pdu.errorStatus > 0) {
  1533. var statusString = ErrorStatus[pdu.errorStatus]
  1534. || ErrorStatus.GeneralError;
  1535. var statusCode = ErrorStatus[statusString]
  1536. || ErrorStatus[ErrorStatus.GeneralError];
  1537. if (pdu.errorIndex <= 0 || pdu.errorIndex > pdu.varbinds.length) {
  1538. req.responseCb(new RequestFailedError(statusString, statusCode));
  1539. } else {
  1540. var oid = pdu.varbinds[pdu.errorIndex - 1].oid;
  1541. var error = new RequestFailedError(statusString + ": " + oid,
  1542. statusCode);
  1543. req.responseCb(error);
  1544. }
  1545. } else {
  1546. req.feedCb(req, message);
  1547. }
  1548. };
  1549. Session.prototype.registerRequest = function (req) {
  1550. if (!this.reqs[req.getId()]) {
  1551. this.reqs[req.getId()] = req;
  1552. if (this.reqCount <= 0)
  1553. this.dgram.ref();
  1554. this.reqCount++;
  1555. }
  1556. var me = this;
  1557. req.timer = setTimeout(function () {
  1558. if (req.retries-- > 0) {
  1559. me.send(req);
  1560. } else {
  1561. me.unregisterRequest(req.getId());
  1562. req.responseCb(new RequestTimedOutError(
  1563. "Request timed out"));
  1564. }
  1565. }, req.timeout);
  1566. };
  1567. Session.prototype.send = function (req, noWait) {
  1568. try {
  1569. var me = this;
  1570. var buffer = req.message.toBuffer();
  1571. this.dgram.send(buffer, 0, buffer.length, req.port, this.target,
  1572. function (error, bytes) {
  1573. if (error) {
  1574. req.responseCb(error);
  1575. } else {
  1576. if (noWait) {
  1577. req.responseCb(null);
  1578. } else {
  1579. me.registerRequest(req);
  1580. }
  1581. }
  1582. });
  1583. } catch (error) {
  1584. req.responseCb(error);
  1585. }
  1586. return this;
  1587. };
  1588. Session.prototype.set = function (varbinds, responseCb) {
  1589. function feedCb(req, message) {
  1590. var pdu = message.pdu;
  1591. var varbinds = [];
  1592. if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
  1593. req.responseCb(new ResponseInvalidError("Requested OIDs do not "
  1594. + "match response OIDs"));
  1595. } else {
  1596. for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
  1597. if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
  1598. req.responseCb(new ResponseInvalidError("OID '"
  1599. + req.message.pdu.varbinds[i].oid
  1600. + "' in request at positiion '" + i + "' does not "
  1601. + "match OID '" + pdu.varbinds[i].oid + "' in response "
  1602. + "at position '" + i + "'"));
  1603. return;
  1604. } else {
  1605. varbinds.push(pdu.varbinds[i]);
  1606. }
  1607. }
  1608. req.responseCb(null, varbinds);
  1609. }
  1610. }
  1611. var pduVarbinds = [];
  1612. for (var i = 0; i < varbinds.length; i++) {
  1613. var varbind = {
  1614. oid: varbinds[i].oid,
  1615. type: varbinds[i].type,
  1616. value: varbinds[i].value
  1617. };
  1618. pduVarbinds.push(varbind);
  1619. }
  1620. this.simpleGet(SetRequestPdu, feedCb, pduVarbinds, responseCb);
  1621. return this;
  1622. };
  1623. Session.prototype.simpleGet = function (pduClass, feedCb, varbinds,
  1624. responseCb, options) {
  1625. try {
  1626. var id = _generateId(this.idBitsSize);
  1627. var pdu = SimplePdu.createFromVariables(pduClass, id, varbinds, options);
  1628. var message;
  1629. var req;
  1630. if (this.version == Version3) {
  1631. if (this.msgSecurityParameters) {
  1632. this.sendV3Req(pdu, feedCb, responseCb, options, this.port);
  1633. } else {
  1634. // SNMPv3 discovery
  1635. var discoveryPdu = createDiscoveryPdu(this.context);
  1636. var discoveryMessage = Message.createDiscoveryV3(discoveryPdu);
  1637. var discoveryReq = new Req(this, discoveryMessage, feedCb, responseCb, options);
  1638. discoveryReq.originalPdu = pdu;
  1639. this.send(discoveryReq);
  1640. }
  1641. } else {
  1642. message = Message.createCommunity(this.version, this.community, pdu);
  1643. req = new Req(this, message, feedCb, responseCb, options);
  1644. this.send(req);
  1645. }
  1646. } catch (error) {
  1647. if (responseCb)
  1648. responseCb(error);
  1649. }
  1650. }
  1651. function subtreeCb(req, varbinds) {
  1652. var done = 0;
  1653. for (var i = varbinds.length; i > 0; i--) {
  1654. if (!oidInSubtree(req.baseOid, varbinds[i - 1].oid)) {
  1655. done = 1;
  1656. varbinds.pop();
  1657. }
  1658. }
  1659. if (varbinds.length > 0)
  1660. req.feedCb(varbinds);
  1661. if (done)
  1662. return true;
  1663. }
  1664. Session.prototype.subtree = function () {
  1665. var me = this;
  1666. var oid = arguments[0];
  1667. var maxRepetitions, feedCb, doneCb;
  1668. if (arguments.length < 4) {
  1669. maxRepetitions = 20;
  1670. feedCb = arguments[1];
  1671. doneCb = arguments[2];
  1672. } else {
  1673. maxRepetitions = arguments[1];
  1674. feedCb = arguments[2];
  1675. doneCb = arguments[3];
  1676. }
  1677. var req = {
  1678. feedCb: feedCb,
  1679. doneCb: doneCb,
  1680. maxRepetitions: maxRepetitions,
  1681. baseOid: oid
  1682. };
  1683. this.walk(oid, maxRepetitions, subtreeCb.bind(me, req), doneCb);
  1684. return this;
  1685. };
  1686. function tableColumnsResponseCb(req, error) {
  1687. if (error) {
  1688. req.responseCb(error);
  1689. } else if (req.error) {
  1690. req.responseCb(req.error);
  1691. } else {
  1692. if (req.columns.length > 0) {
  1693. var column = req.columns.pop();
  1694. var me = this;
  1695. this.subtree(req.rowOid + column, req.maxRepetitions,
  1696. tableColumnsFeedCb.bind(me, req),
  1697. tableColumnsResponseCb.bind(me, req));
  1698. } else {
  1699. req.responseCb(null, req.table);
  1700. }
  1701. }
  1702. }
  1703. function tableColumnsFeedCb(req, varbinds) {
  1704. for (var i = 0; i < varbinds.length; i++) {
  1705. if (isVarbindError(varbinds[i])) {
  1706. req.error = new RequestFailedError(varbindError(varbind[i]));
  1707. return true;
  1708. }
  1709. var oid = varbinds[i].oid.replace(req.rowOid, "");
  1710. if (oid && oid != varbinds[i].oid) {
  1711. var match = oid.match(/^(\d+)\.(.+)$/);
  1712. if (match && match[1] > 0) {
  1713. if (!req.table[match[2]])
  1714. req.table[match[2]] = {};
  1715. req.table[match[2]][match[1]] = varbinds[i].value;
  1716. }
  1717. }
  1718. }
  1719. }
  1720. Session.prototype.tableColumns = function () {
  1721. var me = this;
  1722. var oid = arguments[0];
  1723. var columns = arguments[1];
  1724. var maxRepetitions, responseCb;
  1725. if (arguments.length < 4) {
  1726. responseCb = arguments[2];
  1727. maxRepetitions = 20;
  1728. } else {
  1729. maxRepetitions = arguments[2];
  1730. responseCb = arguments[3];
  1731. }
  1732. var req = {
  1733. responseCb: responseCb,
  1734. maxRepetitions: maxRepetitions,
  1735. baseOid: oid,
  1736. rowOid: oid + ".1.",
  1737. columns: columns.slice(0),
  1738. table: {}
  1739. };
  1740. if (req.columns.length > 0) {
  1741. var column = req.columns.pop();
  1742. this.subtree(req.rowOid + column, maxRepetitions,
  1743. tableColumnsFeedCb.bind(me, req),
  1744. tableColumnsResponseCb.bind(me, req));
  1745. }
  1746. return this;
  1747. };
  1748. function tableResponseCb(req, error) {
  1749. if (error)
  1750. req.responseCb(error);
  1751. else if (req.error)
  1752. req.responseCb(req.error);
  1753. else
  1754. req.responseCb(null, req.table);
  1755. }
  1756. function tableFeedCb(req, varbinds) {
  1757. for (var i = 0; i < varbinds.length; i++) {
  1758. if (isVarbindError(varbinds[i])) {
  1759. req.error = new RequestFailedError(varbindError(varbind[i]));
  1760. return true;
  1761. }
  1762. var oid = varbinds[i].oid.replace(req.rowOid, "");
  1763. if (oid && oid != varbinds[i].oid) {
  1764. var match = oid.match(/^(\d+)\.(.+)$/);
  1765. if (match && match[1] > 0) {
  1766. if (!req.table[match[2]])
  1767. req.table[match[2]] = {};
  1768. req.table[match[2]][match[1]] = varbinds[i].value;
  1769. }
  1770. }
  1771. }
  1772. }
  1773. Session.prototype.table = function () {
  1774. var me = this;
  1775. var oid = arguments[0];
  1776. var maxRepetitions, responseCb;
  1777. if (arguments.length < 3) {
  1778. responseCb = arguments[1];
  1779. maxRepetitions = 20;
  1780. } else {
  1781. maxRepetitions = arguments[1];
  1782. responseCb = arguments[2];
  1783. }
  1784. var req = {
  1785. responseCb: responseCb,
  1786. maxRepetitions: maxRepetitions,
  1787. baseOid: oid,
  1788. rowOid: oid + ".1.",
  1789. table: {}
  1790. };
  1791. this.subtree(oid, maxRepetitions, tableFeedCb.bind(me, req),
  1792. tableResponseCb.bind(me, req));
  1793. return this;
  1794. };
  1795. Session.prototype.trap = function () {
  1796. var req = {};
  1797. try {
  1798. var typeOrOid = arguments[0];
  1799. var varbinds, options = {}, responseCb;
  1800. var message;
  1801. /**
  1802. ** Support the following signatures:
  1803. **
  1804. ** typeOrOid, varbinds, options, callback
  1805. ** typeOrOid, varbinds, agentAddr, callback
  1806. ** typeOrOid, varbinds, callback
  1807. ** typeOrOid, agentAddr, callback
  1808. ** typeOrOid, options, callback
  1809. ** typeOrOid, callback
  1810. **/
  1811. if (arguments.length >= 4) {
  1812. varbinds = arguments[1];
  1813. if (typeof arguments[2] == "string") {
  1814. options.agentAddr = arguments[2];
  1815. } else if (arguments[2].constructor != Array) {
  1816. options = arguments[2];
  1817. }
  1818. responseCb = arguments[3];
  1819. } else if (arguments.length >= 3) {
  1820. if (typeof arguments[1] == "string") {
  1821. varbinds = [];
  1822. options.agentAddr = arguments[1];
  1823. } else if (arguments[1].constructor != Array) {
  1824. varbinds = [];
  1825. options = arguments[1];
  1826. } else {
  1827. varbinds = arguments[1];
  1828. agentAddr = null;
  1829. }
  1830. responseCb = arguments[2];
  1831. } else {
  1832. varbinds = [];
  1833. responseCb = arguments[1];
  1834. }
  1835. var pdu, pduVarbinds = [];
  1836. for (var i = 0; i < varbinds.length; i++) {
  1837. var varbind = {
  1838. oid: varbinds[i].oid,
  1839. type: varbinds[i].type,
  1840. value: varbinds[i].value
  1841. };
  1842. pduVarbinds.push(varbind);
  1843. }
  1844. var id = _generateId(this.idBitsSize);
  1845. if (this.version == Version2c || this.version == Version3) {
  1846. if (typeof typeOrOid != "string")
  1847. typeOrOid = "1.3.6.1.6.3.1.1.5." + (typeOrOid + 1);
  1848. pduVarbinds.unshift(
  1849. {
  1850. oid: "1.3.6.1.2.1.1.3.0",
  1851. type: ObjectType.TimeTicks,
  1852. value: options.upTime || Math.floor(process.uptime() * 100)
  1853. },
  1854. {
  1855. oid: "1.3.6.1.6.3.1.1.4.1.0",
  1856. type: ObjectType.OID,
  1857. value: typeOrOid
  1858. }
  1859. );
  1860. pdu = TrapV2Pdu.createFromVariables(id, pduVarbinds, options);
  1861. } else {
  1862. pdu = TrapPdu.createFromVariables(typeOrOid, pduVarbinds, options);
  1863. }
  1864. if (this.version == Version3) {
  1865. var msgSecurityParameters = {
  1866. msgAuthoritativeEngineID: this.user.engineID,
  1867. msgAuthoritativeEngineBoots: 0,
  1868. msgAuthoritativeEngineTime: 0
  1869. };
  1870. message = Message.createRequestV3(this.user, msgSecurityParameters, pdu);
  1871. } else {
  1872. message = Message.createCommunity(this.version, this.community, pdu);
  1873. }
  1874. req = {
  1875. id: id,
  1876. message: message,
  1877. responseCb: responseCb,
  1878. port: this.trapPort
  1879. };
  1880. this.send(req, true);
  1881. } catch (error) {
  1882. if (req.responseCb)
  1883. req.responseCb(error);
  1884. }
  1885. return this;
  1886. };
  1887. Session.prototype.unregisterRequest = function (id) {
  1888. var req = this.reqs[id];
  1889. if (req) {
  1890. delete this.reqs[id];
  1891. clearTimeout(req.timer);
  1892. delete req.timer;
  1893. this.reqCount--;
  1894. if (this.reqCount <= 0)
  1895. this.dgram.unref();
  1896. return req;
  1897. } else {
  1898. return null;
  1899. }
  1900. };
  1901. function walkCb(req, error, varbinds) {
  1902. var done = 0;
  1903. var oid;
  1904. if (error) {
  1905. if (error instanceof RequestFailedError) {
  1906. if (error.status != ErrorStatus.NoSuchName) {
  1907. req.doneCb(error);
  1908. return;
  1909. } else {
  1910. // signal the version 1 walk code below that it should stop
  1911. done = 1;
  1912. }
  1913. } else {
  1914. req.doneCb(error);
  1915. return;
  1916. }
  1917. }
  1918. if (this.version == Version2c || this.version == Version3) {
  1919. for (var i = varbinds[0].length; i > 0; i--) {
  1920. if (varbinds[0][i - 1].type == ObjectType.EndOfMibView) {
  1921. varbinds[0].pop();
  1922. done = 1;
  1923. }
  1924. }
  1925. if (req.feedCb(varbinds[0]))
  1926. done = 1;
  1927. if (!done)
  1928. oid = varbinds[0][varbinds[0].length - 1].oid;
  1929. } else {
  1930. if (!done) {
  1931. if (req.feedCb(varbinds)) {
  1932. done = 1;
  1933. } else {
  1934. oid = varbinds[0].oid;
  1935. }
  1936. }
  1937. }
  1938. if (done)
  1939. req.doneCb(null);
  1940. else
  1941. this.walk(oid, req.maxRepetitions, req.feedCb, req.doneCb,
  1942. req.baseOid);
  1943. }
  1944. Session.prototype.walk = function () {
  1945. var me = this;
  1946. var oid = arguments[0];
  1947. var maxRepetitions, feedCb, doneCb, baseOid;
  1948. if (arguments.length < 4) {
  1949. maxRepetitions = 20;
  1950. feedCb = arguments[1];
  1951. doneCb = arguments[2];
  1952. } else {
  1953. maxRepetitions = arguments[1];
  1954. feedCb = arguments[2];
  1955. doneCb = arguments[3];
  1956. }
  1957. var req = {
  1958. maxRepetitions: maxRepetitions,
  1959. feedCb: feedCb,
  1960. doneCb: doneCb
  1961. };
  1962. if (this.version == Version2c || this.version == Version3)
  1963. this.getBulk([oid], 0, maxRepetitions,
  1964. walkCb.bind(me, req));
  1965. else
  1966. this.getNext([oid], walkCb.bind(me, req));
  1967. return this;
  1968. };
  1969. Session.prototype.sendV3Req = function (pdu, feedCb, responseCb, options, port) {
  1970. var message = Message.createRequestV3(this.user, this.msgSecurityParameters, pdu);
  1971. var reqOptions = options || {};
  1972. var req = new Req(this, message, feedCb, responseCb, reqOptions);
  1973. req.port = port;
  1974. this.send(req);
  1975. };
  1976. var Engine = function (engineID, engineBoots, engineTime) {
  1977. if (engineID) {
  1978. this.engineID = Buffer.from(engineID, 'hex');
  1979. } else {
  1980. this.generateEngineID();
  1981. }
  1982. this.engineBoots = 0;
  1983. this.engineTime = 10;
  1984. };
  1985. Engine.prototype.generateEngineID = function () {
  1986. // generate a 17-byte engine ID in the following format:
  1987. // 0x80 + 0x00B983 (enterprise OID) | 0x80 (enterprise-specific format) | 12 bytes of random
  1988. this.engineID = Buffer.alloc(17);
  1989. this.engineID.fill('8000B98380', 'hex', 0, 5);
  1990. this.engineID.fill(crypto.randomBytes(12), 5, 17, 'hex');
  1991. }
  1992. var Listener = function (options, receiver) {
  1993. this.receiver = receiver;
  1994. this.callback = receiver.onMsg;
  1995. this.family = options.transport || 'udp4';
  1996. this.port = options.port || 161;
  1997. this.disableAuthorization = options.disableAuthorization || false;
  1998. };
  1999. Listener.prototype.startListening = function (receiver) {
  2000. var me = this;
  2001. this.dgram = dgram.createSocket(this.family);
  2002. this.dgram.bind(this.port);
  2003. this.dgram.on("message", me.callback.bind(me.receiver));
  2004. };
  2005. Listener.prototype.send = function (message, rinfo) {
  2006. var me = this;
  2007. var buffer = message.toBuffer();
  2008. this.dgram.send(buffer, 0, buffer.length, rinfo.port, rinfo.address,
  2009. function (error, bytes) {
  2010. if (error) {
  2011. // me.callback (error);
  2012. console.error("Error sending: " + error.message);
  2013. } else {
  2014. // debug ("Listener sent response message");
  2015. }
  2016. });
  2017. };
  2018. Listener.formatCallbackData = function (pdu, rinfo) {
  2019. if (pdu.contextEngineID) {
  2020. pdu.contextEngineID = pdu.contextEngineID.toString('hex');
  2021. }
  2022. delete pdu.nonRepeaters;
  2023. delete pdu.maxRepetitions;
  2024. return {
  2025. pdu: pdu,
  2026. rinfo: rinfo
  2027. };
  2028. };
  2029. Listener.processIncoming = function (buffer, authorizer, callback) {
  2030. var message = Message.createFromBuffer(buffer);
  2031. var community;
  2032. // Authorization
  2033. if (message.version == Version3) {
  2034. message.user = authorizer.users.filter(localUser => localUser.name ==
  2035. message.msgSecurityParameters.msgUserName)[0];
  2036. message.disableAuthentication = authorizer.disableAuthorization;
  2037. if (!message.user) {
  2038. if (message.msgSecurityParameters.msgUserName != "" && !authorizer.disableAuthorization) {
  2039. callback(new RequestFailedError("Local user not found for message with user " +
  2040. message.msgSecurityParameters.msgUserName));
  2041. return;
  2042. } else if (message.hasAuthentication()) {
  2043. callback(new RequestFailedError("Local user not found and message requires authentication with user " +
  2044. message.msgSecurityParameters.msgUserName));
  2045. return;
  2046. } else {
  2047. message.user = {
  2048. name: "",
  2049. level: SecurityLevel.noAuthNoPriv
  2050. };
  2051. }
  2052. }
  2053. if (!message.processIncomingSecurity(message.user, callback)) {
  2054. return;
  2055. }
  2056. } else {
  2057. community = authorizer.communities.filter(localCommunity => localCommunity == message.community)[0];
  2058. if (!community && !authorizer.disableAuthorization) {
  2059. callback(new RequestFailedError("Local community not found for message with community " + message.community));
  2060. return;
  2061. }
  2062. }
  2063. return message;
  2064. };
  2065. var Authorizer = function () {
  2066. this.communities = [];
  2067. this.users = [];
  2068. }
  2069. Authorizer.prototype.addCommunity = function (community) {
  2070. if (this.getCommunity(community)) {
  2071. return;
  2072. } else {
  2073. this.communities.push(community);
  2074. }
  2075. };
  2076. Authorizer.prototype.getCommunity = function (community) {
  2077. return this.communities.filter(localCommunity => localCommunity == community)[0] || null;
  2078. };
  2079. Authorizer.prototype.getCommunities = function () {
  2080. return this.communities;
  2081. };
  2082. Authorizer.prototype.deleteCommunity = function (community) {
  2083. var index = this.communities.indexOf(community);
  2084. if (index > -1) {
  2085. this.communities.splice(index, 1);
  2086. }
  2087. };
  2088. Authorizer.prototype.addUser = function (user) {
  2089. if (this.getUser(user.name)) {
  2090. this.deleteUser(user.name);
  2091. }
  2092. this.users.push(user);
  2093. };
  2094. Authorizer.prototype.getUser = function (userName) {
  2095. return this.users.filter(localUser => localUser.name == userName)[0] || null;
  2096. };
  2097. Authorizer.prototype.getUsers = function () {
  2098. return this.users;
  2099. };
  2100. Authorizer.prototype.deleteUser = function (userName) {
  2101. var index = this.users.findIndex(localUser => localUser.name == userName);
  2102. if (index > -1) {
  2103. this.users.splice(index, 1);
  2104. }
  2105. };
  2106. /*****************************************************************************
  2107. ** Receiver class definition
  2108. **/
  2109. var Receiver = function (options, callback) {
  2110. DEBUG = options.debug;
  2111. this.listener = new Listener(options, this);
  2112. this.authorizer = new Authorizer();
  2113. this.engine = new Engine(options.engineID);
  2114. this.engineBoots = 0;
  2115. this.engineTime = 10;
  2116. this.disableAuthorization = false;
  2117. this.callback = callback;
  2118. this.family = options.transport || 'udp4';
  2119. this.port = options.port || 162;
  2120. options.port = this.port;
  2121. this.disableAuthorization = options.disableAuthorization || false;
  2122. this.context = (options && options.context) ? options.context : "";
  2123. this.listener = new Listener(options, this);
  2124. };
  2125. Receiver.prototype.addCommunity = function (community) {
  2126. this.authorizer.addCommunity(community);
  2127. };
  2128. Receiver.prototype.getCommunity = function (community) {
  2129. return this.authorizer.getCommunity(community);
  2130. };
  2131. Receiver.prototype.getCommunities = function () {
  2132. return this.authorizer.getCommunities();
  2133. };
  2134. Receiver.prototype.deleteCommunity = function (community) {
  2135. this.authorizer.deleteCommunities(community);
  2136. };
  2137. Receiver.prototype.addUser = function (user) {
  2138. this.authorizer.addUser(user);
  2139. };
  2140. Receiver.prototype.getUser = function (userName) {
  2141. return this.authorizer.getUser(userName);
  2142. };
  2143. Receiver.prototype.getUsers = function () {
  2144. return this.authorizer.getUsers();
  2145. };
  2146. Receiver.prototype.deleteUser = function (userName) {
  2147. this.authorizer.deleteUser(userName);
  2148. };
  2149. Receiver.prototype.onMsg = function (buffer, rinfo) {
  2150. var message = Listener.processIncoming(buffer, this.authorizer, this.callback);
  2151. var reportMessage;
  2152. if (!message) {
  2153. return;
  2154. }
  2155. // The only GetRequest PDUs supported are those used for SNMPv3 discovery
  2156. if (message.pdu.type == PduType.GetRequest) {
  2157. if (message.version != Version3) {
  2158. this.callback(new RequestInvalidError("Only SNMPv3 discovery GetRequests are supported"));
  2159. return;
  2160. } else if (message.hasAuthentication()) {
  2161. this.callback(new RequestInvalidError("Only discovery (noAuthNoPriv) GetRequests are supported but this message has authentication"));
  2162. return;
  2163. } else if (!message.isReportable()) {
  2164. this.callback(new RequestInvalidError("Only discovery GetRequests are supported and this message does not have the reportable flag set"));
  2165. return;
  2166. }
  2167. var reportMessage = message.createReportResponseMessage(this.engine, this.context);
  2168. this.listener.send(reportMessage, rinfo);
  2169. return;
  2170. }
  2171. ;
  2172. // Inform/trap processing
  2173. debug(JSON.stringify(message.pdu, null, 2));
  2174. if (message.pdu.type == PduType.Trap || message.pdu.type == PduType.TrapV2) {
  2175. this.callback(null, this.formatCallbackData(message.pdu, rinfo));
  2176. } else if (message.pdu.type == PduType.InformRequest) {
  2177. message.pdu.type = PduType.GetResponse;
  2178. message.buffer = null;
  2179. message.setReportable(false);
  2180. this.listener.send(message, rinfo);
  2181. message.pdu.type = PduType.InformRequest;
  2182. this.callback(null, this.formatCallbackData(message.pdu, rinfo));
  2183. } else {
  2184. this.callback(new RequestInvalidError("Unexpected PDU type " + message.pdu.type + " (" + PduType[message.pdu.type] + ")"));
  2185. }
  2186. }
  2187. Receiver.prototype.formatCallbackData = function (pdu, rinfo) {
  2188. if (pdu.contextEngineID) {
  2189. pdu.contextEngineID = pdu.contextEngineID.toString('hex');
  2190. }
  2191. delete pdu.nonRepeaters;
  2192. delete pdu.maxRepetitions;
  2193. return {
  2194. pdu: pdu,
  2195. rinfo: rinfo
  2196. };
  2197. };
  2198. Receiver.prototype.close = function () {
  2199. this.listener.close();
  2200. };
  2201. Receiver.create = function (options, callback) {
  2202. var receiver = new Receiver(options, callback);
  2203. receiver.listener.startListening();
  2204. return receiver;
  2205. };
  2206. var MibNode = function (address, parent) {
  2207. this.address = address;
  2208. this.oid = this.address.join('.');
  2209. ;
  2210. this.parent = parent;
  2211. this.children = {};
  2212. };
  2213. MibNode.prototype.child = function (index) {
  2214. return this.children[index];
  2215. };
  2216. MibNode.prototype.listChildren = function (lowest) {
  2217. var sorted = [];
  2218. lowest = lowest || 0;
  2219. this.children.forEach(function (c, i) {
  2220. if (i >= lowest)
  2221. sorted.push(i);
  2222. });
  2223. sorted.sort(function (a, b) {
  2224. return (a - b);
  2225. });
  2226. return sorted;
  2227. };
  2228. MibNode.prototype.isDescendant = function (address) {
  2229. return MibNode.oidIsDescended(this.address, address);
  2230. };
  2231. MibNode.prototype.isAncestor = function (address) {
  2232. return MibNode.oidIsDescended(address, this.address);
  2233. };
  2234. MibNode.prototype.getAncestorProvider = function () {
  2235. if (this.provider) {
  2236. return this;
  2237. } else if (!this.parent) {
  2238. return null;
  2239. } else {
  2240. return this.parent.getAncestorProvider();
  2241. }
  2242. };
  2243. MibNode.prototype.getInstanceNodeForTableRow = function () {
  2244. var childCount = Object.keys(this.children).length;
  2245. if (childCount == 0) {
  2246. if (this.value) {
  2247. return this;
  2248. } else {
  2249. return null;
  2250. }
  2251. } else if (childCount == 1) {
  2252. return this.children[0].getInstanceNodeForTableRow();
  2253. } else if (childCount > 1) {
  2254. return null;
  2255. }
  2256. };
  2257. MibNode.prototype.getInstanceNodeForTableRowIndex = function (index) {
  2258. var childCount = Object.keys(this.children).length;
  2259. if (childCount == 0) {
  2260. if (this.value) {
  2261. return this;
  2262. } else {
  2263. // not found
  2264. return null;
  2265. }
  2266. } else {
  2267. if (index.length == 0) {
  2268. return this.getInstanceNodeForTableRow();
  2269. } else {
  2270. var nextChildIndexPart = index[0];
  2271. if (!nextChildIndexPart) {
  2272. return null;
  2273. }
  2274. remainingIndex = index.slice(1);
  2275. return this.children[nextChildIndexPart].getInstanceNodeForTableRowIndex(remainingIndex);
  2276. }
  2277. }
  2278. };
  2279. MibNode.prototype.getNextInstanceNode = function () {
  2280. node = this;
  2281. if (this.value) {
  2282. // Need upwards traversal first
  2283. node = this;
  2284. while (node) {
  2285. siblingIndex = node.address.slice(-1)[0];
  2286. node = node.parent;
  2287. if (!node) {
  2288. // end of MIB
  2289. return null;
  2290. } else {
  2291. childrenAddresses = Object.keys(node.children).sort((a, b) => a - b);
  2292. siblingPosition = childrenAddresses.indexOf(siblingIndex.toString());
  2293. if (siblingPosition + 1 < childrenAddresses.length) {
  2294. node = node.children[childrenAddresses[siblingPosition + 1]];
  2295. break;
  2296. }
  2297. }
  2298. }
  2299. }
  2300. // Descent
  2301. while (node) {
  2302. if (node.value) {
  2303. return node;
  2304. }
  2305. childrenAddresses = Object.keys(node.children).sort((a, b) => a - b);
  2306. node = node.children[childrenAddresses[0]];
  2307. if (!node) {
  2308. // unexpected
  2309. return null;
  2310. }
  2311. }
  2312. };
  2313. MibNode.prototype.delete = function () {
  2314. if (Object.keys(this.children) > 0) {
  2315. throw new Error("Cannot delete non-leaf MIB node");
  2316. }
  2317. addressLastPart = this.address.slice(-1)[0];
  2318. delete this.parent.children[addressLastPart];
  2319. this.parent = null;
  2320. };
  2321. MibNode.prototype.pruneUpwards = function () {
  2322. if (!this.parent) {
  2323. return
  2324. }
  2325. if (Object.keys(this.children).length == 0) {
  2326. var lastAddressPart = this.address.splice(-1)[0].toString();
  2327. delete this.parent.children[lastAddressPart];
  2328. this.parent.pruneUpwards();
  2329. this.parent = null;
  2330. }
  2331. }
  2332. MibNode.prototype.dump = function (options) {
  2333. var valueString;
  2334. if ((!options.leavesOnly || options.showProviders) && this.provider) {
  2335. console.log(this.oid + " [" + MibProviderType[this.provider.type] + ": " + this.provider.name + "]");
  2336. } else if ((!options.leavesOnly) || Object.keys(this.children).length == 0) {
  2337. if (this.value) {
  2338. valueString = " = ";
  2339. valueString += options.showTypes ? ObjectType[this.valueType] + ": " : "";
  2340. valueString += options.showValues ? this.value : "";
  2341. } else {
  2342. valueString = "";
  2343. }
  2344. console.log(this.oid + valueString);
  2345. }
  2346. for (node of Object.keys(this.children).sort((a, b) => a - b)) {
  2347. this.children[node].dump(options);
  2348. }
  2349. };
  2350. MibNode.oidIsDescended = function (oid, ancestor) {
  2351. var ancestorAddress = Mib.convertOidToAddress(ancestor);
  2352. var address = Mib.convertOidToAddress(oid);
  2353. var isAncestor = true;
  2354. if (address.length <= ancestorAddress.length) {
  2355. return false;
  2356. }
  2357. ancestorAddress.forEach(function (o, i) {
  2358. if (address[i] !== ancestorAddress[i]) {
  2359. isAncestor = false;
  2360. }
  2361. });
  2362. return isAncestor;
  2363. };
  2364. var Mib = function () {
  2365. this.root = new MibNode([], null);
  2366. this.providers = {};
  2367. this.providerNodes = {};
  2368. };
  2369. Mib.prototype.addNodesForOid = function (oidString) {
  2370. var address = Mib.convertOidToAddress(oidString);
  2371. return this.addNodesForAddress(address);
  2372. };
  2373. Mib.prototype.addNodesForAddress = function (address) {
  2374. var address;
  2375. var node;
  2376. var i;
  2377. node = this.root;
  2378. for (i = 0; i < address.length; i++) {
  2379. if (!node.children.hasOwnProperty(address[i])) {
  2380. node.children[address[i]] = new MibNode(address.slice(0, i + 1), node);
  2381. }
  2382. node = node.children[address[i]];
  2383. }
  2384. return node;
  2385. };
  2386. Mib.prototype.lookup = function (oid) {
  2387. var address;
  2388. var i;
  2389. var node;
  2390. address = Mib.convertOidToAddress(oid);
  2391. node = this.root;
  2392. for (i = 0; i < address.length; i++) {
  2393. if (!node.children.hasOwnProperty(address[i])) {
  2394. return null
  2395. }
  2396. node = node.children[address[i]];
  2397. }
  2398. return node;
  2399. };
  2400. Mib.prototype.getProviderNodeForInstance = function (instanceNode) {
  2401. if (instanceNode.provider) {
  2402. throw new ReferenceError("Instance node has provider which should never happen");
  2403. }
  2404. return instanceNode.getAncestorProvider();
  2405. };
  2406. Mib.prototype.addProviderToNode = function (provider) {
  2407. var node = this.addNodesForOid(provider.oid);
  2408. node.provider = provider;
  2409. if (provider.type == MibProviderType.Table) {
  2410. if (!provider.index) {
  2411. provider.index = [1];
  2412. }
  2413. }
  2414. this.providerNodes[provider.name] = node;
  2415. return node;
  2416. };
  2417. Mib.prototype.registerProvider = function (provider) {
  2418. this.providers[provider.name] = provider;
  2419. };
  2420. Mib.prototype.unregisterProvider = function (name) {
  2421. var providerNode = this.providerNodes[name];
  2422. if (providerNode) {
  2423. providerNodeParent = providerNode.parent;
  2424. providerNode.delete();
  2425. providerNodeParent.pruneUpwards();
  2426. delete this.providerNodes[name];
  2427. }
  2428. delete this.providers[name];
  2429. };
  2430. Mib.prototype.getProvider = function (name) {
  2431. return this.providers[name];
  2432. };
  2433. Mib.prototype.getProviders = function () {
  2434. return this.providers;
  2435. };
  2436. Mib.prototype.getScalarValue = function (scalarName) {
  2437. var providerNode = this.providerNodes[scalarName];
  2438. if (!providerNode || !providerNode.provider || providerNode.provider.type != MibProviderType.Scalar) {
  2439. throw new ReferenceError("Failed to get node for registered MIB provider " + scalarName);
  2440. }
  2441. var instanceAddress = providerNode.address.concat([0]);
  2442. if (!this.lookup(instanceAddress)) {
  2443. throw new Error("Failed created instance node for registered MIB provider " + scalarName);
  2444. }
  2445. var instanceNode = this.lookup(instanceAddress);
  2446. return instanceNode.value;
  2447. };
  2448. Mib.prototype.setScalarValue = function (scalarName, newValue) {
  2449. var providerNode;
  2450. var instanceNode;
  2451. if (!this.providers[scalarName]) {
  2452. throw new ReferenceError("Provider " + scalarName + " not registered with this MIB");
  2453. }
  2454. providerNode = this.providerNodes[scalarName];
  2455. if (!providerNode) {
  2456. providerNode = this.addProviderToNode(this.providers[scalarName]);
  2457. }
  2458. if (!providerNode || !providerNode.provider || providerNode.provider.type != MibProviderType.Scalar) {
  2459. throw new ReferenceError("Could not find MIB node for registered provider " + scalarName);
  2460. }
  2461. var instanceAddress = providerNode.address.concat([0]);
  2462. instanceNode = this.lookup(instanceAddress);
  2463. if (!instanceNode) {
  2464. this.addNodesForAddress(instanceAddress);
  2465. instanceNode = this.lookup(instanceAddress);
  2466. instanceNode.valueType = providerNode.provider.scalarType;
  2467. }
  2468. instanceNode.value = newValue;
  2469. };
  2470. Mib.prototype.getProviderNodeForTable = function (table) {
  2471. var providerNode;
  2472. var provider;
  2473. providerNode = this.providerNodes[table];
  2474. if (!providerNode) {
  2475. throw new ReferenceError("No MIB provider registered for " + table);
  2476. }
  2477. provider = providerNode.provider;
  2478. if (!providerNode) {
  2479. throw new ReferenceError("No MIB provider definition for registered provider " + table);
  2480. }
  2481. if (provider.type != MibProviderType.Table) {
  2482. throw new TypeError("Registered MIB provider " + table +
  2483. " is not of the correct type (is type " + MibProviderType[provider.type] + ")");
  2484. }
  2485. return providerNode;
  2486. };
  2487. Mib.prototype.addTableRow = function (table, row) {
  2488. var providerNode;
  2489. var provider;
  2490. var instance = [];
  2491. var instanceAddress;
  2492. var instanceNode;
  2493. if (this.providers[table] && !this.providerNodes[table]) {
  2494. this.addProviderToNode(this.providers[table]);
  2495. }
  2496. providerNode = this.getProviderNodeForTable(table);
  2497. provider = providerNode.provider;
  2498. for (var indexPart of provider.index) {
  2499. columnPosition = provider.columns.findIndex(column => column.number == indexPart);
  2500. instance.push(row[columnPosition]);
  2501. }
  2502. for (var i = 0; i < providerNode.provider.columns.length; i++) {
  2503. var column = providerNode.provider.columns[i];
  2504. instanceAddress = providerNode.address.concat(column.number).concat(instance);
  2505. this.addNodesForAddress(instanceAddress);
  2506. instanceNode = this.lookup(instanceAddress);
  2507. instanceNode.valueType = column.type;
  2508. instanceNode.value = row[i];
  2509. }
  2510. };
  2511. Mib.prototype.getTableColumnDefinitions = function (table) {
  2512. var providerNode;
  2513. var provider;
  2514. providerNode = this.getProviderNodeForTable(table);
  2515. provider = providerNode.provider;
  2516. return provider.columns;
  2517. };
  2518. Mib.prototype.getTableColumnCells = function (table, columnNumber) {
  2519. providerNode = this.getProviderNodeForTable(table);
  2520. columnNode = providerNode.children[columnNumber];
  2521. column = []
  2522. for (var row of Object.keys(columnNode.children)) {
  2523. instanceNode = columnNode.children[row].getInstanceNodeForTableRow();
  2524. column.push(instanceNode.value);
  2525. }
  2526. return column;
  2527. };
  2528. Mib.prototype.getTableRowCells = function (table, rowIndex) {
  2529. var providerNode;
  2530. var columnNode;
  2531. var instanceNode;
  2532. var row = [];
  2533. providerNode = this.getProviderNodeForTable(table);
  2534. for (var columnNumber of Object.keys(providerNode.children)) {
  2535. columnNode = providerNode.children[columnNumber];
  2536. instanceNode = columnNode.getInstanceNodeForTableRowIndex(rowIndex);
  2537. row.push(instanceNode.value);
  2538. }
  2539. return row;
  2540. };
  2541. Mib.prototype.getTableCells = function (table, byRows) {
  2542. var providerNode;
  2543. var columnNode;
  2544. var data = [];
  2545. providerNode = this.getProviderNodeForTable(table);
  2546. for (var columnNumber of Object.keys(providerNode.children)) {
  2547. columnNode = providerNode.children[columnNumber];
  2548. column = [];
  2549. data.push(column);
  2550. for (var row of Object.keys(columnNode.children)) {
  2551. instanceNode = columnNode.children[row].getInstanceNodeForTableRow();
  2552. column.push(instanceNode.value);
  2553. }
  2554. }
  2555. if (byRows) {
  2556. return Object.keys(data[0]).map(function (c) {
  2557. return data.map(function (r) {
  2558. return r[c];
  2559. });
  2560. });
  2561. } else {
  2562. return data;
  2563. }
  2564. };
  2565. Mib.prototype.getTableSingleCell = function (table, columnNumber, rowIndex) {
  2566. var providerNode;
  2567. var columnNode;
  2568. var instanceNode;
  2569. providerNode = this.getProviderNodeForTable(table);
  2570. columnNode = providerNode.children[columnNumber];
  2571. instanceNode = columnNode.getInstanceNodeForTableRowIndex(rowIndex);
  2572. return instanceNode.value;
  2573. };
  2574. Mib.prototype.setTableSingleCell = function (table, columnNumber, rowIndex, value) {
  2575. var providerNode;
  2576. var columnNode;
  2577. var instanceNode;
  2578. providerNode = this.getProviderNodeForTable(table);
  2579. columnNode = providerNode.children[columnNumber];
  2580. instanceNode = columnNode.getInstanceNodeForTableRowIndex(rowIndex);
  2581. instanceNode.value = value;
  2582. };
  2583. Mib.prototype.deleteTableRow = function (table, rowIndex) {
  2584. var providerNode;
  2585. var columnNode;
  2586. var instanceNode;
  2587. var row = [];
  2588. providerNode = this.getProviderNodeForTable(table);
  2589. for (var columnNumber of Object.keys(providerNode.children)) {
  2590. columnNode = providerNode.children[columnNumber];
  2591. instanceNode = columnNode.getInstanceNodeForTableRowIndex(rowIndex);
  2592. if (instanceNode) {
  2593. instanceParentNode = instanceNode.parent;
  2594. instanceNode.delete();
  2595. instanceParentNode.pruneUpwards();
  2596. } else {
  2597. throw new ReferenceError("Cannot find row for index " + rowIndex + " at registered provider " + table);
  2598. }
  2599. }
  2600. return row;
  2601. };
  2602. Mib.prototype.dump = function (options) {
  2603. if (!options) {
  2604. options = {};
  2605. }
  2606. var completedOptions = {
  2607. leavesOnly: options.leavesOnly || true,
  2608. showProviders: options.leavesOnly || true,
  2609. showValues: options.leavesOnly || true,
  2610. showTypes: options.leavesOnly || true
  2611. };
  2612. this.root.dump(completedOptions);
  2613. };
  2614. Mib.convertOidToAddress = function (oid) {
  2615. var address;
  2616. var oidArray;
  2617. var i;
  2618. if (typeof (oid) === 'object' && util.isArray(oid)) {
  2619. address = oid;
  2620. } else if (typeof (oid) === 'string') {
  2621. address = oid.split('.');
  2622. } else {
  2623. throw new TypeError('oid (string or array) is required');
  2624. }
  2625. if (address.length < 3)
  2626. throw new RangeError('object identifier is too short');
  2627. oidArray = [];
  2628. for (i = 0; i < address.length; i++) {
  2629. var n;
  2630. if (address[i] === '')
  2631. continue;
  2632. if (address[i] === true || address[i] === false) {
  2633. throw new TypeError('object identifier component ' +
  2634. address[i] + ' is malformed');
  2635. }
  2636. n = Number(address[i]);
  2637. if (isNaN(n)) {
  2638. throw new TypeError('object identifier component ' +
  2639. address[i] + ' is malformed');
  2640. }
  2641. if (n % 1 !== 0) {
  2642. throw new TypeError('object identifier component ' +
  2643. address[i] + ' is not an integer');
  2644. }
  2645. if (i === 0 && n > 2) {
  2646. throw new RangeError('object identifier does not ' +
  2647. 'begin with 0, 1, or 2');
  2648. }
  2649. if (i === 1 && n > 39) {
  2650. throw new RangeError('object identifier second ' +
  2651. 'component ' + n + ' exceeds encoding limit of 39');
  2652. }
  2653. if (n < 0) {
  2654. throw new RangeError('object identifier component ' +
  2655. address[i] + ' is negative');
  2656. }
  2657. if (n > MAX_INT32) {
  2658. throw new RangeError('object identifier component ' +
  2659. address[i] + ' is too large');
  2660. }
  2661. oidArray.push(n);
  2662. }
  2663. return oidArray;
  2664. };
  2665. var MibRequest = function (requestDefinition) {
  2666. this.operation = requestDefinition.operation;
  2667. this.address = Mib.convertOidToAddress(requestDefinition.oid);
  2668. this.oid = this.address.join('.');
  2669. this.providerNode = requestDefinition.providerNode;
  2670. this.instanceNode = requestDefinition.instanceNode;
  2671. };
  2672. MibRequest.prototype.isScalar = function () {
  2673. return this.providerNode && this.providerNode.provider &&
  2674. this.providerNode.provider.type == MibProviderType.Scalar;
  2675. };
  2676. MibRequest.prototype.isTabular = function () {
  2677. return this.providerNode && this.providerNode.provider &&
  2678. this.providerNode.provider.type == MibProviderType.Table;
  2679. };
  2680. var Agent = function (options, callback) {
  2681. DEBUG = options.debug;
  2682. this.listener = new Listener(options, this);
  2683. this.engine = new Engine(options.engineID);
  2684. this.authorizer = new Authorizer();
  2685. this.mib = new Mib();
  2686. this.callback = callback || function () {
  2687. };
  2688. this.context = "";
  2689. };
  2690. Agent.prototype.getMib = function () {
  2691. return this.mib;
  2692. };
  2693. Agent.prototype.getAuthorizer = function () {
  2694. return this.authorizer;
  2695. };
  2696. Agent.prototype.registerProvider = function (provider) {
  2697. this.mib.registerProvider(provider);
  2698. };
  2699. Agent.prototype.unregisterProvider = function (provider) {
  2700. this.mib.unregisterProvider(provider);
  2701. };
  2702. Agent.prototype.getProvider = function (provider) {
  2703. return this.mib.getProvider(provider);
  2704. };
  2705. Agent.prototype.getProviders = function () {
  2706. return this.mib.getProviders();
  2707. };
  2708. Agent.prototype.onMsg = function (buffer, rinfo) {
  2709. var message = Listener.processIncoming(buffer, this.authorizer, this.callback);
  2710. var reportMessage;
  2711. var responseMessage;
  2712. if (!message) {
  2713. return;
  2714. }
  2715. // SNMPv3 discovery
  2716. if (message.version == Version3 && message.pdu.type == PduType.GetRequest &&
  2717. !message.hasAuthoritativeEngineID() && message.isReportable()) {
  2718. reportMessage = message.createReportResponseMessage(this.engine, this.context);
  2719. this.listener.send(reportMessage, rinfo);
  2720. return;
  2721. }
  2722. // Request processing
  2723. debug(JSON.stringify(message.pdu, null, 2));
  2724. if (message.pdu.type == PduType.GetRequest) {
  2725. responseMessage = this.request(message, rinfo);
  2726. } else if (message.pdu.type == PduType.SetRequest) {
  2727. responseMessage = this.request(message, rinfo);
  2728. } else if (message.pdu.type == PduType.GetNextRequest) {
  2729. responseMessage = this.getNextRequest(message, rinfo);
  2730. } else if (message.pdu.type == PduType.GetBulkRequest) {
  2731. responseMessage = this.getBulkRequest(message, rinfo);
  2732. } else {
  2733. this.callback(new RequestInvalidError("Unexpected PDU type " +
  2734. message.pdu.type + " (" + PduType[message.pdu.type] + ")"));
  2735. }
  2736. };
  2737. Agent.prototype.request = function (requestMessage, rinfo) {
  2738. var me = this;
  2739. var varbindsCompleted = 0;
  2740. var requestPdu = requestMessage.pdu;
  2741. var varbindsLength = requestPdu.varbinds.length;
  2742. var responsePdu = requestPdu.getResponsePduForRequest();
  2743. for (var i = 0; i < requestPdu.varbinds.length; i++) {
  2744. var requestVarbind = requestPdu.varbinds[i];
  2745. var instanceNode = this.mib.lookup(requestVarbind.oid);
  2746. var providerNode;
  2747. var mibRequest;
  2748. var handler;
  2749. var responseVarbindType;
  2750. if (!instanceNode) {
  2751. mibRequest = new MibRequest({
  2752. operation: requestPdu.type,
  2753. oid: requestVarbind.oid
  2754. });
  2755. handler = function getNsoHandler(mibRequestForNso) {
  2756. mibRequestForNso.done({
  2757. errorStatus: ErrorStatus.NoSuchName,
  2758. errorIndex: i
  2759. });
  2760. };
  2761. } else {
  2762. providerNode = this.mib.getProviderNodeForInstance(instanceNode);
  2763. mibRequest = new MibRequest({
  2764. operation: requestPdu.type,
  2765. providerNode: providerNode,
  2766. instanceNode: instanceNode,
  2767. oid: requestVarbind.oid
  2768. });
  2769. handler = providerNode.provider.handler;
  2770. }
  2771. mibRequest.done = function (error) {
  2772. if (error) {
  2773. responsePdu.errorStatus = error.errorStatus;
  2774. responsePdu.errorIndex = error.errorIndex;
  2775. responseVarbind = {
  2776. oid: mibRequest.oid,
  2777. type: ObjectType.Null,
  2778. value: null
  2779. };
  2780. } else {
  2781. if (requestPdu.type == PduType.SetRequest) {
  2782. mibRequest.instanceNode.value = requestVarbind.value;
  2783. }
  2784. if (requestPdu.type == PduType.GetNextRequest && requestVarbind.type == ObjectType.EndOfMibView) {
  2785. responseVarbindType = ObjectType.EndOfMibView;
  2786. } else {
  2787. responseVarbindType = mibRequest.instanceNode.valueType;
  2788. }
  2789. responseVarbind = {
  2790. oid: mibRequest.oid,
  2791. type: responseVarbindType,
  2792. value: mibRequest.instanceNode.value
  2793. };
  2794. }
  2795. me.setSingleVarbind(responsePdu, i, responseVarbind);
  2796. if (++varbindsCompleted == varbindsLength) {
  2797. me.sendResponse.call(me, rinfo, requestMessage, responsePdu);
  2798. }
  2799. };
  2800. if (handler) {
  2801. handler(mibRequest);
  2802. } else {
  2803. mibRequest.done();
  2804. }
  2805. }
  2806. ;
  2807. };
  2808. Agent.prototype.addGetNextVarbind = function (targetVarbinds, startOid) {
  2809. var startNode = this.mib.lookup(startOid);
  2810. var getNextNode;
  2811. if (!startNode) {
  2812. // Off-tree start specified
  2813. targetVarbinds.push({
  2814. oid: requestVarbind.oid,
  2815. type: ObjectType.Null,
  2816. value: null
  2817. });
  2818. } else {
  2819. getNextNode = startNode.getNextInstanceNode();
  2820. if (!getNextNode) {
  2821. // End of MIB
  2822. targetVarbinds.push({
  2823. oid: requestVarbind.oid,
  2824. type: ObjectType.EndOfMibView,
  2825. value: null
  2826. });
  2827. } else {
  2828. // Normal response
  2829. targetVarbinds.push({
  2830. oid: getNextNode.oid,
  2831. type: getNextNode.valueType,
  2832. value: getNextNode.value
  2833. });
  2834. }
  2835. }
  2836. return getNextNode;
  2837. };
  2838. Agent.prototype.getNextRequest = function (requestMessage, rinfo) {
  2839. var requestPdu = requestMessage.pdu;
  2840. var varbindsLength = requestPdu.varbinds.length;
  2841. var getNextVarbinds = [];
  2842. for (var i = 0; i < varbindsLength; i++) {
  2843. this.addGetNextVarbind(getNextVarbinds, requestPdu.varbinds[i].oid);
  2844. }
  2845. requestMessage.pdu.varbinds = getNextVarbinds;
  2846. this.request(requestMessage, rinfo);
  2847. };
  2848. Agent.prototype.getBulkRequest = function (requestMessage, rinfo) {
  2849. var requestPdu = requestMessage.pdu;
  2850. var requestVarbinds = requestPdu.varbinds;
  2851. var getBulkVarbinds = [];
  2852. var startOid = [];
  2853. var getNextNode;
  2854. for (var n = 0; n < requestPdu.nonRepeaters; n++) {
  2855. this.addGetNextVarbind(getBulkVarbinds, requestVarbinds[n].oid);
  2856. }
  2857. for (var v = requestPdu.nonRepeaters; v < requestVarbinds.length; v++) {
  2858. startOid.push(requestVarbinds[v].oid);
  2859. }
  2860. for (var r = 0; r < requestPdu.maxRepetitions; r++) {
  2861. for (var v = requestPdu.nonRepeaters; v < requestVarbinds.length; v++) {
  2862. getNextNode = this.addGetNextVarbind(getBulkVarbinds, startOid[v - requestPdu.nonRepeaters]);
  2863. if (getNextNode) {
  2864. startOid[v - requestPdu.nonRepeaters] = getNextNode.oid;
  2865. }
  2866. }
  2867. }
  2868. requestMessage.pdu.varbinds = getBulkVarbinds;
  2869. this.request(requestMessage, rinfo);
  2870. };
  2871. Agent.prototype.setSingleVarbind = function (responsePdu, index, responseVarbind) {
  2872. responsePdu.varbinds[index] = responseVarbind;
  2873. };
  2874. Agent.prototype.sendResponse = function (rinfo, requestMessage, responsePdu) {
  2875. var responseMessage = requestMessage.createResponseForRequest(responsePdu);
  2876. this.listener.send(responseMessage, rinfo);
  2877. this.callback(null, Listener.formatCallbackData(responseMessage.pdu, rinfo));
  2878. };
  2879. Agent.create = function (options, callback) {
  2880. var agent = new Agent(options, callback);
  2881. agent.listener.startListening();
  2882. return agent;
  2883. };
  2884. /*****************************************************************************
  2885. ** Exports
  2886. **/
  2887. exports.Session = Session;
  2888. exports.createSession = function (target, community, options) {
  2889. if (options.version && !(options.version == Version1 || options.version == Version2c)) {
  2890. throw new ResponseInvalidError("SNMP community session requested but version '" + options.version + "' specified in options not valid");
  2891. } else {
  2892. return new Session(target, community, options);
  2893. }
  2894. };
  2895. exports.createV3Session = function (target, user, options) {
  2896. if (options.version && options.version != Version3) {
  2897. throw new ResponseInvalidError("SNMPv3 session requested but version '" + options.version + "' specified in options");
  2898. } else {
  2899. options.version = Version3;
  2900. }
  2901. return new Session(target, user, options);
  2902. };
  2903. exports.createReceiver = Receiver.create;
  2904. exports.createAgent = Agent.create;
  2905. exports.isVarbindError = isVarbindError;
  2906. exports.varbindError = varbindError;
  2907. exports.Version1 = Version1;
  2908. exports.Version2c = Version2c;
  2909. exports.Version3 = Version3;
  2910. exports.Version = Version;
  2911. exports.ErrorStatus = ErrorStatus;
  2912. exports.TrapType = TrapType;
  2913. exports.ObjectType = ObjectType;
  2914. exports.PduType = PduType;
  2915. exports.MibProviderType = MibProviderType;
  2916. exports.SecurityLevel = SecurityLevel;
  2917. exports.AuthProtocols = AuthProtocols;
  2918. exports.PrivProtocols = PrivProtocols;
  2919. exports.ResponseInvalidError = ResponseInvalidError;
  2920. exports.RequestInvalidError = RequestInvalidError;
  2921. exports.RequestFailedError = RequestFailedError;
  2922. exports.RequestTimedOutError = RequestTimedOutError;
  2923. /**
  2924. ** We've added this for testing.
  2925. **/
  2926. exports.ObjectParser = {
  2927. readInt: readInt,
  2928. readUint: readUint
  2929. };
  2930. exports.Authentication = Authentication;
  2931. exports.Encryption = Encryption;