home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / radius-2.zip / src / builddbm.c < prev    next >
C/C++ Source or Header  |  1994-12-27  |  4KB  |  188 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. "@(#)builddbm.c    1.4 Copyright 1992 Livingston Enterprises Inc";
  30.  
  31. #include    <sys/types.h>
  32. #include    <sys/socket.h>
  33. #include    <sys/time.h>
  34. #include    <sys/file.h>
  35. #include    <netinet/in.h>
  36.  
  37. #include    <stdio.h>
  38. #include    <netdb.h>
  39. #include    <strings.h>
  40. #include    <pwd.h>
  41. #include    <time.h>
  42. #include    <ctype.h>
  43. #include    <dbm.h>
  44.  
  45. #include    "radius.h"
  46.  
  47. char        *progname;
  48. int        debug_flag;
  49. char        *radius_dir;
  50.  
  51. #define FIND_MODE_NAME    0
  52. #define FIND_MODE_REPLY    1
  53. #define FIND_MODE_SKIP    2
  54. #define FIND_MODE_FLUSH    3
  55.  
  56. FILE        *userfd;
  57.  
  58. main(argc,argv)
  59. int argc;
  60. char **argv;
  61. {
  62.     char    name[128];
  63.     char    content[1024];
  64.     char     *progname;
  65.     int    fd;
  66.     datum    named;
  67.     datum    contentd;
  68.  
  69.     progname = *argv;
  70.  
  71.     if((fd = open("users.pag", O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
  72.         fprintf(stderr, "%s: Couldn't open users.pag for writing\n",progname);
  73.         exit(-1);
  74.     }
  75.     close(fd);
  76.     if((fd = open("users.dir", O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
  77.         fprintf(stderr, "%s: Couldn't open users.dir for writing\n",progname);
  78.         exit(-1);
  79.     }
  80.     close(fd);
  81.     radius_dir = ".";
  82.     if(dbminit("users") != 0) {
  83.         fprintf(stderr, "%: Couldn't init dbm\n",progname);
  84.         exit(-1);
  85.     }
  86.  
  87.     while(user_read(name, content) == 0) {
  88.         named.dptr = name;
  89.         named.dsize = strlen(name);
  90.         contentd.dptr = content;
  91.         contentd.dsize = strlen(content);
  92.         if(store(named, contentd) != 0) {
  93.             fprintf(stderr, "%s: Couldn't store datum for %s\n",
  94.                 progname,name);
  95.             exit(-1);
  96.         }
  97.     }
  98.     dbmclose();
  99.     exit(0);
  100. }
  101.  
  102. /*************************************************************************
  103.  *
  104.  *    Function: user_read
  105.  *
  106.  *    Purpose: Return each user in the database - name is key content
  107.  *         is 2 strings - check values, and reply values seperated
  108.  *         by a newline.
  109.  *
  110.  *************************************************************************/
  111.  
  112. user_read(name, content)
  113. char    *name;
  114. char    *content;
  115. {
  116.     char        buffer[256];
  117.     char        *ptr;
  118.     int        namelen;
  119.     int        mode;
  120.     VALUE_PAIR    *check_first;
  121.     VALUE_PAIR    *reply_first;
  122.  
  123.     /*
  124.      * Open the user table
  125.      */
  126.     if(userfd == (FILE *)NULL) {
  127.         sprintf(buffer, "%s/%s", radius_dir, RADIUS_USERS);
  128.         if((userfd = fopen(buffer, "r")) == (FILE *)NULL) {
  129.             fprintf(stderr, "%s:Couldn't open %s for reading\n",
  130.                     progname, buffer);
  131.             exit(-1);
  132.         }
  133.     }
  134.  
  135.     mode = FIND_MODE_NAME;
  136.  
  137.     while(fgets(buffer, sizeof(buffer), userfd) != (char *)NULL) {
  138.         if(mode == FIND_MODE_NAME) {
  139.             /*
  140.              * Find the entry starting with the users name
  141.              */
  142.             if(*buffer != '#' && *buffer != '\t') {
  143.                 ptr = buffer;
  144.                 while(*ptr != ' ' && *ptr != '\t' &&
  145.                                 *ptr != '\0') {
  146.                     *name++ = *ptr++;
  147.                 }
  148.                 *name = '\0';
  149.                 if(*ptr == '\0') {
  150.                     continue;
  151.                 }
  152.                 ptr++;
  153.                 while(*ptr == ' ' || *ptr == '\t') {
  154.                     ptr++;
  155.                 }
  156.                 strcpy(content, ptr);
  157.                 content += strlen(content);
  158.                 mode = FIND_MODE_REPLY;
  159.             }
  160.         }
  161.         else {
  162.             if(*buffer == ' ' || *buffer == '\t') {
  163.                 ptr = buffer;
  164.                 while(*ptr == ' ' || *ptr == '\t') {
  165.                     ptr++;
  166.                 }
  167.                 strcpy(content, ptr);
  168.                 content += strlen(content);
  169.                 content -= 2;
  170.                 while(*content == ' ' || *content == '\t' ) {
  171.                     content--;
  172.                 }
  173.                 content++;
  174.                 *content = '\0';
  175.                 if(*(content - 1) != ',') {
  176.                     return(0);
  177.                 }
  178.             }
  179.             else {
  180.                 /* We are done */
  181.                 return(0);
  182.             }
  183.         }
  184.     }
  185.     fclose(userfd);
  186.     return(-1);
  187. }
  188.