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.
8 * Copyright (c) 1987-2009 by the citadel.org team
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/types.h>
32 #include <sys/socket.h>
40 #include <libcitadel.h>
45 #include "citadel_dirs.h"
50 void strip_trailing_nonprint(char *buf)
52 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) )
53 buf[strlen(buf) - 1] = 0;
57 void timeout(int signum)
63 int uds_connectsock(char *sockpath)
66 struct sockaddr_un addr;
68 memset(&addr, 0, sizeof(addr));
69 addr.sun_family = AF_UNIX;
70 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
72 s = socket(AF_UNIX, SOCK_STREAM, 0);
74 fprintf(stderr, "citmail: Can't create socket: %s\n",
79 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
80 fprintf(stderr, "citmail: can't connect: %s\n",
91 * input binary data from socket
93 void serv_read(char *buf, int bytes)
99 rlen = read(serv_sock, &buf[len], bytes - len);
109 * send binary to server
111 void serv_write(char *buf, int nbytes)
113 int bytes_written = 0;
115 while (bytes_written < nbytes) {
116 retval = write(serv_sock, &buf[bytes_written],
117 nbytes - bytes_written);
121 bytes_written = bytes_written + retval;
128 * input string from socket - implemented in terms of serv_read()
130 void serv_gets(char *buf)
134 /* Read one character at a time.
137 serv_read(&buf[i], 1);
138 if (buf[i] == '\n' || i == (SIZ-1))
142 /* If we got a long line, discard characters until the newline.
145 while (buf[i] != '\n')
146 serv_read(&buf[i], 1);
148 /* Strip all trailing nonprintables (crlf)
151 strip_trailing_nonprint(buf);
152 if (debug) fprintf(stderr, "> %s\n", buf);
157 * send line to server - implemented in terms of serv_write()
159 void serv_puts(char *buf)
161 if (debug) fprintf(stderr, "< %s\n", buf);
162 serv_write(buf, strlen(buf));
168 void cleanup(int exitcode) {
172 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
173 fprintf(stderr, "Please check your Citadel configuration.\n");
182 int main(int argc, char **argv) {
192 char relhome[PATH_MAX]="";
193 char ctdldir[PATH_MAX]=CTDLDIR;
196 char **recipients = NULL;
197 int num_recipients = 0;
199 int read_recipients_from_headers = 0;
200 char *add_these_recipients = NULL;
202 for (i=1; i<argc; ++i) {
203 if (!strcmp(argv[i], "-d")) {
206 else if (!strcmp(argv[i], "-t")) {
207 read_recipients_from_headers = 1;
209 else if (argv[i][0] != '-') {
211 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
212 recipients[num_recipients - 1] = strdup(argv[i]);
216 /* TODO: should we be able to calculate relative dirs? */
217 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
219 pw = getpwuid(getuid());
222 if (fp == NULL) return(errno);
223 serv_sock = uds_connectsock(file_lmtp_socket); /* FIXME: if called as 'sendmail' connect to file_lmtp_unfiltered_socket */
226 fprintf(stderr, "%s\n", &buf[4]);
227 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
231 sp = strchr (buf, ' ');
233 if (debug) fprintf(stderr, "citmail: ould not calculate hostname.\n");
237 ep = strchr (sp, ' ');
238 if (ep == NULL) cleanup(3);
240 strncpy(hostname, sp, sizeof hostname);
242 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
243 while (fgets(buf, 1024, stdin) != NULL) {
244 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
246 if (from_header == 0) {
247 fprintf(fp, "%s%s", fromline, buf);
250 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
251 strcpy(fromline, buf);
255 if (read_recipients_from_headers) {
256 add_these_recipients = NULL;
257 if ((isspace(buf[0])) && (to_or_cc)) {
258 add_these_recipients = buf;
261 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
268 add_these_recipients = &buf[3];
272 if (add_these_recipients) {
273 int num_recp_on_this_line;
276 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
277 for (i=0; i<num_recp_on_this_line; ++i) {
278 extract_token(this_recp, add_these_recipients,
279 i, ',', sizeof this_recp);
281 if (!IsEmptyStr(this_recp)) {
283 recipients = realloc(recipients,
284 (num_recipients * sizeof (char *)));
285 recipients[num_recipients - 1] = strdup(this_recp);
291 fprintf(fp, "%s", buf);
293 strip_trailing_nonprint(fromline);
295 sprintf(buf, "LHLO %s", hostname);
300 } while (buf[3] == '-');
301 if (buf[0] != '2') cleanup(4);
303 snprintf(buf, sizeof buf, "MAIL %s", fromline);
306 if (buf[0] != '2') cleanup(5);
308 for (i=0; i<num_recipients; ++i) {
309 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
318 if (buf[0]!='3') cleanup(6);
321 while (fgets(buf, sizeof buf, fp) != NULL) {
322 strip_trailing_nonprint(buf);
328 fprintf(stderr, "%s\n", &buf[4]);
335 /* We won't actually reach this statement but the compiler will
336 * display a spurious warning about an invalid return type if
337 * we don't return an int.