/*
 * POP3Lite - 3lite POP3 Daemon
 * 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
 */

#include "main.h"

#include <pop3lite.h>
#include <glib.h>

#include <syslog.h>
#include <unistd.h>

#include "core_auth.h"

static const char rcsid[]="$Id: core_auth.c,v 1.8 2001/01/27 13:36:07 algernon Exp $";

CommandResponse *
default_cmd_auth_user ( P3LControl *control, const char *user )
{
#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" );

	g_hash_table_insert ( control->data, "USER", g_strdup ( user ) );
	g_hash_table_insert ( control->data, "CLIENT_USER", g_strdup ( user ) );

	return p3l_respond ( POP3_OK, g_strdup_printf ("%s selected", user ) );
}

CommandResponse *
default_cmd_auth_pass ( P3LControl *control, const char *pass )
{
	char *user;
	char *pw;
	UserInfo *ui;

	user = P3L_GET_DATA ( "USER" );

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: handling PASS (%s)",
			       __FILE__, __LINE__, user );
#endif

	if ( user == NULL )
	{
		control->system->log ( control, LOG_INFO, "%s: no username", __FILE__ );
		return p3l_respond ( POP3_ERR, "Select a username first" );
	}

	pw = ( pass == NULL ) ? "" : g_strdup ( pass );

	if ( control->system->authenticate ( control, user, pw ) == AUTH_RESULT_OK )
	{
		/*
		 * Ok message is sent in trans_init, because
		 * if the mailbox can't be opened/locked/etc,
		 * we send an error,
		 */
		ui = control->system->getuinam ( control, user );
		if (  ui == NULL )
		{
			g_hash_table_remove ( control->data, "USER" );
			control->system->log ( control, LOG_WARNING, "Attempted login: %s (invalid uid)", user );
			sleep ( 5 );
			return p3l_respond ( POP3_ERR, "Invalid username or password" );
		}

		control->system->log ( control, LOG_NOTICE, "User %s logged in", user );
		control->state = POP3_STATE_TRANS;
	}
	else
	{
		g_hash_table_remove ( control->data, "USER" );
		control->system->log ( control, LOG_WARNING, "Attempted login: %s", user );
		return p3l_respond ( POP3_ERR, "Invalid username or password" );
	}

	return p3l_respond ( POP3_OK_HIDDEN, NULL );
}

CommandResponse *
default_cmd_auth_quit ( P3LControl *control, const char *args )
{
	g_hash_table_remove ( control->data, "USER" );
	control->system->log ( control, LOG_NOTICE, "Session end" );
	return p3l_respond ( POP3_OK_EXIT, "Good bye" );
}


syntax highlighted by Code2HTML, v. 0.9.1