home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radius-2.zip / src / dict.c < prev    next >
C/C++ Source or Header  |  1994-12-27  |  7KB  |  307 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    <sys/types.h>
  33. #include    <pwd.h>
  34. #include    <ctype.h>
  35.  
  36. #include    "radius.h"
  37.  
  38. extern char        *progname;
  39. extern int        debug_flag;
  40. extern char        *radius_dir;
  41.  
  42. static DICT_ATTR    *dictionary_attributes;
  43. static DICT_VALUE    *dictionary_values;
  44.  
  45. /*************************************************************************
  46.  *
  47.  *    Function: dict_init
  48.  *
  49.  *    Purpose: Initialize the dictionary.  Read all ATTRIBUTES into
  50.  *         the dictionary_attributes list.  Read all VALUES into
  51.  *         the dictionary_values list.
  52.  *
  53.  *************************************************************************/
  54.  
  55. int
  56. dict_init()
  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    *
  216. dict_attrget(attribute)
  217. int    attribute;
  218. {
  219.     DICT_ATTR    *attr;
  220.  
  221.     attr = dictionary_attributes;
  222.     while(attr != (DICT_ATTR *)NULL) {
  223.         if(attr->value == attribute) {
  224.             return(attr);
  225.         }
  226.         attr = attr->next;
  227.     }
  228.     return((DICT_ATTR *)NULL);
  229. }
  230.  
  231. /*************************************************************************
  232.  *
  233.  *    Function: dict_attrfind
  234.  *
  235.  *    Purpose: Return the full attribute structure based on the
  236.  *         attribute name.
  237.  *
  238.  *************************************************************************/
  239.  
  240. DICT_ATTR    *
  241. dict_attrfind(attrname)
  242. char    *attrname;
  243. {
  244.     DICT_ATTR    *attr;
  245.  
  246.     attr = dictionary_attributes;
  247.     while(attr != (DICT_ATTR *)NULL) {
  248.         if(strcmp(attr->name, attrname) == 0) {
  249.             return(attr);
  250.         }
  251.         attr = attr->next;
  252.     }
  253.     return((DICT_ATTR *)NULL);
  254. }
  255.  
  256. /*************************************************************************
  257.  *
  258.  *    Function: dict_valfind
  259.  *
  260.  *    Purpose: Return the full value structure based on the
  261.  *         value name.
  262.  *
  263.  *************************************************************************/
  264.  
  265. DICT_VALUE    *
  266. dict_valfind(valname)
  267. char    *valname;
  268. {
  269.     DICT_VALUE    *val;
  270.  
  271.     val = dictionary_values;
  272.     while(val != (DICT_VALUE *)NULL) {
  273.         if(strcmp(val->name, valname) == 0) {
  274.             return(val);
  275.         }
  276.         val = val->next;
  277.     }
  278.     return((DICT_VALUE *)NULL);
  279. }
  280.  
  281. /*************************************************************************
  282.  *
  283.  *    Function: dict_valget
  284.  *
  285.  *    Purpose: Return the full value structure based on the
  286.  *         actual value and the associated attribute name.
  287.  *
  288.  *************************************************************************/
  289.  
  290. DICT_VALUE    *
  291. dict_valget(value, attrname)
  292. UINT4    value;
  293. char    *attrname;
  294. {
  295.     DICT_VALUE    *val;
  296.  
  297.     val = dictionary_values;
  298.     while(val != (DICT_VALUE *)NULL) {
  299.         if(strcmp(val->attrname, attrname) == 0 &&
  300.                         val->value == value) {
  301.             return(val);
  302.         }
  303.         val = val->next;
  304.     }
  305.     return((DICT_VALUE *)NULL);
  306. }
  307.