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