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 |
|
---|
42 | #define PORTCHK_VERSION "1.0"
|
---|
43 |
|
---|
44 | #if defined(TEST_ONLY) || (defined(SH_USE_PORTCHECK) && (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)))
|
---|
45 |
|
---|
46 |
|
---|
47 | #define PORTMAP
|
---|
48 | #include <rpc/rpc.h>
|
---|
49 | #ifdef HAVE_RPC_RPCENT_H
|
---|
50 | #include <rpc/rpcent.h>
|
---|
51 | #endif
|
---|
52 | #include <rpc/pmap_clnt.h>
|
---|
53 | #include <rpc/pmap_prot.h>
|
---|
54 | #include <netdb.h>
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * struct pmaplist {
|
---|
58 | * struct pmap pml_map;
|
---|
59 | * struct pmaplist *pml_next;
|
---|
60 | * };
|
---|
61 | */
|
---|
62 |
|
---|
63 | /* struct pmap {
|
---|
64 | * long unsigned pm_prog;
|
---|
65 | * long unsigned pm_vers;
|
---|
66 | * long unsigned pm_prot;
|
---|
67 | * long unsigned pm_port;
|
---|
68 | * };
|
---|
69 | */
|
---|
70 |
|
---|
71 | /* TIME_WAIT ? 60-240 seconds */
|
---|
72 |
|
---|
73 | /* the size of an interface string
|
---|
74 | */
|
---|
75 | #define SH_INTERFACE_SIZE 16
|
---|
76 |
|
---|
77 | #define SH_PORT_NOT 0
|
---|
78 | #define SH_PORT_REQ 1
|
---|
79 | #define SH_PORT_OPT 2
|
---|
80 |
|
---|
81 | #define SH_PORT_MISS 0
|
---|
82 | #define SH_PORT_ISOK 1
|
---|
83 | #define SH_PORT_UNKN 2
|
---|
84 |
|
---|
85 | #define SH_PORT_NOREPT 0
|
---|
86 | #define SH_PORT_REPORT 1
|
---|
87 |
|
---|
88 | struct sh_portentry {
|
---|
89 | int port;
|
---|
90 | char interface[SH_INTERFACE_SIZE];
|
---|
91 | char * service;
|
---|
92 | char * error;
|
---|
93 | int flag; /* required or not */
|
---|
94 | int status; /* missing or not */
|
---|
95 | struct sh_portentry * next;
|
---|
96 | };
|
---|
97 |
|
---|
98 | static struct sh_portentry * portlist_tcp = NULL;
|
---|
99 | static struct sh_portentry * portlist_udp = NULL;
|
---|
100 |
|
---|
101 | #define SH_PORTCHK_INTERVAL 300
|
---|
102 |
|
---|
103 | static int sh_portchk_check_udp = 1;
|
---|
104 | static int sh_portchk_active = 1;
|
---|
105 | static int sh_portchk_interval = SH_PORTCHK_INTERVAL;
|
---|
106 | #if !defined(TEST_ONLY)
|
---|
107 |
|
---|
108 | #define FIL__ _("sh_portcheck.c")
|
---|
109 | #include "samhain.h"
|
---|
110 | #include "sh_error.h"
|
---|
111 | #include "sh_mem.h"
|
---|
112 | #include "sh_utils.h"
|
---|
113 | #include "sh_modules.h"
|
---|
114 |
|
---|
115 | static int sh_portchk_severity = SH_ERR_SEVERE;
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | /* Exported interface to add required ports as 'iface:portlist'
|
---|
119 | */
|
---|
120 | static int sh_portchk_add_required (const char * str);
|
---|
121 |
|
---|
122 | /* Exported interface to add optional ports as 'iface:portlist'
|
---|
123 | */
|
---|
124 | static int sh_portchk_add_optional (const char * str);
|
---|
125 |
|
---|
126 | /* Exported interface to add an ethernet interface
|
---|
127 | */
|
---|
128 | static int sh_portchk_add_interface (const char * str);
|
---|
129 |
|
---|
130 |
|
---|
131 | #ifndef TEST_ONLY
|
---|
132 |
|
---|
133 | static int sh_portchk_set_interval (const char * c)
|
---|
134 | {
|
---|
135 | int retval = 0;
|
---|
136 | long val;
|
---|
137 |
|
---|
138 | SL_ENTER(_("sh_portchk_set_interval"));
|
---|
139 | val = strtol (c, (char **)NULL, 10);
|
---|
140 | if (val <= 0)
|
---|
141 | {
|
---|
142 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
143 | _("port check interval"), c);
|
---|
144 | retval = -1;
|
---|
145 | }
|
---|
146 |
|
---|
147 | val = (val <= 0 ? 60 : val);
|
---|
148 |
|
---|
149 | sh_portchk_interval = (time_t) val;
|
---|
150 | SL_RETURN(0, _("sh_portchk_set_interval"));
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | static int sh_portchk_set_active (const char * str)
|
---|
155 | {
|
---|
156 | return sh_util_flagval(str, &sh_portchk_active);
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int sh_portchk_set_udp (const char * str)
|
---|
160 | {
|
---|
161 | return sh_util_flagval(str, &sh_portchk_check_udp);
|
---|
162 | }
|
---|
163 |
|
---|
164 | static int sh_portchk_set_severity (const char * str)
|
---|
165 | {
|
---|
166 | char tmp[32];
|
---|
167 | tmp[0] = '='; tmp[1] = '\0';
|
---|
168 | sl_strlcat (tmp, str, 32);
|
---|
169 | return sh_error_set_level (tmp, &sh_portchk_severity);
|
---|
170 | }
|
---|
171 |
|
---|
172 | sh_rconf sh_portchk_table[] = {
|
---|
173 | {
|
---|
174 | N_("severityportcheck"),
|
---|
175 | sh_portchk_set_severity,
|
---|
176 | },
|
---|
177 | {
|
---|
178 | N_("portcheckrequired"),
|
---|
179 | sh_portchk_add_required,
|
---|
180 | },
|
---|
181 | {
|
---|
182 | N_("portcheckoptional"),
|
---|
183 | sh_portchk_add_optional,
|
---|
184 | },
|
---|
185 | {
|
---|
186 | N_("portcheckactive"),
|
---|
187 | sh_portchk_set_active,
|
---|
188 | },
|
---|
189 | {
|
---|
190 | N_("portcheckinterface"),
|
---|
191 | sh_portchk_add_interface,
|
---|
192 | },
|
---|
193 | {
|
---|
194 | N_("portcheckinterval"),
|
---|
195 | sh_portchk_set_interval,
|
---|
196 | },
|
---|
197 | {
|
---|
198 | N_("portcheckudp"),
|
---|
199 | sh_portchk_set_udp,
|
---|
200 | },
|
---|
201 | {
|
---|
202 | NULL,
|
---|
203 | NULL
|
---|
204 | }
|
---|
205 | };
|
---|
206 |
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | /* Interface to initialize port check
|
---|
210 | */
|
---|
211 | int sh_portchk_init ();
|
---|
212 |
|
---|
213 | /* Interface to reset port check
|
---|
214 | */
|
---|
215 | int sh_portchk_reset ();
|
---|
216 |
|
---|
217 | /* Interface to run port check
|
---|
218 | */
|
---|
219 | int sh_portchk_check ();
|
---|
220 |
|
---|
221 |
|
---|
222 | static char * check_services (int port, char * proto);
|
---|
223 |
|
---|
224 | #ifdef TEST_ONLY
|
---|
225 |
|
---|
226 | static int portchk_debug = 0;
|
---|
227 | #define SH_ALLOC malloc
|
---|
228 | #define SH_FREE free
|
---|
229 | #define sh_util_strdup strdup
|
---|
230 | #define sl_strlcpy strncpy
|
---|
231 | #define _(a) a
|
---|
232 |
|
---|
233 | #else
|
---|
234 |
|
---|
235 | static int portchk_debug = 0;
|
---|
236 |
|
---|
237 | #endif
|
---|
238 |
|
---|
239 | static void sh_portchk_add_to_list (char * proto,
|
---|
240 | int port, struct in_addr haddr, char * service,
|
---|
241 | int flag, int status)
|
---|
242 | {
|
---|
243 | struct sh_portentry * new = SH_ALLOC (sizeof(struct sh_portentry));
|
---|
244 |
|
---|
245 | if (portchk_debug)
|
---|
246 | fprintf(stderr, _("add to list: port %d/%s %d %d (%s)\n"),
|
---|
247 | port, proto, flag, status, service ? service : _("undef"));
|
---|
248 |
|
---|
249 | new->port = port;
|
---|
250 | sl_strlcpy (new->interface, inet_ntoa(haddr), SH_INTERFACE_SIZE);
|
---|
251 | new->status = status;
|
---|
252 | new->flag = flag;
|
---|
253 |
|
---|
254 | new->error = NULL;
|
---|
255 |
|
---|
256 | if (service)
|
---|
257 | new->service = sh_util_strdup (service);
|
---|
258 | else
|
---|
259 | new->service = NULL;
|
---|
260 | if (0 == strcmp(proto, "tcp"))
|
---|
261 | {
|
---|
262 | new->next = portlist_tcp;
|
---|
263 | portlist_tcp = new;
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | new->next = portlist_udp;
|
---|
268 | portlist_udp = new;
|
---|
269 | }
|
---|
270 | return;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* Reset the list by setting all entries to UNKN.
|
---|
274 | * In the next cycle we will check, and set found ports to ISOK.
|
---|
275 | * Thereafter, we check for entries that are still UNKN.
|
---|
276 | */
|
---|
277 | static void sh_portchk_reset_lists ()
|
---|
278 | {
|
---|
279 | struct sh_portentry * portlist;
|
---|
280 |
|
---|
281 | portlist = portlist_tcp;
|
---|
282 | while (portlist)
|
---|
283 | {
|
---|
284 | if (portlist->status != SH_PORT_MISS)
|
---|
285 | portlist->status = SH_PORT_UNKN;
|
---|
286 | portlist = portlist->next;
|
---|
287 | }
|
---|
288 | portlist = portlist_udp;
|
---|
289 | while (portlist)
|
---|
290 | {
|
---|
291 | if (portlist->status != SH_PORT_MISS)
|
---|
292 | portlist->status = SH_PORT_UNKN;
|
---|
293 | portlist = portlist->next;
|
---|
294 | }
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|
298 | static struct sh_portentry * sh_portchk_kill_list (struct sh_portentry * head)
|
---|
299 | {
|
---|
300 | if (head)
|
---|
301 | {
|
---|
302 | if (head->next)
|
---|
303 | sh_portchk_kill_list (head->next);
|
---|
304 |
|
---|
305 | if (head->service)
|
---|
306 | SH_FREE(head->service);
|
---|
307 | SH_FREE(head);
|
---|
308 | }
|
---|
309 | return NULL;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* check the list of open ports for any that are marked as UNKN
|
---|
313 | */
|
---|
314 | static void sh_portchk_check_list (struct sh_portentry ** head, char * proto, int report)
|
---|
315 | {
|
---|
316 | struct sh_portentry * ptr = *head;
|
---|
317 | struct sh_portentry * pre = *head;
|
---|
318 | char errbuf[256];
|
---|
319 |
|
---|
320 | while (ptr)
|
---|
321 | {
|
---|
322 | if (portchk_debug && report)
|
---|
323 | fprintf(stderr, _("check list: port %d/%s %d %d\n"),
|
---|
324 | ptr->port, proto, ptr->flag, ptr->status);
|
---|
325 |
|
---|
326 | if (ptr->status == SH_PORT_UNKN)
|
---|
327 | {
|
---|
328 | /* Don't report missing ports that are marked as optional
|
---|
329 | */
|
---|
330 | if (ptr->flag != SH_PORT_OPT)
|
---|
331 | {
|
---|
332 | snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceMissing] port %s:%d/%s (%s)"),
|
---|
333 | ptr->interface, ptr->port, proto,
|
---|
334 | ptr->service ? ptr->service : check_services(ptr->port, proto));
|
---|
335 | #ifdef TEST_ONLY
|
---|
336 | if (report == SH_PORT_REPORT)
|
---|
337 | fprintf(stderr, _("%s\n"), errbuf);
|
---|
338 | #else
|
---|
339 | if (report == SH_PORT_REPORT)
|
---|
340 | sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
|
---|
341 | MSG_PORT_REPORT, errbuf);
|
---|
342 | #endif
|
---|
343 | }
|
---|
344 |
|
---|
345 | ptr->status = SH_PORT_MISS;
|
---|
346 |
|
---|
347 | if ((ptr->flag != SH_PORT_REQ) && (ptr->flag != SH_PORT_OPT))
|
---|
348 | {
|
---|
349 | if (portchk_debug && report)
|
---|
350 | fprintf(stderr, _("removing: port %d/%s %d %d\n"),
|
---|
351 | ptr->port, proto, ptr->flag, ptr->status);
|
---|
352 |
|
---|
353 | if (ptr == *head)
|
---|
354 | {
|
---|
355 | *head = ptr->next;
|
---|
356 | if (ptr->service)
|
---|
357 | SH_FREE(ptr->service);
|
---|
358 | SH_FREE(ptr);
|
---|
359 | ptr = *head;
|
---|
360 | pre = *head;
|
---|
361 | continue;
|
---|
362 | }
|
---|
363 | else if (ptr->next == NULL)
|
---|
364 | {
|
---|
365 | pre->next = NULL;
|
---|
366 | if (ptr->service)
|
---|
367 | SH_FREE(ptr->service);
|
---|
368 | SH_FREE(ptr);
|
---|
369 | return;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | pre->next = ptr->next;
|
---|
374 | if (ptr->service)
|
---|
375 | SH_FREE(ptr->service);
|
---|
376 | SH_FREE(ptr);
|
---|
377 | ptr = pre->next;
|
---|
378 | continue;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | }
|
---|
382 | pre = ptr;
|
---|
383 | ptr = ptr->next;
|
---|
384 | }
|
---|
385 | return;
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | static struct sh_portentry * sh_portchk_get_from_list (char * proto, int port,
|
---|
390 | struct in_addr haddr, char * service)
|
---|
391 | {
|
---|
392 | struct sh_portentry * portlist;
|
---|
393 | char iface_all[8];
|
---|
394 |
|
---|
395 | sl_strlcpy (iface_all, _("0.0.0.0"), sizeof(iface_all));
|
---|
396 |
|
---|
397 | if (0 == strcmp(proto, "tcp"))
|
---|
398 | portlist = portlist_tcp;
|
---|
399 | else
|
---|
400 | portlist = portlist_udp;
|
---|
401 |
|
---|
402 | if (service)
|
---|
403 | {
|
---|
404 | while (portlist)
|
---|
405 | {
|
---|
406 | if (portlist->service &&
|
---|
407 | 0 == strcmp(service, portlist->service) &&
|
---|
408 | (0 == strcmp(portlist->interface, inet_ntoa(haddr)) ||
|
---|
409 | 0 == strcmp(portlist->interface, iface_all)))
|
---|
410 | return portlist;
|
---|
411 | portlist = portlist->next;
|
---|
412 | }
|
---|
413 | }
|
---|
414 | else
|
---|
415 | {
|
---|
416 | while (portlist)
|
---|
417 | {
|
---|
418 | if (port == portlist->port &&
|
---|
419 | (0 == strcmp(portlist->interface, inet_ntoa(haddr)) ||
|
---|
420 | 0 == strcmp(portlist->interface, iface_all)))
|
---|
421 | return portlist;
|
---|
422 | portlist = portlist->next;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | return NULL;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | static void sh_portchk_cmp_to_list (char * proto, int port, struct in_addr haddr, char * service)
|
---|
430 | {
|
---|
431 | struct sh_portentry * portent;
|
---|
432 | char errbuf[256];
|
---|
433 |
|
---|
434 |
|
---|
435 | portent = sh_portchk_get_from_list (proto, port, haddr, service);
|
---|
436 |
|
---|
437 | if (service)
|
---|
438 | {
|
---|
439 | if (!portent)
|
---|
440 | {
|
---|
441 | snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceNew] port %s:%d/%s (%s)"),
|
---|
442 | inet_ntoa(haddr), port, proto, service);
|
---|
443 | #ifdef TEST_ONLY
|
---|
444 | fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
|
---|
445 | inet_ntoa(haddr), port, proto, service);
|
---|
446 | #else
|
---|
447 | sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
|
---|
448 | MSG_PORT_REPORT, errbuf);
|
---|
449 | #endif
|
---|
450 | /*
|
---|
451 | * was not there, thus it is not in 'required' or 'optional' list
|
---|
452 | */
|
---|
453 | sh_portchk_add_to_list (proto, port, haddr, service, SH_PORT_NOT, SH_PORT_ISOK);
|
---|
454 | }
|
---|
455 | else if (portent->status == SH_PORT_MISS)
|
---|
456 | {
|
---|
457 | snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceRestarted] port %s:%d/%s to %d/%s (%s)"),
|
---|
458 | inet_ntoa(haddr), portent->port, proto, port, proto, service);
|
---|
459 | #ifdef TEST_ONLY
|
---|
460 | fprintf(stderr, _("service: %s\n"), errbuf);
|
---|
461 | #else
|
---|
462 | sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
|
---|
463 | MSG_PORT_REPORT, errbuf);
|
---|
464 | #endif
|
---|
465 |
|
---|
466 | portent->status = SH_PORT_ISOK;
|
---|
467 | }
|
---|
468 | else if (port != portent->port && (-1) != portent->port)
|
---|
469 | {
|
---|
470 | snprintf (errbuf, sizeof(errbuf), _("POLICY [ServicePortSwitch] port %s:%d/%s to %d/%s (%s)"),
|
---|
471 | inet_ntoa(haddr), portent->port, proto, port, proto, service);
|
---|
472 | #ifdef TEST_ONLY
|
---|
473 | fprintf(stderr, _("service: %s\n"), errbuf);
|
---|
474 | #else
|
---|
475 | sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
|
---|
476 | MSG_PORT_REPORT, errbuf);
|
---|
477 | #endif
|
---|
478 |
|
---|
479 | portent->status = SH_PORT_ISOK;
|
---|
480 | }
|
---|
481 | else
|
---|
482 | {
|
---|
483 | portent->status = SH_PORT_ISOK;
|
---|
484 | }
|
---|
485 | }
|
---|
486 | else
|
---|
487 | {
|
---|
488 | if (!portent)
|
---|
489 | {
|
---|
490 | snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceNew] port %s:%d/%s (%s)"),
|
---|
491 | inet_ntoa(haddr), port, proto, check_services(port, proto));
|
---|
492 | #ifdef TEST_ONLY
|
---|
493 | fprintf(stderr, _("open port: %s:%d/%s (%s)\n"),
|
---|
494 | inet_ntoa(haddr), port, proto, check_services(port, proto));
|
---|
495 | #else
|
---|
496 | sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
|
---|
497 | MSG_PORT_REPORT, errbuf);
|
---|
498 | #endif
|
---|
499 |
|
---|
500 | /* was not there, thus it is not in 'required' or 'optional' list
|
---|
501 | */
|
---|
502 | sh_portchk_add_to_list (proto, port, haddr, service, SH_PORT_NOT, SH_PORT_ISOK);
|
---|
503 | }
|
---|
504 | else if (portent->status == SH_PORT_MISS)
|
---|
505 | {
|
---|
506 | snprintf (errbuf, sizeof(errbuf), _("POLICY [ServiceRestarted] port %s:%d/%s (%s)"),
|
---|
507 | inet_ntoa(haddr), port, proto, check_services(port, proto));
|
---|
508 | #ifdef TEST_ONLY
|
---|
509 | fprintf(stderr, _("port : %s\n"), errbuf);
|
---|
510 | #else
|
---|
511 | sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0,
|
---|
512 | MSG_PORT_REPORT, errbuf);
|
---|
513 | #endif
|
---|
514 |
|
---|
515 | portent->status = SH_PORT_ISOK;
|
---|
516 | }
|
---|
517 | else
|
---|
518 | {
|
---|
519 | portent->status = SH_PORT_ISOK;
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | return;
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | /* Returns a static buffer containing the name of the service
|
---|
528 | * running on port <port> (from /etc/services)
|
---|
529 | * Returns NULL on failure
|
---|
530 | */
|
---|
531 | static char * check_services (int port, char * proto)
|
---|
532 | {
|
---|
533 | static char buf[256];
|
---|
534 | struct servent * service = getservbyport(htons(port), proto);
|
---|
535 |
|
---|
536 | if (service && service->s_name && service->s_name[0] != '\0')
|
---|
537 | {
|
---|
538 | snprintf (buf, sizeof(buf), _("maybe_%s"), service->s_name);
|
---|
539 | }
|
---|
540 | else
|
---|
541 | {
|
---|
542 | snprintf (buf, sizeof(buf), _("unknown"));
|
---|
543 | }
|
---|
544 | return buf;
|
---|
545 | }
|
---|
546 |
|
---|
547 | /* Returns a static buffer containing the name of the service
|
---|
548 | * running on port <port> at <address> (from portmap daemon)
|
---|
549 | * Returns NULL on failure
|
---|
550 | */
|
---|
551 | static char * check_rpc_list (int port, struct sockaddr_in * address,
|
---|
552 | unsigned long prot)
|
---|
553 | {
|
---|
554 | struct pmaplist * head;
|
---|
555 | struct rpcent *r;
|
---|
556 | static char buf[256];
|
---|
557 |
|
---|
558 | head = pmap_getmaps(address);
|
---|
559 |
|
---|
560 | if (head)
|
---|
561 | {
|
---|
562 | do /* while (head != NULL) */
|
---|
563 | {
|
---|
564 | if ((head->pml_map.pm_prot == prot) &&
|
---|
565 | (port == (int)head->pml_map.pm_port))
|
---|
566 | {
|
---|
567 | r = getrpcbynumber((int)head->pml_map.pm_prog);
|
---|
568 | if (r && r->r_name && r->r_name[0] != '\0')
|
---|
569 | {
|
---|
570 | snprintf (buf, sizeof(buf), "%s", r->r_name);
|
---|
571 | return buf;
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | snprintf (buf, sizeof(buf), "RPC_%lu",
|
---|
576 | (unsigned long)head->pml_map.pm_prog);
|
---|
577 | return buf;
|
---|
578 | }
|
---|
579 | }
|
---|
580 | head = head->pml_next;
|
---|
581 | }
|
---|
582 | while (head != NULL);
|
---|
583 | }
|
---|
584 |
|
---|
585 | return NULL;
|
---|
586 | }
|
---|
587 |
|
---|
588 | static int check_port_udp_internal (int fd, int port, struct in_addr haddr)
|
---|
589 | {
|
---|
590 | struct sockaddr_in sinr;
|
---|
591 | /* struct in_addr haddr; */
|
---|
592 | int retval;
|
---|
593 | char * p;
|
---|
594 | char buf[8];
|
---|
595 |
|
---|
596 |
|
---|
597 | /* inet_aton(interface, &haddr); */
|
---|
598 |
|
---|
599 | sinr.sin_family = AF_INET;
|
---|
600 | sinr.sin_port = htons (port);
|
---|
601 | sinr.sin_addr = haddr;
|
---|
602 |
|
---|
603 | retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
|
---|
604 | if (retval == -1)
|
---|
605 | {
|
---|
606 | #ifdef TEST_ONLY
|
---|
607 | if (portchk_debug)
|
---|
608 | perror(_("connect"));
|
---|
609 | #else
|
---|
610 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
611 | sh_error_message(errno), _("connect"));
|
---|
612 | #endif
|
---|
613 | }
|
---|
614 | else
|
---|
615 | {
|
---|
616 | retval = send (fd, buf, 0, 0);
|
---|
617 |
|
---|
618 | if (retval == -1 && errno == ECONNREFUSED)
|
---|
619 | {
|
---|
620 | if (portchk_debug)
|
---|
621 | fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
|
---|
622 | port, inet_ntoa(haddr));
|
---|
623 | }
|
---|
624 | else
|
---|
625 | {
|
---|
626 | retval = send (fd, buf, 0, 0);
|
---|
627 | if (retval == -1 && errno == ECONNREFUSED)
|
---|
628 | {
|
---|
629 | if (portchk_debug)
|
---|
630 | fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
|
---|
631 | port, inet_ntoa(haddr));
|
---|
632 | }
|
---|
633 | else if (retval != -1)
|
---|
634 | {
|
---|
635 | /* Try to get service name from portmap
|
---|
636 | */
|
---|
637 | p = check_rpc_list (port, &sinr, IPPROTO_UDP);
|
---|
638 |
|
---|
639 | sh_portchk_cmp_to_list ("udp", port, haddr, p ? p : NULL);
|
---|
640 |
|
---|
641 | /* If not an RPC service, try to get name from /etc/services
|
---|
642 | */
|
---|
643 | if (!p)
|
---|
644 | p = check_services(port, "udp");
|
---|
645 |
|
---|
646 | if (portchk_debug)
|
---|
647 | fprintf(stderr, _("check port: %5d/udp on %15s open %s\n"),
|
---|
648 | port, inet_ntoa(haddr), p);
|
---|
649 |
|
---|
650 | }
|
---|
651 | }
|
---|
652 | }
|
---|
653 | close (fd);
|
---|
654 | return 0;
|
---|
655 | }
|
---|
656 |
|
---|
657 | static int check_port_tcp_internal (int fd, int port, struct in_addr haddr)
|
---|
658 | {
|
---|
659 | struct sockaddr_in sinr;
|
---|
660 | /* struct in_addr haddr; */
|
---|
661 | int retval;
|
---|
662 | char * p;
|
---|
663 |
|
---|
664 |
|
---|
665 | /* inet_aton(interface, &haddr); */
|
---|
666 |
|
---|
667 | sinr.sin_family = AF_INET;
|
---|
668 | sinr.sin_port = htons (port);
|
---|
669 | sinr.sin_addr = haddr;
|
---|
670 |
|
---|
671 | retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
|
---|
672 | if (retval == -1 && errno == ECONNREFUSED)
|
---|
673 | {
|
---|
674 | if (portchk_debug)
|
---|
675 | fprintf(stderr, _("check port: %5d on %15s established/time_wait\n"),
|
---|
676 | port, inet_ntoa(haddr));
|
---|
677 | }
|
---|
678 | else if (retval == -1)
|
---|
679 | {
|
---|
680 | #ifdef TEST_ONLY
|
---|
681 | if (portchk_debug)
|
---|
682 | perror(_("connect"));
|
---|
683 | #else
|
---|
684 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
685 | sh_error_message(errno), _("connect"));
|
---|
686 | #endif
|
---|
687 | }
|
---|
688 | else
|
---|
689 | {
|
---|
690 | /* Try to get service name from portmap
|
---|
691 | */
|
---|
692 | p = check_rpc_list (port, &sinr, IPPROTO_TCP);
|
---|
693 |
|
---|
694 | sh_portchk_cmp_to_list ("tcp", port, haddr, p ? p : NULL);
|
---|
695 |
|
---|
696 | /* If not an RPC service, try to get name from /etc/services
|
---|
697 | */
|
---|
698 | if (!p)
|
---|
699 | p = check_services(port, "tcp");
|
---|
700 |
|
---|
701 | if (portchk_debug)
|
---|
702 | fprintf(stderr, _("check port: %5d on %15s open %s\n"),
|
---|
703 | port, inet_ntoa(haddr), p);
|
---|
704 | }
|
---|
705 | close (fd);
|
---|
706 | return 0;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /* typedef uint32_t in_addr_t;
|
---|
710 | * struct in_addr
|
---|
711 | * {
|
---|
712 | * in_addr_t s_addr;
|
---|
713 | * };
|
---|
714 | */
|
---|
715 |
|
---|
716 | #define SH_IFACE_MAX 16
|
---|
717 |
|
---|
718 | struct portchk_interfaces {
|
---|
719 | struct in_addr iface[SH_IFACE_MAX];
|
---|
720 | int used;
|
---|
721 | };
|
---|
722 |
|
---|
723 | static struct portchk_interfaces iface_list;
|
---|
724 | static int iface_initialized = 0;
|
---|
725 |
|
---|
726 | #ifdef TEST_ONLY
|
---|
727 | static char * portchk_hostname = NULL;
|
---|
728 | #else
|
---|
729 | static char * portchk_hostname = sh.host.name;
|
---|
730 | #endif
|
---|
731 |
|
---|
732 | int sh_portchk_init ()
|
---|
733 | {
|
---|
734 | struct hostent * hent;
|
---|
735 | int i = 0;
|
---|
736 | char errbuf[256];
|
---|
737 |
|
---|
738 | if (portchk_debug)
|
---|
739 | fprintf(stderr, _("checking ports on: %s\n"), portchk_hostname ? portchk_hostname : _("NULL"));
|
---|
740 | if (!portchk_hostname)
|
---|
741 | return -1;
|
---|
742 |
|
---|
743 | if (iface_initialized == 0)
|
---|
744 | {
|
---|
745 | iface_list.used = 0;
|
---|
746 | iface_initialized = 1;
|
---|
747 | }
|
---|
748 |
|
---|
749 | hent = gethostbyname(portchk_hostname);
|
---|
750 |
|
---|
751 | while (hent && hent->h_addr_list[i] && (iface_list.used < SH_IFACE_MAX))
|
---|
752 | {
|
---|
753 | memcpy (&(iface_list.iface[iface_list.used].s_addr), hent->h_addr_list[i], sizeof(in_addr_t));
|
---|
754 | sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"),
|
---|
755 | inet_ntoa(iface_list.iface[iface_list.used]));
|
---|
756 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
757 | errbuf, _("sh_portchk_init"));
|
---|
758 |
|
---|
759 | ++iface_list.used;
|
---|
760 | ++i;
|
---|
761 | }
|
---|
762 |
|
---|
763 | return 0;
|
---|
764 | }
|
---|
765 |
|
---|
766 | #if !defined(TEST_ONLY)
|
---|
767 | int sh_portchk_reconf ()
|
---|
768 | {
|
---|
769 | iface_initialized = 0;
|
---|
770 |
|
---|
771 | portlist_udp = sh_portchk_kill_list (portlist_udp);
|
---|
772 | portlist_tcp = sh_portchk_kill_list (portlist_tcp);
|
---|
773 | return 0;
|
---|
774 | }
|
---|
775 |
|
---|
776 | int sh_portchk_cleanup ()
|
---|
777 | {
|
---|
778 | return sh_portchk_reconf ();
|
---|
779 | }
|
---|
780 |
|
---|
781 | int sh_portchk_timer (time_t tcurrent)
|
---|
782 | {
|
---|
783 | static time_t lastcheck = 0;
|
---|
784 |
|
---|
785 | SL_ENTER(_("sh_portchk_timer"));
|
---|
786 | if ((time_t) (tcurrent - lastcheck) >= sh_portchk_interval)
|
---|
787 | {
|
---|
788 | lastcheck = tcurrent;
|
---|
789 | SL_RETURN((-1), _("sh_portchk_timer"));
|
---|
790 | }
|
---|
791 | SL_RETURN(0, _("sh_portchk_timer"));
|
---|
792 | }
|
---|
793 | #endif
|
---|
794 |
|
---|
795 | static int check_port_generic (int port, int type, int protocol)
|
---|
796 | {
|
---|
797 | int i = 0;
|
---|
798 | int sock = -1;
|
---|
799 | int flag = 1; /* non-zero to enable an option */
|
---|
800 | struct in_addr haddr;
|
---|
801 |
|
---|
802 | /* Check all interfaces for this host
|
---|
803 | */
|
---|
804 | while (i < iface_list.used)
|
---|
805 | {
|
---|
806 | if ((sock = socket(AF_INET, type, protocol)) < 0 )
|
---|
807 | {
|
---|
808 | #ifdef TEST_ONLY
|
---|
809 | if (portchk_debug)
|
---|
810 | perror(_("socket"));
|
---|
811 | #else
|
---|
812 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
813 | sh_error_message(errno), _("socket"));
|
---|
814 | #endif
|
---|
815 | }
|
---|
816 | if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
---|
817 | (void *) &flag, sizeof(flag)) < 0 )
|
---|
818 | {
|
---|
819 | #ifdef TEST_ONLY
|
---|
820 | if (portchk_debug)
|
---|
821 | perror(_("setsockopt"));
|
---|
822 | #else
|
---|
823 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
824 | sh_error_message(errno), _("setsockopt"));
|
---|
825 | #endif
|
---|
826 | }
|
---|
827 |
|
---|
828 | memcpy (&(haddr.s_addr), &(iface_list.iface[i].s_addr), sizeof(in_addr_t));
|
---|
829 |
|
---|
830 | if (protocol == IPPROTO_TCP)
|
---|
831 | check_port_tcp_internal(sock, port, haddr);
|
---|
832 | else
|
---|
833 | check_port_udp_internal(sock, port, haddr);
|
---|
834 |
|
---|
835 | ++i;
|
---|
836 | }
|
---|
837 |
|
---|
838 | return 0;
|
---|
839 | }
|
---|
840 |
|
---|
841 |
|
---|
842 |
|
---|
843 | static int check_port_udp (int port)
|
---|
844 | {
|
---|
845 | return check_port_generic(port, SOCK_DGRAM, IPPROTO_UDP);
|
---|
846 | }
|
---|
847 |
|
---|
848 | static int check_port_tcp (int port)
|
---|
849 | {
|
---|
850 | return check_port_generic(port, SOCK_STREAM, IPPROTO_TCP);
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 |
|
---|
855 | static int sh_portchk_scan_ports_generic (int min_port, int max_port, int type, int protocol)
|
---|
856 | {
|
---|
857 | /*
|
---|
858 | int min_port = 1024;
|
---|
859 | int max_port = 65535;
|
---|
860 | */
|
---|
861 |
|
---|
862 | int port;
|
---|
863 | int retval;
|
---|
864 | int sock = -1;
|
---|
865 | int flag = 1; /* non-zero to enable an option */
|
---|
866 |
|
---|
867 | struct sockaddr_in addr;
|
---|
868 | int addrlen = sizeof(addr);
|
---|
869 |
|
---|
870 | if (min_port == -1)
|
---|
871 | min_port = 0;
|
---|
872 | if (max_port == -1)
|
---|
873 | max_port = 65535;
|
---|
874 |
|
---|
875 | for (port = min_port; port <= max_port; ++port)
|
---|
876 | {
|
---|
877 |
|
---|
878 | if ((sock = socket(AF_INET, type, protocol)) < 0 )
|
---|
879 | {
|
---|
880 | #ifdef TEST_ONLY
|
---|
881 | if (portchk_debug)
|
---|
882 | perror(_("socket"));
|
---|
883 | #else
|
---|
884 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
885 | sh_error_message(errno), _("socket"));
|
---|
886 | #endif
|
---|
887 | }
|
---|
888 | if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
---|
889 | (void *) &flag, sizeof(flag)) < 0 )
|
---|
890 | {
|
---|
891 | #ifdef TEST_ONLY
|
---|
892 | if (portchk_debug)
|
---|
893 | perror(_("setsockopt"));
|
---|
894 | #else
|
---|
895 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
896 | sh_error_message(errno), _("setsockopt"));
|
---|
897 | #endif
|
---|
898 | }
|
---|
899 |
|
---|
900 | addr.sin_family = AF_INET;
|
---|
901 | addr.sin_port = htons(port);
|
---|
902 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
903 |
|
---|
904 | retval = bind (sock, (struct sockaddr *) &addr, addrlen);
|
---|
905 |
|
---|
906 | if (retval == 0)
|
---|
907 | {
|
---|
908 | /* we can bind the port, thus it is unused
|
---|
909 | */
|
---|
910 | close (sock);
|
---|
911 | }
|
---|
912 | else
|
---|
913 | {
|
---|
914 | if (errno == EINVAL || errno == EADDRINUSE)
|
---|
915 | {
|
---|
916 | /* try to connect to the port
|
---|
917 | */
|
---|
918 | if (protocol == IPPROTO_TCP)
|
---|
919 | check_port_tcp(port);
|
---|
920 | else
|
---|
921 | check_port_udp(port);
|
---|
922 | }
|
---|
923 | else
|
---|
924 | {
|
---|
925 | #ifdef TEST_ONLY
|
---|
926 | if (portchk_debug)
|
---|
927 | perror(_("bind"));
|
---|
928 | #else
|
---|
929 | sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
|
---|
930 | sh_error_message(errno), _("bind"));
|
---|
931 | #endif
|
---|
932 | }
|
---|
933 | close (sock);
|
---|
934 | }
|
---|
935 | }
|
---|
936 | return 0;
|
---|
937 | }
|
---|
938 |
|
---|
939 | static int sh_portchk_scan_ports_tcp (int min_port, int max_port)
|
---|
940 | {
|
---|
941 | return sh_portchk_scan_ports_generic (min_port, max_port, SOCK_STREAM, IPPROTO_TCP);
|
---|
942 | }
|
---|
943 |
|
---|
944 | static int sh_portchk_scan_ports_udp (int min_port, int max_port)
|
---|
945 | {
|
---|
946 | return sh_portchk_scan_ports_generic (min_port, max_port, SOCK_DGRAM, IPPROTO_UDP);
|
---|
947 | }
|
---|
948 |
|
---|
949 | /* Subroutine to add an interface
|
---|
950 | */
|
---|
951 | static int sh_portchk_add_interface (const char * str)
|
---|
952 | {
|
---|
953 | struct in_addr haddr;
|
---|
954 | char errbuf[256];
|
---|
955 |
|
---|
956 | if (iface_initialized == 0)
|
---|
957 | {
|
---|
958 | iface_list.used = 0;
|
---|
959 | iface_initialized = 1;
|
---|
960 | }
|
---|
961 |
|
---|
962 | if (0 == inet_aton(str, &haddr))
|
---|
963 | return -1;
|
---|
964 |
|
---|
965 | if (iface_list.used == SH_IFACE_MAX)
|
---|
966 | return -1;
|
---|
967 |
|
---|
968 | sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"), inet_ntoa(haddr));
|
---|
969 | sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
970 | errbuf, _("sh_portchk_add_interface"));
|
---|
971 |
|
---|
972 | memcpy (&(iface_list.iface[iface_list.used].s_addr), &(haddr.s_addr), sizeof(in_addr_t));
|
---|
973 | ++iface_list.used;
|
---|
974 |
|
---|
975 | return 0;
|
---|
976 | }
|
---|
977 |
|
---|
978 |
|
---|
979 | /* Subroutine to add a required or optional port/service
|
---|
980 | */
|
---|
981 | static int sh_portchk_add_required_port_generic (char * service, char * interface, int type)
|
---|
982 | {
|
---|
983 | char buf[256];
|
---|
984 | char proto[4];
|
---|
985 | char * p;
|
---|
986 | char * endptr;
|
---|
987 | unsigned long int port;
|
---|
988 | struct in_addr haddr;
|
---|
989 | struct sh_portentry * portent;
|
---|
990 |
|
---|
991 | if (0 == inet_aton(interface, &haddr))
|
---|
992 | return -1;
|
---|
993 |
|
---|
994 | sl_strlcpy (buf, service, sizeof(buf));
|
---|
995 |
|
---|
996 | p = strchr(buf, '/');
|
---|
997 | if (!p)
|
---|
998 | return -1;
|
---|
999 | if (0 == strcmp(p, _("/tcp")))
|
---|
1000 | sl_strlcpy(proto, _("tcp"), sizeof(proto));
|
---|
1001 | else if (0 == strcmp(p, _("/udp")))
|
---|
1002 | sl_strlcpy(proto, _("udp"), sizeof(proto));
|
---|
1003 | else
|
---|
1004 | return -1;
|
---|
1005 |
|
---|
1006 | *p = '\0';
|
---|
1007 | port = strtoul(buf, &endptr, 0);
|
---|
1008 |
|
---|
1009 | if (*endptr != '\0')
|
---|
1010 | {
|
---|
1011 | portent = sh_portchk_get_from_list (proto, -1, haddr, buf);
|
---|
1012 | if (!portent)
|
---|
1013 | sh_portchk_add_to_list (proto, -1, haddr, buf, type, SH_PORT_UNKN);
|
---|
1014 | else
|
---|
1015 | {
|
---|
1016 | #ifdef TEST_ONLY
|
---|
1017 | fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, proto);
|
---|
1018 | #else
|
---|
1019 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1020 | _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
|
---|
1021 | #endif
|
---|
1022 | return -1;
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 | else if (port <= 65535)
|
---|
1026 | {
|
---|
1027 | portent = sh_portchk_get_from_list (proto, port, haddr, NULL);
|
---|
1028 | if (!portent)
|
---|
1029 | sh_portchk_add_to_list (proto, port, haddr, NULL, type, SH_PORT_UNKN);
|
---|
1030 | else
|
---|
1031 | {
|
---|
1032 | #ifdef TEST_ONLY
|
---|
1033 | fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, proto);
|
---|
1034 | #else
|
---|
1035 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1036 | _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
|
---|
1037 | #endif
|
---|
1038 | return -1;
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 | else
|
---|
1042 | return -1;
|
---|
1043 |
|
---|
1044 | return 0;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | /* Internal interface to add required or optional ports as 'iface:portlist'
|
---|
1048 | */
|
---|
1049 | static int sh_portchk_add_required_generic (const char * str, int type)
|
---|
1050 | {
|
---|
1051 | size_t len;
|
---|
1052 | size_t ll = 0;
|
---|
1053 |
|
---|
1054 | char * interface = NULL;
|
---|
1055 | char * list;
|
---|
1056 | char * p;
|
---|
1057 |
|
---|
1058 | if (!str)
|
---|
1059 | return -1;
|
---|
1060 |
|
---|
1061 | if (strchr(str, ':'))
|
---|
1062 | {
|
---|
1063 | len = strlen(str);
|
---|
1064 | for (ll = 0; ll < len; ++ll)
|
---|
1065 | {
|
---|
1066 | if (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
|
---|
1067 | {
|
---|
1068 | interface = SH_ALLOC(ll+1);
|
---|
1069 | sl_strlcpy(interface, str, ll+1);
|
---|
1070 | interface[ll] = '\0';
|
---|
1071 | while (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
|
---|
1072 | ++ll;
|
---|
1073 | break;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 | else
|
---|
1078 | {
|
---|
1079 | interface = SH_ALLOC(8);
|
---|
1080 | sl_strlcpy(interface, _("0.0.0.0"), 8);
|
---|
1081 | interface[7] = '\0';
|
---|
1082 | while (str[ll] == ' ' || str[ll] == '\t')
|
---|
1083 | ++ll;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | if (!interface)
|
---|
1087 | return -1;
|
---|
1088 |
|
---|
1089 | if (str[ll] == '\0')
|
---|
1090 | {
|
---|
1091 | SH_FREE(interface);
|
---|
1092 | return -1;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | if (portchk_debug)
|
---|
1096 | fprintf(stderr, "add ports for interface: %s\n", interface);
|
---|
1097 |
|
---|
1098 | list = sh_util_strdup(&str[ll]);
|
---|
1099 | p = strtok (list, " ,\t");
|
---|
1100 | if (!p)
|
---|
1101 | {
|
---|
1102 | SH_FREE(interface);
|
---|
1103 | SH_FREE(list);
|
---|
1104 | return -1;
|
---|
1105 | }
|
---|
1106 | while (p)
|
---|
1107 | {
|
---|
1108 | if (-1 == sh_portchk_add_required_port_generic (p, interface, type))
|
---|
1109 | {
|
---|
1110 | SH_FREE(interface);
|
---|
1111 | SH_FREE(list);
|
---|
1112 | return -1;
|
---|
1113 | }
|
---|
1114 | p = strtok (NULL, " ,\t");
|
---|
1115 | }
|
---|
1116 | SH_FREE(interface);
|
---|
1117 | SH_FREE(list);
|
---|
1118 | return 0;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /* User interface to add required ports as 'iface:portlist'
|
---|
1122 | */
|
---|
1123 | static int sh_portchk_add_required (const char * str)
|
---|
1124 | {
|
---|
1125 | return sh_portchk_add_required_generic (str, SH_PORT_REQ);
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | /* User interface to add optional ports as 'iface:portlist'
|
---|
1129 | */
|
---|
1130 | static int sh_portchk_add_optional (const char * str)
|
---|
1131 | {
|
---|
1132 | return sh_portchk_add_required_generic (str, SH_PORT_OPT);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | /* Interface to run port check
|
---|
1136 | */
|
---|
1137 | int sh_portchk_check ()
|
---|
1138 | {
|
---|
1139 | int min_port = 0;
|
---|
1140 |
|
---|
1141 | sh_portchk_reset_lists();
|
---|
1142 | if (0 != geteuid())
|
---|
1143 | {
|
---|
1144 | min_port = 1024;
|
---|
1145 | #ifdef TEST_ONLY
|
---|
1146 | fprintf(stderr, "** WARNING not scanning ports < 1024\n");
|
---|
1147 | #else
|
---|
1148 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
|
---|
1149 | _("not scanning ports below 1024"), _("sh_portchk_check"));
|
---|
1150 | #endif
|
---|
1151 | }
|
---|
1152 | if (sh_portchk_check_udp == 1)
|
---|
1153 | sh_portchk_scan_ports_udp(min_port, -1);
|
---|
1154 | sh_portchk_scan_ports_tcp(min_port, -1);
|
---|
1155 | sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_REPORT);
|
---|
1156 | if (sh_portchk_check_udp == 1)
|
---|
1157 | sh_portchk_check_list (&portlist_udp, "udp", SH_PORT_REPORT);
|
---|
1158 | return 0;
|
---|
1159 | }
|
---|
1160 | #endif
|
---|
1161 |
|
---|
1162 | #ifdef SH_CUTEST
|
---|
1163 | #include "CuTest.h"
|
---|
1164 |
|
---|
1165 | void Test_portcheck_lists (CuTest *tc)
|
---|
1166 | {
|
---|
1167 | #if defined(SH_USE_PORTCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
|
---|
1168 | struct in_addr haddr_local;
|
---|
1169 | struct sh_portentry * portent;
|
---|
1170 |
|
---|
1171 | CuAssertTrue(tc, 0 != inet_aton("127.0.0.1", &haddr_local));
|
---|
1172 |
|
---|
1173 | sh_portchk_add_to_list ("tcp", 8000, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1174 |
|
---|
1175 | portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, NULL);
|
---|
1176 | CuAssertPtrNotNull(tc, portent);
|
---|
1177 |
|
---|
1178 | CuAssertTrue(tc, portent->port == 8000);
|
---|
1179 | CuAssertTrue(tc, 0 == strcmp("127.0.0.1", portent->interface));
|
---|
1180 | CuAssertTrue(tc, portent->status == SH_PORT_UNKN);
|
---|
1181 | CuAssertTrue(tc, portent->flag == SH_PORT_NOT);
|
---|
1182 |
|
---|
1183 | sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_NOREPT);
|
---|
1184 |
|
---|
1185 | CuAssertTrue(tc, NULL == portlist_tcp);
|
---|
1186 |
|
---|
1187 | sh_portchk_add_to_list ("tcp", 8000, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1188 | sh_portchk_add_to_list ("tcp", 8001, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1189 | sh_portchk_add_to_list ("tcp", 8002, haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1190 | sh_portchk_add_to_list ("tcp", 8003, haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1191 | sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1192 | sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1193 | sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
|
---|
1194 | sh_portchk_add_to_list ("tcp", -1, haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
|
---|
1195 |
|
---|
1196 | sh_portchk_check_list (&portlist_tcp, "tcp", SH_PORT_NOREPT);
|
---|
1197 |
|
---|
1198 | CuAssertPtrNotNull(tc, portlist_tcp);
|
---|
1199 |
|
---|
1200 | portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, NULL);
|
---|
1201 | CuAssertPtrNotNull(tc, portent);
|
---|
1202 |
|
---|
1203 | portent = sh_portchk_get_from_list("tcp", 8001, haddr_local, NULL);
|
---|
1204 | CuAssertTrue(tc, NULL == portent);
|
---|
1205 |
|
---|
1206 | portent = sh_portchk_get_from_list("tcp", 8002, haddr_local, NULL);
|
---|
1207 | CuAssertPtrNotNull(tc, portent);
|
---|
1208 |
|
---|
1209 | portent = sh_portchk_get_from_list("tcp", 8003, haddr_local, NULL);
|
---|
1210 | CuAssertTrue(tc, NULL == portent);
|
---|
1211 |
|
---|
1212 | portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo1");
|
---|
1213 | CuAssertTrue(tc, NULL == portent);
|
---|
1214 |
|
---|
1215 | portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo2");
|
---|
1216 | CuAssertPtrNotNull(tc, portent);
|
---|
1217 | CuAssertTrue(tc, 0 == strcmp(portent->service, "foo2"));
|
---|
1218 |
|
---|
1219 | portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo3");
|
---|
1220 | CuAssertTrue(tc, NULL == portent);
|
---|
1221 |
|
---|
1222 | portent = sh_portchk_get_from_list("tcp", 8000, haddr_local, "foo4");
|
---|
1223 | CuAssertPtrNotNull(tc, portent);
|
---|
1224 | CuAssertTrue(tc, 0 == strcmp(portent->service, "foo4"));
|
---|
1225 | #else
|
---|
1226 | (void) tc; /* fix compiler warning */
|
---|
1227 | #endif
|
---|
1228 | return;
|
---|
1229 | }
|
---|
1230 | #endif
|
---|
1231 |
|
---|
1232 | #ifdef TEST_ONLY
|
---|
1233 |
|
---|
1234 | void usage (char * pname)
|
---|
1235 | {
|
---|
1236 | printf ("%s [-r|--required interface:portlist][-o|--optional interface:portlist][--no-udp][-d|--debug] hostname\n\n", pname);
|
---|
1237 | printf (" Check local host for open ports; Version %s\n\n", PORTCHK_VERSION);
|
---|
1238 | printf (" Interface: Numeric address for an interface, e.g. 127.0.0.1\n");
|
---|
1239 | printf (" Portlist: List of ports or services, e.g. 22/tcp,nfs/udp,nlockmgr/udp\n");
|
---|
1240 | printf (" required -> must be open\n");
|
---|
1241 | printf (" optional -> may be open or closed\n");
|
---|
1242 | printf (" RPC services must be specified with service **name**, others with **port number**\n\n");
|
---|
1243 | printf (" Example:\n");
|
---|
1244 | printf (" %s --required 192.168.1.2:22/tcp,nfs/udp,nlockmgr/udp\n\n", pname);
|
---|
1245 | return;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | int main(int argc, char *argv[])
|
---|
1249 | {
|
---|
1250 | char * pname = argv[0];
|
---|
1251 |
|
---|
1252 |
|
---|
1253 | /*
|
---|
1254 | test_lists();
|
---|
1255 |
|
---|
1256 | portlist_tcp = sh_portchk_kill_list (portlist_tcp);
|
---|
1257 | portlist_udp = sh_portchk_kill_list (portlist_udp);
|
---|
1258 | */
|
---|
1259 |
|
---|
1260 | // sh_portchk_add_required ("127.0.0.1 : nlockmgr/tcp, 5308/tcp, nfs/tcp");
|
---|
1261 |
|
---|
1262 | while (argc > 1 && argv[1][0] == '-')
|
---|
1263 | {
|
---|
1264 | if (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h"))
|
---|
1265 | {
|
---|
1266 | usage(pname);
|
---|
1267 | exit (0);
|
---|
1268 | }
|
---|
1269 | else if (0 == strcmp(argv[1], "--required") || 0 == strcmp(argv[1], "-r"))
|
---|
1270 | {
|
---|
1271 | if (argc < 3)
|
---|
1272 | {
|
---|
1273 | usage(pname);
|
---|
1274 | exit (1);
|
---|
1275 | }
|
---|
1276 | sh_portchk_add_required (argv[2]);
|
---|
1277 | --argc; ++argv;
|
---|
1278 | }
|
---|
1279 | else if (0 == strcmp(argv[1], "--optional") || 0 == strcmp(argv[1], "-o"))
|
---|
1280 | {
|
---|
1281 | if (argc < 3)
|
---|
1282 | {
|
---|
1283 | usage(pname);
|
---|
1284 | exit (1);
|
---|
1285 | }
|
---|
1286 | sh_portchk_add_optional (argv[2]);
|
---|
1287 | --argc; ++argv;
|
---|
1288 | }
|
---|
1289 | else if (0 == strcmp(argv[1], "--no-udp"))
|
---|
1290 | {
|
---|
1291 | sh_portchk_check_udp = 0;
|
---|
1292 | }
|
---|
1293 | else if (0 == strcmp(argv[1], "--debug") || 0 == strcmp(argv[1], "-d"))
|
---|
1294 | {
|
---|
1295 | portchk_debug = 1;
|
---|
1296 | }
|
---|
1297 | else
|
---|
1298 | {
|
---|
1299 | usage(pname);
|
---|
1300 | exit (1);
|
---|
1301 | }
|
---|
1302 | --argc; ++argv;
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | if (argc < 2)
|
---|
1306 | {
|
---|
1307 | usage(pname);
|
---|
1308 | exit (1);
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | portchk_hostname = argv[1];
|
---|
1312 |
|
---|
1313 | if (0 != sh_portchk_init ())
|
---|
1314 | {
|
---|
1315 | usage(pname);
|
---|
1316 | exit (1);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | sh_portchk_check();
|
---|
1320 |
|
---|
1321 | /*
|
---|
1322 | sleep(10);
|
---|
1323 |
|
---|
1324 | sh_portchk_check();
|
---|
1325 | */
|
---|
1326 |
|
---|
1327 | return 0;
|
---|
1328 | }
|
---|
1329 | #endif
|
---|