]> git.ipfire.org Git - thirdparty/sarg.git/blame - log.c
Fix several possible sprintf buffer overflows
[thirdparty/sarg.git] / log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
67302a9e 3 * 1998, 2013
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"
27d1fa35 29#include "include/readlog.h"
6068ae56 30#include "include/filelist.h"
25697a35 31
9b179eb0
FM
32#ifdef HAVE_GETOPT_H
33#include <getopt.h>
34#endif
35
27d1fa35
FM
36//! The log file filtering.
37struct ReadLogDataStruct ReadFilter;
f2ec8c75 38
27d1fa35
FM
39//! The list of the system users.
40/*@null@*/char *userfile=NULL;
25697a35 41
6068ae56
FM
42//! List of the input log files to process.
43FileListObject AccessLog=NULL;
44
45
936c9905 46static void getusers(const char *pwdfile, int debug);
25697a35 47
25f0793f 48int main(int argc,char *argv[])
25697a35 49{
9bd92830
FM
50 extern int optind;
51 extern int optopt;
52 extern char *optarg;
53
9bd92830
FM
54 char hm_str[15];
55 char uagent[MAXLEN];
56 char hexclude[MAXLEN];
2c7e8c23 57 char splitprefix[MAXLEN];
9bd92830 58 int ch;
9bd92830 59 int errflg=0;
9bd92830 60 bool dns=false;
9bd92830 61 int iarq=0;
9bd92830 62 int lastlog=-1;
27d1fa35 63 int LogStatus;
9bd92830
FM
64 bool realt;
65 bool userip;
0459449f
FM
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;
6068ae56 74 FileListIterator FIter;
9bd92830
FM
75 static int split=0;
76 static int convert=0;
77 static int output_css=0;
0459449f 78 static int show_statis=0;
9bd92830
FM
79 int option_index;
80 static struct option long_options[]=
81 {
82 {"convert",no_argument,&convert,1},
83 {"css",no_argument,&output_css,1},
c6551b37 84 {"help",no_argument,NULL,'h'},
9bd92830
FM
85 {"lastlog",required_argument,NULL,2},
86 {"keeplogs",no_argument,NULL,3},
87 {"split",no_argument,&split,1},
2c7e8c23 88 {"splitprefix",required_argument,NULL,'P'},
0459449f 89 {"statistics",no_argument,&show_statis,1},
9bd92830
FM
90 {0,0,0,0}
91 };
25697a35 92
0459449f
FM
93 start_time=time(NULL);
94
abfc169e 95#ifdef HAVE_LOCALE_H
9bd92830 96 setlocale(LC_TIME,"");
abfc169e
FM
97#endif
98
cec3eed3 99#if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
9bd92830
FM
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 }
abfc169e
FM
112#endif //ENABLE_NLS
113
9bd92830
FM
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;
9bd92830 131
8d811f76 132 snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR);
9bd92830
FM
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");
829a53c2 147 AnonymousOutputFiles=false;
9bd92830 148 Ip2Name=false;
81a022d8 149 DateFormat='u';
9bd92830
FM
150 OverwriteReport=false;
151 RemoveTempFiles=true;
152 strcpy(ReplaceIndex,"index.html");
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 |
007905af
FM
172 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
173 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
174 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
9bd92830 175 UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
007905af
FM
176 USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
177 USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
9bd92830
FM
178 strcpy(DataFileDelimiter,";");
179 DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
007905af 180 DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
9bd92830 181 ShowReadStatistics=true;
2f4787e6 182 ShowReadPercent=false;
9bd92830
FM
183 strcpy(IndexSortOrder,"D");
184 ShowSargInfo=true;
185 ShowSargLogo=true;
8949e757 186 ParsedOutputLog[0]='\0';
9bd92830
FM
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;
3becf85c 194#if defined(FONTDIR)
9bd92830 195 strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
3becf85c 196#else
9bd92830 197 GraphFont[0]='\0';
3becf85c 198#endif
9bd92830
FM
199 strcpy(Ulimit,"20000");
200 NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
201 IndexTree=INDEX_TREE_FILE;
9aaa3361 202 IndexFields=INDEXFIELDS_DIRSIZE;
9bd92830
FM
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");
fd75cd90 219 LDAPNativeCharset[0]='\0';
9bd92830
FM
220 SortTableJs[0]='\0';
221
9bd92830 222 tmp[0]='\0';
9bd92830 223 us[0]='\0';
27d1fa35 224 ReadFilter.DateRange[0]='\0';
81a022d8 225 df='\0';
9bd92830
FM
226 uagent[0]='\0';
227 hexclude[0]='\0';
228 addr[0]='\0';
27d1fa35
FM
229 ReadFilter.StartTime=-1;
230 ReadFilter.EndTime=-1;
9bd92830
FM
231 site[0]='\0';
232 outdir[0]='\0';
2c7e8c23 233 splitprefix[0]='\0';
9bd92830 234 email[0]='\0';
9bd92830
FM
235 UserInvalidChar[0]='\0';
236 DataFile[0]='\0';
237 SquidGuardConf[0]='\0';
238 DansGuardianConf[0]='\0';
9bd92830 239 hm_str[0]='\0';
22715352 240 HostAliasFile[0]='\0';
c4f0ea8f 241 UserAliasFile[0]='\0';
9bd92830 242
9bd92830 243 dansguardian_count=0;
330b1c52 244 redirector_count=0;
9bd92830
FM
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;
9bd92830
FM
262 squid24=false;
263 dfrom=0;
264 duntil=0;
11767c6a 265 KeepTempLog=false;
4d62cb0a
FM
266 NumLogSuccessiveErrors=3;
267 NumLogTotalErrors=50;
0459449f
FM
268 lines_read=0UL;
269 records_kept=0UL;
270 nusers=0UL;
9bd92830
FM
271
272 bzero(IncludeUsers, sizeof(IncludeUsers));
273 bzero(ExcludeString, sizeof(ExcludeString));
9bd92830
FM
274 memset(&period,0,sizeof(period));
275
9bd92830
FM
276 AccessLogFromCmdLine=0;
277 RedirectorLogFromCmdLine=0;
278
279 strcpy(Title,_("Squid User Access Report"));
280
11767c6a 281 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){
9bd92830
FM
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':
a87d4d11 293 safe_strcpy(addr,optarg,sizeof(addr));
9bd92830 294 break;
d6f08599 295 case 'b': //unused option
a87d4d11 296 safe_strcpy(uagent,optarg,sizeof(uagent));
9bd92830
FM
297 break;
298 case 'c':
a87d4d11 299 safe_strcpy(hexclude,optarg,sizeof(hexclude));
9bd92830
FM
300 break;
301 case 'd':
27d1fa35 302 safe_strcpy(ReadFilter.DateRange,optarg,sizeof(ReadFilter.DateRange));
60ec7f09 303 date_from(ReadFilter.DateRange,sizeof(ReadFilter.DateRange), &dfrom, &duntil);
9bd92830
FM
304 break;
305 case 'e':
a87d4d11 306 safe_strcpy(email,optarg,sizeof(email));
9bd92830
FM
307 break;
308 case 'f':
a87d4d11 309 safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile));
9bd92830
FM
310 break;
311 case 'g':
81a022d8 312 df=*optarg;
9bd92830
FM
313 break;
314 case 'h':
315 usage(argv[0]);
316 exit(EXIT_SUCCESS);
317 case 'i':
318 iprel=true;
319 break;
11767c6a
FM
320 case 'k':
321 KeepTempLog=true;
322 break;
9bd92830 323 case 'l':
6068ae56
FM
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"));
9bd92830
FM
328 exit(EXIT_FAILURE);
329 }
9bd92830
FM
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':
a87d4d11 352 safe_strcpy(outdir,optarg,sizeof(outdir));
9bd92830
FM
353 break;
354 case 'p':
355 userip=true;
356 break;
2c7e8c23 357 case 'P':
a87d4d11 358 safe_strcpy(splitprefix,optarg,sizeof(splitprefix));
2c7e8c23 359 break;
9bd92830
FM
360 case 'r':
361 realt=true;
362 break;
363 case 's':
a87d4d11 364 safe_strcpy(site,optarg,sizeof(site));
9bd92830
FM
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) {
223422fe 372 debuga(_("Time period passed on the command line with option -t must be HH:MM\n"));
9bd92830
FM
373 exit(EXIT_FAILURE);
374 }
27d1fa35
FM
375 ReadFilter.StartTime=h1*100+m1;
376 ReadFilter.EndTime=ReadFilter.StartTime;
9bd92830
FM
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) {
223422fe 380 debuga(_("Time range passed on the command line with option -t must be HH:MM-HH:MM\n"));
9bd92830
FM
381 exit(EXIT_FAILURE);
382 }
27d1fa35
FM
383 ReadFilter.StartTime=h1*100+m1;
384 ReadFilter.EndTime=h2*100+m2;
9bd92830
FM
385 snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2);
386 }
387 break;
388 }
389 case 'u':
a87d4d11 390 safe_strcpy(us,optarg,sizeof(us));
9bd92830
FM
391 break;
392 case 'v':
393 version();
394 break;
395 case 'w':
a87d4d11 396 safe_strcpy(tmp,optarg,sizeof(tmp));
9bd92830
FM
397 break;
398 case 'x':
399 debug++;
400 break;
d6f08599 401 case 'y': //unused option
9bd92830
FM
402 langcode++;
403 break;
404 case 'z':
405 debugz++;
406 break;
007905af
FM
407 case ':':
408 debuga(_("Option -%c requires an argument\n"),optopt);
409 exit(EXIT_FAILURE);
9bd92830
FM
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
6068ae56
FM
423 if(output_css) {
424 css_content(stdout);
425 exit(EXIT_SUCCESS);
426 }
427
9bd92830 428 if (optind<argc) {
6068ae56
FM
429 if (!AccessLog)
430 AccessLog=FileList_Create();
9bd92830 431 for (iarq=optind ; iarq<argc ; iarq++) {
6068ae56
FM
432 if (!FileList_AddFile(AccessLog,argv[iarq])) {
433 debuga(_("Not enough memory to store the input log file names\n"));
9bd92830
FM
434 exit(EXIT_FAILURE);
435 }
9bd92830
FM
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
51b166d4 453 if(dns) ip2name_forcedns();
9bd92830
FM
454
455 if (lastlog>=0) LastLog=lastlog;
456
2c7e8c23
FM
457 if(outdir[0] == '\0') strcpy(outdir,OutputDir);
458 if(outdir[0] != '\0') strcat(outdir,"/");
459
9bd92830
FM
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
11284535 470 dataonly=(DataFile[0] != '\0');
9bd92830 471
81a022d8
FM
472 if (df=='\0') df=DateFormat;
473 if (df=='\0') df='u';
474 if (df=='w')
3e80818c
FM
475 IndexTree=INDEX_TREE_FILE;
476
6068ae56
FM
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 }
9bd92830
FM
483 }
484
9bd92830 485 if(split) {
6068ae56
FM
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);
9bd92830
FM
492 exit(EXIT_SUCCESS);
493 }
494 if(convert) {
6068ae56
FM
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);
9bd92830
FM
501 exit(EXIT_SUCCESS);
502 }
503
504 load_excludecodes(ExcludeCodes);
505
506 if(access(PasswdFile, R_OK) == 0) {
507 getusers(PasswdFile,debug);
27d1fa35
FM
508 ReadFilter.SysUsers=true;
509 } else {
510 ReadFilter.SysUsers=false;
9bd92830
FM
511 }
512
513 if(hexclude[0] == '\0')
514 strcpy(hexclude,ExcludeHosts);
515 if(hexclude[0] != '\0') {
516 gethexclude(hexclude,debug);
27d1fa35
FM
517 ReadFilter.HostFilter=true;
518 } else {
519 ReadFilter.HostFilter=false;
9bd92830
FM
520 }
521
522 if(ReportType == 0) {
523 ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
007905af
FM
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;
9bd92830
FM
526 }
527
528 if(access(ExcludeUsers, R_OK) == 0) {
529 getuexclude(ExcludeUsers,debug);
27d1fa35
FM
530 ReadFilter.UserFilter=true;
531 } else {
532 ReadFilter.UserFilter=false;
9bd92830 533 }
22715352
FM
534 if (HostAliasFile[0] != '\0')
535 read_hostalias(HostAliasFile);
c4f0ea8f
FM
536 if (UserAliasFile[0] != '\0')
537 read_useralias(UserAliasFile);
9bd92830 538
6588b137 539 indexonly=false;
27d1fa35 540 if(ReadFilter.UserFilter) {
9bd92830 541 if(is_indexonly())
6588b137 542 indexonly=true;
9bd92830 543 }
6588b137
FM
544 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true;
545 if(Index == INDEX_ONLY) indexonly=true;
9bd92830 546
27d1fa35
FM
547 if(MaxElapsed[0] != '\0')
548 ReadFilter.max_elapsed=atol(MaxElapsed);
549 else
550 ReadFilter.max_elapsed=0;
9bd92830 551
9bd92830
FM
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
9bd92830
FM
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) {
170a77ea
FM
576 if (debug) debuga(_("Deleting temporary directory \"%s\"\n"),tmp);
577 emptytmpdir(tmp);
9bd92830
FM
578 }
579 my_mkdir(tmp);
9bd92830
FM
580
581 if(debug) {
6068ae56
FM
582 const char *file;
583
9bd92830
FM
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);
27d1fa35 588 debuga(_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
9bd92830
FM
589 debuga(_(" Email address to send reports (-e) = %s\n"),email);
590 debuga(_(" Config file (-f) = %s\n"),ConfigFile);
81a022d8 591 if (df=='e')
9bd92830 592 debuga(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
81a022d8 593 else if (df=='u')
9bd92830 594 debuga(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
81a022d8 595 else if (df=='w')
9bd92830
FM
596 debuga(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
597 debuga(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
11767c6a 598 debuga(_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
6068ae56
FM
599 FIter=FileListIter_Open(AccessLog);
600 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
601 debuga(_(" Input log (-l) = %s\n"),file);
602 FileListIter_Close(FIter);
9bd92830
FM
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) {
6068ae56 619 const char *file;
9bd92830
FM
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);
27d1fa35 624 printf(_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
9bd92830
FM
625 printf(_(" Email address to send reports (-e) = %s\n"),email);
626 printf(_(" Config file (-f) = %s\n"),ConfigFile);
81a022d8 627 if (df=='e')
9bd92830 628 printf(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
81a022d8 629 else if (df=='u')
9bd92830 630 printf(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
81a022d8 631 else if (df=='w')
9bd92830
FM
632 printf(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
633 printf(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
11767c6a 634 printf(_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
6068ae56
FM
635 FIter=FileListIter_Open(AccessLog);
636 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
637 printf(_(" Input log (-l) = %s\n"),file);
638 FileListIter_Close(FIter);
9bd92830
FM
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);
25697a35 656
04a01ed3 657#ifdef ENABLE_DOUBLE_CHECK_DATA
9bd92830 658 debuga(_("Sarg compiled to report warnings if the output is inconsistent\n"));
04a01ed3
FM
659#endif
660
e112fa1f 661#ifdef HAVE_RLIM_T
9bd92830
FM
662 if (Ulimit[0] != '\0') {
663 struct rlimit rl;
664 long l1, l2;
665 int rc=0;
243a9fab 666
25697a35 667#if defined(RLIMIT_NOFILE)
9bd92830 668 getrlimit (RLIMIT_NOFILE, &rl);
ed1ac52f 669#elif defined(RLIMIT_OFILE)
9bd92830 670 getrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
671#else
672#warning "No rlimit resource for the number of open files"
25697a35 673#endif
9bd92830
FM
674 l1 = rl.rlim_cur;
675 l2 = rl.rlim_max;
d6e703cc 676
9bd92830
FM
677 rl.rlim_cur = atol(Ulimit);
678 rl.rlim_max = atol(Ulimit);
243a9fab 679#if defined(RLIMIT_NOFILE)
9bd92830 680 rc=setrlimit (RLIMIT_NOFILE, &rl);
243a9fab 681#elif defined(RLIMIT_OFILE)
9bd92830 682 rc=setrlimit (RLIMIT_OFILE, &rl);
243a9fab
FM
683#else
684#warning "No rlimit resource for the number of open files"
685#endif
9bd92830 686 if(rc == -1) {
007905af 687 debuga(_("setrlimit error - %s\n"),strerror(errno));
9bd92830 688 }
25697a35 689
9bd92830 690 if(debug)
c4633554 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);
9bd92830 692 }
e112fa1f 693#endif
25697a35 694
9bd92830
FM
695 init_usertab(UserTabFile);
696
0459449f 697 read_start_time=time(NULL);
27d1fa35 698 LogStatus=ReadLogFile(&ReadFilter);
0459449f
FM
699 read_end_time=time(NULL);
700 read_elapsed=(double)read_end_time-(double)read_start_time;
9bd92830 701
6068ae56 702 FileList_Destroy(&AccessLog);
9bd92830
FM
703 free_download();
704 free_excludecodes();
705 free_exclude();
706
27d1fa35 707 if (!LogStatus){
9bd92830
FM
708 debuga(_("No records found\n"));
709 debuga(_("End\n"));
9bd92830
FM
710 userinfo_free();
711 if(userfile) free(userfile);
712 close_usertab();
713 exit(EXIT_SUCCESS);
714 }
715
27d1fa35 716 if (ReadFilter.DateRange[0]!='\0') {
9bd92830
FM
717 char date0[30], date1[30];
718
719 strftime(date0,sizeof(date0),"%d/%m/%Y",&period.start);
720 strftime(date1,sizeof(date1),"%d/%m/%Y",&period.end);
721 debuga(_("Period covered by log files: %s-%s\n"),date0,date1);
722 getperiod_fromrange(&period,dfrom,duntil);
723 }
724 if (getperiod_buildtext(&period)<0) {
725 debuga(_("Failed to build the string representation of the date range\n"));
726 exit(EXIT_FAILURE);
727 }
728
9bd92830
FM
729 if(debug)
730 debuga(_("Period: %s\n"),period.text);
25697a35 731
0459449f 732 process_start_time=time(NULL);
9bd92830
FM
733 if(DataFile[0] != '\0')
734 data_file(tmp);
735 else
736 gerarel();
0459449f
FM
737 process_end_time=time(NULL);
738 process_elapsed=(double)process_end_time-(double)process_start_time;
9bd92830 739
60b48ae5 740 denied_cleanup();
16b013cc 741 authfail_cleanup();
11284535 742 download_cleanup();
9bd92830 743
11767c6a 744 if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) {
9bd92830
FM
745 unlinkdir(tmp,0);
746 }
747
0326d73b 748 ip2name_cleanup();
0a6722a1 749 free_hostalias();
c4f0ea8f 750 free_useralias();
9bd92830
FM
751 userinfo_free();
752 if(userfile)
753 free(userfile);
754 close_usertab();
755
0459449f
FM
756 end_time=time(NULL);
757
758 if (show_statis) {
759 double elapsed=(double)end_time-(double)start_time;
760 debuga(_("Total execution time: %.0lf seconds\n"),elapsed);
761 if (read_elapsed>0.) {
762 debuga(_("Lines read: %lu lines in %.0lf seconds (%.0lf lines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_elapsed);
763 }
764 if (process_elapsed>0.) {
765 debuga(_("Processed records: %lu records in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process_elapsed);
766 debuga(_("Users: %lu users in %.0lf seconds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed);
767 }
768 }
769
9bd92830
FM
770 if(debug)
771 debuga(_("End\n"));
772
773 exit(EXIT_SUCCESS);
25697a35
GS
774}
775
776
936c9905 777static void getusers(const char *pwdfile, int debug)
25697a35 778{
9bd92830
FM
779 FILE *fp_usr;
780 char buf[255];
781 char *str;
782 long int nreg=0;
783
784 if(debug)
785 debuga(_("Loading password file from %s\n"),pwdfile);
786
787 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
788 debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno));
789 exit(EXIT_FAILURE);
790 }
791
792 if (fseek(fp_usr, 0, SEEK_END)==-1) {
793 debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno));
794 exit(EXIT_FAILURE);
795 }
796 nreg = ftell(fp_usr);
797 if (nreg<0) {
798 debuga(_("Cannot get the size of file %s\n"),pwdfile);
799 exit(EXIT_FAILURE);
800 }
801 nreg = nreg+5000;
802 if (fseek(fp_usr, 0, SEEK_SET)==-1) {
803 debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno));
804 exit(EXIT_FAILURE);
805 }
806
807 if((userfile=(char *) malloc(nreg))==NULL){
808 debuga(_("malloc error (%ld)\n"),nreg);
809 exit(EXIT_FAILURE);
810 }
811
812 bzero(userfile,nreg);
813 strcpy(userfile,":");
814
815 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
816 str=strchr(buf,':');
817 if (!str) {
818 debuga(_("You have an invalid user in your %s file\n"),pwdfile);
819 exit(EXIT_FAILURE);
820 }
821 str[1]='\0';
822 strcat(userfile,buf);
823 }
824
825 fclose(fp_usr);
826
827 return;
25697a35 828}