Scripting Dot Macby Matthew Russell
Editor's note: Last week we compared .Mac to Spymac's Wheel and in the process provided a good overview of .Mac services. One of the tools missing, especially for those who want to publish web pages on their .Mac account, was server-side scripting. Well, just because it isn't in the package doesn't mean you can't do it. Take a look at this tutorial and see what you think.
A Server-Side Scripting Hack
The two primary obstacles to overcome in order to incorporate server-side capabilities into a .Mac home page are 1) transparently redirecting users to a different server without having a blatant redirect and URL change happen in the browser, and 2) overcoming periodic IP address changes because of DHCP services.
There are 101 different ways you could complete this hack, but they all follow the same general recipe.
- Pick a server platform.
- Common options include Mac OS X, Linux, Unix, or Windows.
- Pick a server technology.
- Consider choosing Apache (httpd), Tomcat (Jakarta), or Internet Information Services (IIS).
- Install, configure, and get your server running.
- If you're using Mac OS X, this might take ten minutes. If you're doing a Linux install that turns out to be painful, it might take an hour or two.
- Set up a .Mac access page as the gateway to your server using some frames.
- Easy as pie. I'll provide the template.
- Run a simple daemon on your server that redirects to your server's current IP address.
- Not too hard, and I'll even throw in the daemon script.
- Polish it up.
- Use Cascading Style Sheets (CSS), hook in a MySQL database with PHP, run some Java code... the sky's the limit.
From here, let's step through the recipe. Where possible, I provide specifics about issues I encountered with my configuration, but otherwise I try to be as general as possible.
Pick a Server Platform
This step involves selecting a hardware and a software platform.
Presumably, you want to hack this together for free and with minimal hassle. If you have an old box lying around then use it, or if you know someone who's getting ready to watch one fall out of a ten-story window, convince them to give it to you instead. Machines capable of running as a low traffic server sell on eBay for around $1 (literally, but excluding postage and handling), so worst case, you're looking at saving your lunch money for a day or two. Wireless NICs on Linux installations can often cause headaches, so use a simple Ethernet NIC (they also sell on eBay for around $1) if you want things to go smoothly. If you do choose to use your Mac as a server, be sure to have all of the latest security updates and check out this report on securing OS X.
If you're using your Mac, the operating system decision is already made for you unless you want to try installing Linux on your Mac. If you choose to run IIS because you enjoy Microsoft technologies, you need a licensed copy of Windows and should be prepared to stay on top of your security updates. I'd encourage you to acquire an old box and glean the benefits of completing a Linux install/configuration. Learning to administer your own server box is an interesting and rewarding process if that kind of thing even remotely interests you. Other choices that come to mind are installing Pear PC, or installing a stand-alone version of Open Darwin. Just a few of the popular Linux distributions to choose from include Red Hat, Mandrake, Fedora, and SUSE. If you're installing Linux, choose to install everything if possible. In any event, don't skimp on developer tools or server options.
I installed Mandrake Linux 9.1 on an old box lying around the house that still had the original copy of Windows 98 on it, hailing with 64MB of RAM, a 12GB hard drive, and a 500MHz processor. If this gives you any indication, you don't need anything fancy.
Pick a Server Technology
Two factors you should consider when choosing a particular server technology are 1) your desire to learn the technology and 2) how easy it makes the job you want to do. For example, if you're running a Windows server and want to be able to run Perl scripts, you might be making your life a little difficult by choosing IIS. If you anticipate doing lots of database operations and prefer MySQL, you might want to choose a PHP/MySQL combo because they play very well together. If you're really into Java and like what JSPs and servlets can do for you, then Tomcat is probably the way to go. Still unsure? You're pretty safe with Apache. You can always change your mind later.
Install, Configure, and Get Your Server Running
If you're using a Mac, you might want to check out Apache on OS X for some good advice. The latest information on running Apache or Tomcat on virtually any platform is very well documented on the Net.
If you installed a Linux server like Apache (httpd), it's likely that the web server service is already set to startup. If not, find the menu that allows you to configure particular services and go from there. In Mandrake Linux, this is through the "Mandrake Control Center" menu or by typing
drakconf at a prompt as root. An overview of getting Apache/MySQL/PHP running on Red Hat and Mandrake can be found here. If you can type
http://localhost in a browser on your Linux box and pull up a default page that verifies the server is running, then you're golden. For Mandrake 9.1, I was immediately able to type
http://localhost in my browser and see the default Apache startup page without any configuration.
A Quick Aside
After a Linux installation, you might eventually want to remove the monitor and other peripherals from the box and move it out of the way somewhere. You can simply use a secure shell (SSH) to login and remotely administer from Terminal. If you start an X11 Server on your Mac, you can specify the
-X option to SSH (type
man ssh in Terminal) and even startup the window manager to get a graphical display. Check here for a list of other handy commands you'll find useful in the Terminal, most of which work in your OS X Terminal as well. Also remember the man pages are always available by typing
man [commandName] at a prompt.
Set Up Your .Mac Access Page as the Gateway to Your Server
Now that your server is up and running, we need to get the access point on your .Mac home page set up. In Finder choose "Go -> iDisk -> My iDisk" from the menu. In your iDisk's "Sites" directory, copy the following template files into it: frames.html and sidebar.html, and take a look at their source. The interesting page is "frames.html", which tries to load the content from the nonexistent IP Address
123.456.789.012 into the frame.
If you didn't realize it, you can type IP addresses into a browser instead of the name that DNS lookup services use. In Terminal type
ping www.macdevcenter.com and you'll get a response similar to the following (press ctrl-c to stop the output)
PING macdevcenter.com (188.8.131.52): 56 data bytes 64 bytes from 184.108.40.206: icmp_seq=0 ttl=48 time=102.319 ms 64 bytes from 220.127.116.11: icmp_seq=1 ttl=48 time=102.973 ms 64 bytes from 18.104.22.168: icmp_seq=2 ttl=48 time=101.438 ms
If you type
http://22.214.171.124 into your browser, guess where you go? Our plan is to redirect users to our server by replacing the 123.456.789.012 in "frames.html" with our server's IP address.
If you want more guidelines for using frames, look at the W3C Recommendation for frames. They're nice for our cause because they mask a URL redirect to a site without a DNS lookup name, and thus, prevent viewers from bookmarking pages identified by a changing IP address.
Run a simple daemon on your server that redirects to your server's current IP address
This step is the most complex one in the recipe, so let's do a practice run first without the daemon to make sure things are working.
Before you actually get into the daemon specifics that will periodically refresh your IP address, make sure that the overall scheme for redirecting works for your current IP address by replacing the bogus one in "frames.html" with your external IP address and configuring your home network's firewall to allow incoming traffic on port 80. Port 80 is the one that web servers expect to receive requests on. The IP address you need is not the internal one that your firewall/gateway assigned to you if you have a home network. You need the actual IP address your gateway is assigned from its gateway, which is what identifies your home network to the rest of the Net. If you have a home network, typing
ifconfig into Terminal will almost definitely give you an internal IP address, which is not what you want.
My home network uses a 2Wire Home Portal, so I can simply type a specific URL into my web browser. This brings up a control panel to get my external IP address and configure my firewall to allow traffic on port 80. If you're unsure of how to access the control panel for your router, check your router's documentation, search online, or call the manufacturer or Internet service provider for help. It should be a simple mechanical process for DSL and cable modem configurations.
After opening port 80 on your firewall, you should be able to type your external IP address into any web browser in the world and see a template page that displays your web server as running, or go to http://homepage.mac.com/your username/frames.html and have the main frame load content from your server without any visible redirection. If you experience problems, it is likely that your web server isn't running, your firewall isn't letting traffic through, or you've specified the wrong IP address.
Accessing the iDisk via WebDAV from Linux
The script daemon I wrote is provided here and is extensively documented. Take a quick look at it and see how it fits into the architecture diagram below. A simple "kill script" is also provided here for your convenience.
You noticed in the script that I use "cadaver" to accomplish copying the updated "frames.html" page to the iDisk, because the iDisk is where .Mac looks to load content each time a page is requested. Cadaver is free software released under the GNU GPL and the first tool I could find that allowed me to access a WebDAV share (the iDisk) from my Mandrake Linux installation. On any Linux system, you should be able to compile it without hassles just by reading the "INSTALL" and "README" files. This download is the modified source for version 0.22.2 that I hacked to work noninteractively with the script daemon. Check the INSTALL file for some comments I made about the patch and for details about compiling the code. Windows users should be able to use the script daemon and cadaver with a recent Cygwin installation or find an alternative route.
The script daemon should require very little change to work with your configuration. Presumably, you can bring up a web page with "wget" that contains your IP address and filter it out with a regex. You can check here for a piece with a quick refresher on regexes.
Fire It Up
To fire up the daemon, you need to copy it to your Linux box, change its permissions to executable with "chmod u+x IPRefreshDaemon.scr" in a terminal, and run it as a background process by typing
./IPRefreshDaemon & at a prompt. You should be able to go to your iDisk folder and see that "frames.html" has updated to reflect your server's IP address if everything is working properly. Accessing "frames.html" via your .Mac home page should verify that the frames load content from your server.
Polish and Afterthoughts
At this point, you can replace the simple welcome page from your server with your own custom content (check in the "/var/www/html" directory for Apache), use CSS to make it look nice and easy to manage, hook in a MySQL database connection with PHP, or whatever else your heart desires. Keep in mind that by running your own server, you're vulnerable to hacking, so make sure to keep all of your software up to date and do some homework on tightening up security. Aside from the bandwidth and massive processing power of hosting services, you can potentially do anything they can do--and for all the world knows, it's all through your .Mac home page. Who would have guessed? Having made it this far, you really do deserve a pat on the back.
Once again, there are probably scores of ways to accomplish the same objective, so feel free to be creative and do whatever is the most fun for you. This exercise is a perfect opportunity to start a threaded discussion with the "talkback" feature if you have any troubles, or if you find something you'd like to share with others.
Matthew Russell is a computer scientist from middle Tennessee; and serves Digital Reasoning Systems as the Director of Advanced Technology. Hacking and writing are two activities essential to his renaissance man regimen.
Return to MacDevCenter.com.