Monthly Archives: February 2014

Open a DOS Prompt in Specific Folder

I use the Windows command prompt a lot and have always wondered if you could open it in a specific folder from Windows Explorer. To save myself all that tedious mucking about changing directories (cd \, cd .., long file names urghhh) once the command prompt was open. After digging about a bit today I’ve hit on a fabulous way of doing it. Here’s how:

Navigate to your folder of choice in Windows Explorer

Choosing a Folder in Windows Explorer

Choosing a Folder in Windows Explorer

Type CMD into Windows Explorer address bar

Type CMD into the address bar

Type CMD into the address bar

Enjoy your DOS prompt window goodness

DOS Prompt Window Goodies

DOS Prompt Window Goodies

Checking if Someone is Logged into MyBB

I run a little web forum that uses MyBB. I’ve been building some tools for members of the forum to use and thought it would be good to restrict access to those tools to logged in forum members. Turns out this was VERY easy to do. MyBB drops a cookie for logged in users called mybbuser. This cookie contains data in this form:

uid_loginkey

uid and loginkey are columns from the MyBB users table. So, checking if someone is logged into MyBB is simply a matter of checking for the existence of the mybbuser cookie which can then be exploded to select the users details from the MyBB table. Here’s some code I knocked up in PHP to select the MyBB username if the user is logged into my forum.

<?php 
	if (isset($_COOKIE['mybbuser']))
	{
		
		$user_details=$_COOKIE['mybbuser'];
		$user_details=explode("_",$user_details);
		
		$database="database_name";
		$server="localhost";
		$user="mysql_user";
		$password="password";
		$table_prefix="mybb_";
		
  	        $link_id = mysql_connect($server,$user,$password);
         	@mysql_select_db ($database);	
		
		$username='';
		
		$result=mysql_query("select * FROM ".$table_prefix."users where uid=".$user_details[0]." and loginkey='".$user_details[1]."'");
		if ($result)
		{
	  	if (mysql_num_rows($result)>0)
	  	{	
		  	$row=mysql_fetch_assoc($result);
				$username=$row['username'];
			}
		}

	}
	
?>

You can now use that username anywhere in the web page. Or use it as a conditional check to display certain content to logged in MySQL users and different content to non logged in users.

Converting Microsoft Error Numbers into Something Useful

For whatever reason sometimes when I’m using OLEDB or ADODB error numbers are given as longs rather than as the more useful HEX numbers. More often than not these days when OLEDB or ADODB errors are thrown there’s no accompanying description so having the error number in a useful format is vital so I can work out what the heck the error actually is. The long format is not useful but the HEX format is because that’s the number Microsoft use in all their documentation. So how do you convert an error like this to something useful:

Error Number:-2147467259
Error Description:

Turns out it’s fairly simple. Just fire up the Windows calculator and put it into programmer mode, choose the Dec number format and enter the error code. It should look something like this:

Error in Decimal Format

Error in Decimal Format

Once you’ve done that convert the long to a HEX number and it should look like this:

Error in HEX Format

Error in HEX Format

The vital thing we see her is the last 8 digits. The error number is 80004005 which is then easily Googled. It turns out that it’s some sort of error establishing a connection to a database using OLEDB.

Installing Munin on a Linode Node

I’ve followed this guide for installing Munin on my nodes at Linode a few times now. Munin is a remote server monitoring tool that allows you to monitor all sort of things about the health of your servers. Servers you want to monitor are “munin-nodes” and the server where you view all of the reports from is the “munin-master”. It all makes sense but the Linode installation guide fails horrifically if your apache webroot is not the default /var/www that Munin expects it to be. So as a reminder to myself, here’s how I got Munin working on a server where the web root was /srv/www.

First, follow the install guide exactly as instructed by the good folk at Linode. Then modify /etc/munin/apache.conf to remove references to /var/www and change them to your web root of choice. My file looks something like this:

Alias /munin /srv/www/munin
<Directory /srv/www/munin>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Order allow,deny
                allow from all
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault M310
    </IfModule>
</Directory>

Then I restarted Munin with:

sudo /etc/init.d/munin-node restart

I happen to use my munin-master as a dumb web host so the pretty Munin web pages are just in a folder off of the web root. You could be tricky and put them on their own sub-domain but I’ve chosen not to do that. So they live in a sub-folder and I can access them using this address:

http://www.your-site.com/munin/

And when I did I was seeing a 403 forbidden error. And when I took at look at the apache error log saw an entry like this:

[Mon Feb 03 04:09:23 2014] [error] [client 123.123.123.123] client denied by server configuration: /var/cache/munin/

Clearly Munin is still trying to access a folder outside of my webroot. So then it was a matter of adding this new section to my /etc/apache2/sites-available/default file:

        <Directory /var/cache/munin/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

Then restarting apache with

sudo /etc/init.d/apache2  restart

Now if I visited the Munin pages using the same address as above I can see pretty graphs telling me all about my servers! w00t!

Installing the RFC 868 TIME Service on Ubuntu

One of my products uses an old OCX control that can query NIST and old RFC 868 TIME servers to get the current time. To make things easier for the users of the product I have a server that provided the RFC 868 TIME service for them to use. The main reason I did this is that service for such an old protocol is a little patchy and I wanted them to have a server they could rely on. Main problem is that the old protocol isn’t included in Ubuntu any more (the newer NTP protocol is supported though with NTPDATE or NTPD). So I’ve had to work out how to enable the RFC 868 Protocol on one of my new servers. Turns out it was pretty simple. First download and install the XINETD super-server.

sudo apt-get install nfs-common nfs-kernel-server xinetd

Then edit /etc/xinetd.d/time to enable the time server. I changed mine to the following because I only wanted to use the TCP version.

service time
{
        disable         = no
        type            = INTERNAL
        id              = time-stream
        socket_type     = stream
        protocol        = tcp
        user            = root
        wait            = no
}

# This is the udp version.
service time
{
        disable         = yes
        type            = INTERNAL
        id              = time-dgram
        socket_type     = dgram
        protocol        = udp
        user            = root
        wait            = yes
}

Final step was to restart the XINETD super server with:

sudo /etc/init.d/xinetd restart

Note to self. Ditch the OCX control and build something in .NET that uses the newer protocol on port 127.