summaryrefslogtreecommitdiff
path: root/rpcapd/fileconf.c
blob: 2f15c014204f18e6c9548221ac8c13c88333e66f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
79
80
81
82
83
84
85
86
87
88
89
90
91
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
122
123
124
125
126
127
128
129
130
131
132
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
197
198
199
200
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
317
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 * Copyright (c) 1987, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ftmacros.h"

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <pcap.h>		// for PCAP_ERRBUF_SIZE

#include "portability.h"
#include "rpcapd.h"
#include "config_params.h"	// configuration file parameters
#include "fileconf.h"
#include "rpcap-protocol.h"
#include "log.h"

//
// Parameter names.
//
#define PARAM_ACTIVECLIENT	"ActiveClient"
#define PARAM_PASSIVECLIENT	"PassiveClient"
#define PARAM_NULLAUTHPERMIT	"NullAuthPermit"

static char *skipws(char *ptr);

void fileconf_read(void)
{
	FILE *fp;
	unsigned int num_active_clients;

	if ((fp = fopen(loadfile, "r")) != NULL)
	{
		char line[MAX_LINE + 1];
		unsigned int lineno;

		hostlist[0] = 0;
		num_active_clients = 0;
		lineno = 0;

		while (fgets(line, MAX_LINE, fp) != NULL)
		{
			size_t linelen;
			char *ptr;
			char *param;
			size_t result;
			size_t toklen;

			lineno++;

			linelen = strlen(line);
			if (line[linelen - 1] != '\n')
			{
				int c;

				//
				// Either the line doesn't fit in
				// the buffer, or we got an EOF
				// before the EOL.  Assume it's the
				// former.
				//
				rpcapd_log(LOGPRIO_ERROR,
				    "%s, line %u is longer than %u characters",
				    loadfile, lineno, MAX_LINE);

				//
				// Eat characters until we get an NL.
				//
				while ((c = getc(fp)) != '\n')
				{
					if (c == EOF)
						goto done;
				}

				//
				// Try the next line.
				//
				continue;
			}
			ptr = line;

			//
			// Skip leading white space, if any.
			//
			ptr = skipws(ptr);
			if (ptr == NULL)
			{
				// Blank line.
				continue;
			}

			//
			// Is the next character a "#"?  If so, this
			// line is a comment; skip to the next line.
			//
			if (*ptr == '#')
				continue;

			//
			// Is the next character alphabetic?  If not,
			// this isn't a valid parameter name.
			//
			if (!isascii((unsigned char)*ptr) ||
			    !isalpha((unsigned char)*ptr))
			{
				rpcapd_log(LOGPRIO_ERROR,
				    "%s, line %u doesn't have a valid parameter name",
				    loadfile, lineno);
				continue;
			}

			//
			// Grab the first token, which is made of
			// alphanumerics, underscores, and hyphens.
			// That's the name of the parameter being set.
			//
			param = ptr;
			while (isascii((unsigned char)*ptr) &&
			    (isalnum((unsigned char)*ptr) || *ptr == '-' || *ptr == '_'))
				ptr++;

			//
			// Skip over white space, if any.
			//
			ptr = skipws(ptr);
			if (ptr == NULL || *ptr != '=')
			{
				//
				// We hit the end of the line before
				// finding a non-white space character,
				// or we found one but it's not an "=".
				// That means there's no "=", so this
				// line is invalid.  Complain and skip
				// this line.
				//
				rpcapd_log(LOGPRIO_ERROR,
				    "%s, line %u has a parameter but no =",
				    loadfile, lineno);
				continue;
			}

			//
			// We found the '='; set it to '\0', and skip
			// past it.
			//
			*ptr++ = '\0';

			//
			// Skip past any white space after the "=".
			//
			ptr = skipws(ptr);
			if (ptr == NULL)
			{
				//
				// The value is empty.
				//
				rpcapd_log(LOGPRIO_ERROR,
				    "%s, line %u has a parameter but no value",
				    loadfile, lineno);
				continue;
			}

			//
			// OK, what parameter is this?
			//
			if (strcmp(param, PARAM_ACTIVECLIENT) == 0) {
				//
				// Add this to the list of active clients.
				//
				char *address, *port;

				//
				// We can't have more than MAX_ACTIVE_LIST
				// active clients.
				//
				if (num_active_clients >= MAX_ACTIVE_LIST)
				{
					//
					// Too many entries for the active
					// client list.  Complain and
					// ignore it.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has an %s parameter, but we already have %u active clients",
					    loadfile, lineno, PARAM_ACTIVECLIENT,
					    MAX_ACTIVE_LIST);
					continue;
				}

				//
				// Get the address.
				// It's terminated by a host list separator
				// *or* a #; there *shouldn't* be a #, as
				// that starts a comment, and that would
				// mean that we have no port.
				//
				address = ptr;
				toklen = strcspn(ptr, RPCAP_HOSTLIST_SEP "#");
				ptr += toklen;	// skip to the terminator
				if (toklen == 0)
				{
					if (isascii((unsigned char)*ptr) &&
					    (isspace((unsigned char)*ptr) || *ptr == '#' || *ptr == '\0'))
					{
						//
						// The first character it saw
						// was a whitespace character
						// or a comment character.
						// This means that there's
						// no value.
						//
						rpcapd_log(LOGPRIO_ERROR,
						    "%s, line %u has a parameter but no value",
						    loadfile, lineno);
					}
					else
					{
						//
						// This means that the first
						// character it saw was a
						// separator.  This means that
						// there's no address in the
						// value, just a port.
						//
						rpcapd_log(LOGPRIO_ERROR,
						    "%s, line %u has an %s parameter with a value containing no address",
						    loadfile, lineno, PARAM_ACTIVECLIENT);
					}
					continue;
				}

				//
				// Null-terminate the address, and skip past
				// it.
				//
				*ptr++ = '\0';

				//
				// Skip any white space following the
				// separating character.
				//
				ptr = skipws(ptr);
				if (ptr == NULL)
				{
					//
					// The value is empty, so there's
					// no port in the value.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has an %s parameter with a value containing no port",
					    loadfile, lineno, PARAM_ACTIVECLIENT);
					continue;
				}

				//
				// Get the port.
				// We look for a white space character
				// or a # as a terminator; the # introduces
				// a comment that runs to the end of the
				// line.
				//
				port = ptr;
				toklen = strcspn(ptr, " \t#\r\n");
				ptr += toklen;
				if (toklen == 0)
				{
					//
					// The value is empty, so there's
					// no port in the value.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has an %s parameter with a value containing no port",
					    loadfile, lineno, PARAM_ACTIVECLIENT);
					continue;
				}

				//
				// Null-terminate the port, and skip past
				// it.
				//
				*ptr++ = '\0';
				result = pcap_strlcpy(activelist[num_active_clients].address, address, sizeof(activelist[num_active_clients].address));
				if (result >= sizeof(activelist[num_active_clients].address))
				{
					//
					// It didn't fit.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has an %s parameter with an address with more than %u characters",
					    loadfile, lineno, PARAM_ACTIVECLIENT,
					    (unsigned int)(sizeof(activelist[num_active_clients].address) - 1));
					continue;
				}
				if (strcmp(port, "DEFAULT") == 0) // the user choose a custom port
					result = pcap_strlcpy(activelist[num_active_clients].port, RPCAP_DEFAULT_NETPORT_ACTIVE, sizeof(activelist[num_active_clients].port));
				else
					result = pcap_strlcpy(activelist[num_active_clients].port, port, sizeof(activelist[num_active_clients].port));
				if (result >= sizeof(activelist[num_active_clients].address))
				{
					//
					// It didn't fit.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has an %s parameter with an port with more than %u characters",
					    loadfile, lineno, PARAM_ACTIVECLIENT,
					    (unsigned int)(sizeof(activelist[num_active_clients].port) - 1));
					continue;
				}

				num_active_clients++;
			}
			else if (strcmp(param, PARAM_PASSIVECLIENT) == 0)
			{
				char *eos;
				char *host;

				//
				// Get the host.
				// We look for a white space character
				// or a # as a terminator; the # introduces
				// a comment that runs to the end of the
				// line.
				//
				host = ptr;
				toklen = strcspn(ptr, " \t#\r\n");
				if (toklen == 0)
				{
					//
					// The first character it saw
					// was a whitespace character
					// or a comment character.
					// This means that there's
					// no value.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has a parameter but no value",
					    loadfile, lineno);
					continue;
				}
				ptr += toklen;
				*ptr++ = '\0';

				//
				// Append this to the host list.
				// Save the curren end-of-string for the
				// host list, in case the new host doesn't
				// fit, so that we can discard the partially-
				// copied host name.
				//
				eos = hostlist + strlen(hostlist);
				if (eos != hostlist)
				{
					//
					// The list is not empty, so prepend
					// a comma before adding this host.
					//
					result = pcap_strlcat(hostlist, ",", sizeof(hostlist));
					if (result >= sizeof(hostlist))
					{
						//
						// It didn't fit.  Discard
						// the comma (which wasn't
						// added, but...), complain,
						// and ignore this line.
						//
						*eos = '\0';
						rpcapd_log(LOGPRIO_ERROR,
						    "%s, line %u has a %s parameter with a host name that doesn't fit",
						    loadfile, lineno, PARAM_PASSIVECLIENT);
						continue;
					}
				}
				result = pcap_strlcat(hostlist, host, sizeof(hostlist));
				if (result >= sizeof(hostlist))
				{
					//
					// It didn't fit.  Discard the comma,
					// complain, and ignore this line.
					//
					*eos = '\0';
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has a %s parameter with a host name that doesn't fit",
					    loadfile, lineno, PARAM_PASSIVECLIENT);
					continue;
				}
			}
			else if (strcmp(param, PARAM_NULLAUTHPERMIT) == 0)
			{
				char *setting;

				//
				// Get the setting.
				// We look for a white space character
				// or a # as a terminator; the # introduces
				// a comment that runs to the end of the
				// line.
				//
				setting = ptr;
				toklen = strcspn(ptr, " \t#\r\n");
				ptr += toklen;
				if (toklen == 0)
				{
					//
					// The first character it saw
					// was a whitespace character
					// or a comment character.
					// This means that there's
					// no value.
					//
					rpcapd_log(LOGPRIO_ERROR,
					    "%s, line %u has a parameter but no value",
					    loadfile, lineno);
					continue;
				}
				*ptr++ = '\0';

				//
				// XXX - should we complain if it's
				// neither "yes" nor "no"?
				//
				if (strcmp(setting, "YES") == 0)
					nullAuthAllowed = 1;
				else
					nullAuthAllowed = 0;
			}
			else
			{
				rpcapd_log(LOGPRIO_ERROR,
				    "%s, line %u has an unknown parameter %s",
				    loadfile, lineno, param);
				continue;
			}
		}

done:
		// clear the remaining fields of the active list
		for (int i = num_active_clients; i < MAX_ACTIVE_LIST; i++)
		{
			activelist[i].address[0] = 0;
			activelist[i].port[0] = 0;
			num_active_clients++;
		}

		rpcapd_log(LOGPRIO_DEBUG, "New passive host list: %s", hostlist);
		fclose(fp);
	}
}

int fileconf_save(const char *savefile)
{
	FILE *fp;

	if ((fp = fopen(savefile, "w")) != NULL)
	{
		char *token; /*, *port;*/					// temp, needed to separate items into the hostlist
		char temphostlist[MAX_HOST_LIST + 1];
		int i = 0;
		char *lasts;

		fprintf(fp, "# Configuration file help.\n\n");

		// Save list of clients which are allowed to connect to us in passive mode
		fprintf(fp, "# Hosts which are allowed to connect to this server (passive mode)\n");
		fprintf(fp, "# Format: PassiveClient = <name or address>\n\n");

		strncpy(temphostlist, hostlist, MAX_HOST_LIST);
		temphostlist[MAX_HOST_LIST] = 0;

		token = pcap_strtok_r(temphostlist, RPCAP_HOSTLIST_SEP, &lasts);
		while(token != NULL)
		{
			fprintf(fp, "%s = %s\n", PARAM_PASSIVECLIENT, token);
			token = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts);
		}


		// Save list of clients which are allowed to connect to us in active mode
		fprintf(fp, "\n\n");
		fprintf(fp, "# Hosts to which this server is trying to connect to (active mode)\n");
		fprintf(fp, "# Format: ActiveClient = <name or address>, <port | DEFAULT>\n\n");


		while ((i < MAX_ACTIVE_LIST) && (activelist[i].address[0] != 0))
		{
			fprintf(fp, "%s = %s, %s\n", PARAM_ACTIVECLIENT,
			    activelist[i].address, activelist[i].port);
			i++;
		}

		// Save if we want to permit NULL authentication
		fprintf(fp, "\n\n");
		fprintf(fp, "# Permit NULL authentication: YES or NO\n\n");

		fprintf(fp, "%s = %s\n", PARAM_NULLAUTHPERMIT,
		    nullAuthAllowed ? "YES" : "NO");

		fclose(fp);
		return 0;
	}
	else
	{
		return -1;
	}

}

//
// Skip over white space.
// If we hit a CR or LF, return NULL, otherwise return a pointer to
// the first non-white space character.  Replace white space characters
// other than CR or LF with '\0', so that, if we're skipping white space
// after a token, the token is null-terminated.
//
static char *skipws(char *ptr)
{
	while (isascii((unsigned char)*ptr) && isspace((unsigned char)*ptr)) {
		if (*ptr == '\r' || *ptr == '\n')
			return NULL;
		*ptr++ = '\0';
	}
	return ptr;
}