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-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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
32 #include <libcitadel.h>
34 #include "citadel_dirs.h"
39 void strip_trailing_nonprint(char *buf)
41 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) )
42 buf[strlen(buf) - 1] = 0;
46 void timeout(int signum)
52 int uds_connectsock(char *sockpath)
55 struct sockaddr_un addr;
57 memset(&addr, 0, sizeof(addr));
58 addr.sun_family = AF_UNIX;
59 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
61 s = socket(AF_UNIX, SOCK_STREAM, 0);
63 fprintf(stderr, "citmail: Can't create socket: %s\n",
68 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
69 fprintf(stderr, "citmail: can't connect: %s\n",
80 * input binary data from socket
82 void serv_read(char *buf, int bytes)
88 rlen = read(serv_sock, &buf[len], bytes - len);
98 * send binary to server
100 void serv_write(char *buf, int nbytes)
102 int bytes_written = 0;
104 while (bytes_written < nbytes) {
105 retval = write(serv_sock, &buf[bytes_written],
106 nbytes - bytes_written);
110 bytes_written = bytes_written + retval;
117 * input string from socket - implemented in terms of serv_read()
119 void serv_gets(char *buf)
123 /* Read one character at a time.
126 serv_read(&buf[i], 1);
127 if (buf[i] == '\n' || i == (SIZ-1))
131 /* If we got a long line, discard characters until the newline.
134 while (buf[i] != '\n')
135 serv_read(&buf[i], 1);
137 /* Strip all trailing nonprintables (crlf)
140 strip_trailing_nonprint(buf);
141 if (debug) fprintf(stderr, "> %s\n", buf);
146 * send line to server - implemented in terms of serv_write()
148 void serv_puts(char *buf)
150 if (debug) fprintf(stderr, "< %s\n", buf);
151 serv_write(buf, strlen(buf));
157 void cleanup(int exitcode) {
161 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
162 fprintf(stderr, "Please check your Citadel configuration.\n");
171 int main(int argc, char **argv) {
179 char ctdldir[PATH_MAX]=CTDLDIR;
182 char **recipients = NULL;
183 int num_recipients = 0;
185 int read_recipients_from_headers = 0;
186 char *add_these_recipients = NULL;
188 for (i=1; i<argc; ++i) {
189 if (!strcmp(argv[i], "-d")) {
192 else if (!strcmp(argv[i], "-t")) {
193 read_recipients_from_headers = 1;
195 else if (!strncmp(argv[i], "-h", 2)) {
196 safestrncpy(ctdldir, &argv[i][2], sizeof ctdldir);
198 else if (argv[i][0] != '-') {
200 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
201 recipients[num_recipients - 1] = strdup(argv[i]);
205 if (chdir(ctdldir) != 0) {
206 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
210 pw = getpwuid(getuid());
213 if (fp == NULL) return(errno);
214 serv_sock = uds_connectsock(file_lmtp_socket);
217 fprintf(stderr, "%s\n", &buf[4]);
218 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
222 sp = strchr (buf, ' ');
224 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
228 ep = strchr (sp, ' ');
230 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
236 strncpy(hostname, sp, sizeof hostname);
238 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
239 while (fgets(buf, 1024, stdin) != NULL) {
240 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
242 if (from_header == 0) {
243 fprintf(fp, "%s%s", fromline, buf);
246 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
247 strcpy(fromline, buf);
251 if (read_recipients_from_headers) {
252 add_these_recipients = NULL;
253 if ((isspace(buf[0])) && (to_or_cc)) {
254 add_these_recipients = buf;
257 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
264 add_these_recipients = &buf[3];
268 if (add_these_recipients) {
269 int num_recp_on_this_line;
272 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
273 for (i=0; i<num_recp_on_this_line; ++i) {
274 extract_token(this_recp, add_these_recipients,
275 i, ',', sizeof this_recp);
277 if (!IsEmptyStr(this_recp)) {
279 recipients = realloc(recipients,
280 (num_recipients * sizeof (char *)));
281 recipients[num_recipients - 1] = strdup(this_recp);
287 fprintf(fp, "%s", buf);
289 strip_trailing_nonprint(fromline);
291 sprintf(buf, "LHLO %s", hostname);
296 } while (buf[3] == '-');
298 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
302 snprintf(buf, sizeof buf, "MAIL %s", fromline);
306 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
310 for (i=0; i<num_recipients; ++i) {
311 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
321 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
326 while (fgets(buf, sizeof buf, fp) != NULL) {
327 strip_trailing_nonprint(buf);
333 fprintf(stderr, "%s\n", &buf[4]);
340 /* We won't actually reach this statement but the compiler will
341 * display a spurious warning about an invalid return type if
342 * we don't return an int.