source: trunk/src/sh_ipvx.c@ 541

Last change on this file since 541 was 541, checked in by katerina, 6 years ago

Fix for ticket #433 (coding standardisation).

File size: 13.1 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2010 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#include "config_xor.h"
21
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netdb.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <string.h>
28#include <ctype.h>
29
30#undef FIL__
31#define FIL__ _("sh_ipvx.c")
32
33#include "samhain.h"
34#define SH_NEED_GETHOSTBYXXX
35#include "sh_static.h"
36#include "sh_pthread.h"
37#include "sh_utils.h"
38#include "sh_ipvx.h"
39
40static int sh_ipvx_is_ipv4 (const char * addr)
41{
42 int j;
43 int count = 0;
44 int len = sl_strlen(addr);
45 unsigned int a, b, c, d;
46
47 if (len > 15) /* 3*4 + 3 dots */
48 return (1 == 0);
49
50 for (j = 0; j < len; ++j) {
51 if ( (addr[j] < '0' || addr[j] > '9') && addr[j] != '.')
52 return (1 == 0);
53 if (addr[j] == '.') ++count;
54 }
55
56 if (count != 3)
57 return (1 == 0);
58
59 if (sscanf(addr, "%3u.%3u.%3u.%3u", &a, &b, &c, &d) != 4)
60 return( 1 == 0 );
61
62 if ((a|b|c|d) > 255)
63 return( 1 == 0 );
64
65 return (1 == 1);
66}
67
68#if defined(USE_IPVX)
69static int sh_ipvx_is_ipv6 (const char * addr)
70{
71 int j, k = 0;
72 char c;
73 int len = sl_strlen(addr);
74
75 for (j = 0; j < len; ++j) {
76 c = addr[j];
77 if (( c < '0' || c > '9' ) &&
78 ( c < 'a' || c > 'f' ) &&
79 ( c < 'A' || c > 'F' ) &&
80 ( c != ':') && ( c != '.'))
81 return (1 == 0);
82 else if (c == ':')
83 ++k;
84 else if (c == '.' && k < 2)
85 return (1 == 0); /* ::ffff:ipv4 */
86 }
87 if (k < 2 || k > 7)
88 return (1 == 0);
89
90 return (1 == 1);
91}
92#endif
93
94
95int sh_ipvx_is_numeric (const char * addr)
96{
97#if defined(USE_IPVX)
98 if (!sh_ipvx_is_ipv4(addr))
99 return sh_ipvx_is_ipv6(addr);
100 else
101 return (1 == 1);
102#else
103 return sh_ipvx_is_ipv4(addr);
104#endif
105}
106
107int sh_ipvx_isany (struct sh_sockaddr * a)
108{
109#if defined(HOST_IS_CYGWIN)
110 /*
111 * Cygwin implementation gives 'missing braces around initializer'
112 * warning, thus replace it with correct version.
113 */
114#undef IN6ADDR_ANY_INIT
115#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
116#endif
117
118#if defined(USE_IPVX)
119 struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
120#endif
121
122 switch (a->ss_family)
123 {
124 case AF_INET:
125 if ((a->sin).sin_addr.s_addr == INADDR_ANY)
126 return 1;
127 break;
128#if defined(USE_IPVX)
129 case AF_INET6:
130 if (0 == memcmp(&((a->sin6).sin6_addr.s6_addr), &anyaddr, 16))
131 return 1;
132 break;
133#endif
134 }
135 return 0;
136}
137
138int sh_ipvx_cmp (struct sh_sockaddr * a, struct sh_sockaddr * b)
139{
140 if (a->ss_family != b->ss_family)
141 return 1;
142
143 switch (a->ss_family)
144 {
145 case AF_INET:
146 return memcmp(&((a->sin).sin_addr.s_addr), &((b->sin).sin_addr.s_addr), 4);
147 break;
148#if defined(USE_IPVX)
149 case AF_INET6:
150 return memcmp(&((a->sin6).sin6_addr.s6_addr), &((b->sin6).sin6_addr.s6_addr), 16);
151 break;
152#endif
153 }
154 return 1;
155}
156
157int sh_ipvx_ntoa (char * name, size_t name_size, struct sh_sockaddr * ss)
158{
159#if defined(USE_IPVX)
160 int len = (ss->ss_family == AF_INET) ?
161 sizeof(struct sockaddr_in) :
162 sizeof(struct sockaddr_in6);
163
164 int ret = getnameinfo(sh_ipvx_sockaddr_cast(ss), len,
165 name, name_size, NULL, 0, NI_NUMERICHOST);
166
167 if (ret != 0 && name_size > 0)
168 {
169 name[name_size-1] = '\0';
170
171 if (!sh_ipvx_is_numeric(name))
172 {
173 if (name_size > 7) {
174 name[0] = '0'; name[1] = '.'; name[2] = '0'; name[3] = '.';
175 name[4] = '0'; name[5] = '.'; name[6] = '0'; name[7] = '\0';
176 } else {
177 name[0] = '\0';
178 }
179 }
180 }
181 return ret;
182#else
183 char * p = inet_ntoa((ss->sin).sin_addr);
184 sl_strlcpy(name, p, name_size);
185 return 0;
186#endif
187}
188
189struct sockaddr * sh_ipvx_sockaddr_cast (struct sh_sockaddr * ss)
190{
191#if defined(USE_IPVX)
192 if (ss->ss_family == AF_INET6)
193 return (struct sockaddr *) &(ss->sin6);
194#endif
195 return (struct sockaddr *) &(ss->sin);
196}
197
198char * sh_ipvx_print_sockaddr (struct sockaddr * sa, int sa_family)
199{
200 struct sh_sockaddr ss;
201 static char ipbuf[SH_IP_BUF];
202
203 sh_ipvx_save(&ss, sa_family, sa);
204 sh_ipvx_ntoa (ipbuf, sizeof(ipbuf), &ss);
205 return ipbuf;
206}
207
208void sh_ipvx_save(struct sh_sockaddr * ss, int sa_family, struct sockaddr * sa)
209{
210 switch (sa_family)
211 {
212 case AF_INET:
213 ss->ss_family = AF_INET;
214 memcpy(&(ss->sin), (struct sockaddr_in*) sa, sizeof(struct sockaddr_in));
215 break;
216#if defined(USE_IPVX)
217 case AF_INET6:
218 ss->ss_family = AF_INET6;
219 memcpy(&(ss->sin6), (struct sockaddr_in6*) sa, sizeof(struct sockaddr_in6));
220 break;
221#endif
222 default:
223 break;
224 }
225 return;
226}
227
228int sh_ipvx_set_port(struct sh_sockaddr * ss, int port)
229{
230#if defined(USE_IPVX)
231
232 switch (ss->ss_family)
233 {
234 case AF_INET:
235 (ss->sin).sin_family = AF_INET;
236 (ss->sin).sin_port = htons (port);
237 break;
238 case AF_INET6:
239 (ss->sin6).sin6_family = AF_INET6;
240 (ss->sin6).sin6_port = htons (port);
241 break;
242 }
243 return 0;
244#else
245 (ss->sin).sin_family = AF_INET;
246 (ss->sin).sin_port = htons (port);
247 return 0;
248#endif
249}
250
251int sh_ipvx_get_port(struct sh_sockaddr * sa)
252{
253 int port = 0;
254#if defined(USE_IPVX)
255
256 switch (sa->ss_family)
257 {
258 case AF_INET:
259 port = ntohs((sa->sin).sin_port);
260 break;
261 case AF_INET6:
262 port = ntohs((sa->sin6).sin6_port);
263 break;
264 }
265#else
266 port = ntohs((sa->sin).sin_port);
267#endif
268 return port;
269}
270
271int sh_ipvx_aton (const char * name, struct sh_sockaddr * ss)
272{
273#if defined(USE_IPVX)
274 int ret;
275 struct addrinfo hints;
276 struct addrinfo *res;
277
278 memset(&hints, 0, sizeof(hints));
279 hints.ai_family = PF_UNSPEC;
280 hints.ai_flags = AI_NUMERICHOST;
281 ret = getaddrinfo(name, NULL, &hints, &res);
282
283 if (ret)
284 return 0;
285
286 memset(ss, 0, sizeof(struct sh_sockaddr));
287 switch(res->ai_family)
288 {
289 case AF_INET:
290 memcpy(&(ss->sin), res->ai_addr, sizeof(struct sockaddr_in));
291 ss->ss_family = AF_INET;
292 break;
293 case AF_INET6:
294 memcpy(&(ss->sin6), res->ai_addr, sizeof(struct sockaddr_in6));
295 ss->ss_family = AF_INET6;
296 break;
297 default:
298 return 0;
299 break;
300 }
301 freeaddrinfo(res);
302 return 1;
303#else
304 int ret = inet_aton(name, &((ss->sin).sin_addr));
305 ss->ss_family = AF_INET;
306 return ret;
307#endif
308}
309
310#if !defined(USE_IPVX)
311static const char * sh_ipvx_h_name (struct hostent * host_entry)
312{
313 char ** p;
314 if (strchr(host_entry->h_name, '.')) {
315 return host_entry->h_name;
316 } else {
317 for (p = host_entry->h_aliases; *p; ++p) {
318 if (strchr(*p, '.'))
319 return *p;
320 }
321 }
322 return host_entry->h_name;
323}
324#endif
325
326static char * sh_tolower (char * s)
327{
328 char * ret = s;
329 if (s)
330 {
331 for (; *s; ++s)
332 {
333 *s = tolower((unsigned char) *s);
334 }
335 }
336 return ret;
337}
338
339void * sh_dummy_341_out;
340
341char * sh_ipvx_canonical(const char * hostname, char * numeric, size_t nlen)
342{
343 volatile int flag = 0;
344 char *out = NULL;
345#if defined(USE_IPVX)
346 struct addrinfo hints;
347 struct addrinfo *res;
348 struct sockaddr *sa;
349 int salen;
350 int err;
351 struct sh_sockaddr ss;
352 const char * host;
353 char hostbuf[SH_BUFSIZE];
354
355 SL_ENTER(_("sh_ipvx_canonical"));
356
357 numeric[0] = '\0';
358
359 sh_dummy_341_out = (void *) &out;
360
361 if (sh_ipvx_is_numeric(hostname))
362 {
363 sh_ipvx_aton (hostname, &ss);
364 if (0 == getnameinfo(sh_ipvx_sockaddr_cast(&ss), SH_SS_LEN(ss),
365 hostbuf, sizeof(hostbuf), NULL, 0, NI_NAMEREQD))
366 host = hostbuf;
367 else
368 host = hostname;
369 }
370 else
371 {
372 host = hostname;
373 }
374
375 memset(&hints, 0, sizeof(hints));
376 hints.ai_family = PF_UNSPEC;
377#if defined(AI_CANONNAME)
378 hints.ai_flags = AI_CANONNAME;
379#endif
380
381 err = getaddrinfo(host, NULL, &hints, &res);
382 if (err == 0)
383 {
384 struct addrinfo * res_orig = res;
385
386#if defined(AI_CANONNAME)
387 if (res->ai_canonname && strlen(res->ai_canonname) > 0)
388 {
389 out = sh_util_strdup(res->ai_canonname);
390 sh_tolower (out);
391 if (strchr(out, '.'))
392 flag = 1;
393 }
394#endif
395
396 sa = res->ai_addr;
397 salen = res->ai_addrlen;
398 getnameinfo(sa, salen,
399 numeric, nlen, NULL, 0, NI_NUMERICHOST);
400
401 if (!flag)
402 out = SH_ALLOC(SH_PATHBUF);
403
404 while (res && !flag)
405 {
406 sa = res->ai_addr;
407 salen = res->ai_addrlen;
408
409 getnameinfo(sa, salen,
410 out, SH_PATHBUF, NULL, 0, 0);
411 sh_tolower (out);
412 if (strchr(out, '.'))
413 flag = 1;
414
415 res = res->ai_next;
416 }
417
418 freeaddrinfo(res_orig);
419 }
420#else
421 struct hostent *he;
422 struct sh_sockaddr ss;
423 volatile int isNum = 0;
424 struct sockaddr_in *sin;
425
426 SL_ENTER(_("sh_ipvx_canonical"));
427
428 numeric[0] = '\0';
429
430 sh_dummy_341_out = (void *) &out;
431
432 if (sh_ipvx_is_numeric(hostname))
433 {
434 sh_ipvx_aton (hostname, &ss);
435 isNum = 1;
436 }
437
438
439 SH_MUTEX_LOCK(mutex_resolv);
440
441 if (isNum == 0)
442 {
443 he = sh_gethostbyname(hostname);
444 }
445 else
446 {
447 sin = (struct sockaddr_in *) sh_ipvx_sockaddr_cast(&ss);
448 he = sh_gethostbyaddr(&(sin->sin_addr), sizeof(sin->sin_addr), AF_INET);
449 }
450
451 if (he != NULL)
452 {
453 out = sh_util_strdup(sh_ipvx_h_name(he));
454 sh_tolower (out);
455 sl_strlcpy (numeric,
456 inet_ntoa (*(struct in_addr *) he->h_addr),
457 nlen);
458 flag = 1;
459 }
460 SH_MUTEX_UNLOCK(mutex_resolv);
461#endif
462
463 if (flag) {
464 SL_RETURN(out, _("sh_ipvx_canonical"));
465 }
466
467 if (out)
468 SH_FREE(out);
469 if (numeric[0] == '\0')
470 sl_strlcpy (numeric, _("0.0.0.0"), nlen);
471 SL_RETURN(NULL, _("sh_ipvx_canonical"));
472}
473
474char * sh_ipvx_addrtoname(struct sh_sockaddr * ss)
475{
476#if defined(USE_IPVX)
477 char namebuf[SH_BUFSIZE];
478
479 if (getnameinfo(sh_ipvx_sockaddr_cast(ss), SH_SSP_LEN(ss),
480 namebuf, sizeof(namebuf), NULL, 0, NI_NAMEREQD) != 0)
481 {
482 return NULL;
483 }
484 return sh_util_strdup(namebuf);
485#else
486 struct sockaddr_in *sin;
487 struct hostent *he;
488
489 sin = (struct sockaddr_in *) sh_ipvx_sockaddr_cast(ss);
490
491 he = sh_gethostbyaddr(&(sin->sin_addr), sizeof(sin->sin_addr), AF_INET);
492
493 if (he && he->h_name)
494 {
495 return sh_util_strdup(he->h_name);
496 }
497
498 return NULL;
499#endif
500}
501
502int sh_ipvx_reverse_check_ok (char * peer, int port, struct sh_sockaddr * ss)
503{
504#if defined(USE_IPVX)
505 struct addrinfo *res;
506 struct addrinfo hints;
507 char sport[32];
508 struct addrinfo *p;
509
510 SL_ENTER(_("sh_ipvx_reverse_check_ok"));
511
512 sl_snprintf(sport, sizeof(sport), "%d", port);
513
514 memset(&hints, 0, sizeof(hints));
515 hints.ai_socktype = SOCK_STREAM;
516 hints.ai_flags = AI_ADDRCONFIG;
517
518 if (getaddrinfo(peer, sport, &hints, &res) != 0)
519 {
520 SL_RETURN((0), _("sh_ipvx_reverse_check_ok"));
521 }
522
523 p = res;
524 while (p != NULL)
525 {
526 if (ss->ss_family == p->ai_family)
527 {
528 struct sh_sockaddr pp;
529
530 char dst1[SH_IP_BUF];
531 char dst2[SH_IP_BUF];
532
533 sh_ipvx_save(&pp, p->ai_family, p->ai_addr);
534
535 sh_ipvx_ntoa (dst1, sizeof(dst1), &pp);
536 sh_ipvx_ntoa (dst2, sizeof(dst2), ss);
537
538 if (0 == sl_strcmp(dst1, dst2))
539 {
540 SL_RETURN((1), _("sh_ipvx_reverse_check_ok"));
541 }
542 }
543 p = p->ai_next;
544 }
545 freeaddrinfo(res);
546#else
547 struct hostent * he;
548 char ** p;
549 struct sockaddr_in * sin = (struct sockaddr_in *) sh_ipvx_sockaddr_cast(ss);
550
551 (void) port;
552
553 SL_ENTER(_("sh_ipvx_reverse_check_ok"));
554
555 he = sh_gethostbyname(peer);
556 if (he != NULL)
557 {
558 for (p = he->h_addr_list; *p; ++p)
559 {
560 if (0 == memcmp (*p, &(sin->sin_addr), sizeof(in_addr_t)) ) {
561 SL_RETURN((1), _("sh_ipvx_reverse_check_ok")); }
562 }
563 }
564#endif
565 SL_RETURN((0), _("sh_ipvx_reverse_check_ok"));
566}
567
568#ifdef SH_CUTEST
569#include <stdlib.h>
570#include "CuTest.h"
571
572void Test_ipvx (CuTest *tc) {
573
574 int result;
575
576 result = sh_ipvx_is_ipv4("123456789");
577 CuAssertTrue(tc, result == FALSE);
578
579 result = sh_ipvx_is_ipv4("123456789123...");
580 CuAssertTrue(tc, result == FALSE);
581
582 result = sh_ipvx_is_ipv4("123.456.789.123");
583 CuAssertTrue(tc, result == FALSE);
584
585 result = sh_ipvx_is_ipv4("123.156.189.254");
586 CuAssertTrue(tc, result == TRUE);
587
588 result = sh_ipvx_is_ipv4("255.255.255.255");
589 CuAssertTrue(tc, result == TRUE);
590
591 result = sh_ipvx_is_ipv4("0.0.0.0");
592 CuAssertTrue(tc, result == TRUE);
593
594 result = sh_ipvx_is_ipv4("0022.156.189.254");
595 CuAssertTrue(tc, result == FALSE);
596
597 result = sh_ipvx_is_ipv4("999999999.1.1.2");
598 CuAssertTrue(tc, result == FALSE);
599
600}
601#endif
602
Note: See TracBrowser for help on using the repository browser.