Home

Setting up sybase-ct with encrypted password connections on Linux using ASE 15

Print this articlePrint this article

First off, I recommend uninstalling FreeTDS unless it is needed for SQL Server connections. It is a great TDS implementation, but they haven't reverse engineered the Sybase encryption yet. Leaving it installed can add complications. Though it appears that as long as you specify the appname parameter in sybase_connect it will work fine.  Having both installed wasn't an issue with PHP 4.4.9, but was with PHP 5.2.9.

This setup has been tested on: PHP 5.2.9 running on Red Hat Enterprise Linux Server release 5.3 and PHP 4.4.9 running on Red Hat Enterprise Linux AS release 3. You'll need to recompile PHP and make slight code changes to the sybase-ct extension, so be sure to have the PHP source on hand.

Installing Sybase Open Client:
1) Download Sybase ASE Express for Linux from http://www.sybase.com/linux/ase. You'll need to register, but it is free and includes all the drivers you need.

2) Install Sybase - just follow their directions, they are fairly straight forward. You don't need to install the server, but you do need the Open Client, and isql and other tools are pretty useful.

3) I setup sybase in /opt/sybase-15 and make a symlink to /opt/sybase. You must setup the /opt/sybase/interfaces file and the /opt/sybase/OCS-15_0/config/ocs.cfg files. The settings I used for testing are below. NOTE: these are not the settings that should be used in production - if you don't know which servers to use contact sybadmin_at_columbia.edu.

> cat /opt/sybase/interfaces 

# define the servers we connect to...

servername
query tcp ether right.server.columbia.edu 4101
master tcp ether right.server.columbia.edu 4101
NOTE: with the ocs.cfg file you must have a newline at the end.  For more about the ocs.cfg file see: http://www.sybase.com/detail?id=20039.  The extra CS_SEC_ENCRYPTION aren't strictly needed, but they make me feel better.

 

 > cat /opt/sybase/OCS-15_0/config/ocs.cfg

; This is the external configuration definition file.
;

[DEFAULT]
; This is the default section loaded by applications that use
; the external configuration (CS_EXTERNAL_CONFIG) feature, but
; which do not specify their own application name (CS_APPNAME).
; Initially this section is empty - defaults from all properties
; will be the same as earlier releases of Open Client libraries.
CS_SEC_ENCRYPTION = CS_TRUE

[isql]
CS_SEC_ENCRYPTION = CS_TRUE

[bcp]
CS_SEC_ENCRYPTION = CS_TRUE

[defncopy]
CS_SEC_ENCRYPTION = CS_TRUE

[IBI Adapter for Sybase]
CS_SEC_ENCRYPTION = CS_TRUE

[sampleappname]
CS_SEC_ENCRYPTION = CS_TRUE

4) Test connecting to Sybase via isql (-X enables the encrypted password):

> cd /opt/sybase/OCS-15_0/bin
> . /opt/sybase/SYBASE.sh # note the space after the period
> ./isql -Uuser -Ddatabase -Sservername -w255 -X
Password: <type your password>
1> select au_lname, city from pubs2..authors where state = 'CA'
2> go

You should get back a list of names and cities from the example database.  Once this works, you have the Open Client installed and now need to configure PHP to use it.

 5) Add SYBASE variables to path:

> cat /opt/sybase/SYBASE.sh >> /etc/profile 

You may have an /etc/profile.d directory setup, in which case you could do this instead:

> cd /etc/profile.d
> ln -s /opt/sybase/SYBASE.sh

Exit your shell and open a new one.

Configuring PHP to use Sybase:

1) Get yourself setup with the PHP source.  Mine is in /usr/src/PHP/php-4.4.9

2) Normally, at this point you could go ahead and compile:

You don't need to execute the below - it can take awhile.  But since you may get different errors than I do, it is a good way to check that your setup is the same as mine.  Not that I've only included the configure option that matters for Sybase-CT.  Most servers have a much longer list of options.  To check your existing ones execute php -i and check the "Configure Command" section.

> cd /usr/src/PHP/php-4.4.9
> ./configure --with-sybase-ct=/opt/sybase/OCS-15_0
> make
> make test
> make install

But this is where things begin to deviate... if you do that you'd get the following message after saying "make":

/usr/bin/d: cannot find -lintl
collect2: ld returned 1 exit status
make: *** [sapi/cgi/php] Error 1

3) So we need to make it so PHP can find the sybase libraries, which used to be named like libintl.a and such, but are now named syblibintl.a.  On my system the way to do this is:

> cd /etc/ld.so.conf.d/
> echo "/opt/sybase/OCS-15_0/lib" >> sybase-i386.conf
> ln -s /opt/sybase/OCS-15_0/lib/libsybcomn.a /opt/sybase/OCS-15_0/lib/libcomn.a
> ln -s /opt/sybase/OCS-15_0/lib/libsybcs.a /opt/sybase/OCS-15_0/lib/libcs.a
> ln -s /opt/sybase/OCS-15_0/lib/libsybct.a /opt/sybase/OCS-15_0/lib/libct.a
> ln -s /opt/sybase/OCS-15_0/lib/libsybintl.a /opt/sybase/OCS-15_0/lib/libintl.a
> ln -sd /opt/sybase /home/sybase # this is where PHP expects sybase
> /sbin/ldconfig -v # rebuild the ld library

But if you don't have a ld.so.conf.d you may need to edit the ld.so.conf file and add the directory on the last line.

Go ahead and confirm the above worked by running make again if you like.  PHP should build, but we're not done.

3) Alter the Sybase-CT module to use the newest CTLIB_VERSION:

Edit /usr/src/PHP/php-4.4.9/ext/sybase_ct/php_sybase_ct.h
Change:

#define CTLIB_VERSION CS_VERSION_100

to

#define CTLIB_VERSION CS_VERSION_150

or change it to CS_CURRENT_VERSION
I have not tested this yet in production, but it is likely the better option.

 #define CTLIB_VERSION CS_CURRENT_VERSION

4) Recompile everything:

> cd /usr/src/PHP/php-4.4.9
> make clean
> ./configure --with-sybase-ct=/opt/sybase/OCS-15_0
> make
> make test
> make install

5) Run a test php script, here is mine:

<?php
$con = sybase_connect("servername", "user", "password", "", "sampleappname");
$qry = sybase_query("select au_lname, city from pubs2..authors where state = 'CA'", $con);
while($row = sybase_fetch_array($qry)) {
var_dump($row);
}
sybase_close($con);
?>

The most important thing to note here is that the "sampleappname", which is the appname parameter to sybase_connect must match up with the [sampleappname] in the ocs.cfg file.