I did :-)

1 Comment // Written on Jul 01, 2009 // Monologues

[root@bentley ~]# curl -s -D - http://si2.php.net | head
HTTP/1.1 200 OK
Date: Wed, 01 Jul 2009 08:26:06 GMT
Server: Apache
Last-Modified: Wed, 01 Jul 2009 08:39:37 GMT
Content-language: en
Set-Cookie: COUNTRY=SVN%2C231.127.244.131; expires=Wed, 08-Jul-2009 08:26:06 GMT; path=/; domain=.php.net
X-PHP-QUESTION: I wonder if anyone will ever notice this
Transfer-Encoding: chunked
Content-Type: text/html;charset=utf-8

Create dummy file on freebsd

1 Comment // Written on Jun 04, 2009 // Monologues
  1. dd if=/dev/random of=test bs=1m count=2

This will create 2.0M file called “test”.

  1. [root@bentley ~]# ls -alh test
  2. -rw-r–r–  1 root  wheel   2.0M Jun  4 15:20 test
  3. [root@bentley ~]#

This also works on linux, but he doesn’t understand “m” as unit.

Very big bug! :-)

4 Comments // Written on Apr 17, 2009 // Monologues
  1. [root@box /var/tmp]# ls -alh | grep magi
  2. -rw——- 1 www wheel 566G Sep 15 2008 magick-6kCJDiQf
  3. -rw——- 1 www wheel 4.9T Sep 15 2008 magick-HBj7nGN4
  4. -rw——- 1 www wheel 3.0T Sep 15 2008 magick-IXNRvqtK
  5. -rw——- 1 www wheel 2.7T Sep 15 2008 magick-irgHIYUQ
  6. -rw——- 1 www wheel 566G Sep 15 2008 magick-qDaohFgm
  7. [root@box /var/tmp]#

IE8 madness

3 Comments // Written on Mar 26, 2009 // Monologues

I cannot believe IT! A year ago i was thinking, gr8 Microsoft is getting closer to other browsers and i don’t need to think about IE, like all the time. “Will this work on IE? Is there some kind of a hack to get this work on IE”… IE7 was cool! The frikkin’ madness about IE8 … It comes with a little button called “Compatibility view”. Few weeks ago i started some cool project, which i’ll reveal in the future, without any concerns about IE (I considered IE6 as obsolete and i HANDLE it with “you’re here with IE6 buddy, i’m sorry for you, you cannot see this page”).

I finished with design and checked the site on firefox, chrome, opera, safari - it worked flawlessly. And then i tried it on IE (I accidentally thrown windows 7 beta on my PC and it came with IE8 beta). I could cry when i saw the page… NOT AGAIN! Microsoft took things in his hands… Could not believe my eyes. I clicked “Compatibility view” button when this message popped out: “Websites designed for older browsers will often look better, and problems such as out-of-place menus, images, or text will be corrected with compatibility view”. ?!!?!? It’s like, you’re about to tell me that i don’t know how to make sites for todays standards. (Page is valid - XHTML 1.0 Strict, CSS too, it works in every browser… except your browser!! stupid fucks…).

Nah, when i clicked that button, it all worked OK as it should (i think they’re using IE7 way to display it right). I went to their blog and few other sites to read a thing or two about it. They told me that IE8 is more strict towards web Microsoft standards , and you know what else? There are high-volume sites like facebook.com, myspace.com, bbc.co.uk, and cnn.com which DON’T work properly on IE8. Oh i forgot, msn.com and microsoft.com neither!

Microsoft’s blog: http://blogs.msdn.com/ie/archive/2008/08/27/introducing-compatibility-view.aspx

Here’s one to you Microsoft _.|..! Go fuck yourselves, your stupid browser and releases. I rather break my leg than support your stupid standards.

Apache, basic auth and mysql

1 Comment // Written on Feb 19, 2009 // Geek candies

Today i had some issues with mysql dbd module for apache.

I had two servers: One located in Slovenia and second located in US.
Imagine how rocket-fast this thing was, when i got 50 requests per page and all of them were authenticated via dbd mysql module.

So i decided to wrote my own script which will fetch all the users from database, convert their passwords with proper encryption and then put them into an .htpasswd file. Script will do this every few minutes.

Copy this code into your favorite nix editor, edit variables and save it on chosen location.

  1. < ?php
  2. # Variables
  3.  
  4. define("DB_HOST", "localhost");
  5. define("DB_USER", "htpasswd");
  6. define("DB_PASS", "somepasswd");
  7. define("DB_NAME", "htpasswd");
  8. # data dir - for .htpasswd file
  9. define("ROOTDIR", "/usr/local/www/htpasswd/");
  10. # .htpasswd file - we will call this file from .htaccess
  11. define("HTPASSWDFILE", ROOTDIR . ".htpasswd");
  12. # tmp file for dumping data
  13. define("HTPASSWDTMP", ROOTDIR . ".htpasswd.tmp");
  14. # backup of previous .htpasswd file (if something's foo)
  15. define("HTPASSWDBAK", ROOTDIR . ".htpasswd.bak");
  16.  
  17. # maybe you would want to add some static users,
  18. # which will be here even if database is empty or there are no "valid" users in database.
  19. $static_users = array(
  20.         'staticuser' => 'hispass'
  21. );
  22.  
  23. # FUNCTIONS
  24.  
  25. # htpasswd
  26. function htpass($pass) {
  27.         return crypt($pass, base64_encode($pass));
  28. }
  29.  
  30. # database
  31. function connect_to_db() {
  32.         if(!$link = @mysql_connect(DB_HOST,DB_USER,DB_PASS)){
  33.             die("MYSQL ERROR: " .mysql_error(). "\n");
  34.         }else{
  35.             if(!@mysql_select_db(DB_NAME)){
  36.                 die ("MYSQL ERROR:".mysql_error());
  37.             }else{
  38.                 return $link;
  39.             }
  40.         }
  41. }
  42.  
  43. # init
  44. $link = connect_to_db();
  45.  
  46. $query = "select username, password from protected_users where DATE(NOW()) < expires";
  47.  
  48. if(!$result = @mysql_query($query)) {
  49.         die(mysql_error(). "\n". $query);
  50. } else {
  51.         # create backup file if .htpasswd already exists
  52.         if(file_exists(HTPASSWDFILE)) {
  53.                 copy(HTPASSWDFILE, HTPASSWDBAK);
  54.         }
  55.  
  56.         # dump DB users into file
  57.         $file = fopen(HTPASSWDTMP, 'w') or die("Can't open file");
  58.         while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  59.            fwrite($file, $row[0]. ":" .htpass($row[1]). "\n");
  60.         }
  61.  
  62.         # dump static users into file
  63.         foreach($static_users as $user=>$pass) {
  64.            fwrite($file, $user. ":" .htpass($pass). "\n");
  65.         }
  66.  
  67.         # close links / files
  68.         mysql_close($link);
  69.         fclose($file);
  70.  
  71.         rename(HTPASSWDTMP, HTPASSWDFILE);
  72. }
  73.  
  74. ?>

Create the database and prepare tables:

  1. CREATE DATABASE IF NOT EXISTS htpasswd;
  2.  
  3. CREATE TABLE IF NOT EXISTS `protected_users` (
  4.   `id` INT(11) NOT NULL AUTO_INCREMENT,
  5.   `username` VARCHAR(32) NOT NULL,
  6.   `password` VARCHAR(32) NOT NULL COMMENT 'plain password',
  7.   `expires` DATE NOT NULL,
  8.   PRIMARY KEY  (`id`)
  9. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
  10.  
  11. INSERT INTO `protected_users` (`id`, `username`, `password`, `expires`) VALUES
  12. (1, 'test', 'test', '2012-01-01');

Give some privileges to DB user “htpasswd”:

  1. GRANT SELECT ON htpasswd.* TO 'htpasswd'@'localhost' IDENTIFIED BY 'somepassword';

Now set up your .htaccess file

  1.      AuthType Basic
  2.      AuthName "Protected"
  3.      AuthBasicProvider file
  4.      AuthUserFile /usr/local/www/htpasswd/.htpasswd
  5.      Require valid-user

and cron job:

  1. */5 * * * * cd /path/to/your/script/; /path/to/bin/php yourscript.php

.htpassword will be updated every 5 minutes. If you want to increase/decrease time between updates just change the value in cron job.

Have fun!

BASH - Simple random password generator

2 Comments // Written on Feb 17, 2009 // Geek candies

Ever wondered how cool it will be to have an random password generator in your shell?

Here’s a little bash script i wrote:

Put this

  1. #!/usr/local/bin/bash
  2. if [ -n $1 ]; then
  3.       CHARS=$1
  4.       while [[ "$COUNT" -ne "$CHARS" ]]; do
  5.             let COUNT=COUNT+1;
  6.       done
  7. else
  8.       COUNT=8; # DEFAULT NUMBER OF CHARACTERS
  9. fi
  10.  
  11. /usr/bin/perl -le'print map+(A..Z,a..z,0..9)[rand 62],1..'$COUNT;

into /usr/local/bin/passgen and then chmod it properly:

  1. [root@box ~]# chmod +x /usr/local/bin/passgen

Now try it in action:

  1. [root@box ~]# passgen
  2. UJXG1JMx
  3. [root@box ~]# passgen 15
  4. l3LMgNOuCK3OYnH
  5. [root@box ~]# passgen 1
  6. J
  7. [root@box ~]#

Have fun! :-P

Change users umask - FreeBSD

No Comment // Written on Feb 16, 2009 // Geek candies

If you want to have safe shared hosting u will definitely change some user permissions and the UMASK.

Let me first explain what the umask is:

UMASK is an unix env variable which automatically sets file permissions on newly created files.

By default it is set to 022, which means it creates newly created files with permissions 755. (More about permissions - File system permissions)

Now if we want to set user permissions to 700 , we will change that default umask to 007 for this particular user.

Here is the command to set up users class:

pw usermod username -L someclass

Add this line to /etc/login.conf:

someclass:
	:umask=007:
	:tc=default:

Run the command:

cap_mkdb /etc/login.conf

to rebuild database.

Now were set, login to macihne as user “username”:

ssh username@box

Create some random directory:

[username@box ~]$ mkdir smth

And the directory permissions should look like this:

[username@box ~]$ ls -al | grep smth
drwx------  2 username  username    512 Feb 16 19:04 smth
[username@box ~]$

If you want to have different permissions, just change the value in /etc/login.conf “:umask=007:” to one that fits to you. More about UMASK - UMASK.

D.

Get rid of those stupid ^M’s from your code.

5 Comments // Written on Feb 15, 2009 // Geek candies

Yes i’m geek and i’m using VI. I found tons of ^M’s in wordpress files while i was coding this blog.

Here’s how to get rid of them:

Open your file in vi

# vi file.php

then press “:” and type this:

:%s/[ctrl+v][ctrl+m]//g

[ctrl+v] … hold control and press v
[ctrl+m] … hold control and press m

Voila, there’s no more ^M’s and your code is clean in the way it should be!

Hello world! ;-)

No Comment // Written on Feb 15, 2009 // Monologues

Hi there! ;-)

Firing up a brand new blog, powered by wordpress <3.

It’s beta and i hope it will be an stable release soon. I have a lots of geek candies for you! ;-)