1 /*  Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
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 #define _GNU_SOURCE /* for struct ucred */
33 
34 #include <mysql/plugin_auth.h>
35 #include <sys/socket.h>
36 #include <pwd.h>
37 #include <string.h>
38 
socket_auth(MYSQL_PLUGIN_VIO * vio,MYSQL_SERVER_AUTH_INFO * info)39 static int socket_auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
40 {
41   unsigned char *pkt;
42   MYSQL_PLUGIN_VIO_INFO vio_info;
43   struct ucred cred;
44   socklen_t cred_len= sizeof(cred);
45   struct passwd pwd_buf, *pwd;
46   char buf[1024];
47 
48   /* no user name yet ? read the client handshake packet with the user name */
49   if (info->user_name == 0)
50   {
51     if (vio->read_packet(vio, &pkt) < 0)
52       return CR_ERROR;
53   }
54 
55   info->password_used= PASSWORD_USED_NO_MENTION;
56 
57   vio->info(vio, &vio_info);
58   if (vio_info.protocol != MYSQL_VIO_SOCKET)
59     return CR_ERROR;
60 
61   /* get the UID of the client process */
62   if (getsockopt(vio_info.socket, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len))
63     return CR_ERROR;
64 
65   if (cred_len != sizeof(cred))
66     return CR_ERROR;
67 
68   /* and find the username for this uid */
69   getpwuid_r(cred.uid, &pwd_buf, buf, sizeof(buf), &pwd);
70   if (pwd == NULL)
71     return CR_ERROR;
72 
73   /* now it's simple as that */
74   return strcmp(pwd->pw_name, info->user_name) ? CR_ERROR : CR_OK;
75 }
76 
77 static struct st_mysql_auth socket_auth_handler=
78 {
79   MYSQL_AUTHENTICATION_INTERFACE_VERSION,
80   0,
81   socket_auth
82 };
83 
mysql_declare_plugin(socket_auth)84 mysql_declare_plugin(socket_auth)
85 {
86   MYSQL_AUTHENTICATION_PLUGIN,
87   &socket_auth_handler,
88   "auth_socket",
89   "Sergei Golubchik",
90   "Unix Socket based authentication",
91   PLUGIN_LICENSE_GPL,
92   NULL,
93   NULL,
94   0x0100,
95   NULL,
96   NULL,
97   NULL,
98   0,
99 }
100 mysql_declare_plugin_end;
101 
102