* don't use the config to get the hostname anymore, so we don't need to run suid...
[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 LMTP
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 #ifndef HAVE_SNPRINTF
27 #include "snprintf.h"
28 #endif
29 #include "citadel_dirs.h"
30
31 /* #define DEBUG  */    /* uncomment to get protocol traces */
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 addr;
53
54         memset(&addr, 0, sizeof(addr));
55         addr.sun_family = AF_UNIX;
56         strncpy(addr.sun_path, sockpath, sizeof addr.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 *) &addr, sizeof(addr)) < 0) {
66                 fprintf(stderr, "can't connect: %s\n",
67                         strerror(errno));
68                 close(s);
69                 exit(3);
70         }
71
72         return s;
73 }
74
75
76 /*
77  * input binary data from socket
78  */
79 void serv_read(char *buf, int bytes)
80 {
81         int len, rlen;
82
83         len = 0;
84         while (len < bytes) {
85                 rlen = read(serv_sock, &buf[len], bytes - len);
86                 if (rlen < 1) {
87                         return;
88                 }
89                 len = len + rlen;
90         }
91 }
92
93
94 /*
95  * send binary to server
96  */
97 void serv_write(char *buf, int nbytes)
98 {
99         int bytes_written = 0;
100         int retval;
101         while (bytes_written < nbytes) {
102                 retval = write(serv_sock, &buf[bytes_written],
103                                nbytes - bytes_written);
104                 if (retval < 1) {
105                         return;
106                 }
107                 bytes_written = bytes_written + retval;
108         }
109 }
110
111
112
113 /*
114  * input string from socket - implemented in terms of serv_read()
115  */
116 void serv_gets(char *buf)
117 {
118         int i;
119
120         /* Read one character at a time.
121          */
122         for (i = 0;; i++) {
123                 serv_read(&buf[i], 1);
124                 if (buf[i] == '\n' || i == (SIZ-1))
125                         break;
126         }
127
128         /* If we got a long line, discard characters until the newline.
129          */
130         if (i == (SIZ-1))
131                 while (buf[i] != '\n')
132                         serv_read(&buf[i], 1);
133
134         /* Strip all trailing nonprintables (crlf)
135          */
136         buf[i] = 0;
137         strip_trailing_nonprint(buf);
138 #ifdef DEBUG
139         printf("> %s\n", buf);
140 #endif
141 }
142
143
144 /*
145  * send line to server - implemented in terms of serv_write()
146  */
147 void serv_puts(char *buf)
148 {
149 #ifdef DEBUG
150         printf("< %s\n", buf);
151 #endif
152         serv_write(buf, strlen(buf));
153         serv_write("\n", 1);
154 }
155
156
157
158
159
160 void cleanup(int exitcode) {
161         char buf[1024];
162
163         if (exitcode == 1)
164                 printf ("Error while sending mail."
165                         "Check your maildata and make shure "
166                         "citadel is configured properly!");
167         serv_puts("QUIT");
168         serv_gets(buf);
169         exit(exitcode);
170 }
171
172
173
174 int main(int argc, char **argv) {
175         char buf[1024];
176         char fromline[1024];
177         FILE *fp;
178         int i;
179         struct passwd *pw;
180         int from_header = 0;
181         int in_body = 0;
182         int relh=0;
183         int home=0;
184         char relhome[PATH_MAX]="";
185         char ctdldir[PATH_MAX]=CTDLDIR;
186         char *sp, *ep;
187                
188
189         /* TODO: should we be able to calculate relative dirs? */
190         calc_dirs_n_files(relh, home, relhome, ctdldir);
191
192         pw = getpwuid(getuid());
193
194         fp = tmpfile();
195         if (fp == NULL) return(errno);
196         serv_sock = uds_connectsock(file_lmtp_socket);
197         serv_gets(buf);
198         if (buf[0]!='2') cleanup(1);
199
200         sp = strchr (buf, ' ');
201         if (sp == NULL) cleanup(1);
202         sp ++;
203         ep = strchr (sp, ' ');
204         if (ep == NULL) cleanup(1);
205         *ep = '\0';
206
207         snprintf(fromline, sizeof fromline, "From: %s@%s",
208                  pw->pw_name,
209                  sp
210         );
211         while (fgets(buf, 1024, stdin) != NULL) {
212                 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
213                         in_body = 1;
214                         if (from_header == 0) {
215                                 fprintf(fp, "%s%s", fromline, buf);
216                         }
217                 }
218                 if (!strncasecmp(buf, "From:", 5)) {
219                         strcpy(fromline, buf);
220                         if (in_body == 0) {
221                                 from_header = 1;
222                         }
223                 }
224                 fprintf(fp, "%s", buf);
225         }
226         strip_trailing_nonprint(fromline);
227
228
229         serv_puts("LHLO x");
230         do {
231                 serv_gets(buf);
232                 strcat(buf, "    ");
233         } while (buf[3] == '-');
234         if (buf[0] != '2') cleanup(1);
235
236         snprintf(buf, sizeof buf, "MAIL %s", fromline);
237         serv_puts(buf);
238         serv_gets(buf);
239         if (buf[0]!='2') cleanup(1);
240
241         for (i=1; i<argc; ++i) {
242                 if (argv[i][0] != '-') {
243                         snprintf(buf, sizeof buf, "RCPT To: %s", argv[i]);
244                         serv_puts(buf);
245                         serv_gets(buf);
246                         /* if (buf[0]!='2') cleanup(1); */
247                 }
248         }
249
250         serv_puts("DATA");
251         serv_gets(buf);
252         if (buf[0]!='3') cleanup(1);
253
254         rewind(fp);
255         while (fgets(buf, sizeof buf, fp) != NULL) {
256                 strip_trailing_nonprint(buf);
257                 serv_puts(buf);
258         }
259         serv_puts(".");
260         serv_gets(buf);
261         if (buf[0]!='2') cleanup(1);
262         else cleanup(0);
263         return(0);
264 }