1 /*
2 * sma -- Sendmail log analyser
3 *
4 * Copyright (c) 2000 - 2003 Jarkko Turkulainen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY JARKKO TURKULAINEN ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JARKKO TURKULAINEN BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Date: 2002/09/12 11:36:00 $
30 */
31
32
33 #include "sma.h"
34
35 #ifndef __DragonFly__
36 int isspace(int);
37 #endif
38
39 void
init(FILE * conf)40 init(FILE *conf) {
41 char buff[1024];
42 char *str1, *str2;
43 int k, i, lines;
44
45 lines = 0;
46 while (fgets(buff, 1024, conf)) {
47 lines++;
48
49 /* null-terminate: */
50 buff[strlen(buff)-1] = '\0';
51
52 /* skip leading white space: */
53 for ( i = 0 ; isspace(buff[i]) ; i++)
54 ;
55
56 /* empty line? */
57 if (!strlen(&buff[i]))
58 continue;
59
60 /* comment? */
61 if (!strncmp("#", &buff[i], 1))
62 continue;
63
64 /* fetch key and value: */
65
66 k = 0;
67 str1 = get_string(&buff[i]);
68 i += strlen(&buff[i])+1;
69 for ( ; isspace(buff[i]) ; i++ )
70 ;
71 k = 1;
72 str2 = get_string(&buff[i]);
73
74 /* if not found... */
75 if (!strlen(str2)) {
76 if (!qflag)
77 fprintf(stderr, "%s: config file parse"
78 " error, line %d\n", pname, lines);
79 /* Parse configuration keys */
80 } else {
81 if (!strncmp(str1, "EnvelopePairs", 13)) {
82 epnum = atoi(str2);
83 } else if (!strncmp(str1, "EnvelopeSenders", 15)) {
84 if (!lflag) {
85 lrflag = 1;
86 lnum = atoi(str2);
87 }
88 } else if (!strncmp(str1, "EnvelopeRecipients", 18)) {
89 if (!lflag) {
90 lrflag = 1;
91 lrnum = atoi(str2);
92 }
93 } else if (!strncmp(str1, "RelayPairs", 10)) {
94 rpnum = atoi(str2);
95 } else if (!strncmp(str1, "RelaySenders", 12)) {
96 if (!rflag) {
97 rrflag = 1;
98 rnum = atoi(str2);
99 }
100 } else if (!strncmp(str1, "RelayRecipients", 15)) {
101 if (!rflag) {
102 rrflag = 1;
103 rrnum = atoi(str2);
104 }
105 } else if (!strncmp(str1, "Comment", 7)) {
106 if (!Cflag)
107 if (!(Cchar = strdup(str2)))
108 error_memory();
109 } else if (!strncmp(str1, "BounceAddress", 13)) {
110 if (!(bechar = strdup(str2)))
111 error_memory();
112 } else if (!strncmp(str1, "ShowUsers", 9)) {
113 if (strncmp(str2, "yes", 3) && !dflag)
114 dflag = 1;
115 } else if (!strncmp(str1, "Format", 6)) {
116 if (!Oflag) {
117 Oflag = 1;
118 if (!(Ochar = strdup(str2)))
119 error_memory();
120 }
121 } else if (!strncmp(str1, "HostName", 8)) {
122 if (!Hflag)
123 if(!(Hchar = strdup(str2)))
124 error_memory();
125 } else if (!strncmp(str1, "BgColor", 7)) {
126 if (!bflag)
127 if (!(bchar = strdup(str2)))
128 error_memory();
129 } else if (!strncmp(str1, "TbColor", 7)) {
130 if (!(tbchar = strdup(str2)))
131 error_memory();
132 } else if (!strncmp(str1, "Debug", 5)) {
133 if (!strncmp(str2, "yes", 3) && !vflag)
134 vflag = 1;
135 } else if (!strncmp(str1, "PrintTime", 9)) {
136 if (strncmp(str2, "yes", 3) && !nflag)
137 nflag = 1;
138 } else if (!strncmp(str1, "PrintGeneralInfo", 16)) {
139 if (!strncmp(str2, "no", 3))
140 pgflag = 0;
141 } else if (!strncmp(str1, "PrintStatus", 11)) {
142 stnum = atoi(str2);
143 } else if (!strncmp(str1, "PrintRuleset", 12)) {
144 rsnum = atoi(str2);
145 } else if (!strncmp(str1, "RulesetRelays", 13)) {
146 rsrnum = atoi(str2);
147 } else if (!strncmp(str1, "OutFile", 7)) {
148 if (!oflag)
149 if (!(ochar = strdup(str2)))
150 error_memory();
151 } else if (!strncmp(str1, "Sorting", 7)) {
152 if (strncmp(str2, "number", 6) && !sflag)
153 sflag = 1;
154 } else if (!strncmp(str1, "Silent", 6)) {
155 if (!strncmp(str2, "yes", 3) && !qflag)
156 qflag = 1;
157 } else if (!strncmp(str1, "SyslogTag", 9)) {
158 if (!Lflag)
159 if (!(Lchar = strdup(str2)))
160 error_memory();
161 } else if (!strncmp(str1, "EnvelopeSenderFilter", 20)) {
162 if (!(sef = strdup(str2)))
163 error_memory();
164 } else if (!strncmp(str1, "EnvelopeRecipientFilter", 23)) {
165 if (!(ref = strdup(str2)))
166 error_memory();
167 } else if (!strncmp(str1, "RelaySenderFilter", 17)) {
168 if (!(srf = strdup(str2)))
169 error_memory();
170 } else if (!strncmp(str1, "RelayRecipientFilter", 20)) {
171 if (!(rrf = strdup(str2)))
172 error_memory();
173 } else if (!strncmp(str1, "StartTime", 9)) {
174 if (!Dflag)
175 if (!(sstring = strdup(str2)))
176 error_memory();
177 } else if (!strncmp(str1, "EndTime", 7)) {
178 if (!Dflag)
179 if (!(estring = strdup(str2)))
180 error_memory();
181 } else if (!strncmp(str1, "PictureURL", 10)) {
182 if (!(puchar = strdup(str2)))
183 error_memory();
184 } else if (!strncmp(str1, "PictureALT", 10)) {
185 if (!(pachar = strdup(str2)))
186 error_memory();
187 } else if (!strncmp(str1, "PictureParameters", 17)) {
188 if (!(ppchar = strdup(str2)))
189 error_memory();
190 } else if (!strncmp(str1, "PictureLink", 11)) {
191 if (!(plchar = strdup(str2)))
192 error_memory();
193 } else if (!strncmp(str1, "HeaderText", 10)) {
194 if (!(htchar = strdup(str2)))
195 error_memory();
196 } else if (!strncmp(str1, "FooterText", 10)) {
197 if (!(ftchar = strdup(str2)))
198 error_memory();
199 } else if (!strncmp(str1, "CaseSensitive", 13)) {
200 if (!strncmp(str2, "yes", 3))
201 csflag = 1;
202 else
203 csflag = 0;
204 } else if (!strncmp(str1, "ClogFormat", 10)) {
205 if (!(cfchar = strdup(str2)))
206 error_memory();
207 } else if (!strncmp(str1, "ClogSentOnly", 12)) {
208 if (!strncmp(str2, "yes", 3))
209 clsflag = 1;
210 else
211 clsflag = 0;
212 } else if (!strncmp(str1, "IncludeAscii", 12)) {
213 if (!strncmp(str2, "yes", 3) && !iflag)
214 iflag = 1;
215 } else if (!strncmp(str1, "HashTables", 10)) {
216 if (!tflag) {
217 if(!(tchar = strdup(str2)))
218 error_memory();
219 tflag = 1;
220 }
221 } else if (!strncmp(str1, "DowncaseAddresses", 17)) {
222 if (!strncmp(str2, "yes", 3) && !dcaddrflag) {
223 dcaddrflag = 1;
224 }
225 } else {
226 if (!qflag)
227 fprintf(stderr, "%s: unknown config parameter"
228 " \"%s\", line %d\n", pname, str1, lines);
229 }
230 }
231 }
232 }
233
234 /*
235 * Find string, strip off '"'s.
236 */
237
238 char *
get_string(char * str)239 get_string(char *str) {
240 char *tmpstr;
241 char *retstr;
242 int c;
243
244 /* Malloc space for string: */
245 if (!(tmpstr = malloc(strlen(str))))
246 error_memory();
247
248 /* check for '"': */
249 if (*str != '"') c = 0;
250 else c = 1;
251
252 /* not '"': */
253 if (!c) {
254 retstr = str;
255 free(tmpstr);
256 while (*str++ != '\0') {
257 if (isspace(*str)) {
258 break;
259 }
260 }
261 *str = '\0';
262
263 /* starts with '"': */
264 } else {
265 retstr = tmpstr;
266 while (*str++ != '\0') {
267 if (*str == '\\' && *(str+1) && *(str+1) == '"') {
268 *tmpstr++ = *(str+1);
269 str++;
270 } else if (*str == '"')
271 break;
272 else
273 *tmpstr++ = *str;
274 }
275 *tmpstr = '\0';
276 }
277 return retstr;
278 }
279