Saturday, February 2, 2008

Using SFU/SUA to replace Cygwin?

Microsoft's Services for Unix / Subsystem for Unix-based Applications (SUA) might make an excellent replacement for Cygwin.

Since SFU/SUA interacts directly with the Windows kernel, it should be several times faster than Cygwin. This might make SFU an excellent platform for the porting of *nix applications to Windows.

With a good package manager, SFU can be the Windows equivalent of Fink (which is an excellent product). It should be able to compile MOST *nix applications with a few minor modifications.

Several package managers have SFU/SUA ports underway, including Gentoo and Debian. An easy way to install those should dramatically increase their use as.

SUA one other major advantage above Cygwin: It is included with Windows 2003 R2 and Windows Vista (Enterprise and Utimate)

Compiling (relatively) simple programs under SFU

Microsoft Services for Unix (SFU) provides a basic Unix-like environment under Windows and is useful for many things. It allows you to use things like *nix shell scripts and many *nix utilities under Windows.

For the instructions posted below, I assume that you already have a working bash installation. See this post as well as this information (the last link is the newer information) to get a working bash version installed. You may need to disable DEP from boot.ini in order to get bash running.

The steps that I took before attempting to install any packages:
  • Installed Interop System's installer and bash
  • Installed the GNU SDK from the SFU installer
  • Installed the following development-related packages: (everything on this list might not be needed)
    • autoconf
    • automake
    • c89
    • gmake
    • install
    • m4-gnu
  • Renamed /bin/sh and replaced it with a symbolic link to /usr/local/bash (configure scripts usually crashed with a NT exception on the standard sh (ksh IIRC))
Once sh is replaced with bash, configure scripts run fine (assuming you're not attempting to build large, complex programs with several dependencies)

Using the normal "./configure", followed by "make && make install" procedure I have successfully compiled and installed the following applications:
  • sarg
  • nano
It is also possible to run Gentoo under SFU, which should provide a nice package manager that automates compilation. Gentoo under Interix (The name of SFU's Unix) was originally a April's fool joke. However, according to this page in Gentoowiki it appears to be possible. A HOWTO can be found here.

Learning vi

For some reason, probably laziness, I never bother learning one of the classic *nix text editors, vi or emacs. Normally, I simply use nano / pico since there is no need to learn them first, and I usually avoid programming from the command-line.

However for some reason MS Services for Unix's (SFU) version of crontab seems to ignore the EDITOR environment variable (I actually compiled nano from source) and always uses vi as the editor. This forced me to learn vi.

I found a nice little tutorial on using vi which probably a good thing to read if you are using any form of unix.

Friday, February 1, 2008

Running squid on Windows SBS 2003

I have a Windows Small Business Server 2003 Standard SP2 on my network and needed a proxy server in order to somewhat reduce the bandwidth usage.

I decided on squid, since it is free, well-supported and run on Windows. I decided on the SquidNT version, although the version available (free registration required for download) for Microsoft Services for Unix (or Subsystem for Unix Applications in newer Windows versions) should work as well.

SquidNT does not include an installer and is simply extracted to the hard drive (preferably to "c:\"). It requires a bit of configuration before starting. (Simply run "squid.exe" from the command-prompt to attempt to run it) The log file ("C:\squid\var\logs\cache.log") also provides help figuring out why squid would not start. You need to generate cache folders before squid would start. ("squid -z")

Once squid is running the configuration can be tweaked a bit further. (For basic configuration see here; for advanced tweaking see here).

Once configuration is complete, it is a good idea to install it as a service ("squid -i") and set it to start automatically from "Services" under Administrative tools.

For log file analysis I use SARG, combined with the sarg-reports script. (get my configured version here) Since sarg-reports is a unix shell script and expects to be run form a Unix-like environment, I recommend installing Microsoft Services for Unix (SFU). Once SFU is installed, install Interop Systems' package manager. After that, it might be a good idea to install a few packages, install at least bash and mktemp ("pkg_update -L bash" for bash). Certain SFU applications need DEP to be turned off (AlwaysOff mode). This might affect security negatively.

For SARG's installation, I recommend downloading its source code from sourceforge. Extract the source code with the following command: "tar -xzf sarg-" (replace filename with correct version). Change into the correct directory and run "./configure" followed by "make" and "make install". Sarg's configuration file can be found in "/usr/local/sarg". Edit it to correctly reflect the paths of your log files and web server documents directory. (/dev/fs/C/inetpub/wwwroot for IIS) You might want to creat a symlink from /var/www/html as well (since sarg sometimes ignores its config file).

The next step would be to install sarg-reports and log rotation. Copy sarg-reports to /usr/local/bin (C:\SFU\usr\local\bin from a Windows POV). Make it executable (chmod +x /usr/local/bin/sarg-reports). Edit sarg-reports in a text editor (supporting unix text files, such as notepad2) and modify the first line to read "#!/usr/local/bin/bash". Set any other settings that you wish.

Sarg-reports needs the GNU coreutils version of date to run. Download and install coreutils from here. Copy date.exe, libiconv2.dll and libintl3.dll from "C:\Program Files\GnuWin32\bin" to c:\SFU\common (backup original first). Rename /bin/date and replace it with a symbolic link to /common/date.exe. Attempt to run sarg-reports to ensure that it returns no errors.

Now the cron entries need to be added. Run "crontab -e" and add the following: (followed by Ctrl-D) (all text might not show correctly at a low resolution and high zoom. View source to copy all text)
00 *      * * * /usr/local/bin/sarg-reports today
00 00 * * * /usr/local/bin/sarg-reports daily
00 01 * * 1 /usr/local/bin/sarg-reports weekly
30 02 1 * * /usr/local/bin/sarg-reports monthly && /dev/fs/C/squid/sbin/squid.exe -k rotate -n squid

Run "crontab -p" in order to save your password for use by cron.

SARG should run regularly now.