1 /*
2  *   logtool - a logfile parsing/monitoring/manipulation utility
3  *
4  *   Copyright (C) Y2K (2000) A.L.Lambert
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2, or (at your option)
9  *   any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include "includes.h"
22 
23 CONFIG cf;
24 STRUCTURE_REGEXS reg;
25 
lt_set_config_err(char * option,char * val)26 void lt_set_config_err(char *option, char *val) {
27 	printf("Error: invalid value [%s] to option [%s] in cfg file\n", val, option);
28 	exit (1);
29 }
30 
31 /* internal function to set a value to int TRUE/FALSE depending on string "true" or "false"	*/
lt_sctf(char * val)32 short int lt_sctf(char *val) {
33 	if(strncmp(val, "true", 32) == 0) return TRUE;
34 	if(strncmp(val, "false", 32) == 0) return FALSE;
35 	return -1;
36 }
37 
38 /* this sets up the filename and such for a regex file	*/
lt_set_regfile(char ** file,char * val)39 short int lt_set_regfile(char **file, char *val) {
40 	*file = malloc(strlen(val) + 2);
41 	/* if we couldn't malloc that space, then return false	*/
42 	if(file == NULL) return FALSE;
43 
44 	/* well, we made it here, we must have memory; copy that sucka	*/
45 	strcpy(*file, val);
46 
47 	/* we assume all went well. :)		*/
48 	return TRUE;
49 }
50 
lt_set_config()51 void lt_set_config() {
52 	char *val;
53 
54 	/* our file variables ; knock them out in one quick whack	*/
55 	/* if you're adding one of these, see logtool.h, readconf.c and regex.c */
56 	if((val = getenv("includefile")) != NULL) { reg.include_check = lt_set_regfile(&reg.include_file, val); }
57 	if((val = getenv("excludefile")) != NULL) { reg.exclude_check = lt_set_regfile(&reg.exclude_file, val); }
58 
59 	if((val = getenv("whitefile")) != NULL) { reg.white_check = lt_set_regfile(&reg.white_file, val); }
60 	if((val = getenv("brightwhitefile")) != NULL) { reg.brightwhite_check = lt_set_regfile(&reg.brightwhite_file, val); }
61 
62 	if((val = getenv("greenfile")) != NULL) { reg.green_check = lt_set_regfile(&reg.green_file, val); }
63 	if((val = getenv("brightgreenfile")) != NULL) { reg.brightgreen_check = lt_set_regfile(&reg.brightgreen_file, val); }
64 
65 	if((val = getenv("yellowfile")) != NULL) { reg.yellow_check = lt_set_regfile(&reg.yellow_file, val); }
66 	if((val = getenv("brightyellowfile")) != NULL) { reg.brightyellow_check = lt_set_regfile(&reg.brightyellow_file, val); }
67 
68 	if((val = getenv("bluefile")) != NULL) { reg.blue_check = lt_set_regfile(&reg.blue_file, val); }
69 	if((val = getenv("brightbluefile")) != NULL) { reg.brightblue_check = lt_set_regfile(&reg.brightblue_file, val); }
70 
71 	if((val = getenv("cyanfile")) != NULL) { reg.cyan_check = lt_set_regfile(&reg.cyan_file, val); }
72 	if((val = getenv("brightcyanfile")) != NULL) { reg.brightcyan_check = lt_set_regfile(&reg.brightcyan_file, val); }
73 
74 	if((val = getenv("magentafile")) != NULL) { reg.magenta_check = lt_set_regfile(&reg.magenta_file, val); }
75 	if((val = getenv("brightmagentafile")) != NULL) { reg.brightmagenta_check = lt_set_regfile(&reg.brightmagenta_file, val); }
76 
77 	if((val = getenv("brightredfile")) != NULL) { reg.brightred_check = lt_set_regfile(&reg.brightred_file, val); }
78 
79 	/* a few things we take a bit more time with			*/
80 	if((val = getenv("output_format")) != NULL) {
81 		if(strncasecmp(val, "ansi", 32) == 0) {
82 			cf.outfmt = OUTPUT_ANSI;
83 		} else if(strncasecmp(val, "ascii", 32) == 0) {
84 			cf.outfmt = OUTPUT_ASCII;
85 		} else if(strncasecmp(val, "csv", 32) == 0) {
86 			cf.outfmt = OUTPUT_CSV;
87 		} else if(strncasecmp(val, "html", 32) == 0) {
88 			cf.outfmt = OUTPUT_HTML;
89 		} else if(strncasecmp(val, "raw", 32) == 0) {
90 			cf.outfmt = OUTPUT_RAW;
91 		} else {
92 			lt_set_config_err("output_format", val);
93 		}
94 	}
95 
96 	/* do we beep when a red event occurs (only in ANSI mode	*/
97 	if((val = getenv("redbeep")) != NULL) cf.redbeep = lt_sctf(val);
98 	if(cf.redbeep == -1) lt_set_config_err("redbeep", val);
99 
100 	/* do we suppress dupe messages? (only ANSI/ASCII/HTML)		*/
101 	if((val = getenv("supdupes")) != NULL) cf.supdupes = lt_sctf(val);
102 	if(cf.supdupes == -1) lt_set_config_err("supdupes", val);
103 
104 	/* do we be verbose to the user about stuff?			*/
105 	if((val = getenv("verbose")) != NULL) cf.verbose = lt_sctf(val);
106 	if(cf.verbose == -1) lt_set_config_err("verbose", val);
107 
108 	/* do we display the host that generated this message?	*/
109 	if((val = getenv("show_source")) != NULL) cf.showsrc = lt_sctf(val);
110 	if(cf.showsrc == -1) lt_set_config_err("show_source", val);
111 
112 	/* do we display the program that generated this message?	*/
113 	if((val = getenv("show_program")) != NULL) cf.showprog = lt_sctf(val);
114 	if(cf.showprog == -1) lt_set_config_err("show_program", val);
115 
116 	/* do we be verbose to the user about stuff?			*/
117 	if((val = getenv("verbose")) != NULL) cf.verbose = lt_sctf(val);
118 	if(cf.verbose == -1) lt_set_config_err("verbose", val);
119 
120 	/* do we be strip syslog-ng *@'s from host field?		*/
121 	if((val = getenv("sys_ng_host")) != NULL) cf.sys_ng_host = lt_sctf(val);
122 	if(cf.sys_ng_host == -1) lt_set_config_err("sys_ng_host", val);
123 
124 	/* if we're a syslog-ng host collecting for the whole network,
125 	 * we have options to parse the host field specially	*/
126 	if((val = getenv("hostfmt")) != NULL) {
127 		if(strncmp(val, "name", 32) == 0) {
128 			cf.hostfmt = NG_HOST_NAME;
129 		} else if(strncmp(val, "ip", 32) == 0) {
130 			cf.hostfmt = NG_HOST_IP;
131 		} else if(strncmp(val, "both", 32) == 0) {
132 			cf.hostfmt = NG_HOST_BOTH;
133 		}
134 	}
135 
136 	if((val = getenv("time_format")) != NULL) {
137 		if(strncmp(val, "long", 32) == 0) {
138 			cf.timefmt = DATE_LONG;
139 		} else if(strncmp(val, "short", 32) == 0) {
140 			cf.timefmt = DATE_SHORT;
141 		} else {
142 			lt_set_config_err("time_format", val);
143 		}
144 	}
145 }
146 
147 /* this function will open and read our config file into memory 	*/
lt_read_config()148 int lt_read_config() {
149 	char line[LSIZE];
150 	FILE *cfgfile;
151 
152 	/* if we got no config file, skip this	*/
153 	if(cf.configfile[0] == '\0') return -1;
154 
155 	/* try to open our config file	*/
156 	cfgfile = fopen(cf.configfile, "r");
157 	if( ! cfgfile ) {
158 		/* if we failed, bitch to user, but don't stop processing	*/
159 		perror(cf.configfile);
160 		return -1;
161 	}
162 
163 	while(fgets(line, 1023, cfgfile) != NULL) {
164 		if(lt_match(line, ".*=.*") == TRUE) {
165 			lt_putenv(line);
166 		}
167 	}
168 	/* close yee ole config file	*/
169 	fclose(cfgfile);
170 
171 	/* call the function above to set things based on config file	*/
172 	lt_set_config();
173 	return 0;
174 }
175 
176