* Add Unix domain socket support to citmail.c
[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 <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <sys/un.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include "citadel.h"
29 #include "citadel_decls.h"
30 #include "ipc.h"
31 #ifndef HAVE_SNPRINTF
32 #include "snprintf.h"
33 #endif
34
35 #ifndef INADDR_NONE
36 #define INADDR_NONE 0xffffffff
37 #endif
38
39 int serv_sock;
40
41
42 void strip_trailing_nonprint(char *buf)
43 {
44         while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
45                 buf[strlen(buf) - 1] = 0;
46 }
47
48
49
50
51
52
53 void timeout(int signum)
54 {
55         exit(signum);
56 }
57
58
59 int connectsock(char *host, char *service, char *protocol)
60 {
61         struct hostent *phe;
62         struct servent *pse;
63         struct protoent *ppe;
64         struct sockaddr_in sin;
65         int s, type;
66         struct sockaddr_un sun;
67
68         if ( (!strcmp(protocol, "unix")) || (atoi(service)<0) ) {
69                 memset(&sun, 0, sizeof(sun));
70                 sun.sun_family = AF_UNIX;
71                 sprintf(sun.sun_path, USOCKPATH, 0-atoi(service) );
72
73                 s = socket(AF_UNIX, SOCK_STREAM, 0);
74                 if (s < 0) {
75                         fprintf(stderr, "Can't create socket: %s\n",
76                                 strerror(errno));
77                         exit(3);
78                 }
79
80                 if (connect(s, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
81                         fprintf(stderr, "can't connect: %s\n",
82                                 strerror(errno));
83                         exit(3);
84                 }
85
86                 return s;
87         }
88
89
90         /* otherwise it's a network connection */
91
92         memset(&sin, 0, sizeof(sin));
93         sin.sin_family = AF_INET;
94
95         pse = getservbyname(service, protocol);
96         if (pse) {
97                 sin.sin_port = pse->s_port;
98         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
99                 fprintf(stderr, "Can't get %s service entry: %s\n",
100                         service, strerror(errno));
101                 exit(3);
102         }
103         phe = gethostbyname(host);
104         if (phe) {
105                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
106         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
107                 fprintf(stderr, "Can't get %s host entry: %s\n",
108                         host, strerror(errno));
109                 exit(3);
110         }
111         if ((ppe = getprotobyname(protocol)) == 0) {
112                 fprintf(stderr, "Can't get %s protocol entry: %s\n",
113                         protocol, strerror(errno));
114                 exit(3);
115         }
116         if (!strcmp(protocol, "udp")) {
117                 type = SOCK_DGRAM;
118         } else {
119                 type = SOCK_STREAM;
120         }
121
122         s = socket(PF_INET, type, ppe->p_proto);
123         if (s < 0) {
124                 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
125                 exit(3);
126         }
127         signal(SIGALRM, timeout);
128         alarm(30);
129
130         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
131                 fprintf(stderr, "can't connect to %s.%s: %s\n",
132                         host, service, strerror(errno));
133                 exit(3);
134         }
135         alarm(0);
136         signal(SIGALRM, SIG_IGN);
137
138         return (s);
139 }
140
141 /*
142  * convert service and host entries into a six-byte numeric in the format
143  * expected by a SOCKS v4 server
144  */
145 void numericize(char *buf, char *host, char *service, char *protocol)
146 {
147         struct hostent *phe;
148         struct servent *pse;
149         struct sockaddr_in sin;
150
151         memset(&sin, 0, sizeof(sin));
152         sin.sin_family = AF_INET;
153
154         pse = getservbyname(service, protocol);
155         if (pse) {
156                 sin.sin_port = pse->s_port;
157         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
158                 fprintf(stderr, "Can't get %s service entry: %s\n",
159                         service, strerror(errno));
160                 exit(3);
161         }
162         buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
163         buf[0] = ((sin.sin_port) & 0x00FF);
164
165         phe = gethostbyname(host);
166         if (phe) {
167                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
168         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
169                 fprintf(stderr, "Can't get %s host entry: %s\n",
170                         host, strerror(errno));
171                 exit(3);
172         }
173         buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
174         buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
175         buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
176         buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
177 }
178
179 /*
180  * input binary data from socket
181  */
182 void serv_read(char *buf, int bytes)
183 {
184         int len, rlen;
185
186         len = 0;
187         while (len < bytes) {
188                 rlen = read(serv_sock, &buf[len], bytes - len);
189                 if (rlen < 1) {
190                         return;
191                 }
192                 len = len + rlen;
193         }
194 }
195
196
197 /*
198  * send binary to server
199  */
200 void serv_write(char *buf, int nbytes)
201 {
202         int bytes_written = 0;
203         int retval;
204         while (bytes_written < nbytes) {
205                 retval = write(serv_sock, &buf[bytes_written],
206                                nbytes - bytes_written);
207                 if (retval < 1) {
208                         return;
209                 }
210                 bytes_written = bytes_written + retval;
211         }
212 }
213
214
215
216 /*
217  * input string from socket - implemented in terms of serv_read()
218  */
219 void serv_gets(char *buf)
220 {
221         int i;
222
223         /* Read one character at a time.
224          */
225         for (i = 0;; i++) {
226                 serv_read(&buf[i], 1);
227                 if (buf[i] == '\n' || i == 255)
228                         break;
229         }
230
231         /* If we got a long line, discard characters until the newline.
232          */
233         if (i == 255)
234                 while (buf[i] != '\n')
235                         serv_read(&buf[i], 1);
236
237         /* Strip all trailing nonprintables (crlf)
238          */
239         buf[i] = 0;
240         strip_trailing_nonprint(buf);
241 }
242
243
244 /*
245  * send line to server - implemented in terms of serv_write()
246  */
247 void serv_puts(char *buf)
248 {
249         /* printf("< %s\n", buf); */
250         serv_write(buf, strlen(buf));
251         serv_write("\n", 1);
252 }
253
254
255
256
257
258 void cleanup(int exitcode) {
259         char buf[1024];
260
261         serv_puts("QUIT");
262         serv_gets(buf);
263         fprintf(stderr, "%s\n", buf);
264         exit(exitcode);
265 }
266
267
268
269 int main(int argc, char **argv) {
270         char buf[1024];
271         char fromline[1024];
272         FILE *fp;
273
274         fp = tmpfile();
275         if (fp == NULL) return(errno);
276         sprintf(fromline, "From: someone@somewhere.org");
277         while (fgets(buf, 1024, stdin) != NULL) {
278                 fprintf(fp, "%s", buf);
279                 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
280         }
281         strip_trailing_nonprint(fromline);
282
283         sprintf(buf, "%d", SMTP_PORT);
284         serv_sock = connectsock("localhost", buf, "tcp");
285         serv_gets(buf);
286         fprintf(stderr, "%s\n", buf);
287         if (buf[0]!='2') cleanup(1);
288
289         serv_puts("HELO localhost");
290         serv_gets(buf);
291         fprintf(stderr, "%s\n", buf);
292         if (buf[0]!='2') cleanup(1);
293
294         sprintf(buf, "MAIL %s", fromline);
295         serv_puts(buf);
296         serv_gets(buf);
297         fprintf(stderr, "%s\n", buf);
298         if (buf[0]!='2') cleanup(1);
299
300         sprintf(buf, "RCPT To: %s", argv[1]);
301         serv_puts(buf);
302         serv_gets(buf);
303         fprintf(stderr, "%s\n", buf);
304         if (buf[0]!='2') cleanup(1);
305
306         serv_puts("DATA");
307         serv_gets(buf);
308         fprintf(stderr, "%s\n", buf);
309         if (buf[0]!='3') cleanup(1);
310
311         rewind(fp);
312         while (fgets(buf, sizeof buf, fp) != NULL) {
313                 strip_trailing_nonprint(buf);
314                 serv_puts(buf);
315         }
316         serv_puts(".");
317         serv_gets(buf);
318         fprintf(stderr, "%s\n", buf);
319         if (buf[0]!='2') cleanup(1);
320         else cleanup(0);
321         return(0);
322 }