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