TFTP Server on Mac OSX 10.8.5

In working with switches and routers, it is very common to TFTP a file to my laptop so that I can save the configuration or output or whatever. It is doable on OSX, but Apple has made a Microsoftian maneuver and requires that a file already exist before the TFTP server can write it. This is a royal pain to have to create an empty file, then TFTP the contents over, and repeat for every single file! Yes, I completely understand that this keeps it safer, but for a protocol that has to be enabled manually anyhow, it is simply a pain! Fortunately, there are options, such as the Open TFTP Server, that we can use to make it work like we want!

Before we can do anything, we need to install Xcode and then install the Command Line Tools for Xcode. This gives us the compiler, linker, libraries, and so on that we need to build software. On my computer, I am running OS X 10.8.5 with Xcode 5.1.1. If I do a cc -v the compiler reports Apple LLVM version 5.0.

The next step is going to be to download the software. Visit the Open TFTP Server SourceForge site and download the multithreaded version, and then unpack it somewhere, perhaps in your Documents folder.

With the code downloaded and unpacked, compiling is remarkably simple, even if you have never compiled a single line of code before! Open up the terminal application and change directory to where you unpacked the source code, which is Documents/opentftp in my case. While not ideal, you can safely ignore the warnings that the compiler spits out.


$ cd Documents/opentftp
$ cc -o opentftpd opentftpd.cpp
...
16 warnings generated.
$

Now we need to move this executable file somewhere. I recommend your bin directory. We'll assume that you don't already have one. Once the file is copied, we will change our current directory to be the bin directory.


$ mkdir ~/bin
$ cp opentftpd ~/bin
$ cd ~/bin
$

TFTP normally operates by listening to UDP port 69 for incoming requests. Port 69 is considered a "well known" port, and as such, only root is able to open it. We have two options here. First, we could use sudo every time we launch our TFTP server to elevate our privilege level, or we could use the setuid bit to automatically have our privilege level elevated whenever the program is executed. We're going for the cleaner latter version, but we have to use sudo to get us there.


$ sudo chown root opentftpd
$ sudo chmod +s opentftpd
$ ls -l opentftpd
-rwsr-sr-x  1 root  staff  55588 Jul 24 09:35 opentftpd
$

If you did everything correctly, your ls output will look like the example above, with the proper permissions and root as the owner.

Open TFTP also needs a config file to enable to options we want, in particular the ability to write files and a home location as to where to write these files. Below is a sample config file. Create it in your bin folder as opentftpd.ini, taking care to update the home folder to something appropriate for your system. Our example uses a folder on the desktop, which we find convenient. Go ahead and create this folder now.


[LISTEN-ON]
0.0.0.0

[HOME]
/Users/Tridata/Desktop/tftp

[TFTP-OPTIONS]
ThreadPoolSize=1
Write=Y
Overwrite=Y

Of course, we want this to be easy to start, so let's automate it with an AppleScript. Open up the AppleScript Editor (under Applications/Utilties) and paste the following, making adjustments to the location of your executable and location of your ini file.


tell application "Terminal"
	set currentTab to do script ("~/bin/opentftpd -v -i ~/bin/opentftpd.ini")
end tell

Then do a File/Export.. and export the script as an Application somewhere convenient. I used TFTPD right on the desktop. Now when I double-click TFTPD on the desktop, I have a terminal window open and the TFTP server automatically launch! Sweet!

When it comes time to shut the TFTP server down, click into the terminal window where the server is running and press Ctrl-C, then you can close the terminal window. If you close the terminal window first, the TFTP server will continue to run in the background, so make sure you do the Ctrl-C first!

As a final note, if you are running a firewall, you will need to make sure it allows for UDP port 69 to get through, otherwise the TFTP server experience will be less than exciting.