2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Process an incoming SPA data packet for fwknopd.
10 * Copyright 2010 Damien Stuart (dstuart@dstuart.org)
12 * License (GNU Public License):
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 *****************************************************************************
31 #include "fwknopd_common.h"
32 #include "netinet_common.h"
38 #include "incoming_spa.h"
44 #include "fwknopd_errors.h"
45 #include "replay_cache.h"
47 /* Validate and in some cases preprocess/reformat the SPA data. Return an
48 * error code value if there is any indication the data is not valid spa data.
51 preprocess_spa_data(fko_srv_options_t *opts, const char *src_ip)
53 spa_pkt_info_t *spa_pkt = &(opts->spa_pkt);
55 char *ndx = (char *)&(spa_pkt->packet_data);
56 int pkt_data_len = spa_pkt->packet_data_len;
59 /* At this point, we can reset the packet data length to 0. This is our
60 * indicator to the rest of the program that we do not have a current
61 * spa packet to process (after this one that is).
63 spa_pkt->packet_data_len = 0;
65 /* Detect and parse out SPA data from an HTTP request. If the SPA data
66 * starts with "GET /" and the user agent starts with "Fwknop", then
67 * assume it is a SPA over HTTP request.
69 if(strncasecmp(opts->config[CONF_ENABLE_SPA_OVER_HTTP], "N", 1) == 0
70 && strncasecmp(ndx, "GET /", 5) == 0
71 && strstr(ndx, "User-Agent: Fwknop") != NULL)
73 /* This looks like an HTTP request, so let's see if we are
74 * configured to accept such request and if so, find the SPA
78 /* Now extract, adjust (convert characters translated by the fwknop
79 * client), and reset the SPA message itself.
81 strlcpy((char *)spa_pkt->packet_data, ndx+5, pkt_data_len);
83 for(i=0; i<pkt_data_len; i++)
85 if(isspace(*ndx)) /* The first space marks the end of the req */
90 else if(*ndx == '-') /* Convert '-' to '+' */
92 else if(*ndx == '_') /* Convert '_' to '/' */
99 /* Require base64-encoded data
101 if(! is_base64(spa_pkt->packet_data, pkt_data_len))
102 return(SPA_MSG_NOT_SPA_DATA);
105 /* --DSS: Are there other checks we can do here ??? */
107 /* If we made it here, we have no reason to assume this is not SPA data
108 * (at least until we come up with more checks).
113 /* For replay attack detection
116 get_raw_digest(char **digest, char *pkt_data)
118 fko_ctx_t ctx = NULL;
119 char *tmp_digest = NULL;
120 int res = FKO_SUCCESS;
122 /* initialize an FKO context with no decryption key just so
123 * we can get the outer message digest
125 res = fko_new_with_data(&ctx, (char *)pkt_data, NULL);
126 if(res != FKO_SUCCESS)
128 log_msg(LOG_WARNING, "Error initializing FKO context from SPA data: %s",
131 return(SPA_MSG_FKO_CTX_ERROR);
134 res = fko_set_raw_spa_digest_type(ctx, FKO_DEFAULT_DIGEST);
135 if(res != FKO_SUCCESS)
137 log_msg(LOG_WARNING, "Error setting digest type for SPA data: %s",
140 return(SPA_MSG_DIGEST_ERROR);
143 res = fko_set_raw_spa_digest(ctx);
144 if(res != FKO_SUCCESS)
146 log_msg(LOG_WARNING, "Error setting digest for SPA data: %s",
149 return(SPA_MSG_DIGEST_ERROR);
152 res = fko_get_raw_spa_digest(ctx, &tmp_digest);
153 if(res != FKO_SUCCESS)
155 log_msg(LOG_WARNING, "Error getting digest from SPA data: %s",
158 return(SPA_MSG_DIGEST_ERROR);
161 *digest = strdup(tmp_digest);
164 return SPA_MSG_ERROR;
171 /* Popluate a spa_data struct from an initialized (and populated) FKO context.
174 get_spa_data_fields(fko_ctx_t ctx, spa_data_t *spdat)
176 int res = FKO_SUCCESS;
178 res = fko_get_username(ctx, &(spdat->username));
179 if(res != FKO_SUCCESS)
182 res = fko_get_timestamp(ctx, &(spdat->timestamp));
183 if(res != FKO_SUCCESS)
186 res = fko_get_version(ctx, &(spdat->version));
187 if(res != FKO_SUCCESS)
190 res = fko_get_spa_message_type(ctx, &(spdat->message_type));
191 if(res != FKO_SUCCESS)
194 res = fko_get_spa_message(ctx, &(spdat->spa_message));
195 if(res != FKO_SUCCESS)
198 res = fko_get_spa_nat_access(ctx, &(spdat->nat_access));
199 if(res != FKO_SUCCESS)
202 res = fko_get_spa_server_auth(ctx, &(spdat->server_auth));
203 if(res != FKO_SUCCESS)
206 res = fko_get_spa_client_timeout(ctx, (int *)&(spdat->client_timeout));
207 if(res != FKO_SUCCESS)
213 /* Check for access.conf stanza SOURCE match based on SPA packet
217 is_src_match(acc_stanza_t *acc, const uint32_t ip)
221 if(compare_addr_list(acc->source_list, ip))
229 /* Process the SPA packet data
232 incoming_spa(fko_srv_options_t *opts)
234 /* Always a good idea to initialize ctx to null if it will be used
235 * repeatedly (especially when using fko_new_with_data()).
237 fko_ctx_t ctx = NULL;
239 char *spa_ip_demark, *gpg_id, *raw_digest = NULL;
241 int res, status, ts_diff, enc_type, stanza_num=0;
242 int added_replay_digest = 0;
244 spa_pkt_info_t *spa_pkt = &(opts->spa_pkt);
246 /* This will hold our pertinent SPA data.
250 /* Loop through all access stanzas looking for a match
252 acc_stanza_t *acc = opts->acc_stanzas;
254 inet_ntop(AF_INET, &(spa_pkt->packet_src_ip),
255 spadat.pkt_source_ip, sizeof(spadat.pkt_source_ip));
257 /* At this point, we want to validate and (if needed) preprocess the
258 * SPA data and/or to be reasonably sure we have a SPA packet (i.e
259 * try to eliminate obvious non-spa packets).
261 res = preprocess_spa_data(opts, spadat.pkt_source_ip);
262 if(res != FKO_SUCCESS)
264 if(opts->verbose > 1)
265 log_msg(LOG_INFO, "preprocess_spa_data() returned error %i: '%s' for incoming packet.",
266 res, get_errstr(res));
270 if (is_src_match(opts->acc_stanzas, ntohl(spa_pkt->packet_src_ip)))
272 if(strncasecmp(opts->config[CONF_ENABLE_DIGEST_PERSISTENCE], "Y", 1) == 0)
273 /* Check for a replay attack
275 res = get_raw_digest(&raw_digest, (char *)spa_pkt->packet_data);
276 if(res != FKO_SUCCESS)
278 if (raw_digest != NULL)
282 if (raw_digest == NULL)
285 if (is_replay(opts, raw_digest) != SPA_MSG_SUCCESS)
291 "No access data found for source IP: %s", spadat.pkt_source_ip
296 /* Now that we know there is a matching access.conf stanza and the
297 * incoming SPA packet is not a replay, see if we should grant any
304 /* Check for a match for the SPA source IP and the access stanza
306 if(! compare_addr_list(acc->source_list, ntohl(spa_pkt->packet_src_ip)))
312 log_msg(LOG_INFO, "(stanza #%d) SPA Packet from IP: %s received with access source match",
313 stanza_num, spadat.pkt_source_ip);
315 if(opts->verbose > 1)
316 log_msg(LOG_INFO, "SPA Packet: '%s'\n", spa_pkt->packet_data);
318 /* Make sure this access stanza has not expired
320 if(acc->access_expire_time > 0)
329 if(time(NULL) > acc->access_expire_time)
331 log_msg(LOG_INFO, "(stanza #%d) Access stanza has expired",
340 /* Get encryption type and try its decoding routine first (if the key
341 * for that type is set)
343 enc_type = fko_encryption_type((char *)spa_pkt->packet_data);
345 if(enc_type == FKO_ENCRYPTION_RIJNDAEL)
348 res = fko_new_with_data(&ctx,
349 (char *)spa_pkt->packet_data, acc->key, acc->encryption_mode);
353 "(stanza #%d) No KEY for RIJNDAEL encrypted messages",
360 else if(enc_type == FKO_ENCRYPTION_GPG)
362 /* For GPG we create the new context without decrypting on the fly
363 * so we can set some GPG parameters first.
365 if(acc->gpg_decrypt_pw != NULL)
367 res = fko_new_with_data(&ctx, (char *)spa_pkt->packet_data, NULL,
368 acc->encryption_mode);
369 if(res != FKO_SUCCESS)
372 "(stanza #%d) Error creating fko context (before decryption): %s",
373 stanza_num, fko_errstr(res)
379 /* Set whatever GPG parameters we have.
381 if(acc->gpg_home_dir != NULL)
382 res = fko_set_gpg_home_dir(ctx, acc->gpg_home_dir);
383 if(res != FKO_SUCCESS)
386 "(stanza #%d) Error setting GPG keyring path to %s: %s",
387 stanza_num, acc->gpg_home_dir, fko_errstr(res)
393 if(acc->gpg_decrypt_id != NULL)
394 fko_set_gpg_recipient(ctx, acc->gpg_decrypt_id);
396 /* If GPG_REQUIRE_SIG is set for this acc stanza, then set
397 * the FKO context accordingly and check the other GPG Sig-
398 * related parameters. This also applies when REMOTE_ID is
401 if(acc->gpg_require_sig)
403 fko_set_gpg_signature_verify(ctx, 1);
405 /* Set whether or not to ignore signature verification errors.
407 fko_set_gpg_ignore_verify_error(ctx, acc->gpg_ignore_sig_error);
411 fko_set_gpg_signature_verify(ctx, 0);
412 fko_set_gpg_ignore_verify_error(ctx, 1);
415 /* Now decrypt the data.
417 res = fko_decrypt_spa_data(ctx, acc->gpg_decrypt_pw);
423 "(stanza #%d) No GPG_DECRYPT_PW for GPG encrypted messages",
432 log_msg(LOG_ERR, "(stanza #%d) Unable to determing encryption type. Got type=%i.",
433 stanza_num, enc_type);
438 /* Do we have a valid FKO context? Did the SPA decrypt properly?
440 if(res != FKO_SUCCESS)
442 log_msg(LOG_WARNING, "(stanza #%d) Error creating fko context: %s",
443 stanza_num, fko_errstr(res));
445 if(IS_GPG_ERROR(res))
446 log_msg(LOG_WARNING, "(stanza #%d) - GPG ERROR: %s",
447 stanza_num, fko_gpg_errstr(ctx));
455 /* Add this SPA packet into the replay detection cache
457 if (! added_replay_digest)
459 res = add_replay(opts, raw_digest);
460 if (res != SPA_MSG_SUCCESS)
462 log_msg(LOG_WARNING, "(stanza #%d) Could not add digest to replay cache",
469 added_replay_digest = 1;
472 /* At this point, we assume the SPA data is valid. Now we need to see
473 * if it meets our access criteria.
475 if(opts->verbose > 1)
476 log_msg(LOG_INFO, "(stanza #%d) SPA Decode (res=%i):\n%s",
477 stanza_num, res, dump_ctx(ctx));
479 /* First, if this is a GPG message, and GPG_REMOTE_ID list is not empty,
480 * then we need to make sure this incoming message is signer ID matches
481 * an entry in the list.
483 if(enc_type == FKO_ENCRYPTION_GPG && acc->gpg_require_sig)
485 res = fko_get_gpg_signature_id(ctx, &gpg_id);
486 if(res != FKO_SUCCESS)
488 log_msg(LOG_WARNING, "(stanza #%d) Error pulling the GPG signature ID from the context: %s",
489 stanza_num, fko_gpg_errstr(ctx));
497 log_msg(LOG_INFO, "(stanza #%d) Incoming SPA data signed by '%s'.",
500 if(acc->gpg_remote_id != NULL && !acc_check_gpg_remote_id(acc, gpg_id))
503 "(stanza #%d) Incoming SPA packet signed by ID: %s, but that ID is not the GPG_REMOTE_ID list.",
512 /* Populate our spa data struct for future reference.
514 res = get_spa_data_fields(ctx, &spadat);
516 /* Figure out what our timeout will be. If it is specified in the SPA
517 * data, then use that. If not, try the FW_ACCESS_TIMEOUT from the
518 * access.conf file (if there is one). Otherwise use the default.
520 if(spadat.client_timeout > 0)
521 spadat.fw_access_timeout = spadat.client_timeout;
522 else if(acc->fw_access_timeout > 0)
523 spadat.fw_access_timeout = acc->fw_access_timeout;
525 spadat.fw_access_timeout = DEF_FW_ACCESS_TIMEOUT;
527 if(res != FKO_SUCCESS)
529 log_msg(LOG_ERR, "(stanza #%d) Unexpected error pulling SPA data from the context: %s",
530 stanza_num, fko_errstr(res));
538 /* Check packet age if so configured.
540 if(strncasecmp(opts->config[CONF_ENABLE_SPA_PACKET_AGING], "Y", 1) == 0)
544 ts_diff = abs(now_ts - spadat.timestamp);
546 if(ts_diff > atoi(opts->config[CONF_MAX_SPA_PACKET_AGE]))
548 log_msg(LOG_WARNING, "(stanza #%d) SPA data time difference is too great (%i seconds).",
549 stanza_num, ts_diff);
558 /* At this point, we have enough to check the embedded (or packet source)
559 * IP address against the defined access rights. We start by splitting
560 * the spa msg source IP from the remainder of the message.
562 spa_ip_demark = strchr(spadat.spa_message, ',');
563 if(spa_ip_demark == NULL)
565 log_msg(LOG_WARNING, "(stanza #%d) Error parsing SPA message string: %s",
566 stanza_num, fko_errstr(res));
574 strlcpy(spadat.spa_message_src_ip, spadat.spa_message, (spa_ip_demark-spadat.spa_message)+1);
575 strlcpy(spadat.spa_message_remain, spa_ip_demark+1, 1024);
577 /* If use source IP was requested (embedded IP of 0.0.0.0), make sure it
580 if(strcmp(spadat.spa_message_src_ip, "0.0.0.0") == 0)
582 if(acc->require_source_address)
585 "(stanza #%d) Got 0.0.0.0 when valid source IP was required.",
595 spadat.use_src_ip = spadat.pkt_source_ip;
598 spadat.use_src_ip = spadat.spa_message_src_ip;
600 /* If REQUIRE_USERNAME is set, make sure the username in this SPA data
603 if(acc->require_username != NULL)
605 if(strcmp(spadat.username, acc->require_username) != 0)
608 "(stanza #%d) Username in SPA data (%s) does not match required username: %s",
609 stanza_num, spadat.username, acc->require_username
619 /* Take action based on SPA message type.
621 if(spadat.message_type == FKO_LOCAL_NAT_ACCESS_MSG
622 || spadat.message_type == FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG
623 || spadat.message_type == FKO_NAT_ACCESS_MSG
624 || spadat.message_type == FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG)
626 #if FIREWALL_IPTABLES
627 if(strncasecmp(opts->config[CONF_ENABLE_IPT_FORWARDING], "Y", 1)!=0)
630 "(stanza #%d) SPA packet from %s requested NAT access, but is not enabled",
631 stanza_num, spadat.pkt_source_ip
641 "(stanza #%d) SPA packet from %s requested unsupported NAT access",
642 stanza_num, spadat.pkt_source_ip
654 if(spadat.message_type == FKO_COMMAND_MSG)
656 if(!acc->enable_cmd_exec)
659 "(stanza #%d) SPA Command message are not allowed in the current configuration.",
671 "(stanza #%d) Processing SPA Command message: command='%s'.",
672 stanza_num, spadat.spa_message_remain
675 /* Do we need to become another user? If so, we call
676 * run_extcmd_as and pass the cmd_exec_uid.
678 if(acc->cmd_exec_user != NULL && strncasecmp(acc->cmd_exec_user, "root", 4) != 0)
681 log_msg(LOG_INFO, "(stanza #%d) Setting effective user to %s (UID=%i) before running command.",
682 stanza_num, acc->cmd_exec_user, acc->cmd_exec_uid);
685 res = run_extcmd_as(acc->cmd_exec_uid,
686 spadat.spa_message_remain, NULL, 0, 0);
688 else /* Just run it as we are (root that is). */
689 res = run_extcmd(spadat.spa_message_remain, NULL, 0, 5);
691 /* --DSS XXX: I have found that the status (and res for that
692 * matter) have been unreliable indicators of the
693 * actual exit status of some commands. Not sure
694 * why yet. For now, we will take what we get.
696 status = WEXITSTATUS(res);
698 if(opts->verbose > 1)
700 "(stanza #%d) CMD_EXEC: command returned %i",
704 res = SPA_MSG_COMMAND_ERROR;
709 /* we processed the command on a matching access stanza, so we
710 * don't look for anything else to do with this SPA packet
716 /* From this point forward, we have some kind of access message. So
717 * we first see if access is allowed by checking access against
718 * restrict_ports and open_ports.
720 * --DSS TODO: We should add BLACKLIST support here as well.
722 if(! acc_check_port_access(acc, spadat.spa_message_remain))
725 "(stanza #%d) One or more requested protocol/ports was denied per access.conf.",
735 /* At this point, we process the SPA request and break out of the
736 * access stanza loop (first valid access stanza stops us looking
739 process_spa_request(opts, acc, &spadat);
745 if (raw_digest != NULL)