OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / auth-rad.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for Authentication by RADIUS
4
5 Copyright (C) 2002 Opengate Project Team
6 Written by Yoshiaki Watanabe
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
24 /*
25   Thanks to programs and documentations refered.
26    The experimental implementation by Fuyuta Sato.
27    Manual files about RADIUS.
28 */
29
30 #include <radlib.h>
31 #include "opengatemmng.h"
32
33 /*****************************************/
34 /* Authenticate by RADIUS */
35 /*****************************************/
36 int authRadius(char *userid, char *passwd)
37 {
38   char* confFile;
39   int           authResult;
40   struct rad_handle *radh;
41   char hostname[ADDRMAXLN];
42
43   /* get radius config file path */
44   confFile=GetConfValue("AuthServer/ConfFile");
45
46   /* If not set, set default conf file path */
47   if(isNull(confFile)){
48     confFile=RADIUSCONF;
49   }
50
51   if( !(radh=rad_auth_open()) ){
52     err_msg("ERR at %s#%d: rad_auth_open",__FILE__,__LINE__);
53     return DENY;
54   }
55
56   if(gethostname(hostname,ADDRMAXLN) < 0){
57     err_msg("ERR at %s#%d: gethostname",__FILE__,__LINE__);
58     rad_close(radh);
59     return DENY;
60   }
61     
62   if(rad_config(radh,confFile) < 0){
63     err_msg("ERR at %s#%d: rad_config: %s",__FILE__,__LINE__,   
64             rad_strerror(radh));
65     rad_close(radh);
66     return DENY;
67   }
68
69   if(rad_create_request(radh, RAD_ACCESS_REQUEST) < 0){
70     err_msg("ERR at %s#%d: rad_ceate_request: %s",__FILE__,__LINE__,
71             rad_strerror(radh));
72     rad_close(radh);
73     return DENY;
74   }
75
76   if(rad_put_string(radh,RAD_USER_NAME,userid)<0){
77     err_msg("ERR at %s#%d: rad_put_string: %s",__FILE__,__LINE__,
78             rad_strerror(radh));
79     rad_close(radh);
80     return DENY;
81   }
82   if(rad_put_string(radh,RAD_USER_PASSWORD,passwd)<0){
83     err_msg("ERR at %s#%d: rad_put_string: %s",__FILE__,__LINE__,
84             rad_strerror(radh));
85     rad_close(radh);
86     return DENY;
87   }
88   if(rad_put_string(radh,RAD_NAS_IDENTIFIER,hostname)<0){
89     err_msg("ERR at %s#%d: rad_put_string: %s",__FILE__,__LINE__,
90             rad_strerror(radh));
91     rad_close(radh);
92     return DENY;
93   }
94   if(rad_put_int(radh,RAD_SERVICE_TYPE,RAD_LOGIN)<0){
95     err_msg("ERR at %s#%d: rad_put_int: %s",__FILE__,__LINE__,
96             rad_strerror(radh));
97     rad_close(radh);
98     return DENY;
99   }
100
101   switch(rad_send_request(radh)){
102   case RAD_ACCESS_ACCEPT:
103     authResult=ACCEPT;
104     break;
105   case -1:
106     err_msg("ERR at %s#%d: rad_send_request: %s",__FILE__,__LINE__,
107             rad_strerror(radh));
108     authResult=DENY;
109     break; 
110   case RAD_ACCESS_REJECT:
111   case RAD_ACCESS_CHALLENGE:
112   case RAD_ACCOUNTING_RESPONSE:
113   default:
114     authResult=DENY;
115     break;
116   }
117   rad_close(radh);
118   return authResult;
119 }
120   
121 int AuthRadius(char *userid, char *passwd)
122 {
123   int ret;
124
125   if(debug>1) err_msg("DEBUG:=>authRadius(%s,passwd)",userid);
126   ret=authRadius(userid,passwd);
127   if(debug>1) err_msg("DEBUG:(%d)<=authRadius( )",ret);
128
129   return ret;
130 }