Debian Codegraph @

snmplib/transports/snmpCallbackDomain.c

raw
1 #include <net-snmp/net-snmp-config.h>
2
3 #include <net-snmp/library/snmpCallbackDomain.h>
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <errno.h>
9
10 #ifdef WIN32
11 #include <net-snmp/library/winpipe.h>
12 #endif
13 #ifdef HAVE_STRING_H
14 #include <string.h>
15 #else
16 #include <strings.h>
17 #endif
18 #ifdef HAVE_STDLIB_H
19 #include <stdlib.h>
20 #endif
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
26 #endif
27 #ifdef HAVE_SYS_UN_H
28 #include <sys/un.h>
29 #endif
30 #ifdef HAVE_IO_H
31 #include <io.h>
32 #endif
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36
37 #include <net-snmp/types.h>
38 #include <net-snmp/output_api.h>
39 #include <net-snmp/config_api.h>
40 #include <net-snmp/utilities.h>
41
42 #include <net-snmp/library/snmp_transport.h>
43 #include <net-snmp/library/snmp_api.h>
44 #include <net-snmp/library/snmp_client.h>
45
46 #ifndef NETSNMP_STREAM_QUEUE_LEN
47 #define NETSNMP_STREAM_QUEUE_LEN 5
48 #endif
49
50 #ifdef NETSNMP_TRANSPORT_CALLBACK_DOMAIN
51
52 static netsnmp_transport_list *trlist = NULL;
53
54 static int callback_count = 0;
55
56 typedef struct callback_hack_s {
57 void *orig_transport_data;
58 netsnmp_pdu *pdu;
59 } callback_hack;
60
61 typedef struct callback_queue_s {
62 int callback_num;
63 netsnmp_callback_pass *item;
64 struct callback_queue_s *next, *prev;
65 } callback_queue;
66
67 callback_queue *thequeue;
68
69 static netsnmp_transport *
70 find_transport_from_callback_num(int num)
71 {
72 netsnmp_transport_list *ptr;
73 netsnmp_callback_info *ci;
74
75 for (ptr = trlist; ptr; ptr = ptr->next) {
76 ci = ptr->transport->data;
77 if (ci->callback_num == num)
78 return ptr->transport;
79 }
80 return NULL;
81 }
82
83 static void
84 callback_debug_pdu(const char *ourstring, netsnmp_pdu *pdu)
85 {
86 netsnmp_variable_list *vb;
87 int i = 1;
88 DEBUGMSGTL((ourstring,
89 "PDU: command = %d, errstat = %ld, errindex = %ld\n",
90 pdu->command, pdu->errstat, pdu->errindex));
91 for (vb = pdu->variables; vb; vb = vb->next_variable) {
92 DEBUGMSGTL((ourstring, " var %d:", i++));
93 DEBUGMSGVAR((ourstring, vb));
94 DEBUGMSG((ourstring, "\n"));
95 }
96 }
97
98 void
99 callback_push_queue(int num, netsnmp_callback_pass *item)
100 {
101 callback_queue *newitem = SNMP_MALLOC_TYPEDEF(callback_queue);
102 callback_queue *ptr;
103
104 if (newitem == NULL)
105 return;
106 newitem->callback_num = num;
107 newitem->item = item;
108 if (thequeue) {
109 for (ptr = thequeue; ptr && ptr->next; ptr = ptr->next) {
110 }
111 ptr->next = newitem;
112 newitem->prev = ptr;
113 } else {
114 thequeue = newitem;
115 }
116 DEBUGIF("dump_send_callback_transport") {
117 callback_debug_pdu("dump_send_callback_transport", item->pdu);
118 }
119 }
120
121 netsnmp_callback_pass *
122 callback_pop_queue(int num)
123 {
124 netsnmp_callback_pass *cp;
125 callback_queue *ptr;
126
127 for (ptr = thequeue; ptr; ptr = ptr->next) {
128 if (ptr->callback_num == num) {
129 if (ptr->prev) {
130 ptr->prev->next = ptr->next;
131 } else {
132 thequeue = ptr->next;
133 }
134 if (ptr->next) {
135 ptr->next->prev = ptr->prev;
136 }
137 cp = ptr->item;
138 SNMP_FREE(ptr);
139 DEBUGIF("dump_recv_callback_transport") {
140 callback_debug_pdu("dump_recv_callback_transport",
141 cp->pdu);
142 }
143 return cp;
144 }
145 }
146 return NULL;
147 }
148
149 /*
150 * Return a string representing the address in data, or else the "far end"
151 * address if data is NULL.
152 */
153
154 char *
155 netsnmp_callback_fmtaddr(netsnmp_transport *t, const void *data, int len)
156 {
157 char *buf;
158 netsnmp_callback_info *mystuff;
159
160 if (!t || !t->data)
161 return strdup("callback: unknown");
162
163 mystuff = t->data;
164 if (asprintf(&buf, "callback: %d on fd %d", mystuff->callback_num,
165 mystuff->pipefds[0]) < 0)
166 buf = NULL;
167 return buf;
168 }
169
170
171
172 /*
173 * You can write something into opaque that will subsequently get passed back
174 * to your send function if you like. For instance, you might want to
175 * remember where a PDU came from, so that you can send a reply there...
176 */
177
178 int
179 netsnmp_callback_recv(netsnmp_transport *t, void *buf, int size,
180 void **opaque, int *olength)
181 {
182 int rc = -1;
183 char newbuf[1];
184 netsnmp_callback_info *mystuff = t->data;
185
186 DEBUGMSGTL(("transport_callback", "hook_recv enter\n"));
187
188 while (rc < 0) {
189 #ifdef WIN32
190 rc = recvfrom(mystuff->pipefds[0], newbuf, 1, 0, NULL, 0);
191 #else
192 rc = read(mystuff->pipefds[0], newbuf, 1);
193 #endif
194 if (rc < 0 && errno != EINTR) {
195 break;
196 }
197 }
198 if (rc > 0)
199 memset(buf, 0, rc);
200
201 if (mystuff->linkedto) {
202 /*
203 * we're the client. We don't need to do anything.
204 */
205 } else {
206 /*
207 * malloc the space here, but it's filled in by
208 * snmp_callback_created_pdu() below
209 */
210 *opaque = calloc(1, sizeof(int));
211 *olength = sizeof(int);
212 }
213 DEBUGMSGTL(("transport_callback", "hook_recv exit\n"));
214 return rc;
215 }
216
217
218
219 int
220 netsnmp_callback_send(netsnmp_transport *t, const void *buf, int size,
221 void **opaque, int *olength)
222 {
223 int from, rc = -1;
224 netsnmp_callback_info *mystuff = t->data, *theirstuff;
225 netsnmp_callback_pass *cp;
226
227 /*
228 * extract the pdu from the hacked buffer
229 */
230 netsnmp_transport *other_side;
231 callback_hack *ch = *opaque;
232 netsnmp_pdu *pdu = ch->pdu;
233
234 *opaque = ch->orig_transport_data;
235 SNMP_FREE(ch);
236
237 DEBUGMSGTL(("transport_callback", "hook_send enter\n"));
238
239 cp = SNMP_MALLOC_TYPEDEF(netsnmp_callback_pass);
240 if (!cp)
241 return -1;
242
243 cp->pdu = snmp_clone_pdu(pdu);
244 if (cp->pdu->transport_data) {
245 /*
246 * not needed and not properly freed later
247 */
248 SNMP_FREE(cp->pdu->transport_data);
249 }
250
251 if (cp->pdu->flags & UCD_MSG_FLAG_EXPECT_RESPONSE)
252 cp->pdu->flags ^= UCD_MSG_FLAG_EXPECT_RESPONSE;
253
254 /*
255 * push the sent pdu onto the stack
256 */
257 /*
258 * AND send a bogus byte to the remote callback receiver's pipe
259 */
260 if (mystuff->linkedto) {
261 /*
262 * we're the client, send it to the parent
263 */
264 cp->return_transport_num = mystuff->callback_num;
265
266 other_side = find_transport_from_callback_num(mystuff->linkedto);
267 if (!other_side) {
268 snmp_free_pdu(cp->pdu);
269 SNMP_FREE(cp);
270 return -1;
271 }
272
273 theirstuff = other_side->data;
274 while (rc < 0) {
275 #ifdef WIN32
276 rc = sendto(theirstuff->pipefds[1], " ", 1, 0, NULL, 0);
277 #else
278 rc = write(theirstuff->pipefds[1], " ", 1);
279 #endif
280 if (rc < 0 && errno != EINTR) {
281 break;
282 }
283 }
284 callback_push_queue(mystuff->linkedto, cp);
285 /*
286 * we don't need the transport data any more
287 */
288 SNMP_FREE(*opaque);
289 } else {
290 /*
291 * we're the server, send it to the person that sent us the request
292 */
293 from = **((int **) opaque);
294 /*
295 * we don't need the transport data any more
296 */
297 SNMP_FREE(*opaque);
298 other_side = find_transport_from_callback_num(from);
299 if (!other_side) {
300 snmp_free_pdu(cp->pdu);
301 SNMP_FREE(cp);
302 return -1;
303 }
304 theirstuff = other_side->data;
305 while (rc < 0) {
306 #ifdef WIN32
307 rc = sendto(theirstuff->pipefds[1], " ", 1, 0, NULL, 0);
308 #else
309 rc = write(theirstuff->pipefds[1], " ", 1);
310 #endif
311 if (rc < 0 && errno != EINTR) {
312 break;
313 }
314 }
315 callback_push_queue(from, cp);
316 }
317
318 DEBUGMSGTL(("transport_callback", "hook_send exit\n"));
319 return 0;
320 }
321
322
323
324 int
325 netsnmp_callback_close(netsnmp_transport *t)
326 {
327 int rc;
328 netsnmp_callback_info *mystuff = t->data;
329 DEBUGMSGTL(("transport_callback", "hook_close enter\n"));
330
331 #ifdef HAVE_CLOSESOCKET
332 rc = closesocket(mystuff->pipefds[0]);
333 rc |= closesocket(mystuff->pipefds[1]);
334 #else
335 rc = close(mystuff->pipefds[0]);
336 rc |= close(mystuff->pipefds[1]);
337 #endif
338
339 rc |= netsnmp_transport_remove_from_list(&trlist, t);
340
341 DEBUGMSGTL(("transport_callback", "hook_close exit\n"));
342 return rc;
343 }
344
345
346
347 int
348 netsnmp_callback_accept(netsnmp_transport *t)
349 {
350 DEBUGMSGTL(("transport_callback", "hook_accept enter\n"));
351 DEBUGMSGTL(("transport_callback", "hook_accept exit\n"));
352 return -1;
353 }
354
355
356
357 /*
358 * Open a Callback-domain transport for SNMP. Local is TRUE if addr
359 * is the local address to bind to (i.e. this is a server-type
360 * session); otherwise addr is the remote address to send things to
361 * (and we make up a temporary name for the local end of the
362 * connection).
363 */
364
365 netsnmp_transport *
366 netsnmp_callback_transport(int to)
367 {
368
369 netsnmp_transport *t = NULL;
370 netsnmp_callback_info *mydata;
371 int rc;
372
373 /*
374 * transport
375 */
376 t = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
377 if (!t)
378 return NULL;
379
380 /*
381 * our stuff
382 */
383 mydata = SNMP_MALLOC_TYPEDEF(netsnmp_callback_info);
384 if (!mydata) {
385 SNMP_FREE(t);
386 return NULL;
387 }
388 mydata->linkedto = to;
389 mydata->callback_num = ++callback_count;
390 mydata->data = NULL;
391 t->data = mydata;
392
393 #ifdef WIN32
394 rc = create_winpipe_transport(mydata->pipefds);
395 #else
396 rc = pipe(mydata->pipefds);
397 #endif
398
399 if (rc) {
400 netsnmp_transport_free(t);
401 return NULL;
402 }
403
404 netsnmp_assert(mydata->pipefds[0] != -1);
405 t->sock = mydata->pipefds[0];
406
407 /*
408 * Message size is not limited by this transport (hence msgMaxSize
409 * is equal to the maximum legal size of an SNMP message).
410 */
411
412 t->msgMaxSize = SNMP_MAX_PACKET_LEN;
413 t->f_recv = netsnmp_callback_recv;
414 t->f_send = netsnmp_callback_send;
415 t->f_close = netsnmp_callback_close;
416 t->f_accept = netsnmp_callback_accept;
417 t->f_fmtaddr = netsnmp_callback_fmtaddr;
418
419 netsnmp_transport_add_to_list(&trlist, t);
420
421 if (to)
422 DEBUGMSGTL(("transport_callback", "initialized %d linked to %d\n",
423 mydata->callback_num, to));
424 else
425 DEBUGMSGTL(("transport_callback",
426 "initialized master listening on %d\n",
427 mydata->callback_num));
428 return t;
429 }
430
431 int
432 netsnmp_callback_hook_parse(netsnmp_session * sp,
433 netsnmp_pdu *pdu,
434 u_char * packetptr, size_t len)
435 {
436 if (SNMP_MSG_RESPONSE == pdu->command ||
437 SNMP_MSG_REPORT == pdu->command)
438 pdu->flags |= UCD_MSG_FLAG_RESPONSE_PDU;
439 else
440 pdu->flags &= (~UCD_MSG_FLAG_RESPONSE_PDU);
441
442 return SNMP_ERR_NOERROR;
443 }
444
445 int
446 netsnmp_callback_hook_build(netsnmp_session * sp,
447 netsnmp_pdu *pdu, u_char * ptk, size_t * len)
448 {
449 /*
450 * very gross hack, as this is passed later to the transport_send
451 * function
452 */
453 callback_hack *ch = SNMP_MALLOC_TYPEDEF(callback_hack);
454 if (ch == NULL)
455 return -1;
456 DEBUGMSGTL(("transport_callback", "hook_build enter\n"));
457 ch->pdu = pdu;
458 ch->orig_transport_data = pdu->transport_data;
459 pdu->transport_data = ch;
460 switch (pdu->command) {
461 case SNMP_MSG_GETBULK:
462 if (pdu->max_repetitions < 0) {
463 sp->s_snmp_errno = SNMPERR_BAD_REPETITIONS;
464 return -1;
465 }
466 if (pdu->non_repeaters < 0) {
467 sp->s_snmp_errno = SNMPERR_BAD_REPEATERS;
468 return -1;
469 }
470 break;
471 case SNMP_MSG_RESPONSE:
472 case SNMP_MSG_TRAP:
473 case SNMP_MSG_TRAP2:
474 pdu->flags &= (~UCD_MSG_FLAG_EXPECT_RESPONSE);
475 /* FALL THROUGH */
476 default:
477 if (pdu->errstat == SNMP_DEFAULT_ERRSTAT)
478 pdu->errstat = 0;
479 if (pdu->errindex == SNMP_DEFAULT_ERRINDEX)
480 pdu->errindex = 0;
481 break;
482 }
483
484 /*
485 * Copy missing values from session defaults
486 */
487 switch (pdu->version) {
488 #ifndef NETSNMP_DISABLE_SNMPV1
489 case SNMP_VERSION_1:
490 #endif
491 #ifndef NETSNMP_DISABLE_SNMPV2C
492 case SNMP_VERSION_2c:
493 #endif
494 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
495 if (pdu->community_len == 0) {
496 if (sp->community_len == 0) {
497 sp->s_snmp_errno = SNMPERR_BAD_COMMUNITY;
498 return -1;
499 }
500 pdu->community = malloc(sp->community_len);
501 if (pdu->community == NULL) {
502 sp->s_snmp_errno = SNMPERR_MALLOC;
503 return -1;
504 }
505 memmove(pdu->community,
506 sp->community, sp->community_len);
507 pdu->community_len = sp->community_len;
508 }
509 break;
510 #endif
511 case SNMP_VERSION_3:
512 if (pdu->securityNameLen == 0) {
513 pdu->securityName = netsnmp_memdup(sp->securityName,
514 sp->securityNameLen + 1);
515 if (pdu->securityName == NULL) {
516 sp->s_snmp_errno = SNMPERR_MALLOC;
517 return -1;
518 }
519 pdu->securityNameLen = sp->securityNameLen;
520 }
521 if (pdu->securityModel == -1)
522 pdu->securityModel = sp->securityModel;
523 if (pdu->securityLevel == 0)
524 pdu->securityLevel = sp->securityLevel;
525 /* WHAT ELSE ?? */
526 }
527 ptk[0] = 0;
528 *len = 1;
529 DEBUGMSGTL(("transport_callback", "hook_build exit\n"));
530 return 1;
531 }
532
533 int
534 netsnmp_callback_check_packet(u_char * pkt, size_t len)
535 {
536 return 1;
537 }
538
539 netsnmp_pdu *
540 netsnmp_callback_create_pdu(netsnmp_transport *transport,
541 void *opaque, size_t olength)
542 {
543 netsnmp_pdu *pdu;
544 netsnmp_callback_pass *cp =
545 callback_pop_queue(((netsnmp_callback_info *) transport->data)->
546 callback_num);
547 if (!cp)
548 return NULL;
549 pdu = cp->pdu;
550 pdu->transport_data = opaque;
551 pdu->transport_data_length = olength;
552 if (opaque) /* if created, we're the server */
553 *((int *) opaque) = cp->return_transport_num;
554 SNMP_FREE(cp);
555 return pdu;
556 }
557
558 netsnmp_session *
559 netsnmp_callback_open(int attach_to,
560 int (*return_func) (int op,
561 netsnmp_session * session,
562 int reqid, netsnmp_pdu *pdu,
563 void *magic),
564 int (*fpre_parse) (netsnmp_session *,
565 struct netsnmp_transport_s *,
566 void *, int),
567 int (*fpost_parse) (netsnmp_session *, netsnmp_pdu *,
568 int))
569 {
570 netsnmp_session callback_sess, *callback_ss;
571 netsnmp_transport *callback_tr;
572
573 callback_tr = netsnmp_callback_transport(attach_to);
574 snmp_sess_init(&callback_sess);
575 callback_sess.callback = return_func;
576 if (attach_to) {
577 /*
578 * client
579 */
580 /*
581 * trysess.community = (u_char *) callback_ss;
582 */
583 } else {
584 callback_sess.isAuthoritative = SNMP_SESS_AUTHORITATIVE;
585 }
586 callback_sess.retries = 0;
587 callback_sess.timeout = 30000000;
588 callback_sess.version = SNMP_DEFAULT_VERSION; /* (mostly) bogus */
589 callback_ss = snmp_add_full(&callback_sess, callback_tr,
590 fpre_parse,
591 netsnmp_callback_hook_parse, fpost_parse,
592 netsnmp_callback_hook_build,
593 NULL,
594 netsnmp_callback_check_packet,
595 netsnmp_callback_create_pdu);
596 if (callback_ss)
597 callback_ss->local_port =
598 ((netsnmp_callback_info *) callback_tr->data)->callback_num;
599 return callback_ss;
600 }
601
602
603
604 void
605 netsnmp_clear_callback_list(void)
606 {
607
608 netsnmp_transport_list *list = trlist, *next = NULL;
609 netsnmp_transport *tr = NULL;
610
611 DEBUGMSGTL(("callback_clear", "called netsnmp_callback_clear_list()\n"));
612 while (list != NULL) {
613 next = list->next;
614 tr = list->transport;
615
616 if (tr != NULL) {
617 tr->f_close(tr);
618 netsnmp_transport_remove_from_list(&trlist, tr);
619 netsnmp_transport_free(tr);
620 }
621 list = next;
622 }
623 trlist = NULL;
624
625 }
626
627 #endif /* NETSNMP_TRANSPORT_CALLBACK_DOMAIN */