21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

MySQL<br />

By default, SSLsupport is disabled when you are building MySQL. To build MySQL<br />

with OpenSSLsupport enabled, you must specify the --with-vio and --with-openssl<br />

options on the command line to the configuration script. Once you have an SSLenabled<br />

version of MySQLbuilt, installed, and running, you can verify that SSLis<br />

supported with the following SQL command:<br />

SHOW VARIABLES LIKE 'have_openssl'<br />

If the result of the command is yes, SSL support is enabled.<br />

With an SSL-enabled version of MySQL running, you can use the GRANT command to<br />

designate SSLrequirements for accessing a particular database or table by user. Any<br />

client can specify that it wants to connect to the server using SSL, but with the GRANT<br />

options, it can be required.<br />

When writing code using the MySQLC API, use the following mysql_real_connect( )<br />

function to establish a connection to the server instead of using mysql_connect( ),<br />

which has been deprecated. All that is actually required to establish an SSLconnection<br />

from the client to the server is to specify the CLIENT_SSL flag to mysql_real_<br />

connect( ).<br />

#include <br />

#include <br />

#include <br />

#include <br />

int spc_mysql_real_connect(MYSQL *mysql, const char *host, const char *pw,<br />

const char *db, unsigned int flags) {<br />

int port = 0, result = 0;<br />

char *host_copy = 0, *p;<br />

const char *socket = 0, *user = 0;<br />

if (host) {<br />

if (!(host_copy = strdup(host))) return 0;<br />

if ((p = strchr(host_copy, '@')) != 0) {<br />

user = host_copy;<br />

*p++ = 0;<br />

host = p;<br />

}<br />

if ((p = strchr((p ? p : host_copy), ':')) != 0) {<br />

*p++ = 0;<br />

port = atoi(p);<br />

}<br />

if (*host = = '/') {<br />

socket = host;<br />

host = 0;<br />

}<br />

}<br />

/* this bit of magic is all it takes to enable SSL connections */<br />

flags |= CLIENT_SSL;<br />

488 | Chapter 9: Networking<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!