/*
 * (POP3Lite) IPLog - 3lite POP3 Daemon (IP logging)
 * 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 <config.h>
#endif

#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

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

#include <syslog.h>

static const char rcsid[]="$Id: iplog.c,v 1.2.2.1 2001/05/02 17:31:09 algernon Exp $";

int ip_LTX_module_done ( P3LControl *control );
int ip_LTX_module_init ( P3LControl *control );


static void iplog_drop_privileges ( P3LControl *control );

static P3LSysCon_drop_privileges B_iplog_drop_privileges;

/**
 * p3l_get_peer_addr: get the peer's IP address
 *
 * This one figures out the remote end's IP address.
 *
 * Returns: the address.
 **/

static char *
p3l_get_peer_addr ( void )
{
	struct sockaddr_in peer;
	socklen_t peersize = sizeof peer;
	char *hostname;

	if ( getpeername ( 0, (struct sockaddr *) &peer, &peersize ) < 0 )
		return NULL;
	if ( peer.sin_family != AF_INET )
		return NULL;

	hostname = inet_ntoa ( peer.sin_addr );

	return hostname;
}

/**
 * iplog_drop_privileges: log remote ip before dropping privileges
 * @control: the usual control struct
 *
 * Logs the IP address of the remote end before dropping
 * all privileges
 *
 * Returns: nothing
 **/

static void
iplog_drop_privileges ( P3LControl *control )
{
	char *lfn = P3L_GET_FIRST_OPTION ( "IPLOG.FILENAME" );
	int lfd;

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

	if ( lfn != NULL && 
	     ( lfd = open ( lfn, O_APPEND | O_CREAT | O_WRONLY ) ) > 0 )
	{
		char *ip = g_strdup_printf ( "%s\n", p3l_get_peer_addr() );

		write ( lfd, ip, strlen ( ip ) );
		g_free ( ip );

		close ( lfd );
	}


	(*B_iplog_drop_privileges) ( control );
}

int
iplog_LTX_module_init ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-IPLog",
			       __FILE__, __LINE__ );
#endif

	/*
	 * Replace control->system->drop_privileges
	 */
	if ( control->system->drop_privileges != NULL )
	{
		B_iplog_drop_privileges = control->system->drop_privileges;
		control->system->drop_privileges = iplog_drop_privileges;
	} else return -1;

	return 0;
}

int
iplog_LTX_module_done ( P3LControl *control )
{

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d done mod-IPLog",
			       __FILE__, __LINE__ );
#endif

	control->system->drop_privileges = B_iplog_drop_privileges;

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1