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