Archive for the ‘Ruby On Rails’ Category

Turing on x86_64 bit machines

Monday, September 28th, 2009

You might have read about implementing Captcha on your website using a Ruby on Rails gem called Turing in my earlier blog post. Turing is really very good captcha system suited for my needs and may be many of yours.

Recently I have faced an issue with Turing on 64 bit machine (server, where I host my website). It did throw "500 an internal server error" when trying to access a webpage which shows captcha image using Turing, while the webpage used to work perfectly on i386 machine locally. I tried running the code manually from the console and it did gave "/usr/lib/ruby/gems/1.8/gems/gd2-1.1.1/lib/gd2/font.rb:234: [BUG] Segmentation fault" error indicating some issue with gd2 font.rb file.

After digging through the error message I came across two important urls that had solved the problem. The urls to refer are:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/227097
https://boxpanel.blueboxgrp.com/public/the_vault/index.php/Turing_Gem

The problem isn’t exactly within Turing or gd2 gem but ruby needs a better code to work with these gems.

URLs above describe the problem, reason of the problem and solution as well. Tried to write them in a much simpler way, that could ease the process of getting the problem solved.

## 1. Download the latest ruby source code instead of ruby-1.8.6.tar.gz
wget -q ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.gz

## 2. Extract the downloaded tarball
tar xzf ruby-1.8.6.tar.gz

## 3. Change directory to extracted source
cd ruby-1.8.6

## 4. Change directory to place where the patch has to be applied
cd ext/dl

## 5. Make a backup of original file where the patch is going to be applied
cp sym.c sym.c.orig

## 6. Download the patch
wget http://www.ankit644.com/ror/sym.c.patch

## 7. Apply a patch
patch sym.c sym.c.patch

## 8. Change directory to parent directory
cd ../../

## 9. Configure with below command
./configure –prefix=/home/local –enable-pthread –with-readline-dir=/home/local

## 10. Make the binaries
make

## 11. Install the binaries to your system.
make install

When your website is on a shared hosting web server, your hosting provider will not allow you to install a custom patch for solving an individual needs. So, you have to install custom ruby in your local directories and make your custom settings. e.g.

  • Installing newly compiled ruby into your local directories (as per ‘make’ and ‘make install’)
  • $PATH environment variable should take your custom built ‘ruby’ command
  • "#!/usr/bin/ruby" (a shebang line) in public/{dispatch.cgi, dispatch.fcgi} has to be changed to "#!/home/local/bin/ruby"

And you are done!

Please do provide your feedback, if you think issue could be solved in a better way.

Thanks for reading!
Ankit

How can you fail spammers attacking your website?

Sunday, June 28th, 2009

Do you think we can stop spammers attacking our websites?

I think, NO! There is no way to stop spammers trying to attack your website, especially when your website starts getting popularity. But there are certain ways to prevent your website from spam attacks. Captcha is a way to fail spammers attacking your website.

Some rubygems are available for Captcha implementation under Ruby On Rails. You can find all of these Captcha rubygems here. Simple Captcha by Expressica.com is very popular Captcha rubygem out of the mentioned. A simple captcha Ruby/CAPTCHA is also good. ruby-recaptcha is another good example. Apart from all these, captchator.com also does provide a very good facility of using captchas in your website, which never requires you to install anything on your server to implement captcha validation.

But, the one I liked best suitable to my needs is Turing. There are number of reasons, why I liked it:

  • Easy to install (e.g. gem install turing)
  • Easy to configure (e.g. add “require ‘turing’” to config/environment.rb file)
  • Easy to implement (check here: example, entire manual can be found here)
  • Inbuilt glossary of words, which can be modified to meet individual’s needs, used to pick the random word for captcha images.
  • Various images used to pick as random background image for captcha
  • And the best part is: automatic removal of captcha images when you validate user input text with image text. (e.g. tc.valid_answer?(id, text))
  • No dependency on any website for captcha images or validation. Everything is done locally on the server
  • Turing is Licensed under GPLv2

There is no active development on Turing since 2007, however it fulfills most of my needs and works nicely without any kind of problem.

Despite these good features, there are improvement areas:

  • Along with making captcha images complex for machines to read, it should be made easy for humans to read. :)
  • OCRs might be able to decode your image text even though the captcha images look distorted.
  • Auto-removal of captcha images on the validation, which is being taken care by Turing, but I guess not by others.
  • Auto-removal of captcha images on the page unload event. Imagine a spam script continuously reloads a page every second. Your space on the server might get filled up in minutes.
  • Accessibility featues for disabled. Some captchas like ruby-recaptcha is successful in this area upto some extent.

I am still trying to find better ways of auto-removal of captcha images on the page unload event. Let me know, if you have got some inputs on it with respect to Turing rubygem.

/Ankit