home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radius_2.zip / dict.c < prev    next >
C/C++ Source or Header  |  1996-05-28  |  7KB  |  308 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
  57. dict_init()
  58. {
  59.     FILE    *dictfd;
  60.     char    dummystr[64];
  61.     char    namestr[64];
  62.     char    valstr[64];
  63.     char    attrstr[64];
  64.     char    typestr[64];
  65.     int    line_no;
  66.     DICT_ATTR    *attr;
  67.     DICT_VALUE    *dval;
  68.     char    buffer[256];
  69.     int    value;
  70.     int    type;
  71.  
  72.     sprintf(buffer, "%s/%s", radius_dir, RADIUS_DICTIONARY);
  73.     if((dictfd = fopen(buffer, "r")) == (FILE *)NULL) {
  74.         fprintf (stderr, "%s: Couldn't open dictionary: %s\n",
  75.             progname, buffer);
  76.         return(-1);
  77.     }
  78.  
  79.     line_no = 0;
  80.     while(fgets(buffer, sizeof(buffer), dictfd) != (char *)NULL) {
  81.         line_no++;
  82.         
  83.         /* Skip empty space */
  84.         if(*buffer == '#' || *buffer == '\0' || *buffer == '\n') {
  85.             continue;
  86.         }
  87.  
  88.         if(strncmp(buffer, "ATTRIBUTE", 9) == 0) {
  89.  
  90.             /* Read the ATTRIBUTE line */
  91.             if(sscanf(buffer, "%s%s%s%s", dummystr, namestr,
  92.                     valstr, typestr) != 4) {
  93.                 fprintf(stderr,
  94.             "%s: Invalid attribute on line %d of dictionary\n",
  95.                     progname, line_no);
  96.                 return(-1);
  97.             }
  98.  
  99.             /*
  100.              * Validate all entries
  101.              */
  102.             if(strlen(namestr) > 31) {
  103.                 fprintf(stderr,
  104.             "%s: Invalid name length on line %d of dictionary\n",
  105.                     progname, line_no);
  106.                 return(-1);
  107.             }
  108.  
  109.             if(!isdigit(*valstr)) {
  110.                 fprintf(stderr,
  111.             "%s: Invalid value on line %d of dictionary\n",
  112.                     progname, line_no);
  113.                 return(-1);
  114.             }
  115.             value = atoi(valstr);
  116.  
  117.             if(strcmp(typestr, "string") == 0) {
  118.                 type = PW_TYPE_STRING;
  119.             }
  120.             else if(strcmp(typestr, "integer") == 0) {
  121.                 type = PW_TYPE_INTEGER;
  122.             }
  123.             else if(strcmp(typestr, "ipaddr") == 0) {
  124.                 type = PW_TYPE_IPADDR;
  125.             }
  126.             else if(strcmp(typestr, "date") == 0) {
  127.                 type = PW_TYPE_DATE;
  128.             }
  129.             else {
  130.                 fprintf(stderr,
  131.             "%s: Invalid type on line %d of dictionary\n",
  132.                     progname, line_no);
  133.                 return(-1);
  134.             }
  135.  
  136.             /* Create a new attribute for the list */
  137.             if((attr = (DICT_ATTR *)malloc(sizeof(DICT_ATTR))) ==
  138.                     (DICT_ATTR *)NULL) {
  139.                 fprintf(stderr, "%s: out of memory\n",
  140.                             progname);
  141.                 return(-1);
  142.             }
  143.             strcpy(attr->name, namestr);
  144.             attr->value = value;
  145.             attr->type = type;
  146.  
  147.             /* Insert it into the list */
  148.             attr->next = dictionary_attributes;
  149.             dictionary_attributes = attr;
  150.         }
  151.         else if(strncmp(buffer, "VALUE", 5) == 0) {
  152.  
  153.             /* Read the VALUE line */
  154.             if(sscanf(buffer, "%s%s%s%s", dummystr, attrstr,
  155.                         namestr, valstr) != 4) {
  156.                 fprintf(stderr,
  157.             "%s: Invalid value entry on line %d of dictionary\n",
  158.                     progname, line_no);
  159.                 return(-1);
  160.             }
  161.  
  162.             /*
  163.              * Validate all entries
  164.              */
  165.             if(strlen(attrstr) > 31) {
  166.                 fprintf(stderr,
  167.             "%s: Invalid attribute length on line %d of dictionary\n",
  168.                     progname, line_no);
  169.                 return(-1);
  170.             }
  171.  
  172.             if(strlen(namestr) > 31) {
  173.                 fprintf(stderr,
  174.             "%s: Invalid name length on line %d of dictionary\n",
  175.                     progname, line_no);
  176.                 return(-1);
  177.             }
  178.  
  179.             if(!isdigit(*valstr)) {
  180.                 fprintf(stderr,
  181.             "%s: Invalid value on line %d of dictionary\n",
  182.                     progname, line_no);
  183.                 return(-1);
  184.             }
  185.             value = atoi(valstr);
  186.  
  187.             /* Create a new VALUE entry for the list */
  188.             if((dval = (DICT_VALUE *)malloc(sizeof(DICT_VALUE))) ==
  189.                     (DICT_VALUE *)NULL) {
  190.                 fprintf(stderr, "%s: out of memory\n",
  191.                             progname);
  192.                 return(-1);
  193.             }
  194.             strcpy(dval->attrname, attrstr);
  195.             strcpy(dval->name, namestr);
  196.             dval->value = value;
  197.  
  198.             /* Insert it into the list */
  199.             dval->next = dictionary_values;
  200.             dictionary_values = dval;
  201.         }
  202.     }
  203.     fclose(dictfd);
  204.     return(0);
  205. }
  206.  
  207. /*************************************************************************
  208.  *
  209.  *    Function: dict_attrget
  210.  *
  211.  *    Purpose: Return the full attribute structure based on the
  212.  *         attribute id number.
  213.  *
  214.  *************************************************************************/
  215.  
  216. DICT_ATTR    *
  217. dict_attrget(attribute)
  218. int    attribute;
  219. {
  220.     DICT_ATTR    *attr;
  221.  
  222.     attr = dictionary_attributes;
  223.     while(attr != (DICT_ATTR *)NULL) {
  224.         if(attr->value == attribute) {
  225.             return(attr);
  226.         }
  227.         attr = attr->next;
  228.     }
  229.     return((DICT_ATTR *)NULL);
  230. }
  231.  
  232. /*************************************************************************
  233.  *
  234.  *    Function: dict_attrfind
  235.  *
  236.  *    Purpose: Return the full attribute structure based on the
  237.  *         attribute name.
  238.  *
  239.  *************************************************************************/
  240.  
  241. DICT_ATTR    *
  242. dict_attrfind(attrname)
  243. char    *attrname;
  244. {
  245.     DICT_ATTR    *attr;
  246.  
  247.     attr = dictionary_attributes;
  248.     while(attr != (DICT_ATTR *)NULL) {
  249.         if(strcmp(attr->name, attrname) == 0) {
  250.             return(attr);
  251.         }
  252.         attr = attr->next;
  253.     }
  254.     return((DICT_ATTR *)NULL);
  255. }
  256.  
  257. /*************************************************************************
  258.  *
  259.  *    Function: dict_valfind
  260.  *
  261.  *    Purpose: Return the full value structure based on the
  262.  *         value name.
  263.  *
  264.  *************************************************************************/
  265.  
  266. DICT_VALUE    *
  267. dict_valfind(valname)
  268. char    *valname;
  269. {
  270.     DICT_VALUE    *val;
  271.  
  272.     val = dictionary_values;
  273.     while(val != (DICT_VALUE *)NULL) {
  274.         if(strcmp(val->name, valname) == 0) {
  275.             return(val);
  276.         }
  277.         val = val->next;
  278.     }
  279.     return((DICT_VALUE *)NULL);
  280. }
  281.  
  282. /*************************************************************************
  283.  *
  284.  *    Function: dict_valget
  285.  *
  286.  *    Purpose: Return the full value structure based on the
  287.  *         actual value and the associated attribute name.
  288.  *
  289.  *************************************************************************/
  290.  
  291. DICT_VALUE    *
  292. dict_valget(value, attrname)
  293. int    value;
  294. char    *attrname;
  295. {
  296.     DICT_VALUE    *val;
  297.  
  298.     val = dictionary_values;
  299.     while(val != (DICT_VALUE *)NULL) {
  300.         if(strcmp(val->attrname, attrname) == 0 &&
  301.                         val->value == value) {
  302.             return(val);
  303.         }
  304.         val = val->next;
  305.     }
  306.     return((DICT_VALUE *)NULL);
  307. }
  308.