a7a4c9006e69b6d083f673b929437ed8616aa090
[citadel.git] / citadel / utils / citmail.c
1 /*
2  * This program attempts to act like a local MDA if you're using sendmail or
3  * some other non-Citadel MTA.  It basically just contacts the Citadel LMTP
4  * listener on a unix domain socket and transmits the message.
5  *
6  * Copyright (c) 1987-2012 by the citadel.org team
7  *
8  *  This program is open source software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 3.
10  *
11  *  This program 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
17 #include "sysdep.h"
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <signal.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <netdb.h>
27 #include <string.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <limits.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #ifndef HAVE_SNPRINTF
35 #include "snprintf.h"
36 #endif
37 #include "citadel_dirs.h"
38
39 int serv_sock;
40 int debug = 0;
41
42 void strip_trailing_nonprint(char *buf)
43 {
44         while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) )
45                 buf[strlen(buf) - 1] = 0;
46 }
47
48
49 void timeout(int signum)
50 {
51         exit(signum);
52 }
53
54
55 int uds_connectsock(char *sockpath)
56 {
57         int s;
58         struct sockaddr_un addr;
59
60         memset(&addr, 0, sizeof(addr));
61         addr.sun_family = AF_UNIX;
62         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
63
64         s = socket(AF_UNIX, SOCK_STREAM, 0);
65         if (s < 0) {
66                 fprintf(stderr, "citmail: Can't create socket: %s\n",
67                         strerror(errno));
68                 exit(3);
69         }
70
71         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
72                 fprintf(stderr, "citmail: can't connect: %s\n",
73                         strerror(errno));
74                 close(s);
75                 exit(3);
76         }
77
78         return s;
79 }
80
81
82 /*
83  * input binary data from socket
84  */
85 void serv_read(char *buf, int bytes)
86 {
87         int len, rlen;
88
89         len = 0;
90         while (len < bytes) {
91                 rlen = read(serv_sock, &buf[len], bytes - len);
92                 if (rlen < 1) {
93                         return;
94                 }
95                 len = len + rlen;
96         }
97 }
98
99
100 /*
101  * send binary to server
102  */
103 void serv_write(char *buf, int nbytes)
104 {
105         int bytes_written = 0;
106         int retval;
107         while (bytes_written < nbytes) {
108                 retval = write(serv_sock, &buf[bytes_written],
109                                nbytes - bytes_written);
110                 if (retval < 1) {
111                         return;
112                 }
113                 bytes_written = bytes_written + retval;
114         }
115 }
116
117
118
119 /*
120  * input string from socket - implemented in terms of serv_read()
121  */
122 void serv_gets(char *buf)
123 {
124         int i;
125
126         /* Read one character at a time.
127          */
128         for (i = 0;; i++) {
129                 serv_read(&buf[i], 1);
130                 if (buf[i] == '\n' || i == (SIZ-1))
131                         break;
132         }
133
134         /* If we got a long line, discard characters until the newline.
135          */
136         if (i == (SIZ-1))
137                 while (buf[i] != '\n')
138                         serv_read(&buf[i], 1);
139
140         /* Strip all trailing nonprintables (crlf)
141          */
142         buf[i] = 0;
143         strip_trailing_nonprint(buf);
144         if (debug) fprintf(stderr, "> %s\n", buf);
145 }
146
147
148 /*
149  * send line to server - implemented in terms of serv_write()
150  */
151 void serv_puts(char *buf)
152 {
153         if (debug) fprintf(stderr, "< %s\n", buf);
154         serv_write(buf, strlen(buf));
155         serv_write("\n", 1);
156 }
157
158
159
160 void cleanup(int exitcode) {
161         char buf[1024];
162
163         if (exitcode != 0) {
164                 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
165                 fprintf(stderr, "Please check your Citadel configuration.\n");
166         }
167         serv_puts("QUIT");
168         serv_gets(buf);
169         exit(exitcode);
170 }
171
172
173
174 int main(int argc, char **argv) {
175         char buf[1024];
176         char fromline[1024];
177         FILE *fp;
178         int i;
179         struct passwd *pw;
180         int from_header = 0;
181         int in_body = 0;
182         int relh=0;
183         int home=0;
184         char relhome[PATH_MAX]="";
185         char ctdldir[PATH_MAX]=CTDLDIR;
186         char *sp, *ep;
187         char hostname[256];
188         char **recipients = NULL;
189         int num_recipients = 0;
190         int to_or_cc = 0;
191         int read_recipients_from_headers = 0;
192         char *add_these_recipients = NULL;
193
194         for (i=1; i<argc; ++i) {
195                 if (!strcmp(argv[i], "-d")) {
196                         debug = 1;
197                 }
198                 else if (!strcmp(argv[i], "-t")) {
199                         read_recipients_from_headers = 1;
200                 }
201                 else if (argv[i][0] != '-') {
202                         ++num_recipients;
203                         recipients = realloc(recipients, (num_recipients * sizeof (char *)));
204                         recipients[num_recipients - 1] = strdup(argv[i]);
205                 }
206         }
207                
208         /* TODO: should we be able to calculate relative dirs? */
209         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
210
211         pw = getpwuid(getuid());
212
213         fp = tmpfile();
214         if (fp == NULL) return(errno);
215         serv_sock = uds_connectsock(file_lmtp_socket);  /* FIXME: if called as 'sendmail' connect to file_lmtp_unfiltered_socket */
216         serv_gets(buf);
217         if (buf[0] != '2') {
218                 fprintf(stderr, "%s\n", &buf[4]);
219                 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
220                 cleanup(1);
221         }
222
223         sp = strchr (buf, ' ');
224         if (sp == NULL) {
225                 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
226                 cleanup(2);
227         }
228         sp ++;
229         ep = strchr (sp, ' ');
230         if (ep == NULL) {
231                 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
232                 cleanup(3);
233         }
234         *ep = '\0';
235         strncpy(hostname, sp, sizeof hostname);
236
237         snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
238         while (fgets(buf, 1024, stdin) != NULL) {
239                 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
240                         in_body = 1;
241                         if (from_header == 0) {
242                                 fprintf(fp, "%s%s", fromline, buf);
243                         }
244                 }
245                 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
246                         strcpy(fromline, buf);
247                         from_header = 1;
248                 }
249
250                 if (read_recipients_from_headers) {
251                         add_these_recipients = NULL;
252                         if ((isspace(buf[0])) && (to_or_cc)) {
253                                 add_these_recipients = buf;
254                         }
255                         else {
256                                 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
257                                         to_or_cc = 1;
258                                 }
259                                 else {
260                                         to_or_cc = 0;
261                                 }
262                                 if (to_or_cc) {
263                                         add_these_recipients = &buf[3];
264                                 }
265                         }
266
267                         if (add_these_recipients) {
268                                 int num_recp_on_this_line;
269                                 char this_recp[256];
270
271                                 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
272                                 for (i=0; i<num_recp_on_this_line; ++i) {
273                                         extract_token(this_recp, add_these_recipients,
274                                                 i, ',', sizeof this_recp);
275                                         striplt(this_recp);
276                                         if (!IsEmptyStr(this_recp)) {
277                                                 ++num_recipients;
278                                                 recipients = realloc(recipients,
279                                                         (num_recipients * sizeof (char *)));
280                                                 recipients[num_recipients - 1] = strdup(this_recp);
281                                         }
282                                 }
283                         }
284                 }
285
286                 fprintf(fp, "%s", buf);
287         }
288         strip_trailing_nonprint(fromline);
289
290         sprintf(buf, "LHLO %s", hostname);
291         serv_puts(buf);
292         do {
293                 serv_gets(buf);
294                 strcat(buf, "    ");
295         } while (buf[3] == '-');
296         if (buf[0] != '2') {
297                 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
298                 cleanup(4);
299         }
300
301         snprintf(buf, sizeof buf, "MAIL %s", fromline);
302         serv_puts(buf);
303         serv_gets(buf);
304         if (buf[0] != '2') {
305                 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
306                 cleanup(5);
307         }
308
309         for (i=0; i<num_recipients; ++i) {
310                 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
311                 serv_puts(buf);
312                 serv_gets(buf);
313                 free(recipients[i]);
314         }
315         free(recipients);
316
317         serv_puts("DATA");
318         serv_gets(buf);
319         if (buf[0]!='3') {
320                 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
321                 cleanup(6);
322         }
323
324         rewind(fp);
325         while (fgets(buf, sizeof buf, fp) != NULL) {
326                 strip_trailing_nonprint(buf);
327                 serv_puts(buf);
328         }
329         serv_puts(".");
330         serv_gets(buf);
331         if (buf[0] != '2') {
332                 fprintf(stderr, "%s\n", &buf[4]);
333                 cleanup(7);
334         }
335         else {
336                 cleanup(0);
337         }
338
339         /* We won't actually reach this statement but the compiler will
340          * display a spurious warning about an invalid return type if
341          * we don't return an int.
342          */
343         return(0);
344 }