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