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