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);
267 /* File exist, and we have access - create in-memory digest cache
269 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "r")) == NULL)
271 log_msg(LOG_WARNING, "Could not open digest cache: %s",
272 opts->config[CONF_DIGEST_FILE]);
277 * <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>
279 * 7XgadOyqv0tF5xG8uhg2iIrheeNKglCWKmxQDgYP1dY 17 127.0.0.1 40305 127.0.0.1 62201 1313283481
281 while ((fgets(line_buf, MAX_LINE_LEN, digest_file_ptr)) != NULL)
284 line_buf[MAX_LINE_LEN-1] = '\0';
286 if(IS_EMPTY_LINE(line_buf[0]))
289 /* Initialize a digest cache list element, and add it into the list if
292 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
294 fprintf(stderr, "Could not allocate digest list element\n");
297 if ((digest_elm->cache_info.digest = calloc(1, MAX_DIGEST_SIZE+1)) == NULL)
300 fprintf(stderr, "Could not allocate digest string\n");
306 if(sscanf(line_buf, "%s %hhu %s %hu %s %hu %ld",
307 digest_elm->cache_info.digest,
308 &(digest_elm->cache_info.proto),
310 &(digest_elm->cache_info.src_port),
312 &(digest_elm->cache_info.dst_port),
317 "*Skipping invalid digest file entry in %s at line %i.\n - %s",
318 opts->config[CONF_DIGEST_FILE], num_lines, line_buf
320 free(digest_elm->cache_info.digest);
324 digest_elm->cache_info.created = time_tmp;
327 if (inet_pton(AF_INET, src_ip, &(digest_elm->cache_info.src_ip)) != 1)
329 free(digest_elm->cache_info.digest);
334 if (inet_pton(AF_INET, dst_ip, &(digest_elm->cache_info.dst_ip)) != 1)
336 free(digest_elm->cache_info.digest);
341 digest_elm->next = opts->digest_cache;
342 opts->digest_cache = digest_elm;
345 if(opts->verbose > 3)
347 "DIGEST FILE: %s, VALID LINE: %s",
348 opts->config[CONF_DIGEST_FILE], line_buf
353 fclose(digest_file_ptr);
358 #else /* USE_FILE_CACHE */
360 /* Check for the existence of the replay dbm file, and create it if it does
361 * not exist. Returns the number of db entries or -1 on error.
364 replay_db_cache_init(fko_srv_options_t *opts)
366 #ifdef NO_DIGEST_CACHE
376 datum db_key, db_ent, db_next_key;
381 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
385 opts->config[CONF_DIGEST_DB_FILE], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR
392 "Unable to open digest cache file: '%s': %s",
393 opts->config[CONF_DIGEST_DB_FILE],
394 MY_DBM_STRERROR(errno)
401 db_key = gdbm_firstkey(rpdb);
403 while (db_key.dptr != NULL)
406 db_next_key = gdbm_nextkey(rpdb, db_key);
408 db_key = db_next_key;
411 for (db_key = dbm_firstkey(rpdb); db_ent.dptr != NULL; db_key = dbm_nextkey(rpdb))
418 #endif /* NO_DIGEST_CACHE */
420 #endif /* USE_FILE_CACHE */
422 /* Take an fko context, pull the digest and use it as the key to check the
423 * replay db (digest cache). Returns 1 if there was a match (a replay),
424 * 0 for no match, and -1 on error.
427 replay_check(fko_srv_options_t *opts, fko_ctx_t ctx)
429 #ifdef NO_DIGEST_CACHE
434 return replay_check_file_cache(opts, ctx);
436 return replay_check_dbm_cache(opts, ctx);
438 #endif /* NO_DIGEST_CACHE */
443 replay_check_file_cache(fko_srv_options_t *opts, fko_ctx_t ctx)
446 char src_ip[INET_ADDRSTRLEN+1] = {0};
447 char dst_ip[INET_ADDRSTRLEN+1] = {0};
448 int res = 0, digest_len = 0;
449 FILE *digest_file_ptr = NULL;
451 struct digest_cache_list *digest_list_ptr = NULL, *digest_elm = NULL;
453 res = fko_get_spa_digest(ctx, &digest);
454 if(res != FKO_SUCCESS)
456 log_msg(LOG_WARNING, "Error getting digest from SPA data: %s",
459 return(SPA_MSG_DIGEST_ERROR);
462 digest_len = strlen(digest);
464 /* Check the cache for the SPA packet digest
466 for (digest_list_ptr = opts->digest_cache;
467 digest_list_ptr != NULL;
468 digest_list_ptr = digest_list_ptr->next) {
470 if (strncmp(digest_list_ptr->cache_info.digest, digest, digest_len) == 0) {
472 replay_warning(opts, &(digest_list_ptr->cache_info));
474 return(SPA_MSG_REPLAY);
478 /* If we make it here, then this is a new SPA packet that needs to be
479 * added to the cache. We've already decrypted the data, so we know that
480 * the contents are valid.
482 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
484 log_msg(LOG_WARNING, "Error calloc() returned NULL for digest cache element",
485 fko_errstr(SPA_MSG_ERROR));
487 return(SPA_MSG_ERROR);
489 if ((digest_elm->cache_info.digest = calloc(1, digest_len+1)) == NULL)
491 log_msg(LOG_WARNING, "Error calloc() returned NULL for digest cache string",
492 fko_errstr(SPA_MSG_ERROR));
494 return(SPA_MSG_ERROR);
497 strlcpy(digest_elm->cache_info.digest, digest, digest_len+1);
498 digest_elm->cache_info.proto = opts->spa_pkt.packet_proto;
499 digest_elm->cache_info.src_ip = opts->spa_pkt.packet_src_ip;
500 digest_elm->cache_info.dst_ip = opts->spa_pkt.packet_dst_ip;
501 digest_elm->cache_info.src_port = opts->spa_pkt.packet_src_port;
502 digest_elm->cache_info.dst_port = opts->spa_pkt.packet_dst_port;
503 digest_elm->cache_info.created = time(NULL);
505 /* First, add the digest at the head of the in-memory list
507 digest_elm->next = opts->digest_cache;
508 opts->digest_cache = digest_elm;
510 /* Now, write the digest to disk
512 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "a")) == NULL)
514 log_msg(LOG_WARNING, "Could not open digest cache: %s",
515 opts->config[CONF_DIGEST_FILE]);
516 return(SPA_MSG_DIGEST_CACHE_ERROR);
519 inet_ntop(AF_INET, &(digest_elm->cache_info.src_ip),
520 src_ip, INET_ADDRSTRLEN);
521 inet_ntop(AF_INET, &(digest_elm->cache_info.dst_ip),
522 dst_ip, INET_ADDRSTRLEN);
523 fprintf(digest_file_ptr, "%s %d %s %d %s %d %d\n",
525 digest_elm->cache_info.proto,
527 (int) digest_elm->cache_info.src_port,
529 digest_elm->cache_info.dst_port,
530 (int) digest_elm->cache_info.created);
532 fclose(digest_file_ptr);
534 return(SPA_MSG_SUCCESS);
536 #endif /* USE_FILE_CACHE */
540 replay_check_dbm_cache(fko_srv_options_t *opts, fko_ctx_t ctx)
542 #ifdef NO_DIGEST_CACHE
551 datum db_key, db_ent;
556 digest_cache_info_t dc_info;
558 res = fko_get_spa_digest(ctx, &digest);
559 if(res != FKO_SUCCESS)
561 log_msg(LOG_WARNING, "Error getting digest from SPA data: %s",
564 return(SPA_MSG_DIGEST_ERROR);
567 digest_len = strlen(digest);
569 db_key.dptr = digest;
570 db_key.dsize = digest_len;
572 /* Check the db for the key
576 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
579 rpdb = dbm_open(opts->config[CONF_DIGEST_DB_FILE], O_RDWR, 0);
584 log_msg(LOG_WARNING, "Error opening digest_cache: '%s': %s",
585 opts->config[CONF_DIGEST_DB_FILE],
586 MY_DBM_STRERROR(errno)
589 return(SPA_MSG_DIGEST_CACHE_ERROR);
592 db_ent = MY_DBM_FETCH(rpdb, db_key);
594 /* If the datum is not null, we have a match. Otherwise, we add
595 * this entry to the cache.
597 if(db_ent.dptr != NULL)
599 replay_warning(opts, (digest_cache_info_t *)db_ent.dptr);
601 /* Save it back to the digest cache
603 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_REPLACE) != 0)
604 log_msg(LOG_WARNING, "Error updating entry in digest_cache: '%s': %s",
605 opts->config[CONF_DIGEST_DB_FILE],
606 MY_DBM_STRERROR(errno)
613 res = SPA_MSG_REPLAY;
615 /* This is a new SPA packet that needs to be added to the cache.
617 dc_info.src_ip = opts->spa_pkt.packet_src_ip;
618 dc_info.dst_ip = opts->spa_pkt.packet_dst_ip;
619 dc_info.src_port = opts->spa_pkt.packet_src_port;
620 dc_info.dst_port = opts->spa_pkt.packet_dst_port;
621 dc_info.proto = opts->spa_pkt.packet_proto;
622 dc_info.created = time(NULL);
623 dc_info.first_replay = dc_info.last_replay = dc_info.replay_count = 0;
625 db_ent.dsize = sizeof(digest_cache_info_t);
626 db_ent.dptr = (char*)&(dc_info);
628 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_INSERT) != 0)
630 log_msg(LOG_WARNING, "Error adding entry digest_cache: %s",
631 MY_DBM_STRERROR(errno)
634 res = SPA_MSG_DIGEST_CACHE_ERROR;
637 res = SPA_MSG_SUCCESS;
643 #endif /* NO_DIGEST_CACHE */
645 #endif /* USE_FILE_CACHE */
648 /* Free replay list memory
651 free_replay_list(fko_srv_options_t *opts)
653 #ifdef NO_DIGEST_CACHE
656 struct digest_cache_list *digest_list_ptr = NULL, *digest_tmp = NULL;
658 if (opts->digest_cache == NULL)
661 digest_list_ptr = opts->digest_cache;
662 while (digest_list_ptr != NULL)
664 digest_tmp = digest_list_ptr->next;
665 if (digest_list_ptr->cache_info.digest != NULL
666 && digest_list_ptr->cache_info.digest[0] != '\0')
668 free(digest_list_ptr->cache_info.digest);
670 free(digest_list_ptr);
671 digest_list_ptr = digest_tmp;