1 /* Copyright (c) 2015, 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 <mysqld_error.h>
37 #include <mysql/client_plugin.h>
38 #include <mysql.h>
39 #include <stdio.h>
40 #include "common.h"
41 
42 extern int auth_client(char *principal_name,
43                        char *mech,
44                        MYSQL *mysql,
45                        MYSQL_PLUGIN_VIO *vio);
46 
parse_server_packet(char * packet,size_t packet_len,char * spn,char * mech)47 static void parse_server_packet(char *packet, size_t packet_len, char *spn, char *mech)
48 {
49   size_t spn_len;
50   spn_len = strnlen(packet, packet_len);
51   strncpy(spn, packet, PRINCIPAL_NAME_MAX);
52   if (spn_len == packet_len - 1)
53   {
54     /* Mechanism not included into packet */
55     *mech = 0;
56   }
57   else
58   {
59     strncpy(mech, packet + spn_len + 1, MECH_NAME_MAX);
60   }
61 }
62 
63 /**
64   Set client error message.
65  */
log_client_error(MYSQL * mysql,const char * format,...)66 void log_client_error(MYSQL *mysql,  const char *format, ...)
67 {
68   NET *net= &mysql->net;
69   va_list args;
70 
71   net->last_errno= ER_UNKNOWN_ERROR;
72   va_start(args, format);
73   vsnprintf(net->last_error, sizeof(net->last_error) - 1,
74           format, args);
75   va_end(args);
76   memcpy(net->sqlstate, "HY000", sizeof(net->sqlstate));
77 }
78 
79 /**
80   The main client function of the GSSAPI plugin.
81  */
gssapi_auth_client(MYSQL_PLUGIN_VIO * vio,MYSQL * mysql)82 static int gssapi_auth_client(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
83 {
84   int packet_len;
85   unsigned char *packet;
86   char spn[PRINCIPAL_NAME_MAX + 1];
87   char mech[MECH_NAME_MAX + 1];
88 
89   /* read from server for service principal name */
90   packet_len= vio->read_packet(vio, &packet);
91   if (packet_len < 0)
92   {
93     return CR_ERROR;
94   }
95   parse_server_packet((char *)packet, (size_t)packet_len, spn, mech);
96   return auth_client(spn, mech, mysql, vio);
97 }
98 
99 
100 /* register client plugin */
101 mysql_declare_client_plugin(AUTHENTICATION)
102   "auth_gssapi_client",
103   "Shuang Qiu, Robbie Harwood, Vladislav Vaintroub",
104   "GSSAPI/SSPI based authentication",
105   {0, 1, 0},
106   "BSD",
107   NULL,
108   NULL,
109   NULL,
110   NULL,
111   gssapi_auth_client
112 mysql_end_client_plugin;
113