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 |
|
---|
1276 | #ifndef EPROTONOSUPPORT
|
---|
1277 | #define EPROTONOSUPPORT 0
|
---|
1278 | #endif
|
---|
1279 | #ifndef EAFNOSUPPORT
|
---|
1280 | #define EAFNOSUPPORT 0
|
---|
1281 | #endif
|
---|
1282 | if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
|
---|
1283 | {
|
---|
1284 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1285 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1286 | sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
|
---|
1287 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1288 | }
|
---|
1289 | #endif
|
---|
1290 | continue;
|
---|
1291 | }
|
---|
1292 | if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
---|
1293 | (void *) &flag, sizeof(flag)) < 0 )
|
---|
1294 | {
|
---|
1295 | ++i;
|
---|
1296 | #ifdef TEST_ONLY
|
---|
1297 | if (portchk_debug)
|
---|
1298 | perror(_("setsockopt"));
|
---|
1299 | #else
|
---|
1300 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1301 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1302 | sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
|
---|
1303 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1304 | #endif
|
---|
1305 | continue;
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 |
|
---|
1309 | if (protocol == IPPROTO_TCP)
|
---|
1310 | check_port_tcp_internal(sock, port, &paddr);
|
---|
1311 | else
|
---|
1312 | check_port_udp_internal(sock, port, &paddr);
|
---|
1313 |
|
---|
1314 | ++i;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | return 0;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 |
|
---|
1321 |
|
---|
1322 | static int check_port_udp (int port, int domain)
|
---|
1323 | {
|
---|
1324 | return check_port_generic(port, domain, SOCK_DGRAM, IPPROTO_UDP);
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | static int check_port_tcp (int port, int domain)
|
---|
1328 | {
|
---|
1329 | return check_port_generic(port, domain, SOCK_STREAM, IPPROTO_TCP);
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 |
|
---|
1333 | static int sh_portchk_scan_ports_generic (int min_port, int max_port_arg,
|
---|
1334 | int domain, int type, int protocol)
|
---|
1335 | {
|
---|
1336 | /*
|
---|
1337 | int min_port = 1024;
|
---|
1338 | int max_port = 65535;
|
---|
1339 | */
|
---|
1340 |
|
---|
1341 | volatile int port; /* might be clobbered by âlongjmpâ or âvforkâ*/
|
---|
1342 | volatile int max_port = max_port_arg;
|
---|
1343 | int retval;
|
---|
1344 | int sock = -1;
|
---|
1345 | int flag = 1; /* non-zero to enable an option */
|
---|
1346 |
|
---|
1347 | struct sockaddr_in addr4;
|
---|
1348 | struct sockaddr_in6 addr6;
|
---|
1349 |
|
---|
1350 | int addrlen4 = sizeof(addr4);
|
---|
1351 | int addrlen6 = sizeof(addr6);
|
---|
1352 |
|
---|
1353 | struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
|
---|
1354 |
|
---|
1355 | char errbuf[SH_ERRBUF_SIZE];
|
---|
1356 |
|
---|
1357 | if (min_port == -1)
|
---|
1358 | min_port = 0;
|
---|
1359 | if (max_port == -1)
|
---|
1360 | max_port = 65535;
|
---|
1361 |
|
---|
1362 | for (port = min_port; port <= max_port; ++port)
|
---|
1363 | {
|
---|
1364 | if ((sock = socket(domain, type, protocol)) < 0 )
|
---|
1365 | {
|
---|
1366 | #ifdef TEST_ONLY
|
---|
1367 | if (portchk_debug)
|
---|
1368 | perror(_("socket"));
|
---|
1369 | #else
|
---|
1370 | #ifndef EPROTONOSUPPORT
|
---|
1371 | #define EPROTONOSUPPORT 0
|
---|
1372 | #endif
|
---|
1373 | #ifndef EAFNOSUPPORT
|
---|
1374 | #define EAFNOSUPPORT 0
|
---|
1375 | #endif
|
---|
1376 | if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
|
---|
1377 | {
|
---|
1378 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1379 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1380 | sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
|
---|
1381 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1382 | }
|
---|
1383 | #endif
|
---|
1384 | continue;
|
---|
1385 | }
|
---|
1386 | if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
---|
1387 | (void *) &flag, sizeof(flag)) < 0 )
|
---|
1388 | {
|
---|
1389 | #ifdef TEST_ONLY
|
---|
1390 | if (portchk_debug)
|
---|
1391 | perror(_("setsockopt"));
|
---|
1392 | #else
|
---|
1393 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1394 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1395 | sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
|
---|
1396 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1397 | #endif
|
---|
1398 | continue;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | if (domain == AF_INET)
|
---|
1402 | {
|
---|
1403 | addr4.sin_family = AF_INET;
|
---|
1404 | addr4.sin_port = htons(port);
|
---|
1405 | addr4.sin_addr.s_addr = INADDR_ANY;
|
---|
1406 | retval = bind (sock, (struct sockaddr *) &addr4, addrlen4);
|
---|
1407 | }
|
---|
1408 | else
|
---|
1409 | {
|
---|
1410 | addr6.sin6_family = AF_INET6;
|
---|
1411 | addr6.sin6_port = htons(port);
|
---|
1412 | memcpy(&(addr6.sin6_addr.s6_addr), &anyaddr, sizeof(anyaddr));
|
---|
1413 | retval = bind (sock, (struct sockaddr *) &addr6, addrlen6);
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | if (retval == 0)
|
---|
1417 | {
|
---|
1418 | /* we can bind the port, thus it is unused
|
---|
1419 | */
|
---|
1420 | sl_close_fd (FIL__, __LINE__, sock);
|
---|
1421 | }
|
---|
1422 | else
|
---|
1423 | {
|
---|
1424 | if (errno == EINVAL || errno == EADDRINUSE)
|
---|
1425 | {
|
---|
1426 | /* try to connect to the port
|
---|
1427 | */
|
---|
1428 | if (protocol == IPPROTO_TCP)
|
---|
1429 | check_port_tcp(port, domain);
|
---|
1430 | else
|
---|
1431 | check_port_udp(port, domain);
|
---|
1432 | }
|
---|
1433 | else
|
---|
1434 | {
|
---|
1435 | #ifdef TEST_ONLY
|
---|
1436 | if (portchk_debug)
|
---|
1437 | perror(_("bind"));
|
---|
1438 | #else
|
---|
1439 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1440 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
1441 | sh_error_message(errno, errbuf, sizeof(errbuf)), _("bind"));
|
---|
1442 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1443 | #endif
|
---|
1444 | }
|
---|
1445 | sl_close_fd (FIL__, __LINE__, sock);
|
---|
1446 | }
|
---|
1447 | }
|
---|
1448 | return 0;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | static int sh_portchk_scan_ports_tcp (int min_port, int max_port)
|
---|
1452 | {
|
---|
1453 | #if defined(USE_IPVX)
|
---|
1454 | sh_portchk_scan_ports_generic (min_port, max_port, AF_INET6,
|
---|
1455 | SOCK_STREAM, IPPROTO_TCP);
|
---|
1456 | #endif
|
---|
1457 | return sh_portchk_scan_ports_generic (min_port, max_port, AF_INET,
|
---|
1458 | SOCK_STREAM, IPPROTO_TCP);
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | static int sh_portchk_scan_ports_udp (int min_port, int max_port)
|
---|
1462 | {
|
---|
1463 | #if defined(USE_IPVX)
|
---|
1464 | sh_portchk_scan_ports_generic (min_port, max_port, AF_INET6,
|
---|
1465 | SOCK_DGRAM, IPPROTO_UDP);
|
---|
1466 | #endif
|
---|
1467 | return sh_portchk_scan_ports_generic (min_port, max_port, AF_INET,
|
---|
1468 | SOCK_DGRAM, IPPROTO_UDP);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | /* Subroutine to add an interface
|
---|
1472 | */
|
---|
1473 | static void * sh_dummy_str = NULL; /* fix clobbered by.. warning */
|
---|
1474 |
|
---|
1475 | static int sh_portchk_add_interface (const char * str)
|
---|
1476 | {
|
---|
1477 | struct sh_sockaddr saddr;
|
---|
1478 | char errbuf[256];
|
---|
1479 | char buf[64];
|
---|
1480 |
|
---|
1481 | sh_dummy_str = (void*) &str;
|
---|
1482 |
|
---|
1483 | if (iface_initialized == 0)
|
---|
1484 | {
|
---|
1485 | iface_list.used = 0;
|
---|
1486 | iface_initialized = 1;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | do {
|
---|
1490 |
|
---|
1491 | while (*str == ',' || *str == ' ' || *str == '\t') ++str;
|
---|
1492 |
|
---|
1493 | if (*str)
|
---|
1494 | {
|
---|
1495 | char ipbuf[SH_IP_BUF];
|
---|
1496 | unsigned int i = 0;
|
---|
1497 | while (*str && i < (sizeof(buf)-1) &&
|
---|
1498 | *str != ',' && *str != ' ' && *str != '\t')
|
---|
1499 | {
|
---|
1500 | buf[i] = *str; ++str; ++i;
|
---|
1501 | }
|
---|
1502 | buf[i] = '\0';
|
---|
1503 |
|
---|
1504 | if (0 == sh_ipvx_aton(buf, &saddr))
|
---|
1505 | return -1;
|
---|
1506 |
|
---|
1507 | if (iface_list.used == SH_IFACE_MAX)
|
---|
1508 | return -1;
|
---|
1509 |
|
---|
1510 | sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), &saddr);
|
---|
1511 | sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"), ipbuf);
|
---|
1512 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1513 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1514 | errbuf, _("sh_portchk_add_interface"));
|
---|
1515 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1516 |
|
---|
1517 | memcpy (&(iface_list.iface[iface_list.used]), &(saddr), sizeof(saddr));
|
---|
1518 | ++iface_list.used;
|
---|
1519 | }
|
---|
1520 | } while (*str);
|
---|
1521 |
|
---|
1522 | return 0;
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | /* verify whether port/interface is blacklisted (do not check)
|
---|
1526 | */
|
---|
1527 | static int sh_portchk_is_blacklisted(int port, struct sh_sockaddr * saddr,
|
---|
1528 | int proto)
|
---|
1529 | {
|
---|
1530 | struct sh_port * head;
|
---|
1531 |
|
---|
1532 | if (proto == IPPROTO_TCP)
|
---|
1533 | head = blacklist_tcp;
|
---|
1534 | else
|
---|
1535 | head = blacklist_udp;
|
---|
1536 |
|
---|
1537 | while (head)
|
---|
1538 | {
|
---|
1539 | if (head->port == port)
|
---|
1540 | {
|
---|
1541 | if (sh_ipvx_isany(head->paddr) ||
|
---|
1542 | 0 == sh_ipvx_cmp(head->paddr, saddr))
|
---|
1543 | return 1;
|
---|
1544 | else
|
---|
1545 | return 0;
|
---|
1546 | }
|
---|
1547 | head = head->next;
|
---|
1548 | }
|
---|
1549 | return 0;
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 |
|
---|
1553 | static int sh_portchk_blacklist(int port, struct sh_sockaddr * saddr, int proto)
|
---|
1554 | {
|
---|
1555 | struct sh_port * black;
|
---|
1556 | struct sh_port * head;
|
---|
1557 |
|
---|
1558 | if (proto == IPPROTO_TCP)
|
---|
1559 | head = blacklist_tcp;
|
---|
1560 | else
|
---|
1561 | head = blacklist_udp;
|
---|
1562 |
|
---|
1563 | black = head;
|
---|
1564 |
|
---|
1565 | while (black)
|
---|
1566 | {
|
---|
1567 | if (black->port == port &&
|
---|
1568 | 0 == sh_ipvx_cmp(head->paddr, saddr))
|
---|
1569 | return -1;
|
---|
1570 | black = black->next;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | black = SH_ALLOC (sizeof(struct sh_port));
|
---|
1574 | black->paddr = SH_ALLOC (sizeof(struct sh_sockaddr));
|
---|
1575 | black->port = port;
|
---|
1576 | memcpy(black->paddr, saddr, sizeof(struct sh_sockaddr));
|
---|
1577 | black->next = head;
|
---|
1578 |
|
---|
1579 | if (proto == IPPROTO_TCP)
|
---|
1580 | blacklist_tcp = black;
|
---|
1581 | else
|
---|
1582 | blacklist_udp = black;
|
---|
1583 | return 0;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 |
|
---|
1587 | /* Subroutine to add a required or optional port/service
|
---|
1588 | */
|
---|
1589 | static int sh_portchk_add_required_port_generic (char * service,
|
---|
1590 | char * interface, int type)
|
---|
1591 | {
|
---|
1592 | char buf[256];
|
---|
1593 | int proto;
|
---|
1594 | char * p;
|
---|
1595 | char * endptr;
|
---|
1596 | unsigned long int port;
|
---|
1597 | struct sh_sockaddr saddr;
|
---|
1598 | struct sh_portentry * portent;
|
---|
1599 |
|
---|
1600 | if (0 == sh_ipvx_aton(interface, &saddr))
|
---|
1601 | return -1;
|
---|
1602 |
|
---|
1603 | sl_strlcpy (buf, service, sizeof(buf));
|
---|
1604 |
|
---|
1605 | p = strchr(buf, '/');
|
---|
1606 | if (!p)
|
---|
1607 | return -1;
|
---|
1608 | if (0 == strcmp(p, _("/tcp")))
|
---|
1609 | proto = IPPROTO_TCP;
|
---|
1610 | else if (0 == strcmp(p, _("/udp")))
|
---|
1611 | proto = IPPROTO_UDP;
|
---|
1612 | else
|
---|
1613 | return -1;
|
---|
1614 |
|
---|
1615 | *p = '\0';
|
---|
1616 | port = strtoul(buf, &endptr, 0);
|
---|
1617 |
|
---|
1618 | /* Blacklisted ports
|
---|
1619 | */
|
---|
1620 | if (*endptr == '\0' && port <= 65535 && type == SH_PORT_BLACKLIST)
|
---|
1621 | return (sh_portchk_blacklist(port, &saddr, proto));
|
---|
1622 |
|
---|
1623 | if (*endptr != '\0')
|
---|
1624 | {
|
---|
1625 | portent = sh_portchk_get_from_list (proto, -1, &saddr, buf);
|
---|
1626 | if (!portent)
|
---|
1627 | sh_portchk_add_to_list (proto, -1, &saddr, buf, type, SH_PORT_UNKN);
|
---|
1628 | else
|
---|
1629 | {
|
---|
1630 | #ifdef TEST_ONLY
|
---|
1631 | fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, SH_PROTO_STR(proto));
|
---|
1632 | #else
|
---|
1633 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1634 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1635 | _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
|
---|
1636 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1637 | #endif
|
---|
1638 | return -1;
|
---|
1639 | }
|
---|
1640 | }
|
---|
1641 | else if (port <= 65535)
|
---|
1642 | {
|
---|
1643 | portent = sh_portchk_get_from_list (proto, port, &saddr, NULL);
|
---|
1644 | if (!portent)
|
---|
1645 | sh_portchk_add_to_list (proto, port, &saddr, NULL, type, SH_PORT_UNKN);
|
---|
1646 | else
|
---|
1647 | {
|
---|
1648 | #ifdef TEST_ONLY
|
---|
1649 | fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, SH_PROTO_STR(proto));
|
---|
1650 | #else
|
---|
1651 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1652 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1653 | _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
|
---|
1654 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1655 | #endif
|
---|
1656 | return -1;
|
---|
1657 | }
|
---|
1658 | }
|
---|
1659 | else
|
---|
1660 | return -1;
|
---|
1661 |
|
---|
1662 | return 0;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | /* Internal interface to add required or optional ports as 'iface:portlist'
|
---|
1666 | */
|
---|
1667 | static int sh_portchk_add_required_generic (const char * str, int type)
|
---|
1668 | {
|
---|
1669 | size_t len;
|
---|
1670 | size_t ll = 0;
|
---|
1671 | int status;
|
---|
1672 |
|
---|
1673 | char * interface = NULL;
|
---|
1674 | char * list;
|
---|
1675 | char * p;
|
---|
1676 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
|
---|
1677 | char * saveptr;
|
---|
1678 | #endif
|
---|
1679 |
|
---|
1680 | if (!str)
|
---|
1681 | return -1;
|
---|
1682 |
|
---|
1683 | if (strchr(str, ':'))
|
---|
1684 | {
|
---|
1685 | len = strlen(str);
|
---|
1686 | for (ll = 0; ll < len; ++ll)
|
---|
1687 | {
|
---|
1688 | if (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
|
---|
1689 | {
|
---|
1690 | interface = SH_ALLOC(ll+1);
|
---|
1691 | sl_strlcpy(interface, str, ll+1);
|
---|
1692 | interface[ll] = '\0';
|
---|
1693 | while (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
|
---|
1694 | ++ll;
|
---|
1695 | break;
|
---|
1696 | }
|
---|
1697 | }
|
---|
1698 | }
|
---|
1699 | else
|
---|
1700 | {
|
---|
1701 | interface = SH_ALLOC(8);
|
---|
1702 | sl_strlcpy(interface, _("0.0.0.0"), 8);
|
---|
1703 | interface[7] = '\0';
|
---|
1704 | while (str[ll] == ' ' || str[ll] == '\t')
|
---|
1705 | ++ll;
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | if (!interface)
|
---|
1709 | return -1;
|
---|
1710 |
|
---|
1711 | if (str[ll] == '\0')
|
---|
1712 | {
|
---|
1713 | SH_FREE(interface);
|
---|
1714 | return -1;
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | if (portchk_debug)
|
---|
1718 | fprintf(stderr, "add ports for interface: %s\n", interface);
|
---|
1719 |
|
---|
1720 | list = sh_util_strdup(&str[ll]);
|
---|
1721 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
|
---|
1722 | p = strtok_r (list, " ,\t", &saveptr);
|
---|
1723 | #else
|
---|
1724 | p = strtok (list, " ,\t");
|
---|
1725 | #endif
|
---|
1726 | if (!p)
|
---|
1727 | {
|
---|
1728 | SH_FREE(interface);
|
---|
1729 | SH_FREE(list);
|
---|
1730 | return -1;
|
---|
1731 | }
|
---|
1732 | while (p)
|
---|
1733 | {
|
---|
1734 | status = sh_portchk_add_required_port_generic (p, interface, type);
|
---|
1735 |
|
---|
1736 | if (-1 == status)
|
---|
1737 | {
|
---|
1738 | SH_FREE(interface);
|
---|
1739 | SH_FREE(list);
|
---|
1740 | return -1;
|
---|
1741 | }
|
---|
1742 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
|
---|
1743 | p = strtok_r (NULL, " ,\t", &saveptr);
|
---|
1744 | #else
|
---|
1745 | p = strtok (NULL, " ,\t");
|
---|
1746 | #endif
|
---|
1747 | }
|
---|
1748 | SH_FREE(interface);
|
---|
1749 | SH_FREE(list);
|
---|
1750 | return 0;
|
---|
1751 | }
|
---|
1752 |
|
---|
1753 | /* User interface to add required ports as 'iface:portlist'
|
---|
1754 | */
|
---|
1755 | static int sh_portchk_add_required (const char * str)
|
---|
1756 | {
|
---|
1757 | return sh_portchk_add_required_generic (str, SH_PORT_REQ);
|
---|
1758 | }
|
---|
1759 |
|
---|
1760 | /* User interface to add optional ports as 'iface:portlist'
|
---|
1761 | */
|
---|
1762 | static int sh_portchk_add_optional (const char * str)
|
---|
1763 | {
|
---|
1764 | return sh_portchk_add_required_generic (str, SH_PORT_OPT);
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | /* User interface to add ignoreable ports as 'iface:portlist'
|
---|
1768 | */
|
---|
1769 | static int sh_portchk_add_ignore (const char * str)
|
---|
1770 | {
|
---|
1771 | return sh_portchk_add_required_generic (str, SH_PORT_IGN);
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | /* User interface to add ports that should not be checked as 'iface:portlist'
|
---|
1775 | */
|
---|
1776 | static int sh_portchk_add_blacklist (const char * str)
|
---|
1777 | {
|
---|
1778 | return sh_portchk_add_required_generic (str, SH_PORT_BLACKLIST);
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | /* Interface to run port check
|
---|
1782 | */
|
---|
1783 | int sh_portchk_check ()
|
---|
1784 | {
|
---|
1785 | volatile int min_port;
|
---|
1786 |
|
---|
1787 | SH_MUTEX_LOCK(mutex_port_check);
|
---|
1788 |
|
---|
1789 | min_port = (sh_portchk_minport == -1) ? 0 : sh_portchk_minport;
|
---|
1790 |
|
---|
1791 | if (sh_portchk_active != S_FALSE)
|
---|
1792 | {
|
---|
1793 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1794 | _("Checking for open ports"),
|
---|
1795 | _("sh_portchk_check"));
|
---|
1796 |
|
---|
1797 | sh_portchk_reset_lists();
|
---|
1798 | if ((0 != geteuid()) && (min_port < 1024))
|
---|
1799 | {
|
---|
1800 | min_port = 1024;
|
---|
1801 | #ifdef TEST_ONLY
|
---|
1802 | fprintf(stderr, "** WARNING not scanning ports < 1024\n");
|
---|
1803 | #else
|
---|
1804 | SH_MUTEX_LOCK(mutex_thread_nolog);
|
---|
1805 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1806 | _("not scanning ports below 1024"),
|
---|
1807 | _("sh_portchk_check"));
|
---|
1808 | SH_MUTEX_UNLOCK(mutex_thread_nolog);
|
---|
1809 | #endif
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | sh_port2proc_prepare();
|
---|
1813 |
|
---|
1814 | if (sh_portchk_check_udp == 1)
|
---|
1815 | sh_portchk_scan_ports_udp(min_port, sh_portchk_maxport);
|
---|
1816 | sh_portchk_scan_ports_tcp(min_port, sh_portchk_maxport);
|
---|
1817 |
|
---|
1818 |
|
---|
1819 | sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_REPORT);
|
---|
1820 | if (sh_portchk_check_udp == 1)
|
---|
1821 | sh_portchk_check_list (&portlist_udp, IPPROTO_UDP, SH_PORT_REPORT);
|
---|
1822 |
|
---|
1823 | }
|
---|
1824 | SH_MUTEX_UNLOCK(mutex_port_check);
|
---|
1825 | return 0;
|
---|
1826 | }
|
---|
1827 | #endif
|
---|
1828 |
|
---|
1829 | #ifdef SH_CUTEST
|
---|
1830 | #include "CuTest.h"
|
---|
1831 |
|
---|
1832 | void Test_portcheck_lists (CuTest *tc)
|
---|
1833 | {
|
---|
1834 | #if defined(SH_USE_PORTCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
|
---|
1835 | struct sh_sockaddr haddr_local;
|
---|
1836 | struct sh_portentry * portent;
|
---|
1837 | char buf[256];
|
---|
1838 | char * p;
|
---|
1839 |
|
---|
1840 | #ifdef HAVE_RPC_RPC_H
|
---|
1841 | p = sh_getrpcbynumber(0, buf, sizeof(buf));
|
---|
1842 | CuAssertTrue(tc, p == NULL);
|
---|
1843 |
|
---|
1844 | p = sh_getrpcbynumber(100000, buf, sizeof(buf));
|
---|
1845 | CuAssertPtrNotNull(tc, p);
|
---|
1846 | CuAssertTrue(tc, (0 == strcmp(p, "portmapper") || 0 == strcmp(p, "rpcbind")));
|
---|
1847 | CuAssertTrue(tc, (0 == strcmp(buf, "portmapper") || 0 == strcmp(p, "rpcbind")));
|
---|
1848 |
|
---|
1849 | p = sh_getrpcbynumber(100007, buf, sizeof(buf));
|
---|
1850 | CuAssertPtrNotNull(tc, p);
|
---|
1851 | CuAssertTrue(tc, 0 == strcmp(p, "ypbind"));
|
---|
1852 | CuAssertTrue(tc, 0 == strcmp(buf, "ypbind"));
|
---|
1853 | #endif
|
---|
1854 |
|
---|
1855 | p = sh_getservbyport(0, SH_PROTO_STR(IPPROTO_UDP), buf, sizeof(buf));
|
---|
1856 | CuAssertTrue(tc, p == NULL);
|
---|
1857 |
|
---|
1858 | #if !defined(HOST_IS_CYGWIN)
|
---|
1859 | p = sh_getservbyport(22, SH_PROTO_STR(IPPROTO_TCP), buf, sizeof(buf));
|
---|
1860 | CuAssertPtrNotNull(tc, p);
|
---|
1861 | CuAssertTrue(tc, 0 == strcmp(p, "ssh"));
|
---|
1862 | CuAssertTrue(tc, 0 == strcmp(buf, "ssh"));
|
---|
1863 | #endif
|
---|
1864 |
|
---|
1865 | p = sh_getservbyport(13, SH_PROTO_STR(IPPROTO_UDP), buf, sizeof(buf));
|
---|
1866 | CuAssertPtrNotNull(tc, p);
|
---|
1867 | CuAssertTrue(tc, 0 == strcmp(p, "daytime"));
|
---|
1868 | CuAssertTrue(tc, 0 == strcmp(buf, "daytime"));
|
---|
1869 |
|
---|
1870 | CuAssertTrue(tc, 0 != sh_ipvx_aton("127.0.0.1", &haddr_local));
|
---|
1871 |
|
---|
1872 | sh_portchk_add_to_list (IPPROTO_TCP, 8000, &haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1873 |
|
---|
1874 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, NULL);
|
---|
1875 | CuAssertPtrNotNull(tc, portent);
|
---|
1876 |
|
---|
1877 | CuAssertTrue(tc, portent->port == 8000);
|
---|
1878 | CuAssertTrue(tc, 0 == strcmp("127.0.0.1", portent->interface));
|
---|
1879 | CuAssertTrue(tc, portent->status == SH_PORT_UNKN);
|
---|
1880 | CuAssertTrue(tc, portent->flag == SH_PORT_NOT);
|
---|
1881 |
|
---|
1882 | sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
|
---|
1883 |
|
---|
1884 | CuAssertTrue(tc, NULL == portlist_tcp);
|
---|
1885 |
|
---|
1886 | sh_portchk_add_to_list (IPPROTO_TCP, 8000, &haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1887 | sh_portchk_add_to_list (IPPROTO_TCP, 8001, &haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1888 | sh_portchk_add_to_list (IPPROTO_TCP, 8002, &haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1889 | sh_portchk_add_to_list (IPPROTO_TCP, 8003, &haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1890 | sh_portchk_add_to_list (IPPROTO_TCP, 8004, &haddr_local, NULL, SH_PORT_IGN, SH_PORT_UNKN);
|
---|
1891 | sh_portchk_add_to_list (IPPROTO_TCP, -1, &haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1892 | sh_portchk_add_to_list (IPPROTO_TCP, -1, &haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1893 | sh_portchk_add_to_list (IPPROTO_TCP, -1, &haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1894 | sh_portchk_add_to_list (IPPROTO_TCP, -1, &haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1895 | sh_portchk_add_to_list (IPPROTO_TCP, -1, &haddr_local, "foo5", SH_PORT_IGN, SH_PORT_UNKN);
|
---|
1896 |
|
---|
1897 | sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
|
---|
1898 |
|
---|
1899 | CuAssertPtrNotNull(tc, portlist_tcp);
|
---|
1900 |
|
---|
1901 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, NULL);
|
---|
1902 | CuAssertPtrNotNull(tc, portent);
|
---|
1903 |
|
---|
1904 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8001, &haddr_local, NULL);
|
---|
1905 | CuAssertTrue(tc, NULL == portent);
|
---|
1906 |
|
---|
1907 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8002, &haddr_local, NULL);
|
---|
1908 | CuAssertPtrNotNull(tc, portent);
|
---|
1909 |
|
---|
1910 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8003, &haddr_local, NULL);
|
---|
1911 | CuAssertTrue(tc, NULL == portent);
|
---|
1912 |
|
---|
1913 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8004, &haddr_local, NULL);
|
---|
1914 | CuAssertPtrNotNull(tc, portent);
|
---|
1915 |
|
---|
1916 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, "foo1");
|
---|
1917 | CuAssertTrue(tc, NULL == portent);
|
---|
1918 |
|
---|
1919 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, "foo2");
|
---|
1920 | CuAssertPtrNotNull(tc, portent);
|
---|
1921 | CuAssertTrue(tc, 0 == strcmp(portent->service, "foo2"));
|
---|
1922 |
|
---|
1923 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, "foo3");
|
---|
1924 | CuAssertTrue(tc, NULL == portent);
|
---|
1925 |
|
---|
1926 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, "foo4");
|
---|
1927 | CuAssertPtrNotNull(tc, portent);
|
---|
1928 | CuAssertTrue(tc, 0 == strcmp(portent->service, "foo4"));
|
---|
1929 |
|
---|
1930 | portent = sh_portchk_get_from_list(IPPROTO_TCP, 8000, &haddr_local, "foo5");
|
---|
1931 | CuAssertPtrNotNull(tc, portent);
|
---|
1932 | CuAssertTrue(tc, 0 == strcmp(portent->service, "foo5"));
|
---|
1933 |
|
---|
1934 | CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, &haddr_local, IPPROTO_TCP));
|
---|
1935 | CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, &haddr_local, IPPROTO_TCP));
|
---|
1936 | CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, &haddr_local, IPPROTO_TCP));
|
---|
1937 | CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, &haddr_local, IPPROTO_TCP));
|
---|
1938 | CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, &haddr_local, IPPROTO_UDP));
|
---|
1939 | CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, &haddr_local, IPPROTO_UDP));
|
---|
1940 | CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, &haddr_local, IPPROTO_UDP));
|
---|
1941 | CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, &haddr_local, IPPROTO_UDP));
|
---|
1942 |
|
---|
1943 | CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, &haddr_local, IPPROTO_UDP));
|
---|
1944 | CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, &haddr_local, IPPROTO_UDP));
|
---|
1945 | CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, &haddr_local, IPPROTO_UDP));
|
---|
1946 | CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, &haddr_local, IPPROTO_UDP));
|
---|
1947 |
|
---|
1948 | CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, &haddr_local, IPPROTO_TCP));
|
---|
1949 | CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, &haddr_local, IPPROTO_TCP));
|
---|
1950 | CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, &haddr_local, IPPROTO_TCP));
|
---|
1951 | CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, &haddr_local, IPPROTO_TCP));
|
---|
1952 | #else
|
---|
1953 | (void) tc; /* fix compiler warning */
|
---|
1954 | #endif
|
---|
1955 | return;
|
---|
1956 | }
|
---|
1957 | #endif
|
---|
1958 |
|
---|
1959 | #ifdef TEST_ONLY
|
---|
1960 |
|
---|
1961 | void usage (char * pname)
|
---|
1962 | {
|
---|
1963 | printf ("%s [-r|--required interface:portlist][-o|--optional interface:portlist][--no-udp][-d|--debug] hostname\n\n", pname);
|
---|
1964 | printf (" Check local host for open ports; Version %s\n\n", PORTCHK_VERSION);
|
---|
1965 | printf (" Interface: Numeric address for an interface, e.g. 127.0.0.1\n");
|
---|
1966 | printf (" Portlist: List of ports or services, e.g. 22/tcp,nfs/udp,nlockmgr/udp\n");
|
---|
1967 | printf (" required -> must be open\n");
|
---|
1968 | printf (" optional -> may be open or closed\n");
|
---|
1969 | printf (" RPC services must be specified with service **name**, others with **port number**\n\n");
|
---|
1970 | printf (" Example:\n");
|
---|
1971 | printf (" %s --required 192.168.1.2:22/tcp,nfs/udp,nlockmgr/udp\n\n", pname);
|
---|
1972 | return;
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | int main(int argc, char *argv[])
|
---|
1976 | {
|
---|
1977 | char * pname = argv[0];
|
---|
1978 |
|
---|
1979 |
|
---|
1980 | /*
|
---|
1981 | test_lists();
|
---|
1982 |
|
---|
1983 | portlist_tcp = sh_portchk_kill_list (portlist_tcp);
|
---|
1984 | portlist_udp = sh_portchk_kill_list (portlist_udp);
|
---|
1985 | */
|
---|
1986 |
|
---|
1987 | /* sh_portchk_add_required ("127.0.0.1 : nlockmgr/tcp, 5308/tcp, nfs/tcp"); */
|
---|
1988 |
|
---|
1989 | while (argc > 1 && argv[1][0] == '-')
|
---|
1990 | {
|
---|
1991 | if (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h"))
|
---|
1992 | {
|
---|
1993 | usage(pname);
|
---|
1994 | exit (0);
|
---|
1995 | }
|
---|
1996 | else if (0 == strcmp(argv[1], "--required") || 0 == strcmp(argv[1], "-r"))
|
---|
1997 | {
|
---|
1998 | if (argc < 3)
|
---|
1999 | {
|
---|
2000 | usage(pname);
|
---|
2001 | exit (1);
|
---|
2002 | }
|
---|
2003 | sh_portchk_add_required (argv[2]);
|
---|
2004 | --argc; ++argv;
|
---|
2005 | }
|
---|
2006 | else if (0 == strcmp(argv[1], "--optional") || 0 == strcmp(argv[1], "-o"))
|
---|
2007 | {
|
---|
2008 | if (argc < 3)
|
---|
2009 | {
|
---|
2010 | usage(pname);
|
---|
2011 | exit (1);
|
---|
2012 | }
|
---|
2013 | sh_portchk_add_optional (argv[2]);
|
---|
2014 | --argc; ++argv;
|
---|
2015 | }
|
---|
2016 | else if (0 == strcmp(argv[1], "--no-udp"))
|
---|
2017 | {
|
---|
2018 | sh_portchk_check_udp = 0;
|
---|
2019 | }
|
---|
2020 | else if (0 == strcmp(argv[1], "--debug") || 0 == strcmp(argv[1], "-d"))
|
---|
2021 | {
|
---|
2022 | portchk_debug = 1;
|
---|
2023 | }
|
---|
2024 | else
|
---|
2025 | {
|
---|
2026 | usage(pname);
|
---|
2027 | exit (1);
|
---|
2028 | }
|
---|
2029 | --argc; ++argv;
|
---|
2030 | }
|
---|
2031 |
|
---|
2032 | if (argc < 2)
|
---|
2033 | {
|
---|
2034 | usage(pname);
|
---|
2035 | exit (1);
|
---|
2036 | }
|
---|
2037 |
|
---|
2038 | portchk_hostname = argv[1];
|
---|
2039 |
|
---|
2040 | if (0 != sh_portchk_init ())
|
---|
2041 | {
|
---|
2042 | usage(pname);
|
---|
2043 | exit (1);
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | sh_portchk_check();
|
---|
2047 |
|
---|
2048 | return 0;
|
---|
2049 | }
|
---|
2050 | #endif
|
---|