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