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 /* Popluate a spa_data struct from an initialized (and populated) FKO context.
116 get_spa_data_fields(fko_ctx_t ctx, spa_data_t *spdat)
118 int res = FKO_SUCCESS;
120 res = fko_get_username(ctx, &(spdat->username));
121 if(res != FKO_SUCCESS)
124 res = fko_get_timestamp(ctx, &(spdat->timestamp));
125 if(res != FKO_SUCCESS)
128 res = fko_get_version(ctx, &(spdat->version));
129 if(res != FKO_SUCCESS)
132 res = fko_get_spa_message_type(ctx, &(spdat->message_type));
133 if(res != FKO_SUCCESS)
136 res = fko_get_spa_message(ctx, &(spdat->spa_message));
137 if(res != FKO_SUCCESS)
140 res = fko_get_spa_nat_access(ctx, &(spdat->nat_access));
141 if(res != FKO_SUCCESS)
144 res = fko_get_spa_server_auth(ctx, &(spdat->server_auth));
145 if(res != FKO_SUCCESS)
148 res = fko_get_spa_client_timeout(ctx, (int *)&(spdat->client_timeout));
149 if(res != FKO_SUCCESS)
155 /* Check for access.conf stanza SOURCE match based on SPA packet
159 is_src_match(acc_stanza_t *acc, const uint32_t ip)
163 if(compare_addr_list(acc->source_list, ip))
171 /* Process the SPA packet data
174 incoming_spa(fko_srv_options_t *opts)
176 /* Always a good idea to initialize ctx to null if it will be used
177 * repeatedly (especially when using fko_new_with_data()).
179 fko_ctx_t ctx = NULL;
181 char *spa_ip_demark, *gpg_id;
183 int res, status, ts_diff, enc_type, found_acc_sip=0, stanza_num=0;
185 spa_pkt_info_t *spa_pkt = &(opts->spa_pkt);
187 /* This will hold our pertinent SPA data.
191 /* Loop through all access stanzas looking for a match
193 acc_stanza_t *acc = opts->acc_stanzas;
195 inet_ntop(AF_INET, &(spa_pkt->packet_src_ip),
196 spadat.pkt_source_ip, sizeof(spadat.pkt_source_ip));
198 /* At this point, we want to validate and (if needed) preprocess the
199 * SPA data and/or to be reasonably sure we have a SPA packet (i.e
200 * try to eliminate obvious non-spa packets).
202 res = preprocess_spa_data(opts, spadat.pkt_source_ip);
203 if(res != FKO_SUCCESS)
205 if(opts->verbose > 1)
206 log_msg(LOG_INFO, "preprocess_spa_data() returned error %i: '%s' for incoming packet.",
207 res, get_errstr(res));
211 if (is_src_match(opts->acc_stanzas, ntohl(spa_pkt->packet_src_ip)))
213 if(strncasecmp(opts->config[CONF_ENABLE_DIGEST_PERSISTENCE], "Y", 1) == 0)
214 /* Check for a replay attack
216 if (is_replay(opts, spa_pkt->packet_data) != SPA_MSG_SUCCESS)
222 "No access data found for source IP: %s", spadat.pkt_source_ip
227 /* Now that we know there is a matching access.conf stanza and the
228 * incoming SPA packet is not a replay, see if we should grant any
235 /* Check for a match for the SPA source IP and the access stanza
237 if(! compare_addr_list(acc->source_list, ntohl(spa_pkt->packet_src_ip)))
245 log_msg(LOG_INFO, "(stanza #%d) SPA Packet from IP: %s received with access source match",
246 stanza_num, spadat.pkt_source_ip);
248 if(opts->verbose > 1)
249 log_msg(LOG_INFO, "SPA Packet: '%s'\n", spa_pkt->packet_data);
251 /* Make sure this access stanza has not expired
253 if(acc->access_expire_time > 0)
262 if(time(NULL) > acc->access_expire_time)
264 log_msg(LOG_INFO, "(stanza #%d) Access stanza has expired",
273 /* Get encryption type and try its decoding routine first (if the key
274 * for that type is set)
276 enc_type = fko_encryption_type((char *)spa_pkt->packet_data);
278 if(enc_type == FKO_ENCRYPTION_RIJNDAEL)
281 res = fko_new_with_data(&ctx, (char *)spa_pkt->packet_data, acc->key);
285 "(stanza #%d) No KEY for RIJNDAEL encrypted messages",
292 else if(enc_type == FKO_ENCRYPTION_GPG)
294 /* For GPG we create the new context without decrypting on the fly
295 * so we can set some GPG parameters first.
297 if(acc->gpg_decrypt_pw != NULL)
299 res = fko_new_with_data(&ctx, (char *)spa_pkt->packet_data, NULL);
300 if(res != FKO_SUCCESS)
303 "(stanza #%d) Error creating fko context (before decryption): %s",
304 stanza_num, fko_errstr(res)
310 /* Set whatever GPG parameters we have.
312 if(acc->gpg_home_dir != NULL)
313 res = fko_set_gpg_home_dir(ctx, acc->gpg_home_dir);
314 if(res != FKO_SUCCESS)
317 "(stanza #%d) Error setting GPG keyring path to %s: %s",
318 stanza_num, acc->gpg_home_dir, fko_errstr(res)
324 if(acc->gpg_decrypt_id != NULL)
325 fko_set_gpg_recipient(ctx, acc->gpg_decrypt_id);
327 /* If GPG_REQUIRE_SIG is set for this acc stanza, then set
328 * the FKO context accordingly and check the other GPG Sig-
329 * related parameters. This also applies when REMOTE_ID is
332 if(acc->gpg_require_sig)
334 fko_set_gpg_signature_verify(ctx, 1);
336 /* Set whether or not to ignore signature verification errors.
338 fko_set_gpg_ignore_verify_error(ctx, acc->gpg_ignore_sig_error);
342 fko_set_gpg_signature_verify(ctx, 0);
343 fko_set_gpg_ignore_verify_error(ctx, 1);
346 /* Now decrypt the data.
348 res = fko_decrypt_spa_data(ctx, acc->gpg_decrypt_pw);
354 "(stanza #%d) No GPG_DECRYPT_PW for GPG encrypted messages",
363 log_msg(LOG_ERR, "(stanza #%d) Unable to determing encryption type. Got type=%i.",
364 stanza_num, enc_type);
369 /* Do we have a valid FKO context?
371 if(res != FKO_SUCCESS)
373 log_msg(LOG_WARNING, "(stanza #%d) Error creating fko context: %s",
374 stanza_num, fko_errstr(res));
376 if(IS_GPG_ERROR(res))
377 log_msg(LOG_WARNING, "(stanza #%d) - GPG ERROR: %s",
378 stanza_num, fko_gpg_errstr(ctx));
386 /* At this point, we assume the SPA data is valid. Now we need to see
387 * if it meets our access criteria.
389 if(opts->verbose > 1)
390 log_msg(LOG_INFO, "(stanza #%d) SPA Decode (res=%i):\n%s",
391 stanza_num, res, dump_ctx(ctx));
393 /* First, if this is a GPG message, and GPG_REMOTE_ID list is not empty,
394 * then we need to make sure this incoming message is signer ID matches
395 * an entry in the list.
397 if(enc_type == FKO_ENCRYPTION_GPG && acc->gpg_require_sig)
399 res = fko_get_gpg_signature_id(ctx, &gpg_id);
400 if(res != FKO_SUCCESS)
402 log_msg(LOG_WARNING, "(stanza #%d) Error pulling the GPG signature ID from the context: %s",
403 stanza_num, fko_gpg_errstr(ctx));
411 log_msg(LOG_INFO, "(stanza #%d) Incoming SPA data signed by '%s'.",
414 if(acc->gpg_remote_id != NULL && !acc_check_gpg_remote_id(acc, gpg_id))
417 "(stanza #%d) Incoming SPA packet signed by ID: %s, but that ID is not the GPG_REMOTE_ID list.",
426 /* Populate our spa data struct for future reference.
428 res = get_spa_data_fields(ctx, &spadat);
430 /* Figure out what our timeout will be. If it is specified in the SPA
431 * data, then use that. If not, try the FW_ACCESS_TIMEOUT from the
432 * access.conf file (if there is one). Otherwise use the default.
434 if(spadat.client_timeout > 0)
435 spadat.fw_access_timeout = spadat.client_timeout;
436 else if(acc->fw_access_timeout > 0)
437 spadat.fw_access_timeout = acc->fw_access_timeout;
439 spadat.fw_access_timeout = DEF_FW_ACCESS_TIMEOUT;
441 if(res != FKO_SUCCESS)
443 log_msg(LOG_ERR, "(stanza #%d) Unexpected error pulling SPA data from the context: %s",
444 stanza_num, fko_errstr(res));
452 /* Check packet age if so configured.
454 if(strncasecmp(opts->config[CONF_ENABLE_SPA_PACKET_AGING], "Y", 1) == 0)
458 ts_diff = abs(now_ts - spadat.timestamp);
460 if(ts_diff > atoi(opts->config[CONF_MAX_SPA_PACKET_AGE]))
462 log_msg(LOG_WARNING, "(stanza #%d) SPA data time difference is too great (%i seconds).",
463 stanza_num, ts_diff);
472 /* At this point, we have enough to check the embedded (or packet source)
473 * IP address against the defined access rights. We start by splitting
474 * the spa msg source IP from the remainder of the message.
476 spa_ip_demark = strchr(spadat.spa_message, ',');
477 if(spa_ip_demark == NULL)
479 log_msg(LOG_WARNING, "(stanza #%d) Error parsing SPA message string: %s",
480 stanza_num, fko_errstr(res));
488 strlcpy(spadat.spa_message_src_ip, spadat.spa_message, (spa_ip_demark-spadat.spa_message)+1);
489 strlcpy(spadat.spa_message_remain, spa_ip_demark+1, 1024);
491 /* If use source IP was requested (embedded IP of 0.0.0.0), make sure it
494 if(strcmp(spadat.spa_message_src_ip, "0.0.0.0") == 0)
496 if(acc->require_source_address)
499 "(stanza #%d) Got 0.0.0.0 when valid source IP was required.",
509 spadat.use_src_ip = spadat.pkt_source_ip;
512 spadat.use_src_ip = spadat.spa_message_src_ip;
514 /* If REQUIRE_USERNAME is set, make sure the username in this SPA data
517 if(acc->require_username != NULL)
519 if(strcmp(spadat.username, acc->require_username) != 0)
522 "(stanza #%d) Username in SPA data (%s) does not match required username: %s",
523 stanza_num, spadat.username, acc->require_username
533 /* Take action based on SPA message type.
535 if(spadat.message_type == FKO_LOCAL_NAT_ACCESS_MSG
536 || spadat.message_type == FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG
537 || spadat.message_type == FKO_NAT_ACCESS_MSG
538 || spadat.message_type == FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG)
540 #if FIREWALL_IPTABLES
541 if(strncasecmp(opts->config[CONF_ENABLE_IPT_FORWARDING], "Y", 1)!=0)
544 "(stanza #%d) SPA packet from %s requested NAT access, but is not enabled",
545 stanza_num, spadat.pkt_source_ip
555 "(stanza #%d) SPA packet from %s requested unsupported NAT access",
556 stanza_num, spadat.pkt_source_ip
568 if(spadat.message_type == FKO_COMMAND_MSG)
570 if(!acc->enable_cmd_exec)
573 "(stanza #%d) SPA Command message are not allowed in the current configuration.",
585 "(stanza #%d) Processing SPA Command message: command='%s'.",
586 stanza_num, spadat.spa_message_remain
589 /* Do we need to become another user? If so, we call
590 * run_extcmd_as and pass the cmd_exec_uid.
592 if(acc->cmd_exec_user != NULL && strncasecmp(acc->cmd_exec_user, "root", 4) != 0)
595 log_msg(LOG_INFO, "(stanza #%d) Setting effective user to %s (UID=%i) before running command.",
596 stanza_num, acc->cmd_exec_user, acc->cmd_exec_uid);
599 res = run_extcmd_as(acc->cmd_exec_uid,
600 spadat.spa_message_remain, NULL, 0, 0);
602 else /* Just run it as we are (root that is). */
603 res = run_extcmd(spadat.spa_message_remain, NULL, 0, 5);
605 /* --DSS XXX: I have found that the status (and res for that
606 * matter) have been unreliable indicators of the
607 * actual exit status of some commands. Not sure
608 * why yet. For now, we will take what we get.
610 status = WEXITSTATUS(res);
612 if(opts->verbose > 1)
614 "(stanza #%d) CMD_EXEC: command returned %i",
618 res = SPA_MSG_COMMAND_ERROR;
623 /* we processed the command on a matching access stanza, so we
624 * don't look for anything else to do with this SPA packet
630 /* From this point forward, we have some kind of access message. So
631 * we first see if access is allowed by checking access against
632 * restrict_ports and open_ports.
634 * --DSS TODO: We should add BLACKLIST support here as well.
636 if(! acc_check_port_access(acc, spadat.spa_message_remain))
639 "(stanza #%d) One or more requested protocol/ports was denied per access.conf.",
649 /* At this point, we process the SPA request and break out of the
650 * access stanza loop (first valid access stanza stops us looking
653 process_spa_request(opts, acc, &spadat);
662 "No access data found for source IP: %s", spadat.pkt_source_ip