Debian Codegraph @

snmplib/transports/snmpSSHDomain.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-features.h>
3
4 #include <net-snmp/library/snmp_assert.h>
5 #include <net-snmp/library/snmpSSHDomain.h>
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <errno.h>
10
11 #include <libssh2.h>
12 #include <libssh2_sftp.h>
13
14 #ifdef HAVE_SYS_PARAM_H
15 #include <sys/param.h>
16 #endif
17 #ifdef HAVE_STRING_H
18 #include <string.h>
19 #else
20 #include <strings.h>
21 #endif
22 #ifdef HAVE_STDLIB_H
23 #include <stdlib.h>
24 #endif
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
30 #endif
31 #ifdef HAVE_SYS_UN_H
32 #include <sys/un.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
39 #endif
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif
43
44 #include <pwd.h>
45
46 #ifndef MAXPATHLEN
47 #warning no system max path length detected
48 #define MAXPATHLEN 2048
49 #endif
50
51 #include <net-snmp/types.h>
52 #include <net-snmp/output_api.h>
53 #include <net-snmp/library/tools.h>
54 #include <net-snmp/library/system.h>
55 #include <net-snmp/library/default_store.h>
56
57 #include <net-snmp/library/snmp_transport.h>
58 #include <net-snmp/library/snmpIPv4BaseDomain.h>
59 #include <net-snmp/library/snmpSocketBaseDomain.h>
60 #include <net-snmp/library/read_config.h>
61
62 netsnmp_feature_require(user_information);
63
64 #define MAX_NAME_LENGTH 127
65
66 #define NETSNMP_SSHTOSNMP_VERSION1 1
67 #define NETSNMP_MAX_SSHTOSNMP_VERSION 1
68
69 #define DEFAULT_SOCK_NAME "sshdomainsocket"
70
71 typedef struct netsnmp_ssh_addr_pair_s {
72 struct sockaddr_in remote_addr;
73 struct in_addr local_addr;
74 LIBSSH2_SESSION *session;
75 LIBSSH2_CHANNEL *channel;
76 char username[MAX_NAME_LENGTH+1];
77 struct sockaddr_un unix_socket_end;
78 char socket_path[MAXPATHLEN];
79 } netsnmp_ssh_addr_pair;
80
81 const oid netsnmp_snmpSSHDomain[] = { TRANSPORT_DOMAIN_SSH_IP };
82 static netsnmp_tdomain sshDomain;
83
84 #define SNMPSSHDOMAIN_USE_EXTERNAL_PIPE 1
85
86 /*
87 * Return a string representing the address in data, or else the "far end"
88 * address if data is NULL.
89 */
90
91 static char *
92 netsnmp_ssh_fmtaddr(netsnmp_transport *t, const void *data, int len)
93 {
94 const netsnmp_ssh_addr_pair *addr_pair = NULL;
95
96 if (data != NULL && len == sizeof(netsnmp_ssh_addr_pair)) {
97 addr_pair = (const netsnmp_ssh_addr_pair *) data;
98 } else if (t != NULL && t->data != NULL) {
99 addr_pair = (const netsnmp_ssh_addr_pair *) t->data;
100 }
101
102 if (addr_pair == NULL) {
103 return strdup("SSH: unknown");
104 } else {
105 const struct sockaddr_in *to;
106 char *tmp;
107
108 to = (const struct sockaddr_in *) &(addr_pair->remote_addr);
109 if (asprintf(&tmp, "SSH: [%s]:%hd",
110 inet_ntoa(to->sin_addr), ntohs(to->sin_port)) < 0)
111 tmp = NULL;
112 return tmp;
113 }
114 }
115
116 static void netsnmp_ssh_get_taddr(struct netsnmp_transport_s *t,
117 void **addr, size_t *addr_len)
118 {
119 switch (t->remote_length) {
120 case sizeof(struct sockaddr_in):
121 netsnmp_ipv4_get_taddr(t, addr, addr_len);
122 break;
123 default:
124 *addr = NULL;
125 netsnmp_assert(0);
126 }
127 }
128
129 /*
130 * You can write something into opaque that will subsequently get passed back
131 * to your send function if you like. For instance, you might want to
132 * remember where a PDU came from, so that you can send a reply there...
133 */
134
135 static int
136 netsnmp_ssh_recv(netsnmp_transport *t, void *buf, int size,
137 void **opaque, int *olength)
138 {
139 int rc = -1;
140 netsnmp_tmStateReference *tmStateRef = NULL;
141 netsnmp_ssh_addr_pair *addr_pair = NULL;
142 int iamclient = 0;
143
144 DEBUGMSGTL(("ssh", "at the top of ssh_recv\n"));
145 DEBUGMSGTL(("ssh", "t=%p\n", t));
146 if (t != NULL && t->data != NULL) {
147 addr_pair = (netsnmp_ssh_addr_pair *) t->data;
148 }
149
150 DEBUGMSGTL(("ssh", "addr_pair=%p\n", addr_pair));
151 if (t != NULL && addr_pair && addr_pair->channel) {
152 DEBUGMSGTL(("ssh", "t=%p, addr_pair=%p, channel=%p\n",
153 t, addr_pair, addr_pair->channel));
154 iamclient = 1;
155 while (rc < 0) {
156 rc = libssh2_channel_read(addr_pair->channel, buf, size);
157 if (rc < 0) { /* XXX: from tcp; ssh equiv?: && errno != EINTR */
158 DEBUGMSGTL(("ssh", "recv fd %d err %d (\"%s\")\n",
159 t->sock, errno, strerror(errno)));
160 break;
161 }
162 DEBUGMSGTL(("ssh", "recv fd %d got %d bytes\n",
163 t->sock, rc));
164 }
165 } else if (t != NULL) {
166
167 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
168
169 socklen_t tolen = sizeof(struct sockaddr_un);
170
171 if (t != NULL && t->sock >= 0) {
172 struct sockaddr *to;
173 to = (struct sockaddr *) SNMP_MALLOC_STRUCT(sockaddr_un);
174 if (NULL == to) {
175 *opaque = NULL;
176 *olength = 0;
177 return -1;
178 }
179
180 if(getsockname(t->sock, to, &tolen) != 0){
181 free(to);
182 *opaque = NULL;
183 *olength = 0;
184 return -1;
185 };
186
187 if (addr_pair && addr_pair->username[0] == '\0') {
188 /* we don't have a username yet, so this is the first message */
189 struct msghdr msg;
190 struct iovec iov[1];
191 char cmsg[2 * 4096];
192 struct cmsghdr *cmsgptr;
193 u_char *charbuf = buf;
194
195 iov[0].iov_base = buf;
196 iov[0].iov_len = size;
197
198 memset(&msg, 0, sizeof msg);
199 msg.msg_iov = iov;
200 msg.msg_iovlen = 1;
201 msg.msg_control = &cmsg;
202 msg.msg_controllen = sizeof(cmsg);
203
204 rc = recvmsg(t->sock, &msg, MSG_DONTWAIT); /* use DONTWAIT? */
205 if (rc <= 0) {
206 return rc;
207 }
208
209 /* we haven't received the starting info */
210 if ((u_char) charbuf[0] > NETSNMP_SSHTOSNMP_VERSION1) {
211 /* unsupported connection version */
212 snmp_log(LOG_ERR, "received unsupported sshtosnmp version: %d\n", charbuf[0]);
213 return -1;
214 }
215
216 DEBUGMSGTL(("ssh", "received first msg over SSH; internal SSH protocol version %d\n", charbuf[0]));
217
218 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
219 #if defined(SCM_CREDENTIALS)
220 if (cmsgptr->cmsg_level == SOL_SOCKET && cmsgptr->cmsg_type == SCM_CREDENTIALS) {
221 /* received credential info */
222 struct ucred *remoteuser;
223 struct passwd *user_pw;
224
225 remoteuser = (struct ucred *) CMSG_DATA(cmsgptr);
226
227 if ((user_pw = getpwuid(remoteuser->uid)) == NULL) {
228 snmp_log(LOG_ERR, "No user found for uid %d\n",
229 remoteuser->uid);
230 return -1;
231 }
232 if (strlen(user_pw->pw_name) >
233 sizeof(addr_pair->username)-1) {
234 snmp_log(LOG_ERR,
235 "User name '%s' too long for snmp\n",
236 user_pw->pw_name);
237 return -1;
238 }
239 strlcpy(addr_pair->username, user_pw->pw_name,
240 sizeof(addr_pair->username));
241 }
242 #elif defined(SCM_CREDS)
243 if (cmsgptr->cmsg_level == SOL_SOCKET && cmsgptr->cmsg_type == SCM_CREDS) {
244 /* received credential info */
245 struct cmsgcred *remoteuser;
246 struct passwd *user_pw;
247
248 remoteuser = (void *)CMSG_DATA(cmsgptr);
249
250 if ((user_pw = getpwuid(remoteuser->cmcred_uid)) == NULL) {
251 snmp_log(LOG_ERR, "No user found for uid %d\n",
252 remoteuser->cmcred_uid);
253 return -1;
254 }
255 if (strlen(user_pw->pw_name) >
256 sizeof(addr_pair->username)-1) {
257 snmp_log(LOG_ERR,
258 "User name '%s' too long for snmp\n",
259 user_pw->pw_name);
260 return -1;
261 }
262 strlcpy(addr_pair->username, user_pw->pw_name,
263 sizeof(addr_pair->username));
264 }
265 #endif
266 DEBUGMSGTL(("ssh", "Setting user name to %s\n",
267 addr_pair->username));
268 }
269
270 if (addr_pair->username[0] == '\0') {
271 snmp_log(LOG_ERR,
272 "failed to extract username from sshd connected unix socket\n");
273 return -1;
274 }
275
276 if (rc == 1) {
277 /* the only packet we received was the starting one */
278 t->flags |= NETSNMP_TRANSPORT_FLAG_EMPTY_PKT;
279 return 0;
280 }
281
282 rc -= 1;
283 memmove(charbuf, &charbuf[1], rc);
284 } else {
285 while (rc < 0) {
286 rc = recvfrom(t->sock, buf, size, 0, NULL, NULL);
287 if (rc < 0 && errno != EINTR) {
288 DEBUGMSGTL(("ssh", "recv fd %d err %d (\"%s\")\n",
289 t->sock, errno, strerror(errno)));
290 return rc;
291 }
292 *opaque = (void*)to;
293 *olength = sizeof(struct sockaddr_un);
294 }
295 }
296 DEBUGMSGTL(("ssh", "recv fd %d got %d bytes\n",
297 t->sock, rc));
298 }
299
300 #else /* we're called directly by sshd and use stdin/out */
301
302 struct passwd *user_pw;
303
304 iamclient = 0;
305 /* we're on the server side and should read from stdin */
306 while (rc < 0) {
307 rc = read(STDIN_FILENO, buf, size);
308 if (rc < 0 && errno != EINTR) {
309 DEBUGMSGTL(("ssh",
310 " read on stdin failed: %d (\"%s\")\n",
311 errno, strerror(errno)));
312 break;
313 }
314 if (rc == 0) {
315 /* 0 input is probably bad since we selected on it */
316 DEBUGMSGTL(("ssh", "got a 0 read on stdin\n"));
317 return -1;
318 }
319 DEBUGMSGTL(("ssh", "read on stdin got %d bytes\n", rc));
320 }
321
322 /* XXX: need to check the username, but addr_pair doesn't exist! */
323 /*
324 DEBUGMSGTL(("ssh", "current username=%s\n", c));
325 if (addr_pair->username[0] == '\0') {
326 if ((user_pw = getpwuid(getuid())) == NULL) {
327 snmp_log(LOG_ERR, "No user found for uid %d\n", getuid());
328 return -1;
329 }
330 if (strlen(user_pw->pw_name) > sizeof(addr_pair->username)-1) {
331 snmp_log(LOG_ERR, "User name '%s' too long for snmp\n",
332 user_pw->pw_name);
333 return -1;
334 }
335 strlcpy(addr_pair->username, user_pw->pw_name,
336 sizeof(addr_pair->username));
337 }
338 */
339
340 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
341 }
342
343 /* create a tmStateRef cache */
344 tmStateRef = SNMP_MALLOC_TYPEDEF(netsnmp_tmStateReference);
345
346 /* secshell document says were always authpriv, even if NULL algorithms */
347 /* ugh! */
348 /* XXX: disallow NULL in our implementations */
349 tmStateRef->transportSecurityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
350
351 /* XXX: figure out how to get the specified local secname from the session */
352 if (iamclient && 0) {
353 /* XXX: we're on the client; we should have named the
354 connection ourselves... pull this from session somehow? */
355 strlcpy(tmStateRef->securityName, addr_pair->username,
356 sizeof(tmStateRef->securityName));
357 } else {
358 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
359 strlcpy(tmStateRef->securityName, addr_pair->username,
360 sizeof(tmStateRef->securityName));
361 #else /* we're called directly by sshd and use stdin/out */
362 /* we're on the server... */
363 /* XXX: this doesn't copy properly and can get pointer
364 reference issues */
365 if (strlen(getenv("USER")) > 127) {
366 /* ruh roh */
367 /* XXX: clean up */
368 return -1;
369 exit;
370 }
371
372 /* XXX: detect and throw out overflow secname sizes rather
373 than truncating. */
374 strlcpy(tmStateRef->securityName, getenv("USER"),
375 sizeof(tmStateRef->securityName));
376 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
377 }
378 tmStateRef->securityName[sizeof(tmStateRef->securityName)-1] = '\0';
379 tmStateRef->securityNameLen = strlen(tmStateRef->securityName);
380 *opaque = tmStateRef;
381 *olength = sizeof(netsnmp_tmStateReference);
382
383 return rc;
384 }
385
386
387
388 static int
389 netsnmp_ssh_send(netsnmp_transport *t, const void *buf, int size,
390 void **opaque, int *olength)
391 {
392 int rc = -1;
393
394 netsnmp_ssh_addr_pair *addr_pair = NULL;
395 const netsnmp_tmStateReference *tmStateRef = NULL;
396
397 if (t != NULL && t->data != NULL) {
398 addr_pair = (netsnmp_ssh_addr_pair *) t->data;
399 }
400
401 if (opaque != NULL && *opaque != NULL &&
402 *olength == sizeof(netsnmp_tmStateReference)) {
403 tmStateRef = (const netsnmp_tmStateReference *) *opaque;
404 }
405
406 if (!tmStateRef) {
407 /* this is now an error according to my memory in the recent draft */
408 snmp_log(LOG_ERR, "netsnmp_ssh_send wasn't passed a valid tmStateReference\n");
409 return -1;
410 }
411
412 if (NULL != t && NULL != addr_pair && NULL != addr_pair->channel) {
413 if (addr_pair->username[0] == '\0') {
414 strlcpy(addr_pair->username, tmStateRef->securityName,
415 sizeof(addr_pair->username));
416 } else if (strcmp(addr_pair->username, tmStateRef->securityName) != 0 ||
417 strlen(addr_pair->username) != tmStateRef->securityNameLen) {
418 /* error! they must always match */
419 snmp_log(LOG_ERR, "netsnmp_ssh_send was passed a tmStateReference with a securityName not equal to previous messages\n");
420 return -1;
421 }
422 while (rc < 0) {
423 rc = libssh2_channel_write(addr_pair->channel, buf, size);
424 if (rc < 0) { /* XXX: && errno != EINTR */
425 break;
426 }
427 }
428 } else if (t != NULL) {
429 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
430
431 while (rc < 0) {
432 rc = sendto(t->sock, buf, size, 0, NULL, 0);
433
434 if (rc < 0 && errno != EINTR) {
435 break;
436 }
437 }
438
439 #else /* we're called directly by sshd and use stdin/out */
440 /* on the server; send to stdout */
441 while (rc < 0) {
442 rc = write(STDOUT_FILENO, buf, size);
443 fflush(stdout);
444 if (rc < 0 && errno != EINTR) { /* XXX: && errno != EINTR */
445 break;
446 }
447 }
448 #endif
449 }
450
451 return rc;
452 }
453
454
455
456 static int
457 netsnmp_ssh_close(netsnmp_transport *t)
458 {
459 int rc = -1;
460 netsnmp_ssh_addr_pair *addr_pair = NULL;
461
462 if (t != NULL && t->data != NULL) {
463 addr_pair = (netsnmp_ssh_addr_pair *) t->data;
464 }
465
466 if (t != NULL && addr_pair && t->sock >= 0) {
467 DEBUGMSGTL(("ssh", "close fd %d\n", t->sock));
468
469 if (addr_pair->channel) {
470 libssh2_channel_close(addr_pair->channel);
471 libssh2_channel_free(addr_pair->channel);
472 addr_pair->channel = NULL;
473 }
474
475 if (addr_pair->session) {
476 libssh2_session_disconnect(addr_pair->session, "Normal Shutdown");
477 libssh2_session_free(addr_pair->session);
478 addr_pair->session = NULL;
479 }
480
481 #ifndef HAVE_CLOSESOCKET
482 rc = close(t->sock);
483 #else
484 rc = closesocket(t->sock);
485 #endif
486 t->sock = -1;
487
488 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
489
490 if (!addr_pair->session && !addr_pair->channel) {
491 /* XXX: make configurable */
492 unlink(addr_pair->socket_path);
493 }
494
495 #else /* we're called directly by sshd and use stdin/out */
496
497 /* on the server: close stdin/out */
498 close(STDIN_FILENO);
499 close(STDOUT_FILENO);
500 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
501
502 } else {
503
504 #ifndef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
505 /* on the server: close stdin/out */
506 close(STDIN_FILENO);
507 close(STDOUT_FILENO);
508 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
509
510 }
511 return rc;
512 }
513
514
515
516 static int
517 netsnmp_ssh_accept(netsnmp_transport *t)
518 {
519 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
520
521 /* much of this is duplicated from snmpUnixDomain.c */
522
523 netsnmp_ssh_addr_pair *addr_pair;
524 int newsock = -1;
525 struct sockaddr *farend = NULL;
526 socklen_t farendlen = sizeof(struct sockaddr_un);
527
528
529 if (t != NULL && t->sock >= 0) {
530 addr_pair = SNMP_MALLOC_TYPEDEF(netsnmp_ssh_addr_pair);
531
532 if (addr_pair == NULL) {
533 /*
534 * Indicate that the acceptance of this socket failed.
535 */
536 DEBUGMSGTL(("ssh", "accept: malloc failed\n"));
537 return -1;
538 }
539
540 farend = (struct sockaddr *) &addr_pair->unix_socket_end;
541
542 newsock = accept(t->sock, farend, &farendlen);
543
544 if (newsock < 0) {
545 DEBUGMSGTL(("ssh","accept failed rc %d errno %d \"%s\"\n",
546 newsock, errno, strerror(errno)));
547 free(addr_pair);
548 return newsock;
549 }
550
551 #ifdef SO_PASSCRED
552 /* set the SO_PASSCRED option so we can receive the remote uid */
553 {
554 int one = 1;
555 setsockopt(newsock, SOL_SOCKET, SO_PASSCRED, (void *) &one,
556 sizeof(one));
557 }
558 #elif defined(LOCAL_CREDS)
559 {
560 int one = 1;
561 setsockopt(newsock, SOL_SOCKET, LOCAL_CREDS, (void *) &one,
562 sizeof(one));
563 }
564 #endif
565
566 if (t->data != NULL) {
567 free(t->data);
568 }
569
570 DEBUGMSGTL(("ssh", "accept succeeded (farend %p len %d)\n",
571 farend, farendlen));
572 t->data = addr_pair;
573 t->data_length = sizeof(netsnmp_ssh_addr_pair);
574 netsnmp_sock_buffer_set(newsock, SO_SNDBUF, 1, 0);
575 netsnmp_sock_buffer_set(newsock, SO_RCVBUF, 1, 0);
576 return newsock;
577 } else {
578 return -1;
579 }
580
581 #else /* we're called directly by sshd and use stdin/out */
582 /* we don't need to do anything; server side uses stdin/out */
583 /* XXX: check that we're an ssh connection */
584
585 return STDIN_FILENO; /* return stdin */
586 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
587
588 }
589
590
591
592 /*
593 * Open a SSH-based transport for SNMP. Local is TRUE if addr is the local
594 * address to bind to (i.e. this is a server-type session); otherwise addr is
595 * the remote address to send things to.
596 */
597
598 netsnmp_transport *
599 netsnmp_ssh_transport(const struct sockaddr_in *addr, int local)
600 {
601 netsnmp_transport *t = NULL;
602 netsnmp_ssh_addr_pair *addr_pair = NULL;
603 int rc = 0;
604 int i, auth_pw = 0;
605 const char *fingerprint;
606 char *userauthlist;
607 struct sockaddr_un *unaddr;
608 const char *sockpath =
609 netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
610 NETSNMP_DS_LIB_SSHTOSNMP_SOCKET);
611 char tmpsockpath[MAXPATHLEN];
612
613 #ifdef NETSNMP_NO_LISTEN_SUPPORT
614 if (local)
615 return NULL;
616 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
617
618 if (addr == NULL || addr->sin_family != AF_INET) {
619 return NULL;
620 }
621
622 t = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
623 if (t == NULL) {
624 return NULL;
625 }
626
627 t->domain = netsnmp_snmpSSHDomain;
628 t->domain_length = netsnmp_snmpSSHDomain_len;
629 t->flags = NETSNMP_TRANSPORT_FLAG_STREAM | NETSNMP_TRANSPORT_FLAG_TUNNELED;
630
631 addr_pair = SNMP_MALLOC_TYPEDEF(netsnmp_ssh_addr_pair);
632 if (addr_pair == NULL) {
633 netsnmp_transport_free(t);
634 return NULL;
635 }
636 t->data = addr_pair;
637 t->data_length = sizeof(netsnmp_ssh_addr_pair);
638
639 if (local) {
640 #ifndef NETSNMP_NO_LISTEN_SUPPORT
641 #ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
642
643 /* XXX: set t->local and t->local_length */
644
645
646 t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
647
648 unaddr = &addr_pair->unix_socket_end;
649
650 /* open a unix domain socket */
651 /* XXX: get data from the transport def for it's location */
652 unaddr->sun_family = AF_UNIX;
653 if (NULL == sockpath) {
654 sprintf(tmpsockpath, "%s/%s", get_persistent_directory(),
655 DEFAULT_SOCK_NAME);
656 sockpath = tmpsockpath;
657 }
658
659 snprintf(unaddr->sun_path, sizeof(unaddr->sun_path), "%s", sockpath);
660 snprintf(addr_pair->socket_path, sizeof(addr_pair->socket_path), "%s",
661 sockpath);
662
663 t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
664 if (t->sock < 0) {
665 netsnmp_transport_free(t);
666 return NULL;
667 }
668
669 #if defined(SO_PASSCRED)
670 /* set the SO_PASSCRED option so we can receive the remote uid */
671 {
672 int one = 1;
673 setsockopt(t->sock, SOL_SOCKET, SO_PASSCRED, (void *) &one,
674 sizeof(one));
675 }
676 #elif defined(LOCAL_CREDS)
677 {
678 int one = 1;
679 setsockopt(t->sock, SOL_SOCKET, LOCAL_CREDS, (void *) &one,
680 sizeof(one));
681 }
682 #endif
683
684
685 unlink(unaddr->sun_path);
686 rc = bind(t->sock, (struct sockaddr *)unaddr, SUN_LEN(unaddr));
687 if (rc != 0) {
688 DEBUGMSGTL(("netsnmp_ssh_transport",
689 "couldn't bind \"%s\", errno %d (%s)\n",
690 unaddr->sun_path, errno, strerror(errno)));
691 netsnmp_ssh_close(t);
692 netsnmp_transport_free(t);
693 return NULL;
694 }
695
696
697 /* set the socket permissions */
698 {
699 /*
700 * Apply any settings to the ownership/permissions of the
701 * Sshdomain socket
702 */
703 int sshdomain_sock_perm =
704 netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
705 NETSNMP_DS_SSHDOMAIN_SOCK_PERM);
706 int sshdomain_sock_user =
707 netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
708 NETSNMP_DS_SSHDOMAIN_SOCK_USER);
709 int sshdomain_sock_group =
710 netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
711 NETSNMP_DS_SSHDOMAIN_SOCK_GROUP);
712
713 DEBUGMSGTL(("ssh", "here: %s, %d, %d, %d\n",
714 unaddr->sun_path,
715 sshdomain_sock_perm, sshdomain_sock_user,
716 sshdomain_sock_group));
717 if (sshdomain_sock_perm != 0) {
718 DEBUGMSGTL(("ssh", "Setting socket perms to %d\n",
719 sshdomain_sock_perm));
720 chmod(unaddr->sun_path, sshdomain_sock_perm);
721 }
722
723 if (sshdomain_sock_user || sshdomain_sock_group) {
724 /*
725 * If either of user or group haven't been set,
726 * then leave them unchanged.
727 */
728 if (sshdomain_sock_user == 0 )
729 sshdomain_sock_user = -1;
730 if (sshdomain_sock_group == 0 )
731 sshdomain_sock_group = -1;
732 DEBUGMSGTL(("ssh", "Setting socket user/group to %d/%d\n",
733 sshdomain_sock_user, sshdomain_sock_group));
734 if (chown(unaddr->sun_path,
735 sshdomain_sock_user, sshdomain_sock_group) < 0)
736 snmp_log_perror("SSH socket chown");
737 }
738 }
739
740 rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
741 if (rc != 0) {
742 DEBUGMSGTL(("netsnmp_ssh_transport",
743 "couldn't listen to \"%s\", errno %d (%s)\n",
744 unaddr->sun_path, errno, strerror(errno)));
745 netsnmp_ssh_close(t);
746 netsnmp_transport_free(t);
747 return NULL;
748 }
749
750
751 #else /* we're called directly by sshd and use stdin/out */
752 /* for ssh on the server side we've been launched so bind to
753 stdin/out */
754
755 /* nothing to do */
756
757 /* XXX: verify we're inside ssh */
758 t->sock = STDIN_FILENO;
759 #endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */
760 #else /* NETSNMP_NO_LISTEN_SUPPORT */
761 netsnmp_transport_free(t);
762 return NULL;
763 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
764 } else {
765 char *username;
766 char *keyfilepub;
767 char *keyfilepriv;
768
769 /* use the requested user name */
770 /* XXX: default to the current user name on the system like ssh does */
771 username = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
772 NETSNMP_DS_LIB_SSH_USERNAME);
773 if (!username || 0 == *username) {
774 snmp_log(LOG_ERR, "You must specify a ssh username to use. See the snmp.conf manual page\n");
775 netsnmp_transport_free(t);
776 return NULL;
777 }
778
779 /* use the requested public key file */
780 keyfilepub = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
781 NETSNMP_DS_LIB_SSH_PUBKEY);
782 if (!keyfilepub || 0 == *keyfilepub) {
783 /* XXX: default to ~/.ssh/id_rsa.pub */
784 snmp_log(LOG_ERR, "You must specify a ssh public key file to use. See the snmp.conf manual page\n");
785 netsnmp_transport_free(t);
786 return NULL;
787 }
788
789 /* use the requested private key file */
790 keyfilepriv = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
791 NETSNMP_DS_LIB_SSH_PRIVKEY);
792 if (!keyfilepriv || 0 == *keyfilepriv) {
793 /* XXX: default to keyfilepub without the .pub suffix */
794 snmp_log(LOG_ERR, "You must specify a ssh private key file to use. See the snmp.conf manual page\n");
795 netsnmp_transport_free(t);
796 return NULL;
797 }
798
799 /* xxx: need an ipv6 friendly one too (sigh) */
800
801 /* XXX: not ideal when structs don't actually match size wise */
802 memcpy(&(addr_pair->remote_addr), addr, sizeof(struct sockaddr_in));
803
804 t->sock = socket(PF_INET, SOCK_STREAM, 0);
805 if (t->sock < 0) {
806 netsnmp_transport_free(t);
807 return NULL;
808 }
809
810 t->remote_length = sizeof(*addr);
811 t->remote = netsnmp_memdup(addr, sizeof(*addr));
812 if (!t->remote) {
813 netsnmp_ssh_close(t);
814 netsnmp_transport_free(t);
815 return NULL;
816 }
817
818 /*
819 * This is a client-type session, so attempt to connect to the far
820 * end. We don't go non-blocking here because it's not obvious what
821 * you'd then do if you tried to do snmp_sends before the connection
822 * had completed. So this can block.
823 */
824
825 rc = connect(t->sock, addr, sizeof(struct sockaddr));
826
827 if (rc < 0) {
828 netsnmp_ssh_close(t);
829 netsnmp_transport_free(t);
830 return NULL;
831 }
832
833 /*
834 * Allow user to override the send and receive buffers. Default is
835 * to use os default. Don't worry too much about errors --
836 * just plough on regardless.
837 */
838 netsnmp_sock_buffer_set(t->sock, SO_SNDBUF, local, 0);
839 netsnmp_sock_buffer_set(t->sock, SO_RCVBUF, local, 0);
840
841 /* open the SSH session and channel */
842 addr_pair->session = libssh2_session_init();
843 #ifdef HAVE_LIBSSH2_SESSION_HANDSHAKE
844 if (libssh2_session_handshake(addr_pair->session, t->sock)) {
845 #else
846 if (libssh2_session_startup(addr_pair->session, t->sock)) {
847 #endif
848 shutdown:
849 snmp_log(LOG_ERR, "Failed to establish an SSH session\n");
850 netsnmp_ssh_close(t);
851 netsnmp_transport_free(t);
852 return NULL;
853 }
854
855 /* At this point we havn't authenticated, The first thing to
856 do is check the hostkey's fingerprint against our known
857 hosts Your app may have it hard coded, may go to a file,
858 may present it to the user, that's your call
859 */
860 fingerprint =
861 libssh2_hostkey_hash(addr_pair->session, LIBSSH2_HOSTKEY_HASH_MD5);
862 DEBUGMSGTL(("ssh", "Fingerprint: "));
863 for(i = 0; i < 16; i++) {
864 DEBUGMSG(("ssh", "%02x",
865 (unsigned char)fingerprint[i]));
866 }
867 DEBUGMSG(("ssh", "\n"));
868
869 /* check what authentication methods are available */
870 userauthlist =
871 libssh2_userauth_list(addr_pair->session,
872 username, strlen(username));
873 DEBUGMSG(("ssh", "Authentication methods: %s\n", userauthlist));
874
875 /* XXX: allow other types */
876 /* XXX: 4 seems magic to me... */
877 if (strstr(userauthlist, "publickey") != NULL) {
878 auth_pw |= 4;
879 }
880
881 /* XXX: hard coded paths and users */
882 if (auth_pw & 4) {
883 /* public key */
884 if (libssh2_userauth_publickey_fromfile(addr_pair->session,
885 username,
886 keyfilepub, keyfilepriv,
887 NULL)) {
888 snmp_log(LOG_ERR,"Authentication by public key failed!\n");
889 goto shutdown;
890 } else {
891 DEBUGMSG(("ssh",
892 "\tAuthentication by public key succeeded.\n"));
893 }
894 } else {
895 snmp_log(LOG_ERR,"Authentication by public key failed!\n");
896 goto shutdown;
897 }
898
899 /* we've now authenticated both sides; contining onward ... */
900
901 /* Request a channel */
902 if (!(addr_pair->channel =
903 libssh2_channel_open_session(addr_pair->session))) {
904 snmp_log(LOG_ERR, "Unable to open a session\n");
905 goto shutdown;
906 }
907
908 /* Request a terminal with 'vanilla' terminal emulation
909 * See /etc/termcap for more options
910 */
911 /* XXX: needed? doubt it */
912 /* if (libssh2_channel_request_pty(addr_pair->channel, "vanilla")) { */
913 /* snmp_log(LOG_ERR, "Failed requesting pty\n"); */
914 /* goto shutdown; */
915 /* } */
916 if (libssh2_channel_subsystem(addr_pair->channel, "snmp")) {
917 snmp_log(LOG_ERR, "Failed to request the ssh 'snmp' subsystem\n");
918 goto shutdown;
919 }
920 }
921
922 DEBUGMSG(("ssh","Opened connection.\n"));
923 /*
924 * Message size is not limited by this transport (hence msgMaxSize
925 * is equal to the maximum legal size of an SNMP message).
926 */
927
928 t->msgMaxSize = SNMP_MAX_PACKET_LEN;
929 t->f_recv = netsnmp_ssh_recv;
930 t->f_send = netsnmp_ssh_send;
931 t->f_close = netsnmp_ssh_close;
932 t->f_accept = netsnmp_ssh_accept;
933 t->f_fmtaddr = netsnmp_ssh_fmtaddr;
934 t->f_get_taddr = netsnmp_ssh_get_taddr;
935
936 return t;
937 }
938
939
940
941 netsnmp_transport *
942 netsnmp_ssh_create_tstring(const char *str, int local,
943 const char *default_target)
944 {
945 struct sockaddr_in addr;
946
947 if (netsnmp_sockaddr_in2(&addr, str, default_target)) {
948 return netsnmp_ssh_transport(&addr, local);
949 } else {
950 return NULL;
951 }
952 }
953
954
955
956 netsnmp_transport *
957 netsnmp_ssh_create_ostring(const void *o, size_t o_len, int local)
958 {
959 struct sockaddr_in sin;
960
961 if (netsnmp_ipv4_ostring_to_sockaddr(&sin, o, o_len))
962 return netsnmp_ssh_transport(&sin, local);
963 else
964 netsnmp_assert(0);
965 return NULL;
966 }
967
968 void
969 sshdomain_parse_socket(const char *token, char *cptr)
970 {
971 char *socket_perm, *socket_user, *socket_group;
972 int uid = -1;
973 int gid = -1;
974 int s_perm = -1;
975 char *st;
976
977 DEBUGMSGTL(("ssh/config", "parsing socket info: %s\n", cptr));
978 socket_perm = strtok_r(cptr, " \t", &st);
979 socket_user = strtok_r(NULL, " \t", &st);
980 socket_group = strtok_r(NULL, " \t", &st);
981
982 if (socket_perm) {
983 s_perm = strtol(socket_perm, NULL, 8);
984 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
985 NETSNMP_DS_SSHDOMAIN_SOCK_PERM, s_perm);
986 DEBUGMSGTL(("ssh/config", "socket permissions: %o (%d)\n",
987 s_perm, s_perm));
988 }
989 /*
990 * Try to handle numeric UIDs or user names for the socket owner
991 */
992 if (socket_user) {
993 uid = netsnmp_str_to_uid(socket_user);
994 if ( uid != 0 )
995 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
996 NETSNMP_DS_SSHDOMAIN_SOCK_USER, uid);
997 DEBUGMSGTL(("ssh/config", "socket owner: %s (%d)\n",
998 socket_user, uid));
999 }
1000
1001 /*
1002 * and similarly for the socket group ownership
1003 */
1004 if (socket_group) {
1005 gid = netsnmp_str_to_gid(socket_group);
1006 if ( gid != 0 )
1007 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
1008 NETSNMP_DS_SSHDOMAIN_SOCK_GROUP, gid);
1009 DEBUGMSGTL(("ssh/config", "socket group: %s (%d)\n",
1010 socket_group, gid));
1011 }
1012 }
1013
1014 void
1015 netsnmp_ssh_ctor(void)
1016 {
1017 sshDomain.name = netsnmp_snmpSSHDomain;
1018 sshDomain.name_length = netsnmp_snmpSSHDomain_len;
1019 sshDomain.prefix = (const char **)calloc(2, sizeof(char *));
1020 sshDomain.prefix[0] = "ssh";
1021
1022 sshDomain.f_create_from_tstring_new = netsnmp_ssh_create_tstring;
1023 sshDomain.f_create_from_ostring = netsnmp_ssh_create_ostring;
1024
1025 register_config_handler("snmp", "sshtosnmpsocketperms",
1026 &sshdomain_parse_socket, NULL,
1027 "socketperms [username [groupname]]");
1028
1029 netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "sshtosnmpsocket",
1030 NETSNMP_DS_LIBRARY_ID,
1031 NETSNMP_DS_LIB_SSHTOSNMP_SOCKET);
1032
1033 netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "sshusername",
1034 NETSNMP_DS_LIBRARY_ID,
1035 NETSNMP_DS_LIB_SSH_USERNAME);
1036
1037 netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "sshpublickey",
1038 NETSNMP_DS_LIBRARY_ID,
1039 NETSNMP_DS_LIB_SSH_PUBKEY);
1040
1041 netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "sshprivatekey",
1042 NETSNMP_DS_LIBRARY_ID,
1043 NETSNMP_DS_LIB_SSH_PRIVKEY);
1044
1045 DEBUGMSGTL(("ssh", "registering the ssh domain\n"));
1046 netsnmp_tdomain_register(&sshDomain);
1047 }
1048
1049