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