1 /* Copyright (c) 2001, 2010, 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 as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
15 
16 #include <my_global.h>
17 #include <mysql.h>
18 #include <mysqld_error.h>
19 #include <my_pthread.h>
20 #include <my_sys.h>
21 #include <mysys_err.h>
22 #include <m_string.h>
23 #include <m_ctype.h>
24 #include "errmsg.h"
25 #include <violite.h>
26 #include <sys/stat.h>
27 #include <signal.h>
28 #include <time.h>
29 #include <sql_common.h>
30 #include "embedded_priv.h"
31 #include "client_settings.h"
32 #ifdef	 HAVE_PWD_H
33 #include <pwd.h>
34 #endif
35 #if !defined(__WIN__)
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <netdb.h>
40 #ifdef HAVE_SELECT_H
41 #  include <select.h>
42 #endif
43 #ifdef HAVE_SYS_SELECT_H
44 #include <sys/select.h>
45 #endif
46 #endif
47 #ifdef HAVE_SYS_UN_H
48 #  include <sys/un.h>
49 #endif
50 #ifndef INADDR_NONE
51 #define INADDR_NONE	-1
52 #endif
53 
54 extern ulong net_buffer_length;
55 extern ulong max_allowed_packet;
56 
57 #if defined(__WIN__)
58 #define ERRNO WSAGetLastError()
59 #define perror(A)
60 #else
61 #include <errno.h>
62 #define ERRNO errno
63 #define SOCKET_ERROR -1
64 #define closesocket(A) close(A)
65 #endif
66 
67 #ifdef HAVE_GETPWUID
68 struct passwd *getpwuid(uid_t);
69 char* getlogin(void);
70 #endif
71 
72 #ifdef __WIN__
is_NT(void)73 static my_bool is_NT(void)
74 {
75   char *os=getenv("OS");
76   return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
77 }
78 #endif
79 
80 int mysql_init_character_set(MYSQL *mysql);
81 
82 MYSQL * STDCALL
mysql_real_connect(MYSQL * mysql,const char * host,const char * user,const char * passwd,const char * db,uint port,const char * unix_socket,ulong client_flag)83 mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
84 		   const char *passwd, const char *db,
85 		   uint port, const char *unix_socket,ulong client_flag)
86 {
87   char name_buff[USERNAME_LENGTH];
88 
89   DBUG_ENTER("mysql_real_connect");
90   DBUG_PRINT("enter",("host: %s  db: %s  user: %s (libmysqld)",
91 		      host ? host : "(Null)",
92 		      db ? db : "(Null)",
93 		      user ? user : "(Null)"));
94 
95   /* Test whether we're already connected */
96   if (mysql->server_version)
97   {
98     set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
99     DBUG_RETURN(0);
100   }
101 
102   if (!host || !host[0])
103     host= mysql->options.host;
104 
105   if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
106       (mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
107        host && *host && strcmp(host,LOCAL_HOST)))
108     DBUG_RETURN(cli_mysql_real_connect(mysql, host, user,
109 				       passwd, db, port,
110 				       unix_socket, client_flag));
111 
112   mysql->methods= &embedded_methods;
113 
114   /* use default options */
115   if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
116   {
117     mysql_read_default_options(&mysql->options,
118 			       (mysql->options.my_cnf_file ?
119 				mysql->options.my_cnf_file : "my"),
120 			       mysql->options.my_cnf_group);
121     my_free(mysql->options.my_cnf_file);
122     my_free(mysql->options.my_cnf_group);
123     mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
124 
125     if (mysql->options.protocol == UINT_MAX32)
126       goto error;
127   }
128 
129   if (!db || !db[0])
130     db=mysql->options.db;
131 
132   if (!user || !user[0])
133     user=mysql->options.user;
134 
135 #ifndef NO_EMBEDDED_ACCESS_CHECKS
136   if (!passwd)
137   {
138     passwd=mysql->options.password;
139 #if !defined(DONT_USE_MYSQL_PWD)
140     if (!passwd)
141       passwd=getenv("MYSQL_PWD");		/* get it from environment */
142 #endif
143   }
144   mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
145 #endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
146   if (!user || !user[0])
147   {
148     read_user_name(name_buff);
149     if (name_buff[0])
150       user= name_buff;
151   }
152 
153   if (!user)
154     user= "";
155    /*
156       We need to alloc some space for mysql->info but don't want to
157       put extra 'my_free's in mysql_close.
158       So we alloc it with the 'user' string to be freed at once
159    */
160   mysql->user= my_strdup(user, MYF(0));
161 
162   port=0;
163   unix_socket=0;
164 
165   client_flag|=mysql->options.client_flag;
166   /* Send client information for access check */
167   client_flag|=CLIENT_CAPABILITIES;
168   if (client_flag & CLIENT_MULTI_STATEMENTS)
169     client_flag|= CLIENT_MULTI_RESULTS;
170   /*
171     no compression in embedded as we don't send any data,
172     and no pluggable auth, as we cannot do a client-server dialog
173   */
174   client_flag&= ~(CLIENT_COMPRESS | CLIENT_PLUGIN_AUTH);
175   if (db)
176     client_flag|=CLIENT_CONNECT_WITH_DB;
177 
178   mysql->info_buffer= my_malloc(MYSQL_ERRMSG_SIZE, MYF(0));
179   mysql->thd= create_embedded_thd(client_flag);
180 
181   init_embedded_mysql(mysql, client_flag);
182 
183   if (mysql_init_character_set(mysql))
184     goto error;
185 
186   if (check_embedded_connection(mysql, db))
187     goto error;
188 
189   mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
190 
191   if (mysql->options.init_commands)
192   {
193     DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
194     char **ptr= (char**)init_commands->buffer;
195     char **end= ptr + init_commands->elements;
196 
197     for (; ptr<end; ptr++)
198     {
199       MYSQL_RES *res;
200       if (mysql_query(mysql,*ptr))
201 	goto error;
202       if (mysql->fields)
203       {
204 	if (!(res= (*mysql->methods->use_result)(mysql)))
205 	  goto error;
206 	mysql_free_result(res);
207       }
208     }
209   }
210 
211   DBUG_PRINT("exit",("Mysql handler: %p", mysql));
212   DBUG_RETURN(mysql);
213 
214 error:
215   DBUG_PRINT("error",("message: %u (%s)",
216                       mysql->net.last_errno,
217                       mysql->net.last_error));
218   {
219     /* Free alloced memory */
220     my_bool free_me=mysql->free_me;
221     free_old_query(mysql);
222     mysql->free_me=0;
223     mysql_close(mysql);
224     mysql->free_me=free_me;
225   }
226   DBUG_RETURN(0);
227 }
228 
229