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>
29 #include "citadel_dirs.h"
34 void strip_trailing_nonprint(char *buf)
36 while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
37 buf[strlen(buf) - 1] = 0;
41 void timeout(int signum)
47 int uds_connectsock(char *sockpath)
50 struct sockaddr_un addr;
52 memset(&addr, 0, sizeof(addr));
53 addr.sun_family = AF_UNIX;
54 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
56 s = socket(AF_UNIX, SOCK_STREAM, 0);
58 fprintf(stderr, "Can't create socket: %s\n",
63 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
64 fprintf(stderr, "can't connect: %s\n",
75 * input binary data from socket
77 void serv_read(char *buf, int bytes)
83 rlen = read(serv_sock, &buf[len], bytes - len);
93 * send binary to server
95 void serv_write(char *buf, int nbytes)
97 int bytes_written = 0;
99 while (bytes_written < nbytes) {
100 retval = write(serv_sock, &buf[bytes_written],
101 nbytes - bytes_written);
105 bytes_written = bytes_written + retval;
112 * input string from socket - implemented in terms of serv_read()
114 void serv_gets(char *buf)
118 /* Read one character at a time.
121 serv_read(&buf[i], 1);
122 if (buf[i] == '\n' || i == (SIZ-1))
126 /* If we got a long line, discard characters until the newline.
129 while (buf[i] != '\n')
130 serv_read(&buf[i], 1);
132 /* Strip all trailing nonprintables (crlf)
135 strip_trailing_nonprint(buf);
136 if (debug) fprintf(stderr, "> %s\n", buf);
141 * send line to server - implemented in terms of serv_write()
143 void serv_puts(char *buf)
145 if (debug) fprintf(stderr, "< %s\n", buf);
146 serv_write(buf, strlen(buf));
152 void cleanup(int exitcode) {
156 fprintf(stderr, "Error while sending mail. Please check your Citadel configuration.\n");
165 int main(int argc, char **argv) {
175 char relhome[PATH_MAX]="";
176 char ctdldir[PATH_MAX]=CTDLDIR;
179 char **recipients = NULL;
180 int num_recipients = 0;
182 int read_recipients_from_headers = 0;
183 char *add_these_recipients = NULL;
185 for (i=1; i<argc; ++i) {
186 if (!strcmp(argv[i], "-d")) {
189 else if (!strcmp(argv[i], "-t")) {
190 read_recipients_from_headers = 1;
192 else if (argv[i][0] != '-') {
194 recipients = realloc(recipients, (num_recipients * sizeof (char *)));
195 recipients[num_recipients - 1] = strdup(argv[i]);
199 /* TODO: should we be able to calculate relative dirs? */
200 calc_dirs_n_files(relh, home, relhome, ctdldir);
202 pw = getpwuid(getuid());
205 if (fp == NULL) return(errno);
206 serv_sock = uds_connectsock(file_lmtp_socket); /* FIXME: if called as 'sendmail' connect to file_lmtp_unfiltered_socket */
209 if (debug) fprintf(stderr, "Could not connect to LMTP socket.\n");
213 sp = strchr (buf, ' ');
215 if (debug) fprintf(stderr, "Could not calculate hostname.\n");
219 ep = strchr (sp, ' ');
220 if (ep == NULL) cleanup(1);
222 strncpy(hostname, sp, sizeof hostname);
224 snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
225 while (fgets(buf, 1024, stdin) != NULL) {
226 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
228 if (from_header == 0) {
229 fprintf(fp, "%s%s", fromline, buf);
232 if (!strncasecmp(buf, "From:", 5)) {
233 strcpy(fromline, buf);
239 if (read_recipients_from_headers) {
240 add_these_recipients = NULL;
241 if ((isspace(buf[0])) && (to_or_cc)) {
242 add_these_recipients = buf;
245 if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
252 add_these_recipients = &buf[3];
256 if (add_these_recipients) {
257 /* FIXME do something with them */
261 fprintf(fp, "%s", buf);
263 strip_trailing_nonprint(fromline);
265 sprintf(buf, "LHLO %s", hostname);
270 } while (buf[3] == '-');
271 if (buf[0] != '2') cleanup(1);
273 snprintf(buf, sizeof buf, "MAIL %s", fromline);
276 if (buf[0] != '2') cleanup(1);
278 for (i=0; i<num_recipients; ++i) {
279 snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
288 if (buf[0]!='3') cleanup(1);
291 while (fgets(buf, sizeof buf, fp) != NULL) {
292 strip_trailing_nonprint(buf);
304 /* We won't actually reach this statement but the compiler will
305 * display a spurious warning about an invalid return type if
306 * we don't return an int.