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