Exim, Dovecot and MySQL

Few years ago I was setting up SMTP+IMAP with MySQL virtual users and somehow I didn’t post about the installation here. These days I have similar thing to do (bit simplier, but still) and since I retrieved my old configuration description, I decided to post it here for future reference.

The whole setup is based on Exim (for SMTP), Dovecot (for IMAP, but might also be used for POP3) and MySQL (for configuration storage).

So here are the bits of changes/configs relevant to setup:

Continue reading “Exim, Dovecot and MySQL”

Prime-Tel is the best

I have a 4MBit ADSL line from PrimeTel at home. I’ve been noticing that sometimes the speed goes closer to 5MBit, but today morning, when I was downloading some music, I saw a record speed of almost 10MBit! What I can say? PrimeTel is just the best.

Moreover, when I told my brother about this – he pointed out for some testing turbo stuff promo from PrimeTel, which I somehow skipped. Just signed in for it – wonder if the speed will increase even more ;)

(Not that I am greedy or need that much speed – more of the curiosity)

Fedora 13

Upgraded to Fedora 13 – looks fine to me. Used preupgrade. The only problem was that my grub.conf had a setting to disable kernel mod set for video, which I had to comment out after upgrade, since my X server wouldn\’t start otherwise.

jQuery fancybox with ajax

I was fighting with bloody fancybox jQuery plugin the whole day yesterday. While it was working find on loading large image popups in normal scenarios, it was refusing to work when content was supplied through AJAX, meaning that DOM was altered after the fancybox was initialized.

Have tried several methods like using livequery plugin for jQuery, putting fancybox init in success callback of AJAX request and a lot of other things – non helped.

Finally, the solution was to put fancybox reinit in success callback of AJAX request, but not directly. Instead I had to use setTimeout method with delay of 600 to reinit fancybox as follows:

$.getJSON(ajax_url,function(html) {

    // populate my DOM with some HTML here

    // make fancybox reinit
    setTimeout(
        function() {
            $(\"a.some_fancy_box_element_class\").fancybox();
        },
        600
    );
});

This way it is working fine for me. Not sure why timeout is needed and what is the best delay time though. Can assume that DOM needs some time to insert all the elements and that by default fancybox was faster to check it than the actual update was finished.

Keep in mind that standard fancybox init should still remain in document ready event to adjust all elements on initial page load complete.

RT3 ActiveDirectory User Attributes Sync

Ok, here is another small script to deal with RT3 and Active Directory. If you apply the apache ldap auth described previously to your RT3 installation, you will have no problem getting people logged in, but you will still have to adjust their names and emails. Doing this manually is not the best choice, so here is a small script, which can be run from the cron (or manually) to update user info in RT3 according to user attributes in Active Directory:

#!/usr/bin/php
< ?php

# Debug flag. Set to non-zero for verbose output
$debug = 0;

# Settings to use while connecting to active directory
$ldap_host = \"10.10.10.1\"; # AD server
$ldap_user = \"someuser@example.com\"; # User in AD with read writes
$ldap_pass = \"someuser_password\"; # Password for the user above
$ldap_base = \"dc=example,dc=com\"; # AD base to search (recursivly)

# Settings to use while connecting to rt3 MySQL DB
$sql_host = \"127.0.0.1\"; # MySQL server
$sql_name = \"rt3\"; # RT3 DB name in MySQL
$sql_user = \"rt3_user\"; # User to connect to above DB
$sql_pass = \"rt3_pass\"; # Password for the user above

# Map of RT3 -> AD attributes
$attr_map = array(
	\'RealName\'		=> \'displayName\',
	\'EmailAddress\'	=> \'mail\'
);

# Connect to AD and authenticate
$ldap = ldap_connect($ldap_host);
if (!$ldap) {
	die (\"Failed to connect to LDAP server: \" . ldap_error() . \"\\n\");
}
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if (!ldap_bind($ldap,$ldap_user,$ldap_pass)) {
	die (\"Failed to bind to LDAP server: \" . ldap_error() . \"\\n\");
}

# Connect to MySQL
$sql = mysql_connect($sql_host,$sql_user,$sql_pass);
if (!$sql) {
	die (\"Failed to connect to MySQL server: \" . mysql_error() . \"\\n\");
}
if (!mysql_select_db($sql_name)) {
	die (\"Failed to select MySQL database: \" . mysql_error() . \"\\n\");
}

# Get a list of RT3 users from MySQL
$users = get_rt3_users();

# Update attributes for each RT3 user according to AD attributes
foreach ($users as $user) {
	set_rt3_user_info($user,get_ldap_user_attr($user));
}

# Close the connections to MySQL and AD
mysql_close($sql);
ldap_unbind($ldap);

# Gets a list of RT3 users from MySQL
function get_rt3_users () {
	global $sql;

	# Skips the external users (the ones that look like email address)
	$result = mysql_query(\"SELECT Name FROM Users WHERE Name NOT LIKE \'%@%\'\",$sql);
	$users = array();
	while ($user = mysql_fetch_array($result)) {
		array_push($users,$user[0]);
	}
	return $users;
}

# Gets AD attributes for the given user
function get_ldap_user_attr ($user) {
	global $ldap,$ldap_base,$attr_map,$debug;

	if ($debug) { print \"Searching for user $user\\n\"; }
	$result = ldap_search($ldap,$ldap_base,\"(sAMAccountName=$user)\");
	$entries = array();
	if ($result) {
		$entries = ldap_get_entries($ldap,$result);
	} else {
		die(\"Failed to search LDAP: \" . ldap_error($ldap) . \"\\n\");
	}
	return $entries;
}

# Updates RT3 user in MySQL with given AD attributes
function set_rt3_user_info ($user,$attr) {
	global $sql,$attr_map;

	# Construct an update SQL query arguments
	$query = \"\";
	foreach ($attr_map as $k => $v) {

		# Update field only if it is set and non empty
		if (isset($attr[0][strtolower($v)][0]) and $attr[0][strtolower($v)][0] != \"\") {
			$query .= \",$k=\'\" . mysql_escape_string($attr[0][strtolower($v)][0]) . \"\'\";
		}
	}

	# Run the actual query
	$query = \"UPDATE Users SET \".substr($query,1).\" WHERE Name=\'\".mysql_escape_string($user).\"\';\";
	mysql_query($query);
}

?>

Now each time a new user logs in to RT3 and his username appears in RT3 database, this script will update his/her name and email. You can extend a list of mapped attributes to have more info updated if you want so.