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;
172 /* Popluate a spa_data struct from an initialized (and populated) FKO context.
175 get_spa_data_fields(fko_ctx_t ctx, spa_data_t *spdat)
177 int res = FKO_SUCCESS;
179 res = fko_get_username(ctx, &(spdat->username));
180 if(res != FKO_SUCCESS)
183 res = fko_get_timestamp(ctx, &(spdat->timestamp));
184 if(res != FKO_SUCCESS)
187 res = fko_get_version(ctx, &(spdat->version));
188 if(res != FKO_SUCCESS)
191 res = fko_get_spa_message_type(ctx, &(spdat->message_type));
192 if(res != FKO_SUCCESS)
195 res = fko_get_spa_message(ctx, &(spdat->spa_message));
196 if(res != FKO_SUCCESS)
199 res = fko_get_spa_nat_access(ctx, &(spdat->nat_access));
200 if(res != FKO_SUCCESS)
203 res = fko_get_spa_server_auth(ctx, &(spdat->server_auth));
204 if(res != FKO_SUCCESS)
207 res = fko_get_spa_client_timeout(ctx, (int *)&(spdat->client_timeout));
208 if(res != FKO_SUCCESS)
214 /* Check for access.conf stanza SOURCE match based on SPA packet
218 is_src_match(acc_stanza_t *acc, const uint32_t ip)
222 if(compare_addr_list(acc->source_list, ip))
230 /* Process the SPA packet data
233 incoming_spa(fko_srv_options_t *opts)
235 /* Always a good idea to initialize ctx to null if it will be used
236 * repeatedly (especially when using fko_new_with_data()).
238 fko_ctx_t ctx = NULL;
240 char *spa_ip_demark, *gpg_id, *raw_digest = NULL;
242 int res, status, ts_diff, enc_type, stanza_num=0;
243 int added_replay_digest = 0;
245 spa_pkt_info_t *spa_pkt = &(opts->spa_pkt);
247 /* This will hold our pertinent SPA data.
251 /* Loop through all access stanzas looking for a match
253 acc_stanza_t *acc = opts->acc_stanzas;
255 inet_ntop(AF_INET, &(spa_pkt->packet_src_ip),
256 spadat.pkt_source_ip, sizeof(spadat.pkt_source_ip));
258 /* At this point, we want to validate and (if needed) preprocess the
259 * SPA data and/or to be reasonably sure we have a SPA packet (i.e
260 * try to eliminate obvious non-spa packets).
262 res = preprocess_spa_data(opts, spadat.pkt_source_ip);
263 if(res != FKO_SUCCESS)
265 if(opts->verbose > 1)
266 log_msg(LOG_INFO, "preprocess_spa_data() returned error %i: '%s' for incoming packet.",
267 res, get_errstr(res));
271 if (is_src_match(opts->acc_stanzas, ntohl(spa_pkt->packet_src_ip)))
273 if(strncasecmp(opts->config[CONF_ENABLE_DIGEST_PERSISTENCE], "Y", 1) == 0)
274 /* Check for a replay attack
276 res = get_raw_digest(&raw_digest, (char *)spa_pkt->packet_data);
277 if(res != FKO_SUCCESS)
279 if (raw_digest != NULL)
283 if (raw_digest == NULL)
286 if (is_replay(opts, raw_digest) != SPA_MSG_SUCCESS)
295 "No access data found for source IP: %s", spadat.pkt_source_ip
300 /* Now that we know there is a matching access.conf stanza and the
301 * incoming SPA packet is not a replay, see if we should grant any
308 /* Check for a match for the SPA source IP and the access stanza
310 if(! compare_addr_list(acc->source_list, ntohl(spa_pkt->packet_src_ip)))
316 log_msg(LOG_INFO, "(stanza #%d) SPA Packet from IP: %s received with access source match",
317 stanza_num, spadat.pkt_source_ip);
319 if(opts->verbose > 1)
320 log_msg(LOG_INFO, "SPA Packet: '%s'\n", spa_pkt->packet_data);
322 /* Make sure this access stanza has not expired
324 if(acc->access_expire_time > 0)
333 if(time(NULL) > acc->access_expire_time)
335 log_msg(LOG_INFO, "(stanza #%d) Access stanza has expired",
344 /* Get encryption type and try its decoding routine first (if the key
345 * for that type is set)
347 enc_type = fko_encryption_type((char *)spa_pkt->packet_data);
349 if(enc_type == FKO_ENCRYPTION_RIJNDAEL)
352 res = fko_new_with_data(&ctx, (char *)spa_pkt->packet_data, acc->key);
356 "(stanza #%d) No KEY for RIJNDAEL encrypted messages",
363 else if(enc_type == FKO_ENCRYPTION_GPG)
365 /* For GPG we create the new context without decrypting on the fly
366 * so we can set some GPG parameters first.
368 if(acc->gpg_decrypt_pw != NULL)
370 res = fko_new_with_data(&ctx, (char *)spa_pkt->packet_data, NULL);
371 if(res != FKO_SUCCESS)
374 "(stanza #%d) Error creating fko context (before decryption): %s",
375 stanza_num, fko_errstr(res)
381 /* Set whatever GPG parameters we have.
383 if(acc->gpg_home_dir != NULL)
384 res = fko_set_gpg_home_dir(ctx, acc->gpg_home_dir);
385 if(res != FKO_SUCCESS)
388 "(stanza #%d) Error setting GPG keyring path to %s: %s",
389 stanza_num, acc->gpg_home_dir, fko_errstr(res)
395 if(acc->gpg_decrypt_id != NULL)
396 fko_set_gpg_recipient(ctx, acc->gpg_decrypt_id);
398 /* If GPG_REQUIRE_SIG is set for this acc stanza, then set
399 * the FKO context accordingly and check the other GPG Sig-
400 * related parameters. This also applies when REMOTE_ID is
403 if(acc->gpg_require_sig)
405 fko_set_gpg_signature_verify(ctx, 1);
407 /* Set whether or not to ignore signature verification errors.
409 fko_set_gpg_ignore_verify_error(ctx, acc->gpg_ignore_sig_error);
413 fko_set_gpg_signature_verify(ctx, 0);
414 fko_set_gpg_ignore_verify_error(ctx, 1);
417 /* Now decrypt the data.
419 res = fko_decrypt_spa_data(ctx, acc->gpg_decrypt_pw);
425 "(stanza #%d) No GPG_DECRYPT_PW for GPG encrypted messages",
434 log_msg(LOG_ERR, "(stanza #%d) Unable to determing encryption type. Got type=%i.",
435 stanza_num, enc_type);
440 /* Do we have a valid FKO context? Did the SPA decrypt properly?
442 if(res != FKO_SUCCESS)
444 log_msg(LOG_WARNING, "(stanza #%d) Error creating fko context: %s",
445 stanza_num, fko_errstr(res));
447 if(IS_GPG_ERROR(res))
448 log_msg(LOG_WARNING, "(stanza #%d) - GPG ERROR: %s",
449 stanza_num, fko_gpg_errstr(ctx));
457 /* Add this SPA packet into the replay detection cache
459 if (! added_replay_digest)
461 res = add_replay(opts, raw_digest);
462 if (res != SPA_MSG_SUCCESS)
464 log_msg(LOG_WARNING, "(stanza #%d) Could not add digest to replay cache",
471 added_replay_digest = 1;
474 /* At this point, we assume the SPA data is valid. Now we need to see
475 * if it meets our access criteria.
477 if(opts->verbose > 1)
478 log_msg(LOG_INFO, "(stanza #%d) SPA Decode (res=%i):\n%s",
479 stanza_num, res, dump_ctx(ctx));
481 /* First, if this is a GPG message, and GPG_REMOTE_ID list is not empty,
482 * then we need to make sure this incoming message is signer ID matches
483 * an entry in the list.
485 if(enc_type == FKO_ENCRYPTION_GPG && acc->gpg_require_sig)
487 res = fko_get_gpg_signature_id(ctx, &gpg_id);
488 if(res != FKO_SUCCESS)
490 log_msg(LOG_WARNING, "(stanza #%d) Error pulling the GPG signature ID from the context: %s",
491 stanza_num, fko_gpg_errstr(ctx));
499 log_msg(LOG_INFO, "(stanza #%d) Incoming SPA data signed by '%s'.",
502 if(acc->gpg_remote_id != NULL && !acc_check_gpg_remote_id(acc, gpg_id))
505 "(stanza #%d) Incoming SPA packet signed by ID: %s, but that ID is not the GPG_REMOTE_ID list.",
514 /* Populate our spa data struct for future reference.
516 res = get_spa_data_fields(ctx, &spadat);
518 /* Figure out what our timeout will be. If it is specified in the SPA
519 * data, then use that. If not, try the FW_ACCESS_TIMEOUT from the
520 * access.conf file (if there is one). Otherwise use the default.
522 if(spadat.client_timeout > 0)
523 spadat.fw_access_timeout = spadat.client_timeout;
524 else if(acc->fw_access_timeout > 0)
525 spadat.fw_access_timeout = acc->fw_access_timeout;
527 spadat.fw_access_timeout = DEF_FW_ACCESS_TIMEOUT;
529 if(res != FKO_SUCCESS)
531 log_msg(LOG_ERR, "(stanza #%d) Unexpected error pulling SPA data from the context: %s",
532 stanza_num, fko_errstr(res));
540 /* Check packet age if so configured.
542 if(strncasecmp(opts->config[CONF_ENABLE_SPA_PACKET_AGING], "Y", 1) == 0)
546 ts_diff = abs(now_ts - spadat.timestamp);
548 if(ts_diff > atoi(opts->config[CONF_MAX_SPA_PACKET_AGE]))
550 log_msg(LOG_WARNING, "(stanza #%d) SPA data time difference is too great (%i seconds).",
551 stanza_num, ts_diff);
560 /* At this point, we have enough to check the embedded (or packet source)
561 * IP address against the defined access rights. We start by splitting
562 * the spa msg source IP from the remainder of the message.
564 spa_ip_demark = strchr(spadat.spa_message, ',');
565 if(spa_ip_demark == NULL)
567 log_msg(LOG_WARNING, "(stanza #%d) Error parsing SPA message string: %s",
568 stanza_num, fko_errstr(res));
576 strlcpy(spadat.spa_message_src_ip,
577 spadat.spa_message, (spa_ip_demark-spadat.spa_message)+1);
579 if(strnlen(spadat.spa_message_src_ip,
580 MIN_IPV4_STR_LEN) < MIN_IPV4_STR_LEN)
582 log_msg(LOG_WARNING, "(stanza #%d) Invalid source IP in SPA message, ignoring SPA packet",
583 stanza_num, fko_errstr(res));
591 strlcpy(spadat.spa_message_remain, spa_ip_demark+1, 1024);
593 /* If use source IP was requested (embedded IP of 0.0.0.0), make sure it
596 if(strcmp(spadat.spa_message_src_ip, "0.0.0.0") == 0)
598 if(acc->require_source_address)
601 "(stanza #%d) Got 0.0.0.0 when valid source IP was required.",
611 spadat.use_src_ip = spadat.pkt_source_ip;
614 spadat.use_src_ip = spadat.spa_message_src_ip;
616 /* If REQUIRE_USERNAME is set, make sure the username in this SPA data
619 if(acc->require_username != NULL)
621 if(strcmp(spadat.username, acc->require_username) != 0)
624 "(stanza #%d) Username in SPA data (%s) does not match required username: %s",
625 stanza_num, spadat.username, acc->require_username
635 /* Take action based on SPA message type.
637 if(spadat.message_type == FKO_LOCAL_NAT_ACCESS_MSG
638 || spadat.message_type == FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG
639 || spadat.message_type == FKO_NAT_ACCESS_MSG
640 || spadat.message_type == FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG)
642 #if FIREWALL_IPTABLES
643 if(strncasecmp(opts->config[CONF_ENABLE_IPT_FORWARDING], "Y", 1)!=0)
646 "(stanza #%d) SPA packet from %s requested NAT access, but is not enabled",
647 stanza_num, spadat.pkt_source_ip
657 "(stanza #%d) SPA packet from %s requested unsupported NAT access",
658 stanza_num, spadat.pkt_source_ip
670 if(spadat.message_type == FKO_COMMAND_MSG)
672 if(!acc->enable_cmd_exec)
675 "(stanza #%d) SPA Command message are not allowed in the current configuration.",
687 "(stanza #%d) Processing SPA Command message: command='%s'.",
688 stanza_num, spadat.spa_message_remain
691 /* Do we need to become another user? If so, we call
692 * run_extcmd_as and pass the cmd_exec_uid.
694 if(acc->cmd_exec_user != NULL && strncasecmp(acc->cmd_exec_user, "root", 4) != 0)
697 log_msg(LOG_INFO, "(stanza #%d) Setting effective user to %s (UID=%i) before running command.",
698 stanza_num, acc->cmd_exec_user, acc->cmd_exec_uid);
701 res = run_extcmd_as(acc->cmd_exec_uid,
702 spadat.spa_message_remain, NULL, 0, 0);
704 else /* Just run it as we are (root that is). */
705 res = run_extcmd(spadat.spa_message_remain, NULL, 0, 5);
707 /* --DSS XXX: I have found that the status (and res for that
708 * matter) have been unreliable indicators of the
709 * actual exit status of some commands. Not sure
710 * why yet. For now, we will take what we get.
712 status = WEXITSTATUS(res);
714 if(opts->verbose > 1)
716 "(stanza #%d) CMD_EXEC: command returned %i",
720 res = SPA_MSG_COMMAND_ERROR;
725 /* we processed the command on a matching access stanza, so we
726 * don't look for anything else to do with this SPA packet
732 /* From this point forward, we have some kind of access message. So
733 * we first see if access is allowed by checking access against
734 * restrict_ports and open_ports.
736 * --DSS TODO: We should add BLACKLIST support here as well.
738 if(! acc_check_port_access(acc, spadat.spa_message_remain))
741 "(stanza #%d) One or more requested protocol/ports was denied per access.conf.",
751 /* At this point, we process the SPA request and break out of the
752 * access stanza loop (first valid access stanza stops us looking
755 process_spa_request(opts, acc, &spadat);
761 if (raw_digest != NULL)