2 *****************************************************************************
6 * Author: Damien S. Stuart, Michael Rash
8 * Purpose: Fwknop routines for managing pf firewall rules.
10 * Copyright 2011 Damien Stuart (dstuart@dstuart.org),
11 * Michael Rash (mbr@cipherdyne.org)
13 * License (GNU Public License):
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 *****************************************************************************
32 #include "fwknopd_common.h"
42 static struct fw_config fwc;
43 static char cmd_buf[CMD_BUFSIZE];
44 static char err_buf[CMD_BUFSIZE];
45 static char cmd_out[STANDARD_CMD_OUT_BUFSIZE];
48 zero_cmd_buffers(void)
50 memset(cmd_buf, 0x0, CMD_BUFSIZE);
51 memset(err_buf, 0x0, CMD_BUFSIZE);
52 memset(cmd_out, 0x0, STANDARD_CMD_OUT_BUFSIZE);
55 /* Print all firewall rules currently instantiated by the running fwknopd
59 fw_dump_rules(const fko_srv_options_t *opts)
63 printf("Listing fwknopd pf rules...\n");
67 /* Create the list command for active rules
69 snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " PF_LIST_ANCHOR_RULES_ARGS,
70 opts->fw_config->fw_command,
71 opts->fw_config->anchor
74 printf("\nActive Rules in PF anchor '%s':\n", opts->fw_config->anchor);
75 res = system(cmd_buf);
77 /* Expect full success on this */
78 if(! EXTCMD_IS_SUCCESS(res))
80 log_msg(LOG_ERR, "Error %i from cmd:'%s': %s", res, cmd_buf, err_buf);
87 /* Check to see if the fwknop anchor is linked into the main policy. If not,
88 * any rules added/deleted by fwknopd will have no effect on real traffic.
91 anchor_active(const fko_srv_options_t *opts)
94 char anchor_search_str[MAX_PF_ANCHOR_SEARCH_LEN] = {0};
96 /* Build our anchor search string
98 snprintf(anchor_search_str, MAX_PF_ANCHOR_SEARCH_LEN-1, "%s\n",
99 opts->fw_config->anchor);
103 snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " PF_ANCHOR_CHECK_ARGS,
104 opts->fw_config->fw_command
107 res = run_extcmd(cmd_buf, cmd_out, STANDARD_CMD_OUT_BUFSIZE, 0);
109 if(!EXTCMD_IS_SUCCESS(res))
111 log_msg(LOG_ERR, "Error %i from cmd:'%s': %s", res, cmd_buf, cmd_out);
115 /* Check to see if the anchor exists and is linked into the main policy
118 if(strstr(cmd_out, anchor_search_str) == NULL)
125 delete_all_anchor_rules(const fko_srv_options_t *opts)
131 snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " PF_DEL_ALL_ANCHOR_RULES,
136 res = run_extcmd(cmd_buf, err_buf, CMD_BUFSIZE, 0);
138 /* Expect full success on this */
139 if(! EXTCMD_IS_SUCCESS(res))
140 log_msg(LOG_ERR, "Error %i from cmd:'%s': %s", res, cmd_buf, err_buf);
146 fw_config_init(fko_srv_options_t *opts)
148 memset(&fwc, 0x0, sizeof(struct fw_config));
150 /* Set our firewall exe command path
152 strlcpy(fwc.fw_command, opts->config[CONF_FIREWALL_EXE], MAX_PATH_LEN);
154 /* Set the PF anchor name
156 strlcpy(fwc.anchor, opts->config[CONF_PF_ANCHOR_NAME], MAX_PF_ANCHOR_LEN);
158 /* Let us find it via our opts struct as well.
160 opts->fw_config = &fwc;
166 fw_initialize(const fko_srv_options_t *opts)
169 if (! anchor_active(opts))
171 fprintf(stderr, "Warning: the fwknop anchor is not active in the pf policy\n");
175 /* Delete any existing rules in the fwknop anchor
177 delete_all_anchor_rules(opts);
183 fw_cleanup(const fko_srv_options_t *opts)
185 delete_all_anchor_rules(opts);
189 /****************************************************************************/
191 /* Rule Processing - Create an access request...
194 process_spa_request(const fko_srv_options_t *opts, const acc_stanza_t *acc, spa_data_t *spadat)
196 char new_rule[MAX_PF_NEW_RULE_LEN];
197 char write_cmd[CMD_BUFSIZE];
199 FILE *pfctl_fd = NULL;
201 acc_port_list_t *port_list = NULL;
202 acc_port_list_t *ple;
204 unsigned int fst_proto;
205 unsigned int fst_port;
211 /* Parse and expand our access message.
213 expand_acc_port_list(&port_list, spadat->spa_message_remain);
215 /* Start at the top of the proto-port list...
219 /* Remember the first proto/port combo in case we need them
220 * for NAT access requests.
222 fst_proto = ple->proto;
223 fst_port = ple->port;
225 /* Set our expire time value.
228 exp_ts = now + spadat->fw_access_timeout;
230 /* For straight access requests, we currently support multiple proto/port
233 if(spadat->message_type == FKO_ACCESS_MSG
234 || spadat->message_type == FKO_CLIENT_TIMEOUT_ACCESS_MSG)
236 /* Create an access command for each proto/port for the source ip.
242 snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " PF_LIST_ANCHOR_RULES_ARGS,
243 opts->fw_config->fw_command,
244 opts->fw_config->anchor
247 /* Cache the current anchor rule set
249 res = run_extcmd(cmd_buf, cmd_out, STANDARD_CMD_OUT_BUFSIZE, 0);
251 /* Build the new rule string
253 memset(new_rule, 0x0, MAX_PF_NEW_RULE_LEN);
254 snprintf(new_rule, MAX_PF_NEW_RULE_LEN-1, PF_ADD_RULE_ARGS "\n",
261 if (strlen(cmd_out) + strlen(new_rule) < STANDARD_CMD_OUT_BUFSIZE)
263 /* We add the rule to the running policy
265 strlcat(cmd_out, new_rule, STANDARD_CMD_OUT_BUFSIZE);
267 memset(write_cmd, 0x0, CMD_BUFSIZE);
269 snprintf(write_cmd, CMD_BUFSIZE-1, "%s " PF_WRITE_ANCHOR_RULES_ARGS,
270 opts->fw_config->fw_command,
271 opts->fw_config->anchor
274 if ((pfctl_fd = popen(write_cmd, "w")) == NULL)
276 log_msg(LOG_WARNING, "Could not execute command: %s",
281 if (fwrite(cmd_out, strlen(cmd_out), 1, pfctl_fd) == 1)
283 log_msg(LOG_INFO, "Added Rule for %s, %s expires at %u",
285 spadat->spa_message_remain,
291 /* Reset the next expected expire time for this chain if it
294 if(fwc.next_expire < now || exp_ts < fwc.next_expire)
295 fwc.next_expire = exp_ts;
298 log_msg(LOG_WARNING, "Could not write rule to pf anchor");
304 /* We don't have enough room to add the new firewall rule,
305 * so throw a warning and bail. Once some of the existing
306 * rules are expired the user will once again be able to gain
307 * access. Note that we don't expect to really ever hit this
308 * limit because of STANDARD_CMD_OUT_BUFSIZE is quite a number
311 log_msg(LOG_WARNING, "Max anchor rules reached, try again later.");
321 /* No other SPA request modes are supported yet.
323 if(spadat->message_type == FKO_LOCAL_NAT_ACCESS_MSG
324 || spadat->message_type == FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG)
326 log_msg(LOG_WARNING, "Local NAT requests are not currently supported.");
328 else if(spadat->message_type == FKO_NAT_ACCESS_MSG
329 || spadat->message_type == FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG)
331 log_msg(LOG_WARNING, "Forwarding/NAT requests are not currently supported.");
340 /* Iterate over the configure firewall access chains and purge expired
344 check_firewall_rules(const fko_srv_options_t *opts)
347 char anchor_rules_copy[STANDARD_CMD_OUT_BUFSIZE];
348 char write_cmd[CMD_BUFSIZE];
349 char *ndx, *tmp_mark, *tmp_ndx, *newline_tmp_ndx;
351 time_t now, rule_exp, min_exp=0;
352 int i=0, res=0, anchor_ndx=0, is_delete=0;
354 FILE *pfctl_fd = NULL;
356 /* If we have not yet reached our expected next expire
359 if(fwc.next_expire == 0)
364 if (fwc.next_expire > now)
369 /* There should be a rule to delete. Get the current list of
370 * rules and delete the ones that are expired.
372 snprintf(cmd_buf, CMD_BUFSIZE-1, "%s " PF_LIST_ANCHOR_RULES_ARGS,
373 opts->fw_config->fw_command,
374 opts->fw_config->anchor
377 res = run_extcmd(cmd_buf, cmd_out, STANDARD_CMD_OUT_BUFSIZE, 0);
379 if(!EXTCMD_IS_SUCCESS(res))
381 log_msg(LOG_ERR, "Error %i from cmd:'%s': %s", res, cmd_buf, cmd_out);
385 /* Find the first _exp_ string (if any).
387 ndx = strstr(cmd_out, EXPIRE_COMMENT_PREFIX);
391 /* we did not find an expected rule.
394 "Did not find expire comment in rules list %i.\n", i);
399 memset(anchor_rules_copy, 0x0, STANDARD_CMD_OUT_BUFSIZE);
401 /* Walk the list and process rules as needed.
405 /* Jump forward and extract the timestamp
407 ndx += strlen(EXPIRE_COMMENT_PREFIX);
409 /* remember this spot for when we look for the next
414 strlcpy(exp_str, ndx, 11);
415 rule_exp = (time_t)atoll(exp_str);
419 /* We are going to delete this rule, and because we rebuild the
420 * PF anchor to include all rules that haven't expired, to delete
421 * this rule we just skip to the next one.
423 log_msg(LOG_INFO, "Deleting rule with expire time of %u.", rule_exp);
425 if (fwc.active_rules > 0)
432 /* The rule has not expired, so copy it into the anchor string that
433 * lists current rules and will be used to feed
434 * 'pfctl -a <anchor> -f -'.
437 /* back up to the previous newline or the beginning of the rules
441 while(--tmp_ndx > cmd_out)
452 /* may sure the rule begins with the string "pass", and make sure
453 * it ends with a newline. Bail if either test fails.
455 if (strlen(tmp_ndx) <= strlen("pass")
456 || strncmp(tmp_ndx, "pass", strlen("pass")) != 0)
461 newline_tmp_ndx = tmp_ndx;
462 while (*newline_tmp_ndx != '\n' && *newline_tmp_ndx != '\0')
467 if (*newline_tmp_ndx != '\n')
470 /* copy the whole rule to the next newline (includes the expiration
473 while (*tmp_ndx != '\n' && *tmp_ndx != '\0'
474 && anchor_ndx < STANDARD_CMD_OUT_BUFSIZE)
476 anchor_rules_copy[anchor_ndx] = *tmp_ndx;
480 anchor_rules_copy[anchor_ndx] = '\n';
483 /* Track the minimum future rule expire time.
486 min_exp = (min_exp < rule_exp) ? min_exp : rule_exp;
489 /* Push our tracking index forward beyond (just processed) _exp_
490 * string so we can continue to the next rule in the list.
492 ndx = strstr(tmp_mark, EXPIRE_COMMENT_PREFIX);
498 /* We re-instantiate the anchor rules with the new rules string that
499 * has the rule(s) deleted. If there isn't at least one "pass" rule,
500 * then we just flush the anchor.
503 if (strlen(anchor_rules_copy) > strlen("pass")
504 && strncmp(anchor_rules_copy, "pass", strlen("pass")) == 0)
506 memset(write_cmd, 0x0, CMD_BUFSIZE);
508 snprintf(write_cmd, CMD_BUFSIZE-1, "%s " PF_WRITE_ANCHOR_RULES_ARGS,
509 opts->fw_config->fw_command,
510 opts->fw_config->anchor
513 if ((pfctl_fd = popen(write_cmd, "w")) == NULL)
515 log_msg(LOG_WARNING, "Could not execute command: %s",
520 if (fwrite(anchor_rules_copy, strlen(anchor_rules_copy), 1, pfctl_fd) != 1)
522 log_msg(LOG_WARNING, "Could not write rules to pf anchor");
528 delete_all_anchor_rules(opts);
533 /* Set the next pending expire time accordingly. 0 if there are no
534 * more rules, or whatever the next expected (min_exp) time will be.
536 if(fwc.active_rules < 1)
539 fwc.next_expire = min_exp;
544 #endif /* FIREWALL_PF */