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 = strlen(msg);
158 /* Should have a valid allow IP.
160 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
163 /* Commands are fairly free-form so all we can really verify is
164 * there is something at all. Get past the IP and comma, and make
165 * sure we have some string leftover...
167 ndx = strchr(msg, ',');
168 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
169 return(FKO_ERROR_INVALID_SPA_COMMAND_MSG);
175 validate_access_msg(const char *msg)
178 int res = FKO_SUCCESS;
179 int startlen = strlen(msg);
181 /* Should have a valid allow IP.
183 if((res = got_allow_ip(msg)) != FKO_SUCCESS)
186 /* Position ourselves beyond the allow IP and make sure we are
189 ndx = strchr(msg, ',');
190 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
191 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
193 /* Look for a comma to see if this is a multi-part access request.
197 res = validate_proto_port_spec(ndx);
198 } while((ndx = strchr(ndx, ',')));
204 validate_proto_port_spec(const char *msg)
206 int startlen = strlen(msg);
208 const char *ndx = msg;
210 /* Now check for proto/port string. Currenly we only allow protos
211 * 'tcp', 'udp', and 'icmp'.
213 if(strncmp(ndx, "tcp", 3)
214 && strncmp(ndx, "udp", 3)
215 && strncmp(ndx, "icmp", 4)
216 && strncmp(ndx, "none", 4))
217 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
219 ndx = strchr(ndx, '/');
220 if(ndx == NULL || (1+(ndx - msg)) >= startlen)
221 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
223 /* Skip over the ',' and make sure we only have digits.
228 if(isdigit(*ndx) == 0)
229 return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
237 validate_nat_access_msg(const char *msg)
239 int res = FKO_SUCCESS;
241 /* Should have a valid access message.
243 if((res = validate_access_msg(msg)) != FKO_SUCCESS)
246 // --DSS TODO: XXX: Put nat_access validation code here
252 got_allow_ip(const char *msg)
254 const char *ndx = msg;
256 int res = FKO_SUCCESS;
258 while(*ndx != ',' && *ndx != '\0')
262 else if(isdigit(*ndx) == 0)
264 res = FKO_ERROR_INVALID_ALLOW_IP;
272 res = FKO_ERROR_INVALID_ALLOW_IP;