1 /*
2  * readcf.c
3  *
4  * routines to read and parse the configuration file
5  */
6 
7 
8 /* config header if available */
9 
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13 
14 
15 /* system includes */
16 
17 #include <stdio.h>
18 
19 #if HAVE_STDLIB_H || STDC_HEADERS
20 #include <stdlib.h>
21 #endif
22 
23 /* ugly, but portable */
24 #if HAVE_STRING_H || STDC_HEADERS
25 #include <string.h>
26 #else
27 #if HAVE_STRINGS_H
28 #include <strings.h>
29 #else
30 #if HAVE_STRCHR
31 char *strchr();
32 #else
33 #if HAVE_INDEX
34 #define strchr index
35 char *strchr();
36 #endif
37 #endif
38 #endif
39 #endif
40 
41 #include <ctype.h>
42 
43 #if HAVE_LIMITS_H
44 #include <limits.h>
45 #endif
46 
47 
48 /* local includes */
49 
50 #include "logsurfer.h"
51 #include "globals.h"
52 #include "str_util.h"
53 #include "rule.h"
54 #include "readline.h"
55 #include "readcf.h"
56 
57 
58 /*
59  * process one line from the configuration file
60  */
61 void
process_cfline(cfline,line_number)62 process_cfline(cfline, line_number)
63 	char	*cfline;
64 	long	line_number;
65 {
66 	struct rule	*new_rule;
67 
68 	if ( (new_rule=parse_rule(cfline)) == NULL ) {
69 		(void) fprintf(stderr, "config error arround line %ld: %s\n",
70 			line_number, cfline);
71 		exit(4);
72 	}
73 	if ( all_rules == NULL ) {
74 		all_rules=new_rule;
75 		all_rules_end=new_rule;
76 	}
77 	else {
78 		all_rules_end->next=new_rule;
79 		new_rule->previous=all_rules_end;
80 		all_rules_end=new_rule;
81 	}
82 	return;
83 }
84 
85 
86 /*
87  * read the configuration file
88  */
89 int
read_config(filename)90 read_config(filename)
91 	char	*filename;
92 {
93 	FILE	*infile;		/* filehandel for configfile	*/
94 	char	*buffer;		/* buffer for reading		*/
95 	int	buf_size, buf_pos;	/* used for buffer description	*/
96 	char	*cfline;		/* one configuration line	*/
97 	int	cfline_size;		/* size of cfline buffer	*/
98 	char	*input_line;		/* a new line from the cf file	*/
99 	long	line_number;		/* linenumber within configfile */
100 
101 	if ( (infile=fopen(filename, "r")) == NULL ) {
102 		(void) fprintf(stderr, "error opening configfile %s\n",
103 			filename);
104 		return(1);
105 	}
106 	line_number=0;
107 
108 	/* we start with input lines not longer than 1024 bytes */
109 	buf_size=1024;
110 	buf_pos=0;
111 	if ( (buffer=(char *)malloc(buf_size)) == NULL ) {
112 		(void) fprintf(stderr, "memory problems reading configfile\n");
113 		return(1);
114 	}
115 	buffer[0] = '\0';
116 	/* dito for one complete configline */
117 	cfline_size=1024;
118 	if ( (cfline=(char *)malloc(cfline_size)) == NULL ) {
119 		(void) fprintf(stderr, "memory problems reading configfile\n");
120 		return(1);
121 	}
122 	cfline[0] = '\0';
123 
124 	while ( ((input_line=readline(infile, &buffer, &buf_size, &buf_pos)) != NULL) ||
125 		(buf_pos != 0) ) {
126 		/* did we read in an incomplete line at the end of file? */
127 		if ( input_line == NULL ) {
128 			/* so there must be some unprocessed stuff in the buffer */
129 			if ( (input_line=(char *) malloc(strlen(buffer)+1)) == NULL ) {
130 				(void) fprintf(stderr, "memory problems reading configfile\n");
131 				return(1);
132 			}
133 			(void) strcpy(input_line, buffer);
134 			buf_pos=0;
135 		}
136 		/* we got a new line from the config-file */
137 		line_number++;
138 		if ( (input_line[0] == '\0' || input_line[0] == '#'
139 			|| !isspace(input_line[0])) && cfline[0] != '\0' ) {
140 			process_cfline(cfline, line_number);
141 			cfline[0]='\0';
142 		}
143 		if ( input_line[0] == '\0' || input_line[0] == '#' ) {
144 			(void) free(input_line);
145 			continue;
146 		}
147 		if ( isspace(input_line[0]) ) {
148 			trim(input_line);
149 			/* add a space and the input_line to the cfline */
150 			if ( expand_buf(&cfline, &cfline_size,
151 				strlen(cfline)+1+strlen(input_line)+1) != 0 ) {
152 				(void) fprintf(stderr, "memory problems reading configfile");
153 				return(1);
154 			}
155 			(void) strcat(cfline, " ");
156 			(void) strcat(cfline, input_line);
157 			(void) free(input_line);
158 			continue;
159 		}
160 		if ( cfline[0] != '\0' ) {
161 			process_cfline(cfline, line_number);
162 			cfline[0]='\0';
163 		}
164 		/* copy the input_line to the cfline */
165 		if ( expand_buf(&cfline, &cfline_size, strlen(input_line)+1) != 0 ) {
166 			(void) fprintf(stderr, "memory problems reading configfile");
167 			return(1);
168 		}
169 		if ( expand_buf(&cfline, &cfline_size, strlen(input_line)+1) != 0 ) {
170 			(void) fprintf(stderr, "memory problems reading configfile");
171 			return(1);
172 		}
173 		(void) strcpy(cfline, input_line);
174 		(void) free(input_line);
175 	}
176 	if ( cfline[0] != '\0' )
177 		process_cfline(cfline, line_number);
178 
179 	(void) fclose(infile);
180 	return(0);
181 }
182 
183