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