PHP driver for SQL Server 2005+

October 16th, 2008 | Category: Technical Review

If some of you have been using PHP on Windows with SQL Server 2005, you may have hit some problems especially if you wanted to exploit new features of SQL Server 2005 like xml datatype, NVARCHAR(MAX), etc.

The driver that had the better support for these were the PHP ODBC wrapper combined with the SQL Server 2000 ODBC driver.  The bad news is, by default, PHP ODBC uses server-side dynamic cursors which is the thing Microsoft says to avoid as much as possible (unless you have a need for that).  It is very slow, server resource intensive, poor performing, etc.

Some succeeded to change the way ODBC were handling resultset by using a hint at connection time (SQL_CUR_USE_ODBC), but it didn’t help for us.  Some perfectly valid parameterized queries where just giving unexpected results.

I even downloaded PHP’s source code to see why it was using dynamic cursors by default.  If I could, at least, change the default cursor, we may had a little performance increase…  It was hardcoded to “dynamic” with the following comment on top of it:

Try to set CURSOR_TYPE to dynamic. Driver will replace this with other type if not possible.

So next thing would be to change it and recompile…  forgot about it!

Then, some time at the end of 2007, I discovered an alpha community preview release of a new driver made by Microsoft.  Wohoo!  This version was unstable with xml datatypes at a point that it was making my Apache server crash…  :(

Fortunately, the official release finally got out et we are testing it for some time now!  So far, there are no blocking bugs.  The quality is good enough that I took the time to created a Creole wrapper (our web application uses Creole as database wrapper API) for it and start using it full-time on our develpment environment.

Here are some observations:

  • On my laptop, based on a non-official, non-extensive performance test, I had a 400% to 500% performance boost for fetching 200 records of a large (numerous fields) table.
  • UTF-8 support exists, but conversion must be done manually, field by field (better have a database wrapper API…) and at a huge performance cost.
  • UTF-8 support works only for query parameters and resultset values.  If you hardcoded a query filter (I know it is not a best practice, but we all supports legacy applications…) you will have to rewrite it with parameters or drop UTF-8 support.
  • If you are using PHP from a Linux server, you are still left alone because the Microsoft driver relies on the ODBC SQL Server Native Client driver that works only on Windows.

If you need more information about the driver, you can visit the Microsoft blog and forum.

No comments

Remote Desktop through SSH with PuTTY and Tomato firmware

September 06th, 2008 | Category: How-To

First of all, for simplicity, let’s assume we have a desktop at home that we want to connect to (control remotely) using a laptop while we are at a friend’s home. We will use Remote Desktop to connect from the laptop to the home desktop. The home network is behind a router (firewall) compatible with Tomato (ex: Linksys WRT54GL).

Remote Desktop is a server application that uses TCP/IP network to enable remote control of a machine. It opens the port 3389 to handle network communications. By default Remote Desktop is disabled on Windows XP.

How to enable Remote Desktop on Windows XP (on the home desktop):

  • Start the system properties in the Control Panel (or right click “My Computer” and click properties).
  • Go to the “Remote” tab.
  • Enable remote desktop by checking “Allow users to connect remotely to this computer”.

Note 1: Don’t forget to set a password on the user you intend to use to connect remotely because Remote Desktop will prevent connection with blank passwords.

Note 2: By defaut, only users with the administrative priviledges are allowed to connect remotely.

Enable Remote Desktop on Windows XP

At this point, Windows can handle remote connections.  In order to do it through the Internet you could simply forward the port 3389 from your router to the actual machine but at the same time you would expose your machine to the whole world. If you can do it, somebody else can try too.

Instead, we use a 3rd party firmware (called Tomato) on the Linksys router. This firmware allows us to connect using SSH (a secured command line shell).

With SSH we can create encrypted communication links (called Tunnels) between the laptop and the router. SSH protocol requires a server that will also open a listening port. We need to login to this server to establish the SSH connection and create tunnels.

Why this instead of just opening Remote Desktop port (3389)?

  • Because we will use a *different password* (will we? well we should… and a strong one!) than the one on your computer.
  • If we would have multiple computers to remote desktop to, we wouldn’t need to open additional ports, just create additional tunnels instead.
  • If somebody cracks our router password, he is still limited in he can do, he has to guess/find our machine and crack its password.
  • We can make SSH connection more secured by using a key file. This file is needed to establish the connection link, so another level of difficulty for a pirate…
  • The communication is encrypted between the two ends of the Tunnels (more privacy)

So how to enable the SSH Server on the Tomato firmware?

  • Login to Tomato’s web interface
  • Go to Administration / Admin Access
  • In SSH Deamon section, set the following :
    • Enable at startup: checked
    • Remote access: checked
    • Remote port: <choose one>
      (ex: 5555 — used from Internet)
    • Port: 22
      (used from inside the LAN)
    • Authorized Keys: <empty>
      (for simplicity of this post)

Note: Even if the screenshot shows this, we should use a different port than 22 (or 2222). If somebody discover our machine using a port scanner, he will have to guess what is the protocol (is it SSH, RDP, HTTP, FTP, etc?). If we leave the default, the guess is easy.

Tomato SSH Deamon Configuration

The home computer is enabled for remote desktop and the router is configured for SSH. Now we need to establish the SSH connection/tunnels, so put the laptop on the backpack and let’s ride to our best friend! Since the laptop is also running Windows XP, we are going to use PuTTY, an open source SSH client software (download it here).

Let’s configure PuTTY:

  • Start PuTTY and fill the information of the first tab:
    • Host name or IP Address: <enter yours>
    • Port: <the port you set earlier>
      (ex: 5555)
PuTTY - Session Tab
  • Configure tunnels in Connections / Tunnels tab:
    • Enter a source port: <choose>
      (ex: 153389)
    • Enter a destination address and port: <ip>:<port>
      (ex: 192.168.1.15:3389)
    • Local: Checked
    • Auto: Checked
    • Click add button

Note: The destination address and port is the address of our home desktop which is often in the range of 192.168.x.x depending on the router.  Our Linksys uses 192.168.1.x range by default.  IP address could be anything within the range when address is assigned by a DHCP server.  To make sure the desktop computer always have the same IP address, we can set a fixed address or configure Tomato DHCP server to always assign the same (see this post)

PuTTY - Tunnel Creation
PuTTY - Tunnel Creation

When we are going to open the SSH connection, PuTTY will open a local port defined by “source port”.  All the communication to that port will be encrypted and forwarded on the LAN to the destination address and port specified (ex: 192.168.1.15 port 3389).

Now let’s login to SSH Server

  • Click the Open button, a black screen should appear.
  • We now have to enter the router’s username and password
PuTTY - Login
PuTTY - Login

The only step left is to run Remote Desktop Connection software on the laptop:

  • Open up remote desktop and specify the computer:
    • localhost:<source port>
      (ex: localhost:153389)
  • Click connect
Remote Desktop Login

Voilà  we are now controlling the home desktop from outside the house using the laptop!

Specifications of the software used in this post:

Tomato firmware version 1.21.1515
PuTTY version 0.60
Windows version Windows XP Service Pack 3
8 comments

Tomato firmware: Remote desktop through SSH, using Wake-On-Lan and configuring LAN Hostnames

August 13th, 2008 | Category: How-To

Hi,

I’ve been using 3rd party firmware for my Linksys WRT54GL router for some time.  I’ve used DD-WRT, OpenWRT and finally Tomato.   I switched from DD-WRT (for a reason I can’t remember) to go with OpenWRT.  I learned a lot on OpenWRT and really like their Kamikaze version.  The work they did for making it more like a standard distribution (no more NVRAM variables) is really cool.  Finally I recently moved to Tomato because of the lack of User Interface of OpenWRT (Yes I could use X-WRT but I tried it before and prefered to explore Tomato…).  When I feel lazy I prefer to use a GUI than go to the CLI…

Now that I am more familar with my WRT (thanks to OpenWRT and all its documentation), it has been pretty easy to configure Tomato (firmwares are different, but in the end, they often use the same tools… busybox, dnsmasq, etc.).  I have some specific needs that drove me to install a 3rd party firmware in the first place:

  1. I need to be able to access my computers from the internet using remote desktop

  2. But I don’t want to leave my computers open all day long for nothing, so I need to be able to use Wake-On-Lan to power them up remotely

  3. I don’t want to open remote desktop port (3389) on the firewall

  4. I want to use DHCP server, but I need to set some specific address (static DHCP) on some computers
    (so port forwarding is always configure correctly for my P2P machine…)

  5. I need to be able to use the same domain name (ex: mysite.dyndns.org) with my laptop no matter if I connected to my LAN or to a public network (through the Internet)

My Solution:

Feature Tomato Configuration
Access my computers from the internet using remote desktop.
  1. I setup an account with dynDNS. It is free up to 5 domain names (if I remember correctly).This account allows me to use a software to update a domain name with my new IP address when my ISP changes it. From the internet I can just connect to my router using example.dyndns.org.Most routers supports dynDNS (and some others as well) natively.
  2. I enabled the SSH Deamon
    (Administration / Admin access)Enabled at Startup: checked
    Remote Access: checked
    Remote Port: [your choice]
    Allow Password Login: checked
  3. When I want to connect to my computers, I use a SSH client software (PuTTY) to connect to my router.PuTTY allows me to configure SSH Tunnels so I can redirect a local port to a remote machine on my LAN without opening any additionnal ports.
I need to power up my computers only when needed. This requires the Wake-On-Lan feature.Most modern computer has this feature (My old Pentium 2 266Mhz had it back in 1998) but you usually have to turn the feature ON in your computer BIOS because it is often disabled by default.When you want to power-up a computer, all you need is its mac address (in Windows, you can find it by running IPCONFIG at the command line) and a software that will broadcast a “magic packet” on your LAN.Tomato has a built-in software called ether-wake that does the job.My configuration is like this:On startup of the router, generate a file called wakeup-mycomputer.sh that will be placed in the root home directory. This file will contain the command able to wake-up the computer you want.

Then when you logon to your router using SSH (from the LAN or the Internet), you can issue this command “sh wakeup-mycomputer.sh” and you’re done.

You wait a little bit (until your computer responds to ping) and use the steps of the previous feature to connect remotely…

How to achieve this:

  1. In the Initialization script of the firmware (Administration / Scripts / Init tab), type this:
    echo "/usr/bin/ether-wake 01:23:45:67:89:AB" > /tmp/home/root/wakeup-mycomputer.sh
    (change 01:23:45:67:89:AB to the mac address of the computer you want to wake-up)
  2. Save and reboot the router, you should see the file “wakeup-mycomputer.sh” appear in the root home directory when you connect using SSH.
  3. Try it “sh wakeup-mycomputer.sh”

Note: for the wake-on-lan to work, the computer must have been turn off (soft off). After a power outage or a hard off (unplug, press power off for 5 seconds) the wake-on-lan may not work. Just turn on the computer manually and perform a shutdown.

Note2: I found out that some Linux distribution does not “soft off”. With Ubuntu 8.04, for example, I needed to add a command in the shutdown script… can’t remember. Just post a comment if you want me to digg it again…

Configure static DHCP addresses. This is easy, just navigate to the Tomato “Basic / static DHCP” menu item. Use the mac address of the computer you want a fix address and the address you want for it.
Use the same domaine name inside the LAN and outside (from the Internet).

When I am connected from the Internet, my domain “example.dyndns.org” resolve to my router public address which forward the port to the right LAN machine.When I am on the LAN, I want to be able to connect using “example.dyndns.org”. But if this domaine resolve to the public address of the router while you are already in the LAN, it does not work. To correct the problem, you could use a host file on your LAN computers, but when you have a laptop that is sometimes on the LAN, sometimes on an external network, you would need to enable/disable your host file every time…The way I succeed to anwer my need, is by providing a host file to the router. I found 2 ways with the Tomato firmware:

First way is explained here on Tomato FAQ.  Unfortunately, I needed to provide multiple host names (domain name) to the same IP address, you can do it by separating them with a space, but Tomate host name field is not wide enough for me.

So my solution goes like this:

  1. Create a host file with my ip / host bindings at router boot time (before firewall is started).
    In “Administration / Scripts” menu item, on “Init” tab, place the following lines (use your IP addresses and domain names):
    echo "192.168.1.xxx  example.dyndns.org" > /tmp/hosts.local
    echo "192.168.1.yyy  example2.dyndns.org" >> /tmp/hosts.local
  2. Configure the firewall (dnsmasq) to use this additional host file.
    In “Advanced / DHCP/DNS” menu item, in custom configuration, place this to enable your custom host file:
    addn-hosts=/tmp/hosts.local
  3. Save and reboot the router.
  4. Test by doing a ping of you domain names (ping example.dyndns.org).
    The result should be your configured local IP addresss, not the router’s public IP address.
10 comments

Gartner analysts say Windows is ‘collapsing’

April 12th, 2008 | Category: Opinion, Web Link

In this article, the analysts give recommendations Microsoft should follow to prevent collapsing of Windows. That’s interesting to see that some of their recommendations reaches some of my wishes stated in this post.

I’ve been using many Linux distributions (mainly Ubuntu) for over a year now and I’m getting more and more familiar with them. With the acquisition of a little NAS I have restructured my network and my home server. I took the opportunity to rebuild everything from scratch and now my server is running Ubuntu Server and my Windows Server is now a virtual machine. Nice but, what’s the relation with the title of the article? Well I could have done it using VMWare Server on a Windows host but I chose Ubuntu Server because its faster on my hardware. The longer Microsoft will take to answer market needs, the more irreversible market shares the competition will take. That ain’t bad since good competition brings innovation.

No comments

Migrating a Subversion repository from Windows to Linux

January 07th, 2008 | Category: How-To

Migrating subversion from a Windows installation to a Linux installation is not just copying the repository structure and files. But it is still quite easy. Here are the steps:

  1. Extract the repository data from Windows repository and store it into an OS independant format using the dump command:
    svnadmin dump [repo location] > [dump file]
    ex: svnadmin dump /svn/MyProject > c:\temp\MyProject.dmp
  2. Upload the exported file on the Linux system.
  3. Create a Subversion repository:
    svnadmin create [repo location]
  4. Adjust security settings on the new repository directory (if necessary).
  5. Restore the dump file into the new repository:
    svnadmin load [repo location] < [dump file]

Here is an example of loading a dumped file into a Subversion installation for accessing through Apache2 and Ubuntu Server 7.04:

  1. create repository:
    sudo svnadmin create /var/svn/MyProject
  2. change owner:
    sudo chown -R www-data:www-data /var/svn/MyProject
  3. change security:
    sudo chmod -R g+ws /var/svn/MyProject
  4. Load dump file:
    sudo svnadmin load /var/svn/MyProject < ~/MyProject.dmp
No comments

Linux Ubuntu recognized as a Windows Genuine installation

June 28th, 2007 | Category: Opinion, Web Link

Funny: The “Windows Genuine Validation Tool” recognized Linux Ubuntu as a Genuine Windows installation:
http://ubuntuforums.org/showthread.php?t=475709

It is probably not easy to build a tool that would be able to perform this Genuine validation and be 100% accurate, and that makes me wonder if it is worth it.

I’d like Microsoft to change its strategy, drop the genuine validation tool, Windows activation and other strategies that slow down machines and exist only for the sake of protecting Microsoft against piracy. I’d like Microsoft to go back to its old strategy… one OS for desktop. Combine all editions of Vista into a single one where you can choose to install packages (ex: aero, business tools, multimedia tools, etc.). All that at a decent price of 100-150$ per PC. To compensate for the lost, you may have less pirates willing to spend their time just to “fight” Microsoft strategies. That may not be enough, so since the price is low, I’d agree that Microsoft charges annual subscription fees for accessing the Windows Update site and allowing automatic updates. For those who wouldn’t sign-up, they should wait for the next service pack.

With its millions, I’d like Microsoft to work on what is worth it, enhance its operating system and increase security, stability and interoperability. Stop reworking user interface, end users need to learn it again and again, that’s not going to help increase user performance (and ROI)… We knew Microsoft Office menus and options, now we have to re-learn them in Office 2007.

It is not hard to find where Windows needs enhancements:

  • Make the shell easy for administrators to script tasks
  • Make it easy to work using reduced privileges
  • Allow more desktop customization

But doing all that would be listening to their users… not their investors…

No comments