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 /* Set the SPA message type.
38 fko_set_spa_message_type(fko_ctx_t ctx, const short msg_type)
40 /* Must be initialized
42 if(!CTX_INITIALIZED(ctx))
43 return FKO_ERROR_CTX_NOT_INITIALIZED;
45 if(msg_type < 0 || msg_type >= FKO_LAST_MSG_TYPE)
46 return(FKO_ERROR_INVALID_DATA);
48 ctx->message_type = msg_type;
50 ctx->state |= FKO_SPA_MSG_TYPE_MODIFIED;
55 /* Return the SPA message type.
58 fko_get_spa_message_type(fko_ctx_t ctx, short *msg_type)
60 /* Must be initialized
62 if(!CTX_INITIALIZED(ctx))
63 return FKO_ERROR_CTX_NOT_INITIALIZED;
65 *msg_type = ctx->message_type;
70 /* Set the SPA MESSAGE data
73 fko_set_spa_message(fko_ctx_t ctx, const char *msg)
75 int res = FKO_ERROR_UNKNOWN;
77 /* Context must be initialized.
79 if(!CTX_INITIALIZED(ctx))
80 return FKO_ERROR_CTX_NOT_INITIALIZED;
82 /* Gotta have a valid string.
84 if(msg == NULL || strnlen(msg, MAX_SPA_MESSAGE_SIZE) == 0)
85 return(FKO_ERROR_INVALID_DATA);
87 /* --DSS XXX: Bail out for now. But consider just
88 * truncating in the future...
90 if(strnlen(msg, MAX_SPA_MESSAGE_SIZE) == MAX_SPA_MESSAGE_SIZE)
91 return(FKO_ERROR_DATA_TOO_LARGE);
93 /* Basic message type and format checking...
95 switch(ctx->message_type)
98 res = validate_cmd_msg(msg);
102 case FKO_CLIENT_TIMEOUT_ACCESS_MSG:
103 res = validate_access_msg(msg);
106 case FKO_NAT_ACCESS_MSG:
107 case FKO_LOCAL_NAT_ACCESS_MSG:
108 case FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG:
109 case FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG:
110 res = validate_nat_access_msg(msg);
114 if(res != FKO_SUCCESS)
117 /* Just in case this is a subsquent call to this function. We
118 * do not want to be leaking memory.
120 if(ctx->message != NULL)
123 ctx->message = strdup(msg);
125 ctx->state |= FKO_DATA_MODIFIED;
127 if(ctx->message == NULL)
128 return(FKO_ERROR_MEMORY_ALLOCATION);
133 /* Return the SPA message data.
136 fko_get_spa_message(fko_ctx_t ctx, char **msg)
138 /* Must be initialized
140 if(!CTX_INITIALIZED(ctx))
141 return(FKO_ERROR_CTX_NOT_INITIALIZED);
148 /* Validate a command message format.
151 validate_cmd_msg(const char *msg)
154 int res = FKO_SUCCESS;
155 int startlen = strnlen(msg, MAX_SPA_CMD_LEN);
157 if(startlen == MAX_SPA_CMD_LEN)
158 return(FKO_ERROR_INVALID_DATA);
160 /* Should have a valid allow IP.
162 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
165 /* Commands are fairly free-form so all we can really verify is
166 * there is something at all. Get past the IP and comma, and make
167 * sure we have some string leftover...
169 ndx = strchr(msg, ',');
170 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
171 return(FKO_ERROR_INVALID_SPA_COMMAND_MSG);
177 validate_access_msg(const char *msg)
180 int res = FKO_SUCCESS;
181 int startlen = strnlen(msg, MAX_SPA_MESSAGE_SIZE);
183 if(startlen == MAX_SPA_MESSAGE_SIZE)
184 return(FKO_ERROR_INVALID_DATA);
186 /* Should have a valid allow IP.
188 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
191 /* Position ourselves beyond the allow IP and make sure we are
194 ndx = strchr(msg, ',');
195 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
196 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
198 /* Look for a comma to see if this is a multi-part access request.
202 res = validate_proto_port_spec(ndx);
203 if(res != FKO_SUCCESS)
205 } while((ndx = strchr(ndx, ',')));
211 validate_proto_port_spec(const char *msg)
213 int startlen = strnlen(msg, MAX_SPA_MESSAGE_SIZE), port_str_len = 0;
214 const char *ndx = msg;
216 if(startlen == MAX_SPA_MESSAGE_SIZE)
217 return(FKO_ERROR_INVALID_DATA);
219 /* Now check for proto/port string.
221 if(strncmp(ndx, "tcp", 3)
222 && strncmp(ndx, "udp", 3)
223 && strncmp(ndx, "icmp", 4)
224 && strncmp(ndx, "none", 4))
225 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
227 ndx = strchr(ndx, '/');
228 if(ndx == NULL || ((1+(ndx - msg)) > MAX_PROTO_STR_LEN))
229 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
231 /* Skip over the '/' and make sure we only have digits.
235 /* Must have at least one digit for the port number
237 if(isdigit(*ndx) == 0)
238 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
240 while(*ndx != '\0' && *ndx != ',')
243 if((isdigit(*ndx) == 0) || (port_str_len > MAX_PORT_STR_LEN))
244 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
251 validate_nat_access_msg(const char *msg)
253 int res = FKO_SUCCESS;
255 /* Should have a valid access message.
257 if((res = validate_access_msg(msg)) != FKO_SUCCESS)
260 // --DSS TODO: XXX: Put nat_access validation code here
266 got_allow_ip(const char *msg)
268 const char *ndx = msg;
270 int res = FKO_SUCCESS;
272 while(*ndx != ',' && *ndx != '\0')
276 else if(isdigit(*ndx) == 0)
278 res = FKO_ERROR_INVALID_ALLOW_IP;
286 res = FKO_ERROR_INVALID_ALLOW_IP;