aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tru/widget-url.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tru/widget-url.c')
-rw-r--r--src/tru/widget-url.c183
1 files changed, 180 insertions, 3 deletions
diff --git a/src/tru/widget-url.c b/src/tru/widget-url.c
index 8106c32..ce14a20 100644
--- a/src/tru/widget-url.c
+++ b/src/tru/widget-url.c
@@ -40,9 +40,33 @@
40#include "tinyradutil.h" 40#include "tinyradutil.h"
41 41
42#include <stdio.h> 42#include <stdio.h>
43#include <stdarg.h>
44#include <stdlib.h>
45#include <string.h>
46#include <strings.h>
47#include <unistd.h>
48#include <arpa/inet.h>
43#include <assert.h> 49#include <assert.h>
44 50
45 51
52//////////////////
53// //
54// Prototypes //
55// //
56//////////////////
57#pragma mark - Prototypes
58
59int
60tru_widget_url_print(
61 TinyRadURLDesc * trudp );
62
63
64int
65tru_widget_url_usage(
66 TinyRadUtilConf * cnf,
67 const char * short_opt );
68
69
46///////////////// 70/////////////////
47// // 71// //
48// Functions // 72// Functions //
@@ -54,11 +78,164 @@ int
54tru_widget_url( 78tru_widget_url(
55 TinyRadUtilConf * cnf ) 79 TinyRadUtilConf * cnf )
56{ 80{
57 int rc; 81 int rc;
58 if ((rc = tru_load_tinyrad(cnf)) != TRAD_SUCCESS) 82 int pos;
59 return(rc); 83 TinyRadURLDesc * trudp;
84 TinyRadURLDesc ** trudpp;
85 TinyRadURLDesc * trudp_next;
86
87 // getopt options
88 static char short_opt[] = "r" TRU_COMMON_SHORT TRU_NET_SHORT;
89 static struct option long_opt[] =
90 {
91 { "resolve", no_argument, NULL, 'r' }, \
92 TRU_NET_LONG \
93 TRU_COMMON_LONG
94 };
95
96 trudp = NULL;
97 trudp_next = NULL;
98
99 // process widget cli options
100 if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &tru_widget_url_usage)) != 0)
101 {
102 return((rc == 0) ? 0 : 1);
103 };
104 if (optind >= cnf->argc)
105 {
106 fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME);
107 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name);
108 return(1);
109 };
110
111 // parse and resolve URLs
112 trudpp = &trudp;
113 for(pos = optind; (pos < cnf->argc); pos++)
114 {
115 if ((rc = tinyrad_urldesc_parse(cnf->argv[pos], trudpp)) != TRAD_SUCCESS)
116 {
117 if (!(cnf->opts & TRUTILS_OPT_QUIET))
118 {
119 fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, cnf->argv[pos], tinyrad_strerror(rc));
120 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name);
121 };
122 return(1);
123 };
124 if ((cnf->opts & TRU_OPT_RESOLVE))
125 {
126 if ((rc = tinyrad_urldesc_resolve(*trudpp, cnf->tr_opts)) != TRAD_SUCCESS)
127 {
128 if (!(cnf->opts & TRUTILS_OPT_QUIET))
129 {
130 fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, cnf->argv[pos], tinyrad_strerror(rc));
131 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name);
132 };
133 return(1);
134 };
135 };
136 trudpp = &(*trudpp)->trud_next;
137 };
138
139 // exit if not displaying output
140 if (!(cnf->opts & TRUTILS_OPT_VERBOSE))
141 {
142 tinyrad_urldesc_free(trudp);
143 return(0);
144 };
145
146 // display URLs
147 trudp_next = trudp;
148 while ((trudp_next))
149 {
150 if ((tru_widget_url_print(trudp_next)))
151 {
152 tinyrad_urldesc_free(trudp);
153 return(1);
154 };
155 trudp_next = trudp_next->trud_next;
156 };
157
158 tinyrad_urldesc_free(trudp);
159
60 return(TRAD_SUCCESS); 160 return(TRAD_SUCCESS);
61} 161}
62 162
63 163
164int
165tru_widget_url_print(
166 TinyRadURLDesc * trudp )
167{
168 int pos;
169 char * str;
170 const char * scheme;
171 struct sockaddr_storage * sas;
172 char addr[INET6_ADDRSTRLEN];
173
174 if ((str = tinyrad_urldesc2str(trudp)) == NULL)
175 {
176 fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME);
177 tinyrad_urldesc_free(trudp);
178 return(1);
179 };
180 switch(trudp->trud_opts & TRAD_SCHEME)
181 {
182 case TRAD_RADIUS: scheme = "radius (RFC2865 RADIUS)"; break;;
183 case TRAD_RADIUS_ACCT: scheme = "radius-acct (RFC2866 RADIUS Accounting)"; break;;
184 case TRAD_RADIUS_DYNAUTH: scheme = "radius-dynauth (RFC2866 RADIUS Dynamic Authorization)"; break;;
185 case TRAD_RADSEC: scheme = "radsec (RFC6614/RFC7360 RADIUS TLS/DTLS)"; break;;
186 default: scheme = "Unknown or unsupported URL scheme"; break;
187 };
188
189 printf("%s\n", str);
190 printf(" scheme: %s\n", scheme);
191 printf(" host: %s\n", trudp->trud_host);
192 printf(" port: %i\n", trudp->trud_port);
193 printf(" secret: %s\n", (((trudp->trud_secret)) ? trudp->trud_secret : "n/a"));
194 printf(" protocol: %s\n", ((trudp->trud_opts & TRAD_TCP)) ? "tcp" : "udp");
195 if ( ((trudp->trud_sockaddrs)) && ((trudp->trud_sockaddrs[0])) )
196 {
197 for(pos = 0; ((trudp->trud_sockaddrs[pos])); pos++)
198 {
199 sas = trudp->trud_sockaddrs[pos];
200 switch(sas->ss_family)
201 {
202 case AF_INET: inet_ntop(sas->ss_family, &((struct sockaddr_in *)sas)->sin_addr, addr, sizeof(addr)); break;
203 case AF_INET6: inet_ntop(sas->ss_family, &((struct sockaddr_in6 *)sas)->sin6_addr, addr, sizeof(addr)); break;
204 default:
205 addr[0] = '\0';
206 break;
207 };
208 printf(" %s%s\n", ((!(pos)) ? "addresses: " : " "), addr);
209 };
210 };
211 printf("\n");
212
213 tinyrad_free(str);
214
215 return(0);
216}
217
218
219int
220tru_widget_url_usage(
221 TinyRadUtilConf * cnf,
222 const char * short_opt )
223{
224 tru_usage_summary(cnf);
225 tru_usage_options(short_opt);
226 printf("\n");
227 printf("URL Syntax:\n");
228 printf(" radius://hostport/[secret][?proto] use RFC2865 (udp/port 1812)\n");
229 printf(" radius-acct://hostport/[secret][?proto] use RFC2866 (udp/port 1813)\n");
230 printf(" radsec://hostport/[?proto] use RFC6614/RFC7360 (tcp/port 2083)\n");
231 printf(" radius-dynauth://hostport/[secret][?proto] use RFC5176 (udp/port 3799)\n");
232 printf("\n");
233 printf(" where:\n");
234 printf(" 'hostport' is a host name with an optional ':portnumber'\n");
235 printf(" 'secret' is the RADIUS secret encoded using character encoding in RFC3986\n");
236 printf(" 'proto' is communication protocol used by the connection (UDP or TCP)\n");
237 printf("\n");
238 return(0);
239}
240
64/* end of source */ 241/* end of source */