MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SocketAuthenticator.cpp
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 as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 
19 #include <ndb_global.h>
20 
21 #include <SocketClient.hpp>
22 #include <SocketAuthenticator.hpp>
23 #include <InputStream.hpp>
24 #include <OutputStream.hpp>
25 #include <NdbOut.hpp>
26 
27 SocketAuthSimple::SocketAuthSimple(const char *username, const char *passwd) {
28  if (username)
29  m_username= strdup(username);
30  else
31  m_username= 0;
32  if (passwd)
33  m_passwd= strdup(passwd);
34  else
35  m_passwd= 0;
36 }
37 
38 SocketAuthSimple::~SocketAuthSimple()
39 {
40  if (m_passwd)
41  free((void*)m_passwd);
42  if (m_username)
43  free((void*)m_username);
44 }
45 
46 bool SocketAuthSimple::client_authenticate(NDB_SOCKET_TYPE sockfd)
47 {
48  SocketOutputStream s_output(sockfd);
49  SocketInputStream s_input(sockfd);
50 
51  s_output.println("%s", m_username ? m_username : "");
52  s_output.println("%s", m_passwd ? m_passwd : "");
53 
54  char buf[16];
55  if (s_input.gets(buf, 16) == 0) return false;
56  if (strncmp("ok", buf, 2) == 0)
57  return true;
58 
59  return false;
60 }
61 
62 bool SocketAuthSimple::server_authenticate(NDB_SOCKET_TYPE sockfd)
63 {
64 
65  SocketOutputStream s_output(sockfd);
66  SocketInputStream s_input(sockfd);
67 
68  char buf[256];
69 
70  if (s_input.gets(buf, 256) == 0) return false;
71  buf[255]= 0;
72  if (m_username)
73  free((void*)m_username);
74  m_username= strdup(buf);
75 
76  if (s_input.gets(buf, 256) == 0) return false;
77  buf[255]= 0;
78  if (m_passwd)
79  free((void*)m_passwd);
80  m_passwd= strdup(buf);
81 
82  s_output.println("ok");
83 
84  return true;
85 }