source: trunk/src/sh_portcheck.c@ 211

Last change on this file since 211 was 210, checked in by katerina, 16 years ago

Fix for ticket #136 (compile error); also enhance testsuite to catch compiler warnings.

  • Property svn:executable set to *
File size: 43.9 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#define SH_PORT_BLACKLIST 4
83
84#define SH_PORT_MISS 0
85#define SH_PORT_ISOK 1
86#define SH_PORT_UNKN 2
87
88#define SH_PORT_NOREPT 0
89#define SH_PORT_REPORT 1
90
91#define SH_PROTO_TCP 0
92#define SH_PROTO_UDP 1
93#define SH_PROTO_STR(a) (((a) == IPPROTO_TCP) ? _("tcp") : _("udp"))
94
95struct sh_portentry {
96 int port;
97 char interface[SH_INTERFACE_SIZE];
98 char * service;
99 char * error;
100 int flag; /* required or not */
101 int status; /* missing or not */
102 struct sh_portentry * next;
103};
104
105static struct sh_portentry * portlist_tcp = NULL;
106static struct sh_portentry * portlist_udp = NULL;
107
108struct sh_port {
109 int port;
110 struct in_addr haddr;
111 struct sh_port * next;
112};
113
114static struct sh_port * blacklist_tcp = NULL;
115static struct sh_port * blacklist_udp = NULL;
116
117#define SH_PORTCHK_INTERVAL 300
118
119static int sh_portchk_check_udp = 1;
120static int sh_portchk_active = 1;
121static int sh_portchk_interval = SH_PORTCHK_INTERVAL;
122#if !defined(TEST_ONLY)
123
124#define FIL__ _("sh_portcheck.c")
125#include "samhain.h"
126#include "sh_error.h"
127#include "sh_mem.h"
128#include "sh_calls.h"
129#include "sh_utils.h"
130#include "sh_modules.h"
131#define SH_NEED_GETHOSTBYXXX
132#include "sh_static.h"
133#include "sh_pthread.h"
134
135SH_MUTEX_STATIC(mutex_port_check, PTHREAD_MUTEX_INITIALIZER);
136
137static int sh_portchk_severity = SH_ERR_SEVERE;
138
139extern char * sh_port2proc_query(int proto, struct in_addr * saddr, int sport,
140 unsigned long * pid, char * user, size_t userlen);
141extern int sh_port2proc_prepare();
142
143#endif
144
145/* Exported interface to add ignoreable ports as 'iface:portlist'
146 */
147static int sh_portchk_add_ignore (const char * str);
148
149/* Exported interface to add required ports as 'iface:portlist'
150 */
151static int sh_portchk_add_required (const char * str);
152
153/* Exported interface to add optional ports as 'iface:portlist'
154 */
155static int sh_portchk_add_optional (const char * str);
156
157/* Exported interface to add blacklisted ports as 'iface:portlist'
158 */
159static int sh_portchk_add_blacklist (const char * str);
160
161/* Exported interface to add an ethernet interface
162 */
163static int sh_portchk_add_interface (const char * str);
164
165/* verify whether port/interface is blacklisted (do not check)
166 */
167static int sh_portchk_is_blacklisted(int port, struct in_addr haddr, int proto);
168
169#ifndef TEST_ONLY
170
171static int sh_portchk_set_interval (const char * c)
172{
173 int retval = 0;
174 long val;
175
176 SL_ENTER(_("sh_portchk_set_interval"));
177 val = strtol (c, (char **)NULL, 10);
178 if (val <= 0)
179 {
180 SH_MUTEX_LOCK(mutex_thread_nolog);
181 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
182 _("port check interval"), c);
183 SH_MUTEX_UNLOCK(mutex_thread_nolog);
184 retval = -1;
185 }
186
187 sh_portchk_interval = (time_t) val;
188 SL_RETURN(0, _("sh_portchk_set_interval"));
189}
190
191
192static int sh_portchk_set_active (const char * str)
193{
194 return sh_util_flagval(str, &sh_portchk_active);
195}
196
197static int sh_portchk_set_udp (const char * str)
198{
199 return sh_util_flagval(str, &sh_portchk_check_udp);
200}
201
202static int sh_portchk_set_severity (const char * str)
203{
204 char tmp[32];
205 tmp[0] = '='; tmp[1] = '\0';
206 sl_strlcat (tmp, str, 32);
207 return sh_error_set_level (tmp, &sh_portchk_severity);
208}
209
210sh_rconf sh_portchk_table[] = {
211 {
212 N_("severityportcheck"),
213 sh_portchk_set_severity,
214 },
215 {
216 N_("portcheckrequired"),
217 sh_portchk_add_required,
218 },
219 {
220 N_("portcheckoptional"),
221 sh_portchk_add_optional,
222 },
223 {
224 N_("portcheckignore"),
225 sh_portchk_add_ignore,
226 },
227 {
228 N_("portcheckskip"),
229 sh_portchk_add_blacklist,
230 },
231 {
232 N_("portcheckactive"),
233 sh_portchk_set_active,
234 },
235 {
236 N_("portcheckinterface"),
237 sh_portchk_add_interface,
238 },
239 {
240 N_("portcheckinterval"),
241 sh_portchk_set_interval,
242 },
243 {
244 N_("portcheckudp"),
245 sh_portchk_set_udp,
246 },
247 {
248 NULL,
249 NULL
250 }
251};
252
253#endif
254
255/* Interface to initialize port check
256 */
257int sh_portchk_init (struct mod_type * arg);
258
259/* Interface to reset port check
260 */
261int sh_portchk_reset (void);
262
263/* Interface to run port check
264 */
265int sh_portchk_check (void);
266
267
268static char * check_services (int port, int proto);
269
270#ifdef TEST_ONLY
271
272static int portchk_debug = 0;
273#define SH_ALLOC malloc
274#define SH_FREE free
275#define sh_util_strdup strdup
276#define sl_strlcpy strncpy
277#define _(a) a
278
279#else
280
281static int portchk_debug = 0;
282
283#endif
284
285static void sh_portchk_add_to_list (int proto,
286 int port, struct in_addr haddr, char * service,
287 int flag, int status)
288{
289 struct sh_portentry * new = SH_ALLOC (sizeof(struct sh_portentry));
290
291 if (portchk_debug)
292 fprintf(stderr, _("add to list: port %d/%s %d %d (%s)\n"),
293 port, SH_PROTO_STR(proto), flag, status, service ? service : _("undef"));
294
295 new->port = port;
296 sl_strlcpy (new->interface, inet_ntoa(haddr), SH_INTERFACE_SIZE);
297 new->status = status;
298 new->flag = flag;
299
300 new->error = NULL;
301
302 if (service)
303 new->service = sh_util_strdup (service);
304 else
305 new->service = NULL;
306 if (proto == IPPROTO_TCP)
307 {
308 new->next = portlist_tcp;
309 portlist_tcp = new;
310 }
311 else
312 {
313 new->next = portlist_udp;
314 portlist_udp = new;
315 }
316 return;
317}
318
319/* Reset the list by setting all entries to UNKN.
320 * In the next cycle we will check, and set found ports to ISOK.
321 * Thereafter, we check for entries that are still UNKN.
322 */
323static void sh_portchk_reset_lists (void)
324{
325 struct sh_portentry * portlist;
326
327 portlist = portlist_tcp;
328 while (portlist)
329 {
330 if (portlist->status != SH_PORT_MISS)
331 portlist->status = SH_PORT_UNKN;
332 portlist = portlist->next;
333 }
334 portlist = portlist_udp;
335 while (portlist)
336 {
337 if (portlist->status != SH_PORT_MISS)
338 portlist->status = SH_PORT_UNKN;
339 portlist = portlist->next;
340 }
341 return;
342}
343
344static struct sh_portentry * sh_portchk_kill_list (struct sh_portentry * head)
345{
346 if (head)
347 {
348 if (head->next)
349 sh_portchk_kill_list (head->next);
350
351 if (head->service)
352 SH_FREE(head->service);
353 SH_FREE(head);
354 }
355 return NULL;
356}
357
358static struct sh_port * sh_portchk_kill_blacklist (struct sh_port * head)
359{
360 if (head)
361 {
362 if (head->next)
363 sh_portchk_kill_blacklist (head->next);
364
365 SH_FREE(head);
366 }
367 return NULL;
368}
369
370/* These variables are not used anywhere. They only exist
371 * to assign &pre, &ptr to them, which keeps gcc from
372 * putting it into a register, and avoids the 'clobbered
373 * by longjmp' warning. And no, 'volatile' proved insufficient.
374 */
375static void * sh_dummy_pre = NULL;
376static void * sh_dummy_ptr = NULL;
377
378/* check the list of open ports for any that are marked as UNKN
379 */
380static void sh_portchk_check_list (struct sh_portentry ** head, int proto, int report)
381{
382 struct sh_portentry * ptr = *head;
383 struct sh_portentry * pre = *head;
384 char errbuf[256];
385
386 /* Take the address to keep gcc from putting them into registers.
387 * Avoids the 'clobbered by longjmp' warning.
388 */
389 sh_dummy_pre = (void*) &pre;
390 sh_dummy_ptr = (void*) &ptr;
391
392 while (ptr)
393 {
394 if (portchk_debug && report)
395 fprintf(stderr, _("check list: port %d/%s %d %d\n"),
396 ptr->port, SH_PROTO_STR(proto), ptr->flag, ptr->status);
397
398 if (ptr->status == SH_PORT_UNKN)
399 {
400 /* Don't report missing ports that are marked as optional
401 */
402 if (ptr->flag != SH_PORT_OPT && ptr->flag != SH_PORT_IGN)
403 {
404 snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"),
405 ptr->interface, ptr->port, SH_PROTO_STR(proto),
406 ptr->service ? ptr->service : check_services(ptr->port, proto));
407#ifdef TEST_ONLY
408 if (report == SH_PORT_REPORT)
409 fprintf(stderr, _("%s\n"), errbuf);
410#else
411 if (report == SH_PORT_REPORT)
412 {
413 SH_MUTEX_LOCK(mutex_thread_nolog);
414 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
415 MSG_PORT_MISS, errbuf);
416 SH_MUTEX_UNLOCK(mutex_thread_nolog);
417 }
418#endif
419 }
420
421 ptr->status = SH_PORT_MISS;
422
423 if ((ptr->flag != SH_PORT_REQ) && (ptr->flag != SH_PORT_OPT) && (ptr->flag != SH_PORT_IGN))
424 {
425 if (portchk_debug && report)
426 fprintf(stderr, _("removing: port %d/%s %d %d\n"),
427 ptr->port, SH_PROTO_STR(proto), ptr->flag, ptr->status);
428
429 if (ptr == *head)
430 {
431 *head = ptr->next;
432 if (ptr->service)
433 SH_FREE(ptr->service);
434 SH_FREE(ptr);
435 ptr = *head;
436 pre = *head;
437 continue;
438 }
439 else if (ptr->next == NULL)
440 {
441 pre->next = NULL;
442 if (ptr->service)
443 SH_FREE(ptr->service);
444 SH_FREE(ptr);
445 return;
446 }
447 else
448 {
449 pre->next = ptr->next;
450 if (ptr->service)
451 SH_FREE(ptr->service);
452 SH_FREE(ptr);
453 ptr = pre->next;
454 continue;
455 }
456 }
457 }
458 pre = ptr;
459 ptr = ptr->next;
460 }
461 return;
462}
463
464
465static struct sh_portentry * sh_portchk_get_from_list (int proto, int port,
466 struct in_addr haddr, char * service)
467{
468 struct sh_portentry * portlist;
469 char iface_all[8];
470
471 sl_strlcpy (iface_all, _("0.0.0.0"), sizeof(iface_all));
472
473 if (proto == IPPROTO_TCP)
474 portlist = portlist_tcp;
475 else
476 portlist = portlist_udp;
477
478 if (service)
479 {
480 while (portlist)
481 {
482 if (portlist->service &&
483 0 == strcmp(service, portlist->service) &&
484 (0 == strcmp(portlist->interface, inet_ntoa(haddr)) ||
485 0 == strcmp(portlist->interface, iface_all)))
486 return portlist;
487 portlist = portlist->next;
488 }
489 }
490 else
491 {
492 while (portlist)
493 {
494 if (port == portlist->port &&
495 (0 == strcmp(portlist->interface, inet_ntoa(haddr)) ||
496 0 == strcmp(portlist->interface, iface_all)))
497 return portlist;
498 portlist = portlist->next;
499 }
500 }
501 return NULL;
502}
503
504
505static void sh_portchk_cmp_to_list (int proto, int port, struct in_addr haddr, char * service)
506{
507 struct sh_portentry * portent;
508 char errbuf[256];
509
510
511 portent = sh_portchk_get_from_list (proto, port, haddr, service);
512
513 if (service)
514 {
515 if (!portent)
516 {
517 char * path;
518 unsigned long qpid;
519 char user[USER_MAX];
520
521 snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"),
522 inet_ntoa(haddr), port, SH_PROTO_STR(proto), service);
523#ifdef TEST_ONLY
524 fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
525 inet_ntoa(haddr), port, SH_PROTO_STR(proto), service);
526#else
527 path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
528 SH_MUTEX_LOCK(mutex_thread_nolog);
529 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
530 MSG_PORT_NEW, errbuf, path, qpid, user);
531 SH_MUTEX_UNLOCK(mutex_thread_nolog);
532 SH_FREE(path);
533#endif
534 /*
535 * was not there, thus it is not in 'required' or 'optional' list
536 */
537 sh_portchk_add_to_list (proto, port, haddr, service, SH_PORT_NOT, SH_PORT_ISOK);
538 }
539 else if (portent->status == SH_PORT_MISS && portent->flag != SH_PORT_IGN)
540 {
541 char * path;
542 unsigned long qpid;
543 char user[USER_MAX];
544
545 snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s), was %d/%s"),
546 inet_ntoa(haddr), port, SH_PROTO_STR(proto), service, portent->port, SH_PROTO_STR(proto));
547#ifdef TEST_ONLY
548 fprintf(stderr, _("service: %s\n"), errbuf);
549#else
550 path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
551 SH_MUTEX_LOCK(mutex_thread_nolog);
552 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
553 MSG_PORT_RESTART, errbuf, path, qpid, user);
554 SH_MUTEX_UNLOCK(mutex_thread_nolog);
555 SH_FREE(path);
556#endif
557
558 portent->status = SH_PORT_ISOK;
559 }
560 else if (port != portent->port && (-1) != portent->port)
561 {
562 char * path;
563 unsigned long qpid;
564 char user[USER_MAX];
565
566 snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s), was %d/%s"),
567 inet_ntoa(haddr), port, SH_PROTO_STR(proto), service, portent->port, SH_PROTO_STR(proto));
568#ifdef TEST_ONLY
569 fprintf(stderr, _("service: %s\n"), errbuf);
570#else
571 path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
572 SH_MUTEX_LOCK(mutex_thread_nolog);
573 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
574 MSG_PORT_NEWPORT, errbuf, path, qpid, user);
575 SH_MUTEX_UNLOCK(mutex_thread_nolog);
576 SH_FREE(path);
577#endif
578 portent->port = port;
579 portent->status = SH_PORT_ISOK;
580 }
581 else
582 {
583 portent->status = SH_PORT_ISOK;
584 }
585 }
586 else
587 {
588 if (!portent)
589 {
590 char * path;
591 unsigned long qpid;
592 char user[USER_MAX];
593
594 snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"),
595 inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
596#ifdef TEST_ONLY
597 fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
598 inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
599#else
600 path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
601 SH_MUTEX_LOCK(mutex_thread_nolog);
602 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
603 MSG_PORT_NEW, errbuf, path, qpid, user);
604 SH_MUTEX_UNLOCK(mutex_thread_nolog);
605 SH_FREE(path);
606#endif
607
608 /* was not there, thus it is not in 'required' or 'optional' list
609 */
610 sh_portchk_add_to_list (proto, port, haddr, service, SH_PORT_NOT, SH_PORT_ISOK);
611 }
612 else if (portent->status == SH_PORT_MISS && portent->flag != SH_PORT_IGN)
613 {
614 char * path;
615 unsigned long qpid;
616 char user[USER_MAX];
617
618 snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"),
619 inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
620#ifdef TEST_ONLY
621 fprintf(stderr, _("port : %s\n"), errbuf);
622#else
623 path = sh_port2proc_query(proto, &haddr, port, &qpid, user, sizeof(user));
624 SH_MUTEX_LOCK(mutex_thread_nolog);
625 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
626 MSG_PORT_RESTART, errbuf, path, qpid, user);
627 SH_MUTEX_UNLOCK(mutex_thread_nolog);
628 SH_FREE(path);
629#endif
630
631 portent->status = SH_PORT_ISOK;
632 }
633 else
634 {
635 portent->status = SH_PORT_ISOK;
636 }
637 }
638
639 return;
640}
641
642
643/* Returns a static buffer containing the name of the service
644 * running on port <port> (from /etc/services)
645 * Returns NULL on failure
646 */
647static char * check_services (int port, int proto)
648{
649 static char buf[256];
650 struct servent * service = getservbyport(htons(port), SH_PROTO_STR(proto));
651
652 if (service && service->s_name && service->s_name[0] != '\0')
653 {
654 snprintf (buf, sizeof(buf), _("maybe_%s"), service->s_name);
655 }
656 else
657 {
658 snprintf (buf, sizeof(buf), "%s",_("unknown"));
659 }
660 return buf;
661}
662
663/* Returns a static buffer containing the name of the service
664 * running on port <port> at <address> (from portmap daemon)
665 * Returns NULL on failure
666 */
667static char * check_rpc_list (int port, struct sockaddr_in * address,
668 unsigned long prot)
669{
670 struct pmaplist * head;
671 struct rpcent *r;
672 static char buf[256];
673
674 head = pmap_getmaps(address);
675
676 if (head)
677 {
678 do /* while (head != NULL) */
679 {
680 if ((head->pml_map.pm_prot == prot) &&
681 (port == (int)head->pml_map.pm_port))
682 {
683 r = getrpcbynumber((int)head->pml_map.pm_prog);
684 if (r && r->r_name && r->r_name[0] != '\0')
685 {
686 snprintf (buf, sizeof(buf), "%s", r->r_name);
687 return buf;
688 }
689 else
690 {
691 snprintf (buf, sizeof(buf), "RPC_%lu",
692 (unsigned long)head->pml_map.pm_prog);
693 return buf;
694 }
695 }
696 head = head->pml_next;
697 }
698 while (head != NULL);
699 }
700
701 return NULL;
702}
703
704static int check_port_udp_internal (int fd, int port, struct in_addr haddr)
705{
706 struct sockaddr_in sinr;
707 /* struct in_addr haddr; */
708 int retval;
709 char * p;
710 char buf[8];
711#ifndef TEST_ONLY
712 char errmsg[256];
713 int nerr;
714#endif
715 char errbuf[SH_ERRBUF_SIZE];
716
717 /* inet_aton(interface, &haddr); */
718
719 sinr.sin_family = AF_INET;
720 sinr.sin_port = htons (port);
721 sinr.sin_addr = haddr;
722
723 do {
724 retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
725 } while (retval < 0 && (errno == EINTR || errno == EINPROGRESS));
726
727 if (retval == -1)
728 {
729#ifdef TEST_ONLY
730 if (portchk_debug)
731 perror(_("connect"));
732#else
733 nerr = errno;
734 sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/udp on %15s: %s"),
735 port, inet_ntoa(haddr), sh_error_message(errno, errbuf, sizeof(errbuf)));
736 SH_MUTEX_LOCK(mutex_thread_nolog);
737 sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
738 errmsg, _("connect"));
739 SH_MUTEX_UNLOCK(mutex_thread_nolog);
740#endif
741 }
742 else
743 {
744 do {
745 retval = send (fd, buf, 0, 0);
746 } while (retval < 0 && errno == EINTR);
747
748 if (retval == -1 && errno == ECONNREFUSED)
749 {
750 if (portchk_debug)
751 fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
752 port, inet_ntoa(haddr));
753 }
754 else
755 {
756 /* Only the second send() may catch the error
757 */
758 do {
759 retval = send (fd, buf, 0, 0);
760 } while (retval < 0 && errno == EINTR);
761
762 if (retval == -1 && errno == ECONNREFUSED)
763 {
764 if (portchk_debug)
765 fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
766 port, inet_ntoa(haddr));
767 }
768 else if (retval != -1)
769 {
770 /* Try to get service name from portmap
771 */
772 p = check_rpc_list (port, &sinr, IPPROTO_UDP);
773
774 sh_portchk_cmp_to_list (IPPROTO_UDP, port, haddr, p ? p : NULL);
775
776 /* If not an RPC service, try to get name from /etc/services
777 */
778 if (!p)
779 p = check_services(port, IPPROTO_UDP);
780
781 if (portchk_debug)
782 fprintf(stderr, _("check port: %5d/udp on %15s open %s\n"),
783 port, inet_ntoa(haddr), p);
784
785 }
786 }
787 }
788 close (fd);
789 return 0;
790}
791
792static int check_port_tcp_internal (int fd, int port, struct in_addr haddr)
793{
794 struct sockaddr_in sinr;
795 /* struct in_addr haddr; */
796 int retval;
797 int flags;
798 char * p;
799#ifndef TEST_ONLY
800 char errmsg[256];
801 int nerr;
802#endif
803 char errbuf[SH_ERRBUF_SIZE];
804
805 /* inet_aton(interface, &haddr); */
806
807 sinr.sin_family = AF_INET;
808 sinr.sin_port = htons (port);
809 sinr.sin_addr = haddr;
810
811 do {
812 retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
813 } while (retval < 0 && (errno == EINTR || errno == EINPROGRESS));
814
815 if (retval == -1 && errno == ECONNREFUSED)
816 {
817 if (portchk_debug)
818 fprintf(stderr, _("check port: %5d on %15s established/time_wait\n"),
819 port, inet_ntoa(haddr));
820 }
821 else if (retval == -1)
822 {
823#ifdef TEST_ONLY
824 if (portchk_debug)
825 perror(_("connect"));
826#else
827 nerr = errno;
828 sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/tcp on %15s: %s"),
829 port, inet_ntoa(haddr), sh_error_message(errno, errbuf, sizeof(errbuf)));
830 SH_MUTEX_LOCK(mutex_thread_nolog);
831 sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
832 errmsg, _("connect"));
833 SH_MUTEX_UNLOCK(mutex_thread_nolog);
834#endif
835 }
836 else
837 {
838 /* Try to get service name from portmap
839 */
840 p = check_rpc_list (port, &sinr, IPPROTO_TCP);
841
842 sh_portchk_cmp_to_list (IPPROTO_TCP, port, haddr, p ? p : NULL);
843
844 /* If not an RPC service, try to get name from /etc/services
845 */
846 if (!p)
847 p = check_services(port, IPPROTO_TCP);
848
849 if (portchk_debug)
850 fprintf(stderr, _("check port: %5d on %15s open %s\n"),
851 port, inet_ntoa(haddr), p);
852
853#if !defined(O_NONBLOCK)
854#if defined(O_NDELAY)
855#define O_NONBLOCK O_NDELAY
856#else
857#define O_NONBLOCK 0
858#endif
859#endif
860
861 /* prepare to close connection gracefully
862 */
863 if (port == 22) /* ssh */
864 {
865 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
866 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
867 retval = write (fd, _("SSH-2.0-Foobar"), 14);
868 if (retval > 0) retval = write (fd, "\r\n", 2);
869 }
870 else if (port == 25) /* smtp */
871 {
872 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
873 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
874 retval = write (fd, _("QUIT"), 4);
875 if (retval > 0) retval = write (fd, "\r\n", 2);
876 }
877 else if (port == 79) /* finger */
878 {
879 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
880 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
881 retval = write (fd, "\r\n", 2);
882 }
883 else if (port == 110) /* pop3 */
884 {
885 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
886 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
887 retval = write (fd, _("QUIT"), 4);
888 if (retval > 0) retval = write (fd, "\r\n", 2);
889 }
890 else if (port == 143) /* imap */
891 {
892 flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
893 retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
894 retval = write (fd, _("A01 LOGOUT"), 10);
895 if (retval > 0) retval = write (fd, "\r\n", 2);
896 }
897
898 if (portchk_debug && retval < 0)
899 fprintf(stderr, _("check port: error writing to port %5d\n"),
900 port);
901 }
902 close (fd);
903 return 0;
904}
905
906/* typedef uint32_t in_addr_t;
907 * struct in_addr
908 * {
909 * in_addr_t s_addr;
910 * };
911 */
912
913#define SH_IFACE_MAX 16
914
915struct portchk_interfaces {
916 struct in_addr iface[SH_IFACE_MAX];
917 int used;
918};
919
920static struct portchk_interfaces iface_list;
921static int iface_initialized = 0;
922
923#ifdef TEST_ONLY
924static char * portchk_hostname = NULL;
925#else
926static char * portchk_hostname = sh.host.name;
927#endif
928
929static int sh_portchk_init_internal (void)
930{
931 struct hostent * hent;
932 volatile int i; /* might be clobbered by ‘longjmp’ or ‘vfork’*/
933 char errbuf[256];
934
935 if (portchk_debug)
936 fprintf(stderr, _("checking ports on: %s\n"), portchk_hostname ? portchk_hostname : _("NULL"));
937
938 if (!portchk_hostname)
939 return -1;
940
941 if (sh_portchk_active == S_FALSE)
942 return -1;
943
944 SH_MUTEX_LOCK(mutex_port_check);
945 if (iface_initialized == 0)
946 {
947 iface_list.used = 0;
948 iface_initialized = 1;
949 }
950
951 SH_MUTEX_LOCK(mutex_resolv);
952 hent = sh_gethostbyname(portchk_hostname);
953 i = 0;
954 while (hent && hent->h_addr_list[i] && (iface_list.used < SH_IFACE_MAX))
955 {
956 memcpy (&(iface_list.iface[iface_list.used].s_addr), hent->h_addr_list[i], sizeof(in_addr_t));
957 ++iface_list.used;
958 ++i;
959 }
960 SH_MUTEX_UNLOCK(mutex_resolv);
961
962 for (i = 0; i < iface_list.used; ++i)
963 {
964 sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"),
965 inet_ntoa(iface_list.iface[i]));
966 SH_MUTEX_LOCK(mutex_thread_nolog);
967 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
968 errbuf, _("sh_portchk_init"));
969 SH_MUTEX_UNLOCK(mutex_thread_nolog);
970 }
971 SH_MUTEX_UNLOCK(mutex_port_check);
972
973 return 0;
974}
975
976int sh_portchk_init (struct mod_type * arg)
977{
978#ifndef HAVE_PTHREAD
979 (void) arg;
980#endif
981
982 if (sh_portchk_active == S_FALSE)
983 return SH_MOD_FAILED;
984 if (!portchk_hostname)
985 return SH_MOD_FAILED;
986
987#ifdef HAVE_PTHREAD
988 if (arg != NULL && arg->initval < 0 &&
989 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
990 {
991 if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
992 return SH_MOD_THREAD;
993 else
994 return SH_MOD_FAILED;
995 }
996#endif
997 return sh_portchk_init_internal();
998}
999
1000
1001
1002#if !defined(TEST_ONLY)
1003int sh_portchk_reconf (void)
1004{
1005 SH_MUTEX_LOCK(mutex_port_check);
1006 iface_initialized = 0;
1007 sh_portchk_active = 1;
1008 sh_portchk_check_udp = 1;
1009 sh_portchk_interval = SH_PORTCHK_INTERVAL;
1010
1011 portlist_udp = sh_portchk_kill_list (portlist_udp);
1012 portlist_tcp = sh_portchk_kill_list (portlist_tcp);
1013
1014 blacklist_udp = sh_portchk_kill_blacklist (blacklist_udp);
1015 blacklist_tcp = sh_portchk_kill_blacklist (blacklist_tcp);
1016 SH_MUTEX_UNLOCK(mutex_port_check);
1017 return 0;
1018}
1019
1020int sh_portchk_cleanup (void)
1021{
1022 return sh_portchk_reconf ();
1023}
1024
1025int sh_portchk_timer (time_t tcurrent)
1026{
1027 static time_t lastcheck = 0;
1028
1029 SL_ENTER(_("sh_portchk_timer"));
1030 if ((time_t) (tcurrent - lastcheck) >= sh_portchk_interval)
1031 {
1032 lastcheck = tcurrent;
1033 SL_RETURN((-1), _("sh_portchk_timer"));
1034 }
1035 SL_RETURN(0, _("sh_portchk_timer"));
1036}
1037#endif
1038
1039static int check_port_generic (int port, int type, int protocol)
1040{
1041 volatile int i = 0;
1042 int sock = -1;
1043 int flag = 1; /* non-zero to enable an option */
1044 struct in_addr haddr;
1045 char errbuf[SH_ERRBUF_SIZE];
1046
1047 /* Check all interfaces for this host
1048 */
1049 while (i < iface_list.used)
1050 {
1051 haddr.s_addr = iface_list.iface[i].s_addr;
1052
1053 if (0 != sh_portchk_is_blacklisted(port, haddr, protocol))
1054 {
1055 ++i; continue;
1056 }
1057
1058 if ((sock = socket(AF_INET, type, protocol)) < 0 )
1059 {
1060 ++i;
1061#ifdef TEST_ONLY
1062 if (portchk_debug)
1063 perror(_("socket"));
1064#else
1065 SH_MUTEX_LOCK(mutex_thread_nolog);
1066 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
1067 sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
1068 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1069#endif
1070 continue;
1071 }
1072 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1073 (void *) &flag, sizeof(flag)) < 0 )
1074 {
1075 ++i;
1076#ifdef TEST_ONLY
1077 if (portchk_debug)
1078 perror(_("setsockopt"));
1079#else
1080 SH_MUTEX_LOCK(mutex_thread_nolog);
1081 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
1082 sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
1083 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1084#endif
1085 continue;
1086 }
1087
1088
1089 if (protocol == IPPROTO_TCP)
1090 check_port_tcp_internal(sock, port, haddr);
1091 else
1092 check_port_udp_internal(sock, port, haddr);
1093
1094 ++i;
1095 }
1096
1097 return 0;
1098}
1099
1100
1101
1102static int check_port_udp (int port)
1103{
1104 return check_port_generic(port, SOCK_DGRAM, IPPROTO_UDP);
1105}
1106
1107static int check_port_tcp (int port)
1108{
1109 return check_port_generic(port, SOCK_STREAM, IPPROTO_TCP);
1110}
1111
1112
1113
1114static int sh_portchk_scan_ports_generic (int min_port, int max_port_arg, int type, int protocol)
1115{
1116 /*
1117 int min_port = 1024;
1118 int max_port = 65535;
1119 */
1120
1121 volatile int port; /* might be clobbered by ‘longjmp’ or ‘vfork’*/
1122 volatile int max_port = max_port_arg;
1123 int retval;
1124 int sock = -1;
1125 int flag = 1; /* non-zero to enable an option */
1126
1127 struct sockaddr_in addr;
1128 int addrlen = sizeof(addr);
1129 char errbuf[SH_ERRBUF_SIZE];
1130
1131 if (min_port == -1)
1132 min_port = 0;
1133 if (max_port == -1)
1134 max_port = 65535;
1135
1136 for (port = min_port; port <= max_port; ++port)
1137 {
1138
1139 if ((sock = socket(AF_INET, type, protocol)) < 0 )
1140 {
1141#ifdef TEST_ONLY
1142 if (portchk_debug)
1143 perror(_("socket"));
1144#else
1145 SH_MUTEX_LOCK(mutex_thread_nolog);
1146 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
1147 sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
1148 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1149#endif
1150 continue;
1151 }
1152 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1153 (void *) &flag, sizeof(flag)) < 0 )
1154 {
1155#ifdef TEST_ONLY
1156 if (portchk_debug)
1157 perror(_("setsockopt"));
1158#else
1159 SH_MUTEX_LOCK(mutex_thread_nolog);
1160 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
1161 sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
1162 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1163#endif
1164 continue;
1165 }
1166
1167 addr.sin_family = AF_INET;
1168 addr.sin_port = htons(port);
1169 addr.sin_addr.s_addr = INADDR_ANY;
1170
1171 retval = bind (sock, (struct sockaddr *) &addr, addrlen);
1172
1173 if (retval == 0)
1174 {
1175 /* we can bind the port, thus it is unused
1176 */
1177 close (sock);
1178 }
1179 else
1180 {
1181 if (errno == EINVAL || errno == EADDRINUSE)
1182 {
1183 /* try to connect to the port
1184 */
1185 if (protocol == IPPROTO_TCP)
1186 check_port_tcp(port);
1187 else
1188 check_port_udp(port);
1189 }
1190 else
1191 {
1192#ifdef TEST_ONLY
1193 if (portchk_debug)
1194 perror(_("bind"));
1195#else
1196 SH_MUTEX_LOCK(mutex_thread_nolog);
1197 sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
1198 sh_error_message(errno, errbuf, sizeof(errbuf)), _("bind"));
1199 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1200#endif
1201 }
1202 close (sock);
1203 }
1204 }
1205 return 0;
1206}
1207
1208static int sh_portchk_scan_ports_tcp (int min_port, int max_port)
1209{
1210 return sh_portchk_scan_ports_generic (min_port, max_port, SOCK_STREAM, IPPROTO_TCP);
1211}
1212
1213static int sh_portchk_scan_ports_udp (int min_port, int max_port)
1214{
1215 return sh_portchk_scan_ports_generic (min_port, max_port, SOCK_DGRAM, IPPROTO_UDP);
1216}
1217
1218/* Subroutine to add an interface
1219 */
1220static int sh_portchk_add_interface (const char * str)
1221{
1222 struct in_addr haddr;
1223 char errbuf[256];
1224
1225 if (iface_initialized == 0)
1226 {
1227 iface_list.used = 0;
1228 iface_initialized = 1;
1229 }
1230
1231 if (0 == inet_aton(str, &haddr))
1232 return -1;
1233
1234 if (iface_list.used == SH_IFACE_MAX)
1235 return -1;
1236
1237 sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"), inet_ntoa(haddr));
1238 SH_MUTEX_LOCK(mutex_thread_nolog);
1239 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1240 errbuf, _("sh_portchk_add_interface"));
1241 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1242
1243 memcpy (&(iface_list.iface[iface_list.used].s_addr), &(haddr.s_addr), sizeof(in_addr_t));
1244 ++iface_list.used;
1245
1246 return 0;
1247}
1248
1249/* verify whether port/interface is blacklisted (do not check)
1250 */
1251static int sh_portchk_is_blacklisted(int port, struct in_addr haddr, int proto)
1252{
1253 struct sh_port * head;
1254
1255 if (proto == IPPROTO_TCP)
1256 head = blacklist_tcp;
1257 else
1258 head = blacklist_udp;
1259
1260 while (head)
1261 {
1262 if (head->port == port)
1263 {
1264 if ((head->haddr.s_addr == 0) || (head->haddr.s_addr == haddr.s_addr))
1265 return 1;
1266 else
1267 return 0;
1268 }
1269 head = head->next;
1270 }
1271 return 0;
1272}
1273
1274
1275static int sh_portchk_blacklist(int port, struct in_addr haddr, int proto)
1276{
1277 struct sh_port * black;
1278 struct sh_port * head;
1279
1280 if (proto == IPPROTO_TCP)
1281 head = blacklist_tcp;
1282 else
1283 head = blacklist_udp;
1284
1285 black = head;
1286
1287 while (black)
1288 {
1289 if (black->port == port && head->haddr.s_addr == haddr.s_addr)
1290 return -1;
1291 black = black->next;
1292 }
1293 black = SH_ALLOC (sizeof(struct sh_port));
1294 black->port = port;
1295 black->haddr.s_addr = haddr.s_addr;
1296 black->next = head;
1297
1298 if (proto == IPPROTO_TCP)
1299 blacklist_tcp = black;
1300 else
1301 blacklist_udp = black;
1302 return 0;
1303}
1304
1305
1306/* Subroutine to add a required or optional port/service
1307 */
1308static int sh_portchk_add_required_port_generic (char * service, char * interface, int type)
1309{
1310 char buf[256];
1311 int proto;
1312 char * p;
1313 char * endptr;
1314 unsigned long int port;
1315 struct in_addr haddr;
1316 struct sh_portentry * portent;
1317
1318 if (0 == inet_aton(interface, &haddr))
1319 return -1;
1320
1321 sl_strlcpy (buf, service, sizeof(buf));
1322
1323 p = strchr(buf, '/');
1324 if (!p)
1325 return -1;
1326 if (0 == strcmp(p, _("/tcp")))
1327 proto = IPPROTO_TCP;
1328 else if (0 == strcmp(p, _("/udp")))
1329 proto = IPPROTO_UDP;
1330 else
1331 return -1;
1332
1333 *p = '\0';
1334 port = strtoul(buf, &endptr, 0);
1335
1336 /* Blacklisted ports
1337 */
1338 if (*endptr == '\0' && port <= 65535 && type == SH_PORT_BLACKLIST)
1339 return (sh_portchk_blacklist(port, haddr, proto));
1340
1341 if (*endptr != '\0')
1342 {
1343 portent = sh_portchk_get_from_list (proto, -1, haddr, buf);
1344 if (!portent)
1345 sh_portchk_add_to_list (proto, -1, haddr, buf, type, SH_PORT_UNKN);
1346 else
1347 {
1348#ifdef TEST_ONLY
1349 fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, SH_PROTO_STR(proto));
1350#else
1351 SH_MUTEX_LOCK(mutex_thread_nolog);
1352 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1353 _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
1354 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1355#endif
1356 return -1;
1357 }
1358 }
1359 else if (port <= 65535)
1360 {
1361 portent = sh_portchk_get_from_list (proto, port, haddr, NULL);
1362 if (!portent)
1363 sh_portchk_add_to_list (proto, port, haddr, NULL, type, SH_PORT_UNKN);
1364 else
1365 {
1366#ifdef TEST_ONLY
1367 fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, SH_PROTO_STR(proto));
1368#else
1369 SH_MUTEX_LOCK(mutex_thread_nolog);
1370 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1371 _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
1372 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1373#endif
1374 return -1;
1375 }
1376 }
1377 else
1378 return -1;
1379
1380 return 0;
1381}
1382
1383/* Internal interface to add required or optional ports as 'iface:portlist'
1384 */
1385static int sh_portchk_add_required_generic (const char * str, int type)
1386{
1387 size_t len;
1388 size_t ll = 0;
1389 int status;
1390
1391 char * interface = NULL;
1392 char * list;
1393 char * p;
1394#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1395 char * saveptr;
1396#endif
1397
1398 if (!str)
1399 return -1;
1400
1401 if (strchr(str, ':'))
1402 {
1403 len = strlen(str);
1404 for (ll = 0; ll < len; ++ll)
1405 {
1406 if (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
1407 {
1408 interface = SH_ALLOC(ll+1);
1409 sl_strlcpy(interface, str, ll+1);
1410 interface[ll] = '\0';
1411 while (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
1412 ++ll;
1413 break;
1414 }
1415 }
1416 }
1417 else
1418 {
1419 interface = SH_ALLOC(8);
1420 sl_strlcpy(interface, _("0.0.0.0"), 8);
1421 interface[7] = '\0';
1422 while (str[ll] == ' ' || str[ll] == '\t')
1423 ++ll;
1424 }
1425
1426 if (!interface)
1427 return -1;
1428
1429 if (str[ll] == '\0')
1430 {
1431 SH_FREE(interface);
1432 return -1;
1433 }
1434
1435 if (portchk_debug)
1436 fprintf(stderr, "add ports for interface: %s\n", interface);
1437
1438 list = sh_util_strdup(&str[ll]);
1439#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1440 p = strtok_r (list, " ,\t", &saveptr);
1441#else
1442 p = strtok (list, " ,\t");
1443#endif
1444 if (!p)
1445 {
1446 SH_FREE(interface);
1447 SH_FREE(list);
1448 return -1;
1449 }
1450 while (p)
1451 {
1452 status = sh_portchk_add_required_port_generic (p, interface, type);
1453
1454 if (-1 == status)
1455 {
1456 SH_FREE(interface);
1457 SH_FREE(list);
1458 return -1;
1459 }
1460#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1461 p = strtok_r (NULL, " ,\t", &saveptr);
1462#else
1463 p = strtok (NULL, " ,\t");
1464#endif
1465 }
1466 SH_FREE(interface);
1467 SH_FREE(list);
1468 return 0;
1469}
1470
1471/* User interface to add required ports as 'iface:portlist'
1472 */
1473static int sh_portchk_add_required (const char * str)
1474{
1475 return sh_portchk_add_required_generic (str, SH_PORT_REQ);
1476}
1477
1478/* User interface to add optional ports as 'iface:portlist'
1479 */
1480static int sh_portchk_add_optional (const char * str)
1481{
1482 return sh_portchk_add_required_generic (str, SH_PORT_OPT);
1483}
1484
1485/* User interface to add ignoreable ports as 'iface:portlist'
1486 */
1487static int sh_portchk_add_ignore (const char * str)
1488{
1489 return sh_portchk_add_required_generic (str, SH_PORT_IGN);
1490}
1491
1492/* User interface to add ports that should not be checked as 'iface:portlist'
1493 */
1494static int sh_portchk_add_blacklist (const char * str)
1495{
1496 return sh_portchk_add_required_generic (str, SH_PORT_BLACKLIST);
1497}
1498
1499/* Interface to run port check
1500 */
1501int sh_portchk_check ()
1502{
1503 volatile int min_port;
1504
1505 SH_MUTEX_LOCK(mutex_port_check);
1506
1507 min_port = 0;
1508
1509 if (sh_portchk_active != S_FALSE)
1510 {
1511 sh_portchk_reset_lists();
1512 if (0 != geteuid())
1513 {
1514 min_port = 1024;
1515#ifdef TEST_ONLY
1516 fprintf(stderr, "** WARNING not scanning ports < 1024\n");
1517#else
1518 SH_MUTEX_LOCK(mutex_thread_nolog);
1519 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1520 _("not scanning ports below 1024"), _("sh_portchk_check"));
1521 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1522#endif
1523 }
1524
1525 sh_port2proc_prepare();
1526
1527 if (sh_portchk_check_udp == 1)
1528 sh_portchk_scan_ports_udp(min_port, -1);
1529 sh_portchk_scan_ports_tcp(min_port, -1);
1530
1531
1532 sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_REPORT);
1533 if (sh_portchk_check_udp == 1)
1534 sh_portchk_check_list (&portlist_udp, IPPROTO_UDP, SH_PORT_REPORT);
1535
1536 }
1537 SH_MUTEX_UNLOCK(mutex_port_check);
1538 return 0;
1539}
1540#endif
1541
1542#ifdef SH_CUTEST
1543#include "CuTest.h"
1544
1545void Test_portcheck_lists (CuTest *tc)
1546{
1547#if defined(SH_USE_PORTCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
1548 struct in_addr haddr_local;
1549 struct sh_portentry * portent;
1550
1551 CuAssertTrue(tc, 0 != inet_aton("127.0.0.1", &haddr_local));
1552
1553 sh_portchk_add_to_list (IPPROTO_TCP, 8000, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
1554
1555 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, NULL);
1556 CuAssertPtrNotNull(tc, portent);
1557
1558 CuAssertTrue(tc, portent->port == 8000);
1559 CuAssertTrue(tc, 0 == strcmp("127.0.0.1", portent->interface));
1560 CuAssertTrue(tc, portent->status == SH_PORT_UNKN);
1561 CuAssertTrue(tc, portent->flag == SH_PORT_NOT);
1562
1563 sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
1564
1565 CuAssertTrue(tc, NULL == portlist_tcp);
1566
1567 sh_portchk_add_to_list (IPPROTO_TCP, 8000, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
1568 sh_portchk_add_to_list (IPPROTO_TCP, 8001, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
1569 sh_portchk_add_to_list (IPPROTO_TCP, 8002, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
1570 sh_portchk_add_to_list (IPPROTO_TCP, 8003, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
1571 sh_portchk_add_to_list (IPPROTO_TCP, 8004, haddr_local, NULL, SH_PORT_IGN, SH_PORT_UNKN);
1572 sh_portchk_add_to_list (IPPROTO_TCP, -1, haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
1573 sh_portchk_add_to_list (IPPROTO_TCP, -1, haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
1574 sh_portchk_add_to_list (IPPROTO_TCP, -1, haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
1575 sh_portchk_add_to_list (IPPROTO_TCP, -1, haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
1576 sh_portchk_add_to_list (IPPROTO_TCP, -1, haddr_local, "foo5", SH_PORT_IGN, SH_PORT_UNKN);
1577
1578 sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
1579
1580 CuAssertPtrNotNull(tc, portlist_tcp);
1581
1582 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, NULL);
1583 CuAssertPtrNotNull(tc, portent);
1584
1585 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8001, haddr_local, NULL);
1586 CuAssertTrue(tc, NULL == portent);
1587
1588 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8002, haddr_local, NULL);
1589 CuAssertPtrNotNull(tc, portent);
1590
1591 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8003, haddr_local, NULL);
1592 CuAssertTrue(tc, NULL == portent);
1593
1594 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8004, haddr_local, NULL);
1595 CuAssertPtrNotNull(tc, portent);
1596
1597 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, "foo1");
1598 CuAssertTrue(tc, NULL == portent);
1599
1600 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, "foo2");
1601 CuAssertPtrNotNull(tc, portent);
1602 CuAssertTrue(tc, 0 == strcmp(portent->service, "foo2"));
1603
1604 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, "foo3");
1605 CuAssertTrue(tc, NULL == portent);
1606
1607 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, "foo4");
1608 CuAssertPtrNotNull(tc, portent);
1609 CuAssertTrue(tc, 0 == strcmp(portent->service, "foo4"));
1610
1611 portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, haddr_local, "foo5");
1612 CuAssertPtrNotNull(tc, portent);
1613 CuAssertTrue(tc, 0 == strcmp(portent->service, "foo5"));
1614
1615 CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, haddr_local, IPPROTO_TCP));
1616 CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, haddr_local, IPPROTO_TCP));
1617 CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, haddr_local, IPPROTO_TCP));
1618 CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, haddr_local, IPPROTO_TCP));
1619 CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, haddr_local, IPPROTO_UDP));
1620 CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, haddr_local, IPPROTO_UDP));
1621 CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, haddr_local, IPPROTO_UDP));
1622 CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, haddr_local, IPPROTO_UDP));
1623
1624 CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, haddr_local, IPPROTO_UDP));
1625 CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, haddr_local, IPPROTO_UDP));
1626 CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, haddr_local, IPPROTO_UDP));
1627 CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, haddr_local, IPPROTO_UDP));
1628
1629 CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, haddr_local, IPPROTO_TCP));
1630 CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, haddr_local, IPPROTO_TCP));
1631 CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, haddr_local, IPPROTO_TCP));
1632 CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, haddr_local, IPPROTO_TCP));
1633#else
1634 (void) tc; /* fix compiler warning */
1635#endif
1636 return;
1637}
1638#endif
1639
1640#ifdef TEST_ONLY
1641
1642void usage (char * pname)
1643{
1644 printf ("%s [-r|--required interface:portlist][-o|--optional interface:portlist][--no-udp][-d|--debug] hostname\n\n", pname);
1645 printf (" Check local host for open ports; Version %s\n\n", PORTCHK_VERSION);
1646 printf (" Interface: Numeric address for an interface, e.g. 127.0.0.1\n");
1647 printf (" Portlist: List of ports or services, e.g. 22/tcp,nfs/udp,nlockmgr/udp\n");
1648 printf (" required -> must be open\n");
1649 printf (" optional -> may be open or closed\n");
1650 printf (" RPC services must be specified with service **name**, others with **port number**\n\n");
1651 printf (" Example:\n");
1652 printf (" %s --required 192.168.1.2:22/tcp,nfs/udp,nlockmgr/udp\n\n", pname);
1653 return;
1654}
1655
1656int main(int argc, char *argv[])
1657{
1658 char * pname = argv[0];
1659
1660
1661 /*
1662 test_lists();
1663
1664 portlist_tcp = sh_portchk_kill_list (portlist_tcp);
1665 portlist_udp = sh_portchk_kill_list (portlist_udp);
1666 */
1667
1668 // sh_portchk_add_required ("127.0.0.1 : nlockmgr/tcp, 5308/tcp, nfs/tcp");
1669
1670 while (argc > 1 && argv[1][0] == '-')
1671 {
1672 if (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h"))
1673 {
1674 usage(pname);
1675 exit (0);
1676 }
1677 else if (0 == strcmp(argv[1], "--required") || 0 == strcmp(argv[1], "-r"))
1678 {
1679 if (argc < 3)
1680 {
1681 usage(pname);
1682 exit (1);
1683 }
1684 sh_portchk_add_required (argv[2]);
1685 --argc; ++argv;
1686 }
1687 else if (0 == strcmp(argv[1], "--optional") || 0 == strcmp(argv[1], "-o"))
1688 {
1689 if (argc < 3)
1690 {
1691 usage(pname);
1692 exit (1);
1693 }
1694 sh_portchk_add_optional (argv[2]);
1695 --argc; ++argv;
1696 }
1697 else if (0 == strcmp(argv[1], "--no-udp"))
1698 {
1699 sh_portchk_check_udp = 0;
1700 }
1701 else if (0 == strcmp(argv[1], "--debug") || 0 == strcmp(argv[1], "-d"))
1702 {
1703 portchk_debug = 1;
1704 }
1705 else
1706 {
1707 usage(pname);
1708 exit (1);
1709 }
1710 --argc; ++argv;
1711 }
1712
1713 if (argc < 2)
1714 {
1715 usage(pname);
1716 exit (1);
1717 }
1718
1719 portchk_hostname = argv[1];
1720
1721 if (0 != sh_portchk_init ())
1722 {
1723 usage(pname);
1724 exit (1);
1725 }
1726
1727 sh_portchk_check();
1728
1729 return 0;
1730}
1731#endif
Note: See TracBrowser for help on using the repository browser.