1 /* Copyright (c) 2010, 2021, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 /**
24 @file
25
26 auth_socket authentication plugin.
27
28 Authentication is successful if the connection is done via a unix socket and
29 the owner of the client process matches the user name that was used when
30 connecting to mysqld.
31 */
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE /* for struct ucred */
34 #endif
35
36 #include <mysql/plugin_auth.h>
37 #include <sys/socket.h>
38 #include <pwd.h>
39 #include <string.h>
40
socket_auth(MYSQL_PLUGIN_VIO * vio,MYSQL_SERVER_AUTH_INFO * info)41 static int socket_auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
42 {
43 unsigned char *pkt;
44 MYSQL_PLUGIN_VIO_INFO vio_info;
45 struct ucred cred;
46 socklen_t cred_len= sizeof(cred);
47 struct passwd pwd_buf, *pwd;
48 char buf[1024];
49
50 /* no user name yet ? read the client handshake packet with the user name */
51 if (info->user_name == 0)
52 {
53 if (vio->read_packet(vio, &pkt) < 0)
54 return CR_ERROR;
55 }
56
57 info->password_used= PASSWORD_USED_NO_MENTION;
58
59 vio->info(vio, &vio_info);
60 if (vio_info.protocol != MYSQL_VIO_SOCKET)
61 return CR_ERROR;
62
63 /* get the UID of the client process */
64 if (getsockopt(vio_info.socket, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len))
65 return CR_ERROR;
66
67 if (cred_len != sizeof(cred))
68 return CR_ERROR;
69
70 /* and find the socket user name for this uid */
71 getpwuid_r(cred.uid, &pwd_buf, buf, sizeof(buf), &pwd);
72 if (pwd == NULL)
73 return CR_ERROR;
74
75 /* fill in the external user name used */
76 strncpy(info->external_user, pwd->pw_name, sizeof(info->external_user) - 1);
77 info->external_user[sizeof(info->external_user) - 1]= 0;
78
79 if (!strcmp(pwd->pw_name, info->user_name) ||
80 !strcmp(pwd->pw_name, info->auth_string))
81 return CR_OK;
82 else
83 return CR_ERROR;
84 }
85
generate_auth_string_hash(char * outbuf MY_ATTRIBUTE ((unused)),unsigned int * buflen,const char * inbuf MY_ATTRIBUTE ((unused)),unsigned int inbuflen MY_ATTRIBUTE ((unused)))86 int generate_auth_string_hash(char *outbuf MY_ATTRIBUTE((unused)),
87 unsigned int *buflen,
88 const char *inbuf MY_ATTRIBUTE((unused)),
89 unsigned int inbuflen MY_ATTRIBUTE((unused)))
90 {
91 *buflen= 0;
92 return 0;
93 }
94
validate_auth_string_hash(char * const inbuf MY_ATTRIBUTE ((unused)),unsigned int buflen MY_ATTRIBUTE ((unused)))95 int validate_auth_string_hash(char* const inbuf MY_ATTRIBUTE((unused)),
96 unsigned int buflen MY_ATTRIBUTE((unused)))
97 {
98 return 0;
99 }
100
set_salt(const char * password MY_ATTRIBUTE ((unused)),unsigned int password_len MY_ATTRIBUTE ((unused)),unsigned char * salt MY_ATTRIBUTE ((unused)),unsigned char * salt_len)101 int set_salt(const char* password MY_ATTRIBUTE((unused)),
102 unsigned int password_len MY_ATTRIBUTE((unused)),
103 unsigned char* salt MY_ATTRIBUTE((unused)),
104 unsigned char* salt_len)
105 {
106 *salt_len= 0;
107 return 0;
108 }
109
110 static struct st_mysql_auth socket_auth_handler=
111 {
112 MYSQL_AUTHENTICATION_INTERFACE_VERSION,
113 0,
114 socket_auth,
115 generate_auth_string_hash,
116 validate_auth_string_hash,
117 set_salt,
118 AUTH_FLAG_PRIVILEGED_USER_FOR_PASSWORD_CHANGE
119 };
120
mysql_declare_plugin(socket_auth)121 mysql_declare_plugin(socket_auth)
122 {
123 MYSQL_AUTHENTICATION_PLUGIN,
124 &socket_auth_handler,
125 "auth_socket",
126 "Sergei Golubchik",
127 "Unix Socket based authentication",
128 PLUGIN_LICENSE_GPL,
129 NULL,
130 NULL,
131 0x0101,
132 NULL,
133 NULL,
134 NULL,
135 0,
136 }
137 mysql_declare_plugin_end;
138
139