2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Provides the functions to check for possible replay attacks
9 * by using a cache of previously seen digests. This cache is a
10 * simple file by default, but can be made to use a dbm solution
11 * (ndbm or gdbm in ndbm compatibility mode) file to store the digest
12 * of a previously received SPA packets.
14 * Copyright 2010 Damien Stuart (dstuart@dstuart.org)
16 * License (GNU Public License):
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 *****************************************************************************
35 #include "replay_cache.h"
37 #include "fwknopd_errors.h"
45 #define MY_DBM_FETCH(d, k) gdbm_fetch(d, k)
46 #define MY_DBM_STORE(d, k, v, m) gdbm_store(d, k, v, m)
47 #define MY_DBM_STRERROR(x) gdbm_strerror(x)
48 #define MY_DBM_CLOSE(d) gdbm_close(d)
50 #define MY_DBM_REPLACE GDBM_REPLACE
51 #define MY_DBM_INSERT GDBM_INSERT
56 #define MY_DBM_FETCH(d, k) dbm_fetch(d, k)
57 #define MY_DBM_STORE(d, k, v, m) dbm_store(d, k, v, m)
58 #define MY_DBM_STRERROR(x) strerror(x)
59 #define MY_DBM_CLOSE(d) dbm_close(d)
61 #define MY_DBM_REPLACE DBM_REPLACE
62 #define MY_DBM_INSERT DBM_INSERT
66 #error "File cache method disabled, and No GDBM or NDBM header file found. WTF?"
71 #include <sys/socket.h>
73 #include <arpa/inet.h>
78 #define MAX_DIGEST_SIZE 64
80 /* Rotate the digest file by simply renaming it.
83 rotate_digest_cache_file(fko_srv_options_t *opts)
85 #ifdef NO_DIGEST_CACHE
86 log_msg(LOG_WARNING, "Digest cache not supported. Nothing to rotate.");
89 char *new_file = NULL;
91 log_msg(LOG_INFO, "Rotating digest cache file.");
94 new_file = malloc(strlen(opts->config[CONF_DIGEST_FILE])+5);
96 new_file = malloc(strlen(opts->config[CONF_DIGEST_DB_FILE])+5);
101 log_msg(LOG_ERR, "rotate_digest_cache_file: Memory allocation error.");
102 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
105 /* The new filename is just the original with a trailing '-old'.
108 strlcpy(new_file, opts->config[CONF_DIGEST_FILE],
109 strlen(opts->config[CONF_DIGEST_FILE])+5);
111 strlcpy(new_file, opts->config[CONF_DIGEST_DB_FILE],
112 strlen(opts->config[CONF_DIGEST_DB_FILE])+5);
114 strcat(new_file, "-old");
117 res = rename(opts->config[CONF_DIGEST_FILE], new_file);
119 res = rename(opts->config[CONF_DIGEST_DB_FILE], new_file);
123 log_msg(LOG_ERR, "Unable to rename digest file: %s to %s: %s",
125 opts->config[CONF_DIGEST_FILE], new_file, strerror(errno)
127 opts->config[CONF_DIGEST_DB_FILE], new_file, strerror(errno)
130 #endif /* NO_DIGEST_CACHE */
134 replay_warning(fko_srv_options_t *opts, digest_cache_info_t *digest_info)
136 char src_ip[INET_ADDRSTRLEN+1] = {0};
137 char orig_src_ip[INET_ADDRSTRLEN+1] = {0};
138 char created[DATE_LEN];
141 char first[DATE_LEN], last[DATE_LEN];
144 /* Convert the IPs to a human readable form
146 inet_ntop(AF_INET, &(opts->spa_pkt.packet_src_ip),
147 src_ip, INET_ADDRSTRLEN);
148 inet_ntop(AF_INET, &(digest_info->src_ip), orig_src_ip, INET_ADDRSTRLEN);
151 /* Mark the last_replay time.
153 digest_info->last_replay = time(NULL);
155 /* Increment the replay count and check to see if it is the first one.
157 if(++(digest_info->replay_count) == 1)
159 /* This is the first replay so make it the same as last_replay
161 digest_info->first_replay = digest_info->last_replay;
164 strftime(first, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->first_replay)));
165 strftime(last, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->last_replay)));
168 strftime(created, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->created)));
171 "Replay detected from source IP: %s\n"
172 " Destination proto/port: %d/%d\n"
173 " Original source IP: %s\n"
174 " Original dst proto/port: %d/%d\n"
176 " Entry created: %s\n",
178 " Entry created: %s\n"
179 " First replay: %s\n"
181 " Replay count: %i\n",
184 opts->spa_pkt.packet_proto,
185 opts->spa_pkt.packet_dst_port,
188 digest_info->dst_port,
195 digest_info->replay_count
203 replay_cache_init(fko_srv_options_t *opts)
205 #ifdef NO_DIGEST_CACHE
209 /* If rotation was specified, do it.
211 if(opts->rotate_digest_cache)
212 rotate_digest_cache_file(opts);
215 return replay_file_cache_init(opts);
217 return replay_db_cache_init(opts);
220 #endif /* NO_DIGEST_CACHE */
225 replay_file_cache_init(fko_srv_options_t *opts)
227 FILE *digest_file_ptr = NULL;
228 unsigned int num_lines = 0, digest_ctr = 0;
229 char line_buf[MAX_LINE_LEN] = {0};
230 char src_ip[INET_ADDRSTRLEN+1] = {0};
231 char dst_ip[INET_ADDRSTRLEN+1] = {0};
234 struct digest_cache_list *digest_elm = NULL;
236 /* if the file exists, import the previous SPA digests into
239 if (access(opts->config[CONF_DIGEST_FILE], F_OK) == 0)
243 if (access(opts->config[CONF_DIGEST_FILE], R_OK|W_OK) != 0)
245 log_msg(LOG_WARNING, "Digest file '%s' exists but: '%s'",
246 opts->config[CONF_DIGEST_FILE], strerror(errno));
252 /* the file does not exist yet, so it will be created when the first
253 * successful SPA packet digest is written to disk
255 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "w")) == NULL)
257 log_msg(LOG_WARNING, "Could not open digest cache: %s",
258 opts->config[CONF_DIGEST_FILE]);
260 fprintf(digest_file_ptr,
261 "# <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>\n");
262 fclose(digest_file_ptr);
264 set_file_perms(opts->config[CONF_DIGEST_FILE]);
268 verify_file_perms_ownership(opts->config[CONF_DIGEST_FILE]);
270 /* File exists, and we have access - create in-memory digest cache
272 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "r")) == NULL)
274 log_msg(LOG_WARNING, "Could not open digest cache: %s",
275 opts->config[CONF_DIGEST_FILE]);
280 * <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>
282 * 7XgadOyqv0tF5xG8uhg2iIrheeNKglCWKmxQDgYP1dY 17 127.0.0.1 40305 127.0.0.1 62201 1313283481
284 while ((fgets(line_buf, MAX_LINE_LEN, digest_file_ptr)) != NULL)
287 line_buf[MAX_LINE_LEN-1] = '\0';
289 if(IS_EMPTY_LINE(line_buf[0]))
292 /* Initialize a digest cache list element, and add it into the list if
295 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
297 fprintf(stderr, "Could not allocate digest list element\n");
300 if ((digest_elm->cache_info.digest = calloc(1, MAX_DIGEST_SIZE+1)) == NULL)
303 fprintf(stderr, "Could not allocate digest string\n");
309 if(sscanf(line_buf, "%s %hhu %s %hu %s %hu %ld",
310 digest_elm->cache_info.digest,
311 &(digest_elm->cache_info.proto),
313 &(digest_elm->cache_info.src_port),
315 &(digest_elm->cache_info.dst_port),
320 "*Skipping invalid digest file entry in %s at line %i.\n - %s",
321 opts->config[CONF_DIGEST_FILE], num_lines, line_buf
323 free(digest_elm->cache_info.digest);
327 digest_elm->cache_info.created = time_tmp;
330 if (inet_pton(AF_INET, src_ip, &(digest_elm->cache_info.src_ip)) != 1)
332 free(digest_elm->cache_info.digest);
337 if (inet_pton(AF_INET, dst_ip, &(digest_elm->cache_info.dst_ip)) != 1)
339 free(digest_elm->cache_info.digest);
344 digest_elm->next = opts->digest_cache;
345 opts->digest_cache = digest_elm;
348 if(opts->verbose > 3)
350 "DIGEST FILE: %s, VALID LINE: %s",
351 opts->config[CONF_DIGEST_FILE], line_buf
356 fclose(digest_file_ptr);
361 #else /* USE_FILE_CACHE */
363 /* Check for the existence of the replay dbm file, and create it if it does
364 * not exist. Returns the number of db entries or -1 on error.
367 replay_db_cache_init(fko_srv_options_t *opts)
369 #ifdef NO_DIGEST_CACHE
380 datum db_key, db_next_key;
385 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
389 opts->config[CONF_DIGEST_DB_FILE], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR
396 "Unable to open digest cache file: '%s': %s",
397 opts->config[CONF_DIGEST_DB_FILE],
398 MY_DBM_STRERROR(errno)
405 db_key = gdbm_firstkey(rpdb);
407 while (db_key.dptr != NULL)
410 db_next_key = gdbm_nextkey(rpdb, db_key);
412 db_key = db_next_key;
415 for (db_key = dbm_firstkey(rpdb); db_ent.dptr != NULL; db_key = dbm_nextkey(rpdb))
422 #endif /* NO_DIGEST_CACHE */
424 #endif /* USE_FILE_CACHE */
426 /* Take an fko context, pull the digest and use it as the key to check the
427 * replay db (digest cache).
430 is_replay(fko_srv_options_t *opts, char *digest)
432 #ifdef NO_DIGEST_CACHE
437 return is_replay_file_cache(opts, digest);
439 return is_replay_dbm_cache(opts, digest);
441 #endif /* NO_DIGEST_CACHE */
445 add_replay(fko_srv_options_t *opts, char *digest)
447 #ifdef NO_DIGEST_CACHE
452 return add_replay_file_cache(opts, digest);
454 return add_replay_dbm_cache(opts, digest);
456 #endif /* NO_DIGEST_CACHE */
461 is_replay_file_cache(fko_srv_options_t *opts, char *digest)
465 struct digest_cache_list *digest_list_ptr = NULL;
467 digest_len = strlen(digest);
469 /* Check the cache for the SPA packet digest
471 for (digest_list_ptr = opts->digest_cache;
472 digest_list_ptr != NULL;
473 digest_list_ptr = digest_list_ptr->next) {
475 if (strncmp(digest_list_ptr->cache_info.digest, digest, digest_len) == 0) {
477 replay_warning(opts, &(digest_list_ptr->cache_info));
479 return(SPA_MSG_REPLAY);
482 return(SPA_MSG_SUCCESS);
486 add_replay_file_cache(fko_srv_options_t *opts, char *digest)
488 FILE *digest_file_ptr = NULL;
490 char src_ip[INET_ADDRSTRLEN+1] = {0};
491 char dst_ip[INET_ADDRSTRLEN+1] = {0};
493 struct digest_cache_list *digest_elm = NULL;
495 digest_len = strlen(digest);
497 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
499 log_msg(LOG_WARNING, "Error calloc() returned NULL for digest cache element",
500 fko_errstr(SPA_MSG_ERROR));
502 return(SPA_MSG_ERROR);
504 if ((digest_elm->cache_info.digest = calloc(1, digest_len+1)) == NULL)
506 log_msg(LOG_WARNING, "Error calloc() returned NULL for digest cache string",
507 fko_errstr(SPA_MSG_ERROR));
509 return(SPA_MSG_ERROR);
512 strlcpy(digest_elm->cache_info.digest, digest, digest_len+1);
513 digest_elm->cache_info.proto = opts->spa_pkt.packet_proto;
514 digest_elm->cache_info.src_ip = opts->spa_pkt.packet_src_ip;
515 digest_elm->cache_info.dst_ip = opts->spa_pkt.packet_dst_ip;
516 digest_elm->cache_info.src_port = opts->spa_pkt.packet_src_port;
517 digest_elm->cache_info.dst_port = opts->spa_pkt.packet_dst_port;
518 digest_elm->cache_info.created = time(NULL);
520 /* First, add the digest at the head of the in-memory list
522 digest_elm->next = opts->digest_cache;
523 opts->digest_cache = digest_elm;
525 /* Now, write the digest to disk
527 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "a")) == NULL)
529 log_msg(LOG_WARNING, "Could not open digest cache: %s",
530 opts->config[CONF_DIGEST_FILE]);
531 return(SPA_MSG_DIGEST_CACHE_ERROR);
534 inet_ntop(AF_INET, &(digest_elm->cache_info.src_ip),
535 src_ip, INET_ADDRSTRLEN);
536 inet_ntop(AF_INET, &(digest_elm->cache_info.dst_ip),
537 dst_ip, INET_ADDRSTRLEN);
538 fprintf(digest_file_ptr, "%s %d %s %d %s %d %d\n",
540 digest_elm->cache_info.proto,
542 (int) digest_elm->cache_info.src_port,
544 digest_elm->cache_info.dst_port,
545 (int) digest_elm->cache_info.created);
547 fclose(digest_file_ptr);
549 return(SPA_MSG_SUCCESS);
551 #endif /* USE_FILE_CACHE */
555 is_replay_dbm_cache(fko_srv_options_t *opts, char *digest)
557 #ifdef NO_DIGEST_CACHE
566 datum db_key, db_ent;
568 int digest_len, res = SPA_MSG_SUCCESS;
570 digest_len = strlen(digest);
572 db_key.dptr = digest;
573 db_key.dsize = digest_len;
575 /* Check the db for the key
579 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
582 rpdb = dbm_open(opts->config[CONF_DIGEST_DB_FILE], O_RDWR, 0);
587 log_msg(LOG_WARNING, "Error opening digest_cache: '%s': %s",
588 opts->config[CONF_DIGEST_DB_FILE],
589 MY_DBM_STRERROR(errno)
592 return(SPA_MSG_DIGEST_CACHE_ERROR);
595 db_ent = MY_DBM_FETCH(rpdb, db_key);
597 /* If the datum is not null, we have a match. Otherwise, we add
598 * this entry to the cache.
600 if(db_ent.dptr != NULL)
602 replay_warning(opts, (digest_cache_info_t *)db_ent.dptr);
604 /* Save it back to the digest cache
606 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_REPLACE) != 0)
607 log_msg(LOG_WARNING, "Error updating entry in digest_cache: '%s': %s",
608 opts->config[CONF_DIGEST_DB_FILE],
609 MY_DBM_STRERROR(errno)
615 res = SPA_MSG_REPLAY;
621 #endif /* NO_DIGEST_CACHE */
625 add_replay_dbm_cache(fko_srv_options_t *opts, char *digest)
627 #ifdef NO_DIGEST_CACHE
636 datum db_key, db_ent;
638 int digest_len, res = SPA_MSG_SUCCESS;
640 digest_cache_info_t dc_info;
642 digest_len = strlen(digest);
644 db_key.dptr = digest;
645 db_key.dsize = digest_len;
647 /* Check the db for the key
651 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
654 rpdb = dbm_open(opts->config[CONF_DIGEST_DB_FILE], O_RDWR, 0);
659 log_msg(LOG_WARNING, "Error opening digest_cache: '%s': %s",
660 opts->config[CONF_DIGEST_DB_FILE],
661 MY_DBM_STRERROR(errno)
664 return(SPA_MSG_DIGEST_CACHE_ERROR);
667 db_ent = MY_DBM_FETCH(rpdb, db_key);
669 /* If the datum is null, we have a new entry.
671 if(db_ent.dptr == NULL)
673 /* This is a new SPA packet that needs to be added to the cache.
675 dc_info.src_ip = opts->spa_pkt.packet_src_ip;
676 dc_info.dst_ip = opts->spa_pkt.packet_dst_ip;
677 dc_info.src_port = opts->spa_pkt.packet_src_port;
678 dc_info.dst_port = opts->spa_pkt.packet_dst_port;
679 dc_info.proto = opts->spa_pkt.packet_proto;
680 dc_info.created = time(NULL);
681 dc_info.first_replay = dc_info.last_replay = dc_info.replay_count = 0;
683 db_ent.dsize = sizeof(digest_cache_info_t);
684 db_ent.dptr = (char*)&(dc_info);
686 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_INSERT) != 0)
688 log_msg(LOG_WARNING, "Error adding entry digest_cache: %s",
689 MY_DBM_STRERROR(errno)
692 res = SPA_MSG_DIGEST_CACHE_ERROR;
695 res = SPA_MSG_SUCCESS;
698 res = SPA_MSG_DIGEST_CACHE_ERROR;
703 #endif /* NO_DIGEST_CACHE */
705 #endif /* USE_FILE_CACHE */
708 /* Free replay list memory
711 free_replay_list(fko_srv_options_t *opts)
713 #ifdef NO_DIGEST_CACHE
716 struct digest_cache_list *digest_list_ptr = NULL, *digest_tmp = NULL;
718 if (opts->digest_cache == NULL)
721 digest_list_ptr = opts->digest_cache;
722 while (digest_list_ptr != NULL)
724 digest_tmp = digest_list_ptr->next;
725 if (digest_list_ptr->cache_info.digest != NULL
726 && digest_list_ptr->cache_info.digest[0] != '\0')
728 free(digest_list_ptr->cache_info.digest);
730 free(digest_list_ptr);
731 digest_list_ptr = digest_tmp;