snmp.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  16. | Mike Jackson <mhjack@tscnet.com> |
  17. | Steven Lawrance <slawrance@technologist.com> |
  18. | Harrie Hazewinkel <harrie@lisanza.net> |
  19. | Johann Hanne <jonny@nurfuerspam.de> |
  20. | Boris Lytockin <lytboris@gmail.com> |
  21. +----------------------------------------------------------------------+
  22. */
  23. /* $Id$ */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "php.h"
  28. #include "main/php_network.h"
  29. #include "ext/standard/info.h"
  30. #include "php_snmp.h"
  31. #include "zend_exceptions.h"
  32. #if HAVE_SPL
  33. #include "ext/spl/spl_exceptions.h"
  34. #endif
  35. #if HAVE_SNMP
  36. #include <sys/types.h>
  37. #ifdef PHP_WIN32
  38. #include <winsock2.h>
  39. #include <errno.h>
  40. #include <process.h>
  41. #include "win32/time.h"
  42. #elif defined(NETWARE)
  43. #ifdef USE_WINSOCK
  44. #include <novsock2.h>
  45. #else
  46. #include <sys/socket.h>
  47. #endif
  48. #include <errno.h>
  49. #include <sys/timeval.h>
  50. #else
  51. #include <sys/socket.h>
  52. #include <netinet/in.h>
  53. #include <arpa/inet.h>
  54. #ifndef _OSD_POSIX
  55. #include <sys/errno.h>
  56. #else
  57. #include <errno.h> /* BS2000/OSD uses <errno.h>, not <sys/errno.h> */
  58. #endif
  59. #include <netdb.h>
  60. #endif
  61. #ifdef HAVE_UNISTD_H
  62. #include <unistd.h>
  63. #endif
  64. #ifndef __P
  65. #ifdef __GNUC__
  66. #define __P(args) args
  67. #else
  68. #define __P(args) ()
  69. #endif
  70. #endif
  71. #include <net-snmp/net-snmp-config.h>
  72. #include <net-snmp/net-snmp-includes.h>
  73. /* For net-snmp prior to 5.4 */
  74. #ifndef HAVE_SHUTDOWN_SNMP_LOGGING
  75. extern netsnmp_log_handler *logh_head;
  76. #define shutdown_snmp_logging() \
  77. { \
  78. snmp_disable_log(); \
  79. while(NULL != logh_head) \
  80. netsnmp_remove_loghandler( logh_head ); \
  81. }
  82. #endif
  83. #define SNMP_VALUE_LIBRARY (0 << 0)
  84. #define SNMP_VALUE_PLAIN (1 << 0)
  85. #define SNMP_VALUE_OBJECT (1 << 1)
  86. typedef struct snmp_session php_snmp_session;
  87. #define PHP_SNMP_SESSION_RES_NAME "SNMP session"
  88. #define PHP_SNMP_ADD_PROPERTIES(a, b) \
  89. { \
  90. int i = 0; \
  91. while (b[i].name != NULL) { \
  92. php_snmp_add_property((a), (b)[i].name, (b)[i].name_length, \
  93. (php_snmp_read_t)(b)[i].read_func, (php_snmp_write_t)(b)[i].write_func TSRMLS_CC); \
  94. i++; \
  95. } \
  96. }
  97. #define PHP_SNMP_ERRNO_NOERROR 0
  98. #define PHP_SNMP_ERRNO_GENERIC (1 << 1)
  99. #define PHP_SNMP_ERRNO_TIMEOUT (1 << 2)
  100. #define PHP_SNMP_ERRNO_ERROR_IN_REPLY (1 << 3)
  101. #define PHP_SNMP_ERRNO_OID_NOT_INCREASING (1 << 4)
  102. #define PHP_SNMP_ERRNO_OID_PARSING_ERROR (1 << 5)
  103. #define PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES (1 << 6)
  104. #define PHP_SNMP_ERRNO_ANY ( \
  105. PHP_SNMP_ERRNO_GENERIC | \
  106. PHP_SNMP_ERRNO_TIMEOUT | \
  107. PHP_SNMP_ERRNO_ERROR_IN_REPLY | \
  108. PHP_SNMP_ERRNO_OID_NOT_INCREASING | \
  109. PHP_SNMP_ERRNO_OID_PARSING_ERROR | \
  110. PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES | \
  111. PHP_SNMP_ERRNO_NOERROR \
  112. )
  113. ZEND_DECLARE_MODULE_GLOBALS(snmp)
  114. static PHP_GINIT_FUNCTION(snmp);
  115. /* constant - can be shared among threads */
  116. static oid objid_mib[] = {1, 3, 6, 1, 2, 1};
  117. static int le_snmp_session;
  118. /* Handlers */
  119. static zend_object_handlers php_snmp_object_handlers;
  120. /* Class entries */
  121. zend_class_entry *php_snmp_ce;
  122. zend_class_entry *php_snmp_exception_ce;
  123. /* Class object properties */
  124. static HashTable php_snmp_properties;
  125. /* {{{ arginfo */
  126. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpget, 0, 0, 3)
  127. ZEND_ARG_INFO(0, host)
  128. ZEND_ARG_INFO(0, community)
  129. ZEND_ARG_INFO(0, object_id)
  130. ZEND_ARG_INFO(0, timeout)
  131. ZEND_ARG_INFO(0, retries)
  132. ZEND_END_ARG_INFO()
  133. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpgetnext, 0, 0, 3)
  134. ZEND_ARG_INFO(0, host)
  135. ZEND_ARG_INFO(0, community)
  136. ZEND_ARG_INFO(0, object_id)
  137. ZEND_ARG_INFO(0, timeout)
  138. ZEND_ARG_INFO(0, retries)
  139. ZEND_END_ARG_INFO()
  140. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpwalk, 0, 0, 3)
  141. ZEND_ARG_INFO(0, host)
  142. ZEND_ARG_INFO(0, community)
  143. ZEND_ARG_INFO(0, object_id)
  144. ZEND_ARG_INFO(0, timeout)
  145. ZEND_ARG_INFO(0, retries)
  146. ZEND_END_ARG_INFO()
  147. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmprealwalk, 0, 0, 3)
  148. ZEND_ARG_INFO(0, host)
  149. ZEND_ARG_INFO(0, community)
  150. ZEND_ARG_INFO(0, object_id)
  151. ZEND_ARG_INFO(0, timeout)
  152. ZEND_ARG_INFO(0, retries)
  153. ZEND_END_ARG_INFO()
  154. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpset, 0, 0, 5)
  155. ZEND_ARG_INFO(0, host)
  156. ZEND_ARG_INFO(0, community)
  157. ZEND_ARG_INFO(0, object_id)
  158. ZEND_ARG_INFO(0, type)
  159. ZEND_ARG_INFO(0, value)
  160. ZEND_ARG_INFO(0, timeout)
  161. ZEND_ARG_INFO(0, retries)
  162. ZEND_END_ARG_INFO()
  163. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get_quick_print, 0, 0, 1)
  164. ZEND_ARG_INFO(0, d)
  165. ZEND_END_ARG_INFO()
  166. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_quick_print, 0, 0, 1)
  167. ZEND_ARG_INFO(0, quick_print)
  168. ZEND_END_ARG_INFO()
  169. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_enum_print, 0, 0, 1)
  170. ZEND_ARG_INFO(0, enum_print)
  171. ZEND_END_ARG_INFO()
  172. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_oid_output_format, 0, 0, 1)
  173. ZEND_ARG_INFO(0, oid_format)
  174. ZEND_END_ARG_INFO()
  175. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_get, 0, 0, 3)
  176. ZEND_ARG_INFO(0, host)
  177. ZEND_ARG_INFO(0, community)
  178. ZEND_ARG_INFO(0, object_id)
  179. ZEND_ARG_INFO(0, timeout)
  180. ZEND_ARG_INFO(0, retries)
  181. ZEND_END_ARG_INFO()
  182. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_getnext, 0, 0, 3)
  183. ZEND_ARG_INFO(0, host)
  184. ZEND_ARG_INFO(0, community)
  185. ZEND_ARG_INFO(0, object_id)
  186. ZEND_ARG_INFO(0, timeout)
  187. ZEND_ARG_INFO(0, retries)
  188. ZEND_END_ARG_INFO()
  189. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_walk, 0, 0, 3)
  190. ZEND_ARG_INFO(0, host)
  191. ZEND_ARG_INFO(0, community)
  192. ZEND_ARG_INFO(0, object_id)
  193. ZEND_ARG_INFO(0, timeout)
  194. ZEND_ARG_INFO(0, retries)
  195. ZEND_END_ARG_INFO()
  196. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_real_walk, 0, 0, 3)
  197. ZEND_ARG_INFO(0, host)
  198. ZEND_ARG_INFO(0, community)
  199. ZEND_ARG_INFO(0, object_id)
  200. ZEND_ARG_INFO(0, timeout)
  201. ZEND_ARG_INFO(0, retries)
  202. ZEND_END_ARG_INFO()
  203. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_set, 0, 0, 5)
  204. ZEND_ARG_INFO(0, host)
  205. ZEND_ARG_INFO(0, community)
  206. ZEND_ARG_INFO(0, object_id)
  207. ZEND_ARG_INFO(0, type)
  208. ZEND_ARG_INFO(0, value)
  209. ZEND_ARG_INFO(0, timeout)
  210. ZEND_ARG_INFO(0, retries)
  211. ZEND_END_ARG_INFO()
  212. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_get, 0, 0, 8)
  213. ZEND_ARG_INFO(0, host)
  214. ZEND_ARG_INFO(0, sec_name)
  215. ZEND_ARG_INFO(0, sec_level)
  216. ZEND_ARG_INFO(0, auth_protocol)
  217. ZEND_ARG_INFO(0, auth_passphrase)
  218. ZEND_ARG_INFO(0, priv_protocol)
  219. ZEND_ARG_INFO(0, priv_passphrase)
  220. ZEND_ARG_INFO(0, object_id)
  221. ZEND_ARG_INFO(0, timeout)
  222. ZEND_ARG_INFO(0, retries)
  223. ZEND_END_ARG_INFO()
  224. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_getnext, 0, 0, 8)
  225. ZEND_ARG_INFO(0, host)
  226. ZEND_ARG_INFO(0, sec_name)
  227. ZEND_ARG_INFO(0, sec_level)
  228. ZEND_ARG_INFO(0, auth_protocol)
  229. ZEND_ARG_INFO(0, auth_passphrase)
  230. ZEND_ARG_INFO(0, priv_protocol)
  231. ZEND_ARG_INFO(0, priv_passphrase)
  232. ZEND_ARG_INFO(0, object_id)
  233. ZEND_ARG_INFO(0, timeout)
  234. ZEND_ARG_INFO(0, retries)
  235. ZEND_END_ARG_INFO()
  236. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_walk, 0, 0, 8)
  237. ZEND_ARG_INFO(0, host)
  238. ZEND_ARG_INFO(0, sec_name)
  239. ZEND_ARG_INFO(0, sec_level)
  240. ZEND_ARG_INFO(0, auth_protocol)
  241. ZEND_ARG_INFO(0, auth_passphrase)
  242. ZEND_ARG_INFO(0, priv_protocol)
  243. ZEND_ARG_INFO(0, priv_passphrase)
  244. ZEND_ARG_INFO(0, object_id)
  245. ZEND_ARG_INFO(0, timeout)
  246. ZEND_ARG_INFO(0, retries)
  247. ZEND_END_ARG_INFO()
  248. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_real_walk, 0, 0, 8)
  249. ZEND_ARG_INFO(0, host)
  250. ZEND_ARG_INFO(0, sec_name)
  251. ZEND_ARG_INFO(0, sec_level)
  252. ZEND_ARG_INFO(0, auth_protocol)
  253. ZEND_ARG_INFO(0, auth_passphrase)
  254. ZEND_ARG_INFO(0, priv_protocol)
  255. ZEND_ARG_INFO(0, priv_passphrase)
  256. ZEND_ARG_INFO(0, object_id)
  257. ZEND_ARG_INFO(0, timeout)
  258. ZEND_ARG_INFO(0, retries)
  259. ZEND_END_ARG_INFO()
  260. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_set, 0, 0, 10)
  261. ZEND_ARG_INFO(0, host)
  262. ZEND_ARG_INFO(0, sec_name)
  263. ZEND_ARG_INFO(0, sec_level)
  264. ZEND_ARG_INFO(0, auth_protocol)
  265. ZEND_ARG_INFO(0, auth_passphrase)
  266. ZEND_ARG_INFO(0, priv_protocol)
  267. ZEND_ARG_INFO(0, priv_passphrase)
  268. ZEND_ARG_INFO(0, object_id)
  269. ZEND_ARG_INFO(0, type)
  270. ZEND_ARG_INFO(0, value)
  271. ZEND_ARG_INFO(0, timeout)
  272. ZEND_ARG_INFO(0, retries)
  273. ZEND_END_ARG_INFO()
  274. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_valueretrieval, 0, 0, 1)
  275. ZEND_ARG_INFO(0, method)
  276. ZEND_END_ARG_INFO()
  277. ZEND_BEGIN_ARG_INFO(arginfo_snmp_get_valueretrieval, 0)
  278. ZEND_END_ARG_INFO()
  279. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_read_mib, 0, 0, 1)
  280. ZEND_ARG_INFO(0, filename)
  281. ZEND_END_ARG_INFO()
  282. /* OO arginfo */
  283. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_create, 0, 0, 3)
  284. ZEND_ARG_INFO(0, version)
  285. ZEND_ARG_INFO(0, host)
  286. ZEND_ARG_INFO(0, community)
  287. ZEND_ARG_INFO(0, timeout)
  288. ZEND_ARG_INFO(0, retries)
  289. ZEND_END_ARG_INFO()
  290. ZEND_BEGIN_ARG_INFO(arginfo_snmp_void, 0)
  291. ZEND_END_ARG_INFO()
  292. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_setSecurity, 0, 0, 8)
  293. ZEND_ARG_INFO(0, sec_level)
  294. ZEND_ARG_INFO(0, auth_protocol)
  295. ZEND_ARG_INFO(0, auth_passphrase)
  296. ZEND_ARG_INFO(0, priv_protocol)
  297. ZEND_ARG_INFO(0, priv_passphrase)
  298. ZEND_ARG_INFO(0, contextName)
  299. ZEND_ARG_INFO(0, contextEngineID)
  300. ZEND_END_ARG_INFO()
  301. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get, 0, 0, 1)
  302. ZEND_ARG_INFO(0, object_id)
  303. ZEND_ARG_INFO(0, use_orignames)
  304. ZEND_END_ARG_INFO()
  305. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_walk, 0, 0, 4)
  306. ZEND_ARG_INFO(0, object_id)
  307. ZEND_ARG_INFO(0, suffix_keys)
  308. ZEND_ARG_INFO(0, max_repetitions)
  309. ZEND_ARG_INFO(0, non_repeaters)
  310. ZEND_END_ARG_INFO()
  311. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set, 0, 0, 3)
  312. ZEND_ARG_INFO(0, object_id)
  313. ZEND_ARG_INFO(0, type)
  314. ZEND_ARG_INFO(0, value)
  315. ZEND_END_ARG_INFO()
  316. ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_class_set_quick_print, 0, 0, 1)
  317. ZEND_ARG_INFO(0, quick_print)
  318. ZEND_END_ARG_INFO()
  319. /* }}} */
  320. struct objid_query {
  321. int count;
  322. int offset;
  323. int step;
  324. long non_repeaters;
  325. long max_repetitions;
  326. int valueretrieval;
  327. int array_output;
  328. int oid_increasing_check;
  329. snmpobjarg *vars;
  330. };
  331. /* {{{ snmp_functions[]
  332. */
  333. const zend_function_entry snmp_functions[] = {
  334. PHP_FE(snmpget, arginfo_snmpget)
  335. PHP_FE(snmpgetnext, arginfo_snmpgetnext)
  336. PHP_FE(snmpwalk, arginfo_snmpwalk)
  337. PHP_FE(snmprealwalk, arginfo_snmprealwalk)
  338. PHP_FALIAS(snmpwalkoid, snmprealwalk, arginfo_snmprealwalk)
  339. PHP_FE(snmpset, arginfo_snmpset)
  340. PHP_FE(snmp_get_quick_print, arginfo_snmp_get_quick_print)
  341. PHP_FE(snmp_set_quick_print, arginfo_snmp_set_quick_print)
  342. PHP_FE(snmp_set_enum_print, arginfo_snmp_set_enum_print)
  343. PHP_FE(snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
  344. PHP_FALIAS(snmp_set_oid_numeric_print, snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
  345. PHP_FE(snmp2_get, arginfo_snmp2_get)
  346. PHP_FE(snmp2_getnext, arginfo_snmp2_getnext)
  347. PHP_FE(snmp2_walk, arginfo_snmp2_walk)
  348. PHP_FE(snmp2_real_walk, arginfo_snmp2_real_walk)
  349. PHP_FE(snmp2_set, arginfo_snmp2_set)
  350. PHP_FE(snmp3_get, arginfo_snmp3_get)
  351. PHP_FE(snmp3_getnext, arginfo_snmp3_getnext)
  352. PHP_FE(snmp3_walk, arginfo_snmp3_walk)
  353. PHP_FE(snmp3_real_walk, arginfo_snmp3_real_walk)
  354. PHP_FE(snmp3_set, arginfo_snmp3_set)
  355. PHP_FE(snmp_set_valueretrieval, arginfo_snmp_set_valueretrieval)
  356. PHP_FE(snmp_get_valueretrieval, arginfo_snmp_get_valueretrieval)
  357. PHP_FE(snmp_read_mib, arginfo_snmp_read_mib)
  358. PHP_FE_END
  359. };
  360. /* }}} */
  361. /* query an agent with GET method */
  362. #define SNMP_CMD_GET (1<<0)
  363. /* query an agent with GETNEXT method */
  364. #define SNMP_CMD_GETNEXT (1<<1)
  365. /* query an agent with SET method */
  366. #define SNMP_CMD_SET (1<<2)
  367. /* walk the mib */
  368. #define SNMP_CMD_WALK (1<<3)
  369. /* force values-only output */
  370. #define SNMP_NUMERIC_KEYS (1<<7)
  371. /* use user-supplied OID names for keys in array output mode in GET method */
  372. #define SNMP_ORIGINAL_NAMES_AS_KEYS (1<<8)
  373. /* use OID suffix (`index') for keys in array output mode in WALK method */
  374. #define SNMP_USE_SUFFIX_AS_KEYS (1<<9)
  375. #ifdef COMPILE_DL_SNMP
  376. ZEND_GET_MODULE(snmp)
  377. #endif
  378. /* THREAD_LS snmp_module php_snmp_module; - may need one of these at some point */
  379. /* {{{ PHP_GINIT_FUNCTION
  380. */
  381. static PHP_GINIT_FUNCTION(snmp)
  382. {
  383. snmp_globals->valueretrieval = SNMP_VALUE_LIBRARY;
  384. }
  385. /* }}} */
  386. #define PHP_SNMP_SESSION_FREE(a) { \
  387. if ((*session)->a) { \
  388. efree((*session)->a); \
  389. (*session)->a = NULL; \
  390. } \
  391. }
  392. static void netsnmp_session_free(php_snmp_session **session)
  393. {
  394. if (*session) {
  395. PHP_SNMP_SESSION_FREE(peername);
  396. PHP_SNMP_SESSION_FREE(community);
  397. PHP_SNMP_SESSION_FREE(securityName);
  398. PHP_SNMP_SESSION_FREE(contextEngineID);
  399. efree(*session);
  400. *session = NULL;
  401. }
  402. }
  403. static void php_snmp_session_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  404. {
  405. php_snmp_session *session = (php_snmp_session *)rsrc->ptr;
  406. netsnmp_session_free(&session);
  407. }
  408. static void php_snmp_object_free_storage(void *object TSRMLS_DC)
  409. {
  410. php_snmp_object *intern = (php_snmp_object *)object;
  411. if (!intern) {
  412. return;
  413. }
  414. netsnmp_session_free(&(intern->session));
  415. zend_object_std_dtor(&intern->zo TSRMLS_CC);
  416. efree(intern);
  417. }
  418. static zend_object_value php_snmp_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
  419. {
  420. zend_object_value retval;
  421. php_snmp_object *intern;
  422. /* Allocate memory for it */
  423. intern = emalloc(sizeof(php_snmp_object));
  424. memset(&intern->zo, 0, sizeof(php_snmp_object));
  425. zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
  426. object_properties_init(&intern->zo, class_type);
  427. retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) php_snmp_object_free_storage, NULL TSRMLS_CC);
  428. retval.handlers = (zend_object_handlers *) &php_snmp_object_handlers;
  429. return retval;
  430. }
  431. /* {{{ php_snmp_error
  432. *
  433. * Record last SNMP-related error in object
  434. *
  435. */
  436. static void php_snmp_error(zval *object, const char *docref TSRMLS_DC, int type, const char *format, ...)
  437. {
  438. va_list args;
  439. php_snmp_object *snmp_object = NULL;
  440. if (object) {
  441. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  442. if (type == PHP_SNMP_ERRNO_NOERROR) {
  443. memset(snmp_object->snmp_errstr, 0, sizeof(snmp_object->snmp_errstr));
  444. } else {
  445. va_start(args, format);
  446. vsnprintf(snmp_object->snmp_errstr, sizeof(snmp_object->snmp_errstr) - 1, format, args);
  447. va_end(args);
  448. }
  449. snmp_object->snmp_errno = type;
  450. }
  451. if (type == PHP_SNMP_ERRNO_NOERROR) {
  452. return;
  453. }
  454. if (object && (snmp_object->exceptions_enabled & type)) {
  455. zend_throw_exception_ex(php_snmp_exception_ce, type TSRMLS_CC, "%s", snmp_object->snmp_errstr);
  456. } else {
  457. va_start(args, format);
  458. php_verror(docref, "", E_WARNING, format, args TSRMLS_CC);
  459. va_end(args);
  460. }
  461. }
  462. /* }}} */
  463. /* {{{ php_snmp_getvalue
  464. *
  465. * SNMP value to zval converter
  466. *
  467. */
  468. static void php_snmp_getvalue(struct variable_list *vars, zval *snmpval TSRMLS_DC, int valueretrieval)
  469. {
  470. zval *val;
  471. char sbuf[512];
  472. char *buf = &(sbuf[0]);
  473. char *dbuf = (char *)NULL;
  474. int buflen = sizeof(sbuf) - 1;
  475. int val_len = vars->val_len;
  476. /* use emalloc() for large values, use static array otherwize */
  477. /* There is no way to know the size of buffer snprint_value() needs in order to print a value there.
  478. * So we are forced to probe it
  479. */
  480. while ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
  481. *buf = '\0';
  482. if (snprint_value(buf, buflen, vars->name, vars->name_length, vars) == -1) {
  483. if (val_len > 512*1024) {
  484. php_error_docref(NULL TSRMLS_CC, E_WARNING, "snprint_value() asks for a buffer more than 512k, Net-SNMP bug?");
  485. break;
  486. }
  487. /* buffer is not long enough to hold full output, double it */
  488. val_len *= 2;
  489. } else {
  490. break;
  491. }
  492. if (buf == dbuf) {
  493. dbuf = (char *)erealloc(dbuf, val_len + 1);
  494. } else {
  495. dbuf = (char *)emalloc(val_len + 1);
  496. }
  497. if (!dbuf) {
  498. php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed: %s, fallback to static buffer", strerror(errno));
  499. buf = &(sbuf[0]);
  500. buflen = sizeof(sbuf) - 1;
  501. break;
  502. }
  503. buf = dbuf;
  504. buflen = val_len;
  505. }
  506. if((valueretrieval & SNMP_VALUE_PLAIN) && val_len > buflen){
  507. if ((dbuf = (char *)emalloc(val_len + 1))) {
  508. buf = dbuf;
  509. buflen = val_len;
  510. } else {
  511. php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed: %s, fallback to static buffer", strerror(errno));
  512. }
  513. }
  514. MAKE_STD_ZVAL(val);
  515. if (valueretrieval & SNMP_VALUE_PLAIN) {
  516. *buf = 0;
  517. switch (vars->type) {
  518. case ASN_BIT_STR: /* 0x03, asn1.h */
  519. ZVAL_STRINGL(val, (char *)vars->val.bitstring, vars->val_len, 1);
  520. break;
  521. case ASN_OCTET_STR: /* 0x04, asn1.h */
  522. case ASN_OPAQUE: /* 0x44, snmp_impl.h */
  523. ZVAL_STRINGL(val, (char *)vars->val.string, vars->val_len, 1);
  524. break;
  525. case ASN_NULL: /* 0x05, asn1.h */
  526. ZVAL_NULL(val);
  527. break;
  528. case ASN_OBJECT_ID: /* 0x06, asn1.h */
  529. snprint_objid(buf, buflen, vars->val.objid, vars->val_len / sizeof(oid));
  530. ZVAL_STRING(val, buf, 1);
  531. break;
  532. case ASN_IPADDRESS: /* 0x40, snmp_impl.h */
  533. snprintf(buf, buflen, "%d.%d.%d.%d",
  534. (vars->val.string)[0], (vars->val.string)[1],
  535. (vars->val.string)[2], (vars->val.string)[3]);
  536. buf[buflen]=0;
  537. ZVAL_STRING(val, buf, 1);
  538. break;
  539. case ASN_COUNTER: /* 0x41, snmp_impl.h */
  540. case ASN_GAUGE: /* 0x42, snmp_impl.h */
  541. /* ASN_UNSIGNED is the same as ASN_GAUGE */
  542. case ASN_TIMETICKS: /* 0x43, snmp_impl.h */
  543. case ASN_UINTEGER: /* 0x47, snmp_impl.h */
  544. snprintf(buf, buflen, "%lu", *vars->val.integer);
  545. buf[buflen]=0;
  546. ZVAL_STRING(val, buf, 1);
  547. break;
  548. case ASN_INTEGER: /* 0x02, asn1.h */
  549. snprintf(buf, buflen, "%ld", *vars->val.integer);
  550. buf[buflen]=0;
  551. ZVAL_STRING(val, buf, 1);
  552. break;
  553. #if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
  554. case ASN_OPAQUE_FLOAT: /* 0x78, asn1.h */
  555. snprintf(buf, buflen, "%f", *vars->val.floatVal);
  556. ZVAL_STRING(val, buf, 1);
  557. break;
  558. case ASN_OPAQUE_DOUBLE: /* 0x79, asn1.h */
  559. snprintf(buf, buflen, "%Lf", *vars->val.doubleVal);
  560. ZVAL_STRING(val, buf, 1);
  561. break;
  562. case ASN_OPAQUE_I64: /* 0x80, asn1.h */
  563. printI64(buf, vars->val.counter64);
  564. ZVAL_STRING(val, buf, 1);
  565. break;
  566. case ASN_OPAQUE_U64: /* 0x81, asn1.h */
  567. #endif
  568. case ASN_COUNTER64: /* 0x46, snmp_impl.h */
  569. printU64(buf, vars->val.counter64);
  570. ZVAL_STRING(val, buf, 1);
  571. break;
  572. default:
  573. ZVAL_STRING(val, "Unknown value type", 1);
  574. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown value type: %u", vars->type);
  575. break;
  576. }
  577. } else /* use Net-SNMP value translation */ {
  578. /* we have desired string in buffer, just use it */
  579. ZVAL_STRING(val, buf, 1);
  580. }
  581. if (valueretrieval & SNMP_VALUE_OBJECT) {
  582. object_init(snmpval);
  583. add_property_long(snmpval, "type", vars->type);
  584. add_property_zval(snmpval, "value", val);
  585. } else {
  586. *snmpval = *val;
  587. zval_copy_ctor(snmpval);
  588. }
  589. zval_ptr_dtor(&val);
  590. if(dbuf){ /* malloc was used to store value */
  591. efree(dbuf);
  592. }
  593. }
  594. /* }}} */
  595. /* {{{ php_snmp_internal
  596. *
  597. * SNMP object fetcher/setter for all SNMP versions
  598. *
  599. */
  600. static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
  601. struct snmp_session *session,
  602. struct objid_query *objid_query)
  603. {
  604. struct snmp_session *ss;
  605. struct snmp_pdu *pdu=NULL, *response;
  606. struct variable_list *vars;
  607. oid root[MAX_NAME_LEN];
  608. size_t rootlen = 0;
  609. int status, count, found;
  610. char buf[2048];
  611. char buf2[2048];
  612. int keepwalking=1;
  613. char *err;
  614. zval *snmpval = NULL;
  615. int snmp_errno;
  616. /* we start with retval=FALSE. If any actual data is acquired, retval will be set to appropriate type */
  617. RETVAL_FALSE;
  618. /* reset errno and errstr */
  619. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_NOERROR, "");
  620. if (st & SNMP_CMD_WALK) { /* remember root OID */
  621. memmove((char *)root, (char *)(objid_query->vars[0].name), (objid_query->vars[0].name_length) * sizeof(oid));
  622. rootlen = objid_query->vars[0].name_length;
  623. objid_query->offset = objid_query->count;
  624. }
  625. if ((ss = snmp_open(session)) == NULL) {
  626. snmp_error(session, NULL, NULL, &err);
  627. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open snmp connection: %s", err);
  628. free(err);
  629. RETVAL_FALSE;
  630. return;
  631. }
  632. if ((st & SNMP_CMD_SET) && objid_query->count > objid_query->step) {
  633. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES, "Can not fit all OIDs for SET query into one packet, using multiple queries");
  634. }
  635. while (keepwalking) {
  636. keepwalking = 0;
  637. if (st & SNMP_CMD_WALK) {
  638. if (session->version == SNMP_VERSION_1) {
  639. pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
  640. } else {
  641. pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
  642. pdu->non_repeaters = objid_query->non_repeaters;
  643. pdu->max_repetitions = objid_query->max_repetitions;
  644. }
  645. snmp_add_null_var(pdu, objid_query->vars[0].name, objid_query->vars[0].name_length);
  646. } else {
  647. if (st & SNMP_CMD_GET) {
  648. pdu = snmp_pdu_create(SNMP_MSG_GET);
  649. } else if (st & SNMP_CMD_GETNEXT) {
  650. pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
  651. } else if (st & SNMP_CMD_SET) {
  652. pdu = snmp_pdu_create(SNMP_MSG_SET);
  653. } else {
  654. snmp_close(ss);
  655. php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unknown SNMP command (internals)");
  656. RETVAL_FALSE;
  657. return;
  658. }
  659. for (count = 0; objid_query->offset < objid_query->count && count < objid_query->step; objid_query->offset++, count++){
  660. if (st & SNMP_CMD_SET) {
  661. if ((snmp_errno = snmp_add_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value))) {
  662. snprint_objid(buf, sizeof(buf), objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
  663. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Could not add variable: OID='%s' type='%c' value='%s': %s", buf, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value, snmp_api_errstring(snmp_errno));
  664. snmp_free_pdu(pdu);
  665. snmp_close(ss);
  666. RETVAL_FALSE;
  667. return;
  668. }
  669. } else {
  670. snmp_add_null_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
  671. }
  672. }
  673. if(pdu->variables == NULL){
  674. snmp_free_pdu(pdu);
  675. snmp_close(ss);
  676. RETVAL_FALSE;
  677. return;
  678. }
  679. }
  680. retry:
  681. status = snmp_synch_response(ss, pdu, &response);
  682. if (status == STAT_SUCCESS) {
  683. if (response->errstat == SNMP_ERR_NOERROR) {
  684. if (st & SNMP_CMD_SET) {
  685. if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
  686. keepwalking = 1;
  687. continue;
  688. }
  689. snmp_free_pdu(response);
  690. snmp_close(ss);
  691. RETVAL_TRUE;
  692. return;
  693. }
  694. for (vars = response->variables; vars; vars = vars->next_variable) {
  695. /* do not output errors as values */
  696. if ( vars->type == SNMP_ENDOFMIBVIEW ||
  697. vars->type == SNMP_NOSUCHOBJECT ||
  698. vars->type == SNMP_NOSUCHINSTANCE ) {
  699. if ((st & SNMP_CMD_WALK) && Z_TYPE_P(return_value) == IS_ARRAY) {
  700. break;
  701. }
  702. snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
  703. snprint_value(buf2, sizeof(buf2), vars->name, vars->name_length, vars);
  704. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, buf2);
  705. continue;
  706. }
  707. if ((st & SNMP_CMD_WALK) &&
  708. (vars->name_length < rootlen || memcmp(root, vars->name, rootlen * sizeof(oid)))) { /* not part of this subtree */
  709. if (Z_TYPE_P(return_value) == IS_ARRAY) { /* some records are fetched already, shut down further lookup */
  710. keepwalking = 0;
  711. } else {
  712. /* first fetched OID is out of subtree, fallback to GET query */
  713. st |= SNMP_CMD_GET;
  714. st ^= SNMP_CMD_WALK;
  715. objid_query->offset = 0;
  716. keepwalking = 1;
  717. }
  718. break;
  719. }
  720. MAKE_STD_ZVAL(snmpval);
  721. php_snmp_getvalue(vars, snmpval TSRMLS_CC, objid_query->valueretrieval);
  722. if (objid_query->array_output) {
  723. if (Z_TYPE_P(return_value) == IS_BOOL) {
  724. array_init(return_value);
  725. }
  726. if (st & SNMP_NUMERIC_KEYS) {
  727. add_next_index_zval(return_value, snmpval);
  728. } else if (st & SNMP_ORIGINAL_NAMES_AS_KEYS && st & SNMP_CMD_GET) {
  729. found = 0;
  730. for (count = 0; count < objid_query->count; count++) {
  731. if (objid_query->vars[count].name_length == vars->name_length && snmp_oid_compare(objid_query->vars[count].name, objid_query->vars[count].name_length, vars->name, vars->name_length) == 0) {
  732. found = 1;
  733. objid_query->vars[count].name_length = 0; /* mark this name as used */
  734. break;
  735. }
  736. }
  737. if (found) {
  738. add_assoc_zval(return_value, objid_query->vars[count].oid, snmpval);
  739. } else {
  740. snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
  741. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find original OID name for '%s'", buf2);
  742. }
  743. } else if (st & SNMP_USE_SUFFIX_AS_KEYS && st & SNMP_CMD_WALK) {
  744. snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
  745. if (rootlen <= vars->name_length && snmp_oid_compare(root, rootlen, vars->name, rootlen) == 0) {
  746. buf2[0] = '\0';
  747. count = rootlen;
  748. while(count < vars->name_length){
  749. sprintf(buf, "%lu.", vars->name[count]);
  750. strcat(buf2, buf);
  751. count++;
  752. }
  753. buf2[strlen(buf2) - 1] = '\0'; /* remove trailing '.' */
  754. }
  755. add_assoc_zval(return_value, buf2, snmpval);
  756. } else {
  757. snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
  758. add_assoc_zval(return_value, buf2, snmpval);
  759. }
  760. } else {
  761. *return_value = *snmpval;
  762. zval_copy_ctor(return_value);
  763. zval_ptr_dtor(&snmpval);
  764. break;
  765. }
  766. /* OID increase check */
  767. if (st & SNMP_CMD_WALK) {
  768. if (objid_query->oid_increasing_check == TRUE && snmp_oid_compare(objid_query->vars[0].name, objid_query->vars[0].name_length, vars->name, vars->name_length) >= 0) {
  769. snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
  770. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_NOT_INCREASING, "Error: OID not increasing: %s", buf2);
  771. keepwalking = 0;
  772. } else {
  773. memmove((char *)(objid_query->vars[0].name), (char *)vars->name, vars->name_length * sizeof(oid));
  774. objid_query->vars[0].name_length = vars->name_length;
  775. keepwalking = 1;
  776. }
  777. }
  778. }
  779. if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
  780. keepwalking = 1;
  781. }
  782. } else {
  783. if (st & SNMP_CMD_WALK && response->errstat == SNMP_ERR_TOOBIG && objid_query->max_repetitions > 1) { /* Answer will not fit into single packet */
  784. objid_query->max_repetitions /= 2;
  785. snmp_free_pdu(response);
  786. keepwalking = 1;
  787. continue;
  788. }
  789. if (!(st & SNMP_CMD_WALK) || response->errstat != SNMP_ERR_NOSUCHNAME || Z_TYPE_P(return_value) == IS_BOOL) {
  790. for ( count=1, vars = response->variables;
  791. vars && count != response->errindex;
  792. vars = vars->next_variable, count++);
  793. if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT) && response->errstat == SNMP_ERR_TOOBIG && objid_query->step > 1) { /* Answer will not fit into single packet */
  794. objid_query->offset = ((objid_query->offset > objid_query->step) ? (objid_query->offset - objid_query->step) : 0 );
  795. objid_query->step /= 2;
  796. snmp_free_pdu(response);
  797. keepwalking = 1;
  798. continue;
  799. }
  800. if (vars) {
  801. snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
  802. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, snmp_errstring(response->errstat));
  803. } else {
  804. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at %u object_id: %s", response->errindex, snmp_errstring(response->errstat));
  805. }
  806. if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT)) { /* cut out bogus OID and retry */
  807. if ((pdu = snmp_fix_pdu(response, ((st & SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT) )) != NULL) {
  808. snmp_free_pdu(response);
  809. goto retry;
  810. }
  811. }
  812. snmp_free_pdu(response);
  813. snmp_close(ss);
  814. if (objid_query->array_output) {
  815. zval_dtor(return_value);
  816. }
  817. RETVAL_FALSE;
  818. return;
  819. }
  820. }
  821. } else if (status == STAT_TIMEOUT) {
  822. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_TIMEOUT, "No response from %s", session->peername);
  823. if (objid_query->array_output) {
  824. zval_dtor(return_value);
  825. }
  826. snmp_close(ss);
  827. RETVAL_FALSE;
  828. return;
  829. } else { /* status == STAT_ERROR */
  830. snmp_error(ss, NULL, NULL, &err);
  831. php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_GENERIC, "Fatal error: %s", err);
  832. free(err);
  833. if (objid_query->array_output) {
  834. zval_dtor(return_value);
  835. }
  836. snmp_close(ss);
  837. RETVAL_FALSE;
  838. return;
  839. }
  840. if (response) {
  841. snmp_free_pdu(response);
  842. }
  843. } /* keepwalking */
  844. snmp_close(ss);
  845. }
  846. /* }}} */
  847. /* {{{ php_snmp_parse_oid
  848. *
  849. * OID parser (and type, value for SNMP_SET command)
  850. */
  851. static int php_snmp_parse_oid(zval *object, int st, struct objid_query *objid_query, zval **oid, zval **type, zval **value TSRMLS_DC)
  852. {
  853. char *pptr;
  854. HashPosition pos_oid, pos_type, pos_value;
  855. zval **tmp_oid, **tmp_type, **tmp_value;
  856. if (Z_TYPE_PP(oid) != IS_ARRAY) {
  857. if (Z_ISREF_PP(oid)) {
  858. SEPARATE_ZVAL(oid);
  859. }
  860. convert_to_string_ex(oid);
  861. } else if (Z_TYPE_PP(oid) == IS_ARRAY) {
  862. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(oid), &pos_oid);
  863. }
  864. if (st & SNMP_CMD_SET) {
  865. if (Z_TYPE_PP(type) != IS_ARRAY) {
  866. if (Z_ISREF_PP(type)) {
  867. SEPARATE_ZVAL(type);
  868. }
  869. convert_to_string_ex(type);
  870. } else if (Z_TYPE_PP(type) == IS_ARRAY) {
  871. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(type), &pos_type);
  872. }
  873. if (Z_TYPE_PP(value) != IS_ARRAY) {
  874. if (Z_ISREF_PP(value)) {
  875. SEPARATE_ZVAL(value);
  876. }
  877. convert_to_string_ex(value);
  878. } else if (Z_TYPE_PP(value) == IS_ARRAY) {
  879. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(value), &pos_value);
  880. }
  881. }
  882. objid_query->count = 0;
  883. objid_query->array_output = ((st & SNMP_CMD_WALK) ? TRUE : FALSE);
  884. if (Z_TYPE_PP(oid) == IS_STRING) {
  885. objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg));
  886. if (objid_query->vars == NULL) {
  887. php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed while parsing oid: %s", strerror(errno));
  888. efree(objid_query->vars);
  889. return FALSE;
  890. }
  891. objid_query->vars[objid_query->count].oid = Z_STRVAL_PP(oid);
  892. if (st & SNMP_CMD_SET) {
  893. if (Z_TYPE_PP(type) == IS_STRING && Z_TYPE_PP(value) == IS_STRING) {
  894. if (Z_STRLEN_PP(type) != 1) {
  895. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bogus type '%s', should be single char, got %u", Z_STRVAL_PP(type), Z_STRLEN_PP(type));
  896. efree(objid_query->vars);
  897. return FALSE;
  898. }
  899. pptr = Z_STRVAL_PP(type);
  900. objid_query->vars[objid_query->count].type = *pptr;
  901. objid_query->vars[objid_query->count].value = Z_STRVAL_PP(value);
  902. } else {
  903. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Single objid and multiple type or values are not supported");
  904. efree(objid_query->vars);
  905. return FALSE;
  906. }
  907. }
  908. objid_query->count++;
  909. } else if (Z_TYPE_PP(oid) == IS_ARRAY) { /* we got objid array */
  910. if (zend_hash_num_elements(Z_ARRVAL_PP(oid)) == 0) {
  911. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got empty OID array");
  912. return FALSE;
  913. }
  914. objid_query->vars = (snmpobjarg *)safe_emalloc(sizeof(snmpobjarg), zend_hash_num_elements(Z_ARRVAL_PP(oid)), 0);
  915. if (objid_query->vars == NULL) {
  916. php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed while parsing oid array: %s", strerror(errno));
  917. efree(objid_query->vars);
  918. return FALSE;
  919. }
  920. objid_query->array_output = ( (st & SNMP_CMD_SET) ? FALSE : TRUE );
  921. for ( zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(oid), &pos_oid);
  922. zend_hash_get_current_data_ex(Z_ARRVAL_PP(oid), (void **) &tmp_oid, &pos_oid) == SUCCESS;
  923. zend_hash_move_forward_ex(Z_ARRVAL_PP(oid), &pos_oid) ) {
  924. convert_to_string_ex(tmp_oid);
  925. objid_query->vars[objid_query->count].oid = Z_STRVAL_PP(tmp_oid);
  926. if (st & SNMP_CMD_SET) {
  927. if (Z_TYPE_PP(type) == IS_STRING) {
  928. pptr = Z_STRVAL_PP(type);
  929. objid_query->vars[objid_query->count].type = *pptr;
  930. } else if (Z_TYPE_PP(type) == IS_ARRAY) {
  931. if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(type), (void **) &tmp_type, &pos_type)) {
  932. convert_to_string_ex(tmp_type);
  933. if (Z_STRLEN_PP(tmp_type) != 1) {
  934. php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s': bogus type '%s', should be single char, got %u", Z_STRVAL_PP(tmp_oid), Z_STRVAL_PP(tmp_type), Z_STRLEN_PP(tmp_type));
  935. efree(objid_query->vars);
  936. return FALSE;
  937. }
  938. pptr = Z_STRVAL_PP(tmp_type);
  939. objid_query->vars[objid_query->count].type = *pptr;
  940. zend_hash_move_forward_ex(Z_ARRVAL_PP(type), &pos_type);
  941. } else {
  942. php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s': no type set", Z_STRVAL_PP(tmp_oid));
  943. efree(objid_query->vars);
  944. return FALSE;
  945. }
  946. }
  947. if (Z_TYPE_PP(value) == IS_STRING) {
  948. objid_query->vars[objid_query->count].value = Z_STRVAL_PP(value);
  949. } else if (Z_TYPE_PP(value) == IS_ARRAY) {
  950. if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(value), (void **) &tmp_value, &pos_value)) {
  951. convert_to_string_ex(tmp_value);
  952. objid_query->vars[objid_query->count].value = Z_STRVAL_PP(tmp_value);
  953. zend_hash_move_forward_ex(Z_ARRVAL_PP(value), &pos_value);
  954. } else {
  955. php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s': no value set", Z_STRVAL_PP(tmp_oid));
  956. efree(objid_query->vars);
  957. return FALSE;
  958. }
  959. }
  960. }
  961. objid_query->count++;
  962. }
  963. }
  964. /* now parse all OIDs */
  965. if (st & SNMP_CMD_WALK) {
  966. if (objid_query->count > 1) {
  967. php_snmp_error(object, NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Multi OID walks are not supported!");
  968. efree(objid_query->vars);
  969. return FALSE;
  970. }
  971. objid_query->vars[0].name_length = MAX_NAME_LEN;
  972. if (strlen(objid_query->vars[0].oid)) { /* on a walk, an empty string means top of tree - no error */
  973. if (!snmp_parse_oid(objid_query->vars[0].oid, objid_query->vars[0].name, &(objid_query->vars[0].name_length))) {
  974. php_snmp_error(object, NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[0].oid);
  975. efree(objid_query->vars);
  976. return FALSE;
  977. }
  978. } else {
  979. memmove((char *)objid_query->vars[0].name, (char *)objid_mib, sizeof(objid_mib));
  980. objid_query->vars[0].name_length = sizeof(objid_mib) / sizeof(oid);
  981. }
  982. } else {
  983. for (objid_query->offset = 0; objid_query->offset < objid_query->count; objid_query->offset++) {
  984. objid_query->vars[objid_query->offset].name_length = MAX_OID_LEN;
  985. if (!snmp_parse_oid(objid_query->vars[objid_query->offset].oid, objid_query->vars[objid_query->offset].name, &(objid_query->vars[objid_query->offset].name_length))) {
  986. php_snmp_error(object, NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[objid_query->offset].oid);
  987. efree(objid_query->vars);
  988. return FALSE;
  989. }
  990. }
  991. }
  992. objid_query->offset = 0;
  993. objid_query->step = objid_query->count;
  994. return (objid_query->count > 0);
  995. }
  996. /* }}} */
  997. /* {{{ netsnmp_session_init
  998. allocates memory for session and session->peername, caller should free it manually using netsnmp_session_free() and efree()
  999. */
  1000. static int netsnmp_session_init(php_snmp_session **session_p, int version, char *hostname, char *community, int timeout, int retries TSRMLS_DC)
  1001. {
  1002. php_snmp_session *session;
  1003. char *pptr, *host_ptr;
  1004. int force_ipv6 = FALSE;
  1005. int n;
  1006. struct sockaddr **psal;
  1007. struct sockaddr **res;
  1008. *session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
  1009. session = *session_p;
  1010. if (session == NULL) {
  1011. php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed allocating session");
  1012. return (-1);
  1013. }
  1014. memset(session, 0, sizeof(php_snmp_session));
  1015. snmp_sess_init(session);
  1016. session->version = version;
  1017. session->remote_port = SNMP_PORT;
  1018. session->peername = emalloc(MAX_NAME_LEN);
  1019. if (session->peername == NULL) {
  1020. php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed while copying hostname");
  1021. return (-1);
  1022. }
  1023. /* we copy original hostname for further processing */
  1024. strlcpy(session->peername, hostname, MAX_NAME_LEN);
  1025. host_ptr = session->peername;
  1026. /* Reading the hostname and its optional non-default port number */
  1027. if (*host_ptr == '[') { /* IPv6 address */
  1028. force_ipv6 = TRUE;
  1029. host_ptr++;
  1030. if ((pptr = strchr(host_ptr, ']'))) {
  1031. if (pptr[1] == ':') {
  1032. session->remote_port = atoi(pptr + 2);
  1033. }
  1034. *pptr = '\0';
  1035. } else {
  1036. php_error_docref(NULL TSRMLS_CC, E_WARNING, "malformed IPv6 address, closing square bracket missing");
  1037. return (-1);
  1038. }
  1039. } else { /* IPv4 address */
  1040. if ((pptr = strchr(host_ptr, ':'))) {
  1041. session->remote_port = atoi(pptr + 1);
  1042. *pptr = '\0';
  1043. }
  1044. }
  1045. /* since Net-SNMP library requires 'udp6:' prefix for all IPv6 addresses (in FQDN form too) we need to
  1046. perform possible name resolution before running any SNMP queries */
  1047. if ((n = php_network_getaddresses(host_ptr, SOCK_DGRAM, &psal, NULL TSRMLS_CC)) == 0) { /* some resolver error */
  1048. /* warnings sent, bailing out */
  1049. return (-1);
  1050. }
  1051. /* we have everything we need in psal, flush peername and fill it properly */
  1052. *(session->peername) = '\0';
  1053. res = psal;
  1054. while (n-- > 0) {
  1055. pptr = session->peername;
  1056. #if HAVE_GETADDRINFO && HAVE_IPV6 && HAVE_INET_NTOP
  1057. if (force_ipv6 && (*res)->sa_family != AF_INET6) {
  1058. res++;
  1059. continue;
  1060. }
  1061. if ((*res)->sa_family == AF_INET6) {
  1062. strcpy(session->peername, "udp6:[");
  1063. pptr = session->peername + strlen(session->peername);
  1064. inet_ntop((*res)->sa_family, &(((struct sockaddr_in6*)(*res))->sin6_addr), pptr, MAX_NAME_LEN);
  1065. strcat(pptr, "]");
  1066. } else if ((*res)->sa_family == AF_INET) {
  1067. inet_ntop((*res)->sa_family, &(((struct sockaddr_in*)(*res))->sin_addr), pptr, MAX_NAME_LEN);
  1068. } else {
  1069. res++;
  1070. continue;
  1071. }
  1072. #else
  1073. if ((*res)->sa_family != AF_INET) {
  1074. res++;
  1075. continue;
  1076. }
  1077. strcat(pptr, inet_ntoa(((struct sockaddr_in*)(*res))->sin_addr));
  1078. #endif
  1079. break;
  1080. }
  1081. if (strlen(session->peername) == 0) {
  1082. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown failure while resolving '%s'", hostname);
  1083. return (-1);
  1084. }
  1085. /* XXX FIXME
  1086. There should be check for non-empty session->peername!
  1087. */
  1088. /* put back non-standard SNMP port */
  1089. if (session->remote_port != SNMP_PORT) {
  1090. pptr = session->peername + strlen(session->peername);
  1091. sprintf(pptr, ":%d", session->remote_port);
  1092. }
  1093. php_network_freeaddresses(psal);
  1094. if (version == SNMP_VERSION_3) {
  1095. /* Setting the security name. */
  1096. session->securityName = estrdup(community);
  1097. session->securityNameLen = strlen(session->securityName);
  1098. } else {
  1099. session->authenticator = NULL;
  1100. session->community = (u_char *)estrdup(community);
  1101. session->community_len = strlen(community);
  1102. }
  1103. session->retries = retries;
  1104. session->timeout = timeout;
  1105. return (0);
  1106. }
  1107. /* }}} */
  1108. /* {{{ int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
  1109. Set the security level in the snmpv3 session */
  1110. static int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
  1111. {
  1112. if (!strcasecmp(level, "noAuthNoPriv") || !strcasecmp(level, "nanp")) {
  1113. s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
  1114. } else if (!strcasecmp(level, "authNoPriv") || !strcasecmp(level, "anp")) {
  1115. s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
  1116. } else if (!strcasecmp(level, "authPriv") || !strcasecmp(level, "ap")) {
  1117. s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
  1118. } else {
  1119. return (-1);
  1120. }
  1121. return (0);
  1122. }
  1123. /* }}} */
  1124. /* {{{ int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
  1125. Set the authentication protocol in the snmpv3 session */
  1126. static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot TSRMLS_DC)
  1127. {
  1128. if (!strcasecmp(prot, "MD5")) {
  1129. s->securityAuthProto = usmHMACMD5AuthProtocol;
  1130. s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
  1131. } else if (!strcasecmp(prot, "SHA")) {
  1132. s->securityAuthProto = usmHMACSHA1AuthProtocol;
  1133. s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
  1134. } else {
  1135. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown authentication protocol '%s'", prot);
  1136. return (-1);
  1137. }
  1138. return (0);
  1139. }
  1140. /* }}} */
  1141. /* {{{ int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot)
  1142. Set the security protocol in the snmpv3 session */
  1143. static int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot TSRMLS_DC)
  1144. {
  1145. if (!strcasecmp(prot, "DES")) {
  1146. s->securityPrivProto = usmDESPrivProtocol;
  1147. s->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
  1148. #ifdef HAVE_AES
  1149. } else if (!strcasecmp(prot, "AES128") || !strcasecmp(prot, "AES")) {
  1150. s->securityPrivProto = usmAESPrivProtocol;
  1151. s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
  1152. #endif
  1153. } else {
  1154. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown security protocol '%s'", prot);
  1155. return (-1);
  1156. }
  1157. return (0);
  1158. }
  1159. /* }}} */
  1160. /* {{{ int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass)
  1161. Make key from pass phrase in the snmpv3 session */
  1162. static int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass TSRMLS_DC)
  1163. {
  1164. int snmp_errno;
  1165. s->securityAuthKeyLen = USM_AUTH_KU_LEN;
  1166. if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
  1167. (u_char *) pass, strlen(pass),
  1168. s->securityAuthKey, &(s->securityAuthKeyLen)))) {
  1169. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error generating a key for authentication pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
  1170. return (-1);
  1171. }
  1172. return (0);
  1173. }
  1174. /* }}} */
  1175. /* {{{ int netsnmp_session_gen_sec_key(struct snmp_session *s, u_char *pass)
  1176. Make key from pass phrase in the snmpv3 session */
  1177. static int netsnmp_session_gen_sec_key(struct snmp_session *s, char *pass TSRMLS_DC)
  1178. {
  1179. int snmp_errno;
  1180. s->securityPrivKeyLen = USM_PRIV_KU_LEN;
  1181. if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
  1182. (u_char *)pass, strlen(pass),
  1183. s->securityPrivKey, &(s->securityPrivKeyLen)))) {
  1184. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error generating a key for privacy pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
  1185. return (-2);
  1186. }
  1187. return (0);
  1188. }
  1189. /* }}} */
  1190. /* {{{ in netsnmp_session_set_contextEngineID(struct snmp_session *s, u_char * contextEngineID)
  1191. Set context Engine Id in the snmpv3 session */
  1192. static int netsnmp_session_set_contextEngineID(struct snmp_session *s, char * contextEngineID TSRMLS_DC)
  1193. {
  1194. size_t ebuf_len = 32, eout_len = 0;
  1195. u_char *ebuf = (u_char *) emalloc(ebuf_len);
  1196. if (ebuf == NULL) {
  1197. php_error_docref(NULL TSRMLS_CC, E_WARNING, "malloc failure setting contextEngineID");
  1198. return (-1);
  1199. }
  1200. if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, contextEngineID)) {
  1201. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad engine ID value '%s'", contextEngineID);
  1202. efree(ebuf);
  1203. return (-1);
  1204. }
  1205. if (s->contextEngineID) {
  1206. efree(s->contextEngineID);
  1207. }
  1208. s->contextEngineID = ebuf;
  1209. s->contextEngineIDLen = eout_len;
  1210. return (0);
  1211. }
  1212. /* }}} */
  1213. /* {{{ php_set_security(struct snmp_session *session, char *sec_level, char *auth_protocol, char *auth_passphrase, char *priv_protocol, char *priv_passphrase, char *contextName, char *contextEngineID)
  1214. Set all snmpv3-related security options */
  1215. static int netsnmp_session_set_security(struct snmp_session *session, char *sec_level, char *auth_protocol, char *auth_passphrase, char *priv_protocol, char *priv_passphrase, char *contextName, char *contextEngineID TSRMLS_DC)
  1216. {
  1217. /* Setting the security level. */
  1218. if (netsnmp_session_set_sec_level(session, sec_level)) {
  1219. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid security level '%s'", sec_level);
  1220. return (-1);
  1221. }
  1222. if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
  1223. /* Setting the authentication protocol. */
  1224. if (netsnmp_session_set_auth_protocol(session, auth_protocol TSRMLS_CC)) {
  1225. /* Warning message sent already, just bail out */
  1226. return (-1);
  1227. }
  1228. /* Setting the authentication passphrase. */
  1229. if (netsnmp_session_gen_auth_key(session, auth_passphrase TSRMLS_CC)) {
  1230. /* Warning message sent already, just bail out */
  1231. return (-1);
  1232. }
  1233. if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
  1234. /* Setting the security protocol. */
  1235. if (netsnmp_session_set_sec_protocol(session, priv_protocol TSRMLS_CC)) {
  1236. /* Warning message sent already, just bail out */
  1237. return (-1);
  1238. }
  1239. /* Setting the security protocol passphrase. */
  1240. if (netsnmp_session_gen_sec_key(session, priv_passphrase TSRMLS_CC)) {
  1241. /* Warning message sent already, just bail out */
  1242. return (-1);
  1243. }
  1244. }
  1245. }
  1246. /* Setting contextName if specified */
  1247. if (contextName) {
  1248. session->contextName = contextName;
  1249. session->contextNameLen = strlen(contextName);
  1250. }
  1251. /* Setting contextEngineIS if specified */
  1252. if (contextEngineID && strlen(contextEngineID) && netsnmp_session_set_contextEngineID(session, contextEngineID TSRMLS_CC)) {
  1253. /* Warning message sent already, just bail out */
  1254. return (-1);
  1255. }
  1256. return (0);
  1257. }
  1258. /* }}} */
  1259. /* {{{ php_snmp
  1260. *
  1261. * Generic SNMP handler for all versions.
  1262. * This function makes use of the internal SNMP object fetcher.
  1263. * Used both in old (non-OO) and OO API
  1264. *
  1265. */
  1266. static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
  1267. {
  1268. zval **oid, **value, **type;
  1269. char *a1, *a2, *a3, *a4, *a5, *a6, *a7;
  1270. int a1_len, a2_len, a3_len, a4_len, a5_len, a6_len, a7_len;
  1271. zend_bool use_orignames = 0, suffix_keys = 0;
  1272. long timeout = SNMP_DEFAULT_TIMEOUT;
  1273. long retries = SNMP_DEFAULT_RETRIES;
  1274. int argc = ZEND_NUM_ARGS();
  1275. struct objid_query objid_query;
  1276. php_snmp_session *session;
  1277. int session_less_mode = (getThis() == NULL);
  1278. php_snmp_object *snmp_object;
  1279. php_snmp_object glob_snmp_object;
  1280. objid_query.max_repetitions = -1;
  1281. objid_query.non_repeaters = 0;
  1282. objid_query.valueretrieval = SNMP_G(valueretrieval);
  1283. objid_query.oid_increasing_check = TRUE;
  1284. if (session_less_mode) {
  1285. if (version == SNMP_VERSION_3) {
  1286. if (st & SNMP_CMD_SET) {
  1287. if (zend_parse_parameters(argc TSRMLS_CC, "sssssssZZZ|ll", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
  1288. &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len, &oid, &type, &value, &timeout, &retries) == FAILURE) {
  1289. RETURN_FALSE;
  1290. }
  1291. } else {
  1292. /* SNMP_CMD_GET
  1293. * SNMP_CMD_GETNEXT
  1294. * SNMP_CMD_WALK
  1295. */
  1296. if (zend_parse_parameters(argc TSRMLS_CC, "sssssssZ|ll", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
  1297. &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len, &oid, &timeout, &retries) == FAILURE) {
  1298. RETURN_FALSE;
  1299. }
  1300. }
  1301. } else {
  1302. if (st & SNMP_CMD_SET) {
  1303. if (zend_parse_parameters(argc TSRMLS_CC, "ssZZZ|ll", &a1, &a1_len, &a2, &a2_len, &oid, &type, &value, &timeout, &retries) == FAILURE) {
  1304. RETURN_FALSE;
  1305. }
  1306. } else {
  1307. /* SNMP_CMD_GET
  1308. * SNMP_CMD_GETNEXT
  1309. * SNMP_CMD_WALK
  1310. */
  1311. if (zend_parse_parameters(argc TSRMLS_CC, "ssZ|ll", &a1, &a1_len, &a2, &a2_len, &oid, &timeout, &retries) == FAILURE) {
  1312. RETURN_FALSE;
  1313. }
  1314. }
  1315. }
  1316. } else {
  1317. if (st & SNMP_CMD_SET) {
  1318. if (zend_parse_parameters(argc TSRMLS_CC, "ZZZ", &oid, &type, &value) == FAILURE) {
  1319. RETURN_FALSE;
  1320. }
  1321. } else if (st & SNMP_CMD_WALK) {
  1322. if (zend_parse_parameters(argc TSRMLS_CC, "Z|bll", &oid, &suffix_keys, &(objid_query.max_repetitions), &(objid_query.non_repeaters)) == FAILURE) {
  1323. RETURN_FALSE;
  1324. }
  1325. if (suffix_keys) {
  1326. st |= SNMP_USE_SUFFIX_AS_KEYS;
  1327. }
  1328. } else if (st & SNMP_CMD_GET) {
  1329. if (zend_parse_parameters(argc TSRMLS_CC, "Z|b", &oid, &use_orignames) == FAILURE) {
  1330. RETURN_FALSE;
  1331. }
  1332. if (use_orignames) {
  1333. st |= SNMP_ORIGINAL_NAMES_AS_KEYS;
  1334. }
  1335. } else {
  1336. /* SNMP_CMD_GETNEXT
  1337. */
  1338. if (zend_parse_parameters(argc TSRMLS_CC, "Z", &oid) == FAILURE) {
  1339. RETURN_FALSE;
  1340. }
  1341. }
  1342. }
  1343. if (!php_snmp_parse_oid(getThis(), st, &objid_query, oid, type, value TSRMLS_CC)) {
  1344. RETURN_FALSE;
  1345. }
  1346. if (session_less_mode) {
  1347. if (netsnmp_session_init(&session, version, a1, a2, timeout, retries TSRMLS_CC)) {
  1348. efree(objid_query.vars);
  1349. netsnmp_session_free(&session);
  1350. RETURN_FALSE;
  1351. }
  1352. if (version == SNMP_VERSION_3 && netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL TSRMLS_CC)) {
  1353. efree(objid_query.vars);
  1354. netsnmp_session_free(&session);
  1355. /* Warning message sent already, just bail out */
  1356. RETURN_FALSE;
  1357. }
  1358. } else {
  1359. zval *object = getThis();
  1360. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  1361. session = snmp_object->session;
  1362. if (!session) {
  1363. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or uninitialized SNMP object");
  1364. efree(objid_query.vars);
  1365. RETURN_FALSE;
  1366. }
  1367. if (snmp_object->max_oids > 0) {
  1368. objid_query.step = snmp_object->max_oids;
  1369. if (objid_query.max_repetitions < 0) { /* unspecified in function call, use session-wise */
  1370. objid_query.max_repetitions = snmp_object->max_oids;
  1371. }
  1372. }
  1373. objid_query.oid_increasing_check = snmp_object->oid_increasing_check;
  1374. objid_query.valueretrieval = snmp_object->valueretrieval;
  1375. glob_snmp_object.enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
  1376. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, snmp_object->enum_print);
  1377. glob_snmp_object.quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
  1378. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, snmp_object->quick_print);
  1379. glob_snmp_object.oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
  1380. netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, snmp_object->oid_output_format);
  1381. }
  1382. if (objid_query.max_repetitions < 0) {
  1383. objid_query.max_repetitions = 20; /* provide correct default value */
  1384. }
  1385. php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, session, &objid_query);
  1386. efree(objid_query.vars);
  1387. if (session_less_mode) {
  1388. netsnmp_session_free(&session);
  1389. } else {
  1390. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
  1391. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
  1392. netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
  1393. }
  1394. }
  1395. /* }}} */
  1396. /* {{{ proto mixed snmpget(string host, string community, mixed object_id [, int timeout [, int retries]])
  1397. Fetch a SNMP object */
  1398. PHP_FUNCTION(snmpget)
  1399. {
  1400. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_1);
  1401. }
  1402. /* }}} */
  1403. /* {{{ proto mixed snmpgetnext(string host, string community, mixed object_id [, int timeout [, int retries]])
  1404. Fetch a SNMP object */
  1405. PHP_FUNCTION(snmpgetnext)
  1406. {
  1407. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_1);
  1408. }
  1409. /* }}} */
  1410. /* {{{ proto mixed snmpwalk(string host, string community, mixed object_id [, int timeout [, int retries]])
  1411. Return all objects under the specified object id */
  1412. PHP_FUNCTION(snmpwalk)
  1413. {
  1414. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_1);
  1415. }
  1416. /* }}} */
  1417. /* {{{ proto mixed snmprealwalk(string host, string community, mixed object_id [, int timeout [, int retries]])
  1418. Return all objects including their respective object id within the specified one */
  1419. PHP_FUNCTION(snmprealwalk)
  1420. {
  1421. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_1);
  1422. }
  1423. /* }}} */
  1424. /* {{{ proto bool snmpset(string host, string community, mixed object_id, mixed type, mixed value [, int timeout [, int retries]])
  1425. Set the value of a SNMP object */
  1426. PHP_FUNCTION(snmpset)
  1427. {
  1428. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_1);
  1429. }
  1430. /* }}} */
  1431. /* {{{ proto bool snmp_get_quick_print(void)
  1432. Return the current status of quick_print */
  1433. PHP_FUNCTION(snmp_get_quick_print)
  1434. {
  1435. if (zend_parse_parameters_none() == FAILURE) {
  1436. return;
  1437. }
  1438. RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));
  1439. }
  1440. /* }}} */
  1441. /* {{{ proto bool snmp_set_quick_print(int quick_print)
  1442. Return all objects including their respective object id within the specified one */
  1443. PHP_FUNCTION(snmp_set_quick_print)
  1444. {
  1445. long a1;
  1446. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &a1) == FAILURE) {
  1447. RETURN_FALSE;
  1448. }
  1449. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (int)a1);
  1450. RETURN_TRUE;
  1451. }
  1452. /* }}} */
  1453. /* {{{ proto bool snmp_set_enum_print(int enum_print)
  1454. Return all values that are enums with their enum value instead of the raw integer */
  1455. PHP_FUNCTION(snmp_set_enum_print)
  1456. {
  1457. long a1;
  1458. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &a1) == FAILURE) {
  1459. RETURN_FALSE;
  1460. }
  1461. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (int) a1);
  1462. RETURN_TRUE;
  1463. }
  1464. /* }}} */
  1465. /* {{{ proto bool snmp_set_oid_output_format(int oid_format)
  1466. Set the OID output format. */
  1467. PHP_FUNCTION(snmp_set_oid_output_format)
  1468. {
  1469. long a1;
  1470. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &a1) == FAILURE) {
  1471. RETURN_FALSE;
  1472. }
  1473. switch((int) a1) {
  1474. case NETSNMP_OID_OUTPUT_SUFFIX:
  1475. case NETSNMP_OID_OUTPUT_MODULE:
  1476. case NETSNMP_OID_OUTPUT_FULL:
  1477. case NETSNMP_OID_OUTPUT_NUMERIC:
  1478. case NETSNMP_OID_OUTPUT_UCD:
  1479. case NETSNMP_OID_OUTPUT_NONE:
  1480. netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
  1481. RETURN_TRUE;
  1482. break;
  1483. default:
  1484. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP output print format '%d'", (int) a1);
  1485. RETURN_FALSE;
  1486. break;
  1487. }
  1488. }
  1489. /* }}} */
  1490. /* {{{ proto mixed snmp2_get(string host, string community, mixed object_id [, int timeout [, int retries]])
  1491. Fetch a SNMP object */
  1492. PHP_FUNCTION(snmp2_get)
  1493. {
  1494. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_2c);
  1495. }
  1496. /* }}} */
  1497. /* {{{ proto mixed snmp2_getnext(string host, string community, mixed object_id [, int timeout [, int retries]])
  1498. Fetch a SNMP object */
  1499. PHP_FUNCTION(snmp2_getnext)
  1500. {
  1501. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_2c);
  1502. }
  1503. /* }}} */
  1504. /* {{{ proto mixed snmp2_walk(string host, string community, mixed object_id [, int timeout [, int retries]])
  1505. Return all objects under the specified object id */
  1506. PHP_FUNCTION(snmp2_walk)
  1507. {
  1508. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_2c);
  1509. }
  1510. /* }}} */
  1511. /* {{{ proto mixed snmp2_real_walk(string host, string community, mixed object_id [, int timeout [, int retries]])
  1512. Return all objects including their respective object id within the specified one */
  1513. PHP_FUNCTION(snmp2_real_walk)
  1514. {
  1515. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_2c);
  1516. }
  1517. /* }}} */
  1518. /* {{{ proto bool snmp2_set(string host, string community, mixed object_id, mixed type, mixed value [, int timeout [, int retries]])
  1519. Set the value of a SNMP object */
  1520. PHP_FUNCTION(snmp2_set)
  1521. {
  1522. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_2c);
  1523. }
  1524. /* }}} */
  1525. /* {{{ proto mixed snmp3_get(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
  1526. Fetch the value of a SNMP object */
  1527. PHP_FUNCTION(snmp3_get)
  1528. {
  1529. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_3);
  1530. }
  1531. /* }}} */
  1532. /* {{{ proto mixed snmp3_getnext(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
  1533. Fetch the value of a SNMP object */
  1534. PHP_FUNCTION(snmp3_getnext)
  1535. {
  1536. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_3);
  1537. }
  1538. /* }}} */
  1539. /* {{{ proto mixed snmp3_walk(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
  1540. Fetch the value of a SNMP object */
  1541. PHP_FUNCTION(snmp3_walk)
  1542. {
  1543. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_3);
  1544. }
  1545. /* }}} */
  1546. /* {{{ proto mixed snmp3_real_walk(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
  1547. Fetch the value of a SNMP object */
  1548. PHP_FUNCTION(snmp3_real_walk)
  1549. {
  1550. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_3);
  1551. }
  1552. /* }}} */
  1553. /* {{{ proto bool snmp3_set(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id, mixed type, mixed value [, int timeout [, int retries]])
  1554. Fetch the value of a SNMP object */
  1555. PHP_FUNCTION(snmp3_set)
  1556. {
  1557. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_3);
  1558. }
  1559. /* }}} */
  1560. /* {{{ proto bool snmp_set_valueretrieval(int method)
  1561. Specify the method how the SNMP values will be returned */
  1562. PHP_FUNCTION(snmp_set_valueretrieval)
  1563. {
  1564. long method;
  1565. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
  1566. RETURN_FALSE;
  1567. }
  1568. if (method >= 0 && method <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
  1569. SNMP_G(valueretrieval) = method;
  1570. RETURN_TRUE;
  1571. } else {
  1572. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP value retrieval method '%ld'", method);
  1573. RETURN_FALSE;
  1574. }
  1575. }
  1576. /* }}} */
  1577. /* {{{ proto int snmp_get_valueretrieval()
  1578. Return the method how the SNMP values will be returned */
  1579. PHP_FUNCTION(snmp_get_valueretrieval)
  1580. {
  1581. if (zend_parse_parameters_none() == FAILURE) {
  1582. RETURN_FALSE;
  1583. }
  1584. RETURN_LONG(SNMP_G(valueretrieval));
  1585. }
  1586. /* }}} */
  1587. /* {{{ proto bool snmp_read_mib(string filename)
  1588. Reads and parses a MIB file into the active MIB tree. */
  1589. PHP_FUNCTION(snmp_read_mib)
  1590. {
  1591. char *filename;
  1592. int filename_len;
  1593. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) {
  1594. RETURN_FALSE;
  1595. }
  1596. if (!read_mib(filename)) {
  1597. char *error = strerror(errno);
  1598. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
  1599. RETURN_FALSE;
  1600. }
  1601. RETURN_TRUE;
  1602. }
  1603. /* }}} */
  1604. /* {{{ proto SNMP SNMP::__construct(int version, string hostname, string community|securityName [, long timeout [, long retries]])
  1605. Creates a new SNMP session to specified host. */
  1606. PHP_METHOD(snmp, __construct)
  1607. {
  1608. php_snmp_object *snmp_object;
  1609. zval *object = getThis();
  1610. char *a1, *a2;
  1611. int a1_len, a2_len;
  1612. long timeout = SNMP_DEFAULT_TIMEOUT;
  1613. long retries = SNMP_DEFAULT_RETRIES;
  1614. long version = SNMP_DEFAULT_VERSION;
  1615. int argc = ZEND_NUM_ARGS();
  1616. zend_error_handling error_handling;
  1617. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  1618. zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
  1619. if (zend_parse_parameters(argc TSRMLS_CC, "lss|ll", &version, &a1, &a1_len, &a2, &a2_len, &timeout, &retries) == FAILURE) {
  1620. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1621. return;
  1622. }
  1623. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1624. switch(version) {
  1625. case SNMP_VERSION_1:
  1626. case SNMP_VERSION_2c:
  1627. case SNMP_VERSION_3:
  1628. break;
  1629. default:
  1630. zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unknown SNMP protocol version", 0 TSRMLS_CC);
  1631. return;
  1632. }
  1633. /* handle re-open of snmp session */
  1634. if (snmp_object->session) {
  1635. netsnmp_session_free(&(snmp_object->session));
  1636. }
  1637. if (netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries TSRMLS_CC)) {
  1638. return;
  1639. }
  1640. snmp_object->max_oids = 0;
  1641. snmp_object->valueretrieval = SNMP_G(valueretrieval);
  1642. snmp_object->enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
  1643. snmp_object->oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
  1644. snmp_object->quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
  1645. snmp_object->oid_increasing_check = TRUE;
  1646. snmp_object->exceptions_enabled = 0;
  1647. }
  1648. /* }}} */
  1649. /* {{{ proto bool SNMP::close()
  1650. Close SNMP session */
  1651. PHP_METHOD(snmp, close)
  1652. {
  1653. php_snmp_object *snmp_object;
  1654. zval *object = getThis();
  1655. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  1656. if (zend_parse_parameters_none() == FAILURE) {
  1657. RETURN_FALSE;
  1658. }
  1659. netsnmp_session_free(&(snmp_object->session));
  1660. RETURN_TRUE;
  1661. }
  1662. /* }}} */
  1663. /* {{{ proto mixed SNMP::get(mixed object_id [, bool preserve_keys])
  1664. Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
  1665. PHP_METHOD(snmp, get)
  1666. {
  1667. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, (-1));
  1668. }
  1669. /* }}} */
  1670. /* {{{ proto mixed SNMP::getnext(mixed object_id)
  1671. Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
  1672. PHP_METHOD(snmp, getnext)
  1673. {
  1674. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, (-1));
  1675. }
  1676. /* }}} */
  1677. /* {{{ proto mixed SNMP::walk(mixed object_id [, bool $suffix_as_key = FALSE [, int $max_repetitions [, int $non_repeaters]])
  1678. Return all objects including their respective object id within the specified one as array of oid->value pairs */
  1679. PHP_METHOD(snmp, walk)
  1680. {
  1681. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, (-1));
  1682. }
  1683. /* }}} */
  1684. /* {{{ proto bool SNMP::set(mixed object_id, mixed type, mixed value)
  1685. Set the value of a SNMP object */
  1686. PHP_METHOD(snmp, set)
  1687. {
  1688. php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, (-1));
  1689. }
  1690. /* {{{ proto bool SNMP::setSecurity(string sec_level, [ string auth_protocol, string auth_passphrase [, string priv_protocol, string priv_passphrase [, string contextName [, string contextEngineID]]]])
  1691. Set SNMPv3 security-related session parameters */
  1692. PHP_METHOD(snmp, setSecurity)
  1693. {
  1694. php_snmp_object *snmp_object;
  1695. zval *object = getThis();
  1696. char *a1 = "", *a2 = "", *a3 = "", *a4 = "", *a5 = "", *a6 = "", *a7 = "";
  1697. int a1_len = 0, a2_len = 0, a3_len = 0, a4_len = 0, a5_len = 0, a6_len = 0, a7_len = 0;
  1698. int argc = ZEND_NUM_ARGS();
  1699. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  1700. if (zend_parse_parameters(argc TSRMLS_CC, "s|ssssss", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
  1701. &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len) == FAILURE) {
  1702. RETURN_FALSE;
  1703. }
  1704. if (netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7 TSRMLS_CC)) {
  1705. /* Warning message sent already, just bail out */
  1706. RETURN_FALSE;
  1707. }
  1708. RETURN_TRUE;
  1709. }
  1710. /* }}} */
  1711. /* {{{ proto long SNMP::getErrno()
  1712. Get last error code number */
  1713. PHP_METHOD(snmp, getErrno)
  1714. {
  1715. php_snmp_object *snmp_object;
  1716. zval *object = getThis();
  1717. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  1718. RETVAL_LONG(snmp_object->snmp_errno);
  1719. return;
  1720. }
  1721. /* }}} */
  1722. /* {{{ proto long SNMP::getError()
  1723. Get last error message */
  1724. PHP_METHOD(snmp, getError)
  1725. {
  1726. php_snmp_object *snmp_object;
  1727. zval *object = getThis();
  1728. snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
  1729. RETVAL_STRING(snmp_object->snmp_errstr, 1);
  1730. return;
  1731. }
  1732. /* }}} */
  1733. /* {{{ */
  1734. void php_snmp_add_property(HashTable *h, const char *name, size_t name_length, php_snmp_read_t read_func, php_snmp_write_t write_func TSRMLS_DC)
  1735. {
  1736. php_snmp_prop_handler p;
  1737. p.name = (char*) name;
  1738. p.name_length = name_length;
  1739. p.read_func = (read_func) ? read_func : NULL;
  1740. p.write_func = (write_func) ? write_func : NULL;
  1741. zend_hash_add(h, (char *)name, name_length + 1, &p, sizeof(php_snmp_prop_handler), NULL);
  1742. }
  1743. /* }}} */
  1744. /* {{{ php_snmp_read_property(zval *object, zval *member, int type[, const zend_literal *key])
  1745. Generic object property reader */
  1746. zval *php_snmp_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
  1747. {
  1748. zval tmp_member;
  1749. zval *retval;
  1750. php_snmp_object *obj;
  1751. php_snmp_prop_handler *hnd;
  1752. int ret;
  1753. ret = FAILURE;
  1754. obj = (php_snmp_object *)zend_objects_get_address(object TSRMLS_CC);
  1755. if (Z_TYPE_P(member) != IS_STRING) {
  1756. tmp_member = *member;
  1757. zval_copy_ctor(&tmp_member);
  1758. convert_to_string(&tmp_member);
  1759. member = &tmp_member;
  1760. }
  1761. ret = zend_hash_find(&php_snmp_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  1762. if (ret == SUCCESS && hnd->read_func) {
  1763. ret = hnd->read_func(obj, &retval TSRMLS_CC);
  1764. if (ret == SUCCESS) {
  1765. /* ensure we're creating a temporary variable */
  1766. Z_SET_REFCOUNT_P(retval, 0);
  1767. } else {
  1768. retval = EG(uninitialized_zval_ptr);
  1769. }
  1770. } else {
  1771. zend_object_handlers * std_hnd = zend_get_std_object_handlers();
  1772. retval = std_hnd->read_property(object, member, type, key TSRMLS_CC);
  1773. }
  1774. if (member == &tmp_member) {
  1775. zval_dtor(member);
  1776. }
  1777. return(retval);
  1778. }
  1779. /* }}} */
  1780. /* {{{ php_snmp_write_property(zval *object, zval *member, zval *value[, const zend_literal *key])
  1781. Generic object property writer */
  1782. void php_snmp_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
  1783. {
  1784. zval tmp_member;
  1785. php_snmp_object *obj;
  1786. php_snmp_prop_handler *hnd;
  1787. int ret;
  1788. if (Z_TYPE_P(member) != IS_STRING) {
  1789. tmp_member = *member;
  1790. zval_copy_ctor(&tmp_member);
  1791. convert_to_string(&tmp_member);
  1792. member = &tmp_member;
  1793. }
  1794. ret = FAILURE;
  1795. obj = (php_snmp_object *)zend_objects_get_address(object TSRMLS_CC);
  1796. ret = zend_hash_find(&php_snmp_properties, Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, (void **) &hnd);
  1797. if (ret == SUCCESS && hnd->write_func) {
  1798. hnd->write_func(obj, value TSRMLS_CC);
  1799. if (! PZVAL_IS_REF(value) && Z_REFCOUNT_P(value) == 0) {
  1800. Z_ADDREF_P(value);
  1801. zval_ptr_dtor(&value);
  1802. }
  1803. } else {
  1804. zend_object_handlers * std_hnd = zend_get_std_object_handlers();
  1805. std_hnd->write_property(object, member, value, key TSRMLS_CC);
  1806. }
  1807. if (member == &tmp_member) {
  1808. zval_dtor(member);
  1809. }
  1810. }
  1811. /* }}} */
  1812. /* {{{ php_snmp_has_property(zval *object, zval *member, int has_set_exists[, const zend_literal *key])
  1813. Generic object property checker */
  1814. static int php_snmp_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC)
  1815. {
  1816. php_snmp_prop_handler *hnd;
  1817. int ret = 0;
  1818. if (zend_hash_find(&php_snmp_properties, Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, (void **)&hnd) == SUCCESS) {
  1819. switch (has_set_exists) {
  1820. case 2:
  1821. ret = 1;
  1822. break;
  1823. case 0: {
  1824. zval *value = php_snmp_read_property(object, member, BP_VAR_IS, key TSRMLS_CC);
  1825. if (value != EG(uninitialized_zval_ptr)) {
  1826. ret = Z_TYPE_P(value) != IS_NULL? 1:0;
  1827. /* refcount is 0 */
  1828. Z_ADDREF_P(value);
  1829. zval_ptr_dtor(&value);
  1830. }
  1831. break;
  1832. }
  1833. default: {
  1834. zval *value = php_snmp_read_property(object, member, BP_VAR_IS, key TSRMLS_CC);
  1835. if (value != EG(uninitialized_zval_ptr)) {
  1836. convert_to_boolean(value);
  1837. ret = Z_BVAL_P(value)? 1:0;
  1838. /* refcount is 0 */
  1839. Z_ADDREF_P(value);
  1840. zval_ptr_dtor(&value);
  1841. }
  1842. break;
  1843. }
  1844. }
  1845. } else {
  1846. zend_object_handlers * std_hnd = zend_get_std_object_handlers();
  1847. ret = std_hnd->has_property(object, member, has_set_exists, key TSRMLS_CC);
  1848. }
  1849. return ret;
  1850. }
  1851. /* }}} */
  1852. static HashTable *php_snmp_get_gc(zval *object, zval ***gc_data, int *gc_data_count TSRMLS_DC) /* {{{ */
  1853. {
  1854. *gc_data = NULL;
  1855. *gc_data_count = 0;
  1856. return zend_std_get_properties(object TSRMLS_CC);
  1857. }
  1858. /* }}} */
  1859. /* {{{ php_snmp_get_properties(zval *object)
  1860. Returns all object properties. Injects SNMP properties into object on first call */
  1861. static HashTable *php_snmp_get_properties(zval *object TSRMLS_DC)
  1862. {
  1863. php_snmp_object *obj;
  1864. php_snmp_prop_handler *hnd;
  1865. HashTable *props;
  1866. zval *val;
  1867. char *key;
  1868. uint key_len;
  1869. HashPosition pos;
  1870. ulong num_key;
  1871. obj = (php_snmp_object *)zend_objects_get_address(object TSRMLS_CC);
  1872. props = zend_std_get_properties(object TSRMLS_CC);
  1873. zend_hash_internal_pointer_reset_ex(&php_snmp_properties, &pos);
  1874. while (zend_hash_get_current_data_ex(&php_snmp_properties, (void**)&hnd, &pos) == SUCCESS) {
  1875. zend_hash_get_current_key_ex(&php_snmp_properties, &key, &key_len, &num_key, 0, &pos);
  1876. if (!hnd->read_func || hnd->read_func(obj, &val TSRMLS_CC) != SUCCESS) {
  1877. val = EG(uninitialized_zval_ptr);
  1878. Z_ADDREF_P(val);
  1879. }
  1880. zend_hash_update(props, key, key_len, (void *)&val, sizeof(zval *), NULL);
  1881. zend_hash_move_forward_ex(&php_snmp_properties, &pos);
  1882. }
  1883. return obj->zo.properties;
  1884. }
  1885. /* }}} */
  1886. /* {{{ */
  1887. static int php_snmp_read_info(php_snmp_object *snmp_object, zval **retval TSRMLS_DC)
  1888. {
  1889. zval *val;
  1890. MAKE_STD_ZVAL(*retval);
  1891. array_init(*retval);
  1892. if (snmp_object->session == NULL) {
  1893. return SUCCESS;
  1894. }
  1895. MAKE_STD_ZVAL(val);
  1896. ZVAL_STRINGL(val, snmp_object->session->peername, strlen(snmp_object->session->peername), 1);
  1897. add_assoc_zval(*retval, "hostname", val);
  1898. MAKE_STD_ZVAL(val);
  1899. ZVAL_LONG(val, snmp_object->session->remote_port);
  1900. add_assoc_zval(*retval, "port", val);
  1901. MAKE_STD_ZVAL(val);
  1902. ZVAL_LONG(val, snmp_object->session->timeout);
  1903. add_assoc_zval(*retval, "timeout", val);
  1904. MAKE_STD_ZVAL(val);
  1905. ZVAL_LONG(val, snmp_object->session->retries);
  1906. add_assoc_zval(*retval, "retries", val);
  1907. return SUCCESS;
  1908. }
  1909. /* }}} */
  1910. /* {{{ */
  1911. static int php_snmp_read_max_oids(php_snmp_object *snmp_object, zval **retval TSRMLS_DC)
  1912. {
  1913. MAKE_STD_ZVAL(*retval);
  1914. if (snmp_object->max_oids > 0) {
  1915. ZVAL_LONG(*retval, snmp_object->max_oids);
  1916. } else {
  1917. ZVAL_NULL(*retval);
  1918. }
  1919. return SUCCESS;
  1920. }
  1921. /* }}} */
  1922. #define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
  1923. static int php_snmp_read_##name(php_snmp_object *snmp_object, zval **retval TSRMLS_DC) \
  1924. { \
  1925. MAKE_STD_ZVAL(*retval); \
  1926. ZVAL_BOOL(*retval, snmp_object->name); \
  1927. return SUCCESS; \
  1928. }
  1929. PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(oid_increasing_check)
  1930. PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)
  1931. PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(enum_print)
  1932. #define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
  1933. static int php_snmp_read_##name(php_snmp_object *snmp_object, zval **retval TSRMLS_DC) \
  1934. { \
  1935. MAKE_STD_ZVAL(*retval); \
  1936. ZVAL_LONG(*retval, snmp_object->name); \
  1937. return SUCCESS; \
  1938. }
  1939. PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
  1940. PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
  1941. PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
  1942. /* {{{ */
  1943. static int php_snmp_write_info(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
  1944. {
  1945. php_error_docref(NULL TSRMLS_CC, E_WARNING, "info property is read-only");
  1946. return FAILURE;
  1947. }
  1948. /* }}} */
  1949. /* {{{ */
  1950. static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
  1951. {
  1952. zval ztmp;
  1953. int ret = SUCCESS;
  1954. if (Z_TYPE_P(newval) == IS_NULL) {
  1955. snmp_object->max_oids = 0;
  1956. return ret;
  1957. }
  1958. if (Z_TYPE_P(newval) != IS_LONG) {
  1959. ztmp = *newval;
  1960. zval_copy_ctor(&ztmp);
  1961. convert_to_long(&ztmp);
  1962. newval = &ztmp;
  1963. }
  1964. if (Z_LVAL_P(newval) > 0) {
  1965. snmp_object->max_oids = Z_LVAL_P(newval);
  1966. } else {
  1967. php_error_docref(NULL TSRMLS_CC, E_WARNING, "max_oids should be positive integer or NULL, got %ld", Z_LVAL_P(newval));
  1968. }
  1969. if (newval == &ztmp) {
  1970. zval_dtor(newval);
  1971. }
  1972. return ret;
  1973. }
  1974. /* }}} */
  1975. /* {{{ */
  1976. static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
  1977. {
  1978. zval ztmp;
  1979. int ret = SUCCESS;
  1980. if (Z_TYPE_P(newval) != IS_LONG) {
  1981. ztmp = *newval;
  1982. zval_copy_ctor(&ztmp);
  1983. convert_to_long(&ztmp);
  1984. newval = &ztmp;
  1985. }
  1986. if (Z_LVAL_P(newval) >= 0 && Z_LVAL_P(newval) <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
  1987. snmp_object->valueretrieval = Z_LVAL_P(newval);
  1988. } else {
  1989. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP value retrieval method '%ld'", Z_LVAL_P(newval));
  1990. ret = FAILURE;
  1991. }
  1992. if (newval == &ztmp) {
  1993. zval_dtor(newval);
  1994. }
  1995. return ret;
  1996. }
  1997. /* }}} */
  1998. #define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
  1999. static int php_snmp_write_##name(php_snmp_object *snmp_object, zval *newval TSRMLS_DC) \
  2000. { \
  2001. zval ztmp; \
  2002. if (Z_TYPE_P(newval) != IS_BOOL) { \
  2003. ztmp = *newval; \
  2004. zval_copy_ctor(&ztmp); \
  2005. convert_to_boolean(&ztmp); \
  2006. newval = &ztmp; \
  2007. } \
  2008. \
  2009. snmp_object->name = Z_LVAL_P(newval); \
  2010. \
  2011. if (newval == &ztmp) { \
  2012. zval_dtor(newval); \
  2013. } \
  2014. return SUCCESS; \
  2015. }
  2016. PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(quick_print)
  2017. PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)
  2018. PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
  2019. /* {{{ */
  2020. static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
  2021. {
  2022. zval ztmp;
  2023. int ret = SUCCESS;
  2024. if (Z_TYPE_P(newval) != IS_LONG) {
  2025. ztmp = *newval;
  2026. zval_copy_ctor(&ztmp);
  2027. convert_to_long(&ztmp);
  2028. newval = &ztmp;
  2029. }
  2030. switch(Z_LVAL_P(newval)) {
  2031. case NETSNMP_OID_OUTPUT_SUFFIX:
  2032. case NETSNMP_OID_OUTPUT_MODULE:
  2033. case NETSNMP_OID_OUTPUT_FULL:
  2034. case NETSNMP_OID_OUTPUT_NUMERIC:
  2035. case NETSNMP_OID_OUTPUT_UCD:
  2036. case NETSNMP_OID_OUTPUT_NONE:
  2037. snmp_object->oid_output_format = Z_LVAL_P(newval);
  2038. break;
  2039. default:
  2040. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP output print format '%ld'", Z_LVAL_P(newval));
  2041. ret = FAILURE;
  2042. break;
  2043. }
  2044. if (newval == &ztmp) {
  2045. zval_dtor(newval);
  2046. }
  2047. return ret;
  2048. }
  2049. /* }}} */
  2050. /* {{{ */
  2051. static int php_snmp_write_exceptions_enabled(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
  2052. {
  2053. zval ztmp;
  2054. int ret = SUCCESS;
  2055. if (Z_TYPE_P(newval) != IS_LONG) {
  2056. ztmp = *newval;
  2057. zval_copy_ctor(&ztmp);
  2058. convert_to_long(&ztmp);
  2059. newval = &ztmp;
  2060. }
  2061. snmp_object->exceptions_enabled = Z_LVAL_P(newval);
  2062. if (newval == &ztmp) {
  2063. zval_dtor(newval);
  2064. }
  2065. return ret;
  2066. }
  2067. /* }}} */
  2068. /* {{{ php_snmp_class_methods[] */
  2069. static zend_function_entry php_snmp_class_methods[] = {
  2070. PHP_ME(snmp, __construct, arginfo_snmp_create, ZEND_ACC_PUBLIC)
  2071. PHP_ME(snmp, close, arginfo_snmp_void, ZEND_ACC_PUBLIC)
  2072. PHP_ME(snmp, setSecurity, arginfo_snmp_setSecurity, ZEND_ACC_PUBLIC)
  2073. PHP_ME(snmp, get, arginfo_snmp_get, ZEND_ACC_PUBLIC)
  2074. PHP_ME(snmp, getnext, arginfo_snmp_get, ZEND_ACC_PUBLIC)
  2075. PHP_ME(snmp, walk, arginfo_snmp_walk, ZEND_ACC_PUBLIC)
  2076. PHP_ME(snmp, set, arginfo_snmp_set, ZEND_ACC_PUBLIC)
  2077. PHP_ME(snmp, getErrno, arginfo_snmp_void, ZEND_ACC_PUBLIC)
  2078. PHP_ME(snmp, getError, arginfo_snmp_void, ZEND_ACC_PUBLIC)
  2079. PHP_FE_END
  2080. };
  2081. #define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
  2082. { "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, php_snmp_write_##name }
  2083. const php_snmp_prop_handler php_snmp_property_entries[] = {
  2084. PHP_SNMP_PROPERTY_ENTRY_RECORD(info),
  2085. PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
  2086. PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
  2087. PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
  2088. PHP_SNMP_PROPERTY_ENTRY_RECORD(enum_print),
  2089. PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_output_format),
  2090. PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_increasing_check),
  2091. PHP_SNMP_PROPERTY_ENTRY_RECORD(exceptions_enabled),
  2092. { NULL, 0, NULL, NULL}
  2093. };
  2094. /* }}} */
  2095. /* {{{ PHP_MINIT_FUNCTION
  2096. */
  2097. PHP_MINIT_FUNCTION(snmp)
  2098. {
  2099. netsnmp_log_handler *logh;
  2100. zend_class_entry ce, cex;
  2101. le_snmp_session = zend_register_list_destructors_ex(php_snmp_session_destructor, NULL, PHP_SNMP_SESSION_RES_NAME, module_number);
  2102. init_snmp("snmpapp");
  2103. #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
  2104. /* Prevent update of the snmpapp.conf file */
  2105. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
  2106. #endif
  2107. /* Disable logging, use exit status'es and related variabled to detect errors */
  2108. shutdown_snmp_logging();
  2109. logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, LOG_ERR);
  2110. if (logh) {
  2111. logh->pri_max = LOG_ERR;
  2112. }
  2113. memcpy(&php_snmp_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  2114. php_snmp_object_handlers.read_property = php_snmp_read_property;
  2115. php_snmp_object_handlers.write_property = php_snmp_write_property;
  2116. php_snmp_object_handlers.has_property = php_snmp_has_property;
  2117. php_snmp_object_handlers.get_properties = php_snmp_get_properties;
  2118. php_snmp_object_handlers.get_gc = php_snmp_get_gc;
  2119. /* Register SNMP Class */
  2120. INIT_CLASS_ENTRY(ce, "SNMP", php_snmp_class_methods);
  2121. ce.create_object = php_snmp_object_new;
  2122. php_snmp_object_handlers.clone_obj = NULL;
  2123. php_snmp_ce = zend_register_internal_class(&ce TSRMLS_CC);
  2124. /* Register SNMP Class properties */
  2125. zend_hash_init(&php_snmp_properties, 0, NULL, NULL, 1);
  2126. PHP_SNMP_ADD_PROPERTIES(&php_snmp_properties, php_snmp_property_entries);
  2127. REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_SUFFIX", NETSNMP_OID_OUTPUT_SUFFIX, CONST_CS | CONST_PERSISTENT);
  2128. REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_MODULE", NETSNMP_OID_OUTPUT_MODULE, CONST_CS | CONST_PERSISTENT);
  2129. REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_FULL", NETSNMP_OID_OUTPUT_FULL, CONST_CS | CONST_PERSISTENT);
  2130. REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NUMERIC", NETSNMP_OID_OUTPUT_NUMERIC, CONST_CS | CONST_PERSISTENT);
  2131. REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_UCD", NETSNMP_OID_OUTPUT_UCD, CONST_CS | CONST_PERSISTENT);
  2132. REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NONE", NETSNMP_OID_OUTPUT_NONE, CONST_CS | CONST_PERSISTENT);
  2133. REGISTER_LONG_CONSTANT("SNMP_VALUE_LIBRARY", SNMP_VALUE_LIBRARY, CONST_CS | CONST_PERSISTENT);
  2134. REGISTER_LONG_CONSTANT("SNMP_VALUE_PLAIN", SNMP_VALUE_PLAIN, CONST_CS | CONST_PERSISTENT);
  2135. REGISTER_LONG_CONSTANT("SNMP_VALUE_OBJECT", SNMP_VALUE_OBJECT, CONST_CS | CONST_PERSISTENT);
  2136. REGISTER_LONG_CONSTANT("SNMP_BIT_STR", ASN_BIT_STR, CONST_CS | CONST_PERSISTENT);
  2137. REGISTER_LONG_CONSTANT("SNMP_OCTET_STR", ASN_OCTET_STR, CONST_CS | CONST_PERSISTENT);
  2138. REGISTER_LONG_CONSTANT("SNMP_OPAQUE", ASN_OPAQUE, CONST_CS | CONST_PERSISTENT);
  2139. REGISTER_LONG_CONSTANT("SNMP_NULL", ASN_NULL, CONST_CS | CONST_PERSISTENT);
  2140. REGISTER_LONG_CONSTANT("SNMP_OBJECT_ID", ASN_OBJECT_ID, CONST_CS | CONST_PERSISTENT);
  2141. REGISTER_LONG_CONSTANT("SNMP_IPADDRESS", ASN_IPADDRESS, CONST_CS | CONST_PERSISTENT);
  2142. REGISTER_LONG_CONSTANT("SNMP_COUNTER", ASN_GAUGE, CONST_CS | CONST_PERSISTENT);
  2143. REGISTER_LONG_CONSTANT("SNMP_UNSIGNED", ASN_UNSIGNED, CONST_CS | CONST_PERSISTENT);
  2144. REGISTER_LONG_CONSTANT("SNMP_TIMETICKS", ASN_TIMETICKS, CONST_CS | CONST_PERSISTENT);
  2145. REGISTER_LONG_CONSTANT("SNMP_UINTEGER", ASN_UINTEGER, CONST_CS | CONST_PERSISTENT);
  2146. REGISTER_LONG_CONSTANT("SNMP_INTEGER", ASN_INTEGER, CONST_CS | CONST_PERSISTENT);
  2147. REGISTER_LONG_CONSTANT("SNMP_COUNTER64", ASN_COUNTER64, CONST_CS | CONST_PERSISTENT);
  2148. REGISTER_SNMP_CLASS_CONST_LONG("VERSION_1", SNMP_VERSION_1);
  2149. REGISTER_SNMP_CLASS_CONST_LONG("VERSION_2c", SNMP_VERSION_2c);
  2150. REGISTER_SNMP_CLASS_CONST_LONG("VERSION_2C", SNMP_VERSION_2c);
  2151. REGISTER_SNMP_CLASS_CONST_LONG("VERSION_3", SNMP_VERSION_3);
  2152. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_NOERROR", PHP_SNMP_ERRNO_NOERROR);
  2153. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_ANY", PHP_SNMP_ERRNO_ANY);
  2154. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_GENERIC", PHP_SNMP_ERRNO_GENERIC);
  2155. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_TIMEOUT", PHP_SNMP_ERRNO_TIMEOUT);
  2156. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_ERROR_IN_REPLY", PHP_SNMP_ERRNO_ERROR_IN_REPLY);
  2157. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_OID_NOT_INCREASING", PHP_SNMP_ERRNO_OID_NOT_INCREASING);
  2158. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_OID_PARSING_ERROR", PHP_SNMP_ERRNO_OID_PARSING_ERROR);
  2159. REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_MULTIPLE_SET_QUERIES", PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES);
  2160. /* Register SNMPException class */
  2161. INIT_CLASS_ENTRY(cex, "SNMPException", NULL);
  2162. #ifdef HAVE_SPL
  2163. php_snmp_exception_ce = zend_register_internal_class_ex(&cex, spl_ce_RuntimeException, NULL TSRMLS_CC);
  2164. #else
  2165. php_snmp_exception_ce = zend_register_internal_class_ex(&cex, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  2166. #endif
  2167. return SUCCESS;
  2168. }
  2169. /* }}} */
  2170. /* {{{ PHP_MSHUTDOWN_FUNCTION
  2171. */
  2172. PHP_MSHUTDOWN_FUNCTION(snmp)
  2173. {
  2174. snmp_shutdown("snmpapp");
  2175. zend_hash_destroy(&php_snmp_properties);
  2176. return SUCCESS;
  2177. }
  2178. /* }}} */
  2179. /* {{{ PHP_MINFO_FUNCTION
  2180. */
  2181. PHP_MINFO_FUNCTION(snmp)
  2182. {
  2183. php_info_print_table_start();
  2184. php_info_print_table_row(2, "NET-SNMP Support", "enabled");
  2185. php_info_print_table_row(2, "NET-SNMP Version", netsnmp_get_version());
  2186. php_info_print_table_row(2, "PHP SNMP Version", PHP_SNMP_VERSION);
  2187. php_info_print_table_end();
  2188. }
  2189. /* }}} */
  2190. /* {{{ snmp_module_deps[]
  2191. */
  2192. #if ZEND_MODULE_API_NO >= 20050922
  2193. static const zend_module_dep snmp_module_deps[] = {
  2194. #ifdef HAVE_SPL
  2195. ZEND_MOD_REQUIRED("spl")
  2196. #endif
  2197. ZEND_MOD_END
  2198. };
  2199. #endif
  2200. /* }}} */
  2201. /* {{{ snmp_module_entry
  2202. */
  2203. zend_module_entry snmp_module_entry = {
  2204. #if ZEND_MODULE_API_NO >= 20050922
  2205. STANDARD_MODULE_HEADER_EX,
  2206. NULL,
  2207. snmp_module_deps,
  2208. #else
  2209. STANDARD_MODULE_HEADER,
  2210. #endif
  2211. "snmp",
  2212. snmp_functions,
  2213. PHP_MINIT(snmp),
  2214. PHP_MSHUTDOWN(snmp),
  2215. NULL,
  2216. NULL,
  2217. PHP_MINFO(snmp),
  2218. PHP_SNMP_VERSION,
  2219. PHP_MODULE_GLOBALS(snmp),
  2220. PHP_GINIT(snmp),
  2221. NULL,
  2222. NULL,
  2223. STANDARD_MODULE_PROPERTIES_EX
  2224. };
  2225. /* }}} */
  2226. #endif
  2227. /*
  2228. * Local variables:
  2229. * tab-width: 4
  2230. * c-basic-offset: 4
  2231. * End:
  2232. * vim600: sw=4 ts=4 fdm=marker
  2233. * vim<600: sw=4 ts=4
  2234. */