]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed | 2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
60bf929c | 3 | * 1998, 2009 |
94ff9470 GS |
4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
5 | * | |
25697a35 | 6 | * SARG donations: |
94ff9470 | 7 | * please look at http://sarg.sourceforge.net/donations.php |
25697a35 GS |
8 | * --------------------------------------------------------------------- |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
23 | * | |
24 | */ | |
25 | ||
26 | #include "include/conf.h" | |
5f3cfd1d | 27 | #include "include/defs.h" |
25697a35 | 28 | |
25697a35 GS |
29 | #define REPORT_EVERY_X_LINES 5000 |
30 | ||
31 | ||
32 | char *userfile; | |
33 | char *excludefile; | |
34 | char *excludeuser; | |
35 | ||
25697a35 GS |
36 | numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 }; |
37 | numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, | |
38 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 }; | |
39 | ||
936c9905 | 40 | static void getusers(const char *pwdfile, int debug); |
2357ef77 FM |
41 | static void gethexclude(const char *hexfile, int debug); |
42 | static void getuexclude(const char *uexfile, int debug); | |
25697a35 | 43 | |
25f0793f | 44 | int main(int argc,char *argv[]) |
25697a35 | 45 | { |
076cbab8 FM |
46 | enum isa_col_id { |
47 | ISACOL_Ip, | |
48 | ISACOL_UserName, | |
49 | ISACOL_Date, | |
50 | ISACOL_Time, | |
51 | ISACOL_TimeTaken, | |
52 | ISACOL_Bytes, | |
53 | ISACOL_Uri, | |
54 | ISACOL_Status, | |
55 | ISACOL_Last //last entry of the list ! | |
56 | }; | |
58d7d8fe FM |
57 | enum InputLogFormat { |
58 | ILF_Unknown, | |
59 | ILF_Squid, | |
60 | ILF_Common, | |
61 | ILF_Sarg, | |
62 | ILF_Isa, | |
63 | ILF_Last //last entry of the list ! | |
64 | }; | |
25697a35 | 65 | |
120d768c | 66 | FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL; |
25697a35 GS |
67 | |
68 | char sz_Download_Unsort[ 20000 ] ; | |
69 | FILE * fp_Download_Unsort = NULL ; | |
70 | FILE * fp_Write_User = NULL ; | |
71 | ||
72 | extern int optind; | |
73 | extern int optopt; | |
74 | extern char *optarg; | |
75 | ||
25f0793f | 76 | char data[255]; |
25697a35 | 77 | char elap[255]; |
25697a35 GS |
78 | char ip[MAXLEN]; |
79 | char msg[MAXLEN]; | |
80 | char tam[255]; | |
25f0793f FM |
81 | char fun[MAXLEN]; |
82 | char wuser[MAXLEN]; | |
25697a35 | 83 | char smartfilter[MAXLEN]; |
2357ef77 | 84 | char dia[128]; |
25697a35 GS |
85 | char wdata[128]; |
86 | char mes[30]; | |
87 | char ano[30]; | |
88 | char hora[30]; | |
89 | char wtemp[MAXLEN]; | |
90 | char wtemp2[255]; | |
91 | char date[255]; | |
92 | char arq[255]; | |
93 | char arq_log[255]; | |
9c7c6346 | 94 | char hm[15], hmf[15], hmr[15]; |
25697a35 GS |
95 | int chm=0; |
96 | char uagent[MAXLEN]; | |
97 | char hexclude[MAXLEN]; | |
456d78a5 FM |
98 | char csort[MAXLEN]; |
99 | int cstatus; | |
25697a35 GS |
100 | char tbuf[128]; |
101 | char tbuf2[128]; | |
102 | char zip[20]; | |
103 | char *str; | |
104 | char bufz[MAXLEN]; | |
105 | char bufy[MAXLEN]; | |
06ced858 | 106 | char tmp2[MAXLEN]; |
58d7d8fe FM |
107 | enum InputLogFormat ilf; |
108 | int ilf_count[ILF_Last]; | |
25697a35 | 109 | int ch; |
d6e703cc | 110 | int x, l; |
25697a35 GS |
111 | int errflg=0; |
112 | int puser=0; | |
113 | int fhost=0; | |
114 | int dns=0; | |
115 | int fuser=0; | |
116 | int idata=0; | |
ed1ac52f | 117 | int mindate=0; |
25697a35 GS |
118 | int iarq=0; |
119 | int exstring=0; | |
076cbab8 | 120 | int isa_ncols=0,isa_cols[ISACOL_Last]; |
58d7d8fe | 121 | int from_stdin; |
2357ef77 | 122 | long totregsl=0; |
25697a35 GS |
123 | long totregsg=0; |
124 | long totregsx=0; | |
125 | long totper=0; | |
58d7d8fe | 126 | long int max_elapsed=0; |
25697a35 GS |
127 | time_t tt; |
128 | struct tm *t; | |
7c0b0078 FM |
129 | unsigned long recs1=0UL; |
130 | unsigned long recs2=0UL; | |
25697a35 | 131 | int OutputNonZero = REPORT_EVERY_X_LINES ; |
304a739d FM |
132 | int download_flag; |
133 | char download_url[MAXLEN]; | |
120d768c | 134 | char sz_Last_User[MAXLEN]=""; |
9c7c6346 | 135 | struct getwordstruct gwarea; |
25697a35 GS |
136 | |
137 | BgImage[0]='\0'; | |
138 | LogoImage[0]='\0'; | |
139 | LogoText[0]='\0'; | |
140 | PasswdFile[0]='\0'; | |
141 | OutputEmail[0]='\0'; | |
25697a35 GS |
142 | UserAgentLog[0]='\0'; |
143 | ExcludeHosts[0]='\0'; | |
144 | ExcludeUsers[0]='\0'; | |
145 | ConfigFile[0]='\0'; | |
146 | code[0]='\0'; | |
147 | LastLog[0]='\0'; | |
25697a35 GS |
148 | ReportType[0]='\0'; |
149 | UserTabFile[0]='\0'; | |
150 | BlockIt[0]='\0'; | |
151 | ExternalCSSFile[0]='\0'; | |
152 | SquidGuardLogFormat[0]='\0'; | |
491b862f | 153 | SquidGuardLogAlternate[0]='\0'; |
58d7d8fe | 154 | for (ilf=0 ; ilf<ILF_Last ; ilf++) ilf_count[ilf]=0; |
25697a35 | 155 | |
25697a35 GS |
156 | sprintf(ExcludeCodes,"%s/exclude_codes",SYSCONFDIR); |
157 | strcpy(GraphDaysBytesBarColor,"orange"); | |
491b862f | 158 | strcpy(BgColor,"#ffffff"); |
25697a35 GS |
159 | strcpy(TxColor,"#000000"); |
160 | strcpy(TxBgColor,"lavender"); | |
161 | strcpy(TiColor,"darkblue"); | |
162 | strcpy(Width,"80"); | |
163 | strcpy(Height,"45"); | |
164 | strcpy(LogoTextColor,"#000000"); | |
165 | strcpy(HeaderColor,"darkblue"); | |
491b862f | 166 | strcpy(HeaderBgColor,"#dddddd"); |
94ff9470 | 167 | strcpy(LogoTextColor,"#006699"); |
25697a35 GS |
168 | strcpy(FontSize,"9px"); |
169 | strcpy(TempDir,"/tmp"); | |
d6e703cc | 170 | strcpy(OutputDir,"/var/www/html/squid-reports"); |
246c8489 | 171 | Ip2Name=0; |
25697a35 | 172 | strcpy(DateFormat,"u"); |
e6414a9d FM |
173 | OverwriteReport=0; |
174 | RemoveTempFiles=1; | |
25697a35 GS |
175 | strcpy(ReplaceIndex,"index.html"); |
176 | strcpy(Index,"yes"); | |
177 | strcpy(RecordsWithoutUser,"ip"); | |
e6414a9d | 178 | UseComma=0; |
25697a35 GS |
179 | strcpy(MailUtility,"mailx"); |
180 | strcpy(TopSitesNum,"100"); | |
246c8489 | 181 | UserIp=0; |
25697a35 GS |
182 | strcpy(TopuserSortField,"BYTES"); |
183 | strcpy(UserSortField,"BYTES"); | |
184 | strcpy(TopuserSortOrder,"reverse"); | |
185 | strcpy(UserSortOrder,"reverse"); | |
186 | strcpy(TopsitesSortField,"CONNECT"); | |
187 | strcpy(TopsitesSortType,"D"); | |
246c8489 | 188 | LongUrl=0; |
25697a35 GS |
189 | strcpy(language,"English"); |
190 | strcpy(FontFace,"Verdana,Tahoma,Arial"); | |
d6e703cc | 191 | strcpy(datetimeby,"elap"); |
25697a35 | 192 | strcpy(CharSet,"ISO-8859-1"); |
e6414a9d | 193 | Privacy=0; |
25697a35 GS |
194 | strcpy(PrivacyString,"***.***.***.***"); |
195 | strcpy(PrivacyStringColor,"blue"); | |
e6414a9d | 196 | SuccessfulMsg=1; |
25697a35 GS |
197 | strcpy(TopUserFields,"NUM DATE_TIME USERID CONNECT BYTES %BYTES IN-CACHE-OUT USED_TIME MILISEC %TIME TOTAL AVERAGE"); |
198 | strcpy(UserReportFields,"CONNECT BYTES %BYTES IN-CACHE-OUT USED_TIME MILISEC %TIME TOTAL AVERAGE"); | |
199 | strcpy(DataFileDelimiter,";"); | |
200 | strcpy(DataFileFields,"user;date;time;url;connect;bytes;in_cache;out_cache;elapsed"); | |
201 | strcpy(SiteUserTimeDateType,"table"); | |
e6414a9d | 202 | ShowReadStatistics=1; |
25697a35 | 203 | strcpy(IndexSortOrder,"D"); |
e6414a9d FM |
204 | ShowSargInfo=1; |
205 | ShowSargLogo=1; | |
25697a35 GS |
206 | strcpy(ParsedOutputLog,"no"); |
207 | strcpy(ParsedOutputLogCompress,"/bin/gzip"); | |
208 | strcpy(DisplayedValues,"abbreviation"); | |
209 | strcpy(HeaderFontSize,"9px"); | |
210 | strcpy(TitleFontSize,"11px"); | |
211 | strcpy(AuthUserFile,"/usr/local/sarg/passwd"); | |
212 | strcpy(AuthName,"SARG, Restricted Access"); | |
213 | strcpy(AuthType,"basic"); | |
214 | strcpy(Require,"require user admin %u"); | |
6e792ade | 215 | 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"); |
e6414a9d | 216 | Graphs=1; |
25697a35 GS |
217 | strcpy(Ulimit,"20000"); |
218 | strcpy(NtlmUserFormat,"domainname+username"); | |
491b862f GS |
219 | strcpy(IndexTree,"file"); |
220 | strcpy(RealtimeTypes,"GET,PUT,CONNECT"); | |
d6e703cc | 221 | strcpy(RealtimeUnauthRec,"show"); |
e6414a9d FM |
222 | SquidguardIgnoreDate=0; |
223 | DansguardianIgnoreDate=0; | |
491b862f | 224 | strcpy(DataFileUrl,"ip"); |
d6e703cc | 225 | strcpy(MaxElapsed,"28800000"); |
e6414a9d FM |
226 | BytesInSitesUsersReport=0; |
227 | UserAuthentication=0; | |
25697a35 GS |
228 | |
229 | dia[0]='\0'; | |
230 | mes[0]='\0'; | |
231 | ano[0]='\0'; | |
232 | hora[0]='\0'; | |
233 | tmp[0]='\0'; | |
234 | tmp2[0]='\0'; | |
235 | tmp3[0]='\0'; | |
236 | wtemp[0]='\0'; | |
237 | wtemp2[0]='\0'; | |
238 | us[0]='\0'; | |
239 | date[0]='\0'; | |
240 | df[0]='\0'; | |
241 | uagent[0]='\0'; | |
242 | hexclude[0]='\0'; | |
243 | addr[0]='\0'; | |
244 | hm[0]='\0'; | |
245 | hmf[0]='\0'; | |
246 | site[0]='\0'; | |
247 | outdir[0]='\0'; | |
248 | elap[0]='\0'; | |
249 | email[0]='\0'; | |
250 | zip[0]='\0'; | |
251 | UserInvalidChar[0]='\0'; | |
252 | DataFile[0]='\0'; | |
253 | SquidGuardConf[0]='\0'; | |
254 | DansGuardianConf[0]='\0'; | |
255 | ||
25697a35 GS |
256 | denied_count=0; |
257 | download_count=0; | |
258 | authfail_count=0; | |
259 | dansguardian_count=0; | |
260 | squidguard_count=0; | |
261 | DeniedReportLimit=10; | |
262 | AuthfailReportLimit=10; | |
263 | DansGuardianReportLimit=10; | |
264 | SquidGuardReportLimit=10; | |
94ff9470 | 265 | DownloadReportLimit=50; |
25697a35 GS |
266 | UserReportLimit=0; |
267 | debug=0; | |
268 | debugz=0; | |
269 | debugm=0; | |
270 | iprel=0; | |
271 | userip=0; | |
272 | color1=0; | |
273 | color2=0; | |
274 | color3=0; | |
491b862f GS |
275 | dotinuser=0; |
276 | realt=0; | |
277 | realtime_refresh=3; | |
278 | realtime_access_log_lines=1000; | |
279 | cost=0.01; | |
280 | nocost=50000000; | |
d6e703cc FM |
281 | ndownload=0; |
282 | squid24=0; | |
25697a35 GS |
283 | |
284 | bzero(IncludeUsers, MAXLEN); | |
285 | bzero(ExcludeString, MAXLEN); | |
286 | ||
a1de61fe FM |
287 | #ifdef HAVE_LOCALE_H |
288 | setlocale(LC_TIME,""); | |
289 | #endif | |
290 | ||
0a4e18e1 FM |
291 | NAccessLog=0; |
292 | for(x=0; x<=MAXLOGS; x++) | |
293 | AccessLog[x][0]='\0'; | |
294 | AccessLogFromCmdLine=0; | |
25697a35 GS |
295 | |
296 | language_load(language); | |
297 | strcpy(Title,text[88]); | |
298 | ||
491b862f | 299 | while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:u:l:L:o:s:t:w:hijmnprvxyz")) != -1){ |
25697a35 GS |
300 | switch(ch) |
301 | { | |
302 | case 'a': | |
303 | strcpy(addr,optarg); | |
304 | break; | |
305 | case 'b': | |
306 | strcpy(uagent,optarg); | |
307 | break; | |
308 | case 'c': | |
309 | strcpy(hexclude,optarg); | |
310 | break; | |
311 | case 'd': | |
9c7c6346 FM |
312 | strncpy(date,optarg,sizeof(date)-1); |
313 | date[sizeof(date)-1]='\0'; | |
314 | getword_start(&gwarea,optarg); | |
315 | if (getword(cdfrom,sizeof(cdfrom),&gwarea,'-')<0 || getword(cduntil,sizeof(cduntil),&gwarea,0)<0) { | |
4bcb77cf FM |
316 | printf("SARG: Maybe you have a broken record or garbage in your date range.\n"); |
317 | exit(1); | |
318 | } | |
25697a35 GS |
319 | date_from(date, cdfrom, cduntil); |
320 | dfrom=atoi(cdfrom); | |
321 | duntil=atoi(cduntil); | |
322 | break; | |
323 | case 'e': | |
324 | strcpy(email,optarg); | |
325 | break; | |
326 | case 'f': | |
327 | strcpy(ConfigFile,optarg); | |
328 | break; | |
329 | case 'g': | |
330 | strcpy(df,optarg); | |
331 | break; | |
936c9905 FM |
332 | case 'h': |
333 | usage(argv[0]); | |
25697a35 | 334 | exit(0); |
936c9905 | 335 | break; |
25697a35 GS |
336 | case 'i': |
337 | iprel++; | |
338 | break; | |
339 | case 'l': | |
0a4e18e1 | 340 | if (NAccessLog>=MAXLOGS) { |
4af2f8ff FM |
341 | printf("SARG: Too many log files.\n"); |
342 | exit(1); | |
343 | } | |
0a4e18e1 FM |
344 | strcpy(AccessLog[NAccessLog],optarg); |
345 | NAccessLog++; | |
346 | AccessLogFromCmdLine++; | |
25697a35 | 347 | break; |
491b862f GS |
348 | case 'L': |
349 | strcpy(SquidGuardLogAlternate,optarg); | |
350 | break; | |
25697a35 GS |
351 | case 'm': |
352 | debugm++; | |
353 | break; | |
354 | case 'n': | |
355 | dns++; | |
356 | break; | |
357 | case 'o': | |
358 | strcpy(outdir,optarg); | |
359 | break; | |
360 | case 'p': | |
361 | userip++; | |
362 | break; | |
491b862f GS |
363 | case 'r': |
364 | realt++; | |
365 | break; | |
25697a35 GS |
366 | case 's': |
367 | strcpy(site,optarg); | |
368 | break; | |
369 | case 't': | |
9c7c6346 FM |
370 | { |
371 | int h,m; | |
372 | ||
25697a35 GS |
373 | if(strstr(optarg,"-") == 0) { |
374 | strcpy(hm,optarg); | |
375 | strcpy(hmf,optarg); | |
376 | } else { | |
9c7c6346 FM |
377 | getword_start(&gwarea,optarg); |
378 | if (getword(hm,sizeof(hm),&gwarea,'-')<0 || getword(hmf,sizeof(hmf),&gwarea,0)<0) { | |
379 | fprintf(stderr,"SARG: Maybe you have a broken record or garbage in your time range.\n"); | |
4bcb77cf FM |
380 | exit(1); |
381 | } | |
25697a35 | 382 | } |
9c7c6346 FM |
383 | if(sscanf(hm,"%d:%d",&h,&m)!=2) { |
384 | fprintf(stderr,"SARG: time period must be MM or MM:SS. Exit.\n"); | |
936c9905 | 385 | exit(1); |
25697a35 | 386 | } |
9c7c6346 FM |
387 | sprintf(hm,"%02d%02d",h,m); |
388 | if(sscanf(hmf,"%d:%d",&h,&m)!=2) { | |
389 | fprintf(stderr,"SARG: time period must be MM or MM:SS. Exit.\n"); | |
390 | exit(1); | |
25697a35 | 391 | } |
9c7c6346 | 392 | sprintf(hmf,"%02d%02d",h,m); |
25697a35 | 393 | break; |
9c7c6346 | 394 | } |
25697a35 GS |
395 | case 'u': |
396 | strcpy(us,optarg); | |
397 | break; | |
398 | case 'v': | |
399 | version(); | |
400 | break; | |
401 | case 'w': | |
402 | strcpy(tmp,optarg); | |
403 | break; | |
404 | case 'x': | |
405 | debug++; | |
406 | break; | |
407 | case 'y': | |
408 | langcode++; | |
409 | break; | |
410 | case 'z': | |
411 | debugz++; | |
412 | break; | |
413 | case ':': | |
936c9905 FM |
414 | fprintf(stderr, "Option -%c require an argument\n",optopt); |
415 | errflg++; | |
416 | break; | |
25697a35 GS |
417 | case '?': |
418 | usage(argv[0]); | |
419 | exit(1); | |
420 | break; | |
421 | } | |
422 | ||
423 | } | |
424 | ||
425 | if (errflg) { | |
426 | usage(argv[0]); | |
427 | exit(2); | |
428 | } | |
429 | ||
d6e703cc | 430 | if(debug) debuga("Init"); |
25697a35 | 431 | |
d6e703cc | 432 | if(ConfigFile[0] == '\0') sprintf(ConfigFile,"%s/sarg.conf",SYSCONFDIR); |
936c9905 | 433 | if(access(ConfigFile, R_OK) != 0) { |
d2fe0c32 | 434 | debuga("Cannot open config file: %s - %s",ConfigFile,strerror(errno)); |
936c9905 FM |
435 | exit(1); |
436 | } | |
437 | ||
25697a35 | 438 | if(access(ConfigFile, R_OK) == 0) |
d6e703cc | 439 | getconf(); |
936c9905 | 440 | |
491b862f GS |
441 | if(realt) { |
442 | realtime(); | |
443 | exit(0); | |
444 | } | |
25697a35 | 445 | |
491b862f GS |
446 | if(strcmp(IndexTree,"file") == 0) |
447 | strcpy(ImageFile,"../images"); | |
448 | else | |
449 | strcpy(ImageFile,"../../../images"); | |
450 | ||
25697a35 | 451 | dataonly=0; |
c274f011 | 452 | if(DataFile[0] != '\0') |
25697a35 | 453 | dataonly++; |
936c9905 | 454 | |
48864d28 | 455 | subs(TopUserFields,sizeof(TopUserFields),"%BYTES","SETYB"); |
936c9905 | 456 | |
48864d28 | 457 | subs(UserReportFields,sizeof(UserReportFields),"%BYTES","SETYB"); |
25697a35 | 458 | |
0a4e18e1 FM |
459 | if(!NAccessLog) { |
460 | strcpy(AccessLog[0],"/var/log/squid/access.log"); | |
461 | NAccessLog++; | |
25697a35 GS |
462 | } |
463 | ||
464 | if(strcmp(hexclude,"onvert") == 0 && strcmp(site,"plit") != 0) { | |
0a4e18e1 | 465 | convlog(AccessLog[0], df, dfrom, duntil); |
25697a35 GS |
466 | exit(0); |
467 | } | |
936c9905 | 468 | |
25697a35 | 469 | if(strcmp(site,"plit") == 0) { |
0a4e18e1 | 470 | splitlog(AccessLog[0], df, dfrom, duntil, hexclude); |
25697a35 GS |
471 | exit(0); |
472 | } | |
473 | ||
48864d28 | 474 | load_excludecodes(ExcludeCodes); |
25697a35 GS |
475 | |
476 | if(access(PasswdFile, R_OK) == 0) { | |
477 | getusers(PasswdFile,debug); | |
478 | puser++; | |
479 | } | |
480 | ||
481 | if(hexclude[0] == '\0') | |
482 | strcpy(hexclude,ExcludeHosts); | |
483 | if(strlen(hexclude) > 0) { | |
484 | if(access(hexclude, R_OK) != 0) { | |
d2fe0c32 | 485 | debuga("Cannot open exclude_hosts file: %s - %s",hexclude,strerror(errno)); |
25697a35 | 486 | exit(1); |
25697a35 | 487 | } |
936c9905 FM |
488 | gethexclude(hexclude,debug); |
489 | fhost++; | |
25697a35 GS |
490 | } |
491 | ||
492 | if(ReportType[0] == '\0') | |
491b862f | 493 | strcpy(ReportType,"topusers topsites users_sites sites_users date_time denied auth_failures site_user_time_date downloads"); |
25697a35 GS |
494 | |
495 | if(access(ExcludeUsers, R_OK) == 0) { | |
496 | getuexclude(ExcludeUsers,debug); | |
497 | fuser++; | |
498 | } | |
499 | ||
500 | indexonly=0; | |
501 | if(fuser) { | |
502 | if(strstr(excludeuser,"indexonly") != 0) | |
503 | indexonly++; | |
504 | } | |
d6e703cc FM |
505 | if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly++; |
506 | if(strcmp(Index,"only") == 0) indexonly++; | |
25697a35 | 507 | |
246c8489 | 508 | if(dns) Ip2Name=1; |
25697a35 | 509 | |
246c8489 | 510 | if(UserIp) userip++; |
25697a35 | 511 | |
d6e703cc | 512 | if(strlen(MaxElapsed)>1) max_elapsed=atol(MaxElapsed); |
25697a35 | 513 | |
d6e703cc | 514 | if(strlen(outdir)<1) strcpy(outdir,OutputDir); |
25697a35 GS |
515 | strcat(outdir,"/"); |
516 | ||
d6e703cc | 517 | if(uagent[0] == '\0') strcpy(uagent,UserAgentLog); |
25697a35 | 518 | |
d6e703cc FM |
519 | if(tmp[0] == '\0') strcpy(tmp,TempDir); |
520 | else strcpy(TempDir,tmp); | |
25697a35 | 521 | |
d6e703cc FM |
522 | if(df[0] == '\0') strcpy(df,DateFormat); |
523 | else strcpy(DateFormat,df); | |
25697a35 GS |
524 | |
525 | if(df[0] == '\0') { | |
526 | strcpy(df,"u"); | |
527 | strcpy(DateFormat,"u"); | |
528 | } | |
529 | ||
4af2f8ff | 530 | if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail); |
25697a35 GS |
531 | |
532 | strcpy(tmp2,tmp); | |
25697a35 | 533 | |
e6414a9d | 534 | if(email[0] != '\0') { |
d6e703cc | 535 | sprintf(wtemp2,"%s/sarg",tmp2); |
25697a35 | 536 | my_mkdir(wtemp2); |
d6e703cc | 537 | strcat(tmp2,"/sarg"); |
25697a35 GS |
538 | strcpy(outdir,tmp2); |
539 | strcat(outdir,"/"); | |
d6e703cc | 540 | } |
25697a35 GS |
541 | |
542 | strcat(tmp2,"/sarg.log"); | |
543 | ||
544 | sprintf(warea,"%s/sarg",tmp); | |
545 | if(access(warea, R_OK) == 0) { | |
05b90947 FM |
546 | if (snprintf(tmp3,sizeof(tmp3),"rm -rf \"%s\"",warea)>=sizeof(tmp3)) { |
547 | fprintf(stderr,"SARG: Directory name too long: %s\n",warea); | |
548 | exit(1); | |
549 | } | |
550 | cstatus=system(tmp3); | |
551 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
552 | fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); | |
553 | fprintf(stderr, "SARG: command: %s\n",tmp3); | |
554 | exit(1); | |
555 | } | |
25697a35 | 556 | } |
2357ef77 | 557 | |
25697a35 GS |
558 | sprintf(tmp3,"%s/sarg",tmp); |
559 | my_mkdir(tmp3); | |
491b862f GS |
560 | strcpy(tmp4,tmp3); |
561 | strcpy(tmp5,tmp3); | |
562 | strcpy(tmp6,tmp3); | |
25697a35 GS |
563 | strcat(tmp4,"/denied.log.unsort"); |
564 | strcat(tmp5,"/denied.log"); | |
565 | strcat(tmp6,"/authfail.log.unsort"); | |
566 | ||
567 | if(debug) { | |
568 | fprintf(stderr, "SARG: %s:\nSARG:\n",text[22]); | |
569 | fprintf(stderr, "SARG: %35s (-a) = %s\n",text[23],addr); | |
570 | fprintf(stderr, "SARG: %35s (-b) = %s\n",text[71],uagent); | |
571 | fprintf(stderr, "SARG: %35s (-c) = %s\n",text[69],hexclude); | |
572 | fprintf(stderr, "SARG: %35s (-d) = %s\n",text[24],date); | |
573 | fprintf(stderr, "SARG: %35s (-e) = %s\n",text[41],email); | |
574 | fprintf(stderr, "SARG: %35s (-f) = %s\n",text[70],ConfigFile); | |
575 | if(strcmp(df,"e") == 0) | |
576 | fprintf(stderr, "SARG: %35s (-g) = %s (dd/mm/yyyy)\n",text[25],text[26]); | |
577 | if(strcmp(df,"u") == 0) | |
578 | fprintf(stderr, "SARG: %35s (-g) = %s (mm/dd/yyyy)\n",text[25],text[27]); | |
579 | if(strcmp(df,"w") == 0) | |
580 | fprintf(stderr, "SARG: %35s (-g) = %s (yyyy/ww)\n",text[25],text[85]); | |
581 | if(iprel) | |
582 | fprintf(stderr, "SARG: %35s (-i) = %s\n",text[28],text[1]); | |
0a4e18e1 | 583 | else |
25697a35 | 584 | fprintf(stderr, "SARG: %35s (-i) = %s\n",text[28],text[2]); |
0a4e18e1 FM |
585 | for (iarq=0 ; iarq<NAccessLog ; iarq++) |
586 | fprintf(stderr, "SARG: %35s (-l) = %s\n",text[37],AccessLog[iarq]); | |
246c8489 | 587 | if(Ip2Name) |
25697a35 GS |
588 | fprintf(stderr, "SARG: %35s (-n) = %s\n",text[65],text[1]); |
589 | else | |
590 | fprintf(stderr, "SARG: %35s (-n) = %s\n",text[65],text[2]); | |
591 | fprintf(stderr, "SARG: %35s (-o) = %s\n",text[38],outdir); | |
246c8489 | 592 | if(UserIp) |
25697a35 GS |
593 | fprintf(stderr, "SARG: %35s (-p) = %s\n",text[29],text[1]); |
594 | else | |
595 | fprintf(stderr, "SARG: %35s (-p) = %s\n",text[29],text[2]); | |
596 | fprintf(stderr, "SARG: %35s (-s) = %s\n",text[30],site); | |
597 | fprintf(stderr, "SARG: %35s (-t) = %s\n",text[31],hm); | |
598 | fprintf(stderr, "SARG: %35s (-u) = %s\n",text[32],us); | |
599 | fprintf(stderr, "SARG: %35s (-w) = %s\n",text[34],tmp); | |
600 | if(debug) | |
601 | fprintf(stderr, "SARG: %35s (-x) = %s\n",text[35],text[1]); | |
602 | else | |
603 | fprintf(stderr, "SARG: %35s (-x) = %s\n",text[35],text[2]); | |
604 | if(debugz) | |
605 | fprintf(stderr, "SARG: %35s (-z) = %s\n",text[36],text[1]); | |
606 | else | |
607 | fprintf(stderr, "SARG: %35s (-z) = %s\n",text[36],text[2]); | |
608 | fprintf(stderr, "SARG:\n"); | |
609 | } | |
610 | ||
611 | if(debugm) { | |
612 | printf("%s:\nSARG:\n",text[22]); | |
613 | printf("%35s (-a) = %s\n",text[23],addr); | |
614 | printf("%35s (-b) = %s\n",text[71],uagent); | |
615 | printf("%35s (-c) = %s\n",text[69],hexclude); | |
616 | printf("%35s (-d) = %s\n",text[24],date); | |
617 | printf("%35s (-e) = %s\n",text[41],email); | |
618 | printf("%35s (-f) = %s\n",text[70],ConfigFile); | |
619 | if(strcmp(df,"e") == 0) | |
620 | printf("%35s (-g) = %s (dd/mm/yyyy)\n",text[25],text[26]); | |
621 | if(strcmp(df,"u") == 0) | |
622 | printf("%35s (-g) = %s (mm/dd/yyyy)\n",text[25],text[27]); | |
623 | if(strcmp(df,"w") == 0) | |
624 | printf("%35s (-g) = %s (yyyy/ww)\n",text[25],text[85]); | |
625 | if(iprel) | |
626 | printf("%35s (-i) = %s\n",text[28],text[1]); | |
0a4e18e1 | 627 | else |
25697a35 | 628 | printf("%35s (-i) = %s\n",text[28],text[2]); |
0a4e18e1 FM |
629 | for (iarq=0 ; iarq<NAccessLog ; iarq++) |
630 | printf("%35s (-l) = %s\n",text[37],AccessLog[iarq]); | |
246c8489 | 631 | if(Ip2Name) |
25697a35 GS |
632 | printf("%35s (-n) = %s\n",text[65],text[1]); |
633 | else | |
634 | printf("%35s (-n) = %s\n",text[65],text[2]); | |
635 | printf("%35s (-o) = %s\n",text[38],outdir); | |
246c8489 | 636 | if(UserIp) |
25697a35 GS |
637 | printf("%35s (-p) = %s\n",text[29],text[1]); |
638 | else | |
639 | printf("%35s (-p) = %s\n",text[29],text[2]); | |
640 | printf("%35s (-s) = %s\n",text[30],site); | |
641 | printf("%35s (-t) = %s\n",text[31],hm); | |
642 | printf("%35s (-u) = %s\n",text[32],us); | |
643 | printf("%35s (-w) = %s\n",text[34],tmp); | |
644 | if(debug) | |
645 | printf("%35s (-x) = %s\n",text[35],text[1]); | |
646 | else | |
647 | printf("%35s (-x) = %s\n",text[35],text[2]); | |
648 | if(debugz) | |
649 | printf("%35s (-z) = %s\n",text[36],text[1]); | |
650 | else | |
651 | printf("%35s (-z) = %s\n",text[36],text[2]); | |
652 | printf("sarg %s: %s\n",text[73],VERSION); | |
653 | printf("Language=%s\n\n",text[3]); | |
654 | } | |
655 | ||
d2fe0c32 FM |
656 | if(debug) |
657 | debuga("sarg %s: %s",text[73],VERSION); | |
25697a35 | 658 | |
e112fa1f | 659 | #ifdef HAVE_RLIM_T |
eee60323 | 660 | if (Ulimit[0] != '\0') { |
e112fa1f | 661 | struct rlimit rl; |
243a9fab FM |
662 | long l1, l2; |
663 | int rc=0; | |
664 | ||
25697a35 | 665 | #if defined(RLIMIT_NOFILE) |
d6e703cc | 666 | getrlimit (RLIMIT_NOFILE, &rl); |
ed1ac52f | 667 | #elif defined(RLIMIT_OFILE) |
d6e703cc | 668 | getrlimit (RLIMIT_OFILE, &rl); |
243a9fab FM |
669 | #else |
670 | #warning "No rlimit resource for the number of open files" | |
25697a35 | 671 | #endif |
d6e703cc FM |
672 | l1 = rl.rlim_cur; |
673 | l2 = rl.rlim_max; | |
674 | ||
eee60323 FM |
675 | rl.rlim_cur = atol(Ulimit); |
676 | rl.rlim_max = atol(Ulimit); | |
243a9fab | 677 | #if defined(RLIMIT_NOFILE) |
eee60323 | 678 | rc=setrlimit (RLIMIT_NOFILE, &rl); |
243a9fab | 679 | #elif defined(RLIMIT_OFILE) |
eee60323 | 680 | rc=setrlimit (RLIMIT_OFILE, &rl); |
243a9fab FM |
681 | #else |
682 | #warning "No rlimit resource for the number of open files" | |
683 | #endif | |
eee60323 FM |
684 | if(rc == -1) { |
685 | debuga("setrlimit error - %s\n",strerror(errno)); | |
d6e703cc | 686 | } |
25697a35 | 687 | |
d2fe0c32 FM |
688 | if(debug) |
689 | debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING,l1,l2,rl.rlim_cur,rl.rlim_max); | |
25697a35 | 690 | } |
e112fa1f | 691 | #endif |
25697a35 | 692 | |
936c9905 | 693 | read_usertab(UserTabFile); |
25697a35 GS |
694 | |
695 | sprintf ( sz_Download_Unsort , "%s/sarg/download.unsort", tmp); | |
25697a35 | 696 | |
294cb3d7 FM |
697 | if(strstr(ReportType,"denied") != 0) { |
698 | if((fp_denied=MY_FOPEN(tmp4,"w"))==NULL) { | |
699 | fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],tmp4,strerror(errno)); | |
700 | exit(1); | |
701 | } | |
702 | } | |
703 | ||
704 | if(DataFile[0]=='\0') { | |
705 | if(strstr(ReportType,"denied") != 0 || strstr(ReportType,"auth_failures") != 0) { | |
706 | if((fp_authfail=MY_FOPEN(tmp6,"w"))==NULL) { | |
707 | fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[45],tmp6,strerror(errno)); | |
708 | exit(1); | |
709 | } | |
710 | } | |
711 | } | |
712 | ||
0a4e18e1 FM |
713 | for (iarq=0 ; iarq<NAccessLog ; iarq++) { |
714 | strcpy(arq,AccessLog[iarq]); | |
25697a35 | 715 | |
936c9905 | 716 | strcpy(arqtt,arq); |
d6e703cc | 717 | |
58d7d8fe FM |
718 | if(strcmp(arq,"-")==0) { |
719 | if(debug) | |
720 | debuga("%s: %s",text[7],"stdin"); | |
721 | fp_in=stdin; | |
722 | from_stdin=1; | |
723 | } else { | |
724 | decomp(arq,zip,tmp); | |
725 | if(debug) | |
726 | debuga("%s: %s",text[7],arq); | |
727 | if((fp_in=MY_FOPEN(arq,"r"))==NULL) { | |
728 | fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[8],arq,strerror(errno)); | |
936c9905 FM |
729 | exit(1); |
730 | } | |
58d7d8fe | 731 | from_stdin=0; |
25697a35 | 732 | } |
58d7d8fe | 733 | ilf=ILF_Unknown; |
936c9905 | 734 | // pre-Read the file only if I have to show stats |
e6414a9d | 735 | if(ShowReadStatistics && !from_stdin) { |
936c9905 | 736 | rewind(fp_in); |
7c0b0078 FM |
737 | recs1=0UL; |
738 | recs2=0UL; | |
25697a35 | 739 | |
936c9905 FM |
740 | while( fgets(bufz,sizeof(bufz),fp_in) != NULL ) recs1++; |
741 | rewind(fp_in); | |
7c0b0078 | 742 | printf("SARG: Records in file: %lu, reading: %3.2f%%\r",recs1,(float) 0); |
936c9905 FM |
743 | fflush( stdout ) ; |
744 | } | |
745 | ||
746 | while(fgets(bufz,sizeof(bufz),fp_in)!=NULL) { | |
58d7d8fe FM |
747 | |
748 | if (ilf==ILF_Unknown) { | |
749 | if(strncmp(bufz,"#Software: Mic",14) == 0) { | |
750 | fixendofline(bufz); | |
751 | debuga("%s: %s",text[143],bufz); | |
752 | ilf=ILF_Isa; | |
753 | ilf_count[ilf]++; | |
754 | continue; | |
755 | } | |
756 | ||
757 | if(strncmp(bufz,"*** SARG Log ***",16) == 0) { | |
9c7c6346 FM |
758 | getword_start(&gwarea,arqtt); |
759 | if (getword(val2,sizeof(val2),&gwarea,'-')<0 || getword(val2,sizeof(val2),&gwarea,'_')<0 || | |
760 | getword(val3,sizeof(val3),&gwarea,'-')<0 || getword(val3,sizeof(val3),&gwarea,'_')<0) { | |
58d7d8fe FM |
761 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
762 | exit(1); | |
763 | } | |
764 | sprintf(period,"%s-%s",val2,val3); | |
765 | ilf=ILF_Sarg; | |
766 | ilf_count[ilf]++; | |
767 | continue; | |
768 | } | |
769 | } | |
770 | ||
771 | if(strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg) { | |
772 | if(access(ParsedOutputLog,R_OK) != 0) { | |
773 | sprintf(csort,"%s",ParsedOutputLog); | |
774 | my_mkdir(csort); | |
775 | } | |
776 | sprintf(arq_log,"%s/sarg_temp.log",ParsedOutputLog); | |
777 | if((fp_log=MY_FOPEN(arq_log,"w"))==NULL) { | |
778 | fprintf(stderr, "%s: (log) %s: %s - %s\n",argv[0],text[8],arq_log,strerror(errno)); | |
779 | exit(1); | |
780 | } | |
781 | fputs("*** SARG Log ***\n",fp_log); | |
782 | } | |
783 | ||
936c9905 | 784 | recs2++; |
e6414a9d | 785 | if( ShowReadStatistics && !from_stdin && ! --OutputNonZero) { |
25697a35 GS |
786 | perc = recs2 * 100 ; |
787 | perc = perc / recs1 ; | |
7c0b0078 | 788 | printf("SARG: Records in file: %lu, reading: %3.2f%%\r",recs1,perc); |
25697a35 GS |
789 | fflush (stdout); |
790 | OutputNonZero = REPORT_EVERY_X_LINES ; | |
936c9905 FM |
791 | } |
792 | if(strlen(bufz) > MAXLEN-1) continue; | |
793 | if(!bufz[0]) continue; | |
794 | if(strstr(bufz,"HTTP/0.0") != 0) continue; | |
795 | if(strstr(bufz,"logfile turned over") != 0) continue; | |
796 | if(bufz[0] == ' ') continue; | |
797 | if(strlen(bufz) < 58) continue; | |
798 | ||
799 | // Record only hours usage which is required | |
800 | tt = (time_t) strtoul( bufz, NULL, 10 ); | |
801 | t = localtime( &tt ); | |
802 | ||
803 | if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len, | |
804 | sizeof( int ), compar ) == NULL ) | |
805 | continue; | |
806 | ||
807 | if( bsearch( &( t -> tm_hour ), hours.list, hours.len, | |
808 | sizeof( int ), compar ) == NULL ) | |
809 | continue; | |
810 | ||
811 | // exclude_string | |
812 | exstring=0; | |
9c7c6346 | 813 | if(ExcludeString[0] != '\0') { |
936c9905 | 814 | strcpy(warea,bufz); |
9c7c6346 FM |
815 | getword_start(&gwarea,ExcludeString); |
816 | while(strchr(gwarea.current,':') != 0) { | |
817 | if (getword_multisep(val1,sizeof(val1),&gwarea,':')<0) { | |
936c9905 FM |
818 | printf("SARG: Maybe you have a broken record or garbage in your exclusion string.\n"); |
819 | exit(1); | |
820 | } | |
821 | if((str=(char *) strstr(warea,val1)) != (char *) NULL ) | |
822 | exstring++; | |
823 | } | |
9c7c6346 | 824 | if((str=(char *) strstr(warea,gwarea.current)) != (char *) NULL ) |
936c9905 FM |
825 | exstring++; |
826 | } | |
827 | if(exstring) continue; | |
828 | ||
829 | strcpy(bufy,bufz); | |
830 | if ((str = strchr(bufz, '\n')) != NULL) | |
2357ef77 | 831 | *str = '\0'; /* strip \n */ |
936c9905 FM |
832 | |
833 | totregsl++; | |
936c9905 FM |
834 | if(debugm) |
835 | printf("BUF=%s\n",bufz); | |
836 | ||
58d7d8fe | 837 | if (ilf==ILF_Squid || ilf==ILF_Common || ilf==ILF_Unknown) { |
9c7c6346 FM |
838 | getword_start(&gwarea,bufz); |
839 | if (getword(data,sizeof(data),&gwarea,' ')<0) { | |
936c9905 FM |
840 | printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); |
841 | exit(1); | |
842 | } | |
0ff563a2 FM |
843 | if((str=(char *) strchr(data, '.')) != (char *) NULL ) { |
844 | if((str=(char *) strchr(str+1, '.')) != (char *) NULL ) { | |
936c9905 FM |
845 | strcpy(ip,data); |
846 | strcpy(elap,"0"); | |
847 | if(squid24) { | |
9c7c6346 | 848 | if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) { |
936c9905 FM |
849 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
850 | exit(1); | |
851 | } | |
852 | } else { | |
9c7c6346 | 853 | if (getword_skip(255,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) { |
936c9905 FM |
854 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
855 | exit(1); | |
856 | } | |
857 | } | |
9c7c6346 FM |
858 | if (getword(data,sizeof(data),&gwarea,']')<0 || getword_skip(MAXLEN,&gwarea,'"')<0 || |
859 | getword(fun,sizeof(fun),&gwarea,' ')<0 || getword(url,sizeof(url),&gwarea,' ')<0 || | |
860 | getword_skip(MAXLEN,&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0 || | |
861 | getword(tam,sizeof(tam),&gwarea,' ')<0) { | |
936c9905 FM |
862 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
863 | exit(1); | |
864 | } | |
9c7c6346 FM |
865 | if((str=(char *) strchr(gwarea.current, ' ')) != (char *) NULL ) { |
866 | if (getword(code,sizeof(code),&gwarea,' ')<0) { | |
867 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); | |
868 | exit(1); | |
869 | } | |
870 | } else { | |
871 | if (getword(code,sizeof(code),&gwarea,'\0')<0) { | |
936c9905 FM |
872 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
873 | exit(1); | |
874 | } | |
875 | } | |
936c9905 FM |
876 | |
877 | if ((str = strchr(code, ':')) != NULL) | |
2357ef77 | 878 | *str = '/'; |
936c9905 FM |
879 | |
880 | if(strcmp(tam,"\0") == 0) | |
881 | strcpy(tam,"0"); | |
882 | ||
58d7d8fe FM |
883 | ilf=ILF_Common; |
884 | ilf_count[ilf]++; | |
936c9905 FM |
885 | } |
886 | } | |
887 | ||
58d7d8fe | 888 | if(ilf==ILF_Unknown || ilf==ILF_Squid) { |
9c7c6346 | 889 | if (getword(elap,sizeof(elap),&gwarea,' ')<0) { |
936c9905 FM |
890 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
891 | exit(1); | |
892 | } | |
9c7c6346 FM |
893 | while(strcmp(elap,"") == 0 && gwarea.current[0] != '\0') |
894 | if (getword(elap,sizeof(elap),&gwarea,' ')<0) { | |
936c9905 FM |
895 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
896 | exit(1); | |
897 | } | |
898 | if(strlen(elap) < 1) continue; | |
9c7c6346 | 899 | if (getword(ip,sizeof(ip),&gwarea,' ')<0){ |
936c9905 FM |
900 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
901 | exit(1); | |
902 | } | |
9c7c6346 | 903 | if (getword(code,sizeof(code),&gwarea,' ')<0){ |
936c9905 FM |
904 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
905 | exit(1); | |
906 | } | |
9c7c6346 | 907 | if (getword(tam,sizeof(tam),&gwarea,' ')<0){ |
936c9905 FM |
908 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
909 | exit(1); | |
910 | } | |
9c7c6346 | 911 | if (getword(fun,sizeof(fun),&gwarea,' ')<0){ |
936c9905 FM |
912 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
913 | exit(1); | |
914 | } | |
9c7c6346 | 915 | if (getword(url,sizeof(url),&gwarea,' ')<0){ |
936c9905 FM |
916 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
917 | exit(1); | |
918 | } | |
d6e703cc FM |
919 | // while (strstr(bufz,"%20") != 0) { |
920 | // getword(warea,bufz,' '); | |
921 | // strcat(url,warea); | |
922 | // } | |
9c7c6346 | 923 | if (getword(user,sizeof(user),&gwarea,' ')<0){ |
936c9905 FM |
924 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
925 | exit(1); | |
926 | } | |
58d7d8fe FM |
927 | ilf=ILF_Squid; |
928 | ilf_count[ilf]++; | |
936c9905 | 929 | } |
58d7d8fe FM |
930 | } |
931 | if (ilf==ILF_Sarg) { | |
9c7c6346 FM |
932 | getword_start(&gwarea,bufz); |
933 | if (getword(data,sizeof(data),&gwarea,' ')<0){ | |
936c9905 FM |
934 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
935 | exit(1); | |
936 | } | |
9c7c6346 | 937 | if (getword(hora,sizeof(hora),&gwarea,' ')<0) { |
936c9905 FM |
938 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
939 | exit(1); | |
940 | } | |
9c7c6346 | 941 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
936c9905 FM |
942 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
943 | exit(1); | |
944 | } | |
9c7c6346 | 945 | if (getword(ip,sizeof(ip),&gwarea,' ')<0) { |
936c9905 FM |
946 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
947 | exit(1); | |
948 | } | |
9c7c6346 | 949 | if (getword(url,sizeof(url),&gwarea,' ')<0){ |
936c9905 FM |
950 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
951 | exit(1); | |
952 | } | |
9c7c6346 | 953 | if (getword(tam,sizeof(tam),&gwarea,' ')<0){ |
936c9905 FM |
954 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
955 | exit(1); | |
956 | } | |
9c7c6346 | 957 | if (getword(code,sizeof(code),&gwarea,' ')<0){ |
936c9905 FM |
958 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
959 | exit(1); | |
960 | } | |
9c7c6346 | 961 | if (getword(elap,sizeof(elap),&gwarea,' ')<0){ |
936c9905 FM |
962 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
963 | exit(1); | |
964 | } | |
9c7c6346 | 965 | if (getword(smartfilter,sizeof(smartfilter),&gwarea,' ')<0){ |
936c9905 FM |
966 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
967 | exit(1); | |
968 | } | |
58d7d8fe FM |
969 | } |
970 | if (ilf==ILF_Isa) { | |
076cbab8 FM |
971 | if (bufz[0] == '#') { |
972 | int ncols,cols[ISACOL_Last]; | |
973 | ||
974 | fixendofline(bufz); | |
9c7c6346 | 975 | getword_start(&gwarea,bufz); |
076cbab8 | 976 | // remove the #Fields: column at the beginning of the line |
9c7c6346 | 977 | if (getword_skip(1000,&gwarea,' ')<0){ |
936c9905 FM |
978 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
979 | exit(1); | |
980 | } | |
076cbab8 FM |
981 | for (ncols=0 ; ncols<ISACOL_Last ; ncols++) cols[ncols]=-1; |
982 | ncols=0; | |
9c7c6346 FM |
983 | while(gwarea.current[0] != '\0') { |
984 | if (getword(val1,sizeof(val1),&gwarea,'\t')<0){ | |
936c9905 FM |
985 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
986 | exit(1); | |
987 | } | |
076cbab8 FM |
988 | if(strcmp(val1,"c-ip") == 0) cols[ISACOL_Ip]=ncols; |
989 | if(strcmp(val1,"cs-username") == 0) cols[ISACOL_UserName]=ncols; | |
990 | if(strcmp(val1,"date") == 0) cols[ISACOL_Date]=ncols; | |
991 | if(strcmp(val1,"time") == 0) cols[ISACOL_Time]=ncols; | |
992 | if(strcmp(val1,"time-taken") == 0) cols[ISACOL_TimeTaken]=ncols; | |
993 | if(strcmp(val1,"sc-bytes") == 0) cols[ISACOL_Bytes]=ncols; | |
994 | if(strcmp(val1,"cs-uri") == 0) cols[ISACOL_Uri]=ncols; | |
995 | if(strcmp(val1,"sc-status") == 0) cols[ISACOL_Status]=ncols; | |
996 | ncols++; | |
936c9905 | 997 | } |
076cbab8 FM |
998 | if (cols[ISACOL_Ip]>=0) { |
999 | isa_ncols=ncols; | |
1000 | for (isa_ncols=0 ; isa_ncols<ncols ; isa_ncols++) | |
1001 | isa_cols[isa_ncols]=cols[isa_ncols]; | |
936c9905 | 1002 | } |
076cbab8 | 1003 | continue; |
936c9905 | 1004 | } |
076cbab8 | 1005 | if (!isa_ncols) continue; |
9c7c6346 | 1006 | getword_start(&gwarea,bufz); |
076cbab8 | 1007 | for (x=0 ; x<isa_ncols ; x++) { |
9c7c6346 | 1008 | if (getword(val1,sizeof(val1),&gwarea,'\t')<0) { |
936c9905 FM |
1009 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
1010 | exit(1); | |
1011 | } | |
076cbab8 | 1012 | if (x==isa_cols[ISACOL_Ip]) { |
5160b452 FM |
1013 | if (strlen(val1)>=sizeof(ip)) { |
1014 | printf("SARG: Maybe you have a broken IP in your %s file.\n",arq); | |
1015 | exit(1); | |
1016 | } | |
1017 | strcpy(ip,val1); | |
076cbab8 | 1018 | } else if (x==isa_cols[ISACOL_UserName]) { |
5160b452 FM |
1019 | if (strlen(val1)>=sizeof(user)) { |
1020 | printf("SARG: Maybe you have a broken user in your %s file.\n",arq); | |
1021 | exit(1); | |
1022 | } | |
1023 | strcpy(user,val1); | |
076cbab8 | 1024 | } else if (x==isa_cols[ISACOL_Date]) { |
5160b452 FM |
1025 | if (strlen(val1)>=sizeof(data)) { |
1026 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); | |
1027 | exit(1); | |
1028 | } | |
1029 | strcpy(data,val1); | |
076cbab8 | 1030 | } else if (x==isa_cols[ISACOL_Time]) { |
5160b452 FM |
1031 | if (strlen(val1)>=sizeof(hora)) { |
1032 | printf("SARG: Maybe you have a broken time in your %s file.\n",arq); | |
1033 | exit(1); | |
1034 | } | |
1035 | strcpy(hora,val1); | |
076cbab8 | 1036 | } else if (x==isa_cols[ISACOL_TimeTaken]) { |
5160b452 FM |
1037 | if (strlen(val1)>=sizeof(elap)) { |
1038 | printf("SARG: Maybe you have a broken download duration in your %s file.\n",arq); | |
1039 | exit(1); | |
1040 | } | |
1041 | strcpy(elap,val1); | |
076cbab8 | 1042 | } else if (x==isa_cols[ISACOL_Bytes]) { |
5160b452 FM |
1043 | if (strlen(val1)>=sizeof(tam)) { |
1044 | printf("SARG: Maybe you have a broken download size in your %s file.\n",arq); | |
1045 | exit(1); | |
1046 | } | |
1047 | strcpy(tam,val1); | |
076cbab8 | 1048 | } else if (x==isa_cols[ISACOL_Uri]) { |
5160b452 FM |
1049 | if (strlen(val1)>=sizeof(url)) { |
1050 | printf("SARG: Maybe you have a broken URL in your %s file.\n",arq); | |
1051 | exit(1); | |
1052 | } | |
1053 | strcpy(url,val1); | |
076cbab8 | 1054 | } else if (x==isa_cols[ISACOL_Status]) { |
5160b452 FM |
1055 | if (strlen(val1)>=sizeof(code)) { |
1056 | printf("SARG: Maybe you have a broken access code in your %s file.\n",arq); | |
1057 | exit(1); | |
1058 | } | |
1059 | strcpy(code,val1); | |
936c9905 FM |
1060 | } |
1061 | } | |
1062 | ||
1063 | if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) { | |
1064 | sprintf(val1,"DENIED/%s",code); | |
1065 | strcpy(code,val1); | |
1066 | } | |
9c7c6346 FM |
1067 | getword_start(&gwarea,data); |
1068 | if (getword(ano,sizeof(ano),&gwarea,'-')<0){ | |
1069 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); | |
1070 | exit(1); | |
1071 | } | |
1072 | if (getword(mes,sizeof(mes),&gwarea,'-')<0){ | |
936c9905 FM |
1073 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
1074 | exit(1); | |
1075 | } | |
9c7c6346 | 1076 | if (getword(dia,sizeof(dia),&gwarea,'\0')<0){ |
936c9905 FM |
1077 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",arq); |
1078 | exit(1); | |
1079 | } | |
936c9905 FM |
1080 | conv_month_name(mes); |
1081 | sprintf(data," %s/%s/%s:%s",dia,mes,ano,hora); | |
1082 | } | |
1083 | ||
1084 | if(strlen(user) > 150) { | |
5160b452 | 1085 | if (debugm) printf("User too long: %s\n",user); |
936c9905 FM |
1086 | totregsx++; |
1087 | continue; | |
1088 | } | |
1089 | ||
1090 | // include_users | |
4af2f8ff | 1091 | if(IncludeUsers[0] != '\0') { |
936c9905 FM |
1092 | sprintf(val1,":%s:",user); |
1093 | if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL ) | |
1094 | continue; | |
1095 | } | |
1096 | ||
48864d28 | 1097 | if(vercode(code)) { |
5160b452 | 1098 | if (debugm) printf("Excluded code: %s\n",code); |
48864d28 FM |
1099 | totregsx++; |
1100 | continue; | |
936c9905 FM |
1101 | } |
1102 | ||
1103 | if(testvaliduserchar(user)) | |
1104 | continue; | |
1105 | ||
06ced858 | 1106 | #if 0 |
0ff563a2 FM |
1107 | if((str = strstr(user,"%20")) != NULL) { |
1108 | /* | |
1109 | Why is it necessary to truncate the user name at the first space ? | |
1110 | ||
1111 | The old code used to truncate the user name at the first % if a %20 was | |
1112 | found anywhere in the string. That means the string could be truncated | |
1113 | at the wrong place if another % occured before the %20. This new code should | |
1114 | avoid that problem and only truncate at the space. There is no bug | |
1115 | report indicating that anybody noticed this. | |
1116 | */ | |
1117 | *str='\0'; | |
936c9905 FM |
1118 | } |
1119 | ||
9c7c6346 FM |
1120 | /* |
1121 | Code prior to 2.2.7 used to replace any %xx by a dot as long as a %5c was | |
1122 | found in the user name. | |
1123 | */ | |
1124 | while((str = strstr(user,"%5c")) != NULL) { | |
1125 | *str='.'; | |
1126 | for (x=3 ; str[x] ; x++) str[x-2]=str[x]; | |
936c9905 | 1127 | } |
06ced858 | 1128 | #endif |
936c9905 | 1129 | |
5542cb59 FM |
1130 | for(str=user; *str; str++) { |
1131 | if(*str=='.') dotinuser++; | |
1132 | if(*str=='?' || *str=='.' || *str==':' || *str=='/' || *str=='\\') | |
1133 | *str='_'; | |
936c9905 FM |
1134 | } |
1135 | ||
1136 | strlow(user); | |
1137 | if(strncmp(NtlmUserFormat,"user",4) == 0) { | |
9c7c6346 FM |
1138 | if((str = strchr(user,'_')) != 0) { |
1139 | strcpy(warea,str+1); | |
1140 | strcpy(user,warea); | |
1141 | } | |
1142 | if((str = strchr(user,'+')) != 0) { | |
1143 | strcpy(warea,str+1); | |
1144 | strcpy(user,warea); | |
1145 | } | |
936c9905 FM |
1146 | } |
1147 | ||
1148 | if(strstr(ReportType,"denied") != 0) | |
1149 | strcpy(urly,url); | |
1150 | ||
304a739d FM |
1151 | download_flag=is_download_suffix(url); |
1152 | if (download_flag) { | |
1153 | strcpy(download_url,url); | |
1154 | download_count++; | |
936c9905 FM |
1155 | } |
1156 | ||
9c7c6346 FM |
1157 | // remove any protocol:// at the beginning of the URL |
1158 | if ((str = strchr(url,'/')) != NULL && str[1] == '/') { | |
1159 | int i; | |
1160 | ||
1161 | str+=2; | |
1162 | for (i=0 ; str[i] ; i++) | |
1163 | url[i]=str[i]; | |
1164 | url[i]='\0'; | |
936c9905 FM |
1165 | } |
1166 | ||
246c8489 | 1167 | if(!LongUrl) { |
4157aa09 FM |
1168 | char *endofhost=strchr(url,'/'); |
1169 | if (endofhost) | |
1170 | *endofhost='\0'; | |
1171 | if(strlen(url) > 512 && (endofhost=strchr(url,'%')) != NULL) { | |
1172 | *endofhost='\0'; | |
936c9905 FM |
1173 | } |
1174 | } | |
1175 | ||
58d7d8fe FM |
1176 | if(ilf==ILF_Squid) { |
1177 | tt=atoi(data); | |
1178 | t=localtime(&tt); | |
1179 | ||
1180 | strftime(tbuf2, sizeof(tbuf2), "%H%M", t); | |
e6414a9d FM |
1181 | sprintf(mes,"%d",t->tm_mon+1); |
1182 | conv_month_name(mes); | |
58d7d8fe | 1183 | if(strncmp(df,"u",1) == 0) |
e6414a9d | 1184 | sprintf(tbuf, "%04d%s%02d", t->tm_year+1900, mes, t->tm_mday); |
58d7d8fe | 1185 | if(strncmp(df,"e",1) == 0) |
e6414a9d | 1186 | sprintf(tbuf, "%02d%s%04d", t->tm_mday, mes, t->tm_year+1900); |
58d7d8fe FM |
1187 | if(strncmp(df,"w",1) == 0) { |
1188 | strcpy(IndexTree,"file"); | |
1189 | strftime(tbuf, sizeof(tbuf), "%Y.%U", t); | |
1190 | } | |
936c9905 | 1191 | |
58d7d8fe FM |
1192 | strftime(wdata, sizeof(wdata), "%Y%m%d", t); |
1193 | idata=atoi(wdata); | |
936c9905 | 1194 | |
58d7d8fe FM |
1195 | if(strncmp(df,"u",1)==0) |
1196 | strftime(dia, sizeof(dia), "%m/%d/%Y", t); | |
1197 | else | |
1198 | strftime(dia, sizeof(dia), "%d/%m/%Y", t); | |
1199 | sprintf(hora,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec); | |
1200 | } else if(ilf==ILF_Common || ilf==ILF_Isa) { | |
9c7c6346 FM |
1201 | getword_start(&gwarea,data+1); |
1202 | if (getword_multisep(data,sizeof(data),&gwarea,':')<0){ | |
58d7d8fe FM |
1203 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1204 | exit(1); | |
1205 | } | |
9c7c6346 | 1206 | if (getword_multisep(hora,sizeof(hora),&gwarea,' ')<0){ |
58d7d8fe FM |
1207 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1208 | exit(1); | |
1209 | } | |
9c7c6346 FM |
1210 | getword_start(&gwarea,data); |
1211 | if (getword_multisep(dia,sizeof(dia),&gwarea,'/')<0){ | |
58d7d8fe FM |
1212 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1213 | exit(1); | |
936c9905 | 1214 | } |
9c7c6346 | 1215 | if (getword_multisep(mes,sizeof(mes),&gwarea,'/')<0){ |
58d7d8fe FM |
1216 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1217 | exit(1); | |
1218 | } | |
9c7c6346 | 1219 | if (getword_multisep(ano,sizeof(ano),&gwarea,'/')<0){ |
58d7d8fe FM |
1220 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1221 | exit(1); | |
1222 | } | |
1223 | ||
1224 | if(strcmp(df,"u") == 0) | |
1225 | snprintf(tbuf,sizeof(tbuf),"%s%s%s",ano,mes,dia); | |
1226 | if(strcmp(df,"e") == 0) | |
1227 | snprintf(tbuf,sizeof(tbuf),"%s%s%s",dia,mes,ano); | |
1228 | builddia(dia,mes,ano,df,wdata); | |
1229 | idata=atoi(wdata); | |
1230 | } else if (ilf==ILF_Sarg) { | |
9c7c6346 FM |
1231 | getword_start(&gwarea,data); |
1232 | if (getword_multisep(mes,sizeof(mes),&gwarea,'/')<0){ | |
936c9905 FM |
1233 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1234 | exit(1); | |
1235 | } | |
9c7c6346 | 1236 | if (getword_multisep(dia,sizeof(dia),&gwarea,'/')<0){ |
936c9905 FM |
1237 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1238 | exit(1); | |
1239 | } | |
9c7c6346 | 1240 | if (getword_multisep(ano,sizeof(ano),&gwarea,0)<0){ |
936c9905 FM |
1241 | printf("SARG: Maybe you have a broken date in your %s file.\n",arq); |
1242 | exit(1); | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | if(debugm) | |
1247 | printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil); | |
1248 | ||
1249 | l=1; | |
936c9905 | 1250 | if(l){ |
5542cb59 | 1251 | if(addr[0] != '\0'){ |
936c9905 FM |
1252 | if(strcmp(addr,ip)==0) |
1253 | l=1;else l=0; | |
1254 | } | |
1255 | if(fhost) { | |
94ff9470 | 1256 | // l=vhexclude(excludefile,ip); |
936c9905 | 1257 | l=vhexclude(excludefile,url); |
5160b452 FM |
1258 | if(!l) { |
1259 | if (debugm) printf("Excluded site: %s\n",url); | |
936c9905 | 1260 | totregsx++; |
5160b452 | 1261 | } |
936c9905 FM |
1262 | } |
1263 | } | |
25697a35 | 1264 | |
936c9905 | 1265 | if(l){ |
5542cb59 | 1266 | if(date[0] != '\0'){ |
936c9905 FM |
1267 | if(idata >= dfrom && idata <= duntil) |
1268 | l=1;else l=0; | |
1269 | } | |
1270 | } | |
1271 | if(l){ | |
5542cb59 | 1272 | if(hm[0] != '\0') { |
9c7c6346 | 1273 | bzero(hmr,sizeof(hmr)); |
936c9905 | 1274 | chm++; |
9c7c6346 | 1275 | getword_start(&gwarea,hora); |
936c9905 | 1276 | while(chm) { |
9c7c6346 | 1277 | if (getword_multisep(warea,sizeof(warea),&gwarea,':')<0){ |
936c9905 FM |
1278 | printf("SARG: Maybe you have a broken time in your %s file.\n",arq); |
1279 | exit(1); | |
1280 | } | |
1281 | strncat(hmr,warea,2); | |
1282 | chm--; | |
1283 | } | |
9c7c6346 | 1284 | strncat(hmr,gwarea.current,2); |
25697a35 | 1285 | |
936c9905 FM |
1286 | if(atoi(hmr) >= atoi(hm) && atoi(hmr) <= atoi(hmf)) |
1287 | l=1;else l=0; | |
1288 | } | |
1289 | } | |
1290 | if(l){ | |
5542cb59 | 1291 | if(site[0] != '\0'){ |
936c9905 FM |
1292 | if(strstr(url,site)!=0) |
1293 | l=1;else l=0; | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | if(userip) | |
1298 | strcpy(user,ip); | |
1299 | ||
1300 | if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) { | |
1301 | if(strcmp(RecordsWithoutUser,"ip") == 0) | |
2357ef77 | 1302 | strcpy(user,ip); |
936c9905 | 1303 | if(strcmp(RecordsWithoutUser,"ignore") == 0) |
2357ef77 | 1304 | continue; |
936c9905 | 1305 | if(strcmp(RecordsWithoutUser,"everybody") == 0) |
2357ef77 | 1306 | strcpy(user,"everybody"); |
936c9905 | 1307 | } |
120d768c FM |
1308 | if(us[0] != '\0'){ |
1309 | if(strcmp(user,us)==0) | |
1310 | l=1; | |
1311 | else | |
1312 | l=0; | |
1313 | } | |
936c9905 FM |
1314 | |
1315 | if(dotinuser) { | |
48864d28 | 1316 | subs(user,sizeof(user),"_","."); |
936c9905 FM |
1317 | dotinuser=0; |
1318 | } | |
1319 | ||
1320 | if(puser) { | |
1321 | sprintf(wuser,":%s:",user); | |
1322 | if(strstr(userfile, wuser) == 0) | |
1323 | continue; | |
1324 | } | |
1325 | ||
1326 | if(l) { | |
1327 | if(fuser) { | |
1328 | l=vuexclude(excludeuser,user); | |
5160b452 FM |
1329 | if(!l) { |
1330 | if (debugm) printf("Excluded user: %s\n",user); | |
936c9905 | 1331 | totregsx++; |
5160b452 | 1332 | } |
936c9905 FM |
1333 | } |
1334 | } | |
1335 | ||
1336 | if(l) { | |
1337 | if(userip) | |
1338 | fixip(user); | |
1339 | } | |
1340 | ||
5542cb59 | 1341 | if(l && max_elapsed) { |
936c9905 FM |
1342 | if(atol(elap)>max_elapsed) { |
1343 | elap[0]='0'; | |
1344 | elap[1]='\0'; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | if(l) { | |
5542cb59 | 1349 | if(strcmp(user,"-") !=0 && url[0] != '\0' && strcmp(user," ") !=0 && strcmp(user,"") !=0 && strcmp(user,":") !=0){ |
936c9905 | 1350 | if((str=(char *) strstr(bufz, "[SmartFilter:")) != (char *) NULL ) { |
120d768c | 1351 | fixendofline(str); |
936c9905 FM |
1352 | sprintf(smartfilter,"\"%s\"",str+1); |
1353 | } else sprintf(smartfilter,"\"\""); | |
1354 | ||
120d768c | 1355 | sprintf(bufz, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter); |
25697a35 | 1356 | |
936c9905 FM |
1357 | if ( strcmp ( user , sz_Last_User ) != 0 ) { |
1358 | if ( fp_Write_User ) | |
1359 | fclose( fp_Write_User ) ; | |
1360 | sprintf (tmp3, "%s/sarg/%s.unsort", tmp, user); | |
1361 | ||
1362 | if ((fp_Write_User = MY_FOPEN (tmp3, "a")) == NULL) { | |
1363 | fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno)); | |
1364 | exit (1); | |
25697a35 | 1365 | } |
936c9905 FM |
1366 | strcpy( sz_Last_User , user ) ; |
1367 | } | |
1368 | fputs (bufz, fp_Write_User); | |
25697a35 | 1369 | |
58d7d8fe | 1370 | if(strcmp(ParsedOutputLog, "no") != 0 && ilf!=ILF_Sarg) |
936c9905 | 1371 | fputs(bufz,fp_log); |
25697a35 | 1372 | |
936c9905 | 1373 | totregsg++; |
25697a35 | 1374 | |
936c9905 FM |
1375 | if(download_flag && strstr(code,"DENIED") == 0) { |
1376 | ndownload = 1; | |
25697a35 | 1377 | |
936c9905 FM |
1378 | if ( ! fp_Download_Unsort ) { |
1379 | if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) { | |
1380 | fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], text[9], tmp3, strerror(errno)); | |
1381 | exit (1); | |
1382 | } | |
1383 | } | |
120d768c | 1384 | fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url); |
936c9905 FM |
1385 | } |
1386 | ||
1387 | if(strstr(ReportType,"denied") != 0 || strstr(ReportType,"auth_failures") != 0) { | |
1388 | if(strstr(code,"DENIED/403") != 0) { | |
120d768c | 1389 | fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly); |
936c9905 FM |
1390 | denied_count++; |
1391 | } | |
1392 | if(strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0) { | |
1393 | if(fp_authfail) | |
120d768c | 1394 | fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly); |
936c9905 FM |
1395 | authfail_count++; |
1396 | } | |
1397 | } | |
1398 | ||
58d7d8fe | 1399 | if((!totper || idata<mindate) && ilf!=ILF_Sarg){ |
936c9905 FM |
1400 | totper++; |
1401 | mindate=idata; | |
1402 | sprintf(period,"%s-",tbuf); | |
1403 | sprintf(per_hour,"%s-",tbuf2); | |
120d768c | 1404 | if(date[0] != '\0') |
936c9905 FM |
1405 | fixper(tbuf, period, cduntil); |
1406 | if(debugz){ | |
1407 | debugaz("tbuf",tbuf); | |
1408 | debugaz("period",period); | |
1409 | } | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | if(debugm){ | |
1414 | printf("IP=\t%s\n",ip); | |
1415 | printf("USER=\t%s\n",user); | |
1416 | printf("ELAP=\t%s\n",elap); | |
1417 | printf("DATE=\t%s\n",dia); | |
1418 | printf("TIME=\t%s\n",hora); | |
1419 | printf("FUNC=\t%s\n",fun); | |
1420 | printf("URL=\t%s\n",url); | |
1421 | printf("CODE=\t%s\n",code); | |
1422 | printf("LEN=\t%s\n",tam); | |
1423 | } | |
1424 | } | |
25697a35 | 1425 | } |
58d7d8fe FM |
1426 | if (!from_stdin) { |
1427 | fclose(fp_in); | |
e6414a9d | 1428 | if( ShowReadStatistics ) |
7c0b0078 | 1429 | printf("SARG: Records in file: %lu, reading: %3.2f%%\n",recs1, (float) 100 ); |
58d7d8fe | 1430 | } |
25697a35 GS |
1431 | } |
1432 | ||
2357ef77 | 1433 | if ( fp_Download_Unsort ) |
25697a35 GS |
1434 | fclose (fp_Download_Unsort); |
1435 | ||
2357ef77 | 1436 | if (fp_Write_User) |
25697a35 GS |
1437 | fclose (fp_Write_User); |
1438 | ||
1439 | if(debug) { | |
58d7d8fe FM |
1440 | int totalcount=0; |
1441 | ||
1442 | for (ilf=0 ; ilf<ILF_Last ; ilf++) totalcount+=ilf_count[ilf]; | |
1443 | ||
d2fe0c32 | 1444 | debuga(" %s: %ld, %s: %ld, %s: %ld",text[10],totregsl,text[11],totregsg,text[68],totregsx); |
936c9905 | 1445 | |
58d7d8fe | 1446 | if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]>0) |
d2fe0c32 | 1447 | debuga("%s",text[12]); |
936c9905 | 1448 | |
58d7d8fe | 1449 | if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0) |
d2fe0c32 | 1450 | debuga("%s",text[13]); |
936c9905 | 1451 | |
58d7d8fe | 1452 | if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0) |
d2fe0c32 | 1453 | debuga("%s",text[14]); |
25697a35 | 1454 | |
58d7d8fe | 1455 | if(ilf_count[ILF_Sarg]>0) |
d2fe0c32 | 1456 | debuga("%s",text[124]); |
936c9905 | 1457 | |
58d7d8fe | 1458 | if(totalcount==0) { |
25697a35 GS |
1459 | if(!totregsg) { |
1460 | fprintf(stderr, "SARG: %s\n",text[16]); | |
1461 | fprintf(stderr, "SARG: %s\n",text[21]); | |
936c9905 | 1462 | } else fprintf(stderr, "SARG: %s\n",text[15]); |
25697a35 | 1463 | bzero(msg,sizeof(msg)); |
25697a35 GS |
1464 | if(fp_denied) |
1465 | fclose(fp_denied); | |
1466 | if(fp_authfail) | |
1467 | fclose(fp_authfail); | |
3d254696 FM |
1468 | free_excludecodes(); |
1469 | if(userfile) | |
1470 | free(userfile); | |
1471 | if(excludefile) | |
1472 | free(excludefile); | |
1473 | if(excludeuser) | |
1474 | free(excludeuser); | |
6e792ade | 1475 | free_download(); |
32e71fa4 FM |
1476 | unlink(tmp4); |
1477 | unlink(tmp6); | |
25697a35 GS |
1478 | unlink(tmp3); |
1479 | exit(0); | |
1480 | } | |
1481 | } | |
936c9905 | 1482 | |
25697a35 GS |
1483 | if(!totregsg){ |
1484 | fprintf(stderr, "SARG: %s\n",text[16]); | |
1485 | fprintf(stderr, "SARG: %s\n",text[21]); | |
25697a35 GS |
1486 | // fclose(fp_ou); |
1487 | if(fp_denied) | |
1488 | fclose(fp_denied); | |
1489 | if(fp_authfail) | |
1490 | fclose(fp_authfail); | |
3d254696 FM |
1491 | free_excludecodes(); |
1492 | if(userfile) | |
1493 | free(userfile); | |
1494 | if(excludefile) | |
1495 | free(excludefile); | |
1496 | if(excludeuser) | |
1497 | free(excludeuser); | |
6e792ade | 1498 | free_download(); |
25697a35 GS |
1499 | exit(0); |
1500 | } | |
1501 | ||
58d7d8fe | 1502 | if(date[0] == '\0' && ilf_count[ILF_Sarg]==0) { |
d6e703cc | 1503 | strcat(period,tbuf); |
25697a35 GS |
1504 | strcat(per_hour,tbuf2); |
1505 | } | |
1506 | ||
1507 | if(debugz){ | |
1508 | debugaz("data",dia); | |
1509 | debugaz("tbuf",tbuf); | |
d6e703cc | 1510 | debugaz("period",period); |
25697a35 GS |
1511 | } |
1512 | ||
d2fe0c32 FM |
1513 | if(debug) |
1514 | debuga("%s: %s",text[17],period); | |
25697a35 | 1515 | |
25697a35 GS |
1516 | // fclose(fp_ou); |
1517 | if(fp_denied) | |
1518 | fclose(fp_denied); | |
1519 | if(fp_authfail) | |
1520 | fclose(fp_authfail); | |
1521 | ||
58d7d8fe | 1522 | if(fp_log != NULL) { |
2357ef77 | 1523 | fclose(fp_log); |
9c7c6346 FM |
1524 | getword_start(&gwarea,period); |
1525 | if (getword_multisep(val2,sizeof(val2),&gwarea,'-')<0){ | |
1526 | printf("SARG: Maybe you have a broken date range definition.\n"); | |
1527 | exit(1); | |
1528 | } | |
1529 | if (getword_multisep(val1,sizeof(val1),&gwarea,'\0')<0){ | |
4bcb77cf FM |
1530 | printf("SARG: Maybe you have a broken date range definition.\n"); |
1531 | exit(1); | |
1532 | } | |
9c7c6346 FM |
1533 | getword_start(&gwarea,per_hour); |
1534 | if (getword_multisep(val3,sizeof(val3),&gwarea,'-')<0){ | |
4bcb77cf FM |
1535 | printf("SARG: Maybe you have a broken date range definition.\n"); |
1536 | exit(1); | |
1537 | } | |
9c7c6346 | 1538 | sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,val3,val1,gwarea.current); |
25697a35 GS |
1539 | rename(arq_log,val4); |
1540 | strcpy(arq_log,val4); | |
1541 | ||
1542 | if(strcmp(ParsedOutputLogCompress,"nocompress") != 0) { | |
9a2efbd0 | 1543 | sprintf(val1,"\"%s\" \"%s\"",ParsedOutputLogCompress,arq_log); |
456d78a5 FM |
1544 | cstatus=system(val1); |
1545 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
1546 | fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); | |
1547 | fprintf(stderr, "SARG: command: %s\n",val1); | |
1548 | exit(1); | |
1549 | } | |
25697a35 GS |
1550 | } |
1551 | ||
d2fe0c32 FM |
1552 | if(debug) |
1553 | debuga("%s %s",text[123],arq_log); | |
25697a35 | 1554 | } |
25697a35 GS |
1555 | |
1556 | if(strstr(ReportType,"denied") != 0) { | |
9a2efbd0 | 1557 | sprintf(csort,"sort -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,tmp5,tmp4); |
456d78a5 FM |
1558 | cstatus=system(csort); |
1559 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
1560 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
1561 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
1562 | exit(1); | |
1563 | } | |
25697a35 GS |
1564 | unlink(tmp4); |
1565 | } | |
936c9905 | 1566 | |
491b862f | 1567 | sort_users_log(tmp, debug); |
25697a35 | 1568 | |
4157aa09 | 1569 | if(DataFile[0] != '\0') |
25697a35 GS |
1570 | data_file(tmp); |
1571 | else | |
1572 | gerarel(); | |
1573 | ||
1574 | unlink(tmp2); | |
1575 | if(strstr(ReportType,"denied") != 0) | |
1576 | unlink(tmp5); | |
936c9905 | 1577 | |
5542cb59 | 1578 | if(zip[0] != '\0' && strcmp(zip,"zcat") !=0) { |
936c9905 FM |
1579 | recomp(arq, zip); |
1580 | } | |
25697a35 GS |
1581 | // else unlink(arq); |
1582 | ||
d6e703cc | 1583 | if(strcmp(tmp,"/tmp") != 0) { |
05b90947 FM |
1584 | if (snprintf(tmp4,sizeof(tmp4),"rm -rf \"%s\"",tmp)>=sizeof(tmp4)) { |
1585 | fprintf(stderr,"SARG: Directory name too long: %s\n",tmp); | |
1586 | exit(1); | |
1587 | } | |
1588 | cstatus=system(tmp4); | |
1589 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
1590 | fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); | |
1591 | fprintf(stderr, "SARG: command: %s\n",tmp4); | |
1592 | exit(1); | |
1593 | } | |
d6e703cc | 1594 | } |
25697a35 | 1595 | |
48864d28 | 1596 | free_excludecodes(); |
491b862f GS |
1597 | if(userfile) |
1598 | free(userfile); | |
1599 | if(excludefile) | |
1600 | free(excludefile); | |
1601 | if(excludeuser) | |
1602 | free(excludeuser); | |
6e792ade | 1603 | free_download(); |
491b862f | 1604 | |
d6e703cc | 1605 | if(debug) |
d2fe0c32 | 1606 | debuga("%s",text[21]); |
d6e703cc | 1607 | |
25697a35 GS |
1608 | exit(0); |
1609 | ||
1610 | } | |
1611 | ||
1612 | ||
936c9905 | 1613 | static void getusers(const char *pwdfile, int debug) |
25697a35 GS |
1614 | { |
1615 | ||
1616 | FILE *fp_usr; | |
1617 | char buf[255]; | |
936c9905 | 1618 | char *str; |
39186a2f | 1619 | long int nreg=0; |
25697a35 | 1620 | |
d2fe0c32 FM |
1621 | if(debug) |
1622 | debuga("%s: %s",text[60],pwdfile); | |
25697a35 GS |
1623 | |
1624 | if ((fp_usr = fopen(pwdfile, "r")) == NULL) { | |
1625 | fprintf(stderr, "SARG: (getusers) %s: %s - %s\n",text[45],pwdfile,strerror(errno)); | |
1626 | exit(1); | |
1627 | } | |
1628 | ||
ddd6ce01 | 1629 | fseek(fp_usr, 0, SEEK_END); |
d6dfc139 | 1630 | nreg = ftell(fp_usr); |
5355d243 FM |
1631 | if (nreg<0) { |
1632 | printf("SARG: Cannot get the size of file %s",pwdfile); | |
1633 | exit(1); | |
1634 | } | |
936c9905 | 1635 | nreg = nreg+5000; |
ddd6ce01 | 1636 | fseek(fp_usr, 0, SEEK_SET); |
25697a35 GS |
1637 | |
1638 | if((userfile=(char *) malloc(nreg))==NULL){ | |
1639 | fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg); | |
1640 | exit(1); | |
1641 | } | |
1642 | ||
1643 | bzero(userfile,nreg); | |
936c9905 FM |
1644 | strcpy(userfile,":"); |
1645 | ||
1646 | while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { | |
1647 | str=strchr(buf,':'); | |
1648 | if (!str) { | |
1649 | printf("SARG: You have an invalid user in your %s file.\n",pwdfile); | |
1650 | exit(1); | |
1651 | } | |
1652 | str[1]=0; | |
1653 | strcat(userfile,buf); | |
25697a35 GS |
1654 | } |
1655 | ||
1656 | fclose(fp_usr); | |
1657 | ||
1658 | return; | |
1659 | } | |
1660 | ||
1661 | ||
2357ef77 | 1662 | static void gethexclude(const char *hexfile, int debug) |
25697a35 GS |
1663 | { |
1664 | ||
1665 | FILE *fp_ex; | |
1666 | char buf[255]; | |
39186a2f | 1667 | long int nreg=0; |
25697a35 | 1668 | |
d2fe0c32 FM |
1669 | if(debug) |
1670 | debuga("%s: %s",text[67],hexfile); | |
25697a35 GS |
1671 | |
1672 | if ((fp_ex = fopen(hexfile, "r")) == NULL) { | |
1673 | fprintf(stderr, "SARG: (gethexclude) %s: %s - %s\n",text[45],hexfile,strerror(errno)); | |
1674 | exit(1); | |
1675 | } | |
1676 | ||
ddd6ce01 | 1677 | fseek(fp_ex, 0, SEEK_END); |
5355d243 FM |
1678 | nreg = ftell(fp_ex); |
1679 | if (nreg<0) { | |
1680 | printf("SARG: Cannot get the size of file %s",hexfile); | |
1681 | exit(1); | |
1682 | } | |
1683 | nreg += 11; | |
ddd6ce01 | 1684 | fseek(fp_ex, 0, SEEK_SET); |
25697a35 | 1685 | |
2357ef77 | 1686 | if((excludefile=(char *) malloc(nreg))==NULL){ |
25697a35 GS |
1687 | fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg); |
1688 | exit(1); | |
1689 | } | |
1690 | ||
2357ef77 | 1691 | bzero(excludefile,nreg); |
25697a35 | 1692 | |
2357ef77 FM |
1693 | while(fgets(buf,sizeof(buf),fp_ex)!=NULL){ |
1694 | if(strchr(buf,'#') != NULL) | |
25697a35 | 1695 | continue; |
2357ef77 | 1696 | fixendofline(buf); |
25697a35 GS |
1697 | strcat(excludefile,buf); |
1698 | strcat(excludefile," "); | |
1699 | } | |
1700 | ||
d6e703cc | 1701 | strcat(excludefile,"*END* "); |
25697a35 GS |
1702 | |
1703 | fclose(fp_ex); | |
1704 | ||
1705 | return; | |
1706 | } | |
1707 | ||
1708 | ||
2357ef77 | 1709 | static void getuexclude(const char *uexfile, int debug) |
25697a35 GS |
1710 | { |
1711 | ||
1712 | FILE *fp_ex; | |
1713 | char buf[255]; | |
39186a2f | 1714 | long int nreg=0; |
25697a35 | 1715 | |
d2fe0c32 FM |
1716 | if(debug) |
1717 | debuga("%s: %s",text[67],uexfile); | |
25697a35 GS |
1718 | |
1719 | if ((fp_ex = fopen(uexfile, "r")) == NULL) { | |
1720 | fprintf(stderr, "SARG: (gethexclude) %s: %s - %s\n",text[45],uexfile,strerror(errno)); | |
1721 | exit(1); | |
1722 | } | |
1723 | ||
ddd6ce01 | 1724 | fseek(fp_ex, 0, SEEK_END); |
5355d243 FM |
1725 | nreg = ftell(fp_ex); |
1726 | if (nreg<0) { | |
1727 | printf("SARG: Cannot get the size of file %s",uexfile); | |
1728 | exit(1); | |
1729 | } | |
1730 | nreg += 11; | |
ddd6ce01 | 1731 | fseek(fp_ex, 0, SEEK_SET); |
25697a35 | 1732 | |
2357ef77 | 1733 | if((excludeuser=(char *) malloc(nreg))==NULL){ |
25697a35 GS |
1734 | fprintf(stderr, "SARG: %s (%ld):\n",text[59],nreg); |
1735 | exit(1); | |
1736 | } | |
1737 | ||
2357ef77 | 1738 | bzero(excludeuser,nreg); |
25697a35 | 1739 | |
2357ef77 FM |
1740 | while(fgets(buf,sizeof(buf),fp_ex)!=NULL){ |
1741 | if(strchr(buf,'#') != NULL) | |
25697a35 | 1742 | continue; |
2357ef77 | 1743 | fixendofline(buf); |
25697a35 GS |
1744 | strcat(excludeuser,buf); |
1745 | strcat(excludeuser," "); | |
1746 | } | |
1747 | ||
d6e703cc | 1748 | strcat(excludeuser,"*END* "); |
25697a35 GS |
1749 | |
1750 | fclose(fp_ex); | |
1751 | ||
1752 | return; | |
1753 | } |