2 ******************************************************************************
6 * Author: Damien Stuart
8 * Purpose: Access.conf file processing for fwknop server.
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 ******************************************************************************
34 #include <sys/socket.h>
37 #include "fwknopd_common.h"
38 #include <arpa/inet.h>
44 /* Add an access string entry
47 add_acc_string(char **var, const char *val)
52 if((*var = strdup(val)) == NULL)
55 "Fatal memory allocation error adding access list entry: %s", var
61 /* Add an access int entry
64 add_acc_int(int *var, const char *val)
66 return(*var = atoi(val));
69 /* Add an access bool entry (unsigned char of 1 or 0)
72 add_acc_bool(unsigned char *var, const char *val)
74 return(*var = (strncasecmp(val, "Y", 1) == 0) ? 1 : 0);
77 /* Add expiration time - convert date to epoch seconds
80 add_acc_expire_time(fko_srv_options_t *opts, time_t *access_expire_time, const char *val)
84 memset(&tm, 0, sizeof(struct tm));
86 if (sscanf(val, "%2d/%2d/%4d", &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3)
90 "Fatal: invalid date value '%s' (need MM/DD/YYYY) for access stanza expiration time",
93 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
97 tm.tm_mon -= 1; /* 0-11 */
99 /* number of years since 1900
101 if(tm.tm_year > 1900)
107 *access_expire_time = mktime(&tm);
112 /* Add expiration time via epoch seconds defined in access.conf
115 add_acc_expire_time_epoch(fko_srv_options_t *opts, time_t *access_expire_time, const char *val)
118 unsigned long expire_time = 0;
122 expire_time = (time_t) strtoul(val, &endptr, 10);
124 if (errno == ERANGE || (errno != 0 && expire_time == 0))
127 "Fatal: invalid epoch seconds value '%s' for access stanza expiration time",
130 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
133 *access_expire_time = (time_t) expire_time;
138 #if FIREWALL_IPTABLES
140 add_acc_force_nat(fko_srv_options_t *opts, acc_stanza_t *curr_acc, const char *val)
142 char ip_str[MAX_IPV4_STR_LEN] = {0};
144 if (sscanf(val, "%15s %5u", ip_str, &curr_acc->force_nat_port) != 2)
148 "Fatal: invalid FORCE_NAT arg '%s', need <IP> <PORT>",
151 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
154 if (curr_acc->force_nat_port > MAX_PORT)
157 "Fatal: invalid FORCE_NAT port '%d'", curr_acc->force_nat_port);
158 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
161 curr_acc->force_nat = 1;
162 add_acc_string(&(curr_acc->force_nat_ip), ip_str);
168 /* Take an IP or Subnet/Mask and convert it to mask for later
169 * comparisons of incoming source IPs against this mask.
172 add_source_mask(acc_stanza_t *acc, const char *ip)
175 char ip_str[MAX_IPV4_STR_LEN] = {0};
180 acc_int_list_t *last_sle, *new_sle, *tmp_sle;
182 if((new_sle = calloc(1, sizeof(acc_int_list_t))) == NULL)
185 "Fatal memory allocation error adding stanza source_list entry"
190 /* If this is not the first entry, we walk our pointer to the
193 if(acc->source_list == NULL)
195 acc->source_list = new_sle;
199 tmp_sle = acc->source_list;
203 } while((tmp_sle = tmp_sle->next));
205 last_sle->next = new_sle;
208 /* Convert the IP data into the appropriate mask
210 if(strcasecmp(ip, "ANY") == 0)
212 new_sle->maddr = 0x0;
217 /* See if we have a subnet component. If so pull out the IP and
218 * mask values, then create the final mask value.
220 if((ndx = strchr(ip, '/')) != NULL)
223 strlcpy(ip_str, ip, (ndx-ip)+1);
228 strlcpy(ip_str, ip, strlen(ip)+1);
231 if(inet_aton(ip_str, &in) == 0)
234 "Error parsing IP to int for: %s", ip_str
243 /* Store our mask converted from CIDR to a 32-bit value.
245 new_sle->mask = (0xFFFFFFFF << (32 - mask));
247 /* Store our masked address for comparisons with future incoming
250 new_sle->maddr = ntohl(in.s_addr) & new_sle->mask;
255 /* Expand the access SOURCE string to a list of masks.
258 expand_acc_source(acc_stanza_t *acc)
261 char buf[ACCESS_BUF_LEN];
266 for(ndx = start; *ndx; ndx++)
270 /* Skip over any leading whitespace.
272 while(isspace(*start))
275 if(((ndx-start)+1) >= ACCESS_BUF_LEN)
278 strlcpy(buf, start, (ndx-start)+1);
279 res = add_source_mask(acc, buf);
286 /* Skip over any leading whitespace (once again for the last in the list).
288 while(isspace(*start))
291 if(((ndx-start)+1) >= ACCESS_BUF_LEN)
294 strlcpy(buf, start, (ndx-start)+1);
295 res = add_source_mask(acc, buf);
301 parse_proto_and_port(char *pstr, int *proto, int *port)
304 char proto_str[ACCESS_BUF_LEN];
306 /* Parse the string into its components.
308 if((ndx = strchr(pstr, '/')) == NULL)
311 "Parse error on access port entry: %s", pstr);
316 if(((ndx - pstr)+1) >= ACCESS_BUF_LEN)
319 "Parse error on access port entry: %s", pstr);
323 strlcpy(proto_str, pstr, (ndx - pstr)+1);
327 if((*port < 0) || (*port > MAX_PORT))
330 "Invalid port in access request: %s", pstr);
334 if(strcasecmp(proto_str, "tcp") == 0)
336 else if(strcasecmp(proto_str, "udp") == 0)
341 "Invalid protocol in access port entry: %s", pstr);
348 /* Take a proto/port string and convert it to appropriate integer values
349 * for comparisons of incoming SPA requests.
352 add_port_list_ent(acc_port_list_t **plist, char *port_str)
356 acc_port_list_t *last_plist, *new_plist, *tmp_plist;
358 /* Parse the string into its components and continue only if there
359 * are no problems with the incoming string.
361 if(parse_proto_and_port(port_str, &proto_int, &port) != 0)
364 if((new_plist = calloc(1, sizeof(acc_port_list_t))) == NULL)
367 "Fatal memory allocation error adding stanza source_list entry"
372 /* If this is not the first entry, we walk our pointer to the
384 last_plist = tmp_plist;
385 } while((tmp_plist = tmp_plist->next));
387 last_plist->next = new_plist;
390 new_plist->proto = proto_int;
391 new_plist->port = port;
394 /* Add a string list entry to the given acc_string_list.
397 add_string_list_ent(acc_string_list_t **stlist, const char *str_str)
399 acc_string_list_t *last_stlist, *new_stlist, *tmp_stlist;
401 if((new_stlist = calloc(1, sizeof(acc_string_list_t))) == NULL)
404 "Fatal memory allocation error creating string list entry"
409 /* If this is not the first entry, we walk our pointer to the
414 *stlist = new_stlist;
418 tmp_stlist = *stlist;
421 last_stlist = tmp_stlist;
422 } while((tmp_stlist = tmp_stlist->next));
424 last_stlist->next = new_stlist;
427 if(new_stlist->str != NULL)
428 free(new_stlist->str);
430 new_stlist->str = strdup(str_str);
432 if(new_stlist->str == NULL)
435 "Fatal memory allocation error adding string list entry item"
442 /* Expand a proto/port access string to a list of access proto-port struct.
445 expand_acc_port_list(acc_port_list_t **plist, char *plist_str)
448 char buf[ACCESS_BUF_LEN];
452 for(ndx = start; *ndx != '\0'; ndx++)
456 /* Skip over any leading whitespace.
458 while(isspace(*start))
461 if(((ndx-start)+1) >= ACCESS_BUF_LEN)
464 strlcpy(buf, start, (ndx-start)+1);
465 add_port_list_ent(plist, buf);
470 /* Skip over any leading whitespace (once again for the last in the list).
472 while(isspace(*start))
475 if(((ndx-start)+1) >= ACCESS_BUF_LEN)
478 strlcpy(buf, start, (ndx-start)+1);
480 add_port_list_ent(plist, buf);
485 /* Expand a comma-separated string into a simple acc_string_list.
488 expand_acc_string_list(acc_string_list_t **stlist, char *stlist_str)
495 for(ndx = start; *ndx; ndx++)
499 /* Skip over any leading whitespace.
501 while(isspace(*start))
504 if(((ndx-start)+1) >= 1024)
506 fprintf(stderr, "Fatal str->list too long");
510 strlcpy(buf, start, (ndx-start)+1);
511 add_string_list_ent(stlist, buf);
516 /* Skip over any leading whitespace (once again for the last in the list).
518 while(isspace(*start))
521 if(((ndx-start)+1) >= 1024)
523 fprintf(stderr, "Fatal str->list too long");
527 strlcpy(buf, start, (ndx-start)+1);
529 add_string_list_ent(stlist, buf);
532 /* Free the acc source_list
535 free_acc_source_list(acc_int_list_t *sle)
537 acc_int_list_t *last_sle;
542 sle = last_sle->next;
551 free_acc_port_list(acc_port_list_t *ple)
553 acc_port_list_t *last_ple;
558 ple = last_ple->next;
564 /* Free a string_list
567 free_acc_string_list(acc_string_list_t *stl)
569 acc_string_list_t *last_stl;
574 stl = last_stl->next;
581 /* Free any allocated content of an access stanza.
583 * NOTE: If a new access.conf parameter is created, and it is a string
584 * value, it also needs to be added to the list of items to check
588 free_acc_stanza_data(acc_stanza_t *acc)
591 if(acc->source != NULL)
594 free_acc_source_list(acc->source_list);
597 if(acc->open_ports != NULL)
599 free(acc->open_ports);
600 free_acc_port_list(acc->oport_list);
603 if(acc->restrict_ports != NULL)
605 free(acc->restrict_ports);
606 free_acc_port_list(acc->rport_list);
609 if(acc->force_nat_ip != NULL)
610 free(acc->force_nat_ip);
615 if(acc->cmd_exec_user != NULL)
616 free(acc->cmd_exec_user);
618 if(acc->require_username != NULL)
619 free(acc->require_username);
621 if(acc->gpg_home_dir != NULL)
622 free(acc->gpg_home_dir);
624 if(acc->gpg_decrypt_id != NULL)
625 free(acc->gpg_decrypt_id);
627 if(acc->gpg_decrypt_pw != NULL)
628 free(acc->gpg_decrypt_pw);
630 if(acc->gpg_remote_id != NULL)
632 free(acc->gpg_remote_id);
633 free_acc_string_list(acc->gpg_remote_id_list);
637 /* Expand any access entries that may be multi-value.
640 expand_acc_ent_lists(fko_srv_options_t *opts)
642 acc_stanza_t *acc = opts->acc_stanzas;
644 /* We need to do this for each stanza.
648 /* Expand the source string to 32-bit integer masks foreach entry.
650 if(expand_acc_source(acc) == 0)
656 /* Now expand the open_ports string.
658 if(acc->open_ports != NULL && strlen(acc->open_ports))
659 expand_acc_port_list(&(acc->oport_list), acc->open_ports);
661 if(acc->restrict_ports != NULL && strlen(acc->restrict_ports))
662 expand_acc_port_list(&(acc->rport_list), acc->restrict_ports);
664 /* Expand the GPG_REMOTE_ID string.
666 if(acc->gpg_remote_id != NULL && strlen(acc->gpg_remote_id))
667 expand_acc_string_list(&(acc->gpg_remote_id_list), acc->gpg_remote_id);
674 free_acc_stanzas(fko_srv_options_t *opts)
676 acc_stanza_t *acc, *last_acc;
678 /* Free any resources first (in case of reconfig). Assume non-NULL
679 * entry needs to be freed.
681 acc = opts->acc_stanzas;
686 acc = last_acc->next;
688 free_acc_stanza_data(last_acc);
695 /* Wrapper for free_acc_stanzas(), we may put additional initialization
699 acc_stanza_init(fko_srv_options_t *opts)
701 /* Free any resources first (in case of reconfig). Assume non-NULL
702 * entry needs to be freed.
704 free_acc_stanzas(opts);
709 /* Add a new stanza bay allocating the required memory at the required
710 * location, yada-yada-yada.
713 acc_stanza_add(fko_srv_options_t *opts)
715 acc_stanza_t *acc = opts->acc_stanzas;
716 acc_stanza_t *new_acc = calloc(1, sizeof(acc_stanza_t));
717 acc_stanza_t *last_acc;
722 "Fatal memory allocation error adding access stanza"
724 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
727 /* If this is not the first acc entry, we walk our acc pointer to the
728 * end of the existing list.
732 opts->acc_stanzas = new_acc;
738 } while((acc = acc->next));
740 last_acc->next = new_acc;
746 /* Scan the access options for entries that have not bees set, but need
750 set_acc_defaults(fko_srv_options_t *opts)
752 acc_stanza_t *acc = opts->acc_stanzas;
759 /* set default fw_access_timeout if necessary
761 if(acc->fw_access_timeout < 1)
762 acc->fw_access_timeout = DEF_FW_ACCESS_TIMEOUT;
764 /* set default gpg keyring path if necessary
766 if(acc->gpg_decrypt_pw != NULL && acc->gpg_home_dir == NULL)
767 add_acc_string(&(acc->gpg_home_dir), opts->config[CONF_GPG_HOME_DIR]);
773 /* Perform some sanity checks on an acc stanza data.
776 acc_data_is_valid(const acc_stanza_t *acc)
778 if(((acc->key == NULL || !strlen(acc->key))
779 && (acc->gpg_decrypt_pw == NULL || !strlen(acc->gpg_decrypt_pw)))
780 || (acc->use_rijndael == 0 && acc->use_gpg == 0 && acc->gpg_allow_no_pw == 0))
783 "[*] No keys found for access stanza source: '%s'\n", acc->source
791 /* Read and parse the access file, popluating the access data as we go.
794 parse_access_file(fko_srv_options_t *opts)
799 unsigned int num_lines = 0;
801 char access_line_buf[MAX_LINE_LEN] = {0};
802 char var[MAX_LINE_LEN] = {0};
803 char val[MAX_LINE_LEN] = {0};
808 acc_stanza_t *curr_acc = NULL;
810 /* First see if the access file exists. If it doesn't, complain
813 if(stat(opts->config[CONF_ACCESS_FILE], &st) != 0)
815 fprintf(stderr, "[*] Access file: '%s' was not found.\n",
816 opts->config[CONF_ACCESS_FILE]);
818 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
821 verify_file_perms_ownership(opts->config[CONF_ACCESS_FILE]);
823 if ((file_ptr = fopen(opts->config[CONF_ACCESS_FILE], "r")) == NULL)
825 fprintf(stderr, "[*] Could not open access file: %s\n",
826 opts->config[CONF_ACCESS_FILE]);
829 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
832 /* Initialize the access list.
834 acc_stanza_init(opts);
836 /* Now walk through access file pulling the access entries into the
839 while ((fgets(access_line_buf, MAX_LINE_LEN, file_ptr)) != NULL)
842 access_line_buf[MAX_LINE_LEN-1] = '\0';
844 /* Get past comments and empty lines (note: we only look at the
847 if(IS_EMPTY_LINE(access_line_buf[0]))
850 if(sscanf(access_line_buf, "%s %[^;\n\r]", var, val) != 2)
853 "*Invalid access file entry in %s at line %i.\n - '%s'",
854 opts->config[CONF_ACCESS_FILE], num_lines, access_line_buf
859 /* Remove any colon that may be on the end of the var
861 if((ndx = strrchr(var, ':')) != NULL)
866 if(opts->verbose > 3)
868 "ACCESS FILE: %s, LINE: %s\tVar: %s, Val: '%s'\n",
869 opts->config[CONF_ACCESS_FILE], access_line_buf, var, val
872 /* Process the entry.
874 * NOTE: If a new access.conf parameter is created. It also needs
875 * to be accounted for in the following if/if else construct.
878 if(CONF_VAR_IS(var, "SOURCE"))
880 /* If this is not the first stanza, sanity check the previous
881 * stanza for the minimum required data.
883 if(curr_acc != NULL) {
884 if(!acc_data_is_valid(curr_acc))
887 "[*] Data error in access file: '%s'\n",
888 opts->config[CONF_ACCESS_FILE]);
889 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
895 curr_acc = acc_stanza_add(opts);
897 add_acc_string(&(curr_acc->source), val);
901 else if (curr_acc == NULL)
903 /* The stanza must start with the "SOURCE" variable
907 else if(CONF_VAR_IS(var, "OPEN_PORTS"))
909 add_acc_string(&(curr_acc->open_ports), val);
911 else if(CONF_VAR_IS(var, "RESTRICT_PORTS"))
913 add_acc_string(&(curr_acc->restrict_ports), val);
915 else if(CONF_VAR_IS(var, "KEY"))
917 if(strcasecmp(val, "__CHANGEME__") == 0)
920 "[*] KEY value is not properly set in stanza source '%s' in access file: '%s'\n",
921 curr_acc->source, opts->config[CONF_ACCESS_FILE]);
922 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
924 add_acc_string(&(curr_acc->key), val);
925 add_acc_bool(&(curr_acc->use_rijndael), "Y");
927 else if(CONF_VAR_IS(var, "FW_ACCESS_TIMEOUT"))
929 add_acc_int(&(curr_acc->fw_access_timeout), val);
931 else if(CONF_VAR_IS(var, "ENABLE_CMD_EXEC"))
933 add_acc_bool(&(curr_acc->enable_cmd_exec), val);
935 else if(CONF_VAR_IS(var, "CMD_EXEC_USER"))
937 add_acc_string(&(curr_acc->cmd_exec_user), val);
944 fprintf(stderr, "Unable to determine UID for CMD_EXEC_USER: %s.\n",
945 errno ? strerror(errno) : "Not a user on this system");
946 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
949 curr_acc->cmd_exec_uid = pw->pw_uid;
951 else if(CONF_VAR_IS(var, "REQUIRE_USERNAME"))
953 add_acc_string(&(curr_acc->require_username), val);
955 else if(CONF_VAR_IS(var, "REQUIRE_SOURCE_ADDRESS"))
957 add_acc_bool(&(curr_acc->require_source_address), val);
959 else if(CONF_VAR_IS(var, "REQUIRE_SOURCE")) /* synonym for REQUIRE_SOURCE_ADDRESS */
961 add_acc_bool(&(curr_acc->require_source_address), val);
963 else if(CONF_VAR_IS(var, "GPG_HOME_DIR"))
965 if (is_valid_dir(val))
967 add_acc_string(&(curr_acc->gpg_home_dir), val);
972 "[*] GPG_HOME_DIR directory '%s' stat()/existence problem in stanza source '%s' in access file: '%s'\n",
973 val, curr_acc->source, opts->config[CONF_ACCESS_FILE]);
974 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
977 else if(CONF_VAR_IS(var, "GPG_DECRYPT_ID"))
979 add_acc_string(&(curr_acc->gpg_decrypt_id), val);
981 else if(CONF_VAR_IS(var, "GPG_DECRYPT_PW"))
983 if(strcasecmp(val, "__CHANGEME__") == 0)
986 "[*] GPG_DECRYPT_PW value is not properly set in stanza source '%s' in access file: '%s'\n",
987 curr_acc->source, opts->config[CONF_ACCESS_FILE]);
988 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
990 add_acc_string(&(curr_acc->gpg_decrypt_pw), val);
991 add_acc_bool(&(curr_acc->use_gpg), "Y");
993 else if(CONF_VAR_IS(var, "GPG_ALLOW_NO_PW"))
995 add_acc_bool(&(curr_acc->gpg_allow_no_pw), val);
996 if(curr_acc->gpg_allow_no_pw == 1)
998 add_acc_bool(&(curr_acc->use_gpg), "Y");
999 if(curr_acc->gpg_decrypt_pw != NULL && curr_acc->gpg_decrypt_pw[0] != '\0')
1000 free(curr_acc->gpg_decrypt_pw);
1001 add_acc_string(&(curr_acc->gpg_decrypt_pw), "");
1004 else if(CONF_VAR_IS(var, "GPG_REQUIRE_SIG"))
1006 add_acc_bool(&(curr_acc->gpg_require_sig), val);
1008 else if(CONF_VAR_IS(var, "GPG_IGNORE_SIG_VERIFY_ERROR"))
1010 add_acc_bool(&(curr_acc->gpg_ignore_sig_error), val);
1012 else if(CONF_VAR_IS(var, "GPG_REMOTE_ID"))
1014 add_acc_string(&(curr_acc->gpg_remote_id), val);
1016 else if(CONF_VAR_IS(var, "ACCESS_EXPIRE"))
1018 add_acc_expire_time(opts, &(curr_acc->access_expire_time), val);
1020 else if(CONF_VAR_IS(var, "ACCESS_EXPIRE_EPOCH"))
1022 add_acc_expire_time_epoch(opts, &(curr_acc->access_expire_time), val);
1024 else if(CONF_VAR_IS(var, "FORCE_NAT"))
1026 #if FIREWALL_IPTABLES
1027 if(strncasecmp(opts->config[CONF_ENABLE_IPT_FORWARDING], "Y", 1) !=0 )
1030 "[*] FORCE_NAT requires ENABLE_IPT_FORWARDING to be enabled in fwknopd.conf\n");
1031 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
1033 add_acc_force_nat(opts, curr_acc, val);
1036 "[*] FORCE_NAT not supported.\n");
1037 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
1043 "*Ignoring unknown access parameter: '%s' in %s\n",
1044 var, opts->config[CONF_ACCESS_FILE]
1051 /* Basic check to ensure that we got at least one SOURCE stanza with
1052 * a valid KEY defined (valid meaning it has a value that is not
1055 if (got_source == 0)
1058 "[*] Could not find valid SOURCE stanza in access file: '%s'\n",
1059 opts->config[CONF_ACCESS_FILE]);
1060 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
1063 /* Sanity check the last stanza
1065 if(!acc_data_is_valid(curr_acc))
1068 "[*] Data error in access file: '%s'\n",
1069 opts->config[CONF_ACCESS_FILE]);
1070 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
1073 /* Expand our the expandable fields into their respective data buckets.
1076 expand_acc_ent_lists(opts);
1078 /* Make sure default values are set where needed.
1081 set_acc_defaults(opts);
1087 compare_addr_list(acc_int_list_t *source_list, const uint32_t ip)
1093 if((ip & source_list->mask) == (source_list->maddr & source_list->mask))
1099 source_list = source_list->next;
1105 /* Compare the contents of 2 port lists. Return true on a match.
1106 * Match depends on the match_any flag. if match_any is 1 then any
1107 * entry in the incoming data need only match one item to return true.
1108 * Otherwise all entries in the incoming data must have a corresponding
1109 * match in the access port_list.
1112 compare_port_list(acc_port_list_t *in, acc_port_list_t *ac, const int match_any)
1117 acc_port_list_t *tlist;
1125 if(in->proto == tlist->proto && in->port == tlist->port)
1131 tlist = tlist->next;
1136 return(i_cnt == a_cnt);
1139 /* Take a proto/port string (or mulitple comma-separated strings) and check
1140 * them against the list for the given access stanza.
1142 * Return 1 if we are allowed
1145 acc_check_port_access(acc_stanza_t *acc, char *port_str)
1147 int res = 1, ctr = 0;
1149 char buf[ACCESS_BUF_LEN];
1152 acc_port_list_t *o_pl = acc->oport_list;
1153 acc_port_list_t *r_pl = acc->rport_list;
1155 acc_port_list_t *in_pl = NULL;
1159 /* Create our own internal port_list from the incoming SPA data
1162 for(ndx = start; *ndx != '\0'; ndx++)
1166 if((ctr >= ACCESS_BUF_LEN)
1167 || (((ndx-start)+1) >= ACCESS_BUF_LEN))
1170 "Unable to create acc_port_list from incoming data: %s",
1175 strlcpy(buf, start, (ndx-start)+1);
1176 add_port_list_ent(&in_pl, buf);
1182 if((ctr >= ACCESS_BUF_LEN)
1183 || (((ndx-start)+1) >= ACCESS_BUF_LEN))
1186 "Unable to create acc_port_list from incoming data: %s",
1191 strlcpy(buf, start, (ndx-start)+1);
1192 add_port_list_ent(&in_pl, buf);
1197 "Unable to create acc_port_list from incoming data: %s", port_str
1202 /* Start with restricted ports (if any). Any match (even if only one
1203 * entry) means not allowed.
1205 if((acc->rport_list != NULL) && (compare_port_list(in_pl, r_pl, 1)))
1208 goto cleanup_and_bail;
1211 /* For open port list, all must match.
1213 if((acc->oport_list != NULL) && (!compare_port_list(in_pl, o_pl, 0)))
1217 free_acc_port_list(in_pl);
1221 /* Take a GPG ID string and check it against the list of allowed
1224 * Return 1 if we are allowed
1227 acc_check_gpg_remote_id(acc_stanza_t *acc, const char *gpg_id)
1229 acc_string_list_t *ndx;
1231 for(ndx = acc->gpg_remote_id_list; ndx != NULL; ndx=ndx->next)
1232 if(strcasecmp(ndx->str, gpg_id) == 0)
1238 /* Dump the configuration
1241 dump_access_list(const fko_srv_options_t *opts)
1245 acc_stanza_t *acc = opts->acc_stanzas;
1247 fprintf(stdout, "Current fwknopd access settings:\n");
1251 fprintf(stderr, "\n ** No Access Settings Defined **\n\n");
1259 "==============================================================\n"
1261 " RESTRICT_PORTS: %s\n"
1262 " KEY: <see the access.conf file>\n"
1263 " FW_ACCESS_TIMEOUT: %i\n"
1264 " ENABLE_CMD_EXEC: %s\n"
1265 " CMD_EXEC_USER: %s\n"
1266 " REQUIRE_USERNAME: %s\n"
1267 " REQUIRE_SOURCE_ADDRESS: %s\n"
1268 " FORCE_NAT (ip): %s\n"
1269 " FORCE_NAT (proto): %s\n"
1270 " FORCE_NAT (port): %d\n"
1271 " ACCESS_EXPIRE: %s" /* asctime() adds a newline */
1272 " GPG_HOME_DIR: %s\n"
1273 " GPG_DECRYPT_ID: %s\n"
1274 " GPG_DECRYPT_PW: <see the access.conf file>\n"
1275 " GPG_REQUIRE_SIG: %s\n"
1276 "GPG_IGNORE_SIG_VERIFY_ERROR: %s\n"
1277 " GPG_REMOTE_ID: %s\n",
1280 (acc->open_ports == NULL) ? "<not set>" : acc->open_ports,
1281 (acc->restrict_ports == NULL) ? "<not set>" : acc->restrict_ports,
1282 //(acc->key == NULL) ? "<not set>" : acc->key,
1283 acc->fw_access_timeout,
1284 acc->enable_cmd_exec ? "Yes" : "No",
1285 (acc->cmd_exec_user == NULL) ? "<not set>" : acc->cmd_exec_user,
1286 (acc->require_username == NULL) ? "<not set>" : acc->require_username,
1287 acc->require_source_address ? "Yes" : "No",
1288 acc->force_nat ? acc->force_nat_ip : "<not set>",
1289 acc->force_nat && acc->force_nat_proto != NULL ? acc->force_nat_proto : "<not set>",
1290 acc->force_nat ? acc->force_nat_port : 0,
1291 (acc->access_expire_time > 0) ? asctime(localtime(&acc->access_expire_time)) : "<not set>\n",
1292 (acc->gpg_home_dir == NULL) ? "<not set>" : acc->gpg_home_dir,
1293 (acc->gpg_decrypt_id == NULL) ? "<not set>" : acc->gpg_decrypt_id,
1294 //(acc->gpg_decrypt_pw == NULL) ? "<not set>" : acc->gpg_decrypt_pw,
1295 acc->gpg_require_sig ? "Yes" : "No",
1296 acc->gpg_ignore_sig_error ? "Yes" : "No",
1297 (acc->gpg_remote_id == NULL) ? "<not set>" : acc->gpg_remote_id
1300 fprintf(stdout, "\n");
1305 fprintf(stdout, "\n");