Commit 51d1695e 51d1695e96b9a286bcffd0eb0a674ea9ee034f23 by Sergey Poznyakoff

Maidag cleanup

* maidag/maidag.c (mda_mode,lmtp_mode,url_option): Remove.
(maidag_mode): New variable.
(parse_opt): The --lmtp, --url and --mda options set the delivery-mode
configuration statement.
(cb_delivery_mode): New callback.
(maidag_cfg_param) <delivery-mode>: New statement.
(main): Redo delivery mode selection.
* maidag/maidag.h (maidag_mode): New enum.
(maidag_mode): New extern.
(mda_mode,lmtp_mode,url_option): Remove.
(maidag_delivery_fn): New typedef.
(maidag_stdio_delivery,mda): Change signature.
(deliver_to_url, deliver_to_user): New prototypes.

* maidag/deliver.c (mda): Take delivery function as a parameter.
(maidag_stdio_delivery): Likewise.
(deliver_to_user): Rename to deliver_to_mailbox.
(deliver_url): Rename to do_delivery.
(deliver): Rename to deliver_to_url.
(deliver_to_user): New function.
* maidag/forward.c (process_forward): Use deliver_to_user.
* maidag/lmtp.c (dot_deliver): Likewise.
1 parent 04334427
......@@ -128,7 +128,7 @@ make_tmp (const char *from)
}
int
mda (mu_mailbox_t mbx, char *username)
mda (mu_mailbox_t mbx, char *dest_id, maidag_delivery_fn deliver)
{
int status;
mu_message_t msg;
......@@ -140,7 +140,7 @@ mda (mu_mailbox_t mbx, char *username)
return EX_TEMPFAIL;
}
deliver (msg, username, NULL);
deliver (msg, dest_id, NULL);
if (multiple_delivery)
exit_code = EX_OK;
......@@ -149,7 +149,7 @@ mda (mu_mailbox_t mbx, char *username)
}
int
maidag_stdio_delivery (int argc, char **argv)
maidag_stdio_delivery (maidag_delivery_fn delivery_fun, int argc, char **argv)
{
mu_mailbox_t mbox = make_tmp (sender_address);
......@@ -157,7 +157,7 @@ maidag_stdio_delivery (int argc, char **argv)
multiple_delivery = argc > 1;
for (; *argv; argv++)
mda (mbox, *argv);
mda (mbox, *argv, delivery_fun);
return exit_code;
}
......@@ -224,9 +224,9 @@ attach_notify (mu_mailbox_t mbox)
}
int
deliver_to_user (mu_mailbox_t mbox, mu_message_t msg,
struct mu_auth_data *auth,
char **errp)
deliver_to_mailbox (mu_mailbox_t mbox, mu_message_t msg,
struct mu_auth_data *auth,
char **errp)
{
int status;
char *path;
......@@ -349,8 +349,8 @@ deliver_to_user (mu_mailbox_t mbox, mu_message_t msg,
return failed ? exit_code : 0;
}
int
deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
static int
do_delivery (mu_url_t url, mu_message_t msg, const char *name, char **errp)
{
struct mu_auth_data *auth = NULL;
mu_mailbox_t mbox;
......@@ -437,7 +437,7 @@ deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
will be created */
if (switch_user_id (auth, 1))
return EX_TEMPFAIL;
status = deliver_to_user (mbox, msg, auth, errp);
status = deliver_to_mailbox (mbox, msg, auth, errp);
if (switch_user_id (auth, 0))
return EX_TEMPFAIL;
......@@ -448,45 +448,42 @@ deliver_url (mu_url_t url, mu_message_t msg, const char *name, char **errp)
}
int
deliver (mu_message_t msg, char *dest_id, char **errp)
deliver_to_url (mu_message_t msg, char *dest_id, char **errp)
{
int status;
const char *name;
mu_url_t url = NULL;
if (url_option)
status = mu_url_create (&url, dest_id);
if (status)
{
status = mu_url_create (&url, dest_id);
if (status)
{
maidag_error (_("%s: cannot create url: %s"), dest_id,
mu_strerror (status));
return EX_NOUSER;
}
status = mu_url_parse (url);
if (status)
{
maidag_error (_("%s: cannot parse url: %s"), dest_id,
mu_strerror (status));
mu_url_destroy (&url);
return EX_NOUSER;
}
status = mu_url_sget_user (url, &name);
if (status == MU_ERR_NOENT)
name = NULL;
else if (status)
{
maidag_error (_("%s: cannot get user name from url: %s"),
dest_id, mu_strerror (status));
mu_url_destroy (&url);
return EX_NOUSER;
}
maidag_error (_("%s: cannot create url: %s"), dest_id,
mu_strerror (status));
return EX_NOUSER;
}
status = mu_url_parse (url);
if (status)
{
maidag_error (_("%s: cannot parse url: %s"), dest_id,
mu_strerror (status));
mu_url_destroy (&url);
return EX_NOUSER;
}
else
status = mu_url_sget_user (url, &name);
if (status == MU_ERR_NOENT)
name = NULL;
else if (status)
{
name = dest_id;
dest_id = NULL;
maidag_error (_("%s: cannot get user name from url: %s"),
dest_id, mu_strerror (status));
mu_url_destroy (&url);
return EX_NOUSER;
}
return deliver_url (url, msg, name, errp);
return do_delivery (url, msg, name, errp);
}
int
deliver_to_user (mu_message_t msg, char *dest_id, char **errp)
{
return do_delivery (NULL, msg, dest_id, errp);
}
......
......@@ -243,7 +243,7 @@ process_forward (mu_message_t msg, char *filename, const char *myname)
if (result == maidag_forward_ok)
result = maidag_forward_metoo;
}
else if (deliver (msg, p, NULL))
else if (deliver_to_user (msg, p, NULL))
result = maidag_forward_error;
}
}
......
......@@ -317,7 +317,7 @@ dot_deliver (void *item, void *cbdata)
mu_stream_t iostr = cbdata;
char *errp = NULL;
switch (deliver (mesg, name, &errp))
switch (deliver_to_user (mesg, name, &errp))
{
case 0:
lmtp_reply (iostr, "250", "2.0.0", "%s: delivered", name);
......
......@@ -18,8 +18,7 @@
#include "maidag.h"
int mda_mode; /* Force local MDA mode even if not started as
root */
enum maidag_mode maidag_mode = mode_default;
int multiple_delivery; /* Don't return errors when delivering to multiple
recipients */
int ex_quota_tempfail; /* Return temporary failure if mailbox quota is
......@@ -51,8 +50,6 @@ char *message_id_header; /* Use the value of this header as message
/* For LMTP mode */
mu_m_server_t server;
int lmtp_mode;
int url_option;
char *lmtp_url_string;
int reuse_lmtp_address = 1;
int maidag_transcript;
......@@ -216,13 +213,13 @@ parse_opt (int key, char *arg, struct argp_state *state)
break;
case LMTP_OPTION:
mu_argp_node_list_new (lst, "lmtp", "yes");
mu_argp_node_list_new (lst, "delivery-mode", "lmtp");
if (arg)
mu_argp_node_list_new (lst, "listen", arg);
break;
case MDA_OPTION:
mda_mode = 1;
mu_argp_node_list_new (lst, "delivery-mode", "mda");
break;
case TRANSCRIPT_OPTION:
......@@ -267,7 +264,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
break;
case URL_OPTION:
url_option = 1;
mu_argp_node_list_new (lst, "delivery-mode", "url");
break;
case ARGP_KEY_INIT:
......@@ -418,8 +415,35 @@ struct mu_cfg_param filter_cfg_param[] = {
N_("Set script pattern.") },
{ NULL }
};
static int
cb_delivery_mode (mu_debug_t debug, void *data, mu_config_value_t *val)
{
static mu_kwd_t mode_tab[] = {
{ "default", mode_default },
{ "mda", mode_mda },
{ "url", mode_url },
{ "lmtp", mode_lmtp },
{ NULL }
};
int n;
if (mu_cfg_assert_value_type (val, MU_CFG_STRING, debug))
return 1;
if (mu_kwd_xlat_name (mode_tab, val->v.string, &n) == 0)
maidag_mode = n;
else
mu_cfg_format_error (debug, MU_DEBUG_ERROR,
_("%s is unknonw"),
val->v.string);
return 0;
}
struct mu_cfg_param maidag_cfg_param[] = {
{ "delivery-mode", mu_cfg_callback, NULL, 0, cb_delivery_mode,
N_("Set delivery mode"),
N_("mode: {default | mda | url | lmtp}") },
{ "exit-multiple-delivery-success", mu_cfg_bool, &multiple_delivery, 0, NULL,
N_("In case of multiple delivery, exit with code 0 if at least one "
"delivery succeeded.") },
......@@ -456,8 +480,6 @@ struct mu_cfg_param maidag_cfg_param[] = {
N_("Configure safety checks for the forward file."),
N_("arg: list") },
/* LMTP support */
{ "lmtp", mu_cfg_bool, &lmtp_mode, 0, NULL,
N_("Run in LMTP mode.") },
{ "group", mu_cfg_callback, &lmtp_groups, 0, cb_group,
N_("In LMTP mode, retain these supplementary groups."),
N_("groups: list of string") },
......@@ -501,7 +523,8 @@ main (int argc, char *argv[])
{
int arg_index;
mu_debug_t debug;
maidag_delivery_fn delivery_fun = NULL;
/* Preparative work: close inherited fds, force a reasonable umask
and prepare a logging. */
close_fds ();
......@@ -551,11 +574,11 @@ main (int argc, char *argv[])
exit (EX_CONFIG);
current_uid = getuid ();
if (!lmtp_mode && current_uid == 0)
mda_mode = 1;
if (maidag_mode == mode_default && current_uid == 0)
maidag_mode = mode_mda;
if (log_to_stderr == -1)
log_to_stderr = url_option || (!lmtp_mode && !mda_mode);
log_to_stderr = maidag_mode == mode_url;
mu_diag_get_debug (&debug);
if (!log_to_stderr)
......@@ -573,46 +596,49 @@ main (int argc, char *argv[])
argc -= arg_index;
argv += arg_index;
if (lmtp_mode && !url_option)
switch (maidag_mode)
{
case mode_lmtp:
if (argc)
{
mu_error (_("too many arguments"));
return EX_USAGE;
}
return maidag_lmtp_server ();
}
else
{
if (!mda_mode)
{
if (url_option)
{
/* FIXME: Verify if the urls are deliverable? */
}
else
{
static char *s_argv[2];
struct mu_auth_data *auth = mu_get_auth_by_uid (current_uid);
if (!auth)
{
mu_error (_("cannot get username"));
return EX_UNAVAILABLE;
}
case mode_default:
{
static char *s_argv[2];
struct mu_auth_data *auth = mu_get_auth_by_uid (current_uid);
if (!auth)
{
mu_error (_("cannot get username"));
return EX_UNAVAILABLE;
}
if (argc > 0 && strcmp (auth->name, argv[0]))
{
mu_error (_("recipients given when running as non-root"));
return EX_USAGE;
}
s_argv[0] = auth->name;
argv = s_argv;
argc = 1;
}
}
return maidag_stdio_delivery (argc, argv);
if (argc > 0 && strcmp (auth->name, argv[0]))
{
mu_error (_("recipients given when running as non-root"));
return EX_USAGE;
}
s_argv[0] = auth->name;
argv = s_argv;
argc = 1;
}
delivery_fun = deliver_to_user;
break;
case mode_url:
/* FIXME: Verify if the urls are deliverable? */
delivery_fun = deliver_to_url;
break;
case mode_mda:
delivery_fun = deliver_to_user;
break;
}
return maidag_stdio_delivery (delivery_fun, argc, argv);
}
......
......@@ -118,6 +118,15 @@ extern int debug_level;
#define FWD_DIR_IWOTH 0x0010 /* forward file in world writable directory */
#define FWD_ALL (FWD_IWGRP|FWD_IWOTH|FWD_LINK|FWD_DIR_IWOTH|FWD_DIR_IWGRP)
enum maidag_mode
{
mode_default,
mode_mda,
mode_url,
mode_lmtp
};
extern enum maidag_mode maidag_mode;
extern int exit_code;
extern int log_to_stderr;
extern int multiple_delivery;
......@@ -136,8 +145,6 @@ extern int sieve_debug_flags;
extern int sieve_enable_log;
extern mu_m_server_t server;
extern int lmtp_mode;
extern int url_option;
extern char *lmtp_url_string;
extern int reuse_lmtp_address;
extern mu_list_t lmtp_groups;
......@@ -147,7 +154,12 @@ extern int maidag_transcript;
void close_fds (void);
int switch_user_id (struct mu_auth_data *auth, int user);
int maidag_stdio_delivery (int argc, char **argv);
typedef int (*maidag_delivery_fn) (mu_message_t, char *, char **);
int deliver_to_url (mu_message_t msg, char *dest_id, char **errp);
int deliver_to_user (mu_message_t msg, char *dest_id, char **errp);
int maidag_stdio_delivery (maidag_delivery_fn fun, int argc, char **argv);
int maidag_lmtp_server (void);
int lmtp_connection (int fd, struct sockaddr *sa, int salen, void *data,
mu_ip_server_t srv, time_t timeout, int transcript);
......@@ -156,8 +168,7 @@ void maidag_error (const char *fmt, ...) MU_PRINTFLIKE(1, 2);
void notify_biff (mu_mailbox_t mbox, char *name, size_t size);
void guess_retval (int ec);
int mda (mu_mailbox_t mbx, char *username);
int deliver (mu_message_t msg, char *name, char **errp);
int mda (mu_mailbox_t mbx, char *username, maidag_delivery_fn fun);
int sieve_test (struct mu_auth_data *auth, mu_message_t msg);
int check_quota (struct mu_auth_data *auth, mu_off_t size, mu_off_t *rest);
......