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