1 // This program attempts to act like a local MDA if you're using postfix 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-2022 by the citadel.org team
8 // This program is open source software. Use, duplication, or disclosure
9 // is subject to the terms of the GNU General Public License, version 3.
16 #include <sys/types.h>
17 #include <sys/socket.h>
25 #include <libcitadel.h>
26 #include "../server/sysdep.h"
27 #include "../server/citadel.h"
28 #include "../server/citadel_dirs.h"
33 void strip_trailing_nonprint(char *buf) {
34 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) ) {
35 buf[strlen(buf) - 1] = 0;
40 void timeout(int signum) {
45 int uds_connectsock(char *sockpath) {
47 struct sockaddr_un addr;
49 memset(&addr, 0, sizeof(addr));
50 addr.sun_family = AF_UNIX;
51 strcpy(addr.sun_path, sockpath);
53 s = socket(AF_UNIX, SOCK_STREAM, 0);
55 fprintf(stderr, "citmail: Can't create socket: %s\n",
60 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
61 fprintf(stderr, "citmail: can't connect: %s\n",
71 // input binary data from socket
72 void serv_read(char *buf, int bytes) {
77 rlen = read(serv_sock, &buf[len], bytes - len);
86 // send binary to server
87 void serv_write(char *buf, int nbytes) {
88 int bytes_written = 0;
90 while (bytes_written < nbytes) {
91 retval = write(serv_sock, &buf[bytes_written],
92 nbytes - bytes_written);
96 bytes_written = bytes_written + retval;
101 // input string from socket - implemented in terms of serv_read()
102 void serv_gets(char *buf) {
105 // Read one character at a time.
107 serv_read(&buf[i], 1);
108 if (buf[i] == '\n' || i == (SIZ-1))
112 // If we got a long line, discard characters until the newline.
114 while (buf[i] != '\n')
115 serv_read(&buf[i], 1);
117 // Strip all trailing nonprintables (crlf)
119 strip_trailing_nonprint(buf);
120 if (debug) fprintf(stderr, "> %s\n", buf);
124 // send line to server - implemented in terms of serv_write()
125 void serv_puts(char *buf) {
126 if (debug) fprintf(stderr, "< %s\n", buf);
127 serv_write(buf, strlen(buf));
132 void cleanup(int exitcode) {
136 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
137 fprintf(stderr, "Please check your Citadel configuration.\n");
145 int main(int argc, char **argv) {
153 char ctdldir[PATH_MAX]=CTDLDIR;
156 char **recipients = NULL;
157 int num_recipients = 0;
159 int read_recipients_from_headers = 0;
160 char *add_these_recipients = NULL;
162 for (i=1; i<argc; ++i) {
163 if (!strcmp(argv[i], "-d")) {
166 else if (!strcmp(argv[i], "-t")) {
167 read_recipients_from_headers = 1;
169 else if (!strncmp(argv[i], "-h", 2)) {
170 safestrncpy(ctdldir, &argv[i][2], sizeof ctdldir);
172 else if (argv[i][0] != '-') {
174 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
175 recipients[num_recipients - 1] = strdup(argv[i]);
179 if (chdir(ctdldir) != 0) {
180 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
184 pw = getpwuid(getuid());
187 if (fp == NULL) return(errno);
188 serv_sock = uds_connectsock(file_lmtp_socket);
191 fprintf(stderr, "%s\n", &buf[4]);
192 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
196 sp = strchr (buf, ' ');
198 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
202 ep = strchr (sp, ' ');
204 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
210 strncpy(hostname, sp, sizeof hostname);
212 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
213 while (fgets(buf, 1024, stdin) != NULL) {
214 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
216 if (from_header == 0) {
217 fprintf(fp, "%s%s", fromline, buf);
220 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
221 strcpy(fromline, buf);
225 if (read_recipients_from_headers) {
226 add_these_recipients = NULL;
227 if ((isspace(buf[0])) && (to_or_cc)) {
228 add_these_recipients = buf;
231 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
238 add_these_recipients = &buf[3];
242 if (add_these_recipients) {
243 int num_recp_on_this_line;
246 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
247 for (i=0; i<num_recp_on_this_line; ++i) {
248 extract_token(this_recp, add_these_recipients,
249 i, ',', sizeof this_recp);
251 if (!IsEmptyStr(this_recp)) {
253 recipients = realloc(recipients,
254 (num_recipients * sizeof (char *)));
255 recipients[num_recipients - 1] = strdup(this_recp);
261 fprintf(fp, "%s", buf);
263 strip_trailing_nonprint(fromline);
265 sprintf(buf, "LHLO %s", hostname);
270 } while (buf[3] == '-');
272 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
276 snprintf(buf, sizeof buf, "MAIL %s", fromline);
280 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
284 for (i=0; i<num_recipients; ++i) {
285 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
295 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
300 while (fgets(buf, sizeof buf, fp) != NULL) {
301 strip_trailing_nonprint(buf);
307 fprintf(stderr, "%s\n", &buf[4]);
314 // We won't actually reach this statement but the compiler will
315 // display a spurious warning about an invalid return type if
316 // we don't return an int.