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 last_ip[INET_ADDRSTRLEN+1] = {0};
142 char first[DATE_LEN], last[DATE_LEN];
145 /* Convert the IPs to a human readable form
147 inet_ntop(AF_INET, &(opts->spa_pkt.packet_src_ip),
148 src_ip, INET_ADDRSTRLEN);
149 inet_ntop(AF_INET, &(digest_info->src_ip), orig_src_ip, INET_ADDRSTRLEN);
152 /* Mark the last_replay time.
154 digest_info->last_replay = time(NULL);
156 /* Increment the replay count and check to see if it is the first one.
158 if(++(digest_info->replay_count) == 1)
160 /* This is the first replay so make it the same as last_replay
162 digest_info->first_replay = digest_info->last_replay;
165 strftime(first, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->first_replay)));
166 strftime(last, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->last_replay)));
169 strftime(created, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->created)));
172 "Replay detected from source IP: %s\n"
173 " Destination proto/port: %d/%d\n"
174 " Original source IP: %s\n"
175 " Original dst proto/port: %d/%d\n"
177 " Entry created: %s\n",
179 " Entry created: %s\n"
180 " First replay: %s\n"
182 " Replay count: %i\n",
185 opts->spa_pkt.packet_proto,
186 opts->spa_pkt.packet_dst_port,
189 digest_info->dst_port,
196 digest_info->replay_count
204 replay_cache_init(fko_srv_options_t *opts)
206 #ifdef NO_DIGEST_CACHE
210 /* If rotation was specified, do it.
212 if(opts->rotate_digest_cache)
213 rotate_digest_cache_file(opts);
216 return replay_file_cache_init(opts);
218 return replay_db_cache_init(opts);
221 #endif /* NO_DIGEST_CACHE */
226 replay_file_cache_init(fko_srv_options_t *opts)
228 FILE *digest_file_ptr = NULL;
229 unsigned int num_lines = 0, digest_ctr = 0;
230 char line_buf[MAX_LINE_LEN] = {0};
231 char src_ip[INET_ADDRSTRLEN+1] = {0};
232 char dst_ip[INET_ADDRSTRLEN+1] = {0};
235 struct digest_cache_list *digest_elm = NULL;
237 /* if the file exists, import the previous SPA digests into
240 if (access(opts->config[CONF_DIGEST_FILE], F_OK) == 0)
244 if (access(opts->config[CONF_DIGEST_FILE], R_OK|W_OK) != 0)
246 log_msg(LOG_WARNING, "Digest file '%s' exists but: '%s'",
247 opts->config[CONF_DIGEST_FILE], strerror(errno));
253 /* the file does not exist yet, so it will be created when the first
254 * successful SPA packet digest is written to disk
256 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "w")) == NULL)
258 log_msg(LOG_WARNING, "Could not open digest cache: %s",
259 opts->config[CONF_DIGEST_FILE]);
261 fprintf(digest_file_ptr,
262 "# <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>\n");
263 fclose(digest_file_ptr);
265 set_file_perms(opts->config[CONF_DIGEST_FILE]);
269 verify_file_perms_ownership(opts->config[CONF_DIGEST_FILE]);
271 /* File exists, and we have access - create in-memory digest cache
273 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "r")) == NULL)
275 log_msg(LOG_WARNING, "Could not open digest cache: %s",
276 opts->config[CONF_DIGEST_FILE]);
281 * <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>
283 * 7XgadOyqv0tF5xG8uhg2iIrheeNKglCWKmxQDgYP1dY 17 127.0.0.1 40305 127.0.0.1 62201 1313283481
285 while ((fgets(line_buf, MAX_LINE_LEN, digest_file_ptr)) != NULL)
288 line_buf[MAX_LINE_LEN-1] = '\0';
290 if(IS_EMPTY_LINE(line_buf[0]))
293 /* Initialize a digest cache list element, and add it into the list if
296 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
298 fprintf(stderr, "Could not allocate digest list element\n");
301 if ((digest_elm->cache_info.digest = calloc(1, MAX_DIGEST_SIZE+1)) == NULL)
304 fprintf(stderr, "Could not allocate digest string\n");
310 if(sscanf(line_buf, "%s %hhu %s %hu %s %hu %ld",
311 digest_elm->cache_info.digest,
312 &(digest_elm->cache_info.proto),
314 &(digest_elm->cache_info.src_port),
316 &(digest_elm->cache_info.dst_port),
321 "*Skipping invalid digest file entry in %s at line %i.\n - %s",
322 opts->config[CONF_DIGEST_FILE], num_lines, line_buf
324 free(digest_elm->cache_info.digest);
328 digest_elm->cache_info.created = time_tmp;
331 if (inet_pton(AF_INET, src_ip, &(digest_elm->cache_info.src_ip)) != 1)
333 free(digest_elm->cache_info.digest);
338 if (inet_pton(AF_INET, dst_ip, &(digest_elm->cache_info.dst_ip)) != 1)
340 free(digest_elm->cache_info.digest);
345 digest_elm->next = opts->digest_cache;
346 opts->digest_cache = digest_elm;
349 if(opts->verbose > 3)
351 "DIGEST FILE: %s, VALID LINE: %s",
352 opts->config[CONF_DIGEST_FILE], line_buf
357 fclose(digest_file_ptr);
362 #else /* USE_FILE_CACHE */
364 /* Check for the existence of the replay dbm file, and create it if it does
365 * not exist. Returns the number of db entries or -1 on error.
368 replay_db_cache_init(fko_srv_options_t *opts)
370 #ifdef NO_DIGEST_CACHE
380 datum db_key, db_ent, 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;
569 int digest_len, res = SPA_MSG_SUCCESS;
571 digest_cache_info_t dc_info;
573 digest_len = strlen(digest);
575 db_key.dptr = digest;
576 db_key.dsize = digest_len;
578 /* Check the db for the key
582 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
585 rpdb = dbm_open(opts->config[CONF_DIGEST_DB_FILE], O_RDWR, 0);
590 log_msg(LOG_WARNING, "Error opening digest_cache: '%s': %s",
591 opts->config[CONF_DIGEST_DB_FILE],
592 MY_DBM_STRERROR(errno)
595 return(SPA_MSG_DIGEST_CACHE_ERROR);
598 db_ent = MY_DBM_FETCH(rpdb, db_key);
600 /* If the datum is not null, we have a match. Otherwise, we add
601 * this entry to the cache.
603 if(db_ent.dptr != NULL)
605 replay_warning(opts, (digest_cache_info_t *)db_ent.dptr);
607 /* Save it back to the digest cache
609 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_REPLACE) != 0)
610 log_msg(LOG_WARNING, "Error updating entry in digest_cache: '%s': %s",
611 opts->config[CONF_DIGEST_DB_FILE],
612 MY_DBM_STRERROR(errno)
618 res = SPA_MSG_REPLAY;
624 #endif /* NO_DIGEST_CACHE */
628 add_replay_dbm_cache(fko_srv_options_t *opts, char *digest)
630 #ifdef NO_DIGEST_CACHE
639 datum db_key, db_ent;
642 int digest_len, res = SPA_MSG_SUCCESS;
644 digest_cache_info_t dc_info;
646 digest_len = strlen(digest);
648 db_key.dptr = digest;
649 db_key.dsize = digest_len;
651 /* Check the db for the key
655 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
658 rpdb = dbm_open(opts->config[CONF_DIGEST_DB_FILE], O_RDWR, 0);
663 log_msg(LOG_WARNING, "Error opening digest_cache: '%s': %s",
664 opts->config[CONF_DIGEST_DB_FILE],
665 MY_DBM_STRERROR(errno)
668 return(SPA_MSG_DIGEST_CACHE_ERROR);
671 db_ent = MY_DBM_FETCH(rpdb, db_key);
673 /* If the datum is null, we have a new entry.
675 if(db_ent.dptr == NULL)
677 /* This is a new SPA packet that needs to be added to the cache.
679 dc_info.src_ip = opts->spa_pkt.packet_src_ip;
680 dc_info.dst_ip = opts->spa_pkt.packet_dst_ip;
681 dc_info.src_port = opts->spa_pkt.packet_src_port;
682 dc_info.dst_port = opts->spa_pkt.packet_dst_port;
683 dc_info.proto = opts->spa_pkt.packet_proto;
684 dc_info.created = time(NULL);
685 dc_info.first_replay = dc_info.last_replay = dc_info.replay_count = 0;
687 db_ent.dsize = sizeof(digest_cache_info_t);
688 db_ent.dptr = (char*)&(dc_info);
690 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_INSERT) != 0)
692 log_msg(LOG_WARNING, "Error adding entry digest_cache: %s",
693 MY_DBM_STRERROR(errno)
696 res = SPA_MSG_DIGEST_CACHE_ERROR;
699 res = SPA_MSG_SUCCESS;
702 res = SPA_MSG_DIGEST_CACHE_ERROR;
707 #endif /* NO_DIGEST_CACHE */
709 #endif /* USE_FILE_CACHE */
712 /* Free replay list memory
715 free_replay_list(fko_srv_options_t *opts)
717 #ifdef NO_DIGEST_CACHE
720 struct digest_cache_list *digest_list_ptr = NULL, *digest_tmp = NULL;
722 if (opts->digest_cache == NULL)
725 digest_list_ptr = opts->digest_cache;
726 while (digest_list_ptr != NULL)
728 digest_tmp = digest_list_ptr->next;
729 if (digest_list_ptr->cache_info.digest != NULL
730 && digest_list_ptr->cache_info.digest[0] != '\0')
732 free(digest_list_ptr->cache_info.digest);
734 free(digest_list_ptr);
735 digest_list_ptr = digest_tmp;