"Fossies" - the Fresh Open Source Software Archive

Member "xymon-4.3.30/lib/environ.c" (23 Jul 2019, 13848 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 "environ.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 monitor library.                                                     */
    3 /*                                                                            */
    4 /* This is a library module, part of libxymon.                                */
    5 /* It contains environment variable handling routines.                        */
    6 /*                                                                            */
    7 /* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk>                 */
    8 /*                                                                            */
    9 /* This program is released under the GNU General Public License (GPL),       */
   10 /* version 2. See the file "COPYING" for details.                             */
   11 /*                                                                            */
   12 /*----------------------------------------------------------------------------*/
   13 
   14 static char rcsid[] = "$Id: environ.c 8069 2019-07-23 15:29:06Z jccleaver $";
   15 
   16 #include <ctype.h>
   17 #include <string.h>
   18 #include <stdlib.h>
   19 #include <errno.h>
   20 
   21 #include "libxymon.h"
   22 
   23 const static struct {
   24     char *name;
   25     char *val;
   26 } xymonenv[] = {
   27     { "XYMONDREL", VERSION },
   28     { "XYMONSERVERROOT", XYMONTOPDIR },
   29     { "XYMONSERVERLOGS", XYMONLOGDIR },
   30     { "XYMONSERVERHOSTNAME", XYMONHOSTNAME },
   31     { "XYMONSERVERIP", XYMONHOSTIP },
   32     { "XYMONSERVEROS", XYMONHOSTOS },
   33     { "XYMONSERVERWWWNAME", XYMONHOSTNAME },
   34     { "XYMONSERVERWWWURL", "/xymon" },
   35     { "XYMONSERVERCGIURL", "/xymon-cgi" },
   36     { "XYMONSERVERSECURECGIURL", "/xymon-seccgi" },
   37     { "XYMONNETWORK", "" },
   38     { "BBLOCATION", "" },
   39     { "PATH", "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:"XYMONHOME"/bin" },
   40     { "DELAYRED", "" },
   41     { "DELAYYELLOW", "" },
   42     { "XYMONDPORT", "1984" },
   43     { "XYMSRV", "$XYMONSERVERIP" },
   44     { "XYMSERVERS", "" },
   45     { "FQDN", "TRUE" },
   46     { "PAGELEVELS", "red yellow purple" },
   47     { "PURPLEDELAY", "30" },
   48     { "XYMONLOGSTATUS", "DYNAMIC" },
   49     { "PINGCOLUMN", "conn" },
   50     { "INFOCOLUMN", "info" },
   51     { "TRENDSCOLUMN", "trends" },
   52     { "CLIENTCOLUMN", "clientlog" },
   53     { "DOCOMBO", "TRUE" },
   54     { "MAXMSGSPERCOMBO", "100" },
   55     { "SLEEPBETWEENMSGS", "0" },
   56     { "SERVEROSTYPE", "$XYMONSERVEROS" },
   57     { "MACHINEDOTS", "$XYMONSERVERHOSTNAME" },
   58     { "MACHINEADDR", "$XYMONSERVERIP" },
   59     { "XYMONWEBHOST", "http://$XYMONSERVERWWWNAME" },
   60     { "XYMONWEBHOSTURL", "$XYMONWEBHOST$XYMONSERVERWWWURL" },
   61     { "XYMONWEBHTMLLOGS", "$XYMONWEBHOSTURL/html"    },
   62     { "XYMONWEB", "$XYMONSERVERWWWURL" },
   63     { "XYMONSKIN", "$XYMONSERVERWWWURL/gifs" },
   64     { "XYMONHELPSKIN", "$XYMONSERVERWWWURL/help" },
   65     { "XYMONNOTESSKIN", "$XYMONSERVERWWWURL/notes" },
   66     { "XYMONMENUSKIN", "$XYMONSERVERWWWURL/menu" },
   67     { "XYMONREPURL", "$XYMONSERVERWWWURL/rep" },
   68     { "XYMONSNAPURL", "$XYMONSERVERWWWURL/snap" },
   69     { "XYMONWAP", "$XYMONSERVERWWWURL/wml" },
   70     { "CGIBINURL", "$XYMONSERVERCGIURL" },
   71     { "XYMONHOME", XYMONHOME },
   72     { "XYMONTMP", "$XYMONHOME/tmp" },
   73     { "HOSTSCFG", "$XYMONHOME/etc/hosts.cfg" },
   74     { "XYMON", "$XYMONHOME/bin/xymon" },
   75     { "XYMONGEN", "$XYMONHOME/bin/xymongen" },
   76     { "XYMONVAR", "$XYMONSERVERROOT/data" },
   77     { "XYMONACKDIR", "$XYMONVAR/acks" },
   78     { "XYMONDATADIR", "$XYMONVAR/data" },
   79     { "XYMONDISABLEDDIR", "$XYMONVAR/disabled" },
   80     { "XYMONHISTDIR", "$XYMONVAR/hist" },
   81     { "XYMONHISTLOGS", "$XYMONVAR/histlogs" },
   82     { "XYMONRAWSTATUSDIR", "$XYMONVAR/logs" },
   83     { "XYMONWWWDIR", "$XYMONHOME/www" },
   84     { "XYMONHTMLSTATUSDIR", "$XYMONWWWDIR/html" },
   85     { "XYMONNOTESDIR", "$XYMONWWWDIR/notes" },
   86     { "XYMONREPDIR", "$XYMONWWWDIR/rep" },
   87     { "XYMONSNAPDIR", "$XYMONWWWDIR/snap" },
   88     { "XYMONALLHISTLOG", "TRUE" },
   89     { "XYMONHOSTHISTLOG", "TRUE" },
   90     { "SAVESTATUSLOG", "TRUE" },
   91     { "CLIENTLOGS", "$XYMONVAR/hostdata" },
   92     { "DU", "du -k" },
   93     { "MAILC", "mail" },
   94     { "MAIL", "$MAILC -s" },
   95     { "SVCCODES", "disk:100,cpu:200,procs:300,svcs:350,msgs:400,conn:500,http:600,dns:800,smtp:725,telnet:723,ftp:721,pop:810,pop3:810,pop-3:810,ssh:722,imap:843,ssh1:722,ssh2:722,imap2:843,imap3:843,imap4:843,pop2:809,pop-2:809,nntp:819,test:901" },
   96     { "ALERTCOLORS", "red,yellow,purple" },
   97     { "OKCOLORS", "green,blue,clear" },
   98     { "ALERTREPEAT", "30" },
   99     { "XYMWEBREFRESH", "60" },
  100     { "MAXMSG_ALERTSCRIPT", "8164" },
  101     { "CONNTEST", "TRUE" },
  102     { "IPTEST_2_CLEAR_ON_FAILED_CONN", "TRUE" },
  103     { "NONETPAGE", "" },
  104     { "FPING", "xymonping" },
  105     { "FPINGOPTS", "-Ae" },
  106     { "SNTP", "sntp" },
  107     { "SNTPOPTS", "-u" },
  108     { "NTPDATE", "ntpdate" },
  109     { "NTPDATEOPTS", "-u -q -p 1" },
  110     { "TRACEROUTE", "traceroute" },
  111     { "TRACEROUTEOPTS", "-n -q 2 -w 2 -m 15" },
  112     { "RPCINFO", "rpcinfo" },
  113     { "XYMONROUTERTEXT", "router" },
  114     { "NETFAILTEXT", "not OK" },
  115     { "XYMONRRDS", "$XYMONVAR/rrd" },
  116     { "TEST2RRD", "cpu=la,disk,memory,$PINGCOLUMN=tcp,http=tcp,dns=tcp,dig=tcp,time=ntpstat,vmstat,iostat,netstat,temperature,apache,bind,sendmail,nmailq,socks,bea,iishealth,citrix,xymongen,xymonnet,xymonproxy,xymond" },
  117     { "GRAPHS", "la,disk:disk_part:5,memory,users,vmstat,iostat,tcp.http,tcp,netstat,temperature,ntpstat,apache,bind,sendmail,nmailq,socks,bea,iishealth,citrix,xymongen,xymonnet,xymonproxy,xymond" },
  118     { "SUMMARY_SET_BKG", "FALSE" },
  119     { "XYMONNONGREENEXT", "eventlog.sh acklog.sh" },
  120     { "DOTHEIGHT", "16" },
  121     { "DOTWIDTH", "16" },
  122     { "IMAGEFILETYPE", "gif" },
  123     { "RRDHEIGHT", "120" },
  124     { "RRDWIDTH", "576" },
  125     { "COLUMNDOCURL", "$CGIBINURL/columndoc.sh?%s" },
  126     { "HOSTDOCURL", "" },
  127     { "XYMONLOGO", "Xymon" },
  128     { "XYMONPAGELOCAL", "<B><I>Pages Hosted Locally</I></B>" },
  129     { "XYMONPAGEREMOTE", "<B><I>Remote Status Display</I></B>" },
  130     { "XYMONPAGESUBLOCAL", "<B><I>Subpages Hosted Locally</I></B>" },
  131     { "XYMONPAGEACKFONT", "COLOR=\"#33ebf4\" SIZE=\"-1\"" },
  132     { "XYMONPAGECOLFONT", "COLOR=\"#87a9e5\" SIZE=\"-1\"" },
  133     { "XYMONPAGEROWFONT", "SIZE=\"+1\" COLOR=\"#FFFFCC\" FACE=\"Tahoma, Arial, Helvetica\"" },
  134     { "XYMONPAGETITLE", "COLOR=\"ivory\" SIZE=\"+1\"" },
  135     { "XYMONDATEFORMAT", "%a %b %d %H:%M:%S %Y" },
  136     { "XYMONRSSTITLE", "Xymon Alerts" },
  137     { "ACKUNTILMSG", "Next update at: %H:%M %Y-%m-%d" },
  138     { "WMLMAXCHARS", "1500" },
  139     { "XYMONREPWARN", "97" },
  140     { "XYMONGENREPOPTS", "--recentgifs --subpagecolumns=2" },
  141     { "XYMONGENSNAPOPTS", "--recentgifs --subpagecolumns=2" },
  142     { "XYMONSTDEXT", "" },
  143     { "XYMONHISTEXT", "" },
  144     { "TASKSLEEP", "300" },
  145     { "XYMONPAGECOLREPEAT", "0" },
  146     { "ALLOWALLCONFIGFILES", "" },
  147     { "XYMONHTACCESS", "" },
  148     { "XYMONPAGEHTACCESS", "" },
  149     { "XYMONSUBPAGEHTACCESS", "" },
  150     { "XYMONNETSVCS", "smtp telnet ftp pop pop3 pop-3 ssh imap ssh1 ssh2 imap2 imap3 imap4 pop2 pop-2 nntp" },
  151     { "HTMLCONTENTTYPE", "text/html" },
  152     { "HOLIDAYFORMAT", "%d/%m" },
  153     { "WEEKSTART", "1" },
  154     { "XYMONBODYCSS", "$XYMONSKIN/xymonbody.css" },
  155     { "XYMONBODYMENUCSS", "$XYMONMENUSKIN/xymonmenu.css" },
  156     { "XYMONBODYHEADER", "file:$XYMONHOME/etc/xymonmenu.cfg" },
  157     { "XYMONBODYFOOTER", "" },
  158     { "LOGFETCHSKIPTEXT", "<...SKIPPED...>" },
  159     { "LOGFETCHCURRENTTEXT", "<...CURRENT...>" },
  160     { "XYMONALLOKTEXT", "<FONT SIZE=+2 FACE=\"Arial, Helvetica\"><BR><BR><I>All Monitored Systems OK</I></FONT><BR><BR>" },
  161     { "HOSTPOPUP", "CDI" },
  162     { "STATUSLIFETIME", "30" },
  163     { "ACK_COOKIE_EXPIRATION", "86400" },
  164     { NULL, NULL }
  165 };
  166 
  167 char *xgetenv(const char *name)
  168 {
  169     char *result;
  170     SBUF_DEFINE(newstr);
  171     int i;
  172 
  173     result = getenv(name);
  174     if ((result == NULL) && (strcmp(name, "MACHINE") == 0) && xgetenv("MACHINEDOTS")) {
  175         /* If MACHINE is undefined, but MACHINEDOTS is there, create MACHINE  */
  176         SBUF_DEFINE(oneenv);
  177         char *p;
  178         
  179 #ifdef HAVE_SETENV
  180         oneenv = strdup(xgetenv("MACHINEDOTS"));
  181         p = oneenv; while ((p = strchr(p, '.')) != NULL) *p = ',';
  182         setenv(name, oneenv, 1);
  183         xfree(oneenv);
  184 #else
  185         SBUF_MALLOC(10 + strlen(xgetenv("MACHINEDOTS")));
  186         snprintf(oneenv, oneenv_buflen, "%s=%s", name, xgetenv("MACHINEDOTS"));
  187         p = oneenv; while ((p = strchr(p, '.')) != NULL) *p = ',';
  188         putenv(oneenv);
  189 #endif
  190         result = getenv(name);
  191     }
  192 
  193     if (result == NULL) {
  194         for (i=0; (xymonenv[i].name && (strcmp(xymonenv[i].name, name) != 0)); i++) ;
  195         if (xymonenv[i].name) result = expand_env(xymonenv[i].val);
  196         if (result == NULL) {
  197             errprintf("xgetenv: Cannot find value for variable %s\n", name);
  198             return NULL;
  199         }
  200 
  201         /* 
  202          * If we got a result, put it into the environment so it will stay there.
  203          * Allocate memory for this new environment string - this stays allocated.
  204          */
  205 #ifdef HAVE_SETENV
  206         setenv(name, result, 1);
  207 #else
  208         SBUF_MALLOC(newstr, strlen(name) + strlen(result) + 2); 
  209         snprintf(newstr, newstr_buflen, "%s=%s", name, result);
  210         putenv(newstr);
  211 #endif
  212         /*
  213          * Return pointer to the environment string.
  214          */
  215         result = getenv(name);
  216     }
  217 
  218     return result;
  219 }
  220 
  221 void envcheck(char *envvars[])
  222 {
  223     int i;
  224     int ok = 1;
  225 
  226     for (i = 0; (envvars[i]); i++) {
  227         if (xgetenv(envvars[i]) == NULL) {
  228             errprintf("Environment variable %s not defined\n", envvars[i]);
  229             ok = 0;
  230         }
  231     }
  232 
  233     if (!ok) {
  234         errprintf("Aborting\n");
  235         exit (1);
  236     }
  237 }
  238 
  239 void loadenv(char *envfile, char *area)
  240 {
  241     FILE *fd;
  242     strbuffer_t *inbuf;
  243     char *p, *marker;
  244     SBUF_DEFINE(oneenv);
  245 
  246     MEMDEFINE(l);
  247     inbuf = newstrbuffer(0);
  248 
  249     fd = stackfopen(envfile, "r", NULL);
  250     if (fd) {
  251         while (stackfgets(inbuf, NULL)) {
  252             char *equalpos;
  253             int appendto = 0;
  254 
  255             sanitize_input(inbuf, 1, 1);
  256 
  257             if ((STRBUFLEN(inbuf) == 0) || ((equalpos = strchr(STRBUF(inbuf), '=')) == NULL)) continue;
  258 
  259             appendto = ((equalpos > STRBUF(inbuf)) && (*(equalpos-1) == '+'));
  260 
  261             /*
  262              * Do the environment "area" stuff: If the input
  263              * is of the form AREA/NAME=VALUE, then setup the variable
  264              * only if we're called with the correct AREA setting.
  265              */
  266             oneenv = NULL;
  267 
  268             p = STRBUF(inbuf);
  269 
  270             /* Skip ahead for anyone who thinks this is a shell include */
  271             if ((strncmp(p, "export ", 7) == 0) || (strncmp(p, "export\t", 7) == 0)) { p += 6; p += strspn(p, " \t"); }
  272 
  273             marker = p + strcspn(p, "=/");
  274             if (*marker == '/') {
  275                 if (area) {
  276                     *marker = '\0';
  277                     if (strcasecmp(p, area) == 0) {
  278                         oneenv = strdup(expand_env(marker+1));
  279                         oneenv_buflen = strlen(oneenv)+1;
  280                     }
  281                 }
  282             }
  283             else {
  284                 oneenv = strdup(expand_env(p));
  285                 oneenv_buflen = strlen(oneenv)+1;
  286             }
  287 
  288             if (oneenv) {
  289                 p = strchr(oneenv, '=');
  290                 if (*(p+1) == '"') {
  291                     /* Move string over the first '"' */
  292                     memmove(p+1, p+2, strlen(p+2)+1);
  293                     /* Kill a trailing '"' */
  294                     if (*(oneenv + strlen(oneenv) - 1) == '"') *(oneenv + strlen(oneenv) - 1) = '\0';
  295                 }
  296 
  297                 if (appendto) {
  298                     char *oldval, *addstring, *p;
  299 
  300                     addstring = strchr(oneenv, '='); if (addstring) { *addstring = '\0'; addstring++; }
  301                     p = strchr(oneenv, '+'); if (p) *p = '\0';
  302 
  303                     oldval = getenv(oneenv);
  304                     if (oldval) {
  305                         SBUF_DEFINE(combinedenv);
  306 
  307                         SBUF_MALLOC(combinedenv, strlen(oneenv) + strlen(oldval) + strlen(addstring) + 2);
  308                         snprintf(combinedenv, combinedenv_buflen, "%s=%s%s", oneenv, oldval, (addstring));
  309                         xfree(oneenv);
  310                         oneenv = combinedenv;
  311                     }
  312                     else {
  313                         /* oneenv is now VARxxVALUE, so fix it to be a normal env. variable format */
  314                         strncat(oneenv, "=", oneenv_buflen-strlen(oneenv));
  315                         memmove(oneenv+strlen(oneenv), addstring, strlen(addstring) + 1);
  316                     }
  317                 }
  318 
  319                 putenv(oneenv);
  320             }
  321         }
  322         stackfclose(fd);
  323     }
  324     else {
  325         errprintf("Cannot open env file %s - %s\n", envfile, strerror(errno));
  326     }
  327 
  328     freestrbuffer(inbuf);
  329     MEMUNDEFINE(l);
  330 }
  331 
  332 char *getenv_default(char *envname, char *envdefault, char **buf)
  333 {
  334     static char *val;
  335 
  336     val = getenv(envname);  /* Don't use xgetenv() here! */
  337     if (!val) {
  338         unsigned int val_buflen = strlen(envname) + strlen(envdefault) + 2;
  339         val = (char *)malloc(val_buflen);
  340         snprintf(val, val_buflen, "%s=%s", envname, envdefault);
  341         putenv(val);
  342         /* Don't free the string - it must be kept for the environment to work */
  343         val = xgetenv(envname); /* OK to use xgetenv here */
  344     }
  345 
  346     if (buf) *buf = val;
  347     return val;
  348 }
  349 
  350 
  351 typedef struct envxp_t {
  352     char *result;
  353     int resultlen;
  354     struct envxp_t *next;
  355 } envxp_t;
  356 static envxp_t *xps = NULL;
  357 
  358 char *expand_env(char *s)
  359 {
  360     static char *res = NULL;
  361     static int depth = 0;
  362     char *sCopy, *bot, *tstart, *tend, *envval;
  363     char savech;
  364     envxp_t *myxp;
  365 
  366     if ((depth == 0) && res) xfree(res);
  367     depth++;
  368 
  369     myxp = (envxp_t *)malloc(sizeof(envxp_t));
  370     myxp->next = xps;
  371     xps = myxp;
  372 
  373     myxp->resultlen = 4096;
  374     myxp->result = (char *)malloc(myxp->resultlen);
  375     *(myxp->result) = '\0';
  376 
  377     sCopy = strdup(s);
  378     bot = sCopy;
  379     do {
  380         tstart = strchr(bot, '$');
  381         if (tstart) *tstart = '\0'; 
  382 
  383         if ((strlen(myxp->result) + strlen(bot) + 1) > myxp->resultlen) {
  384             myxp->resultlen += strlen(bot) + 4096;
  385             myxp->result = (char *)realloc(myxp->result, myxp->resultlen);
  386         }
  387         strncat(myxp->result, bot, (myxp->resultlen - strlen(myxp->result)));
  388 
  389         if (tstart) {
  390             tstart++;
  391             envval = NULL;
  392 
  393             if (*tstart == '{') {
  394                 tstart++;
  395                 tend = strchr(tstart, '}');
  396                 if (tend) { 
  397                     *tend = '\0'; 
  398                     envval = xgetenv(tstart);
  399                     bot = tend+1;
  400                 } 
  401                 else {
  402                     envval = xgetenv(tstart);
  403                     bot = NULL;
  404                 }
  405             }
  406             else {
  407                 tend = tstart + strspn(tstart, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
  408                 savech = *tend;
  409                 *tend = '\0';
  410                 envval = xgetenv(tstart);
  411                 *tend = savech;
  412                 bot = tend;
  413             }
  414 
  415             if (envval) {
  416                 if ((strlen(myxp->result) + strlen(envval) + 1) > myxp->resultlen) {
  417                     myxp->resultlen += strlen(envval) + 4096;
  418                     myxp->result = (char *)realloc(myxp->result, myxp->resultlen);
  419                 }
  420                 strncat(myxp->result, envval, (myxp->resultlen - strlen(myxp->result)));
  421             }
  422         }
  423         else {
  424             bot = NULL;
  425         }
  426     } while (bot);
  427     xfree(sCopy);
  428 
  429     depth--;
  430     if (depth == 0) {
  431         envxp_t *tmp;
  432         
  433         /* Free all xps except the last one (which is myxp) */
  434         while (xps->next) { tmp = xps; xps = xps->next; xfree(tmp->result); xfree(tmp); }
  435         if (xps != myxp) {
  436             errprintf("Assertion failed: xps != myxp\n");
  437             abort();
  438         }
  439 
  440         /* We KNOW that xps == myxp */
  441         res = myxp->result;
  442         xfree(myxp); 
  443         xps = NULL;
  444 
  445         return res;
  446     }
  447     else return myxp->result;
  448 }
  449