[imp] Hook - translate char /create username from full email.

Lauro Costa G. Borges lauro at npd.ufsc.br
Mon Dec 17 12:19:11 UTC 2007


Citando Otto Stolz <Otto.Stolz at uni-konstanz.de>:

> Hi, Lauro Costa G. Borges,
>
> you wrote:
>>  The problem is Horde is set to use Imp as auth app, and Imp does    
>>    not use that hook, of course.
>
> I guess, you mean _prefs_hook_fullname, and perhaps
> _prefs_hook_from_addr.

      No, I also use these, they are ok. I want to change the username to
log in to the IMAP server.

>
>
> Or perhaps, you mean _username_hook_frombackend and
> _username_hook_tobackend? These translate the username between
> a horde-internal, and an external, form. The internal form is
> used, e. g. as an index in the preferences database.

      I saw these, but didn't understand their purpose until you
mentioned. I wrote a function to the _username_hook_frombackend, which
takes the username from the form (as you pointed ->
Util::getFormData('imapuser') ) and replaces "@" by "~".
      There are two problems:

      Problem A) This hook only works after the user logged in. Is that right?
        I changed the Cyrus/saslauthd ldap search filter, and enabled
virtual domains on Cyrus, because, on the login form (imp's login.php)
the username the user typed (a full email address) is not modified, so
the IMAP server receives an email as username, and uses it ("> login
some at something.com password"). By enabling virtual domains Cyrus takes
everything before the "@" and saslauthd sees it as %u (ldap search
filter), and everything after the "@" as %d (ldap search filter), my
ldap search filter is uid=%u~%d. To be clear:

        1) the user types "some at something.com" at the imp login form
(horde uses imp for auth)
        2) horde takes this "some at something.com" and sends it along with
the password to the IMAP server (I sniffed the traffic).
        3) As virtual domains are enabled, Cyrus/saslauthd would use only
%u ->  "some" to search on the ldap server (I don't use virtual
domains, but I need to enable it, otherwise saslauthd refuses to use
"some at something.com" as username). Since my ldap search filter
(configured on saslauthd) is %u~%d, it takes everything before the
"@", sees it as %u, and everything after the "@", sees it as %d, and
finally searches for uid=some~something.com, receives the right DN
from the LDAP server, and binds with that DN.
       4) Login succeeds

      My point: I would like to have the modification to the username
before log in, So that ugly Cyrus/saslauthd configuration would not be
necessary. I would use %u as ldap search filter. Without the
cyrus/saslauthd virtual domains option and the modified search filter,
"some at something.com" is used to search, which returns zero entries.


      Problem B) Ok, now I'm logged.

       5) Horde doesn't see the mailbox! I guess it has
"some at something.com" as the mailbox, right?

       I have a similar tobackend hook, which doesn't get executed, only
frombackend (these hook functions log a message showing data they
received, and data they returned). Horde still thinks the username is
"some at something.com", but there is no user/some at something.com folder
in the IMAP server, just user/some~something.com . When I click on
"Mail" in Horde, an yellow background is shown where the messages list
should be.


> Or perhaps, you mean _imp_hook_vinfo? This is called during
> login to process the input from the Imp login form.
> - It is activated via the HTML interface: Setup/Mail/CustomHooks.
> - If it is invoked as _imp_hook_vinfo('vdomain'),
>   it is supposed to yield a domain string to be placed in the
>   input form next to the Username field.
> - If it is invoked as _imp_hook_vinfo(), it is supposed to
>   yield the (possibly modified) username.
> - Note that, when this hook is called, both IMP::getCurrentServer()
>   and $_SESSION['imp'] are still undefined, IMP::serverString()
>   is still inoperable, and the data from the login form may have been
>   either transmitted via GET, or via POST, so you should access
>   them via the Util::getFormData method.
>
> This is my declaration:
>> if (!function_exists('_imp_hook_vinfo')) :
>>  function _imp_hook_vinfo($type='username')
>>  { global $servers; // cf. imp/config/servers.php
>>    $Imp_user    = Util::getFormData('imapuser');
>>    $IMAP_Server = Util::getFormData('server');
>>    if (empty($IMAP_Server)) :      // if pristine Login form,
>>      reset($servers);
>>      $IMAP_Server = key($servers); // then use the the topmost server
>>    endif;
>>
>>    if ($type=='username') :
>>      $vinfo = knu_user_data('uid', $Imp_user, $IMAP_Server);
>>    elseif ($type=='vdomain') :
>>      $vinfo = isset($servers[$IMAP_Server]) ?       
>> $servers[$IMAP_Server]['maildomain'] : '';
>>    else :
>>      $vinfo = '*?*';
>>    endif;
>>
>>    knu_debug( __LINE__
>>             , '_imp_hook_vinfo'
>>             , array($type)
>>             , array( '$IMAP_Server'       => $IMAP_Server
>>                    , '$Imp_user'          => $Imp_user
>>                    , '$_GET'              => $_GET
>>                    , '$_POST'             => $_POST
>>                    , '$_SESSION[\'imp\']' =>       
>> (isset($_SESSION['imp'])?$_SESSION['imp']:NULL)
>>                    )
>>             , $vinfo);
>>    return $vinfo;
>>  }
>> endif;
> where, again, knu_user_data does the processing in earnest, and
> knu_debug optionally logs its arguments.
>
> Good luck,
>   Otto Stolz


     About vinfo hook:

--------
This is my vinfo hook:
     if (!function_exists('_imp_hook_vinfo')) {
         function _imp_hook_vinfo($type = 'username')
         {
            $var1 = Util::getFormData('imapuser');
            $han = fopen("/tmp/horde-debug.txt", "a");
            fwrite($han, "\n _imp_hook_vinfo> form: '$var1'\n");
            fflush($han); fclose($han);

             if ($type == 'username') {
                    $userid_retorno = eregi_replace("@", "~", $var1);
                    $han = fopen("/tmp/horde-debug.txt", "a");
                    fwrite($han, "\n type == username, userid_retorno =
'$userid_retorno'\n");
                    fflush($han); fclose($han);
                    return $userid_retorno;

             // ----->  "if type vdomain"  <------

             } elseif ($type == 'vdomain') {
                    $userid_retorno = eregi_replace("@", "~", $var1);
                    $han = fopen("/tmp/horde-debug.txt", "a");
                    fwrite($han, "\n type == vdomain!, userid_retorno =
'$userid_retorno'\n");
                    fflush($han); fclose($han);
                    return $userid_retorno;
           //         $han = fopen("/tmp/horde-debug.txt", "a");
           //        fwrite($han, "\n type == vdomain, vdomain =  
'$vdomain'\n");
           //         fflush($han); fclose($han);
           //      return $vdomain;
             } else {
                    $han = fopen("/tmp/horde-debug.txt", "a");
                    fwrite($han, "\n error, type = '$type'\n");
                    fflush($han); fclose($han);
                 return PEAR::raiseError('invalid type: ' . $type);
             }
         }
     }

----

     This hook solved my problem, but there is a small problem:

     - An "@" is shown right after the username box, this will get users
confused, how can I get rid of that "@"?


      I noticed it first enters the "if type vdomain", and no form data
exists, then when I type something and hit enter, it enters the
previous if() clause, and correctly translates some at something.com to
some~something.com.
     When using vinfo hook all hooks are disabled, except for:  
_passwd_username, fullname and from_addr.


     Lauro







----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.



More information about the imp mailing list