]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
9 | * --------------------------------------------------------------------- |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | // #define LEGACY_MY_ATOLL | |
28 | // #define LEGACY_TESTVALIDUSERCHAR | |
29 | ||
30 | #include "include/conf.h" | |
5f3cfd1d | 31 | #include "include/defs.h" |
25697a35 | 32 | |
e6414a9d | 33 | #if defined(HAVE_BACKTRACE) |
ac422f9b | 34 | #define USE_GETWORD_BACKTRACE 1 |
e6414a9d FM |
35 | #else |
36 | #define USE_GETWORD_BACKTRACE 0 | |
37 | #endif | |
38 | ||
25697a35 | 39 | static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; |
48864d28 FM |
40 | |
41 | //! The list of the HTTP codes to exclude from the report. | |
42 | static char *excludecode=NULL; | |
25697a35 | 43 | |
d6e703cc FM |
44 | /*void fgetword(char *word, char *line, int stop) |
45 | { | |
46 | //VARIANT N1 | |
47 | int x; | |
2357ef77 | 48 | |
d6e703cc FM |
49 | for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x]; |
50 | word[x] = '\0'; | |
51 | ||
52 | //VARIANT N2 | |
53 | char *tchar; | |
54 | int difflen; | |
2357ef77 | 55 | |
d6e703cc FM |
56 | tchar = strchr(line, stop); |
57 | if (tchar == NULL) strcpy(word, line); | |
58 | else | |
59 | { | |
60 | difflen = tchar - line; | |
61 | strncpy(word, line, difflen); | |
62 | word[difflen] = '\0'; | |
63 | } | |
64 | }*/ | |
65 | ||
e6414a9d FM |
66 | #if USE_GETWORD_BACKTRACE |
67 | static void getword_backtrace(void) | |
68 | { | |
69 | void *buffer[5]; | |
70 | int i, n; | |
71 | char **calls; | |
72 | ||
73 | n=backtrace(buffer,sizeof(buffer)/sizeof(buffer[0])); | |
74 | if (n<=0) return; | |
75 | calls=backtrace_symbols(buffer,n); | |
76 | if (calls) { | |
10210234 | 77 | debuga(_("getword backtrace:\n")); |
e6414a9d FM |
78 | for (i=0 ; i<n ; i++) { |
79 | fprintf(stderr,"SARG: %d:%s\n",i+1,calls[i]); | |
80 | } | |
81 | free(calls); | |
82 | } | |
83 | } | |
84 | #endif //USE_GETWORD_BACKTRACE | |
85 | ||
9c7c6346 | 86 | void getword_start(struct getwordstruct *gwarea, const char *line) |
25697a35 | 87 | { |
9c7c6346 FM |
88 | gwarea->beginning=line; |
89 | gwarea->current=line; | |
e5b2c6f0 | 90 | gwarea->modified=0; |
9c7c6346 | 91 | } |
25697a35 | 92 | |
9c7c6346 | 93 | void getword_restart(struct getwordstruct *gwarea) |
25697a35 | 94 | { |
e5b2c6f0 | 95 | if (gwarea->modified) { |
10210234 | 96 | debuga(_("Cannot parse again the line as it was modified\n")); |
06b39c87 | 97 | exit(EXIT_FAILURE); |
e5b2c6f0 | 98 | } |
9c7c6346 FM |
99 | gwarea->current=gwarea->beginning; |
100 | } | |
25697a35 | 101 | |
06b39c87 | 102 | int getword(char *word, int limit, struct getwordstruct *gwarea, char stop) |
9c7c6346 FM |
103 | { |
104 | int x; | |
25697a35 | 105 | |
9c7c6346 | 106 | for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) { |
4bcb77cf FM |
107 | if(x>=limit) { |
108 | printf("SARG: getword loop detected after %d bytes.\n",x); | |
9c7c6346 FM |
109 | printf("SARG: Line=\"%s\"\n",gwarea->beginning); |
110 | printf("SARG: Record=\"%s\"\n",gwarea->current); | |
4bcb77cf FM |
111 | printf("SARG: searching for \'x%x\'\n",stop); |
112 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
120d768c | 113 | word[(limit>0) ? limit-1 : 0]='\0'; |
e6414a9d FM |
114 | #if USE_GETWORD_BACKTRACE |
115 | getword_backtrace(); | |
116 | #endif | |
4bcb77cf FM |
117 | return(-1); |
118 | } | |
9c7c6346 | 119 | word[x] = gwarea->current[x]; |
25697a35 GS |
120 | } |
121 | ||
122 | word[x] = '\0'; | |
9c7c6346 FM |
123 | if (gwarea->current[x]) ++x; |
124 | gwarea->current+=x; | |
4bcb77cf | 125 | return(0); |
25697a35 GS |
126 | } |
127 | ||
06b39c87 | 128 | int getword_limit(char *word, int limit, struct getwordstruct *gwarea, char stop) |
e5b2c6f0 FM |
129 | { |
130 | int x; | |
131 | ||
132 | limit--; | |
133 | for(x=0; x<limit && gwarea->current[x] && gwarea->current[x] != stop ;x++) { | |
134 | word[x] = gwarea->current[x]; | |
135 | } | |
136 | word[x] = '\0'; | |
137 | gwarea->current+=x; | |
138 | while (*gwarea->current && *gwarea->current != stop) gwarea->current++; | |
139 | if (*gwarea->current) ++gwarea->current; | |
140 | return(0); | |
141 | } | |
142 | ||
06b39c87 | 143 | int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, char stop) |
4bcb77cf | 144 | { |
9c7c6346 | 145 | int x; |
4bcb77cf | 146 | |
9c7c6346 | 147 | for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) { |
4bcb77cf FM |
148 | if(x>=limit) { |
149 | printf("SARG: getword_multisep loop detected.\n"); | |
9c7c6346 FM |
150 | printf("SARG: Line=\"%s\"\n",gwarea->beginning); |
151 | printf("SARG: Record=\"%s\"\n",gwarea->current); | |
4bcb77cf FM |
152 | printf("SARG: searching for \'x%x\'\n",stop); |
153 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
154 | if (limit>0) word[limit-1]='\0'; | |
e6414a9d FM |
155 | #if USE_GETWORD_BACKTRACE |
156 | getword_backtrace(); | |
157 | #endif | |
06b39c87 | 158 | //exit(EXIT_FAILURE); |
4bcb77cf FM |
159 | return(-1); |
160 | } | |
9c7c6346 | 161 | word[x] = gwarea->current[x]; |
4bcb77cf FM |
162 | } |
163 | ||
164 | word[x] = '\0'; | |
9c7c6346 FM |
165 | while (gwarea->current[x] && gwarea->current[x]==stop) ++x; |
166 | gwarea->current+=x; | |
4bcb77cf FM |
167 | return(0); |
168 | } | |
169 | ||
06b39c87 | 170 | int getword_skip(int limit, struct getwordstruct *gwarea, char stop) |
076cbab8 | 171 | { |
9c7c6346 | 172 | int x; |
076cbab8 | 173 | |
9c7c6346 | 174 | for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) { |
076cbab8 FM |
175 | if(x>=limit) { |
176 | printf("SARG: getword_skip loop detected after %d bytes.\n",x); | |
9c7c6346 FM |
177 | printf("SARG: Line=\"%s\"\n",gwarea->beginning); |
178 | printf("SARG: Record=\"%s\"\n",gwarea->current); | |
076cbab8 FM |
179 | printf("SARG: searching for \'x%x\'\n",stop); |
180 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
e6414a9d FM |
181 | #if USE_GETWORD_BACKTRACE |
182 | getword_backtrace(); | |
183 | #endif | |
076cbab8 FM |
184 | return(-1); |
185 | } | |
186 | } | |
187 | ||
9c7c6346 FM |
188 | if (gwarea->current[x]) ++x; |
189 | gwarea->current+=x; | |
076cbab8 FM |
190 | return(0); |
191 | } | |
192 | ||
06b39c87 | 193 | int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop) |
25697a35 | 194 | { |
0a4e18e1 | 195 | int x; |
e6414a9d | 196 | int sign=+1; |
25697a35 | 197 | |
e6414a9d FM |
198 | if (gwarea->current[0] == '-') { |
199 | gwarea->current++; | |
200 | sign=-1; | |
201 | } else if (gwarea->current[0] == '+') { | |
202 | gwarea->current++; | |
203 | } | |
0a4e18e1 FM |
204 | *number=0LL; |
205 | for(x=0;isdigit(gwarea->current[x]);x++) { | |
206 | *number=(*number * 10) + gwarea->current[x]-'0'; | |
207 | } | |
208 | if(gwarea->current[x] && gwarea->current[x]!=stop) { | |
209 | printf("SARG: getword_atoll loop detected after %d bytes.\n",x); | |
210 | printf("SARG: Line=\"%s\"\n",gwarea->beginning); | |
211 | printf("SARG: Record=\"%s\"\n",gwarea->current); | |
212 | printf("SARG: searching for \'x%x\'\n",stop); | |
213 | //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); | |
e6414a9d FM |
214 | #if USE_GETWORD_BACKTRACE |
215 | getword_backtrace(); | |
216 | #endif | |
0a4e18e1 FM |
217 | return(-1); |
218 | } | |
e6414a9d | 219 | *number*=sign; |
25697a35 | 220 | |
0a4e18e1 FM |
221 | if (gwarea->current[x]) ++x; |
222 | gwarea->current+=x; | |
223 | return(0); | |
224 | } | |
25697a35 | 225 | |
25697a35 | 226 | |
06b39c87 | 227 | int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop) |
e5b2c6f0 FM |
228 | { |
229 | /*! | |
230 | \note Why pass the original buffer to the function ? Because we must modify it to | |
231 | insert the terminating ASCII zero for the word we return and that's not compatible | |
232 | with getword_restart(). Moreover, getword_start() sometime works on constant strings | |
233 | so this function require the original buffer to detect any missuse. | |
234 | */ | |
235 | int x; | |
236 | int sep; | |
237 | int start; | |
238 | ||
239 | if (orig_line && orig_line!=gwarea->beginning) { | |
10210234 | 240 | debuga(_("Invalid buffer passed to getword_ptr\n")); |
e5b2c6f0 FM |
241 | return(-1); |
242 | } | |
243 | ||
244 | start=(gwarea->current-gwarea->beginning); | |
245 | if (word && orig_line) *word=orig_line+start; | |
246 | for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++); | |
247 | sep=(gwarea->current[x]!='\0'); | |
248 | if (word && orig_line) orig_line[start+x] = '\0'; | |
249 | if (sep) ++x; | |
250 | gwarea->current+=x; | |
251 | gwarea->modified=1; | |
252 | return(0); | |
253 | } | |
254 | ||
48864d28 | 255 | #define MAXLLL 30 //!< Maximum number of digits in long long (a guess). |
25697a35 GS |
256 | long long int my_atoll (const char *nptr) |
257 | { | |
0a4e18e1 | 258 | long long int returnval=0LL; |
25697a35 GS |
259 | int max_digits = MAXLLL ; |
260 | ||
261 | // Soak up all the white space | |
0a4e18e1 FM |
262 | while (isspace( *nptr )) { |
263 | nptr++; | |
264 | } | |
25697a35 GS |
265 | |
266 | //For each character left to right | |
267 | //change the character to a single digit | |
268 | //multiply what we had before by 10 and add the new digit | |
269 | ||
0a4e18e1 | 270 | while (--max_digits && isdigit( *nptr )) |
25697a35 | 271 | { |
0a4e18e1 | 272 | returnval = ( returnval * 10 ) + ( *nptr++ - '0' ) ; |
25697a35 GS |
273 | } |
274 | ||
275 | return returnval; | |
0a4e18e1 | 276 | } |
25697a35 | 277 | |
e6414a9d | 278 | int is_absolute(const char *path) |
6798f0a7 FM |
279 | { |
280 | if (*path=='/') return(1); | |
281 | #ifdef WINDOWS | |
282 | if (isalpha(path[0]) && path[1]==':') return(1); | |
283 | #endif | |
284 | return(0); | |
285 | } | |
25697a35 | 286 | |
32e71fa4 | 287 | void my_mkdir(const char *name) |
25697a35 | 288 | { |
a1de61fe FM |
289 | char w0[MAXLEN]; |
290 | int i; | |
291 | int chars; | |
25697a35 | 292 | |
6798f0a7 | 293 | if(!is_absolute(name)) { |
fcdc0918 FM |
294 | debuga(_("Invalid path (%s). Please, use absolute paths only.\n"),name); |
295 | debuga(_("process aborted.\n")); | |
06b39c87 | 296 | exit(EXIT_FAILURE); |
25697a35 GS |
297 | } |
298 | ||
a1de61fe FM |
299 | chars=0; |
300 | for (i=0 ; name[i] ; i++) { | |
301 | if (i>=sizeof(w0)) { | |
fcdc0918 | 302 | debuga(_("directory name too long: %s\n"),name); |
06b39c87 | 303 | exit(EXIT_FAILURE); |
4bcb77cf | 304 | } |
a1de61fe FM |
305 | if (chars>0 && name[i] == '/') { |
306 | w0[i] = '\0'; | |
307 | if(access(w0, R_OK) != 0) { | |
308 | if(mkdir(w0,0755)) { | |
fcdc0918 FM |
309 | debuga(_("mkdir %s %s\n"),w0,strerror(errno)); |
310 | debuga(_("process aborted.\n")); | |
06b39c87 | 311 | exit(EXIT_FAILURE); |
a1de61fe | 312 | } |
25697a35 GS |
313 | } |
314 | } | |
a1de61fe FM |
315 | if (name[i] != '/') chars++; |
316 | w0[i] = name[i]; | |
25697a35 | 317 | } |
a1de61fe FM |
318 | |
319 | if(access(name, R_OK) != 0) { | |
320 | if(mkdir(name,0755)) { | |
fcdc0918 FM |
321 | debuga(_("mkdir %s %s\n"),name,strerror(errno)); |
322 | debuga(_("process aborted.\n")); | |
06b39c87 | 323 | exit(EXIT_FAILURE); |
25697a35 GS |
324 | } |
325 | } | |
326 | } | |
327 | ||
328 | ||
e5b2c6f0 | 329 | void my_lltoa(unsigned long long int n, char *s, int ssize, int len) |
25697a35 | 330 | { |
0a4e18e1 FM |
331 | int i; |
332 | int slen = 0; | |
333 | int j; | |
334 | char c; | |
2357ef77 | 335 | |
e5b2c6f0 FM |
336 | ssize--; |
337 | if (len>ssize) { | |
fcdc0918 | 338 | debuga(_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize); |
e5b2c6f0 FM |
339 | abort(); |
340 | } | |
341 | ||
0a4e18e1 FM |
342 | do { |
343 | s[slen++] = (n % 10) + '0'; | |
e5b2c6f0 | 344 | } while ((n /= 10) > 0 && slen<ssize); |
0a4e18e1 | 345 | s[slen] = '\0'; |
2357ef77 | 346 | |
0a4e18e1 FM |
347 | for (i = 0, j = slen-1; i<j; i++, j--) { |
348 | c = s[i]; | |
349 | s[i] = s[j]; | |
350 | s[j] = c; | |
351 | } | |
2357ef77 | 352 | |
0a4e18e1 FM |
353 | if(len>slen) { |
354 | i=len-slen; | |
355 | for(j=slen; j>=0; j--) | |
356 | s[j+i]=s[j]; | |
357 | for(j=0 ; j<i ; j++) | |
358 | s[j]='0'; | |
359 | } | |
25697a35 GS |
360 | } |
361 | ||
fa6552b0 | 362 | int month2num(const char *month) |
25697a35 | 363 | { |
fa6552b0 | 364 | int m; |
25697a35 | 365 | |
fa6552b0 FM |
366 | for(m=0 ; m<12 && strcmp(mtab1[m],month) != 0; m++); |
367 | return(m); | |
368 | } | |
25697a35 | 369 | |
fa6552b0 FM |
370 | int builddia(int day, int month, int year) |
371 | { | |
372 | return(year*10000+month*100+day); | |
25697a35 GS |
373 | } |
374 | ||
375 | ||
32e71fa4 | 376 | void buildymd(const char *dia, const char *mes, const char *ano, char *wdata) |
25697a35 | 377 | { |
48864d28 | 378 | int nmes; |
25697a35 | 379 | |
fa6552b0 | 380 | nmes=month2num(mes); |
b25b96fe | 381 | sprintf(wdata,"%04d%02d%02d",atoi(ano),nmes+1,atoi(dia)); |
25697a35 GS |
382 | } |
383 | ||
384 | ||
fa6552b0 | 385 | int conv_month(const char *month) |
25697a35 GS |
386 | { |
387 | int x; | |
388 | ||
fa6552b0 FM |
389 | for(x=0; x<12 && strncmp(mtab1[x],month,3)!=0; x++); |
390 | return(x+1); | |
25697a35 GS |
391 | } |
392 | ||
393 | ||
fa6552b0 | 394 | const char *conv_month_name(int month) |
25697a35 | 395 | { |
fa6552b0 | 396 | static char str[4]; |
25697a35 | 397 | |
fa6552b0 FM |
398 | if (month<1 || month>12) { |
399 | snprintf(str,sizeof(str),"%03d",month); | |
400 | return(str); | |
401 | } | |
402 | return(mtab1[month-1]); | |
25697a35 GS |
403 | } |
404 | ||
405 | ||
4bcb77cf | 406 | void name_month(char *month,int month_len) |
491b862f GS |
407 | { |
408 | int x, z=atoi(month)-1; | |
409 | char m[255]; | |
410 | char w[20]; | |
9c7c6346 | 411 | struct getwordstruct gwarea; |
491b862f | 412 | |
c36c7384 | 413 | strcpy(m,_("January,February,March,April,May,June,July,August,September,October,November,December")); |
9c7c6346 | 414 | getword_start(&gwarea,m); |
491b862f GS |
415 | |
416 | for(x=0; x<z; x++) | |
9c7c6346 | 417 | if (getword_multisep(w,sizeof(w),&gwarea,',')<0) { |
4bcb77cf | 418 | printf("SARG: Maybe you have a broken record or garbage in the names of the months.\n"); |
06b39c87 | 419 | exit(EXIT_FAILURE); |
4bcb77cf | 420 | } |
9c7c6346 | 421 | if (getword_multisep(month,month_len,&gwarea,',')<0) { |
4bcb77cf | 422 | printf("SARG: Maybe you have a broken record or garbage in the name of the months.\n"); |
06b39c87 | 423 | exit(EXIT_FAILURE); |
4bcb77cf | 424 | } |
491b862f GS |
425 | } |
426 | ||
427 | ||
d2fe0c32 | 428 | void debuga(const char *msg,...) |
25697a35 | 429 | { |
d2fe0c32 | 430 | va_list ap; |
25697a35 | 431 | |
f2ec8c75 | 432 | fputs(_("SARG: "),stderr); |
d2fe0c32 FM |
433 | va_start(ap,msg); |
434 | vfprintf(stderr,msg,ap); | |
435 | va_end(ap); | |
25697a35 GS |
436 | } |
437 | ||
438 | ||
32e71fa4 | 439 | void debugaz(const char *head, const char *msg) |
25697a35 GS |
440 | { |
441 | fprintf(stderr, "SARG: (util) %s=%s\n",head, msg); | |
442 | ||
443 | } | |
444 | ||
445 | ||
25697a35 | 446 | char *fixnum(long long int value, int n) |
25697a35 | 447 | { |
32e71fa4 | 448 | #define MAXLEN_FIXNUM 1024 |
fabbc7cc | 449 | char num[MAXLEN_FIXNUM]=""; |
32e71fa4 | 450 | char buf[MAXLEN_FIXNUM * 2]; |
25697a35 | 451 | char *pbuf; |
32e71fa4 | 452 | static char ret[MAXLEN_FIXNUM * 2]; |
25697a35 | 453 | char *pret; |
25697a35 | 454 | register int i, j, k; |
fabbc7cc | 455 | int numlen; |
25697a35 | 456 | static char abbrev[30]; |
2357ef77 | 457 | |
e5b2c6f0 | 458 | my_lltoa(value, num, sizeof(num), 0); |
25697a35 GS |
459 | |
460 | if(strcmp(DisplayedValues,"abbreviation") == 0) { | |
fabbc7cc FM |
461 | numlen = strlen(num); |
462 | if(numlen <= 3) | |
25697a35 | 463 | sprintf(abbrev,"%s",num); |
fabbc7cc | 464 | if(numlen == 4 || numlen == 7 || numlen == 10 || numlen == 13) { |
25697a35 GS |
465 | snprintf(abbrev,2,"%s",num); |
466 | strncat(abbrev,".",1); | |
467 | strncat(abbrev,num+1,2); | |
468 | if(!n) return(abbrev); | |
fabbc7cc | 469 | if(numlen == 4) |
25697a35 | 470 | strncat(abbrev,"K",1); |
fabbc7cc | 471 | else if(numlen == 7) |
25697a35 | 472 | strncat(abbrev,"M",1); |
fabbc7cc | 473 | else if(numlen == 10) |
25697a35 | 474 | strncat(abbrev,"G",1); |
fabbc7cc | 475 | else if(numlen == 13) |
25697a35 GS |
476 | strncat(abbrev,"T",1); |
477 | } | |
fabbc7cc | 478 | if(numlen == 5 || numlen == 8 || numlen == 11 || numlen == 14) { |
25697a35 GS |
479 | snprintf(abbrev,3,"%s",num); |
480 | strncat(abbrev,".",1); | |
481 | strncat(abbrev,num+2,2); | |
482 | if(!n) return(abbrev); | |
fabbc7cc | 483 | if(numlen == 5) |
25697a35 | 484 | strncat(abbrev,"K",1); |
fabbc7cc | 485 | else if(numlen == 8) |
25697a35 | 486 | strncat(abbrev,"M",1); |
fabbc7cc | 487 | else if(numlen == 11) |
25697a35 | 488 | strncat(abbrev,"G",1); |
fabbc7cc | 489 | else if(numlen == 14) |
25697a35 GS |
490 | strncat(abbrev,"T",1); |
491 | } | |
fabbc7cc | 492 | if(numlen == 6 || numlen == 9 || numlen == 12 || numlen == 15) { |
25697a35 GS |
493 | snprintf(abbrev,4,"%s",num); |
494 | strncat(abbrev,".",1); | |
495 | strncat(abbrev,num+3,2); | |
496 | if(!n) return(abbrev); | |
fabbc7cc | 497 | if(numlen == 6) |
25697a35 | 498 | strncat(abbrev,"K",1); |
fabbc7cc | 499 | else if(numlen == 9) |
25697a35 | 500 | strncat(abbrev,"M",1); |
fabbc7cc | 501 | else if(numlen == 12) |
25697a35 | 502 | strncat(abbrev,"G",1); |
fabbc7cc | 503 | else if(numlen == 15) |
25697a35 GS |
504 | strncat(abbrev,"T",1); |
505 | } | |
506 | ||
507 | return(abbrev); | |
508 | } | |
509 | ||
32e71fa4 | 510 | bzero(buf, MAXLEN_FIXNUM*2); |
25697a35 GS |
511 | |
512 | pbuf = buf; | |
513 | pret = ret; | |
514 | k = 0; | |
515 | ||
516 | for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { | |
517 | if ( k == 2 && i != 0 ) { | |
518 | k = 0; | |
519 | pbuf[j++] = num[i]; | |
e6414a9d | 520 | pbuf[j++] = (UseComma) ? ',' : '.'; |
25697a35 GS |
521 | continue; |
522 | } | |
523 | pbuf[j] = num[i]; | |
524 | j++; | |
525 | k++; | |
526 | } | |
527 | ||
528 | pret[0]='\0'; | |
529 | ||
530 | for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) | |
531 | pret[j] = pbuf[i]; | |
532 | ||
48864d28 | 533 | pret[j] = '\0'; |
25697a35 | 534 | |
48864d28 | 535 | return pret; |
25697a35 GS |
536 | } |
537 | ||
538 | ||
d6e703cc | 539 | char *fixnum2(long long int value, int n) |
d6e703cc | 540 | { |
32e71fa4 FM |
541 | #define MAXLEN_FIXNUM2 1024 |
542 | char num[MAXLEN_FIXNUM2]; | |
543 | char buf[MAXLEN_FIXNUM2 * 2]; | |
d6e703cc | 544 | char *pbuf; |
32e71fa4 | 545 | static char ret[MAXLEN_FIXNUM2 * 2]; |
d6e703cc FM |
546 | char *pret; |
547 | register int i, j, k; | |
2357ef77 | 548 | |
e5b2c6f0 | 549 | my_lltoa(value, num, sizeof(num), 0); |
32e71fa4 | 550 | bzero(buf, MAXLEN_FIXNUM2*2); |
d6e703cc FM |
551 | |
552 | pbuf = buf; | |
553 | pret = ret; | |
554 | k = 0; | |
555 | ||
556 | for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { | |
557 | if ( k == 2 && i != 0 ) { | |
558 | k = 0; | |
559 | pbuf[j++] = num[i]; | |
e6414a9d | 560 | pbuf[j++] = (UseComma) ? ',' : '.'; |
d6e703cc FM |
561 | continue; |
562 | } | |
563 | pbuf[j] = num[i]; | |
564 | j++; | |
565 | k++; | |
566 | } | |
567 | ||
568 | pret[0]='\0'; | |
569 | ||
570 | for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) | |
571 | pret[j] = pbuf[i]; | |
572 | ||
32e71fa4 | 573 | pret[j] = '\0'; |
d6e703cc | 574 | |
32e71fa4 | 575 | return pret; |
d6e703cc FM |
576 | } |
577 | ||
578 | ||
579 | ||
25697a35 GS |
580 | void buildhref(char * href) |
581 | { | |
582 | char whref[MAXLEN]; | |
583 | ||
584 | if(strcmp(href,"./") == 0){ | |
585 | href[0]='\0'; | |
586 | strcat(href,"<a href='"); | |
587 | return; | |
588 | } | |
589 | ||
590 | href[strlen(href)-1]='\0'; | |
591 | sprintf(whref,"%s",strrchr(href,'/')); | |
592 | ||
593 | strcpy(href,"<a href='"); | |
594 | strcat(href,whref); | |
595 | strcat(href,"/"); | |
596 | ||
597 | return; | |
598 | ||
599 | } | |
600 | ||
601 | ||
602 | char *buildtime(long long int elap) | |
603 | { | |
604 | ||
605 | int num = elap / 1000; | |
606 | int hor = 0; | |
607 | int min = 0; | |
608 | int sec = 0; | |
609 | static char buf[12]; | |
610 | ||
611 | buf[0]='\0'; | |
612 | ||
613 | hor=num / 3600; | |
614 | min=(num % 3600) / 60; | |
615 | sec=num % 60; | |
616 | sprintf(buf,"%02d:%02d:%02d",hor,min,sec); | |
617 | ||
618 | return(buf); | |
619 | ||
620 | } | |
621 | ||
622 | ||
32e71fa4 | 623 | void obtdate(const char *dirname, const char *name, char *data) |
25697a35 GS |
624 | { |
625 | ||
626 | FILE *fp_in; | |
627 | char wdir[MAXLEN]; | |
628 | ||
d6e703cc | 629 | sprintf(wdir,"%s%s/sarg-date",dirname,name); |
6798f0a7 | 630 | if ((fp_in = fopen(wdir, "rt")) == 0) { |
d6e703cc | 631 | sprintf(wdir,"%s%s/date",dirname,name); |
6798f0a7 | 632 | if ((fp_in = fopen(wdir, "rt")) == 0) { |
d6e703cc FM |
633 | data[0]='\0'; |
634 | return; | |
635 | } | |
25697a35 GS |
636 | } |
637 | ||
05b90947 | 638 | if (!fgets(data,80,fp_in)) { |
d5d021c5 | 639 | debuga(_("Failed to read the date in %s\n"),wdir); |
06b39c87 | 640 | exit(EXIT_FAILURE); |
05b90947 | 641 | } |
25697a35 | 642 | fclose(fp_in); |
05b90947 | 643 | fixendofline(data); |
25697a35 GS |
644 | |
645 | return; | |
646 | ||
647 | } | |
648 | ||
649 | ||
a1de61fe | 650 | void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst) |
9e41ca7e FM |
651 | { |
652 | struct tm ltm; | |
fabbc7cc FM |
653 | time_t unixtime; |
654 | struct tm *fulltm; | |
9e41ca7e FM |
655 | |
656 | memset(<m,0,sizeof(ltm)); | |
a1de61fe FM |
657 | if (year>=1900) ltm.tm_year=year-1900; |
658 | if (month>=1 && month<=12) ltm.tm_mon=month-1; | |
659 | if (day>=1 && day<=31) ltm.tm_mday=day; | |
660 | if (hour>=0 && hour<24) ltm.tm_hour=hour; | |
661 | if (minute>=0 && minute<60) ltm.tm_min=minute; | |
662 | if (second>=0 && second<60) ltm.tm_sec=second; | |
663 | ltm.tm_isdst=dst; | |
fabbc7cc FM |
664 | unixtime=mktime(<m); //fill the missing entries |
665 | fulltm=localtime(&unixtime); | |
666 | //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm); | |
667 | strftime(date,date_size,"%c",fulltm); | |
9e41ca7e FM |
668 | } |
669 | ||
670 | ||
fa6552b0 | 671 | void computedate(int year,int month,int day,struct tm *t) |
9426efec | 672 | { |
fa6552b0 FM |
673 | memset(t,0,sizeof(*t)); |
674 | t->tm_year=year-1900; | |
675 | t->tm_mon=month-1; | |
676 | t->tm_mday=day; | |
9426efec FM |
677 | } |
678 | ||
679 | ||
d25d4e6a | 680 | int obtuser(const char *dirname, const char *name) |
25697a35 GS |
681 | { |
682 | ||
683 | FILE *fp_in; | |
684 | char wdir[MAXLEN]; | |
d25d4e6a FM |
685 | char tuser[20]; |
686 | int nuser; | |
25697a35 | 687 | |
d6e703cc | 688 | sprintf(wdir,"%s%s/sarg-users",dirname,name); |
2357ef77 | 689 | if((fp_in=fopen(wdir,"r"))==NULL) { |
d6e703cc | 690 | sprintf(wdir,"%s%s/users",dirname,name); |
2357ef77 | 691 | if((fp_in=fopen(wdir,"r"))==NULL) { |
d25d4e6a | 692 | return(0); |
d6e703cc | 693 | } |
25697a35 GS |
694 | } |
695 | ||
d25d4e6a | 696 | if (!fgets(tuser,sizeof(tuser),fp_in)) { |
10210234 | 697 | debuga(_("Failed to read the number of users in %s\n"),wdir); |
06b39c87 | 698 | exit(EXIT_FAILURE); |
05b90947 | 699 | } |
25697a35 | 700 | fclose(fp_in); |
d25d4e6a | 701 | nuser=atoi(tuser); |
25697a35 | 702 | |
d25d4e6a | 703 | return(nuser); |
25697a35 GS |
704 | } |
705 | ||
706 | ||
d25d4e6a | 707 | void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, char *media) |
25697a35 | 708 | { |
25697a35 | 709 | FILE *fp_in; |
2240dcea | 710 | char *buf; |
25697a35 | 711 | char wdir[MAXLEN]; |
2240dcea | 712 | char user[MAX_USER_LEN]; |
0511cf2d | 713 | char sep; |
25697a35 | 714 | long long int med=0; |
fabbc7cc | 715 | long long int twork=0; |
9c7c6346 | 716 | struct getwordstruct gwarea; |
afaa3b67 | 717 | longline line; |
25697a35 | 718 | |
0511cf2d FM |
719 | twork=0; |
720 | tbytes[0]='\0'; | |
721 | media[0]='\0'; | |
25697a35 | 722 | |
d6e703cc | 723 | sprintf(wdir,"%s%s/sarg-general",dirname,name); |
25697a35 | 724 | if ((fp_in = fopen(wdir, "r")) == 0) { |
d6e703cc FM |
725 | sprintf(wdir,"%s%s/general",dirname,name); |
726 | if ((fp_in = fopen(wdir, "r")) == 0) { | |
d6e703cc FM |
727 | return; |
728 | } | |
25697a35 GS |
729 | } |
730 | ||
afaa3b67 | 731 | if ((line=longline_create())==NULL) { |
10210234 | 732 | debuga(_("Not enough memory to read the file %s\n"),wdir); |
06b39c87 | 733 | exit(EXIT_FAILURE); |
2240dcea FM |
734 | } |
735 | ||
afaa3b67 | 736 | while((buf=longline_read(fp_in,line))!=NULL) { |
0511cf2d FM |
737 | if (strncmp(buf,"TOTAL\t",6) == 0) |
738 | sep='\t'; //new file | |
739 | else if (strncmp(buf,"TOTAL ",6) == 0) | |
740 | sep=' '; //old file | |
741 | else | |
742 | continue; | |
9c7c6346 | 743 | getword_start(&gwarea,buf); |
2240dcea | 744 | if (getword(user,sizeof(user),&gwarea,sep)<0) { |
10210234 | 745 | debuga(_("There is a invalid user in file %s\n"),wdir); |
06b39c87 | 746 | exit(EXIT_FAILURE); |
4bcb77cf | 747 | } |
ab6fadd0 | 748 | if(strcmp(user,"TOTAL") != 0) |
25697a35 | 749 | continue; |
0511cf2d | 750 | if (getword_skip(MAXLEN,&gwarea,sep)<0) { |
10210234 | 751 | debuga(_("There a broken total number of access in file %s\n"),wdir); |
06b39c87 | 752 | exit(EXIT_FAILURE); |
4bcb77cf | 753 | } |
0511cf2d | 754 | if (getword_atoll(&twork,&gwarea,sep)<0) { |
10210234 | 755 | debuga(_("There is a broken number of bytes in file %s\n"),wdir); |
06b39c87 | 756 | exit(EXIT_FAILURE); |
4bcb77cf | 757 | } |
48864d28 | 758 | strcpy(tbytes,fixnum(twork,1)); |
6e792ade | 759 | break; |
25697a35 GS |
760 | } |
761 | fclose(fp_in); | |
afaa3b67 | 762 | longline_destroy(&line); |
25697a35 | 763 | |
d25d4e6a | 764 | if(nuser <= 0) { |
48864d28 | 765 | strcpy(media,"0"); |
25697a35 GS |
766 | return; |
767 | } | |
48864d28 | 768 | |
d25d4e6a | 769 | med=twork / nuser; |
48864d28 | 770 | strcpy(media,fixnum(med,1)); |
25697a35 GS |
771 | |
772 | return; | |
25697a35 GS |
773 | } |
774 | ||
fa6552b0 | 775 | int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period) |
25697a35 | 776 | { |
fa6552b0 FM |
777 | const char *str; |
778 | int day0, month0, year0, hour0, minute0; | |
779 | int day1, month1, year1, hour1, minute1; | |
780 | char month[4]; | |
781 | int i; | |
25697a35 | 782 | |
fa6552b0 FM |
783 | memset(period,0,sizeof(*period)); |
784 | ||
785 | str=arqtt; | |
786 | while((str=strstr(str,"sarg-"))!=NULL) { | |
787 | str+=5; | |
788 | if (!isdigit(str[0]) || !isdigit(str[1])) continue; | |
789 | day0=(str[0]-'0')*10+(str[1]-'0'); | |
790 | str+=2; | |
791 | strncpy(month,str,3); | |
792 | month[3]=0; | |
793 | month0=month2num(month); | |
794 | if (month0>=12) continue; | |
795 | str+=3; | |
796 | year0=0; | |
797 | for (i=0 ; isdigit(str[i]) && i<4 ; i++) year0=year0*10+(str[i]-'0'); | |
798 | if (i!=4) continue; | |
799 | str+=4; | |
800 | ||
801 | if (!isdigit(str[0]) || !isdigit(str[1])) continue; | |
802 | hour0=(str[0]-'0')*10+(str[1]-'0'); | |
803 | str+=2; | |
804 | if (!isdigit(str[0]) || !isdigit(str[1])) continue; | |
805 | minute0=(str[0]-'0')*10+(str[1]-'0'); | |
806 | str+=2; | |
807 | ||
808 | if (*str != '_') continue; | |
809 | ||
810 | if (!isdigit(str[0]) || !isdigit(str[1])) continue; | |
811 | day1=(str[0]-'0')*10+(str[1]-'0'); | |
812 | str+=2; | |
813 | strncpy(month,str,3); | |
814 | month[3]=0; | |
815 | month1=month2num(month); | |
816 | if (month1>=12) continue; | |
817 | str+=3; | |
818 | year1=0; | |
819 | for (i=0 ; isdigit(str[i]) && i<4 ; i++) year1=year1*10+(str[i]-'0'); | |
820 | if (i!=4) continue; | |
821 | ||
822 | if (!isdigit(str[0]) || !isdigit(str[1])) continue; | |
823 | hour1=(str[0]-'0')*10+(str[1]-'0'); | |
824 | str+=2; | |
825 | if (!isdigit(str[0]) || !isdigit(str[1])) continue; | |
826 | minute1=(str[0]-'0')*10+(str[1]-'0'); | |
827 | str+=2; | |
828 | ||
829 | period->start.tm_mday=day0; | |
830 | period->start.tm_mon=month0; | |
831 | period->start.tm_year=year0-1900; | |
832 | period->start.tm_hour=hour0; | |
833 | period->start.tm_min=minute0; | |
834 | period->end.tm_mday=day1; | |
835 | period->end.tm_mon=month1; | |
836 | period->end.tm_year=year1-1900; | |
837 | period->end.tm_hour=hour1; | |
838 | period->end.tm_min=minute1; | |
839 | return(0); | |
840 | } | |
841 | return(-1); | |
842 | } | |
48864d28 | 843 | |
42b117e3 FM |
844 | void getperiod_fromrange(struct periodstruct *period,int dfrom,int duntil) |
845 | { | |
846 | memset(&period->start,0,sizeof(period->start)); | |
847 | period->start.tm_mday=dfrom%100; | |
848 | period->start.tm_mon=(dfrom/100)%100-1; | |
849 | period->start.tm_year=(dfrom/10000)-1900; | |
850 | ||
851 | memset(&period->end,0,sizeof(period->end)); | |
852 | period->end.tm_mday=duntil%100; | |
853 | period->end.tm_mon=(duntil/100)%100-1; | |
854 | period->end.tm_year=(duntil/10000)-1900; | |
855 | } | |
856 | ||
fa6552b0 FM |
857 | int getperiod_buildtext(struct periodstruct *period) |
858 | { | |
859 | int i; | |
fd46f082 FM |
860 | int range; |
861 | char text1[40], text2[40]; | |
862 | ||
863 | if(df[0]=='u') { | |
864 | i=strftime(text1, sizeof(text1), "%Y %b %d", &period->start); | |
865 | }else if(df[0]=='e') { | |
866 | i=strftime(text1, sizeof(text1), "%d %b %Y", &period->start); | |
867 | } else /*if(df[0]=='w')*/ { | |
fa6552b0 | 868 | IndexTree=INDEX_TREE_FILE; |
fd46f082 | 869 | i=strftime(text1, sizeof(text1), "%Y.%U", &period->start); |
fa6552b0 | 870 | } |
fd46f082 | 871 | if (i == 0) return(-1); |
fa6552b0 | 872 | |
fd46f082 | 873 | range=(period->start.tm_year!=period->end.tm_year || |
fa6552b0 | 874 | period->start.tm_mon!=period->end.tm_mon || |
fd46f082 FM |
875 | period->start.tm_mday!=period->end.tm_mday); |
876 | if (range) { | |
877 | if(df[0]=='u') { | |
878 | i=strftime(text2, sizeof(text2)-i, "%Y %b %d", &period->end); | |
879 | } else if(df[0]=='e') { | |
880 | i=strftime(text2, sizeof(text2)-i, "%d %b %Y", &period->end); | |
881 | } else { | |
882 | i=strftime(text2, sizeof(text2)-i, "%Y.%U", &period->end); | |
883 | } | |
fa6552b0 | 884 | if (i == 0) return(-1); |
e21b6c02 | 885 | } |
fd46f082 FM |
886 | |
887 | if (range) { | |
888 | snprintf(period->text,sizeof(period->text),"%s-%s",text1,text2); | |
889 | snprintf(period->html,sizeof(period->html),"%s—%s",text1,text2); | |
890 | } else { | |
891 | strncpy(period->text,text1,sizeof(period->text)-1); | |
892 | period->text[sizeof(period->text)-1]='\0'; | |
893 | strncpy(period->html,text1,sizeof(period->html)-1); | |
894 | period->html[sizeof(period->html)-1]='\0'; | |
895 | } | |
fa6552b0 | 896 | return(0); |
25697a35 GS |
897 | } |
898 | ||
06ced858 | 899 | static void copy_images(void) |
25697a35 GS |
900 | { |
901 | FILE *img_in, *img_ou; | |
06ced858 FM |
902 | char images[512]; |
903 | char imgdir[MAXLEN]; | |
904 | char srcfile[MAXLEN]; | |
905 | char dstfile[MAXLEN]; | |
906 | DIR *dirp; | |
907 | struct dirent *direntp; | |
908 | char buffer[MAXLEN]; | |
909 | size_t nread; | |
910 | struct stat info; | |
911 | ||
912 | if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) { | |
10210234 | 913 | debuga(_("Cannot copy images to target directory %simages\n"),outdir); |
06b39c87 | 914 | exit(EXIT_FAILURE); |
06ced858 FM |
915 | } |
916 | if (access(images,R_OK)!=0) { | |
917 | mkdir(images,0755); | |
918 | } | |
919 | ||
920 | strcpy(imgdir,IMAGEDIR); | |
921 | dirp = opendir(imgdir); | |
922 | if(dirp==NULL) { | |
10210234 | 923 | debuga(_("(util) Can't open directory %s: %s\n"),imgdir,strerror(errno)); |
06ced858 FM |
924 | return; |
925 | } | |
926 | while ((direntp = readdir( dirp )) != NULL ){ | |
927 | if(direntp->d_name[0]=='.') | |
928 | continue; | |
929 | sprintf(srcfile,"%s/%s",imgdir,direntp->d_name); | |
930 | if (stat(srcfile,&info)) { | |
10210234 | 931 | debuga(_("Cannot stat \"%s\" - %s\n"),srcfile,strerror(errno)); |
06ced858 FM |
932 | continue; |
933 | } | |
934 | if (S_ISREG(info.st_mode)) { | |
935 | sprintf(dstfile,"%s/%s",images,direntp->d_name); | |
936 | img_in = fopen(srcfile, "rb"); | |
937 | if(img_in!=NULL) { | |
938 | img_ou = fopen(dstfile, "wb"); | |
939 | if(img_ou!=NULL) { | |
940 | while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) { | |
b5f13803 | 941 | if (fwrite(buffer,1,nread,img_ou)!=nread) { |
10210234 | 942 | debuga(_("Failed to copy image %s to %s\n"),srcfile,dstfile); |
b5f13803 FM |
943 | break; |
944 | } | |
06ced858 FM |
945 | } |
946 | fclose(img_ou); | |
947 | } else | |
c36c7384 | 948 | fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open/create file", dstfile, strerror(errno)); |
06ced858 FM |
949 | fclose(img_in); |
950 | } else | |
c36c7384 | 951 | fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open file", srcfile, strerror(errno)); |
06ced858 FM |
952 | } |
953 | } | |
954 | (void) closedir(dirp); | |
955 | ||
956 | return; | |
957 | } | |
958 | ||
fa6552b0 | 959 | int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us, const char *form) |
06ced858 | 960 | { |
6798f0a7 | 961 | FILE *fp_ou; |
25697a35 | 962 | int num=1, count=0; |
25697a35 | 963 | char wdir[MAXLEN]; |
25697a35 | 964 | char dirname2[MAXLEN]; |
fa6552b0 FM |
965 | int y1, y2; |
966 | int m1, m2; | |
967 | int d1, d2; | |
968 | int wlen, wlen2; | |
6798f0a7 | 969 | time_t curtime; |
a1de61fe | 970 | struct tm *loctm; |
25697a35 | 971 | |
fa6552b0 FM |
972 | strcpy(wdir,outdir); |
973 | wlen=strlen(wdir); | |
974 | y1=per1->start.tm_year+1900; | |
975 | y2=per1->end.tm_year+1900; | |
976 | m1=per1->start.tm_mon+1; | |
977 | m2=per1->end.tm_mon+1; | |
978 | d1=per1->start.tm_mday; | |
979 | d2=per1->end.tm_mday; | |
0349fa24 | 980 | if(IndexTree == INDEX_TREE_DATE) { |
fa6552b0 FM |
981 | wlen+=sprintf(wdir+wlen,"%04d",y1); |
982 | if(y1!=y2) wlen+=sprintf(wdir+wlen,"-%04d",y2); | |
491b862f GS |
983 | if(access(wdir, R_OK) != 0) |
984 | my_mkdir(wdir); | |
2357ef77 | 985 | |
fa6552b0 FM |
986 | wlen+=sprintf(wdir+wlen,"/%02d",m1); |
987 | if(m1 != m2) wlen+=sprintf(wdir+wlen,"-%02d",m2); | |
491b862f GS |
988 | if(access(wdir, R_OK) != 0) |
989 | my_mkdir(wdir); | |
2357ef77 | 990 | |
fa6552b0 FM |
991 | wlen+=sprintf(wdir+wlen,"/%02d",d1); |
992 | if(d1!=d2) wlen+=sprintf(wdir+wlen,"-%02d",d2); | |
e6414a9d | 993 | } else { |
fa6552b0 FM |
994 | if(df[0] == 'u') { |
995 | wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%04d%s%02d-%04d%s%02d",y1, | |
996 | conv_month_name(m1),d1,y2,conv_month_name(m2),d2); | |
997 | } else if(df[0] == 'e') { | |
998 | wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%02d%s%04d-%02d%s%04d",d1, | |
999 | conv_month_name(m1),y1,d2,conv_month_name(m2),y2); | |
1000 | } else if(df[0] == 'w') { | |
1001 | wlen2=strftime(wdir+wlen, sizeof(wdir)-wlen, "%Y.%U", &per1->start); | |
1002 | if (wlen2==0) return(-1); | |
1003 | wlen+=wlen2; | |
1004 | } | |
e6414a9d | 1005 | } |
25697a35 | 1006 | |
e6414a9d | 1007 | if(us[0] != '\0') { |
25697a35 GS |
1008 | strcat(wdir,"-"); |
1009 | strcat(wdir,us); | |
1010 | } | |
e6414a9d | 1011 | if(addr[0] != '\0') { |
25697a35 GS |
1012 | strcat(wdir,"-"); |
1013 | strcat(wdir,addr); | |
1014 | } | |
e6414a9d | 1015 | if(site[0] != '\0') { |
25697a35 GS |
1016 | strcat(wdir,"-"); |
1017 | strcat(wdir,site); | |
1018 | } | |
1019 | ||
d5d021c5 | 1020 | strcpy(outdirname,wdir); |
25697a35 | 1021 | |
0349fa24 | 1022 | if(IndexTree != INDEX_TREE_DATE) { |
e6414a9d | 1023 | if(!OverwriteReport) { |
491b862f GS |
1024 | while(num) { |
1025 | if(access(wdir,R_OK) == 0) { | |
d5d021c5 | 1026 | sprintf(wdir,"%s.%d",outdirname,num); |
491b862f GS |
1027 | num++; |
1028 | count++; | |
1029 | } else | |
1030 | break; | |
1031 | } | |
25697a35 | 1032 | |
491b862f GS |
1033 | if(count > 0) { |
1034 | if(debug) | |
d5d021c5 FM |
1035 | debuga(_("File %s already exists, moved to %s\n"),outdirname,wdir); |
1036 | rename(outdirname,wdir); | |
491b862f GS |
1037 | } |
1038 | } else { | |
d5d021c5 FM |
1039 | if(access(outdirname,R_OK) == 0) { |
1040 | unlinkdir(outdirname,1); | |
491b862f | 1041 | } |
25697a35 | 1042 | } |
d5d021c5 | 1043 | my_mkdir(outdirname); |
25697a35 | 1044 | } else { |
491b862f | 1045 | strcpy(dirname2,wdir); |
e6414a9d | 1046 | if(!OverwriteReport) { |
491b862f GS |
1047 | while(num) { |
1048 | if(access(wdir,R_OK) == 0) { | |
1049 | sprintf(wdir,"%s.%d",dirname2,num); | |
491b862f GS |
1050 | num++; |
1051 | count++; | |
1052 | } else | |
1053 | break; | |
1054 | } | |
48864d28 | 1055 | |
491b862f GS |
1056 | if(count > 0) { |
1057 | if(debug) | |
d5d021c5 | 1058 | debuga(_("File %s already exists, moved to %s\n"),dirname2,wdir); |
491b862f GS |
1059 | rename(dirname2,wdir); |
1060 | strcpy(dirname2,wdir); | |
1061 | } | |
1062 | } else { | |
1063 | if(access(wdir,R_OK) == 0) { | |
51465d08 | 1064 | unlinkdir(wdir,1); |
491b862f | 1065 | } |
25697a35 | 1066 | } |
48864d28 | 1067 | |
491b862f GS |
1068 | if(access(wdir, R_OK) != 0) |
1069 | my_mkdir(wdir); | |
25697a35 GS |
1070 | } |
1071 | ||
25697a35 | 1072 | strcpy(dirname2,wdir); |
25697a35 | 1073 | |
d5d021c5 | 1074 | sprintf(wdir,"%s/sarg-date",outdirname); |
6798f0a7 | 1075 | if ((fp_ou = fopen(wdir, "wt")) == 0) { |
d5d021c5 | 1076 | debuga(_("cannot open %s for writing\n"),wdir); |
6798f0a7 | 1077 | perror("SARG:"); |
06b39c87 | 1078 | exit(EXIT_FAILURE); |
456d78a5 | 1079 | } |
6798f0a7 | 1080 | time(&curtime); |
fd4dbc54 | 1081 | //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime)); |
a1de61fe FM |
1082 | loctm=localtime(&curtime); |
1083 | strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm); | |
fa6552b0 | 1084 | if (fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst)<0) { |
0e4df8c0 | 1085 | debuga(_("Failed to write the date in %s\n"),wdir); |
fa6552b0 FM |
1086 | perror("SARG:"); |
1087 | exit(EXIT_FAILURE); | |
1088 | } | |
1089 | if (fclose(fp_ou)==EOF) { | |
0e4df8c0 | 1090 | debuga(_("Failed to write the date in %s\n"),wdir); |
fa6552b0 FM |
1091 | perror("SARG:"); |
1092 | exit(EXIT_FAILURE); | |
1093 | } | |
25697a35 | 1094 | |
06ced858 | 1095 | copy_images(); |
fa6552b0 | 1096 | return(0); |
25697a35 GS |
1097 | } |
1098 | ||
25697a35 GS |
1099 | void strip_latin(char *line) |
1100 | { | |
9c7c6346 FM |
1101 | int i,j; |
1102 | int skip; | |
1103 | ||
1104 | j=0; | |
1105 | skip=0; | |
1106 | for (i=0;line[i];i++){ | |
1107 | if (skip){ | |
1108 | if (line[i]==';') skip=0; | |
1109 | } else { | |
1110 | if (line[i]=='&') | |
1111 | skip=1; | |
1112 | else | |
1113 | line[j++]=line[i]; | |
4bcb77cf | 1114 | } |
25697a35 | 1115 | } |
9c7c6346 | 1116 | line[j]='\0'; |
25697a35 GS |
1117 | return; |
1118 | ||
1119 | } | |
1120 | ||
120d768c | 1121 | void zdate(char *ftime,int ftimesize, const char *DateFormat) |
25697a35 GS |
1122 | { |
1123 | ||
1124 | time_t t; | |
1125 | struct tm *local; | |
1126 | ||
1127 | t = time(NULL); | |
1128 | local = localtime(&t); | |
1129 | if(strcmp(DateFormat,"u") == 0) | |
120d768c | 1130 | strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local); |
25697a35 | 1131 | if(strcmp(DateFormat,"e") == 0) |
120d768c | 1132 | strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local); |
25697a35 | 1133 | if(strcmp(DateFormat,"w") == 0) |
120d768c | 1134 | strftime(ftime, ftimesize, "%V-%H-%M", local); |
25697a35 GS |
1135 | return; |
1136 | } | |
1137 | ||
1138 | ||
1139 | char *fixtime(long int elap) | |
1140 | { | |
1141 | ||
1142 | int num = elap / 1000; | |
1143 | int hor = 0; | |
1144 | int min = 0; | |
1145 | int sec = 0; | |
1146 | static char buf[12]; | |
1147 | ||
1148 | if(strcmp(datetimeby,"bytes") == 0) { | |
48864d28 | 1149 | strcpy(buf,fixnum(elap,1)); |
25697a35 GS |
1150 | return buf; |
1151 | } | |
1152 | ||
25697a35 GS |
1153 | if(num<1) { |
1154 | sprintf(buf,"00:00:%02ld",elap); | |
1155 | return buf; | |
1156 | } | |
1157 | ||
1158 | hor=num / 3600; | |
1159 | min=(num % 3600) / 60; | |
1160 | sec=num % 60; | |
1161 | ||
48864d28 | 1162 | if(hor==0 && min==0 && sec==0) |
25697a35 | 1163 | strcpy(buf,"0"); |
48864d28 FM |
1164 | else |
1165 | sprintf(buf,"%01d:%02d:%02d",hor,min,sec); | |
25697a35 GS |
1166 | |
1167 | return buf; | |
1168 | ||
1169 | } | |
1170 | ||
1171 | ||
42b117e3 | 1172 | void date_from(char *date, int *dfrom, int *duntil) |
25697a35 | 1173 | { |
42b117e3 FM |
1174 | int d0; |
1175 | int m0; | |
1176 | int y0; | |
1177 | int d1; | |
1178 | int m1; | |
1179 | int y1; | |
1180 | int next=-1; | |
1181 | ||
1182 | if (sscanf(date,"%d/%d/%d%n",&d0,&m0,&y0,&next)!=3 || y0<100 || m0<1 || m0>12 || d0<1 || d0>31 || next<0) { | |
1183 | debuga(_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); | |
1184 | exit(EXIT_FAILURE); | |
1185 | } | |
1186 | if (date[next]=='-') { | |
1187 | if (sscanf(date+next+1,"%d/%d/%d",&d1,&m1,&y1)!=3 || y1<100 || m1<1 || m1>12 || d1<1 || d1>31) { | |
1188 | debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); | |
06b39c87 | 1189 | exit(EXIT_FAILURE); |
9c7c6346 | 1190 | } |
42b117e3 FM |
1191 | } else if (date[next]!='\0') { |
1192 | debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); | |
06b39c87 | 1193 | exit(EXIT_FAILURE); |
42b117e3 FM |
1194 | } else { |
1195 | d1=d0; | |
1196 | m1=m0; | |
1197 | y1=y0; | |
4bcb77cf | 1198 | } |
25697a35 | 1199 | |
42b117e3 FM |
1200 | *dfrom=y0*10000+m0*100+d0; |
1201 | *duntil=y1*10000+m1*100+d1; | |
1202 | sprintf(date,"%02d/%02d/%04d-%02d/%02d/%04d",d0,m0,y0,d1,m1,y1); | |
25697a35 GS |
1203 | return; |
1204 | } | |
1205 | ||
1206 | ||
1207 | char *strlow(char *string) | |
1208 | { | |
32e71fa4 | 1209 | char *s; |
25697a35 | 1210 | |
32e71fa4 FM |
1211 | if (string) |
1212 | { | |
1213 | for (s = string; *s; ++s) | |
1214 | *s = tolower(*s); | |
1215 | } | |
25697a35 | 1216 | |
32e71fa4 | 1217 | return string; |
25697a35 GS |
1218 | } |
1219 | ||
1220 | ||
1221 | ||
1222 | ||
1223 | char *strup(char *string) | |
1224 | { | |
32e71fa4 | 1225 | char *s; |
25697a35 | 1226 | |
32e71fa4 FM |
1227 | if (string) |
1228 | { | |
1229 | for (s = string; *s; ++s) | |
1230 | *s = toupper(*s); | |
1231 | } | |
25697a35 | 1232 | |
32e71fa4 | 1233 | return string; |
25697a35 GS |
1234 | } |
1235 | ||
1236 | ||
32e71fa4 | 1237 | void removetmp(const char *outdir) |
25697a35 GS |
1238 | { |
1239 | ||
1240 | FILE *fp_in; | |
1241 | char warea[256]; | |
06b39c87 | 1242 | char buf[MAXLEN]; |
25697a35 | 1243 | |
e6414a9d | 1244 | if(!RemoveTempFiles) |
25697a35 | 1245 | return; |
48864d28 | 1246 | |
32e71fa4 | 1247 | if(debug) { |
9f70c14e | 1248 | debuga(_("Removing temporary files sarg-general, sarg-period\n")); |
32e71fa4 | 1249 | } |
48864d28 | 1250 | if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) { |
9f70c14e | 1251 | debuga(_("(removetmp) directory too long to remove %s/sarg-period\n"),outdir); |
06b39c87 | 1252 | exit(EXIT_FAILURE); |
48864d28 | 1253 | } |
32e71fa4 | 1254 | if((fp_in=fopen(warea,"r"))==NULL){ |
9f70c14e | 1255 | debuga(_("(removetmp) Cannot open file %s\n"),warea); |
06b39c87 | 1256 | exit(EXIT_FAILURE); |
32e71fa4 FM |
1257 | } |
1258 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
2240dcea | 1259 | if(strncmp(buf,"TOTAL",5) == 0 && (buf[6]=='\t' || buf[6]==' ')) |
32e71fa4 FM |
1260 | break; |
1261 | } | |
1262 | fclose(fp_in); | |
fa6552b0 | 1263 | |
32e71fa4 | 1264 | if((fp_in=fopen(warea,"w"))==NULL){ |
9f70c14e | 1265 | debuga(_("(removetmp) Cannot open file %s\n"),warea); |
06b39c87 | 1266 | exit(EXIT_FAILURE); |
25697a35 | 1267 | } |
fa6552b0 FM |
1268 | if (fputs(buf,fp_in)==EOF) { |
1269 | debuga(_("Failed to write the total line in %s - %s\n"),warea,strerror(errno)); | |
1270 | exit(EXIT_FAILURE); | |
1271 | } | |
1272 | if (fclose(fp_in)==EOF) { | |
1273 | debuga(_("Failed to close %s after writing the total line - %s\n"),warea,strerror(errno)); | |
06b39c87 | 1274 | exit(EXIT_FAILURE); |
32e71fa4 | 1275 | } |
48864d28 | 1276 | |
25697a35 GS |
1277 | return; |
1278 | } | |
1279 | ||
48864d28 | 1280 | void load_excludecodes(const char *ExcludeCodes) |
25697a35 GS |
1281 | { |
1282 | ||
1283 | FILE *fp_in; | |
1284 | char data[80]; | |
48864d28 FM |
1285 | int i; |
1286 | ||
1287 | if(ExcludeCodes[0] == '\0') | |
1288 | return; | |
1289 | ||
1290 | if((excludecode=(char *) malloc(1024))==NULL) { | |
fcdc0918 | 1291 | debuga(_("malloc error (1024)\n")); |
06b39c87 | 1292 | exit(EXIT_FAILURE); |
48864d28 FM |
1293 | } |
1294 | bzero(excludecode,1024); | |
25697a35 GS |
1295 | |
1296 | if((fp_in=fopen(ExcludeCodes,"r"))==NULL) { | |
fcdc0918 | 1297 | debuga(_("(util) Cannot open file %s (exclude_codes)\n"),ExcludeCodes); |
06b39c87 | 1298 | exit(EXIT_FAILURE); |
25697a35 GS |
1299 | } |
1300 | ||
32e71fa4 | 1301 | while(fgets(data,sizeof(data),fp_in)!=NULL) { |
48864d28 FM |
1302 | for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]=0; |
1303 | if (i<0) continue; | |
25697a35 GS |
1304 | strcat(excludecode,data); |
1305 | strcat(excludecode,";"); | |
25697a35 GS |
1306 | } |
1307 | ||
1308 | fclose(fp_in); | |
1309 | return; | |
1310 | ||
1311 | } | |
1312 | ||
48864d28 FM |
1313 | void free_excludecodes(void) |
1314 | { | |
1315 | if (excludecode) { | |
1316 | free(excludecode); | |
1317 | excludecode=NULL; | |
1318 | } | |
1319 | } | |
1320 | ||
32e71fa4 | 1321 | int vercode(const char *code) |
25697a35 | 1322 | { |
48864d28 FM |
1323 | char *cod; |
1324 | int clen; | |
1325 | ||
1326 | if (excludecode && excludecode[0]!=0) { | |
1327 | clen=strlen(code); | |
1328 | for (cod=excludecode ; cod ; cod=strchr(cod+1,';')) { | |
1329 | if (strncmp(code,cod,clen)==0 && cod[clen]==';') | |
1330 | return 1; | |
4bcb77cf | 1331 | } |
25697a35 GS |
1332 | } |
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | void fixnone(char *str) | |
1337 | { | |
48864d28 | 1338 | int i; |
32e71fa4 | 1339 | |
48864d28 FM |
1340 | for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--); |
1341 | if(i==3 && strncmp(str,"none",4) == 0) | |
25697a35 GS |
1342 | str[0]='\0'; |
1343 | ||
1344 | return; | |
1345 | } | |
1346 | ||
2357ef77 FM |
1347 | void fixendofline(char *str) |
1348 | { | |
1349 | int i; | |
1350 | ||
1351 | for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--) str[i]=0; | |
1352 | } | |
1353 | ||
25697a35 | 1354 | #ifdef LEGACY_TESTVALIDUSERCHAR |
32e71fa4 | 1355 | int testvaliduserchar(const char *user) |
25697a35 GS |
1356 | { |
1357 | ||
1358 | int x=0; | |
1359 | int y=0; | |
1360 | ||
1361 | for (y=0; y<strlen(UserInvalidChar); y++) { | |
1362 | for (x=0; x<strlen(user); x++) { | |
1363 | if(user[x] == UserInvalidChar[y]) | |
1364 | return 1; | |
1365 | } | |
1366 | } | |
1367 | return 0; | |
1368 | } | |
1369 | #else | |
32e71fa4 | 1370 | int testvaliduserchar(const char *user) |
25697a35 GS |
1371 | { |
1372 | ||
1373 | char * p_UserInvalidChar = UserInvalidChar ; | |
32e71fa4 | 1374 | const char * p_user ; |
25697a35 GS |
1375 | |
1376 | while( *p_UserInvalidChar ) { | |
1377 | p_user = user ; | |
1378 | while ( *p_user ) { | |
1379 | if( *p_UserInvalidChar == *p_user ) | |
1380 | return 1; | |
1381 | p_user++ ; | |
1382 | } | |
1383 | p_UserInvalidChar++ ; | |
1384 | } | |
1385 | return 0; | |
1386 | } | |
1387 | #endif | |
1388 | ||
1389 | int compar( const void *a, const void *b ) | |
1390 | { if( *(int *)a > *(int *)b ) return 1; | |
1391 | if( *(int *)a < *(int *)b ) return -1; | |
1392 | return 0; | |
1393 | } | |
1394 | ||
1395 | int getnumlist( char *buf, numlist *list, const int len, const int maxvalue ) | |
48864d28 FM |
1396 | { |
1397 | int i, j, d, flag, r1, r2; | |
1398 | char *pbuf, **bp, *strbufs[ 24 ]; | |
1399 | ||
1400 | bp = strbufs; | |
1401 | strtok( buf, " \t" ); | |
1402 | for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) { | |
1403 | if( ++bp >= &strbufs[ 24 ] ) | |
1404 | break; | |
1405 | list->len++; | |
25697a35 | 1406 | } |
48864d28 FM |
1407 | if( ! list->len ) |
1408 | return -1; | |
1409 | d = 0; | |
1410 | for( i = 0; i < list->len; i++ ) { | |
1411 | if( strchr( strbufs[ i ], '-' ) != 0 ) { | |
1412 | pbuf = strbufs[ i ]; | |
1413 | strtok( pbuf, "-" ); | |
1414 | pbuf = strtok( NULL, "\0" ); | |
1415 | r1 = atoi( strbufs[ i ] ); | |
1416 | if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 ) | |
1417 | return -1; | |
1418 | if( i + d + ( r2 - r1 ) + 1 <= len ) { | |
1419 | for( j = r1; j <= r2; j++ ) | |
1420 | list->list[ i + d++ ] = j; | |
1421 | d--; | |
25697a35 GS |
1422 | } |
1423 | } | |
48864d28 FM |
1424 | else |
1425 | if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue ) | |
1426 | return 1; | |
25697a35 | 1427 | } |
48864d28 FM |
1428 | list->len += d; |
1429 | qsort( list->list, list->len, sizeof( int ), compar ); | |
1430 | do { | |
1431 | flag = 0; | |
1432 | for( i = 0; i < list->len - 1; i++ ) | |
1433 | if( list->list[ i ] == list->list[ i + 1 ] ) { | |
1434 | for( j = i + 1; j < list->len; j++ ) | |
1435 | list->list[ j - 1 ] = list->list[ j ]; | |
1436 | list->len--; | |
1437 | flag = 1; | |
1438 | break; | |
1439 | } | |
25697a35 | 1440 | } while( flag ); |
48864d28 | 1441 | return 0; |
25697a35 GS |
1442 | } |
1443 | ||
1444 | ||
32e71fa4 | 1445 | char *get_size(const char *path, const char *file) |
491b862f GS |
1446 | { |
1447 | FILE *fp; | |
9c7c6346 | 1448 | static char response[255]; |
32e71fa4 | 1449 | char cmd[255]; |
9c7c6346 | 1450 | char *ptr; |
491b862f | 1451 | |
32e71fa4 | 1452 | if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) { |
10210234 | 1453 | debuga(_("Cannot get disk space because the path %s%s is too long\n"),path,file); |
06b39c87 | 1454 | exit(EXIT_FAILURE); |
32e71fa4 | 1455 | } |
fa6552b0 FM |
1456 | if ((fp = popen(cmd, "r")) == NULL) { |
1457 | debuga(_("Cannot get disk space with command %s\n"),cmd); | |
1458 | exit(EXIT_FAILURE); | |
1459 | } | |
05b90947 | 1460 | if (!fgets(response, sizeof(response), fp)) { |
10210234 | 1461 | debuga(_("Cannot get disk size with command %s\n"),cmd); |
06b39c87 | 1462 | exit(EXIT_FAILURE); |
05b90947 | 1463 | } |
9c7c6346 FM |
1464 | ptr=strchr(response,'\t'); |
1465 | if (ptr==NULL) { | |
10210234 | 1466 | debuga(_("The command %s failed\n"),cmd); |
06b39c87 | 1467 | exit(EXIT_FAILURE); |
4bcb77cf | 1468 | } |
491b862f | 1469 | pclose(fp); |
9c7c6346 | 1470 | *ptr='\0'; |
491b862f | 1471 | |
9c7c6346 | 1472 | return (response); |
491b862f GS |
1473 | } |
1474 | ||
dfb337be FM |
1475 | void show_info(FILE *fp_ou) |
1476 | { | |
1477 | char ftime[127]; | |
1478 | ||
e6414a9d | 1479 | if(!ShowSargInfo) return; |
120d768c | 1480 | zdate(ftime, sizeof(ftime), DateFormat); |
c36c7384 | 1481 | fprintf(fp_ou,"<div class=\"info\">%s <a href='%s'>%s-%s</a> %s %s</div>\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); |
dfb337be FM |
1482 | } |
1483 | ||
c0ec9cc7 | 1484 | void show_sarg(FILE *fp_ou, int depth) |
dfb337be | 1485 | { |
c0ec9cc7 FM |
1486 | int i; |
1487 | ||
1488 | if(!ShowSargLogo) return; | |
1489 | fputs("<div class=\"logo\"><a href=\"http://sarg.sourceforge.net\"><img src=\"",fp_ou); | |
1490 | for (i=0 ; i<depth ; i++) | |
1491 | fputs("../",fp_ou); | |
1492 | fputs("images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a> Squid Analysis Report Generator</div>\n",fp_ou); | |
dfb337be FM |
1493 | } |
1494 | ||
1495 | void write_logo_image(FILE *fp_ou) | |
1496 | { | |
1497 | if(LogoImage[0]!='\0') | |
c0ec9cc7 | 1498 | fprintf(fp_ou, "<div class=\"logo\"><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"Logo\"> %s</div>\n",LogoImage,Width,Height,LogoText); |
dfb337be | 1499 | } |
491b862f | 1500 | |
c0ec9cc7 | 1501 | void write_html_header(FILE *fp_ou, int depth, const char *page_title) |
491b862f | 1502 | { |
c0ec9cc7 FM |
1503 | //fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n",fp_ou); |
1504 | fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n",fp_ou); | |
1505 | fprintf(fp_ou, "<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
1506 | if (page_title) fprintf(fp_ou,"<title>%s</title>\n",page_title); | |
491b862f | 1507 | css(fp_ou); |
dfb337be FM |
1508 | fprintf(fp_ou,"</head>\n<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); |
1509 | write_logo_image(fp_ou); | |
c0ec9cc7 FM |
1510 | show_sarg(fp_ou, depth); |
1511 | fprintf(fp_ou,"<div class=\"title\"><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title_c\">%s</th></tr>\n",Title); | |
1512 | } | |
1513 | ||
1514 | void close_html_header(FILE *fp_ou) | |
1515 | { | |
1516 | fputs("</table></div>\n",fp_ou); | |
1517 | } | |
1518 | ||
fa6552b0 | 1519 | int write_html_trailer(FILE *fp_ou) |
c0ec9cc7 FM |
1520 | { |
1521 | show_info(fp_ou); | |
fa6552b0 FM |
1522 | if (fputs("</body>\n</html>\n",fp_ou)==EOF) return(-1); |
1523 | return(0); | |
dfb337be FM |
1524 | } |
1525 | ||
ac422f9b | 1526 | void output_html_string(FILE *fp_ou,const char *str,int maxlen) |
dfb337be | 1527 | { |
ac422f9b | 1528 | int i=0; |
dfb337be | 1529 | |
ac422f9b | 1530 | while (*str && (maxlen<=0 || i<maxlen)) { |
dfb337be FM |
1531 | switch (*str) { |
1532 | case '&': | |
1533 | fputs("&",fp_ou); | |
1534 | break; | |
1535 | case '<': | |
1536 | fputs("<",fp_ou); | |
1537 | break; | |
1538 | case '>': | |
1539 | fputs(">",fp_ou); | |
1540 | break; | |
1541 | case '"': | |
1542 | fputs(""",fp_ou); | |
1543 | break; | |
1544 | case '\'': | |
1545 | fputs("'",fp_ou); | |
1546 | break; | |
1547 | default: | |
1548 | fputc(*str,fp_ou); | |
1549 | } | |
1550 | str++; | |
ac422f9b FM |
1551 | i++; |
1552 | } | |
1553 | if (maxlen>0 && i>=maxlen) | |
1554 | fputs("…",fp_ou); | |
1555 | } | |
1556 | ||
1557 | void output_html_url(FILE *fp_ou,const char *url) | |
1558 | { | |
1559 | while (*url) { | |
1560 | if (*url=='&') | |
1561 | fputs("&",fp_ou); | |
1562 | else | |
1563 | fputc(*url,fp_ou); | |
1564 | url++; | |
dfb337be | 1565 | } |
491b862f GS |
1566 | } |
1567 | ||
32e71fa4 | 1568 | void baddata(void) |
491b862f | 1569 | { |
51465d08 | 1570 | char dir[1024]; |
05b90947 | 1571 | |
d6e703cc | 1572 | printf("SARG: ------------------------------------------------------------------------------\n"); |
10210234 FM |
1573 | printf(_("SARG: MALICIUS CODE DETECTED.\n")); |
1574 | printf(_("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n")); | |
1575 | printf(_("SARG: please review your access.log and/or your useragent.log file.\n")); | |
1576 | printf(_("SARG: process stoped. No actions taken.\n")); | |
d6e703cc FM |
1577 | printf("SARG: ------------------------------------------------------------------------------\n"); |
1578 | ||
51465d08 | 1579 | if (snprintf(dir,sizeof(dir),"%s/sarg",tmp)>=sizeof(dir)) { |
10210234 | 1580 | debuga(_("temporary directory too long: %s/sarg\n"),tmp); |
06b39c87 | 1581 | exit(EXIT_FAILURE); |
05b90947 | 1582 | } |
51465d08 | 1583 | unlinkdir(dir,0); |
d5d021c5 | 1584 | unlinkdir(outdirname,0); |
d6e703cc | 1585 | |
06b39c87 | 1586 | exit(EXIT_FAILURE); |
491b862f GS |
1587 | } |
1588 | ||
f84a35a3 FM |
1589 | void url_hostname(const char *url,char *hostname,int hostsize) |
1590 | { | |
1591 | int i; | |
1592 | ||
1593 | hostsize--; | |
1594 | for (i=0 ; i<hostsize && url[i] && url[i]!='/' ; i++) | |
1595 | hostname[i]=url[i]; | |
1596 | hostname[i]='\0'; | |
1597 | } | |
491b862f | 1598 | |
48864d28 | 1599 | void url_module(const char *url, char *w2) |
25697a35 GS |
1600 | { |
1601 | int x, y; | |
1602 | char w[255]; | |
1603 | ||
25697a35 GS |
1604 | y=0; |
1605 | for(x=strlen(url)-1; x>=0; x--) { | |
48864d28 FM |
1606 | if(url[x] == '/' || y>=sizeof(w)-1) break; |
1607 | w[y++]=url[x]; | |
25697a35 | 1608 | } |
4157aa09 FM |
1609 | if (x<0) { |
1610 | w2[0]='\0'; | |
1611 | return; | |
1612 | } | |
25697a35 | 1613 | |
48864d28 FM |
1614 | x=0; |
1615 | for(y=y-1; y>=0; y--) { | |
1616 | w2[x++]=w[y]; | |
25697a35 | 1617 | } |
4157aa09 | 1618 | w2[x]='\0'; |
25697a35 GS |
1619 | } |
1620 | ||
e5b2c6f0 FM |
1621 | void url_to_file(const char *url,char *file,int filesize) |
1622 | { | |
1623 | int i,skip; | |
1624 | ||
1625 | filesize--; | |
1626 | skip=0; | |
1627 | for(i=0; i<filesize && *url; url++) { | |
1628 | if(*url=='?' || *url=='-' || *url=='.' || *url==':' || *url=='/' || *url=='\\' || | |
1629 | *url=='*' || *url=='\'' || *url=='\"' || *url=='$') { | |
1630 | if (!skip) file[i++]='_'; | |
1631 | skip=1; | |
1632 | } else { | |
1633 | file[i++]=*url; | |
1634 | skip=0; | |
1635 | } | |
1636 | } | |
1637 | file[i]='\0'; | |
1638 | } | |
d6e703cc | 1639 | |
32e71fa4 | 1640 | void version(void) |
25697a35 | 1641 | { |
d25d4e6a | 1642 | printf(_("SARG Version: %s\n"),VERSION); |
2824ec9b | 1643 | exit(EXIT_SUCCESS); |
25697a35 | 1644 | } |
5f3cfd1d FM |
1645 | |
1646 | char *get_param_value(const char *param,char *line) | |
1647 | { | |
1648 | int plen; | |
2357ef77 | 1649 | |
5f3cfd1d FM |
1650 | while (*line==' ' || *line=='\t') line++; |
1651 | plen=strlen(param); | |
1652 | if (strncasecmp(line,param,plen)) return(NULL); | |
1653 | if (line[plen]!=' ' && line[plen]!='\t') return(NULL); | |
1654 | line+=plen; | |
1655 | while (*line==' ' || *line=='\t') line++; | |
1656 | return(line); | |
1657 | } | |
936c9905 | 1658 | |
51465d08 | 1659 | void unlinkdir(const char *dir,int contentonly) |
304a739d | 1660 | { |
51465d08 FM |
1661 | struct stat st; |
1662 | DIR *dirp; | |
1663 | struct dirent *direntp; | |
1664 | char dname[MAXLEN]; | |
463f8e09 | 1665 | int err; |
51465d08 FM |
1666 | |
1667 | dirp=opendir(dir); | |
1668 | if (!dirp) return; | |
1669 | while ((direntp = readdir(dirp)) != NULL) { | |
1670 | if (direntp->d_name[0] == '.' && (direntp->d_name[1] == '\0' || | |
1671 | (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0'))) | |
1672 | continue; | |
1673 | if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) { | |
10210234 | 1674 | debuga(_("directory name to delete too long: %s/%s\n"),dir,direntp->d_name); |
06b39c87 | 1675 | exit(EXIT_FAILURE); |
51465d08 | 1676 | } |
463f8e09 FM |
1677 | #ifdef HAVE_LSTAT |
1678 | err=lstat(dname,&st); | |
1679 | #else | |
1680 | err=stat(dname,&st); | |
1681 | #endif | |
1682 | if (err) { | |
10210234 | 1683 | debuga(_("cannot stat %s\n"),dname); |
06b39c87 | 1684 | exit(EXIT_FAILURE); |
51465d08 FM |
1685 | } |
1686 | if (S_ISREG(st.st_mode)) { | |
1687 | if (unlink(dname)) { | |
10210234 | 1688 | debuga(_("cannot delete %s - %s\n"),dname,strerror(errno)); |
06b39c87 | 1689 | exit(EXIT_FAILURE); |
304a739d | 1690 | } |
51465d08 | 1691 | } else if (S_ISDIR(st.st_mode)) { |
0511cf2d | 1692 | unlinkdir(dname,0); |
51465d08 | 1693 | } else { |
10210234 | 1694 | debuga(_("unknown path type %s\n"),dname); |
304a739d FM |
1695 | } |
1696 | } | |
51465d08 | 1697 | closedir(dirp); |
304a739d | 1698 | |
51465d08 FM |
1699 | if (!contentonly) { |
1700 | if (rmdir(dir)) { | |
10210234 | 1701 | debuga(_("cannot delete %s - %s\n"),dir,strerror(errno)); |
06b39c87 | 1702 | exit(EXIT_FAILURE); |
51465d08 FM |
1703 | } |
1704 | } | |
1705 | } | |
ac422f9b | 1706 |