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-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
30 #include <sys/socket.h>
38 #include <libcitadel.h>
43 #include "citadel_dirs.h"
48 void strip_trailing_nonprint(char *buf)
50 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) )
51 buf[strlen(buf) - 1] = 0;
55 void timeout(int signum)
61 int uds_connectsock(char *sockpath)
64 struct sockaddr_un addr;
66 memset(&addr, 0, sizeof(addr));
67 addr.sun_family = AF_UNIX;
68 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
70 s = socket(AF_UNIX, SOCK_STREAM, 0);
72 fprintf(stderr, "citmail: Can't create socket: %s\n",
77 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
78 fprintf(stderr, "citmail: can't connect: %s\n",
89 * input binary data from socket
91 void serv_read(char *buf, int bytes)
97 rlen = read(serv_sock, &buf[len], bytes - len);
107 * send binary to server
109 void serv_write(char *buf, int nbytes)
111 int bytes_written = 0;
113 while (bytes_written < nbytes) {
114 retval = write(serv_sock, &buf[bytes_written],
115 nbytes - bytes_written);
119 bytes_written = bytes_written + retval;
126 * input string from socket - implemented in terms of serv_read()
128 void serv_gets(char *buf)
132 /* Read one character at a time.
135 serv_read(&buf[i], 1);
136 if (buf[i] == '\n' || i == (SIZ-1))
140 /* If we got a long line, discard characters until the newline.
143 while (buf[i] != '\n')
144 serv_read(&buf[i], 1);
146 /* Strip all trailing nonprintables (crlf)
149 strip_trailing_nonprint(buf);
150 if (debug) fprintf(stderr, "> %s\n", buf);
155 * send line to server - implemented in terms of serv_write()
157 void serv_puts(char *buf)
159 if (debug) fprintf(stderr, "< %s\n", buf);
160 serv_write(buf, strlen(buf));
166 void cleanup(int exitcode) {
170 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
171 fprintf(stderr, "Please check your Citadel configuration.\n");
180 int main(int argc, char **argv) {
190 char relhome[PATH_MAX]="";
191 char ctdldir[PATH_MAX]=CTDLDIR;
194 char **recipients = NULL;
195 int num_recipients = 0;
197 int read_recipients_from_headers = 0;
198 char *add_these_recipients = NULL;
200 for (i=1; i<argc; ++i) {
201 if (!strcmp(argv[i], "-d")) {
204 else if (!strcmp(argv[i], "-t")) {
205 read_recipients_from_headers = 1;
207 else if (argv[i][0] != '-') {
209 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
210 recipients[num_recipients - 1] = strdup(argv[i]);
214 /* TODO: should we be able to calculate relative dirs? */
215 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
217 pw = getpwuid(getuid());
220 if (fp == NULL) return(errno);
221 serv_sock = uds_connectsock(file_lmtp_socket); /* FIXME: if called as 'sendmail' connect to file_lmtp_unfiltered_socket */
224 fprintf(stderr, "%s\n", &buf[4]);
225 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
229 sp = strchr (buf, ' ');
231 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
235 ep = strchr (sp, ' ');
237 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
241 strncpy(hostname, sp, sizeof hostname);
243 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
244 while (fgets(buf, 1024, stdin) != NULL) {
245 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
247 if (from_header == 0) {
248 fprintf(fp, "%s%s", fromline, buf);
251 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
252 strcpy(fromline, buf);
256 if (read_recipients_from_headers) {
257 add_these_recipients = NULL;
258 if ((isspace(buf[0])) && (to_or_cc)) {
259 add_these_recipients = buf;
262 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
269 add_these_recipients = &buf[3];
273 if (add_these_recipients) {
274 int num_recp_on_this_line;
277 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
278 for (i=0; i<num_recp_on_this_line; ++i) {
279 extract_token(this_recp, add_these_recipients,
280 i, ',', sizeof this_recp);
282 if (!IsEmptyStr(this_recp)) {
284 recipients = realloc(recipients,
285 (num_recipients * sizeof (char *)));
286 recipients[num_recipients - 1] = strdup(this_recp);
292 fprintf(fp, "%s", buf);
294 strip_trailing_nonprint(fromline);
296 sprintf(buf, "LHLO %s", hostname);
301 } while (buf[3] == '-');
303 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
307 snprintf(buf, sizeof buf, "MAIL %s", fromline);
311 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
315 for (i=0; i<num_recipients; ++i) {
316 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
326 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
331 while (fgets(buf, sizeof buf, fp) != NULL) {
332 strip_trailing_nonprint(buf);
338 fprintf(stderr, "%s\n", &buf[4]);
345 /* We won't actually reach this statement but the compiler will
346 * display a spurious warning about an invalid return type if
347 * we don't return an int.