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 #include <gssapi/gssapi.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <mysql/plugin_auth.h>
33 #include <mysqld_error.h>
34 #include <mysql.h>
35 #include "gssapi_errmsg.h"
36 
37 extern void log_client_error(MYSQL *mysql,const char *fmt,...);
38 
39 
40 /* This sends the error to the client */
log_error(MYSQL * mysql,OM_uint32 major,OM_uint32 minor,const char * msg)41 static void log_error(MYSQL *mysql, OM_uint32 major, OM_uint32 minor, const char *msg)
42 {
43   if (GSS_ERROR(major))
44   {
45     char sysmsg[1024];
46     gssapi_errmsg(major, minor, sysmsg, sizeof(sysmsg));
47     log_client_error(mysql,
48       "Client GSSAPI error (major %u, minor %u) : %s - %s",
49        major, minor, msg, sysmsg);
50   }
51   else
52   {
53     log_client_error(mysql, "Client GSSAPI error : %s", msg);
54   }
55 }
56 
auth_client(char * principal_name,char * mech,MYSQL * mysql,MYSQL_PLUGIN_VIO * vio)57 int auth_client(char *principal_name, char *mech, MYSQL *mysql, MYSQL_PLUGIN_VIO *vio)
58 {
59 
60   int ret= CR_ERROR;
61   OM_uint32 major= 0, minor= 0;
62   gss_ctx_id_t ctxt= GSS_C_NO_CONTEXT;
63   gss_name_t service_name= GSS_C_NO_NAME;
64 
65   if (principal_name && principal_name[0])
66   {
67     /* import principal from plain text */
68     gss_buffer_desc principal_name_buf;
69     principal_name_buf.length= strlen(principal_name);
70     principal_name_buf.value= (void *) principal_name;
71     major= gss_import_name(&minor, &principal_name_buf, GSS_C_NT_USER_NAME, &service_name);
72     if (GSS_ERROR(major))
73     {
74       log_error(mysql, major, minor, "gss_import_name");
75       return CR_ERROR;
76     }
77   }
78 
79   gss_buffer_desc input= {0,0};
80   do
81   {
82     gss_buffer_desc output= {0,0};
83     major= gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ctxt, service_name,
84                                 GSS_C_NO_OID, 0, 0, GSS_C_NO_CHANNEL_BINDINGS,
85                                 &input, NULL, &output, NULL, NULL);
86     if (output.length)
87     {
88       /* send credential */
89       if(vio->write_packet(vio, (unsigned char *)output.value, output.length))
90       {
91         /* Server error packet contains detailed message. */
92         ret= CR_OK_HANDSHAKE_COMPLETE;
93         gss_release_buffer (&minor, &output);
94         goto cleanup;
95       }
96     }
97     gss_release_buffer (&minor, &output);
98 
99     if (GSS_ERROR(major))
100     {
101        log_error(mysql, major, minor,"gss_init_sec_context");
102        goto cleanup;
103     }
104 
105     if (major & GSS_S_CONTINUE_NEEDED)
106     {
107       int len= vio->read_packet(vio, (unsigned char **) &input.value);
108       if (len <= 0)
109       {
110         /* Server error packet contains detailed message. */
111         ret= CR_OK_HANDSHAKE_COMPLETE;
112         goto cleanup;
113       }
114       input.length= len;
115     }
116   } while (major & GSS_S_CONTINUE_NEEDED);
117 
118   ret= CR_OK;
119 
120 cleanup:
121   if (service_name != GSS_C_NO_NAME)
122     gss_release_name(&minor, &service_name);
123   if (ctxt != GSS_C_NO_CONTEXT)
124     gss_delete_sec_context(&minor, &ctxt, GSS_C_NO_BUFFER);
125 
126   return ret;
127 }
128