Parse hostname/port for AAA server addresses
[trust_router.git] / common / tr_aaa_server.c
1 /*
2  * Copyright (c) 2012-2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35
36 #include <talloc.h>
37
38 #include <tr_name_internal.h>
39 #include <tr_aaa_server.h>
40 #include <trust_router/tid.h>
41
42 static int tr_aaa_server_destructor(void *obj)
43 {
44   TR_AAA_SERVER *aaa=talloc_get_type_abort(obj, TR_AAA_SERVER);
45   if (aaa->hostname!=NULL)
46     tr_free_name(aaa->hostname);
47   return 0;
48 }
49
50 TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx)
51 {
52   TR_AAA_SERVER *aaa=talloc(mem_ctx, TR_AAA_SERVER);
53   if (aaa!=NULL) {
54     aaa->next=NULL;
55     aaa->hostname = NULL;
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);
58   }
59   return aaa;
60 }
61
62 void tr_aaa_server_free(TR_AAA_SERVER *aaa)
63 {
64   talloc_free(aaa);
65 }
66
67 /**
68  * Parse the port from a hostname:port string
69  *
70  * @param s string to parse
71  * @return the specified port, 0 if none specified, -1 if invalid
72  */
73 static int tr_aaa_server_parse_port(const char *s)
74 {
75   const char *s_port;
76   char *end_of_conversion;
77   long int port; /* long instead of int because we use strtol */
78
79   /* Find the first colon */
80   s_port = strchr(s, ':'); /* port starts at s_port + 1 */
81   if (s_port == NULL)
82     return 0; /* no port */
83
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*/
87
88   s_port += 1; /* port now starts at s_port */
89
90   /* Parse the port number */
91   port = strtol(s, &end_of_conversion, /* base */ 10);
92
93   /* validate */
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)) {
97     return -1;
98   }
99
100   return (int) port;
101 }
102
103 /**
104  * Parse a hostname out of a hostname:port string
105  *
106  * The ":port" section is optional. Ignores the string after the first colon.
107  * Does not validate the port section of the name.
108  *
109  * An empty hostname is allowed (but s must not be null)
110  *
111  * @param s
112  * @return TR_NAME or null on error (i.e., out-of-memory)
113  */
114 static TR_NAME *tr_aaa_server_parse_hostname(const char *s)
115 {
116   const char *colon;
117   char *hostname;
118   size_t hostname_len;
119   TR_NAME *retval;
120
121   if (s == NULL)
122     return NULL;
123
124   /* find the colon */
125   colon = strchr(s, ':');
126   if (colon == NULL)
127     return tr_new_name(s); /* there was no colon, take the whole string */
128
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)
133     return NULL;
134
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);
139
140   /* clean up and return */
141   free(hostname);
142   return retval;
143 }
144
145 /**
146  * Allocate a AAA server record and fill it in by parsing a hostname:port string
147  *
148  * Does not validate hostname or port values. The port will be -1 if the port
149  * could not be parsed properly.
150  *
151  * @return newly allocated TR_AAA_SERVER in the mem_ctx context, or NULL on error
152  */
153 TR_AAA_SERVER *tr_aaa_server_from_string(TALLOC_CTX *mem_ctx, const char *s)
154 {
155   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
156   TR_AAA_SERVER *aaa = tr_aaa_server_new(tmp_ctx);
157
158   if (aaa == NULL)
159     goto failed;
160
161   tr_aaa_server_set_hostname(aaa, tr_aaa_server_parse_hostname(s));
162   if (tr_aaa_server_get_hostname(aaa) == NULL)
163     goto failed;
164
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 */
167   goto succeeded;
168
169 failed:
170   aaa = NULL; /* talloc will free the memory if it was allocated */
171
172 succeeded:
173   talloc_free(tmp_ctx);
174   return aaa;
175 }
176
177
178 /**
179  * Allocate a AAA server record and fill it in by parsing a hostname:port TR_NAME
180  *
181  * Does not validate hostname or port values. The port will be -1 if the port
182  * could not be parsed properly.
183  *
184  * @return newly allocated TR_AAA_SERVER in the mem_ctx context, or NULL on error
185  */
186 TR_AAA_SERVER *tr_aaa_server_from_name(TALLOC_CTX *mem_ctx, TR_NAME *n)
187 {
188   TR_AAA_SERVER *aaa = NULL;
189   char *s = tr_name_strdup(n);
190   if (s != NULL) {
191     aaa = tr_aaa_server_from_string(mem_ctx, s);
192     free(s);
193   }
194   return aaa;
195 }
196
197 TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
198 {
199   return talloc(mem_ctx, TR_AAA_SERVER_ITER);
200 }
201
202 void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
203 {
204   talloc_free(iter);
205 }
206
207 TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
208 {
209   iter->this=aaa;
210   return iter->this;
211 }
212
213 TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
214 {
215   if (iter->this!=NULL) {
216     iter->this=iter->this->next;
217   }
218   return iter->this;
219 }
220
221 TR_NAME *tr_aaa_server_get_hostname(TR_AAA_SERVER *aaa)
222 {
223   return aaa->hostname;
224 }
225
226 /**
227  * Set the hostname for a AAA server
228  *
229  * Takes ownership of the TR_NAME. Does nothing if aaa is null.
230  *
231  * @param aaa
232  * @param hostname
233  */
234 void tr_aaa_server_set_hostname(TR_AAA_SERVER *aaa, TR_NAME *hostname)
235 {
236   if (aaa == NULL)
237     return;
238
239   if (aaa->hostname != NULL) {
240     tr_free_name(aaa->hostname);
241   }
242
243   aaa->hostname = hostname;
244 }
245
246 int tr_aaa_server_get_port(TR_AAA_SERVER *aaa)
247 {
248   return aaa->port;
249 }
250
251 /**
252  * Set the port for a AAA server
253  *
254  * If port is 0, uses the standard TID port (12309). Other invalid values are stored
255  * as-is.
256  *
257  * Does nothing if aaa is null.
258  *
259  * @param aaa
260  * @param port
261  */
262 void tr_aaa_server_set_port(TR_AAA_SERVER *aaa, int port)
263 {
264   if (aaa == NULL)
265     return;
266
267   if (port == 0)
268     port = TID_PORT;
269
270   aaa->port = port;
271 }
272
273