Windows 7 vs Demonic Process

The other day I had to code an application that required write access to the registry. This access is not possible if you are not an administrator and so in my code I began checking to see if the entity that launched the application was part of the administrator group. If they were not, then it would relaunch the application (from within itself) via the runas verb, forcing a prompt. The original launch would then wait for the relaunched version of the application to exit and then it would, itself, exit.

I was being very clumsy as I was coding this feature and after launching the app, I noticed no popups and nothing seemed to be happening so I wrote it off to Windows 7 delaying the runas prompt and sat there for about a minute. I then realized something was wrong as nothing was happening and my system began to lag. Opening task manager, I discovered that there were over 2800 processes running…

Using the shell32.dll I had checked to make sure I was an admin (UAC) and if not, prompt and then start the process again as admin (Using “runas” verb with the System.Diagnostics.Process class). This is fine except without a shellexecute (setting it to false in the StartInfo), it skips this UAC check and starts the process again, which checks if admin…etc, etc.

At this point I could not manage to do anything on my computer so I issued a windows restart and watched process manager as it began shutting down the processes at about 20 procs/sec. However, the process itself was still spawning new ones.

It made me giggle a bit watching windows 7 fight my demonic process. Process quantity reported by task manager: 2800, 2777, 2756, 2780, 2755, 2738, …

Lesson learned!

Strong Named Assemblies? What are those?!

Sometimes I feel like that would be the reply, had I asked why the library I just bought from a 3rd party developer wasn’t strong named. Why would they not want to sign their libraries? Maybe they forgot? At any rate, such an occasion has again arisen and this time I decided to blog about it.

I had to build a C# COM wrapper of a 3rd party (C++/CLI) library so that we could use the 3rd party library from within a VFP project. (Yeah, I know…)

Well I make it a habit of strong naming all of my libraries using our company’s private key. However, you cannot strong name a library, unless all of its dependencies are also strong named. Generally this poses a problem when working with 3rd party libraries as you are not always privy to their source code (in fact you rarely are) and without the source, this gets much, much harder.

Being that I had the source, I was able to get around this issue rather effortlessly. My setup includes VS2010 Ultimate edition on an x64 install of Windows 7. The 3rd party’s library (which we will henceforth refer to as LibX) was written in VS2005. So the first thing I had to do was convert the source project to 2010. You will notice that in VS2010, your CLI projects will default to Framework v4.0 (v100 toolset). However, all of my company’s .NET applications are being built against the v3.5 of the Framework.

In order to compile LibX for .NET 3.5, I needed to install VS2008. Within the project’s properties you have the option to change your toolset to v90 (VS2008) however, without VS2008 being installed, the build was failing. After I installed VS2008, I proceeded with the following step:

To change the version of the .NET Framework for C++/CLI projects (VS 2010)
1. Right click on project in Solution Explorer and click Unload project
2. Right click on unloaded project in Solution Explorer and select Edit <projectname>.vcxproj
3. In project XML file locate node <PropertyGroup Label=”Globals”>
4. In that node locate node <TargetFrameworkVersion> (if the node cannot be found, add it)
5. Inner text of the node defines target framework. It can be v2.0,v3.0, v3.5 or v4.0
6. Save vcxproj file and close it
7 .Right click on unloaded project in Solution Explorer and click Reload Project

Example
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>

Note:
These steps apply only for Visual Studio 2010 as it uses new format of C++ project files.

I found the previously quoted section in an msdn article here as was quoted by a user named Đonny.

After this change was made, I then went into the Configuration Properties->General->PlatformToolset and changed it to v90. I was now compiling against v3.5 of the .NET Framework. The next thing I had to setup was my strong naming. I did this as follows:

  1. Set Linker->Advanced->Key File to the key file you have generated with sn -k

    Key File: $(MSBuildProjectDirectory)\myStrongName.snk
  2. Set Link->Advanced->Delay Sign to [Yes]
  3. Add a post build event (Build Events->Post-Build Event->Command Line) to sign the dll: sn -R <dll> <key file>
    Command Line: sn -R “$(MSBuildProjectDirectory)\Release\LibX.dll” “$(MSBuildProjectDirectory)\myStrongName.snk”

If not for these steps, you would notice that upon building your application, you would receive a warning in your output window (under build) stating that adding the manifest to the dll makes the strong name invalid and that it would have to be resigned.

I placed myStrongName.snk in the root directory for the LibX project. When I built using the Release configuration, I built to root\Release\LibX.dll.

After importing my library into my C# project, I realized two things. One was that my CLI project had no file/product versions. To get these I added a new resource to LibX, a Version resource. In this resource, you can set all of your file properties, such as company name, file version, copyrights, etc. Satisfied with this I realized that two, there was no assembly version. To add an assembly version, I needed to add an AssemblyInfo.cpp file to my LibX source. In this file I placed a line of code as follows:

[assembly:AssemblyVersionAttribute(“10.1.0.0”)];

That was that! Now after I compiled my LibX project, I had a strong named, versioned, 3rd party library, that I could import into my own libraries and still maintain my strong naming convention.

A few things to note about this setup:

  1. This will only work if you are using VS2010. You can do it with other versions of VS, but the steps may be slightly different.
  2. LibX had no other managed dependencies. If it had, they too would have to be strong named, otherwise I would not have been able to strong name it. (That is not 100% true, it did depend on System and System.Data, but these were already strong named Microsoft libraries).

Another WordPress tutorial, pt 3

In this final installment, I will go over setting up WordPress. In the last post, I went over preparing an FTP server on the Ubuntu box. We are now, finally ready to grab a copy of WordPress and install it on our server!

We want to start off by creating a temporary directory (or just using the one we created for Pure-FTP) and navigating into it.

$ wget wordpress.org/latest.tar.gz

And almost as instantly as we pressed enter, we now have the WordPress source in our temporary directory. So extract it.

$ tar xzf latest.tar.gz

Upon extracting this archive, you will notice that there now exists a ./wordpress folder in your current directory. We now want to move this directory to our hosting location. If you remember from when we setup our LAMP server, this directory is /var/www.

$ sudo cp -R ./wordpress/* /var/www/

Listing the /var/www/ directory will show the contents of the WordPress site. Before we get on to configuring it however, we now need to setup our database. Enter MySQL. Luckily for us, this was installed during our LAMP setup so all we have to do now is configure it!

$ sudo mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD('your-password-here') WHERE user='root';
mysql> FLUSH PRIVILEGES;

When you first install MySQL, it uses the root user with no password. We want to set a secure password for our root user. Then we want to create a new account for our WordPress user as well as our WordPress database. While still in the MySQL prompt, continue with the commands below:

mysql> CREATE DATABASE wordpressdb;
mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password-for-user-here';
mysql> GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wpuser'@'localhost'
-> IDENTIFIED BY 'password-for-user-here';
mysql> FLUSH PRIVILEGES;
mysql> EXIT

At this point we now have a MySQL user named wpuser and a database for that user called wordpressdb. The database is currently empty, but it will be filled later. You could have used different names for the user and database if you wanted, just make sure you know what they are.

Next, we setup the WordPress config file. This is a php script that, well, configures WordPress, storing all of its much needed information such as: authentication keys, salts, and your database information. A sample one has been created for you so let us edit it then rename it as follows:

$ sudo vi /var/www/wp-config-sample.php

From in here we press ‘i‘ to go into insert mode and ‘ESC‘ to exit insert mode. After the necessary changes have been made, exit insert mode and type ‘:wq‘ to save and quit VI. Everything we need to change is located in the top portion of the file and should be pretty self-explanatory.

  • database_name_here –> wordpressdb
  • username_here –> wpuser
  • password_here –> password-for-user-here

The next section is a set of 4 keys and 4 salts. These should be replaced with randomly generated strings. The easiest way to do this is to navigate over to the following site. You will be presented with a page containing the 8 defines shown in your wp-config-sample.php file, with the exception, ‘put your unique phrase here‘ will contain random strings. Something like ‘X6swat]&5X?#,1BI.xti&:v?w{@G&,EY=–sT:q>]-u<qM/~Px(o{)Lk+Ljo+TpL‘. So the easiest way for us to get these strings into our php file is like so (this assumes you are using PuTTY).

  1. Press ESC to exit insert mode.
  2. Using your arrow keys, navigate your cursor to the beginning of the line that starts:
    define('AUTH_KEY',
  3. Press the d key twice and you should see the line vanish. Repeat this step for all 7 “define” lines following this one you just deleted.
  4. Re-enter insert mode by pressing the i key.
  5. Set your cursor (using the arrow keys) to the location where you just deleted the “define” statements, assuming it is not already there
  6. Copy the entire page from the site above by highlighting the page contents and pressing CTRL + C
  7. Right click anywhere in the PuTTY window and you should see your clipboard pasted into VI where the cursor was positioned!
  8. Now press ESC to exit insert mode.
  9. Finally type :wq to save and quit VI.

Your config is now setup and all that is left is to rename the file so WordPress knows to read it.

$ sudo mv /var/www/wp-config-sample.php /var/www/wp-config.php

Navigate to http://yourwebsite/wp-admin/install.php (replacing ‘yourwebsite’ with either the static IP that you set on your Ubuntu box or the public IP/Hostname you have given your box. For example, if you setup a dyndns account, you would enter that url). Assuming that everything went smoothly you should be prompted a few questions regarding setting up your site. If, for some reason, your wp-config.php file cannot be found, WordPress will offer to create one for you, though I do not know how this works as I did not take those steps.

That is all there is to it! WordPress’ administrator dashboard is very intuitive and customizable. If you wish to change your pages theme, then head over to the Appearance section. Other then that, you will have to explore as I am doing. Hope these tutorials helped in getting you up and running! If you need more detailed instructions on installation or help with trouble shooting, ask in the comments section or head over to the WordPress page and start digging. There documentation is very good!

Another WordPress tutorial, pt 2

Assuming you have read the first part of the tutorial, let us now take the next few steps on the path to hosting our own blog.

FTP turned out to be the only difficult step in the whole process. I have chosen to use Pure-ftpd 1.0.29 (as was recommended to me) and all in all, I am satisfied with it. Once you get around it’s couple of quirks it is very to the point. Before we get into that though, I wanted to talk a bit about interacting with our shell Ubuntu server that we setup last time. As I said before, I am a windows guy and since I set this server up as a console I needed a way to interface into it without having to set up a whole other KVM. The software I found is PuTTY. This little SSH front-end is very nice and light weight. Everything I have done in my server (after installing from the CD of course) was done through this. That was one of the main goals behind my choice to setup a console server. I wanted to be able to turn the machine on, stick it in a corner and forget about it. So far, this has been acheived!

Pure-FTP is an FTP server. So even after it is setup on the Ubuntu box, I still need a way to use/test it. For this I chose the FileZilla client. This client is run on my windows machine and is how I interface with my Pure-FTP. So, spin up your shell, and log in. Then create a temporary directory for which to download the Pure-FTP server. Once you have navigated to that directory, run the following command:

$ wget ftp://ftp.pureftpd.org/pure-ftpd/release/pure-ftpd-1.0.29.tar.gz

This command will download the tarball into our current directory. You can then extract the tarball using the following command:

$ tar xzf pure-ftpd-1.0.29.tar.gz

This command will spill the contents of the archive into your current directory. After you have extracted the archive, we will need to compile the server. This requires some extra tools that do not yet exist on our Ubuntu box. We get those “coding essentials” by running the following command:

$ sudo apt-get install build-essential

Once the installer finishes, we now have all the compilers and linkers we will ever need for our Ubuntu server. To compile pure-ftp, follow this documentation. The third section is the one you are interested in, titled: ——Compilation—— (use this switch: --with-puredb). If you wish to skip the documentation on compiling (which I would never recommend), here is the short of it:

Create group for Pure-FTP
$ sudo groupadd pure-ftpd

Create user for Pure-FTP
$ sudo useradd -g pure-ftpd -d /var/empty -s /etc pure-ftpd

Create make file
$ sudo ./configure --with-puredb

Run make file
$ sudo make install-strip

Since we have compiled using the puredb switch, we now need to create users to be able to login to our ftp server. I created two users. One “master” user and a wordpress user. See this documentation on adding user accounts.
(Set the home directory of your wordpress user to: /var/www)

Create linux group for virtual users
$ sudo groupadd ftpgroup

Create linux user for virtual users
$ sudo useradd -g ftpgroup -d /dev/null -s /etc ftpuser

Create virtual user for wordpress
$ sudo pure-pw useradd wordpress -u ftpuser -g ftpgroup -d /var/www
(you will be prompted to enter, then reenter a password for this user)

Update virtual user database
$ sudo pure-pw mkdb

Repeat the above steps to create more users. (Note: you can use the same linux group/user for all of your virtual users and this will make permissions a breeze. Though this may not be wise if you plan to use this FTP server for other things besides your wordpress blog.) After you have compiled and created your virtual users, it is time to start the FTP server up. Do so by running the following command:

$ /usr/local/sbin/pure-ftpd -j -lpuredb:/etc/pureftpd.pdb &

Should you want to use a configuration file to startup your ftp server (instead of the command line), see this documentation. If you ever need to kill the server, do so with this command:

$ pkill pure-ftpd

Once your FTP server is up and running, you should now be able to log into it with both accounts you created using your Filezilla client. You can then go into your router (optionally) and setup port forwarding so that you can access your ftp server from outside your local network.

The first time I tried to log into my FTP server, I noticed that I could log in but could not upload/download anything. This was due to the fact that permissions were not set on my home directory for my ftp accounts. When you create your virtual users, you assign them to a linux user group/user. This group/user must have access to read/write to the files in the virtual users home directory. You can set this using the following command:

$ sudo chown -R <uid>:<group> <path>
$ sudo chmod -R 755 <path>

where ‘uid’ is the name of the linux user (created using the useradd command) you have assigned your virtual user to, ‘group’ is the name of the linux group (created using the groupadd command) you have assigned your virtual user to and ‘path’ is the virtual user’s home directory.

In the final part of this tutorial I will go over the actual download/install/setup of WordPress. Very simple stuff!

Another WordPress tutorial, pt 1

Though I don’t remember where, I recently read an article about starting your own technical blog as a way to further your career.  I do a lot of development these days and so I also feel I have many experiences to share.  So then, where to start?  Well what a better segue into the blogging world then how I setup my own blogging environment!

After doing some research, all fingers pointed to WordPress.  The decision came down to hosted (wordpress.com) or self-hosting (wordpress.org).  Being the DIY kind of guy, I decided to roll my own.  I am a Microsoft fan boy and loathe all that is Apple.  Linux however, is  a middle ground that I neither hate nor love, but have never heard anything bad about.  Only that it has a steep learning curve.  I have setup a Windows server in the past and found it quite difficult (assuming you wanted to do anything outside the box). Windows in and of itself is pretty flaky, there are so many factors and variables that things are bound to go boom!  So it was an easy decision to give Linux a shot.  I had briefly tried to setup a SuSe GUI server in the past, and it was quite simple, but I did little with it.  Used it mainly to host my subversion code repository.  This time around I decided to go purely shell and a friend recommended using Ubuntu.  With a spare rack unit (though you could use any old box, especially since it is Linux) lying around, my OS decided upon and my blogging software of choice, I was now ready to setup my blogging system.

Hardware

  • Linksys Cisco Router [WRT54G2]
  • Dell Poweredge 2800

Software

The last 3 pieces of software I have not provided links to as they are installed as a package in Ubuntu.  So first things first.  Download Ubuntu server and burn it to a CD.  I use Roxio as my burning software but of course you can use which ever you prefer.  Installing Ubuntu is a snap.  I used the canned package and just clicked next through the menus as they came up, installing no extra tasks when I was prompted.  Everything went smoothly.

A static IP address had to be setup for the server box so I did that next. Time to setup the LAMP server. Assure that your internet is in working order by trying to ping google.com from within your shell. When I set my static IP, I messed up my routing tables. In order to correct the tables I needed to use the route command. After verifying that you have internet connectivity run sudo tasksel install lamp-server from the shell. After this command finishes, PHP, Apache and MySQL will be installed and ready to go on your server. Apache is initially configured to host on port 80. To test that your Apache is running correctly (as well as your PHP), make a simple php page and drop it into the /var/www directory.

user@server:~$ sudo vi /var/www/info.php
i (brings you into insert mode)

<?php
phpinfo();
?>

ESC (brings you out of insert mode)
:wq (saves the file and quits vi)

You should now be able to navigate to to the static IP address that you set for your server to see the page you just created. For example, if you set your static IP to 192.168.1.100 then (using a browser) navigate to http://192.168.1.100/info.php. If you are setup correctly, you should see a page full of information. All you have to do now is setup a port forward on your router to the static IP address you set for your server on port 80. Now whenever you navigate to your external IP, you will be brought to your hosted website. At this point I recommend, at the very least, signing up for a dynamic DNS address. As an alternative, you may wish to purchase a domain name. Personally, I have been using no-ip.com for a few years and I would recommend them to anyone looking to register and manage their DNS.

The next thing we want to do is setup our FTP. In part 2 I’ll show you what I went through setting up Pure-FTP and how to get ourselves ready for the final step, installing WordPress.

Return top