Archive for September, 2007

Linux disk stuff – what brand drive do you have

I always forget commands I don’t use daily; for instance, to get at least some idea about why, at one provider, more drives were breaking than at another provider, I needed the brand of the drivers. Ofcourse I cannot reach the servers physically, only via SSH. Searching Google didn’t give me much on this, but after a long search in forums, I found it;

apt-get install smartmontools

box46:/disk1# smartctl –all /dev/sdc1
smartctl version 5.36 [i686-pc-linux-gnu] Copyright (C) 2002-6 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

Device: ATA WDC WD2500YS-01S Version: 20.0
Serial number: WD-WCANY1884382
Device type: disk
Local Time is: Fri Sep 28 04:59:32 2007 CDT

And yes, Western Digital drives seem to break (much) more than Samsung and Hitachi for the same work + load.

register_globals Off is not the end of the world

We are getting continues remarks from our hosting users that ‘they cannot work with register_globals Off’.

Register_globals is a way to pass GET & POST variables to a PHP script as normal PHP variables, for instance;

test.php:
echo $test;
?>

test.php?test=hello

would result in

hello

when register_globals is On.

A lot of (older) apps are built on this system as ‘security’ was not a problem before 🙂

Thing is, this is not a security problem per-se, but it invites security issues; if you are not very careful you will introduce them. How? For instance (I actually found this in an open source app… I changed it here to match the same principle in a few lines);

test1.php:
if (!$user) echo “please enter your username”;
else include(‘test2.php’);
?>

test2.php:
if ($user)
mysql_query(“select * from users where user = ‘$user'”);
?>

A malicious user could now do the following;

test2.php?user=’; select * from users;’

or something like that to get all users.

So the preferred way is to make it a bit more difficult for developers so they have to actually think about what they are doing and what variables they are passing; the better way is;

test.php:
echo $_GET[‘test’];
?>

and

test1.php:
$user = $_GET[‘user’];
if (!$user) echo “please enter your username”;
else include(‘test2.php’);
?>

test2.php:
if ($user)
mysql_query(“select * from users where user = ‘$user'”);
?>

executing test2.php as;

test2.php?user=’; select * from users;’

would result in nothing as $user will be empty in test2.php.

Anyway; programmers using ‘the old way’ suck. But to keep the users of your back, you can easily help them by; a) educating them about the dangers b) telling them to rewrite the code. But most cannot or will not. And for them, you can give them this code and pray all will be fine;

foreach($_REQUEST as $k=>$v) $$k=$v;
?>

or;

foreach($_REQUEST as $k=>$v) $$k=$v;
foreach($_COOKIE as $k=>$v) $$k=$v;
?>

Putting this on top of all files (better is one generic header or include file, but even a lot of programs don’t have that….).

It will result in the same idea as register_globals On 🙂

Combine MySQL databases ignoring keys

I would like to do something like this;

on server1; mysqldump mysql > db1.sql
on server2; mysqldump mysql > db2.sql

on server3 (new server);

mysql –force mysql < db1.sql
mysql –force mysql < db2.sql which should give me db1.sql users root etc but it should combination of the users from db1 + db2, so for instance; db1.sql => users{root,a,b,c}
db2.sql => users{root,a,d,e}

on server3 I want to see;

users{root,a,b,c,d,e}

Ofcourse that doesn’t work, as you get duplicate key messages on all double users.

So ofcourse I wrote something to fix that;

I first did;

mysql mysql < db1.sql
mysql mysqlold < db2.sql
PHP:


<?
mysql_connect("localhost", "", "");
mysql_select_db("mysqlold");

$q = mysql_query("select * from mysqlold.user");
while ($r = mysql_fetch_row($q)) {
$q1 = mysql_query("select * from mysql.user where User='".$r[1]."'");
if (mysql_num_rows($q1)) {
continue;
}
$s="";
foreach($r as $r1) {
if ($s) $s.=",";
$s.="'".$r1."'";
}
$qry = "insert into mysql.user values ($s)";
mysql_query($qry);
}
?>

Ofcourse, writing everything yourself was again much faster than searching the solution in the MySQL manual or on Google.