aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-03-28 15:07:10 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-03-28 15:08:18 -0800
commit2f52bb953f654393aaad2dd65c7fa338d0b7ec36 (patch)
treea0dfe6ac1912a0039743604689c6590d183ca204 /src
parent7d15a7935594fd426bf79e57e2985cc8549f0eee (diff)
downloadtinyrad-2f52bb953f654393aaad2dd65c7fa338d0b7ec36.tar.gz
tinyrad-2f52bb953f654393aaad2dd65c7fa338d0b7ec36.tar.bz2
tinyrad-2f52bb953f654393aaad2dd65c7fa338d0b7ec36.tar.xz
tinyrad-2f52bb953f654393aaad2dd65c7fa338d0b7ec36.zip
tinyrad: adding url widget
Diffstat (limited to 'src')
-rw-r--r--src/tru/tinyradutil.c22
-rw-r--r--src/tru/tinyradutil.h7
-rw-r--r--src/tru/widget-url.c183
3 files changed, 209 insertions, 3 deletions
diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c
index 6a77ec4..5acfd7b 100644
--- a/src/tru/tinyradutil.c
+++ b/src/tru/tinyradutil.c
@@ -127,6 +127,13 @@ const TinyRadUtilWidget tru_widget_map[] =
127 .func = &tru_widget_dict 127 .func = &tru_widget_dict
128 }, 128 },
129 { 129 {
130 .name = "url",
131 .desc = "process TinyRad URL",
132 .usage = NULL,
133 .aliases = (const char * const[]) { TRU_PREFIX"-url", NULL },
134 .func = &tru_widget_url
135 },
136 {
130 .name = NULL, 137 .name = NULL,
131 .func = NULL, 138 .func = NULL,
132 .usage = NULL, 139 .usage = NULL,
@@ -356,6 +363,14 @@ tru_getopt(
356 363
357 switch(c = getopt_long(argc, argv, short_opt, long_opt, opt_index)) 364 switch(c = getopt_long(argc, argv, short_opt, long_opt, opt_index))
358 { 365 {
366 case '4':
367 cnf->tr_opts |= TRAD_IPV4;
368 return(TRU_GETOPT_MATCHED);
369
370 case '6':
371 cnf->tr_opts |= TRAD_IPV6;
372 return(TRU_GETOPT_MATCHED);
373
359 case 'b': 374 case 'b':
360 cnf->tr_opts |= TRAD_BUILTIN_DICT; 375 cnf->tr_opts |= TRAD_BUILTIN_DICT;
361 return(TRU_GETOPT_MATCHED); 376 return(TRU_GETOPT_MATCHED);
@@ -386,6 +401,10 @@ tru_getopt(
386 cnf->opts &= ~TRUTILS_OPT_VERBOSE; 401 cnf->opts &= ~TRUTILS_OPT_VERBOSE;
387 return(TRU_GETOPT_MATCHED); 402 return(TRU_GETOPT_MATCHED);
388 403
404 case 'r':
405 cnf->opts |= TRU_OPT_RESOLVE;
406 return(TRU_GETOPT_MATCHED);
407
389 case 'V': 408 case 'V':
390 trutils_version(); 409 trutils_version();
391 return(TRU_GETOPT_EXIT); 410 return(TRU_GETOPT_EXIT);
@@ -448,6 +467,8 @@ tru_usage_options(
448 s = short_opt; 467 s = short_opt;
449 468
450 printf("OPTIONS:\n"); 469 printf("OPTIONS:\n");
470 if ((strchr(s, '4'))) printf(" -4 use IPv4 addresses\n");
471 if ((strchr(s, '6'))) printf(" -6 use IPv6 addresses\n");
451 if ((strchr(s, 'b'))) printf(" -b, --builtin-dict load built-in dictionary\n"); 472 if ((strchr(s, 'b'))) printf(" -b, --builtin-dict load built-in dictionary\n");
452 if ((strchr(s, 'D'))) printf(" -D dictionary include dictionary\n"); 473 if ((strchr(s, 'D'))) printf(" -D dictionary include dictionary\n");
453 if ((strchr(s, 'd'))) printf(" -d level, --debug=level print debug messages\n"); 474 if ((strchr(s, 'd'))) printf(" -d level, --debug=level print debug messages\n");
@@ -455,6 +476,7 @@ tru_usage_options(
455 if ((strchr(s, 'h'))) printf(" -h, --help print this help and exit\n"); 476 if ((strchr(s, 'h'))) printf(" -h, --help print this help and exit\n");
456 if ((strchr(s, 'I'))) printf(" -I path add path to dictionary search paths\n"); 477 if ((strchr(s, 'I'))) printf(" -I path add path to dictionary search paths\n");
457 if ((strchr(s, 'q'))) printf(" -q, --quiet, --silent do not print messages\n"); 478 if ((strchr(s, 'q'))) printf(" -q, --quiet, --silent do not print messages\n");
479 if ((strchr(s, 'r'))) printf(" -r, --resolve resolve URLs\n");
458 if ((strchr(s, 'V'))) printf(" -V, --version print version number and exit\n"); 480 if ((strchr(s, 'V'))) printf(" -V, --version print version number and exit\n");
459 if ((strchr(s, 'v'))) printf(" -v, --verbose print verbose messages\n"); 481 if ((strchr(s, 'v'))) printf(" -v, --verbose print verbose messages\n");
460 482
diff --git a/src/tru/tinyradutil.h b/src/tru/tinyradutil.h
index b3c9b18..328505a 100644
--- a/src/tru/tinyradutil.h
+++ b/src/tru/tinyradutil.h
@@ -76,6 +76,9 @@
76#define TRU_GETOPT_EXIT -4 76#define TRU_GETOPT_EXIT -4
77 77
78 78
79#define TRU_OPT_RESOLVE 0x0001
80
81
79#define TRU_COMMON_SHORT "d:hqVv" 82#define TRU_COMMON_SHORT "d:hqVv"
80#define TRU_COMMON_LONG \ 83#define TRU_COMMON_LONG \
81 { "debug", optional_argument, NULL, 'd' }, \ 84 { "debug", optional_argument, NULL, 'd' }, \
@@ -92,6 +95,10 @@
92 { "builtin-dict", no_argument, NULL, 'b' }, \ 95 { "builtin-dict", no_argument, NULL, 'b' }, \
93 96
94 97
98#define TRU_NET_SHORT "46"
99#define TRU_NET_LONG \
100
101
95#define TRU_REQ_SHORT "f:" 102#define TRU_REQ_SHORT "f:"
96#define TRU_REQ_LONG \ 103#define TRU_REQ_LONG \
97 { "file", optional_argument, NULL, 'f' }, \ 104 { "file", optional_argument, NULL, 'f' }, \
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 */