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 #ifdef PLUGIN_GSSAPI
94 /* system variable */
95 static MYSQL_SYSVAR_STR(keytab_path, srv_keytab_path,
96                         PLUGIN_VAR_RQCMDARG|PLUGIN_VAR_READONLY,
97                         "Keytab file path for Kerberos authentication",
98                         NULL,
99                         NULL,
100                         "");
101 #endif
102 
103 static MYSQL_SYSVAR_STR(principal_name, srv_principal_name,
104                         PLUGIN_VAR_RQCMDARG|PLUGIN_VAR_READONLY,
105                         "GSSAPI target name - service principal name for Kerberos authentication.",
106                         NULL,
107                         NULL,
108                         "");
109 #ifdef PLUGIN_SSPI
110 static const char* mech_names[] = {
111   "Kerberos",
112   "Negotiate",
113   "",
114   NULL
115 };
116 static TYPELIB mech_name_typelib = {
117   3,
118   "mech_name_typelib",
119   mech_names,
120   NULL
121 };
122 static MYSQL_SYSVAR_ENUM(mech_name, srv_mech,
123                         PLUGIN_VAR_RQCMDARG|PLUGIN_VAR_READONLY,
124                         "GSSAPI mechanism",
125                         NULL,
126                         NULL,
127                         2,&mech_name_typelib);
128 #endif
129 
130 static struct st_mysql_sys_var *system_variables[]= {
131   MYSQL_SYSVAR(principal_name),
132 #ifdef PLUGIN_SSPI
133   MYSQL_SYSVAR(mech_name),
134 #endif
135 #ifdef PLUGIN_GSSAPI
136   MYSQL_SYSVAR(keytab_path),
137 #endif
138   NULL
139 };
140 
141 /* Register authentication plugin */
142 static struct st_mysql_auth server_handler= {
143   MYSQL_AUTHENTICATION_INTERFACE_VERSION,
144   "auth_gssapi_client",
145   gssapi_auth, NULL, NULL
146 };
147 
maria_declare_plugin(gssapi_server)148 maria_declare_plugin(gssapi_server)
149 {
150   MYSQL_AUTHENTICATION_PLUGIN,
151   &server_handler,
152   "gssapi",
153   "Shuang Qiu, Robbie Harwood, Vladislav Vaintroub",
154   "Plugin for GSSAPI/SSPI based authentication.",
155   PLUGIN_LICENSE_BSD,
156   initialize_plugin,
157   deinitialize_plugin,                   /* destructor */
158   0x0100,                                /* version */
159   NULL,                                  /* status variables */
160   system_variables,                      /* system variables */
161   "1.0",
162   MariaDB_PLUGIN_MATURITY_STABLE
163 }
164 maria_declare_plugin_end;
165 
166