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. Use, duplication, or disclosure
9 // is subject to the terms of the GNU General Public License, version 3.
10 // The program is distributed without any warranty, expressed or implied.
18 #include <sys/types.h>
19 #include <sys/socket.h>
27 #include <libcitadel.h>
29 #include "citadel_dirs.h"
34 void strip_trailing_nonprint(char *buf) {
35 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) ) {
36 buf[strlen(buf) - 1] = 0;
41 void timeout(int signum) {
46 int uds_connectsock(char *sockpath) {
48 struct sockaddr_un addr;
50 memset(&addr, 0, sizeof(addr));
51 addr.sun_family = AF_UNIX;
52 strcpy(addr.sun_path, sockpath);
54 s = socket(AF_UNIX, SOCK_STREAM, 0);
56 fprintf(stderr, "citmail: Can't create socket: %s\n",
61 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
62 fprintf(stderr, "citmail: can't connect: %s\n",
73 * input binary data from socket
75 void serv_read(char *buf, int bytes)
81 rlen = read(serv_sock, &buf[len], bytes - len);
91 * send binary to server
93 void serv_write(char *buf, int nbytes)
95 int bytes_written = 0;
97 while (bytes_written < nbytes) {
98 retval = write(serv_sock, &buf[bytes_written],
99 nbytes - bytes_written);
103 bytes_written = bytes_written + retval;
110 * input string from socket - implemented in terms of serv_read()
112 void serv_gets(char *buf)
116 /* Read one character at a time.
119 serv_read(&buf[i], 1);
120 if (buf[i] == '\n' || i == (SIZ-1))
124 /* If we got a long line, discard characters until the newline.
127 while (buf[i] != '\n')
128 serv_read(&buf[i], 1);
130 /* Strip all trailing nonprintables (crlf)
133 strip_trailing_nonprint(buf);
134 if (debug) fprintf(stderr, "> %s\n", buf);
139 * send line to server - implemented in terms of serv_write()
141 void serv_puts(char *buf)
143 if (debug) fprintf(stderr, "< %s\n", buf);
144 serv_write(buf, strlen(buf));
150 void cleanup(int exitcode) {
154 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
155 fprintf(stderr, "Please check your Citadel configuration.\n");
164 int main(int argc, char **argv) {
172 char ctdldir[PATH_MAX]=CTDLDIR;
175 char **recipients = NULL;
176 int num_recipients = 0;
178 int read_recipients_from_headers = 0;
179 char *add_these_recipients = NULL;
181 for (i=1; i<argc; ++i) {
182 if (!strcmp(argv[i], "-d")) {
185 else if (!strcmp(argv[i], "-t")) {
186 read_recipients_from_headers = 1;
188 else if (!strncmp(argv[i], "-h", 2)) {
189 safestrncpy(ctdldir, &argv[i][2], sizeof ctdldir);
191 else if (argv[i][0] != '-') {
193 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
194 recipients[num_recipients - 1] = strdup(argv[i]);
198 if (chdir(ctdldir) != 0) {
199 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
203 pw = getpwuid(getuid());
206 if (fp == NULL) return(errno);
207 serv_sock = uds_connectsock(file_lmtp_socket);
210 fprintf(stderr, "%s\n", &buf[4]);
211 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
215 sp = strchr (buf, ' ');
217 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
221 ep = strchr (sp, ' ');
223 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
229 strncpy(hostname, sp, sizeof hostname);
231 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
232 while (fgets(buf, 1024, stdin) != NULL) {
233 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
235 if (from_header == 0) {
236 fprintf(fp, "%s%s", fromline, buf);
239 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
240 strcpy(fromline, buf);
244 if (read_recipients_from_headers) {
245 add_these_recipients = NULL;
246 if ((isspace(buf[0])) && (to_or_cc)) {
247 add_these_recipients = buf;
250 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
257 add_these_recipients = &buf[3];
261 if (add_these_recipients) {
262 int num_recp_on_this_line;
265 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
266 for (i=0; i<num_recp_on_this_line; ++i) {
267 extract_token(this_recp, add_these_recipients,
268 i, ',', sizeof this_recp);
270 if (!IsEmptyStr(this_recp)) {
272 recipients = realloc(recipients,
273 (num_recipients * sizeof (char *)));
274 recipients[num_recipients - 1] = strdup(this_recp);
280 fprintf(fp, "%s", buf);
282 strip_trailing_nonprint(fromline);
284 sprintf(buf, "LHLO %s", hostname);
289 } while (buf[3] == '-');
291 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
295 snprintf(buf, sizeof buf, "MAIL %s", fromline);
299 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
303 for (i=0; i<num_recipients; ++i) {
304 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
314 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
319 while (fgets(buf, sizeof buf, fp) != NULL) {
320 strip_trailing_nonprint(buf);
326 fprintf(stderr, "%s\n", &buf[4]);
333 /* We won't actually reach this statement but the compiler will
334 * display a spurious warning about an invalid return type if
335 * we don't return an int.