* Shut off hostname resolution when dealing with Unix domain sockets
[citadel.git] / citadel / citmail.c
1 /*
2  * 
3  * Completely reworked version of "citmail"
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 forwards the message to
6  * the Citadel SMTP listener on some non-standard port.
7  *
8  * $Id$
9  *
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include "citadel.h"
27 #include "citadel_decls.h"
28 #include "ipc.h"
29 #ifndef HAVE_SNPRINTF
30 #include "snprintf.h"
31 #endif
32
33 /* #define DEBUG  */    /* uncomment to get protocol traces */
34
35 int serv_sock;
36
37
38 void strip_trailing_nonprint(char *buf)
39 {
40         while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
41                 buf[strlen(buf) - 1] = 0;
42 }
43
44
45 void timeout(int signum)
46 {
47         exit(signum);
48 }
49
50
51 int uds_connectsock(char *sockpath)
52 {
53         int s;
54         struct sockaddr_un sun;
55
56         memset(&sun, 0, sizeof(sun));
57         sun.sun_family = AF_UNIX;
58         strncpy(sun.sun_path, sockpath, sizeof sun.sun_path);
59
60         s = socket(AF_UNIX, SOCK_STREAM, 0);
61         if (s < 0) {
62                 fprintf(stderr, "Can't create socket: %s\n",
63                         strerror(errno));
64                 exit(3);
65         }
66
67         if (connect(s, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
68                 fprintf(stderr, "can't connect: %s\n",
69                         strerror(errno));
70                 exit(3);
71         }
72
73         return s;
74 }
75
76
77 /*
78  * input binary data from socket
79  */
80 void serv_read(char *buf, int bytes)
81 {
82         int len, rlen;
83
84         len = 0;
85         while (len < bytes) {
86                 rlen = read(serv_sock, &buf[len], bytes - len);
87                 if (rlen < 1) {
88                         return;
89                 }
90                 len = len + rlen;
91         }
92 }
93
94
95 /*
96  * send binary to server
97  */
98 void serv_write(char *buf, int nbytes)
99 {
100         int bytes_written = 0;
101         int retval;
102         while (bytes_written < nbytes) {
103                 retval = write(serv_sock, &buf[bytes_written],
104                                nbytes - bytes_written);
105                 if (retval < 1) {
106                         return;
107                 }
108                 bytes_written = bytes_written + retval;
109         }
110 }
111
112
113
114 /*
115  * input string from socket - implemented in terms of serv_read()
116  */
117 void serv_gets(char *buf)
118 {
119         int i;
120
121         /* Read one character at a time.
122          */
123         for (i = 0;; i++) {
124                 serv_read(&buf[i], 1);
125                 if (buf[i] == '\n' || i == 255)
126                         break;
127         }
128
129         /* If we got a long line, discard characters until the newline.
130          */
131         if (i == 255)
132                 while (buf[i] != '\n')
133                         serv_read(&buf[i], 1);
134
135         /* Strip all trailing nonprintables (crlf)
136          */
137         buf[i] = 0;
138         strip_trailing_nonprint(buf);
139 #ifdef DEBUG
140         printf("> %s\n", buf);
141 #endif
142 }
143
144
145 /*
146  * send line to server - implemented in terms of serv_write()
147  */
148 void serv_puts(char *buf)
149 {
150 #ifdef DEBUG
151         printf("< %s\n", buf);
152 #endif
153         serv_write(buf, strlen(buf));
154         serv_write("\n", 1);
155 }
156
157
158
159
160
161 void cleanup(int exitcode) {
162         char buf[1024];
163
164         serv_puts("QUIT");
165         serv_gets(buf);
166         exit(exitcode);
167 }
168
169
170
171 int main(int argc, char **argv) {
172         char buf[1024];
173         char fromline[1024];
174         FILE *fp;
175
176         fp = tmpfile();
177         if (fp == NULL) return(errno);
178         sprintf(fromline, "From: someone@somewhere.org");
179         while (fgets(buf, 1024, stdin) != NULL) {
180                 fprintf(fp, "%s", buf);
181                 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
182         }
183         strip_trailing_nonprint(fromline);
184
185         serv_sock = uds_connectsock("smtp.socket");
186         serv_gets(buf);
187         if (buf[0]!='2') cleanup(1);
188
189         serv_puts("HELO localhost");
190         serv_gets(buf);
191         if (buf[0]!='2') cleanup(1);
192
193         sprintf(buf, "MAIL %s", fromline);
194         serv_puts(buf);
195         serv_gets(buf);
196         if (buf[0]!='2') cleanup(1);
197
198         sprintf(buf, "RCPT To: %s", argv[1]);
199         serv_puts(buf);
200         serv_gets(buf);
201         if (buf[0]!='2') cleanup(1);
202
203         serv_puts("DATA");
204         serv_gets(buf);
205         if (buf[0]!='3') cleanup(1);
206
207         rewind(fp);
208         while (fgets(buf, sizeof buf, fp) != NULL) {
209                 strip_trailing_nonprint(buf);
210                 serv_puts(buf);
211         }
212         serv_puts(".");
213         serv_gets(buf);
214         if (buf[0]!='2') cleanup(1);
215         else cleanup(0);
216         return(0);
217 }