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