Logo Search packages:      
Sourcecode: radiusd-livingston version File versions

menu.c

/***********************************************************************

RADIUS
Remote Authentication Dial In User Service

Lucent Technologies Remote Access
4464 Willow Road
Pleasanton, CA   94588

Copyright 1992-1999 Lucent Technologies Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above
     copyright notice, this list of conditions and the following
     disclaimer in the documentation and/or other materials provided
     with the distribution.

   * All advertising materials mentioning features or use of this
     software must display the following acknowledgement:

        This product includes software developed by Lucent
        Technologies and its contributors.

   * Neither the name of the copyright holder nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

This software is provided by the copyright holders and contributors
``as is'' and any express or implied warranties, including, but not
limited to, the implied warranties of merchantability and fitness for a
particular purpose are disclaimed. In no event shall the copyright
holder or contributors be liable for any direct, indirect, incidental,
special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use,
data, or profits; or business interruption) however caused and on any
theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage.

************************************************************************/


static char sccsid[] =
"$Id: menu.c,v 1.9 1999/06/23 23:40:40 cdr Exp $ Copyright 1992-1999 Lucent Technologies Inc";

#include    <sys/types.h>
#include    <sys/socket.h>
#include    <sys/time.h>
#include    <sys/file.h>
#include    <netinet/in.h>

#include    <stdio.h>
#include    <netdb.h>
#include    <fcntl.h>
#include    <pwd.h>
#include    <time.h>
#include    <ctype.h>
#include    <unistd.h>
#include    <signal.h>
#include    <errno.h>
#include    <sys/wait.h>
#include    <string.h>

#include    "radius.h"

extern char *radius_dir;
extern char *radacct_dir;
extern char *progname;

/*************************************************************************
 *
 *      Function: validfilename
 *
 *      Purpose: Check to make sure a filename is valid
 *               I.e. weed out nasties
 *
 *************************************************************************/

/* Valid characters in a filename */
static char *validfilname_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890#-+ \t/";

int
validfilename(filename)
char *filename;
{
        /* Not a valid filename if it is NULL or zero length */
        if(filename==NULL || *filename=='\0'){
                return(0);
        }

        /* Check each character in the filename to make sure
         * it is valid as per validfilname_chars
         */
        while(*filename!='\0'){
                if(strchr(validfilname_chars, *filename)==NULL){
                        return(0);
                }
                filename++;
        }

        /* Not a filename if it ends with a '/' */
        if(*filename=='/'){
                return(0);
        }

        return(1);
}


void
process_menu(authreq, activefd, pw_digest)
AUTH_REQ    *authreq;
int         activefd;
char        *pw_digest;
{
      VALUE_PAIR  *attr;
      VALUE_PAIR  *term_attr;
      VALUE_PAIR  *newattr;
      VALUE_PAIR  *get_attribute();
      VALUE_PAIR  *menu_pairs();
      VALUE_PAIR  *pairalloc();
      char        menu_name[256];
      char        menu_input[32];
      int         i;
      char        state_value[256];
      void        send_accept();
      void        send_reject();
      void        pairfree();
      void        reqfree();

      if((attr = get_attribute(authreq->request, PW_STATE)) !=
            (VALUE_PAIR *)NULL && strncmp(attr->strvalue, "MENU=", 5) == 0){

            strncpy(menu_name, &attr->strvalue[5], sizeof(menu_name)-1);
                menu_name[sizeof(menu_name)-1] = '\0';

            /* The menu input is in the Password Field */
            attr = get_attribute(authreq->request, PW_PASSWORD);
            if(attr == (VALUE_PAIR *)NULL) {
                  *menu_input = '\0';
            }
            else {
                  /*
                   * Decrypt the password in the request.
                   */
                  memcpy(menu_input, attr->strvalue, AUTH_PASS_LEN);
                  for(i = 0;i < AUTH_PASS_LEN;i++) {
                        menu_input[i] ^= pw_digest[i];
                  }
                  menu_input[AUTH_PASS_LEN] = '\0';
            }
            attr = menu_pairs(menu_name, menu_input);
      }

      /* handle termination menu */
      if((term_attr = get_attribute(attr, PW_TERMINATION_MENU)) !=
                                          (VALUE_PAIR *)NULL) {

            /* Change this to a menu state */
            snprintf(state_value, sizeof(state_value), "MENU=%s", 
                        term_attr->strvalue);
            term_attr->attribute = PW_STATE;
            strcpy(term_attr->strvalue, state_value);
            strcpy(term_attr->name, "Challenge-State");

            /* Insert RADIUS termination option */
            /* Set termination values */
            newattr = pairalloc("process_menu");

            newattr->attribute = PW_TERMINATION;
            newattr->type = PW_TYPE_INTEGER;
            newattr->lvalue = PW_TERM_RADIUS_REQUEST;
            strcpy(newattr->name, "Termination-Action");

            /* Insert it */
            newattr->next = term_attr->next;
            term_attr->next = newattr;
      }

      if((term_attr = get_attribute(attr, PW_MENU)) != (VALUE_PAIR *)NULL &&
                        strcmp(term_attr->strvalue, "EXIT") == 0) {
            send_reject(authreq, (char *)"", activefd);
            pairfree(attr,"process_menu");
      }
      else if(attr != (VALUE_PAIR *)NULL) {
            send_accept(authreq, attr, (char *)NULL, activefd);
            pairfree(attr,"process_menu");
      }
      else {
            send_reject(authreq, (char *)NULL, activefd);
      }
      reqfree(authreq,"process_menu");
      return;
}

char  *
get_menu(menu_name)
char  *menu_name;
{
      FILE  *fd;
      static      char menu_buffer[4096];
      int   mode;
      char  *ptr;
      int   nread;
      int   len;

      sprintf(menu_buffer, "%s/menus/%s", radius_dir, menu_name);
      if(!validfilename(menu_buffer) || 
                (fd = fopen(menu_buffer, "r")) == (FILE *)NULL) {
            return("\r\n*** User Menu is Not Available ***\r\n");
      }

      mode = 0;
      nread = 0;
      ptr = menu_buffer;
      *ptr = '\0';
      while(fgets(ptr, 4096 - nread - 4, fd) != NULL && nread < 4096) {

            if(mode == 0) {
                  if(strncmp(ptr, "menu", 4) == 0) {
                        mode = 1;
                  }
            }
            else {
                  if(strncmp(ptr, "end\n", 4) == 0) {
                        if(ptr > menu_buffer) {
                              *(ptr - 2) = '\0';
                        }
                        else {
                              *ptr = '\0';
                        }
                        return(menu_buffer);
                  }
                  len = strlen(ptr);
                  ptr += len - 1;
                  *ptr++ = '\r';
                  *ptr++ = '\n';
                  nread += len + 1;
            }
      }
      *ptr = '\0';
      return(menu_buffer);
}

VALUE_PAIR  *
menu_pairs(menu_name, menu_selection)
char  *menu_name;
char  *menu_selection;
{
      FILE  *fd;
      char  buffer[4096];
      char  selection[32];
      int   mode;
      char  *ptr;
      int   nread;
      int   userparse();
      VALUE_PAIR  *reply_first;
      void  pairfree();
      int   fclose();

      sprintf(buffer, "%s/menus/%s", radius_dir, menu_name);
      if(!validfilename(buffer) || 
                (fd = fopen(buffer, "r")) == (FILE *)NULL) {
            return((VALUE_PAIR *)NULL);
      }

      /* Skip past the menu */
      mode = 0;
      nread = 0;
      while(fgets(buffer, sizeof(buffer) - 1, fd) != NULL) {
                buffer[sizeof(buffer)-1]='\0';
            if(mode == 0) {
                  if(strncmp(buffer, "menu", 4) == 0) {
                        mode = 1;
                  }
            }
            else {
                  if(strncmp(buffer, "end\n", 4) == 0) {
                        break;
                  }
            }
      }

      /* handle default */
      if(*menu_selection == '\0') {
            strcpy(selection, "<CR>");
      }
      else {
            strcpy(selection, menu_selection);
      }
      reply_first = (VALUE_PAIR *)NULL;

      /* Look for a matching menu entry */
      while(fgets(buffer, sizeof(buffer)-1, fd) != NULL) {
                buffer[sizeof(buffer)-1]='\0';

            /* Terminate the buffer */
            ptr = buffer;
            while(*ptr != '\n' && *ptr != '\0') {
                  ptr++;
            }
            if(*ptr == '\n') {
                  *ptr = '\0';
            }

            if(strcmp(selection, buffer) == 0 ||
                              strcmp("DEFAULT", buffer) == 0) {
                  /* We have a match */
                  while(fgets(buffer, sizeof(buffer)-1, fd) != NULL) {
                      if(*buffer == ' ' || *buffer == '\t') {
                        /*
                         * Parse the reply values
                         */
                        if(userparse(buffer, &reply_first) != 0) {
                              log_err("parse error for menu %s\n",menu_name);
                              pairfree(reply_first,"menu_pairs");
                              fclose(fd);
                              return((VALUE_PAIR *)NULL);
                        }
                      }
                      else {
                        /* We are done */
                        fclose(fd);
                        return(reply_first);
                      }
                  }
                  fclose(fd);
                  return(reply_first);
            }
      }
      fclose(fd);
      return((VALUE_PAIR *)NULL);
}

Generated by  Doxygen 1.6.0   Back to index