Commit 1d370559 1d37055984621199501450d05a0232fb904294b8 by Alain Magloire

Register sendmail_record to send mail via _PATH_SENDMAIL.

Modify send.c to sendmail an create temporary filenames more securely.
Copyright change --> 2001 and extra spaces nukes, done by emacs automaticaly.
1 parent 094bda98
......@@ -137,6 +137,8 @@ main (int argc, char **argv)
list_append (bookie, path_record);
list_append (bookie, pop_record);
list_append (bookie, imap_record);
/* Only use sendmail for mail ?? */
list_append (bookie, sendmail_record);
}
signal (SIGPIPE, SIG_IGN);
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -15,6 +15,14 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* FIXME: Those headers should be in "mail.h". */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <paths.h>
#include "mail.h"
/*
......@@ -25,11 +33,35 @@ int
mail_send (int argc, char **argv)
{
char *to = NULL, *cc = NULL, *bcc = NULL, *subj = NULL;
char *filename = tempnam (getenv ("TMPDIR"), "mu");
FILE *file = fopen (filename, "w");
char *buf = NULL;
size_t n;
size_t n = 0;
int done = 0;
int fd;
char *filename;
FILE *file;
const char *tmpdir;
/* We have to be extra carefull about opening temporary files, since we
may be running with extra privilege i.e setgid(). */
tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : "/tmp";
filename = alloca (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
sprintf (filename, "%s/muXXXXXX", tmpdir);
#ifdef HAVE_MKSTEMP
fd = mkstemp (filename);
#else
if (mktemp (filename))
fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, 0600);
else
fd = -1;
#endif
if (fd == -1)
{
fprintf (stderr, "Can not open temporary file");
return 1;
}
file = fdopen (fd, "w");
if (argc < 2)
to = readline ("To: ");
......@@ -186,16 +218,61 @@ mail_send (int argc, char **argv)
file = fopen (filename, "r");
if (file != NULL)
{
/* FIXME: create a mailer here */
mailer_t mailer;
message_t msg = NULL;
int status;
char *mailer_name = alloca (strlen ("sendmail:")
+ strlen (_PATH_SENDMAIL) + 1);
sprintf (mailer_name, "sendmail:%s", _PATH_SENDMAIL);
if ((status = mailer_create (&mailer, mailer_name)) != 0
|| (status = mailer_open (mailer, MU_STREAM_RDWR)) != 0)
{
fprintf (stderr, "%s: %s\n", mailer_name, strerror (status));
remove (filename);
return 1;
}
message_create (&msg, NULL);
/* Fill the header. */
{
header_t header = NULL;
message_get_header (msg, &header);
if (to && *to != '\0')
header_set_value (header, MU_HEADER_TO, to, 0);
if (cc && *cc != '\0')
header_set_value (header, MU_HEADER_CC, cc, 0);
if (bcc && *bcc != '\0')
header_set_value (header, MU_HEADER_BCC , bcc, 0);
if (subj && *subj != '\0')
header_set_value (header, MU_HEADER_SUBJECT, subj, 1);
}
/* Fill the body. */
{
body_t body = NULL;
stream_t stream = NULL;
off_t offset = 0;
message_get_body (msg, &body);
body_get_stream (body, &stream);
while (getline (&buf, &n, file) >= 0)
{
printf("%s", buf);
size_t len = strlen (buf);
stream_write (stream, buf, len, offset, &n);
offset += len;
free (buf);
buf = NULL;
}
fclose (file);
}
/* Send the message. */
mailer_send_message (mailer, msg);
message_destroy (&msg, NULL);
mailer_destroy (&mailer);
remove (filename);
return 0;
}
remove (filename);
return 1;
}
......