1 // This program attempts to act like a local MDA if you're using sendmail or
2 // some other non-Citadel MTA. It basically just contacts the Citadel LMTP
3 // listener on a unix domain socket and transmits the message. Really though,
4 // if your MTA supports LMTP then you definitely should be using that instead.
6 // Copyright (c) 1987-2021 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.
22 #include <sys/types.h>
23 #include <sys/socket.h>
31 #include <libcitadel.h>
33 #include "citadel_dirs.h"
38 void strip_trailing_nonprint(char *buf) {
39 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) ) {
40 buf[strlen(buf) - 1] = 0;
45 void timeout(int signum) {
50 int uds_connectsock(char *sockpath) {
52 struct sockaddr_un addr;
54 memset(&addr, 0, sizeof(addr));
55 addr.sun_family = AF_UNIX;
56 strcpy(addr.sun_path, sockpath);
58 s = socket(AF_UNIX, SOCK_STREAM, 0);
60 fprintf(stderr, "citmail: Can't create socket: %s\n",
65 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
66 fprintf(stderr, "citmail: can't connect: %s\n",
77 * input binary data from socket
79 void serv_read(char *buf, int bytes)
85 rlen = read(serv_sock, &buf[len], bytes - len);
95 * send binary to server
97 void serv_write(char *buf, int nbytes)
99 int bytes_written = 0;
101 while (bytes_written < nbytes) {
102 retval = write(serv_sock, &buf[bytes_written],
103 nbytes - bytes_written);
107 bytes_written = bytes_written + retval;
114 * input string from socket - implemented in terms of serv_read()
116 void serv_gets(char *buf)
120 /* Read one character at a time.
123 serv_read(&buf[i], 1);
124 if (buf[i] == '\n' || i == (SIZ-1))
128 /* If we got a long line, discard characters until the newline.
131 while (buf[i] != '\n')
132 serv_read(&buf[i], 1);
134 /* Strip all trailing nonprintables (crlf)
137 strip_trailing_nonprint(buf);
138 if (debug) fprintf(stderr, "> %s\n", buf);
143 * send line to server - implemented in terms of serv_write()
145 void serv_puts(char *buf)
147 if (debug) fprintf(stderr, "< %s\n", buf);
148 serv_write(buf, strlen(buf));
154 void cleanup(int exitcode) {
158 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
159 fprintf(stderr, "Please check your Citadel configuration.\n");
168 int main(int argc, char **argv) {
176 char ctdldir[PATH_MAX]=CTDLDIR;
179 char **recipients = NULL;
180 int num_recipients = 0;
182 int read_recipients_from_headers = 0;
183 char *add_these_recipients = NULL;
185 for (i=1; i<argc; ++i) {
186 if (!strcmp(argv[i], "-d")) {
189 else if (!strcmp(argv[i], "-t")) {
190 read_recipients_from_headers = 1;
192 else if (!strncmp(argv[i], "-h", 2)) {
193 safestrncpy(ctdldir, &argv[i][2], sizeof ctdldir);
195 else if (argv[i][0] != '-') {
197 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
198 recipients[num_recipients - 1] = strdup(argv[i]);
202 if (chdir(ctdldir) != 0) {
203 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
207 pw = getpwuid(getuid());
210 if (fp == NULL) return(errno);
211 serv_sock = uds_connectsock(file_lmtp_socket);
214 fprintf(stderr, "%s\n", &buf[4]);
215 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
219 sp = strchr (buf, ' ');
221 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
225 ep = strchr (sp, ' ');
227 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
233 strncpy(hostname, sp, sizeof hostname);
235 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
236 while (fgets(buf, 1024, stdin) != NULL) {
237 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
239 if (from_header == 0) {
240 fprintf(fp, "%s%s", fromline, buf);
243 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
244 strcpy(fromline, buf);
248 if (read_recipients_from_headers) {
249 add_these_recipients = NULL;
250 if ((isspace(buf[0])) && (to_or_cc)) {
251 add_these_recipients = buf;
254 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
261 add_these_recipients = &buf[3];
265 if (add_these_recipients) {
266 int num_recp_on_this_line;
269 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
270 for (i=0; i<num_recp_on_this_line; ++i) {
271 extract_token(this_recp, add_these_recipients,
272 i, ',', sizeof this_recp);
274 if (!IsEmptyStr(this_recp)) {
276 recipients = realloc(recipients,
277 (num_recipients * sizeof (char *)));
278 recipients[num_recipients - 1] = strdup(this_recp);
284 fprintf(fp, "%s", buf);
286 strip_trailing_nonprint(fromline);
288 sprintf(buf, "LHLO %s", hostname);
293 } while (buf[3] == '-');
295 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
299 snprintf(buf, sizeof buf, "MAIL %s", fromline);
303 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
307 for (i=0; i<num_recipients; ++i) {
308 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
318 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
323 while (fgets(buf, sizeof buf, fp) != NULL) {
324 strip_trailing_nonprint(buf);
330 fprintf(stderr, "%s\n", &buf[4]);
337 /* We won't actually reach this statement but the compiler will
338 * display a spurious warning about an invalid return type if
339 * we don't return an int.