/* * 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 #include #include #include #include #include #include #include "core.h" #include "core_auth.h" #include "core_sys.h" #include "core_trans.h" static const char rcsid[]="$Id: core.c,v 1.8.2.2 2001/05/02 08:53:26 algernon Exp $"; int errno; /* * FORWARD DECLARATIONS */ static char *default_get_mailbox ( P3LControl *control ); static void default_send_response ( P3LControl *control, POP3Code code, const char *message ); static CommandResponse *default_greeting ( P3LControl *control ); static void default_send_raw ( P3LControl *control, const gpointer data, size_t size ); static CommandResponse *default_state_handler ( P3LControl *control ); static CommandResponse *default_cmd_trans_quit ( P3LControl *control, const char *args ); static CommandResponse *default_cmd_trans_noop ( P3LControl *control, const char *args ); static CommandResponse *default_cmd_trans_last ( P3LControl *control, const char *args ); static CommandResponse *default_cmd_trans_impl ( P3LControl *control, const char *args ); static CommandResponse * default_state_handler ( P3LControl *control ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: default_state_handler", __FILE__, __LINE__ ); #endif control->system->log ( control, LOG_WARNING, "WARNING! default_state_handler runnung!" ); return p3l_respond ( POP3_FATAL, "Internal error" ); } static CommandResponse * default_greeting ( P3LControl *control ) { char *greeting; /* * This is easy... */ #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: Greeting", __FILE__, __LINE__ ); #endif greeting = P3L_GET_FIRST_OPTION ( "DEFAULT.GREETING" ); if ( greeting == NULL ) greeting = "POP3 server ready"; return p3l_respond ( POP3_OK, greeting ); } static void default_send_raw ( P3LControl *control, const gpointer data, size_t size ) { gpointer ptr; ssize_t done; for (ptr = data; size; ptr += done, size -= done) { if ( (done = write ( 1, ptr, size )) == -1 ) { control->system->log ( control, LOG_ERR, g_strdup_printf ( "Write error: %s", g_strerror ( errno ) ) ); exit ( 1 ); } } } static void default_send_response ( P3LControl *control, POP3Code code, const char *message ) { /* * Depending on the code, we echo the status */ switch ( code ) { case POP3_OK_EXIT: case POP3_OK: control->send_raw ( control, "+OK", 3 ); break; case POP3_FATAL: case POP3_ERR: control->send_raw ( control, "-ERR", 4 ); break; case POP3_NOTHING: case POP3_ANSWERED: break; case POP3_OK_HIDDEN: return; break; } /* * And now the message... */ if ( message ) { if ( code != POP3_NOTHING && code != POP3_ANSWERED ) control->send_raw ( control, " ", 1 ); control->send_raw ( control, g_strdup ( message ), strlen ( message ) ); } control->send_raw ( control, "\r\n", 2 ); /* * When running from inetd, we have to flush stdout * or the messages won't appear */ fflush ( stdout ); /* * If exit is requested, exit gracefully */ if ( code == POP3_FATAL ) exit ( 1 ); if ( code == POP3_OK_EXIT ) exit ( 0 ); } static CommandResponse * default_cmd_trans_quit ( P3LControl *control, const char *args ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: handling QUIT (%s)", __FILE__, __LINE__, P3L_GET_DATA ( "USER" ) ); #endif control->state = POP3_STATE_UPDATE; return p3l_respond ( POP3_OK_HIDDEN, NULL ); } static CommandResponse * default_cmd_trans_noop ( P3LControl *control, const char *args ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: handling NOOP (%s)", __FILE__, __LINE__, P3L_GET_DATA ( "USER" ) ); #endif return p3l_respond ( POP3_OK, NULL ); } static CommandResponse * default_cmd_trans_last ( P3LControl *control, const char *args ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: handling LAST (%s)", __FILE__, __LINE__, P3L_GET_DATA ( "USER" ) ); #endif return p3l_respond ( POP3_OK, "0" ); } static CommandResponse * default_cmd_trans_impl ( P3LControl *control, const char *args ) { #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: handling IMPLEMENTATION (%s)", __FILE__, __LINE__, P3L_GET_DATA ( "USER" ) ); #endif return p3l_respond ( POP3_OK, g_strdup_printf ( "POP3Lite %s", VERSION ) ); } static char * default_get_mailbox ( P3LControl *control ) { char *templ; #ifdef DEBUG control->system->log ( control, LOG_DEBUG, "%s:%d: get_mailbox", __FILE__, __LINE__ ); #endif /* * simply do a [prefix]/[username] combination */ templ = P3L_GET_FIRST_OPTION ( "MAILBOX_TEMPLATE" ); if ( templ == NULL ) templ = "/var/mail/%s"; return g_strdup_printf ( templ, g_hash_table_lookup ( control->data, "USER" ) ); } void core_init ( P3LControl *control ) { control->greeting = default_greeting; control->send_response = default_send_response; control->send_raw = default_send_raw; control->trans_init = default_state_handler; control->update = default_state_handler; control->system->log = default_sys_log; control->system->openlog = default_sys_openlog; control->system->closelog = default_sys_closelog; control->system->getuinam = default_sys_getuinam; control->system->authenticate = default_sys_authenticate; control->system->drop_privileges = default_sys_drop_privileges; g_hash_table_insert ( control->auth_commands, "USER", (gpointer) default_cmd_auth_user ); g_hash_table_insert ( control->auth_commands, "PASS", (gpointer) default_cmd_auth_pass ); g_hash_table_insert ( control->auth_commands, "QUIT", (gpointer) default_cmd_auth_quit ); g_hash_table_insert ( control->trans_commands, "QUIT", (gpointer) default_cmd_trans_quit ); g_hash_table_insert ( control->trans_commands, "NOOP", (gpointer) default_cmd_trans_noop ); g_hash_table_insert ( control->trans_commands, "LAST", (gpointer) default_cmd_trans_last ); g_hash_table_insert ( control->trans_commands, "IMPLEMENTATION", (gpointer) default_cmd_trans_impl ); g_hash_table_insert ( control->trans_commands, "LIST", (gpointer) default_cmd_trans_list ); g_hash_table_insert ( control->trans_commands, "UIDL", (gpointer) default_cmd_trans_uidl ); g_hash_table_insert ( control->trans_commands, "RSET", (gpointer) default_cmd_trans_rset ); g_hash_table_insert ( control->trans_commands, "STAT", (gpointer) default_cmd_trans_stat ); g_hash_table_insert ( control->hooks, "GET-MAILBOX", (gpointer) default_get_mailbox ); g_hash_table_insert ( control->capabilities, "IMPLEMENTATION", g_strdup_printf ( "POP3Lite %s", VERSION ) ); } void core_reinit ( P3LControl *control ) { #ifdef WITH_LOGLEVEL_CHECK char *loglevel; loglevel = P3L_GET_FIRST_OPTION ( "MINIMUM_LOG_LEVEL" ); switch ( LOG_DEBUG - atoi ( ( loglevel != 0 ) ? loglevel : "1" ) ) { case LOG_DEBUG: setlogmask ( LOG_UPTO ( LOG_DEBUG ) ); break; case LOG_INFO: setlogmask ( LOG_UPTO ( LOG_INFO ) ); break; case LOG_NOTICE: setlogmask ( LOG_UPTO ( LOG_NOTICE ) ); break; case LOG_WARNING: setlogmask ( LOG_UPTO ( LOG_WARNING ) ); break; case LOG_ERR: setlogmask ( LOG_UPTO ( LOG_ERR ) ); break; case LOG_CRIT: setlogmask ( LOG_UPTO ( LOG_CRIT ) ); break; case LOG_ALERT: setlogmask ( LOG_UPTO ( LOG_ALERT ) ); break; case LOG_EMERG: setlogmask ( LOG_UPTO ( LOG_EMERG ) ); break; default: setlogmask ( LOG_UPTO ( LOG_NOTICE ) ); break; } #endif }