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.
6 * Copyright (c) 1987-2012 by the citadel.org team
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.
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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
32 #include <libcitadel.h>
37 #include "citadel_dirs.h"
42 void strip_trailing_nonprint(char *buf)
44 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) )
45 buf[strlen(buf) - 1] = 0;
49 void timeout(int signum)
55 int uds_connectsock(char *sockpath)
58 struct sockaddr_un addr;
60 memset(&addr, 0, sizeof(addr));
61 addr.sun_family = AF_UNIX;
62 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
64 s = socket(AF_UNIX, SOCK_STREAM, 0);
66 fprintf(stderr, "citmail: Can't create socket: %s\n",
71 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
72 fprintf(stderr, "citmail: can't connect: %s\n",
83 * input binary data from socket
85 void serv_read(char *buf, int bytes)
91 rlen = read(serv_sock, &buf[len], bytes - len);
101 * send binary to server
103 void serv_write(char *buf, int nbytes)
105 int bytes_written = 0;
107 while (bytes_written < nbytes) {
108 retval = write(serv_sock, &buf[bytes_written],
109 nbytes - bytes_written);
113 bytes_written = bytes_written + retval;
120 * input string from socket - implemented in terms of serv_read()
122 void serv_gets(char *buf)
126 /* Read one character at a time.
129 serv_read(&buf[i], 1);
130 if (buf[i] == '\n' || i == (SIZ-1))
134 /* If we got a long line, discard characters until the newline.
137 while (buf[i] != '\n')
138 serv_read(&buf[i], 1);
140 /* Strip all trailing nonprintables (crlf)
143 strip_trailing_nonprint(buf);
144 if (debug) fprintf(stderr, "> %s\n", buf);
149 * send line to server - implemented in terms of serv_write()
151 void serv_puts(char *buf)
153 if (debug) fprintf(stderr, "< %s\n", buf);
154 serv_write(buf, strlen(buf));
160 void cleanup(int exitcode) {
164 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
165 fprintf(stderr, "Please check your Citadel configuration.\n");
174 int main(int argc, char **argv) {
184 char relhome[PATH_MAX]="";
185 char ctdldir[PATH_MAX]=CTDLDIR;
188 char **recipients = NULL;
189 int num_recipients = 0;
191 int read_recipients_from_headers = 0;
192 char *add_these_recipients = NULL;
194 for (i=1; i<argc; ++i) {
195 if (!strcmp(argv[i], "-d")) {
198 else if (!strcmp(argv[i], "-t")) {
199 read_recipients_from_headers = 1;
201 else if (argv[i][0] != '-') {
203 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
204 recipients[num_recipients - 1] = strdup(argv[i]);
208 /* TODO: should we be able to calculate relative dirs? */
209 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
211 pw = getpwuid(getuid());
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 */
218 fprintf(stderr, "%s\n", &buf[4]);
219 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
223 sp = strchr (buf, ' ');
225 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
229 ep = strchr (sp, ' ');
231 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
235 strncpy(hostname, sp, sizeof hostname);
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) ) {
241 if (from_header == 0) {
242 fprintf(fp, "%s%s", fromline, buf);
245 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
246 strcpy(fromline, buf);
250 if (read_recipients_from_headers) {
251 add_these_recipients = NULL;
252 if ((isspace(buf[0])) && (to_or_cc)) {
253 add_these_recipients = buf;
256 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
263 add_these_recipients = &buf[3];
267 if (add_these_recipients) {
268 int num_recp_on_this_line;
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);
276 if (!IsEmptyStr(this_recp)) {
278 recipients = realloc(recipients,
279 (num_recipients * sizeof (char *)));
280 recipients[num_recipients - 1] = strdup(this_recp);
286 fprintf(fp, "%s", buf);
288 strip_trailing_nonprint(fromline);
290 sprintf(buf, "LHLO %s", hostname);
295 } while (buf[3] == '-');
297 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
301 snprintf(buf, sizeof buf, "MAIL %s", fromline);
305 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
309 for (i=0; i<num_recipients; ++i) {
310 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
320 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
325 while (fgets(buf, sizeof buf, fp) != NULL) {
326 strip_trailing_nonprint(buf);
332 fprintf(stderr, "%s\n", &buf[4]);
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.