]> diplodocus.org Git - nmh/blob - uip/viamail.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / viamail.c
1 /* viamail.c -- send multiple files in a MIME message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/md5.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include <h/utils.h>
16 #include "sbr/m_mktemp.h"
17 #include "sbr/base64.h"
18
19 #define VIAMAIL_SWITCHES \
20 X("to mailpath", 0, TOSW) \
21 X("from mailpath", 0, FROMSW) \
22 X("subject subject", 0, SUBJECTSW) \
23 X("parameters arguments", 0, PARAMSW) \
24 X("description text", 0, DESCRIPTSW) \
25 X("comment text", 0, COMMENTSW) \
26 X("delay seconds", 0, DELAYSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
31 X("debug", -5, DEBUGSW) \
32
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(VIAMAIL);
35 #undef X
36
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(VIAMAIL, switches);
39 #undef X
40
41 extern int debugsw;
42 extern int splitsw;
43 extern int verbsw;
44
45 /*
46 * static prototypes
47 */
48 static int via_mail (char *, char *, char *, char *, char *, int, char *);
49
50
51 int
52 main (int argc, char **argv)
53 {
54 int delay = 0;
55 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
56 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
57 char *cp, buf[BUFSIZ];
58 char **argp, **arguments;
59
60 if (nmh_init(argv[0], 2)) { return 1; }
61
62 arguments = getarguments (invo_name, argc, argv, 0);
63 argp = arguments;
64
65 while ((cp = *argp++)) {
66 if (*cp == '-') {
67 switch (smatch (++cp, switches)) {
68 case AMBIGSW:
69 ambigsw (cp, switches);
70 done (1);
71 case UNKWNSW:
72 adios (NULL, "-%s unknown", cp);
73
74 case HELPSW:
75 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
76 print_help (buf, switches, 1);
77 done (0);
78 case VERSIONSW:
79 print_version(invo_name);
80 done (0);
81
82 case TOSW:
83 if (!(f1 = *argp++))
84 adios (NULL, "missing argument to %s", argp[-2]);
85 continue;
86 case SUBJECTSW:
87 if (!(f2 = *argp++))
88 adios (NULL, "missing argument to %s", argp[-2]);
89 continue;
90 case PARAMSW:
91 if (!(f3 = *argp++))
92 adios (NULL, "missing argument to %s", argp[-2]);
93 continue;
94 case DESCRIPTSW:
95 if (!(f4 = *argp++))
96 adios (NULL, "missing argument to %s", argp[-2]);
97 continue;
98 case COMMENTSW:
99 if (!(f5 = *argp++))
100 adios (NULL, "missing argument to %s", argp[-2]);
101 continue;
102 case DELAYSW:
103 if (!(cp = *argp++) || *cp == '-')
104 adios (NULL, "missing argument to %s", argp[-2]);
105
106 /*
107 * If there is an error, just reset the delay parameter
108 * to -1. We will set a default delay later.
109 */
110 if (sscanf (cp, "%d", &delay) != 1)
111 delay = -1;
112 continue;
113 case FROMSW:
114 if (!(f7 = *argp++))
115 adios (NULL, "missing argument to %s", argp[-2]);
116 continue;
117
118 case VERBSW:
119 verbsw = 1;
120 continue;
121 case NVERBSW:
122 verbsw = 0;
123 continue;
124
125 case DEBUGSW:
126 debugsw = 1;
127 continue;
128 }
129 }
130 }
131
132 if (!f1)
133 adios (NULL, "missing -viamail \"mailpath\" switch");
134
135 via_mail (f1, f2, f3, f4, f5, delay, f7);
136 return 0; /* dead code to satisfy the compiler */
137 }
138
139
140 /*
141 * VIAMAIL
142 */
143
144 static int
145 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
146 char *cmntsw, int delay, char *fromsw)
147 {
148 int status, vecp;
149 char tmpfil[BUFSIZ], *program;
150 char **vec;
151 struct stat st;
152 FILE *fp;
153 char *tfile = NULL;
154 char *cp;
155
156 umask (~m_gmprot ());
157
158 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
159 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
160 }
161 strncpy (tmpfil, tfile, sizeof(tmpfil));
162
163 if (!strchr(mailsw, '@'))
164 mailsw = concat (mailsw, "@", LocalName (0), NULL);
165 fprintf (fp, "To: %s\n", mailsw);
166
167 if (subjsw)
168 fprintf (fp, "Subject: %s\n", subjsw);
169
170 if (fromsw) {
171 if (!strchr(fromsw, '@'))
172 fromsw = concat (fromsw, "@", LocalName (0), NULL);
173 fprintf (fp, "From: %s\n", fromsw);
174 }
175
176 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
177 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
178
179 if (parmsw)
180 fprintf (fp, "; %s", parmsw);
181
182 if (cmntsw)
183 fprintf (fp, "\n\t(%s)", cmntsw);
184
185 if (descsw)
186 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
187
188 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
189
190 if (fflush (fp))
191 adios (tmpfil, "error writing to");
192
193 writeBase64aux (stdin, fp, 0);
194 if (fflush (fp))
195 adios (tmpfil, "error writing to");
196
197 if (fstat (fileno (fp), &st) == NOTOK)
198 adios ("failed", "fstat of %s", tmpfil);
199
200 if (delay < 0)
201 splitsw = 10;
202 else
203 splitsw = delay;
204
205 status = 0;
206
207 vec = argsplit(postproc, &program, &vecp);
208 if (verbsw)
209 vec[vecp++] = "-verbose";
210
211 if ((cp = context_find ("credentials"))) {
212 /* post doesn't read context so need to pass credentials. */
213 vec[vecp++] = "-credentials";
214 vec[vecp++] = cp;
215 }
216
217 switch (sendsbr (vec, vecp, program, tmpfil, &st, 0, NULL)) {
218 case DONE:
219 case NOTOK:
220 status++;
221 break;
222 case OK:
223 break;
224 }
225
226 fclose (fp);
227 if (m_unlink (tmpfil) == -1)
228 advise (tmpfil, "unable to remove temp file %s", tmpfil);
229 done (status);
230 return 1;
231 }