Allowing user to “sudo” on CentOS 5.5

On Ubuntu, its really simple.

# adduser jak sudo

and you’re done!

On CentOS, theres a couple more steps.
As root, run

# /usr/sbin/usermod -aG wheel jak

then edit the /etc/sudoers file

# nano /etc/sudoers

go down and find this…

## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL

and remove the hash in front of %wheel so it looks like…

## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL

Once that’s done, your user “jak” is part of the wheel group, who are allowed to use sudo.

Check out this Mac app: Scroll Reverser

Scroll Reverser for Mac, at http://pilotmoon.com/scrollreverser

In preparing for Lion, I’ve decided to see how the reverse scrolling (i.e your fingers move the page and not the scrollbar) would pan out on my MacBook. For web pages, I took to it immediately and it felt right straight away. As I went a little further, with documents and editing in TextMate, it also felt natural with only a couple of slip ups reverting to previous behaviour, especially when I was editing rather than just viewing. Indeed, when I really needed to get down to some serious editing, reverse scrolling had to be switched off it was such a distraction!

My issues became a lot worse when it came to Finder though. It didn’t “feel right” when I was scrolling the display of folders and documents, and I repeatedly kept following my instincts and found myself trying to scroll with the scrollbar. I wonder if this is because I don’t feel like I’m moving a document or piece of paper, or maybe, I’ve become used to reverse scrolling through the use of my iPhone – which especially make sense when I’m scrolling web pages, but the iPhone lacks any kind of file browser and I’ve never had to scroll through a similar interface. This also might explain my difficulties when editing documents – something I rarely ever do on an iPhone, as I’m just a consumer of media on that device rather than my MacBook Pro where I’m both consuming and creating.

In summary, the reverse scrolling, which will be default in OS X 10.7 Lion, is natural for web pages and for viewing documents but frustrating for Finder windows where, even if I’m using reverse scrolling fine everywhere else, it just doesn’t “feel right”.

Best way to stop SQL Injection in PHP – Stack Overflow

Use prepared statements. These are SQL statements that sent to and parsed by the database server separately from any parameters.

If you use PDO you can work with prepared statements like this:

$preparedStatement = $db->prepare('SELECT * FROM employees WHERE name = :name');$preparedStatement->execute(array(':name' => $name));$rows = $preparedStatement->fetchAll();

(where $db is a PDO object, see the PDO documentation)

What happens is that the SQL statement you pass to prepare is parsed and compiled by the database server. By specifying parameters (either a ? or a named parameter like :name in the example above) you tell the database engine where you want to filter on. Then when you call execute the prepared statement is combined with the parameter values you specify.

The important thing here is that the parameter values are combined with the compiled statement, not a SQL string. SQL injection works by tricking the script into including malicious strings when it creates SQL to send to the database. So by sending the actual SQL separately from the parameters you limit the risk of ending up with something you didn’t intend. Any parameters you send when using a prepared statement will just be treated as strings (although the database engine may do some optimization so parameters may end up as numbers too, of course). In the example above, if the $name variable contains 'Sarah'; DELETE * FROM employees the result would simply be a search for the string “‘Sarah’; DELETE * FROM employees”, and you will not end up with an empty table.

Another benefit with using prepared statements is that if you execute the same statement many times in the same session it will only be parsed and compiled once, giving you some speed gains.

Oh, and since you asked about how to do it for an insert, here’s an example:

$preparedStatement = $db->prepare('INSERT INTO table (column) VALUES (:column)');$preparedStatement->execute(array(':column' => $unsafeValue));

Securing /tmp on CentOS 5.5 on a VPS

Backup your fstab file
cp /etc/fstab /etc/fstab.bak

Creating tmpmnt partition file (Around 1Gb in size)
cd /vardd if=/dev/zero of=tmpMnt bs=1024 count=1048576

Format the new partition
mkfs.ext3 -j /var/tmpMnt
Press Y when asked

Making backup of old /tmp
cp -Rp /tmp /tmp_backup

Mount the tmp filesystem
mount -o loop,noexec,nosuid,rw /var/tmpMnt /tmp

Set the right permissions
chmod 0777 /tmp

Copy the files back to new tmp folder
cp -Rp /tmp_backup/* /tmp/

Adding new /tmp filesystem to fstab
echo "/var/tmpMnt /tmp ext3 loop,rw,noexec,nosuid,nodev 0 0" >> /etc/fstab

No need for 2 tmp filesystems, so we symlink /var/tmp to /tmp
rm -rf /var/tmp/ln -s /tmp/ /var/tmp

Transporting Pictures over ASP.NET Web Services

So you can send a lot of stuff over web services, mostly primitive types, but what if you want to send pictures?

I’m not talking about sending the URL to a picture, but sending the actual image data over a web service, so that the client can display it. It’s really very simple, but also a fun addition. On the Web Service side, you need to take the image and encode it into a primitive type that can be handled through SOAP. To do this, I load the image, then convert its contents into a Base64 encoded string. I can then send this string back to the client.

Example:

[WebMethod]
public string GetPicture() { 
  // Load the picture 
  Image img = Image.FromFile("Path/To/Picture.jpg"); 
  // Create a memory stream, and put the image contents into it. 
  MemoryStream stream = new MemoryStream(); 
  img.Save(stream, img.RawFormat); 
  // return data, encoded in Base64 
  return Convert.ToBase64String(stream.ToArray()); 
}

Very simple! Now on the client side, we simply reverse the process.

Here is a synchronous example, for a better asynchronous example, have a look at my Visual Studio solution. This example is for a Windows form application, with a PictureBox object.

private void LoadImage() { 
  // Create web service 
  WebServiceSoapClient service = new WebServiceSoapClient(); 
  // Fetch encoded image using the web service method 
  string encodedImage = service.GetPicture(); 
  // turn the Base64 string back into bytes 
  byte[] buffer = Convert.FromBase64String(encodedImage); 
  // create a stream from the data 
  MemoryStream stream = new MemoryStream(buffer, false); 
  // recreate Image object, and display it in pictureBox1 
  Image img = Image.FromStream(stream); 
  pictureBox1.Image = img; 
} 

If you encounter errors, regarding the max length of strings, simply update the properties: maxBufferSize maxRecievedMessageSize and maxStringContentLength in the app.config file, and set the value to which 2048000 should be big enough!

You can download a solution folder, with a fully working example here: http://www.jak.cx/downloads/PicturesOverWebServiceTest.zip

SilverStripe CMS

Last night I found SilverStripe and it is simply amazing. It’s the perfect solution for small simple static websites I find myself building regularly, and with the templates being so well made I can customise them and having a working site in less than 2 hours.

For example, YDiy-CallMe.co.uk and Yours2Use.co.uk (You’ll have to excuse the content on the latter, it’s a work in progress)

Both of these are customised, and were complete in under an hour. The admin interface is incredibly simple, and the help pages will help novice users a lot. I’m also loving how extendable it is. There may be a lack of modules/widgets (or at least a lack of useful ones) but with SilverStripes modular design it’d be very easy to create new ones.

I’m not sure if it’s issues with our work internet, or firefox, or silverstripe, but I’m coming across occasional bugs – I can only hope they’re ironed out. It will be insteresting to see how simple it is to upgrade/update SilverStripe to newer versions.