]> git.ipfire.org Git - thirdparty/sarg.git/blame - log.c
Add a double check on the data at the user's level
[thirdparty/sarg.git] / log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
94ff9470 4 *
25697a35 5 * SARG donations:
94ff9470 6 * please look at http://sarg.sourceforge.net/donations.php
ad500baa
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
9b179eb0
FM
30#ifdef HAVE_GETOPT_H
31#include <getopt.h>
32#endif
33
25697a35 34#define REPORT_EVERY_X_LINES 5000
f2ec8c75
FM
35#define MAX_OPEN_USER_FILES 10
36
37struct userfilestruct
38{
9bd92830
FM
39 struct userfilestruct *next;
40 struct userinfostruct *user;
41 FILE *file;
f2ec8c75 42};
25697a35 43
2824ec9b 44/*@null@*/static char *userfile=NULL;
25697a35
GS
45
46numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
007905af 47numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
25697a35 48
936c9905 49static void getusers(const char *pwdfile, int debug);
25697a35 50
25f0793f 51int main(int argc,char *argv[])
25697a35 52{
9bd92830
FM
53 enum isa_col_id {
54 ISACOL_Ip,
55 ISACOL_UserName,
56 ISACOL_Date,
57 ISACOL_Time,
58 ISACOL_TimeTaken,
59 ISACOL_Bytes,
60 ISACOL_Uri,
61 ISACOL_Status,
62 ISACOL_Last //last entry of the list !
63 };
64 enum InputLogFormat {
65 ILF_Unknown,
66 ILF_Squid,
67 ILF_Common,
68 ILF_Sarg,
69 ILF_Isa,
70 ILF_Last //last entry of the list !
71 };
72
73 FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL;
74
75 char sz_Download_Unsort[ 20000 ] ;
76 FILE * fp_Download_Unsort = NULL ;
77
78 extern int optind;
79 extern int optopt;
80 extern char *optarg;
81
82 char data[255];
83 char elap[255];
84 char ip[MAXLEN];
85 char tam[255];
86 char fun[MAXLEN];
87 char wuser[MAXLEN];
88 char smartfilter[MAXLEN];
89 char dia[128];
90 char mes[30];
9bd92830 91 char hora[30];
9bd92830
FM
92 char date[255];
93 char arq[255];
94 char arq_log[255];
95 int hm, hmf, hmr;
96 char hm_str[15];
97 char uagent[MAXLEN];
98 char hexclude[MAXLEN];
99 char csort[MAXLEN];
100 int cstatus;
101 char tbuf2[128];
9bd92830
FM
102 char *str;
103 char tmp3[MAXLEN];
104 char denied_unsort[MAXLEN];
105 char denied_sort[MAXLEN];
106 char authfail_unsort[MAXLEN];
107 char start_hour[128];
9bd92830 108 char *linebuf;
22715352
FM
109 const char *url;
110 char *full_url;
9bd92830
FM
111 char *urly;
112 char user[MAX_USER_LEN];
2c7e8c23 113 char splitprefix[MAXLEN];
9bd92830
FM
114 enum InputLogFormat ilf;
115 int ilf_count[ILF_Last];
116 int ch;
117 int x;
118 int errflg=0;
119 int puser=0;
120 bool fhost=false;
121 bool dns=false;
122 bool fuser=false;
123 int idata=0;
124 int mindate=0;
125 int maxdate=0;
126 int iarq=0;
127 int isa_ncols=0,isa_cols[ISACOL_Last];
128 int lastlog=-1;
d91457d2
FM
129 long int nbytes;
130 long int elap_time;
9bd92830
FM
131 bool from_stdin;
132 bool from_pipe;
133 int blen;
134 int maxopenfiles;
135 int nopen;
136 bool id_is_ip;
137 long totregsl=0;
138 long totregsg=0;
139 long totregsx=0;
140 bool totper=false;
141 long int max_elapsed=0;
142 long long int iyear, imonth, iday;
143 bool realt;
144 bool userip;
145 struct tm tt;
146 struct tm *t;
147 unsigned long recs1=0UL;
148 unsigned long recs2=0UL;
149 int OutputNonZero = REPORT_EVERY_X_LINES ;
150 bool download_flag=false;
151 char *download_url=NULL;
152 struct getwordstruct gwarea;
153 longline line;
154 time_t tnum;
155 struct stat logstat;
156 struct userinfostruct *uinfo;
157 struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile;
158 static int split=0;
159 static int convert=0;
160 static int output_css=0;
161 int option_index;
162 static struct option long_options[]=
163 {
164 {"convert",no_argument,&convert,1},
165 {"css",no_argument,&output_css,1},
166 {"lastlog",required_argument,NULL,2},
167 {"keeplogs",no_argument,NULL,3},
168 {"split",no_argument,&split,1},
2c7e8c23 169 {"splitprefix",required_argument,NULL,'P'},
9bd92830
FM
170 {0,0,0,0}
171 };
25697a35 172
abfc169e 173#ifdef HAVE_LOCALE_H
9bd92830 174 setlocale(LC_TIME,"");
abfc169e
FM
175#endif
176
cec3eed3 177#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
9bd92830
FM
178 if (!setlocale (LC_ALL, "")) {
179 fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n");
180 exit(EXIT_FAILURE);
181 }
182 if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) {
183 fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno));
184 exit(EXIT_FAILURE);
185 }
186 if (!textdomain (PACKAGE_NAME)) {
187 fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno));
188 exit(EXIT_FAILURE);
189 }
abfc169e
FM
190#endif //ENABLE_NLS
191
9bd92830
FM
192 BgImage[0]='\0';
193 LogoImage[0]='\0';
194 LogoText[0]='\0';
195 PasswdFile[0]='\0';
196 OutputEmail[0]='\0';
197 UserAgentLog[0]='\0';
198 ExcludeHosts[0]='\0';
199 ExcludeUsers[0]='\0';
200 ConfigFile[0]='\0';
201 code[0]='\0';
202 LastLog=0;
203 ReportType=0UL;
204 UserTabFile[0]='\0';
205 BlockIt[0]='\0';
206 ExternalCSSFile[0]='\0';
207 RedirectorLogFormat[0]='\0';
208 NRedirectorLogs=0;
209 for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0;
210
8d811f76 211 snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR);
9bd92830
FM
212 strcpy(GraphDaysBytesBarColor,"orange");
213 strcpy(BgColor,"#ffffff");
214 strcpy(TxColor,"#000000");
215 strcpy(TxBgColor,"lavender");
216 strcpy(TiColor,"darkblue");
217 strcpy(Width,"80");
218 strcpy(Height,"45");
219 strcpy(LogoTextColor,"#000000");
220 strcpy(HeaderColor,"darkblue");
221 strcpy(HeaderBgColor,"#dddddd");
222 strcpy(LogoTextColor,"#006699");
223 strcpy(FontSize,"9px");
224 strcpy(TempDir,"/tmp");
225 strcpy(OutputDir,"/var/www/html/squid-reports");
829a53c2 226 AnonymousOutputFiles=false;
9bd92830
FM
227 Ip2Name=false;
228 strcpy(DateFormat,"u");
229 OverwriteReport=false;
230 RemoveTempFiles=true;
231 strcpy(ReplaceIndex,"index.html");
232 Index=INDEX_YES;
233 RecordsWithoutUser=RECORDWITHOUTUSER_IP;
234 UseComma=0;
235 strcpy(MailUtility,"mailx");
236 TopSitesNum=100;
237 TopUsersNum=0;
238 UserIp=0;
239 TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE;
240 UserSort=USER_SORT_BYTES | USER_SORT_REVERSE;
241 TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE;
242 LongUrl=0;
243 strcpy(FontFace,"Verdana,Tahoma,Arial");
244 datetimeby=DATETIME_BYTE;
245 strcpy(CharSet,"ISO-8859-1");
246 Privacy=0;
247 strcpy(PrivacyString,"***.***.***.***");
248 strcpy(PrivacyStringColor,"blue");
249 SuccessfulMsg=true;
250 TopUserFields=TOPUSERFIELDS_NUM | TOPUSERFIELDS_DATE_TIME | TOPUSERFIELDS_USERID | TOPUSERFIELDS_CONNECT |
007905af
FM
251 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
252 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
253 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
9bd92830 254 UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
007905af
FM
255 USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
256 USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
9bd92830
FM
257 strcpy(DataFileDelimiter,";");
258 DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
007905af 259 DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
9bd92830
FM
260 ShowReadStatistics=true;
261 strcpy(IndexSortOrder,"D");
262 ShowSargInfo=true;
263 ShowSargLogo=true;
8949e757 264 ParsedOutputLog[0]='\0';
9bd92830
FM
265 strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
266 DisplayedValues=DISPLAY_ABBREV;
267 strcpy(HeaderFontSize,"9px");
268 strcpy(TitleFontSize,"11px");
269 strcpy(AuthUserTemplateFile,"sarg_htaccess");
270 set_download_suffix("7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys,tar,tgz,vcd,vob,wma,wmv,zip");
271 Graphs=true;
3becf85c 272#if defined(FONTDIR)
9bd92830 273 strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
3becf85c 274#else
9bd92830 275 GraphFont[0]='\0';
3becf85c 276#endif
9bd92830
FM
277 strcpy(Ulimit,"20000");
278 NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
279 IndexTree=INDEX_TREE_FILE;
9aaa3361 280 IndexFields=INDEXFIELDS_DIRSIZE;
9bd92830
FM
281 strcpy(RealtimeTypes,"GET,PUT,CONNECT");
282 RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
283 RedirectorFilterOutDate=true;
284 DansguardianFilterOutDate=true;
285 DataFileUrl=DATAFILEURL_IP;
286 strcpy(MaxElapsed,"28800000");
287 BytesInSitesUsersReport=0;
288 UserAuthentication=0;
289 strcpy(LDAPHost,"127.0.0.1");
290 LDAPPort=389;
291 LDAPProtocolVersion=3;
292 LDAPBindDN[0]='\0';
293 LDAPBindPW[0]='\0';
294 LDAPBaseSearch[0]='\0';
295 strcpy(LDAPFilterSearch, "(uid=%s)");
296 strcpy(LDAPTargetAttr, "cn");
297 SortTableJs[0]='\0';
298
299 dia[0]='\0';
300 mes[0]='\0';
9bd92830
FM
301 hora[0]='\0';
302 tmp[0]='\0';
303 tmp3[0]='\0';
9bd92830
FM
304 us[0]='\0';
305 date[0]='\0';
306 df[0]='\0';
307 uagent[0]='\0';
308 hexclude[0]='\0';
309 addr[0]='\0';
310 hm=-1;
311 hmf=-1;
312 site[0]='\0';
313 outdir[0]='\0';
2c7e8c23 314 splitprefix[0]='\0';
9bd92830
FM
315 elap[0]='\0';
316 email[0]='\0';
9bd92830
FM
317 UserInvalidChar[0]='\0';
318 DataFile[0]='\0';
319 SquidGuardConf[0]='\0';
320 DansGuardianConf[0]='\0';
321 start_hour[0]='\0';
9bd92830 322 hm_str[0]='\0';
22715352 323 HostAliasFile[0]='\0';
9bd92830
FM
324
325 denied_count=0;
326 download_count=0;
327 authfail_count=0;
328 dansguardian_count=0;
330b1c52 329 redirector_count=0;
9bd92830
FM
330 useragent_count=0;
331 DeniedReportLimit=10;
332 AuthfailReportLimit=10;
333 DansGuardianReportLimit=10;
334 SquidGuardReportLimit=10;
335 DownloadReportLimit=50;
336 UserReportLimit=0;
337 debug=0;
338 debugz=0;
339 debugm=0;
340 iprel=false;
341 userip=false;
342 realt=false;
343 realtime_refresh=3;
344 realtime_access_log_lines=1000;
345 cost=0.01;
346 nocost=50000000;
347 ndownload=0;
348 squid24=false;
349 dfrom=0;
350 duntil=0;
351
352 bzero(IncludeUsers, sizeof(IncludeUsers));
353 bzero(ExcludeString, sizeof(ExcludeString));
354 first_user_file=NULL;
355 memset(&period,0,sizeof(period));
356
357 NAccessLog=0;
358 for(x=0; x<MAXLOGS; x++)
359 AccessLog[x][0]='\0';
360 AccessLogFromCmdLine=0;
361 RedirectorLogFromCmdLine=0;
362
363 strcpy(Title,_("Squid User Access Report"));
364
2c7e8c23 365 while((ch = getopt_long_only(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:P:hijmnprvxyz",long_options,&option_index)) != -1){
9bd92830
FM
366 switch(ch)
367 {
368 case 0:
369 break;
370 case 2:
371 lastlog=atoi(optarg);
372 break;
373 case 3:
374 lastlog=0;
375 break;
376 case 'a':
a87d4d11 377 safe_strcpy(addr,optarg,sizeof(addr));
9bd92830
FM
378 break;
379 case 'b':
a87d4d11 380 safe_strcpy(uagent,optarg,sizeof(uagent));
9bd92830
FM
381 break;
382 case 'c':
a87d4d11 383 safe_strcpy(hexclude,optarg,sizeof(hexclude));
9bd92830
FM
384 break;
385 case 'd':
a87d4d11 386 safe_strcpy(date,optarg,sizeof(date));
9bd92830
FM
387 date_from(date, &dfrom, &duntil);
388 break;
389 case 'e':
a87d4d11 390 safe_strcpy(email,optarg,sizeof(email));
9bd92830
FM
391 break;
392 case 'f':
a87d4d11 393 safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile));
9bd92830
FM
394 break;
395 case 'g':
a87d4d11 396 safe_strcpy(df,optarg,sizeof(df));
9bd92830
FM
397 break;
398 case 'h':
399 usage(argv[0]);
400 exit(EXIT_SUCCESS);
401 case 'i':
402 iprel=true;
403 break;
404 case 'l':
405 if (NAccessLog>=MAXLOGS) {
406 debuga(_("Too many log files passed on command line with option -l.\n"));
407 exit(EXIT_FAILURE);
408 }
409 if (strlen(optarg)>=MAX_LOG_FILELEN) {
410 debuga(_("Log file name too long passed on command line with option -l: %s\n"),optarg);
411 exit(EXIT_FAILURE);
412 }
413 strcpy(AccessLog[NAccessLog],optarg);
414 NAccessLog++;
415 AccessLogFromCmdLine++;
416 break;
417 case 'L':
418 if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
419 debuga(_("Too many redirector logs passed on command line with option -L.\n"));
420 exit(EXIT_FAILURE);
421 }
422 if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
423 debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg);
424 exit(EXIT_FAILURE);
425 }
426 strcpy(RedirectorLogs[NRedirectorLogs],optarg);
427 NRedirectorLogs++;
428 RedirectorLogFromCmdLine++;
429 break;
430 case 'm':
431 debugm++;
432 break;
433 case 'n':
434 dns=true;
435 break;
436 case 'o':
a87d4d11 437 safe_strcpy(outdir,optarg,sizeof(outdir));
9bd92830
FM
438 break;
439 case 'p':
440 userip=true;
441 break;
2c7e8c23 442 case 'P':
a87d4d11 443 safe_strcpy(splitprefix,optarg,sizeof(splitprefix));
2c7e8c23 444 break;
9bd92830
FM
445 case 'r':
446 realt=true;
447 break;
448 case 's':
a87d4d11 449 safe_strcpy(site,optarg,sizeof(site));
9bd92830
FM
450 break;
451 case 't':
452 {
453 int h1,m1,h2,m2;
454
455 if(strstr(optarg,"-") == 0) {
456 if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) {
457 debuga(_("Time period passed on the command line with option -t must be MM:SS\n"));
458 exit(EXIT_FAILURE);
459 }
460 hm=h1*100+m1;
461 hmf=hm;
462 snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1);
463 } else {
464 if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) {
465 debuga(_("Time range passed on the command line with option -t must be MM:SS-MM:SS\n"));
466 exit(EXIT_FAILURE);
467 }
468 hm=h1*100+m1;
469 hmf=h2*100+m2;
470 snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2);
471 }
472 break;
473 }
474 case 'u':
a87d4d11 475 safe_strcpy(us,optarg,sizeof(us));
9bd92830
FM
476 break;
477 case 'v':
478 version();
479 break;
480 case 'w':
a87d4d11 481 safe_strcpy(tmp,optarg,sizeof(tmp));
9bd92830
FM
482 break;
483 case 'x':
484 debug++;
485 break;
486 case 'y':
487 langcode++;
488 break;
489 case 'z':
490 debugz++;
491 break;
007905af
FM
492 case ':':
493 debuga(_("Option -%c requires an argument\n"),optopt);
494 exit(EXIT_FAILURE);
9bd92830
FM
495 case '?':
496 usage(argv[0]);
497 exit(EXIT_FAILURE);
498 default:
499 abort();
500 }
501 }
502
503 if (errflg>0) {
504 usage(argv[0]);
505 exit(2);
506 }
507
508 if (optind<argc) {
509 for (iarq=optind ; iarq<argc ; iarq++) {
510 if (NAccessLog>=MAXLOGS) {
511 debuga(_("Too many log files passed on command line.\n"));
512 exit(EXIT_FAILURE);
513 }
514 if (strlen(argv[iarq])>=MAX_LOG_FILELEN) {
515 debuga(_("Log file name too long passed on command line: %s\n"),argv[iarq]);
516 exit(EXIT_FAILURE);
517 }
518 strcpy(AccessLog[NAccessLog],argv[iarq]);
519 NAccessLog++;
520 AccessLogFromCmdLine++;
521 }
522 }
523
524 if(debug) debuga(_("Init\n"));
525
526 if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR);
527 if(access(ConfigFile, R_OK) != 0) {
528 debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno));
529 exit(EXIT_FAILURE);
530 }
531
532 if(access(ConfigFile, R_OK) == 0)
533 getconf();
534
535 if(userip) UserIp=true;
536
537 if(dns) Ip2Name=true;
538
539 if (lastlog>=0) LastLog=lastlog;
540
2c7e8c23
FM
541 if(outdir[0] == '\0') strcpy(outdir,OutputDir);
542 if(outdir[0] != '\0') strcat(outdir,"/");
543
9bd92830
FM
544 if(realt) {
545 realtime();
546 exit(EXIT_SUCCESS);
547 }
548
549 if(IndexTree == INDEX_TREE_FILE)
550 strcpy(ImageFile,"../images");
551 else
552 strcpy(ImageFile,"../../../images");
553
554 dataonly=0;
555 if(DataFile[0] != '\0')
556 dataonly++;
557
3e80818c
FM
558 if(df[0] == '\0') strcpy(df,DateFormat);
559 else strcpy(DateFormat,df);
560
561 if(df[0] == '\0') {
562 strcpy(df,"u");
563 strcpy(DateFormat,"u");
564 }
565 if (df[0]=='w')
566 IndexTree=INDEX_TREE_FILE;
567
9bd92830
FM
568 if(NAccessLog == 0) {
569 strcpy(AccessLog[0],"/var/log/squid/access.log");
570 NAccessLog++;
571 }
572
573 if(output_css) {
574 css_content(stdout);
575 exit(EXIT_SUCCESS);
576 }
577 if(split) {
578 for (iarq=0 ; iarq<NAccessLog ; iarq++)
2c7e8c23 579 splitlog(AccessLog[iarq], df, dfrom, duntil, convert, splitprefix);
9bd92830
FM
580 exit(EXIT_SUCCESS);
581 }
582 if(convert) {
583 for (iarq=0 ; iarq<NAccessLog ; iarq++)
584 convlog(AccessLog[iarq], df, dfrom, duntil);
585 exit(EXIT_SUCCESS);
586 }
587
588 load_excludecodes(ExcludeCodes);
589
590 if(access(PasswdFile, R_OK) == 0) {
591 getusers(PasswdFile,debug);
592 puser++;
593 }
594
595 if(hexclude[0] == '\0')
596 strcpy(hexclude,ExcludeHosts);
597 if(hexclude[0] != '\0') {
598 gethexclude(hexclude,debug);
599 fhost=true;
600 }
601
602 if(ReportType == 0) {
603 ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
007905af
FM
604 REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED |
605 REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS;
9bd92830
FM
606 }
607
608 if(access(ExcludeUsers, R_OK) == 0) {
609 getuexclude(ExcludeUsers,debug);
610 fuser=true;
611 }
22715352
FM
612 if (HostAliasFile[0] != '\0')
613 read_hostalias(HostAliasFile);
9bd92830 614
6588b137 615 indexonly=false;
9bd92830
FM
616 if(fuser) {
617 if(is_indexonly())
6588b137 618 indexonly=true;
9bd92830 619 }
6588b137
FM
620 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true;
621 if(Index == INDEX_ONLY) indexonly=true;
9bd92830
FM
622
623 if(MaxElapsed[0] != '\0') max_elapsed=atol(MaxElapsed);
624
9bd92830
FM
625 if(uagent[0] == '\0') strcpy(uagent,UserAgentLog);
626
627 if(tmp[0] == '\0') strcpy(tmp,TempDir);
628 else strcpy(TempDir,tmp);
629 /*
630 For historical reasons, the temporary directory is the subdirectory "sarg" of the path
631 provided by the user.
632 */
633 strcat(tmp,"/sarg");
634
635 if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) {
636 debuga(_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp);
637 exit(EXIT_FAILURE);
638 }
639
9bd92830
FM
640 if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail);
641
642 if(email[0] != '\0') {
643 my_mkdir(tmp);
644 strcpy(outdir,tmp);
645 strcat(outdir,"/");
646 }
647
648 if(access(tmp, R_OK) == 0) {
6b380a79 649 if (debug) debuga(_("Deleting directory %s\n"),tmp);
9bd92830
FM
650 unlinkdir(tmp,1);
651 }
652 my_mkdir(tmp);
653 snprintf(denied_unsort,sizeof(denied_unsort),"%s/denied.log.unsort",tmp);
654 snprintf(denied_sort,sizeof(denied_sort),"%s/denied.log",tmp);
655 snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.log.unsort",tmp);
656
657 if(debug) {
658 debuga(_("Parameters:\n"));
659 debuga(_(" Hostname or IP address (-a) = %s\n"),addr);
660 debuga(_(" Useragent log (-b) = %s\n"),uagent);
661 debuga(_(" Exclude file (-c) = %s\n"),hexclude);
662 debuga(_(" Date from-until (-d) = %s\n"),date);
663 debuga(_(" Email address to send reports (-e) = %s\n"),email);
664 debuga(_(" Config file (-f) = %s\n"),ConfigFile);
665 if(strcmp(df,"e") == 0)
666 debuga(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
667 if(strcmp(df,"u") == 0)
668 debuga(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
669 if(strcmp(df,"w") == 0)
670 debuga(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
671 debuga(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
672 for (iarq=0 ; iarq<NAccessLog ; iarq++)
673 debuga(_(" Input log (-l) = %s\n"),AccessLog[iarq]);
674 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
675 debuga(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
676 debuga(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
677 debuga(_(" Output dir (-o) = %s\n"),outdir);
678 debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
679 debuga(_(" Accessed site (-s) = %s\n"),site);
680 debuga(_(" Time (-t) = %s\n"),hm_str);
681 debuga(_(" User (-u) = %s\n"),us);
682 debuga(_(" Temporary dir (-w) = %s\n"),tmp);
683 debuga(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
684 debuga(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
685 debuga(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
686 debuga("\n");
687 }
688
689 if(debugm) {
690 printf(_("Parameters:\n"));
691 printf(_(" Hostname or IP address (-a) = %s\n"),addr);
692 printf(_(" Useragent log (-b) = %s\n"),uagent);
693 printf(_(" Exclude file (-c) = %s\n"),hexclude);
694 printf(_(" Date from-until (-d) = %s\n"),date);
695 printf(_(" Email address to send reports (-e) = %s\n"),email);
696 printf(_(" Config file (-f) = %s\n"),ConfigFile);
697 if(strcmp(df,"e") == 0)
698 printf(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
699 if(strcmp(df,"u") == 0)
700 printf(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
701 if(strcmp(df,"w") == 0)
702 printf(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
703 printf(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
704 for (iarq=0 ; iarq<NAccessLog ; iarq++)
705 printf(_(" Input log (-l) = %s\n"),AccessLog[iarq]);
706 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
707 printf(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
708 printf(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
709 printf(_(" Output dir (-o) = %s\n"),outdir);
710 printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
711 printf(_(" Accessed site (-s) = %s\n"),site);
712 printf(_(" Time (-t) = %s\n"),hm_str);
713 printf(_(" User (-u) = %s\n"),us);
714 printf(_(" Temporary dir (-w) = %s\n"),tmp);
715 printf(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
716 printf(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
717 printf(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
718 printf(_("sarg version: %s\n"),VERSION);
719 }
720
721 if(debug)
722 debuga(_("sarg version: %s\n"),VERSION);
25697a35 723
04a01ed3 724#ifdef ENABLE_DOUBLE_CHECK_DATA
9bd92830 725 debuga(_("Sarg compiled to report warnings if the output is inconsistent\n"));
04a01ed3
FM
726#endif
727
9bd92830 728 maxopenfiles=MAX_OPEN_USER_FILES;
e112fa1f 729#ifdef HAVE_RLIM_T
9bd92830
FM
730 if (Ulimit[0] != '\0') {
731 struct rlimit rl;
732 long l1, l2;
733 int rc=0;
243a9fab 734
25697a35 735#if defined(RLIMIT_NOFILE)
9bd92830 736 getrlimit (RLIMIT_NOFILE, &rl);
ed1ac52f 737#elif defined(RLIMIT_OFILE)
9bd92830 738 getrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
739#else
740#warning "No rlimit resource for the number of open files"
25697a35 741#endif
9bd92830
FM
742 l1 = rl.rlim_cur;
743 l2 = rl.rlim_max;
d6e703cc 744
9bd92830
FM
745 rl.rlim_cur = atol(Ulimit);
746 rl.rlim_max = atol(Ulimit);
243a9fab 747#if defined(RLIMIT_NOFILE)
9bd92830 748 rc=setrlimit (RLIMIT_NOFILE, &rl);
243a9fab 749#elif defined(RLIMIT_OFILE)
9bd92830 750 rc=setrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
751#else
752#warning "No rlimit resource for the number of open files"
753#endif
9bd92830 754 if(rc == -1) {
007905af 755 debuga(_("setrlimit error - %s\n"),strerror(errno));
9bd92830 756 }
25697a35 757
9bd92830 758 if(debug)
c4633554 759 debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING"\n",l1,l2,rl.rlim_cur,rl.rlim_max);
9bd92830 760 }
e112fa1f 761#endif
25697a35 762
9bd92830
FM
763 init_usertab(UserTabFile);
764
765 if ((line=longline_create())==NULL) {
766 debuga(_("Not enough memory to read a log file\n"));
767 exit(EXIT_FAILURE);
768 }
769
770 snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.unsort", tmp);
771
772 if(DataFile[0]=='\0') {
773 if((ReportType & REPORT_TYPE_DENIED) != 0) {
774 if((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) {
775 debuga(_("(log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno));
776 exit(EXIT_FAILURE);
777 }
778 }
779
780 if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
781 if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
782 debuga(_("(log) Cannot open file: %s - %s\n"),authfail_unsort,strerror(errno));
783 exit(EXIT_FAILURE);
784 }
785 }
786 }
787
788 for (iarq=0 ; iarq<NAccessLog ; iarq++) {
789 strcpy(arq,AccessLog[iarq]);
790
791 strcpy(arqtt,arq);
792
793 if(strcmp(arq,"-")==0) {
794 if(debug)
795 debuga(_("Reading access log file: from stdin\n"));
796 fp_in=stdin;
797 from_stdin=true;
798 } else {
799 if (date[0]!='\0') {
800 if (stat(arq,&logstat)!=0) {
801 debuga(_("Cannot get the modification time of input log file %s (%s). Processing it anyway\n"),arq,strerror(errno));
802 } else {
803 struct tm *logtime=localtime(&logstat.st_mtime);
804 if ((logtime->tm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday<dfrom) {
805 debuga(_("Ignoring old log file %s\n"),arq);
806 continue;
807 }
808 }
809 }
810 fp_in=decomp(arq,&from_pipe);
811 if(fp_in==NULL) {
812 debuga(_("(log) Cannot open log file: %s - %s\n"),arq,strerror(errno));
813 exit(EXIT_FAILURE);
814 }
815 if(debug) debuga(_("Reading access log file: %s\n"),arq);
816 from_stdin=false;
817 }
818 ilf=ILF_Unknown;
819 download_flag=false;
820 // pre-read the file only if we have to show stats
821 if(ShowReadStatistics && !from_stdin && !from_pipe) {
822 size_t nread,i;
823 bool skipcr=false;
824 char tmp4[MAXLEN];
825
826 recs1=0UL;
827 recs2=0UL;
828
829 while ((nread=fread(tmp4,1,sizeof(tmp4),fp_in))>0) {
830 for (i=0 ; i<nread ; i++)
831 if (skipcr) {
832 if (tmp4[i]!='\n' && tmp4[i]!='\r') {
833 skipcr=false;
834 }
835 } else {
836 if (tmp4[i]=='\n' || tmp4[i]=='\r') {
837 skipcr=true;
838 recs1++;
839 }
840 }
841 }
842 rewind(fp_in);
843 printf(_("SARG: Records in file: %lu, reading: %3.2f%%"),recs1,(float) 0);
844 putchar('\r');
845 fflush( stdout ) ;
846 }
847
848 longline_reset(line);
849
850 while ((linebuf=longline_read(fp_in,line))!=NULL) {
851 blen=strlen(linebuf);
852
853 if (ilf==ILF_Unknown) {
854 if(strncmp(linebuf,"#Software: Mic",14) == 0) {
855 fixendofline(linebuf);
856 if (debug)
857 debuga(_("Log is from Microsoft ISA: %s\n"),linebuf);
858 ilf=ILF_Isa;
859 ilf_count[ilf]++;
860 continue;
861 }
862
863 if(strncmp(linebuf,"*** SARG Log ***",16) == 0) {
864 if (getperiod_fromsarglog(arqtt,&period)<0) {
865 debuga(_("The name of the file is invalid: %s\n"),arq);
866 exit(EXIT_FAILURE);
867 }
868 ilf=ILF_Sarg;
869 ilf_count[ilf]++;
870 continue;
871 }
872 }
873
8949e757 874 if(!fp_log && ParsedOutputLog[0] && ilf!=ILF_Sarg) {
9bd92830
FM
875 if(access(ParsedOutputLog,R_OK) != 0) {
876 my_mkdir(ParsedOutputLog);
877 }
8d811f76 878 if (snprintf(arq_log,sizeof(arq_log),"%s/sarg_temp.log",ParsedOutputLog)>=sizeof(arq_log)) {
c4633554 879 debuga(_("File name too long: %s/sarg_temp.log\n"),ParsedOutputLog);
8d811f76
FM
880 exit(EXIT_FAILURE);
881 }
9bd92830
FM
882 if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) {
883 debuga(_("(log) Cannot open log file: %s - %s\n"),arq_log,strerror(errno));
884 exit(EXIT_FAILURE);
885 }
886 fputs("*** SARG Log ***\n",fp_log);
887 }
888
889 recs2++;
890 if( ShowReadStatistics && !from_stdin && !from_pipe && --OutputNonZero<=0) {
007905af
FM
891 double perc = recs2 * 100. / recs1 ;
892 printf(_("SARG: Records in file: %lu, reading: %3.2lf%%"),recs2,perc);
893 putchar('\r');
894 fflush (stdout);
895 OutputNonZero = REPORT_EVERY_X_LINES ;
9bd92830
FM
896 }
897 if(blen < 58) continue;
898 if(strstr(linebuf,"HTTP/0.0") != 0) continue;
899 if(strstr(linebuf,"logfile turned over") != 0) continue;
900 if(linebuf[0] == ' ') continue;
901
902 // exclude_string
903 if(ExcludeString[0] != '\0') {
904 bool exstring=false;
905 getword_start(&gwarea,ExcludeString);
906 while(strchr(gwarea.current,':') != 0) {
907 if (getword_multisep(val1,sizeof(val1),&gwarea,':')<0) {
908 debuga(_("Maybe you have a broken record or garbage in your exclusion string\n"));
909 exit(EXIT_FAILURE);
910 }
911 if((str=(char *) strstr(linebuf,val1)) != (char *) NULL ) {
912 exstring=true;
913 break;
914 }
915 }
916 if(!exstring && (str=(char *) strstr(linebuf,gwarea.current)) != (char *) NULL )
007905af 917 exstring=true;
9bd92830
FM
918 if(exstring) continue;
919 }
920
921 totregsl++;
922 if(debugm)
923 printf("BUF=%s\n",linebuf);
924
925 t=NULL;
926 if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown) {
927 getword_start(&gwarea,linebuf);
928 if (getword(data,sizeof(data),&gwarea,' ')<0) {
929 debuga(_("Maybe you have a broken time in your access.log file\n"));
930 exit(EXIT_FAILURE);
931 }
932 if((str=(char *) strchr(data, '.')) != (char *) NULL && (str=(char *) strchr(str+1, '.')) != (char *) NULL ) {
933 strcpy(ip,data);
934 strcpy(elap,"0");
935 if(squid24) {
936 if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) {
937 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
938 exit(EXIT_FAILURE);
939 }
940 } else {
941 if (getword_skip(255,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) {
942 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
943 exit(EXIT_FAILURE);
944 }
945 }
946 if (getword(data,sizeof(data),&gwarea,']')<0 || getword_skip(MAXLEN,&gwarea,'"')<0 ||
007905af 947 getword(fun,sizeof(fun),&gwarea,' ')<0) {
9bd92830
FM
948 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
949 exit(EXIT_FAILURE);
950 }
22715352 951 if (getword_ptr(linebuf,&full_url,&gwarea,' ')<0) {
9bd92830
FM
952 debuga(_("Maybe you have a broken url in your %s file\n"),arq);
953 exit(EXIT_FAILURE);
954 }
955 if (getword_skip(MAXLEN,&gwarea,' ')<0) {
956 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
957 exit(EXIT_FAILURE);
958 }
959 if (getword(code2,sizeof(code2),&gwarea,' ')<0) {
960 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
961 exit(EXIT_FAILURE);
962 }
963 if (getword(tam,sizeof(tam),&gwarea,' ')<0) {
964 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
965 exit(EXIT_FAILURE);
966 }
967 if((str=(char *) strchr(gwarea.current, ' ')) != (char *) NULL ) {
968 if (getword(code,sizeof(code),&gwarea,' ')<0) {
969 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
970 exit(EXIT_FAILURE);
971 }
972 } else {
973 if (getword(code,sizeof(code),&gwarea,'\0')<0) {
974 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
975 exit(EXIT_FAILURE);
976 }
977 }
978
979 if ((str = strchr(code, ':')) != NULL)
980 *str = '/';
981
982 if(strcmp(tam,"\0") == 0)
983 strcpy(tam,"0");
984
985 ilf=ILF_Common;
986 ilf_count[ilf]++;
987
988 getword_start(&gwarea,data+1);
989 if (getword_multisep(data,sizeof(data),&gwarea,':')<0){
990 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
991 exit(EXIT_FAILURE);
992 }
993 if (getword_multisep(hora,sizeof(hora),&gwarea,' ')<0){
994 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
995 exit(EXIT_FAILURE);
996 }
997 getword_start(&gwarea,data);
998 if (getword_atoll(&iday,&gwarea,'/')<0){
999 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1000 exit(EXIT_FAILURE);
1001 }
1002 if (getword(mes,sizeof(mes),&gwarea,'/')<0){
1003 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1004 exit(EXIT_FAILURE);
1005 }
1006 if (getword_atoll(&iyear,&gwarea,'/')<0){
1007 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1008 exit(EXIT_FAILURE);
1009 }
1010
1011 imonth=month2num(mes)+1;
1012 idata=builddia(iday,imonth,iyear);
1013 computedate(iyear,imonth,iday,&tt);
eaa81082
FM
1014 if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
1015 tt.tm_min<0 || tt.tm_min>=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
1016 debuga(_("Invalid time found in %s\n"),arq);
1017 exit(EXIT_FAILURE);
1018 }
9bd92830
FM
1019 t=&tt;
1020 }
1021
1022 if(ilf==ILF_Unknown || ilf==ILF_Squid) {
1023 if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
1024 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),arq);
1025 exit(EXIT_FAILURE);
1026 }
1027 while(strcmp(elap,"") == 0 && gwarea.current[0] != '\0')
1028 if (getword(elap,sizeof(elap),&gwarea,' ')<0) {
1029 debuga(_("Maybe you have a broken elapsed time in your %s file\n"),arq);
1030 exit(EXIT_FAILURE);
1031 }
1032 if(strlen(elap) < 1) continue;
1033 if (getword(ip,sizeof(ip),&gwarea,' ')<0){
1034 debuga(_("Maybe you have a broken client IP address in your %s file\n"),arq);
1035 exit(EXIT_FAILURE);
1036 }
1037 if (getword(code,sizeof(code),&gwarea,' ')<0){
1038 debuga(_("Maybe you have a broken result code in your %s file\n"),arq);
1039 exit(EXIT_FAILURE);
1040 }
1041 if (getword(tam,sizeof(tam),&gwarea,' ')<0){
1042 debuga(_("Maybe you have a broken amount of data in your %s file\n"),arq);
1043 exit(EXIT_FAILURE);
1044 }
1045 if (getword(fun,sizeof(fun),&gwarea,' ')<0){
1046 debuga(_("Maybe you have a broken request method in your %s file\n"),arq);
1047 exit(EXIT_FAILURE);
1048 }
22715352 1049 if (getword_ptr(linebuf,&full_url,&gwarea,' ')<0){
9bd92830
FM
1050 debuga(_("Maybe you have a broken url in your %s file\n"),arq);
1051 exit(EXIT_FAILURE);
1052 }
1053 if (getword(user,sizeof(user),&gwarea,' ')<0){
1054 debuga(_("Maybe you have a broken user ID in your %s file\n"),arq);
1055 exit(EXIT_FAILURE);
1056 }
1057 ilf=ILF_Squid;
1058 ilf_count[ilf]++;
1059
1060 tnum=atoi(data);
1061 t=localtime(&tnum);
1062 if (t == NULL) {
1063 debuga(_("Cannot convert the timestamp from the squid log file\n"));
1064 exit(EXIT_FAILURE);
1065 }
1066
1067 strftime(tbuf2, sizeof(tbuf2), "%H%M", t);
1068
1069 idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday;
1070 }
1071 }
1072 if (ilf==ILF_Sarg) {
1073 getword_start(&gwarea,linebuf);
1074 if (getword(data,sizeof(data),&gwarea,'\t')<0){
1075 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1076 exit(EXIT_FAILURE);
1077 }
1078 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
1079 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1080 exit(EXIT_FAILURE);
1081 }
1082 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
1083 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1084 exit(EXIT_FAILURE);
1085 }
1086 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
1087 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1088 exit(EXIT_FAILURE);
1089 }
22715352 1090 if (getword_ptr(linebuf,&full_url,&gwarea,'\t')<0){
9bd92830
FM
1091 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1092 exit(EXIT_FAILURE);
1093 }
1094 if (getword(tam,sizeof(tam),&gwarea,'\t')<0){
1095 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1096 exit(EXIT_FAILURE);
1097 }
1098 if (getword(code,sizeof(code),&gwarea,'\t')<0){
1099 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1100 exit(EXIT_FAILURE);
1101 }
1102 if (getword(elap,sizeof(elap),&gwarea,'\t')<0){
1103 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1104 exit(EXIT_FAILURE);
1105 }
1106 if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){
1107 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1108 exit(EXIT_FAILURE);
1109 }
1110 getword_start(&gwarea,data);
1111 if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1 || iday>31){
1112 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1113 exit(EXIT_FAILURE);
1114 }
1115 if (getword_atoll(&imonth,&gwarea,'/')<0 || imonth<1 || imonth>12){
1116 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1117 exit(EXIT_FAILURE);
1118 }
1119 if (getword_atoll(&iyear,&gwarea,'\0')<0){
1120 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1121 exit(EXIT_FAILURE);
1122 }
1123 idata=builddia(iday,imonth,iyear);
1124 computedate(iyear,imonth,iday,&tt);
3be2705a
FM
1125 if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
1126 tt.tm_min<0 || tt.tm_min>=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
1127 debuga(_("Invalid time found in %s\n"),arq);
1128 exit(EXIT_FAILURE);
1129 }
9bd92830
FM
1130 t=&tt;
1131 }
1132 if (ilf==ILF_Isa) {
1133 if (linebuf[0] == '#') {
1134 int ncols,cols[ISACOL_Last];
1135
1136 fixendofline(linebuf);
1137 getword_start(&gwarea,linebuf);
1138 // remove the #Fields: column at the beginning of the line
1139 if (getword_skip(1000,&gwarea,' ')<0){
1140 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1141 exit(EXIT_FAILURE);
1142 }
1143 for (ncols=0 ; ncols<ISACOL_Last ; ncols++) cols[ncols]=-1;
1144 ncols=0;
1145 while(gwarea.current[0] != '\0') {
1146 if (getword(val1,sizeof(val1),&gwarea,'\t')<0){
1147 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1148 exit(EXIT_FAILURE);
1149 }
1150 if(strcmp(val1,"c-ip") == 0) cols[ISACOL_Ip]=ncols;
1151 if(strcmp(val1,"cs-username") == 0) cols[ISACOL_UserName]=ncols;
1152 if(strcmp(val1,"date") == 0) cols[ISACOL_Date]=ncols;
1153 if(strcmp(val1,"time") == 0) cols[ISACOL_Time]=ncols;
1154 if(strcmp(val1,"time-taken") == 0) cols[ISACOL_TimeTaken]=ncols;
1155 if(strcmp(val1,"sc-bytes") == 0) cols[ISACOL_Bytes]=ncols;
1156 if(strcmp(val1,"cs-uri") == 0) cols[ISACOL_Uri]=ncols;
1157 if(strcmp(val1,"sc-status") == 0) cols[ISACOL_Status]=ncols;
1158 ncols++;
1159 }
1160 if (cols[ISACOL_Ip]>=0) {
1161 isa_ncols=ncols;
1162 for (ncols=0 ; ncols<ISACOL_Last ; ncols++)
1163 isa_cols[ncols]=cols[ncols];
1164 }
1165 continue;
1166 }
1167 if (!isa_ncols) continue;
1168 getword_start(&gwarea,linebuf);
1169 for (x=0 ; x<isa_ncols ; x++) {
1170 if (getword_ptr(linebuf,&str,&gwarea,'\t')<0) {
1171 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
1172 exit(EXIT_FAILURE);
1173 }
1174 if (x==isa_cols[ISACOL_Ip]) {
1175 if (strlen(str)>=sizeof(ip)) {
1176 debuga(_("Maybe you have a broken IP in your %s file\n"),arq);
1177 exit(EXIT_FAILURE);
1178 }
1179 strcpy(ip,str);
1180 } else if (x==isa_cols[ISACOL_UserName]) {
1181 if (strlen(str)>=sizeof(user)) {
1182 debuga(_("Maybe you have a broken user ID in your %s file\n"),arq);
1183 exit(EXIT_FAILURE);
1184 }
1185 strcpy(user,str);
1186 } else if (x==isa_cols[ISACOL_Date]) {
1187 if (strlen(str)>=sizeof(data)) {
1188 debuga(_("Maybe you have a broken date in your %s file\n"),arq);
1189 exit(EXIT_FAILURE);
1190 }
1191 strcpy(data,str);
1192 } else if (x==isa_cols[ISACOL_Time]) {
1193 if (strlen(str)>=sizeof(hora)) {
1194 debuga(_("Maybe you have a broken time in your %s file\n"),arq);
1195 exit(EXIT_FAILURE);
1196 }
1197 strcpy(hora,str);
1198 } else if (x==isa_cols[ISACOL_TimeTaken]) {
1199 if (strlen(str)>=sizeof(elap)) {
1200 debuga(_("Maybe you have a broken download duration in your %s file\n"),arq);
1201 exit(EXIT_FAILURE);
1202 }
1203 strcpy(elap,str);
1204 } else if (x==isa_cols[ISACOL_Bytes]) {
1205 if (strlen(str)>=sizeof(tam)) {
1206 debuga(_("Maybe you have a broken download size in your %s file\n"),arq);
1207 exit(EXIT_FAILURE);
1208 }
1209 strcpy(tam,str);
1210 } else if (x==isa_cols[ISACOL_Uri]) {
22715352 1211 full_url=str;
9bd92830
FM
1212 } else if (x==isa_cols[ISACOL_Status]) {
1213 if (strlen(str)>=sizeof(code)) {
1214 debuga(_("Maybe you have a broken access code in your %s file\n"),arq);
1215 exit(EXIT_FAILURE);
1216 }
1217 strcpy(code,str);
1218 }
1219 }
1220
1221 if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) {
1222 sprintf(val1,"DENIED/%s",code);
1223 strcpy(code,val1);
1224 }
1225 getword_start(&gwarea,data);
1226 if (getword_atoll(&iyear,&gwarea,'-')<0){
1227 debuga(_("Maybe you have a broken year in your %s file\n"),arq);
1228 exit(EXIT_FAILURE);
1229 }
1230 if (getword_atoll(&imonth,&gwarea,'-')<0){
1231 debuga(_("Maybe you have a broken month in your %s file\n"),arq);
1232 exit(EXIT_FAILURE);
1233 }
1234 if (getword_atoll(&iday,&gwarea,'\0')<0){
1235 debuga(_("Maybe you have a broken day in your %s file\n"),arq);
1236 exit(EXIT_FAILURE);
1237 }
1238
1239 idata=builddia(iday,imonth,iyear);
1240 computedate(iyear,imonth,iday,&tt);
3be2705a
FM
1241 if (isa_cols[ISACOL_Time]>=0) {
1242 if (sscanf(hora,"%d:%d:%d",&tt.tm_hour,&tt.tm_min,&tt.tm_sec)!=3 || tt.tm_hour<0 || tt.tm_hour>=24 ||
1243 tt.tm_min<0 || tt.tm_min>=60 || tt.tm_sec<0 || tt.tm_sec>=60) {
1244 debuga(_("Invalid time found in %s\n"),arq);
1245 exit(EXIT_FAILURE);
1246 }
1247 }
9bd92830
FM
1248 t=&tt;
1249 }
1250 if (t==NULL) {
1251 debuga(_("Unknown input log file format\n"));
1252 break;
1253 }
1254
1255 strftime(dia, sizeof(dia), "%d/%m/%Y", t);
1256 snprintf(hora,sizeof(hora),"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
1257
1258 if(debugm)
1259 printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil);
1260
1261 if(date[0] != '\0'){
1262 if(idata < dfrom || idata > duntil) continue;
1263 }
1264
1265 // Record only hours usage which is required
1266 if (t) {
007905af 1267 if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len, sizeof( int ), compar ) == NULL )
9bd92830
FM
1268 continue;
1269
007905af 1270 if( bsearch( &( t -> tm_hour ), hours.list, hours.len, sizeof( int ), compar ) == NULL )
9bd92830
FM
1271 continue;
1272 }
1273
1274
1275 if(strlen(user) > MAX_USER_LEN) {
1276 if (debugm) printf(_("User ID too long: %s\n"),user);
1277 totregsx++;
1278 continue;
1279 }
1280
1281 // include_users
1282 if(IncludeUsers[0] != '\0') {
8d811f76 1283 snprintf(val1,sizeof(val1),":%s:",user);
9bd92830
FM
1284 if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL )
1285 continue;
1286 }
1287
1288 if(vercode(code)) {
1289 if (debugm) printf(_("Excluded code: %s\n"),code);
1290 totregsx++;
1291 continue;
1292 }
1293
1294 if(testvaliduserchar(user))
1295 continue;
809a7534
FM
1296
1297#if 0
9bd92830
FM
1298 if((str = strstr(user,"%20")) != NULL) {
1299 /*
1300 This is a patch introduced to solve bug #1624251 reported at sourceforge but
1301 the side effect is to truncate the name at the first space and merge the reports
1302 of people whose name is identical up to the first space.
1303
1304 The old code used to truncate the user name at the first % if a %20 was
1305 found anywhere in the string. That means the string could be truncated
1306 at the wrong place if another % occured before the %20. This new code should
1307 avoid that problem and only truncate at the space. There is no bug
1308 report indicating that anybody noticed this.
1309 */
1310 *str='\0';
1311 }
1312
1313 /*
1314 Code prior to 2.2.7 used to replace any %xx by a dot as long as a %5c was
1315 found in the user name.
1316 */
1317 while((str = strstr(user,"%5c")) != NULL) {
1318 *str='.';
1319 for (x=3 ; str[x] ; x++) str[x-2]=str[x];
1320 }
809a7534 1321#endif
f27d281d
FM
1322 // replace any tab by a single space
1323 for (str=full_url ; *str ; str++)
1324 if (*str=='\t') *str=' ';
1325 for (str=code ; *str ; str++)
1326 if (*str=='\t') *str=' ';
809a7534 1327
22715352 1328 urly=full_url;
9bd92830
FM
1329
1330 if(ilf!=ILF_Sarg) {
1331 /*
1332 The full URL is not saved in sarg log. There is no point in testing the URL to detect
1333 a downloaded file.
1334 */
22715352 1335 download_flag=is_download_suffix(full_url);
9bd92830 1336 if (download_flag) {
22715352 1337 download_url=full_url;
9bd92830
FM
1338 download_count++;
1339 }
1340 } else
1341 download_flag=false;
1342
1a2609b0 1343 url=process_url(full_url,LongUrl);
22715352 1344 if (!url || url[0] == '\0') continue;
9bd92830
FM
1345
1346 if(addr[0] != '\0'){
1347 if(strcmp(addr,ip)!=0) continue;
1348 }
1349 if(fhost) {
1350 if(!vhexclude(url)) {
1351 if (debugm) printf(_("Excluded site: %s\n"),url);
1352 totregsx++;
1353 continue;
1354 }
1355 }
1356
1357 if(hm >= 0 && hmf >= 0) {
1358 hmr=t->tm_hour*100+t->tm_min;
1359 if(hmr < hm || hmr > hmf) continue;
1360 }
1361
1362 if(site[0] != '\0'){
1363 if(strstr(url,site)==0) continue;
1364 }
1365
1366 if(UserIp) {
1367 strcpy(user,ip);
1368 id_is_ip=true;
1369 } else {
1370 id_is_ip=false;
1371 if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) {
1372 if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
1373 strcpy(user,ip);
1374 id_is_ip=true;
1375 }
1376 if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
1377 continue;
1378 if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
1379 strcpy(user,"everybody");
1380 } else {
1381 strlow(user);
1382 if(NtlmUserFormat == NTLMUSERFORMAT_USER) {
1383 if((str = strchr(user,'_')) != 0) {
1384 strcpy(warea,str+1);
1385 strcpy(user,warea);
1386 }
1387 if((str = strchr(user,'+')) != 0) {
1388 strcpy(warea,str+1);
1389 strcpy(user,warea);
1390 }
1391 }
1392 }
1393 }
1394
1395 if(us[0] != '\0'){
1396 if(strcmp(user,us)!=0) continue;
1397 }
1398
1399 if(puser) {
8d811f76 1400 snprintf(wuser,sizeof(wuser),":%s:",user);
9bd92830
FM
1401 if(strstr(userfile, wuser) == 0)
1402 continue;
1403 }
1404
1405 if(fuser) {
1406 if(!vuexclude(user)) {
1407 if (debugm) printf(_("Excluded user: %s\n"),user);
1408 totregsx++;
1409 continue;
1410 }
1411 }
1412
1413 if(strcmp(user,"-") ==0 || strcmp(user," ") ==0 || strcmp(user,"") ==0 || strcmp(user,":") ==0)
1414 continue;
1415
d91457d2
FM
1416 nbytes=atol(tam);
1417 if (nbytes<0) nbytes=0;
1418
1419 elap_time=atol(elap);
1420 if (elap_time<0) elap_time=0;
9bd92830 1421 if(max_elapsed) {
d91457d2
FM
1422 if(elap_time>max_elapsed) {
1423 elap_time=0;
9bd92830
FM
1424 }
1425 }
1426
1427 if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
1428 fixendofline(str);
8d811f76
FM
1429 snprintf(smartfilter,sizeof(smartfilter),"\"%s\"",str+1);
1430 } else strcpy(smartfilter,"\"\"");
9bd92830
FM
1431
1432 nopen=0;
1433 prev_ufile=NULL;
1434 for (ufile=first_user_file ; ufile && strcmp(user,ufile->user->id)!=0 ; ufile=ufile->next) {
1435 prev_ufile=ufile;
1436 if (ufile->file) nopen++;
1437 }
1438 if (!ufile) {
1439 ufile=malloc(sizeof(*ufile));
1440 if (!ufile) {
1441 debuga(_("Not enough memory to store the user %s\n"),user);
1442 exit(EXIT_FAILURE);
1443 }
1444 memset(ufile,0,sizeof(*ufile));
1445 ufile->next=first_user_file;
1446 first_user_file=ufile;
1447 uinfo=userinfo_create(user);
1448 ufile->user=uinfo;
1449 uinfo->id_is_ip=id_is_ip;
1450 } else {
1451 if (prev_ufile) {
1452 prev_ufile->next=ufile->next;
1453 ufile->next=first_user_file;
1454 first_user_file=ufile;
1455 }
1456 }
d91457d2
FM
1457#ifdef ENABLE_DOUBLE_CHECK_DATA
1458 ufile->user->nbytes+=nbytes;
1459 ufile->user->elap+=elap_time;
1460#endif
9bd92830
FM
1461
1462 if (ufile->file==NULL) {
1463 if (nopen>=maxopenfiles) {
1464 x=0;
1465 for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) {
1466 if (ufile1->file!=NULL) {
1467 if (x>=maxopenfiles) {
1468 if (fclose(ufile1->file)==EOF) {
1469 debuga(_("Failed to close the log file of user %s - %s\n"),ufile1->user->id,strerror(errno));
1470 exit(EXIT_FAILURE);
1471 }
1472 ufile1->file=NULL;
1473 }
1474 x++;
1475 }
1476 }
1477 }
1478 if (snprintf (tmp3, sizeof(tmp3), "%s/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) {
1479 debuga(_("Temporary user file name too long: %s/%s.unsort\n"), tmp, ufile->user->filename);
1480 exit(EXIT_FAILURE);
1481 }
1482 if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) {
1483 debuga(_("(log) Cannot open temporary file: %s - %s\n"), tmp3, strerror(errno));
1484 exit (1);
1485 }
1486 }
1487
1488 /*if ( strcmp ( user , sz_Last_User ) != 0 ) {
1489 if ( fp_Write_User )
1490 fclose( fp_Write_User ) ;
1491 sprintf (tmp3, "%s/%s.unsort", tmp, user);
1492
1493 if ((fp_Write_User = MY_FOPEN (tmp3, "a")) == NULL) {
1494 fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], _("Cannot open temporary file"), tmp3, strerror(errno));
1495 exit (1);
1496 }
1497 strcpy( sz_Last_User , user ) ;
1498 }*/
d91457d2 1499 if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%ld\t%s\t%ld\t%s\n",dia,hora,ip,url,nbytes,code,elap_time,smartfilter)<=0) {
9bd92830
FM
1500 debuga(_("Write error in the log file of user %s\n"),user);
1501 exit(EXIT_FAILURE);
1502 }
1503
1504 if(fp_log && ilf!=ILF_Sarg)
d91457d2 1505 fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%ld\t%s\t%ld\t%s\n",dia,hora,user,ip,url,nbytes,code,elap_time,smartfilter);
9bd92830
FM
1506
1507 totregsg++;
1508
1509 if(!dataonly && download_flag && download_url && strstr(code,"DENIED") == 0) {
1510 ndownload = 1;
1511
1512 if ( ! fp_Download_Unsort ) {
1513 if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) {
1514 debuga(_("(log) Cannot open temporary file: %s - %s\n"),sz_Download_Unsort, strerror(errno));
1515 exit (1);
1516 }
1517 }
1518 fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url);
1519 }
1520
1bd73f70 1521 if((ReportType & REPORT_TYPE_DENIED) != 0) {
9bd92830
FM
1522 if(fp_denied && strstr(code,"DENIED/403") != 0) {
1523 fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly);
1524 denied_count++;
1525 }
1bd73f70
FM
1526 }
1527 if((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) {
9bd92830
FM
1528 if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) {
1529 fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly);
1530 authfail_count++;
1531 }
1532 }
1533
1534 if (ilf!=ILF_Sarg) {
1535 if(!totper || idata<mindate){
1536 mindate=idata;
1537 memcpy(&period.start,t,sizeof(*t));
1538 strcpy(start_hour,tbuf2);
1539 }
1540 if (!totper || idata>maxdate) {
1541 maxdate=idata;
1542 memcpy(&period.end,t,sizeof(*t));
1543 }
1544 totper=true;
1545 }
1546
1547 if(debugm){
1548 printf("IP=\t%s\n",ip);
1549 printf("USER=\t%s\n",user);
d91457d2 1550 printf("ELAP=\t%ld\n",elap_time);
9bd92830
FM
1551 printf("DATE=\t%s\n",dia);
1552 printf("TIME=\t%s\n",hora);
1553 printf("FUNC=\t%s\n",fun);
1554 printf("URL=\t%s\n",url);
1555 printf("CODE=\t%s\n",code);
d91457d2 1556 printf("LEN=\t%ld\n",nbytes);
9bd92830
FM
1557 }
1558 }
1559 if (!from_stdin) {
1560 if (from_pipe)
1561 pclose(fp_in);
1562 else {
1563 fclose(fp_in);
1564 if( ShowReadStatistics )
1565 printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 );
1566 }
1567 }
1568 }
1569
1570 if (debug)
1571 debuga(_(" Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx);
1572
1573 longline_destroy(&line);
1574 if ( fp_Download_Unsort )
007905af 1575 fclose (fp_Download_Unsort);
9bd92830
FM
1576
1577 for (ufile=first_user_file ; ufile ; ufile=ufile1) {
1578 ufile1=ufile->next;
1579 if (ufile->file!=NULL) fclose(ufile->file);
1580 free(ufile);
1581 }
1582
1583 free_download();
1584 free_excludecodes();
1585 free_exclude();
1586
1587 if(debug) {
1588 int totalcount=0;
1589
1590 for (ilf=0 ; ilf<ILF_Last ; ilf++) totalcount+=ilf_count[ilf];
1591
1592 if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]>0)
1593 debuga(_("Log with mixed records format (squid and common log)\n"));
1594
1595 if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0)
1596 debuga(_("Common log format\n"));
1597
1598 if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0)
1599 debuga(_("Squid log format\n"));
1600
1601 if(ilf_count[ILF_Sarg]>0)
1602 debuga(_("Sarg log format\n"));
1603
1604 if(totalcount==0 && totregsg)
1605 debuga(_("Log with invalid format\n"));
1606 }
1607
1608 if(!totregsg){
1609 debuga(_("No records found\n"));
1610 debuga(_("End\n"));
1611 if(fp_denied) fclose(fp_denied);
1612 if(fp_authfail) fclose(fp_authfail);
1613 userinfo_free();
1614 if(userfile) free(userfile);
1615 close_usertab();
1616 exit(EXIT_SUCCESS);
1617 }
1618
1619 if (date[0]!='\0') {
1620 char date0[30], date1[30];
1621
1622 strftime(date0,sizeof(date0),"%d/%m/%Y",&period.start);
1623 strftime(date1,sizeof(date1),"%d/%m/%Y",&period.end);
1624 debuga(_("Period covered by log files: %s-%s\n"),date0,date1);
1625 getperiod_fromrange(&period,dfrom,duntil);
1626 }
1627 if (getperiod_buildtext(&period)<0) {
1628 debuga(_("Failed to build the string representation of the date range\n"));
1629 exit(EXIT_FAILURE);
1630 }
1631
1632 if(debugz){
9f93fec3
FM
1633 debugaz(_("date=%s\n"),dia);
1634 debugaz(_("period=%s\n"),period.text);
9bd92830
FM
1635 }
1636
1637 if(debug)
1638 debuga(_("Period: %s\n"),period.text);
25697a35 1639
9bd92830
FM
1640 if(fp_denied)
1641 fclose(fp_denied);
1642 if(fp_authfail)
1643 fclose(fp_authfail);
1644
1645 if(fp_log != NULL) {
8d811f76
FM
1646 char end_hour[128];
1647 char val2[40];
1648 char val4[255];//val4 must not be bigger than arq_log without fixing the strcpy below
1649
9bd92830 1650 fclose(fp_log);
a87d4d11 1651 safe_strcpy(end_hour,tbuf2,sizeof(end_hour));
9bd92830
FM
1652 strftime(val2,sizeof(val2),"%d%m%Y",&period.start);
1653 strftime(val1,sizeof(val1),"%d%m%Y",&period.end);
8d811f76 1654 if (snprintf(val4,sizeof(val4),"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour)>=sizeof(val4)) {
c4633554 1655 debuga(_("File name too long: %s/sarg-%s_%s-%s_%s.log\n"),ParsedOutputLog,val2,start_hour,val1,end_hour);
8d811f76
FM
1656 exit(EXIT_FAILURE);
1657 }
9bd92830
FM
1658 if (rename(arq_log,val4)) {
1659 debuga(_("failed to rename %s to %s - %s\n"),arq_log,val4,strerror(errno));
1660 } else {
1661 strcpy(arq_log,val4);
1662
1663 if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') {
1664 /*
1665 No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are
1666 necessary around the command name, put them in the configuration file.
1667 */
8d811f76 1668 if (snprintf(val1,sizeof(val1),"%s \"%s\"",ParsedOutputLogCompress,arq_log)>=sizeof(val1)) {
c4633554 1669 debuga(_("Command too long: %s \"%s\"\n"),ParsedOutputLogCompress,arq_log);
8d811f76
FM
1670 exit(EXIT_FAILURE);
1671 }
9bd92830
FM
1672 cstatus=system(val1);
1673 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
1674 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
1675 debuga(_("command: %s\n"),val1);
1676 exit(EXIT_FAILURE);
1677 }
1678 }
1679 }
1680 if(debug)
1681 debuga(_("Sarg parsed log saved as %s\n"),arq_log);
1682 }
1683
1684 if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) {
78eeb33f
FM
1685 if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort)>=sizeof(csort)) {
1686 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),denied_unsort,denied_sort);
1687 exit(EXIT_FAILURE);
1688 }
9bd92830
FM
1689 cstatus=system(csort);
1690 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
1691 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
1692 debuga(_("sort command: %s\n"),csort);
1693 exit(EXIT_FAILURE);
1694 }
08f9b029
FM
1695 if (unlink(denied_unsort)) {
1696 debuga(_("Cannot delete %s - %s\n"),denied_unsort,strerror(errno));
1697 exit(EXIT_FAILURE);
1698 }
9bd92830
FM
1699 }
1700
1701 sort_users_log(tmp, debug);
1702
1703 if(DataFile[0] != '\0')
1704 data_file(tmp);
1705 else
1706 gerarel();
1707
08f9b029 1708 if((ReportType & REPORT_TYPE_DENIED) != 0) {
9bd92830 1709 unlink(denied_sort);
08f9b029 1710 }
9bd92830
FM
1711
1712 if(strcmp(tmp,"/tmp") != 0) {
1713 unlinkdir(tmp,0);
1714 }
1715
0a6722a1 1716 free_hostalias();
9bd92830
FM
1717 userinfo_free();
1718 if(userfile)
1719 free(userfile);
1720 close_usertab();
1721
1722 if(debug)
1723 debuga(_("End\n"));
1724
1725 exit(EXIT_SUCCESS);
25697a35
GS
1726}
1727
1728
936c9905 1729static void getusers(const char *pwdfile, int debug)
25697a35 1730{
9bd92830
FM
1731 FILE *fp_usr;
1732 char buf[255];
1733 char *str;
1734 long int nreg=0;
1735
1736 if(debug)
1737 debuga(_("Loading password file from %s\n"),pwdfile);
1738
1739 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
1740 debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno));
1741 exit(EXIT_FAILURE);
1742 }
1743
1744 if (fseek(fp_usr, 0, SEEK_END)==-1) {
1745 debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno));
1746 exit(EXIT_FAILURE);
1747 }
1748 nreg = ftell(fp_usr);
1749 if (nreg<0) {
1750 debuga(_("Cannot get the size of file %s\n"),pwdfile);
1751 exit(EXIT_FAILURE);
1752 }
1753 nreg = nreg+5000;
1754 if (fseek(fp_usr, 0, SEEK_SET)==-1) {
1755 debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno));
1756 exit(EXIT_FAILURE);
1757 }
1758
1759 if((userfile=(char *) malloc(nreg))==NULL){
1760 debuga(_("malloc error (%ld)\n"),nreg);
1761 exit(EXIT_FAILURE);
1762 }
1763
1764 bzero(userfile,nreg);
1765 strcpy(userfile,":");
1766
1767 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
1768 str=strchr(buf,':');
1769 if (!str) {
1770 debuga(_("You have an invalid user in your %s file\n"),pwdfile);
1771 exit(EXIT_FAILURE);
1772 }
1773 str[1]='\0';
1774 strcat(userfile,buf);
1775 }
1776
1777 fclose(fp_usr);
1778
1779 return;
25697a35 1780}