4 * This program attempts to act like a local MDA if you're using sendmail or
5 * some other non-Citadel MTA. It basically just contacts the Citadel LMTP
6 * listener on a unix domain socket and transmits the message.
16 #include <sys/types.h>
17 #include <sys/socket.h>
30 #include "citadel_dirs.h"
35 void strip_trailing_nonprint(char *buf)
37 while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) )
38 buf[strlen(buf) - 1] = 0;
42 void timeout(int signum)
48 int uds_connectsock(char *sockpath)
51 struct sockaddr_un addr;
53 memset(&addr, 0, sizeof(addr));
54 addr.sun_family = AF_UNIX;
55 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
57 s = socket(AF_UNIX, SOCK_STREAM, 0);
59 fprintf(stderr, "Can't create socket: %s\n",
64 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
65 fprintf(stderr, "can't connect: %s\n",
76 * input binary data from socket
78 void serv_read(char *buf, int bytes)
84 rlen = read(serv_sock, &buf[len], bytes - len);
94 * send binary to server
96 void serv_write(char *buf, int nbytes)
98 int bytes_written = 0;
100 while (bytes_written < nbytes) {
101 retval = write(serv_sock, &buf[bytes_written],
102 nbytes - bytes_written);
106 bytes_written = bytes_written + retval;
113 * input string from socket - implemented in terms of serv_read()
115 void serv_gets(char *buf)
119 /* Read one character at a time.
122 serv_read(&buf[i], 1);
123 if (buf[i] == '\n' || i == (SIZ-1))
127 /* If we got a long line, discard characters until the newline.
130 while (buf[i] != '\n')
131 serv_read(&buf[i], 1);
133 /* Strip all trailing nonprintables (crlf)
136 strip_trailing_nonprint(buf);
137 if (debug) fprintf(stderr, "> %s\n", buf);
142 * send line to server - implemented in terms of serv_write()
144 void serv_puts(char *buf)
146 if (debug) fprintf(stderr, "< %s\n", buf);
147 serv_write(buf, strlen(buf));
153 void cleanup(int exitcode) {
157 fprintf(stderr, "Error #%d occurred while sending mail.\n", exitcode);
158 fprintf(stderr, "Please check your Citadel configuration.\n");
167 int main(int argc, char **argv) {
177 char relhome[PATH_MAX]="";
178 char ctdldir[PATH_MAX]=CTDLDIR;
181 char **recipients = NULL;
182 int num_recipients = 0;
184 int read_recipients_from_headers = 0;
185 char *add_these_recipients = NULL;
187 CtdlInitBase64Table();
189 for (i=1; i<argc; ++i) {
190 if (!strcmp(argv[i], "-d")) {
193 else if (!strcmp(argv[i], "-t")) {
194 read_recipients_from_headers = 1;
196 else if (argv[i][0] != '-') {
198 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
199 recipients[num_recipients - 1] = strdup(argv[i]);
203 /* TODO: should we be able to calculate relative dirs? */
204 calc_dirs_n_files(relh, home, relhome, ctdldir);
206 pw = getpwuid(getuid());
209 if (fp == NULL) return(errno);
210 serv_sock = uds_connectsock(file_lmtp_socket); /* FIXME: if called as 'sendmail' connect to file_lmtp_unfiltered_socket */
213 fprintf(stderr, "%s\n", &buf[4]);
214 if (debug) fprintf(stderr, "Could not connect to LMTP socket.\n");
218 sp = strchr (buf, ' ');
220 if (debug) fprintf(stderr, "Could not calculate hostname.\n");
224 ep = strchr (sp, ' ');
225 if (ep == NULL) cleanup(3);
227 strncpy(hostname, sp, sizeof hostname);
229 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
230 while (fgets(buf, 1024, stdin) != NULL) {
231 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
233 if (from_header == 0) {
234 fprintf(fp, "%s%s", fromline, buf);
237 if (!strncasecmp(buf, "From:", 5)) {
238 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] == '-');
290 if (buf[0] != '2') cleanup(4);
292 snprintf(buf, sizeof buf, "MAIL %s", fromline);
295 if (buf[0] != '2') cleanup(5);
297 for (i=0; i<num_recipients; ++i) {
298 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
307 if (buf[0]!='3') cleanup(6);
310 while (fgets(buf, sizeof buf, fp) != NULL) {
311 strip_trailing_nonprint(buf);
317 fprintf(stderr, "%s\n", &buf[4]);
324 /* We won't actually reach this statement but the compiler will
325 * display a spurious warning about an invalid return type if
326 * we don't return an int.