20080416

password management in python

Problem:
How to, change existing password of USER using python.

Solution:

import crypt,random
from subprocess import Popen, PIPE
DES_SALT = list('./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
SALT = "%s%s"%(random.choice(DES_SALT),random.choice(DES_SALT))
DESHASH=crypt.crypt(PASS,SALT)
inout=Popen(["/usr/sbin/usermod","--password",DESHASH,USER],stdin=PIPE, stdout=PIPE,stderr=PIPE).communicate()

Instead of Popen one could write directly to shadow file

apache - redirecting main page to current date

Problem:
Automatic redirection of main page request to current year and month.
Automatic fixing of broken date links (YEARMONTH to YEAR/MONTH/)


Solution:
Use mod_rewrite magic :D

RewriteEngine on
RewriteRule ^/([0-9]{4})([0-9]{2})$ http://%{HTTP_HOST}/$1/$2/ [R,L]
RewriteRule ^/$ http://%{HTTP_HOST}/%{TIME_YEAR}/%{TIME_MON}/ [R,L]

(1.) enables RewriteEngine
(2.) matches http://host.domain/dddddd and transforms it into http://host.domain/dddd/dd
where $1 is the first match - a group of four digits ([0-9]{4}) and $2 is the second match (group of 2 digits)
(3.) redirects main page (http://host.domain/) to http://host.domain/month/year/