2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Set/Get the spa message (access req/command/etc) based
9 * on the current spa data.
11 * Copyright 2009-2010 Damien Stuart (dstuart@dstuart.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 "fko_common.h"
33 #include "fko_message.h"
36 /* Set the SPA message type.
39 fko_set_spa_message_type(fko_ctx_t ctx, const short msg_type)
41 /* Must be initialized
43 if(!CTX_INITIALIZED(ctx))
44 return FKO_ERROR_CTX_NOT_INITIALIZED;
46 if(msg_type < 0 || msg_type >= FKO_LAST_MSG_TYPE)
47 return(FKO_ERROR_INVALID_DATA);
49 ctx->message_type = msg_type;
51 ctx->state |= FKO_SPA_MSG_TYPE_MODIFIED;
56 /* Return the SPA message type.
59 fko_get_spa_message_type(fko_ctx_t ctx, short *msg_type)
61 /* Must be initialized
63 if(!CTX_INITIALIZED(ctx))
64 return FKO_ERROR_CTX_NOT_INITIALIZED;
66 *msg_type = ctx->message_type;
71 /* Set the SPA MESSAGE data
74 fko_set_spa_message(fko_ctx_t ctx, const char *msg)
76 int res = FKO_ERROR_UNKNOWN;
78 /* Context must be initialized.
80 if(!CTX_INITIALIZED(ctx))
81 return FKO_ERROR_CTX_NOT_INITIALIZED;
83 /* Gotta have a valid string.
85 if(msg == NULL || strnlen(msg, MAX_SPA_MESSAGE_SIZE) == 0)
86 return(FKO_ERROR_INVALID_DATA);
88 /* --DSS XXX: Bail out for now. But consider just
89 * truncating in the future...
91 if(strnlen(msg, MAX_SPA_MESSAGE_SIZE) == MAX_SPA_MESSAGE_SIZE)
92 return(FKO_ERROR_DATA_TOO_LARGE);
94 /* Basic message type and format checking...
96 switch(ctx->message_type)
99 res = validate_cmd_msg(msg);
103 case FKO_CLIENT_TIMEOUT_ACCESS_MSG:
104 res = validate_access_msg(msg);
107 case FKO_NAT_ACCESS_MSG:
108 case FKO_LOCAL_NAT_ACCESS_MSG:
109 case FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG:
110 case FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG:
111 res = validate_nat_access_msg(msg);
115 if(res != FKO_SUCCESS)
118 /* Just in case this is a subsquent call to this function. We
119 * do not want to be leaking memory.
121 if(ctx->message != NULL)
124 ctx->message = strdup(msg);
126 ctx->state |= FKO_DATA_MODIFIED;
128 if(ctx->message == NULL)
129 return(FKO_ERROR_MEMORY_ALLOCATION);
134 /* Return the SPA message data.
137 fko_get_spa_message(fko_ctx_t ctx, char **msg)
139 /* Must be initialized
141 if(!CTX_INITIALIZED(ctx))
142 return(FKO_ERROR_CTX_NOT_INITIALIZED);
149 /* Validate a command message format.
152 validate_cmd_msg(const char *msg)
155 int res = FKO_SUCCESS;
156 int startlen = strnlen(msg, MAX_SPA_CMD_LEN);
158 if(startlen == MAX_SPA_CMD_LEN)
159 return(FKO_ERROR_INVALID_DATA);
161 /* Should have a valid allow IP.
163 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
166 /* Commands are fairly free-form so all we can really verify is
167 * there is something at all. Get past the IP and comma, and make
168 * sure we have some string leftover...
170 ndx = strchr(msg, ',');
171 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
172 return(FKO_ERROR_INVALID_SPA_COMMAND_MSG);
178 validate_access_msg(const char *msg)
181 int res = FKO_SUCCESS;
182 int startlen = strnlen(msg, MAX_SPA_MESSAGE_SIZE);
184 if(startlen == MAX_SPA_MESSAGE_SIZE)
185 return(FKO_ERROR_INVALID_DATA);
187 /* Should have a valid allow IP.
189 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
192 /* Position ourselves beyond the allow IP and make sure we are
195 ndx = strchr(msg, ',');
196 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
197 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
199 /* Look for a comma to see if this is a multi-part access request.
203 res = validate_proto_port_spec(ndx);
204 if(res != FKO_SUCCESS)
206 } while((ndx = strchr(ndx, ',')));
212 validate_proto_port_spec(const char *msg)
214 int startlen = strnlen(msg, MAX_SPA_MESSAGE_SIZE), port_str_len = 0;
215 const char *ndx = msg;
217 if(startlen == MAX_SPA_MESSAGE_SIZE)
218 return(FKO_ERROR_INVALID_DATA);
220 /* Now check for proto/port string.
222 if(strncmp(ndx, "tcp", 3)
223 && strncmp(ndx, "udp", 3)
224 && strncmp(ndx, "icmp", 4)
225 && strncmp(ndx, "none", 4))
226 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
228 ndx = strchr(ndx, '/');
229 if(ndx == NULL || ((1+(ndx - msg)) > MAX_PROTO_STR_LEN))
230 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
232 /* Skip over the '/' and make sure we only have digits.
236 /* Must have at least one digit for the port number
238 if(isdigit(*ndx) == 0)
239 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
241 while(*ndx != '\0' && *ndx != ',')
244 if((isdigit(*ndx) == 0) || (port_str_len > MAX_PORT_STR_LEN))
245 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
252 validate_nat_access_msg(const char *msg)
254 int res = FKO_SUCCESS;
256 /* Should have a valid access message.
258 if((res = validate_access_msg(msg)) != FKO_SUCCESS)
261 // --DSS TODO: XXX: Put nat_access validation code here
267 got_allow_ip(const char *msg)
269 const char *ndx = msg;
270 char ip_str[MAX_IPV4_STR_LEN];
271 int dot_ctr = 0, char_ctr = 0;
272 int res = FKO_SUCCESS;
273 #if HAVE_SYS_SOCKET_H
277 while(*ndx != ',' && *ndx != '\0')
279 ip_str[char_ctr] = *ndx;
281 if(char_ctr >= MAX_IPV4_STR_LEN)
283 res = FKO_ERROR_INVALID_ALLOW_IP;
288 else if(isdigit(*ndx) == 0)
290 res = FKO_ERROR_INVALID_ALLOW_IP;
296 if(char_ctr < MAX_IPV4_STR_LEN)
297 ip_str[char_ctr] = '\0';
299 res = FKO_ERROR_INVALID_ALLOW_IP;
301 if ((res == FKO_SUCCESS) && (char_ctr < MIN_IPV4_STR_LEN))
302 res = FKO_ERROR_INVALID_ALLOW_IP;
304 if((res == FKO_SUCCESS) && dot_ctr != 3)
305 res = FKO_ERROR_INVALID_ALLOW_IP;
307 #if HAVE_SYS_SOCKET_H
308 /* Stronger IP validation now that we have a candidate that looks
311 if((res == FKO_SUCCESS) && (inet_aton(ip_str, &in) == 0))
312 res = FKO_ERROR_INVALID_ALLOW_IP;