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