| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
|
| 4 |
#include <sys/types.h> |
| 5 |
#include <net-snmp/library/snmpUnixDomain.h> |
| 6 |
|
| 7 |
#include <stddef.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <ctype.h> |
| 10 |
#include <errno.h> |
| 11 |
|
| 12 |
#ifdef HAVE_STRING_H |
| 13 |
#include <string.h> |
| 14 |
#else |
| 15 |
#include <strings.h> |
| 16 |
#endif |
| 17 |
#ifdef HAVE_STDLIB_H |
| 18 |
#include <stdlib.h> |
| 19 |
#endif |
| 20 |
#ifdef HAVE_UNISTD_H |
| 21 |
#include <unistd.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_SYS_SOCKET_H |
| 24 |
#include <sys/socket.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <net-snmp/types.h> |
| 28 |
#include <net-snmp/output_api.h> |
| 29 |
#include <net-snmp/config_api.h> |
| 30 |
|
| 31 |
#include <net-snmp/library/snmp_transport.h> |
| 32 |
#include <net-snmp/library/snmpSocketBaseDomain.h> |
| 33 |
#include <net-snmp/library/system.h> /* mkdirhier */ |
| 34 |
#include <net-snmp/library/tools.h> |
| 35 |
|
| 36 |
#ifndef NETSNMP_NO_SYSTEMD |
| 37 |
#include <net-snmp/library/sd-daemon.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
netsnmp_feature_child_of(transport_unix_socket_all, transport_all); |
| 41 |
netsnmp_feature_child_of(unix_socket_paths, transport_unix_socket_all); |
| 42 |
|
| 43 |
#ifndef NETSNMP_STREAM_QUEUE_LEN |
| 44 |
#define NETSNMP_STREAM_QUEUE_LEN 5 |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifndef SUN_LEN |
| 48 |
/* |
| 49 |
* Evaluate to actual length of the `sockaddr_un' structure. |
| 50 |
*/ |
| 51 |
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \ |
| 52 |
+ strlen ((ptr)->sun_path)) |
| 53 |
#endif |
| 54 |
|
| 55 |
oid netsnmp_UnixDomain[] = { TRANSPORT_DOMAIN_LOCAL }; |
| 56 |
static netsnmp_tdomain unixDomain; |
| 57 |
|
| 58 |
|
| 59 |
/* |
| 60 |
* This is the structure we use to hold transport-specific data. |
| 61 |
*/ |
| 62 |
|
| 63 |
typedef struct _sockaddr_un_pair { |
| 64 |
int local; |
| 65 |
struct sockaddr_un server; |
| 66 |
struct sockaddr_un client; |
| 67 |
} sockaddr_un_pair; |
| 68 |
|
| 69 |
|
| 70 |
/* |
| 71 |
* Return a string representing the address in data, or else the "far end" |
| 72 |
* address if data is NULL. |
| 73 |
*/ |
| 74 |
|
| 75 |
static char * |
| 76 |
netsnmp_unix_fmtaddr(netsnmp_transport *t, const void *data, int len) |
| 77 |
{ |
| 78 |
const struct sockaddr_un *to = NULL; |
| 79 |
|
| 80 |
if (data != NULL) |
| 81 |
to = (const struct sockaddr_un *) data; |
| 82 |
else if (t != NULL && t->data != NULL) |
| 83 |
to = &(((const sockaddr_un_pair *) t->data)->server); |
| 84 |
if (to == NULL) { |
| 85 |
/* |
| 86 |
* "Local IPC" is the Posix.1g term for Unix domain protocols, |
| 87 |
* according to W. R. Stevens, ``Unix Network Programming Volume I |
| 88 |
* Second Edition'', p. 374. |
| 89 |
*/ |
| 90 |
return strdup("Local IPC: unknown"); |
| 91 |
} else if (to->sun_path[0] == 0) { |
| 92 |
/* |
| 93 |
* This is an abstract name. We could render it as hex or something |
| 94 |
* but let's not worry about that for now. |
| 95 |
*/ |
| 96 |
return strdup("Local IPC: abstract"); |
| 97 |
} else { |
| 98 |
char *tmp; |
| 99 |
|
| 100 |
if (asprintf(&tmp, "Local IPC: %s", to->sun_path) < 0) |
| 101 |
tmp = NULL; |
| 102 |
return tmp; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
netsnmp_unix_get_taddr(netsnmp_transport *t, void **addr, size_t *addr_len) |
| 108 |
{ |
| 109 |
*addr_len = t->remote_length; |
| 110 |
*addr = netsnmp_memdup(t->remote, *addr_len); |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* You can write something into opaque that will subsequently get passed back |
| 115 |
* to your send function if you like. For instance, you might want to |
| 116 |
* remember where a PDU came from, so that you can send a reply there... |
| 117 |
*/ |
| 118 |
|
| 119 |
static int |
| 120 |
netsnmp_unix_recv(netsnmp_transport *t, void *buf, int size, |
| 121 |
void **opaque, int *olength) |
| 122 |
{ |
| 123 |
int rc = -1; |
| 124 |
socklen_t tolen = sizeof(struct sockaddr_un); |
| 125 |
struct sockaddr *to; |
| 126 |
|
| 127 |
|
| 128 |
if (t != NULL && t->sock >= 0) { |
| 129 |
to = (struct sockaddr *) malloc(sizeof(struct sockaddr_un)); |
| 130 |
if (to == NULL) { |
| 131 |
*opaque = NULL; |
| 132 |
*olength = 0; |
| 133 |
return -1; |
| 134 |
} else { |
| 135 |
memset(to, 0, tolen); |
| 136 |
} |
| 137 |
if(getsockname(t->sock, to, &tolen) != 0){ |
| 138 |
free(to); |
| 139 |
*opaque = NULL; |
| 140 |
*olength = 0; |
| 141 |
return -1; |
| 142 |
}; |
| 143 |
while (rc < 0) { |
| 144 |
#ifdef MSG_DONTWAIT |
| 145 |
rc = recvfrom(t->sock, buf, size, MSG_DONTWAIT, NULL, NULL); |
| 146 |
#else |
| 147 |
rc = recvfrom(t->sock, buf, size, 0, NULL, NULL); |
| 148 |
#endif |
| 149 |
if (rc < 0 && errno != EINTR) { |
| 150 |
DEBUGMSGTL(("netsnmp_unix", "recv fd %d err %d (\"%s\")\n", |
| 151 |
t->sock, errno, strerror(errno))); |
| 152 |
free(to); |
| 153 |
return rc; |
| 154 |
} |
| 155 |
*opaque = (void*)to; |
| 156 |
*olength = sizeof(struct sockaddr_un); |
| 157 |
} |
| 158 |
DEBUGMSGTL(("netsnmp_unix", "recv fd %d got %d bytes\n", t->sock, rc)); |
| 159 |
} |
| 160 |
return rc; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static int |
| 166 |
netsnmp_unix_send(netsnmp_transport *t, const void *buf, int size, |
| 167 |
void **opaque, int *olength) |
| 168 |
{ |
| 169 |
int rc = -1; |
| 170 |
|
| 171 |
if (t != NULL && t->sock >= 0) { |
| 172 |
DEBUGMSGTL(("netsnmp_unix", "send %d bytes to %p on fd %d\n", |
| 173 |
size, buf, t->sock)); |
| 174 |
while (rc < 0) { |
| 175 |
rc = sendto(t->sock, buf, size, 0, NULL, 0); |
| 176 |
if (rc < 0 && errno != EINTR) { |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
return rc; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static int |
| 187 |
netsnmp_unix_close(netsnmp_transport *t) |
| 188 |
{ |
| 189 |
int rc = 0; |
| 190 |
sockaddr_un_pair *sup = (sockaddr_un_pair *) t->data; |
| 191 |
|
| 192 |
if (t->sock >= 0) { |
| 193 |
#ifndef HAVE_CLOSESOCKET |
| 194 |
rc = close(t->sock); |
| 195 |
#else |
| 196 |
rc = closesocket(t->sock); |
| 197 |
#endif |
| 198 |
t->sock = -1; |
| 199 |
if (sup != NULL) { |
| 200 |
if (sup->local) { |
| 201 |
if (sup->server.sun_path[0] != 0) { |
| 202 |
DEBUGMSGTL(("netsnmp_unix", "close: server unlink(\"%s\")\n", |
| 203 |
sup->server.sun_path)); |
| 204 |
unlink(sup->server.sun_path); |
| 205 |
} |
| 206 |
} else { |
| 207 |
if (sup->client.sun_path[0] != 0) { |
| 208 |
DEBUGMSGTL(("netsnmp_unix", "close: client unlink(\"%s\")\n", |
| 209 |
sup->client.sun_path)); |
| 210 |
unlink(sup->client.sun_path); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
return rc; |
| 215 |
} else { |
| 216 |
return -1; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
static int |
| 223 |
netsnmp_unix_accept(netsnmp_transport *t) |
| 224 |
{ |
| 225 |
struct sockaddr *farend = NULL; |
| 226 |
int newsock = -1; |
| 227 |
socklen_t farendlen = sizeof(struct sockaddr_un); |
| 228 |
|
| 229 |
farend = (struct sockaddr *) malloc(farendlen); |
| 230 |
|
| 231 |
if (farend == NULL) { |
| 232 |
/* |
| 233 |
* Indicate that the acceptance of this socket failed. |
| 234 |
*/ |
| 235 |
DEBUGMSGTL(("netsnmp_unix", "accept: malloc failed\n")); |
| 236 |
return -1; |
| 237 |
} |
| 238 |
memset(farend, 0, farendlen); |
| 239 |
|
| 240 |
if (t != NULL && t->sock >= 0) { |
| 241 |
newsock = accept(t->sock, farend, &farendlen); |
| 242 |
|
| 243 |
if (newsock < 0) { |
| 244 |
DEBUGMSGTL(("netsnmp_unix","accept failed rc %d errno %d \"%s\"\n", |
| 245 |
newsock, errno, strerror(errno))); |
| 246 |
free(farend); |
| 247 |
return newsock; |
| 248 |
} |
| 249 |
|
| 250 |
if (t->data != NULL) { |
| 251 |
free(t->data); |
| 252 |
} |
| 253 |
|
| 254 |
DEBUGMSGTL(("netsnmp_unix", "accept succeeded (farend %p len %d)\n", |
| 255 |
farend, (int) farendlen)); |
| 256 |
t->data = farend; |
| 257 |
t->data_length = sizeof(struct sockaddr_un); |
| 258 |
netsnmp_sock_buffer_set(newsock, SO_SNDBUF, 1, 0); |
| 259 |
netsnmp_sock_buffer_set(newsock, SO_RCVBUF, 1, 0); |
| 260 |
return newsock; |
| 261 |
} else { |
| 262 |
free(farend); |
| 263 |
return -1; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
static int create_path = 0; |
| 268 |
static mode_t create_mode; |
| 269 |
|
| 270 |
#ifndef NETSNMP_FEATURE_REMOVE_UNIX_SOCKET_PATHS |
| 271 |
/** If trying to create unix sockets in nonexisting directories then |
| 272 |
* try to create the directory with mask mode. |
| 273 |
*/ |
| 274 |
void netsnmp_unix_create_path_with_mode(int mode) |
| 275 |
{ |
| 276 |
create_path = 1; |
| 277 |
create_mode = mode; |
| 278 |
} |
| 279 |
|
| 280 |
/** If trying to create unix sockets in nonexisting directories then |
| 281 |
* fail. |
| 282 |
*/ |
| 283 |
void netsnmp_unix_dont_create_path(void) |
| 284 |
{ |
| 285 |
create_path = 0; |
| 286 |
} |
| 287 |
#endif /* NETSNMP_FEATURE_REMOVE_UNIX_SOCKET_PATHS */ |
| 288 |
|
| 289 |
/* |
| 290 |
* Open a Unix-domain transport for SNMP. Local is TRUE if addr is the local |
| 291 |
* address to bind to (i.e. this is a server-type session); otherwise addr is |
| 292 |
* the remote address to send things to (and we make up a temporary name for |
| 293 |
* the local end of the connection). |
| 294 |
*/ |
| 295 |
|
| 296 |
netsnmp_transport * |
| 297 |
netsnmp_unix_transport(const struct sockaddr_un *addr, int local) |
| 298 |
{ |
| 299 |
netsnmp_transport *t = NULL; |
| 300 |
sockaddr_un_pair *sup = NULL; |
| 301 |
int rc = 0; |
| 302 |
int socket_initialized = 0; |
| 303 |
|
| 304 |
#ifdef NETSNMP_NO_LISTEN_SUPPORT |
| 305 |
/* SPECIAL CIRCUMSTANCE: We still want AgentX to be able to operate, |
| 306 |
so we allow for unix domain socktes to still listen when everything |
| 307 |
else isn't allowed to. Thus, we ignore this define in this file. |
| 308 |
*/ |
| 309 |
#endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
| 310 |
|
| 311 |
if (addr == NULL || addr->sun_family != AF_UNIX) { |
| 312 |
return NULL; |
| 313 |
} |
| 314 |
|
| 315 |
t = SNMP_MALLOC_TYPEDEF(netsnmp_transport); |
| 316 |
if (t == NULL) { |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
|
| 320 |
DEBUGIF("netsnmp_unix") { |
| 321 |
char *str = netsnmp_unix_fmtaddr(NULL, addr, |
| 322 |
sizeof(struct sockaddr_un)); |
| 323 |
DEBUGMSGTL(("netsnmp_unix", "open %s %s\n", local ? "local" : "remote", |
| 324 |
str)); |
| 325 |
free(str); |
| 326 |
} |
| 327 |
|
| 328 |
t->domain = netsnmp_UnixDomain; |
| 329 |
t->domain_length = |
| 330 |
sizeof(netsnmp_UnixDomain) / sizeof(netsnmp_UnixDomain[0]); |
| 331 |
|
| 332 |
t->data = malloc(sizeof(sockaddr_un_pair)); |
| 333 |
if (t->data == NULL) { |
| 334 |
netsnmp_transport_free(t); |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
memset(t->data, 0, sizeof(sockaddr_un_pair)); |
| 338 |
t->data_length = sizeof(sockaddr_un_pair); |
| 339 |
sup = (sockaddr_un_pair *) t->data; |
| 340 |
|
| 341 |
#ifndef NETSNMP_NO_SYSTEMD |
| 342 |
/* |
| 343 |
* Maybe the socket was already provided by systemd... |
| 344 |
*/ |
| 345 |
if (local) { |
| 346 |
t->sock = netsnmp_sd_find_unix_socket(SOCK_STREAM, 1, addr->sun_path); |
| 347 |
if (t->sock >= 0) |
| 348 |
socket_initialized = 1; |
| 349 |
} |
| 350 |
#endif |
| 351 |
if (!socket_initialized) |
| 352 |
t->sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 353 |
if (t->sock < 0) { |
| 354 |
netsnmp_transport_free(t); |
| 355 |
return NULL; |
| 356 |
} |
| 357 |
|
| 358 |
t->flags = NETSNMP_TRANSPORT_FLAG_STREAM; |
| 359 |
|
| 360 |
if (local) { |
| 361 |
t->local_length = strlen(addr->sun_path); |
| 362 |
t->local = strdup(addr->sun_path); |
| 363 |
if (t->local == NULL) { |
| 364 |
netsnmp_unix_close(t); |
| 365 |
netsnmp_transport_free(t); |
| 366 |
return NULL; |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
* This session is inteneded as a server, so we must bind to the given |
| 371 |
* path (unlinking it first, to avoid errors). |
| 372 |
*/ |
| 373 |
|
| 374 |
t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN; |
| 375 |
|
| 376 |
if (!socket_initialized) { |
| 377 |
unlink(addr->sun_path); |
| 378 |
rc = bind(t->sock, (const struct sockaddr *)addr, SUN_LEN(addr)); |
| 379 |
if (rc != 0 && errno == ENOENT && create_path) { |
| 380 |
rc = mkdirhier(addr->sun_path, create_mode, 1); |
| 381 |
if (rc != 0) { |
| 382 |
netsnmp_unix_close(t); |
| 383 |
netsnmp_transport_free(t); |
| 384 |
return NULL; |
| 385 |
} |
| 386 |
rc = bind(t->sock, (const struct sockaddr *)addr, |
| 387 |
SUN_LEN(addr)); |
| 388 |
} |
| 389 |
if (rc != 0) { |
| 390 |
DEBUGMSGTL(("netsnmp_unix_transport", |
| 391 |
"couldn't bind \"%s\", errno %d (%s)\n", |
| 392 |
addr->sun_path, errno, strerror(errno))); |
| 393 |
netsnmp_unix_close(t); |
| 394 |
netsnmp_transport_free(t); |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/* |
| 400 |
* Save the address in the transport-specific data pointer for later |
| 401 |
* use by netsnmp_unix_close. |
| 402 |
*/ |
| 403 |
|
| 404 |
sup->server.sun_family = AF_UNIX; |
| 405 |
strcpy(sup->server.sun_path, addr->sun_path); |
| 406 |
sup->local = 1; |
| 407 |
|
| 408 |
/* |
| 409 |
* Now sit here and listen for connections to arrive. |
| 410 |
*/ |
| 411 |
|
| 412 |
if (!socket_initialized) { |
| 413 |
rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); |
| 414 |
if (rc != 0) { |
| 415 |
DEBUGMSGTL(("netsnmp_unix_transport", |
| 416 |
"couldn't listen to \"%s\", errno %d (%s)\n", |
| 417 |
addr->sun_path, errno, strerror(errno))); |
| 418 |
netsnmp_unix_close(t); |
| 419 |
netsnmp_transport_free(t); |
| 420 |
return NULL; |
| 421 |
} |
| 422 |
} |
| 423 |
} else { |
| 424 |
t->remote_length = strlen(addr->sun_path); |
| 425 |
t->remote = strdup(addr->sun_path); |
| 426 |
if (t->remote == NULL) { |
| 427 |
netsnmp_transport_free(t); |
| 428 |
return NULL; |
| 429 |
} |
| 430 |
|
| 431 |
rc = connect(t->sock, (const struct sockaddr *)addr, |
| 432 |
sizeof(struct sockaddr_un)); |
| 433 |
if (rc != 0) { |
| 434 |
DEBUGMSGTL(("netsnmp_unix_transport", |
| 435 |
"couldn't connect to \"%s\", errno %d (%s)\n", |
| 436 |
addr->sun_path, errno, strerror(errno))); |
| 437 |
netsnmp_unix_close(t); |
| 438 |
netsnmp_transport_free(t); |
| 439 |
return NULL; |
| 440 |
} |
| 441 |
|
| 442 |
/* |
| 443 |
* Save the remote address in the transport-specific data pointer for |
| 444 |
* later use by netsnmp_unix_send. |
| 445 |
*/ |
| 446 |
|
| 447 |
sup->server.sun_family = AF_UNIX; |
| 448 |
strcpy(sup->server.sun_path, addr->sun_path); |
| 449 |
sup->local = 0; |
| 450 |
netsnmp_sock_buffer_set(t->sock, SO_SNDBUF, local, 0); |
| 451 |
netsnmp_sock_buffer_set(t->sock, SO_RCVBUF, local, 0); |
| 452 |
} |
| 453 |
|
| 454 |
/* |
| 455 |
* Message size is not limited by this transport (hence msgMaxSize |
| 456 |
* is equal to the maximum legal size of an SNMP message). |
| 457 |
*/ |
| 458 |
|
| 459 |
t->msgMaxSize = SNMP_MAX_PACKET_LEN; |
| 460 |
t->f_recv = netsnmp_unix_recv; |
| 461 |
t->f_send = netsnmp_unix_send; |
| 462 |
t->f_close = netsnmp_unix_close; |
| 463 |
t->f_accept = netsnmp_unix_accept; |
| 464 |
t->f_fmtaddr = netsnmp_unix_fmtaddr; |
| 465 |
t->f_get_taddr = netsnmp_unix_get_taddr; |
| 466 |
|
| 467 |
return t; |
| 468 |
} |
| 469 |
|
| 470 |
netsnmp_transport * |
| 471 |
netsnmp_unix_create_tstring(const char *string, int local, |
| 472 |
const char *default_target) |
| 473 |
{ |
| 474 |
struct sockaddr_un addr; |
| 475 |
|
| 476 |
if (string && *string != '\0') { |
| 477 |
} else if (default_target && *default_target != '\0') { |
| 478 |
string = default_target; |
| 479 |
} |
| 480 |
|
| 481 |
if ((string != NULL && *string != '\0') && |
| 482 |
(strlen(string) < sizeof(addr.sun_path))) { |
| 483 |
addr.sun_family = AF_UNIX; |
| 484 |
memset(addr.sun_path, 0, sizeof(addr.sun_path)); |
| 485 |
strlcpy(addr.sun_path, string, sizeof(addr.sun_path)); |
| 486 |
return netsnmp_unix_transport(&addr, local); |
| 487 |
} else { |
| 488 |
if (string != NULL && *string != '\0') { |
| 489 |
snmp_log(LOG_ERR, "Path too long for Unix domain transport\n"); |
| 490 |
} |
| 491 |
return NULL; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
netsnmp_transport * |
| 498 |
netsnmp_unix_create_ostring(const void *ostring, size_t o_len, int local) |
| 499 |
{ |
| 500 |
struct sockaddr_un addr; |
| 501 |
|
| 502 |
if (o_len > 0 && o_len < (sizeof(addr.sun_path) - 1)) { |
| 503 |
addr.sun_family = AF_UNIX; |
| 504 |
memset(addr.sun_path, 0, sizeof(addr.sun_path)); |
| 505 |
strlcpy(addr.sun_path, ostring, sizeof(addr.sun_path)); |
| 506 |
return netsnmp_unix_transport(&addr, local); |
| 507 |
} else { |
| 508 |
if (o_len > 0) { |
| 509 |
snmp_log(LOG_ERR, "Path too long for Unix domain transport\n"); |
| 510 |
} |
| 511 |
} |
| 512 |
return NULL; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
void |
| 518 |
netsnmp_unix_ctor(void) |
| 519 |
{ |
| 520 |
unixDomain.name = netsnmp_UnixDomain; |
| 521 |
unixDomain.name_length = sizeof(netsnmp_UnixDomain) / sizeof(oid); |
| 522 |
unixDomain.prefix = (const char**)calloc(2, sizeof(char *)); |
| 523 |
unixDomain.prefix[0] = "unix"; |
| 524 |
|
| 525 |
unixDomain.f_create_from_tstring_new = netsnmp_unix_create_tstring; |
| 526 |
unixDomain.f_create_from_ostring = netsnmp_unix_create_ostring; |
| 527 |
|
| 528 |
netsnmp_tdomain_register(&unixDomain); |
| 529 |
} |
| 530 |
|
| 531 |
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C) |
| 532 |
/* support for SNMPv1 and SNMPv2c on unix domain*/ |
| 533 |
|
| 534 |
#define EXAMPLE_COMMUNITY "COMMUNITY" |
| 535 |
typedef struct com2SecUnixEntry_s { |
| 536 |
const char* sockpath; |
| 537 |
const char* secName; |
| 538 |
const char* contextName; |
| 539 |
struct com2SecUnixEntry_s *next; |
| 540 |
unsigned short pathlen; |
| 541 |
const char community[1]; |
| 542 |
} com2SecUnixEntry; |
| 543 |
|
| 544 |
static com2SecUnixEntry *com2SecUnixList = NULL, *com2SecUnixListLast = NULL; |
| 545 |
|
| 546 |
|
| 547 |
int |
| 548 |
netsnmp_unix_getSecName(void *opaque, int olength, |
| 549 |
const char *community, |
| 550 |
size_t community_len, |
| 551 |
const char **secName, const char **contextName) |
| 552 |
{ |
| 553 |
const com2SecUnixEntry *c; |
| 554 |
struct sockaddr_un *to = (struct sockaddr_un *) opaque; |
| 555 |
char *ztcommunity = NULL; |
| 556 |
|
| 557 |
if (secName != NULL) { |
| 558 |
*secName = NULL; /* Haven't found anything yet */ |
| 559 |
} |
| 560 |
|
| 561 |
/* |
| 562 |
* Special case if there are NO entries (as opposed to no MATCHING |
| 563 |
* entries). |
| 564 |
*/ |
| 565 |
|
| 566 |
if (com2SecUnixList == NULL) { |
| 567 |
DEBUGMSGTL(("netsnmp_unix_getSecName", "no com2sec entries\n")); |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
/* |
| 572 |
* If there is no unix socket path, then there can be no valid security |
| 573 |
* name. |
| 574 |
*/ |
| 575 |
|
| 576 |
if (opaque == NULL || olength != sizeof(struct sockaddr_un) || |
| 577 |
to->sun_family != AF_UNIX) { |
| 578 |
DEBUGMSGTL(("netsnmp_unix_getSecName", |
| 579 |
"no unix destine address in PDU?\n")); |
| 580 |
return 1; |
| 581 |
} |
| 582 |
|
| 583 |
DEBUGIF("netsnmp_unix_getSecName") { |
| 584 |
ztcommunity = (char *)malloc(community_len + 1); |
| 585 |
if (ztcommunity != NULL) { |
| 586 |
memcpy(ztcommunity, community, community_len); |
| 587 |
ztcommunity[community_len] = '\0'; |
| 588 |
} |
| 589 |
|
| 590 |
DEBUGMSGTL(("netsnmp_unix_getSecName", "resolve <\"%s\">\n", |
| 591 |
ztcommunity ? ztcommunity : "<malloc error>")); |
| 592 |
} |
| 593 |
|
| 594 |
for (c = com2SecUnixList; c != NULL; c = c->next) { |
| 595 |
DEBUGMSGTL(("netsnmp_unix_getSecName","compare <\"%s\",to socket %s>", |
| 596 |
c->community, c->sockpath )); |
| 597 |
if ((community_len == strlen(c->community)) && |
| 598 |
(memcmp(community, c->community, community_len) == 0) && |
| 599 |
/* compare sockpath, if pathlen == 0, always match */ |
| 600 |
(strlen(to->sun_path) == c->pathlen || c->pathlen == 0) && |
| 601 |
(memcmp(to->sun_path, c->sockpath, c->pathlen) == 0) |
| 602 |
) { |
| 603 |
DEBUGMSG(("netsnmp_unix_getSecName", "... SUCCESS\n")); |
| 604 |
if (secName != NULL) { |
| 605 |
*secName = c->secName; |
| 606 |
*contextName = c->contextName; |
| 607 |
} |
| 608 |
break; |
| 609 |
} |
| 610 |
DEBUGMSG(("netsnmp_unix_getSecName", "... nope\n")); |
| 611 |
} |
| 612 |
if (ztcommunity != NULL) { |
| 613 |
free(ztcommunity); |
| 614 |
} |
| 615 |
return 1; |
| 616 |
} |
| 617 |
|
| 618 |
void |
| 619 |
netsnmp_unix_parse_security(const char *token, char *param) |
| 620 |
{ |
| 621 |
char secName[VACMSTRINGLEN + 1]; |
| 622 |
size_t secNameLen; |
| 623 |
char contextName[VACMSTRINGLEN + 1]; |
| 624 |
size_t contextNameLen; |
| 625 |
char community[COMMUNITY_MAX_LEN + 1]; |
| 626 |
size_t communityLen; |
| 627 |
char sockpath[sizeof(((struct sockaddr_un*)0)->sun_path) + 1]; |
| 628 |
size_t sockpathLen; |
| 629 |
|
| 630 |
param = copy_nword( param, secName, sizeof(secName)); |
| 631 |
if (strcmp(secName, "-Cn") == 0) { |
| 632 |
if (!param) { |
| 633 |
config_perror("missing CONTEXT_NAME parameter"); |
| 634 |
return; |
| 635 |
} |
| 636 |
param = copy_nword( param, contextName, sizeof(contextName)); |
| 637 |
contextNameLen = strlen(contextName) + 1; |
| 638 |
if (contextNameLen > VACMSTRINGLEN) { |
| 639 |
config_perror("context name too long"); |
| 640 |
return; |
| 641 |
} |
| 642 |
if (!param) { |
| 643 |
config_perror("missing NAME parameter"); |
| 644 |
return; |
| 645 |
} |
| 646 |
param = copy_nword( param, secName, sizeof(secName)); |
| 647 |
} else { |
| 648 |
contextNameLen = 0; |
| 649 |
} |
| 650 |
|
| 651 |
secNameLen = strlen(secName) + 1; |
| 652 |
if (secNameLen == 1) { |
| 653 |
config_perror("empty NAME parameter"); |
| 654 |
return; |
| 655 |
} else if (secNameLen > VACMSTRINGLEN) { |
| 656 |
config_perror("security name too long"); |
| 657 |
return; |
| 658 |
} |
| 659 |
|
| 660 |
if (!param) { |
| 661 |
config_perror("missing SOCKPATH parameter"); |
| 662 |
return; |
| 663 |
} |
| 664 |
param = copy_nword( param, sockpath, sizeof(sockpath)); |
| 665 |
if (sockpath[0] == '\0') { |
| 666 |
config_perror("empty SOCKPATH parameter"); |
| 667 |
return; |
| 668 |
} |
| 669 |
sockpathLen = strlen(sockpath) + 1; |
| 670 |
if (sockpathLen > sizeof(((struct sockaddr_un*)0)->sun_path)) { |
| 671 |
config_perror("sockpath too long"); |
| 672 |
return; |
| 673 |
} |
| 674 |
|
| 675 |
if (!param) { |
| 676 |
config_perror("missing COMMUNITY parameter"); |
| 677 |
return; |
| 678 |
} |
| 679 |
param = copy_nword( param, community, sizeof(community)); |
| 680 |
if (community[0] == '\0') { |
| 681 |
config_perror("empty COMMUNITY parameter"); |
| 682 |
return; |
| 683 |
} |
| 684 |
communityLen = strlen(community) + 1; |
| 685 |
if (communityLen >= COMMUNITY_MAX_LEN) { |
| 686 |
config_perror("community name too long"); |
| 687 |
return; |
| 688 |
} |
| 689 |
if (communityLen == sizeof(EXAMPLE_COMMUNITY) && |
| 690 |
memcmp(community, EXAMPLE_COMMUNITY, sizeof(EXAMPLE_COMMUNITY)) == 0) { |
| 691 |
config_perror("example config COMMUNITY not properly configured"); |
| 692 |
return; |
| 693 |
} |
| 694 |
|
| 695 |
/* Deal with the "default" case */ |
| 696 |
if(strcmp(sockpath, "default") == 0) { |
| 697 |
sockpathLen = 0; |
| 698 |
} |
| 699 |
|
| 700 |
{ |
| 701 |
void* v = malloc(offsetof(com2SecUnixEntry, community) + communityLen + |
| 702 |
sockpathLen + secNameLen + contextNameLen); |
| 703 |
com2SecUnixEntry* e = (com2SecUnixEntry*)v; |
| 704 |
char* last = ((char*)v) + offsetof(com2SecUnixEntry, community); |
| 705 |
if (e == NULL) { |
| 706 |
config_perror("memory error"); |
| 707 |
return; |
| 708 |
} |
| 709 |
|
| 710 |
DEBUGMSGTL(("netsnmp_unix_parse_security", |
| 711 |
"<\"%s\", \"%.*s\"> => \"%s\"\n", |
| 712 |
community, (int)sockpathLen, sockpath, secName)); |
| 713 |
|
| 714 |
memcpy(last, community, communityLen); |
| 715 |
last += communityLen; |
| 716 |
|
| 717 |
if (sockpathLen) { |
| 718 |
e->sockpath = last; |
| 719 |
memcpy(last, sockpath, sockpathLen); |
| 720 |
last += sockpathLen; |
| 721 |
e->pathlen = sockpathLen - 1; |
| 722 |
} else { |
| 723 |
e->sockpath = last - 1; |
| 724 |
e->pathlen = 0; |
| 725 |
} |
| 726 |
|
| 727 |
e->secName = last; |
| 728 |
memcpy(last, secName, secNameLen); |
| 729 |
last += secNameLen; |
| 730 |
|
| 731 |
if (contextNameLen) { |
| 732 |
e->contextName = last; |
| 733 |
memcpy(last, contextName, contextNameLen); |
| 734 |
last += contextNameLen; |
| 735 |
} else |
| 736 |
e->contextName = last - 1; |
| 737 |
|
| 738 |
e->next = NULL; |
| 739 |
|
| 740 |
if (com2SecUnixListLast != NULL) { |
| 741 |
com2SecUnixListLast->next = e; |
| 742 |
com2SecUnixListLast = e; |
| 743 |
} else { |
| 744 |
com2SecUnixListLast = com2SecUnixList = e; |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
void |
| 750 |
netsnmp_unix_com2SecList_free(void) |
| 751 |
{ |
| 752 |
com2SecUnixEntry *e = com2SecUnixList; |
| 753 |
while (e != NULL) { |
| 754 |
com2SecUnixEntry *tmp = e; |
| 755 |
e = e->next; |
| 756 |
free(tmp); |
| 757 |
} |
| 758 |
com2SecUnixList = com2SecUnixListLast = NULL; |
| 759 |
} |
| 760 |
#endif /* support for community based SNMP */ |
| 761 |
|
| 762 |
void |
| 763 |
netsnmp_unix_agent_config_tokens_register(void) |
| 764 |
{ |
| 765 |
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C) |
| 766 |
register_app_config_handler("com2secunix", netsnmp_unix_parse_security, |
| 767 |
netsnmp_unix_com2SecList_free, |
| 768 |
"[-Cn CONTEXT] secName sockpath community"); |
| 769 |
#endif /* support for community based SNMP */ |
| 770 |
} |