follow me on Twitter

    Latest Gallery Images

    Joycie!
    Angels Landing Through The Trees
    PHP Women
    LinuxChix
    Blogs By Women
    Creative Commons Licence

    Archive for the ‘Geek’ Category

    Using multiple private keys with SSH

    Friday, July 23rd, 2010

    A couple of days ago I posted on how to set up a subversion server with ssh access. In that post, in the section about creating your private ssh key I said that if you already had an id_rsa or id_dsa file in your ~/.ssh directory not to go any further because the generation of a new private key file could over write it. This post explains how to cope with multiple private keys.

    ssh uses ~/.ssh/identity as the default filename for the private key when using version 1 of the ssh protocol and it uses ~/.ssh/id_rsa and ~/.ssh/id_dsa as the default filenames for the RSA and DSA private keys when using version 2 of the ssh protocol. However there are several ways of changing this behaviour.

    Command line

    ssh allows you to provide a different identity/id file using the -i option on the ssh command line as follows:

    $ ssh -i /path/to/id_rsa user@some.domain.org
    

    Where id_rsa can be substituted with id_dsa or identity.

    This is the simplest method. And when used in conjunction with a subversion client you can create a [tunnel] in the subversion configuration file like so:

    [general]
    ssh = ssh -i /path/to/identity_file
    

    If you want to support different identity/id files create a new [tunnel] ‘alias’ for each identity file. You can also use the -p option if you want to specify a different port at the same time.

    Config file – first method

    Another method is to use the ssh config file. The global file is found in /etc/ssh/config however you can also provide config files on a per user basic in ~/.ssh. In the config file you can restrict declarations using the Host declaration. Per the man page:

    Host
       Restricts the following declarations (up to the next Host
       keyword) to be only for those hosts that match one of the
       patterns given after the keyword.  If more than one pattern
       is provided, they should be separated by whitespace.  A
       single `*' as a pattern can be used to provide global defaults
       for all hosts.  The host is the hostname argument given on
       the command line (i.e. the name is not converted to a
       canonicalized host name before matching).
    

    The declaration IdentityFile allows you to specify the private key file that ssh should use. By using Host and IdentityFile you can let ssh pick the identity/id file itself based upon the host name. The following example will pick ~/.ssh/id_dsa for any host other than my.specific.host.org and my.other.host.com. In those cases the specified private key files are used.

    # General settings
    IdentityFile ~/.ssh/id_rsa
    IdentifyFile ~/.ssh/id_dsa
    IdentityFile ~/.ssh/identity
    
    Host my.specific.host.org
    IdentityFile ~/.ssh/my_specific_key_file
    
    Host my.other.host.com
    IdentityFile ~/.ssh/my_other_key_file
    

    Note how the config file allows you to specify more than one file at once. When you do ssh will try each of the files in turn or as appropriate.

    Config file – second method

    The problem with the above method is that you have to keep adding new entries to the config file for new hosts. IdentityFile is actually very powerful and allows you to have wild cards in the path specification. The full man page definition says:

    IdentityFile
       Specifies a file from which the user's RSA or DSA
       authentication identity is read.  The default is ~/.ssh/identity
       for protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa
       for protocol version 2. Additionally, any identities
       represented by the authentication agent will be used for
       authentication.
    
       The file name may use the tilde syntax to refer to a user's
       home directory or one of the following escape characters:
       `%d' (local user's home directory), `%u' (local user name),
        `%l' (local host name), `%h' (remote host name) or `%r'
       (remote user name).
    
       It is possible to have multiple identity files specified in
       configuration files; all these identities will be tried in
       sequence.
    

    So I have set up my config file as below. It basically tries ~/.ssh/idents/hostname/username/id_* first, then ~/.ssh/idents/hostname/id_*, then the defaults before giving up.

    # SSH config file
    
    IdentityFile ~/.ssh/idents/%h/%u/id_rsa
    IdentityFile ~/.ssh/idents/%h/%u/id_dsa
    IdentityFile ~/.ssh/idents/%h/id_rsa
    IdentityFile ~/.ssh/idents/%h/id_dsa
    IdentityFile ~/.ssh/id_rsa
    IdentityFile ~/.ssh/id_dsa
    

    This allows me to support a new server/key file combination just by putting the key file in the appropriate place in the ~/.ssh without having to edit config files.

    Setting up a subversion plus ssh server

    Tuesday, July 20th, 2010

    As someone who is a strong advocate of source control at work I’m actually quite bad at following what I preach. So this lunch time I decided to finally get around to setting up a Subversion server on my Linux server.

    Installation

    I am currently running Ubuntu 9.10 so installed subversion as follows:

    $ sudo apt-get install subversion
    

    If you don’t have ssh installed (why not?) install it similarly.

    Users and groups

    Once you have subversion and ssh installed you can set up the SVN server. Firstly you need to create a suitable users group, an admin user and a repository.

    Create the group like so:

    $ sudo groupadd -g 127 svnusers
    

    The group id (127 above) needs to be unique so unless you know a free group id value check to see the last id used in the groups file as follows:

    $ tail /etc/group
    

    Then create an SVN admin user using useradd as below:

    $ sudo useradd -c "SVN Admin" -u 2000 -g 127 -d /home/svnadmin -m -s /bin/bash svnadmin
    

    Again the user id (2000 above) needs to be unique. This time look at /etc/passwd to find out the last id used. The group id (127 above) should be the same value as the group you created. The rest of the options tell useradd to create a home directory and to set the default shell to be bash.

    Set the password for the SVN admin user like so:

    $ sudo passwd svnadmin
    

    Finally add any other users to the svnusers group by editing /etc/group and adding the additional users in a comma separated list on the end. For example:

    svnusers:x:127:svnadmin,melanie
    

    Server setup

    Now its time to create a repository. Firstly create the directory where all the repositories will be stored. I put mine in /var since my /var is on separate partition (to protect from disk full issues). However you can put them where you want. So create the directory and set the permissions as below:

    $ mkdir -p /path/to/subversion/repositories
    $ chown -R root.svnusers /path/to/subversion/repositories
    $ chmod -R 770 /path/to/subversion/repositories
    

    When using ssh, svnserve is used as the SVN server. svnserve requires the full path to repository to be provided by the client to access the repository. You may not want to expose your file system that much so you can write a small script to modify the behaviour. Use which to find out where svnserve is and rename it.

    $ which svnserve
    /usr/bin/svnserve
    $ sudo mv /usr/bin/svnserve /usr/bin/svnserve.bin
    

    Then using your favourite editor create the script below and save it under the old svnserve name.

    #!/bin/bash
    exec /usr/bin/svnserve.bin -r /path/to/subversion/repositories "$@"

    Finally set the execute permissions.

    $ sudo chmod 755 /usr/bin/svnserve
    

    Creating a project

    Now its time to create a project. Change to the svnadmin user and enter the password.

    $ su - svnadmin
    password:
    

    Now create a project and turn off other access so that only members of the svnusers group and the admin user can access it.

    $ svnadmin create /path/to/subversion/repositories/my_first_project
    $ chmod -R o-rwx /path/to/subversion/repositories/my_first_project
    

    Finally you need to configure the project’s access permissions by editing

    /path/to/subversion/repositories/my_first_project/conf/svnserve.conf
    

    In the [general] section uncomment or add:

    anon-access = none
    auth-access = write
    

    Now save the file.

    Testing

    Now you should be able to access the svn repository from anywhere provided the user is in the svnusers group. Test as follows from a client.

    $ svn list svn+ssh://user@some.domain.org/my_first_project
    

    You will be asked for the ssh password for the machine running subversion. And that’s it. You should be able to use your project. You can add new projects using the svnadmin user.

    Further tweaks

    There a couple of additional things you may want to do. Firstly my subversion server is behind a firewall. The machine it is on is accessed using ssh on a different port to the default. Secondly keep being asked for the ssh password can become quite a pain. Using public key authentication does make things somewhat easier.

    Using an alternative ssh port

    Annoyingly SVN does not understand the ‘username@domain:port’ syntax. It doesn’t know what to do with the port. However you can use svn’s configuration files to create a custom ‘protocol’. This allows you to add support for an alternative port through the configuration file.

    You client user should have a file

    ~/.subversion/config
    

    If not, create it. In it there should be a [tunnels] section. To that you can add a new ‘protocol’. I created sshhome as follows:

    [general]
    sshhome = ssh -q -p 1234
    

    The -q option prevents the “Killed by signal 15″ message that you see with some versions of subversion. You can then use sshhome instead of ssh as the protocol and it will use the specified port. E.g.

    $ svn list svn+sshhome://user@some.domain.org/my_first_project
    

    Public key authentication

    Finally to save having to enter your password all the time you can create a public key and install it on the server. On the client change to the ~/.ssh directory. Now if there’s already a file in it called id_dsa or id_rsa STOP. I’m not sure how to support multiple keys yet and create a new key will over write these files. If all is well create a set of keys as below. Do not use an empty passphrase. It’s also good security not to use your password as the passphrase.

    $ ssh-keygen -t dsa -C "username@some.domain.org"
    Generating public/private dsa key pair.
    Enter file in which to save the key (/Users/username/.ssh/id_dsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/username/.ssh/id_dsa.
    Your public key has been saved in /Users/username/.ssh/id_dsa.pub.
    The key fingerprint is:
    

    Now you need to put the public key on to the server. Copy the file in to your home directory on the SVN server. You can use scp for that.

    $ scp id_dsa.pub username@some.domain.org:/path/to/home/directory/.ssh
    

    Then log in to the SVN server and change to the ~/.ssh directory. In that directory add the public key to the file of authorized keys and set the permissions. Then delete the public key file. See below:

    $ cat id_dsa.pub  >> ~/.ssh/authorized_keys
    $ chmod 600 ~/.ssh/authorized_keys
    $ rm id_dsa.pub
    

    And that should be it. Try listing your project directory again. This time for the first time it will ask you for your password but then it will only ask for the password infrequently.

    DPC 2010

    Thursday, July 15th, 2010

    A couple of weeks ago I went to the Dutch PHP Conference 2010 and I had an amazing time. The event was held in Amsterdam at the RAI centre. The primary sponsor was iBuildings and the entire event was organised by the totally awesome Lornajane.

    When DPC 2010 announced the call for papers I submitted both a proposal for a tutorial on PHP extensions and one for a talk on web services. Both were excepted. I had proposed both half day and full day versions of the tutorial the organisers chose the full day version. This turned out to be a lot of work. Basically I would be talking for 6 hours in four 90 minute slots.
    (more…)

    DPC2010 – “PHP Extensions Tutorial” and “Web Services For Consumer Devices”

    Saturday, June 12th, 2010

    I am still at the Dutch PHP Conference and very enjoyable it is too. I have now given my two talks. The first was an all day session on writing PHP extensions in C. The second was a regular 45 minute session on Web Services For Consumer Devices.

    PHP Extensions

    This tutorial covered writing PHP extensions from the very beginning right through to writing objects and classes in C. I think this went well. Its a lot for beginners to cover so naturally some people got lost towards the end but I have had some good feedback. Links to the slides and examples are below.

    Slides PHPExtensions-Final-20100612.pdf
    Example – Hello World hello_world-20100612.tgz
    Example – Hello World (Extended) hello_world_extended-20100612.tgz
    Example – Memory Stream memory_stream-20100612.tgz
    Framegrab (PECL) http://pecl.php.net/package/framegrab

    Web Services For Consumer Devices

    This talk covered what to consider when writing a service to be used by an application on a consumer devices. I agree with the commenters who felt it was a little light. Yes it needs revision before it gets reused.

    WebServicesForConsumerDevices-20100612.pdf

    Maker Faire UK 2010

    Thursday, April 22nd, 2010

    So in the middle of March I was panicking about whether I would get everything ready in time for Maker Faire UK. Like last year Maker Faire UK was held at the Life Science’s Centre in Newcastle-upon-Tyne as part of the Science Week there. Unlike last year, this year I had been accepted as an exhibitor and was going to display a variety of projects, so I took the Friday as vacation for final preparation and I needed it. (more…)

    DPC – Dutch PHP Conference 2010

    Thursday, April 22nd, 2010

    Rather belatedly I am delighted to announce that I have been accepted to speak at the Dutch PHP Conference in Amsterdam this June. DPC is the premier European PHP conference and is sponsored by iBuildings.

    I will be running a tutorial Writing a PHP extension in C and speaking about Web Sevices for Consumer Devices. I look forward to seeing you there.

    Capturing still images from video devices in PHP using Framegrab

    Sunday, February 28th, 2010

    I found I had a need to capture and process still video images from video devices using PHP so I wrote the Framegrab PECL extension. This post introduces the basics of Framegrab.
    (more…)

    Serial IO in PHP using the DIO extension

    Monday, February 15th, 2010

    DIO is the Direct IO extension for PHP. I recently took over maintaining this extension and have implemented comprehensive stream support for both POSIX and Windows systems. To demonstrate the use of DIO this post will describe a PHP script that sends an SMS using a USB 3G modem.
    (more…)

    PHP DIO Extension: Looking for beta testers

    Tuesday, February 2nd, 2010

    A few months ago I had a need to do some serial IO from PHP. After asking around I found out there was a PHP extension for this but it was unmaintained, unowned and out of date. To cut a long story short I ended up as the maintainer of said extension and since them I have been fixing and extending it.

    The original DIO API is very basic and POSIX oriented. Serial support is not very configurable and doesn’t work at all on Window platforms. So I have been working on implementing PHP stream extensions that allow you to do raw and serial IO via streams. Anyway its ready for testing by people other than me on POSIX (Linux, OS X etc) and Windows platforms so I’m looking for beta testers.

    (more…)

    Manchester Geek Girl Afternoon Tea

    Monday, January 11th, 2010

    While I’m announcing appearances I am excited to announce that I am presenting an Arduino workshop at Manchester Geek Girl Afternoon Tea this month at Madlab, Manchester’s Hackspace in the Northern Quarter.

    I will be bringing a long several Arduino-a-likes together with a variety of components and CDs of the IDE so that we can get together in groups, code and play.