1 /*****************************************************************************
2 * RRDtool 1.0.33 Copyright Tobias Oetiker, 1997 - 2000
3 *****************************************************************************
4 * rrd_cgi.c RRD Web Page Generator
5 *****************************************************************************/
14 /* global variable for libcgi */
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){
70 for(i=0;calcpr[i];i++){
81 /* create freeable version of the string */
82 char * stralloc(char *str){
83 char *nstr = malloc((strlen(str)+1)*sizeof(char));
88 int main(int argc, char *argv[]) {
91 char *server_url = NULL;
94 #ifdef MUST_DISABLE_SIGFPE
95 signal(SIGFPE,SIG_IGN);
97 #ifdef MUST_DISABLE_FPMASK
100 /* what do we get for cmdline arguments?
102 printf("%d-'%s'\n",i,argv[i]); */
104 static struct option long_options[] =
106 {"filter", no_argument, 0, 'f'},
109 int option_index = 0;
111 opt = getopt_long(argc, argv, "f",
112 long_options, &option_index);
120 printf("unknown commandline option '%s'\n",argv[optind-1]);
128 server_url = getenv("SERVER_URL");
131 if (optind != argc-1) {
132 fprintf(stderr, "ERROR: expected a filename\n");
135 length = readfile(argv[optind], &buffer, 1);
138 if(rrd_test_error()){
139 fprintf(stderr, "ERROR: %s\n",rrd_get_error());
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);
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);
164 for (i=0;buffer[i] != '\0'; i++){
165 i += parse(&buffer,i,"<RRD::GRAPH",drawgraph);
166 i += parse(&buffer,i,"<RRD::PRINT",drawprint);
170 printf ("Content-Type: text/html\n"
171 "Content-Length: %d\n", strlen(buffer));
172 if (labs(goodfor) > 0){
175 printf ("Last-Modified: %s\n",http_time(&now));
176 now += labs(goodfor);
177 printf ("Expires: %s\n",http_time(&now));
179 printf("Refresh: %ld\n", labs(goodfor));
184 printf ("%s", buffer);
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){
197 char *xyz=malloc((strlen(args[0])+strlen(args[1])+3)*sizeof(char));
199 return stralloc("[ERROR: allocating setenv buffer]");
201 sprintf(xyz,"%s=%s",args[0],args[1]);
202 if( putenv(xyz) == -1) {
203 return stralloc("[ERROR: faild to do putenv]");
206 return stralloc("[ERROR: setenv faild because not enough arguments were defined]");
211 char* rrdgetenv(long argc, char **args){
213 return stralloc("[ERROR: getenv faild because it did not get 1 argument only]");
215 else if (getenv(args[0]) == NULL) {
219 return stralloc(getenv(args[0]));
223 char* rrdgoodfor(long argc, char **args){
225 goodfor = atol(args[0]);
227 return stralloc("[ERROR: goodfor expected 1 argument]");
231 return stralloc("[ERROR: goodfor value must not be 0]");
237 char* includefile(long argc, char **args){
240 readfile(args[0], &buffer, 0);
241 if (rrd_test_error()) {
242 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
243 sprintf(err, "[ERROR: %s]",rrd_get_error());
252 return stralloc("[ERROR: No Inclue file defined]");
256 char* rrdstrip(char *buf){
258 if (buf == NULL) return NULL;
259 buf = stralloc(buf); /* make a copy of the buffer */
260 if (buf == NULL) return NULL;
261 while ((start = strstr(buf,"<"))){
264 while ((start = strstr(buf,">"))){
270 char* cgigetq(long argc, char **args){
272 char *buf = rrdstrip(cgiGetValue(cgiArg,args[0]));
276 if (buf==NULL) return NULL;
278 for(c=buf;*c != '\0';c++)
280 if((buf2=malloc((strlen(buf) + qc*4 +4) * sizeof(char)))==NULL){
281 perror("Malloc Buffer");
302 return stralloc("[ERROR: not enough argument for RRD::CV::QUOTE]");
305 /* remove occurences of .. this is a general measure to make
306 paths which came in via cgi do not go UP ... */
308 char* cgigetqp(long argc, char **args){
310 char *buf = rrdstrip(cgiGetValue(cgiArg,args[0]));
314 if (buf==NULL) return NULL;
316 for(c=buf;*c != '\0';c++)
318 if((buf2=malloc((strlen(buf) + qc*4 +4) * sizeof(char)))==NULL){
319 perror("Malloc Buffer");
335 if (*c=='.' && *(c+1) == '.'){
337 *(d++) = '_'; *(d++) ='_';
350 return stralloc("[ERROR: not enough arguments for RRD::CV::PATH]");
355 char* cgiget(long argc, char **args){
357 return rrdstrip(cgiGetValue(cgiArg,args[0]));
359 return stralloc("[ERROR: not enough arguments for RRD::CV]");
364 char* drawgraph(long argc, char **args){
367 if(strcmp(args[i],"--imginfo")==0 || strcmp(args[i],"-g")==0) break;
369 args[argc++] = "--imginfo";
370 args[argc++] = "<IMG SRC=\"./%s\" WIDTH=\"%lu\" HEIGHT=\"%lu\">";
372 optind=0; /* reset gnu getopt */
373 opterr=0; /* reset gnu getopt */
375 if( rrd_graph(argc+1, args-1, &calcpr, &xsize, &ysize) != -1 ) {
376 return stralloc(calcpr[0]);
378 if (rrd_test_error()) {
379 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
380 sprintf(err, "[ERROR: %s]",rrd_get_error());
389 char* drawprint(long argc, char **args){
390 if (argc==1 && calcpr){
392 while (calcpr[i] != NULL) i++; /*determine number lines in calcpr*/
393 if (atol(args[0])<i-1)
394 return stralloc(calcpr[atol(args[0])+1]);
396 return stralloc("[ERROR: RRD::PRINT argument error]");
399 char* printtimelast(long argc, char **args) {
406 return stralloc("[ERROR: allocating strftime buffer]");
408 last = rrd_last(argc+1, args-1);
409 if (rrd_test_error()) {
410 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
411 sprintf(err, "[ERROR: %s]",rrd_get_error());
415 tm_last = *localtime(&last);
416 strftime(buf,254,args[1],&tm_last);
420 return stralloc("[ERROR: too few arguments for RRD::TIME::LAST]");
422 return stralloc("[ERROR: not enough arguments for RRD::TIME::LAST]");
425 char* printtimenow(long argc, char **args) {
426 time_t now = time(NULL);
432 return stralloc("[ERROR: allocating strftime buffer]");
434 tm_now = *localtime(&now);
435 strftime(buf,254,args[0],&tm_now);
439 return stralloc("[ERROR: too few arguments for RRD::TIME::NOW]");
441 return stralloc("[ERROR: not enough arguments for RRD::TIME::NOW]");
444 /* scan aLine until an unescaped '>' arives */
445 char* scanargs(char *aLine, long *argc, char ***args)
452 if (((*args) = (char **) malloc(MEMBLK*sizeof(char *))) == NULL) {
455 /* sikp leading blanks */
456 while (*aLine && *aLine <= ' ') aLine++;
461 while (*getP && !( !Quote && (braket == 0) && ((*getP) == '>'))){
462 if (*getP < ' ') *getP = ' '; /*remove all special chars*/
483 (*args)[++(*argc)] = putP;
490 if (Quote == 0 && (*getP) == '<') {
493 if (Quote == 0 && (*getP) == '>') {
498 (*args)[++(*argc)] = putP;
504 if ((*argc) >= argal-10 ) {
506 if (((*args)=rrd_realloc((*args),(argal)*sizeof(char *))) == NULL) {
518 return getP+1; /* pointer to next char after parameter */
523 int parse(char **buf, long i, char *tag,
524 char *(*func)(long argc, char **args)){
526 /* the name of the vairable ... */
533 /* do we like it ? */
534 if (strncmp((*buf)+i, tag, strlen(tag))!=0) return 0;
535 if (! isspace(*( (*buf) + i + strlen(tag) )) ) return 0;
536 /* scanargs puts \0 into *buf ... so after scanargs it is probably
537 not a good time to use strlen on buf */
538 end = scanargs((*buf)+i+strlen(tag),&argc,&args);
540 for (;argc>2;argc--){
541 *((args[argc-1])-1)=' ';
543 val = stralloc("[ERROR: Parsing Problem with the following text\n"
544 " Check original file. This may have been altered by parsing.]\n\n");
547 val = func(argc-1,args+1);
550 /* for (ii=0;ii<argc;ii++) printf("'%s'\n", args[ii]); */
555 /* make enough room for replacement */
556 end_offset = end - (*buf);
557 if(end-(*buf) < i + valln){ /* make sure we do not shrink the mallocd block */
558 /* calculating the new length of the buffer is simple. add current
559 buffer pos (i) to length of string after replaced tag to length
560 of replacement string and add 1 for the final zero ... */
561 if(((*buf) = rrd_realloc((*buf),
562 (i+strlen(end) + valln +1) * sizeof(char)))==NULL){
563 perror("Realoc buf:");
567 end = (*buf) + end_offset; /* make sure the 'end' pointer gets moved
568 along with the buf pointer when realoc
570 /* splice the variable */
571 memmove ((*buf)+i+valln,end,strlen(end)+1);
572 if (val != NULL ) memmove ((*buf)+i,val, valln);
573 if (val){ free(val);}
574 return valln > 0 ? valln-1: valln;
578 http_time(time_t *now) {
583 strftime(buf,sizeof(buf),"%a, %d %b %Y %H:%M:%S GMT",tmptime);