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