]>
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; | |
dc6fba8b | 252 | SiteUsersReportLimit=0; |
9bd92830 FM |
253 | AuthfailReportLimit=10; |
254 | DansGuardianReportLimit=10; | |
255 | SquidGuardReportLimit=10; | |
256 | DownloadReportLimit=50; | |
257 | UserReportLimit=0; | |
258 | debug=0; | |
259 | debugz=0; | |
9bd92830 FM |
260 | iprel=false; |
261 | userip=false; | |
262 | realt=false; | |
263 | realtime_refresh=3; | |
264 | realtime_access_log_lines=1000; | |
265 | cost=0.01; | |
266 | nocost=50000000; | |
9bd92830 FM |
267 | squid24=false; |
268 | dfrom=0; | |
269 | duntil=0; | |
11767c6a | 270 | KeepTempLog=false; |
4d62cb0a FM |
271 | NumLogSuccessiveErrors=3; |
272 | NumLogTotalErrors=50; | |
0459449f FM |
273 | lines_read=0UL; |
274 | records_kept=0UL; | |
275 | nusers=0UL; | |
9bd92830 | 276 | |
9c1a94f6 FM |
277 | memset(IncludeUsers,0,sizeof(IncludeUsers)); |
278 | memset(ExcludeString,0,sizeof(ExcludeString)); | |
9bd92830 FM |
279 | memset(&period,0,sizeof(period)); |
280 | ||
9bd92830 FM |
281 | AccessLogFromCmdLine=0; |
282 | RedirectorLogFromCmdLine=0; | |
283 | ||
284 | strcpy(Title,_("Squid User Access Report")); | |
285 | ||
4246ae8d | 286 | while((ch = getopt_long(argc, argv, "a:b:c:d:e:f:g:hikl:L:no:P:prs:t:u:Vw:xyz",long_options,&option_index)) != -1){ |
9bd92830 FM |
287 | switch(ch) |
288 | { | |
289 | case 0: | |
290 | break; | |
291 | case 2: | |
292 | lastlog=atoi(optarg); | |
293 | break; | |
294 | case 3: | |
295 | lastlog=0; | |
296 | break; | |
297 | case 'a': | |
a87d4d11 | 298 | safe_strcpy(addr,optarg,sizeof(addr)); |
9bd92830 | 299 | break; |
d6f08599 | 300 | case 'b': //unused option |
137eb63d FM |
301 | UserAgentFromCmdLine=true; |
302 | if (!UserAgentLog) | |
303 | UserAgentLog=FileList_Create(); | |
304 | if (!FileList_AddFile(UserAgentLog,optarg)) { | |
305 | debuga(__FILE__,__LINE__,_("Not enough memory to store a user agent file name\n")); | |
306 | exit(EXIT_FAILURE); | |
307 | } | |
9bd92830 FM |
308 | break; |
309 | case 'c': | |
a87d4d11 | 310 | safe_strcpy(hexclude,optarg,sizeof(hexclude)); |
9bd92830 FM |
311 | break; |
312 | case 'd': | |
27d1fa35 | 313 | safe_strcpy(ReadFilter.DateRange,optarg,sizeof(ReadFilter.DateRange)); |
60ec7f09 | 314 | date_from(ReadFilter.DateRange,sizeof(ReadFilter.DateRange), &dfrom, &duntil); |
9bd92830 FM |
315 | break; |
316 | case 'e': | |
a87d4d11 | 317 | safe_strcpy(email,optarg,sizeof(email)); |
9bd92830 FM |
318 | break; |
319 | case 'f': | |
a87d4d11 | 320 | safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile)); |
9bd92830 FM |
321 | break; |
322 | case 'g': | |
81a022d8 | 323 | df=*optarg; |
9bd92830 FM |
324 | break; |
325 | case 'h': | |
326 | usage(argv[0]); | |
327 | exit(EXIT_SUCCESS); | |
328 | case 'i': | |
329 | iprel=true; | |
330 | break; | |
11767c6a FM |
331 | case 'k': |
332 | KeepTempLog=true; | |
333 | break; | |
9bd92830 | 334 | case 'l': |
6068ae56 FM |
335 | if (!AccessLog) |
336 | AccessLog=FileList_Create(); | |
337 | if (!FileList_AddFile(AccessLog,optarg)) { | |
af961877 | 338 | debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n")); |
9bd92830 FM |
339 | exit(EXIT_FAILURE); |
340 | } | |
9bd92830 FM |
341 | AccessLogFromCmdLine++; |
342 | break; | |
343 | case 'L': | |
344 | if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) { | |
af961877 | 345 | debuga(__FILE__,__LINE__,_("Too many redirector logs passed on command line with option -L.\n")); |
9bd92830 FM |
346 | exit(EXIT_FAILURE); |
347 | } | |
348 | if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) { | |
af961877 | 349 | debuga(__FILE__,__LINE__,_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg); |
9bd92830 FM |
350 | exit(EXIT_FAILURE); |
351 | } | |
352 | strcpy(RedirectorLogs[NRedirectorLogs],optarg); | |
353 | NRedirectorLogs++; | |
354 | RedirectorLogFromCmdLine++; | |
355 | break; | |
9bd92830 FM |
356 | case 'n': |
357 | dns=true; | |
358 | break; | |
359 | case 'o': | |
a87d4d11 | 360 | safe_strcpy(outdir,optarg,sizeof(outdir)); |
9bd92830 FM |
361 | break; |
362 | case 'p': | |
363 | userip=true; | |
364 | break; | |
2c7e8c23 | 365 | case 'P': |
a87d4d11 | 366 | safe_strcpy(splitprefix,optarg,sizeof(splitprefix)); |
2c7e8c23 | 367 | break; |
9bd92830 FM |
368 | case 'r': |
369 | realt=true; | |
370 | break; | |
371 | case 's': | |
a87d4d11 | 372 | safe_strcpy(site,optarg,sizeof(site)); |
9bd92830 FM |
373 | break; |
374 | case 't': | |
375 | { | |
376 | int h1,m1,h2,m2; | |
377 | ||
378 | if(strstr(optarg,"-") == 0) { | |
379 | if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) { | |
af961877 | 380 | debuga(__FILE__,__LINE__,_("Time period passed on the command line with option -t must be HH:MM\n")); |
9bd92830 FM |
381 | exit(EXIT_FAILURE); |
382 | } | |
27d1fa35 FM |
383 | ReadFilter.StartTime=h1*100+m1; |
384 | ReadFilter.EndTime=ReadFilter.StartTime; | |
9bd92830 FM |
385 | snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1); |
386 | } else { | |
387 | if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) { | |
af961877 | 388 | debuga(__FILE__,__LINE__,_("Time range passed on the command line with option -t must be HH:MM-HH:MM\n")); |
9bd92830 FM |
389 | exit(EXIT_FAILURE); |
390 | } | |
27d1fa35 FM |
391 | ReadFilter.StartTime=h1*100+m1; |
392 | ReadFilter.EndTime=h2*100+m2; | |
9bd92830 FM |
393 | snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2); |
394 | } | |
395 | break; | |
396 | } | |
397 | case 'u': | |
a87d4d11 | 398 | safe_strcpy(us,optarg,sizeof(us)); |
9bd92830 | 399 | break; |
4f22e788 | 400 | case 'V': |
d6d7c780 | 401 | show_version=1; |
9bd92830 FM |
402 | break; |
403 | case 'w': | |
a87d4d11 | 404 | safe_strcpy(tmp,optarg,sizeof(tmp)); |
9bd92830 FM |
405 | break; |
406 | case 'x': | |
407 | debug++; | |
408 | break; | |
d6f08599 | 409 | case 'y': //unused option |
9bd92830 FM |
410 | langcode++; |
411 | break; | |
412 | case 'z': | |
413 | debugz++; | |
414 | break; | |
007905af | 415 | case ':': |
af961877 | 416 | debuga(__FILE__,__LINE__,_("Option -%c requires an argument\n"),optopt); |
007905af | 417 | exit(EXIT_FAILURE); |
9bd92830 FM |
418 | case '?': |
419 | usage(argv[0]); | |
420 | exit(EXIT_FAILURE); | |
421 | default: | |
422 | abort(); | |
423 | } | |
424 | } | |
425 | ||
426 | if (errflg>0) { | |
427 | usage(argv[0]); | |
428 | exit(2); | |
429 | } | |
d6d7c780 FM |
430 | if (show_version) { |
431 | version(); | |
432 | } | |
9bd92830 | 433 | |
6068ae56 FM |
434 | if(output_css) { |
435 | css_content(stdout); | |
436 | exit(EXIT_SUCCESS); | |
437 | } | |
438 | ||
9bd92830 | 439 | if (optind<argc) { |
6068ae56 FM |
440 | if (!AccessLog) |
441 | AccessLog=FileList_Create(); | |
9bd92830 | 442 | for (iarq=optind ; iarq<argc ; iarq++) { |
6068ae56 | 443 | if (!FileList_AddFile(AccessLog,argv[iarq])) { |
af961877 | 444 | debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n")); |
9bd92830 FM |
445 | exit(EXIT_FAILURE); |
446 | } | |
9bd92830 FM |
447 | AccessLogFromCmdLine++; |
448 | } | |
449 | } | |
450 | ||
af961877 | 451 | if(debug) debuga(__FILE__,__LINE__,_("Init\n")); |
9bd92830 FM |
452 | |
453 | if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR); | |
454 | if(access(ConfigFile, R_OK) != 0) { | |
af961877 | 455 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ConfigFile,strerror(errno)); |
9bd92830 FM |
456 | exit(EXIT_FAILURE); |
457 | } | |
458 | ||
459 | if(access(ConfigFile, R_OK) == 0) | |
da94b2c7 | 460 | getconf(ConfigFile); |
9bd92830 FM |
461 | |
462 | if(userip) UserIp=true; | |
463 | ||
51b166d4 | 464 | if(dns) ip2name_forcedns(); |
9bd92830 FM |
465 | |
466 | if (lastlog>=0) LastLog=lastlog; | |
467 | ||
2c7e8c23 FM |
468 | if(outdir[0] == '\0') strcpy(outdir,OutputDir); |
469 | if(outdir[0] != '\0') strcat(outdir,"/"); | |
470 | ||
9bd92830 FM |
471 | if(IndexTree == INDEX_TREE_FILE) |
472 | strcpy(ImageFile,"../images"); | |
473 | else | |
474 | strcpy(ImageFile,"../../../images"); | |
475 | ||
11284535 | 476 | dataonly=(DataFile[0] != '\0'); |
9bd92830 | 477 | |
81a022d8 FM |
478 | if (df=='\0') df=DateFormat; |
479 | if (df=='\0') df='u'; | |
480 | if (df=='w') | |
3e80818c FM |
481 | IndexTree=INDEX_TREE_FILE; |
482 | ||
6068ae56 FM |
483 | if(AccessLog==NULL) { |
484 | AccessLog=FileList_Create(); | |
485 | if (!FileList_AddFile(AccessLog,"/var/log/squid/access.log")) { | |
af961877 | 486 | debuga(__FILE__,__LINE__,_("Not enough memory to store the input log file names\n")); |
6068ae56 FM |
487 | exit(EXIT_FAILURE); |
488 | } | |
9bd92830 FM |
489 | } |
490 | ||
8b4e9578 FM |
491 | if(realt) { |
492 | realtime(); | |
493 | exit(EXIT_SUCCESS); | |
494 | } | |
9bd92830 | 495 | if(split) { |
6068ae56 FM |
496 | const char *file; |
497 | ||
498 | FIter=FileListIter_Open(AccessLog); | |
499 | while ((file=FileListIter_Next(FIter))!=NULL) | |
500 | splitlog(file, df, dfrom, duntil, convert, splitprefix); | |
501 | FileListIter_Close(FIter); | |
9bd92830 FM |
502 | exit(EXIT_SUCCESS); |
503 | } | |
504 | if(convert) { | |
6068ae56 FM |
505 | const char *file; |
506 | ||
507 | FIter=FileListIter_Open(AccessLog); | |
508 | while ((file=FileListIter_Next(FIter))!=NULL) | |
509 | convlog(file, df, dfrom, duntil); | |
510 | FileListIter_Close(FIter); | |
9bd92830 FM |
511 | exit(EXIT_SUCCESS); |
512 | } | |
513 | ||
514 | load_excludecodes(ExcludeCodes); | |
515 | ||
516 | if(access(PasswdFile, R_OK) == 0) { | |
517 | getusers(PasswdFile,debug); | |
27d1fa35 FM |
518 | ReadFilter.SysUsers=true; |
519 | } else { | |
520 | ReadFilter.SysUsers=false; | |
9bd92830 FM |
521 | } |
522 | ||
523 | if(hexclude[0] == '\0') | |
524 | strcpy(hexclude,ExcludeHosts); | |
525 | if(hexclude[0] != '\0') { | |
526 | gethexclude(hexclude,debug); | |
27d1fa35 FM |
527 | ReadFilter.HostFilter=true; |
528 | } else { | |
529 | ReadFilter.HostFilter=false; | |
9bd92830 FM |
530 | } |
531 | ||
532 | if(ReportType == 0) { | |
533 | ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES | | |
007905af | 534 | REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED | |
34ad8e64 FM |
535 | REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | |
536 | REPORT_TYPE_DOWNLOADS | REPORT_TYPE_USERAGENT; | |
9bd92830 | 537 | } |
34ad8e64 FM |
538 | if (!FileList_IsEmpty(UserAgentLog)) |
539 | ReportType|=REPORT_TYPE_USERAGENT; | |
9bd92830 FM |
540 | |
541 | if(access(ExcludeUsers, R_OK) == 0) { | |
542 | getuexclude(ExcludeUsers,debug); | |
27d1fa35 FM |
543 | ReadFilter.UserFilter=true; |
544 | } else { | |
545 | ReadFilter.UserFilter=false; | |
9bd92830 | 546 | } |
22715352 FM |
547 | if (HostAliasFile[0] != '\0') |
548 | read_hostalias(HostAliasFile); | |
c4f0ea8f FM |
549 | if (UserAliasFile[0] != '\0') |
550 | read_useralias(UserAliasFile); | |
9bd92830 | 551 | |
6588b137 | 552 | indexonly=false; |
27d1fa35 | 553 | if(ReadFilter.UserFilter) { |
9bd92830 | 554 | if(is_indexonly()) |
6588b137 | 555 | indexonly=true; |
9bd92830 | 556 | } |
6588b137 FM |
557 | if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true; |
558 | if(Index == INDEX_ONLY) indexonly=true; | |
9bd92830 | 559 | |
27d1fa35 FM |
560 | if(MaxElapsed[0] != '\0') |
561 | ReadFilter.max_elapsed=atol(MaxElapsed); | |
562 | else | |
563 | ReadFilter.max_elapsed=0; | |
9bd92830 | 564 | |
9bd92830 FM |
565 | if(tmp[0] == '\0') strcpy(tmp,TempDir); |
566 | else strcpy(TempDir,tmp); | |
567 | /* | |
568 | For historical reasons, the temporary directory is the subdirectory "sarg" of the path | |
569 | provided by the user. | |
570 | */ | |
571 | strcat(tmp,"/sarg"); | |
572 | ||
573 | if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) { | |
af961877 | 574 | debuga(__FILE__,__LINE__,_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp); |
9bd92830 FM |
575 | exit(EXIT_FAILURE); |
576 | } | |
577 | ||
9bd92830 FM |
578 | if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail); |
579 | ||
580 | if(email[0] != '\0') { | |
581 | my_mkdir(tmp); | |
582 | strcpy(outdir,tmp); | |
583 | strcat(outdir,"/"); | |
584 | } | |
585 | ||
586 | if(access(tmp, R_OK) == 0) { | |
af961877 | 587 | if (debug) debuga(__FILE__,__LINE__,_("Deleting temporary directory \"%s\"\n"),tmp); |
170a77ea | 588 | emptytmpdir(tmp); |
9bd92830 FM |
589 | } |
590 | my_mkdir(tmp); | |
9bd92830 FM |
591 | |
592 | if(debug) { | |
6068ae56 FM |
593 | const char *file; |
594 | ||
af961877 FM |
595 | debuga(__FILE__,__LINE__,_("Parameters:\n")); |
596 | debuga(__FILE__,__LINE__,_(" Hostname or IP address (-a) = %s\n"),addr); | |
137eb63d FM |
597 | FIter=FileListIter_Open(UserAgentLog); |
598 | while ((file=FileListIter_NextWithMask(FIter))!=NULL) | |
599 | debuga(__FILE__,__LINE__,_(" Useragent log (-b) = %s\n"),file); | |
600 | FileListIter_Close(FIter); | |
af961877 FM |
601 | debuga(__FILE__,__LINE__,_(" Exclude file (-c) = %s\n"),hexclude); |
602 | debuga(__FILE__,__LINE__,_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange); | |
603 | debuga(__FILE__,__LINE__,_(" Email address to send reports (-e) = %s\n"),email); | |
604 | debuga(__FILE__,__LINE__,_(" Config file (-f) = %s\n"),ConfigFile); | |
81a022d8 | 605 | if (df=='e') |
af961877 | 606 | debuga(__FILE__,__LINE__,_(" Date format (-g) = Europe (dd/mm/yyyy)\n")); |
81a022d8 | 607 | else if (df=='u') |
af961877 | 608 | debuga(__FILE__,__LINE__,_(" Date format (-g) = USA (mm/dd/yyyy)\n")); |
81a022d8 | 609 | else if (df=='w') |
af961877 FM |
610 | debuga(__FILE__,__LINE__,_(" Date format (-g) = Sites & Users (yyyy/ww)\n")); |
611 | debuga(__FILE__,__LINE__,_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No")); | |
612 | debuga(__FILE__,__LINE__,_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No")); | |
6068ae56 FM |
613 | FIter=FileListIter_Open(AccessLog); |
614 | while ((file=FileListIter_NextWithMask(FIter))!=NULL) | |
af961877 | 615 | debuga(__FILE__,__LINE__,_(" Input log (-l) = %s\n"),file); |
6068ae56 | 616 | FileListIter_Close(FIter); |
9bd92830 | 617 | for (iarq=0 ; iarq<NRedirectorLogs ; iarq++) |
af961877 FM |
618 | debuga(__FILE__,__LINE__,_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]); |
619 | debuga(__FILE__,__LINE__,_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No")); | |
620 | debuga(__FILE__,__LINE__,_(" Output dir (-o) = %s\n"),outdir); | |
621 | debuga(__FILE__,__LINE__,_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No")); | |
622 | debuga(__FILE__,__LINE__,_(" Accessed site (-s) = %s\n"),site); | |
623 | debuga(__FILE__,__LINE__,_(" Time (-t) = %s\n"),hm_str); | |
624 | debuga(__FILE__,__LINE__,_(" User (-u) = %s\n"),us); | |
625 | debuga(__FILE__,__LINE__,_(" Temporary dir (-w) = %s\n"),tmp); | |
626 | debuga(__FILE__,__LINE__,_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No")); | |
627 | debuga(__FILE__,__LINE__,_(" Process messages (-z) = %d\n"),debugz); | |
628 | debuga(__FILE__,__LINE__,_(" Previous reports to keep (--lastlog) = %d\n"),LastLog); | |
629 | debuga(__FILE__,__LINE__,"\n"); | |
9bd92830 FM |
630 | } |
631 | ||
9bd92830 | 632 | if(debug) |
af961877 | 633 | debuga(__FILE__,__LINE__,_("sarg version: %s\n"),VERSION); |
25697a35 | 634 | |
04a01ed3 | 635 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
af961877 | 636 | debuga(__FILE__,__LINE__,_("Sarg compiled to report warnings if the output is inconsistent\n")); |
04a01ed3 FM |
637 | #endif |
638 | ||
e112fa1f | 639 | #ifdef HAVE_RLIM_T |
9bd92830 FM |
640 | if (Ulimit[0] != '\0') { |
641 | struct rlimit rl; | |
642 | long l1, l2; | |
643 | int rc=0; | |
243a9fab | 644 | |
25697a35 | 645 | #if defined(RLIMIT_NOFILE) |
9bd92830 | 646 | getrlimit (RLIMIT_NOFILE, &rl); |
ed1ac52f | 647 | #elif defined(RLIMIT_OFILE) |
9bd92830 | 648 | getrlimit (RLIMIT_OFILE, &rl); |
243a9fab FM |
649 | #else |
650 | #warning "No rlimit resource for the number of open files" | |
25697a35 | 651 | #endif |
9bd92830 FM |
652 | l1 = rl.rlim_cur; |
653 | l2 = rl.rlim_max; | |
d6e703cc | 654 | |
9bd92830 FM |
655 | rl.rlim_cur = atol(Ulimit); |
656 | rl.rlim_max = atol(Ulimit); | |
243a9fab | 657 | #if defined(RLIMIT_NOFILE) |
9bd92830 | 658 | rc=setrlimit (RLIMIT_NOFILE, &rl); |
243a9fab | 659 | #elif defined(RLIMIT_OFILE) |
9bd92830 | 660 | rc=setrlimit (RLIMIT_OFILE, &rl); |
243a9fab FM |
661 | #else |
662 | #warning "No rlimit resource for the number of open files" | |
663 | #endif | |
9bd92830 | 664 | if(rc == -1) { |
96dadc9f | 665 | debuga(_("setrlimit error: %s\n"),strerror(errno)); |
9bd92830 | 666 | } |
25697a35 | 667 | |
9bd92830 | 668 | if(debug) |
c4633554 | 669 | debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING"\n",l1,l2,rl.rlim_cur,rl.rlim_max); |
9bd92830 | 670 | } |
e112fa1f | 671 | #endif |
25697a35 | 672 | |
0459449f | 673 | read_start_time=time(NULL); |
27d1fa35 | 674 | LogStatus=ReadLogFile(&ReadFilter); |
0459449f FM |
675 | read_end_time=time(NULL); |
676 | read_elapsed=(double)read_end_time-(double)read_start_time; | |
9bd92830 | 677 | |
6068ae56 | 678 | FileList_Destroy(&AccessLog); |
9bd92830 FM |
679 | free_download(); |
680 | free_excludecodes(); | |
681 | free_exclude(); | |
682 | ||
c4a702b4 FM |
683 | if (debug) { |
684 | char date0[30], date1[30]; | |
685 | struct tm Start,End; | |
686 | ||
b6fb8c79 FM |
687 | if (GetLogPeriod(&Start,&End)) { |
688 | strftime(date0,sizeof(date0),"%x",&Start); | |
689 | strftime(date1,sizeof(date1),"%x",&End); | |
690 | // TRANSLATORS: The %s are the start and end dates in locale format. | |
691 | debuga(__FILE__,__LINE__,_("Period covered by log files: %s-%s\n"),date0,date1); | |
692 | } | |
c4a702b4 FM |
693 | } |
694 | ||
27d1fa35 | 695 | if (!LogStatus){ |
af961877 FM |
696 | debuga(__FILE__,__LINE__,_("No records found\n")); |
697 | debuga(__FILE__,__LINE__,_("End\n")); | |
9bd92830 FM |
698 | userinfo_free(); |
699 | if(userfile) free(userfile); | |
700 | close_usertab(); | |
701 | exit(EXIT_SUCCESS); | |
702 | } | |
703 | ||
6a943fc1 | 704 | if (debug) { |
9bd92830 FM |
705 | char date0[30], date1[30]; |
706 | ||
6a943fc1 FM |
707 | strftime(date0,sizeof(date0),"%x",&period.start); |
708 | strftime(date1,sizeof(date1),"%x",&period.end); | |
709 | // TRANSLATORS: The %s are the start and end dates in locale format. | |
af961877 | 710 | debuga(__FILE__,__LINE__,_("Period extracted from log files: %s-%s\n"),date0,date1); |
5cbbe9a4 FM |
711 | } |
712 | if (ReadFilter.DateRange[0] != '\0') { | |
9bd92830 FM |
713 | getperiod_fromrange(&period,dfrom,duntil); |
714 | } | |
715 | if (getperiod_buildtext(&period)<0) { | |
af961877 | 716 | debuga(__FILE__,__LINE__,_("Failed to build the string representation of the date range\n")); |
9bd92830 FM |
717 | exit(EXIT_FAILURE); |
718 | } | |
719 | ||
0459449f | 720 | process_start_time=time(NULL); |
9bd92830 FM |
721 | if(DataFile[0] != '\0') |
722 | data_file(tmp); | |
723 | else | |
724 | gerarel(); | |
0459449f FM |
725 | process_end_time=time(NULL); |
726 | process_elapsed=(double)process_end_time-(double)process_start_time; | |
9bd92830 | 727 | |
60b48ae5 | 728 | denied_cleanup(); |
16b013cc | 729 | authfail_cleanup(); |
11284535 | 730 | download_cleanup(); |
9bd92830 | 731 | |
11767c6a | 732 | if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) { |
9bd92830 FM |
733 | unlinkdir(tmp,0); |
734 | } | |
735 | ||
0326d73b | 736 | ip2name_cleanup(); |
0a6722a1 | 737 | free_hostalias(); |
c4f0ea8f | 738 | free_useralias(); |
9bd92830 FM |
739 | userinfo_free(); |
740 | if(userfile) | |
741 | free(userfile); | |
742 | close_usertab(); | |
137eb63d | 743 | FileList_Destroy(&UserAgentLog); |
9bd92830 | 744 | |
0459449f FM |
745 | end_time=time(NULL); |
746 | ||
747 | if (show_statis) { | |
748 | double elapsed=(double)end_time-(double)start_time; | |
af961877 | 749 | debuga(__FILE__,__LINE__,_("Total execution time: %.0lf seconds\n"),elapsed); |
0459449f | 750 | if (read_elapsed>0.) { |
af961877 | 751 | 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 |
752 | } |
753 | if (process_elapsed>0.) { | |
af961877 FM |
754 | debuga(__FILE__,__LINE__,_("Processed records: %lu records in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process_elapsed); |
755 | debuga(__FILE__,__LINE__,_("Users: %lu users in %.0lf seconds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed); | |
0459449f FM |
756 | } |
757 | } | |
758 | ||
9bd92830 | 759 | if(debug) |
af961877 | 760 | debuga(__FILE__,__LINE__,_("End\n")); |
9bd92830 FM |
761 | |
762 | exit(EXIT_SUCCESS); | |
25697a35 GS |
763 | } |
764 | ||
765 | ||
936c9905 | 766 | static void getusers(const char *pwdfile, int debug) |
25697a35 | 767 | { |
9bd92830 FM |
768 | FILE *fp_usr; |
769 | char buf[255]; | |
770 | char *str; | |
771 | long int nreg=0; | |
772 | ||
773 | if(debug) | |
af961877 | 774 | debuga(__FILE__,__LINE__,_("Loading password file \"%s\"\n"),pwdfile); |
9bd92830 FM |
775 | |
776 | if ((fp_usr = fopen(pwdfile, "r")) == NULL) { | |
af961877 | 777 | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),pwdfile,strerror(errno)); |
9bd92830 FM |
778 | exit(EXIT_FAILURE); |
779 | } | |
780 | ||
781 | if (fseek(fp_usr, 0, SEEK_END)==-1) { | |
af961877 | 782 | debuga(__FILE__,__LINE__,_("Failed to move till the end of file \"%s\": %s\n"),pwdfile,strerror(errno)); |
9bd92830 FM |
783 | exit(EXIT_FAILURE); |
784 | } | |
785 | nreg = ftell(fp_usr); | |
786 | if (nreg<0) { | |
af961877 | 787 | debuga(__FILE__,__LINE__,_("Cannot get the size of file \"%s\"\n"),pwdfile); |
9bd92830 FM |
788 | exit(EXIT_FAILURE); |
789 | } | |
790 | nreg = nreg+5000; | |
791 | if (fseek(fp_usr, 0, SEEK_SET)==-1) { | |
af961877 | 792 | debuga(__FILE__,__LINE__,_("Failed to rewind file \"%s\": %s\n"),pwdfile,strerror(errno)); |
9bd92830 FM |
793 | exit(EXIT_FAILURE); |
794 | } | |
795 | ||
796 | if((userfile=(char *) malloc(nreg))==NULL){ | |
af961877 | 797 | debuga(__FILE__,__LINE__,_("malloc error (%ld bytes required)\n"),nreg); |
9bd92830 FM |
798 | exit(EXIT_FAILURE); |
799 | } | |
800 | ||
9c1a94f6 | 801 | memset(userfile,0,nreg); |
9bd92830 FM |
802 | strcpy(userfile,":"); |
803 | ||
804 | while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { | |
805 | str=strchr(buf,':'); | |
806 | if (!str) { | |
af961877 | 807 | debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n"),pwdfile); |
9bd92830 FM |
808 | exit(EXIT_FAILURE); |
809 | } | |
810 | str[1]='\0'; | |
811 | strcat(userfile,buf); | |
812 | } | |
813 | ||
204781f4 | 814 | if (fclose(fp_usr)==EOF) { |
af961877 | 815 | debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),pwdfile,strerror(errno)); |
204781f4 FM |
816 | exit(EXIT_FAILURE); |
817 | } | |
9bd92830 FM |
818 | |
819 | return; | |
25697a35 | 820 | } |