Debian Codegraph @

agent/mibgroup/winExtDLL.c

raw
1 /**
2 * @brief winExtDLL Net-SNMP agent extension module.
3 *
4 * Copyright (c) 2006-2009 Alex Burger.
5 * Copyright (c) 2009-2021 Bart Van Assche <[email protected]>.
6 *
7 * This Net-SNMP agent extension module loads Windows SNMP Extension Agent
8 * DLLs in the Net-SNMP agent. Not only extension DLLs provided with Windows
9 * (e.g. hostmib.dll) but also third-party extension DLLs are supported. This
10 * allows Net-SNMP to be a replacement for the Windows SNMP service, and makes
11 * it possible to use the SNMPv3 protocol.
12 *
13 * @see See also <a href="http://msdn.microsoft.com/en-us/library/aa378988(VS.85).aspx">SNMP Functions</a>
14 * for more information about Microsoft's SNMP Extension Agent API.
15 *
16 * @note In order to use this agent extension module, the Windows SNMP service
17 * must be installed first and must be disabled. Installing the Windows SNMP
18 * service is the only way to install the Windows Extension DLLs and to make
19 * sure that information about these DLLs is present in the registry.
20 *
21 * @note All Windows extension DLLs are loaded during startup of the Net-SNMP
22 * service. The Net-SNMP service must be restarted to load new modules. This
23 * extension is NOT for dynamically loading Net-SNMP extensions.
24 *
25 *
26 * History:
27 * - 2010/03/19:
28 * * Multi-varbind set request PDUs are now handled correctly.
29 * * If loading an extension DLL fails, the reason why this failed is now
30 * logged.
31 * * Fixed a memory leak that occurred when SnmpExtensionQuery() or
32 * SnmpExtensionQueryEx() failed while processing an SNMP PDU. Note:
33 * occurrence of an SNMP error does not make these functions fail, and
34 * it is not yet known whether or not it was possible to trigger this
35 * memory leak.
36 * - 2010/03/17: Fixed bug 2971257. Multi-varbind getNext requests with OIDs
37 * in reverse lexicographical order are again processed correctly.
38 * - 2010/01/22: Compiles now with MinGW too.
39 * - 2009/12/11:
40 * * The value of sysUpTime.0 reported by inetmib1.dll is now correct.
41 * * A linkUp or linkDown trap is now sent after the status of a network
42 * interface has changed.
43 * - 2009/03/26:
44 * * Removed several artificial limits. Result: more than 100 SNMP extension
45 * DLLs can now be loaded simultaneously and more than 100 OID ranges can
46 * now be registered. Loading e.g. the Dell OpenManage SNMP extension DLL
47 * does no longer crash Net-SNMP.
48 * * Number of OID ranges registered during startup is now logged.
49 * * It is no longer attempted to free the Broadcom SNMP extension DLLs
50 * bcmif.dll and baspmgnt.dll since doing so triggers a deadlock.
51 * * Added support for reregistration of an OID prefix. As an example, both
52 * both Microsoft's inetmib1.dll and the Eicon Diva divasnmpx.dll register
53 * the OID prefix iso.org.dod.internet.mgmt.mib-2.interfaces
54 * (.1.3.6.1.2.1.2). WinExtDLL will process OIDs with this prefix by using
55 * the handler that was registered last for the OID prefix. A message will
56 * be logged indicating that a handler has been replaced.
57 * - 2009/03/10:
58 * * Fixed several bugs in var_winExtDLL(): looking up extension DLL info
59 * based on the OID in a varbind is wrong. It does happen during GetNext
60 * processing that Net-SNMP passes intentionally varbinds to a handler
61 * with OIDs that are outside the range registered by the handler. Fixed
62 * this by filling in a pointer to the extension DLL info in
63 * netsnmp_mib_handler::myvoid and by using that information in the
64 * var_winExtDLL() handler function.
65 * * SetRequest PDUs are now passed once to an extension DLL instead of
66 * four times.
67 * * The error status and error index of a multi-varbind set request is now
68 * filled in correctly.
69 * * Added support for the SNMP extension DLL three-phase SNMP set.
70 * * Made traps SNMPv2 compliant by adding the sysUpTime.0 varbind.
71 * * The varbind list generated by extension DLLs for e.g. linkUp and
72 * linkDown traps is now passed to Net-SNMP. Previously this varbind list
73 * was discarded for generic traps.
74 * * Fixed memory leaks triggered by Get and GetNext PDU processing.
75 * * Added missing RegCloseKey() calls.
76 * * Added shutdown function shutdown_winExtDLL().
77 * * Replaced #include <cstdio> by #include <stdio.h> such that this source
78 * file compiles with Visual Studio 2005.
79 * * Removed many unused local variables.
80 * * Fixed several other compiler warnings.
81 * - 2006/09/09: creation of this file.
82 */
83
84 #include <net-snmp/net-snmp-config.h>
85 #include <net-snmp/net-snmp-features.h>
86 #include <net-snmp/agent/mib_module_config.h>
87 #include <net-snmp/agent/mib_modules.h>
88
89 #ifdef USING_WINEXTDLL_MODULE
90
91 #include <net-snmp/types.h>
92
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <time.h>
97 #include <windows.h>
98 #include <winerror.h>
99 #include "../../win32/Snmp-winExtDLL.h"
100
101 #include <net-snmp/net-snmp-includes.h>
102 #include <net-snmp/library/snmp_assert.h>
103 #include <net-snmp/agent/net-snmp-agent-includes.h>
104 #include "util_funcs.h"
105 #include "winExtDLL.h"
106
107 netsnmp_feature_require(oid_is_subtree);
108
109
110 #define MAX_VALUE_NAME 16383
111 #define MS_ASN_UINTEGER32 MS_ASN_UNSIGNED32
112
113
114 typedef BOOL(WINAPI *
115 PFNSNMPEXTENSIONINIT) (DWORD dwUpTimeReference,
116 HANDLE * phSubagentTrapEvent,
117 AsnObjectIdentifier *
118 pFirstSupportedRegion);
119
120 typedef BOOL(WINAPI *
121 PFNSNMPEXTENSIONINITEX) (AsnObjectIdentifier *
122 pNextSupportedRegion);
123
124 typedef BOOL(WINAPI *
125 PFNSNMPEXTENSIONMONITOR) (LPVOID pAgentMgmtData);
126
127 typedef BOOL(WINAPI * PFNSNMPEXTENSIONQUERY) (BYTE bPduType,
128 SnmpVarBindList *
129 pVarBindList,
130 AsnInteger32 *
131 pErrorStatus,
132 AsnInteger32 *
133 pErrorIndex);
134
135 typedef BOOL(WINAPI * PFNSNMPEXTENSIONQUERYEX) (UINT nRequestType,
136 UINT
137 nTransactionId,
138 SnmpVarBindList *
139 pVarBindList,
140 AsnOctetString *
141 pContextInfo,
142 AsnInteger32 *
143 pErrorStatus,
144 AsnInteger32 *
145 pErrorIndex);
146
147 typedef BOOL(WINAPI * PFNSNMPEXTENSIONTRAP) (AsnObjectIdentifier *
148 pEnterpriseOid,
149 AsnInteger32 *
150 pGenericTrapId,
151 AsnInteger32 *
152 pSpecificTrapId,
153 AsnTimeticks *
154 pTimeStamp,
155 SnmpVarBindList *
156 pVarBindList);
157
158 typedef VOID(WINAPI * PFNSNMPEXTENSIONCLOSE) (void);
159
160 typedef BOOL (WINAPI *pfIsWow64Process)(HANDLE hProcess, BOOL *Wow64Process);
161
162
163 /**
164 * Extensible array, a data structure similar to the C++ STL class
165 * std::vector<>.
166 */
167 typedef struct {
168 /** Pointer to the memory allocated for the array. */
169 void *p;
170 /** Number of bytes occupied by a single element. */
171 size_t elem_size;
172 /** Number of elements that have been allocated. */
173 int reserved;
174 /** Number of elements currently in use. */
175 int size;
176 } xarray;
177
178 /**
179 * Information managed by winExtDLL about Windows SNMP extension DLL's.
180 */
181 typedef struct {
182 char *dll_name; /**< Dynamically allocated DLL name. */
183 HANDLE dll_handle; /**< DLL handle. */
184 PFNSNMPEXTENSIONINIT pfSnmpExtensionInit;
185 PFNSNMPEXTENSIONINITEX pfSnmpExtensionInitEx;
186 PFNSNMPEXTENSIONCLOSE pfSnmpExtensionClose;
187 PFNSNMPEXTENSIONQUERY pfSnmpExtensionQuery;
188 PFNSNMPEXTENSIONQUERYEX pfSnmpExtensionQueryEx;
189 PFNSNMPEXTENSIONTRAP pfSnmpExtensionTrap;
190 HANDLE subagentTrapEvent;
191 } winextdll;
192
193 /**
194 * Information managed by winExtDLL about a single view of a Windows SNMP
195 * extension DLL.
196 */
197 typedef struct {
198 winextdll *winextdll_info;
199 netsnmp_handler_registration *my_handler;
200 oid name[MAX_OID_LEN]; /**< OID of this view. */
201 size_t name_length;
202 } winextdll_view;
203
204 /**
205 * Per varbind SNMP extension DLL context information for SNMP set operations.
206 */
207 typedef struct context_info_s {
208 struct context_info_s *next;
209 int index;
210 AsnOctetString context_info;
211 } context_info;
212
213
214 /*
215 * External function declarations.
216 */
217 void __declspec(dllimport) WINAPI SnmpSvcInitUptime(void);
218
219
220 /*
221 * Local functions declarations.
222 */
223 static int basename_equals(const char *path, const char *basename);
224 static int register_netsnmp_handler(winextdll_view *
225 const ext_dll_view_info);
226 static void read_extension_dlls_from_registry(void);
227 static void read_extension_dlls_from_registry_at(const char *const subkey);
228 static char *read_extension_dll_path_from_registry(const TCHAR *);
229 static void subagentTrapCheck(unsigned int clientreg, void *clientarg);
230 static int var_winExtDLL(netsnmp_mib_handler *handler,
231 netsnmp_handler_registration *reginfo,
232 netsnmp_agent_request_info *reqinfo,
233 netsnmp_request_info *requests);
234 static int append_windows_varbind_list(netsnmp_variable_list **
235 const net_snmp_varbinds,
236 const SnmpVarBindList *
237 const win_varbinds);
238 static int append_windows_varbind(netsnmp_variable_list **
239 const net_snmp_varbinds,
240 const SnmpVarBind *
241 const win_varbind);
242 static int convert_to_windows_varbind_list(SnmpVarBindList *
243 pVarBindList,
244 netsnmp_variable_list *
245 netsnmp_varbinds);
246 static int convert_win_snmp_err(const int win_snmp_err);
247 static winextdll_view *lookup_view_by_oid(oid * const name,
248 const size_t name_len);
249 static int snmp_oid_compare_n_w(const oid * name1, size_t len1,
250 const UINT * name2, UINT len2);
251 static int snmp_oid_compare_w_n(const UINT * name1, UINT len1,
252 const oid * name2, size_t len2);
253 static int netsnmp_oid_is_subtree_n_w(const oid * name1, size_t len1,
254 const UINT * name2, UINT len2);
255 static void copy_oid(oid * const to_name, size_t * const to_name_len,
256 const oid * const from_name,
257 const size_t from_name_len);
258 static void copy_oid_n_w(oid * const to_name, size_t * const to_name_len,
259 const UINT * const from_name,
260 const UINT from_name_len);
261 static UINT *copy_oid_to_new_windows_oid(AsnObjectIdentifier *
262 const windows_oid,
263 const oid * const name,
264 const size_t name_len);
265 static int snmp_set_var_objid_w(netsnmp_variable_list * var,
266 const UINT * name, UINT name_length);
267 static netsnmp_variable_list *
268 snmp_varlist_add_variable_w(netsnmp_variable_list ** varlist,
269 const UINT * name, UINT name_length,
270 u_char type, const void * value, size_t len);
271 static void send_trap(const AsnObjectIdentifier * const,
272 const AsnInteger, const AsnInteger,
273 const AsnTimeticks,
274 const SnmpVarBindList * const);
275 static u_char *winsnmp_memdup(const void *src, const size_t len);
276 #if 0
277 static void xarray_init(xarray * a, size_t elem_size);
278 #endif
279 static void xarray_destroy(xarray * a);
280 static void *xarray_push_back(xarray * a, const void *elem);
281 #if 0
282 static void xarray_erase(xarray * a, void *const elem);
283 #endif
284 static void *xarray_reserve(xarray * a, int reserved);
285
286
287 /*
288 * Local variable definitions.
289 */
290 #define WINEXTDLL(i) ((winextdll*)s_winextdll.p)[i]
291 #define WINEXTDLL_VIEW(i) ((winextdll_view*)s_winextdll_view.p)[i]
292 #define TRAPEVENT(i) ((HANDLE*)s_trapevent.p)[i]
293 #define TRAPEVENT_TO_DLLINFO(i) ((winextdll**)s_trapevent_to_dllinfo.p)[i]
294 static const oid mibii_system_mib[] = { 1, 3, 6, 1, 2, 1, 1 };
295 static const oid lmmib2_mib[] = { 1, 3, 6, 1, 4, 1, 77 };
296 static OSVERSIONINFO s_versioninfo = { sizeof(s_versioninfo) };
297 static xarray s_winextdll = { 0, sizeof(winextdll) };
298 static xarray s_winextdll_view = { 0, sizeof(winextdll_view) };
299 static xarray s_trapevent = { 0, sizeof(HANDLE) };
300 static xarray s_trapevent_to_dllinfo = { 0, sizeof(winextdll *) };
301 static context_info *context_info_head;
302
303
304 /*
305 * Function definitions.
306 */
307
308 /** Initialize the winExtDLL extension agent. */
309 void
310 init_winExtDLL(void)
311 {
312 BOOL result, is_wow64_process = FALSE;
313 int i;
314 uint32_t uptime_reference;
315 pfIsWow64Process IsWow64Process;
316
317 DEBUGMSG(("winExtDLL", "init_winExtDLL started.\n"));
318
319 GetVersionEx(&s_versioninfo);
320
321 IsWow64Process = (pfIsWow64Process)(uintptr_t)
322 GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
323 if (IsWow64Process)
324 (*IsWow64Process)(GetCurrentProcess(), &is_wow64_process);
325
326 SnmpSvcInitUptime();
327
328 read_extension_dlls_from_registry();
329
330 DEBUGMSG(("winExtDLL",
331 "init_winExtDLL: found %d extension DLLs in the registry.\n",
332 s_winextdll.size));
333
334 xarray_reserve(&s_winextdll, 128);
335
336 /*
337 * Load all the DLLs
338 */
339 for (i = 0; i < s_winextdll.size; i++) {
340 winextdll *const ext_dll_info = &WINEXTDLL(i);
341 AsnObjectIdentifier view;
342 winextdll_view ext_dll_view_info;
343 const char *dll_basename, *last_slash, *last_backslash;
344
345 netsnmp_assert(ext_dll_info);
346 if (!ext_dll_info->dll_name)
347 continue;
348
349 last_slash = strrchr(ext_dll_info->dll_name, '/');
350 last_backslash = strrchr(ext_dll_info->dll_name, '\\');
351 if (last_slash && last_backslash)
352 dll_basename = last_slash > last_backslash ? last_slash + 1:
353 last_backslash + 1;
354 else if (last_slash)
355 dll_basename = last_slash + 1;
356 else if (last_backslash)
357 dll_basename = last_backslash + 1;
358 else
359 dll_basename = ext_dll_info->dll_name;
360 DEBUGMSG(("winExtDLL", "dll_basename = %s\n", dll_basename));
361 if (!should_init(dll_basename)) {
362 DEBUGMSG(("winExtDLL", "Skipping DLL %s.\n",
363 ext_dll_info->dll_name));
364 continue;
365 }
366 DEBUGMSG(("winExtDLL", "loading DLL %s.\n",
367 ext_dll_info->dll_name));
368 ext_dll_info->dll_handle = LoadLibrary(ext_dll_info->dll_name);
369
370 if (ext_dll_info->dll_handle == NULL) {
371 const DWORD dwErrorcode = GetLastError();
372 LPTSTR lpMsgBuf;
373
374 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
375 FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwErrorcode,
376 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
377 (LPTSTR) & lpMsgBuf, 0, NULL);
378 if (lpMsgBuf) {
379 LPTSTR p;
380
381 /*
382 * Remove trailing "\r\n".
383 */
384 p = strchr(lpMsgBuf, '\r');
385 if (p)
386 *p = '\0';
387 }
388 snmp_log(LOG_ERR,
389 "init_winExtDLL: could not load SNMP extension DLL %s: %s\n",
390 ext_dll_info->dll_name, lpMsgBuf ? lpMsgBuf : "(?)");
391 if (lpMsgBuf)
392 LocalFree(lpMsgBuf);
393 continue;
394 }
395
396 /*
397 * Store DLL name and functions in s_extension_dll_info array.
398 */
399 ext_dll_info->pfSnmpExtensionInit = (PFNSNMPEXTENSIONINIT)(uintptr_t)
400 GetProcAddress(ext_dll_info->dll_handle, "SnmpExtensionInit");
401 ext_dll_info->pfSnmpExtensionInitEx =
402 (PFNSNMPEXTENSIONINITEX)(uintptr_t)
403 GetProcAddress(ext_dll_info->dll_handle, "SnmpExtensionInitEx");
404 ext_dll_info->pfSnmpExtensionClose = (PFNSNMPEXTENSIONCLOSE)
405 GetProcAddress(ext_dll_info->dll_handle, "SnmpExtensionClose");
406 ext_dll_info->pfSnmpExtensionQuery = (PFNSNMPEXTENSIONQUERY)(uintptr_t)
407 GetProcAddress(ext_dll_info->dll_handle, "SnmpExtensionQuery");
408 ext_dll_info->pfSnmpExtensionQueryEx =
409 (PFNSNMPEXTENSIONQUERYEX)(uintptr_t)
410 GetProcAddress(ext_dll_info->dll_handle, "SnmpExtensionQueryEx");
411 ext_dll_info->pfSnmpExtensionTrap = (PFNSNMPEXTENSIONTRAP)(uintptr_t)
412 GetProcAddress(ext_dll_info->dll_handle, "SnmpExtensionTrap");
413
414
415 if (ext_dll_info->pfSnmpExtensionQuery == NULL
416 && ext_dll_info->pfSnmpExtensionQueryEx == NULL) {
417 snmp_log(LOG_ERR,
418 "error in extension DLL %s: SNMP query function missing.\n",
419 ext_dll_info->dll_name);
420 }
421
422 /*
423 * Init and get first supported view from Windows SNMP extension DLL.
424 * Note: although according to the documentation of SnmpExtensionInit()
425 * the first argument of this function should be ignored by extension
426 * DLLs, passing a correct value for this first argument is necessary
427 * to make inetmib1.dll work correctly. Passing zero as the first
428 * argument causes inetmib1.dll to report an incorrect value for
429 * sysUpTime.0 and also causes the same DLL not to send linkUp or
430 * linkDown traps.
431 */
432 ext_dll_info->subagentTrapEvent = NULL;
433 view.idLength = 0;
434 view.ids = NULL;
435 if (!is_wow64_process && s_versioninfo.dwMajorVersion >= 6)
436 uptime_reference = GetTickCount() - 10 * SnmpSvcGetUptime();
437 else
438 uptime_reference = GetTickCount() / 10;
439 result =
440 ext_dll_info->pfSnmpExtensionInit(uptime_reference,
441 &ext_dll_info->
442 subagentTrapEvent, &view);
443 DEBUGMSG(("winExtDLL",
444 "init_winExtDLL: DLL %s initialization result %d\n",
445 ext_dll_info->dll_name, result));
446
447 if (ext_dll_info->subagentTrapEvent != NULL) {
448 xarray_push_back(&s_trapevent,
449 &ext_dll_info->subagentTrapEvent);
450 xarray_push_back(&s_trapevent_to_dllinfo, &ext_dll_info);
451 }
452
453 memset(&ext_dll_view_info, 0, sizeof(ext_dll_view_info));
454 ext_dll_view_info.winextdll_info = ext_dll_info;
455 DEBUGMSG(("winExtDLL",
456 "init_winExtDLL: DLL %s view length %d\n",
457 ext_dll_info->dll_name, view.idLength));
458 if (view.idLength) {
459 copy_oid_n_w(ext_dll_view_info.name,
460 &ext_dll_view_info.name_length,
461 view.ids, view.idLength);
462 xarray_push_back(&s_winextdll_view, &ext_dll_view_info);
463 }
464
465 /*
466 * Loop looking for more supported views.
467 */
468 while (ext_dll_info->pfSnmpExtensionInitEx
469 && ext_dll_info->pfSnmpExtensionInitEx(&view)) {
470 DEBUGMSG(("winExtDLL",
471 "init_winExtDLL: DLL %s view length %d\n",
472 ext_dll_info->dll_name, view.idLength));
473 memset(&ext_dll_view_info, 0, sizeof(ext_dll_view_info));
474 ext_dll_view_info.winextdll_info = ext_dll_info;
475 copy_oid_n_w(ext_dll_view_info.name,
476 &ext_dll_view_info.name_length, view.ids,
477 view.idLength);
478 xarray_push_back(&s_winextdll_view, &ext_dll_view_info);
479 }
480 }
481
482 /*
483 * Note: since register_netsnmp_handler() writes a pointer to the
484 * winextdll_view in one of the Net-SNMP data structures, it is not
485 * allowed to move winextdll_view data structures in memory after
486 * registration with Net-SNMP. Or: register_snmp_handler() must be called
487 * only once it is sure that the size of array s_winextdll_view won't change
488 * anymore.
489 */
490 for (i = 0; i < s_winextdll_view.size; i++)
491 register_netsnmp_handler(&WINEXTDLL_VIEW(i));
492
493 DEBUGMSG(("winExtDLL",
494 "init_winExtDLL: registered %d OID ranges.\n",
495 s_winextdll_view.size));
496
497 /*
498 * Let Net-SNMP call subagentTrapCheck() once per second.
499 */
500 if (s_trapevent.size)
501 snmp_alarm_register(1, SA_REPEAT, subagentTrapCheck, NULL);
502
503 DEBUGMSG(("winExtDLL", "init_winExtDLL finished.\n"));
504 }
505
506 void
507 shutdown_winExtDLL(void)
508 {
509 int i;
510
511 DEBUGMSG(("winExtDLL", "shutdown_winExtDLL() started.\n"));
512
513 for (i = s_winextdll_view.size - 1; i >= 0; i--) {
514 winextdll_view *const v = &WINEXTDLL_VIEW(i);
515 if (v && v->my_handler) {
516 DEBUGIF("winExtDLL") {
517 DEBUGMSG(("winExtDLL",
518 "unregistering handler for DLL %s and OID prefix ",
519 v->winextdll_info->dll_name));
520 DEBUGMSGOID(("winExtDLL", v->name, v->name_length));
521 DEBUGMSG(("winExtDLL", " ("));
522 DEBUGMSGSUBOID(("winExtDLL", v->name, v->name_length));
523 DEBUGMSG(("winExtDLL", ").\n"));
524 }
525 netsnmp_unregister_handler(v->my_handler);
526 }
527 }
528 xarray_destroy(&s_winextdll_view);
529
530 for (i = s_winextdll.size - 1; i >= 0; i--) {
531 winextdll *const ext_dll_info = &WINEXTDLL(i);
532 if (ext_dll_info->dll_handle) {
533 if (ext_dll_info->pfSnmpExtensionClose) {
534 DEBUGMSG(("winExtDLL", "closing %s.\n",
535 ext_dll_info->dll_name));
536 ext_dll_info->pfSnmpExtensionClose();
537 }
538 /*
539 * Freeing the Broadcom SNMP extension libraries triggers
540 * a deadlock, so skip bcmif.dll and baspmgnt.dll.
541 */
542 if (!basename_equals(ext_dll_info->dll_name, "bcmif.dll")
543 && !basename_equals(ext_dll_info->dll_name, "baspmgnt.dll")) {
544 DEBUGMSG(("winExtDLL", "unloading %s.\n",
545 ext_dll_info->dll_name));
546 FreeLibrary(ext_dll_info->dll_handle);
547 }
548 }
549 free(ext_dll_info->dll_name);
550 }
551 xarray_destroy(&s_winextdll);
552
553 xarray_destroy(&s_trapevent_to_dllinfo);
554
555 xarray_destroy(&s_trapevent);
556
557 DEBUGMSG(("winExtDLL", "shutdown_winExtDLL() finished.\n"));
558 }
559
560 /**
561 * Compare the basename of a path with a given string.
562 *
563 * @return 1 if the basename matches, 0 if not.
564 */
565 static int
566 basename_equals(const char *path, const char *basename)
567 {
568 const size_t path_len = strlen(path);
569 const size_t basename_len = strlen(basename);
570
571 netsnmp_assert(strchr(path, '/') == 0);
572 netsnmp_assert(strchr(basename, '/') == 0);
573 netsnmp_assert(strchr(basename, '\\') == 0);
574
575 return path_len >= basename_len + 1
576 && path[path_len - basename_len - 1] == '\\'
577 && strcasecmp(path + path_len - basename_len, basename) == 0;
578 }
579
580 /**
581 * Register a single OID subtree with Net-SNMP.
582 *
583 * @return 1 if successful, 0 if not.
584 */
585 int
586 register_netsnmp_handler(winextdll_view * const ext_dll_view_info)
587 {
588 winextdll *ext_dll_info;
589 winextdll_view *previously_registered_view;
590
591 ext_dll_info = ext_dll_view_info->winextdll_info;
592
593 previously_registered_view
594 = lookup_view_by_oid(ext_dll_view_info->name,
595 ext_dll_view_info->name_length);
596
597 if (previously_registered_view) {
598 size_t oid_namelen, outlen;
599 char *oid_name;
600 int buffer_large_enough;
601
602 oid_namelen = 0;
603 outlen = 0;
604 oid_name = NULL;
605 buffer_large_enough =
606 sprint_realloc_objid((u_char **) & oid_name, &oid_namelen,
607 &outlen, 1, ext_dll_view_info->name,
608 ext_dll_view_info->name_length);
609 snmp_log(LOG_INFO, "OID range %s%s: replacing handler %s by %s.\n",
610 oid_name ? oid_name : "",
611 buffer_large_enough ? "" : " [TRUNCATED]",
612 previously_registered_view->winextdll_info->dll_name,
613 ext_dll_view_info->winextdll_info->dll_name);
614 if (oid_name)
615 free(oid_name);
616
617 previously_registered_view->winextdll_info = ext_dll_info;
618 memset(ext_dll_view_info, 0, sizeof(*ext_dll_view_info));
619 return 1;
620 } else {
621 /* Create handler registration */
622 ext_dll_view_info->my_handler
623 = netsnmp_create_handler_registration(ext_dll_info->dll_name,
624 var_winExtDLL,
625 ext_dll_view_info->name,
626 ext_dll_view_info->
627 name_length,
628 HANDLER_CAN_RWRITE);
629
630 if (ext_dll_view_info->my_handler) {
631 ext_dll_view_info->my_handler->handler->myvoid =
632 ext_dll_view_info;
633 if (netsnmp_register_handler(ext_dll_view_info->my_handler)
634 == MIB_REGISTERED_OK) {
635 DEBUGIF("winExtDLL") {
636 DEBUGMSG(("winExtDLL",
637 "registering handler for DLL %s and OID prefix ",
638 ext_dll_info->dll_name));
639 DEBUGMSGOID(("winExtDLL", ext_dll_view_info->name,
640 ext_dll_view_info->name_length));
641 DEBUGMSG(("winExtDLL", " ("));
642 DEBUGMSGSUBOID(("winExtDLL", ext_dll_view_info->name,
643 ext_dll_view_info->name_length));
644 DEBUGMSG(("winExtDLL", ").\n"));
645 }
646 return 1;
647 } else {
648 snmp_log(LOG_ERR,
649 "winExtDLL: handler registration failed for %s.\n",
650 ext_dll_info->dll_name);
651 ext_dll_view_info->my_handler = 0;
652 }
653 } else {
654 snmp_log(LOG_ERR, "handler creation failed.\n");
655 }
656 }
657
658 return 0;
659 }
660
661 /**
662 * Allocate SNMP extension DLL context information. Such context information
663 * is necessary to allow an extension DLL to process a set request.
664 *
665 * @param[in] index Varbind index in original PDU.
666 *
667 * @return NULL if context information for the specified index was already
668 * allocated, and otherwise a pointer to the newly allocated context
669 * information.
670 */
671 static context_info *
672 alloc_context_info(const int index)
673 {
674 context_info *p;
675
676 DEBUGMSG(("winExtDLL:context_info", "alloc_context_info(%d)\n",
677 index));
678
679 for (p = context_info_head; p; p = p->next) {
680 if (p->index == index) {
681 netsnmp_assert(FALSE);
682 return NULL;
683 }
684 }
685
686 p = calloc(1, sizeof(context_info));
687 p->next = context_info_head;
688 context_info_head = p;
689 p->index = index;
690
691 return p;
692 }
693
694 /**
695 * Deallocate SNMP extension DLL context information.
696 *
697 * @param[in] index Varbind index in original PDU.
698 */
699 static void
700 free_context_info(const int index)
701 {
702 context_info **pprev = &context_info_head;
703 context_info *p;
704
705 DEBUGMSG(("winExtDLL:context_info", "free_context_info(%d)\n", index));
706
707 for (p = context_info_head; p; p = p->next) {
708 if (p->index == index) {
709 *pprev = p->next;
710 free(p);
711 break;
712 }
713 pprev = &p->next;
714 }
715 }
716
717 /**
718 * Look up SNMP extension DLL context information.
719 *
720 * @param[in] index Varbind index in original PDU.
721 */
722 static AsnOctetString *
723 get_context_info(const int index)
724 {
725 context_info *p;
726
727 DEBUGMSG(("winExtDLL:context_info", "get_context_info(%d)\n", index));
728
729 for (p = context_info_head; p; p = p->next)
730 if (p->index == index)
731 return &p->context_info;
732
733 netsnmp_assert(FALSE);
734 return NULL;
735 }
736
737 /*
738 * Translate Net-SNMP request mode into an SnmpExtensionQuery() PDU type
739 * or into an SnmpExtensionQueryEx() request type.
740 */
741 static int
742 get_request_type(int mode, int request_type, UINT *nRequestType)
743 {
744 switch (request_type) {
745 case 0:
746 /* SnmpExtensionQuery() PDU type */
747 switch (mode) {
748 case MODE_GET:
749 *nRequestType = SNMP_PDU_GET;
750 return 1;
751 case MODE_GETNEXT:
752 *nRequestType = SNMP_PDU_GETNEXT;
753 return 1;
754 case MODE_SET_RESERVE1:
755 return 0;
756 case MODE_SET_RESERVE2:
757 return 0;
758 case MODE_SET_ACTION:
759 return 0;
760 case MODE_SET_UNDO:
761 return 0;
762 case MODE_SET_COMMIT:
763 *nRequestType = SNMP_PDU_SET;
764 return 1;
765 case MODE_SET_FREE:
766 return 0;
767 default:
768 DEBUGMSG(("winExtDLL", "internal error: invalid mode %d.\n", mode));
769 netsnmp_assert(0);
770 return 0;
771 }
772 case 1:
773 /* SnmpExtensionQueryEx() request type */
774 switch (mode) {
775 case MODE_GET:
776 *nRequestType = SNMP_EXTENSION_GET;
777 return 1;
778 case MODE_GETNEXT:
779 *nRequestType = SNMP_EXTENSION_GET_NEXT;
780 return 1;
781 case MODE_SET_RESERVE1:
782 *nRequestType = SNMP_EXTENSION_SET_TEST;
783 return 1;
784 case MODE_SET_RESERVE2:
785 return 0;
786 case MODE_SET_ACTION:
787 return 0;
788 case MODE_SET_UNDO:
789 *nRequestType = SNMP_EXTENSION_SET_UNDO;
790 return 1;
791 case MODE_SET_COMMIT:
792 *nRequestType = SNMP_EXTENSION_SET_COMMIT;
793 return 1;
794 case MODE_SET_FREE:
795 *nRequestType = SNMP_EXTENSION_SET_CLEANUP;
796 return 1;
797 default:
798 DEBUGMSG(("winExtDLL", "internal error: invalid mode %d.\n", mode));
799 netsnmp_assert(0);
800 return 0;
801 }
802 default:
803 DEBUGMSG(("winExtDLL", "internal error: invalid argument %d.\n",
804 request_type));
805 netsnmp_assert(0);
806 return 0;
807 }
808 }
809
810 static int
811 var_winExtDLL(netsnmp_mib_handler *handler,
812 netsnmp_handler_registration *reginfo,
813 netsnmp_agent_request_info *reqinfo,
814 netsnmp_request_info *requests)
815 {
816 winextdll_view *const ext_dll_view_info = handler->myvoid;
817 winextdll *ext_dll_info;
818 netsnmp_request_info *request;
819 UINT nRequestType;
820 int rc;
821
822 netsnmp_assert(ext_dll_view_info);
823 ext_dll_info = ext_dll_view_info->winextdll_info;
824 #if ! defined(NDEBUG)
825 netsnmp_assert(ext_dll_view_info ==
826 lookup_view_by_oid(reginfo->rootoid, reginfo->rootoid_len));
827 #endif
828
829 if (ext_dll_info == 0) {
830 DEBUGMSG(("winExtDLL",
831 "internal error: no matching extension DLL found.\n"));
832 netsnmp_assert(0);
833 return SNMP_ERR_GENERR;
834 }
835
836 if (!get_request_type(reqinfo->mode, !!ext_dll_info->pfSnmpExtensionQueryEx,
837 &nRequestType)) {
838 return SNMP_ERR_NOERROR;
839 }
840
841 rc = SNMP_ERR_NOERROR;
842
843 for (request = requests; request; request = request->next) {
844 netsnmp_variable_list *varbind;
845 SnmpVarBindList win_varbinds;
846 AsnInteger32 ErrorStatus;
847 AsnInteger32 ErrorIndex;
848 BOOL result;
849 BOOL copy_value;
850
851 memset(&win_varbinds, 0, sizeof(win_varbinds));
852
853 if (request->processed || rc != SNMP_ERR_NOERROR)
854 goto free_win_varbinds;
855
856 if (reqinfo->mode == MODE_SET_RESERVE1)
857 alloc_context_info(request->index);
858
859 varbind = request->requestvb;
860 netsnmp_assert(varbind);
861
862 retry:
863 /*
864 * Convert the Net-SNMP varbind to a Windows SNMP varbind list.
865 */
866 rc = convert_to_windows_varbind_list(&win_varbinds, varbind);
867 if (rc != SNMP_ERR_NOERROR) {
868 DEBUGMSG(("winExtDLL",
869 "converting varbind list to Windows format failed with error code %d.\n",
870 request->status));
871 netsnmp_request_set_error(requests, rc);
872 goto free_win_varbinds;
873 }
874
875 netsnmp_assert(win_varbinds.len == 1);
876
877 /*
878 * For a GetNext PDU, if the varbind OID comes lexicographically
879 * before the root OID of this handler, replace it by the root OID.
880 */
881 if (reqinfo->mode == MODE_GETNEXT
882 && snmp_oid_compare_w_n(win_varbinds.list[0].name.ids,
883 win_varbinds.list[0].name.idLength,
884 reginfo->rootoid,
885 reginfo->rootoid_len) < 0) {
886 DEBUGIF("winExtDLL") {
887 size_t oid1_namelen = 0, oid2_namelen = 0, outlen1 = 0,
888 outlen2 = 0;
889 char *oid1_name = NULL, *oid2_name = NULL;
890 int overflow1 = 0, overflow2 = 0;
891
892 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
893 netsnmp_sprint_realloc_objid((u_char **) & oid1_name,
894 &oid1_namelen, &outlen1, 1,
895 &overflow1, (const oid *)
896 win_varbinds.list[0].name.ids,
897 win_varbinds.list[0].name.idLength);
898 netsnmp_sprint_realloc_objid((u_char **) & oid2_name,
899 &oid2_namelen, &outlen2, 1,
900 &overflow2, reginfo->rootoid,
901 reginfo->rootoid_len);
902 DEBUGMSG(("winExtDLL",
903 "extension DLL %s: replacing OID %s%s by OID %s%s.\n",
904 ext_dll_info->dll_name,
905 oid1_name, overflow1 ? " [TRUNCATED]" : "",
906 oid2_name, overflow2 ? " [TRUNCATED]" : ""));
907 free(oid2_name);
908 free(oid1_name);
909 }
910
911 SnmpUtilOidFree(&win_varbinds.list[0].name);
912 memset(&win_varbinds.list[0].name, 0,
913 sizeof(win_varbinds.list[0].name));
914 copy_oid_to_new_windows_oid(&win_varbinds.list[0].name,
915 reginfo->rootoid,
916 reginfo->rootoid_len);
917 }
918
919 if (ext_dll_info->pfSnmpExtensionQueryEx) {
920 result = ext_dll_info->pfSnmpExtensionQueryEx(nRequestType,
921 1,
922 &win_varbinds,
923 get_context_info(request->index),
924 &ErrorStatus,
925 &ErrorIndex);
926 } else if (ext_dll_info->pfSnmpExtensionQuery) {
927 result =
928 ext_dll_info->pfSnmpExtensionQuery((BYTE) nRequestType,
929 &win_varbinds,
930 &ErrorStatus,
931 &ErrorIndex);
932 } else {
933 snmp_log(LOG_ERR,
934 "error in extension DLL %s: SNMP query function missing.\n",
935 ext_dll_info->dll_name);
936 result = FALSE;
937 }
938
939 if (!result) {
940 snmp_log(LOG_ERR,
941 "extension DLL %s: SNMP query function failed.\n",
942 ext_dll_info->dll_name);
943 rc = SNMP_ERR_GENERR;
944 goto free_win_varbinds;
945 }
946
947 rc = convert_win_snmp_err(ErrorStatus);
948 if (rc != SNMP_ERR_NOERROR) {
949 copy_oid_n_w(varbind->name, &varbind->name_length,
950 win_varbinds.list[0].name.ids,
951 win_varbinds.list[0].name.idLength);
952
953 DEBUGIF("winExtDLL") {
954 size_t oid_namelen = 0, outlen = 0;
955 char *oid_name = NULL;
956 int overflow = 0;
957
958 netsnmp_sprint_realloc_objid((u_char **) & oid_name,
959 &oid_namelen,
960 &outlen, 1, &overflow,
961 varbind->name,
962 varbind->name_length);
963 DEBUGMSG(("winExtDLL",
964 "extension DLL %s: SNMP query function returned error code %u (Windows) / %d (Net-SNMP) for request type %d, OID %s%s, ASN type %d and value %d.\n",
965 ext_dll_info->dll_name, (unsigned int)ErrorStatus, rc,
966 nRequestType, oid_name,
967 overflow ? " [TRUNCATED]" : "",
968 win_varbinds.list[0].value.asnType,
969 (unsigned int)win_varbinds.list[0].value.asnValue.number));
970 free(oid_name);
971 }
972 netsnmp_assert(ErrorIndex == 1);
973 if (rc == SNMP_ERR_GENERR &&
974 reqinfo->mode == MODE_GETNEXT &&
975 varbind->name_length > 0 &&
976 (snmp_oid_compare(varbind->name, varbind->name_length,
977 mibii_system_mib,
978 sizeof(mibii_system_mib) /
979 sizeof(mibii_system_mib[0])) == 0 ||
980 snmp_oidtree_compare(varbind->name, varbind->name_length,
981 lmmib2_mib,
982 sizeof(lmmib2_mib) /
983 sizeof(lmmib2_mib[0])) == 0)) {
984 // Quirk: ignore 'generic error' for the MIB-II system MIB OID.
985 DEBUGMSG(("winExtDLL", "Ignoring the above error\n"));
986 varbind->name[varbind->name_length - 1]++;
987 goto retry;
988 } else {
989 netsnmp_request_set_error(requests, rc);
990 if (rc == SNMP_NOSUCHOBJECT || rc == SNMP_NOSUCHINSTANCE ||
991 rc == SNMP_ERR_NOSUCHNAME)
992 rc = SNMP_ERR_NOERROR;
993 goto free_win_varbinds;
994 }
995 }
996
997 copy_value = FALSE;
998 if (reqinfo->mode == MODE_GET)
999 copy_value = TRUE;
1000 else if (reqinfo->mode == MODE_GETNEXT) {
1001 const SnmpVarBind *win_varbind;
1002
1003 win_varbind = &win_varbinds.list[0];
1004
1005 /*
1006 * Verify whether the OID returned by the extension DLL fits
1007 * inside the OID range this handler has been registered
1008 * with. Also compare the OID passed to the extension DLL with
1009 * the OID returned by the same DLL. If the DLL returned a
1010 * lexicographically earlier OID, this means that there is no
1011 * next OID in the MIB implemented by the DLL.
1012 *
1013 * Note: for some GetNext requests BoundsChecker will report
1014 * that the code below accesses a dangling pointer. This is
1015 * a limitation of BoundsChecker: apparently BoundsChecker is
1016 * not able to cope with reallocation of memory for
1017 * win_varbind by an SNMP extension DLL that has not been
1018 * instrumented by BoundsChecker.
1019 */
1020 if (netsnmp_oid_is_subtree_n_w(ext_dll_view_info->name,
1021 ext_dll_view_info->name_length,
1022 win_varbind->name.ids,
1023 win_varbind->name.idLength) == 0
1024 && snmp_oid_compare_n_w(varbind->name, varbind->name_length,
1025 win_varbind->name.ids,
1026 win_varbind->name.idLength) < 0) {
1027 /*
1028 * Copy the OID returned by the extension DLL to the
1029 * Net-SNMP varbind.
1030 */
1031 snmp_set_var_objid_w(varbind,
1032 win_varbind->name.ids,
1033 win_varbind->name.idLength);
1034 copy_value = TRUE;
1035 }
1036 }
1037 if (copy_value) {
1038 netsnmp_variable_list *result_vb;
1039
1040 /*
1041 * Copy the value returned by the extension DLL to the Net-SNMP
1042 * varbind.
1043 */
1044 result_vb = NULL;
1045 rc = append_windows_varbind(&result_vb, &win_varbinds.list[0]);
1046 netsnmp_assert(result_vb || rc != SNMP_ERR_NOERROR);
1047 if (result_vb) {
1048 snmp_set_var_typed_value(varbind,
1049 result_vb->type,
1050 result_vb->val.string,
1051 result_vb->val_len);
1052 snmp_free_varbind(result_vb);
1053 } else {
1054 netsnmp_request_set_error(requests, rc);
1055 goto free_win_varbinds;
1056 }
1057 }
1058
1059 free_win_varbinds:
1060 if (reqinfo->mode == MODE_SET_COMMIT
1061 || reqinfo->mode == MODE_SET_UNDO
1062 || reqinfo->mode == MODE_SET_FREE)
1063 free_context_info(request->index);
1064 if (win_varbinds.list)
1065 SnmpUtilVarBindListFree(&win_varbinds);
1066 }
1067
1068 return rc;
1069 }
1070
1071 /**
1072 * Iterate over the SNMP extension DLL information in the registry and store
1073 * the retrieved information in s_winextdll[].
1074 *
1075 * At the time an SNMP extension DLL is installed, some information about the
1076 * DLL is written to the registry at one of the two following locations:
1077 * HKLM\SYSTEM\CurrentControlSet\Control\SNMP\Parameters\ExtensionAgents for
1078 * Windows Vista, Windows 7 and Windows 2008 or
1079 * HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ExtensionAgents for
1080 * earlier Windows versions. Under this key zero or more REG_SZ values are
1081 * stored with the names of registry keys containing the DLL path.
1082 */
1083 void
1084 read_extension_dlls_from_registry()
1085 {
1086 DEBUGMSGTL(("winExtDLL",
1087 "read_extension_dlls_from_registry called\n"));
1088
1089 read_extension_dlls_from_registry_at
1090 ("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ExtensionAgents");
1091 read_extension_dlls_from_registry_at
1092 ("SYSTEM\\CurrentControlSet\\Control\\SNMP\\Parameters\\ExtensionAgents");
1093 }
1094
1095 void
1096 read_extension_dlls_from_registry_at(const char *const subkey)
1097 {
1098 DWORD retCode;
1099 HKEY hKey;
1100 int i;
1101 DWORD valueSize;
1102 TCHAR valueName[MAX_VALUE_NAME];
1103 DWORD dataType;
1104 TCHAR data[MAX_VALUE_NAME];
1105 DWORD dataSize;
1106
1107 retCode = RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey,
1108 0, KEY_QUERY_VALUE, &hKey);
1109
1110 if (retCode == ERROR_SUCCESS) {
1111 for (i = 0; ; i++) {
1112 valueSize = sizeof(valueName);
1113 dataSize = sizeof(data);
1114 retCode = RegEnumValue(hKey, i, valueName, &valueSize, NULL,
1115 &dataType, (BYTE *) data, &dataSize);
1116
1117 if (retCode != ERROR_SUCCESS)
1118 break;
1119 if (dataType == REG_SZ) {
1120 winextdll ext_dll_info;
1121
1122 memset(&ext_dll_info, 0, sizeof(ext_dll_info));
1123 ext_dll_info.dll_name =
1124 read_extension_dll_path_from_registry(data);
1125 if (ext_dll_info.dll_name) {
1126 xarray_push_back(&s_winextdll, &ext_dll_info);
1127 DEBUGMSG(("winExtDLL", "registry key %s: DLL %s.\n",
1128 data, ext_dll_info.dll_name));
1129 }
1130 }
1131 }
1132 RegCloseKey(hKey);
1133 }
1134 }
1135
1136 /** Store the DLL path in dynamically allocated memory. */
1137 char *
1138 read_extension_dll_path_from_registry(const TCHAR * keyName)
1139 {
1140 HKEY hKey;
1141 DWORD key_value_type = 0;
1142 TCHAR valueName[MAX_VALUE_NAME];
1143 DWORD key_value_size = MAX_VALUE_NAME;
1144 TCHAR valueNameExpanded[MAX_VALUE_NAME];
1145 DWORD retCode;
1146 char *result = 0;
1147
1148 retCode = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1149 keyName, 0, KEY_QUERY_VALUE, &hKey);
1150
1151 if (retCode != ERROR_SUCCESS)
1152 return 0;
1153
1154 retCode = RegQueryValueExA(hKey, "Pathname", NULL, &key_value_type,
1155 (BYTE *) valueName, &key_value_size);
1156
1157 if (retCode != ERROR_SUCCESS) {
1158 RegCloseKey(hKey);
1159 return 0;
1160 }
1161
1162 if (key_value_type == REG_EXPAND_SZ) {
1163 if (ExpandEnvironmentStrings
1164 (valueName, valueNameExpanded, MAX_VALUE_NAME))
1165 result = strdup(valueNameExpanded);
1166 } else if (key_value_type == REG_SZ)
1167 result = strdup(valueName);
1168
1169 RegCloseKey(hKey);
1170 return result;
1171 }
1172
1173 /**
1174 * Callback function called by the Net-SNMP agent to check for traps waiting
1175 * to be processed.
1176 */
1177 void
1178 subagentTrapCheck(unsigned int clientreg, void *clientarg)
1179 {
1180 while (1) {
1181 DWORD dwWaitResult;
1182 BOOL bResult;
1183 int i;
1184 int j;
1185 const winextdll *ext_dll_info;
1186
1187 if (s_trapevent.size == 0)
1188 return;
1189
1190 dwWaitResult = WaitForMultipleObjects(s_trapevent.size,
1191 &TRAPEVENT(0), FALSE, 0);
1192
1193 i = dwWaitResult - WAIT_OBJECT_0;
1194 if (i < 0 || i >= s_trapevent.size) {
1195 netsnmp_assert(dwWaitResult == WAIT_TIMEOUT);
1196 return;
1197 }
1198
1199 netsnmp_assert(s_trapevent.size == s_trapevent_to_dllinfo.size);
1200 ext_dll_info = TRAPEVENT_TO_DLLINFO(i);
1201 netsnmp_assert(ext_dll_info->subagentTrapEvent == TRAPEVENT(i));
1202
1203 /*
1204 * Reset the signalled event just in case the extension DLL erroneously
1205 * allocated a manual-reset event instead of an auto-reset event. It is
1206 * important to reset the event BEFORE traps are processed, otherwise a
1207 * race condition is triggered between the extension DLL setting the
1208 * event and this code resetting the event.
1209 */
1210 ResetEvent(TRAPEVENT(i));
1211
1212 if (!ext_dll_info->pfSnmpExtensionTrap) {
1213 snmp_log(LOG_ERR,
1214 "internal error in SNMP extension DLL %s: a trap is ready but the function SnmpExtensionTrap() is missing.\n",
1215 ext_dll_info->dll_name);
1216 return;
1217 }
1218
1219 /*
1220 * Process at most hundred traps per extension DLL. If the extension DLL
1221 * has more traps waiting, that's probably a bug in the extension DLL.
1222 */
1223 for (j = 0; j < 100; j++) {
1224 AsnObjectIdentifier Enterprise = { 0, NULL };
1225 AsnInteger GenericTrap = 0;
1226 AsnInteger SpecificTrap = 0;
1227 AsnTimeticks TimeStamp = 0;
1228 SnmpVarBindList TrapVarbinds = { NULL, 0 };
1229
1230 bResult = ext_dll_info->pfSnmpExtensionTrap(&Enterprise,
1231 &GenericTrap,
1232 &SpecificTrap,
1233 &TimeStamp,
1234 &TrapVarbinds);
1235
1236 if (!bResult)
1237 break;
1238
1239 send_trap(&Enterprise, GenericTrap, SpecificTrap, TimeStamp,
1240 &TrapVarbinds);
1241
1242 SnmpUtilVarBindListFree(&TrapVarbinds);
1243 }
1244 }
1245 }
1246
1247 void
1248 send_trap(const AsnObjectIdentifier * const pEnterprise,
1249 const AsnInteger GenericTrap,
1250 const AsnInteger SpecificTrap,
1251 const AsnTimeticks TimeStamp,
1252 const SnmpVarBindList * const pTrapVarbinds)
1253 {
1254 /*
1255 * A quote from the paragraph in RFC 1908 about SNMPv1 to SNMPv2c
1256 * trap translation (http://www.ietf.org/rfc/rfc1908.txt):
1257 * <quote>
1258 * If a Trap-PDU is received, then it is mapped into a SNMPv2-Trap-
1259 * PDU. This is done by prepending onto the variable-bindings field
1260 * two new bindings: sysUpTime.0 [6], which takes its value from the
1261 * timestamp field of the Trap-PDU; and, snmpTrapOID.0 [6], which is
1262 * calculated thusly: if the value of generic-trap field is
1263 * `enterpriseSpecific', then the value used is the concatenation of
1264 * the enterprise field from the Trap-PDU with two additional sub-
1265 * identifiers, `0', and the value of the specific-trap field;
1266 * otherwise, the value of the corresponding trap defined in [6] is
1267 * used.
1268 * </quote>
1269 *
1270 * Reference [6] refers to RFC 1907 (http://www.ietf.org/rfc/rfc1907.txt),
1271 * where the generic trap OIDs have been defined as follows:
1272 * coldStart ::= { snmpTraps 1 }
1273 * warmStart ::= { snmpTraps 2 }
1274 * linkDown ::= { snmpTraps 3 }
1275 * linkUp ::= { snmpTraps 4 }
1276 * authenticationFailure ::= { snmpTraps 5 }
1277 * egpNeighborLoss ::= { snmpTraps 6 }
1278 */
1279 static const oid sysuptime_oid[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
1280 static const size_t sysuptime_oid_len = OID_LENGTH(sysuptime_oid);
1281
1282 static const oid snmptrap_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
1283 static const size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
1284
1285 static const oid snmptraps_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 5 };
1286 static const size_t snmptraps_oid_len = OID_LENGTH(snmptraps_oid);
1287
1288 oid vb2_oid[MAX_OID_LEN];
1289 size_t vb2_oid_len;
1290
1291 netsnmp_variable_list *notification_vars = NULL;
1292
1293
1294 /*
1295 * Append the varbind (sysUpTime.0, TimeStamp).
1296 */
1297 snmp_varlist_add_variable(&notification_vars,
1298 sysuptime_oid, sysuptime_oid_len,
1299 ASN_TIMETICKS,
1300 (const u_char *) &TimeStamp,
1301 sizeof(TimeStamp));
1302
1303 if (GenericTrap == SNMP_GENERICTRAP_ENTERSPECIFIC) {
1304 /*
1305 * Enterprise specific trap: compute the OID
1306 * *pEnterprise + ".0." + SpecificTrap.
1307 */
1308 copy_oid_n_w(vb2_oid, &vb2_oid_len,
1309 pEnterprise->ids, pEnterprise->idLength);
1310 vb2_oid[vb2_oid_len++] = 0;
1311 vb2_oid[vb2_oid_len++] = SpecificTrap;
1312 } else {
1313 /*
1314 * Generic trap: compute the OID snmpTraps + "." + GenericTrap.
1315 * Since the GenericTrap values are those defined in SNMPv1, since
1316 * these values start at zero, and since the corresponding values in
1317 * SNMPv2 start at one, translate the GenericTrap value accordingly.
1318 * See also http://www.ietf.org/rfc/rfc1214.txt and
1319 * http://www.ietf.org/rfc/rfc3418.txt.
1320 */
1321 copy_oid(vb2_oid, &vb2_oid_len, snmptraps_oid, snmptraps_oid_len);
1322 vb2_oid[vb2_oid_len++] = GenericTrap + 1;
1323 }
1324
1325 /*
1326 * Append the varbind (snmpTrap, vb2_oid).
1327 */
1328 snmp_varlist_add_variable(&notification_vars,
1329 snmptrap_oid, snmptrap_oid_len,
1330 ASN_OBJECT_ID,
1331 (u_char *) vb2_oid,
1332 vb2_oid_len * sizeof(vb2_oid[0]));
1333
1334 /*
1335 * Append all the varbinds in pTrapVarbinds.
1336 */
1337 append_windows_varbind_list(&notification_vars, pTrapVarbinds);
1338
1339 /*
1340 * Send trap.
1341 */
1342 send_v2trap(notification_vars);
1343
1344 /*
1345 * Free the memory allocated for notification_vars.
1346 */
1347 snmp_free_varbind(notification_vars);
1348 }
1349
1350 /**
1351 * Convert a Windows varbind to a Net-SNMP varbind and add it to the list of
1352 * varbinds 'net_snmp_varbinds'.
1353 *
1354 * @note The memory allocated inside this function must be freed by the caller
1355 * as follows: snmp_free_varbind(*net_snmp_varbinds).
1356 */
1357 static int
1358 append_windows_varbind_list(netsnmp_variable_list **
1359 const net_snmp_varbinds,
1360 const SnmpVarBindList * const win_varbinds)
1361 {
1362 int i, status = SNMP_ERR_NOERROR;
1363
1364 for (i = 0; i < win_varbinds->len; i++) {
1365 status =
1366 append_windows_varbind(net_snmp_varbinds,
1367 &win_varbinds->list[i]);
1368 if (status != SNMP_ERR_NOERROR)
1369 break;
1370 }
1371 return status;
1372 }
1373
1374 static int
1375 append_windows_varbind(netsnmp_variable_list ** const net_snmp_varbinds,
1376 const SnmpVarBind * const win_varbind)
1377 {
1378 switch (win_varbind->value.asnType) {
1379 case MS_ASN_INTEGER:
1380 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1381 win_varbind->name.idLength,
1382 ASN_INTEGER,
1383 &win_varbind->value.asnValue.number,
1384 sizeof(win_varbind->value.asnValue.
1385 number));
1386 break;
1387 case MS_ASN_BITS:
1388 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1389 win_varbind->name.idLength,
1390 ASN_BIT_STR,
1391 win_varbind->value.asnValue.bits.stream,
1392 win_varbind->value.asnValue.bits.length);
1393 break;
1394 case MS_ASN_OCTETSTRING:
1395 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1396 win_varbind->name.idLength,
1397 ASN_OCTET_STR,
1398 win_varbind->value.asnValue.string.
1399 stream,
1400 win_varbind->value.asnValue.string.
1401 length);
1402 break;
1403 case MS_ASN_NULL:
1404 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1405 win_varbind->name.idLength,
1406 ASN_NULL, 0, 0);
1407 break;
1408 case MS_ASN_OBJECTIDENTIFIER:
1409 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1410 win_varbind->name.idLength,
1411 ASN_OBJECT_ID,
1412 win_varbind->value.asnValue.
1413 object.ids,
1414 win_varbind->value.asnValue.object.
1415 idLength * sizeof(oid));
1416 break;
1417
1418 /*
1419 * MS_ASN_INTEGER32: synonym for MS_ASN_INTEGER.
1420 */
1421
1422 case MS_ASN_SEQUENCE:
1423 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1424 win_varbind->name.idLength,
1425 ASN_SEQUENCE,
1426 win_varbind->value.asnValue.sequence.
1427 stream,
1428 win_varbind->value.asnValue.sequence.
1429 length);
1430 break;
1431 case MS_ASN_IPADDRESS:
1432 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1433 win_varbind->name.idLength,
1434 ASN_IPADDRESS,
1435 win_varbind->value.asnValue.address.
1436 stream,
1437 win_varbind->value.asnValue.address.
1438 length);
1439 break;
1440 case MS_ASN_COUNTER32:
1441 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1442 win_varbind->name.idLength,
1443 ASN_COUNTER,
1444 &win_varbind->value.asnValue.counter,
1445 sizeof(win_varbind->value.asnValue.
1446 counter));
1447 break;
1448 case MS_ASN_GAUGE32:
1449 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1450 win_varbind->name.idLength,
1451 ASN_GAUGE,
1452 &win_varbind->value.asnValue.gauge,
1453 sizeof(win_varbind->value.asnValue.
1454 gauge));
1455 break;
1456 case MS_ASN_TIMETICKS:
1457 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1458 win_varbind->name.idLength,
1459 ASN_TIMETICKS,
1460 &win_varbind->value.asnValue.ticks,
1461 sizeof(win_varbind->value.asnValue.
1462 ticks));
1463 break;
1464 case MS_ASN_OPAQUE: /* AsnOctetString */
1465 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1466 win_varbind->name.idLength,
1467 ASN_OPAQUE,
1468 win_varbind->value.asnValue.arbitrary.
1469 stream,
1470 win_varbind->value.asnValue.arbitrary.
1471 length);
1472 break;
1473 case MS_ASN_COUNTER64:
1474 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1475 win_varbind->name.idLength,
1476 ASN_COUNTER64,
1477 &win_varbind->value.asnValue.counter64,
1478 sizeof(win_varbind->value.asnValue.
1479 counter64));
1480 break;
1481 case MS_ASN_UINTEGER32:
1482 snmp_varlist_add_variable_w(net_snmp_varbinds, win_varbind->name.ids,
1483 win_varbind->name.idLength,
1484 ASN_UNSIGNED,
1485 &win_varbind->value.asnValue.unsigned32,
1486 sizeof(win_varbind->value.asnValue.
1487 unsigned32));
1488 break;
1489 default:
1490 return SNMP_ERR_GENERR;
1491 }
1492
1493 return SNMP_ERR_NOERROR;
1494 }
1495
1496 static int
1497 snmp_set_var_objid_w(netsnmp_variable_list * var, const UINT * name,
1498 UINT name_length)
1499 {
1500 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
1501 return snmp_set_var_objid(var, (const oid *) name, name_length);
1502 }
1503
1504 static netsnmp_variable_list *
1505 snmp_varlist_add_variable_w(netsnmp_variable_list ** varlist, const UINT * name,
1506 UINT name_length, u_char type, const void * value,
1507 size_t len)
1508 {
1509 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
1510 return snmp_varlist_add_variable(varlist, (const oid *) name, name_length, type,
1511 value, len);
1512 }
1513
1514 /**
1515 * Convert a Net-SNMP varbind to a WinSNMP varbind list.
1516 *
1517 * @param[out] pVarBindList WinSNMP varbind list, initialized by this
1518 * function.
1519 * @param[in] varbind Net-SNMP varbind.
1520 */
1521 int
1522 convert_to_windows_varbind_list(SnmpVarBindList * pVarBindList,
1523 netsnmp_variable_list * varbind)
1524 {
1525 SnmpVarBind *win_varbind;
1526
1527 netsnmp_assert(pVarBindList);
1528 netsnmp_assert(varbind);
1529
1530 pVarBindList->len = 1;
1531 pVarBindList->list
1532 = (SnmpVarBind *) SnmpUtilMemAlloc(pVarBindList->len
1533 *
1534 sizeof(pVarBindList->list[0]));
1535 if (pVarBindList->list == 0)
1536 goto generr;
1537
1538 memset(&pVarBindList->list[0], 0, sizeof(pVarBindList->list[0]));
1539
1540 win_varbind = &pVarBindList->list[0];
1541
1542 if (varbind->name
1543 && !copy_oid_to_new_windows_oid(&win_varbind->name,
1544 varbind->name,
1545 varbind->name_length))
1546 goto generr;
1547
1548 switch (varbind->type) {
1549 case ASN_BOOLEAN:
1550 /* There is no equivalent type in Microsoft's <snmp.h>. */
1551 netsnmp_assert(0);
1552 win_varbind->value.asnType = MS_ASN_INTEGER;
1553 win_varbind->value.asnValue.number = *(varbind->val.integer);
1554 break;
1555 case ASN_INTEGER:
1556 win_varbind->value.asnType = MS_ASN_INTEGER;
1557 win_varbind->value.asnValue.number = *(varbind->val.integer);
1558 break;
1559 case ASN_BIT_STR:
1560 win_varbind->value.asnType = MS_ASN_BITS;
1561 win_varbind->value.asnValue.string.stream
1562 = winsnmp_memdup(varbind->val.string, varbind->val_len);
1563 win_varbind->value.asnValue.string.length =
1564 (UINT) (varbind->val_len);
1565 win_varbind->value.asnValue.string.dynamic = TRUE;
1566 break;
1567 case ASN_OCTET_STR:
1568 win_varbind->value.asnType = MS_ASN_OCTETSTRING;
1569 win_varbind->value.asnValue.string.stream
1570 = winsnmp_memdup(varbind->val.string, varbind->val_len);
1571 win_varbind->value.asnValue.string.length =
1572 (UINT) (varbind->val_len);
1573 win_varbind->value.asnValue.string.dynamic = TRUE;
1574 break;
1575 case ASN_NULL:
1576 win_varbind->value.asnType = MS_ASN_NULL;
1577 memset(&win_varbind->value, 0, sizeof(win_varbind->value));
1578 break;
1579 case ASN_OBJECT_ID:
1580 win_varbind->value.asnType = MS_ASN_OBJECTIDENTIFIER;
1581 if (!copy_oid_to_new_windows_oid
1582 (&win_varbind->value.asnValue.object, varbind->val.objid,
1583 varbind->val_len / sizeof(varbind->val.objid[0])))
1584 return SNMP_ERR_GENERR;
1585 break;
1586 case ASN_SEQUENCE:
1587 win_varbind->value.asnType = MS_ASN_SEQUENCE;
1588 win_varbind->value.asnValue.string.stream
1589 = winsnmp_memdup(varbind->val.string, varbind->val_len);
1590 win_varbind->value.asnValue.string.length =
1591 (UINT) (varbind->val_len);
1592 win_varbind->value.asnValue.string.dynamic = TRUE;
1593 break;
1594 case ASN_SET:
1595 /* There is no equivalent type in Microsoft's <snmp.h>. */
1596 netsnmp_assert(0);
1597 win_varbind->value.asnType = MS_ASN_INTEGER;
1598 win_varbind->value.asnValue.number = *(varbind->val.integer);
1599 break;
1600 case ASN_IPADDRESS:
1601 win_varbind->value.asnType = MS_ASN_IPADDRESS;
1602 win_varbind->value.asnValue.string.stream
1603 = winsnmp_memdup(varbind->val.string, varbind->val_len);
1604 win_varbind->value.asnValue.string.length =
1605 (UINT) (varbind->val_len);
1606 win_varbind->value.asnValue.string.dynamic = TRUE;
1607 break;
1608 case ASN_COUNTER:
1609 win_varbind->value.asnType = MS_ASN_COUNTER32;
1610 win_varbind->value.asnValue.counter = *(varbind->val.integer);
1611 break;
1612 /*
1613 * ASN_GAUGE == ASN_UNSIGNED
1614 */
1615 case ASN_UNSIGNED:
1616 win_varbind->value.asnType = MS_ASN_UNSIGNED32;
1617 win_varbind->value.asnValue.unsigned32 = *(varbind->val.integer);
1618 break;
1619 case ASN_TIMETICKS:
1620 win_varbind->value.asnType = MS_ASN_TIMETICKS;
1621 win_varbind->value.asnValue.ticks = *(varbind->val.integer);
1622 break;
1623 case ASN_OPAQUE:
1624 win_varbind->value.asnType = MS_ASN_OPAQUE;
1625 win_varbind->value.asnValue.string.stream
1626 = winsnmp_memdup(varbind->val.string, varbind->val_len);
1627 win_varbind->value.asnValue.string.length =
1628 (UINT) (varbind->val_len);
1629 win_varbind->value.asnValue.string.dynamic = TRUE;
1630 break;
1631 case ASN_COUNTER64:
1632 win_varbind->value.asnType = MS_ASN_COUNTER64;
1633 win_varbind->value.asnValue.counter64.HighPart
1634 = varbind->val.counter64->high;
1635 win_varbind->value.asnValue.counter64.LowPart
1636 = varbind->val.counter64->low;
1637 break;
1638 default:
1639 netsnmp_assert(0);
1640 goto generr;
1641 }
1642
1643 return SNMP_ERR_NOERROR;
1644
1645 generr:
1646 SnmpUtilVarBindListFree(pVarBindList);
1647 memset(pVarBindList, 0, sizeof(*pVarBindList));
1648 return SNMP_ERR_GENERR;
1649 }
1650
1651 /** Convert a Windows SNMP error code to the equivalent Net-SNMP error code. */
1652 int
1653 convert_win_snmp_err(const int win_snmp_err)
1654 {
1655 switch (win_snmp_err) {
1656 case SNMP_ERRORSTATUS_NOERROR:
1657 return SNMP_ERR_NOERROR;
1658 case SNMP_ERRORSTATUS_TOOBIG:
1659 return SNMP_ERR_TOOBIG;
1660 case SNMP_ERRORSTATUS_NOSUCHNAME:
1661 /*
1662 * Note: SNMP extension DLLs return SNMP_ERRORSTATUS_NOSUCHNAME
1663 * when either noSuchObject or noSuchInstance should be returned to
1664 * the SNMP manager (assuming SNMPv2c or SNMPv3). Unfortunately it
1665 * is not possible without consulting the MIB to find out whether
1666 * either SNMP_NOSUCHINSTANCE or SNMP_NOSUCHOBJECT should be returned.
1667 * See also RFC 1448.
1668 */
1669 return SNMP_NOSUCHINSTANCE;
1670 case SNMP_ERRORSTATUS_BADVALUE:
1671 return SNMP_ERR_BADVALUE;
1672 case SNMP_ERRORSTATUS_READONLY:
1673 return SNMP_ERR_READONLY;
1674 case SNMP_ERRORSTATUS_GENERR:
1675 return SNMP_ERR_GENERR;
1676 case SNMP_ERRORSTATUS_NOACCESS:
1677 return SNMP_ERR_NOACCESS;
1678 case SNMP_ERRORSTATUS_WRONGTYPE:
1679 return SNMP_ERR_WRONGTYPE;
1680 case SNMP_ERRORSTATUS_WRONGLENGTH:
1681 return SNMP_ERR_WRONGLENGTH;
1682 case SNMP_ERRORSTATUS_WRONGENCODING:
1683 return SNMP_ERR_WRONGENCODING;
1684 case SNMP_ERRORSTATUS_WRONGVALUE:
1685 return SNMP_ERR_WRONGVALUE;
1686 case SNMP_ERRORSTATUS_NOCREATION:
1687 return SNMP_ERR_NOCREATION;
1688 case SNMP_ERRORSTATUS_INCONSISTENTVALUE:
1689 return SNMP_ERR_INCONSISTENTVALUE;
1690 case SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE:
1691 return SNMP_ERR_RESOURCEUNAVAILABLE;
1692 case SNMP_ERRORSTATUS_COMMITFAILED:
1693 return SNMP_ERR_COMMITFAILED;
1694 case SNMP_ERRORSTATUS_UNDOFAILED:
1695 return SNMP_ERR_UNDOFAILED;
1696 case SNMP_ERRORSTATUS_AUTHORIZATIONERROR:
1697 return SNMP_ERR_AUTHORIZATIONERROR;
1698 case SNMP_ERRORSTATUS_NOTWRITABLE:
1699 return SNMP_ERR_NOTWRITABLE;
1700 case SNMP_ERRORSTATUS_INCONSISTENTNAME:
1701 return SNMP_ERR_INCONSISTENTNAME;
1702 }
1703 netsnmp_assert(0);
1704 return SNMP_ERR_GENERR;
1705 }
1706
1707 /**
1708 * Look up the extension DLL view that was registered with the given OID.
1709 */
1710 static winextdll_view *
1711 lookup_view_by_oid(oid * const name, const size_t name_len)
1712 {
1713 int i;
1714
1715 for (i = 0; i < s_winextdll_view.size; i++) {
1716 if (netsnmp_oid_equals(WINEXTDLL_VIEW(i).name,
1717 WINEXTDLL_VIEW(i).name_length,
1718 name, name_len) == 0
1719 && WINEXTDLL_VIEW(i).my_handler) {
1720 return &WINEXTDLL_VIEW(i);
1721 }
1722 }
1723
1724 return NULL;
1725 }
1726
1727 static int
1728 snmp_oid_compare_n_w(const oid * name1, size_t len1, const UINT * name2,
1729 UINT len2)
1730 {
1731 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
1732 return snmp_oid_compare(name1, len1, (const oid *) name2, len2);
1733 }
1734
1735 static int
1736 snmp_oid_compare_w_n(const UINT * name1, UINT len1, const oid * name2,
1737 size_t len2)
1738 {
1739 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
1740 return snmp_oid_compare((const oid *) name1, len1, name2, len2);
1741 }
1742
1743 static int
1744 netsnmp_oid_is_subtree_n_w(const oid * name1, size_t len1, const UINT * name2,
1745 UINT len2)
1746 {
1747 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
1748 return netsnmp_oid_is_subtree(name1, len1, (const oid *) name2, len2);
1749 }
1750
1751 /**
1752 * Copy an OID.
1753 *
1754 * @param[out] to_name Number of elements written to destination OID.
1755 * @param[out] to_name_len Length of destination OID. Must have at least
1756 * min(from_name_len, MAX_OID_LEN) elements.
1757 * @param[in] from_name Original OID.
1758 * @param[in] from_name_len Length of original OID.
1759 */
1760 static void
1761 copy_oid(oid * const to_name, size_t * const to_name_len,
1762 const oid * const from_name, const size_t from_name_len)
1763 {
1764 int j;
1765
1766 netsnmp_assert(to_name);
1767 netsnmp_assert(to_name_len);
1768 netsnmp_assert(from_name);
1769
1770 for (j = 0; j < from_name_len && j < MAX_OID_LEN; j++)
1771 to_name[j] = from_name[j];
1772
1773 *to_name_len = j;
1774 }
1775
1776 /**
1777 * Copy an OID.
1778 *
1779 * @param[out] to_name Number of elements written to destination OID.
1780 * @param[out] to_name_len Length of destination OID. Must have at least
1781 * min(from_name_len, MAX_OID_LEN) elements.
1782 * @param[in] from_name Original OID.
1783 * @param[in] from_name_len Length of original OID.
1784 */
1785 static void
1786 copy_oid_n_w(oid * const to_name, size_t * const to_name_len,
1787 const UINT * const from_name, const UINT from_name_len)
1788 {
1789 netsnmp_static_assert(sizeof(oid) == sizeof(UINT));
1790 copy_oid(to_name, to_name_len, (const oid *) from_name, from_name_len);
1791 }
1792
1793 /**
1794 * Convert a Net-SNMP OID into a Windows OID and allocate memory for the
1795 * Windows OID.
1796 *
1797 * @param[out] windows_oid Pointer to a AsnObjectIdentifier.
1798 * @param[in] name Pointer to an array with elements of type oid
1799 * and length name_len.
1800 * @param[in] name_len Number of elements of input and output OID.
1801 */
1802 static UINT *
1803 copy_oid_to_new_windows_oid(AsnObjectIdentifier * const windows_oid,
1804 const oid * const name, const size_t name_len)
1805 {
1806 netsnmp_assert(windows_oid);
1807 netsnmp_assert(windows_oid->ids == 0);
1808 netsnmp_assert(windows_oid->idLength == 0);
1809 netsnmp_assert(name);
1810
1811 windows_oid->ids
1812 =
1813 (UINT *) winsnmp_memdup(name,
1814 sizeof(windows_oid->ids[0]) * name_len);
1815 windows_oid->idLength = (UINT) name_len;
1816 return windows_oid->ids;
1817 }
1818
1819 static u_char *
1820 winsnmp_memdup(const void *src, const size_t len)
1821 {
1822 u_char *p;
1823
1824 netsnmp_assert(len == (UINT) len);
1825
1826 p = SnmpUtilMemAlloc((UINT) len);
1827 if (p)
1828 memcpy(p, src, len);
1829 return p;
1830 }
1831
1832 #if 0
1833 /** Initialize array 'a'. */
1834 static void
1835 xarray_init(xarray * a, size_t elem_size)
1836 {
1837 netsnmp_assert(a);
1838
1839 memset(a, 0, sizeof(*a));
1840 a->elem_size = elem_size;
1841 }
1842 #endif
1843
1844 /** Deallocate any memory that was dynamically allocated for 'a'. */
1845 static void
1846 xarray_destroy(xarray * a)
1847 {
1848 netsnmp_assert(a);
1849
1850 xarray_reserve(a, 0);
1851 }
1852
1853 /**
1854 * Append the contents of the address range [ elem, elem + a->elem_size [ to a.
1855 *
1856 * Resize a if necessary.
1857 *
1858 * @return A pointer to the address where the data has been copied upon success,
1859 * or NULL upon failure.
1860 */
1861 static void *
1862 xarray_push_back(xarray * a, const void *elem)
1863 {
1864 netsnmp_assert(a);
1865 netsnmp_assert(elem);
1866 netsnmp_assert(a->size <= a->reserved);
1867
1868 if (a->size == a->reserved)
1869 xarray_reserve(a, a->reserved == 0 ? 16 : 2 * a->reserved);
1870 if (a->size < a->reserved) {
1871 netsnmp_assert(a->size < a->reserved);
1872 return memcpy((char *) (a->p) + a->elem_size * a->size++, elem,
1873 a->elem_size);
1874 }
1875 return NULL;
1876 }
1877
1878 #if 0
1879 /** Erase [ elem, elem + a->elem_size [ from a. */
1880 static void
1881 xarray_erase(xarray * a, void *const elem)
1882 {
1883 netsnmp_assert(a);
1884 netsnmp_assert(a->size >= 1);
1885 netsnmp_assert(a->p <= elem);
1886 netsnmp_assert((const char *) elem + a->elem_size <=
1887 (char *) a->p + a->size * a->elem_size);
1888 netsnmp_assert(((const char *) elem - (char *) a->p) % a->elem_size == 0);
1889
1890 a->size--;
1891 memmove((char *) elem, (char *) elem + a->elem_size,
1892 a->size - ((const char *) elem -
1893 (char *) a->p) / a->elem_size);
1894 }
1895 #endif
1896
1897 /**
1898 * Change the number of allocated elements to 'reserved'.
1899 *
1900 * Can be used either for enlarging or for shrinking the memory allocated for
1901 * 'a'. Does not modify 'a' if memory allocation fails. Newly allocted memory
1902 * is not initialized.
1903 *
1904 * @return != NULL upon success, NULL upon failure.
1905 */
1906 static void *
1907 xarray_reserve(xarray * a, int reserved)
1908 {
1909 netsnmp_assert(a);
1910 netsnmp_assert(a->size <= a->reserved);
1911
1912 if ((a->p = realloc(a->p, a->elem_size * reserved)))
1913 a->reserved = reserved;
1914 else
1915 a->reserved = 0;
1916 return a->p;
1917 }
1918
1919 #endif /* USING_WINEXTDLL_MODULE */