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