1 #include "config.h"
2 #include "info.h"
3 #include "btree_cache.h"
4 
5 #ifdef HAVE_WINSOCK2_H
6 #include <winsock2.h>
7 #endif
8 #ifdef HAVE_WINDOWS_H
9 #include <windows.h>
10 #endif
11 #ifdef HAVE_STDIO_H
12 #include <stdio.h>
13 #endif
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17 #ifdef HAVE_STRING_H
18 #include <string.h>
19 #endif
20 #ifdef HAVE_STRINGS_H
21 #include <strings.h>
22 #endif
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26 #ifdef HAVE_TIME_H
27 #include <time.h>
28 #endif
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #if defined(HAVE_SYS_DIRENT_H) && !defined(HAVE_DIRENT_H)
36 #include <sys/dirent.h>
37 #endif
38 #ifdef HAVE_DIRENT_H
39 #include <dirent.h>
40 #endif
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_NETDB_H
45 #include <netdb.h>
46 #endif
47 #ifdef HAVE_TYPES_H
48 #include <types.h>
49 #endif
50 #ifdef HAVE_NETINET_IN_H
51 #include <netinet/in.h>
52 #endif
53 #ifdef HAVE_ARPA_INET_H
54 #include <arpa/inet.h>
55 #endif
56 #ifdef HAVE_SYS_STAT_H
57 #include <sys/stat.h>
58 #endif
59 #ifdef HAVE_CTYPE_H
60 #include <ctype.h>
61 #endif
62 #ifdef HAVE_ERRNO_H
63 #include <errno.h>
64 #endif
65 #ifdef HAVE_SYS_WAIT_H
66 #include <sys/wait.h>
67 #endif
68 #ifdef HAVE_GD_H
69 #define HAVE_GD 1
70 #include <gd.h>
71 #endif
72 #ifdef HAVE_GDFONTL_H
73 #include <gdfontl.h>
74 #endif
75 #ifdef HAVE_GDFONTT_H
76 #include <gdfontt.h>
77 #endif
78 #ifdef HAVE_GDFONTS_H
79 #include <gdfonts.h>
80 #endif
81 #ifdef HAVE_GDFONTMB_H
82 #include <gdfontmb.h>
83 #endif
84 #ifdef HAVE_GDFONTG_H
85 #include <gdfontg.h>
86 #endif
87 #ifdef HAVE_STDARG_H
88 #include <stdarg.h>
89 #endif
90 #ifdef HAVE_INTTYPES_H
91 #include <inttypes.h>
92 #endif
93 #ifdef HAVE_LIMITS_H
94 #include <limits.h>
95 #endif
96 #ifdef HAVE_WINSOCK_H
97 #include <winsock.h>
98 #endif
99 #ifdef HAVE_MATH_H
100 #include <math.h>
101 #endif
102 #ifdef HAVE_LOCALE_H
103 #include <locale.h>
104 #endif
105 #ifdef HAVE_EXECINFO_H
106 #include <execinfo.h>
107 #endif
108 #ifdef HAVE_LIBGEN_H
109 #include <libgen.h>
110 #endif
111 #ifdef HAVE_STDBOOL_H
112 #include <stdbool.h>
113 #else
114 typedef int bool;
115 #define true 1
116 #define false 0
117 #endif
118 #ifdef HAVE_FCNTL_H
119 #include <fcntl.h>
120 #endif
121 #ifdef HAVE_FNMATCH_H
122 #include <fnmatch.h>
123 #endif
124 
125 #if defined(HAVE_FOPEN64)
126 #define _FILE_OFFSET_BITS 64
127 #define MY_FOPEN fopen64
128 #else
129 #define MY_FOPEN fopen
130 #endif
131 
132 #if !defined(HAVE_BZERO)
133 #define bzero(mem,size) memset(mem,0,size)
134 #endif
135 
136 #if defined(IBERTY_LIB) && !defined(HAVE_MKSTEMP)
137 int mkstemps(char *template, int suffixlen);
138 #define mkstemp(template) mkstemps(template,0)
139 #endif
140 
141 #ifdef __MINGW32__
142 #define pgettext(msgctxt,msgid) (msgid)
143 #define ngettext(singular,plural,number) (plural)
144 #else
145 #include "gettext.h"
146 #endif
147 #if defined(ENABLE_NLS) && defined(HAVE_LIBINTL_H)
148 #define _(String) gettext(String)
149 #ifdef gettext_noop
150 #define N_(String) gettext_noop(String)
151 #else
152 #define N_(String) (String)
153 #endif
154 #else /* No NLS */
155 #define _(String) (String)
156 #define N_(String) (String)
157 #endif //NLS
158 
159 #if defined(__MINGW32__)
160 #define mkdir(p,m) _mkdir(p)
161 
162 #ifndef WIFEXITED
163   #define WIFEXITED(S) 1
164 #endif
165 
166 #ifndef WEXITSTATUS
167   #define WEXITSTATUS(S) (S)
168 #endif
169 
170 #endif /*__MINGW32__*/
171 
172 #ifndef __GNUC__
173 #  define  __attribute__(x)
174 #endif
175 
176 #define MAXLEN 20000
177 #define MAX_URL_LEN 40000
178 #define MAX_TRUNCATED_URL 250
179 #define MAX_USER_LEN 256
180 #define MAX_USER_FNAME_LEN 128
181 #define MAX_IP_LEN 64
182 #define MAX_DATETIME_LEN 32
183 #define MAX_REDIRECTOR_LOGS 64
184 #define MAX_REDIRECTOR_FILELEN 1024
185 /*!
186 Arbitrary limit on the number of days that are accepted in the selected range of the log file.
187 Sarg will complain that there are too many days in the files if this limit is overrun.
188 */
189 #define MAX_DATETIME_DAYS 1000
190 
191 #define REPORT_TYPE_USERS_SITES         0x0001UL
192 #define REPORT_TYPE_SITE_USER_TIME_DATE 0x0002UL
193 #define REPORT_TYPE_TOPUSERS            0x0004UL
194 #define REPORT_TYPE_TOPSITES            0x0008UL
195 #define REPORT_TYPE_SITES_USERS         0x0010UL
196 #define REPORT_TYPE_DATE_TIME           0x0020UL
197 #define REPORT_TYPE_DENIED              0x0040UL
198 #define REPORT_TYPE_AUTH_FAILURES       0x0080UL
199 #define REPORT_TYPE_DOWNLOADS           0x0100UL
200 #define REPORT_TYPE_USERAGENT           0x0200UL
201 
202 #define DATA_FIELD_USER      0x0001UL
203 #define DATA_FIELD_DATE      0x0002UL
204 #define DATA_FIELD_TIME      0x0004UL
205 #define DATA_FIELD_URL       0x0008UL
206 #define DATA_FIELD_CONNECT   0x0010UL
207 #define DATA_FIELD_BYTES     0x0020UL
208 #define DATA_FIELD_IN_CACHE  0x0040UL
209 #define DATA_FIELD_OUT_CACHE 0x0080UL
210 #define DATA_FIELD_ELAPSED   0x0100UL
211 
212 #define TOPUSERFIELDS_NUM          0x0001UL
213 #define TOPUSERFIELDS_DATE_TIME    0x0002UL
214 #define TOPUSERFIELDS_USERID       0x0004UL
215 #define TOPUSERFIELDS_CONNECT      0x0008UL
216 #define TOPUSERFIELDS_BYTES        0x0010UL
217 #define TOPUSERFIELDS_SETYB        0x0020UL
218 #define TOPUSERFIELDS_IN_CACHE_OUT 0x0040UL
219 #define TOPUSERFIELDS_USED_TIME    0x0080UL
220 #define TOPUSERFIELDS_MILISEC      0x0100UL
221 #define TOPUSERFIELDS_PTIME        0x0200UL
222 #define TOPUSERFIELDS_TOTAL        0x0400UL
223 #define TOPUSERFIELDS_AVERAGE      0x0800UL
224 #define TOPUSERFIELDS_USERIP       0x1000UL
225 
226 #define USERREPORTFIELDS_CONNECT      0x0001UL
227 #define USERREPORTFIELDS_BYTES        0x0002UL
228 #define USERREPORTFIELDS_SETYB        0x0004UL
229 #define USERREPORTFIELDS_IN_CACHE_OUT 0x0008UL
230 #define USERREPORTFIELDS_USED_TIME    0x0010UL
231 #define USERREPORTFIELDS_MILISEC      0x0020UL
232 #define USERREPORTFIELDS_PTIME        0x0040UL
233 #define USERREPORTFIELDS_TOTAL        0x0080UL
234 #define USERREPORTFIELDS_AVERAGE      0x0100UL
235 
236 #define INDEX_YES  0x0001UL
237 #define INDEX_NO   0x0002UL
238 #define INDEX_ONLY 0x0004UL
239 
240 #define INDEX_TREE_DATE 0x0001UL
241 #define INDEX_TREE_FILE 0x0002UL
242 
243 #define INDEXFIELDS_DIRSIZE 0x0001UL
244 
245 #define NTLMUSERFORMAT_USER       0x0001UL
246 #define NTLMUSERFORMAT_DOMAINUSER 0x0002UL
247 
248 #define RECORDWITHOUTUSER_IP        0x0001UL
249 #define RECORDWITHOUTUSER_IGNORE    0x0002UL
250 #define RECORDWITHOUTUSER_EVERYBODY 0x0004UL
251 
252 #define DATAFILEURL_IP   0x0001UL
253 #define DATAFILEURL_NAME 0x0002UL
254 
255 #define DISPLAY_BYTES 0x0001UL
256 #define DISPLAY_ABBREV 0x0002UL
257 
258 #define DATETIME_ELAP 0x0001UL
259 #define DATETIME_BYTE 0x0002UL
260 
261 #define REALTIME_UNAUTH_REC_SHOW   0x0001UL
262 #define REALTIME_UNAUTH_REC_IGNORE 0x0002UL
263 
264 #define SORT_REVERSE 0x0001
265 
266 #define TOPUSER_SORT_REVERSE SORT_REVERSE
267 #define TOPUSER_SORT_BYTES   0x0002UL
268 #define TOPUSER_SORT_USER    0x0004UL
269 #define TOPUSER_SORT_CONNECT 0x0008UL
270 #define TOPUSER_SORT_TIME    0x0010UL
271 
272 #define TOPSITE_SORT_REVERSE SORT_REVERSE
273 #define TOPSITE_SORT_BYTES   0x0002UL
274 #define TOPSITE_SORT_CONNECT 0x0004UL
275 #define TOPSITE_SORT_TIME    0x0008UL
276 #define TOPSITE_SORT_USER    0x0010UL
277 
278 #define USER_SORT_REVERSE SORT_REVERSE
279 #define USER_SORT_BYTES   0x0002UL
280 #define USER_SORT_SITE    0x0004UL
281 #define USER_SORT_CONNECT 0x0008UL
282 #define USER_SORT_TIME    0x0010UL
283 
284 //! Value to exclude all the javascripts from the html page.
285 #define HTML_JS_NONE 0x0000
286 //! Bit to include sorttable.js in the html plage.
287 #define HTML_JS_SORTTABLE 0x0001
288 
289 //! The character prefixed in front of the host names that are aliased.
290 #define ALIAS_PREFIX '*'
291 
292 //! Maximum number of limit files that can be stored.
293 #define MAX_USER_LIMITS 16
294 
295 //! Name of the html file containing the index of a report file.
296 #define INDEX_HTML_FILE "index.html"
297 
298 struct periodstruct
299 {
300    //! The first date of the period.
301    struct tm start;
302    //! The last date of the period.
303    struct tm end;
304    //! The textual representation of the date.
305    char text[90];
306    //! The HTML representation of the date.
307    char html[90];
308 };
309 
310 char outdir[MAXLEN];
311 char outdirname[MAXLEN];
312 struct periodstruct period;
313 char code[MAXLEN];
314 char code2[MAXLEN];
315 char tmp[MAXLEN];
316 char parse_out[MAXLEN];
317 char html[MAXLEN];
318 char ConfigFile[MAXLEN];
319 char df;
320 int LastLog;
321 bool RemoveTempFiles;
322 char ReplaceIndex[256];
323 unsigned long int Index;
324 bool OverwriteReport;
325 unsigned long int RecordsWithoutUser;
326 bool UseComma;
327 char MailUtility[PATH_MAX];
328 int TopSitesNum;
329 int TopUsersNum;
330 char ExcludeCodes[256];
331 unsigned long int TopsitesSort;
332 unsigned long int ReportType;
333 char UserTabFile[255];
334 char warea[MAXLEN];
335 char name[MAXLEN];
336 bool LongUrl;
337 bool Ip2Name;
338 int AccessLogFromCmdLine;
339 char Title[MAXLEN];
340 char BgColor[MAXLEN];
341 char BgImage[MAXLEN];
342 char TxColor[MAXLEN];
343 char TxBgColor[MAXLEN];
344 char TiColor[MAXLEN];
345 char LogoImage[MAXLEN];
346 char LogoText[MAXLEN];
347 char LogoTextColor[MAXLEN];
348 char Width[MAXLEN];
349 char Height[MAXLEN];
350 char FontFace[MAXLEN];
351 char HeaderColor[MAXLEN];
352 char HeaderBgColor[MAXLEN];
353 char FontSize[MAXLEN];
354 char PasswdFile[MAXLEN];
355 char TempDir[MAXLEN];
356 char TempDirPath[MAXLEN];
357 char OutputDir[MAXLEN];
358 char OutputEmail[MAXLEN];
359 unsigned long int TopuserSort;
360 unsigned long int UserSort;
361 char module[255];
362 char ExcludeHosts[255];
363 char ExcludeUsers[255];
364 char DateFormat;
365 bool UserIp;
366 char MaxElapsed[255];
367 unsigned long int datetimeby;
368 char CharSet[255];
369 char UserInvalidChar[255];
370 bool Graphs;
371 char GraphDaysBytesBarColor[255];
372 bool Privacy;
373 char PrivacyString[255];
374 char PrivacyStringColor[30];
375 char IncludeUsers[MAXLEN];
376 char ExcludeString[MAXLEN];
377 bool SuccessfulMsg;
378 unsigned long int TopUserFields;
379 unsigned long int UserReportFields;
380 char DataFile[MAXLEN];
381 char DataFileDelimiter[3];
382 unsigned long int DataFileFields;
383 unsigned long int DataFileUrl;
384 //! if \c true, show the number of lines read from the input log file during the reading of the file.
385 bool ShowReadStatistics;
386 /*!
387 If \c true, the read statistics also includes the percent of the number of lines read.
388 
389 Beware that it requires two readings of the input log file. It is not possible if the
390 input log file is stdin or a pipe.
391 */
392 bool ShowReadPercent;
393 char IndexSortOrder[5];
394 char DansGuardianConf[MAXLEN];
395 bool DansguardianFilterOutDate;
396 char SquidGuardConf[MAXLEN];
397 char SquidGuarddbHome[255];
398 char RedirectorLogFormat[4096];
399 int NRedirectorLogs;
400 char RedirectorLogs[MAX_REDIRECTOR_LOGS][MAX_REDIRECTOR_FILELEN];
401 int RedirectorLogFromCmdLine;
402 bool RedirectorFilterOutDate;
403 bool ShowSargInfo;
404 bool BytesInSitesUsersReport;
405 bool ShowSargLogo;
406 char ParsedOutputLog[MAXLEN];
407 char ParsedOutputLogCompress[512];
408 unsigned long int DisplayedValues;
409 char HeaderFontSize[5];
410 char TitleFontSize[5];
411 char wwwDocumentRoot[MAXLEN];
412 char ExternalCSSFile[MAXLEN];
413 char BlockIt[255];
414 unsigned long int NtlmUserFormat;
415 //! How to display the index of the reports.
416 unsigned long int IndexTree;
417 //! The columns to show in the index of the reports.
418 unsigned long int IndexFields;
419 bool UserAuthentication;
420 char AuthUserTemplateFile[1024];
421 //! \c True to use anonymous file and directory names in the report.
422 bool AnonymousOutputFiles;
423 char val1[MAXLEN];
424 char val3[MAXLEN];
425 char val5[MAXLEN];
426 char val6[MAXLEN];
427 char val7[MAXLEN];
428 char val8[MAXLEN];
429 char val9[MAXLEN];
430 char val10[MAXLEN];
431 char val11[MAXLEN];
432 char mask[MAXLEN];
433 char site[MAXLEN];
434 char us[50];
435 char email[MAXLEN];
436 char test[1];
437 char user2[MAXLEN];
438 char wentp[512];
439 char addr[MAXLEN];
440 char Ulimit[6];
441 char RealtimeTypes[1024];
442 char cmd[255];
443 char ImageFile[255];
444 unsigned long int RealtimeUnauthRec;
445 char LDAPHost[255];
446 char LDAPBindDN[512];
447 char LDAPBindPW[255];
448 int LDAPPort;
449 int LDAPProtocolVersion;
450 char LDAPBaseSearch[255];
451 char LDAPFilterSearch[512];
452 char LDAPTargetAttr[64];
453 //! Character set to convert the LDAP returned string to.
454 char LDAPNativeCharset[20];
455 char GraphFont[MAXLEN];
456 //! The full path to sorttable.js if the table in the reports must be dynamicaly sorted.
457 char SortTableJs[256];
458 //! The name of the file containing the host names to replace by an alias in the report.
459 char HostAliasFile[512];
460 //! The name of the file containing the user names to replace by an alias in the report.
461 char UserAliasFile[512];
462 //! The number of consecutive errors allowed in an input log file before the process is interrupted.
463 int NumLogSuccessiveErrors;
464 /*!
465 The total number of errors allowed in an input log file before the process is interrupted. A negative
466 value means the process should never fails irrespective of the number of errors found in the input
467 log files.
468 */
469 int NumLogTotalErrors;
470 //! Count the number of lines read from the input log files.
471 unsigned long int lines_read;
472 //! Count the number of records kept for the processing.
473 unsigned long int records_kept;
474 //! Count the number of users.
475 unsigned long int nusers;
476 
477 int  idate;
478 int  dansguardian_count;
479 int  redirector_count;
480 int  useragent_count;
481 int  z1, z2, z3;
482 int  ttopen;
483 int  sarglog;
484 int  isalog;
485 bool dataonly;
486 bool indexonly;
487 bool iprel;
488 int  langcode;
489 int  debug;
490 int  debugz;
491 int  AuthfailReportLimit;
492 int  DeniedReportLimit;
493 int  DownloadReportLimit;
494 int  SiteUsersReportLimit;
495 int  DansGuardianReportLimit;
496 int  SquidGuardReportLimit;
497 int  UserReportLimit;
498 int  realtime_refresh;
499 int  realtime_access_log_lines;
500 int  rc;
501 int  ntopsites;
502 int  nrepday;
503 bool  squid24;
504 //! \c True to keep the temporary files for inspection.
505 bool KeepTempLog;
506 
507 long long int nocost;
508 float cost;
509