* socket stuff
[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 int serv_sock;
34
35
36 void strip_trailing_nonprint(char *buf)
37 {
38         while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
39                 buf[strlen(buf) - 1] = 0;
40 }
41
42
43 void timeout(int signum)
44 {
45         exit(signum);
46 }
47
48
49 int uds_connectsock(char *sockpath)
50 {
51         int s;
52         struct sockaddr_un sun;
53
54         memset(&sun, 0, sizeof(sun));
55         sun.sun_family = AF_UNIX;
56         strncpy(sun.sun_path, sockpath, sizeof sun.sun_path);
57
58         s = socket(AF_UNIX, SOCK_STREAM, 0);
59         if (s < 0) {
60                 fprintf(stderr, "Can't create socket: %s\n",
61                         strerror(errno));
62                 exit(3);
63         }
64
65         if (connect(s, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
66                 fprintf(stderr, "can't connect: %s\n",
67                         strerror(errno));
68                 exit(3);
69         }
70
71         return s;
72 }
73
74
75 /*
76  * input binary data from socket
77  */
78 void serv_read(char *buf, int bytes)
79 {
80         int len, rlen;
81
82         len = 0;
83         while (len < bytes) {
84                 rlen = read(serv_sock, &buf[len], bytes - len);
85                 if (rlen < 1) {
86                         return;
87                 }
88                 len = len + rlen;
89         }
90 }
91
92
93 /*
94  * send binary to server
95  */
96 void serv_write(char *buf, int nbytes)
97 {
98         int bytes_written = 0;
99         int retval;
100         while (bytes_written < nbytes) {
101                 retval = write(serv_sock, &buf[bytes_written],
102                                nbytes - bytes_written);
103                 if (retval < 1) {
104                         return;
105                 }
106                 bytes_written = bytes_written + retval;
107         }
108 }
109
110
111
112 /*
113  * input string from socket - implemented in terms of serv_read()
114  */
115 void serv_gets(char *buf)
116 {
117         int i;
118
119         /* Read one character at a time.
120          */
121         for (i = 0;; i++) {
122                 serv_read(&buf[i], 1);
123                 if (buf[i] == '\n' || i == 255)
124                         break;
125         }
126
127         /* If we got a long line, discard characters until the newline.
128          */
129         if (i == 255)
130                 while (buf[i] != '\n')
131                         serv_read(&buf[i], 1);
132
133         /* Strip all trailing nonprintables (crlf)
134          */
135         buf[i] = 0;
136         strip_trailing_nonprint(buf);
137 }
138
139
140 /*
141  * send line to server - implemented in terms of serv_write()
142  */
143 void serv_puts(char *buf)
144 {
145         /* printf("< %s\n", buf); */
146         serv_write(buf, strlen(buf));
147         serv_write("\n", 1);
148 }
149
150
151
152
153
154 void cleanup(int exitcode) {
155         char buf[1024];
156
157         serv_puts("QUIT");
158         serv_gets(buf);
159         fprintf(stderr, "%s\n", buf);
160         exit(exitcode);
161 }
162
163
164
165 int main(int argc, char **argv) {
166         char buf[1024];
167         char fromline[1024];
168         FILE *fp;
169
170         fp = tmpfile();
171         if (fp == NULL) return(errno);
172         sprintf(fromline, "From: someone@somewhere.org");
173         while (fgets(buf, 1024, stdin) != NULL) {
174                 fprintf(fp, "%s", buf);
175                 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
176         }
177         strip_trailing_nonprint(fromline);
178
179         sprintf(buf, "%d", SMTP_PORT);
180         serv_sock = uds_connectsock("smtp.socket");
181         serv_gets(buf);
182         fprintf(stderr, "%s\n", buf);
183         if (buf[0]!='2') cleanup(1);
184
185         serv_puts("HELO localhost");
186         serv_gets(buf);
187         fprintf(stderr, "%s\n", buf);
188         if (buf[0]!='2') cleanup(1);
189
190         sprintf(buf, "MAIL %s", fromline);
191         serv_puts(buf);
192         serv_gets(buf);
193         fprintf(stderr, "%s\n", buf);
194         if (buf[0]!='2') cleanup(1);
195
196         sprintf(buf, "RCPT To: %s", argv[1]);
197         serv_puts(buf);
198         serv_gets(buf);
199         fprintf(stderr, "%s\n", buf);
200         if (buf[0]!='2') cleanup(1);
201
202         serv_puts("DATA");
203         serv_gets(buf);
204         fprintf(stderr, "%s\n", 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         fprintf(stderr, "%s\n", buf);
215         if (buf[0]!='2') cleanup(1);
216         else cleanup(0);
217         return(0);
218 }