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-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.
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",
72 // input binary data from socket
73 void serv_read(char *buf, int bytes) {
78 rlen = read(serv_sock, &buf[len], bytes - len);
87 // send binary to server
88 void serv_write(char *buf, int nbytes) {
89 int bytes_written = 0;
91 while (bytes_written < nbytes) {
92 retval = write(serv_sock, &buf[bytes_written],
93 nbytes - bytes_written);
97 bytes_written = bytes_written + retval;
102 // input string from socket - implemented in terms of serv_read()
103 void serv_gets(char *buf) {
106 // Read one character at a time.
108 serv_read(&buf[i], 1);
109 if (buf[i] == '\n' || i == (SIZ-1))
113 // If we got a long line, discard characters until the newline.
115 while (buf[i] != '\n')
116 serv_read(&buf[i], 1);
118 // Strip all trailing nonprintables (crlf)
120 strip_trailing_nonprint(buf);
121 if (debug) fprintf(stderr, "> %s\n", buf);
125 // send line to server - implemented in terms of serv_write()
126 void serv_puts(char *buf) {
127 if (debug) fprintf(stderr, "< %s\n", buf);
128 serv_write(buf, strlen(buf));
133 void cleanup(int exitcode) {
137 fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
138 fprintf(stderr, "Please check your Citadel configuration.\n");
146 int main(int argc, char **argv) {
154 char ctdldir[PATH_MAX]=CTDLDIR;
157 char **recipients = NULL;
158 int num_recipients = 0;
160 int read_recipients_from_headers = 0;
161 char *add_these_recipients = NULL;
163 for (i=1; i<argc; ++i) {
164 if (!strcmp(argv[i], "-d")) {
167 else if (!strcmp(argv[i], "-t")) {
168 read_recipients_from_headers = 1;
170 else if (!strncmp(argv[i], "-h", 2)) {
171 safestrncpy(ctdldir, &argv[i][2], sizeof ctdldir);
173 else if (argv[i][0] != '-') {
175 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
176 recipients[num_recipients - 1] = strdup(argv[i]);
180 if (chdir(ctdldir) != 0) {
181 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
185 pw = getpwuid(getuid());
188 if (fp == NULL) return(errno);
189 serv_sock = uds_connectsock(file_lmtp_socket);
192 fprintf(stderr, "%s\n", &buf[4]);
193 if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
197 sp = strchr (buf, ' ');
199 if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
203 ep = strchr (sp, ' ');
205 if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
211 strncpy(hostname, sp, sizeof hostname);
213 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
214 while (fgets(buf, 1024, stdin) != NULL) {
215 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
217 if (from_header == 0) {
218 fprintf(fp, "%s%s", fromline, buf);
221 if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
222 strcpy(fromline, buf);
226 if (read_recipients_from_headers) {
227 add_these_recipients = NULL;
228 if ((isspace(buf[0])) && (to_or_cc)) {
229 add_these_recipients = buf;
232 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
239 add_these_recipients = &buf[3];
243 if (add_these_recipients) {
244 int num_recp_on_this_line;
247 num_recp_on_this_line = num_tokens(add_these_recipients, ',');
248 for (i=0; i<num_recp_on_this_line; ++i) {
249 extract_token(this_recp, add_these_recipients,
250 i, ',', sizeof this_recp);
252 if (!IsEmptyStr(this_recp)) {
254 recipients = realloc(recipients,
255 (num_recipients * sizeof (char *)));
256 recipients[num_recipients - 1] = strdup(this_recp);
262 fprintf(fp, "%s", buf);
264 strip_trailing_nonprint(fromline);
266 sprintf(buf, "LHLO %s", hostname);
271 } while (buf[3] == '-');
273 if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
277 snprintf(buf, sizeof buf, "MAIL %s", fromline);
281 if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
285 for (i=0; i<num_recipients; ++i) {
286 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
296 if (debug) fprintf(stderr, "citmail: DATA command failed\n");
301 while (fgets(buf, sizeof buf, fp) != NULL) {
302 strip_trailing_nonprint(buf);
308 fprintf(stderr, "%s\n", &buf[4]);
315 // We won't actually reach this statement but the compiler will
316 // display a spurious warning about an invalid return type if
317 // we don't return an int.