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, server side
33 */
34 
35 #ifdef _WIN32
36 typedef unsigned __int64 my_ulonglong;
37 #else
38 typedef unsigned long long my_ulonglong;
39 #endif
40 
41 #include <stdlib.h>
42 #include <mysqld_error.h>
43 #include <typelib.h>
44 #include <mysql/plugin_auth.h>
45 #include "string.h"
46 #include "server_plugin.h"
47 #include "common.h"
48 
49 /* First packet sent from server to client, contains srv_principal_name\0mech\0 */
50 static char first_packet[PRINCIPAL_NAME_MAX + MECH_NAME_MAX +2];
51 static int  first_packet_len;
52 
53 /*
54  Target name in GSSAPI/SSPI , for Kerberos it is service principal name
55  (often user principal name of the server user will work)
56 */
57 char *srv_principal_name;
58 char *srv_keytab_path;
59 const char *srv_mech_name="";
60 unsigned long srv_mech;
61 
62 /**
63   The main server function of the GSSAPI plugin.
64  */
gssapi_auth(MYSQL_PLUGIN_VIO * vio,MYSQL_SERVER_AUTH_INFO * auth_info)65 static int gssapi_auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *auth_info)
66 {
67   /* Send first packet with target name and mech name */
68   if (vio->write_packet(vio, (unsigned char *)first_packet, first_packet_len))
69     return CR_ERROR;
70 
71   return auth_server(vio, auth_info);
72 }
73 
initialize_plugin(void * unused)74 static int initialize_plugin(void *unused)
75 {
76   int rc;
77   rc = plugin_init();
78   if (rc)
79     return rc;
80 
81   strcpy(first_packet, srv_principal_name);
82   strcpy(first_packet + strlen(srv_principal_name) + 1,srv_mech_name);
83   first_packet_len = (int)(strlen(srv_principal_name) + strlen(srv_mech_name) + 2);
84 
85   return 0;
86 }
87 
deinitialize_plugin(void * unused)88 static int deinitialize_plugin(void *unused)
89 {
90   return plugin_deinit();
91 }
92 
93 /* system variable */
94 static MYSQL_SYSVAR_STR(keytab_path, srv_keytab_path,
95                         PLUGIN_VAR_RQCMDARG|PLUGIN_VAR_READONLY,
96                         "Keytab file path for Kerberos authentication",
97                         NULL,
98                         NULL,
99                         "");
100 static MYSQL_SYSVAR_STR(principal_name, srv_principal_name,
101                         PLUGIN_VAR_RQCMDARG|PLUGIN_VAR_READONLY,
102                         "GSSAPI target name - service principal name for Kerberos authentication.",
103                         NULL,
104                         NULL,
105                         "");
106 #ifdef PLUGIN_SSPI
107 static const char* mech_names[] = {
108   "Kerberos",
109   "Negotiate",
110   "",
111   NULL
112 };
113 static TYPELIB mech_name_typelib = {
114   3,
115   "mech_name_typelib",
116   mech_names,
117   NULL
118 };
119 static MYSQL_SYSVAR_ENUM(mech_name, srv_mech,
120                         PLUGIN_VAR_RQCMDARG|PLUGIN_VAR_READONLY,
121                         "GSSAPI mechanism",
122                         NULL,
123                         NULL,
124                         2,&mech_name_typelib);
125 #endif
126 
127 static struct st_mysql_sys_var *system_variables[]= {
128   MYSQL_SYSVAR(principal_name),
129 #ifdef PLUGIN_SSPI
130   MYSQL_SYSVAR(mech_name),
131 #endif
132 #ifdef PLUGIN_GSSAPI
133   MYSQL_SYSVAR(keytab_path),
134 #endif
135   NULL
136 };
137 
138 /* Register authentication plugin */
139 static struct st_mysql_auth server_handler= {
140   MYSQL_AUTHENTICATION_INTERFACE_VERSION,
141   "auth_gssapi_client",
142   gssapi_auth, NULL, NULL
143 };
144 
maria_declare_plugin(gssapi_server)145 maria_declare_plugin(gssapi_server)
146 {
147   MYSQL_AUTHENTICATION_PLUGIN,
148   &server_handler,
149   "gssapi",
150   "Shuang Qiu, Robbie Harwood, Vladislav Vaintroub",
151   "Plugin for GSSAPI/SSPI based authentication.",
152   PLUGIN_LICENSE_BSD,
153   initialize_plugin,
154   deinitialize_plugin,                   /* destructor */
155   0x0100,                                /* version */
156   NULL,                                  /* status variables */
157   system_variables,                      /* system variables */
158   "1.0",
159   MariaDB_PLUGIN_MATURITY_STABLE
160 }
161 maria_declare_plugin_end;
162 
163