fixing dovecot quotas, phpmyadmin special user, opendkim missing key at upgrade time

This commit is contained in:
Benjamin Sonntag 2014-09-29 17:37:13 +02:00
parent b0a5e23299
commit 3487a95e2c
4 changed files with 208 additions and 0 deletions

15
install/upgrades/3.4.0.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
# this script is for 3.1.2 or 3.2.2
# we named it 3.4.0.sh since some of us had a 3.3.0~rc1.sh installed at some point in time
# which means the alternc_status table have this number in mind.
# so we need to have a bigger one
echo "Fix OpenDKIM key generation"
/usr/lib/alternc/alternc_fix_opendkim.php
echo "Fix phpmyadmin special user"
/usr/lib/alternc/alternc_fix_myadm_users.php
echo "Fix of dovecot quotas"
/usr/lib/alternc/update_quota_mail.sh

102
src/alternc_fix_myadm_users.php Executable file
View File

@ -0,0 +1,102 @@
#!/usr/bin/php
<?php
/* Fix the $uid_myadm mysql users access.
This script is idempotent and can be launch anytime
usually after an AlternC upgrade
*/
$f=@fopen("/etc/alternc/my.cnf","rb");
if (!$f) {
echo "Can't open /etc/alternc/my.cnf !\n";
exit(1);
}
$mdb=""; $mhost=""; $muser=""; $mpass="";
while ($s=fgets($f,1024)) {
if (preg_match('#database="([^"]*)#',$s,$mat)) {
$mdb=$mat[1];
}
if (preg_match('#host="([^"]*)#',$s,$mat)) {
$mhost=$mat[1];
}
if (preg_match('#user="([^"]*)#',$s,$mat)) {
$muser=$mat[1];
}
if (preg_match('#password="([^"]*)#',$s,$mat)) {
$mpass=$mat[1];
}
}
fclose($f);
if (!$mdb || !$mhost || !$muser || !$mpass) {
echo "Can't find data in /etc/alternc/my.cnf\n";
exit(1);
}
function create_pass($length = 8){
$chars = "1234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
$password = "";
while ($i <= $length) {
$password .= @$chars{mt_rand(0,strlen($chars))};
$i++;
}
return $password;
}
$res=mysql_connect($mhost,$muser,$mpass);
if (!$res) {
echo "Can't connect to MySQL !\n";
exit(1);
}
if (!mysql_select_db($mdb)) {
echo "Can't connect to DB MySQL !\n";
exit(1);
}
// Fix a bug in 3.0.0
mysql_query("UPDATE dbusers SET enable='ACTIVATED' WHERE name!=CONCAT(uid,'_myadm');");
$r=mysql_query("SELECT * FROM db_servers",$res);
$srv=array();
$client=array();
while ($c=mysql_fetch_array($r)) {
$srv[$c["id"]]=mysql_connect($c["host"],$c["login"],$c["password"]);
if (!$srv[$c["id"]]) {
echo "Can't connect to server having id ".$c["id"]." at host ".$c["host"]." EXITING !\n";
exit();
}
$client[$c["id"]]=$c["client"];
}
$r=mysql_query("SELECT uid, login, db_server_id FROM membres;",$res);
while ($c=mysql_fetch_array($r)) {
$membres[$c["uid"]]=array($c["login"],$c["db_server_id"]);
}
foreach($membres as $uid => $data) {
$membre=$data[0];
$srvid=$data[1];
$ok=@mysql_fetch_array(mysql_query("SELECT * FROM dbusers WHERE uid=$uid AND NAME='".$uid."_myadm';",$res));
if (!$ok) {
echo "Creating user ".$uid."_myadm for login ".$membre."\n";
$pass=create_pass(8);
mysql_query("INSERT INTO dbusers SET uid=$uid, name='".$uid."_myadm', password='$pass', enable='ADMIN';",$res);
echo mysql_error();
} else {
$pass=$ok["password"];
}
echo "Granting rights to user ".$uid."_myadm for login ".$membre." ... ";
// Now granting him access to all user's databases
mysql_query("GRANT USAGE ON *.* TO '".$uid."_myadm'@'".$client[$srvid]."' IDENTIFIED BY '$pass';",$srv[$srvid]);
echo mysql_error();
$t=mysql_query("SELECT * FROM db WHERE uid=$uid;",$res);
echo mysql_error();
while ($d=mysql_fetch_array($t)) {
mysql_query("GRANT ALL ON ".$d["db"].".* TO '".$uid."_myadm'@'".$client[$srvid]."';",$srv[$srvid]);
echo " ".$d["db"];
echo mysql_error();
}
echo "\n";
}

91
src/alternc_fix_opendkim.php Executable file
View File

@ -0,0 +1,91 @@
#!/usr/bin/php
<?php
// create the missing opendkim keys and update dns zones accordingly when necessary.
$f=@fopen("/etc/alternc/my.cnf","rb");
if (!$f) {
echo "Can't open /etc/alternc/my.cnf !\n";
exit(1);
}
$mdb=""; $mhost=""; $muser=""; $mpass="";
while ($s=fgets($f,1024)) {
if (preg_match('#database="([^"]*)#',$s,$mat)) {
$mdb=$mat[1];
}
if (preg_match('#host="([^"]*)#',$s,$mat)) {
$mhost=$mat[1];
}
if (preg_match('#user="([^"]*)#',$s,$mat)) {
$muser=$mat[1];
}
if (preg_match('#password="([^"]*)#',$s,$mat)) {
$mpass=$mat[1];
}
}
fclose($f);
if (!$mdb || !$mhost || !$muser || !$mpass) {
echo "Can't find data in /etc/alternc/my.cnf\n";
exit(1);
}
$ALTERNC_ROOT="/var/alternc/html";
$f=@fopen("/etc/alternc/local.sh","rb");
if (!$f) {
echo "Can't open /etc/alternc/local.sh !\n";
exit(1);
}
while ($s=fgets($f,1024)) {
if (preg_match('#ALTERNC_HTML="([^"]*)#',$s,$mat)) {
$ALTERNC_ROOT=$mat[1];
}
}
fclose($f);
$ALTERNC_ROOT=rtrim($ALTERNC_ROOT,"/");
if (!file_exists("/usr/bin/opendkim-genkey")) {
echo "opendkim-tools not installed, please launch:\n";
echo "apt-get install opendkim-tools\n";
exit(1);
}
$res=mysql_connect($mhost,$muser,$mpass);
if (!$res) {
echo "Can't connect to MySQL !\n";
exit(1);
}
if (!mysql_select_db($mdb)) {
echo "Can't connect to DB MySQL !\n";
exit(1);
}
$hasdoneone=false;
$r=mysql_query("SELECT domaine FROM domaines where gesmx=1 AND gesdns=1;");
while ($c=mysql_fetch_array($r)) {
if (!file_exists("/etc/opendkim/keys/".$c["domaine"]."/alternc.private") ||
!file_exists("/etc/opendkim/keys/".$c["domaine"]."/alternc.txt")) {
echo "Creating Opendkim key for domain ".$c["domaine"]."\n";
if (!is_dir("/etc/opendkim/keys/".$c["domaine"]."")) {
if (!mkdir("/etc/opendkim/keys/".$c["domaine"]."")) {
echo "Error creating the directory /etc/opendkim/keys/".$c["domaine"]." !\n";
} else {
echo "Created the directory /etc/opendkim/keys/".$c["domaine"]."\n";
}
}
chdir("/etc/opendkim/keys/".$c["domaine"]."");
passthru("opendkim-genkey -r -d ".$c["domaine"]." -s alternc 2>&1");
passthru("chown opendkim:opendkim alternc.private 2>&1");
mysql_query("UPDATE domaines SET dns_action='UPDATE' WHERE domaine='".$c["domaine"]."';");
$hasdoneone=true;
}
}
if ($hasdoneone) {
echo "I created some keys, launching update_domaines...\n";
passthru("/usr/lib/alternc/update_domains.sh 2>&1");
} else {
echo "I did nothing, opendkim seems fine...\n";
}

0
src/quota-warning.sh Normal file → Executable file
View File