Ignore:
Timestamp:
Jan 7, 2008, 8:52:13 PM (17 years ago)
Author:
katerina
Message:

Make sh_hash.c thread-safe, remove plenty of tiny allocations, improve sh_mem_dump, modify port check to run as thread, and fix unsetting of sh_thread_pause_flag (was too early).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_portcheck.c

    r140 r149  
    8080#define SH_PORT_OPT 2
    8181#define SH_PORT_IGN 3
     82#define SH_PORT_BLACKLIST 4
    8283
    8384#define SH_PORT_MISS 0
     
    8788#define SH_PORT_NOREPT 0
    8889#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"))
    8994
    9095struct sh_portentry {
     
    101106static struct sh_portentry * portlist_udp = NULL;
    102107
     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
    103117#define SH_PORTCHK_INTERVAL 300
    104118
     
    117131#include "sh_pthread.h"
    118132
     133SH_MUTEX_STATIC(mutex_port_check, PTHREAD_MUTEX_INITIALIZER);
     134
    119135static int sh_portchk_severity  = SH_ERR_SEVERE;
    120136#endif
     
    132148static int sh_portchk_add_optional (const char * str);
    133149
     150/* Exported interface to add blacklisted ports as 'iface:portlist'
     151 */
     152static int sh_portchk_add_blacklist (const char * str);
     153
    134154/* Exported interface to add an ethernet interface
    135155 */
    136156static int sh_portchk_add_interface (const char * str);
    137157
     158/* verify whether port/interface is blacklisted (do not check)
     159 */
     160static int sh_portchk_is_blacklisted(int port, struct in_addr haddr, int proto);
    138161
    139162#ifndef TEST_ONLY
     
    148171  if (val <= 0)
    149172    {
     173      SH_MUTEX_LOCK(mutex_thread_nolog);
    150174      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
    151175                       _("port check interval"), c);
     176      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    152177      retval = -1;
    153178    }
     
    196221    },
    197222    {
     223        N_("portcheckskip"),
     224        sh_portchk_add_blacklist,
     225    },
     226    {
    198227        N_("portcheckactive"),
    199228        sh_portchk_set_active,
     
    232261
    233262
    234 static char * check_services (int port, char * proto);
     263static char * check_services (int port, int proto);
    235264
    236265#ifdef TEST_ONLY
     
    249278#endif
    250279
    251 static void sh_portchk_add_to_list (char * proto,
     280static void sh_portchk_add_to_list (int proto,
    252281                                    int port, struct in_addr haddr, char * service,
    253282                                    int flag, int status)
     
    257286  if (portchk_debug)
    258287    fprintf(stderr, _("add to list: port %d/%s %d %d (%s)\n"),
    259             port, proto, flag, status, service ? service : _("undef"));
     288            port, SH_PROTO_STR(proto), flag, status, service ? service : _("undef"));
    260289
    261290  new->port = port;
     
    270299  else
    271300    new->service = NULL;
    272   if (0 == strcmp(proto, "tcp"))
     301  if (proto == IPPROTO_TCP)
    273302    {
    274303      new->next = portlist_tcp;
     
    322351}
    323352 
     353static struct sh_port * sh_portchk_kill_blacklist (struct sh_port * head)
     354{
     355  if (head)
     356    {
     357      if (head->next)
     358        sh_portchk_kill_blacklist (head->next);
     359
     360      SH_FREE(head);
     361    }
     362  return NULL;
     363}
     364 
    324365/* check the list of open ports for any that are marked as UNKN
    325366 */
    326 static void sh_portchk_check_list (struct sh_portentry ** head, char * proto, int report)
     367static void sh_portchk_check_list (struct sh_portentry ** head, int proto, int report)
    327368{
    328369  struct sh_portentry * ptr = *head;
     
    334375      if (portchk_debug && report)
    335376        fprintf(stderr, _("check list: port %d/%s %d %d\n"),
    336                 ptr->port, proto, ptr->flag, ptr->status);
     377                ptr->port, SH_PROTO_STR(proto), ptr->flag, ptr->status);
    337378
    338379      if (ptr->status == SH_PORT_UNKN)
     
    343384            {
    344385              snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceMissing] port %s:%d/%s (%s)"),
    345                         ptr->interface, ptr->port, proto,
     386                        ptr->interface, ptr->port, SH_PROTO_STR(proto),
    346387                        ptr->service ? ptr->service : check_services(ptr->port, proto));
    347388#ifdef TEST_ONLY
     
    350391#else
    351392              if (report == SH_PORT_REPORT)
    352                 sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    353                                 MSG_PORT_REPORT, errbuf);
     393                {
     394                  SH_MUTEX_LOCK(mutex_thread_nolog);
     395                  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
     396                                  MSG_PORT_REPORT, errbuf);
     397                  SH_MUTEX_UNLOCK(mutex_thread_nolog);
     398                }
    354399#endif
    355400            }
     
    361406              if (portchk_debug && report)
    362407                fprintf(stderr, _("removing: port %d/%s %d %d\n"),
    363                         ptr->port, proto, ptr->flag, ptr->status);
     408                        ptr->port, SH_PROTO_STR(proto), ptr->flag, ptr->status);
    364409             
    365410              if (ptr == *head)
     
    399444
    400445
    401 static struct sh_portentry * sh_portchk_get_from_list (char * proto, int port,
     446static struct sh_portentry * sh_portchk_get_from_list (int proto, int port,
    402447                                                       struct in_addr haddr, char * service)
    403448{
     
    407452  sl_strlcpy (iface_all, _("0.0.0.0"), sizeof(iface_all));
    408453 
    409   if (0 == strcmp(proto, "tcp"))
     454  if (proto == IPPROTO_TCP)
    410455    portlist = portlist_tcp;
    411456  else
     
    439484     
    440485
    441 static void sh_portchk_cmp_to_list (char * proto, int port, struct in_addr haddr, char * service)
     486static void sh_portchk_cmp_to_list (int proto, int port, struct in_addr haddr, char * service)
    442487{
    443488  struct sh_portentry * portent;
     
    452497        {
    453498          snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceNew] port %s:%d/%s (%s)"),
    454                     inet_ntoa(haddr), port, proto, service);
     499                    inet_ntoa(haddr), port, SH_PROTO_STR(proto), service);
    455500#ifdef TEST_ONLY
    456501          fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
    457                   inet_ntoa(haddr), port, proto, service);
    458 #else
     502                  inet_ntoa(haddr), port, SH_PROTO_STR(proto), service);
     503#else
     504          SH_MUTEX_LOCK(mutex_thread_nolog);
    459505          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    460506                          MSG_PORT_REPORT, errbuf);
     507          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    461508#endif
    462509          /*
     
    468515        {
    469516          snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceRestarted] port %s:%d/%s to %d/%s (%s)"),
    470                     inet_ntoa(haddr), portent->port, proto, port, proto, service);
     517                    inet_ntoa(haddr), portent->port, SH_PROTO_STR(proto), port, SH_PROTO_STR(proto), service);
    471518#ifdef TEST_ONLY
    472519          fprintf(stderr, _("service: %s\n"), errbuf);
    473520#else
     521          SH_MUTEX_LOCK(mutex_thread_nolog);
    474522          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    475523                          MSG_PORT_REPORT, errbuf);
     524          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    476525#endif
    477526
     
    481530        {
    482531          snprintf (errbuf, sizeof(errbuf), _("POLICY [ServicePortSwitch] port %s:%d/%s to %d/%s (%s)"),
    483                     inet_ntoa(haddr), portent->port, proto, port, proto, service);
     532                    inet_ntoa(haddr), portent->port, SH_PROTO_STR(proto), port, SH_PROTO_STR(proto), service);
    484533#ifdef TEST_ONLY
    485534          fprintf(stderr, _("service: %s\n"), errbuf);
    486535#else
     536          SH_MUTEX_LOCK(mutex_thread_nolog);
    487537          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    488538                          MSG_PORT_REPORT, errbuf);
     539          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    489540#endif
    490541          portent->port   = port;
     
    501552        {
    502553          snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceNew] port %s:%d/%s (%s)"),
    503                     inet_ntoa(haddr), port, proto, check_services(port, proto));
     554                    inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
    504555#ifdef TEST_ONLY
    505556          fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
    506                   inet_ntoa(haddr), port, proto, check_services(port, proto));
    507 #else
     557                  inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
     558#else
     559          SH_MUTEX_LOCK(mutex_thread_nolog);
    508560          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    509561                          MSG_PORT_REPORT, errbuf);
     562          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    510563#endif
    511564
     
    517570        {
    518571          snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceRestarted] port %s:%d/%s (%s)"),
    519                     inet_ntoa(haddr), port, proto, check_services(port, proto));
     572                    inet_ntoa(haddr), port, SH_PROTO_STR(proto), check_services(port, proto));
    520573#ifdef TEST_ONLY
    521574          fprintf(stderr, _("port   : %s\n"), errbuf);
    522575#else
     576          SH_MUTEX_LOCK(mutex_thread_nolog);
    523577          sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
    524578                          MSG_PORT_REPORT, errbuf);
     579          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    525580#endif
    526581
     
    541596 * Returns NULL on failure
    542597 */
    543 static char * check_services (int port, char * proto)
     598static char * check_services (int port, int proto)
    544599{
    545600  static char buf[256];
    546   struct servent * service = getservbyport(htons(port), proto);
     601  struct servent * service = getservbyport(htons(port), SH_PROTO_STR(proto));
    547602
    548603  if (service && service->s_name && service->s_name[0] != '\0')
     
    630685      sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/udp on %15s: %s"),
    631686                  port, inet_ntoa(haddr), sh_error_message(errno, errbuf, sizeof(errbuf)));
     687      SH_MUTEX_LOCK(mutex_thread_nolog);
    632688      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
    633689                      errmsg, _("connect"));
     690      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    634691#endif
    635692    }
     
    666723              p = check_rpc_list (port, &sinr, IPPROTO_UDP);
    667724             
    668               sh_portchk_cmp_to_list ("udp", port, haddr, p ? p : NULL);
     725              sh_portchk_cmp_to_list (IPPROTO_UDP, port, haddr, p ? p : NULL);
    669726             
    670727              /* If not an RPC service, try to get name from /etc/services
    671728               */
    672729              if (!p)
    673                 p = check_services(port, "udp");
     730                p = check_services(port, IPPROTO_UDP);
    674731             
    675732              if (portchk_debug)
     
    722779      sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/tcp on %15s: %s"),
    723780                  port, inet_ntoa(haddr), sh_error_message(errno, errbuf, sizeof(errbuf)));
     781      SH_MUTEX_LOCK(mutex_thread_nolog);
    724782      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN,
    725783                      errmsg, _("connect"));
     784      SH_MUTEX_UNLOCK(mutex_thread_nolog);
    726785#endif
    727786    }
     
    732791      p = check_rpc_list (port, &sinr, IPPROTO_TCP);
    733792
    734       sh_portchk_cmp_to_list ("tcp", port, haddr, p ? p : NULL);
     793      sh_portchk_cmp_to_list (IPPROTO_TCP, port, haddr, p ? p : NULL);
    735794
    736795      /* If not an RPC service, try to get name from /etc/services
    737796       */
    738797      if (!p)
    739         p = check_services(port, "tcp");
     798        p = check_services(port, IPPROTO_TCP);
    740799
    741800      if (portchk_debug)
     
    815874#endif
    816875
    817 int sh_portchk_init (struct mod_type * arg)
     876static int sh_portchk_init_internal (void)
    818877{
    819878  struct hostent * hent;
    820879  int              i = 0;
    821880  char errbuf[256];
    822   (void) arg;
    823881
    824882  if (portchk_debug)
     
    831889    return -1;
    832890
     891  SH_MUTEX_LOCK(mutex_port_check);
    833892  if (iface_initialized == 0)
    834893    {
     
    852911      sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"),
    853912                  inet_ntoa(iface_list.iface[i]));
     913      SH_MUTEX_LOCK(mutex_thread_nolog);
    854914      sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
    855915                      errbuf, _("sh_portchk_init"));
    856     }
     916      SH_MUTEX_UNLOCK(mutex_thread_nolog);
     917    }
     918  SH_MUTEX_UNLOCK(mutex_port_check);
    857919
    858920  return 0;
    859921}
     922
     923int sh_portchk_init (struct mod_type * arg)
     924{
     925  if (sh_portchk_active == S_FALSE)
     926    return SH_MOD_FAILED;
     927  if (!portchk_hostname)
     928    return SH_MOD_FAILED;
     929
     930#ifdef HAVE_PTHREAD
     931  if (arg != NULL && arg->initval < 0 &&
     932      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
     933    {
     934      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
     935        return SH_MOD_THREAD;
     936      else
     937        return SH_MOD_FAILED;
     938    }
     939#endif
     940  return sh_portchk_init_internal();
     941}
     942
     943
    860944
    861945#if !defined(TEST_ONLY)
    862946int sh_portchk_reconf ()
    863947{
     948  SH_MUTEX_LOCK(mutex_port_check);
    864949  iface_initialized    = 0;
    865 
    866950  sh_portchk_active    = 1;
    867951  sh_portchk_check_udp = 1;
     952  sh_portchk_interval  = SH_PORTCHK_INTERVAL;
    868953
    869954  portlist_udp = sh_portchk_kill_list (portlist_udp);
    870955  portlist_tcp = sh_portchk_kill_list (portlist_tcp);
     956
     957  blacklist_udp = sh_portchk_kill_blacklist (blacklist_udp);
     958  blacklist_tcp = sh_portchk_kill_blacklist (blacklist_tcp);
     959  SH_MUTEX_UNLOCK(mutex_port_check);
    871960  return 0;
    872961}
     
    903992  while (i < iface_list.used)
    904993    {
     994      haddr.s_addr = iface_list.iface[i].s_addr;
     995
     996      if (0 != sh_portchk_is_blacklisted(port, haddr, protocol))
     997        {
     998          ++i; continue;
     999        }
     1000
    9051001      if ((sock = socket(AF_INET, type, protocol)) < 0 )
    9061002        {
     
    9091005            perror(_("socket"));
    9101006#else
     1007          SH_MUTEX_LOCK(mutex_thread_nolog);
    9111008          sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    9121009                          sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
     1010          SH_MUTEX_UNLOCK(mutex_thread_nolog);
     1011          ++i;
     1012          continue;
    9131013#endif
    9141014        }
     
    9201020            perror(_("setsockopt"));
    9211021#else
     1022          SH_MUTEX_LOCK(mutex_thread_nolog);
    9221023          sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    9231024                          sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
    924 #endif
    925         }
    926 
    927       memcpy (&(haddr.s_addr), &(iface_list.iface[i].s_addr), sizeof(in_addr_t));
     1025          SH_MUTEX_UNLOCK(mutex_thread_nolog);
     1026#endif
     1027          ++i;
     1028          continue;
     1029        }
     1030
    9281031
    9291032      if (protocol == IPPROTO_TCP)
     
    9821085            perror(_("socket"));
    9831086#else
     1087          SH_MUTEX_LOCK(mutex_thread_nolog);
    9841088          sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    9851089                          sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
    986 #endif
     1090          SH_MUTEX_UNLOCK(mutex_thread_nolog);
     1091#endif
     1092          continue;
    9871093        }
    9881094      if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
     
    9931099            perror(_("setsockopt"));
    9941100#else
     1101          SH_MUTEX_LOCK(mutex_thread_nolog);
    9951102          sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    9961103                          sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
    997 #endif
     1104          SH_MUTEX_UNLOCK(mutex_thread_nolog);
     1105#endif
     1106          continue;
    9981107        }
    9991108
     
    10271136                perror(_("bind"));
    10281137#else
     1138              SH_MUTEX_LOCK(mutex_thread_nolog);
    10291139              sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
    10301140                              sh_error_message(errno, errbuf, sizeof(errbuf)), _("bind"));
     1141              SH_MUTEX_UNLOCK(mutex_thread_nolog);
    10311142#endif
    10321143            }
     
    10671178
    10681179  sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"), inet_ntoa(haddr));
     1180  SH_MUTEX_LOCK(mutex_thread_nolog);
    10691181  sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
    10701182                  errbuf, _("sh_portchk_add_interface"));
     1183  SH_MUTEX_UNLOCK(mutex_thread_nolog);
    10711184
    10721185  memcpy (&(iface_list.iface[iface_list.used].s_addr), &(haddr.s_addr), sizeof(in_addr_t));
     
    10761189}
    10771190
     1191/* verify whether port/interface is blacklisted (do not check)
     1192 */
     1193static int sh_portchk_is_blacklisted(int port, struct in_addr haddr, int proto)
     1194{
     1195  struct sh_port * head;
     1196
     1197  if (proto == IPPROTO_TCP)
     1198    head = blacklist_tcp;
     1199  else
     1200    head = blacklist_udp;
     1201
     1202  while (head)
     1203    {
     1204      if (head->port == port)
     1205        {
     1206          if ((head->haddr.s_addr == 0) || (head->haddr.s_addr == haddr.s_addr))
     1207            return 1;
     1208          else
     1209            return 0;
     1210        }
     1211      head = head->next;
     1212    }
     1213  return 0;
     1214}
     1215
     1216
     1217static int sh_portchk_blacklist(int port, struct in_addr haddr, int proto)
     1218{
     1219  struct sh_port * black;
     1220  struct sh_port * head;
     1221
     1222  if (proto == IPPROTO_TCP)
     1223    head = blacklist_tcp;
     1224  else
     1225    head = blacklist_udp;
     1226
     1227  black = head;
     1228
     1229  while (black)
     1230    {
     1231      if (black->port == port && head->haddr.s_addr == haddr.s_addr)
     1232        return -1;
     1233      black = black->next;
     1234    }
     1235  black = SH_ALLOC (sizeof(struct sh_port));
     1236  black->port  = port;
     1237  black->haddr.s_addr = haddr.s_addr;
     1238  black->next  = head;
     1239
     1240  if (proto == IPPROTO_TCP)
     1241    blacklist_tcp = black;
     1242  else
     1243    blacklist_udp = black;
     1244  return 0;
     1245}
     1246 
    10781247 
    10791248/* Subroutine to add a required or optional port/service
     
    10821251{
    10831252  char buf[256];
    1084   char proto[4];
     1253  int proto;
    10851254  char * p;
    10861255  char * endptr;
     
    10981267    return -1;
    10991268  if (0 == strcmp(p, _("/tcp")))
    1100     sl_strlcpy(proto, _("tcp"), sizeof(proto));
    1101   else if  (0 == strcmp(p, _("/udp")))   
    1102     sl_strlcpy(proto, _("udp"), sizeof(proto));
     1269    proto = IPPROTO_TCP;
     1270  else if  (0 == strcmp(p, _("/udp")))
     1271    proto = IPPROTO_UDP;
    11031272  else
    11041273    return -1;
     
    11061275  *p = '\0';
    11071276  port = strtoul(buf, &endptr, 0);
     1277
     1278  /* Blacklisted ports
     1279   */
     1280  if (*endptr == '\0' && port <= 65535 && type == SH_PORT_BLACKLIST)
     1281    return (sh_portchk_blacklist(port, haddr, proto));
    11081282
    11091283  if (*endptr != '\0')
     
    11151289        {
    11161290#ifdef TEST_ONLY
    1117           fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, proto);
    1118 #else
     1291          fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, SH_PROTO_STR(proto));
     1292#else
     1293          SH_MUTEX_LOCK(mutex_thread_nolog);
    11191294          sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
    11201295                          _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
     1296          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    11211297#endif
    11221298          return -1;
     
    11311307        {
    11321308#ifdef TEST_ONLY
    1133           fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, proto);
    1134 #else
     1309          fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, SH_PROTO_STR(proto));
     1310#else
     1311          SH_MUTEX_LOCK(mutex_thread_nolog);
    11351312          sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
    11361313                          _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
     1314          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    11371315#endif
    11381316          return -1;
     
    11511329  size_t len;
    11521330  size_t ll = 0;
     1331  int    status;
    11531332
    11541333  char * interface = NULL;
     
    12131392  while (p)
    12141393    {
    1215       if (-1 == sh_portchk_add_required_port_generic (p, interface, type))
     1394      status = sh_portchk_add_required_port_generic (p, interface, type);
     1395
     1396      if (-1 == status)
    12161397        {
    12171398          SH_FREE(interface);
     
    12511432}
    12521433
     1434/* User interface to add ports that should not be checked as 'iface:portlist'
     1435 */
     1436static int sh_portchk_add_blacklist (const char * str)
     1437{
     1438  return sh_portchk_add_required_generic (str, SH_PORT_BLACKLIST);
     1439}
     1440
    12531441/* Interface to run port check
    12541442 */
     
    12571445  int min_port = 0;
    12581446
     1447  SH_MUTEX_LOCK(mutex_port_check);
    12591448  if (sh_portchk_active != S_FALSE)
    12601449    {
     
    12661455          fprintf(stderr, "** WARNING not scanning ports < 1024\n");
    12671456#else
     1457          SH_MUTEX_LOCK(mutex_thread_nolog);
    12681458          sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
    12691459                          _("not scanning ports below 1024"), _("sh_portchk_check"));
     1460          SH_MUTEX_UNLOCK(mutex_thread_nolog);
    12701461#endif
    12711462        }
     
    12731464        sh_portchk_scan_ports_udp(min_port, -1);
    12741465      sh_portchk_scan_ports_tcp(min_port, -1);
    1275       sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_REPORT);
     1466      sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_REPORT);
    12761467      if (sh_portchk_check_udp == 1)
    1277         sh_portchk_check_list (&portlist_udp, "udp", SH_PORT_REPORT);
    1278     }
     1468        sh_portchk_check_list (&portlist_udp, IPPROTO_UDP, SH_PORT_REPORT);
     1469    }
     1470  SH_MUTEX_UNLOCK(mutex_port_check);
    12791471  return 0;
    12801472}
     
    12921484  CuAssertTrue(tc, 0 != inet_aton("127.0.0.1", &haddr_local));
    12931485
    1294   sh_portchk_add_to_list ("tcp",  8000, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
    1295 
    1296   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, NULL);
     1486  sh_portchk_add_to_list (IPPROTO_TCP,  8000, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
     1487
     1488  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, NULL);
    12971489  CuAssertPtrNotNull(tc, portent);
    12981490
     
    13021494  CuAssertTrue(tc, portent->flag == SH_PORT_NOT);
    13031495
    1304   sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_NOREPT);
     1496  sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
    13051497
    13061498  CuAssertTrue(tc, NULL == portlist_tcp);
    13071499
    1308   sh_portchk_add_to_list ("tcp",  8000, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
    1309   sh_portchk_add_to_list ("tcp",  8001, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
    1310   sh_portchk_add_to_list ("tcp",  8002, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
    1311   sh_portchk_add_to_list ("tcp",  8003, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
    1312   sh_portchk_add_to_list ("tcp",  8004, haddr_local, NULL, SH_PORT_IGN, SH_PORT_UNKN);
    1313   sh_portchk_add_to_list ("tcp",    -1, haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
    1314   sh_portchk_add_to_list ("tcp",    -1, haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
    1315   sh_portchk_add_to_list ("tcp",    -1, haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
    1316   sh_portchk_add_to_list ("tcp",    -1, haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
    1317   sh_portchk_add_to_list ("tcp",    -1, haddr_local, "foo5", SH_PORT_IGN, SH_PORT_UNKN);
    1318 
    1319   sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_NOREPT);
     1500  sh_portchk_add_to_list (IPPROTO_TCP,  8000, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
     1501  sh_portchk_add_to_list (IPPROTO_TCP,  8001, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
     1502  sh_portchk_add_to_list (IPPROTO_TCP,  8002, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
     1503  sh_portchk_add_to_list (IPPROTO_TCP,  8003, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
     1504  sh_portchk_add_to_list (IPPROTO_TCP,  8004, haddr_local, NULL, SH_PORT_IGN, SH_PORT_UNKN);
     1505  sh_portchk_add_to_list (IPPROTO_TCP,    -1, haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
     1506  sh_portchk_add_to_list (IPPROTO_TCP,    -1, haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
     1507  sh_portchk_add_to_list (IPPROTO_TCP,    -1, haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
     1508  sh_portchk_add_to_list (IPPROTO_TCP,    -1, haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
     1509  sh_portchk_add_to_list (IPPROTO_TCP,    -1, haddr_local, "foo5", SH_PORT_IGN, SH_PORT_UNKN);
     1510
     1511  sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
    13201512
    13211513  CuAssertPtrNotNull(tc, portlist_tcp);
    13221514
    1323   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, NULL);
     1515  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, NULL);
    13241516  CuAssertPtrNotNull(tc, portent);
    13251517
    1326   portent = sh_portchk_get_from_list("tcp",  8001, haddr_local, NULL);
     1518  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8001, haddr_local, NULL);
    13271519  CuAssertTrue(tc, NULL == portent);
    13281520
    1329   portent = sh_portchk_get_from_list("tcp",  8002, haddr_local, NULL);
     1521  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8002, haddr_local, NULL);
    13301522  CuAssertPtrNotNull(tc, portent);
    13311523
    1332   portent = sh_portchk_get_from_list("tcp",  8003, haddr_local, NULL);
     1524  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8003, haddr_local, NULL);
    13331525  CuAssertTrue(tc, NULL == portent);
    13341526
    1335   portent = sh_portchk_get_from_list("tcp",  8004, haddr_local, NULL);
     1527  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8004, haddr_local, NULL);
    13361528  CuAssertPtrNotNull(tc, portent);
    13371529
    1338   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, "foo1");
     1530  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, "foo1");
    13391531  CuAssertTrue(tc, NULL == portent);
    13401532
    1341   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, "foo2");
     1533  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, "foo2");
    13421534  CuAssertPtrNotNull(tc, portent);
    13431535  CuAssertTrue(tc, 0 == strcmp(portent->service, "foo2"));
    13441536
    1345   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, "foo3");
     1537  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, "foo3");
    13461538  CuAssertTrue(tc, NULL == portent);
    13471539
    1348   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, "foo4");
     1540  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, "foo4");
    13491541  CuAssertPtrNotNull(tc, portent);
    13501542  CuAssertTrue(tc, 0 == strcmp(portent->service, "foo4"));
    13511543
    1352   portent = sh_portchk_get_from_list("tcp",  8000, haddr_local, "foo5");
     1544  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, haddr_local, "foo5");
    13531545  CuAssertPtrNotNull(tc, portent);
    13541546  CuAssertTrue(tc, 0 == strcmp(portent->service, "foo5"));
     1547
     1548  CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, haddr_local, IPPROTO_TCP));
     1549  CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, haddr_local, IPPROTO_TCP));
     1550  CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, haddr_local, IPPROTO_TCP));
     1551  CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, haddr_local, IPPROTO_TCP));
     1552  CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, haddr_local, IPPROTO_UDP));
     1553  CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, haddr_local, IPPROTO_UDP));
     1554  CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, haddr_local, IPPROTO_UDP));
     1555  CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, haddr_local, IPPROTO_UDP));
     1556
     1557  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, haddr_local, IPPROTO_UDP));
     1558  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, haddr_local, IPPROTO_UDP));
     1559  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, haddr_local, IPPROTO_UDP));
     1560  CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, haddr_local, IPPROTO_UDP));
     1561
     1562  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, haddr_local, IPPROTO_TCP));
     1563  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, haddr_local, IPPROTO_TCP));
     1564  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, haddr_local, IPPROTO_TCP));
     1565  CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, haddr_local, IPPROTO_TCP));
    13551566#else
    13561567  (void) tc; /* fix compiler warning */
Note: See TracChangeset for help on using the changeset viewer.