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