home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radi116c.zip / radius116c / src / radius / dict.c < prev    next >
C/C++ Source or Header  |  1997-02-04  |  7KB  |  298 lines

  1. /*
  2.  *
  3.  *    RADIUS
  4.  *    Remote Authentication Dial In User Service
  5.  *
  6.  *
  7.  *    Livingston Enterprises, Inc.
  8.  *    6920 Koll Center Parkway
  9.  *    Pleasanton, CA   94566
  10.  *
  11.  *    Copyright 1992 Livingston Enterprises, Inc.
  12.  *
  13.  *    Permission to use, copy, modify, and distribute this software for any
  14.  *    purpose and without fee is hereby granted, provided that this
  15.  *    copyright and permission notice appear on all copies and supporting
  16.  *    documentation, the name of Livingston Enterprises, Inc. not be used
  17.  *    in advertising or publicity pertaining to distribution of the
  18.  *    program without specific prior permission, and notice be given
  19.  *    in supporting documentation that copying and distribution is by
  20.  *    permission of Livingston Enterprises, Inc.
  21.  *
  22.  *    Livingston Enterprises, Inc. makes no representations about
  23.  *    the suitability of this software for any purpose.  It is
  24.  *    provided "as is" without express or implied warranty.
  25.  *
  26.  */
  27.  
  28. static char sccsid[] =
  29. "@(#)dict.c    1.5 Copyright 1992 Livingston Enterprises Inc";
  30.  
  31. #include    <stdio.h>
  32. #include    <stdlib.h>
  33. #include    <sys/types.h>
  34. #include    <pwd.h>
  35. #include    <ctype.h>
  36.  
  37. #include    "radius.h"
  38.  
  39. extern char        *progname;
  40. extern int        debug_flag;
  41. extern char        *radius_dir;
  42.  
  43. static DICT_ATTR    *dictionary_attributes;
  44. static DICT_VALUE    *dictionary_values;
  45.  
  46. /*************************************************************************
  47.  *
  48.  *    Function: dict_init
  49.  *
  50.  *    Purpose: Initialize the dictionary.  Read all ATTRIBUTES into
  51.  *         the dictionary_attributes list.  Read all VALUES into
  52.  *         the dictionary_values list.
  53.  *
  54.  *************************************************************************/
  55.  
  56. int dict_init(void)
  57. {
  58.     FILE    *dictfd;
  59.     char    dummystr[64];
  60.     char    namestr[64];
  61.     char    valstr[64];
  62.     char    attrstr[64];
  63.     char    typestr[64];
  64.     int    line_no;
  65.     DICT_ATTR    *attr;
  66.     DICT_VALUE    *dval;
  67.     char    buffer[256];
  68.     int    value;
  69.     int    type;
  70.  
  71.     sprintf(buffer, "%s/%s", radius_dir, RADIUS_DICTIONARY);
  72.     if((dictfd = fopen(buffer, "r")) == (FILE *)NULL) {
  73.         fprintf (stderr, "%s: Couldn't open dictionary: %s\n",
  74.             progname, buffer);
  75.         return(-1);
  76.     }
  77.  
  78.     line_no = 0;
  79.     while(fgets(buffer, sizeof(buffer), dictfd) != (char *)NULL) {
  80.         line_no++;
  81.         
  82.         /* Skip empty space */
  83.         if(*buffer == '#' || *buffer == '\0' || *buffer == '\n') {
  84.             continue;
  85.         }
  86.  
  87.         if(strncmp(buffer, "ATTRIBUTE", 9) == 0) {
  88.  
  89.             /* Read the ATTRIBUTE line */
  90.             if(sscanf(buffer, "%s%s%s%s", dummystr, namestr,
  91.                     valstr, typestr) != 4) {
  92.                 fprintf(stderr,
  93.             "%s: Invalid attribute on line %d of dictionary\n",
  94.                     progname, line_no);
  95.                 return(-1);
  96.             }
  97.  
  98.             /*
  99.              * Validate all entries
  100.              */
  101.             if(strlen(namestr) > 31) {
  102.                 fprintf(stderr,
  103.             "%s: Invalid name length on line %d of dictionary\n",
  104.                     progname, line_no);
  105.                 return(-1);
  106.             }
  107.  
  108.             if(!isdigit(*valstr)) {
  109.                 fprintf(stderr,
  110.             "%s: Invalid value on line %d of dictionary\n",
  111.                     progname, line_no);
  112.                 return(-1);
  113.             }
  114.             value = atoi(valstr);
  115.  
  116.             if(strcmp(typestr, "string") == 0) {
  117.                 type = PW_TYPE_STRING;
  118.             }
  119.             else if(strcmp(typestr, "integer") == 0) {
  120.                 type = PW_TYPE_INTEGER;
  121.             }
  122.             else if(strcmp(typestr, "ipaddr") == 0) {
  123.                 type = PW_TYPE_IPADDR;
  124.             }
  125.             else if(strcmp(typestr, "date") == 0) {
  126.                 type = PW_TYPE_DATE;
  127.             }
  128.             else {
  129.                 fprintf(stderr,
  130.             "%s: Invalid type on line %d of dictionary\n",
  131.                     progname, line_no);
  132.                 return(-1);
  133.             }
  134.  
  135.             /* Create a new attribute for the list */
  136.             if((attr = (DICT_ATTR *)malloc(sizeof(DICT_ATTR))) ==
  137.                     (DICT_ATTR *)NULL) {
  138.                 fprintf(stderr, "%s: out of memory\n",
  139.                             progname);
  140.                 return(-1);
  141.             }
  142.             strcpy(attr->name, namestr);
  143.             attr->value = value;
  144.             attr->type = type;
  145.  
  146.             /* Insert it into the list */
  147.             attr->next = dictionary_attributes;
  148.             dictionary_attributes = attr;
  149.         }
  150.         else if(strncmp(buffer, "VALUE", 5) == 0) {
  151.  
  152.             /* Read the VALUE line */
  153.             if(sscanf(buffer, "%s%s%s%s", dummystr, attrstr,
  154.                         namestr, valstr) != 4) {
  155.                 fprintf(stderr,
  156.             "%s: Invalid value entry on line %d of dictionary\n",
  157.                     progname, line_no);
  158.                 return(-1);
  159.             }
  160.  
  161.             /*
  162.              * Validate all entries
  163.              */
  164.             if(strlen(attrstr) > 31) {
  165.                 fprintf(stderr,
  166.             "%s: Invalid attribute length on line %d of dictionary\n",
  167.                     progname, line_no);
  168.                 return(-1);
  169.             }
  170.  
  171.             if(strlen(namestr) > 31) {
  172.                 fprintf(stderr,
  173.             "%s: Invalid name length on line %d of dictionary\n",
  174.                     progname, line_no);
  175.                 return(-1);
  176.             }
  177.  
  178.             if(!isdigit(*valstr)) {
  179.                 fprintf(stderr,
  180.             "%s: Invalid value on line %d of dictionary\n",
  181.                     progname, line_no);
  182.                 return(-1);
  183.             }
  184.             value = atoi(valstr);
  185.  
  186.             /* Create a new VALUE entry for the list */
  187.             if((dval = (DICT_VALUE *)malloc(sizeof(DICT_VALUE))) ==
  188.                     (DICT_VALUE *)NULL) {
  189.                 fprintf(stderr, "%s: out of memory\n",
  190.                             progname);
  191.                 return(-1);
  192.             }
  193.             strcpy(dval->attrname, attrstr);
  194.             strcpy(dval->name, namestr);
  195.             dval->value = value;
  196.  
  197.             /* Insert it into the list */
  198.             dval->next = dictionary_values;
  199.             dictionary_values = dval;
  200.         }
  201.     }
  202.     fclose(dictfd);
  203.     return(0);
  204. }
  205.  
  206. /*************************************************************************
  207.  *
  208.  *    Function: dict_attrget
  209.  *
  210.  *    Purpose: Return the full attribute structure based on the
  211.  *         attribute id number.
  212.  *
  213.  *************************************************************************/
  214.  
  215. DICT_ATTR    * dict_attrget(int attribute)
  216. {
  217.     DICT_ATTR    *attr;
  218.  
  219.     attr = dictionary_attributes;
  220.     while(attr != (DICT_ATTR *)NULL) {
  221.         if(attr->value == attribute) {
  222.             return(attr);
  223.         }
  224.         attr = attr->next;
  225.     }
  226.     return((DICT_ATTR *)NULL);
  227. }
  228.  
  229. /*************************************************************************
  230.  *
  231.  *    Function: dict_attrfind
  232.  *
  233.  *    Purpose: Return the full attribute structure based on the
  234.  *         attribute name.
  235.  *
  236.  *************************************************************************/
  237.  
  238. DICT_ATTR    *dict_attrfind(char *attrname)
  239. {
  240.     DICT_ATTR    *attr;
  241.  
  242.     attr = dictionary_attributes;
  243.     while(attr != (DICT_ATTR *)NULL) {
  244.         if(strcmp(attr->name, attrname) == 0) {
  245.             return(attr);
  246.         }
  247.         attr = attr->next;
  248.     }
  249.     return((DICT_ATTR *)NULL);
  250. }
  251.  
  252. /*************************************************************************
  253.  *
  254.  *    Function: dict_valfind
  255.  *
  256.  *    Purpose: Return the full value structure based on the
  257.  *         value name.
  258.  *
  259.  *************************************************************************/
  260.  
  261. DICT_VALUE    *dict_valfind(char *valname)
  262. {
  263.     DICT_VALUE    *val;
  264.  
  265.     val = dictionary_values;
  266.     while(val != (DICT_VALUE *)NULL) {
  267.         if(strcmp(val->name, valname) == 0) {
  268.             return(val);
  269.         }
  270.         val = val->next;
  271.     }
  272.     return((DICT_VALUE *)NULL);
  273. }
  274.  
  275. /*************************************************************************
  276.  *
  277.  *    Function: dict_valget
  278.  *
  279.  *    Purpose: Return the full value structure based on the
  280.  *         actual value and the associated attribute name.
  281.  *
  282.  *************************************************************************/
  283.  
  284. DICT_VALUE    *dict_valget(int value, char *attrname)
  285. {
  286.     DICT_VALUE    *val;
  287.  
  288.     val = dictionary_values;
  289.     while(val != (DICT_VALUE *)NULL) {
  290.         if(strcmp(val->attrname, attrname) == 0 &&
  291.                         val->value == value) {
  292.             return(val);
  293.         }
  294.         val = val->next;
  295.     }
  296.     return((DICT_VALUE *)NULL);
  297. }
  298.