# cups-pdf-dispatch.conf
# Configuration file for cups-pdf-dispatch.
# This file is interpreted by perl.
# This is (tested) example of using LDAP to fetch user info/e-mail.

#### LOCAL CODE ####
my %USERINFO_CACHE = ();
my $LDAP_HOST = 'localhost';
my $LDAP_BASE = 'ou=people,dc=avtomatikarus,dc=com';
my $LDAP_FILTER_FORMAT = '(&(objectClass=inetOrgPerson)(uid=%s))';
my $LDAP_TLS_CAFILE = '/etc/ldap/cacert.pem';
my $LDAP_TLS_VERIFY = 'require';
sub get_userinfo
{
	my( $username ) = @_;
	my $userinfo = {};

	use Net::LDAP;
	my $LDAP = new Net::LDAP( $LDAP_HOST, onerror => 'die' ) or
		die "$@\n";
	$LDAP->start_tls(
		verify	=> $LDAP_TLS_VERIFY,
		cafile => $LDAP_TLS_CAFILE
	);
	$LDAP->bind();
	my $msg = $LDAP->search(
		base	=> $LDAP_BASE,
		filter	=> sprintf( $LDAP_FILTER_FORMAT, $username ),
		attrs	=> [ 'cn', 'sn', 'mail' ]
	);
	my $entry = $msg->shift_entry();
	if( defined $entry ) {
		$userinfo->{realname} = sprintf(
			"%s %s",
			$entry->get_value( 'cn' ),
			$entry->get_value( 'sn' )
		);
		$userinfo->{mailaddr} =
			$entry->get_value( 'mail' );
	}

	$LDAP->unbind();

	return $userinfo;
}
#### END LOCAL CODE ####

# $GET_USER_MAILADDR_SUB
# Reference to a function which converts username to e-mail address.
# Arguments: username
# Returns: e-mail address
# $GET_USER_MAILADDR_SUB = sub{ $_[0]. '@'. hostname() };
#				# (i.e. user@hostname)
$GET_USER_MAILADDR_SUB = sub {
	unless( defined $USERINFO_CACHE{$_[0]} ) {
		$USERINFO_CACHE{$_[0]} = get_userinfo( $_[0] );
	}
	return $USERINFO_CACHE{$_[0]}->{mailaddr};
};

# $GET_USER_REALNAME_SUB
# Reference to a function which converts username to user's realname (used
# when constructing To: header).
# Arguments: username
# Returns: user's real name
# $GET_USER_REALNAME_SUB = sub{ (split( /,/, (getpwnam($_[0]))[6], 2 ))[0] };
#				# (i.e. user's real name from gecos)
$GET_USER_REALNAME_SUB = sub {
	unless( defined $USERINFO_CACHE{$_[0]} ) {
		$USERINFO_CACHE{$_[0]} = get_userinfo( $_[0] );
	}
	return $USERINFO_CACHE{$_[0]}->{realname};
};

