* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / citmail.c
1 /*
2  * $Id$
3  *
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 SMTP
6  * listener on a unix domain socket and transmits the message.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <limits.h>
25 #include "citadel.h"
26 #include "citadel_decls.h"
27 #include "ipc.h"
28 #ifndef HAVE_SNPRINTF
29 #include "snprintf.h"
30 #endif
31 #include "config.h"
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 addr;
55
56         memset(&addr, 0, sizeof(addr));
57         addr.sun_family = AF_UNIX;
58         strncpy(addr.sun_path, sockpath, sizeof addr.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 *) &addr, sizeof(addr)) < 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         get_config();
177
178         fp = tmpfile();
179         if (fp == NULL) return(errno);
180         sprintf(fromline, "From: someone@somewhere.org");
181         while (fgets(buf, 1024, stdin) != NULL) {
182                 fprintf(fp, "%s", buf);
183                 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
184         }
185         strip_trailing_nonprint(fromline);
186
187         serv_sock = uds_connectsock("smtp.socket");
188         serv_gets(buf);
189         if (buf[0]!='2') cleanup(1);
190
191         serv_puts("HELO localhost");
192         serv_gets(buf);
193         if (buf[0]!='2') cleanup(1);
194
195         sprintf(buf, "MAIL %s", fromline);
196         serv_puts(buf);
197         serv_gets(buf);
198         if (buf[0]!='2') cleanup(1);
199
200         sprintf(buf, "RCPT To: %s", argv[1]);
201         serv_puts(buf);
202         serv_gets(buf);
203         if (buf[0]!='2') cleanup(1);
204
205         serv_puts("DATA");
206         serv_gets(buf);
207         if (buf[0]!='3') cleanup(1);
208
209         rewind(fp);
210         while (fgets(buf, sizeof buf, fp) != NULL) {
211                 strip_trailing_nonprint(buf);
212                 serv_puts(buf);
213         }
214         serv_puts(".");
215         serv_gets(buf);
216         if (buf[0]!='2') cleanup(1);
217         else cleanup(0);
218         return(0);
219 }