"Fossies" - the Fresh Open Source Software Archive

Member "xymon-4.3.30/web/criticaleditor.c" (23 Jul 2019, 12870 Bytes) of package /linux/privat/xymon-4.3.30.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "criticaleditor.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.3.29_vs_4.3.30.

    1 /*----------------------------------------------------------------------------*/
    2 /* Xymon CGI for administering the critical.cfg file                          */
    3 /*                                                                            */
    4 /* Copyright (C) 2006-2011 Henrik Storner <henrik@storner.dk>                 */
    5 /*                                                                            */
    6 /* This program is released under the GNU General Public License (GPL),       */
    7 /* version 2. See the file "COPYING" for details.                             */
    8 /*                                                                            */
    9 /*----------------------------------------------------------------------------*/
   10 
   11 static char rcsid[] = "$Id: criticaleditor.c 8069 2019-07-23 15:29:06Z jccleaver $";
   12 
   13 #include <string.h>
   14 #include <stdlib.h>
   15 #include <unistd.h>
   16 #include <time.h>
   17 #include <limits.h>
   18 #include <ctype.h>
   19 #include <stdio.h>
   20 #include <fcntl.h>
   21 #include <sys/types.h>
   22 #include <sys/stat.h>
   23 
   24 #include "libxymon.h"
   25 
   26 static char *operator = NULL;
   27 
   28 static enum { CRITEDIT_FIND, CRITEDIT_NEXT, CRITEDIT_UPDATE, CRITEDIT_DELETE, CRITEDIT_ADDCLONE, CRITEDIT_DROPCLONE } editaction = CRITEDIT_FIND;
   29 static char *rq_hostname = NULL;
   30 static char *rq_service = NULL;
   31 static int rq_priority = 0;
   32 static char *rq_group = NULL;
   33 static char *rq_extra = NULL;
   34 STATIC_SBUF_DEFINE(rq_crittime);
   35 static time_t rq_start = 0;
   36 static time_t rq_end = 0;
   37 static char *rq_clonestoadd = NULL;
   38 static char *rq_clonestodrop = NULL;
   39 static int  rq_dropevenifcloned = 0;
   40 
   41 static void parse_query(void)
   42 {
   43     cgidata_t *cgidata = cgi_request();
   44     cgidata_t *cwalk;
   45     char *rq_critwkdays = NULL;
   46     char *rq_critslastart = NULL;
   47     char *rq_critslaend = NULL;
   48     int  rq_startday = 0;
   49     int  rq_startmon = 0;
   50     int  rq_startyear = 0;
   51     int  rq_endday = 0;
   52     int  rq_endmon = 0;
   53     int  rq_endyear = 0;
   54 
   55     cwalk = cgidata;
   56     while (cwalk) {
   57         if (strcasecmp(cwalk->name, "Find") == 0) {
   58             editaction = CRITEDIT_FIND;
   59         }
   60         else if (strcasecmp(cwalk->name, "Next") == 0) {
   61             editaction = CRITEDIT_NEXT;
   62         }
   63         else if (strcasecmp(cwalk->name, "Update") == 0) {
   64             editaction = CRITEDIT_UPDATE;
   65         }
   66         else if (strcasecmp(cwalk->name, "Drop") == 0) {
   67             editaction = CRITEDIT_DELETE;
   68         }
   69         else if (strcasecmp(cwalk->name, "Clone") == 0) {
   70             /* The "clone" button does both things */
   71             editaction = CRITEDIT_ADDCLONE;
   72         }
   73         else if (strcasecmp(cwalk->name, "HOSTNAME") == 0) {
   74             if (*cwalk->value) rq_hostname = strdup(cwalk->value);
   75         }
   76         else if (strcasecmp(cwalk->name, "SERVICE") == 0) {
   77             if (*cwalk->value) rq_service = strdup(cwalk->value);
   78         }
   79         else if (strcasecmp(cwalk->name, "PRIORITY") == 0) {
   80             rq_priority = atoi(cwalk->value);
   81         }
   82         else if (strcasecmp(cwalk->name, "GROUP") == 0) {
   83             if (*cwalk->value) rq_group = strdup(cwalk->value);
   84         }
   85         else if (strcasecmp(cwalk->name, "CRITWKDAYS") == 0) {
   86             if (*cwalk->value) {
   87                 if (!rq_critwkdays) rq_critwkdays = strdup(cwalk->value);
   88                 else {
   89                     rq_critwkdays = (char *)realloc(rq_critwkdays, strlen(rq_critwkdays) + strlen(cwalk->value) + 1);
   90                     strcat(rq_critwkdays, cwalk->value);
   91                 }
   92             }
   93         }
   94         else if (strcasecmp(cwalk->name, "CRITSTARTHOUR") == 0) {
   95             if (*cwalk->value) rq_critslastart = strdup(cwalk->value);
   96         }
   97         else if (strcasecmp(cwalk->name, "CRITENDHOUR") == 0) {
   98             if (*cwalk->value) rq_critslaend = strdup(cwalk->value);
   99         }
  100         else if (strcasecmp(cwalk->name, "start-day") == 0) {
  101             rq_startday = atoi(cwalk->value);
  102         }
  103         else if (strcasecmp(cwalk->name, "start-mon") == 0) {
  104             rq_startmon = atoi(cwalk->value);
  105         }
  106         else if (strcasecmp(cwalk->name, "start-yr") == 0) {
  107             rq_startyear = atoi(cwalk->value);
  108         }
  109         else if (strcasecmp(cwalk->name, "end-day") == 0) {
  110             rq_endday = atoi(cwalk->value);
  111         }
  112         else if (strcasecmp(cwalk->name, "end-mon") == 0) {
  113             rq_endmon = atoi(cwalk->value);
  114         }
  115         else if (strcasecmp(cwalk->name, "end-yr") == 0) {
  116             rq_endyear = atoi(cwalk->value);
  117         }
  118         else if (strcasecmp(cwalk->name, "EXTRA") == 0) {
  119             if (*cwalk->value) rq_extra = strdup(cwalk->value);
  120         }
  121         else if (strcasecmp(cwalk->name, "DROPEVENIFCLONED") == 0) {
  122             rq_dropevenifcloned = 1;
  123         }
  124         else if (strcasecmp(cwalk->name, "CRITEDITADDCLONES") == 0) {
  125             if (*cwalk->value) rq_clonestoadd = strdup(cwalk->value);
  126         }
  127         else if (strcasecmp(cwalk->name, "CRITEDITCLONELIST") == 0) {
  128             if (rq_clonestodrop) {
  129                 rq_clonestodrop = (char *)realloc(rq_clonestodrop, strlen(rq_clonestodrop) + strlen(cwalk->value) + 2);
  130                 strcat(rq_clonestodrop, " ");
  131                 strcat(rq_clonestodrop, cwalk->value);
  132             }
  133             else {
  134                 if (*cwalk->value) rq_clonestodrop = strdup(cwalk->value);
  135             }
  136         }
  137 
  138         cwalk = cwalk->next;
  139     }
  140 
  141     if (editaction == CRITEDIT_UPDATE) {
  142         struct tm tm;
  143 
  144         if ((rq_startday == 0) || (rq_startmon == 0) || (rq_startyear == 0))
  145             rq_start = -1;
  146         else {
  147             memset(&tm, 0, sizeof(tm));
  148             tm.tm_mday = rq_startday;
  149             tm.tm_mon = rq_startmon - 1;
  150             tm.tm_year = rq_startyear - 1900;
  151             tm.tm_isdst = -1;
  152             rq_start = mktime(&tm);
  153         }
  154 
  155         if ((rq_endday == 0) || (rq_endmon == 0) || (rq_endyear == 0))
  156             rq_end = -1;
  157         else {
  158             memset(&tm, 0, sizeof(tm));
  159             tm.tm_mday = rq_endday;
  160             tm.tm_mon = rq_endmon - 1;
  161             tm.tm_year = rq_endyear - 1900;
  162             tm.tm_isdst = -1;
  163             rq_end = mktime(&tm);
  164         }
  165 
  166         SBUF_MALLOC(rq_crittime,strlen(rq_critwkdays) + strlen(rq_critslastart) + strlen(rq_critslaend) + 3);
  167         snprintf(rq_crittime, rq_crittime_buflen, "%s:%s:%s", rq_critwkdays, rq_critslastart, rq_critslaend);
  168     }
  169     else if (editaction == CRITEDIT_ADDCLONE) {
  170         if (!rq_clonestoadd && rq_clonestodrop) editaction = CRITEDIT_DROPCLONE;
  171     }
  172 }
  173 
  174 void findrecord(char *hostname, char *service, char *nodatawarning, char *isclonewarning, char *hascloneswarning)
  175 {
  176     critconf_t *rec = NULL;
  177     int isaclone = 0;
  178     int hasclones = 0;
  179     char warnmsg[4096];
  180 
  181     /* Setup the list of cloned records */
  182     sethostenv_critclonelist_clear();
  183 
  184     if (hostname && *hostname) {
  185         SBUF_DEFINE(key);
  186         char *realkey, *clonekey;
  187         critconf_t *clonerec;
  188 
  189         if (service && *service) {
  190             /* First check if the host+service is really a clone of something else */
  191             SBUF_MALLOC(key, strlen(hostname) + strlen(service) + 2);
  192             snprintf(key, key_buflen, "%s|%s", hostname, service);
  193             rec = get_critconfig(key, CRITCONF_FIRSTMATCH, &realkey);
  194         }
  195         else {
  196             key = strdup(hostname);
  197             rec = get_critconfig(key, CRITCONF_FIRSTHOSTMATCH, &realkey);
  198         }
  199 
  200         if (rec && realkey && (strcmp(key, realkey) != 0)) {
  201             char *p;
  202 
  203             xfree(key);
  204             key = strdup(realkey);
  205             hostname = realkey;
  206             p = strchr(realkey, '|');
  207             if (p) {
  208                 *p = '\0';
  209                 service = p+1;
  210             }
  211 
  212             isaclone = 1;
  213         }
  214         xfree(key);
  215 
  216         /* Next, see what hosts are clones of this one */
  217         clonerec = get_critconfig(NULL, CRITCONF_RAW_FIRST, &clonekey);
  218         while (clonerec) {
  219             if ((*(clonekey + strlen(clonekey) -1) == '=') && (strcmp(hostname, (char *)clonerec) == 0)) {
  220                 sethostenv_critclonelist_add(clonekey);
  221                 hasclones = 1;
  222             }
  223             clonerec = get_critconfig(NULL, CRITCONF_RAW_NEXT, &clonekey);
  224         }
  225     }
  226     else {
  227         hostname = "";
  228     }
  229 
  230     if (!service || !(*service)) service="";
  231 
  232     if (rec) sethostenv_critedit(rec->updinfo, rec->priority, rec->ttgroup, rec->starttime, rec->endtime, rec->crittime, rec->ttextra);
  233     else sethostenv_critedit("", 0, NULL, 0, 0, NULL, NULL);
  234 
  235     sethostenv(hostname, "", service, colorname(COL_BLUE), NULL);
  236 
  237     *warnmsg = '\0';
  238     if (!rec && nodatawarning) snprintf(warnmsg, sizeof(warnmsg), "%s", nodatawarning);
  239     if (isaclone && isclonewarning) snprintf(warnmsg, sizeof(warnmsg), "%s", isclonewarning);
  240     if (hasclones && hascloneswarning) snprintf(warnmsg, sizeof(warnmsg), "%s", hascloneswarning);
  241 
  242     printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE"));
  243     showform(stdout, "critedit", "critedit_form", COL_BLUE, getcurrenttime(NULL), warnmsg, NULL);
  244 }
  245 
  246 
  247 void nextrecord(char *hostname, char *service, char *isclonewarning, char *hascloneswarning)
  248 {
  249     critconf_t *rec;
  250     char *nexthost, *nextservice;
  251 
  252     /* First check if the host+service is really a clone of something else */
  253     if (hostname && service) {
  254         SBUF_DEFINE(key);
  255 
  256         SBUF_MALLOC(key, strlen(hostname) + strlen(service) + 2);
  257         snprintf(key, key_buflen, "%s|%s", hostname, service);
  258         rec = get_critconfig(key, CRITCONF_FIRSTMATCH, NULL);
  259         if (rec) rec = get_critconfig(NULL, CRITCONF_NEXT, NULL);
  260         xfree(key);
  261     }
  262     else {
  263         rec = get_critconfig(NULL, CRITCONF_FIRST, NULL);
  264     }
  265 
  266     if (rec) {
  267         nexthost = strdup(rec->key);
  268         nextservice = strchr(nexthost, '|'); if (nextservice) { *nextservice = '\0'; nextservice++; }
  269     }
  270     else {
  271         nexthost = strdup("");
  272         nextservice = "";
  273     }
  274 
  275     findrecord(nexthost, nextservice, NULL, isclonewarning, hascloneswarning);
  276     xfree(nexthost);
  277 }
  278 
  279 void updaterecord(char *hostname, char *service)
  280 {
  281     critconf_t *rec = NULL;
  282 
  283     if (hostname && service) {
  284         SBUF_DEFINE(key);
  285         char *realkey;
  286         char datestr[20];
  287         time_t now = getcurrenttime(NULL);
  288         size_t upd_buflen;
  289 
  290         SBUF_MALLOC(key, strlen(hostname) + strlen(service) + 2);
  291         strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", localtime(&now));
  292         snprintf(key, key_buflen, "%s|%s", hostname, service);
  293         rec = get_critconfig(key, CRITCONF_FIRSTMATCH, &realkey);
  294         if (rec == NULL) {
  295             rec = (critconf_t *)calloc(1, sizeof(critconf_t));
  296             rec->key = strdup(key);
  297         }
  298         rec->priority = rq_priority;
  299         rec->starttime = (rq_start > 0) ? rq_start : 0;
  300         rec->endtime = (rq_end > 0) ? rq_end : 0;
  301 
  302         if (rec->crittime) {
  303             xfree(rec->crittime); rec->crittime = NULL;
  304         }
  305 
  306         if (rq_crittime) {
  307             rec->crittime = (strcmp(rq_crittime, "*:0000:2400") == 0) ? NULL : strdup(rq_crittime);
  308         }
  309 
  310         if (rec->ttgroup) xfree(rec->ttgroup); 
  311         rec->ttgroup = (rq_group ? strdup(rq_group) : NULL);
  312         if (rec->ttextra) xfree(rec->ttextra); 
  313         rec->ttextra = (rq_extra ? strdup(rq_extra) : NULL);
  314         if (rec->updinfo) xfree(rec->updinfo);
  315         upd_buflen = strlen(operator) + strlen(datestr) + 2;
  316         rec->updinfo = (char *)malloc(upd_buflen);
  317         snprintf(rec->updinfo, upd_buflen, "%s %s", operator, datestr);
  318 
  319         update_critconfig(rec);
  320         xfree(key);
  321     }
  322 
  323     findrecord(hostname, service, NULL, NULL, NULL);
  324 }
  325 
  326 void addclone(char *origin, char *newhosts, char *service)
  327 {
  328     char *newclone;
  329 
  330     newclone = strtok(newhosts, " ");
  331     while (newclone) {
  332         addclone_critconfig(origin, newclone);
  333         newclone = strtok(NULL, " ");
  334     }
  335 
  336     update_critconfig(NULL);
  337     findrecord(origin, service, NULL, NULL, NULL);
  338 }
  339 
  340 void dropclone(char *origin, char *drops, char *service)
  341 {
  342     char *drop;
  343 
  344     drop = strtok(drops, " ");
  345     while (drop) {
  346         dropclone_critconfig(drop);
  347         drop = strtok(NULL, " ");
  348     }
  349 
  350     update_critconfig(NULL);
  351     findrecord(origin, service, NULL, NULL, NULL);
  352 }
  353 
  354 void deleterecord(char *hostname, char *service, int evenifcloned)
  355 {
  356     SBUF_DEFINE(key);
  357 
  358     SBUF_MALLOC(key, strlen(hostname) + strlen(service) + 2);
  359     snprintf(key, key_buflen, "%s|%s", hostname, service);
  360     if (delete_critconfig(key, evenifcloned) == 0) {
  361         update_critconfig(NULL);
  362     }
  363 
  364     findrecord(hostname, service, NULL, NULL, 
  365            (evenifcloned ? "Warning: Orphans will be ignored" : "Will not delete record that is cloned"));
  366 }
  367 
  368 int main(int argc, char *argv[])
  369 {
  370     int argi;
  371     char *envarea = NULL;
  372     char *configfn = NULL;
  373 
  374     operator = getenv("REMOTE_USER");
  375     if (!operator) operator = "Anonymous";
  376 
  377     for (argi = 1; (argi < argc); argi++) {
  378         if (argnmatch(argv[argi], "--env=")) {
  379             char *p = strchr(argv[argi], '=');
  380             loadenv(p+1, envarea);
  381         }
  382         else if (argnmatch(argv[argi], "--area=")) {
  383             char *p = strchr(argv[argi], '=');
  384             envarea = strdup(p+1);
  385         }
  386         else if (argnmatch(argv[argi], "--config=")) {
  387             char *p = strchr(argv[argi], '=');
  388             configfn = strdup(p+1);
  389         }
  390         else if (strcmp(argv[argi], "--debug") == 0) {
  391             debug = 1;
  392         }
  393     }
  394 
  395     redirect_cgilog("criticaleditor");
  396 
  397         /* We only want to accept posts from certain pages */
  398     if (cgi_ispost()) {
  399         char cgisource[1024]; char *p;
  400         p = csp_header("criticaleditor"); if (p) fprintf(stdout, "%s", p);
  401         snprintf(cgisource, sizeof(cgisource), "%s/%s", xgetenv("SECURECGIBINURL"), "criticaleditor");
  402         if (!cgi_refererok(cgisource)) {
  403             fprintf(stdout, "Location: %s.sh?\n\n", cgisource);
  404             return 0;
  405         }
  406     }
  407 
  408     parse_query();
  409     load_critconfig(configfn);
  410 
  411     switch (editaction) {
  412       case CRITEDIT_FIND:
  413         findrecord(rq_hostname, rq_service, 
  414                ((rq_hostname && rq_service) ? "No record for this host/service" : NULL),
  415                "Cloned - showing master record", NULL);
  416         break;
  417 
  418       case CRITEDIT_NEXT:
  419         nextrecord(rq_hostname, rq_service, "Cloned - showing master record", NULL);
  420         break;
  421 
  422       case CRITEDIT_UPDATE:
  423         updaterecord(rq_hostname, rq_service);
  424         break;
  425 
  426       case CRITEDIT_DELETE:
  427         deleterecord(rq_hostname, rq_service, rq_dropevenifcloned);
  428         break;
  429 
  430       case CRITEDIT_ADDCLONE:
  431         addclone(rq_hostname, rq_clonestoadd, rq_service);
  432         break;
  433 
  434       case CRITEDIT_DROPCLONE:
  435         dropclone(rq_hostname, rq_clonestodrop, rq_service);
  436         break;
  437     }
  438 
  439     return 0;
  440 }
  441