1 /* Copyright (c) 2015-2016, Shuang Qiu, Robbie Harwood,
2 Vladislav Vaintroub & MariaDB Corporation
3 
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 /**
30   @file
31 
32   GSSAPI authentication plugin, client side
33 */
34 #include <string.h>
35 #include <stdarg.h>
36 #include <ma_global.h>
37 #include <mysql.h>
38 #include <ma_server_error.h>
39 #include <mysql/client_plugin.h>
40 #include <mysql.h>
41 #include <stdio.h>
42 #include "common.h"
43 
44 extern int auth_client(char *principal_name,
45                        char *mech,
46                        MYSQL *mysql,
47                        MYSQL_PLUGIN_VIO *vio);
48 
parse_server_packet(char * packet,size_t packet_len,char * spn,char * mech)49 static void parse_server_packet(char *packet, size_t packet_len, char *spn, char *mech)
50 {
51   size_t spn_len;
52   spn_len = strnlen(packet, packet_len);
53   strncpy(spn, packet, PRINCIPAL_NAME_MAX);
54   if (spn_len == packet_len - 1)
55   {
56     /* Mechanism not included into packet */
57     *mech = 0;
58   }
59   else
60   {
61     strncpy(mech, packet + spn_len + 1, MECH_NAME_MAX);
62   }
63 }
64 
65 /**
66   Set client error message.
67  */
log_client_error(MYSQL * mysql,const char * format,...)68 void log_client_error(MYSQL *mysql,  const char *format, ...)
69 {
70   NET *net= &mysql->net;
71   va_list args;
72 
73   net->last_errno= ER_UNKNOWN_ERROR;
74   va_start(args, format);
75   vsnprintf(net->last_error, sizeof(net->last_error) - 1,
76           format, args);
77   va_end(args);
78   memcpy(net->sqlstate, "HY000", sizeof(net->sqlstate));
79 }
80 
81 /**
82   The main client function of the GSSAPI plugin.
83  */
gssapi_auth_client(MYSQL_PLUGIN_VIO * vio,MYSQL * mysql)84 static int gssapi_auth_client(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
85 {
86   int packet_len;
87   unsigned char *packet;
88   char spn[PRINCIPAL_NAME_MAX + 1];
89   char mech[MECH_NAME_MAX + 1];
90 
91   /* read from server for service principal name */
92   packet_len= vio->read_packet(vio, &packet);
93   if (packet_len < 0)
94   {
95     return CR_ERROR;
96   }
97   parse_server_packet((char *)packet, (size_t)packet_len, spn, mech);
98   return auth_client(spn, mech, mysql, vio);
99 }
100 
101 
102 /* register client plugin */
103 #ifndef PLUGIN_DYNAMIC
104 struct st_mysql_client_plugin_AUTHENTICATION auth_gssapi_client_client_plugin=
105 #else
106 struct st_mysql_client_plugin_AUTHENTICATION _mysql_client_plugin_declaration_ =
107 #endif
108 {
109   MYSQL_CLIENT_AUTHENTICATION_PLUGIN,
110   MYSQL_CLIENT_AUTHENTICATION_PLUGIN_INTERFACE_VERSION,
111   "auth_gssapi_client",
112   "Shuang Qiu, Robbie Harwood, Vladislav Vaintroub, Georg Richter",
113   "GSSAPI/SSPI based authentication",
114   {0, 1, 0},
115   "BSD",
116   NULL,
117   NULL,
118   NULL,
119   NULL,
120   gssapi_auth_client
121 };
122