1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * rrd_cgi.c RRD Web Page Generator
5 *****************************************************************************/
7 #include "rrd_tool.h"
8 #include <cgi.h>
9 #include <time.h>
12 #define MEMBLK 1024
14 /* global variable for libcgi */
15 s_cgi **cgiArg;
17 /* in arg[0] find tags beginning with arg[1] call arg[2] on them
18 and replace by result of arg[2] call */
19 int parse(char **, long, char *, char *(*)(long , char **));
21 /**************************************************/
22 /* tag replacers ... they are called from parse */
23 /* through function pointers */
24 /**************************************************/
26 /* return cgi var named arg[0] */
27 char* cgiget(long , char **);
29 /* return a quoted cgi var named arg[0] */
30 char* cgigetq(long , char **);
32 /* return a quoted and sanitized cgi variable */
33 char* cgigetqp(long , char **);
35 /* call rrd_graph and insert apropriate image tag */
36 char* drawgraph(long, char **);
38 /* return PRINT functions from last rrd_graph call */
39 char* drawprint(long, char **);
41 /* pretty-print the <last></last> value for some.rrd via strftime() */
42 char* printtimelast(long, char **);
44 /* pretty-print current time */
45 char* printtimenow(long,char **);
47 /* set an evironment variable */
48 char* rrdsetenv(long, char **);
50 /* get an evironment variable */
51 char* rrdgetenv(long, char **);
53 /* include the named file at this point */
54 char* includefile(long, char **);
56 /* for how long is the output of the cgi valid ? */
57 char* rrdgoodfor(long, char **);
59 /** http protocol needs special format, and GMT time **/
60 char *http_time(time_t *);
62 /* return a pointer to newly alocated copy of this string */
63 char *stralloc(char *);
65 static long goodfor=0;
66 static char **calcpr=NULL;
67 static void calfree (void){
68 if (calcpr) {
69 long i;
70 for(i=0;calcpr[i];i++){
71 if (calcpr[i]){
72 free(calcpr[i]);
73 }
74 }
75 if (calcpr) {
76 free(calcpr);
77 }
78 }
79 }
81 /* create freeable version of the string */
82 char * stralloc(char *str){
83 char *nstr = malloc((strlen(str)+1)*sizeof(char));
84 strcpy(nstr,str);
85 return(nstr);
86 }
88 int main(int argc, char *argv[]) {
89 long length;
90 char *buffer;
91 char *server_url = NULL;
92 long i;
93 long filter=0;
94 #ifdef MUST_DISABLE_SIGFPE
95 signal(SIGFPE,SIG_IGN);
96 #endif
97 #ifdef MUST_DISABLE_FPMASK
98 fpsetmask(0);
99 #endif
100 /* what do we get for cmdline arguments?
101 for (i=0;i<argc;i++)
102 printf("%d-'%s'\n",i,argv[i]); */
103 while (1){
104 static struct option long_options[] =
105 {
106 {"filter", no_argument, 0, 'f'},
107 {0,0,0,0}
108 };
109 int option_index = 0;
110 int opt;
111 opt = getopt_long(argc, argv, "f",
112 long_options, &option_index);
113 if (opt == EOF)
114 break;
115 switch(opt) {
116 case 'f':
117 filter=1;
118 break;
119 case '?':
120 printf("unknown commandline option '%s'\n",argv[optind-1]);
121 return -1;
122 }
123 }
125 if(filter==0) {
126 cgiDebug(0,0);
127 cgiArg = cgiInit ();
128 server_url = getenv("SERVER_URL");
129 }
131 if (optind != argc-1) {
132 fprintf(stderr, "ERROR: expected a filename\n");
133 exit(1);
134 } else {
135 length = readfile(argv[optind], &buffer, 1);
136 }
138 if(rrd_test_error()){
139 fprintf(stderr, "ERROR: %s\n",rrd_get_error());
140 exit(1);
141 }
144 if(filter==0) {
145 /* pass 1 makes only sense in cgi mode */
146 for (i=0;buffer[i] != '\0'; i++){
147 i +=parse(&buffer,i,"<RRD::CV",cgiget);
148 i +=parse(&buffer,i,"<RRD::CV::QUOTE",cgigetq);
149 i +=parse(&buffer,i,"<RRD::CV::PATH",cgigetqp);
150 i +=parse(&buffer,i,"<RRD::GETENV",rrdgetenv);
151 }
152 }
154 /* pass 2 */
155 for (i=0;buffer[i] != '\0'; i++){
156 i += parse(&buffer,i,"<RRD::GOODFOR",rrdgoodfor);
157 i += parse(&buffer,i,"<RRD::SETENV",rrdsetenv);
158 i += parse(&buffer,i,"<RRD::INCLUDE",includefile);
159 i += parse(&buffer,i,"<RRD::TIME::LAST",printtimelast);
160 i += parse(&buffer,i,"<RRD::TIME::NOW",printtimenow);
161 }
163 /* pass 3 */
164 for (i=0;buffer[i] != '\0'; i++){
165 i += parse(&buffer,i,"<RRD::GRAPH",drawgraph);
166 i += parse(&buffer,i,"<RRD::PRINT",drawprint);
167 }
169 if (filter==0){
170 printf ("Content-Type: text/html\n"
171 "Content-Length: %d\n", strlen(buffer));
172 if (labs(goodfor) > 0){
173 time_t now;
174 now = time(NULL);
175 printf ("Last-Modified: %s\n",http_time(&now));
176 now += labs(goodfor);
177 printf ("Expires: %s\n",http_time(&now));
178 if (goodfor < 0) {
179 printf("Refresh: %ld\n", labs(goodfor));
180 }
181 }
182 printf ("\n");
183 }
184 printf ("%s", buffer);
185 calfree();
186 if (buffer){
187 free(buffer);
188 }
189 exit(0);
190 }
192 /* remove occurences of .. this is a general measure to make
193 paths which came in via cgi do not go UP ... */
195 char* rrdsetenv(long argc, char **args){
196 if (argc >= 2) {
197 char *xyz=malloc((strlen(args[0])+strlen(args[1])+3)*sizeof(char));
198 if (xyz == NULL){
199 return stralloc("[ERROR: allocating setenv buffer]");
200 };
201 sprintf(xyz,"%s=%s",args[0],args[1]);
202 if( putenv(xyz) == -1) {
203 return stralloc("[ERROR: faild to do putenv]");
204 };
205 } else {
206 return stralloc("[ERROR: setenv faild because not enough arguments were defined]");
207 }
208 return stralloc("");
209 }
211 char* rrdgetenv(long argc, char **args){
212 if (argc != 1) {
213 return stralloc("[ERROR: getenv faild because it did not get 1 argument only]");
214 };
215 return stralloc(getenv(args[0]));
216 }
218 char* rrdgoodfor(long argc, char **args){
219 if (argc == 1) {
220 goodfor = atol(args[0]);
221 } else {
222 return stralloc("[ERROR: goodfor expected 1 argument]");
223 }
225 if (goodfor == 0){
226 return stralloc("[ERROR: goodfor value must not be 0]");
227 }
229 return stralloc("");
230 }
232 char* includefile(long argc, char **args){
233 char *buffer;
234 if (argc >= 1) {
235 readfile(args[0], &buffer, 0);
236 if (rrd_test_error()) {
237 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
238 sprintf(err, "[ERROR: %s]",rrd_get_error());
239 rrd_clear_error();
240 return err;
241 } else {
242 return buffer;
243 }
244 }
245 else
246 {
247 return stralloc("[ERROR: No Inclue file defined]");
248 }
249 }
251 char* rrdstrip(char *buf){
252 char *start;
253 if (buf == NULL) return NULL;
254 buf = stralloc(buf); /* make a copy of the buffer */
255 if (buf == NULL) return NULL;
256 while ((start = strstr(buf,"<"))){
257 *start = '_';
258 }
259 while ((start = strstr(buf,">"))){
260 *start = '_';
261 }
262 return buf;
263 }
265 char* cgigetq(long argc, char **args){
266 if (argc>= 1){
267 char *buf = rrdstrip(cgiGetValue(cgiArg,args[0]));
268 char *buf2;
269 char *c,*d;
270 int qc=0;
271 if (buf==NULL) return NULL;
273 for(c=buf;*c != '\0';c++)
274 if (*c == '"') qc++;
275 if((buf2=malloc((strlen(buf) + qc*4 +4) * sizeof(char)))==NULL){
276 perror("Malloc Buffer");
277 exit(1);
278 };
279 c=buf;
280 d=buf2;
281 *(d++) = '"';
282 while(*c != '\0'){
283 if (*c == '"') {
284 *(d++) = '"';
285 *(d++) = '\'';
286 *(d++) = '"';
287 *(d++) = '\'';
288 }
289 *(d++) = *(c++);
290 }
291 *(d++) = '"';
292 *(d) = '\0';
293 free(buf);
294 return buf2;
295 }
297 return stralloc("[ERROR: not enough argument for RRD::CV::QUOTE]");
298 }
300 /* remove occurences of .. this is a general measure to make
301 paths which came in via cgi do not go UP ... */
303 char* cgigetqp(long argc, char **args){
304 if (argc>= 1){
305 char *buf = rrdstrip(cgiGetValue(cgiArg,args[0]));
306 char *buf2;
307 char *c,*d;
308 int qc=0;
309 if (buf==NULL) return NULL;
311 for(c=buf;*c != '\0';c++)
312 if (*c == '"') qc++;
313 if((buf2=malloc((strlen(buf) + qc*4 +4) * sizeof(char)))==NULL){
314 perror("Malloc Buffer");
315 exit(1);
316 };
317 c=buf;
318 d=buf2;
319 *(d++) = '"';
320 while(*c != '\0'){
321 if (*c == '"') {
322 *(d++) = '"';
323 *(d++) = '\'';
324 *(d++) = '"';
325 *(d++) = '\'';
326 }
327 if(*c == '/') {
328 *(d++) = '_';c++;
329 } else {
330 if (*c=='.' && *(c+1) == '.'){
331 c += 2;
332 *(d++) = '_'; *(d++) ='_';
333 } else {
335 *(d++) = *(c++);
336 }
337 }
338 }
339 *(d++) = '"';
340 *(d) = '\0';
341 free(buf);
342 return buf2;
343 }
345 return stralloc("[ERROR: not enough arguments for RRD::CV::PATH]");
347 }
350 char* cgiget(long argc, char **args){
351 if (argc>= 1)
352 return rrdstrip(cgiGetValue(cgiArg,args[0]));
353 else
354 return stralloc("[ERROR: not enough arguments for RRD::CV]");
355 }
359 char* drawgraph(long argc, char **args){
360 int i,xsize, ysize;
361 for(i=0;i<argc;i++)
362 if(strcmp(args[i],"--imginfo")==0 || strcmp(args[i],"-g")==0) break;
363 if(i==argc) {
364 args[argc++] = "--imginfo";
365 args[argc++] = "<IMG SRC=\"./%s\" WIDTH=\"%lu\" HEIGHT=\"%lu\">";
366 }
367 optind=0; /* reset gnu getopt */
368 opterr=0; /* reset gnu getopt */
369 calfree();
370 if( rrd_graph(argc+1, args-1, &calcpr, &xsize, &ysize) != -1 ) {
371 return stralloc(calcpr[0]);
372 } else {
373 if (rrd_test_error()) {
374 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
375 sprintf(err, "[ERROR: %s]",rrd_get_error());
376 rrd_clear_error();
377 calfree();
378 return err;
379 }
380 }
381 return NULL;
382 }
384 char* drawprint(long argc, char **args){
385 if (argc==1 && calcpr){
386 long i=0;
387 while (calcpr[i] != NULL) i++; /*determine number lines in calcpr*/
388 if (atol(args[0])<i-1)
389 return stralloc(calcpr[atol(args[0])+1]);
390 }
391 return stralloc("[ERROR: RRD::PRINT argument error]");
392 }
394 char* printtimelast(long argc, char **args) {
395 time_t last;
396 struct tm tm_last;
397 char *buf;
398 if ( argc == 2 ) {
399 buf = malloc(255);
400 if (buf == NULL){
401 return stralloc("[ERROR: allocating strftime buffer]");
402 };
403 last = rrd_last(argc+1, args-1);
404 if (rrd_test_error()) {
405 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
406 sprintf(err, "[ERROR: %s]",rrd_get_error());
407 rrd_clear_error();
408 return err;
409 }
410 tm_last = *localtime(&last);
411 strftime(buf,254,args[1],&tm_last);
412 return buf;
413 }
414 if ( argc < 2 ) {
415 return stralloc("[ERROR: too few arguments for RRD::TIME::LAST]");
416 }
417 return stralloc("[ERROR: not enough arguments for RRD::TIME::LAST]");
418 }
420 char* printtimenow(long argc, char **args) {
421 time_t now = time(NULL);
422 struct tm tm_now;
423 char *buf;
424 if ( argc == 1 ) {
425 buf = malloc(255);
426 if (buf == NULL){
427 return stralloc("[ERROR: allocating strftime buffer]");
428 };
429 tm_now = *localtime(&now);
430 strftime(buf,254,args[0],&tm_now);
431 return buf;
432 }
433 if ( argc < 1 ) {
434 return stralloc("[ERROR: too few arguments for RRD::TIME::NOW]");
435 }
436 return stralloc("[ERROR: not enough arguments for RRD::TIME::NOW]");
437 }
439 /* scan aLine until an unescaped '>' arives */
440 char* scanargs(char *aLine, long *argc, char ***args)
441 {
442 char *getP, *putP;
443 char Quote = 0;
444 int argal = MEMBLK;
445 int braket = 0;
446 int inArg = 0;
447 if (((*args) = (char **) malloc(MEMBLK*sizeof(char *))) == NULL) {
448 return NULL;
449 }
450 /* sikp leading blanks */
451 while (*aLine && *aLine <= ' ') aLine++;
453 *argc = 0;
454 getP = aLine;
455 putP = aLine;
456 while (*getP && !( !Quote && (braket == 0) && ((*getP) == '>'))){
457 if (*getP < ' ') *getP = ' '; /*remove all special chars*/
458 switch (*getP) {
459 case ' ':
460 if (Quote){
461 *(putP++)=*getP;
462 } else
463 if(inArg) {
464 *(putP++) = 0;
465 inArg = 0;
466 }
467 break;
468 case '"':
469 case '\'':
470 if (Quote != 0) {
471 if (Quote == *getP)
472 Quote = 0;
473 else {
474 *(putP++)=*getP;
475 }
476 } else {
477 if(!inArg){
478 (*args)[++(*argc)] = putP;
479 inArg=1;
480 }
481 Quote = *getP;
482 }
483 break;
484 default:
485 if (Quote == 0 && (*getP) == '<') {
486 braket++;
487 }
488 if (Quote == 0 && (*getP) == '>') {
489 braket--;
490 }
492 if(!inArg){
493 (*args)[++(*argc)] = putP;
494 inArg=1;
495 }
496 *(putP++)=*getP;
497 break;
498 }
499 if ((*argc) >= argal-10 ) {
500 argal += MEMBLK;
501 if (((*args)=rrd_realloc((*args),(argal)*sizeof(char *))) == NULL) {
502 return NULL;
503 }
504 }
505 getP++;
506 }
508 *putP = '\0';
509 (*argc)++;
510 if (Quote)
511 return NULL;
512 else
513 return getP+1; /* pointer to next char after parameter */
514 }
518 int parse(char **buf, long i, char *tag,
519 char *(*func)(long argc, char **args)){
521 /* the name of the vairable ... */
522 char *val;
523 long valln;
524 char **args;
525 char *end;
526 long end_offset;
527 long argc;
528 /* do we like it ? */
529 if (strncmp((*buf)+i, tag, strlen(tag))!=0) return 0;
530 if (! isspace(*( (*buf) + i + strlen(tag) )) ) return 0;
531 /* scanargs puts \0 into *buf ... so after scanargs it is probably
532 not a good time to use strlen on buf */
533 end = scanargs((*buf)+i+strlen(tag),&argc,&args);
534 if (! end) {
535 for (;argc>2;argc--){
536 *((args[argc-1])-1)=' ';
537 }
538 val = stralloc("[ERROR: Parsing Problem with the following text\n"
539 " Check original file. This may have been altered by parsing.]\n\n");
540 end = (*buf)+i+1;
541 } else {
542 val = func(argc-1,args+1);
543 free (args);
544 }
545 /* for (ii=0;ii<argc;ii++) printf("'%s'\n", args[ii]); */
546 if (val != NULL) {
547 valln = strlen(val);
548 } else { valln = 0;}
550 /* make enough room for replacement */
551 end_offset = end - (*buf);
552 if(end-(*buf) < i + valln){ /* make sure we do not shrink the mallocd block */
553 /* calculating the new length of the buffer is simple. add current
554 buffer pos (i) to length of string after replaced tag to length
555 of replacement string and add 1 for the final zero ... */
556 if(((*buf) = rrd_realloc((*buf),
557 (i+strlen(end) + valln +1) * sizeof(char)))==NULL){
558 perror("Realoc buf:");
559 exit(1);
560 };
561 }
562 end = (*buf) + end_offset; /* make sure the 'end' pointer gets moved
563 along with the buf pointer when realoc
564 moves memmory ... */
565 /* splice the variable */
566 memmove ((*buf)+i+valln,end,strlen(end)+1);
567 if (val != NULL ) memmove ((*buf)+i,val, valln);
568 if (val){ free(val);}
569 return valln > 0 ? valln-1: valln;
570 }
572 char *
573 http_time(time_t *now) {
574 struct tm *tmptime;
575 static char buf[60];
577 tmptime=gmtime(now);
578 strftime(buf,sizeof(buf),"%a, %d %b %Y %H:%M:%S GMT",tmptime);
579 return(buf);
580 }