1 /*****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
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 /* format at-time specified times using strftime */
60 char* printstrftime(long, char**);
62 /** http protocol needs special format, and GMT time **/
63 char *http_time(time_t *);
65 /* return a pointer to newly alocated copy of this string */
66 char *stralloc(char *);
68 static long goodfor=0;
69 static char **calcpr=NULL;
70 static void calfree (void){
73 for(i=0;calcpr[i];i++){
84 /* create freeable version of the string */
85 char * stralloc(char *str){
86 char *nstr = malloc((strlen(str)+1)*sizeof(char));
91 int main(int argc, char *argv[]) {
94 char *server_url = NULL;
97 #ifdef MUST_DISABLE_SIGFPE
98 signal(SIGFPE,SIG_IGN);
100 #ifdef MUST_DISABLE_FPMASK
103 /* what do we get for cmdline arguments?
105 printf("%d-'%s'\n",i,argv[i]); */
107 static struct option long_options[] =
109 {"filter", no_argument, 0, 'f'},
112 int option_index = 0;
114 opt = getopt_long(argc, argv, "f",
115 long_options, &option_index);
123 printf("unknown commandline option '%s'\n",argv[optind-1]);
131 server_url = getenv("SERVER_URL");
134 if ( (optind != argc-2 && strstr(getenv("SERVER_SOFTWARE"),"Apache/2") != NULL) && optind != argc-1) {
135 fprintf(stderr, "ERROR: expected a filename\n");
138 length = readfile(argv[optind], &buffer, 1);
141 if(rrd_test_error()){
142 fprintf(stderr, "ERROR: %s\n",rrd_get_error());
148 /* pass 1 makes only sense in cgi mode */
149 for (i=0;buffer[i] != '\0'; i++){
150 i +=parse(&buffer,i,"<RRD::CV",cgiget);
151 i +=parse(&buffer,i,"<RRD::CV::QUOTE",cgigetq);
152 i +=parse(&buffer,i,"<RRD::CV::PATH",cgigetqp);
153 i +=parse(&buffer,i,"<RRD::GETENV",rrdgetenv);
158 for (i=0;buffer[i] != '\0'; i++){
159 i += parse(&buffer,i,"<RRD::GOODFOR",rrdgoodfor);
160 i += parse(&buffer,i,"<RRD::SETENV",rrdsetenv);
161 i += parse(&buffer,i,"<RRD::INCLUDE",includefile);
162 i += parse(&buffer,i,"<RRD::TIME::LAST",printtimelast);
163 i += parse(&buffer,i,"<RRD::TIME::NOW",printtimenow);
164 i += parse(&buffer,i,"<RRD::TIME::STRFTIME",printstrftime);
168 for (i=0;buffer[i] != '\0'; i++){
169 i += parse(&buffer,i,"<RRD::GRAPH",drawgraph);
170 i += parse(&buffer,i,"<RRD::PRINT",drawprint);
174 printf ("Content-Type: text/html\n"
175 "Content-Length: %d\n", strlen(buffer));
176 if (labs(goodfor) > 0){
179 printf ("Last-Modified: %s\n",http_time(&now));
180 now += labs(goodfor);
181 printf ("Expires: %s\n",http_time(&now));
183 printf("Refresh: %ld\n", labs(goodfor));
188 printf ("%s", buffer);
196 /* remove occurences of .. this is a general measure to make
197 paths which came in via cgi do not go UP ... */
199 char* rrdsetenv(long argc, char **args){
201 char *xyz=malloc((strlen(args[0])+strlen(args[1])+3)*sizeof(char));
203 return stralloc("[ERROR: allocating setenv buffer]");
205 sprintf(xyz,"%s=%s",args[0],args[1]);
206 if( putenv(xyz) == -1) {
207 return stralloc("[ERROR: faild to do putenv]");
210 return stralloc("[ERROR: setenv faild because not enough arguments were defined]");
215 char* rrdgetenv(long argc, char **args){
217 return stralloc("[ERROR: getenv faild because it did not get 1 argument only]");
219 else if (getenv(args[0]) == NULL) {
223 return stralloc(getenv(args[0]));
227 char* rrdgoodfor(long argc, char **args){
229 goodfor = atol(args[0]);
231 return stralloc("[ERROR: goodfor expected 1 argument]");
235 return stralloc("[ERROR: goodfor value must not be 0]");
241 /* Format start or end times using strftime. We always need both the
242 * start and end times, because, either might be relative to the other.
244 #define MAX_STRFTIME_SIZE 256
245 char* printstrftime(long argc, char **args){
246 struct time_value start_tv, end_tv;
247 char *parsetime_error = NULL;
248 char formatted[MAX_STRFTIME_SIZE];
250 time_t start_tmp, end_tmp;
252 /* Make sure that we were given the right number of args */
254 rrd_set_error( "wrong number of args %d", argc);
258 /* Init start and end time */
259 parsetime("end-24h", &start_tv);
260 parsetime("now", &end_tv);
262 /* Parse the start and end times we were given */
263 if( (parsetime_error = parsetime( args[1], &start_tv))) {
264 rrd_set_error( "start time: %s", parsetime_error);
267 if( (parsetime_error = parsetime( args[2], &end_tv))) {
268 rrd_set_error( "end time: %s", parsetime_error);
271 if( proc_start_end( &start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
275 /* Do we do the start or end */
276 if( strcasecmp( args[0], "START") == 0) {
277 the_tm = localtime( &start_tmp);
279 else if( strcasecmp( args[0], "END") == 0) {
280 the_tm = localtime( &end_tmp);
283 rrd_set_error( "start/end not found in '%s'", args[0]);
288 if( strftime( formatted, MAX_STRFTIME_SIZE, args[3], the_tm)) {
289 return( stralloc( formatted));
292 rrd_set_error( "strftime failed");
297 char* includefile(long argc, char **args){
300 readfile(args[0], &buffer, 0);
301 if (rrd_test_error()) {
302 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
303 sprintf(err, "[ERROR: %s]",rrd_get_error());
312 return stralloc("[ERROR: No Inclue file defined]");
317 char* rrdstrip(char *buf){
319 if (buf == NULL) return NULL;
320 buf = stralloc(buf); /* make a copy of the buffer */
321 if (buf == NULL) return NULL;
322 while ((start = strstr(buf,"<"))){
325 while ((start = strstr(buf,">"))){
331 char* cgigetq(long argc, char **args){
333 char *buf = rrdstrip(cgiGetValue(cgiArg,args[0]));
337 if (buf==NULL) return NULL;
339 for(c=buf;*c != '\0';c++)
341 if((buf2=malloc((strlen(buf) + qc*4 +4) * sizeof(char)))==NULL){
342 perror("Malloc Buffer");
363 return stralloc("[ERROR: not enough argument for RRD::CV::QUOTE]");
366 /* remove occurences of .. this is a general measure to make
367 paths which came in via cgi do not go UP ... */
369 char* cgigetqp(long argc, char **args){
371 char *buf = rrdstrip(cgiGetValue(cgiArg,args[0]));
375 if (buf==NULL) return NULL;
377 for(c=buf;*c != '\0';c++)
379 if((buf2=malloc((strlen(buf) + qc*4 +4) * sizeof(char)))==NULL){
380 perror("Malloc Buffer");
396 if (*c=='.' && *(c+1) == '.'){
398 *(d++) = '_'; *(d++) ='_';
411 return stralloc("[ERROR: not enough arguments for RRD::CV::PATH]");
416 char* cgiget(long argc, char **args){
418 return rrdstrip(cgiGetValue(cgiArg,args[0]));
420 return stralloc("[ERROR: not enough arguments for RRD::CV]");
425 char* drawgraph(long argc, char **args){
428 if(strcmp(args[i],"--imginfo")==0 || strcmp(args[i],"-g")==0) break;
430 args[argc++] = "--imginfo";
431 args[argc++] = "<IMG SRC=\"./%s\" WIDTH=\"%lu\" HEIGHT=\"%lu\">";
433 optind=0; /* reset gnu getopt */
434 opterr=0; /* reset gnu getopt */
436 if( rrd_graph(argc+1, args-1, &calcpr, &xsize, &ysize) != -1 ) {
437 return stralloc(calcpr[0]);
439 if (rrd_test_error()) {
440 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
441 sprintf(err, "[ERROR: %s]",rrd_get_error());
450 char* drawprint(long argc, char **args){
451 if (argc==1 && calcpr){
453 while (calcpr[i] != NULL) i++; /*determine number lines in calcpr*/
454 if (atol(args[0])<i-1)
455 return stralloc(calcpr[atol(args[0])+1]);
457 return stralloc("[ERROR: RRD::PRINT argument error]");
460 char* printtimelast(long argc, char **args) {
467 return stralloc("[ERROR: allocating strftime buffer]");
469 last = rrd_last(argc+1, args-1);
470 if (rrd_test_error()) {
471 char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
472 sprintf(err, "[ERROR: %s]",rrd_get_error());
476 localtime_r(&last, &tm_last);
477 strftime(buf,254,args[1],&tm_last);
481 return stralloc("[ERROR: too few arguments for RRD::TIME::LAST]");
483 return stralloc("[ERROR: not enough arguments for RRD::TIME::LAST]");
486 char* printtimenow(long argc, char **args) {
487 time_t now = time(NULL);
493 return stralloc("[ERROR: allocating strftime buffer]");
495 localtime_r(&now, &tm_now);
496 strftime(buf,254,args[0],&tm_now);
500 return stralloc("[ERROR: too few arguments for RRD::TIME::NOW]");
502 return stralloc("[ERROR: not enough arguments for RRD::TIME::NOW]");
505 /* scan aLine until an unescaped '>' arives */
507 char* scanargs(char *aLine, long *argc, char ***args)
514 if (((*args) = (char **) malloc(MEMBLK*sizeof(char *))) == NULL) {
517 /* sikp leading blanks */
518 while (*aLine && *aLine <= ' ') aLine++;
523 while (*getP && !( !Quote && (braket == 0) && ((*getP) == '>'))){
524 if ((unsigned)*getP < ' ') *getP = ' '; /*remove all special chars*/
545 (*args)[++(*argc)] = putP;
552 if (Quote == 0 && (*getP) == '<') {
555 if (Quote == 0 && (*getP) == '>') {
560 (*args)[++(*argc)] = putP;
566 if ((*argc) >= argal-10 ) {
568 if (((*args)=rrd_realloc((*args),(argal)*sizeof(char *))) == NULL) {
580 return getP+1; /* pointer to next char after parameter */
585 int parse(char **buf, long i, char *tag,
586 char *(*func)(long argc, char **args)){
588 /* the name of the vairable ... */
595 /* do we like it ? */
596 if (strncmp((*buf)+i, tag, strlen(tag))!=0) return 0;
597 if (! isspace(*( (*buf) + i + strlen(tag) )) ) return 0;
598 /* scanargs puts \0 into *buf ... so after scanargs it is probably
599 not a good time to use strlen on buf */
600 end = scanargs((*buf)+i+strlen(tag),&argc,&args);
602 for (;argc>2;argc--){
603 *((args[argc-1])-1)=' ';
605 val = stralloc("[ERROR: Parsing Problem with the following text\n"
606 " Check original file. This may have been altered by parsing.]\n\n");
609 val = func(argc-1,args+1);
612 /* for (ii=0;ii<argc;ii++) printf("'%s'\n", args[ii]); */
617 /* make enough room for replacement */
618 end_offset = end - (*buf);
619 if(end-(*buf) < i + valln){ /* make sure we do not shrink the mallocd block */
620 /* calculating the new length of the buffer is simple. add current
621 buffer pos (i) to length of string after replaced tag to length
622 of replacement string and add 1 for the final zero ... */
623 if(((*buf) = rrd_realloc((*buf),
624 (i+strlen(end) + valln +1) * sizeof(char)))==NULL){
625 perror("Realoc buf:");
629 end = (*buf) + end_offset; /* make sure the 'end' pointer gets moved
630 along with the buf pointer when realoc
632 /* splice the variable */
633 memmove ((*buf)+i+valln,end,strlen(end)+1);
634 if (val != NULL ) memmove ((*buf)+i,val, valln);
635 if (val){ free(val);}
636 return valln > 0 ? valln-1: valln;
640 http_time(time_t *now) {
644 gmtime_r(now, &tmptime);
645 strftime(buf,sizeof(buf),"%a, %d %b %Y %H:%M:%S GMT", &tmptime);