chiark / gitweb /
agent: Allow threads to interrupt main select loop with SIGCONT.
[gnupg2.git] / tools / send-mail.c
1 /* send-mail.c - Invoke sendmail or other delivery tool.
2  * Copyright (C) 2016 g10 Code GmbH
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "util.h"
26 #include "exectool.h"
27 #include "sysutils.h"
28 #include "send-mail.h"
29
30
31 static gpg_error_t
32 run_sendmail (estream_t data)
33 {
34   gpg_error_t err;
35   const char pgmname[] = "/usr/lib/sendmail";
36   const char *argv[3];
37
38   argv[0] = "-oi";
39   argv[1] = "-t";
40   argv[2] = NULL;
41
42   err = gnupg_exec_tool_stream (pgmname, argv, data, NULL, NULL, NULL, NULL);
43   if (err)
44     log_error ("running '%s' failed: %s\n", pgmname, gpg_strerror (err));
45   return err;
46 }
47
48
49 /* Send the data in FP as mail.  */
50 gpg_error_t
51 send_mail (estream_t fp)
52 {
53   return run_sendmail (fp);
54 }
55
56
57 /* Convenience function to write a mail to a named file. */
58 gpg_error_t
59 send_mail_to_file (estream_t fp, const char *fname)
60 {
61   gpg_error_t err;
62   estream_t outfp = NULL;
63   char *buffer = NULL;
64   size_t buffersize = 32 * 1024;
65   size_t nbytes, nwritten;
66
67   if (!fname)
68     fname = "-";
69
70   buffer = xtrymalloc (buffersize);
71   if (!buffer)
72     return gpg_error_from_syserror ();
73
74   outfp = !strcmp (fname,"-")? es_stdout : es_fopen (fname, "wb");
75   if (!outfp)
76     {
77       err = gpg_error_from_syserror ();
78       log_error ("error creating '%s': %s\n", fname, gpg_strerror (err));
79       goto leave;
80     }
81   for (;;)
82     {
83       if (es_read (fp, buffer, sizeof buffer, &nbytes))
84         {
85           err = gpg_error_from_syserror ();
86           log_error ("error reading '%s': %s\n",
87                      es_fname_get (fp), gpg_strerror (err));
88           goto leave;
89         }
90
91       if (!nbytes)
92         {
93           err = 0;
94           break; /* Ready.  */
95         }
96
97       if (es_write (outfp, buffer, nbytes, &nwritten))
98         {
99           err = gpg_error_from_syserror ();
100           log_error ("error writing '%s': %s\n", fname, gpg_strerror (err));
101           goto leave;
102         }
103       else if (nwritten != nbytes)
104         {
105           err = gpg_error (GPG_ERR_EIO);
106           log_error ("error writing '%s': %s\n", fname, "short write");
107           goto leave;
108         }
109     }
110
111
112  leave:
113   if (err)
114     {
115       if (outfp && outfp != es_stdout)
116         {
117           es_fclose (outfp);
118           gnupg_remove (fname);
119         }
120     }
121   else if (outfp && outfp != es_stdout && es_fclose (outfp))
122     {
123       err = gpg_error_from_syserror ();
124       log_error ("error closing '%s': %s\n", fname, gpg_strerror (err));
125     }
126
127   xfree (buffer);
128   return err;
129 }