]> git.ipfire.org Git - thirdparty/sarg.git/blob - log.c
Use a global constant instead of the "index.html" file name in the code
[thirdparty/sarg.git] / log.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2013
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29 #include "include/readlog.h"
30 #include "include/filelist.h"
31
32 #ifdef HAVE_GETOPT_H
33 #include <getopt.h>
34 #endif
35
36 //! The log file filtering.
37 struct ReadLogDataStruct ReadFilter;
38
39 //! The list of the system users.
40 /*@null@*/char *userfile=NULL;
41
42 //! List of the input log files to process.
43 FileListObject AccessLog=NULL;
44
45
46 static void getusers(const char *pwdfile, int debug);
47
48 int main(int argc,char *argv[])
49 {
50 extern int optind;
51 extern int optopt;
52 extern char *optarg;
53
54 char hm_str[15];
55 char uagent[MAXLEN];
56 char hexclude[MAXLEN];
57 char splitprefix[MAXLEN];
58 int ch;
59 int errflg=0;
60 bool dns=false;
61 int iarq=0;
62 int lastlog=-1;
63 int LogStatus;
64 bool realt;
65 bool userip;
66 time_t start_time;
67 time_t end_time;
68 time_t read_start_time;
69 time_t read_end_time;
70 time_t process_start_time;
71 time_t process_end_time;
72 double read_elapsed;
73 double process_elapsed;
74 FileListIterator FIter;
75 static int split=0;
76 static int convert=0;
77 static int output_css=0;
78 static int show_statis=0;
79 int option_index;
80 static struct option long_options[]=
81 {
82 {"convert",no_argument,&convert,1},
83 {"css",no_argument,&output_css,1},
84 {"help",no_argument,NULL,'h'},
85 {"lastlog",required_argument,NULL,2},
86 {"keeplogs",no_argument,NULL,3},
87 {"split",no_argument,&split,1},
88 {"splitprefix",required_argument,NULL,'P'},
89 {"statistics",no_argument,&show_statis,1},
90 {0,0,0,0}
91 };
92
93 start_time=time(NULL);
94
95 #ifdef HAVE_LOCALE_H
96 setlocale(LC_TIME,"");
97 #endif
98
99 #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
100 if (!setlocale (LC_ALL, "")) {
101 fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n");
102 exit(EXIT_FAILURE);
103 }
104 if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) {
105 fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno));
106 exit(EXIT_FAILURE);
107 }
108 if (!textdomain (PACKAGE_NAME)) {
109 fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno));
110 exit(EXIT_FAILURE);
111 }
112 #endif //ENABLE_NLS
113
114 BgImage[0]='\0';
115 LogoImage[0]='\0';
116 LogoText[0]='\0';
117 PasswdFile[0]='\0';
118 OutputEmail[0]='\0';
119 UserAgentLog[0]='\0';
120 ExcludeHosts[0]='\0';
121 ExcludeUsers[0]='\0';
122 ConfigFile[0]='\0';
123 code[0]='\0';
124 LastLog=0;
125 ReportType=0UL;
126 UserTabFile[0]='\0';
127 BlockIt[0]='\0';
128 ExternalCSSFile[0]='\0';
129 RedirectorLogFormat[0]='\0';
130 NRedirectorLogs=0;
131
132 snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR);
133 strcpy(GraphDaysBytesBarColor,"orange");
134 strcpy(BgColor,"#ffffff");
135 strcpy(TxColor,"#000000");
136 strcpy(TxBgColor,"lavender");
137 strcpy(TiColor,"darkblue");
138 strcpy(Width,"80");
139 strcpy(Height,"45");
140 strcpy(LogoTextColor,"#000000");
141 strcpy(HeaderColor,"darkblue");
142 strcpy(HeaderBgColor,"#dddddd");
143 strcpy(LogoTextColor,"#006699");
144 strcpy(FontSize,"9px");
145 strcpy(TempDir,"/tmp");
146 strcpy(OutputDir,"/var/www/html/squid-reports");
147 AnonymousOutputFiles=false;
148 Ip2Name=false;
149 DateFormat='u';
150 OverwriteReport=false;
151 RemoveTempFiles=true;
152 strcpy(ReplaceIndex,INDEX_HTML_FILE);
153 Index=INDEX_YES;
154 RecordsWithoutUser=RECORDWITHOUTUSER_IP;
155 UseComma=0;
156 strcpy(MailUtility,"mailx");
157 TopSitesNum=100;
158 TopUsersNum=0;
159 UserIp=0;
160 TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE;
161 UserSort=USER_SORT_BYTES | USER_SORT_REVERSE;
162 TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE;
163 LongUrl=0;
164 strcpy(FontFace,"Verdana,Tahoma,Arial");
165 datetimeby=DATETIME_BYTE;
166 strcpy(CharSet,"ISO-8859-1");
167 Privacy=0;
168 strcpy(PrivacyString,"***.***.***.***");
169 strcpy(PrivacyStringColor,"blue");
170 SuccessfulMsg=true;
171 TopUserFields=TOPUSERFIELDS_NUM | TOPUSERFIELDS_DATE_TIME | TOPUSERFIELDS_USERID | TOPUSERFIELDS_CONNECT |
172 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
173 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
174 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
175 UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
176 USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
177 USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
178 strcpy(DataFileDelimiter,";");
179 DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
180 DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
181 ShowReadStatistics=true;
182 ShowReadPercent=false;
183 strcpy(IndexSortOrder,"D");
184 ShowSargInfo=true;
185 ShowSargLogo=true;
186 ParsedOutputLog[0]='\0';
187 strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
188 DisplayedValues=DISPLAY_ABBREV;
189 strcpy(HeaderFontSize,"9px");
190 strcpy(TitleFontSize,"11px");
191 strcpy(AuthUserTemplateFile,"sarg_htaccess");
192 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");
193 Graphs=true;
194 #if defined(FONTDIR)
195 strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
196 #else
197 GraphFont[0]='\0';
198 #endif
199 strcpy(Ulimit,"20000");
200 NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
201 IndexTree=INDEX_TREE_FILE;
202 IndexFields=INDEXFIELDS_DIRSIZE;
203 strcpy(RealtimeTypes,"GET,PUT,CONNECT");
204 RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
205 RedirectorFilterOutDate=true;
206 DansguardianFilterOutDate=true;
207 DataFileUrl=DATAFILEURL_IP;
208 strcpy(MaxElapsed,"28800000");
209 BytesInSitesUsersReport=0;
210 UserAuthentication=0;
211 strcpy(LDAPHost,"127.0.0.1");
212 LDAPPort=389;
213 LDAPProtocolVersion=3;
214 LDAPBindDN[0]='\0';
215 LDAPBindPW[0]='\0';
216 LDAPBaseSearch[0]='\0';
217 strcpy(LDAPFilterSearch, "(uid=%s)");
218 strcpy(LDAPTargetAttr, "cn");
219 LDAPNativeCharset[0]='\0';
220 SortTableJs[0]='\0';
221
222 tmp[0]='\0';
223 us[0]='\0';
224 ReadFilter.DateRange[0]='\0';
225 df='\0';
226 uagent[0]='\0';
227 hexclude[0]='\0';
228 addr[0]='\0';
229 ReadFilter.StartTime=-1;
230 ReadFilter.EndTime=-1;
231 site[0]='\0';
232 outdir[0]='\0';
233 splitprefix[0]='\0';
234 email[0]='\0';
235 UserInvalidChar[0]='\0';
236 DataFile[0]='\0';
237 SquidGuardConf[0]='\0';
238 DansGuardianConf[0]='\0';
239 hm_str[0]='\0';
240 HostAliasFile[0]='\0';
241 UserAliasFile[0]='\0';
242
243 dansguardian_count=0;
244 redirector_count=0;
245 useragent_count=0;
246 DeniedReportLimit=10;
247 AuthfailReportLimit=10;
248 DansGuardianReportLimit=10;
249 SquidGuardReportLimit=10;
250 DownloadReportLimit=50;
251 UserReportLimit=0;
252 debug=0;
253 debugz=0;
254 debugm=0;
255 iprel=false;
256 userip=false;
257 realt=false;
258 realtime_refresh=3;
259 realtime_access_log_lines=1000;
260 cost=0.01;
261 nocost=50000000;
262 squid24=false;
263 dfrom=0;
264 duntil=0;
265 KeepTempLog=false;
266 NumLogSuccessiveErrors=3;
267 NumLogTotalErrors=50;
268 lines_read=0UL;
269 records_kept=0UL;
270 nusers=0UL;
271
272 bzero(IncludeUsers, sizeof(IncludeUsers));
273 bzero(ExcludeString, sizeof(ExcludeString));
274 memset(&period,0,sizeof(period));
275
276 AccessLogFromCmdLine=0;
277 RedirectorLogFromCmdLine=0;
278
279 strcpy(Title,_("Squid User Access Report"));
280
281 while((ch = getopt_long(argc, argv, "a:b:c:d:e:f:g:hikl:L:mno:P:prs:t:u:vw:xyz",long_options,&option_index)) != -1){
282 switch(ch)
283 {
284 case 0:
285 break;
286 case 2:
287 lastlog=atoi(optarg);
288 break;
289 case 3:
290 lastlog=0;
291 break;
292 case 'a':
293 safe_strcpy(addr,optarg,sizeof(addr));
294 break;
295 case 'b': //unused option
296 safe_strcpy(uagent,optarg,sizeof(uagent));
297 break;
298 case 'c':
299 safe_strcpy(hexclude,optarg,sizeof(hexclude));
300 break;
301 case 'd':
302 safe_strcpy(ReadFilter.DateRange,optarg,sizeof(ReadFilter.DateRange));
303 date_from(ReadFilter.DateRange,sizeof(ReadFilter.DateRange), &dfrom, &duntil);
304 break;
305 case 'e':
306 safe_strcpy(email,optarg,sizeof(email));
307 break;
308 case 'f':
309 safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile));
310 break;
311 case 'g':
312 df=*optarg;
313 break;
314 case 'h':
315 usage(argv[0]);
316 exit(EXIT_SUCCESS);
317 case 'i':
318 iprel=true;
319 break;
320 case 'k':
321 KeepTempLog=true;
322 break;
323 case 'l':
324 if (!AccessLog)
325 AccessLog=FileList_Create();
326 if (!FileList_AddFile(AccessLog,optarg)) {
327 debuga(_("Not enough memory to store the input log file names\n"));
328 exit(EXIT_FAILURE);
329 }
330 AccessLogFromCmdLine++;
331 break;
332 case 'L':
333 if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
334 debuga(_("Too many redirector logs passed on command line with option -L.\n"));
335 exit(EXIT_FAILURE);
336 }
337 if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
338 debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg);
339 exit(EXIT_FAILURE);
340 }
341 strcpy(RedirectorLogs[NRedirectorLogs],optarg);
342 NRedirectorLogs++;
343 RedirectorLogFromCmdLine++;
344 break;
345 case 'm':
346 debugm++;
347 break;
348 case 'n':
349 dns=true;
350 break;
351 case 'o':
352 safe_strcpy(outdir,optarg,sizeof(outdir));
353 break;
354 case 'p':
355 userip=true;
356 break;
357 case 'P':
358 safe_strcpy(splitprefix,optarg,sizeof(splitprefix));
359 break;
360 case 'r':
361 realt=true;
362 break;
363 case 's':
364 safe_strcpy(site,optarg,sizeof(site));
365 break;
366 case 't':
367 {
368 int h1,m1,h2,m2;
369
370 if(strstr(optarg,"-") == 0) {
371 if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) {
372 debuga(_("Time period passed on the command line with option -t must be HH:MM\n"));
373 exit(EXIT_FAILURE);
374 }
375 ReadFilter.StartTime=h1*100+m1;
376 ReadFilter.EndTime=ReadFilter.StartTime;
377 snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1);
378 } else {
379 if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) {
380 debuga(_("Time range passed on the command line with option -t must be HH:MM-HH:MM\n"));
381 exit(EXIT_FAILURE);
382 }
383 ReadFilter.StartTime=h1*100+m1;
384 ReadFilter.EndTime=h2*100+m2;
385 snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2);
386 }
387 break;
388 }
389 case 'u':
390 safe_strcpy(us,optarg,sizeof(us));
391 break;
392 case 'v':
393 version();
394 break;
395 case 'w':
396 safe_strcpy(tmp,optarg,sizeof(tmp));
397 break;
398 case 'x':
399 debug++;
400 break;
401 case 'y': //unused option
402 langcode++;
403 break;
404 case 'z':
405 debugz++;
406 break;
407 case ':':
408 debuga(_("Option -%c requires an argument\n"),optopt);
409 exit(EXIT_FAILURE);
410 case '?':
411 usage(argv[0]);
412 exit(EXIT_FAILURE);
413 default:
414 abort();
415 }
416 }
417
418 if (errflg>0) {
419 usage(argv[0]);
420 exit(2);
421 }
422
423 if(output_css) {
424 css_content(stdout);
425 exit(EXIT_SUCCESS);
426 }
427
428 if (optind<argc) {
429 if (!AccessLog)
430 AccessLog=FileList_Create();
431 for (iarq=optind ; iarq<argc ; iarq++) {
432 if (!FileList_AddFile(AccessLog,argv[iarq])) {
433 debuga(_("Not enough memory to store the input log file names\n"));
434 exit(EXIT_FAILURE);
435 }
436 AccessLogFromCmdLine++;
437 }
438 }
439
440 if(debug) debuga(_("Init\n"));
441
442 if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR);
443 if(access(ConfigFile, R_OK) != 0) {
444 debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno));
445 exit(EXIT_FAILURE);
446 }
447
448 if(access(ConfigFile, R_OK) == 0)
449 getconf();
450
451 if(userip) UserIp=true;
452
453 if(dns) ip2name_forcedns();
454
455 if (lastlog>=0) LastLog=lastlog;
456
457 if(outdir[0] == '\0') strcpy(outdir,OutputDir);
458 if(outdir[0] != '\0') strcat(outdir,"/");
459
460 if(realt) {
461 realtime();
462 exit(EXIT_SUCCESS);
463 }
464
465 if(IndexTree == INDEX_TREE_FILE)
466 strcpy(ImageFile,"../images");
467 else
468 strcpy(ImageFile,"../../../images");
469
470 dataonly=(DataFile[0] != '\0');
471
472 if (df=='\0') df=DateFormat;
473 if (df=='\0') df='u';
474 if (df=='w')
475 IndexTree=INDEX_TREE_FILE;
476
477 if(AccessLog==NULL) {
478 AccessLog=FileList_Create();
479 if (!FileList_AddFile(AccessLog,"/var/log/squid/access.log")) {
480 debuga(_("Not enough memory to store the input log file names\n"));
481 exit(EXIT_FAILURE);
482 }
483 }
484
485 if(split) {
486 const char *file;
487
488 FIter=FileListIter_Open(AccessLog);
489 while ((file=FileListIter_Next(FIter))!=NULL)
490 splitlog(file, df, dfrom, duntil, convert, splitprefix);
491 FileListIter_Close(FIter);
492 exit(EXIT_SUCCESS);
493 }
494 if(convert) {
495 const char *file;
496
497 FIter=FileListIter_Open(AccessLog);
498 while ((file=FileListIter_Next(FIter))!=NULL)
499 convlog(file, df, dfrom, duntil);
500 FileListIter_Close(FIter);
501 exit(EXIT_SUCCESS);
502 }
503
504 load_excludecodes(ExcludeCodes);
505
506 if(access(PasswdFile, R_OK) == 0) {
507 getusers(PasswdFile,debug);
508 ReadFilter.SysUsers=true;
509 } else {
510 ReadFilter.SysUsers=false;
511 }
512
513 if(hexclude[0] == '\0')
514 strcpy(hexclude,ExcludeHosts);
515 if(hexclude[0] != '\0') {
516 gethexclude(hexclude,debug);
517 ReadFilter.HostFilter=true;
518 } else {
519 ReadFilter.HostFilter=false;
520 }
521
522 if(ReportType == 0) {
523 ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
524 REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED |
525 REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS;
526 }
527
528 if(access(ExcludeUsers, R_OK) == 0) {
529 getuexclude(ExcludeUsers,debug);
530 ReadFilter.UserFilter=true;
531 } else {
532 ReadFilter.UserFilter=false;
533 }
534 if (HostAliasFile[0] != '\0')
535 read_hostalias(HostAliasFile);
536 if (UserAliasFile[0] != '\0')
537 read_useralias(UserAliasFile);
538
539 indexonly=false;
540 if(ReadFilter.UserFilter) {
541 if(is_indexonly())
542 indexonly=true;
543 }
544 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true;
545 if(Index == INDEX_ONLY) indexonly=true;
546
547 if(MaxElapsed[0] != '\0')
548 ReadFilter.max_elapsed=atol(MaxElapsed);
549 else
550 ReadFilter.max_elapsed=0;
551
552 if(uagent[0] == '\0') strcpy(uagent,UserAgentLog);
553
554 if(tmp[0] == '\0') strcpy(tmp,TempDir);
555 else strcpy(TempDir,tmp);
556 /*
557 For historical reasons, the temporary directory is the subdirectory "sarg" of the path
558 provided by the user.
559 */
560 strcat(tmp,"/sarg");
561
562 if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) {
563 debuga(_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp);
564 exit(EXIT_FAILURE);
565 }
566
567 if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail);
568
569 if(email[0] != '\0') {
570 my_mkdir(tmp);
571 strcpy(outdir,tmp);
572 strcat(outdir,"/");
573 }
574
575 if(access(tmp, R_OK) == 0) {
576 if (debug) debuga(_("Deleting temporary directory \"%s\"\n"),tmp);
577 emptytmpdir(tmp);
578 }
579 my_mkdir(tmp);
580
581 if(debug) {
582 const char *file;
583
584 debuga(_("Parameters:\n"));
585 debuga(_(" Hostname or IP address (-a) = %s\n"),addr);
586 debuga(_(" Useragent log (-b) = %s\n"),uagent);
587 debuga(_(" Exclude file (-c) = %s\n"),hexclude);
588 debuga(_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
589 debuga(_(" Email address to send reports (-e) = %s\n"),email);
590 debuga(_(" Config file (-f) = %s\n"),ConfigFile);
591 if (df=='e')
592 debuga(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
593 else if (df=='u')
594 debuga(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
595 else if (df=='w')
596 debuga(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
597 debuga(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
598 debuga(_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
599 FIter=FileListIter_Open(AccessLog);
600 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
601 debuga(_(" Input log (-l) = %s\n"),file);
602 FileListIter_Close(FIter);
603 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
604 debuga(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
605 debuga(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
606 debuga(_(" Output dir (-o) = %s\n"),outdir);
607 debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
608 debuga(_(" Accessed site (-s) = %s\n"),site);
609 debuga(_(" Time (-t) = %s\n"),hm_str);
610 debuga(_(" User (-u) = %s\n"),us);
611 debuga(_(" Temporary dir (-w) = %s\n"),tmp);
612 debuga(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
613 debuga(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
614 debuga(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
615 debuga("\n");
616 }
617
618 if(debugm) {
619 const char *file;
620 printf(_("Parameters:\n"));
621 printf(_(" Hostname or IP address (-a) = %s\n"),addr);
622 printf(_(" Useragent log (-b) = %s\n"),uagent);
623 printf(_(" Exclude file (-c) = %s\n"),hexclude);
624 printf(_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
625 printf(_(" Email address to send reports (-e) = %s\n"),email);
626 printf(_(" Config file (-f) = %s\n"),ConfigFile);
627 if (df=='e')
628 printf(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
629 else if (df=='u')
630 printf(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
631 else if (df=='w')
632 printf(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
633 printf(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
634 printf(_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
635 FIter=FileListIter_Open(AccessLog);
636 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
637 printf(_(" Input log (-l) = %s\n"),file);
638 FileListIter_Close(FIter);
639 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
640 printf(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
641 printf(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
642 printf(_(" Output dir (-o) = %s\n"),outdir);
643 printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
644 printf(_(" Accessed site (-s) = %s\n"),site);
645 printf(_(" Time (-t) = %s\n"),hm_str);
646 printf(_(" User (-u) = %s\n"),us);
647 printf(_(" Temporary dir (-w) = %s\n"),tmp);
648 printf(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
649 printf(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
650 printf(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
651 printf(_("sarg version: %s\n"),VERSION);
652 }
653
654 if(debug)
655 debuga(_("sarg version: %s\n"),VERSION);
656
657 #ifdef ENABLE_DOUBLE_CHECK_DATA
658 debuga(_("Sarg compiled to report warnings if the output is inconsistent\n"));
659 #endif
660
661 #ifdef HAVE_RLIM_T
662 if (Ulimit[0] != '\0') {
663 struct rlimit rl;
664 long l1, l2;
665 int rc=0;
666
667 #if defined(RLIMIT_NOFILE)
668 getrlimit (RLIMIT_NOFILE, &rl);
669 #elif defined(RLIMIT_OFILE)
670 getrlimit (RLIMIT_OFILE, &rl);
671 #else
672 #warning "No rlimit resource for the number of open files"
673 #endif
674 l1 = rl.rlim_cur;
675 l2 = rl.rlim_max;
676
677 rl.rlim_cur = atol(Ulimit);
678 rl.rlim_max = atol(Ulimit);
679 #if defined(RLIMIT_NOFILE)
680 rc=setrlimit (RLIMIT_NOFILE, &rl);
681 #elif defined(RLIMIT_OFILE)
682 rc=setrlimit (RLIMIT_OFILE, &rl);
683 #else
684 #warning "No rlimit resource for the number of open files"
685 #endif
686 if(rc == -1) {
687 debuga(_("setrlimit error - %s\n"),strerror(errno));
688 }
689
690 if(debug)
691 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);
692 }
693 #endif
694
695 init_usertab(UserTabFile);
696
697 read_start_time=time(NULL);
698 LogStatus=ReadLogFile(&ReadFilter);
699 read_end_time=time(NULL);
700 read_elapsed=(double)read_end_time-(double)read_start_time;
701
702 FileList_Destroy(&AccessLog);
703 free_download();
704 free_excludecodes();
705 free_exclude();
706
707 if (debug) {
708 char date0[30], date1[30];
709 struct tm Start,End;
710
711 GetLogPeriod(&Start,&End);
712 strftime(date0,sizeof(date0),"%x",&Start);
713 strftime(date1,sizeof(date1),"%x",&End);
714 // TRANSLATORS: The %s are the start and end dates in locale format.
715 debuga(_("Period covered by log files: %s-%s\n"),date0,date1);
716 }
717
718 if (!LogStatus){
719 debuga(_("No records found\n"));
720 debuga(_("End\n"));
721 userinfo_free();
722 if(userfile) free(userfile);
723 close_usertab();
724 exit(EXIT_SUCCESS);
725 }
726
727 if (debug) {
728 char date0[30], date1[30];
729
730 strftime(date0,sizeof(date0),"%x",&period.start);
731 strftime(date1,sizeof(date1),"%x",&period.end);
732 // TRANSLATORS: The %s are the start and end dates in locale format.
733 debuga(_("Period extracted from log files: %s-%s\n"),date0,date1);
734 getperiod_fromrange(&period,dfrom,duntil);
735 }
736 if (getperiod_buildtext(&period)<0) {
737 debuga(_("Failed to build the string representation of the date range\n"));
738 exit(EXIT_FAILURE);
739 }
740
741 process_start_time=time(NULL);
742 if(DataFile[0] != '\0')
743 data_file(tmp);
744 else
745 gerarel();
746 process_end_time=time(NULL);
747 process_elapsed=(double)process_end_time-(double)process_start_time;
748
749 denied_cleanup();
750 authfail_cleanup();
751 download_cleanup();
752
753 if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) {
754 unlinkdir(tmp,0);
755 }
756
757 ip2name_cleanup();
758 free_hostalias();
759 free_useralias();
760 userinfo_free();
761 if(userfile)
762 free(userfile);
763 close_usertab();
764
765 end_time=time(NULL);
766
767 if (show_statis) {
768 double elapsed=(double)end_time-(double)start_time;
769 debuga(_("Total execution time: %.0lf seconds\n"),elapsed);
770 if (read_elapsed>0.) {
771 debuga(_("Lines read: %lu lines in %.0lf seconds (%.0lf lines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_elapsed);
772 }
773 if (process_elapsed>0.) {
774 debuga(_("Processed records: %lu records in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process_elapsed);
775 debuga(_("Users: %lu users in %.0lf seconds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed);
776 }
777 }
778
779 if(debug)
780 debuga(_("End\n"));
781
782 exit(EXIT_SUCCESS);
783 }
784
785
786 static void getusers(const char *pwdfile, int debug)
787 {
788 FILE *fp_usr;
789 char buf[255];
790 char *str;
791 long int nreg=0;
792
793 if(debug)
794 debuga(_("Loading password file from %s\n"),pwdfile);
795
796 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
797 debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno));
798 exit(EXIT_FAILURE);
799 }
800
801 if (fseek(fp_usr, 0, SEEK_END)==-1) {
802 debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno));
803 exit(EXIT_FAILURE);
804 }
805 nreg = ftell(fp_usr);
806 if (nreg<0) {
807 debuga(_("Cannot get the size of file %s\n"),pwdfile);
808 exit(EXIT_FAILURE);
809 }
810 nreg = nreg+5000;
811 if (fseek(fp_usr, 0, SEEK_SET)==-1) {
812 debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno));
813 exit(EXIT_FAILURE);
814 }
815
816 if((userfile=(char *) malloc(nreg))==NULL){
817 debuga(_("malloc error (%ld)\n"),nreg);
818 exit(EXIT_FAILURE);
819 }
820
821 bzero(userfile,nreg);
822 strcpy(userfile,":");
823
824 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
825 str=strchr(buf,':');
826 if (!str) {
827 debuga(_("You have an invalid user in your %s file\n"),pwdfile);
828 exit(EXIT_FAILURE);
829 }
830 str[1]='\0';
831 strcat(userfile,buf);
832 }
833
834 fclose(fp_usr);
835
836 return;
837 }