]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
110ce984 | 3 | * 1998, 2015 |
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; | |
728f6a04 FM |
44 | //! Selected locale set through the environment variable. |
45 | char *CurrentLocale=NULL; | |
137eb63d FM |
46 | //! Set to \c true if a useragent log is provided on the command line. |
47 | bool UserAgentFromCmdLine=false; | |
48 | ||
49 | extern FileListObject UserAgentLog; | |
728f6a04 | 50 | |
936c9905 | 51 | static void getusers(const char *pwdfile, int debug); |
25697a35 | 52 | |
25f0793f | 53 | int main(int argc,char *argv[]) |
25697a35 | 54 | { |
9bd92830 FM |
55 | extern int optind; |
56 | extern int optopt; | |
57 | extern char *optarg; | |
58 | ||
9bd92830 | 59 | char hm_str[15]; |
9bd92830 | 60 | char hexclude[MAXLEN]; |
2c7e8c23 | 61 | char splitprefix[MAXLEN]; |
9bd92830 | 62 | int ch; |
9bd92830 | 63 | int errflg=0; |
9bd92830 | 64 | bool dns=false; |
9bd92830 | 65 | int iarq=0; |
9bd92830 | 66 | int lastlog=-1; |
27d1fa35 | 67 | int LogStatus; |
9bd92830 FM |
68 | bool realt; |
69 | bool userip; | |
0459449f FM |
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; | |
6068ae56 | 78 | FileListIterator FIter; |
9bd92830 FM |
79 | static int split=0; |
80 | static int convert=0; | |
81 | static int output_css=0; | |
0459449f | 82 | static int show_statis=0; |
d6d7c780 | 83 | static int show_version=0; |
9bd92830 FM |
84 | int option_index; |
85 | static struct option long_options[]= | |
86 | { | |
87 | {"convert",no_argument,&convert,1}, | |
88 | {"css",no_argument,&output_css,1}, | |
c6551b37 | 89 | {"help",no_argument,NULL,'h'}, |
9bd92830 FM |
90 | {"lastlog",required_argument,NULL,2}, |
91 | {"keeplogs",no_argument,NULL,3}, | |
92 | {"split",no_argument,&split,1}, | |
2c7e8c23 | 93 | {"splitprefix",required_argument,NULL,'P'}, |
0459449f | 94 | {"statistics",no_argument,&show_statis,1}, |
d6d7c780 | 95 | {"version",no_argument,&show_version,'V'}, |
9bd92830 FM |
96 | {0,0,0,0} |
97 | }; | |
25697a35 | 98 | |
0459449f FM |
99 | start_time=time(NULL); |
100 | ||
abfc169e | 101 | #ifdef HAVE_LOCALE_H |
9bd92830 | 102 | setlocale(LC_TIME,""); |
abfc169e FM |
103 | #endif |
104 | ||
cec3eed3 | 105 | #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H) |
728f6a04 FM |
106 | CurrentLocale=setlocale (LC_ALL, ""); |
107 | if (!CurrentLocale) { | |
9bd92830 FM |
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 | } | |
abfc169e FM |
119 | #endif //ENABLE_NLS |
120 | ||
9bd92830 FM |
121 | BgImage[0]='\0'; |
122 | LogoImage[0]='\0'; | |
123 | LogoText[0]='\0'; | |
124 | PasswdFile[0]='\0'; | |
125 | OutputEmail[0]='\0'; | |
9bd92830 FM |
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; | |
9bd92830 | 137 | |
8d811f76 | 138 | snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR); |
9bd92830 FM |
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"); | |
829a53c2 | 153 | AnonymousOutputFiles=false; |
9bd92830 | 154 | Ip2Name=false; |
81a022d8 | 155 | DateFormat='u'; |
9bd92830 FM |
156 | OverwriteReport=false; |
157 | RemoveTempFiles=true; | |
56b97279 | 158 | strcpy(ReplaceIndex,INDEX_HTML_FILE); |
9bd92830 FM |
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 | | |
007905af FM |
178 | TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT | |
179 | TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME | | |
180 | TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE; | |
9bd92830 | 181 | UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB | |
007905af FM |
182 | USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC | |
183 | USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE; | |
9bd92830 FM |
184 | strcpy(DataFileDelimiter,";"); |
185 | DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT | | |
007905af | 186 | DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED; |
9bd92830 | 187 | ShowReadStatistics=true; |
2f4787e6 | 188 | ShowReadPercent=false; |
9bd92830 FM |
189 | strcpy(IndexSortOrder,"D"); |
190 | ShowSargInfo=true; | |
191 | ShowSargLogo=true; | |
8949e757 | 192 | ParsedOutputLog[0]='\0'; |
9bd92830 FM |
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; | |
3becf85c | 200 | #if defined(FONTDIR) |
9bd92830 | 201 | strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf"); |
3becf85c | 202 | #else |
9bd92830 | 203 | GraphFont[0]='\0'; |
3becf85c | 204 | #endif |
9bd92830 FM |
205 | strcpy(Ulimit,"20000"); |
206 | NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER; | |
207 | IndexTree=INDEX_TREE_FILE; | |
9aaa3361 | 208 | IndexFields=INDEXFIELDS_DIRSIZE; |
193b9516 | 209 | strcpy(RealtimeTypes,"GET,PUT,CONNECT,POST"); |
9bd92830 FM |
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"); | |
fd75cd90 | 225 | LDAPNativeCharset[0]='\0'; |
9bd92830 FM |
226 | SortTableJs[0]='\0'; |
227 | ||
9bd92830 | 228 | tmp[0]='\0'; |
9bd92830 | 229 | us[0]='\0'; |
27d1fa35 | 230 | ReadFilter.DateRange[0]='\0'; |
81a022d8 | 231 | df='\0'; |
9bd92830 FM |
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 | 275 | |
9c1a94f6 FM |
276 | memset(IncludeUsers,0,sizeof(IncludeUsers)); |
277 | memset(ExcludeString,0,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 |
137eb63d FM |
300 | UserAgentFromCmdLine=true; |
301 | if (!UserAgentLog) | |
302 | UserAgentLog=FileList_Create(); | |
303 | if (!FileList_AddFile(UserAgentLog,optarg)) { | |
304 | debuga(__FILE__,__LINE__,_("Not enough memory to store a user agent file name\n")); | |
305 | exit(EXIT_FAILURE); | |
306 | } | |
9bd92830 FM |
307 | break; |
308 | case 'c': | |
a87d4d11 | 309 | safe_strcpy(hexclude,optarg,sizeof(hexclude)); |
9bd92830 FM |
310 | break; |
311 | case 'd': | |
27d1fa35 | 312 | safe_strcpy(ReadFilter.DateRange,optarg,sizeof(ReadFilter.DateRange)); |
60ec7f09 | 313 | date_from(ReadFilter.DateRange,sizeof(ReadFilter.DateRange), &dfrom, &duntil); |
9bd92830 FM |
314 | break; |
315 | case 'e': | |
a87d4d11 | 316 | safe_strcpy(email,optarg,sizeof(email)); |
9bd92830 FM |
317 | break; |
318 | case 'f': | |
a87d4d11 | 319 | safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile)); |
9bd92830 FM |
320 | break; |
321 | case 'g': | |
81a022d8 | 322 | df=*optarg; |
9bd92830 FM |
323 | break; |
324 | case 'h': | |
325 | usage(argv[0]); | |
326 | exit(EXIT_SUCCESS); | |
327 | case 'i': | |
328 | iprel=true; | |
329 | break; | |
11767c6a FM |
330 | case 'k': |
331 | KeepTempLog=true; | |
332 | break; | |
9bd92830 | 333 | case 'l': |
6068ae56 FM |
334 | if (!AccessLog) |
335 | AccessLog=FileList_Create(); | |
336 | if (!FileList_AddFile(AccessLog,optarg)) { | |
af961877 | 337 | debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n")); |
9bd92830 FM |
338 | exit(EXIT_FAILURE); |
339 | } | |
9bd92830 FM |
340 | AccessLogFromCmdLine++; |
341 | break; | |
342 | case 'L': | |
343 | if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) { | |
af961877 | 344 | debuga(__FILE__,__LINE__,_("Too many redirector logs passed on command line with option -L.\n")); |
9bd92830 FM |
345 | exit(EXIT_FAILURE); |
346 | } | |
347 | if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) { | |
af961877 | 348 | debuga(__FILE__,__LINE__,_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg); |
9bd92830 FM |
349 | exit(EXIT_FAILURE); |
350 | } | |
351 | strcpy(RedirectorLogs[NRedirectorLogs],optarg); | |
352 | NRedirectorLogs++; | |
353 | RedirectorLogFromCmdLine++; | |
354 | break; | |
9bd92830 FM |
355 | case 'n': |
356 | dns=true; | |
357 | break; | |
358 | case 'o': | |
a87d4d11 | 359 | safe_strcpy(outdir,optarg,sizeof(outdir)); |
9bd92830 FM |
360 | break; |
361 | case 'p': | |
362 | userip=true; | |
363 | break; | |
2c7e8c23 | 364 | case 'P': |
a87d4d11 | 365 | safe_strcpy(splitprefix,optarg,sizeof(splitprefix)); |
2c7e8c23 | 366 | break; |
9bd92830 FM |
367 | case 'r': |
368 | realt=true; | |
369 | break; | |
370 | case 's': | |
a87d4d11 | 371 | safe_strcpy(site,optarg,sizeof(site)); |
9bd92830 FM |
372 | break; |
373 | case 't': | |
374 | { | |
375 | int h1,m1,h2,m2; | |
376 | ||
377 | if(strstr(optarg,"-") == 0) { | |
378 | if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) { | |
af961877 | 379 | debuga(__FILE__,__LINE__,_("Time period passed on the command line with option -t must be HH:MM\n")); |
9bd92830 FM |
380 | exit(EXIT_FAILURE); |
381 | } | |
27d1fa35 FM |
382 | ReadFilter.StartTime=h1*100+m1; |
383 | ReadFilter.EndTime=ReadFilter.StartTime; | |
9bd92830 FM |
384 | snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1); |
385 | } else { | |
386 | if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) { | |
af961877 | 387 | debuga(__FILE__,__LINE__,_("Time range passed on the command line with option -t must be HH:MM-HH:MM\n")); |
9bd92830 FM |
388 | exit(EXIT_FAILURE); |
389 | } | |
27d1fa35 FM |
390 | ReadFilter.StartTime=h1*100+m1; |
391 | ReadFilter.EndTime=h2*100+m2; | |
9bd92830 FM |
392 | snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2); |
393 | } | |
394 | break; | |
395 | } | |
396 | case 'u': | |
a87d4d11 | 397 | safe_strcpy(us,optarg,sizeof(us)); |
9bd92830 | 398 | break; |
4f22e788 | 399 | case 'V': |
d6d7c780 | 400 | show_version=1; |
9bd92830 FM |
401 | break; |
402 | case 'w': | |
a87d4d11 | 403 | safe_strcpy(tmp,optarg,sizeof(tmp)); |
9bd92830 FM |
404 | break; |
405 | case 'x': | |
406 | debug++; | |
407 | break; | |
d6f08599 | 408 | case 'y': //unused option |
9bd92830 FM |
409 | langcode++; |
410 | break; | |
411 | case 'z': | |
412 | debugz++; | |
413 | break; | |
007905af | 414 | case ':': |
af961877 | 415 | debuga(__FILE__,__LINE__,_("Option -%c requires an argument\n"),optopt); |
007905af | 416 | exit(EXIT_FAILURE); |
9bd92830 FM |
417 | case '?': |
418 | usage(argv[0]); | |
419 | exit(EXIT_FAILURE); | |
420 | default: | |
421 | abort(); | |
422 | } | |
423 | } | |
424 | ||
425 | if (errflg>0) { | |
426 | usage(argv[0]); | |
427 | exit(2); | |
428 | } | |
d6d7c780 FM |
429 | if (show_version) { |
430 | version(); | |
431 | } | |
9bd92830 | 432 | |
6068ae56 FM |
433 | if(output_css) { |
434 | css_content(stdout); | |
435 | exit(EXIT_SUCCESS); | |
436 | } | |
437 | ||
9bd92830 | 438 | if (optind<argc) { |
6068ae56 FM |
439 | if (!AccessLog) |
440 | AccessLog=FileList_Create(); | |
9bd92830 | 441 | for (iarq=optind ; iarq<argc ; iarq++) { |
6068ae56 | 442 | if (!FileList_AddFile(AccessLog,argv[iarq])) { |
af961877 | 443 | debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n")); |
9bd92830 FM |
444 | exit(EXIT_FAILURE); |
445 | } | |
9bd92830 FM |
446 | AccessLogFromCmdLine++; |
447 | } | |
448 | } | |
449 | ||
af961877 | 450 | if(debug) debuga(__FILE__,__LINE__,_("Init\n")); |
9bd92830 FM |
451 | |
452 | if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR); | |
453 | if(access(ConfigFile, R_OK) != 0) { | |
af961877 | 454 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ConfigFile,strerror(errno)); |
9bd92830 FM |
455 | exit(EXIT_FAILURE); |
456 | } | |
457 | ||
458 | if(access(ConfigFile, R_OK) == 0) | |
da94b2c7 | 459 | getconf(ConfigFile); |
9bd92830 FM |
460 | |
461 | if(userip) UserIp=true; | |
462 | ||
51b166d4 | 463 | if(dns) ip2name_forcedns(); |
9bd92830 FM |
464 | |
465 | if (lastlog>=0) LastLog=lastlog; | |
466 | ||
2c7e8c23 FM |
467 | if(outdir[0] == '\0') strcpy(outdir,OutputDir); |
468 | if(outdir[0] != '\0') strcat(outdir,"/"); | |
469 | ||
9bd92830 FM |
470 | if(IndexTree == INDEX_TREE_FILE) |
471 | strcpy(ImageFile,"../images"); | |
472 | else | |
473 | strcpy(ImageFile,"../../../images"); | |
474 | ||
11284535 | 475 | dataonly=(DataFile[0] != '\0'); |
9bd92830 | 476 | |
81a022d8 FM |
477 | if (df=='\0') df=DateFormat; |
478 | if (df=='\0') df='u'; | |
479 | if (df=='w') | |
3e80818c FM |
480 | IndexTree=INDEX_TREE_FILE; |
481 | ||
6068ae56 FM |
482 | if(AccessLog==NULL) { |
483 | AccessLog=FileList_Create(); | |
484 | if (!FileList_AddFile(AccessLog,"/var/log/squid/access.log")) { | |
af961877 | 485 | debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n")); |
6068ae56 FM |
486 | exit(EXIT_FAILURE); |
487 | } | |
9bd92830 FM |
488 | } |
489 | ||
8b4e9578 FM |
490 | if(realt) { |
491 | realtime(); | |
492 | exit(EXIT_SUCCESS); | |
493 | } | |
9bd92830 | 494 | if(split) { |
6068ae56 FM |
495 | const char *file; |
496 | ||
497 | FIter=FileListIter_Open(AccessLog); | |
498 | while ((file=FileListIter_Next(FIter))!=NULL) | |
499 | splitlog(file, df, dfrom, duntil, convert, splitprefix); | |
500 | FileListIter_Close(FIter); | |
9bd92830 FM |
501 | exit(EXIT_SUCCESS); |
502 | } | |
503 | if(convert) { | |
6068ae56 FM |
504 | const char *file; |
505 | ||
506 | FIter=FileListIter_Open(AccessLog); | |
507 | while ((file=FileListIter_Next(FIter))!=NULL) | |
508 | convlog(file, df, dfrom, duntil); | |
509 | FileListIter_Close(FIter); | |
9bd92830 FM |
510 | exit(EXIT_SUCCESS); |
511 | } | |
512 | ||
513 | load_excludecodes(ExcludeCodes); | |
514 | ||
515 | if(access(PasswdFile, R_OK) == 0) { | |
516 | getusers(PasswdFile,debug); | |
27d1fa35 FM |
517 | ReadFilter.SysUsers=true; |
518 | } else { | |
519 | ReadFilter.SysUsers=false; | |
9bd92830 FM |
520 | } |
521 | ||
522 | if(hexclude[0] == '\0') | |
523 | strcpy(hexclude,ExcludeHosts); | |
524 | if(hexclude[0] != '\0') { | |
525 | gethexclude(hexclude,debug); | |
27d1fa35 FM |
526 | ReadFilter.HostFilter=true; |
527 | } else { | |
528 | ReadFilter.HostFilter=false; | |
9bd92830 FM |
529 | } |
530 | ||
531 | if(ReportType == 0) { | |
532 | ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES | | |
007905af | 533 | REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED | |
34ad8e64 FM |
534 | REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | |
535 | REPORT_TYPE_DOWNLOADS | REPORT_TYPE_USERAGENT; | |
9bd92830 | 536 | } |
34ad8e64 FM |
537 | if (!FileList_IsEmpty(UserAgentLog)) |
538 | ReportType|=REPORT_TYPE_USERAGENT; | |
9bd92830 FM |
539 | |
540 | if(access(ExcludeUsers, R_OK) == 0) { | |
541 | getuexclude(ExcludeUsers,debug); | |
27d1fa35 FM |
542 | ReadFilter.UserFilter=true; |
543 | } else { | |
544 | ReadFilter.UserFilter=false; | |
9bd92830 | 545 | } |
22715352 FM |
546 | if (HostAliasFile[0] != '\0') |
547 | read_hostalias(HostAliasFile); | |
c4f0ea8f FM |
548 | if (UserAliasFile[0] != '\0') |
549 | read_useralias(UserAliasFile); | |
9bd92830 | 550 | |
6588b137 | 551 | indexonly=false; |
27d1fa35 | 552 | if(ReadFilter.UserFilter) { |
9bd92830 | 553 | if(is_indexonly()) |
6588b137 | 554 | indexonly=true; |
9bd92830 | 555 | } |
6588b137 FM |
556 | if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true; |
557 | if(Index == INDEX_ONLY) indexonly=true; | |
9bd92830 | 558 | |
27d1fa35 FM |
559 | if(MaxElapsed[0] != '\0') |
560 | ReadFilter.max_elapsed=atol(MaxElapsed); | |
561 | else | |
562 | ReadFilter.max_elapsed=0; | |
9bd92830 | 563 | |
9bd92830 FM |
564 | if(tmp[0] == '\0') strcpy(tmp,TempDir); |
565 | else strcpy(TempDir,tmp); | |
566 | /* | |
567 | For historical reasons, the temporary directory is the subdirectory "sarg" of the path | |
568 | provided by the user. | |
569 | */ | |
570 | strcat(tmp,"/sarg"); | |
571 | ||
572 | if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) { | |
af961877 | 573 | debuga(__FILE__,__LINE__,_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp); |
9bd92830 FM |
574 | exit(EXIT_FAILURE); |
575 | } | |
576 | ||
9bd92830 FM |
577 | if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail); |
578 | ||
579 | if(email[0] != '\0') { | |
580 | my_mkdir(tmp); | |
581 | strcpy(outdir,tmp); | |
582 | strcat(outdir,"/"); | |
583 | } | |
584 | ||
585 | if(access(tmp, R_OK) == 0) { | |
af961877 | 586 | if (debug) debuga(__FILE__,__LINE__,_("Deleting temporary directory \"%s\"\n"),tmp); |
170a77ea | 587 | emptytmpdir(tmp); |
9bd92830 FM |
588 | } |
589 | my_mkdir(tmp); | |
9bd92830 FM |
590 | |
591 | if(debug) { | |
6068ae56 FM |
592 | const char *file; |
593 | ||
af961877 FM |
594 | debuga(__FILE__,__LINE__,_("Parameters:\n")); |
595 | debuga(__FILE__,__LINE__,_(" Hostname or IP address (-a) = %s\n"),addr); | |
137eb63d FM |
596 | FIter=FileListIter_Open(UserAgentLog); |
597 | while ((file=FileListIter_NextWithMask(FIter))!=NULL) | |
598 | debuga(__FILE__,__LINE__,_(" Useragent log (-b) = %s\n"),file); | |
599 | FileListIter_Close(FIter); | |
af961877 FM |
600 | debuga(__FILE__,__LINE__,_(" Exclude file (-c) = %s\n"),hexclude); |
601 | debuga(__FILE__,__LINE__,_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange); | |
602 | debuga(__FILE__,__LINE__,_(" Email address to send reports (-e) = %s\n"),email); | |
603 | debuga(__FILE__,__LINE__,_(" Config file (-f) = %s\n"),ConfigFile); | |
81a022d8 | 604 | if (df=='e') |
af961877 | 605 | debuga(__FILE__,__LINE__,_(" Date format (-g) = Europe (dd/mm/yyyy)\n")); |
81a022d8 | 606 | else if (df=='u') |
af961877 | 607 | debuga(__FILE__,__LINE__,_(" Date format (-g) = USA (mm/dd/yyyy)\n")); |
81a022d8 | 608 | else if (df=='w') |
af961877 FM |
609 | debuga(__FILE__,__LINE__,_(" Date format (-g) = Sites & Users (yyyy/ww)\n")); |
610 | debuga(__FILE__,__LINE__,_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No")); | |
611 | debuga(__FILE__,__LINE__,_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No")); | |
6068ae56 FM |
612 | FIter=FileListIter_Open(AccessLog); |
613 | while ((file=FileListIter_NextWithMask(FIter))!=NULL) | |
af961877 | 614 | debuga(__FILE__,__LINE__,_(" Input log (-l) = %s\n"),file); |
6068ae56 | 615 | FileListIter_Close(FIter); |
9bd92830 | 616 | for (iarq=0 ; iarq<NRedirectorLogs ; iarq++) |
af961877 FM |
617 | debuga(__FILE__,__LINE__,_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]); |
618 | debuga(__FILE__,__LINE__,_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No")); | |
619 | debuga(__FILE__,__LINE__,_(" Output dir (-o) = %s\n"),outdir); | |
620 | debuga(__FILE__,__LINE__,_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No")); | |
621 | debuga(__FILE__,__LINE__,_(" Accessed site (-s) = %s\n"),site); | |
622 | debuga(__FILE__,__LINE__,_(" Time (-t) = %s\n"),hm_str); | |
623 | debuga(__FILE__,__LINE__,_(" User (-u) = %s\n"),us); | |
624 | debuga(__FILE__,__LINE__,_(" Temporary dir (-w) = %s\n"),tmp); | |
625 | debuga(__FILE__,__LINE__,_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No")); | |
626 | debuga(__FILE__,__LINE__,_(" Process messages (-z) = %d\n"),debugz); | |
627 | debuga(__FILE__,__LINE__,_(" Previous reports to keep (--lastlog) = %d\n"),LastLog); | |
628 | debuga(__FILE__,__LINE__,"\n"); | |
9bd92830 FM |
629 | } |
630 | ||
9bd92830 | 631 | if(debug) |
af961877 | 632 | debuga(__FILE__,__LINE__,_("sarg version: %s\n"),VERSION); |
25697a35 | 633 | |
04a01ed3 | 634 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
af961877 | 635 | debuga(__FILE__,__LINE__,_("Sarg compiled to report warnings if the output is inconsistent\n")); |
04a01ed3 FM |
636 | #endif |
637 | ||
e112fa1f | 638 | #ifdef HAVE_RLIM_T |
9bd92830 FM |
639 | if (Ulimit[0] != '\0') { |
640 | struct rlimit rl; | |
641 | long l1, l2; | |
642 | int rc=0; | |
243a9fab | 643 | |
25697a35 | 644 | #if defined(RLIMIT_NOFILE) |
9bd92830 | 645 | getrlimit (RLIMIT_NOFILE, &rl); |
ed1ac52f | 646 | #elif defined(RLIMIT_OFILE) |
9bd92830 | 647 | getrlimit (RLIMIT_OFILE, &rl); |
243a9fab FM |
648 | #else |
649 | #warning "No rlimit resource for the number of open files" | |
25697a35 | 650 | #endif |
9bd92830 FM |
651 | l1 = rl.rlim_cur; |
652 | l2 = rl.rlim_max; | |
d6e703cc | 653 | |
9bd92830 FM |
654 | rl.rlim_cur = atol(Ulimit); |
655 | rl.rlim_max = atol(Ulimit); | |
243a9fab | 656 | #if defined(RLIMIT_NOFILE) |
9bd92830 | 657 | rc=setrlimit (RLIMIT_NOFILE, &rl); |
243a9fab | 658 | #elif defined(RLIMIT_OFILE) |
9bd92830 | 659 | rc=setrlimit (RLIMIT_OFILE, &rl); |
243a9fab FM |
660 | #else |
661 | #warning "No rlimit resource for the number of open files" | |
662 | #endif | |
9bd92830 | 663 | if(rc == -1) { |
96dadc9f | 664 | debuga(_("setrlimit error: %s\n"),strerror(errno)); |
9bd92830 | 665 | } |
25697a35 | 666 | |
9bd92830 | 667 | if(debug) |
c4633554 | 668 | 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 | 669 | } |
e112fa1f | 670 | #endif |
25697a35 | 671 | |
0459449f | 672 | read_start_time=time(NULL); |
27d1fa35 | 673 | LogStatus=ReadLogFile(&ReadFilter); |
0459449f FM |
674 | read_end_time=time(NULL); |
675 | read_elapsed=(double)read_end_time-(double)read_start_time; | |
9bd92830 | 676 | |
6068ae56 | 677 | FileList_Destroy(&AccessLog); |
9bd92830 FM |
678 | free_download(); |
679 | free_excludecodes(); | |
680 | free_exclude(); | |
681 | ||
c4a702b4 FM |
682 | if (debug) { |
683 | char date0[30], date1[30]; | |
684 | struct tm Start,End; | |
685 | ||
b6fb8c79 FM |
686 | if (GetLogPeriod(&Start,&End)) { |
687 | strftime(date0,sizeof(date0),"%x",&Start); | |
688 | strftime(date1,sizeof(date1),"%x",&End); | |
689 | // TRANSLATORS: The %s are the start and end dates in locale format. | |
690 | debuga(__FILE__,__LINE__,_("Period covered by log files: %s-%s\n"),date0,date1); | |
691 | } | |
c4a702b4 FM |
692 | } |
693 | ||
27d1fa35 | 694 | if (!LogStatus){ |
af961877 FM |
695 | debuga(__FILE__,__LINE__,_("No records found\n")); |
696 | debuga(__FILE__,__LINE__,_("End\n")); | |
9bd92830 FM |
697 | userinfo_free(); |
698 | if(userfile) free(userfile); | |
699 | close_usertab(); | |
700 | exit(EXIT_SUCCESS); | |
701 | } | |
702 | ||
6a943fc1 | 703 | if (debug) { |
9bd92830 FM |
704 | char date0[30], date1[30]; |
705 | ||
6a943fc1 FM |
706 | strftime(date0,sizeof(date0),"%x",&period.start); |
707 | strftime(date1,sizeof(date1),"%x",&period.end); | |
708 | // TRANSLATORS: The %s are the start and end dates in locale format. | |
af961877 | 709 | debuga(__FILE__,__LINE__,_("Period extracted from log files: %s-%s\n"),date0,date1); |
5cbbe9a4 FM |
710 | } |
711 | if (ReadFilter.DateRange[0] != '\0') { | |
9bd92830 FM |
712 | getperiod_fromrange(&period,dfrom,duntil); |
713 | } | |
714 | if (getperiod_buildtext(&period)<0) { | |
af961877 | 715 | debuga(__FILE__,__LINE__,_("Failed to build the string representation of the date range\n")); |
9bd92830 FM |
716 | exit(EXIT_FAILURE); |
717 | } | |
718 | ||
0459449f | 719 | process_start_time=time(NULL); |
9bd92830 FM |
720 | if(DataFile[0] != '\0') |
721 | data_file(tmp); | |
722 | else | |
723 | gerarel(); | |
0459449f FM |
724 | process_end_time=time(NULL); |
725 | process_elapsed=(double)process_end_time-(double)process_start_time; | |
9bd92830 | 726 | |
60b48ae5 | 727 | denied_cleanup(); |
16b013cc | 728 | authfail_cleanup(); |
11284535 | 729 | download_cleanup(); |
9bd92830 | 730 | |
11767c6a | 731 | if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) { |
9bd92830 FM |
732 | unlinkdir(tmp,0); |
733 | } | |
734 | ||
0326d73b | 735 | ip2name_cleanup(); |
0a6722a1 | 736 | free_hostalias(); |
c4f0ea8f | 737 | free_useralias(); |
9bd92830 FM |
738 | userinfo_free(); |
739 | if(userfile) | |
740 | free(userfile); | |
741 | close_usertab(); | |
137eb63d | 742 | FileList_Destroy(&UserAgentLog); |
9bd92830 | 743 | |
0459449f FM |
744 | end_time=time(NULL); |
745 | ||
746 | if (show_statis) { | |
747 | double elapsed=(double)end_time-(double)start_time; | |
af961877 | 748 | debuga(__FILE__,__LINE__,_("Total execution time: %.0lf seconds\n"),elapsed); |
0459449f | 749 | if (read_elapsed>0.) { |
af961877 | 750 | debuga(__FILE__,__LINE__,_("Lines read: %lu lines in %.0lf seconds (%.0lf lines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_elapsed); |
0459449f FM |
751 | } |
752 | if (process_elapsed>0.) { | |
af961877 FM |
753 | debuga(__FILE__,__LINE__,_("Processed records: %lu records in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process_elapsed); |
754 | debuga(__FILE__,__LINE__,_("Users: %lu users in %.0lf seconds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed); | |
0459449f FM |
755 | } |
756 | } | |
757 | ||
9bd92830 | 758 | if(debug) |
af961877 | 759 | debuga(__FILE__,__LINE__,_("End\n")); |
9bd92830 FM |
760 | |
761 | exit(EXIT_SUCCESS); | |
25697a35 GS |
762 | } |
763 | ||
764 | ||
936c9905 | 765 | static void getusers(const char *pwdfile, int debug) |
25697a35 | 766 | { |
9bd92830 FM |
767 | FILE *fp_usr; |
768 | char buf[255]; | |
769 | char *str; | |
770 | long int nreg=0; | |
771 | ||
772 | if(debug) | |
af961877 | 773 | debuga(__FILE__,__LINE__,_("Loading password file \"%s\"\n"),pwdfile); |
9bd92830 FM |
774 | |
775 | if ((fp_usr = fopen(pwdfile, "r")) == NULL) { | |
af961877 | 776 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),pwdfile,strerror(errno)); |
9bd92830 FM |
777 | exit(EXIT_FAILURE); |
778 | } | |
779 | ||
780 | if (fseek(fp_usr, 0, SEEK_END)==-1) { | |
af961877 | 781 | debuga(__FILE__,__LINE__,_("Failed to move till the end of file \"%s\": %s\n"),pwdfile,strerror(errno)); |
9bd92830 FM |
782 | exit(EXIT_FAILURE); |
783 | } | |
784 | nreg = ftell(fp_usr); | |
785 | if (nreg<0) { | |
af961877 | 786 | debuga(__FILE__,__LINE__,_("Cannot get the size of file \"%s\"\n"),pwdfile); |
9bd92830 FM |
787 | exit(EXIT_FAILURE); |
788 | } | |
789 | nreg = nreg+5000; | |
790 | if (fseek(fp_usr, 0, SEEK_SET)==-1) { | |
af961877 | 791 | debuga(__FILE__,__LINE__,_("Failed to rewind file \"%s\": %s\n"),pwdfile,strerror(errno)); |
9bd92830 FM |
792 | exit(EXIT_FAILURE); |
793 | } | |
794 | ||
795 | if((userfile=(char *) malloc(nreg))==NULL){ | |
af961877 | 796 | debuga(__FILE__,__LINE__,_("malloc error (%ld bytes required)\n"),nreg); |
9bd92830 FM |
797 | exit(EXIT_FAILURE); |
798 | } | |
799 | ||
9c1a94f6 | 800 | memset(userfile,0,nreg); |
9bd92830 FM |
801 | strcpy(userfile,":"); |
802 | ||
803 | while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { | |
804 | str=strchr(buf,':'); | |
805 | if (!str) { | |
af961877 | 806 | debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),pwdfile); |
9bd92830 FM |
807 | exit(EXIT_FAILURE); |
808 | } | |
809 | str[1]='\0'; | |
810 | strcat(userfile,buf); | |
811 | } | |
812 | ||
204781f4 | 813 | if (fclose(fp_usr)==EOF) { |
af961877 | 814 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),pwdfile,strerror(errno)); |
204781f4 FM |
815 | exit(EXIT_FAILURE); |
816 | } | |
9bd92830 FM |
817 | |
818 | return; | |
25697a35 | 819 | } |