]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/snmp/snmp.cpp
Utils for OID conversion, response skeleton, VarBindList printer
[stg.git] / projects / stargazer / plugins / other / snmp / snmp.cpp
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4
5 #include <cstring>
6 #include <cerrno>
7 #include <ctime>
8 #include <csignal>
9
10 #include <vector>
11 #include <algorithm>
12
13 #include "asn1/OpenPDU.h"
14 #include "asn1/ClosePDU.h"
15 #include "asn1/RReqPDU.h"
16 #include "asn1/GetRequest-PDU.h"
17 #include "asn1/GetResponse-PDU.h"
18 #include "asn1/VarBindList.h"
19 #include "asn1/VarBind.h"
20 #include "asn1/OBJECT_IDENTIFIER.h"
21 #include "asn1/ber_decoder.h"
22 #include "asn1/der_encoder.h"
23
24 #include "snmp.h"
25 #include "stg/common.h"
26
27 bool WaitPackets(int sd);
28
29 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
30 {
31 std::string res;
32
33 int arcs[1024];
34 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
35
36 if (count > 1024)
37     return "";
38
39 for (int i = 0; i < count; ++i)
40     {
41     res += ".";
42     std::string arc;
43     strprintf(&arc, "%d", arcs[i]);
44     res += arc;
45     }
46
47 return res;
48 }
49
50 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
51 {
52 size_t left = 0, pos = 0, arcPos = 0;
53 int arcs[1024];
54 pos = str.find_first_of('.', left);
55 if (pos == 0)
56     {
57     left = 1;
58     pos = str.find_first_of('.', left);
59     }
60 while (pos != std::string::npos)
61     {
62     int arc = 0;
63     if (str2x(str.substr(left, left - pos), arc))
64         {
65         return false;
66         }
67     arcs[arcPos++] = arc;
68     left = pos + 1;
69     pos = str.find_first_of('.', left);
70     }
71 if (left < str.length())
72     {
73     int arc = 0;
74     if (str2x(str.substr(left, left - pos), arc))
75         {
76         return false;
77         }
78     arcs[arcPos++] = arc;
79     }
80 printfd(__FILE__, "String2OI() - arcPos: %d\n", arcPos);
81 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos);
82 return true;
83 }
84
85 class SNMP_AGENT_CREATOR
86 {
87 private:
88     SNMP_AGENT * snmpAgent;
89
90 public:
91     SNMP_AGENT_CREATOR()
92         : snmpAgent(new SNMP_AGENT())
93         {
94         };
95     ~SNMP_AGENT_CREATOR()
96         {
97         printfd(__FILE__, "SNMP_AGENT_CREATOR::~SNMP_AGENT_CREATOR()\n");
98         delete snmpAgent;
99         };
100
101     SNMP_AGENT * GetPlugin()
102         {
103         return snmpAgent;
104         };
105 };
106
107 SNMP_AGENT_CREATOR sac;
108
109 PLUGIN * GetPlugin()
110 {
111 return sac.GetPlugin();
112 }
113
114 int SendOpenPDU(int fd)
115 {
116 const char * description = "Stg SNMP Agent";
117 //int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1, 5, 2, 1, 1};
118 asn_enc_rval_t error;
119 OpenPDU_t msg;
120
121 memset(&msg, 0, sizeof(msg));
122
123 msg.present = OpenPDU_PR_simple;
124 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
125 /*OBJECT_IDENTIFIER_set_arcs(&msg.choice.simple.identity,
126                            oid,
127                            sizeof(oid[0]),
128                            7);*/
129 if (!String2OI(".1.3.6.1.4.1.9363", &msg.choice.simple.identity))
130     {
131     printfd(__FILE__, "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
132     return -1;
133     }
134 OCTET_STRING_fromString(&msg.choice.simple.description,
135                      description);
136 OCTET_STRING_fromString(&msg.choice.simple.password,
137                      "");
138
139 char buffer[1024];
140 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
141
142 if (error.encoded == -1)
143     {
144     printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
145             error.failed_type ? error.failed_type->name : "unknown");
146     return -1;
147     }
148 else
149     {
150     write(fd, buffer, error.encoded);
151     printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n", error.encoded);
152     }
153 return 0;
154 }
155
156 int SendClosePDU(int fd)
157 {
158 ClosePDU_t msg;
159
160 memset(&msg, 0, sizeof(msg));
161
162 asn_long2INTEGER(&msg, ClosePDU_goingDown);
163
164 char buffer[1024];
165 asn_enc_rval_t error;
166 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
167
168 if (error.encoded == -1)
169     {
170     printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
171             error.failed_type ? error.failed_type->name : "unknown");
172     return -1;
173     }
174 else
175     {
176     write(fd, buffer, error.encoded);
177     printfd(__FILE__, "ClosePDU encoded successfully\n");
178     }
179 return 0;
180 }
181
182 int SendRReqPDU(int fd)
183 {
184 int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1};
185 asn_enc_rval_t error;
186 RReqPDU_t msg;
187
188 memset(&msg, 0, sizeof(msg));
189
190 msg.priority = 0;
191 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
192 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
193                            oid,
194                            sizeof(oid[0]),
195                            8);
196
197 char buffer[1024];
198 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
199
200 if (error.encoded == -1)
201     {
202     printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
203             error.failed_type ? error.failed_type->name : "unknown");
204     return -1;
205     }
206 else
207     {
208     write(fd, buffer, error.encoded);
209     printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n", error.encoded);
210     }
211 return 0;
212 }
213
214 int SendSetResponsePDU(int fd, const PDU_t * pdu)
215 {
216 //int oid[] = {1, 3, 6, 1, 4, 1, 9363, 1};
217 asn_enc_rval_t error;
218 GetResponse_PDU_t msg;
219
220 memset(&msg, 0, sizeof(msg));
221
222 msg.request_id = pdu->request_id;
223 asn_long2INTEGER(&msg.error_status, PDU__error_status_readOnly);
224 asn_long2INTEGER(&msg.error_index, PDU__error_status_readOnly);
225
226 char buffer[1024];
227 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer, sizeof(buffer));
228
229 if (error.encoded == -1)
230     {
231     printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
232             error.failed_type ? error.failed_type->name : "unknown");
233     return -1;
234     }
235 else
236     {
237     write(fd, buffer, error.encoded);
238     printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n", error.encoded);
239     }
240 return 0;
241 }
242
243 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
244 {
245 char buffer[1024];
246 SMUX_PDUs_t * pdus = NULL;
247
248 memset(buffer, 0, sizeof(buffer));
249
250 size_t length = read(fd, buffer, sizeof(buffer));
251 if (length < 1)
252     return NULL;
253 asn_dec_rval_t error;
254 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
255 if(error.code != RC_OK)
256     {
257     printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
258             (long)error.consumed);
259     return NULL;
260     }
261 return pdus;
262 }
263
264 int ParseIntInRange(const std::string & str,
265                     int min,
266                     int max,
267                     int * val)
268 {
269 if (str2x(str.c_str(), *val))
270     {
271     return -1;
272     }
273 if (*val < min || *val > max)
274     {
275     return -1;
276     }
277 return 0;
278 }
279
280 SNMP_AGENT_SETTINGS::SNMP_AGENT_SETTINGS()
281     : ip(0),
282       port(0)
283 {}
284
285 int SNMP_AGENT_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
286 {
287 PARAM_VALUE pv;
288 std::vector<PARAM_VALUE>::const_iterator pvi;
289 int p;
290 ///////////////////////////
291 pv.param = "Port";
292 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
293 if (pvi == s.moduleParams.end())
294     {
295     errorStr = "Parameter \'Port\' not found.";
296     printfd(__FILE__, "Parameter 'Port' not found\n");
297     return -1;
298     }
299 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
300     {
301     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
302     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
303     return -1;
304     }
305 port = p;
306
307 pv.param = "Password";
308 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
309 if (pvi == s.moduleParams.end())
310     {
311     errorStr = "Parameter \'Password\' not found.";
312     printfd(__FILE__, "Parameter 'Password' not found\n");
313     password = "";
314     }
315 else
316     {
317     password = pvi->value[0];
318     }
319
320 pv.param = "Server";
321 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
322 if (pvi == s.moduleParams.end())
323     {
324     errorStr = "Parameter \'Server\' not found.";
325     printfd(__FILE__, "Parameter 'Server' not found\n");
326     return -1;
327     }
328 ip = inet_strington(pvi->value[0]);
329
330 return 0;
331 }
332
333 SNMP_AGENT::SNMP_AGENT()
334     : PLUGIN(),
335       running(false),
336       stopped(true),
337       sock(-1)
338 {
339 pthread_mutex_init(&mutex, NULL);
340
341 smuxHandlers[SMUX_PDUs_PR_close] = &SNMP_AGENT::CloseHandler;
342 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SNMP_AGENT::RegisterResponseHandler;
343 smuxHandlers[SMUX_PDUs_PR_pdus] = &SNMP_AGENT::PDUsHandler;
344 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SNMP_AGENT::CommitOrRollbackHandler;
345
346 pdusHandlers[PDUs_PR_get_request] = &SNMP_AGENT::GetRequestHandler;
347 pdusHandlers[PDUs_PR_get_next_request] = &SNMP_AGENT::GetNextRequestHandler;
348 pdusHandlers[PDUs_PR_set_request] = &SNMP_AGENT::SetRequestHandler;
349 }
350
351 SNMP_AGENT::~SNMP_AGENT()
352 {
353 printfd(__FILE__, "SNMP_AGENT::~SNMP_AGENT()\n");
354 pthread_mutex_destroy(&mutex);
355 }
356
357 int SNMP_AGENT::ParseSettings()
358 {
359 return snmpAgentSettings.ParseSettings(settings);
360 }
361
362 int SNMP_AGENT::Start()
363 {
364 if (PrepareNet())
365     return -1;
366
367 if (!running)
368     {
369     if (pthread_create(&thread, NULL, Runner, this))
370         {
371         errorStr = "Cannot create thread.";
372         printfd(__FILE__, "Cannot create thread\n");
373         return -1;
374         }
375     }
376
377 return 0;
378 }
379
380 int SNMP_AGENT::Stop()
381 {
382 printfd(__FILE__, "SNMP_AGENT::Stop() - Before\n");
383 running = false;
384
385 if (!stopped)
386     {
387     //5 seconds to thread stops itself
388     for (int i = 0; i < 25 && !stopped; i++)
389         {
390         struct timespec ts = {0, 200000000};
391         nanosleep(&ts, NULL);
392         }
393
394     //after 5 seconds waiting thread still running. now killing it
395     if (!stopped)
396         {
397         printfd(__FILE__, "SNMP_AGENT::Stop() - failed to stop thread, killing it\n");
398         if (pthread_kill(thread, SIGINT))
399             {
400             errorStr = "Cannot kill thread.";
401             printfd(__FILE__, "SNMP_AGENT::Stop() - Cannot kill thread\n");
402             return -1;
403             }
404         printfd(__FILE__, "SNMP_AGENT::Stop() -  killed Run\n");
405         }
406     }
407
408 pthread_join(thread, NULL);
409
410 close(sock);
411
412 printfd(__FILE__, "SNMP_AGENT::Stop() - After\n");
413 return 0;
414 }
415
416 void * SNMP_AGENT::Runner(void * d)
417 {
418 SNMP_AGENT * snmpAgent = static_cast<SNMP_AGENT *>(d);
419
420 snmpAgent->Run();
421
422 return NULL;
423 }
424
425 void SNMP_AGENT::Run()
426 {
427 SendOpenPDU(sock);
428 SendRReqPDU(sock);
429 running = true;
430 stopped = false;
431 printfd(__FILE__, "SNMP_AGENT::Run() - Before\n");
432 while(running)
433     {
434     if (WaitPackets(sock))
435         {
436         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
437         if (pdus)
438             DispatchPDUs(pdus);
439         }
440     if (!running)
441         break;
442     }
443 printfd(__FILE__, "SNMP_AGENT::Run() - After\n");
444 SendClosePDU(sock);
445 stopped = true;
446 }
447
448 bool SNMP_AGENT::PrepareNet()
449 {
450 sock = socket(AF_INET, SOCK_STREAM, 0);
451
452 if (sock < 0)
453     {
454     errorStr = "Cannot create socket.";
455     printfd(__FILE__, "Cannot create socket\n");
456     return true;
457     }
458
459 struct sockaddr_in addr;
460
461 addr.sin_family = AF_INET;
462 addr.sin_port = htons(snmpAgentSettings.GetPort());
463 addr.sin_addr.s_addr = snmpAgentSettings.GetIP();
464
465 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
466     {
467     errorStr = "Cannot connect.";
468     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
469     return true;
470     }
471
472 return false;
473 }
474
475 bool WaitPackets(int sd)
476 {
477 fd_set rfds;
478 FD_ZERO(&rfds);
479 FD_SET(sd, &rfds);
480
481 struct timeval tv;
482 tv.tv_sec = 0;
483 tv.tv_usec = 500000;
484
485 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
486 if (res == -1) // Error
487     {
488     if (errno != EINTR)
489         {
490         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
491         }
492     return false;
493     }
494
495 if (res == 0) // Timeout
496     {
497     return false;
498     }
499
500 return true;
501 }
502
503 bool SNMP_AGENT::DispatchPDUs(const SMUX_PDUs_t * pdus)
504 {
505 SMUXHandlers::iterator it;
506 it = smuxHandlers.find(pdus->present);
507 if (it != smuxHandlers.end())
508     {
509     return (this->*(it->second))(pdus);
510     }
511 else
512     {
513     switch (pdus->present)
514         {
515         case SMUX_PDUs_PR_NOTHING:
516             printfd(__FILE__, "PDUs: nothing\n");
517             break;
518         case SMUX_PDUs_PR_open:
519             printfd(__FILE__, "PDUs: open\n");
520             break;
521         case SMUX_PDUs_PR_registerRequest:
522             printfd(__FILE__, "PDUs: registerRequest\n");
523             break;
524         default:
525             printfd(__FILE__, "PDUs: undefined\n");
526         }
527     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
528     }
529 return false;
530 }
531
532 bool SNMP_AGENT::CloseHandler(const SMUX_PDUs_t * pdus)
533 {
534 printfd(__FILE__, "SNMP_AGENT::CloseHandler()\n");
535 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
536 return false;
537 }
538
539 bool SNMP_AGENT::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
540 {
541 printfd(__FILE__, "SNMP_AGENT::RegisterResponseHandler()\n");
542 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
543 return false;
544 }
545
546 bool SNMP_AGENT::PDUsHandler(const SMUX_PDUs_t * pdus)
547 {
548 printfd(__FILE__, "SNMP_AGENT::PDUsHandler()\n");
549 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
550 PDUsHandlers::iterator it;
551 it = pdusHandlers.find(pdus->choice.pdus.present);
552 if (it != pdusHandlers.end())
553     {
554     return (this->*(it->second))(&pdus->choice.pdus);
555     }
556 else
557     {
558     switch (pdus->present)
559         {
560         case PDUs_PR_NOTHING:
561             printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - nothing\n");
562             break;
563         case PDUs_PR_get_response:
564             printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - get response\n");
565             break;
566         case PDUs_PR_trap:
567             printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - trap\n");
568             break;
569         default:
570             printfd(__FILE__, "SNMP_AGENT::PDUsHandler() - undefined\n");
571         }
572     }
573 return false;
574 }
575
576 bool SNMP_AGENT::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
577 {
578 printfd(__FILE__, "SNMP_AGENT::CommitOrRollbackHandler()\n");
579 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
580 return false;
581 }
582
583 bool SNMP_AGENT::GetRequestHandler(const PDUs_t * pdus)
584 {
585 printfd(__FILE__, "SNMP_AGENT::GetRequestHandler()\n");
586 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
587 const VarBindList_t * vbl = &pdus->choice.get_request.variable_bindings; 
588 for (int i = 0; i < vbl->list.count; ++i)
589     {
590     VarBind_t * vb = pdus->choice.get_request.variable_bindings.list.array[i];
591     printfd(__FILE__, "OID: %s\n", OI2String(&vb->name).c_str());
592     }
593 return false;
594 }
595
596 bool SNMP_AGENT::GetNextRequestHandler(const PDUs_t * pdus)
597 {
598 printfd(__FILE__, "SNMP_AGENT::GetNextRequestHandler()\n");
599 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
600 return false;
601 }
602
603 bool SNMP_AGENT::SetRequestHandler(const PDUs_t * pdus)
604 {
605 printfd(__FILE__, "SNMP_AGENT::SetRequestHandler()\n");
606 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
607 SendSetResponsePDU(sock, &pdus->choice.set_request);
608 return false;
609 }
610