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>
31 /* #define DEBUG */ /* uncomment to get protocol traces */
36 void strip_trailing_nonprint(char *buf)
38 while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
39 buf[strlen(buf) - 1] = 0;
43 void timeout(int signum)
49 int uds_connectsock(char *sockpath)
52 struct sockaddr_un addr;
54 memset(&addr, 0, sizeof(addr));
55 addr.sun_family = AF_UNIX;
56 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
58 s = socket(AF_UNIX, SOCK_STREAM, 0);
60 fprintf(stderr, "Can't create socket: %s\n",
65 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
66 fprintf(stderr, "can't connect: %s\n",
77 * input binary data from socket
79 void serv_read(char *buf, int bytes)
85 rlen = read(serv_sock, &buf[len], bytes - len);
95 * send binary to server
97 void serv_write(char *buf, int nbytes)
99 int bytes_written = 0;
101 while (bytes_written < nbytes) {
102 retval = write(serv_sock, &buf[bytes_written],
103 nbytes - bytes_written);
107 bytes_written = bytes_written + retval;
114 * input string from socket - implemented in terms of serv_read()
116 void serv_gets(char *buf)
120 /* Read one character at a time.
123 serv_read(&buf[i], 1);
124 if (buf[i] == '\n' || i == (SIZ-1))
128 /* If we got a long line, discard characters until the newline.
131 while (buf[i] != '\n')
132 serv_read(&buf[i], 1);
134 /* Strip all trailing nonprintables (crlf)
137 strip_trailing_nonprint(buf);
139 printf("> %s\n", buf);
145 * send line to server - implemented in terms of serv_write()
147 void serv_puts(char *buf)
150 printf("< %s\n", buf);
152 serv_write(buf, strlen(buf));
160 void cleanup(int exitcode) {
170 int main(int argc, char **argv) {
181 pw = getpwuid(getuid());
184 if (fp == NULL) return(errno);
185 snprintf(fromline, sizeof fromline, "From: %s@%s",
189 while (fgets(buf, 1024, stdin) != NULL) {
190 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
192 if (from_header == 0) {
193 fprintf(fp, "%s%s", fromline, buf);
196 if (!strncasecmp(buf, "From:", 5)) {
197 strcpy(fromline, buf);
202 fprintf(fp, "%s", buf);
204 strip_trailing_nonprint(fromline);
206 serv_sock = uds_connectsock(
214 if (buf[0]!='2') cleanup(1);
220 } while (buf[3] == '-');
221 if (buf[0] != '2') cleanup(1);
223 snprintf(buf, sizeof buf, "MAIL %s", fromline);
226 if (buf[0]!='2') cleanup(1);
228 for (i=1; i<argc; ++i) {
229 if (argv[i][0] != '-') {
230 snprintf(buf, sizeof buf, "RCPT To: %s", argv[i]);
233 /* if (buf[0]!='2') cleanup(1); */
239 if (buf[0]!='3') cleanup(1);
242 while (fgets(buf, sizeof buf, fp) != NULL) {
243 strip_trailing_nonprint(buf);
248 if (buf[0]!='2') cleanup(1);