cfg_server.c 8.85 KB
Newer Older
1
/*
Moritz Grimm's avatar
Moritz Grimm committed
2
 * Copyright (c) 2017, 2020 Moritz Grimm <mgrimm@mrsserver.net>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include "compat.h"

#include <sys/queue.h>

Moritz Grimm's avatar
Moritz Grimm committed
25
#include <stdint.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string.h>

#include "cfg_private.h"
#include "cfg_server.h"
#include "xalloc.h"

struct cfg_server {
	TAILQ_ENTRY(cfg_server)  entry;
	char			*name;
	enum cfg_server_protocol protocol;
	char			 hostname[NI_MAXHOST];
	unsigned int		 port;
	char			 user[UCREDS_SIZE];
	char			 password[UCREDS_SIZE];
	enum cfg_server_tls	 tls;
	char			 tls_cipher_suite[CSUITE_SIZE];
	char			 ca_dir[PATH_MAX];
	char			 ca_file[PATH_MAX];
	char			 client_cert[PATH_MAX];
	unsigned int		 reconnect_attempts;
};

TAILQ_HEAD(cfg_server_list, cfg_server);

struct cfg_server_list *
cfg_server_list_create(void)
{
	struct cfg_server_list *sl;

	sl = xcalloc(1UL, sizeof(*sl));
	TAILQ_INIT(sl);

	return (sl);
}

void
cfg_server_list_destroy(cfg_server_list_t *sl_p)
{
	struct cfg_server_list	*sl = *sl_p;
	struct cfg_server	*s;

	if (!sl)
		return;

	while (NULL != (s = TAILQ_FIRST(sl))) {
		TAILQ_REMOVE(sl, s, entry);
		cfg_server_destroy(&s);
	}

	xfree(sl);
	*sl_p = NULL;
}

79
80
81
82
83
84
85
86
87
88
89
90
91
unsigned int
cfg_server_list_nentries(struct cfg_server_list *sl)
{
	struct cfg_server	*s;
	unsigned int		 n = 0;

	TAILQ_FOREACH(s, sl, entry) {
		n++;
	}

	return (n);
}

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
struct cfg_server *
cfg_server_list_find(struct cfg_server_list *sl, const char *name)
{
	struct cfg_server	*s;

	TAILQ_FOREACH(s, sl, entry) {
		if (0 == strcasecmp(s->name, name))
			return (s);
	}

	return (NULL);
}

struct cfg_server *
cfg_server_list_get(struct cfg_server_list *sl, const char *name)
{
	struct cfg_server	*s;

	s = cfg_server_list_find(sl, name);
	if (s)
		return (s);
	s = cfg_server_create(name);
	if (!s)
		return (NULL);

	TAILQ_INSERT_TAIL(sl, s, entry);

	return (s);
}

122
123
124
125
126
127
128
129
130
131
132
void
cfg_server_list_foreach(struct cfg_server_list *sl,
    void (*cb)(cfg_server_t, void *), void *cb_arg)
{
	struct cfg_server	*s;

	TAILQ_FOREACH(s, sl, entry) {
		cb(s, cb_arg);
	}
}

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
struct cfg_server *
cfg_server_create(const char *name)
{
	struct cfg_server	*s;

	if (!name || !name[0])
		return (NULL);

	s = xcalloc(1UL, sizeof(*s));
	s->name = xstrdup(name);

	return (s);
}

void
cfg_server_destroy(struct cfg_server **s_p)
{
	struct cfg_server	*s = *s_p;

	xfree(s->name);
	xfree(s);
	*s_p = NULL;
}

int
cfg_server_set_name(struct cfg_server *s, struct cfg_server_list *sl,
    const char *name, const char **errstrp)
{
	struct cfg_server	*s2;

	if (!name || !name[0]) {
		if (errstrp)
			*errstrp = "empty";
		return (-1);
	}

	s2 = cfg_server_list_find(sl, name);
	if (s2 && s2 != s) {
		if (errstrp)
			*errstrp = "already exists";
		return (-1);
	}

	SET_XSTRDUP(s->name, name, errstrp);

	return (0);
}

int
cfg_server_set_protocol(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *protocol, const char **errstrp)
{
	(void)not_used;

	if (!protocol || !protocol[0]) {
		if (errstrp)
			*errstrp = "empty";
		return (-1);
	}

	if (0 == strcasecmp("http", protocol))
		s->protocol = CFG_PROTO_HTTP;
	else if (0 == strcasecmp("https", protocol))
		s->protocol = CFG_PROTO_HTTPS;
197
198
199
200
	else if (0 == strcasecmp("icy", protocol))
		s->protocol = CFG_PROTO_ICY;
	else if (0 == strcasecmp("roaraudio", protocol))
		s->protocol = CFG_PROTO_ROARAUDIO;
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	else {
		if (NULL != errstrp)
			*errstrp = "unsupported";
		return (-1);
	}
	return (0);
}

int
cfg_server_set_hostname(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *hostname, const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->hostname, hostname, errstrp);
	return (0);
}

int
cfg_server_set_port(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *port_str, const char **errstrp)
{
	const char	*errstr;
	unsigned int	 port;

	(void)not_used;

	if (!port_str || !port_str[0]) {
		if (errstrp)
			*errstrp = "empty";
		return (-1);
	}

	port = (unsigned int)strtonum(port_str, 1, UINT16_MAX, &errstr);
	if (errstr) {
		if (errstrp)
			*errstrp = errstr;
		return (-1);
	}
	s->port = port;

	return (0);
}

int
cfg_server_set_user(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *user, const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->user, user, errstrp);
	return (0);
}

int
cfg_server_set_password(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *password, const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->password, password, errstrp);
	return (0);
}

int
cfg_server_set_tls(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *tls, const char **errstrp)
{
	(void)not_used;

	if (!tls || !tls[0]) {
		if (errstrp)
			*errstrp = "empty";
		return (-1);
	}

	if (0 == strcasecmp("may", tls))
		s->tls = CFG_TLS_MAY;
	else if (0 == strcasecmp("none", tls))
		s->tls = CFG_TLS_NONE;
	else if (0 == strcasecmp("required", tls))
		s->tls = CFG_TLS_REQUIRED;
	else {
		if (NULL != errstrp)
			*errstrp = "invalid";
		return (-1);
	}
	return (0);
}

int
cfg_server_set_tls_cipher_suite(struct cfg_server *s,
    struct cfg_server_list *not_used,const char *suite, const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->tls_cipher_suite, suite, errstrp);
	return (0);
}

int
cfg_server_set_ca_dir(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *ca_dir, const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->ca_dir, ca_dir, errstrp);
	return (0);
}

int
cfg_server_set_ca_file(struct cfg_server *s, struct cfg_server_list *not_used,
    const char *ca_file, const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->ca_file, ca_file, errstrp);
	return (0);
}

int
cfg_server_set_client_cert(struct cfg_server *s,
Moritz Grimm's avatar
Moritz Grimm committed
317
    struct cfg_server_list *not_used, const char *client_cert,
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
    const char **errstrp)
{
	(void)not_used;
	SET_STRLCPY(s->client_cert, client_cert, errstrp);
	return (0);
}

int
cfg_server_set_reconnect_attempts(struct cfg_server *s,
    struct cfg_server_list *not_used,const char *num_str,
    const char **errstrp)
{
	(void)not_used;
	SET_UINTNUM(s->reconnect_attempts, num_str, errstrp);
	return (0);
}

int
cfg_server_validate(struct cfg_server *s, const char **errstrp)
{
	if (!cfg_server_get_hostname(s)) {
		if (NULL != errstrp)
			*errstrp = "hostname missing";
		return (-1);
	}

	if (!cfg_server_get_password(s)) {
		if (NULL != errstrp)
			*errstrp = "password missing";
		return (-1);
	}

	return (0);
}

const char *
cfg_server_get_name(struct cfg_server *s)
{
	return (s->name);
}

enum cfg_server_protocol
cfg_server_get_protocol(struct cfg_server *s)
{
	return (s->protocol);
}

const char *
cfg_server_get_protocol_str(struct cfg_server *s)
{
	switch (s->protocol) {
	case CFG_PROTO_HTTPS:
		return ("https");
371
372
373
374
	case CFG_PROTO_ICY:
		return ("icy");
	case CFG_PROTO_ROARAUDIO:
		return ("roaraudio");
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	case CFG_PROTO_HTTP:
	default:
		return ("http");
	}
}

const char *
cfg_server_get_hostname(struct cfg_server *s)
{
	return (s->hostname[0] ? s->hostname : NULL);
}

unsigned int
cfg_server_get_port(struct cfg_server *s)
{
390
	return (s->port ? s->port : CFG_SERVER_DEFAULT_PORT);
391
392
393
394
395
}

const char *
cfg_server_get_user(struct cfg_server *s)
{
396
	return (s->user[0] ? s->user : CFG_SERVER_DEFAULT_USER);
397
398
399
400
401
402
403
404
405
406
407
}

const char *
cfg_server_get_password(struct cfg_server *s)
{
	return (s->password[0] ? s->password : NULL);
}

enum cfg_server_tls
cfg_server_get_tls(struct cfg_server *s)
{
408
409
	if (CFG_PROTO_HTTPS == s->protocol)
		return (CFG_TLS_REQUIRED);
410
411
412
	return (s->tls);
}

413
414
415
const char *
cfg_server_get_tls_str(struct cfg_server *s)
{
416
417
	if (CFG_PROTO_HTTPS == s->protocol)
		return ("required");
418
419
420
421
422
423
424
425
426
427
428
	switch (s->tls) {
	case CFG_TLS_NONE:
		return ("none");
	case CFG_TLS_REQUIRED:
		return ("required");
	case CFG_TLS_MAY:
	default:
		return ("may");
	}
}

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
const char *
cfg_server_get_tls_cipher_suite(struct cfg_server *s)
{
	return (s->tls_cipher_suite[0]
	    ? s->tls_cipher_suite
	    : NULL);
}

const char *
cfg_server_get_ca_dir(struct cfg_server *s)
{
	return (s->ca_dir[0] ? s->ca_dir : NULL);
}

const char *
cfg_server_get_ca_file(struct cfg_server *s)
{
	return (s->ca_file[0] ? s->ca_file : NULL);
}

const char *
cfg_server_get_client_cert(struct cfg_server *s)
{
	return (s->client_cert[0] ? s->client_cert : NULL);
}

unsigned int
cfg_server_get_reconnect_attempts(struct cfg_server *s)
{
	return (s->reconnect_attempts);
}