Feb 4, 2013

Laravel: Passing parameters to Controllers and Routers

The basic idea is that you want to pass some parameters to your Laravel router so it can do some preprocessing, and then pass the rest of the parameters to the controller.

To get this happening you need to remember that when registering a route in application/routes.php Laravel captures any wildcards as parameters for the function you are registering. This can either be the controller or a custom function that you are registering. You can also call any action from a controller using the Controller::call() interface.

Putting all this together yields:
Route::get('(:any)/welcome/(:any)', function($client, $action)
{
    print($client . "\n");
    return Controller::call('home@index',array($action));
});

The first line of code is the registration function for Router, and we use the (:any) wildcard to capture almost any alpha-numeric character. The function declaration takes two parameters that Laravel fills with our wildcards (NOTE: Laravel uses positional parameters so the first wildcard will take the first variable, and so on).

The second line just prints out our first positional parameter.

The third line runs Index action of the Home controller, passing our second wildcard as it's parameter.

And that's pretty much it. Simple!

Dec 1, 2012

Migrating and transfering gitolite to a new server

I am going to assume you have already installed gitolite on two servers (if you haven't, check out my guide on installing gitolite). This post will outline how you will move your repositories from one server to your new one.

The server side

  1. You can edit some of the configuration files stored in the home directory of the gitolite user. You only have to do this step if you have an unusual set-up; it should work fine if you did a default install.
  2. Copy the contents of your repositories folder to the new server (except gitolite-admin; we will do this at a later stage). Use either the commands cp or scp. For instance:
    scp -r repo.git/ root@192.168.0.1:/var/lib/gitolite/repositories/
  3. Change the owner and group of the copied repositories:
    chown -R gitolite:gitolite repo.git/
  4. Clone the gitolite configuration repository on your administration machine:
    git clone gitolite@192.168.0.1:gitolite-admin.git
  5. Add the keys and configuration files from your old repository and place them into your new repository. Do a commit and then a push:
    git add .
    git commit -as
    git push
  6. The server is now set up and ready to use! Optional: The guide quoted that you may need to run gl-setup again once the repositories have been copied across, but I didn't need to. You may want to do that step....

The client side

To point your git repository to the new server (so you don't have to reconfigure your IDE or scripts) just run the following commands:

git remote rename origin old
git remote add origin git@192.168.0.1:repo.git
git remote -v
git remote rm old

Further Reading:

Nov 30, 2012

Installing gitolite in CentOS 6

Gitolite is an management service that sits on top of git. It helps restrict users to certain projects (and what they can do on those projects). In this post we will install gitolite in a CentOS 6 environment.

  1. First we need to enable the EPEL repository. You could download and install gitolite directly, but I prefer to manage everything through the package manager for auditing purposes.
    wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
    rpm -ivh ./epel-release-6-7.noarch.rpm
  2. Install gitolite (it will most likely install a variety of dependencies):
    yum install gitlolite
  3. If this is a brand new gitolite installation you will need to create a public SSH key on the account you will be using to administer your gitolite installation. The creation of these keys are outside the scope of this documentation. Once the key pair has been created, copy the public version to a common place where gitolite can access it (like temp). Use the command cp or scp to acheive this.
  4. Rename your copied public key with some sort of identifier. Gitolite uses the name of your keys to determine access.
  5. Log in as the gitolite user:
    su - gitolite
  6. Initialize your gitolite service with the key:
    gl-setup -q /tmp/user.pub
  7. Now you can use gitolite!
    ssh gitolite@192.168.0.1 info
    git clone gitolite@192.168.0.1:gitolite-admin.git

Further Reading:

Nov 29, 2012

Setting up a virtual guest on a headless CentOS 6 host

This guide assumes you have at least followed my guides for setting up the host (either my 6.2 or 6.3 version) and have set up the bridge networking interface. You optionally can see my other posts such as auditing your software installs, hardening your accounts, network hardening, services hardening and clearing out orphaned packages.

Note that if you followed my guide for services hardening you may want to turn the messagebus daemon back on. If the avahi daemon and zeroconf is disabled, you will need to edit /etc/libvirt/libvirtd.conf with the following:
mdns_adv=0
The rest of the guide should apply to virtually everyone else:

  1. Edit /etc/libvirt/qemu.conf to allow the VNC server to listen on all ports:
    vnc_listen='0.0.0.0'
  2. Restart the libvirt daemon:
    service libvirtd restart
  3. If you don't already have one create the LVM partition that we will be our VM's hard-disk:
    lvcreate -L20G -n lv_vm1 VolGroup
  4. Poke a hole in the firewall so we can connect via VNC to the server. You can choose any port you wish, but in this case we will be using port 7601. Make sure you change the network to match your own settings! Edit /etc/sysconfig/iptables
    -A INPUT -m --state NEW -s 192.168.0.0/24 -m tcp -p tcp --dport 7601 -j ACCEPT

  5. Restart the firewall:
    service iptables restart
  6. Run the installation command:
    virt-install -n vm1 -r 512 --vcpus=2 --disk path=/dev/VolGroup/lv_vm1 -c /path/to/disk.iso -v --accelerate -w bridge:br0 --vnc --vncport=7601 --noautoconsole --os-type linux --osvariant rhel6
  7. Now use a VNC client to connect to your server by connecting to the firewall hole we created earlier. Follow through with the rest of the installation process.
  8. To start and stop your VM just use the virsh command. The VM has been configured to use port 7601 for VNC, so you can always connect to it using that port unless you close it.
    virsh start vm1 

Further reading

Nov 28, 2012

Bridge Networking in CentOS 6.3

Bridge networking is a useful technique to allow Virtual Guests to access your networking hardware. This guide was written in mind for CentOS 6.3 but should be applicable to other Linux versions (with modifications).

  1. Copy the file /etc/sysconfig/networking-scripts/ifcfg-eth0 as br0
    cp /etc/sysconfig/networking-scripts/ifcfg-eth0 /etc/sysconfig/networking-scripts/ifcfg-br0

  2. Edit the file /etc/sysconfig/networking-scripts/ifcfg-eth0 and add the line:
    BRIDGE=br0
    You can also delete the lines:
    BOOTPROTO
    IPADDR
    GATEWAY
    DNS1
    DNS2
  3. Edit the file /etc/sysconfig/networking-scripts/ifcfg-br0 and edit the lines:
    DEVICE=br0
    TYPE=Bridge
    You can also delete the lines:
    HWADDR
    UUID
  4. Restart your network:
    service network restart
     

References

Nov 27, 2012

SSH Hardening on CentOS 6.3

This is a follow on post from my guide to installing CentOS 6.2 (or you can read my updated 6.3 version). You can see my other posts such as auditing your software installs, hardening your accounts, network hardening, services hardening and clearing out orphaned packages.

This post outlines how you can harden your SSH server.

  1. Strengthen your IP table firewall rules by editing /etc/sysconfig/iptables and adding or changing the line (NOTE: Any old SSH rule will be using port 22; change it accordingly):
    -A INPUT -m state --state NEW -s network/mask -p tcp --dport 4444 -j ACCEPT
    where network/mask is replaced with your actual network and mask values i.e 10.0.0.0/24
  2. Since SSH uses the TCP wrappers library we will need to allow the service in /etc/hosts.allow
    sshd: 10.0.0.

  3. Edit /etc/ssh/sshd_config with the following changes:
    # Use Port 4444 instead of Port 22
    Port 4444

    # Ensure we use Protocol 2 by default
    Protocol 2

    # Set idle timeouts (15 minutes)
    ClientAliveInterval 900
    ClientAliveCountMax 0

    # Disable rhost behaviour
    IgnoreRhosts yes

    # Do not trust other hosts
    HostbasedAuthentication no

    # Do not allow root logins
    PermitRootLogin no

    # Do not allow empty passwords
    PermitEmptyPasswords no

    #Disable environment alteration
    PermitUserEnvironment no

    #Disable X11 forwarding
    X11Forwarding no

    # Disable TCP forwarding
    AllowTCPForwarding no

    # Log level
    LogLevel INFO
  4. Restart everything

    service sshd restart
    service iptables restart
    service network restart

References

Nov 26, 2012

Fun Stuff: Getting Wolfram Alpha to talk

There was a post on Reddit's /r/math board that inspired me to make Wolfram Alpha bend to my will and say whatever I want it to say. Here is the image of Wolfram Alpha repeating 'make it stop':

0.makeitstopmakeitstopmakeitstop....

So I did a bit of research into the topic and came away with a Python script to figure out the Base-36 fraction that will make Wolfram Alpha repeatedly say an arbitrary sentence.

# The sentence to convert (characters only, no symbols)
q='makeitstop'

# Convert string to a base-36 number. This is numerator
n=long(q,36)

# Get the power of 36 that is equal to the length of the string, minus 1
d=pow(36,len(q))-1

# Print out the equation
print 'convert',n,'/',d,'to base 36'

Once you run the code you just copy and paste the printed result into http://www.wolframalpha.com/

(NOTE: You may need to click on 'Hide Block Form' to force wolfram to use the alpha-numerical representation)

If you want to know more about the equation/formula I used, look at the following post http://mathforum.org/library/drmath/view/61257.html