Back to index

nagios-plugins  1.4.16
Defines | Functions | Variables
dict.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <ctype.h>
#include "radiusd.h"

Go to the source code of this file.

Defines

#define DICT_STRCMP   strcmp

Functions

static void dict_free (void)
static int addvendor (char *name, int value)
int dict_init (char *fn)
DICT_ATTRdict_attrget (int attribute)
DICT_ATTRdict_attrfind (char *attrname)
DICT_VALUEdict_valfind (char *valname)
DICT_VALUEdict_valget (UINT4 value, char *attrname)
int dict_vendorpec (int code)
int dict_vendorcode (int pec)

Variables

char dict_sccsid [] = "@(#)dict.c 2.2 Copyright 1998 Cistron Internet Services B.V."
static DICT_ATTRdictionary_attributes
static DICT_VALUEdictionary_values
static DICT_VENDORdictionary_vendors
static int vendorno = 1

Define Documentation

#define DICT_STRCMP   strcmp

Definition at line 49 of file dict.c.


Function Documentation

static int addvendor ( char *  name,
int  value 
) [static]

Definition at line 82 of file dict.c.

{
       DICT_VENDOR *vval;

       if ((vval =(DICT_VENDOR *)malloc(sizeof(DICT_VENDOR))) ==
           (DICT_VENDOR *)NULL) {
              log(L_ERR|L_CONS, "dict_init: out of memory");
              return(-1);
       }
       strNcpy(vval->vendorname, name, sizeof(vval->vendorname));
       vval->vendorpec  = value;
       vval->vendorcode = vendorno++;

       /* Insert at front. */
       vval->next = dictionary_vendors;
       dictionary_vendors = vval;

       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

DICT_ATTR* dict_attrfind ( char *  attrname)

Definition at line 431 of file dict.c.

{
       DICT_ATTR     *attr;

       attr = dictionary_attributes;
       while(attr != (DICT_ATTR *)NULL) {
              if(DICT_STRCMP(attr->name, attrname) == 0) {
                     return(attr);
              }
              attr = attr->next;
       }
       return((DICT_ATTR *)NULL);
}
DICT_ATTR* dict_attrget ( int  attribute)

Definition at line 406 of file dict.c.

{
       DICT_ATTR     *attr;

       attr = dictionary_attributes;
       while(attr != (DICT_ATTR *)NULL) {
              if(attr->value == attribute) {
                     return(attr);
              }
              attr = attr->next;
       }
       return((DICT_ATTR *)NULL);
}

Here is the caller graph for this function:

static void dict_free ( void  ) [static]

Definition at line 55 of file dict.c.

{
       DICT_ATTR     *dattr, *anext;
       DICT_VALUE    *dval, *vnext;
       DICT_VENDOR   *dvend, *enext;

       for (dattr = dictionary_attributes; dattr; dattr = anext) {
              anext = dattr->next;
              free(dattr);
       }
       for (dval = dictionary_values; dval; dval = vnext) {
              vnext = dval->next;
              free(dval);
       }
       for (dvend = dictionary_vendors; dvend; dvend = enext) {
              enext = dvend->next;
              free(dvend);
       }
       dictionary_attributes = NULL;
       dictionary_values = NULL;
       dictionary_vendors = NULL;
       vendorno = 1;
}

Here is the caller graph for this function:

int dict_init ( char *  fn)

Definition at line 108 of file dict.c.

{
       FILE   *dictfd;
       char   dummystr[64];
       char   namestr[64];
       char   valstr[64];
       char   attrstr[64];
       char   typestr[64];
       char   vendorstr[64];
       int    line_no;
       DICT_ATTR     *attr;
       DICT_VALUE    *dval;
       DICT_VENDOR   *v;
       char   buffer[256];
       int    value;
       int    type;
       int    vendor;
       int    is_attrib;
#ifdef ATTRIB_NMC
       int    vendor_usr_seen = 0;
       int    is_nmc = 0;
#endif

       if (fn == NULL) dict_free();

       if (fn) {
              if (fn[0] == '/')
                     strNcpy(buffer, fn, sizeof(buffer));
              else
                     sprintf(buffer, "%.127s/%.127s", radius_dir, fn);
       } else
              sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_DICTIONARY);

       if((dictfd = fopen(buffer, "r")) == (FILE *)NULL) {
              log(L_CONS|L_ERR, "dict_init: Couldn't open dictionary: %s",
                     buffer);
              return(-1);
       }

       line_no = 0;
       while(fgets(buffer, sizeof(buffer), dictfd) != (char *)NULL) {
              line_no++;
              
              /* Skip empty space */
              if(*buffer == '#' || *buffer == '\0' || *buffer == '\n') {
                     continue;
              }

              if (strncasecmp(buffer, "$INCLUDE", 8) == 0) {

                     /* Read the $INCLUDE line */
                     if(sscanf(buffer, "%63s%63s", dummystr, valstr) != 2) {
                            log(L_ERR,
                     "%s: Invalid filename on line %d of dictionary\n",
                                   progname, line_no);
                            return(-1);
                     }
                     if (dict_init(valstr) < 0)
                            return -1;
                     continue;
              }

              is_attrib = 0;
              if (strncmp(buffer, "ATTRIBUTE", 9) == 0)
                     is_attrib = 1;
#ifdef ATTRIB_NMC
              is_nmc = 0;
              if (strncmp(buffer, "ATTRIB_NMC", 10) == 0)
                     is_attrib = is_nmc = 1;
#endif
              if (is_attrib) {
                     /* Read the ATTRIBUTE line */
                     vendor = 0;
                     vendorstr[0] = 0;
                     if(sscanf(buffer, "%63s%63s%63s%63s%63s", dummystr,
                            namestr, valstr, typestr, vendorstr) < 4) {
                            log(L_ERR,
                     "%s: Invalid attribute on line %d of dictionary\n",
                                   progname, line_no);
                            return(-1);
                     }

#ifdef ATTRIB_NMC
                     /*
                      *     Convert ATTRIB_NMC into our format.
                      *     We might need to add USR to the list of
                      *     vendors first.
                      */
                     if (is_nmc && vendorstr[0] == 0) {
                            if (!vendor_usr_seen) {
                                   if (addvendor("USR", VENDORPEC_USR) < 0)
                                          return -1;
                                   vendor_usr_seen = 1;
                            }
                            strcpy(vendorstr, "USR");
                     }
#endif

                     /*
                      * Validate all entries
                      */
                     if(strlen(namestr) > 31) {
                            log(L_ERR|L_CONS,
              "dict_init: Invalid name length on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     if(!isdigit(*valstr)) {
                            log(L_ERR|L_CONS,
                     "dict_init: Invalid value on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }
                     if (valstr[0] != '0')
                            value = atoi(valstr);
                     else
                            sscanf(valstr, "%i", &value);

                     if(strcmp(typestr, "string") == 0) {
                            type = PW_TYPE_STRING;
                     }
                     else if(strcmp(typestr, "integer") == 0) {
                            type = PW_TYPE_INTEGER;
                     }
                     else if(strcmp(typestr, "ipaddr") == 0) {
                            type = PW_TYPE_IPADDR;
                     }
                     else if(strcmp(typestr, "date") == 0) {
                            type = PW_TYPE_DATE;
                     }
                     else {
                            log(L_ERR|L_CONS,
                     "dict_init: Invalid type on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     for (v = dictionary_vendors; v; v = v->next) {
                            if (strcmp(vendorstr, v->vendorname) == 0)
                                   vendor = v->vendorcode;
                     }
                     if (vendorstr[0] && !vendor) {
                            log(L_ERR|L_CONS,
                     "dict_init: unknown vendor %s on line %d of dictionary",
                            vendorstr, line_no);
                            return -1;
                     }

#ifdef COMPAT_1543
                     /*
                      *     Convert old values 221,1036-1039 to the new
                      *     ones, in case the dictionary is still the
                      *     one from 1.5.4.3
                      *
                      *     XXX - this is a HACK !!
                      */
                     switch (value) {
                            case 221:
                                   value = PW_HUNTGROUP_NAME;
                                   break;
                            case 1036:
                                   value = PW_FALL_THROUGH;
                                   break;
                            case 1037:
                                   value = PW_ADD_PORT_TO_IP_ADDRESS;
                                   break;
                            case 1038:
                                   value = PW_EXEC_PROGRAM;
                                   break;
                            case 1039:
                                   value = PW_EXEC_PROGRAM_WAIT;
                                   break;
                     }
#endif
                     /* Create a new attribute for the list */
                     if((attr = (DICT_ATTR *)malloc(sizeof(DICT_ATTR))) ==
                                   (DICT_ATTR *)NULL) {
                            log(L_ERR|L_CONS, "dict_init: out of memory");
                            return(-1);
                     }
                     strNcpy(attr->name, namestr, sizeof(attr->name));
                     attr->value = value;
                     attr->type = type;
                     if (vendor)
                            attr->value |= (vendor << 16);

                     /*
                      *     Add to the front of the list, so that
                      *     values at the end of the file override
                      *     those in the begin.
                      */
                     attr->next = dictionary_attributes;
                     dictionary_attributes = attr;

              }
              else if (strncmp(buffer, "VALUE", 5) == 0) {

                     /* Read the VALUE line */
                     if(sscanf(buffer, "%63s%63s%63s%63s", dummystr, attrstr,
                                          namestr, valstr) != 4) {
                            log(L_ERR|L_CONS,
              "dict_init: Invalid value entry on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     /*
                      * Validate all entries
                      */
                     if(strlen(attrstr) > 31) {
                            log(L_ERR|L_CONS,
              "dict_init: Invalid attribute length on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     if(strlen(namestr) > 31) {
                            log(L_ERR|L_CONS,
              "dict_init: Invalid name length on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     if(!isdigit(*valstr)) {
                            log(L_ERR|L_CONS,
                     "dict_init: Invalid value on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }
                     value = atoi(valstr);

                     /* Create a new VALUE entry for the list */
                     if((dval = (DICT_VALUE *)malloc(sizeof(DICT_VALUE))) ==
                                   (DICT_VALUE *)NULL) {
                            log(L_ERR|L_CONS, "dict_init: out of memory");
                            return(-1);
                     }
                     strNcpy(dval->attrname, attrstr, sizeof(dval->attrname));
                     strNcpy(dval->name, namestr, sizeof(dval->name));
                     dval->value = value;

                     /* Insert at front. */
                     dval->next = dictionary_values;
                     dictionary_values = dval;
              }
              else if(strncmp(buffer, "VENDOR", 6) == 0) {

                     /* Read the VENDOR line */
                     if(sscanf(buffer, "%63s%63s%63s", dummystr, attrstr,
                                          valstr) != 3) {
                            log(L_ERR|L_CONS,
              "dict_init: Invalid vendor entry on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     /*
                      * Validate all entries
                      */
                     if(strlen(attrstr) > 31) {
                            log(L_ERR|L_CONS,
              "dict_init: Invalid attribute length on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }

                     if(!isdigit(*valstr)) {
                            log(L_ERR|L_CONS,
                     "dict_init: Invalid value on line %d of dictionary",
                                   line_no);
                            return(-1);
                     }
                     value = atoi(valstr);

                     /* Create a new VENDOR entry for the list */
                     if (addvendor(attrstr, value) < 0)
                            return -1;
#ifdef ATTRIB_NMC
                     if (value == VENDORPEC_USR)
                            vendor_usr_seen = 1;
#endif
              }
       }
       fclose(dictfd);
       return(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

DICT_VALUE* dict_valfind ( char *  valname)

Definition at line 456 of file dict.c.

{
       DICT_VALUE    *val;

       val = dictionary_values;
       while(val != (DICT_VALUE *)NULL) {
              if(DICT_STRCMP(val->name, valname) == 0) {
                     return(val);
              }
              val = val->next;
       }
       return((DICT_VALUE *)NULL);
}
DICT_VALUE* dict_valget ( UINT4  value,
char *  attrname 
)

Definition at line 481 of file dict.c.

{
       DICT_VALUE    *val;

       val = dictionary_values;
       while(val != (DICT_VALUE *)NULL) {
              if(DICT_STRCMP(val->attrname, attrname) == 0 &&
                                          val->value == value) {
                     return(val);
              }
              val = val->next;
       }
       return((DICT_VALUE *)NULL);
}

Here is the caller graph for this function:

int dict_vendorcode ( int  pec)

Definition at line 516 of file dict.c.

{
       DICT_VENDOR   *v;

       for (v = dictionary_vendors; v; v = v->next)
              if (v->vendorpec == pec)
                     break;

       return v ? v->vendorcode : 0;
}
int dict_vendorpec ( int  code)

Definition at line 502 of file dict.c.

{
       DICT_VENDOR   *v;

       for (v = dictionary_vendors; v; v = v->next)
              if (v->vendorcode == code)
                     break;

       return v ? v->vendorpec : 0;
}

Variable Documentation

char dict_sccsid[] = "@(#)dict.c 2.2 Copyright 1998 Cistron Internet Services B.V."

Definition at line 28 of file dict.c.

Definition at line 40 of file dict.c.

Definition at line 41 of file dict.c.

Definition at line 42 of file dict.c.

int vendorno = 1 [static]

Definition at line 44 of file dict.c.