/* * (POP3Lite) Alias - 3lite POP3 Daemon (user alias support) * Copyright (C) 2000, 2001 Gergely Nagy <8@free.bsd.hu> * * This file is part of POP3Lite. * * POP3Lite is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * POP3Lite is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #ifdef DEBUG # include #endif #include "alias.h" static const char rcsid[]="$Id: alias.c,v 1.6 2001/01/12 15:58:59 algernon Exp $"; int alias_LTX_module_done ( P3LControl *control ); int alias_LTX_module_init ( P3LControl *control ); static char* alias_default_get_alias ( P3LControl *control, const char *user ); static CommandResponse *alias_cmd_auth_user ( P3LControl *control, const char *user ); static P3LHook_alias_get_alias B_alias_get_alias; static p3l_pop3_command B_alias_auth_user; /** * alias_default_get_alias: default ALIAS-GET-ALIAS hook * @control: the usual control struct * @user: the user whose alias we want * * Retrieves the alias to a given username, from a file. * Multiple-level aliases (eg: www-data->root->user) * are NOT supported. (But a module calling this function * in a loop can easily implement that behaviour too.) * * Returns: the alias or the original filename **/ static char * alias_default_get_alias ( P3LControl *control, const char *user ) { gpointer buffer; char *filename, **line_s, *line, *alias; P3LString **lines; size_t linecnt, bsize, i=0; #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: handling ALIAS-GET-ALIAS", __FILE__, __LINE__ ); #endif alias = g_strdup ( user ); /* * Get the filename... */ filename = P3L_GET_FIRST_OPTION ( "ALIAS.ALIAS_MAP" ); if ( filename == NULL ) filename = SYSCONFDIR "/pop3lite.usermap"; /* * Read the file */ if ( ( buffer = p3l_read_file ( filename, &bsize ) ) == NULL ) return alias; /* * Get the alias */ lines = p3l_split_lines ( buffer, bsize, &linecnt ); while ( lines[i] != NULL && i < linecnt ) { line = (char *) g_malloc ( lines[i]->length + 1 ); memcpy ( line, lines[i]->str, lines[i]->length ); line[lines[i]->length-1]=0; /* * Split into at most three parts. We only use * the first two, but other modules may use the * rest (eg the virtual-host module), so we allow * other info to be on the line */ line_s = g_strsplit ( line, ":", 2 ); g_free ( line ); if ( ! strcmp ( line_s[0], user ) ) { alias = g_strdup ( line_s[1] ); g_strfreev ( line_s ); break; } i++; g_strfreev ( line_s ); } g_free ( buffer ); return alias; } /** * alias_cmd_auth_user: USER hook * @control: the usual control struct * @user: the username given by the client * * Adds a hook on top of the original USER handler. * Note: ON TOP! This is not a replacement! * * Returns: the same as the underlying handler would, * after the alias resolving, with the given username. **/ static CommandResponse * alias_cmd_auth_user ( P3LControl *control, const char *user ) { char *alias; CommandResponse *resp; #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: handling USER (%s)", __FILE__, __LINE__, user ); #endif if ( user == NULL ) return p3l_respond ( POP3_ERR, "Invalid username" ); if ( P3L_HOOK_EXISTS ( "ALIAS-GET-ALIAS" ) ) alias = P3L_CALL_HOOK ( P3LHook_alias_get_alias, "ALIAS-GET-ALIAS" ) ( control, user ); else alias = g_strdup ( user ); resp = (*B_alias_auth_user) ( control, alias ); if ( resp->code == POP3_OK ) resp->message = g_strdup_printf ("%s selected", user ); g_hash_table_insert ( control->data, "CLIENT_USER", g_strdup ( user ) ); return resp; } int alias_LTX_module_init ( P3LControl *control ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-Alias", __FILE__, __LINE__ ); #endif /* * If we have an existing USER handler, everything is fine, if not, * we do not install ourselves, because we depend on the underlying * handler. */ if ( g_hash_table_lookup ( control->auth_commands, "USER" ) != NULL ) { B_alias_get_alias = (P3LHook_alias_get_alias) p3l_command_replace ( control->hooks, "ALIAS-GET-ALIAS", (gpointer) alias_default_get_alias ); B_alias_auth_user = (p3l_pop3_command) p3l_command_replace ( control->auth_commands, "USER", (gpointer) alias_cmd_auth_user ); return 0; } return -1; } int alias_LTX_module_done ( P3LControl *control ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: done mod-Alias", __FILE__, __LINE__ ); #endif g_hash_table_insert ( control->hooks, "ALIAS-GET-ALIAS", (gpointer) B_alias_get_alias ); g_hash_table_insert ( control->auth_commands, "USER", (gpointer) B_alias_auth_user ); return 0; }