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