| 1 |
/* |
| 2 |
* util_funcs.c |
| 3 |
*/ |
| 4 |
/* |
| 5 |
* Portions of this file are copyrighted by: |
| 6 |
* Copyright Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
| 7 |
* Use is subject to license terms specified in the COPYING file |
| 8 |
* distributed with the Net-SNMP package. |
| 9 |
* |
| 10 |
* Portions of this file are copyrighted by: |
| 11 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 12 |
* Use is subject to license terms specified in the COPYING file |
| 13 |
* distributed with the Net-SNMP package. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <net-snmp/net-snmp-config.h> |
| 17 |
#include <net-snmp/net-snmp-features.h> |
| 18 |
|
| 19 |
#include <sys/types.h> |
| 20 |
#ifdef HAVE_IO_H |
| 21 |
#include <io.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_SPAWN_H |
| 24 |
#include <spawn.h> |
| 25 |
#endif |
| 26 |
#include <stdio.h> |
| 27 |
#ifdef HAVE_STDLIB_H |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
#ifdef HAVE_MALLOC_H |
| 31 |
#include <malloc.h> |
| 32 |
#endif |
| 33 |
#ifdef __alpha |
| 34 |
#ifndef _BSD |
| 35 |
#define _BSD |
| 36 |
#define _myBSD |
| 37 |
#endif |
| 38 |
#endif |
| 39 |
#ifdef HAVE_SYS_WAIT_H |
| 40 |
# include <sys/wait.h> |
| 41 |
#endif |
| 42 |
#ifdef __alpha |
| 43 |
#ifdef _myBSD |
| 44 |
#undef _BSD |
| 45 |
#undef _myBSD |
| 46 |
#endif |
| 47 |
#endif |
| 48 |
#ifndef WEXITSTATUS |
| 49 |
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) |
| 50 |
#endif |
| 51 |
#ifndef WIFEXITED |
| 52 |
# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
| 53 |
#endif |
| 54 |
#ifdef TIME_WITH_SYS_TIME |
| 55 |
# include <sys/time.h> |
| 56 |
# include <time.h> |
| 57 |
#else |
| 58 |
# ifdef HAVE_SYS_TIME_H |
| 59 |
# include <sys/time.h> |
| 60 |
# else |
| 61 |
# include <time.h> |
| 62 |
# endif |
| 63 |
#endif |
| 64 |
#ifdef HAVE_UNISTD_H |
| 65 |
#include <unistd.h> |
| 66 |
#endif |
| 67 |
#ifdef HAVE_FCNTL_H |
| 68 |
#include <fcntl.h> |
| 69 |
#endif |
| 70 |
#include <errno.h> |
| 71 |
#include <signal.h> |
| 72 |
#ifdef HAVE_STRING_H |
| 73 |
#include <string.h> |
| 74 |
#else |
| 75 |
#include <strings.h> |
| 76 |
#endif |
| 77 |
#include <ctype.h> |
| 78 |
#ifdef HAVE_NETINET_IN_H |
| 79 |
#include <netinet/in.h> |
| 80 |
#endif |
| 81 |
#ifdef HAVE_BASETSD_H |
| 82 |
#include <basetsd.h> |
| 83 |
#define ssize_t SSIZE_T |
| 84 |
#endif |
| 85 |
#ifdef HAVE_RAISE |
| 86 |
#define alarm raise |
| 87 |
#endif |
| 88 |
#ifdef HAVE_SYS_STAT_H |
| 89 |
#include <sys/stat.h> |
| 90 |
#endif |
| 91 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 92 |
#include <sys/ioctl.h> |
| 93 |
#include <linux/if.h> |
| 94 |
#include <linux/sockios.h> |
| 95 |
#ifdef HAVE_LINUX_ETHTOOL_NEEDS_U64 |
| 96 |
#include <linux/types.h> |
| 97 |
typedef __u64 u64; /* hack, so we may include kernel's ethtool.h */ |
| 98 |
typedef __u32 u32; /* ditto */ |
| 99 |
typedef __u16 u16; /* ditto */ |
| 100 |
typedef __u8 u8; /* ditto */ |
| 101 |
#endif |
| 102 |
#include <linux/ethtool.h> |
| 103 |
#endif /* HAVE_LINUX_ETHTOOL_H */ |
| 104 |
#include <net-snmp/net-snmp-includes.h> |
| 105 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 106 |
#include <net-snmp/library/snmp_logging.h> |
| 107 |
#include <net-snmp/agent/netsnmp_close_fds.h> |
| 108 |
|
| 109 |
#include "struct.h" |
| 110 |
#include "util_funcs.h" |
| 111 |
#include "utilities/execute.h" |
| 112 |
|
| 113 |
#ifdef HAVE_LIMITS_H |
| 114 |
#include "limits.h" |
| 115 |
#endif |
| 116 |
#ifdef USING_UCD_SNMP_ERRORMIB_MODULE |
| 117 |
#include "ucd-snmp/errormib.h" |
| 118 |
#else |
| 119 |
#define setPerrorstatus(x) snmp_log_perror(x) |
| 120 |
#endif |
| 121 |
|
| 122 |
netsnmp_feature_child_of(util_funcs, libnetsnmpmibs); |
| 123 |
|
| 124 |
netsnmp_feature_child_of(shell_command, util_funcs); |
| 125 |
netsnmp_feature_child_of(get_exten_instance, util_funcs); |
| 126 |
netsnmp_feature_child_of(clear_cache, util_funcs); |
| 127 |
netsnmp_feature_child_of(find_field, util_funcs); |
| 128 |
netsnmp_feature_child_of(parse_miboid, util_funcs); |
| 129 |
netsnmp_feature_child_of(string_append_int, util_funcs); |
| 130 |
netsnmp_feature_child_of(internal_mib_table, util_funcs); |
| 131 |
|
| 132 |
#if defined(HAVE_LINUX_RTNETLINK_H) |
| 133 |
netsnmp_feature_child_of(prefix_info_all, util_funcs); |
| 134 |
netsnmp_feature_child_of(prefix_info, prefix_info_all); |
| 135 |
netsnmp_feature_child_of(update_prefix_info, prefix_info_all); |
| 136 |
netsnmp_feature_child_of(delete_prefix_info, prefix_info_all); |
| 137 |
netsnmp_feature_child_of(find_prefix_info, prefix_info_all); |
| 138 |
netsnmp_feature_child_of(create_prefix_info, prefix_info_all); |
| 139 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 140 |
|
| 141 |
#if defined(NETSNMP_EXCACHETIME) && defined(USING_UTILITIES_EXECUTE_MODULE) && defined(HAVE_EXECV) |
| 142 |
static long cachetime; |
| 143 |
#endif |
| 144 |
|
| 145 |
/** deprecated, use netsnmp_mktemp instead */ |
| 146 |
const char * |
| 147 |
make_tempfile(void) |
| 148 |
{ |
| 149 |
return netsnmp_mktemp(); |
| 150 |
} |
| 151 |
|
| 152 |
#ifndef NETSNMP_FEATURE_REMOVE_SHELL_COMMAND |
| 153 |
int |
| 154 |
shell_command(struct extensible *ex) |
| 155 |
{ |
| 156 |
#ifdef HAVE_SYSTEM |
| 157 |
const char *ofname; |
| 158 |
char *shellline = NULL; |
| 159 |
FILE *shellout; |
| 160 |
|
| 161 |
ofname = make_tempfile(); |
| 162 |
if (ofname == NULL) { |
| 163 |
ex->output[0] = 0; |
| 164 |
ex->result = 127; |
| 165 |
return ex->result; |
| 166 |
} |
| 167 |
|
| 168 |
if (asprintf(&shellline, "%s > %s", ex->command, ofname) >= 0) { |
| 169 |
ex->result = system(shellline); |
| 170 |
ex->result = WEXITSTATUS(ex->result); |
| 171 |
free(shellline); |
| 172 |
} |
| 173 |
shellout = fopen(ofname, "r"); |
| 174 |
if (shellout != NULL) { |
| 175 |
if (fgets(ex->output, sizeof(ex->output), shellout) == NULL) { |
| 176 |
ex->output[0] = 0; |
| 177 |
} |
| 178 |
fclose(shellout); |
| 179 |
} |
| 180 |
unlink(ofname); |
| 181 |
#else |
| 182 |
ex->output[0] = 0; |
| 183 |
ex->result = 0; |
| 184 |
#endif |
| 185 |
return (ex->result); |
| 186 |
} |
| 187 |
#endif /* NETSNMP_FEATURE_REMOVE_SHELL_COMMAND */ |
| 188 |
|
| 189 |
#define MAXOUTPUT 300 |
| 190 |
|
| 191 |
int |
| 192 |
exec_command(struct extensible *ex) |
| 193 |
{ |
| 194 |
#if defined (HAVE_EXECV) || defined (WIN32) |
| 195 |
int fd; |
| 196 |
FILE *file; |
| 197 |
|
| 198 |
if ((fd = get_exec_output(ex)) != -1) { |
| 199 |
file = fdopen(fd, "r"); |
| 200 |
if (fgets(ex->output, sizeof(ex->output), file) == NULL) { |
| 201 |
ex->output[0] = 0; |
| 202 |
} |
| 203 |
fclose(file); |
| 204 |
wait_on_exec(ex); |
| 205 |
} else |
| 206 |
#endif /* HAVE_EXECV */ |
| 207 |
{ |
| 208 |
ex->output[0] = 0; |
| 209 |
ex->result = 0; |
| 210 |
} |
| 211 |
return (ex->result); |
| 212 |
} |
| 213 |
|
| 214 |
#ifndef NETSNMP_FEATURE_REMOVE_GET_EXTEN_INSTANCE |
| 215 |
struct extensible * |
| 216 |
get_exten_instance(struct extensible *exten, size_t inst) |
| 217 |
{ |
| 218 |
int i; |
| 219 |
|
| 220 |
if (exten == NULL) |
| 221 |
return (NULL); |
| 222 |
for (i = 1; i != (int) inst && exten != NULL; i++) |
| 223 |
exten = exten->next; |
| 224 |
return (exten); |
| 225 |
} |
| 226 |
#endif /* NETSNMP_FEATURE_REMOVE_GET_EXTEN_INSTANCE */ |
| 227 |
|
| 228 |
void |
| 229 |
wait_on_exec(struct extensible *ex) |
| 230 |
{ |
| 231 |
#if defined(WIN32) && !defined (mingw32) |
| 232 |
int rc; |
| 233 |
if (ex->tid != 0 && ex->pid != 0) { |
| 234 |
HANDLE hThread = ex->tid; |
| 235 |
HANDLE hProcess = ex->pid; |
| 236 |
rc = WaitForSingleObject(hProcess, NETSNMP_TIMEOUT_WAITFORSINGLEOBJECT); |
| 237 |
DEBUGMSGT(("exec:wait_on_exec","WaitForSingleObject rc=(%d)\n",rc )); |
| 238 |
rc = CloseHandle( hThread ); |
| 239 |
DEBUGMSGT(("exec:wait_on_exec","CloseHandle hThread=(%d)\n",rc )); |
| 240 |
rc = CloseHandle( hProcess ); |
| 241 |
DEBUGMSGT(("exec:wait_on_exec","CloseHandle hProcess=(%d)\n",rc )); |
| 242 |
ex->pid = 0; |
| 243 |
ex->tid = 0; |
| 244 |
} |
| 245 |
#else |
| 246 |
#ifndef NETSNMP_EXCACHETIME |
| 247 |
if (ex->pid && waitpid(ex->pid, &ex->result, 0) < 0) { |
| 248 |
setPerrorstatus("waitpid"); |
| 249 |
} |
| 250 |
ex->pid = 0; |
| 251 |
#endif /* NETSNMP_EXCACHETIME */ |
| 252 |
#endif /* WIN32 */ |
| 253 |
} |
| 254 |
|
| 255 |
#define MAXARGS 30 |
| 256 |
|
| 257 |
int |
| 258 |
get_exec_output(struct extensible *ex) |
| 259 |
{ |
| 260 |
#ifndef USING_UTILITIES_EXECUTE_MODULE |
| 261 |
ex->result = -1; |
| 262 |
NETSNMP_LOGONCE((LOG_WARNING, "support for run_exec_command not available\n")); |
| 263 |
#else |
| 264 |
#ifdef HAVE_EXECV |
| 265 |
char cachefile[STRMAX]; |
| 266 |
char cache[NETSNMP_MAXCACHESIZE]; |
| 267 |
int cachebytes; |
| 268 |
int cfd; |
| 269 |
#ifdef NETSNMP_EXCACHETIME |
| 270 |
long curtime; |
| 271 |
static char lastcmd[STRMAX]; |
| 272 |
static int lastresult; |
| 273 |
#endif |
| 274 |
|
| 275 |
DEBUGMSGTL(("exec:get_exec_output","calling %s\n", ex->command)); |
| 276 |
|
| 277 |
sprintf(cachefile, "%s/%s", get_persistent_directory(), NETSNMP_CACHEFILE); |
| 278 |
#ifdef NETSNMP_EXCACHETIME |
| 279 |
curtime = time(NULL); |
| 280 |
if (curtime > (cachetime + NETSNMP_EXCACHETIME) || |
| 281 |
strcmp(ex->command, lastcmd) != 0) { |
| 282 |
strlcpy(lastcmd, ex->command, sizeof(lastcmd)); |
| 283 |
cachetime = curtime; |
| 284 |
#endif |
| 285 |
|
| 286 |
cachebytes = NETSNMP_MAXCACHESIZE; |
| 287 |
ex->result = run_exec_command( ex->command, NULL, cache, &cachebytes ); |
| 288 |
|
| 289 |
unlink(cachefile); |
| 290 |
/* |
| 291 |
* XXX Use SNMP_FILEMODE_CLOSED instead of 644? |
| 292 |
*/ |
| 293 |
if ((cfd = open(cachefile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) { |
| 294 |
snmp_log(LOG_ERR,"can not create cache file\n"); |
| 295 |
setPerrorstatus(cachefile); |
| 296 |
#ifdef NETSNMP_EXCACHETIME |
| 297 |
cachetime = 0; |
| 298 |
#endif |
| 299 |
return -1; |
| 300 |
} |
| 301 |
if (cachebytes > 0) |
| 302 |
NETSNMP_IGNORE_RESULT(write(cfd, cache, cachebytes)); |
| 303 |
close(cfd); |
| 304 |
#ifdef NETSNMP_EXCACHETIME |
| 305 |
lastresult = ex->result; |
| 306 |
} else { |
| 307 |
ex->result = lastresult; |
| 308 |
} |
| 309 |
#endif |
| 310 |
DEBUGMSGTL(("exec:get_exec_output","using cached value\n")); |
| 311 |
if ((cfd = open(cachefile, O_RDONLY)) < 0) { |
| 312 |
snmp_log(LOG_ERR,"can not open cache file\n"); |
| 313 |
setPerrorstatus(cachefile); |
| 314 |
return -1; |
| 315 |
} |
| 316 |
return (cfd); |
| 317 |
#elif defined(HAVE__GET_OSFHANDLE) && defined(HAVE__OPEN_OSFHANDLE) |
| 318 |
/* MSVC and MinGW32. Cygwin has execv() and fork(). */ |
| 319 |
int fd; |
| 320 |
|
| 321 |
/* Reference: MS tech note: 190351 */ |
| 322 |
HANDLE hOutputReadTmp, hOutputRead, hOutputWrite = NULL; |
| 323 |
|
| 324 |
HANDLE hErrorWrite; |
| 325 |
SECURITY_ATTRIBUTES sa; |
| 326 |
PROCESS_INFORMATION pi; |
| 327 |
STARTUPINFO si; |
| 328 |
|
| 329 |
sa.nLength= sizeof(SECURITY_ATTRIBUTES); |
| 330 |
sa.lpSecurityDescriptor = NULL; |
| 331 |
sa.bInheritHandle = TRUE; |
| 332 |
|
| 333 |
DEBUGMSGTL(("exec:get_exec_output","calling %s\n", ex->command)); |
| 334 |
|
| 335 |
/* Child temporary output pipe with Inheritance on (sa.bInheritHandle is true) */ |
| 336 |
if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,&sa,0)) { |
| 337 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes CreatePipe ChildOut: %lu\n", |
| 338 |
GetLastError())); |
| 339 |
return -1; |
| 340 |
} |
| 341 |
|
| 342 |
/* Copy the stdout handle to the stderr handle in case the child closes one of |
| 343 |
* its stdout handles. */ |
| 344 |
if (!DuplicateHandle(GetCurrentProcess(),hOutputWrite, GetCurrentProcess(), |
| 345 |
&hErrorWrite,0, TRUE,DUPLICATE_SAME_ACCESS)) { |
| 346 |
DEBUGMSGTL(("util_funcs", "get_exec_output DuplicateHandle: %lu\n", GetLastError())); |
| 347 |
return -1; |
| 348 |
} |
| 349 |
|
| 350 |
/* Create new copies of the input and output handles but set bInheritHandle to |
| 351 |
* FALSE so the new handle can not be inherited. Otherwise the handles can not |
| 352 |
* be closed. */ |
| 353 |
if (!DuplicateHandle(GetCurrentProcess(), hOutputReadTmp, GetCurrentProcess(), |
| 354 |
&hOutputRead, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 355 |
DEBUGMSGTL(("util_funcs", "get_exec_output DupliateHandle ChildOut: %lu\n", GetLastError())); |
| 356 |
CloseHandle(hErrorWrite); |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
/* Close the temporary output and input handles */ |
| 361 |
if (!CloseHandle(hOutputReadTmp)) { |
| 362 |
DEBUGMSGTL(("util_funcs", "get_exec_output CloseHandle (hOutputReadTmp): %lu\n", GetLastError())); |
| 363 |
CloseHandle(hErrorWrite); |
| 364 |
CloseHandle(hOutputRead); |
| 365 |
return -1; |
| 366 |
} |
| 367 |
|
| 368 |
/* Associates a C run-time file descriptor with an existing operating-system file handle. */ |
| 369 |
fd = _open_osfhandle((intptr_t) hOutputRead, 0); |
| 370 |
|
| 371 |
/* Set up STARTUPINFO for CreateProcess with the handles and have it hide the window |
| 372 |
* for the new process. */ |
| 373 |
ZeroMemory(&si,sizeof(STARTUPINFO)); |
| 374 |
si.cb = sizeof(STARTUPINFO); |
| 375 |
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 376 |
si.hStdOutput = hOutputWrite; |
| 377 |
si.hStdError = hErrorWrite; |
| 378 |
si.wShowWindow = SW_HIDE; |
| 379 |
|
| 380 |
/* Launch the process that you want to redirect. Example snmpd.conf pass_persist: |
| 381 |
* pass_persist .1.3.6.1.4.1.2021.255 c:/perl/bin/perl c:/temp/pass_persisttest |
| 382 |
*/ |
| 383 |
if (!CreateProcess(NULL, ex->command, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { |
| 384 |
DEBUGMSGTL(("util_funcs","get_exec_output CreateProcess:'%s' %lu\n",ex->command, GetLastError())); |
| 385 |
CloseHandle(hErrorWrite); |
| 386 |
CloseHandle(hOutputRead); |
| 387 |
return -1; |
| 388 |
} |
| 389 |
|
| 390 |
/* Set global child process handle */ |
| 391 |
ex->pid = pi.hProcess; |
| 392 |
ex->tid = pi.hThread; |
| 393 |
|
| 394 |
/* Close pipe handles to make sure that no handles to the write end of the |
| 395 |
* output pipe are maintained in this process or else the pipe will |
| 396 |
* not close when the child process exits and any calls to ReadFile |
| 397 |
* will hang. |
| 398 |
*/ |
| 399 |
|
| 400 |
if (!CloseHandle(hOutputWrite)){ |
| 401 |
DEBUGMSGTL(("util_funcs","get_exec_output CloseHandle hOutputWrite: %lu\n", |
| 402 |
GetLastError())); |
| 403 |
return -1; |
| 404 |
} |
| 405 |
if (!CloseHandle(hErrorWrite)) { |
| 406 |
DEBUGMSGTL(("util_funcs","get_exec_output CloseHandle hErrorWrite: %lu\n", |
| 407 |
GetLastError())); |
| 408 |
return -1; |
| 409 |
} |
| 410 |
return fd; |
| 411 |
#endif /* HAVE_EXECV */ |
| 412 |
#endif /* !defined(USING_UTILITIES_EXECUTE_MODULE) */ |
| 413 |
return -1; |
| 414 |
} |
| 415 |
|
| 416 |
/* |
| 417 |
* Split @cmd into words and return an array with pointers to these words. |
| 418 |
* Store a pointer to the split command string into *@args. The caller must |
| 419 |
* free both *@args and the returned pointer. |
| 420 |
*/ |
| 421 |
static NETSNMP_ATTRIBUTE_UNUSED char ** |
| 422 |
parse_cmd(char **args, const char *cmd) |
| 423 |
{ |
| 424 |
int i, cnt; |
| 425 |
const char *cptr1; |
| 426 |
char *cptr2, **argv, **aptr; |
| 427 |
|
| 428 |
*args = strdup(cmd); |
| 429 |
if (!*args) |
| 430 |
return NULL; |
| 431 |
for (cnt = 1, cptr1 = cmd, cptr2 = *args; *cptr1 != '\0'; |
| 432 |
cptr2++, cptr1++) { |
| 433 |
*cptr2 = *cptr1; |
| 434 |
if (*cptr1 == ' ') { |
| 435 |
*cptr2++ = '\0'; |
| 436 |
cptr1 = skip_white_const(cptr1); |
| 437 |
if (!cptr1) |
| 438 |
break; |
| 439 |
*cptr2 = *cptr1; |
| 440 |
if (*cptr1) |
| 441 |
cnt++; |
| 442 |
} |
| 443 |
} |
| 444 |
argv = malloc((cnt + 1) * sizeof(argv[0])); |
| 445 |
if (argv == NULL) { |
| 446 |
free(*args); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
aptr = argv; |
| 450 |
*aptr++ = *args; |
| 451 |
for (cptr2 = *args, i = 1; i != cnt; cptr2++) { |
| 452 |
if (*cptr2 == '\0') { |
| 453 |
*aptr++ = cptr2 + 1; |
| 454 |
i++; |
| 455 |
} |
| 456 |
} |
| 457 |
*aptr++ = NULL; |
| 458 |
return argv; |
| 459 |
} |
| 460 |
|
| 461 |
#if defined(HAVE_EXECV) |
| 462 |
static int |
| 463 |
get_exec_pipes_fork(const char *cmd, int *fdIn, int *fdOut, netsnmp_pid_t *pid) |
| 464 |
{ |
| 465 |
int fd[2][2]; |
| 466 |
char **argv, *args; |
| 467 |
|
| 468 |
/* |
| 469 |
* Setup our pipes |
| 470 |
*/ |
| 471 |
if (pipe(fd[0]) || pipe(fd[1])) { |
| 472 |
setPerrorstatus("pipe"); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
if ((*pid = fork()) == 0) { /* First handle for the child */ |
| 476 |
close(fd[0][1]); |
| 477 |
close(fd[1][0]); |
| 478 |
if (dup2(fd[0][0], STDIN_FILENO) < 0) { |
| 479 |
setPerrorstatus("dup stdin"); |
| 480 |
return 0; |
| 481 |
} |
| 482 |
close(fd[0][0]); |
| 483 |
if (dup2(fd[1][1], STDOUT_FILENO) < 0) { |
| 484 |
setPerrorstatus("dup stdout"); |
| 485 |
return 0; |
| 486 |
} |
| 487 |
close(fd[1][1]); |
| 488 |
|
| 489 |
/* |
| 490 |
* write standard output and standard error to pipe. |
| 491 |
*/ |
| 492 |
/* |
| 493 |
* close all non-standard open file descriptors |
| 494 |
*/ |
| 495 |
netsnmp_close_fds(STDOUT_FILENO); |
| 496 |
NETSNMP_IGNORE_RESULT(dup2(STDOUT_FILENO, STDERR_FILENO)); |
| 497 |
|
| 498 |
argv = parse_cmd(&args, cmd); |
| 499 |
if (!argv) { |
| 500 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes(): argv == NULL\n")); |
| 501 |
return 0; |
| 502 |
} |
| 503 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes(): argv[0] = %s\n", argv[0])); |
| 504 |
execv(argv[0], argv); |
| 505 |
perror(argv[0]); |
| 506 |
free(argv); |
| 507 |
free(args); |
| 508 |
exit(1); |
| 509 |
} else { |
| 510 |
close(fd[0][0]); |
| 511 |
close(fd[1][1]); |
| 512 |
if (*pid < 0) { |
| 513 |
close(fd[0][1]); |
| 514 |
close(fd[1][0]); |
| 515 |
setPerrorstatus("fork"); |
| 516 |
return 0; |
| 517 |
} |
| 518 |
*fdIn = fd[1][0]; |
| 519 |
*fdOut = fd[0][1]; |
| 520 |
return (1); /* We are returning 0 for error... */ |
| 521 |
} |
| 522 |
} |
| 523 |
#endif |
| 524 |
|
| 525 |
#if defined(HAVE_POSIX_SPAWN) |
| 526 |
static int |
| 527 |
NETSNMP_ATTRIBUTE_UNUSED |
| 528 |
get_exec_pipes_spawn(const char *cmd, int *fdIn, int *fdOut, netsnmp_pid_t *pid) |
| 529 |
{ |
| 530 |
int fd[2][2], spawn_res; |
| 531 |
char **argv, *args; |
| 532 |
posix_spawnattr_t attr; |
| 533 |
posix_spawn_file_actions_t file_actions; |
| 534 |
|
| 535 |
argv = parse_cmd(&args, cmd); |
| 536 |
if (!argv) { |
| 537 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes(): argv == NULL\n")); |
| 538 |
goto err; |
| 539 |
} |
| 540 |
if (pipe(fd[0])) { |
| 541 |
setPerrorstatus("pipe 0"); |
| 542 |
goto free_argv; |
| 543 |
} |
| 544 |
if (pipe(fd[1])) { |
| 545 |
setPerrorstatus("pipe 1"); |
| 546 |
goto close_pipe_0; |
| 547 |
} |
| 548 |
posix_spawnattr_init(&attr); |
| 549 |
posix_spawn_file_actions_init(&file_actions); |
| 550 |
posix_spawn_file_actions_addclose(&file_actions, fd[0][1]); |
| 551 |
posix_spawn_file_actions_addclose(&file_actions, fd[1][0]); |
| 552 |
posix_spawn_file_actions_adddup2(&file_actions, fd[0][0], STDIN_FILENO); |
| 553 |
posix_spawn_file_actions_addclose(&file_actions, fd[0][0]); |
| 554 |
posix_spawn_file_actions_adddup2(&file_actions, fd[1][1], STDOUT_FILENO); |
| 555 |
posix_spawn_file_actions_addclose(&file_actions, fd[1][1]); |
| 556 |
posix_spawn_file_actions_adddup2(&file_actions, STDOUT_FILENO, |
| 557 |
STDERR_FILENO); |
| 558 |
spawn_res = posix_spawn(pid, argv[0], &file_actions, &attr, argv, NULL); |
| 559 |
posix_spawn_file_actions_destroy(&file_actions); |
| 560 |
posix_spawnattr_destroy(&attr); |
| 561 |
if (spawn_res != 0) { |
| 562 |
setPerrorstatus("posix_spawn"); |
| 563 |
goto close_pipe_1; |
| 564 |
} |
| 565 |
close(fd[0][0]); |
| 566 |
close(fd[1][1]); |
| 567 |
*fdIn = fd[1][0]; |
| 568 |
*fdOut = fd[0][1]; |
| 569 |
return 1; |
| 570 |
|
| 571 |
close_pipe_1: |
| 572 |
close(fd[1][0]); |
| 573 |
close(fd[1][1]); |
| 574 |
|
| 575 |
close_pipe_0: |
| 576 |
close(fd[0][0]); |
| 577 |
close(fd[0][1]); |
| 578 |
|
| 579 |
free_argv: |
| 580 |
free(argv); |
| 581 |
|
| 582 |
err: |
| 583 |
return 0; |
| 584 |
} |
| 585 |
#endif |
| 586 |
|
| 587 |
#if defined(HAVE__GET_OSFHANDLE) && defined(HAVE__OPEN_OSFHANDLE) |
| 588 |
static int |
| 589 |
get_exec_pipes_win32(const char *cmd, int *fdIn, int *fdOut, netsnmp_pid_t *pid) |
| 590 |
{ |
| 591 |
/* MSVC and MinGW32. Cygwin has execv() and fork(). */ |
| 592 |
/* Reference: MS tech note: 190351 */ |
| 593 |
HANDLE hInputWriteTmp, hInputRead, hInputWrite = NULL; |
| 594 |
HANDLE hOutputReadTmp, hOutputRead, hOutputWrite = NULL; |
| 595 |
|
| 596 |
HANDLE hErrorWrite; |
| 597 |
SECURITY_ATTRIBUTES sa; |
| 598 |
PROCESS_INFORMATION pi; |
| 599 |
STARTUPINFO si; |
| 600 |
|
| 601 |
sa.nLength= sizeof(SECURITY_ATTRIBUTES); |
| 602 |
sa.lpSecurityDescriptor = NULL; |
| 603 |
sa.bInheritHandle = TRUE; |
| 604 |
|
| 605 |
/* Child temporary output pipe with Inheritance on (sa.bInheritHandle is true) */ |
| 606 |
if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,&sa,0)) { |
| 607 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes CreatePipe ChildOut: %d\n", |
| 608 |
(unsigned int)GetLastError())); |
| 609 |
return 0; |
| 610 |
} |
| 611 |
/* Child temporary input pipe with Inheritance on (sa.bInheritHandle is true) */ |
| 612 |
if (!CreatePipe(&hInputRead,&hInputWriteTmp,&sa,0)) { |
| 613 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes CreatePipe ChildIn: %d\n", |
| 614 |
(unsigned int)GetLastError())); |
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
/* Copy the stdout handle to the stderr handle in case the child closes one of |
| 619 |
* its stdout handles. */ |
| 620 |
if (!DuplicateHandle(GetCurrentProcess(),hOutputWrite, GetCurrentProcess(), |
| 621 |
&hErrorWrite,0, TRUE,DUPLICATE_SAME_ACCESS)) { |
| 622 |
DEBUGMSGTL(("util_funcs", "get_exec_pipes DuplicateHandle: %d\n", |
| 623 |
(unsigned int)GetLastError())); |
| 624 |
return 0; |
| 625 |
} |
| 626 |
|
| 627 |
/* Create new copies of the input and output handles but set bInheritHandle to |
| 628 |
* FALSE so the new handle can not be inherited. Otherwise the handles can not |
| 629 |
* be closed. */ |
| 630 |
if (!DuplicateHandle(GetCurrentProcess(), hOutputReadTmp, |
| 631 |
GetCurrentProcess(), &hOutputRead, 0, FALSE, |
| 632 |
DUPLICATE_SAME_ACCESS)) { |
| 633 |
DEBUGMSGTL(("util_funcs", |
| 634 |
"get_exec_pipes DupliateHandle ChildOut: %d\n", |
| 635 |
(unsigned int)GetLastError())); |
| 636 |
CloseHandle(hErrorWrite); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
if (!DuplicateHandle(GetCurrentProcess(),hInputWriteTmp, |
| 640 |
GetCurrentProcess(), &hInputWrite, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 641 |
DEBUGMSGTL(("util_funcs","get_exec_pipes DupliateHandle ChildIn: %d\n", |
| 642 |
(unsigned int)GetLastError())); |
| 643 |
CloseHandle(hErrorWrite); |
| 644 |
CloseHandle(hOutputRead); |
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
/* Close the temporary output and input handles */ |
| 649 |
if (!CloseHandle(hOutputReadTmp)) { |
| 650 |
DEBUGMSGTL(("util_funcs", |
| 651 |
"get_exec_pipes CloseHandle (hOutputReadTmp): %d\n", |
| 652 |
(unsigned int)GetLastError())); |
| 653 |
CloseHandle(hErrorWrite); |
| 654 |
CloseHandle(hOutputRead); |
| 655 |
CloseHandle(hInputWrite); |
| 656 |
return 0; |
| 657 |
} |
| 658 |
if (!CloseHandle(hInputWriteTmp)) { |
| 659 |
DEBUGMSGTL(("util_funcs", |
| 660 |
"get_exec_pipes CloseHandle (hInputWriteTmp): %d\n", |
| 661 |
(unsigned int)GetLastError())); |
| 662 |
CloseHandle(hErrorWrite); |
| 663 |
CloseHandle(hOutputRead); |
| 664 |
CloseHandle(hInputWrite); |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
/* Associates a C run-time file descriptor with an existing operating-system file handle. */ |
| 669 |
*fdIn = _open_osfhandle((intptr_t) hOutputRead, 0); |
| 670 |
*fdOut = _open_osfhandle((intptr_t) hInputWrite, 0); |
| 671 |
|
| 672 |
/* Set up STARTUPINFO for CreateProcess with the handles and have it hide the window |
| 673 |
* for the new process. */ |
| 674 |
ZeroMemory(&si,sizeof(STARTUPINFO)); |
| 675 |
si.cb = sizeof(STARTUPINFO); |
| 676 |
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 677 |
si.hStdOutput = hOutputWrite; |
| 678 |
si.hStdInput = hInputRead; |
| 679 |
si.hStdError = hErrorWrite; |
| 680 |
si.wShowWindow = SW_HIDE; |
| 681 |
|
| 682 |
/* Launch the process that you want to redirect. Example snmpd.conf pass_persist: |
| 683 |
* pass_persist .1.3.6.1.4.1.2021.255 c:/perl/bin/perl c:/temp/pass_persisttest |
| 684 |
*/ |
| 685 |
if (!CreateProcess(NULL, NETSNMP_REMOVE_CONST(char *, cmd), NULL, NULL, |
| 686 |
TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { |
| 687 |
DEBUGMSGTL(("util_funcs","get_exec_pipes CreateProcess:'%s' %d\n", cmd, |
| 688 |
(unsigned int)GetLastError())); |
| 689 |
CloseHandle(hErrorWrite); |
| 690 |
CloseHandle(hOutputRead); |
| 691 |
CloseHandle(hInputWrite); |
| 692 |
return 0; |
| 693 |
} |
| 694 |
|
| 695 |
DEBUGMSGTL(("util_funcs","child hProcess (stored in pid): %p\n", pi.hProcess)); |
| 696 |
DEBUGMSGTL(("util_funcs","child dwProcessId (task manager): %d\n",(int)pi.dwProcessId)); |
| 697 |
|
| 698 |
/* Set global child process handle */ |
| 699 |
*pid = pi.hProcess; |
| 700 |
|
| 701 |
/* Cleanup */ |
| 702 |
if (!CloseHandle(pi.hThread)) |
| 703 |
DEBUGMSGTL(("util_funcs", |
| 704 |
"get_exec_pipes(%s) CloseHandle pi.hThread: %d\n", |
| 705 |
cmd, (unsigned int)GetLastError())); |
| 706 |
|
| 707 |
/* Close pipe handles to make sure that no handles to the write end of the |
| 708 |
* output pipe are maintained in this process or else the pipe will |
| 709 |
* not close when the child process exits and any calls to ReadFile |
| 710 |
* will hang. |
| 711 |
*/ |
| 712 |
|
| 713 |
if (!CloseHandle(hOutputWrite)){ |
| 714 |
DEBUGMSGTL(("util_funcs", |
| 715 |
"get_exec_pipes(%s) CloseHandle hOutputWrite: %d\n", |
| 716 |
cmd, (unsigned int)GetLastError())); |
| 717 |
return 0; |
| 718 |
} |
| 719 |
if (!CloseHandle(hInputRead)) { |
| 720 |
DEBUGMSGTL(("util_funcs", |
| 721 |
"get_exec_pipes(%s) CloseHandle hInputRead: %d\n", |
| 722 |
cmd, (unsigned int)GetLastError())); |
| 723 |
return 0; |
| 724 |
} |
| 725 |
if (!CloseHandle(hErrorWrite)) { |
| 726 |
DEBUGMSGTL(("util_funcs", |
| 727 |
"get_exec_pipes(%s) CloseHandle hErrorWrite: %d\n", |
| 728 |
cmd, (unsigned int)GetLastError())); |
| 729 |
return 0; |
| 730 |
} |
| 731 |
return 1; |
| 732 |
} |
| 733 |
#endif /* WIN32 */ |
| 734 |
|
| 735 |
int |
| 736 |
get_exec_pipes(const char *cmd, int *fdIn, int *fdOut, netsnmp_pid_t *pid) |
| 737 |
{ |
| 738 |
#if defined(HAVE_EXECV) |
| 739 |
return get_exec_pipes_fork(cmd, fdIn, fdOut, pid); |
| 740 |
#elif defined(HAVE_POSIX_SPAWN) |
| 741 |
return get_exec_pipes_fork(cmd, fdIn, fdOut, pid); |
| 742 |
#elif defined(HAVE__GET_OSFHANDLE) && defined(HAVE__OPEN_OSFHANDLE) |
| 743 |
return get_exec_pipes_win32(cmd, fdIn, fdOut, pid); |
| 744 |
#endif |
| 745 |
DEBUGMSGTL(("util_funcs", |
| 746 |
"get_exec_pipes() has not yet been implemented for this platform")); |
| 747 |
return 0; |
| 748 |
} |
| 749 |
|
| 750 |
#ifndef NETSNMP_FEATURE_REMOVE_CLEAR_CACHE |
| 751 |
int |
| 752 |
clear_cache(int action, |
| 753 |
u_char * var_val, |
| 754 |
u_char var_val_type, |
| 755 |
size_t var_val_len, |
| 756 |
u_char * statP, oid * name, size_t name_len) |
| 757 |
{ |
| 758 |
if (var_val_type != ASN_INTEGER) { |
| 759 |
snmp_log(LOG_NOTICE, "Wrong type != int\n"); |
| 760 |
return SNMP_ERR_WRONGTYPE; |
| 761 |
} |
| 762 |
#if defined(NETSNMP_EXCACHETIME) && defined(USING_UTILITIES_EXECUTE_MODULE) && defined(HAVE_EXECV) |
| 763 |
else { |
| 764 |
long tmp = 0; |
| 765 |
tmp = *((long *) var_val); |
| 766 |
if (tmp == 1 && action == COMMIT) { |
| 767 |
cachetime = 0; /* reset the cache next read */ |
| 768 |
} |
| 769 |
} |
| 770 |
#endif |
| 771 |
return SNMP_ERR_NOERROR; |
| 772 |
} |
| 773 |
#endif /* NETSNMP_FEATURE_REMOVE_CLEAR_CACHE */ |
| 774 |
|
| 775 |
void |
| 776 |
print_mib_oid(oid name[], size_t len) |
| 777 |
{ |
| 778 |
char *buffer; |
| 779 |
buffer = (char *) malloc(11 * len); /* maximum digit lengths for int32 + a '.' */ |
| 780 |
if (!buffer) { |
| 781 |
snmp_log(LOG_ERR, "Malloc failed - out of memory?"); |
| 782 |
return; |
| 783 |
} |
| 784 |
sprint_mib_oid(buffer, name, len); |
| 785 |
snmp_log(LOG_NOTICE, "Mib: %s\n", buffer); |
| 786 |
free(buffer); |
| 787 |
} |
| 788 |
|
| 789 |
void |
| 790 |
sprint_mib_oid(char *buf, const oid *name, size_t len) |
| 791 |
{ |
| 792 |
int i; |
| 793 |
|
| 794 |
for (i = 0; i < (int) len; i++) |
| 795 |
buf += sprintf(buf, ".%" NETSNMP_PRIo "u", name[i]); |
| 796 |
} |
| 797 |
|
| 798 |
/* |
| 799 |
* checkmib(): provided for backwards compatibility, do not use: |
| 800 |
*/ |
| 801 |
int |
| 802 |
checkmib(struct variable *vp, oid * name, size_t * length, |
| 803 |
int exact, size_t * var_len, WriteMethod ** write_method, int max) |
| 804 |
{ |
| 805 |
/* |
| 806 |
* checkmib used to be header_simple_table, with reveresed boolean |
| 807 |
* return output. header_simple_table() was created to match |
| 808 |
* header_generic(). |
| 809 |
*/ |
| 810 |
return (!header_simple_table(vp, name, length, exact, var_len, |
| 811 |
write_method, max)); |
| 812 |
} |
| 813 |
|
| 814 |
#ifndef NETSNMP_FEATURE_REMOVE_FIND_FIELD |
| 815 |
char * |
| 816 |
find_field(char *ptr, int field) |
| 817 |
{ |
| 818 |
int i; |
| 819 |
char *init = ptr; |
| 820 |
|
| 821 |
if (field == NETSNMP_LASTFIELD) { |
| 822 |
/* |
| 823 |
* skip to end |
| 824 |
*/ |
| 825 |
while (*ptr++); |
| 826 |
ptr = ptr - 2; |
| 827 |
/* |
| 828 |
* rewind a field length |
| 829 |
*/ |
| 830 |
while (*ptr != 0 && isspace((unsigned char)(*ptr)) && init <= ptr) |
| 831 |
ptr--; |
| 832 |
while (*ptr != 0 && !isspace((unsigned char)(*ptr)) && init <= ptr) |
| 833 |
ptr--; |
| 834 |
if (isspace((unsigned char)(*ptr))) |
| 835 |
ptr++; /* past space */ |
| 836 |
if (ptr < init) |
| 837 |
ptr = init; |
| 838 |
if (!isspace((unsigned char)(*ptr)) && *ptr != 0) |
| 839 |
return (ptr); |
| 840 |
} else { |
| 841 |
if ((ptr = skip_white(ptr)) == NULL) |
| 842 |
return (NULL); |
| 843 |
for (i = 1; *ptr != 0 && i != field; i++) { |
| 844 |
if ((ptr = skip_not_white(ptr)) == NULL) |
| 845 |
return (NULL); |
| 846 |
if ((ptr = skip_white(ptr)) == NULL) |
| 847 |
return (NULL); |
| 848 |
} |
| 849 |
if (*ptr != 0 && i == field) |
| 850 |
return (ptr); |
| 851 |
return (NULL); |
| 852 |
} |
| 853 |
return (NULL); |
| 854 |
} |
| 855 |
#endif /* NETSNMP_FEATURE_REMOVE_FIND_FIELD */ |
| 856 |
|
| 857 |
#ifndef NETSNMP_FEATURE_REMOVE_PARSE_MIBOID |
| 858 |
int |
| 859 |
parse_miboid(const char *buf, oid * oidout) |
| 860 |
{ |
| 861 |
int i; |
| 862 |
|
| 863 |
if (!buf) |
| 864 |
return 0; |
| 865 |
if (*buf == '.') |
| 866 |
buf++; |
| 867 |
for (i = 0; isdigit((unsigned char)(*buf)); i++) { |
| 868 |
/* Subidentifiers are unsigned values, up to 2^32-1 |
| 869 |
* so we need to use 'strtoul' rather than 'atoi' |
| 870 |
*/ |
| 871 |
oidout[i] = strtoul(buf, NULL, 10) & 0xffffffff; |
| 872 |
while (isdigit((unsigned char)(*buf))) buf++; |
| 873 |
if (*buf == '.') |
| 874 |
buf++; |
| 875 |
} |
| 876 |
/* |
| 877 |
* oidout[i] = -1; hmmm |
| 878 |
*/ |
| 879 |
return i; |
| 880 |
} |
| 881 |
#endif /* NETSNMP_FEATURE_REMOVE_PARSE_MIBOID */ |
| 882 |
|
| 883 |
#ifndef NETSNMP_FEATURE_REMOVE_STRING_APPEND_INT |
| 884 |
void |
| 885 |
string_append_int(char *s, int val) |
| 886 |
{ |
| 887 |
char textVal[16]; |
| 888 |
|
| 889 |
if (val < 10) { |
| 890 |
*s++ = '0' + val; |
| 891 |
*s = '\0'; |
| 892 |
return; |
| 893 |
} |
| 894 |
sprintf(textVal, "%d", val); |
| 895 |
strcpy(s, textVal); |
| 896 |
return; |
| 897 |
} |
| 898 |
#endif /* NETSNMP_FEATURE_REMOVE_STRING_APPEND_INT */ |
| 899 |
|
| 900 |
#ifndef NETSNMP_FEATURE_REMOVE_INTERNAL_MIB_TABLE |
| 901 |
|
| 902 |
struct internal_mib_table { |
| 903 |
int max_size; /* Size of the current data table */ |
| 904 |
int next_index; /* Index of the next free entry */ |
| 905 |
int current_index; /* Index of the 'current' entry */ |
| 906 |
int cache_timeout; |
| 907 |
marker_t cache_markerM; |
| 908 |
RELOAD *reload; /* Routine to read in the data */ |
| 909 |
COMPARE *compare; /* Routine to compare two entries */ |
| 910 |
int data_size; /* Size of an individual entry */ |
| 911 |
void *data; /* The table itself */ |
| 912 |
}; |
| 913 |
|
| 914 |
mib_table_t |
| 915 |
Initialise_Table(int size, int timeout, RELOAD *reload, COMPARE *compare) |
| 916 |
{ |
| 917 |
struct internal_mib_table *t; |
| 918 |
|
| 919 |
t = (struct internal_mib_table *) |
| 920 |
malloc(sizeof(struct internal_mib_table)); |
| 921 |
if (t == NULL) |
| 922 |
return NULL; |
| 923 |
|
| 924 |
t->max_size = 0; |
| 925 |
t->next_index = 1; /* Don't use index 0 */ |
| 926 |
t->current_index = 1; |
| 927 |
t->cache_timeout = timeout; |
| 928 |
t->cache_markerM = NULL; |
| 929 |
t->reload = reload; |
| 930 |
t->compare = compare; |
| 931 |
t->data_size = size; |
| 932 |
t->data = NULL; |
| 933 |
|
| 934 |
return (mib_table_t) t; |
| 935 |
} |
| 936 |
|
| 937 |
#define TABLE_ADD( x, y ) ((void*)((char*)(x) + y)) |
| 938 |
#define TABLE_INDEX(t, i) (TABLE_ADD(t->data, i * t->data_size)) |
| 939 |
#define TABLE_START(t) (TABLE_INDEX(t, 1)) |
| 940 |
#define TABLE_NEXT(t) (TABLE_INDEX(t, t->next_index)) |
| 941 |
#define TABLE_CURRENT(t) (TABLE_INDEX(t, t->current_index)) |
| 942 |
|
| 943 |
int |
| 944 |
check_and_reload_table(struct internal_mib_table *table) |
| 945 |
{ |
| 946 |
/* |
| 947 |
* If the saved data is fairly recent, |
| 948 |
* we don't need to reload it |
| 949 |
*/ |
| 950 |
if (table->cache_markerM && |
| 951 |
!(netsnmp_ready_monotonic(table->cache_markerM, |
| 952 |
table->cache_timeout * 1000))) |
| 953 |
return 1; |
| 954 |
|
| 955 |
|
| 956 |
/* |
| 957 |
* Call the routine provided to read in the data |
| 958 |
* |
| 959 |
* N.B: Update the cache marker *before* calling |
| 960 |
* this routine, to avoid problems with recursion |
| 961 |
*/ |
| 962 |
netsnmp_set_monotonic_marker(&table->cache_markerM); |
| 963 |
|
| 964 |
table->next_index = 1; |
| 965 |
if (table->reload((mib_table_t) table) < 0) { |
| 966 |
free(table->cache_markerM); |
| 967 |
table->cache_markerM = NULL; |
| 968 |
return 0; |
| 969 |
} |
| 970 |
table->current_index = 1; |
| 971 |
if (table->compare != NULL) /* Sort the table */ |
| 972 |
qsort(TABLE_START(table), table->next_index-1, |
| 973 |
table->data_size, table->compare); |
| 974 |
return 1; |
| 975 |
} |
| 976 |
|
| 977 |
int |
| 978 |
Search_Table(mib_table_t t, void *entry, int exact) |
| 979 |
{ |
| 980 |
struct internal_mib_table *table = (struct internal_mib_table *) t; |
| 981 |
void *entry2; |
| 982 |
int res; |
| 983 |
|
| 984 |
if (!check_and_reload_table(table)) |
| 985 |
return -1; |
| 986 |
|
| 987 |
if (table->compare == NULL) { |
| 988 |
/* |
| 989 |
* XXX - not sure this is right ? |
| 990 |
*/ |
| 991 |
memcpy(entry, table->data, table->data_size); |
| 992 |
return 0; |
| 993 |
} |
| 994 |
|
| 995 |
if (table->next_index == table->current_index) |
| 996 |
table->current_index = 1; |
| 997 |
|
| 998 |
entry2 = TABLE_CURRENT(table); |
| 999 |
res = table->compare(entry, entry2); |
| 1000 |
if ((res < 0) && (table->current_index != 1)) { |
| 1001 |
table->current_index = 1; |
| 1002 |
entry2 = TABLE_CURRENT(table); |
| 1003 |
res = table->compare(entry, entry2); |
| 1004 |
} |
| 1005 |
|
| 1006 |
while (res > 0) { |
| 1007 |
table->current_index++; |
| 1008 |
if (table->next_index == table->current_index) |
| 1009 |
return -1; |
| 1010 |
entry2 = TABLE_CURRENT(table); |
| 1011 |
res = table->compare(entry, entry2); |
| 1012 |
} |
| 1013 |
|
| 1014 |
if (exact && res != 0) |
| 1015 |
return -1; |
| 1016 |
|
| 1017 |
if (!exact && res == 0) { |
| 1018 |
table->current_index++; |
| 1019 |
if (table->next_index == table->current_index) |
| 1020 |
return -1; |
| 1021 |
entry2 = TABLE_CURRENT(table); |
| 1022 |
} |
| 1023 |
memcpy(entry, entry2, table->data_size); |
| 1024 |
return 0; |
| 1025 |
} |
| 1026 |
|
| 1027 |
int |
| 1028 |
Add_Entry(mib_table_t t, void *entry) |
| 1029 |
{ |
| 1030 |
struct internal_mib_table *table = (struct internal_mib_table *) t; |
| 1031 |
int new_max; |
| 1032 |
void *new_data; /* Used for |
| 1033 |
* a) extending the data table |
| 1034 |
* b) the next entry to use |
| 1035 |
*/ |
| 1036 |
|
| 1037 |
if (table->max_size <= table->next_index) { |
| 1038 |
/* |
| 1039 |
* Table is full, so extend it to double the size |
| 1040 |
*/ |
| 1041 |
new_max = 2 * table->max_size; |
| 1042 |
if (new_max == 0) |
| 1043 |
new_max = 10; /* Start with 10 entries */ |
| 1044 |
|
| 1045 |
new_data = (void *) malloc(new_max * table->data_size); |
| 1046 |
if (new_data == NULL) |
| 1047 |
return -1; |
| 1048 |
|
| 1049 |
if (table->data) { |
| 1050 |
memcpy(new_data, table->data, |
| 1051 |
table->max_size * table->data_size); |
| 1052 |
free(table->data); |
| 1053 |
} |
| 1054 |
table->data = new_data; |
| 1055 |
table->max_size = new_max; |
| 1056 |
} |
| 1057 |
|
| 1058 |
/* |
| 1059 |
* Insert the new entry into the data array |
| 1060 |
*/ |
| 1061 |
new_data = TABLE_NEXT(table); |
| 1062 |
memcpy(new_data, entry, table->data_size); |
| 1063 |
table->next_index++; |
| 1064 |
return 0; |
| 1065 |
} |
| 1066 |
|
| 1067 |
void * |
| 1068 |
Retrieve_Table_Data(mib_table_t t, int *max_idx) |
| 1069 |
{ |
| 1070 |
struct internal_mib_table *table = (struct internal_mib_table *) t; |
| 1071 |
|
| 1072 |
if (!check_and_reload_table(table)) |
| 1073 |
return NULL; |
| 1074 |
*max_idx = table->next_index - 1; |
| 1075 |
return table->data; |
| 1076 |
} |
| 1077 |
#endif /* NETSNMP_FEATURE_REMOVE_INTERNAL_MIB_TABLE */ |
| 1078 |
|
| 1079 |
#if defined(HAVE_LINUX_RTNETLINK_H) |
| 1080 |
|
| 1081 |
#ifndef NETSNMP_FEATURE_REMOVE_CREATE_PREFIX_INFO |
| 1082 |
prefix_cbx *net_snmp_create_prefix_info(unsigned long OnLinkFlag, |
| 1083 |
unsigned long AutonomousFlag, |
| 1084 |
char *in6ptr) |
| 1085 |
{ |
| 1086 |
prefix_cbx *node = SNMP_MALLOC_TYPEDEF(prefix_cbx); |
| 1087 |
if(!in6ptr) { |
| 1088 |
free(node); |
| 1089 |
return NULL; |
| 1090 |
} |
| 1091 |
if(!node) { |
| 1092 |
free(node); |
| 1093 |
return NULL; |
| 1094 |
} |
| 1095 |
node->next_info = NULL; |
| 1096 |
node->ipAddressPrefixOnLinkFlag = OnLinkFlag; |
| 1097 |
node->ipAddressPrefixAutonomousFlag = AutonomousFlag; |
| 1098 |
memcpy(node->in6p, in6ptr, sizeof(node->in6p)); |
| 1099 |
|
| 1100 |
return node; |
| 1101 |
} |
| 1102 |
#endif /* NETSNMP_FEATURE_REMOVE_CREATE_PREFIX_INFO */ |
| 1103 |
|
| 1104 |
#ifndef NETSNMP_FEATURE_REMOVE_FIND_PREFIX_INFO |
| 1105 |
int net_snmp_find_prefix_info(prefix_cbx **head, |
| 1106 |
char *address, |
| 1107 |
prefix_cbx *node_to_find) |
| 1108 |
{ |
| 1109 |
int iret; |
| 1110 |
memset(node_to_find, 0, sizeof(prefix_cbx)); |
| 1111 |
if(!*head) |
| 1112 |
return -1; |
| 1113 |
memcpy(node_to_find->in6p, address, sizeof(node_to_find->in6p)); |
| 1114 |
|
| 1115 |
iret = net_snmp_search_update_prefix_info(head, node_to_find, 1); |
| 1116 |
if(iret < 0) { |
| 1117 |
DEBUGMSGTL(("util_funcs:prefix", "Unable to search the list\n")); |
| 1118 |
return -1; |
| 1119 |
} else if (!iret) { |
| 1120 |
DEBUGMSGTL(("util_funcs:prefix", "Could not find prefix info\n")); |
| 1121 |
return -1; |
| 1122 |
} else |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
#endif /* NETSNMP_FEATURE_REMOVE_FIND_PREFIX_INFO */ |
| 1126 |
|
| 1127 |
#ifndef NETSNMP_FEATURE_REMOVE_UPDATE_PREFIX_INFO |
| 1128 |
int net_snmp_update_prefix_info(prefix_cbx **head, |
| 1129 |
prefix_cbx *node_to_update) |
| 1130 |
{ |
| 1131 |
int iret; |
| 1132 |
iret = net_snmp_search_update_prefix_info(head, node_to_update, 0); |
| 1133 |
if(iret < 0) { |
| 1134 |
DEBUGMSGTL(("util_funcs:prefix", "Unable to update prefix info\n")); |
| 1135 |
return -1; |
| 1136 |
} else if (!iret) { |
| 1137 |
DEBUGMSGTL(("util_funcs:prefix", "Unable to find the node to update\n")); |
| 1138 |
return -1; |
| 1139 |
} else |
| 1140 |
return 0; |
| 1141 |
} |
| 1142 |
#endif /* NETSNMP_FEATURE_REMOVE_UPDATE_PREFIX_INFO */ |
| 1143 |
|
| 1144 |
int net_snmp_search_update_prefix_info(prefix_cbx **head, |
| 1145 |
prefix_cbx *node_to_use, |
| 1146 |
int functionality) |
| 1147 |
{ |
| 1148 |
|
| 1149 |
/* We define functionality based on need * |
| 1150 |
* 0 - Need to do a search and update. We have to provide the node_to_use structure filled fully * |
| 1151 |
* 1 - Need to do only search. Provide the node_to_use with in6p value filled */ |
| 1152 |
|
| 1153 |
prefix_cbx *temp_node; |
| 1154 |
netsnmp_assert(NULL != head); |
| 1155 |
netsnmp_assert(NULL != node_to_use); |
| 1156 |
|
| 1157 |
if(functionality > 1) |
| 1158 |
return -1; |
| 1159 |
if(!node_to_use) |
| 1160 |
return -1; |
| 1161 |
|
| 1162 |
|
| 1163 |
if (!functionality) { |
| 1164 |
if (!*head) { |
| 1165 |
*head = node_to_use; |
| 1166 |
return 1; |
| 1167 |
} |
| 1168 |
|
| 1169 |
for (temp_node = *head; temp_node->next_info != NULL ; temp_node = temp_node->next_info) { |
| 1170 |
if (0 == strcmp(temp_node->in6p, node_to_use->in6p)) { |
| 1171 |
temp_node->ipAddressPrefixOnLinkFlag = node_to_use->ipAddressPrefixOnLinkFlag; |
| 1172 |
temp_node->ipAddressPrefixAutonomousFlag = node_to_use->ipAddressPrefixAutonomousFlag; |
| 1173 |
return 2; |
| 1174 |
} |
| 1175 |
} |
| 1176 |
temp_node->next_info = node_to_use; |
| 1177 |
return 1; |
| 1178 |
} else { |
| 1179 |
for (temp_node = *head; temp_node != NULL ; temp_node = temp_node->next_info) { |
| 1180 |
if (0 == strcmp(temp_node->in6p, node_to_use->in6p)) { |
| 1181 |
/*need yo put sem here as i read here */ |
| 1182 |
node_to_use->ipAddressPrefixOnLinkFlag = temp_node->ipAddressPrefixOnLinkFlag; |
| 1183 |
node_to_use->ipAddressPrefixAutonomousFlag = temp_node->ipAddressPrefixAutonomousFlag; |
| 1184 |
return 1; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
return 0; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
#ifndef NETSNMP_FEATURE_REMOVE_DELETE_PREFIX_INFO |
| 1192 |
int net_snmp_delete_prefix_info(prefix_cbx **head, |
| 1193 |
char *address) |
| 1194 |
{ |
| 1195 |
|
| 1196 |
prefix_cbx *temp_node,*prev_node; |
| 1197 |
if(!address) |
| 1198 |
return -1; |
| 1199 |
if(!head) |
| 1200 |
return -1; |
| 1201 |
|
| 1202 |
for (temp_node = *head, prev_node = NULL; temp_node; |
| 1203 |
prev_node = temp_node, temp_node = temp_node->next_info) { |
| 1204 |
|
| 1205 |
if (strcmp(temp_node->in6p, address) == 0) { |
| 1206 |
if (prev_node) |
| 1207 |
prev_node->next_info = temp_node->next_info; |
| 1208 |
else |
| 1209 |
*head = temp_node->next_info; |
| 1210 |
free(temp_node); |
| 1211 |
return 1; |
| 1212 |
} |
| 1213 |
|
| 1214 |
} |
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
#endif /* NETSNMP_FEATURE_REMOVE_DELETE_PREFIX_INFO */ |
| 1218 |
|
| 1219 |
#endif /* HAVE_LINUX_RTNETLINK_H */ |
| 1220 |
|
| 1221 |
/** |
| 1222 |
* netsnmp_get_link_settings |
| 1223 |
* @name |
| 1224 |
* |
| 1225 |
* @returns 0 success |
| 1226 |
* @returns -1 Both ETHTOOL_GLINKSETTINGS and ETHTOOL_GSET failed |
| 1227 |
* @returns -2 HAVE_LINUX_ETHTOOL_H is not defined |
| 1228 |
*/ |
| 1229 |
int netsnmp_get_link_settings(struct netsnmp_linux_link_settings *nlls, |
| 1230 |
int fd, const char *name) |
| 1231 |
{ |
| 1232 |
int err = -2; |
| 1233 |
|
| 1234 |
#ifdef HAVE_LINUX_ETHTOOL_H |
| 1235 |
struct ifreq ifr; |
| 1236 |
|
| 1237 |
err = -1; |
| 1238 |
memset(&ifr, 0, sizeof(ifr)); |
| 1239 |
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 1240 |
|
| 1241 |
#ifdef ETHTOOL_GLINKSETTINGS |
| 1242 |
{ |
| 1243 |
/* |
| 1244 |
* For Linux kernel v6.3 __ETHTOOL_LINK_MODE_MASK_NBITS == 101 or |
| 1245 |
* 4 32-bit words. Increase the 'nwords' constant if necessary. |
| 1246 |
*/ |
| 1247 |
enum { mask_nwords = 8 }; |
| 1248 |
union { |
| 1249 |
struct ethtool_link_settings elinkset; |
| 1250 |
uint8_t _data[sizeof(struct ethtool_link_settings) + |
| 1251 |
mask_nwords * 3 * 4]; |
| 1252 |
} data; |
| 1253 |
|
| 1254 |
memset(&data, 0, sizeof(data)); |
| 1255 |
data.elinkset.cmd = ETHTOOL_GLINKSETTINGS; |
| 1256 |
ifr.ifr_data = &data.elinkset; |
| 1257 |
err = ioctl(fd, SIOCETHTOOL, &ifr, name); |
| 1258 |
/* |
| 1259 |
* See also the struct ethtool_link_settings documentation in |
| 1260 |
* Linux kernel header file include/uapi/linux/ethtool.h. |
| 1261 |
*/ |
| 1262 |
if (data.elinkset.link_mode_masks_nwords < 0 && |
| 1263 |
-data.elinkset.link_mode_masks_nwords <= mask_nwords) { |
| 1264 |
data.elinkset.link_mode_masks_nwords = |
| 1265 |
-data.elinkset.link_mode_masks_nwords; |
| 1266 |
err = ioctl(fd, SIOCETHTOOL, &ifr); |
| 1267 |
} |
| 1268 |
if (err >= 0) { |
| 1269 |
nlls->duplex = data.elinkset.duplex; |
| 1270 |
nlls->speed = data.elinkset.speed; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
#endif /* ETHTOOL_GLINKSETTINGS */ |
| 1274 |
|
| 1275 |
if (err < 0) { |
| 1276 |
struct ethtool_cmd edata; |
| 1277 |
|
| 1278 |
memset(&edata, 0, sizeof(edata)); |
| 1279 |
edata.cmd = ETHTOOL_GSET; |
| 1280 |
ifr.ifr_data = (char *)&edata; |
| 1281 |
err = ioctl(fd, SIOCETHTOOL, &ifr, name); |
| 1282 |
if (err >= 0) { |
| 1283 |
nlls->duplex = edata.duplex; |
| 1284 |
nlls->speed = edata.speed; |
| 1285 |
#ifdef HAVE_STRUCT_ETHTOOL_CMD_SPEED_HI |
| 1286 |
nlls->speed |= edata.speed_hi << 16; |
| 1287 |
#endif |
| 1288 |
} else { |
| 1289 |
err = -1; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
#endif /* HAVE_LINUX_ETHTOOL_H */ |
| 1293 |
|
| 1294 |
return err; |
| 1295 |
} |