summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGilles Chehade <gilles@cvs.openbsd.org>2011-03-26 11:00:00 +0000
committerGilles Chehade <gilles@cvs.openbsd.org>2011-03-26 11:00:00 +0000
commit4db89ed3cec2d63b23c424662cc58039b2554196 (patch)
tree882a1890b32958a2ff02071fe30cfd420461b303
parent05f52a3324581237ca2cae342c90111b1b8bc541 (diff)
have the client API receive a stdio stream rather than a fd to the message
fd. this shifts responsibility for the fclose to the caller, prevents a memory leak and makes everyone happy. diff by Jared Yanovich, thanks !
-rw-r--r--usr.sbin/smtpd/bounce.c21
-rw-r--r--usr.sbin/smtpd/client.c7
-rw-r--r--usr.sbin/smtpd/client.h4
-rw-r--r--usr.sbin/smtpd/enqueue.c5
-rw-r--r--usr.sbin/smtpd/mta.c15
-rw-r--r--usr.sbin/smtpd/smtpd.h4
6 files changed, 32 insertions, 24 deletions
diff --git a/usr.sbin/smtpd/bounce.c b/usr.sbin/smtpd/bounce.c
index 7d4aada11e8..780e1f894b5 100644
--- a/usr.sbin/smtpd/bounce.c
+++ b/usr.sbin/smtpd/bounce.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bounce.c,v 1.25 2011/03/21 13:06:25 gilles Exp $ */
+/* $OpenBSD: bounce.c,v 1.26 2011/03/26 10:59:59 gilles Exp $ */
/*
* Copyright (c) 2009 Gilles Chehade <gilles@openbsd.org>
@@ -43,6 +43,7 @@ struct client_ctx {
struct message m;
struct smtp_client *pcb;
struct smtpd *env;
+ FILE *msgfp;
};
int
@@ -51,19 +52,22 @@ bounce_session(struct smtpd *env, int fd, struct message *messagep)
struct client_ctx *cc = NULL;
int msgfd = -1;
char *reason;
+ FILE *msgfp = NULL;
/* get message content */
if ((msgfd = queue_open_message_file(messagep->message_id)) == -1)
goto fail;
+ msgfp = fdopen(msgfd, "r");
+ if (msgfp == NULL)
+ fatal("fdopen");
/* init smtp session */
- if ((cc = calloc(1, sizeof(*cc))) == NULL) {
- close(msgfd);
+ if ((cc = calloc(1, sizeof(*cc))) == NULL)
goto fail;
- }
- cc->pcb = client_init(fd, msgfd, env->sc_hostname, 1);
+ cc->pcb = client_init(fd, msgfp, env->sc_hostname, 1);
cc->env = env;
cc->m = *messagep;
+ cc->msgfp = msgfp;
client_ssl_optional(cc->pcb);
client_sender(cc->pcb, "");
@@ -107,8 +111,10 @@ bounce_session(struct smtpd *env, int fd, struct message *messagep)
return 1;
fail:
- if (cc && cc->pcb)
- client_close(cc->pcb);
+ if (cc)
+ fclose(cc->msgfp);
+ else if (msgfd != -1)
+ close(msgfd);
free(cc);
return 0;
}
@@ -154,6 +160,7 @@ out:
cc->env->stats->runner.active--;
cc->env->stats->runner.bounces_active--;
client_close(cc->pcb);
+ fclose(cc->msgfp);
free(cc);
return;
diff --git a/usr.sbin/smtpd/client.c b/usr.sbin/smtpd/client.c
index 2a3eac97d4a..9f8d985c9bd 100644
--- a/usr.sbin/smtpd/client.c
+++ b/usr.sbin/smtpd/client.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: client.c,v 1.34 2011/03/21 09:21:57 gilles Exp $ */
+/* $OpenBSD: client.c,v 1.35 2011/03/26 10:59:59 gilles Exp $ */
/*
* Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
@@ -50,7 +50,7 @@ int ssl_buf_write(SSL *, struct msgbuf *);
* Initialize SMTP session.
*/
struct smtp_client *
-client_init(int fd, int body, char *ehlo, int verbose)
+client_init(int fd, FILE *body, char *ehlo, int verbose)
{
struct smtp_client *sp = NULL;
struct client_cmd *c;
@@ -76,8 +76,7 @@ client_init(int fd, int body, char *ehlo, int verbose)
sp->verbose = stdout;
else if ((sp->verbose = fopen("/dev/null", "a")) == NULL)
fatal("client_init: fopen");
- if ((sp->body = fdopen(body, "r")) == NULL)
- fatal("client_init: fdopen");
+ sp->body = body;
sp->timeout.tv_sec = 300;
msgbuf_init(&sp->w);
sp->w.fd = fd;
diff --git a/usr.sbin/smtpd/client.h b/usr.sbin/smtpd/client.h
index c7ab300e027..a0197e6ad2c 100644
--- a/usr.sbin/smtpd/client.h
+++ b/usr.sbin/smtpd/client.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: client.h,v 1.13 2010/11/28 13:56:43 gilles Exp $ */
+/* $OpenBSD: client.h,v 1.14 2011/03/26 10:59:59 gilles Exp $ */
/*
* Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
@@ -103,7 +103,7 @@ struct smtp_client {
char status[1024];
};
-struct smtp_client *client_init(int, int, char *, int);
+struct smtp_client *client_init(int, FILE *, char *, int);
void client_ssl_smtps(struct smtp_client *);
void client_ssl_optional(struct smtp_client *);
void client_certificate(struct smtp_client *, char *,
diff --git a/usr.sbin/smtpd/enqueue.c b/usr.sbin/smtpd/enqueue.c
index 7a36cf83599..1c304012535 100644
--- a/usr.sbin/smtpd/enqueue.c
+++ b/usr.sbin/smtpd/enqueue.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: enqueue.c,v 1.41 2010/11/28 14:35:58 gilles Exp $ */
+/* $OpenBSD: enqueue.c,v 1.42 2011/03/26 10:59:59 gilles Exp $ */
/*
* Copyright (c) 2005 Henning Brauer <henning@bulabula.org>
@@ -196,7 +196,7 @@ enqueue(int argc, char *argv[])
/* init session */
rewind(fp);
- msg.pcb = client_init(msg.fd, fileno(fp), "localhost", verbose);
+ msg.pcb = client_init(msg.fd, fp, "localhost", verbose);
/* set envelope from */
client_sender(msg.pcb, "%s", msg.from);
@@ -237,6 +237,7 @@ enqueue(int argc, char *argv[])
err(1, "event_dispatch");
client_close(msg.pcb);
+ fclose(fp);
exit(0);
}
diff --git a/usr.sbin/smtpd/mta.c b/usr.sbin/smtpd/mta.c
index 7b83e44caed..48e8f9c02de 100644
--- a/usr.sbin/smtpd/mta.c
+++ b/usr.sbin/smtpd/mta.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mta.c,v 1.98 2011/03/21 13:02:52 gilles Exp $ */
+/* $OpenBSD: mta.c,v 1.99 2011/03/26 10:59:59 gilles Exp $ */
/*
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -76,7 +76,6 @@ mta_imsg(struct smtpd *env, struct imsgev *iev, struct imsg *imsg)
s->id = b->id;
s->state = MTA_INIT;
s->env = env;
- s->datafd = -1;
/* establish host name */
if (b->rule.r_action == A_RELAYVIA) {
@@ -444,7 +443,7 @@ mta_enter_state(struct mta_session *s, int newstate, void *p)
*/
log_debug("mta: entering smtp phase");
- pcb = client_init(s->fd, s->datafd, s->env->sc_hostname, 1);
+ pcb = client_init(s->fd, s->datafp, s->env->sc_hostname, 1);
/* lookup SSL certificate */
if (s->cert) {
@@ -513,6 +512,7 @@ mta_enter_state(struct mta_session *s, int newstate, void *p)
TAILQ_REMOVE(&s->relays, relay, entry);
free(relay);
}
+ fclose(s->datafp);
free(s->secret);
free(s->host);
free(s->cert);
@@ -567,11 +567,12 @@ mta_pickup(struct mta_session *s, void *p)
case MTA_DATA:
/* QUEUE replied to body fd request. */
- s->datafd = *(int *)p;
- if (s->datafd == -1)
+ if (*(int *)p == -1)
fatalx("mta cannot obtain msgfd");
- else
- mta_enter_state(s, MTA_CONNECT, NULL);
+ s->datafp = fdopen(*(int *)p, "r");
+ if (s->datafp == NULL)
+ fatal("fdopen");
+ mta_enter_state(s, MTA_CONNECT, NULL);
break;
case MTA_CONNECT:
diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h
index 270a31ba01a..66458a24865 100644
--- a/usr.sbin/smtpd/smtpd.h
+++ b/usr.sbin/smtpd/smtpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpd.h,v 1.203 2011/03/26 10:54:22 eric Exp $ */
+/* $OpenBSD: smtpd.h,v 1.204 2011/03/26 10:59:59 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -852,7 +852,7 @@ struct mta_session {
objid_t secmapid;
char *secret;
int fd;
- int datafd;
+ FILE *datafp;
struct event ev;
char *cert;
void *pcb;