snmp.c 64 KB

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