]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed FM |
2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
3 | * 1998, 2008 | |
94ff9470 | 4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
25697a35 GS |
5 | * |
6 | * SARG donations: | |
7 | * please look at http://sarg.sourceforge.net/donations.php | |
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 | // #define LEGACY_MY_ATOLL | |
27 | // #define LEGACY_TESTVALIDUSERCHAR | |
28 | ||
29 | #include "include/conf.h" | |
5f3cfd1d | 30 | #include "include/defs.h" |
25697a35 GS |
31 | |
32 | static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; | |
48864d28 FM |
33 | |
34 | //! The list of the HTTP codes to exclude from the report. | |
35 | static char *excludecode=NULL; | |
25697a35 | 36 | |
d6e703cc FM |
37 | /*void fgetword(char *word, char *line, int stop) |
38 | { | |
39 | //VARIANT N1 | |
40 | int x; | |
41 | ||
42 | for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x]; | |
43 | word[x] = '\0'; | |
44 | ||
45 | //VARIANT N2 | |
46 | char *tchar; | |
47 | int difflen; | |
48 | ||
49 | tchar = strchr(line, stop); | |
50 | if (tchar == NULL) strcpy(word, line); | |
51 | else | |
52 | { | |
53 | difflen = tchar - line; | |
54 | strncpy(word, line, difflen); | |
55 | word[difflen] = '\0'; | |
56 | } | |
57 | }*/ | |
58 | ||
4bcb77cf | 59 | int getword(char *word, int limit, char *line, int stop) |
25697a35 | 60 | { |
4bcb77cf | 61 | int x = 0,y; |
25697a35 | 62 | |
4bcb77cf | 63 | /*if(strlen(line) < 3) { |
25697a35 | 64 | word[0]='\0'; |
4bcb77cf FM |
65 | return(0); |
66 | }*/ | |
25697a35 | 67 | |
4bcb77cf FM |
68 | for(x=0;((line[x]) && (line[x] != stop ));x++) { |
69 | if(x>=limit) { | |
70 | printf("SARG: getword loop detected after %d bytes.\n",x); | |
71 | printf("SARG: Record=\"%s\"\n",line); | |
72 | printf("SARG: searching for \'x%x\'\n",stop); | |
73 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
74 | if (limit>0) word[limit-1]='\0'; | |
75 | //exit(1); | |
76 | return(-1); | |
77 | } | |
78 | word[x] = line[x]; | |
25697a35 GS |
79 | } |
80 | ||
81 | word[x] = '\0'; | |
d6e703cc | 82 | if (line[x]) ++x; |
4bcb77cf | 83 | y=0; |
25697a35 GS |
84 | |
85 | while((line[y++] = line[x++])); | |
4bcb77cf | 86 | return(0); |
25697a35 GS |
87 | } |
88 | ||
4bcb77cf FM |
89 | int getword_multisep(char *word, int limit, char *line, int stop) |
90 | { | |
91 | int x = 0,y; | |
92 | ||
4bcb77cf FM |
93 | for(x=0;((line[x]) && (line[x] != stop ));x++) { |
94 | if(x>=limit) { | |
95 | printf("SARG: getword_multisep loop detected.\n"); | |
96 | printf("SARG: Record=\"%s\"\n",line); | |
97 | printf("SARG: searching for \'x%x\'\n",stop); | |
98 | printf("SARG: searching for \'x%x\'\n",stop); | |
99 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
100 | if (limit>0) word[limit-1]='\0'; | |
101 | //exit(1); | |
102 | return(-1); | |
103 | } | |
104 | word[x] = line[x]; | |
105 | } | |
106 | ||
107 | word[x] = '\0'; | |
108 | while (line[x] && line[x]==stop) ++x; | |
109 | y=0; | |
110 | ||
111 | while((line[y++] = line[x++])); | |
112 | return(0); | |
113 | } | |
114 | ||
4bcb77cf | 115 | int getword3(char *word, int limit, char *line, int stop) |
25697a35 | 116 | { |
d6e703cc | 117 | int x = 0, y = 0; |
25697a35 | 118 | |
4bcb77cf FM |
119 | for(x=0; x<limit && (line[x] && (line[x] != stop)) ; x++) word[x] = line[x]; |
120 | if(x>=limit) { | |
121 | printf("SARG: getword3 loop detected after %d bytes.\n",x); | |
122 | printf("SARG: Buffer=\"%s\"\n",line); | |
123 | printf("SARG: searching for \'x%x\'\n",stop); | |
124 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
125 | //exit(1); | |
126 | return(-1); | |
127 | } | |
25697a35 GS |
128 | word[x] = '\0'; |
129 | if(line[x]) ++x; | |
25697a35 | 130 | while((line[y++] = line[x++])); |
4bcb77cf | 131 | return(0); |
25697a35 GS |
132 | } |
133 | ||
134 | ||
135 | #ifdef LEGACY_MY_ATOLL | |
136 | ||
137 | // BMG (bguillory@email.com) | |
138 | // 3 August 1999 | |
139 | long long int my_atoll (const char *nptr) | |
140 | #define MAXLLL 30 //maximum number of digits in long long (a guess) | |
141 | { | |
142 | int offset=0, x; | |
143 | long long int returnval=0; | |
144 | char one_digit[2]; | |
145 | ||
146 | one_digit[1]='\0'; | |
147 | ||
148 | // Soak up all the white space | |
149 | while (isspace(nptr[offset])) { | |
150 | offset++; | |
151 | } //while | |
152 | ||
153 | //For each character left to right | |
154 | //change the character to a single digit | |
155 | //multiply what we had before by 10 and add the new digit | |
156 | for(x=offset; x<=MAXLLL+offset && isdigit(nptr[x]); x++) { | |
157 | sprintf(one_digit, "%c", nptr[x]); //I don't know how else to do this | |
158 | returnval = (returnval * 10) + atoi(one_digit); | |
159 | } //for | |
160 | ||
161 | return returnval; | |
162 | ||
163 | } //my_atoll | |
164 | ||
165 | #else | |
166 | ||
48864d28 | 167 | #define MAXLLL 30 //!< Maximum number of digits in long long (a guess). |
25697a35 GS |
168 | long long int my_atoll (const char *nptr) |
169 | { | |
170 | long long int returnval=0; | |
32e71fa4 | 171 | const char * t = nptr ; |
25697a35 GS |
172 | int max_digits = MAXLLL ; |
173 | ||
174 | // Soak up all the white space | |
175 | while (isspace( *t )) { | |
176 | t++; | |
177 | } //while | |
178 | ||
179 | //For each character left to right | |
180 | //change the character to a single digit | |
181 | //multiply what we had before by 10 and add the new digit | |
182 | ||
183 | for( ; --max_digits && isdigit( *t ) ; t++ ) | |
184 | { | |
185 | returnval = ( returnval * 10 ) + ( *t - '0' ) ; | |
186 | } | |
187 | ||
188 | return returnval; | |
189 | ||
190 | } //my_atoll | |
191 | ||
192 | #endif | |
193 | ||
194 | ||
32e71fa4 | 195 | void my_mkdir(const char *name) |
25697a35 GS |
196 | { |
197 | char w0[255]; | |
198 | char w1[255]; | |
199 | char w2[255]; | |
200 | ||
201 | if(strncmp(name,".",1) == 0 || strncmp(name,"/",1) != 0) { | |
4bcb77cf | 202 | fprintf(stderr,"SARG: Invalid path (%s). Please, use absolute paths only.\n",name); |
25697a35 GS |
203 | fprintf(stderr,"SARG: process aborted.\n"); |
204 | exit(1); | |
205 | } | |
206 | ||
207 | strcpy(w0,name); | |
208 | strcpy(w2,"/"); | |
4bcb77cf FM |
209 | if (getword_multisep(w1,sizeof(w1),w0,'/')<0) { |
210 | printf("SARG: Maybe you have a broken record or garbage in the directory name %s.\n",name); | |
211 | exit(1); | |
212 | } | |
213 | while(strchr(w0,'/')) { | |
214 | if (getword_multisep(w1,sizeof(w1),w0,'/')<0) { | |
215 | printf("SARG: Maybe you have a broken record or garbage in the directory name %s.\n",name); | |
216 | exit(1); | |
217 | } | |
25697a35 GS |
218 | strcat(w2,w1); |
219 | if(access(w2, R_OK) != 0) { | |
220 | if(mkdir(w2,0755)) { | |
221 | fprintf(stderr,"SARG: mkdir %s %s\n",w2,strerror(errno)); | |
222 | fprintf(stderr,"SARG: process aborted.\n"); | |
223 | exit(1); | |
224 | } | |
225 | } | |
226 | strcat(w2,"/"); | |
227 | } | |
228 | strcat(w2,w0); | |
229 | if(access(w2, R_OK) != 0) { | |
230 | if(mkdir(w2,0755)) { | |
231 | fprintf(stderr,"SARG: mkdir %s %s\n",w2,strerror(errno)); | |
232 | fprintf(stderr,"SARG: process aborted.\n"); | |
233 | exit(1); | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
238 | ||
239 | void my_lltoa(unsigned long long int n, char s[], int len) | |
240 | { | |
241 | int i = 0; | |
242 | int x = 0; | |
243 | char ww[50]; | |
244 | do { | |
245 | s[i++] = (n % 10) + '0'; | |
246 | } while ((n /= 10) > 0); | |
247 | s[i] = '\0'; | |
248 | { | |
249 | int c,i,j; | |
250 | for (i = 0, j = strlen(s)-1; i<j; i++, j--) | |
251 | { | |
252 | c = s[i]; | |
253 | s[i] = s[j]; | |
254 | s[j] = c; | |
255 | } | |
256 | } | |
257 | ||
258 | if(len) { | |
259 | bzero(ww,sizeof(ww)); | |
260 | i=len-strlen(s)-1; | |
261 | for(x=0; x<=i; x++) | |
262 | ww[x]='0'; | |
263 | i=strlen(s); | |
264 | strncat(ww,s,i>sizeof(ww)?sizeof(ww):i); | |
265 | strcpy(s,ww); | |
266 | } | |
267 | ||
268 | } | |
269 | ||
270 | ||
32e71fa4 | 271 | void builddia(char *dia, const char *mes, const char *ano, const char *df, char *wdata) |
25697a35 GS |
272 | { |
273 | char ndia[11]; | |
48864d28 | 274 | int nmes; |
25697a35 GS |
275 | |
276 | if(strlen(dia) < 1) return; | |
277 | ||
48864d28 FM |
278 | for(nmes=0; nmes<12; nmes++) { |
279 | if(strcmp(mtab1[nmes],mes) == 0) { | |
25697a35 GS |
280 | break; |
281 | } | |
282 | } | |
48864d28 | 283 | nmes++; |
25697a35 | 284 | |
48864d28 | 285 | snprintf(wdata,9,"%s%02d%s",ano,nmes,dia); |
25697a35 | 286 | |
48864d28 FM |
287 | if(df[0]!='u') |
288 | snprintf(ndia,sizeof(ndia),"%s/%02d/%s",dia,nmes,ano); | |
25697a35 | 289 | else |
48864d28 | 290 | snprintf(ndia,sizeof(ndia),"%02d/%s/%s",nmes,dia,ano); |
25697a35 GS |
291 | |
292 | strcpy(dia,ndia); | |
293 | ||
294 | } | |
295 | ||
296 | ||
32e71fa4 | 297 | void buildymd(const char *dia, const char *mes, const char *ano, char *wdata) |
25697a35 | 298 | { |
48864d28 | 299 | int nmes; |
25697a35 | 300 | |
48864d28 FM |
301 | for(nmes=0; nmes<12; nmes++) { |
302 | if(strcmp(mtab1[nmes],mes) == 0) | |
303 | break; | |
25697a35 GS |
304 | } |
305 | ||
48864d28 | 306 | sprintf(wdata,"%s%02d%s",ano,nmes+1,dia); |
25697a35 GS |
307 | |
308 | } | |
309 | ||
310 | ||
311 | void conv_month(char *month) | |
312 | { | |
313 | int x; | |
314 | ||
48864d28 FM |
315 | for(x=0; x<12 && strcmp(mtab1[x],month)!=0; x++); |
316 | sprintf(month,"%02d",x+1); | |
25697a35 GS |
317 | |
318 | } | |
319 | ||
320 | ||
321 | void conv_month_name(char *month) | |
322 | { | |
323 | int x; | |
324 | ||
48864d28 FM |
325 | x=atoi(month); |
326 | if (x>=1 && x<=12) | |
327 | strcpy(month,mtab1[x-1]); | |
25697a35 GS |
328 | } |
329 | ||
330 | ||
4bcb77cf | 331 | void name_month(char *month,int month_len) |
491b862f GS |
332 | { |
333 | int x, z=atoi(month)-1; | |
334 | char m[255]; | |
335 | char w[20]; | |
336 | ||
337 | strcpy(m,text[133]); | |
338 | ||
339 | for(x=0; x<z; x++) | |
4bcb77cf FM |
340 | if (getword_multisep(w,sizeof(w),m,',')<0) { |
341 | printf("SARG: Maybe you have a broken record or garbage in the names of the months.\n"); | |
342 | exit(1); | |
343 | } | |
344 | if (getword_multisep(month,month_len,m,',')<0) { | |
345 | printf("SARG: Maybe you have a broken record or garbage in the name of the months.\n"); | |
346 | exit(1); | |
347 | } | |
491b862f GS |
348 | } |
349 | ||
350 | ||
32e71fa4 | 351 | void fixper(char *tbuf, char *period, const char *duntil) |
25697a35 GS |
352 | { |
353 | ||
354 | char warea[50]; | |
355 | char dia[5], mes[5], ano[5]; | |
356 | int x; | |
357 | ||
358 | warea[0]='\0'; | |
359 | ||
360 | strncpy(dia,duntil+6,2); | |
361 | dia[2]='\0'; | |
362 | strncpy(mes,duntil+4,2); | |
363 | mes[2]='\0'; | |
364 | strncpy(ano,duntil,4); | |
365 | ano[4]='\0'; | |
366 | ||
48864d28 FM |
367 | x=atoi(mes); |
368 | if (x>=1 && x<=12) | |
369 | strcpy(mes,mtab1[x-1]); | |
25697a35 GS |
370 | |
371 | if(strcmp(df,"e") == 0) | |
372 | sprintf(warea,"%s%s%s",dia,mes,ano); | |
373 | if(strcmp(df,"u") == 0) | |
48864d28 | 374 | sprintf(warea,"%s%s%s",mes,dia,ano); |
25697a35 | 375 | |
d6e703cc | 376 | strcat(period,warea); |
25697a35 GS |
377 | } |
378 | ||
379 | ||
d2fe0c32 | 380 | void debuga(const char *msg,...) |
25697a35 | 381 | { |
d2fe0c32 | 382 | va_list ap; |
25697a35 | 383 | |
d2fe0c32 FM |
384 | fputs("SARG: ",stderr); |
385 | va_start(ap,msg); | |
386 | vfprintf(stderr,msg,ap); | |
387 | va_end(ap); | |
388 | fputc('\n',stderr); | |
25697a35 GS |
389 | } |
390 | ||
391 | ||
32e71fa4 | 392 | void debugaz(const char *head, const char *msg) |
25697a35 GS |
393 | { |
394 | fprintf(stderr, "SARG: (util) %s=%s\n",head, msg); | |
395 | ||
396 | } | |
397 | ||
398 | ||
399 | void fixip(char *ip) | |
400 | { | |
48864d28 FM |
401 | int i; |
402 | char sep='_'; | |
403 | char search=0; | |
404 | int nrepl=0; | |
405 | ||
406 | for (i=0; ip[i]; i++) { | |
407 | if (ip[i]=='.') { | |
408 | search='.'; | |
409 | sep='_'; | |
410 | break; | |
4bcb77cf | 411 | } |
48864d28 FM |
412 | if (ip[i]=='_') { |
413 | search='_'; | |
414 | sep='.'; | |
415 | break; | |
416 | } | |
417 | } | |
418 | for (; ip[i]; i++) { | |
419 | if (ip[i]==search) { | |
420 | ip[i]=sep; | |
421 | nrepl++; | |
422 | if (nrepl>=3) break; | |
4bcb77cf | 423 | } |
25697a35 | 424 | } |
25697a35 GS |
425 | } |
426 | ||
427 | ||
428 | char *fixnum(long long int value, int n) | |
25697a35 | 429 | { |
32e71fa4 FM |
430 | #define MAXLEN_FIXNUM 1024 |
431 | char num[MAXLEN_FIXNUM]; | |
432 | char buf[MAXLEN_FIXNUM * 2]; | |
25697a35 | 433 | char *pbuf; |
32e71fa4 | 434 | static char ret[MAXLEN_FIXNUM * 2]; |
25697a35 | 435 | char *pret; |
25697a35 GS |
436 | register int i, j, k; |
437 | static char abbrev[30]; | |
438 | ||
439 | my_lltoa(value, num, 0); | |
440 | ||
441 | if(strcmp(DisplayedValues,"abbreviation") == 0) { | |
442 | if(strlen(num) <= 3) | |
443 | sprintf(abbrev,"%s",num); | |
444 | if(strlen(num) == 4 || strlen(num) == 7 || strlen(num) == 10 || strlen(num) == 13) { | |
445 | snprintf(abbrev,2,"%s",num); | |
446 | strncat(abbrev,".",1); | |
447 | strncat(abbrev,num+1,2); | |
448 | if(!n) return(abbrev); | |
449 | if(strlen(num) == 4) | |
450 | strncat(abbrev,"K",1); | |
451 | else if(strlen(num) == 7) | |
452 | strncat(abbrev,"M",1); | |
453 | else if(strlen(num) == 10) | |
454 | strncat(abbrev,"G",1); | |
455 | else if(strlen(num) == 13) | |
456 | strncat(abbrev,"T",1); | |
457 | } | |
458 | if(strlen(num) == 5 || strlen(num) == 8 || strlen(num) == 11 || strlen(num) == 14) { | |
459 | snprintf(abbrev,3,"%s",num); | |
460 | strncat(abbrev,".",1); | |
461 | strncat(abbrev,num+2,2); | |
462 | if(!n) return(abbrev); | |
463 | if(strlen(num) == 5) | |
464 | strncat(abbrev,"K",1); | |
465 | else if(strlen(num) == 8) | |
466 | strncat(abbrev,"M",1); | |
467 | else if(strlen(num) == 11) | |
468 | strncat(abbrev,"G",1); | |
469 | else if(strlen(num) == 14) | |
470 | strncat(abbrev,"T",1); | |
471 | } | |
472 | if(strlen(num) == 6 || strlen(num) == 9 || strlen(num) == 12 || strlen(num) == 15) { | |
473 | snprintf(abbrev,4,"%s",num); | |
474 | strncat(abbrev,".",1); | |
475 | strncat(abbrev,num+3,2); | |
476 | if(!n) return(abbrev); | |
477 | if(strlen(num) == 6) | |
478 | strncat(abbrev,"K",1); | |
479 | else if(strlen(num) == 9) | |
480 | strncat(abbrev,"M",1); | |
481 | else if(strlen(num) == 12) | |
482 | strncat(abbrev,"G",1); | |
483 | else if(strlen(num) == 15) | |
484 | strncat(abbrev,"T",1); | |
485 | } | |
486 | ||
487 | return(abbrev); | |
488 | } | |
489 | ||
32e71fa4 | 490 | bzero(buf, MAXLEN_FIXNUM*2); |
25697a35 GS |
491 | |
492 | pbuf = buf; | |
493 | pret = ret; | |
494 | k = 0; | |
495 | ||
496 | for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { | |
497 | if ( k == 2 && i != 0 ) { | |
498 | k = 0; | |
499 | pbuf[j++] = num[i]; | |
500 | if(strcmp(UseComma,"yes") == 0) | |
501 | pbuf[j++] = ','; | |
502 | else pbuf[j++] = '.'; | |
503 | continue; | |
504 | } | |
505 | pbuf[j] = num[i]; | |
506 | j++; | |
507 | k++; | |
508 | } | |
509 | ||
510 | pret[0]='\0'; | |
511 | ||
512 | for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) | |
513 | pret[j] = pbuf[i]; | |
514 | ||
48864d28 | 515 | pret[j] = '\0'; |
25697a35 | 516 | |
48864d28 | 517 | return pret; |
25697a35 GS |
518 | } |
519 | ||
520 | ||
d6e703cc | 521 | char *fixnum2(long long int value, int n) |
d6e703cc | 522 | { |
32e71fa4 FM |
523 | #define MAXLEN_FIXNUM2 1024 |
524 | char num[MAXLEN_FIXNUM2]; | |
525 | char buf[MAXLEN_FIXNUM2 * 2]; | |
d6e703cc | 526 | char *pbuf; |
32e71fa4 | 527 | static char ret[MAXLEN_FIXNUM2 * 2]; |
d6e703cc FM |
528 | char *pret; |
529 | register int i, j, k; | |
d6e703cc FM |
530 | |
531 | my_lltoa(value, num, 0); | |
32e71fa4 | 532 | bzero(buf, MAXLEN_FIXNUM2*2); |
d6e703cc FM |
533 | |
534 | pbuf = buf; | |
535 | pret = ret; | |
536 | k = 0; | |
537 | ||
538 | for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { | |
539 | if ( k == 2 && i != 0 ) { | |
540 | k = 0; | |
541 | pbuf[j++] = num[i]; | |
542 | if(strcmp(UseComma,"yes") == 0) | |
543 | pbuf[j++] = ','; | |
544 | else pbuf[j++] = '.'; | |
545 | continue; | |
546 | } | |
547 | pbuf[j] = num[i]; | |
548 | j++; | |
549 | k++; | |
550 | } | |
551 | ||
552 | pret[0]='\0'; | |
553 | ||
554 | for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) | |
555 | pret[j] = pbuf[i]; | |
556 | ||
32e71fa4 | 557 | pret[j] = '\0'; |
d6e703cc | 558 | |
32e71fa4 | 559 | return pret; |
d6e703cc FM |
560 | } |
561 | ||
562 | ||
563 | ||
25697a35 GS |
564 | void buildhref(char * href) |
565 | { | |
566 | char whref[MAXLEN]; | |
567 | ||
568 | if(strcmp(href,"./") == 0){ | |
569 | href[0]='\0'; | |
570 | strcat(href,"<a href='"); | |
571 | return; | |
572 | } | |
573 | ||
574 | href[strlen(href)-1]='\0'; | |
575 | sprintf(whref,"%s",strrchr(href,'/')); | |
576 | ||
577 | strcpy(href,"<a href='"); | |
578 | strcat(href,whref); | |
579 | strcat(href,"/"); | |
580 | ||
581 | return; | |
582 | ||
583 | } | |
584 | ||
585 | ||
586 | char *buildtime(long long int elap) | |
587 | { | |
588 | ||
589 | int num = elap / 1000; | |
590 | int hor = 0; | |
591 | int min = 0; | |
592 | int sec = 0; | |
593 | static char buf[12]; | |
594 | ||
595 | buf[0]='\0'; | |
596 | ||
597 | hor=num / 3600; | |
598 | min=(num % 3600) / 60; | |
599 | sec=num % 60; | |
600 | sprintf(buf,"%02d:%02d:%02d",hor,min,sec); | |
601 | ||
602 | return(buf); | |
603 | ||
604 | } | |
605 | ||
606 | ||
32e71fa4 | 607 | void obtdate(const char *dirname, const char *name, char *data) |
25697a35 GS |
608 | { |
609 | ||
610 | FILE *fp_in; | |
611 | char wdir[MAXLEN]; | |
612 | ||
d6e703cc | 613 | sprintf(wdir,"%s%s/sarg-date",dirname,name); |
25697a35 | 614 | if ((fp_in = fopen(wdir, "r")) == 0) { |
d6e703cc FM |
615 | sprintf(wdir,"%s%s/date",dirname,name); |
616 | if ((fp_in = fopen(wdir, "r")) == 0) { | |
617 | data[0]='\0'; | |
618 | return; | |
619 | } | |
25697a35 GS |
620 | } |
621 | ||
622 | fgets(data,80,fp_in); | |
623 | fclose(fp_in); | |
624 | data[strlen(data)-1]='\0'; | |
625 | ||
626 | return; | |
627 | ||
628 | } | |
629 | ||
630 | ||
32e71fa4 | 631 | void obtuser(const char *dirname, const char *name, char *tuser) |
25697a35 GS |
632 | { |
633 | ||
634 | FILE *fp_in; | |
635 | char wdir[MAXLEN]; | |
636 | ||
d6e703cc FM |
637 | sprintf(wdir,"%s%s/sarg-users",dirname,name); |
638 | if((fp_in=fopen(wdir,"r"))==NULL) { | |
639 | sprintf(wdir,"%s%s/users",dirname,name); | |
640 | if((fp_in=fopen(wdir,"r"))==NULL) { | |
641 | tuser[0]='\0'; | |
642 | return; | |
643 | } | |
25697a35 GS |
644 | } |
645 | ||
646 | fgets(tuser,20,fp_in); | |
647 | tuser[strlen(tuser)-1]='\0'; | |
648 | fclose(fp_in); | |
649 | ||
650 | return; | |
651 | ||
652 | } | |
653 | ||
654 | ||
32e71fa4 | 655 | void obttotal(const char *dirname, const char *name, char *tbytes, char *tuser, char *media) |
25697a35 GS |
656 | { |
657 | ||
658 | FILE *fp_in; | |
659 | char wdir[MAXLEN]; | |
660 | long long int med=0; | |
661 | long long int wtuser=0; | |
48864d28 | 662 | long long int twork; |
25697a35 | 663 | |
d6e703cc | 664 | sprintf(wdir,"%s%s/sarg-general",dirname,name); |
25697a35 | 665 | if ((fp_in = fopen(wdir, "r")) == 0) { |
d6e703cc FM |
666 | sprintf(wdir,"%s%s/general",dirname,name); |
667 | if ((fp_in = fopen(wdir, "r")) == 0) { | |
668 | tbytes=0; | |
669 | return; | |
670 | } | |
25697a35 GS |
671 | } |
672 | ||
673 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
4bcb77cf FM |
674 | if (getword_multisep(warea,sizeof(warea),buf,' ')<0) { |
675 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir); | |
676 | exit(1); | |
677 | } | |
25697a35 GS |
678 | if(strcmp(warea,"TOTAL") != 0) |
679 | continue; | |
4bcb77cf FM |
680 | if (getword_multisep(warea,sizeof(warea),buf,' ')<0) { |
681 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir); | |
682 | exit(1); | |
683 | } | |
684 | if (getword_multisep(warea,sizeof(warea),buf,' ')<0) { | |
685 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir); | |
686 | exit(1); | |
687 | } | |
25697a35 | 688 | twork=my_atoll(warea); |
48864d28 | 689 | strcpy(tbytes,fixnum(twork,1)); |
25697a35 GS |
690 | } |
691 | fclose(fp_in); | |
692 | ||
48864d28 FM |
693 | wtuser=my_atoll(tuser); |
694 | if(wtuser == 0) { | |
695 | strcpy(media,"0"); | |
25697a35 GS |
696 | return; |
697 | } | |
48864d28 | 698 | |
25697a35 | 699 | med=my_atoll(warea) / wtuser; |
48864d28 | 700 | strcpy(media,fixnum(med,1)); |
25697a35 GS |
701 | |
702 | return; | |
703 | ||
704 | } | |
705 | ||
706 | ||
48864d28 | 707 | void gperiod(const char *dirname, const char *period) |
25697a35 | 708 | { |
48864d28 | 709 | |
25697a35 | 710 | FILE *fp_ou; |
25697a35 GS |
711 | char wdirname[MAXLEN]; |
712 | ||
713 | strcpy(wdirname,dirname); | |
714 | strcat(wdirname,"/"); | |
d6e703cc | 715 | strcat(wdirname,"sarg-period"); |
25697a35 GS |
716 | |
717 | if((fp_ou=fopen(wdirname,"w"))==NULL){ | |
718 | fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname); | |
719 | exit(1); | |
720 | } | |
48864d28 | 721 | |
d6e703cc | 722 | fputs(period,fp_ou); |
25697a35 GS |
723 | fclose(fp_ou); |
724 | ||
725 | if(debug) | |
d2fe0c32 | 726 | debuga("%s",(char *)text[50]); |
48864d28 | 727 | |
25697a35 GS |
728 | return; |
729 | ||
730 | } | |
731 | ||
32e71fa4 | 732 | void vrfydir(const char *dir, const char *per1, const char *addr, const char *site, const char *us, const char *form) |
25697a35 GS |
733 | { |
734 | FILE *img_in, *img_ou; | |
735 | int num=1, count=0; | |
736 | int c; | |
737 | char wdir[MAXLEN]; | |
738 | char per2[MAXLEN]; | |
739 | char dirname2[MAXLEN]; | |
25697a35 GS |
740 | char images[512]; |
741 | DIR *dirp; | |
742 | struct dirent *direntp; | |
456d78a5 | 743 | int cstatus; |
32e71fa4 FM |
744 | char y1[5], y2[5]; |
745 | char d1[3], d2[3]; | |
746 | char m1[4], m2[4]; | |
25697a35 | 747 | |
491b862f GS |
748 | if(strcmp(IndexTree,"date") == 0) { |
749 | bzero(y1,5); | |
750 | bzero(y2,5); | |
751 | bzero(d1,3); | |
752 | bzero(d2,3); | |
753 | bzero(m1,4); | |
754 | bzero(m2,4); | |
755 | if(strncmp(df,"u",1) == 0) { | |
d6e703cc FM |
756 | strncpy(y1,period,4); |
757 | strncpy(y2,period+10,4); | |
758 | strncpy(m1,period+4,3); | |
759 | strncpy(m2,period+14,3); | |
760 | strncpy(d1,period+7,2); | |
761 | strncpy(d2,period+17,2); | |
491b862f | 762 | } else if(strncmp(df,"e",1) == 0) { |
d6e703cc FM |
763 | strncpy(d1,period+0,2); |
764 | strncpy(d2,period+10,2); | |
765 | strncpy(m1,period+2,3); | |
766 | strncpy(m2,period+12,3); | |
767 | strncpy(y1,period+5,4); | |
768 | strncpy(y2,period+15,4); | |
491b862f GS |
769 | } |
770 | conv_month(m1); | |
771 | conv_month(m2); | |
772 | ||
773 | sprintf(wdir,"%s%s",outdir,y1); | |
774 | if(strcmp(y1,y2) != 0) { | |
775 | strncat(wdir,"-",1); | |
776 | strncat(wdir,y2,strlen(y2)); | |
777 | } | |
778 | if(access(wdir, R_OK) != 0) | |
779 | my_mkdir(wdir); | |
780 | ||
781 | strncat(wdir,"/",1); | |
782 | strncat(wdir,m1,strlen(m1)); | |
783 | if(strcmp(m1,m2) != 0) { | |
784 | strncat(wdir,"-",1); | |
785 | strncat(wdir,m2,strlen(m2)); | |
786 | } | |
787 | if(access(wdir, R_OK) != 0) | |
788 | my_mkdir(wdir); | |
789 | ||
790 | strncat(wdir,"/",1); | |
791 | strncat(wdir,d1,strlen(d1)); | |
792 | if(strcmp(d1,d2) != 0) { | |
793 | strncat(wdir,"-",1); | |
794 | strncat(wdir,d2,strlen(d2)); | |
795 | } | |
796 | } else | |
32e71fa4 | 797 | strcpy(wdir,dir); |
25697a35 GS |
798 | |
799 | if(strlen(us) > 0) { | |
800 | strcat(wdir,"-"); | |
801 | strcat(wdir,us); | |
802 | } | |
803 | if(strlen(addr) > 0) { | |
804 | strcat(wdir,"-"); | |
805 | strcat(wdir,addr); | |
806 | } | |
807 | if(strlen(site) > 0) { | |
808 | strcat(wdir,"-"); | |
809 | strcat(wdir,site); | |
810 | } | |
811 | ||
812 | if(strcmp(dirname,wdir) != 0) | |
813 | strcpy(dirname,wdir); | |
814 | ||
491b862f GS |
815 | if(strcmp(IndexTree,"date") != 0) { |
816 | strcpy(dirname2,dirname); | |
817 | if(strcmp(OverwriteReport,"no") == 0) { | |
818 | while(num) { | |
819 | if(access(wdir,R_OK) == 0) { | |
820 | sprintf(wdir,"%s.%d",dirname,num); | |
821 | sprintf(per2,"%s.%d",per1,num); | |
822 | num++; | |
823 | count++; | |
824 | } else | |
825 | break; | |
826 | } | |
25697a35 | 827 | |
491b862f GS |
828 | if(count > 0) { |
829 | if(debug) | |
830 | fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir); | |
831 | rename(dirname2,wdir); | |
832 | } | |
833 | } else { | |
834 | if(access(dir,R_OK) == 0) { | |
835 | sprintf(csort,"rm -r %s",dir); | |
836 | system(csort); | |
837 | } | |
25697a35 | 838 | } |
491b862f | 839 | my_mkdir(dirname); |
25697a35 | 840 | } else { |
491b862f GS |
841 | strcpy(dirname2,wdir); |
842 | if(strcmp(OverwriteReport,"no") == 0) { | |
843 | while(num) { | |
844 | if(access(wdir,R_OK) == 0) { | |
845 | sprintf(wdir,"%s.%d",dirname2,num); | |
846 | sprintf(per2,"%s.%d",per1,num); | |
847 | num++; | |
848 | count++; | |
849 | } else | |
850 | break; | |
851 | } | |
48864d28 | 852 | |
491b862f GS |
853 | if(count > 0) { |
854 | if(debug) | |
855 | fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir); | |
856 | rename(dirname2,wdir); | |
857 | strcpy(dirname2,wdir); | |
858 | } | |
859 | } else { | |
860 | if(access(wdir,R_OK) == 0) { | |
861 | sprintf(csort,"rm -r %s",wdir); | |
862 | system(csort); | |
863 | } | |
25697a35 | 864 | } |
48864d28 | 865 | |
491b862f GS |
866 | if(access(wdir, R_OK) != 0) |
867 | my_mkdir(wdir); | |
25697a35 GS |
868 | } |
869 | ||
25697a35 GS |
870 | strcpy(dirname2,wdir); |
871 | sprintf(images,"%simages",outdir); | |
872 | mkdir(images,0755); | |
873 | ||
456d78a5 FM |
874 | sprintf(wdir,"date \"+%%a %%b %%d %%H:%%M:%%S %%Z %%Y\" >%s/sarg-date",dirname); |
875 | cstatus=system(wdir); | |
876 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
877 | fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus)); | |
878 | fprintf(stderr, "SARG: command: %s\n",wdir); | |
879 | exit(1); | |
880 | } | |
25697a35 GS |
881 | |
882 | sprintf(per2,"%s/images",SYSCONFDIR); | |
883 | ||
884 | dirp = opendir(per2); | |
885 | if(dirp==NULL) { | |
886 | fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", per2,strerror(errno)); | |
887 | return; | |
888 | } | |
889 | while ((direntp = readdir( dirp )) != NULL ){ | |
890 | if(strncmp(direntp->d_name,".",1) == 0) | |
891 | continue; | |
892 | sprintf(val10,"%s/%s",per2,direntp->d_name); | |
893 | sprintf(val11,"%s/%s",images,direntp->d_name); | |
894 | img_in = fopen(val10, "rb"); | |
895 | if(img_in!=NULL) { | |
896 | img_ou = fopen(val11, "wb"); | |
897 | if(img_ou!=NULL) { | |
48864d28 | 898 | while ((c = fgetc(img_in))!=EOF) { |
25697a35 GS |
899 | fputc(c,img_ou); |
900 | } | |
25697a35 GS |
901 | fclose(img_ou); |
902 | } else | |
903 | fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", val11, strerror(errno)); | |
48864d28 | 904 | fclose(img_in); |
25697a35 GS |
905 | } else |
906 | fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", val10, strerror(errno)); | |
25697a35 GS |
907 | } |
908 | (void) rewinddir(dirp); | |
909 | (void) closedir(dirp); | |
910 | ||
911 | return; | |
912 | ||
913 | ||
914 | } | |
915 | ||
916 | ||
917 | void strip_latin(char *line) | |
918 | { | |
919 | char buf[255]; | |
920 | char warea[255]; | |
921 | ||
922 | while(strstr(line,"&") != 0){ | |
4bcb77cf FM |
923 | if (getword_multisep(warea,sizeof(warea),line,'&')<0) { |
924 | printf("SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities.\n"); | |
925 | exit(1); | |
926 | } | |
25697a35 | 927 | strncat(warea,line,1); |
4bcb77cf FM |
928 | if (getword_multisep(buf,sizeof(buf),line,';')<0) { |
929 | printf("SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities.\n"); | |
930 | exit(1); | |
931 | } | |
25697a35 GS |
932 | strcat(warea,line); |
933 | strcpy(line,warea); | |
934 | } | |
935 | ||
936 | return; | |
937 | ||
938 | } | |
939 | ||
32e71fa4 | 940 | void zdate(char *ftime, const char *DateFormat) |
25697a35 GS |
941 | { |
942 | ||
943 | time_t t; | |
944 | struct tm *local; | |
945 | ||
946 | t = time(NULL); | |
947 | local = localtime(&t); | |
948 | if(strcmp(DateFormat,"u") == 0) | |
949 | strftime(ftime, 127, "%b/%d/%Y %H:%M", local); | |
950 | if(strcmp(DateFormat,"e") == 0) | |
951 | strftime(ftime, 127, "%d/%b/%Y-%H:%M", local); | |
952 | if(strcmp(DateFormat,"w") == 0) | |
953 | strftime(ftime, 127, "%V-%H-%M", local); | |
954 | ||
955 | return; | |
956 | } | |
957 | ||
958 | ||
959 | char *fixtime(long int elap) | |
960 | { | |
961 | ||
962 | int num = elap / 1000; | |
963 | int hor = 0; | |
964 | int min = 0; | |
965 | int sec = 0; | |
966 | static char buf[12]; | |
967 | ||
968 | if(strcmp(datetimeby,"bytes") == 0) { | |
48864d28 | 969 | strcpy(buf,fixnum(elap,1)); |
25697a35 GS |
970 | return buf; |
971 | } | |
972 | ||
25697a35 GS |
973 | if(num<1) { |
974 | sprintf(buf,"00:00:%02ld",elap); | |
975 | return buf; | |
976 | } | |
977 | ||
978 | hor=num / 3600; | |
979 | min=(num % 3600) / 60; | |
980 | sec=num % 60; | |
981 | ||
48864d28 | 982 | if(hor==0 && min==0 && sec==0) |
25697a35 | 983 | strcpy(buf,"0"); |
48864d28 FM |
984 | else |
985 | sprintf(buf,"%01d:%02d:%02d",hor,min,sec); | |
25697a35 GS |
986 | |
987 | return buf; | |
988 | ||
989 | } | |
990 | ||
991 | ||
992 | void date_from(char *date, char *dfrom, char *duntil) | |
993 | { | |
994 | ||
995 | char diaf[10]; | |
996 | char mesf[10]; | |
997 | char anof[10]; | |
998 | char diau[10]; | |
999 | char mesu[10]; | |
1000 | char anou[10]; | |
1001 | static char wdate[50]; | |
1002 | ||
1003 | ||
1004 | strcpy(wdate,date); | |
48864d28 | 1005 | if(strchr(wdate,'-') == NULL) { |
25697a35 GS |
1006 | strcat(wdate,"-"); |
1007 | strcat(wdate,date); | |
1008 | strcpy(date,wdate); | |
1009 | } | |
1010 | ||
4bcb77cf FM |
1011 | if (getword(diaf,sizeof(diaf),wdate,'/')<0 || getword(mesf,sizeof(mesf),wdate,'/')<0 || getword(anof,sizeof(anof),wdate,'-')<0 || |
1012 | getword(diau,sizeof(diau),wdate,'/')<0 || getword(mesu,sizeof(mesu),wdate,'/')<0 || getword(anou,sizeof(anou),wdate,0)<0) { | |
1013 | printf("SARG: Maybe you have a broken record or garbage in a date.\n"); | |
1014 | exit(1); | |
1015 | } | |
25697a35 GS |
1016 | |
1017 | sprintf(dfrom,"%s%s%s",anof,mesf,diaf); | |
1018 | sprintf(duntil,"%s%s%s",anou,mesu,diau); | |
1019 | return; | |
1020 | } | |
1021 | ||
1022 | ||
1023 | char *strlow(char *string) | |
1024 | { | |
32e71fa4 | 1025 | char *s; |
25697a35 | 1026 | |
32e71fa4 FM |
1027 | if (string) |
1028 | { | |
1029 | for (s = string; *s; ++s) | |
1030 | *s = tolower(*s); | |
1031 | } | |
25697a35 | 1032 | |
32e71fa4 | 1033 | return string; |
25697a35 GS |
1034 | } |
1035 | ||
1036 | ||
1037 | ||
1038 | ||
1039 | char *strup(char *string) | |
1040 | { | |
32e71fa4 | 1041 | char *s; |
25697a35 | 1042 | |
32e71fa4 FM |
1043 | if (string) |
1044 | { | |
1045 | for (s = string; *s; ++s) | |
1046 | *s = toupper(*s); | |
1047 | } | |
25697a35 | 1048 | |
32e71fa4 | 1049 | return string; |
25697a35 GS |
1050 | } |
1051 | ||
1052 | ||
48864d28 | 1053 | void subs(char *str, int size, char *from, char *to) |
25697a35 GS |
1054 | { |
1055 | char *tmp; | |
48864d28 FM |
1056 | int i; |
1057 | int ss, st, sf; | |
1058 | int len; | |
1059 | int shift; | |
d6e703cc | 1060 | |
48864d28 FM |
1061 | tmp = strstr(str, from); |
1062 | if(tmp == NULL) | |
1063 | return; | |
d6e703cc | 1064 | |
48864d28 FM |
1065 | ss = strlen(str); |
1066 | sf = strlen(from); | |
1067 | st = strlen(to); | |
1068 | shift=st-sf; | |
25697a35 | 1069 | |
48864d28 FM |
1070 | if (shift>0) { |
1071 | if (ss+shift>=size){ | |
1072 | fprintf(stderr,"SARG: Cannot replace %s by %s in %s and fit within %d bytes\n",from,to,str,size); | |
1073 | exit(1); | |
1074 | } | |
1075 | for (i=strlen(tmp) ; i>=sf ; i--) | |
1076 | tmp[i+shift]=tmp[i]; | |
1077 | } else if (shift<0) { | |
1078 | len=strlen(tmp); | |
1079 | for (i=sf ; i<=len ; i++) | |
1080 | tmp[i+shift]=tmp[i]; | |
1081 | } | |
1082 | memcpy(tmp, to, st); | |
1083 | return; | |
25697a35 GS |
1084 | } |
1085 | ||
1086 | ||
32e71fa4 | 1087 | void removetmp(const char *outdir) |
25697a35 GS |
1088 | { |
1089 | ||
1090 | FILE *fp_in; | |
1091 | char warea[256]; | |
1092 | ||
32e71fa4 | 1093 | if(strcmp(RemoveTempFiles,"yes") != 0) |
25697a35 | 1094 | return; |
48864d28 | 1095 | |
32e71fa4 | 1096 | if(debug) { |
d2fe0c32 | 1097 | debuga("%s: sarg-general, sarg-period",text[82]); |
32e71fa4 | 1098 | } |
48864d28 FM |
1099 | if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) { |
1100 | fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir); | |
1101 | exit(1); | |
1102 | } | |
32e71fa4 FM |
1103 | if((fp_in=fopen(warea,"r"))==NULL){ |
1104 | fprintf(stderr, "===SARG: (removetmp) %s: %s\n",text[45],warea); | |
1105 | exit(1); | |
1106 | } | |
1107 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
1108 | if(strncmp(buf,"TOTAL",5) == 0) | |
1109 | break; | |
1110 | } | |
1111 | fclose(fp_in); | |
1112 | if((fp_in=fopen(warea,"w"))==NULL){ | |
1113 | fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea); | |
1114 | exit(1); | |
25697a35 | 1115 | } |
32e71fa4 FM |
1116 | fputs(buf,fp_in); |
1117 | fclose(fp_in); | |
1118 | if (snprintf(warea,sizeof(warea),"%s/sarg-period",outdir)>=sizeof(warea)) { | |
1119 | fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir); | |
1120 | exit(1); | |
1121 | } | |
1122 | unlink(warea); | |
48864d28 | 1123 | |
25697a35 GS |
1124 | return; |
1125 | } | |
1126 | ||
48864d28 | 1127 | void load_excludecodes(const char *ExcludeCodes) |
25697a35 GS |
1128 | { |
1129 | ||
1130 | FILE *fp_in; | |
1131 | char data[80]; | |
48864d28 FM |
1132 | int i; |
1133 | ||
1134 | if(ExcludeCodes[0] == '\0') | |
1135 | return; | |
1136 | ||
1137 | if((excludecode=(char *) malloc(1024))==NULL) { | |
1138 | fprintf(stderr, "SARG: %s (1024):\n",text[59]); | |
1139 | exit(1); | |
1140 | } | |
1141 | bzero(excludecode,1024); | |
25697a35 GS |
1142 | |
1143 | if((fp_in=fopen(ExcludeCodes,"r"))==NULL) { | |
1144 | fprintf(stderr, "SARG: (util) Cannot open file: %s (exclude_codes)\n",ExcludeCodes); | |
1145 | exit(1); | |
1146 | } | |
1147 | ||
32e71fa4 | 1148 | while(fgets(data,sizeof(data),fp_in)!=NULL) { |
48864d28 FM |
1149 | for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]=0; |
1150 | if (i<0) continue; | |
25697a35 GS |
1151 | strcat(excludecode,data); |
1152 | strcat(excludecode,";"); | |
25697a35 GS |
1153 | } |
1154 | ||
1155 | fclose(fp_in); | |
1156 | return; | |
1157 | ||
1158 | } | |
1159 | ||
48864d28 FM |
1160 | void free_excludecodes(void) |
1161 | { | |
1162 | if (excludecode) { | |
1163 | free(excludecode); | |
1164 | excludecode=NULL; | |
1165 | } | |
1166 | } | |
1167 | ||
32e71fa4 | 1168 | int vercode(const char *code) |
25697a35 | 1169 | { |
48864d28 FM |
1170 | char *cod; |
1171 | int clen; | |
1172 | ||
1173 | if (excludecode && excludecode[0]!=0) { | |
1174 | clen=strlen(code); | |
1175 | for (cod=excludecode ; cod ; cod=strchr(cod+1,';')) { | |
1176 | if (strncmp(code,cod,clen)==0 && cod[clen]==';') | |
1177 | return 1; | |
4bcb77cf | 1178 | } |
25697a35 GS |
1179 | } |
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | void fixnone(char *str) | |
1184 | { | |
48864d28 | 1185 | int i; |
32e71fa4 | 1186 | |
48864d28 FM |
1187 | for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--); |
1188 | if(i==3 && strncmp(str,"none",4) == 0) | |
25697a35 GS |
1189 | str[0]='\0'; |
1190 | ||
1191 | return; | |
1192 | } | |
1193 | ||
1194 | #ifdef LEGACY_TESTVALIDUSERCHAR | |
32e71fa4 | 1195 | int testvaliduserchar(const char *user) |
25697a35 GS |
1196 | { |
1197 | ||
1198 | int x=0; | |
1199 | int y=0; | |
1200 | ||
1201 | for (y=0; y<strlen(UserInvalidChar); y++) { | |
1202 | for (x=0; x<strlen(user); x++) { | |
1203 | if(user[x] == UserInvalidChar[y]) | |
1204 | return 1; | |
1205 | } | |
1206 | } | |
1207 | return 0; | |
1208 | } | |
1209 | #else | |
32e71fa4 | 1210 | int testvaliduserchar(const char *user) |
25697a35 GS |
1211 | { |
1212 | ||
1213 | char * p_UserInvalidChar = UserInvalidChar ; | |
32e71fa4 | 1214 | const char * p_user ; |
25697a35 GS |
1215 | |
1216 | while( *p_UserInvalidChar ) { | |
1217 | p_user = user ; | |
1218 | while ( *p_user ) { | |
1219 | if( *p_UserInvalidChar == *p_user ) | |
1220 | return 1; | |
1221 | p_user++ ; | |
1222 | } | |
1223 | p_UserInvalidChar++ ; | |
1224 | } | |
1225 | return 0; | |
1226 | } | |
1227 | #endif | |
1228 | ||
1229 | int compar( const void *a, const void *b ) | |
1230 | { if( *(int *)a > *(int *)b ) return 1; | |
1231 | if( *(int *)a < *(int *)b ) return -1; | |
1232 | return 0; | |
1233 | } | |
1234 | ||
1235 | int getnumlist( char *buf, numlist *list, const int len, const int maxvalue ) | |
48864d28 FM |
1236 | { |
1237 | int i, j, d, flag, r1, r2; | |
1238 | char *pbuf, **bp, *strbufs[ 24 ]; | |
1239 | ||
1240 | bp = strbufs; | |
1241 | strtok( buf, " \t" ); | |
1242 | for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) { | |
1243 | if( ++bp >= &strbufs[ 24 ] ) | |
1244 | break; | |
1245 | list->len++; | |
25697a35 | 1246 | } |
48864d28 FM |
1247 | if( ! list->len ) |
1248 | return -1; | |
1249 | d = 0; | |
1250 | for( i = 0; i < list->len; i++ ) { | |
1251 | if( strchr( strbufs[ i ], '-' ) != 0 ) { | |
1252 | pbuf = strbufs[ i ]; | |
1253 | strtok( pbuf, "-" ); | |
1254 | pbuf = strtok( NULL, "\0" ); | |
1255 | r1 = atoi( strbufs[ i ] ); | |
1256 | if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 ) | |
1257 | return -1; | |
1258 | if( i + d + ( r2 - r1 ) + 1 <= len ) { | |
1259 | for( j = r1; j <= r2; j++ ) | |
1260 | list->list[ i + d++ ] = j; | |
1261 | d--; | |
25697a35 GS |
1262 | } |
1263 | } | |
48864d28 FM |
1264 | else |
1265 | if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue ) | |
1266 | return 1; | |
25697a35 | 1267 | } |
48864d28 FM |
1268 | list->len += d; |
1269 | qsort( list->list, list->len, sizeof( int ), compar ); | |
1270 | do { | |
1271 | flag = 0; | |
1272 | for( i = 0; i < list->len - 1; i++ ) | |
1273 | if( list->list[ i ] == list->list[ i + 1 ] ) { | |
1274 | for( j = i + 1; j < list->len; j++ ) | |
1275 | list->list[ j - 1 ] = list->list[ j ]; | |
1276 | list->len--; | |
1277 | flag = 1; | |
1278 | break; | |
1279 | } | |
25697a35 | 1280 | } while( flag ); |
48864d28 | 1281 | return 0; |
25697a35 GS |
1282 | } |
1283 | ||
1284 | ||
1285 | void show_info(FILE *fp_ou) | |
1286 | { | |
48864d28 FM |
1287 | char ftime[127]; |
1288 | ||
1289 | if(strcmp(ShowSargInfo,"yes") != 0) return; | |
1290 | zdate(ftime, DateFormat); | |
1291 | fprintf(fp_ou,"<center><table><tr><td><br><br></td><td class=\"info\">%s <a href='%s'><font class=\"info\">%s-%s</font></a> %s %s</td></tr></table></center>\n",text[108],URL,PGM,VERSION,text[109],ftime); | |
25697a35 GS |
1292 | } |
1293 | ||
32e71fa4 | 1294 | void show_sarg(FILE *fp_ou, const char *ind) |
25697a35 | 1295 | { |
d6e703cc | 1296 | if(strcmp(ShowSargLogo,"yes") == 0) fprintf(fp_ou,"<center><table cellpadding=0 cellspacing=0>\n<tr><th class=\"logo\"><a href=\"http://sarg.sourceforge.net\"><img src=\"%s/images/sarg.png\" border=\"0\" align=\"absmiddle\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\"></a> <font class=\"logo\">Squid Analysis Report Generator</font></th></tr>\n<tr><th class=\"title\"> </th></tr>\n<table>\n",ind); |
25697a35 GS |
1297 | } |
1298 | ||
32e71fa4 | 1299 | char *get_size(const char *path, const char *file) |
491b862f GS |
1300 | { |
1301 | FILE *fp; | |
1302 | char response[255]; | |
32e71fa4 | 1303 | char cmd[255]; |
491b862f | 1304 | |
32e71fa4 FM |
1305 | if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) { |
1306 | printf("SARG: Cannot get disk space because the path %s%s is too long.\n",path,file); | |
1307 | exit(1); | |
1308 | } | |
491b862f | 1309 | fp = popen(cmd, "r"); |
32e71fa4 | 1310 | fgets(response, sizeof(response), fp); |
4bcb77cf | 1311 | if (getword_multisep(val5,sizeof(val5),response,'\t')<0) { |
32e71fa4 | 1312 | printf("SARG: Maybe the command %s failed.\n",cmd); |
4bcb77cf FM |
1313 | exit(1); |
1314 | } | |
491b862f GS |
1315 | pclose(fp); |
1316 | ||
1317 | return (val5); | |
1318 | } | |
1319 | ||
1320 | ||
32e71fa4 | 1321 | void write_html_header(FILE *fp_ou, const char * ind) |
491b862f | 1322 | { |
d6e703cc | 1323 | fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n</head>\n",CharSet); |
491b862f | 1324 | css(fp_ou); |
d6e703cc FM |
1325 | fprintf(fp_ou,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); |
1326 | if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText); | |
491b862f | 1327 | show_sarg(fp_ou, ind); |
d6e703cc | 1328 | fprintf(fp_ou,"<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title\">%s</th></tr>\n</table></center>\n<center><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",Title); |
491b862f GS |
1329 | } |
1330 | ||
32e71fa4 | 1331 | void baddata(void) |
491b862f | 1332 | { |
d6e703cc FM |
1333 | printf("SARG: ------------------------------------------------------------------------------\n"); |
1334 | printf("SARG: MALICIUS CODE DETECTED.\n"); | |
1335 | printf("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n"); | |
1336 | printf("SARG: please review your access.log and/or your useragent.log file.\n"); | |
1337 | printf("SARG: process stoped. No actions taken.\n"); | |
1338 | printf("SARG: ------------------------------------------------------------------------------\n"); | |
1339 | ||
1340 | system("rm -rf /tmp/sarg"); | |
1341 | sprintf(tmp4,"rm -rf %s",dirname); | |
1342 | system(tmp4); | |
1343 | system("rm -rf /tmp/sarg"); | |
1344 | ||
1345 | exit(1); | |
491b862f GS |
1346 | } |
1347 | ||
1348 | ||
48864d28 | 1349 | void url_module(const char *url, char *w2) |
25697a35 GS |
1350 | { |
1351 | int x, y; | |
1352 | char w[255]; | |
1353 | ||
25697a35 GS |
1354 | y=0; |
1355 | for(x=strlen(url)-1; x>=0; x--) { | |
48864d28 FM |
1356 | if(url[x] == '/' || y>=sizeof(w)-1) break; |
1357 | w[y++]=url[x]; | |
25697a35 GS |
1358 | } |
1359 | ||
48864d28 FM |
1360 | x=0; |
1361 | for(y=y-1; y>=0; y--) { | |
1362 | w2[x++]=w[y]; | |
25697a35 | 1363 | } |
48864d28 | 1364 | w2[x]=0; |
25697a35 GS |
1365 | } |
1366 | ||
d6e703cc FM |
1367 | |
1368 | void write_html_trailer(FILE *fp_ou) | |
1369 | { | |
1370 | fputs("</table></center>\n",fp_ou); | |
1371 | zdate(ftime, DateFormat); | |
1372 | show_info(fp_ou); | |
1373 | fputs("</body>\n</html>\n",fp_ou); | |
1374 | } | |
1375 | ||
32e71fa4 | 1376 | void version(void) |
25697a35 GS |
1377 | { |
1378 | printf("SARG Version: %s\n",VERSION); | |
1379 | exit(0); | |
1380 | } | |
5f3cfd1d FM |
1381 | |
1382 | char *get_param_value(const char *param,char *line) | |
1383 | { | |
1384 | int plen; | |
1385 | ||
1386 | while (*line==' ' || *line=='\t') line++; | |
1387 | plen=strlen(param); | |
1388 | if (strncasecmp(line,param,plen)) return(NULL); | |
1389 | if (line[plen]!=' ' && line[plen]!='\t') return(NULL); | |
1390 | line+=plen; | |
1391 | while (*line==' ' || *line=='\t') line++; | |
1392 | return(line); | |
1393 | } | |
936c9905 FM |
1394 | |
1395 | void read_usertab(const char *UserTabFile) | |
1396 | { | |
1397 | FILE *fp_usr; | |
1398 | unsigned int nreg; | |
1399 | char buf[MAXLEN]; | |
1400 | char bufy[MAXLEN]; | |
1401 | int z2; | |
1402 | int z1; | |
a4aabea6 | 1403 | int i; |
936c9905 FM |
1404 | |
1405 | if (UserTabFile[0] != '\0') { | |
1406 | if(debug) { | |
d2fe0c32 | 1407 | debuga("%s: %s",text[86],UserTabFile); |
936c9905 FM |
1408 | } |
1409 | if((fp_usr=fopen(UserTabFile,"r"))==NULL) { | |
1410 | fprintf(stderr, "SARG: (log) %s: %s - %s\n",text[45],UserTabFile,strerror(errno)); | |
1411 | exit(1); | |
1412 | } | |
1413 | fseek(fp_usr, 0, SEEK_END); | |
1414 | nreg = ftell(fp_usr)+100; | |
1415 | fseek(fp_usr, 0, SEEK_SET); | |
1416 | if((userfile=(char *) malloc(nreg))==NULL){ | |
1417 | fprintf(stderr, "SARG ERROR: %s",text[87]); | |
1418 | exit(1); | |
1419 | } | |
a4aabea6 | 1420 | strcpy(userfile,"\t"); |
936c9905 FM |
1421 | z2=1; |
1422 | while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { | |
1423 | if (buf[0]=='#') continue; | |
a4aabea6 | 1424 | for (i=strlen(buf)-1 ; i>=0 && (unsigned char)buf[i]<=' ' ; i--) buf[i]=0; |
936c9905 FM |
1425 | if (getword_multisep(bufy,sizeof(bufy),buf,' ')<0) { |
1426 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile); | |
1427 | exit(1); | |
1428 | } | |
1429 | if (z2+strlen(bufy)+strlen(buf)+3>=nreg) { | |
1430 | printf("SARG: The list of the users is too long in your %s file.\n",UserTabFile); | |
1431 | exit(1); | |
1432 | } | |
1433 | for(z1=0; bufy[z1]; z1++) | |
1434 | userfile[z2++]=bufy[z1]; | |
a4aabea6 | 1435 | userfile[z2++]='\n'; |
936c9905 FM |
1436 | for(z1=0; buf[z1]; z1++) |
1437 | userfile[z2++]=buf[z1]; | |
a4aabea6 | 1438 | userfile[z2++]='\t'; |
936c9905 FM |
1439 | } |
1440 | userfile[z2]=0; | |
1441 | fclose(fp_usr); | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | void get_usertab_name(const char *user,char *name,int namelen) | |
1446 | { | |
1447 | char warea[MAXLEN]; | |
1448 | char *str; | |
1449 | ||
1450 | namelen--; | |
1451 | if(UserTabFile[0] == '\0') { | |
1452 | strncpy(name,user,namelen); | |
1453 | name[namelen]=0; | |
1454 | } else { | |
a4aabea6 | 1455 | sprintf(warea,"\t%s\n",user); |
936c9905 FM |
1456 | if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) { |
1457 | strncpy(name,user,namelen); | |
1458 | name[namelen]=0; | |
1459 | } else { | |
a4aabea6 | 1460 | str=strchr(str+1,'\n'); |
936c9905 | 1461 | str++; |
a4aabea6 | 1462 | for(z1=0; *str != '\t' && z1<namelen ; z1++) { |
936c9905 FM |
1463 | name[z1]=*str++; |
1464 | } | |
1465 | name[z1]=0; | |
1466 | } | |
1467 | } | |
1468 | } |