The restless working iPhone Dev Team has just released the source-code of their commandline tool collection xPwn. Although the tool collection allows to jailbreak (iPhone 2G and 3G) and unlock (iPhone 2G only), it targets mainly at developers.
Get the source-code for Linux, MacOS X and Windows and here. Planetbeing of the iPhone Dev Team highly recommends to read the README before playing around with the tool collection.