Saturday, October 2, 2010

How to build mod_v8 in Ubuntu

mod_v8 is a simple Apache module which enable Server Side JavaScript(SSJS) support in Apache Server. It uses Google V8 engine for the JavaScript execution. What this module basically does is, it adds a Handler for a particular file extension(.v8) and then, those files are treated as SSJS files which then get executed via V8 engine.

While I was building this amazing mod_v8 module, I had to do small modification to the process. So I though it might be worth to have a post with the steps I followed.

1. Checkout V8 development branch
svn checkout http://v8.googlecode.com/svn/branches/bleeding_edge/ v8

2. Build V8 using scons with the library=shared option. Optionally you might need to add arch option too. You can find more info about it at Building V8 in Ubuntu 64 bit
scons library=shared arch=x64

3. Assuming your v8 checkout is at /home/wageesha/v8, building process will create following obj, libv8.so etc.. directories and files within v8 directory. You need to copy this libv8.so into /usr/lib.
cp ~/wageesha/v8/libv8.so /usr/lib

4. Checkout mod_v8 code
svn checkout https://svn.i-want-a-pony.com/repos/mod_v8/trunk/ mod_v8

5. Build mod_v8 using scons. Depending on your scons version, you might need to change
opts = Options('build.py') into opts = Variables('build.py')
and
opts.Add(PathOption(....)) into opts.Add(PathVariable(....))

in the SConstruct. Further, if you haven't copied libv8.so into the /usr/lib directory as in step 3, build process will fail saying
/usr/bin/ld: cannot find -lv8

6. Then open /etc/apache2/apache2.conf and add following lines, with the correct path to libmod_v8.so
LoadModule v8_module /home/wageesha/mod_v8/trunk/libmod_v8.so

<IfModule mod_v8.cpp>
#Adds the v8 handler
AddHandler v8-script .v8
</IfModule>

7. Copy /home/wageesha/mod_v8/trunk/test.v8 into /var/www folder.

8. Restart apache and access test.v8
http://localhost/test.v8

which will then print
Hello World!

in the browser.

Friday, October 1, 2010

Building V8 in Ubuntu 64 bit

If you are trying to build Google V8 in a Ubuntu 64bit machine you might get
/usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file or directory
error when you ran scons with default arguments. When default arguments are used, it tries to guess the OS. In my case it failed to detect the correct architecture (i.e. x64). So you can specify arch argument when you run the scons to manually set the architecture. i.e.
scons arch=x64
arch argument accepts any of arm, ia32, x64 or mips

Thursday, July 29, 2010

Regular Expressions with Javascript

Here is an excellent demonstration of Regular Expression usage in Javascript.

http://lawrence.ecorp.net/inet/samples/regexp-format.php

Thursday, July 8, 2010

How to Create Client/Server Keystores using Java Keytool

Here I have described how to create client/server keystores which can be used to secure Axsi2 webservices and invoke Axis2 secured webservices. i.e. It can be easily used with any WSO2 Product to experience security scenarios.More detailed explanation on creating client/server keystores using openssl including Certificate Authority(CA) Requests, can be found at http://wso2.org/library/174.

Java keytool stores the keys and certificates in a keystore, protected by a keystore password. Further, it protects private key again with another password. A Java keystore contains private-public key pair and multiple trusted certificate entries. All entries in a keystore are referred by aliases. Both private key and self signed public key is referred by one alias while any other trusted certificates are referred by different individual aliases.

As the first step, let's create a keystore for server. In order to do it, execute following command in a terminal. "server" in the following command corresponds to the private key/self signed public key certificate alias in the keystore while "server.jks" is the name of the creating keystore file.
keytool -genkey -alias server -keyalg RSA -keystore server.jks
when you execute the above command it will first prompt you to specify a password which is corresponded to the keystore password. Then it will prompt several questions. You can give answers that you wish. At the end it will ask for a password again, which will be used to secure the generated private key.
Enter keystore password:  
Re-enter new password: 
What is your first and last name?
[Unknown]:  Ruchira Wageesha
What is the name of your organizational unit?
[Unknown]:  Mashup Server
What is the name of your organization?
[Unknown]:  WSO2
What is the name of your City or Locality?
[Unknown]:  Ahangama
What is the name of your State or Province?
[Unknown]:  Southern
What is the two-letter country code for this unit?
[Unknown]:  LK
Is CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK correct?
[no]: yes
Enter key password for 
(RETURN if same as keystore password):
Once you successfully completed this, java keytool will create a file named "server.jks". In the same way, you can create a client keystore named "client.jks" with the alias "client" using following command.
keytool -genkey -alias client -keyalg RSA -keystore client.jks
Now, you have two files named client.jks and server.jks. You can view the content of these keystore files using the following command. Replacess "ruchira" with the keystore password you entered while creating the keystore.
keytool -list -v -keystore server.jks -storepass ruchira
This will list something like this.
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: server
Creation date: Jul 8, 2010
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c356225
Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010
Certificate fingerprints:
MD5:  60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8
SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************
The next step is, getting server's self signed public key certificate and storing it in client's keystore. And getting and storing client's self signed public key certificate in server's keystore. In order to do that, first we need to export both server and client public key certificates into files. Using the following command, you can export server's public key certificate into server.cert file and client's public key certificate into client.cert file.
keytool -export -file server.cert -keystore server.jks -storepass ruchira -alias server
keytool -export -file client.cert -keystore client.jks -storepass ruchira -alias client
Now you have server.cert and client.cert. You can use following commands to view certificate contents.
keytool -printcert -v -file server.cert
keytool -printcert -v -file client.cert
As the last step, we need to import server.cert into client keystore and client.cert into server keystore. As I mentioned earlier, each entry of a Java Keystore is stored against an alias. So, we need to specify aliases here, which will be used to refer the certificates that we are going to store.
keytool -import -file client.cert -keystore server.jks -storepass ruchira -alias client
Above command will store client's self signed public key certificate(client.cert) in server.jks against the alias "client". So, using "client" alias on server.jks, we can refer client's certificate anytime. Likewise, following command will store server.cert within client.jks against the alias "server".
keytool -import -file server.cert -keystore client.jks -storepass ruchira -alias server
After all, please view the content of both keystore again using following commands.
keytool -list -v -keystore server.jks -storepass ruchira
keytool -list -v -keystore client.jks -storepass ruchira
It will give you something like bellow for server.jks
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 2 entries

Alias name: server
Creation date: Jul 8, 2010
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c3562a0
Valid from: Thu Jul 08 11:01:12 IST 2010 until: Wed Oct 06 11:01:12 IST 2010
Certificate fingerprints:
MD5:  AB:77:72:F1:0D:09:55:E3:B6:D3:DC:A6:4D:D4:39:36
SHA1: D7:C1:60:5C:7E:34:40:A9:0B:E4:2C:65:6C:E0:79:7C:EE:37:A7:19
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************


Alias name: client
Creation date: Jul 8, 2010
Entry type: trustedCertEntry

Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c356225
Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010
Certificate fingerprints:
MD5:  60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8
SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************

something like below for client.jks
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 2 entries

Alias name: server
Creation date: Jul 8, 2010
Entry type: trustedCertEntry

Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c3562a0
Valid from: Thu Jul 08 11:01:12 IST 2010 until: Wed Oct 06 11:01:12 IST 2010
Certificate fingerprints:
MD5:  AB:77:72:F1:0D:09:55:E3:B6:D3:DC:A6:4D:D4:39:36
SHA1: D7:C1:60:5C:7E:34:40:A9:0B:E4:2C:65:6C:E0:79:7C:EE:37:A7:19
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************


Alias name: client
Creation date: Jul 8, 2010
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Issuer: CN=Ruchira Wageesha, OU=Mashup Server, O=WSO2, L=Ahangama, ST=Southern, C=LK
Serial number: 4c356225
Valid from: Thu Jul 08 10:59:09 IST 2010 until: Wed Oct 06 10:59:09 IST 2010
Certificate fingerprints:
MD5:  60:0B:48:0D:DB:56:8C:68:8C:2D:94:4A:D6:DA:04:B8
SHA1: A7:CE:57:10:70:87:C1:2C:C0:9D:1D:90:8C:BB:69:B6:66:26:97:13
Signature algorithm name: SHA1withRSA
Version: 3


*******************************************
*******************************************
If everything went well, you might have successfully created server.jks and client.jks which can be used to secure Axis2 Services and access those secured services.

You can also find a summary of Java keytool commands at http://ruchirawageesha.blogspot.com/2010/07/java-keytool-keystore-commands.html

Wednesday, July 7, 2010

Java Keytool Keystore Commands

Create Keystore, Keys and Certificate Requests
  • Generate a Java keystore and key pair
    keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -storepass password
  • Generate a certificate signing request (CSR) for an existing Java keystore
    keytool -certreq -alias mydomain -keystore keystore.jks -storepass password -file mydomain.csr
  • Generate a keystore and self-signed certificate
    keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360
Import Certificates
  • Import a root or intermediate CA certificate to an existing Java keystore
    keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks -storepass password
  • Import a signed primary certificate to an existing Java keystore
    keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password
  • Import New CA into Trusted Certs
    keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts
Export Certificates
  • Export a certificate from a keystore
    keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password
Check/List/View
  • Check a stand-alone certificate
    keytool -printcert -v -file mydomain.crt
  • Check which certificates are in a Java keystore
    keytool -list -v -keystore keystore.jks -storepass password
  • Check a particular keystore entry using an alias
    keytool -list -v -keystore keystore.jks -storepass password -alias mydomain
  • List Trusted CA Certs
    keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
Delete Certificates
  • Delete a certificate from a Java Keytool keystore
    keytool -delete -alias mydomain -keystore keystore.jks -storepass password
Change Passwords
  • Change a Java keystore password
    keytool -storepasswd -new new_storepass -keystore keystore.jks -storepass password
  • Change a private key password
    keytool -keypasswd -alias client -keypass old_password -new new_password -keystore client.jks -storepass password

Friday, June 18, 2010

Ubuntu - Copy Everything Except

If you want to copy everything within a directory, except certain files or directories, you can use the following command.
cp -a `ls | grep -v yourexcludepattern` ../destinationdir/

Assume, you want to copy all files, except *.java within from the current directory to the foo directory. Then, the command would be,
cp -a `ls | grep -v *.java` /home/wageesha/foo

Friday, June 11, 2010

How to copy a file into multiple directories in Ubuntu

Assume that you want to copy a file into multiple directories in the current directory, then all you have to do is, just entering following command in the terminal. Here, file.txt will be copied into all directories in the current directory. In case of a directory coping, replace file.txt with -rf [your dir].
for dir in *; do [ -d "$dir" ] && cp file.txt "$dir" ; done
for dir in *; do [ -d "$dir" ] && cp -rf /home/wageesha/images "$dir" ; done

Sunday, May 23, 2010

WSO2 Mashup Server - Google Maps API Key Mashup

If you are using Google Maps in your web site, you need to get an API key from Google.com. In order to do that, you need to login using an existing Google account, accept their terms and policies and finally you will be able to get the Google Maps API key. Although this is the usual procedure, if they provide a web service for it, it will be much useful in places where you want to get the key at runtime.

As an example, WSO2 Mashup Server has a Twitter Maps Mashup which shows recent tweets in a Google map. In order to have the proper functionality of this mashup independent of the domain where the server is running, it needs to dynamically generate an API key for that domain and use it within the Twitter map mashup. That was the original motivation towards this mashup.

You will need to deploy the following mashup in the WSO2 Mashup Server 2.0.2 or a newer one.
getAPIKey.inputTypes = { "username" : "string", "password" : "string", "url" : "string" };
getAPIKey.output = "string";
function getAPIKey(username, password, url) {
    var client = new HttpClient();
    var code = client.executeMethod("GET", "https://www.google.com/accounts/Login");
    if (code == 200) {
        var galx = client.cookies[0].value;
        var content = [
            { name : "Email", value : username },
            { name : "Passwd", value : password },
            { name : "signIn", value : "Sign in" },
            { name : "GALX", value : String(galx) },
            { name : "dsh", value : "5537526595243201224"},
            { name : "rmShown", value : "1"},
            { name : "PersistentCookie", value : "yes"}
        ];
        code = client.executeMethod("POST", "https://www.google.com/accounts/LoginAuth", content);
        if (code == 302 || code == 200) {
            code = client.executeMethod("GET", "http://code.google.com/apis/maps/signup/createkey", [
                { name : "referer", value : url }
            ]);
            if (code == 200) {
                var response = eval('(' + client.response + ')');
                client.releaseConnection();
                return response.generated_key;
            } else {
                client.releaseConnection();
                return new XML("" + code + "" + client.statusText + "");
            }
        } else {
            client.releaseConnection();
            return new XML("" + code + "" + client.statusText + "");
        }
    } else {
        client.releaseConnection();
        return new XML("" + code + "" + client.statusText + "");
    }
}

Friday, April 23, 2010

Subversion: How to revert to a previous revision

Assume that your current revision is 100 and you want to revert the repository to the revision 90. Then you can use the following command,
$ svn merge -rHEAD:90 .
As you have used . at the end, it will revert your local repository. Once commited you will get a new revision such as 101 which is same as revision 90. You can do this directly to a remote repository as well. In that case, replace . with repository url.

Wednesday, April 7, 2010

GCE O/L, A/L and Grade V Examination Results

Do you want to view O/L, A/L and Grade V results a bit earlier than http://www.doenets.lk lets us. I have tried it when it says, "New Await Results". You can do this using firebug addon on firefox.

www.doenets.lk lets us to check previous result too. These are displayed by the "New Await Results" table. So what you need to do is first select one of those forms and change action url to exam_OL.jsp, exam_AL.jsp or exam_GV.jsp as appropriate. You would also need to change the size/maxLength of the inputput field as well. Otherwise it won't let us to insert our full index number. That's all, fill it with your index number, submit and get your results. You can also do a http POST using your own form as well.

Attention : I am not responsible for the accuracy of results as www.doenets.lk might not have officialy released the results when you use this.

Sunday, March 21, 2010

How to enable mod rewrite on Apache

Tired up trying to enable mode rewrite on apache? Try this link which explains it quite well.
http://drupal.org/node/134439