1 /*
2    Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 
26 #include <ndb_global.h>
27 
28 #include <SocketClient.hpp>
29 #include <SocketAuthenticator.hpp>
30 #include <InputStream.hpp>
31 #include <OutputStream.hpp>
32 #include <NdbOut.hpp>
33 
SocketAuthSimple(const char * username,const char * passwd)34 SocketAuthSimple::SocketAuthSimple(const char *username, const char *passwd) {
35   if (username)
36     m_username= strdup(username);
37   else
38     m_username= 0;
39   if (passwd)
40     m_passwd= strdup(passwd);
41   else
42     m_passwd= 0;
43 }
44 
~SocketAuthSimple()45 SocketAuthSimple::~SocketAuthSimple()
46 {
47   if (m_passwd)
48     free((void*)m_passwd);
49   if (m_username)
50     free((void*)m_username);
51 }
52 
client_authenticate(NDB_SOCKET_TYPE sockfd)53 bool SocketAuthSimple::client_authenticate(NDB_SOCKET_TYPE sockfd)
54 {
55   SocketOutputStream s_output(sockfd);
56   SocketInputStream  s_input(sockfd);
57 
58   s_output.println("%s", m_username ? m_username : "");
59   s_output.println("%s", m_passwd ? m_passwd : "");
60 
61   char buf[16];
62   if (s_input.gets(buf, 16) == 0) return false;
63   if (strncmp("ok", buf, 2) == 0)
64     return true;
65 
66   return false;
67 }
68 
server_authenticate(NDB_SOCKET_TYPE sockfd)69 bool SocketAuthSimple::server_authenticate(NDB_SOCKET_TYPE sockfd)
70 {
71 
72   SocketOutputStream s_output(sockfd);
73   SocketInputStream  s_input(sockfd);
74 
75   char buf[256];
76 
77   if (s_input.gets(buf, 256) == 0) return false;
78   buf[255]= 0;
79   if (m_username)
80     free((void*)m_username);
81   m_username= strdup(buf);
82 
83   if (s_input.gets(buf, 256) == 0) return false;
84   buf[255]= 0;
85   if (m_passwd)
86     free((void*)m_passwd);
87   m_passwd= strdup(buf);
88 
89   s_output.println("ok");
90 
91   return true;
92 }
93