2 * Copyright (c) 2012-2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <tr_name_internal.h>
39 #include <tr_aaa_server.h>
40 #include <trust_router/tid.h>
42 static int tr_aaa_server_destructor(void *obj)
44 TR_AAA_SERVER *aaa=talloc_get_type_abort(obj, TR_AAA_SERVER);
45 if (aaa->hostname!=NULL)
46 tr_free_name(aaa->hostname);
50 TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx)
52 TR_AAA_SERVER *aaa=talloc(mem_ctx, TR_AAA_SERVER);
56 tr_aaa_server_set_port(aaa, 0); /* go through setter to guarantee consistent default */
57 talloc_set_destructor((void *)aaa, tr_aaa_server_destructor);
62 void tr_aaa_server_free(TR_AAA_SERVER *aaa)
68 * Parse the port from a hostname:port string
70 * @param s string to parse
71 * @return the specified port, 0 if none specified, -1 if invalid
73 static int tr_aaa_server_parse_port(const char *s)
76 char *end_of_conversion;
77 long int port; /* long instead of int because we use strtol */
79 /* Find the first colon */
80 s_port = strchr(s, ':'); /* port starts at s_port + 1 */
82 return 0; /* no port */
84 /* Check that the last colon is the same as the first */
85 if (strrchr(s, ':') != s_port)
86 return -1; /* multiple colons are invalid*/
88 s_port += 1; /* port now starts at s_port */
90 /* Parse the port number */
91 port = strtol(s, &end_of_conversion, /* base */ 10);
94 if ((end_of_conversion == s_port) /* there was no port, just a colon */
95 || (*end_of_conversion != '\0') /* did not reach the end of the string */
96 || (port <= 0) || (port > 65535)) {
104 * Parse a hostname out of a hostname:port string
106 * The ":port" section is optional. Ignores the string after the first colon.
107 * Does not validate the port section of the name.
109 * An empty hostname is allowed (but s must not be null)
112 * @return TR_NAME or null on error (i.e., out-of-memory)
114 static TR_NAME *tr_aaa_server_parse_hostname(const char *s)
125 colon = strchr(s, ':');
127 return tr_new_name(s); /* there was no colon, take the whole string */
129 /* make a copy of the hostname portion of the string */
130 hostname_len = colon - s;
131 hostname = malloc(hostname_len + 1); /* +1 for the null termination */
132 if (hostname == NULL)
135 /* copy up to the colon, add a null termination, and make a TR_NAME */
136 strncpy(hostname, s, hostname_len);
137 hostname[hostname_len] = '\0';
138 retval = tr_new_name(hostname);
140 /* clean up and return */
146 * Allocate a AAA server record and fill it in by parsing a hostname:port string
148 * Does not validate hostname or port values. The port will be -1 if the port
149 * could not be parsed properly.
151 * @return newly allocated TR_AAA_SERVER in the mem_ctx context, or NULL on error
153 TR_AAA_SERVER *tr_aaa_server_from_string(TALLOC_CTX *mem_ctx, const char *s)
155 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
156 TR_AAA_SERVER *aaa = tr_aaa_server_new(tmp_ctx);
161 tr_aaa_server_set_hostname(aaa, tr_aaa_server_parse_hostname(s));
162 if (tr_aaa_server_get_hostname(aaa) == NULL)
165 tr_aaa_server_set_port(aaa, tr_aaa_server_parse_port(s));
166 talloc_steal(mem_ctx, aaa); /*put this in the caller's context */
170 aaa = NULL; /* talloc will free the memory if it was allocated */
173 talloc_free(tmp_ctx);
179 * Allocate a AAA server record and fill it in by parsing a hostname:port TR_NAME
181 * Does not validate hostname or port values. The port will be -1 if the port
182 * could not be parsed properly.
184 * @return newly allocated TR_AAA_SERVER in the mem_ctx context, or NULL on error
186 TR_AAA_SERVER *tr_aaa_server_from_name(TALLOC_CTX *mem_ctx, TR_NAME *n)
188 TR_AAA_SERVER *aaa = NULL;
189 char *s = tr_name_strdup(n);
191 aaa = tr_aaa_server_from_string(mem_ctx, s);
197 TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
199 return talloc(mem_ctx, TR_AAA_SERVER_ITER);
202 void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
207 TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
213 TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
215 if (iter->this!=NULL) {
216 iter->this=iter->this->next;
221 TR_NAME *tr_aaa_server_get_hostname(TR_AAA_SERVER *aaa)
223 return aaa->hostname;
227 * Set the hostname for a AAA server
229 * Takes ownership of the TR_NAME. Does nothing if aaa is null.
234 void tr_aaa_server_set_hostname(TR_AAA_SERVER *aaa, TR_NAME *hostname)
239 if (aaa->hostname != NULL) {
240 tr_free_name(aaa->hostname);
243 aaa->hostname = hostname;
246 int tr_aaa_server_get_port(TR_AAA_SERVER *aaa)
252 * Set the port for a AAA server
254 * If port is 0, uses the standard TID port (12309). Other invalid values are stored
257 * Does nothing if aaa is null.
262 void tr_aaa_server_set_port(TR_AAA_SERVER *aaa, int port)