OSDN Git Service

modified code to prevent DB inflation
[opengatem/opengatem.git] / mngsrc / auth-pop3s.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for Authentication by POP3S
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    Sample client application cli.cpp found in the OpenSSL site 
27     (developed by Sampo Kellomaki and simplified by Wade Scholine) 
28    Apache module mod_auth_pam.c by Ingo Luetkebohle
29 */
30
31 #include "opengatemmng.h"
32
33 /*************************/
34 /* Authenticate by POP3S */
35 /*************************/
36 int authPop3s(char *userid, char *passwd)
37 {
38   char* serverAddr;
39   char* port;
40   int           sockfd, n;
41   char          recvline[BUFFMAXLN];
42   int           authResult;
43   SSL_CTX    *ctx;
44   SSL        *ssl;
45   const SSL_METHOD *meth;
46
47   /* get auth server address */
48   serverAddr=GetConfValue("AuthServer/Address");
49
50   if(isNull(serverAddr)){
51     err_msg("ERR at %s#%d: Missing address for POP3s server in config",
52             __FILE__,__LINE__);
53     return DENY;
54   }
55
56   /* get auth server port */
57   port=GetConfValue("AuthServer/Port");
58
59   /* POP3S server connect */
60   if(isNull(port)){
61     sockfd = Tcp_connect(serverAddr, "pop3s");
62   }else{
63     sockfd = Tcp_connect(serverAddr, port);
64   }
65   if(sockfd<0){
66     err_msg("ERR at %s#%d: Pop3s server is not normal 0",__FILE__,__LINE__);
67     return DENY;
68   }
69
70   /* prepare SSL */
71   SSLeay_add_ssl_algorithms();
72   meth = SSLv23_client_method();
73   SSL_load_error_strings();
74   ctx = SSL_CTX_new (meth);
75   if( ctx == NULL ){
76     err_msg("ERR at %s#%d: SSL_CTX_new returns NULL",__FILE__,__LINE__);
77     return DENY;
78   }
79   
80   /* ----------------------------------------------- */
81   /* start SSL negotiation. */
82   
83   ssl = SSL_new (ctx);
84   if( ssl == NULL ){
85     err_msg("ERR at %s#%d: SSL_new returns NULL",__FILE__,__LINE__);
86     return DENY;
87   }
88
89   SSL_set_fd (ssl, sockfd);
90   if( SSL_connect (ssl) == -1 ){
91     err_msg("ERR at %s#%d: SSL_connect returns error",__FILE__,__LINE__);
92     return DENY;
93   }
94
95   /* --------------------------------------------------- */
96   /* DATA EXCHANGE - Send a message and receive a reply. */
97   /*  pop3 message exchange */
98
99   /* get [+OK POP3 <host> <ver> server ready]*/
100   if((n = readlnSSL(ssl, recvline, BUFFMAXLN)) < 0) {
101     err_msg("ERR at %s#%d: Pop3s server is not normal 1",__FILE__,__LINE__);
102     authResult=DENY;
103     goto EXITPOINT;
104   }
105
106   if(strstr(recvline,"+OK")!=recvline){
107     err_msg("ERR at %s#%d: Pop3s server is not normal 2",__FILE__,__LINE__);
108     authResult=DENY;
109     goto EXITPOINT;
110   }
111
112   /* put [user <userid>] */
113   WritefmtSSL(ssl, "user %s\r\n", userid);
114
115   /* get [+OK User name accepted, password please] */
116   if((n = readlnSSL(ssl, recvline, BUFFMAXLN)) < 0) {
117     err_msg("ERR at %s#%d: Pop3s server is not normal 3",__FILE__,__LINE__);
118     authResult=DENY;
119     goto EXITPOINT;
120   }
121
122   if(strstr(recvline,"+OK")!=recvline){
123     err_msg("ERR at %s#%d: Pop3s server is not normal 4",__FILE__,__LINE__);
124     authResult=DENY;
125     goto EXITPOINT;
126   }
127
128   /* put [pass <password>] */
129   WritefmtSSL(ssl, "pass %s\r\n", passwd);
130
131   /* get [+OK Mailbox open, <count> messages] */
132   if((n = readlnSSL(ssl, recvline, BUFFMAXLN)) < 0) {
133     err_msg("ERR at %s#%d: Pop3s server is not normal 5",__FILE__,__LINE__);
134     authResult=DENY;
135     goto EXITPOINT;
136   }
137
138   if(strstr(recvline,"+OK")==recvline){
139     authResult=ACCEPT;
140   }else{
141     authResult=DENY;
142   }
143   
144   /* put [quit] */
145   WritefmtSSL(ssl,"quit\r\n");
146
147 EXITPOINT:
148   SSL_shutdown (ssl);  /* send SSL/TLS close_notify */
149
150   /* Clean up. */
151   Close(sockfd);
152   SSL_free(ssl);
153   SSL_CTX_free(ctx);
154
155   return authResult;
156 }
157
158 /*****************************************/
159 /* function for debugging                */
160 /*****************************************/
161 int AuthPop3s(char *userid, char *passwd)
162 {
163   int ret;
164
165   if(debug>1) err_msg("DEBUG:=>authPop3s(%s,passwd)",userid);
166   ret=authPop3s(userid,passwd);
167   if(debug>1) err_msg("DEBUG:(%d)<=authPop3s( )",ret);
168
169   return ret;
170 }