source: trunk/src/sh_portcheck.c@ 132

Last change on this file since 132 was 132, checked in by rainer, 18 years ago

Make utility functions thread-safe.

  • Property svn:executable set to *
File size: 35.3 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2006 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/***************************************************************************
21 *
22 * This file provides a module for samhain to check for open ports
23 * on the local machine.
24 *
25 */
26
27
28/* #define TEST_ONLY */
29#ifndef TEST_ONLY
30#include "config_xor.h"
31#endif
32
33#include <stdio.h>
34#include <string.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <errno.h>
40#include <unistd.h>
41#include <fcntl.h>
42
43#define PORTCHK_VERSION "1.0"
44
45#if defined(TEST_ONLY) || (defined(SH_USE_PORTCHECK) && (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)))
46
47
48#define PORTMAP
49#include <rpc/rpc.h>
50#ifdef HAVE_RPC_RPCENT_H
51#include <rpc/rpcent.h>
52#endif
53#include <rpc/pmap_clnt.h>
54#include <rpc/pmap_prot.h>
55#include <netdb.h>
56
57/*
58 * struct pmaplist {
59 * struct pmap pml_map;
60 * struct pmaplist *pml_next;
61 * };
62 */
63
64/* struct pmap {
65 * long unsigned pm_prog;
66 * long unsigned pm_vers;
67 * long unsigned pm_prot;
68 * long unsigned pm_port;
69 * };
70 */
71
72/* TIME_WAIT ? 60-240 seconds */
73
74/* the size of an interface string
75 */
76#define SH_INTERFACE_SIZE 16
77
78#define SH_PORT_NOT 0
79#define SH_PORT_REQ 1
80#define SH_PORT_OPT 2
81#define SH_PORT_IGN 3
82
83#define SH_PORT_MISS 0
84#define SH_PORT_ISOK 1
85#define SH_PORT_UNKN 2
86
87#define SH_PORT_NOREPT 0
88#define SH_PORT_REPORT 1
89
90struct sh_portentry {
91 int port;
92 char interface[SH_INTERFACE_SIZE];
93 char * service;
94 char * error;
95 int flag; /* required or not */
96 int status; /* missing or not */
97 struct sh_portentry * next;
98};
99
100static struct sh_portentry * portlist_tcp = NULL;
101static struct sh_portentry * portlist_udp = NULL;
102
103#define SH_PORTCHK_INTERVAL 300
104
105static int sh_portchk_check_udp = 1;
106static int sh_portchk_active = 1;
107static int sh_portchk_interval = SH_PORTCHK_INTERVAL;
108#if !defined(TEST_ONLY)
109
110#define FIL__ _("sh_portcheck.c")
111#include "samhain.h"
112#include "sh_error.h"
113#include "sh_mem.h"
114#include "sh_calls.h"
115#include "sh_utils.h"
116#include "sh_modules.h"
117
118static int sh_portchk_severity = SH_ERR_SEVERE;
119#endif
120
121/* Exported interface to add ignoreable ports as 'iface:portlist'
122 */
123static int sh_portchk_add_ignore (const char * str);
124
125/* Exported interface to add required ports as 'iface:portlist'
126 */
127static int sh_portchk_add_required (const char * str);
128
129/* Exported interface to add optional ports as 'iface:portlist'
130 */
131static int sh_portchk_add_optional (const char * str);
132
133/* Exported interface to add an ethernet interface
134 */
135static int sh_portchk_add_interface (const char * str);
136
137
138#ifndef TEST_ONLY
139
140static int sh_portchk_set_interval (const char * c)
141{
142 int retval = 0;
143 long val;
144
145 SL_ENTER(_("sh_portchk_set_interval"));
146 val = strtol (c, (char **)NULL, 10);
147 if (val <= 0)
148 {
149 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
150 _("port check interval"), c);
151 retval = -1;
152 }
153
154 val = (val <= 0 ? 60 : val);
155
156 sh_portchk_interval = (time_t) val;
157 SL_RETURN(0, _("sh_portchk_set_interval"));
158}
159
160
161static int sh_portchk_set_active (const char * str)
162{
163 return sh_util_flagval(str, &sh_portchk_active);
164}
165
166static int sh_portchk_set_udp (const char * str)
167{
168 return sh_util_flagval(str, &sh_portchk_check_udp);
169}
170
171static int sh_portchk_set_severity (const char * str)
172{
173 char tmp[32];
174 tmp[0] = '='; tmp[1] = '\0';
175 sl_strlcat (tmp, str, 32);
176 return sh_error_set_level (tmp, &sh_portchk_severity);
177}
178
179sh_rconf sh_portchk_table[] = {
180 {
181 N_("severityportcheck"),
182 sh_portchk_set_severity,
183 },
184 {
185 N_("portcheckrequired"),
186 sh_portchk_add_required,
187 },
188 {
189 N_("portcheckoptional"),
190 sh_portchk_add_optional,
191 },
192 {
193 N_("portcheckignore"),
194 sh_portchk_add_ignore,
195 },
196 {
197 N_("portcheckactive"),
198 sh_portchk_set_active,
199 },
200 {
201 N_("portcheckinterface"),
202 sh_portchk_add_interface,
203 },
204 {
205 N_("portcheckinterval"),
206 sh_portchk_set_interval,
207 },
208 {
209 N_("portcheckudp"),
210 sh_portchk_set_udp,
211 },
212 {
213 NULL,
214 NULL
215 }
216};
217
218#endif
219
220/* Interface to initialize port check
221 */
222int sh_portchk_init ();
223
224/* Interface to reset port check
225 */
226int sh_portchk_reset ();
227
228/* Interface to run port check
229 */
230int sh_portchk_check ();
231
232
233static char * check_services (int port, char * proto);
234
235#ifdef TEST_ONLY
236
237static int portchk_debug = 0;
238#define SH_ALLOC malloc
239#define SH_FREE free
240#define sh_util_strdup strdup
241#define sl_strlcpy strncpy
242#define _(a) a
243
244#else
245
246static int portchk_debug = 0;
247
248#endif
249
250static void sh_portchk_add_to_list (char * proto,
251 int port, struct in_addr haddr, char * service,
252 int flag, int status)
253{
254 struct sh_portentry * new = SH_ALLOC (sizeof(struct sh_portentry));
255
256 if (portchk_debug)
257 fprintf(stderr, _("add to list: port %d/%s %d %d (%s)\n"),
258 port, proto, flag, status, service ? service : _("undef"));
259
260 new->port = port;
261 sl_strlcpy (new->interface, inet_ntoa(haddr), SH_INTERFACE_SIZE);
262 new->status = status;
263 new->flag = flag;
264
265 new->error = NULL;
266
267 if (service)
268 new->service = sh_util_strdup (service);
269 else
270 new->service = NULL;
271 if (0 == strcmp(proto, "tcp"))
272 {
273 new->next = portlist_tcp;
274 portlist_tcp = new;
275 }
276 else
277 {
278 new->next = portlist_udp;
279 portlist_udp = new;
280 }
281 return;
282}
283
284/* Reset the list by setting all entries to UNKN.
285 * In the next cycle we will check, and set found ports to ISOK.
286 * Thereafter, we check for entries that are still UNKN.
287 */
288static void sh_portchk_reset_lists ()
289{
290 struct sh_portentry * portlist;
291
292 portlist = portlist_tcp;
293 while (portlist)
294 {
295 if (portlist->status != SH_PORT_MISS)
296 portlist->status = SH_PORT_UNKN;
297 portlist = portlist->next;
298 }
299 portlist = portlist_udp;
300 while (portlist)
301 {
302 if (portlist->status != SH_PORT_MISS)
303 portlist->status = SH_PORT_UNKN;
304 portlist = portlist->next;
305 }
306 return;
307}
308
309static struct sh_portentry * sh_portchk_kill_list (struct sh_portentry * head)
310{
311 if (head)
312 {
313 if (head->next)
314 sh_portchk_kill_list (head->next);
315
316 if (head->service)
317 SH_FREE(head->service);
318 SH_FREE(head);
319 }
320 return NULL;
321}
322
323/* check the list of open ports for any that are marked as UNKN
324 */
325static void sh_portchk_check_list (struct sh_portentry ** head, char * proto, int report)
326{
327 struct sh_portentry * ptr = *head;
328 struct sh_portentry * pre = *head;
329 char errbuf[256];
330
331 while (ptr)
332 {
333 if (portchk_debug && report)
334 fprintf(stderr, _("check list: port %d/%s %d %d\n"),
335 ptr->port, proto, ptr->flag, ptr->status);
336
337 if (ptr->status == SH_PORT_UNKN)
338 {
339 /* Don't report missing ports that are marked as optional
340 */
341 if (ptr->flag != SH_PORT_OPT && ptr->flag != SH_PORT_IGN)
342 {
343 snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceMissing] port %s:%d/%s (%s)"),
344 ptr->interface, ptr->port, proto,
345 ptr->service ? ptr->service : check_services(ptr->port, proto));
346#ifdef TEST_ONLY
347 if (report == SH_PORT_REPORT)
348 fprintf(stderr, _("%s\n"), errbuf);
349#else
350 if (report == SH_PORT_REPORT)
351 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
352 MSG_PORT_REPORT, errbuf);
353#endif
354 }
355
356 ptr->status = SH_PORT_MISS;
357
358 if ((ptr->flag != SH_PORT_REQ) && (ptr->flag != SH_PORT_OPT) && (ptr->flag != SH_PORT_IGN))
359 {
360 if (portchk_debug && report)
361 fprintf(stderr, _("removing: port %d/%s %d %d\n"),
362 ptr->port, proto, ptr->flag, ptr->status);
363
364 if (ptr == *head)
365 {
366 *head = ptr->next;
367 if (ptr->service)
368 SH_FREE(ptr->service);
369 SH_FREE(ptr);
370 ptr = *head;
371 pre = *head;
372 continue;
373 }
374 else if (ptr->next == NULL)
375 {
376 pre->next = NULL;
377 if (ptr->service)
378 SH_FREE(ptr->service);
379 SH_FREE(ptr);
380 return;
381 }
382 else
383 {
384 pre->next = ptr->next;
385 if (ptr->service)
386 SH_FREE(ptr->service);
387 SH_FREE(ptr);
388 ptr = pre->next;
389 continue;
390 }
391 }
392 }
393 pre = ptr;
394 ptr = ptr->next;
395 }
396 return;
397}
398
399
400static struct sh_portentry * sh_portchk_get_from_list (char * proto, int port,
401 struct in_addr haddr, char * service)
402{
403 struct sh_portentry * portlist;
404 char iface_all[8];
405
406 sl_strlcpy (iface_all, _("0.0.0.0"), sizeof(iface_all));
407
408 if (0 == strcmp(proto, "tcp"))
409 portlist = portlist_tcp;
410 else
411 portlist = portlist_udp;
412
413 if (service)
414 {
415 while (portlist)
416 {
417 if (portlist->service &&
418 0 == strcmp(service, portlist->service) &&
419 (0 == strcmp(portlist->interface, inet_ntoa(haddr)) ||
420 0 == strcmp(portlist->interface, iface_all)))
421 return portlist;
422 portlist = portlist->next;
423 }
424 }
425 else
426 {
427 while (portlist)
428 {
429 if (port == portlist->port &&
430 (0 == strcmp(portlist->interface, inet_ntoa(haddr)) ||
431 0 == strcmp(portlist->interface, iface_all)))
432 return portlist;
433 portlist = portlist->next;
434 }
435 }
436 return NULL;
437}
438
439
440static void sh_portchk_cmp_to_list (char * proto, int port, struct in_addr haddr, char * service)
441{
442 struct sh_portentry * portent;
443 char errbuf[256];
444
445
446 portent = sh_portchk_get_from_list (proto, port, haddr, service);
447
448 if (service)
449 {
450 if (!portent)
451 {
452 snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceNew] port %s:%d/%s (%s)"),
453 inet_ntoa(haddr), port, proto, service);
454#ifdef TEST_ONLY
455 fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
456 inet_ntoa(haddr), port, proto, service);
457#else
458 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
459 MSG_PORT_REPORT, errbuf);
460#endif
461 /*
462 * was not there, thus it is not in 'required' or 'optional' list
463 */
464 sh_portchk_add_to_list (proto, port, haddr, service, SH_PORT_NOT, SH_PORT_ISOK);
465 }
466 else if (portent->status == SH_PORT_MISS && portent->flag != SH_PORT_IGN)
467 {
468 snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceRestarted] port %s:%d/%s to %d/%s (%s)"),
469 inet_ntoa(haddr), portent->port, proto, port, proto, service);
470#ifdef TEST_ONLY
471 fprintf(stderr, _("service: %s\n"), errbuf);
472#else
473 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
474 MSG_PORT_REPORT, errbuf);
475#endif
476
477 portent->status = SH_PORT_ISOK;
478 }
479 else if (port != portent->port && (-1) != portent->port)
480 {
481 snprintf (errbuf, sizeof(errbuf), _("POLICY [ServicePortSwitch] port %s:%d/%s to %d/%s (%s)"),
482 inet_ntoa(haddr), portent->port, proto, port, proto, service);
483#ifdef TEST_ONLY
484 fprintf(stderr, _("service: %s\n"), errbuf);
485#else
486 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
487 MSG_PORT_REPORT, errbuf);
488#endif
489 portent->port = port;
490 portent->status = SH_PORT_ISOK;
491 }
492 else
493 {
494 portent->status = SH_PORT_ISOK;
495 }
496 }
497 else
498 {
499 if (!portent)
500 {
501 snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceNew] port %s:%d/%s (%s)"),
502 inet_ntoa(haddr), port, proto, check_services(port, proto));
503#ifdef TEST_ONLY
504 fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
505 inet_ntoa(haddr), port, proto, check_services(port, proto));
506#else
507 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
508 MSG_PORT_REPORT, errbuf);
509#endif
510
511 /* was not there, thus it is not in 'required' or 'optional' list
512 */
513 sh_portchk_add_to_list (proto, port, haddr, service, SH_PORT_NOT, SH_PORT_ISOK);
514 }
515 else if (portent->status == SH_PORT_MISS && portent->flag != SH_PORT_IGN)
516 {
517 snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceRestarted] port %s:%d/%s (%s)"),
518 inet_ntoa(haddr), port, proto, check_services(port, proto));
519#ifdef TEST_ONLY
520 fprintf(stderr, _("port : %s\n"), errbuf);
521#else
522 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
523 MSG_PORT_REPORT, errbuf);
524#endif
525
526 portent->status = SH_PORT_ISOK;
527 }
528 else
529 {
530 portent->status = SH_PORT_ISOK;
531 }
532 }
533
534 return;
535}
536
537
538/* Returns a static buffer containing the name of the service
539 * running on port <port> (from /etc/services)
540 * Returns NULL on failure
541 */
542static char * check_services (int port, char * proto)
543{
544 static char buf[256];
545 struct servent * service = getservbyport(htons(port), proto);
546
547 if (service && service->s_name && service->s_name[0] != '\0')
548 {
549 snprintf (buf, sizeof(buf), _("maybe_%s"), service->s_name);
550 }
551 else
552 {
553 snprintf (buf, sizeof(buf), _("unknown"));
554 }
555 return buf;
556}
557
558/* Returns a static buffer containing the name of the service
559 * running on port <port> at <address> (from portmap daemon)
560 * Returns NULL on failure
561 */
562static char * check_rpc_list (int port, struct sockaddr_in * address,
563 unsigned long prot)
564{
565 struct pmaplist * head;
566 struct rpcent *r;
567 static char buf[256];
568
569 head = pmap_getmaps(address);
570
571 if (head)
572 {
573 do /* while (head != NULL) */
574 {
575 if ((head->pml_map.pm_prot == prot) &&
576 (port == (int)head->pml_map.pm_port))
577 {
578 r = getrpcbynumber((int)head->pml_map.pm_prog);
579 if (r && r->r_name && r->r_name[0] != '\0')
580 {
581 snprintf (buf, sizeof(buf), "%s", r->r_name);
582 return buf;
583 }
584 else
585 {
586 snprintf (buf, sizeof(buf), "RPC_%lu",
587 (unsigned long)head->pml_map.pm_prog);
588 return buf;
589 }
590 }
591 head = head->pml_next;
592 }
593 while (head != NULL);
594 }
595
596 return NULL;
597}
598
599static int check_port_udp_internal (int fd, int port, struct in_addr haddr)
600{
601 struct sockaddr_in sinr;
602 /* struct in_addr haddr; */
603 int retval;
604 char * p;
605 char buf[8];
606#ifndef TEST_ONLY
607 char errmsg[256];
608 int nerr;
609#endif
610 char errbuf[SH_ERRBUF_SIZE];
611
612 /* inet_aton(interface, &haddr); */
613
614 sinr.sin_family = AF_INET;
615 sinr.sin_port = htons (port);
616 sinr.sin_addr = haddr;
617
618 do {
619 retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
620 } while (retval < 0 && errno == EINTR);
621
622 if (retval == -1)
623 {
624#ifdef TEST_ONLY
625 if (portchk_debug)
626 perror(_("connect"));
627#else
628 nerr = errno;
629 sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/udp on %15s: %s"),
630 port, inet_ntoa(haddr), sh_error_message(errno, errbuf, sizeof(errbuf)));
631 sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
632 errmsg, _("connect"));
633#endif
634 }
635 else
636 {
637 do {
638 retval = send (fd, buf, 0, 0);
639 } while (retval < 0 && errno == EINTR);
640
641 if (retval == -1 && errno == ECONNREFUSED)
642 {
643 if (portchk_debug)
644 fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
645 port, inet_ntoa(haddr));
646 }
647 else
648 {
649 /* Only the second send() may catch the error
650 */
651 do {
652 retval = send (fd, buf, 0, 0);
653 } while (retval < 0 && errno == EINTR);
654
655 if (retval == -1 && errno == ECONNREFUSED)
656 {
657 if (portchk_debug)
658 fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
659 port, inet_ntoa(haddr));
660 }
661 else if (retval != -1)
662 {
663 /* Try to get service name from portmap
664 */
665 p = check_rpc_list (port, &sinr, IPPROTO_UDP);
666
667 sh_portchk_cmp_to_list ("udp", port, haddr, p ? p : NULL);
668
669 /* If not an RPC service, try to get name from /etc/services
670 */
671 if (!p)
672 p = check_services(port, "udp");
673
674 if (portchk_debug)
675 fprintf(stderr, _("check port: %5d/udp on %15s open %s\n"),
676 port, inet_ntoa(haddr), p);
677
678 }
679 }
680 }
681 close (fd);
682 return 0;
683}
684
685static int check_port_tcp_internal (int fd, int port, struct in_addr haddr)
686{
687 struct sockaddr_in sinr;
688 /* struct in_addr haddr; */
689 int retval;
690 int flags;
691 char * p;
692#ifndef TEST_ONLY
693 char errmsg[256];
694 int nerr;
695#endif
696 char errbuf[SH_ERRBUF_SIZE];
697
698 /* inet_aton(interface, &haddr); */
699
700 sinr.sin_family = AF_INET;
701 sinr.sin_port = htons (port);
702 sinr.sin_addr = haddr;
703
704 do {
705 retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
706 } while (retval < 0 && errno == EINTR);
707
708 if (retval == -1 && errno == ECONNREFUSED)
709 {
710 if (portchk_debug)
711 fprintf(stderr, _("check port: %5d on %15s established/time_wait\n"),
712 port, inet_ntoa(haddr));
713 }
714 else if (retval == -1)
715 {
716#ifdef TEST_ONLY
717 if (portchk_debug)
718 perror(_("connect"));
719#else
720 nerr = errno;
721 sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/tcp on %15s: %s"),
722 port, inet_ntoa(haddr), sh_error_message(errno, errbuf, sizeof(errbuf)));
723 sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
724 errmsg, _("connect"));
725#endif
726 }
727 else
728 {
729 /* Try to get service name from portmap
730 */
731 p = check_rpc_list (port, &sinr, IPPROTO_TCP);
732
733 sh_portchk_cmp_to_list ("tcp", port, haddr, p ? p : NULL);
734
735 /* If not an RPC service, try to get name from /etc/services
736 */
737 if (!p)
738 p = check_services(port, "tcp");
739
740 if (portchk_debug)
741 fprintf(stderr, _("check port: %5d on %15s open %s\n"),
742 port, inet_ntoa(haddr), p);
743
744#if !defined(O_NONBLOCK)
745#if defined(O_NDELAY)
746#define O_NONBLOCK O_NDELAY
747#else
748#define O_NONBLOCK 0
749#endif
750#endif
751
752 /* prepare to close connection gracefully
753 */
754 if (port == 22) /* ssh */
755 {
756 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
757 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
758 write (fd, _("SSH-2.0-Foobar"), 14);
759 write (fd, "\r\n", 2);
760 }
761 else if (port == 25) /* smtp */
762 {
763 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
764 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
765 write (fd, _("QUIT"), 4);
766 write (fd, "\r\n", 2);
767 }
768 else if (port == 79) /* finger */
769 {
770 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
771 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
772 write (fd, "\r\n", 2);
773 }
774 else if (port == 110) /* pop3 */
775 {
776 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
777 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
778 write (fd, _("QUIT"), 4);
779 write (fd, "\r\n", 2);
780 }
781 else if (port == 143) /* imap */
782 {
783 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
784 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
785 write (fd, _("A01 LOGOUT"), 10);
786 write (fd, "\r\n", 2);
787 }
788 }
789 close (fd);
790 return 0;
791}
792
793/* typedef uint32_t in_addr_t;
794 * struct in_addr
795 * {
796 * in_addr_t s_addr;
797 * };
798 */
799
800#define SH_IFACE_MAX 16
801
802struct portchk_interfaces {
803 struct in_addr iface[SH_IFACE_MAX];
804 int used;
805};
806
807static struct portchk_interfaces iface_list;
808static int iface_initialized = 0;
809
810#ifdef TEST_ONLY
811static char * portchk_hostname = NULL;
812#else
813static char * portchk_hostname = sh.host.name;
814#endif
815
816int sh_portchk_init ()
817{
818 struct hostent * hent;
819 int i = 0;
820 char errbuf[256];
821
822 if (portchk_debug)
823 fprintf(stderr, _("checking ports on: %s\n"), portchk_hostname ? portchk_hostname : _("NULL"));
824
825 if (!portchk_hostname)
826 return -1;
827
828 if (sh_portchk_active == S_FALSE)
829 return -1;
830
831 if (iface_initialized == 0)
832 {
833 iface_list.used = 0;
834 iface_initialized = 1;
835 }
836
837 hent = gethostbyname(portchk_hostname);
838
839 while (hent && hent->h_addr_list[i] && (iface_list.used < SH_IFACE_MAX))
840 {
841 memcpy (&(iface_list.iface[iface_list.used].s_addr), hent->h_addr_list[i], sizeof(in_addr_t));
842 sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"),
843 inet_ntoa(iface_list.iface[iface_list.used]));
844 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
845 errbuf, _("sh_portchk_init"));
846
847 ++iface_list.used;
848 ++i;
849 }
850
851 return 0;
852}
853
854#if !defined(TEST_ONLY)
855int sh_portchk_reconf ()
856{
857 iface_initialized = 0;
858
859 sh_portchk_active = 1;
860 sh_portchk_check_udp = 1;
861
862 portlist_udp = sh_portchk_kill_list (portlist_udp);
863 portlist_tcp = sh_portchk_kill_list (portlist_tcp);
864 return 0;
865}
866
867int sh_portchk_cleanup ()
868{
869 return sh_portchk_reconf ();
870}
871
872int sh_portchk_timer (time_t tcurrent)
873{
874 static time_t lastcheck = 0;
875
876 SL_ENTER(_("sh_portchk_timer"));
877 if ((time_t) (tcurrent - lastcheck) >= sh_portchk_interval)
878 {
879 lastcheck = tcurrent;
880 SL_RETURN((-1), _("sh_portchk_timer"));
881 }
882 SL_RETURN(0, _("sh_portchk_timer"));
883}
884#endif
885
886static int check_port_generic (int port, int type, int protocol)
887{
888 int i = 0;
889 int sock = -1;
890 int flag = 1; /* non-zero to enable an option */
891 struct in_addr haddr;
892 char errbuf[SH_ERRBUF_SIZE];
893
894 /* Check all interfaces for this host
895 */
896 while (i < iface_list.used)
897 {
898 if ((sock = socket(AF_INET, type, protocol)) < 0 )
899 {
900#ifdef TEST_ONLY
901 if (portchk_debug)
902 perror(_("socket"));
903#else
904 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
905 sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
906#endif
907 }
908 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
909 (void *) &flag, sizeof(flag)) < 0 )
910 {
911#ifdef TEST_ONLY
912 if (portchk_debug)
913 perror(_("setsockopt"));
914#else
915 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
916 sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
917#endif
918 }
919
920 memcpy (&(haddr.s_addr), &(iface_list.iface[i].s_addr), sizeof(in_addr_t));
921
922 if (protocol == IPPROTO_TCP)
923 check_port_tcp_internal(sock, port, haddr);
924 else
925 check_port_udp_internal(sock, port, haddr);
926
927 ++i;
928 }
929
930 return 0;
931}
932
933
934
935static int check_port_udp (int port)
936{
937 return check_port_generic(port, SOCK_DGRAM, IPPROTO_UDP);
938}
939
940static int check_port_tcp (int port)
941{
942 return check_port_generic(port, SOCK_STREAM, IPPROTO_TCP);
943}
944
945
946
947static int sh_portchk_scan_ports_generic (int min_port, int max_port, int type, int protocol)
948{
949 /*
950 int min_port = 1024;
951 int max_port = 65535;
952 */
953
954 int port;
955 int retval;
956 int sock = -1;
957 int flag = 1; /* non-zero to enable an option */
958
959 struct sockaddr_in addr;
960 int addrlen = sizeof(addr);
961 char errbuf[SH_ERRBUF_SIZE];
962
963 if (min_port == -1)
964 min_port = 0;
965 if (max_port == -1)
966 max_port = 65535;
967
968 for (port = min_port; port <= max_port; ++port)
969 {
970
971 if ((sock = socket(AF_INET, type, protocol)) < 0 )
972 {
973#ifdef TEST_ONLY
974 if (portchk_debug)
975 perror(_("socket"));
976#else
977 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
978 sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
979#endif
980 }
981 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
982 (void *) &flag, sizeof(flag)) < 0 )
983 {
984#ifdef TEST_ONLY
985 if (portchk_debug)
986 perror(_("setsockopt"));
987#else
988 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
989 sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
990#endif
991 }
992
993 addr.sin_family = AF_INET;
994 addr.sin_port = htons(port);
995 addr.sin_addr.s_addr = INADDR_ANY;
996
997 retval = bind (sock, (struct sockaddr *) &addr, addrlen);
998
999 if (retval == 0)
1000 {
1001 /* we can bind the port, thus it is unused
1002 */
1003 close (sock);
1004 }
1005 else
1006 {
1007 if (errno == EINVAL || errno == EADDRINUSE)
1008 {
1009 /* try to connect to the port
1010 */
1011 if (protocol == IPPROTO_TCP)
1012 check_port_tcp(port);
1013 else
1014 check_port_udp(port);
1015 }
1016 else
1017 {
1018#ifdef TEST_ONLY
1019 if (portchk_debug)
1020 perror(_("bind"));
1021#else
1022 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
1023 sh_error_message(errno, errbuf, sizeof(errbuf)), _("bind"));
1024#endif
1025 }
1026 close (sock);
1027 }
1028 }
1029 return 0;
1030}
1031
1032static int sh_portchk_scan_ports_tcp (int min_port, int max_port)
1033{
1034 return sh_portchk_scan_ports_generic (min_port, max_port, SOCK_STREAM, IPPROTO_TCP);
1035}
1036
1037static int sh_portchk_scan_ports_udp (int min_port, int max_port)
1038{
1039 return sh_portchk_scan_ports_generic (min_port, max_port, SOCK_DGRAM, IPPROTO_UDP);
1040}
1041
1042/* Subroutine to add an interface
1043 */
1044static int sh_portchk_add_interface (const char * str)
1045{
1046 struct in_addr haddr;
1047 char errbuf[256];
1048
1049 if (iface_initialized == 0)
1050 {
1051 iface_list.used = 0;
1052 iface_initialized = 1;
1053 }
1054
1055 if (0 == inet_aton(str, &haddr))
1056 return -1;
1057
1058 if (iface_list.used == SH_IFACE_MAX)
1059 return -1;
1060
1061 sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"), inet_ntoa(haddr));
1062 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1063 errbuf, _("sh_portchk_add_interface"));
1064
1065 memcpy (&(iface_list.iface[iface_list.used].s_addr), &(haddr.s_addr), sizeof(in_addr_t));
1066 ++iface_list.used;
1067
1068 return 0;
1069}
1070
1071
1072/* Subroutine to add a required or optional port/service
1073 */
1074static int sh_portchk_add_required_port_generic (char * service, char * interface, int type)
1075{
1076 char buf[256];
1077 char proto[4];
1078 char * p;
1079 char * endptr;
1080 unsigned long int port;
1081 struct in_addr haddr;
1082 struct sh_portentry * portent;
1083
1084 if (0 == inet_aton(interface, &haddr))
1085 return -1;
1086
1087 sl_strlcpy (buf, service, sizeof(buf));
1088
1089 p = strchr(buf, '/');
1090 if (!p)
1091 return -1;
1092 if (0 == strcmp(p, _("/tcp")))
1093 sl_strlcpy(proto, _("tcp"), sizeof(proto));
1094 else if (0 == strcmp(p, _("/udp")))
1095 sl_strlcpy(proto, _("udp"), sizeof(proto));
1096 else
1097 return -1;
1098
1099 *p = '\0';
1100 port = strtoul(buf, &endptr, 0);
1101
1102 if (*endptr != '\0')
1103 {
1104 portent = sh_portchk_get_from_list (proto, -1, haddr, buf);
1105 if (!portent)
1106 sh_portchk_add_to_list (proto, -1, haddr, buf, type, SH_PORT_UNKN);
1107 else
1108 {
1109#ifdef TEST_ONLY
1110 fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, proto);
1111#else
1112 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1113 _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
1114#endif
1115 return -1;
1116 }
1117 }
1118 else if (port <= 65535)
1119 {
1120 portent = sh_portchk_get_from_list (proto, port, haddr, NULL);
1121 if (!portent)
1122 sh_portchk_add_to_list (proto, port, haddr, NULL, type, SH_PORT_UNKN);
1123 else
1124 {
1125#ifdef TEST_ONLY
1126 fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, proto);
1127#else
1128 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1129 _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
1130#endif
1131 return -1;
1132 }
1133 }
1134 else
1135 return -1;
1136
1137 return 0;
1138}
1139
1140/* Internal interface to add required or optional ports as 'iface:portlist'
1141 */
1142static int sh_portchk_add_required_generic (const char * str, int type)
1143{
1144 size_t len;
1145 size_t ll = 0;
1146
1147 char * interface = NULL;
1148 char * list;
1149 char * p;
1150#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1151 char * saveptr;
1152#endif
1153
1154 if (!str)
1155 return -1;
1156
1157 if (strchr(str, ':'))
1158 {
1159 len = strlen(str);
1160 for (ll = 0; ll < len; ++ll)
1161 {
1162 if (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
1163 {
1164 interface = SH_ALLOC(ll+1);
1165 sl_strlcpy(interface, str, ll+1);
1166 interface[ll] = '\0';
1167 while (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
1168 ++ll;
1169 break;
1170 }
1171 }
1172 }
1173 else
1174 {
1175 interface = SH_ALLOC(8);
1176 sl_strlcpy(interface, _("0.0.0.0"), 8);
1177 interface[7] = '\0';
1178 while (str[ll] == ' ' || str[ll] == '\t')
1179 ++ll;
1180 }
1181
1182 if (!interface)
1183 return -1;
1184
1185 if (str[ll] == '\0')
1186 {
1187 SH_FREE(interface);
1188 return -1;
1189 }
1190
1191 if (portchk_debug)
1192 fprintf(stderr, "add ports for interface: %s\n", interface);
1193
1194 list = sh_util_strdup(&str[ll]);
1195#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1196 p = strtok_r (list, " ,\t", &saveptr);
1197#else
1198 p = strtok (list, " ,\t");
1199#endif
1200 if (!p)
1201 {
1202 SH_FREE(interface);
1203 SH_FREE(list);
1204 return -1;
1205 }
1206 while (p)
1207 {
1208 if (-1 == sh_portchk_add_required_port_generic (p, interface, type))
1209 {
1210 SH_FREE(interface);
1211 SH_FREE(list);
1212 return -1;
1213 }
1214#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1215 p = strtok_r (NULL, " ,\t", &saveptr);
1216#else
1217 p = strtok (NULL, " ,\t");
1218#endif
1219 }
1220 SH_FREE(interface);
1221 SH_FREE(list);
1222 return 0;
1223}
1224
1225/* User interface to add required ports as 'iface:portlist'
1226 */
1227static int sh_portchk_add_required (const char * str)
1228{
1229 return sh_portchk_add_required_generic (str, SH_PORT_REQ);
1230}
1231
1232/* User interface to add optional ports as 'iface:portlist'
1233 */
1234static int sh_portchk_add_optional (const char * str)
1235{
1236 return sh_portchk_add_required_generic (str, SH_PORT_OPT);
1237}
1238
1239/* User interface to add ignoreable ports as 'iface:portlist'
1240 */
1241static int sh_portchk_add_ignore (const char * str)
1242{
1243 return sh_portchk_add_required_generic (str, SH_PORT_IGN);
1244}
1245
1246/* Interface to run port check
1247 */
1248int sh_portchk_check ()
1249{
1250 int min_port = 0;
1251
1252 if (sh_portchk_active != S_FALSE)
1253 {
1254 sh_portchk_reset_lists();
1255 if (0 != geteuid())
1256 {
1257 min_port = 1024;
1258#ifdef TEST_ONLY
1259 fprintf(stderr, "** WARNING not scanning ports < 1024\n");
1260#else
1261 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1262 _("not scanning ports below 1024"), _("sh_portchk_check"));
1263#endif
1264 }
1265 if (sh_portchk_check_udp == 1)
1266 sh_portchk_scan_ports_udp(min_port, -1);
1267 sh_portchk_scan_ports_tcp(min_port, -1);
1268 sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_REPORT);
1269 if (sh_portchk_check_udp == 1)
1270 sh_portchk_check_list (&portlist_udp, "udp", SH_PORT_REPORT);
1271 }
1272 return 0;
1273}
1274#endif
1275
1276#ifdef SH_CUTEST
1277#include "CuTest.h"
1278
1279void Test_portcheck_lists (CuTest *tc)
1280{
1281#if defined(SH_USE_PORTCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
1282 struct in_addr haddr_local;
1283 struct sh_portentry * portent;
1284
1285 CuAssertTrue(tc, 0 != inet_aton("127.0.0.1", &haddr_local));
1286
1287 sh_portchk_add_to_list ("tcp", 8000, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
1288
1289 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, NULL);
1290 CuAssertPtrNotNull(tc, portent);
1291
1292 CuAssertTrue(tc, portent->port == 8000);
1293 CuAssertTrue(tc, 0 == strcmp("127.0.0.1", portent->interface));
1294 CuAssertTrue(tc, portent->status == SH_PORT_UNKN);
1295 CuAssertTrue(tc, portent->flag == SH_PORT_NOT);
1296
1297 sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_NOREPT);
1298
1299 CuAssertTrue(tc, NULL == portlist_tcp);
1300
1301 sh_portchk_add_to_list ("tcp", 8000, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
1302 sh_portchk_add_to_list ("tcp", 8001, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
1303 sh_portchk_add_to_list ("tcp", 8002, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
1304 sh_portchk_add_to_list ("tcp", 8003, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
1305 sh_portchk_add_to_list ("tcp", 8004, haddr_local, NULL, SH_PORT_IGN, SH_PORT_UNKN);
1306 sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
1307 sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
1308 sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
1309 sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
1310 sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo5", SH_PORT_IGN, SH_PORT_UNKN);
1311
1312 sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_NOREPT);
1313
1314 CuAssertPtrNotNull(tc, portlist_tcp);
1315
1316 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, NULL);
1317 CuAssertPtrNotNull(tc, portent);
1318
1319 portent = sh_portchk_get_from_list("tcp", 8001, haddr_local, NULL);
1320 CuAssertTrue(tc, NULL == portent);
1321
1322 portent = sh_portchk_get_from_list("tcp", 8002, haddr_local, NULL);
1323 CuAssertPtrNotNull(tc, portent);
1324
1325 portent = sh_portchk_get_from_list("tcp", 8003, haddr_local, NULL);
1326 CuAssertTrue(tc, NULL == portent);
1327
1328 portent = sh_portchk_get_from_list("tcp", 8004, haddr_local, NULL);
1329 CuAssertPtrNotNull(tc, portent);
1330
1331 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo1");
1332 CuAssertTrue(tc, NULL == portent);
1333
1334 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo2");
1335 CuAssertPtrNotNull(tc, portent);
1336 CuAssertTrue(tc, 0 == strcmp(portent->service, "foo2"));
1337
1338 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo3");
1339 CuAssertTrue(tc, NULL == portent);
1340
1341 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo4");
1342 CuAssertPtrNotNull(tc, portent);
1343 CuAssertTrue(tc, 0 == strcmp(portent->service, "foo4"));
1344
1345 portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo5");
1346 CuAssertPtrNotNull(tc, portent);
1347 CuAssertTrue(tc, 0 == strcmp(portent->service, "foo5"));
1348#else
1349 (void) tc; /* fix compiler warning */
1350#endif
1351 return;
1352}
1353#endif
1354
1355#ifdef TEST_ONLY
1356
1357void usage (char * pname)
1358{
1359 printf ("%s [-r|--required interface:portlist][-o|--optional interface:portlist][--no-udp][-d|--debug] hostname\n\n", pname);
1360 printf (" Check local host for open ports; Version %s\n\n", PORTCHK_VERSION);
1361 printf (" Interface: Numeric address for an interface, e.g. 127.0.0.1\n");
1362 printf (" Portlist: List of ports or services, e.g. 22/tcp,nfs/udp,nlockmgr/udp\n");
1363 printf (" required -> must be open\n");
1364 printf (" optional -> may be open or closed\n");
1365 printf (" RPC services must be specified with service **name**, others with **port number**\n\n");
1366 printf (" Example:\n");
1367 printf (" %s --required 192.168.1.2:22/tcp,nfs/udp,nlockmgr/udp\n\n", pname);
1368 return;
1369}
1370
1371int main(int argc, char *argv[])
1372{
1373 char * pname = argv[0];
1374
1375
1376 /*
1377 test_lists();
1378
1379 portlist_tcp = sh_portchk_kill_list (portlist_tcp);
1380 portlist_udp = sh_portchk_kill_list (portlist_udp);
1381 */
1382
1383 // sh_portchk_add_required ("127.0.0.1 : nlockmgr/tcp, 5308/tcp, nfs/tcp");
1384
1385 while (argc > 1 && argv[1][0] == '-')
1386 {
1387 if (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h"))
1388 {
1389 usage(pname);
1390 exit (0);
1391 }
1392 else if (0 == strcmp(argv[1], "--required") || 0 == strcmp(argv[1], "-r"))
1393 {
1394 if (argc < 3)
1395 {
1396 usage(pname);
1397 exit (1);
1398 }
1399 sh_portchk_add_required (argv[2]);
1400 --argc; ++argv;
1401 }
1402 else if (0 == strcmp(argv[1], "--optional") || 0 == strcmp(argv[1], "-o"))
1403 {
1404 if (argc < 3)
1405 {
1406 usage(pname);
1407 exit (1);
1408 }
1409 sh_portchk_add_optional (argv[2]);
1410 --argc; ++argv;
1411 }
1412 else if (0 == strcmp(argv[1], "--no-udp"))
1413 {
1414 sh_portchk_check_udp = 0;
1415 }
1416 else if (0 == strcmp(argv[1], "--debug") || 0 == strcmp(argv[1], "-d"))
1417 {
1418 portchk_debug = 1;
1419 }
1420 else
1421 {
1422 usage(pname);
1423 exit (1);
1424 }
1425 --argc; ++argv;
1426 }
1427
1428 if (argc < 2)
1429 {
1430 usage(pname);
1431 exit (1);
1432 }
1433
1434 portchk_hostname = argv[1];
1435
1436 if (0 != sh_portchk_init ())
1437 {
1438 usage(pname);
1439 exit (1);
1440 }
1441
1442 sh_portchk_check();
1443
1444 /*
1445 sleep(10);
1446
1447 sh_portchk_check();
1448 */
1449
1450 return 0;
1451}
1452#endif
Note: See TracBrowser for help on using the repository browser.