From 566c484db7a60a0bdbfc84e9b53651b42a3e3552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Fonseca?= Date: Tue, 13 Jan 2004 16:50:40 +0000 Subject: Add a 'postconnect' keyword to execute a command after closing a SMTP connection. --- NEWS | 7 ++++++- TODO | 3 +++ lexer.l | 1 + parser.y | 3 ++- smtp.c | 43 +++++++++++++++++++++++++++++++++++++++++++ smtp.h | 7 ++++--- 6 files changed, 59 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 99d1611..6613986 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,9 @@ News ~~~~ * Version 0.5.1 (under development): + + * A 'postconnect' keyword to execute a command after closing a SMTP + connection. * Version 0.5.0 (2003-11-14): @@ -12,7 +15,7 @@ News * Detailed documentation on how to use the StartTLS extension. More verbosity on StartTLS error messages. - * New 'preconnect' keyword to execute a command prior to opening an SMTP + * New 'preconnect' keyword to execute a command prior to opening a SMTP connection (Daniel Richard G.). * Fixed a bug which prevented to send mail when libesmtp had no @@ -20,6 +23,7 @@ News * New 'default' keyword for identities (Vitezslav Batrla). + * Version 0.4.1 (2003-07-14): * Packaging fixes. @@ -71,3 +75,4 @@ News * Initial release. + diff --git a/TODO b/TODO index b17f226..19a390a 100644 --- a/TODO +++ b/TODO @@ -10,4 +10,7 @@ To do * Include simple scripts to queue emails for dial-up connections. + * In conjunction with "preconnect", a "postconnect" configuration option + would be a good idea (example: tear down a SSH tunnel). + diff --git a/lexer.l b/lexer.l index 9529d0e..4563c9d 100644 --- a/lexer.l +++ b/lexer.l @@ -61,6 +61,7 @@ pass(word)? { BEGIN(NAME); return PASSWORD; } (start)?tls { return STARTTLS; } (certificate_)?passphrase { return CERTIFICATE_PASSPHRASE; } preconnect { return PRECONNECT; } +postconnect { return POSTCONNECT; } mda { return MDA; } = { return MAP; } diff --git a/parser.y b/parser.y index 428e1c3..145fd18 100644 --- a/parser.y +++ b/parser.y @@ -54,7 +54,7 @@ void yyerror (const char *s); char *sval; } -%token IDENTITY DEFAULT HOSTNAME USERNAME PASSWORD STARTTLS CERTIFICATE_PASSPHRASE PRECONNECT MDA +%token IDENTITY DEFAULT HOSTNAME USERNAME PASSWORD STARTTLS CERTIFICATE_PASSPHRASE PRECONNECT POSTCONNECT MDA %token MAP @@ -101,6 +101,7 @@ statement : HOSTNAME map STRING { identity->host = xstrdup($3); SET_DEFAULT_IDEN | STARTTLS map REQUIRED { identity->starttls = Starttls_REQUIRED; SET_DEFAULT_IDENTITY; } | CERTIFICATE_PASSPHRASE map STRING { identity->certificate_passphrase = xstrdup($3); SET_DEFAULT_IDENTITY; } | PRECONNECT map STRING { identity->preconnect = xstrdup($3); SET_DEFAULT_IDENTITY; } + | PRECONNECT map STRING { identity->postconnect = xstrdup($3); SET_DEFAULT_IDENTITY; } | MDA map STRING { mda = xstrdup($3); } | DEFAULT { default_identity = identity; } ; diff --git a/smtp.c b/smtp.c index 9fe8aa1..2ca96bb 100644 --- a/smtp.c +++ b/smtp.c @@ -62,6 +62,12 @@ void identity_free(identity_t *identity) if(identity->certificate_passphrase) free(identity->certificate_passphrase); + if(identity->preconnect) + free(identity->preconnect); + + if(identity->postconnect) + free(identity->postconnect); + free(identity); } @@ -563,6 +569,43 @@ void smtp_send(message_t *msg) auth_destroy_context (authctx); auth_client_exit (); + /* Execute post-connect command if one was specified. */ + if (identity->postconnect) + { + int ret, exit_status; + + if (verbose) + fprintf (stdout, "Executing post-connect command: %s\n", identity->postconnect); + + ret = system (identity->postconnect); + exit_status = WEXITSTATUS(ret); + + /* Check whether the child process caught a signal meant for us */ + if (WIFSIGNALED(ret)) + { + int sig = WTERMSIG(ret); + + if (sig == SIGINT || sig == SIGQUIT) + { + fprintf (stderr, "Post-connect command received signal %d\n", sig); + exit (EX_SOFTWARE); + } + } + + if (ret == -1) + { + fputs ("Error executing post-connect command\n", stderr); + exit (EX_OSERR); + } + + if (exit_status != 0) + { + fprintf (stderr, "Post-connect command \"%s\" exited with non-zero status %d\n", + identity->postconnect, exit_status); + exit (EX_SOFTWARE); + } + } + return; failure: diff --git a/smtp.h b/smtp.h index 5df10ca..f5800fc 100644 --- a/smtp.h +++ b/smtp.h @@ -27,21 +27,22 @@ typedef struct { char *host; /**< hostname and service (port) */ - /** \name Auth Extension */ + /** \name Auth extension */ /*@{*/ char *user; char *pass; /*@}*/ - /** \name StartTLS Extension */ + /** \name StartTLS extension */ /*@{*/ enum starttls_option starttls; char *certificate_passphrase; /*@}*/ - /** \name Pre-connect Command */ + /** \name Pre- and post-connect commands */ /*@{*/ char *preconnect; + char *postconnect; /*@}*/ } identity_t; -- cgit v1.2.3