Introduction
============

Through the years computers are being faster and faster, and so with it the
encryption of passwords have to more secure. In this example we convert
passwords stored in MySQL with basic CRYPT-encryption to SSHA256-encryption
(Salted SHA256).

See <Authentication.PasswordSchemes.txt> for a list of supported password
schemes.

We used php to generate the new passwords, but you can use any language you
want

Example
=======

 * Copy the CRYPT-passwords to a new field (newpw) but with the prefix '
   {CRYPT}'. This might start you off in the right direction for mysql: 'UPDATE
   `your_table` SET field_name = CONCAT('{CRYPT}', field_name)'
 * Change dovecot-sql.conf, so it will look at the new fields

---%<-------------------------------------------------------------------------
# Comment default_pass_scheme so dovecot will look at the prefix
# default_pass_scheme = CRYPT

# update your password_query so it will look at the new field
# AND add a %w field in the queury so we have the plain password in our
Enviroment
password_query = SELECT id as user, newpw as password, home as userdb_home, \
uid as userdb_uid, gid as userdb_gid, '%w' as userdb_plain_pass FROM users
WHERE id = '%u'
---%<-------------------------------------------------------------------------

 * Make sure you configured

---%<-------------------------------------------------------------------------
 userdb {
  driver = prefetch
}
---%<-------------------------------------------------------------------------

 * Now reload dovecot, and see everything is still working
 * Make the postlogin-script (which is executed after login) and save it as
   '/usr/local/etc/popafter.sh'

---CodeArea-------------------------------------------------------------------
#!/bin/sh    1
/usr/local/etc/convertpw.php $USER $PLAIN_PASS    2
exec "$@"    3
---CodeArea-------------------------------------------------------------------

 * Make the php-script which updates the password and save it as
   '/usr/local/etc/convertpw.php'

---CodeArea-------------------------------------------------------------------
#!/usr/local/bin/php    1
<?php    2
$mysqlhost  = "localhost";    3
$mysqluser  = "mysqlusername"; // username which is used to connect to the
database    4
$mysqlpass  = "mysqlpassword"; // password which is used to connect to the
database    5
$mysqldb    = "databasename";  // databasename where the passwords are stored  
 6
$mysqltable = "users";         // table where the passwords are stored    7
$idfield    = "id";            // fieldname where the userlogin is stored    8 

$passfield  = "newpw";         // fieldname where the passwords is stored    9 

   10
$usr = $argv[1];   11
$ruw = $argv[2];   12
function hexToAscii($hex){   13
    $strLength = strlen($hex);   14
    $returnVal = '';   15
    for($i=0; $i<$strLength; $i += 2) {   16
        $dec_val = hexdec(substr($hex, $i, 2));   17
        $returnVal .= chr($dec_val);   18
    }   19
    return $returnVal;   20
}   21
$link = mysql_connect ("$mysqlhost", "$mysqluser", "$mysqlpass")  or die
("Could not connect");   22
@mysql_select_db("$mysqldb") or die( "Unable to select database");   23
$result = mysql_query("SELECT $passfield FROM $mysqltable WHERE $idfield =
'$usr' AND$passfield like '{SSHA%'");   24
if (mysql_num_rows($result)==0){   25
        $salt=substr(sha1(uniqid()),18,8);   26
        $salt_ascii = hexToAscii($salt);   27
        $newq= "UPDATE $mysqltable SET
$passfield='{SSHA256.hex}".hash('sha256',$ruw.$salt_ascii).$salt."' WHERE
$idfield='".$usr."'";   28
        $res2 = mysql_query($newq);   29
}   30
exit;   31
?>   32
---CodeArea-------------------------------------------------------------------

 * update your dovecot.conf so it will use the scripts we just made

---%<-------------------------------------------------------------------------
# insert these lines so dovecot uses our scripts
service pop3 {
  executable = pop3 pop3-postlogin
}
service pop3-postlogin {
  executable = script-login /usr/local/etc/popafter.sh
  user = $default_internal_user
  unix_listener pop3-postlogin {
  }
}
# end insert
---%<-------------------------------------------------------------------------

 * now reload dovecot.

As of now each user which connects through POP will convert their password to
SSHA256. If you look at the database you will see for example
{SSHA256.hex}fb0e7f39c88c1d7017169f7f6b9cd6977d1e3291149382b90da4a390a31e81bab3cdced8
instead off {CRYPT}$1$.gvrgDqc$Slvoapz5zkpVmmJAxi.0k1

When every record is updated you can update dovecot.conf (remove the extra
lines), and dovecot-sql (remove the %w-part).

SHA512-CRYPT
============

To use SHA512-CRYPT passwords use '/usr/local/etc/popafter.sh'

---CodeArea-------------------------------------------------------------------
#!/bin/sh    1
DOVECOTPW=$(doveadm pw -s SHA512-CRYPT -p $PLAIN_PASS)    2
/usr/local/etc/convertpw.php $USER $DOVECOTPW    3
exec "$@"    4
---CodeArea-------------------------------------------------------------------

'/usr/local/etc/convertpw.php'

---CodeArea-------------------------------------------------------------------
#!/usr/bin/php    1
<?php    2
$mysqlhost  = "127.0.0.1";    3
$mysqluser  = "postfix"; // username which is used to connect to the database  
 4
$mysqlpass  = "password"; // password which is used to connect to the database 
  5
$mysqldb    = "postfix";  // databasename where the passwords are stored    6
$mysqltable = "mailbox";         // table where the passwords are stored    7
$idfield    = "username";            // fieldname where the userlogin is stored
   8
$passfield  = "password";         // fieldname where the passwords is stored   
9
   10
$usr = $argv[1];   11
$dov = $argv[2];   12
function hexToAscii($hex){   13
    $strLength = strlen($hex);   14
    $returnVal = '';   15
    for($i=0; $i<$strLength; $i += 2) {   16
        $dec_val = hexdec(substr($hex, $i, 2));   17
        $returnVal .= chr($dec_val);   18
    }   19
    return $returnVal;   20
}   21
$link = mysql_connect ("$mysqlhost", "$mysqluser", "$mysqlpass")  or die
("Could not connect");   22
@mysql_select_db("$mysqldb") or die( "Unable to select database");   23
$result = mysql_query("SELECT $passfield FROM $mysqltable WHERE $idfield =
'$usr' AND$passfield like '{SHA%'");   24
if (mysql_num_rows($result)==0){   25
        $salt=substr(sha1(uniqid()),18,8);   26
        $salt_ascii = hexToAscii($salt);   27
        $newq= "UPDATE $mysqltable SET $passfield='".$dov."' WHERE
$idfield='".$usr."'";   28
        $res2 = mysql_query($newq);   29
}   30
exit;   31
?>   32
---CodeArea-------------------------------------------------------------------

selinux
=======

---CodeArea-------------------------------------------------------------------
chcon -u system_u /usr/local/etc/convertpw.php    1
chcon -t bin_t /usr/local/etc/convertpw.php    2
chcon -u system_u /usr/local/etc/popafter.sh    3
chcon -t bin_t /usr/local/etc/popafter.sh    4
---CodeArea-------------------------------------------------------------------

(This file was created from the wiki on 2014-02-11 22:29)
