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"
35 /* SPA message format validation functions.
36 * (These called from the spa_message function here only).
38 int validate_cmd_msg(const char *msg);
39 int validate_access_msg(const char *msg);
40 int validate_proto_port_spec(const char *msg);
41 int validate_nat_access_msg(const char *msg);
42 int got_allow_ip(const char *msg);
44 /* Set the SPA message type.
47 fko_set_spa_message_type(fko_ctx_t ctx, const short msg_type)
49 /* Must be initialized
51 if(!CTX_INITIALIZED(ctx))
52 return FKO_ERROR_CTX_NOT_INITIALIZED;
54 if(msg_type < 0 || msg_type >= FKO_LAST_MSG_TYPE)
55 return(FKO_ERROR_INVALID_DATA);
57 ctx->message_type = msg_type;
59 ctx->state |= FKO_SPA_MSG_TYPE_MODIFIED;
64 /* Return the SPA message type.
67 fko_get_spa_message_type(fko_ctx_t ctx, short *msg_type)
69 /* Must be initialized
71 if(!CTX_INITIALIZED(ctx))
72 return FKO_ERROR_CTX_NOT_INITIALIZED;
74 *msg_type = ctx->message_type;
79 /* Set the SPA MESSAGE data
82 fko_set_spa_message(fko_ctx_t ctx, const char *msg)
84 int res = FKO_ERROR_UNKNOWN;
86 /* Context must be initialized.
88 if(!CTX_INITIALIZED(ctx))
89 return FKO_ERROR_CTX_NOT_INITIALIZED;
91 /* Gotta have a valid string.
93 if(msg == NULL || strlen(msg) == 0)
94 return(FKO_ERROR_INVALID_DATA);
96 /* --DSS XXX: Bail out for now. But consider just
97 * truncating in the future...
99 if(strlen(msg) > MAX_SPA_MESSAGE_SIZE)
100 return(FKO_ERROR_DATA_TOO_LARGE);
102 /* Basic message type and format checking...
104 switch(ctx->message_type)
106 case FKO_COMMAND_MSG:
107 res = validate_cmd_msg(msg);
111 case FKO_CLIENT_TIMEOUT_ACCESS_MSG:
112 res = validate_access_msg(msg);
115 case FKO_NAT_ACCESS_MSG:
116 case FKO_LOCAL_NAT_ACCESS_MSG:
117 case FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG:
118 case FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG:
119 res = validate_nat_access_msg(msg);
123 if(res != FKO_SUCCESS)
126 /* Just in case this is a subsquent call to this function. We
127 * do not want to be leaking memory.
129 if(ctx->message != NULL)
132 ctx->message = strdup(msg);
134 ctx->state |= FKO_DATA_MODIFIED;
136 if(ctx->message == NULL)
137 return(FKO_ERROR_MEMORY_ALLOCATION);
142 /* Return the SPA message data.
145 fko_get_spa_message(fko_ctx_t ctx, char **msg)
147 /* Must be initialized
149 if(!CTX_INITIALIZED(ctx))
150 return(FKO_ERROR_CTX_NOT_INITIALIZED);
157 /* Validate a command message format.
160 validate_cmd_msg(const char *msg)
163 int res = FKO_SUCCESS;
164 int startlen = strlen(msg);
167 /* Should have a valid allow IP.
169 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
172 /* Commands are fairly free-form so all we can really verify is
173 * there is something at all. Get past the IP and comma, and make
174 * sure we have some string leftover...
176 ndx = strchr(msg, ',');
177 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
178 return(FKO_ERROR_INVALID_SPA_COMMAND_MSG);
184 validate_access_msg(const char *msg)
187 int res = FKO_SUCCESS;
188 int startlen = strlen(msg);
190 /* Should have a valid allow IP.
192 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
195 /* Position ourselves beyond the allow IP and make sure we are
198 ndx = strchr(msg, ',');
199 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
200 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
202 /* Look for a comma to see if this is a multi-part access request.
206 res = validate_proto_port_spec(ndx);
207 } while((ndx = strchr(ndx, ',')));
213 validate_proto_port_spec(const char *msg)
215 int startlen = strlen(msg);
217 const char *ndx = msg;
219 /* Now check for proto/port string. Currenly we only allow protos
220 * 'tcp', 'udp', and 'icmp'.
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)) >= startlen)
230 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
232 /* Skip over the ',' and make sure we only have digits.
237 if(isdigit(*ndx) == 0)
238 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
246 validate_nat_access_msg(const char *msg)
248 int res = FKO_SUCCESS;
250 /* Should have a valid access message.
252 if((res = validate_access_msg(msg)) != FKO_SUCCESS)
255 // --DSS TODO: XXX: Put nat_access validation code here
261 got_allow_ip(const char *msg)
263 const char *ndx = msg;
265 int res = FKO_SUCCESS;
267 while(*ndx != ',' && *ndx != '\0')
271 else if(isdigit(*ndx) == 0)
273 res = FKO_ERROR_INVALID_ALLOW_IP;
281 res = FKO_ERROR_INVALID_ALLOW_IP;