further work on data parsing.
[apps/pfixtools.git] / postfix.c
1 /******************************************************************************/
2 /*          postlicyd: a postfix policy daemon with a lot of features         */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2006-2007 Pierre Habouzit
34  */
35
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdbool.h>
39 #include <syslog.h>
40 #include <unistd.h>
41
42 #include "job.h"
43 #include "postfix.h"
44 #include "buffer.h"
45 #include "tokens.h"
46
47 struct jpriv_t {
48     buffer_t ibuf;
49     buffer_t obuf;
50 };
51
52 static jpriv_t *postfix_jpriv_init(jpriv_t *jp)
53 {
54     buffer_init(&jp->ibuf);
55     buffer_init(&jp->obuf);
56     return jp;
57 }
58 static void postfix_jpriv_wipe(jpriv_t *jp)
59 {
60     buffer_wipe(&jp->ibuf);
61     buffer_wipe(&jp->obuf);
62 }
63 DO_NEW(jpriv_t, postfix_jpriv);
64 DO_DELETE(jpriv_t, postfix_jpriv);
65
66 static void postfix_stop(job_t *job)
67 {
68     postfix_jpriv_delete(&job->jdata);
69 }
70
71 static int postfix_parsejob(job_t *job)
72 {
73     const char *p = skipspaces(job->jdata->ibuf.data);
74
75     for (;;) {
76         const char *k, *v;
77         int klen, vlen;
78
79         while (*p == ' ' || *p == '\t')
80             p++;
81         p = strchrnul(k = p, '=');
82         if (!*p)
83             return -1;
84         for (klen = p - k; k[klen] == ' ' || k[klen] == '\t'; klen--);
85         p += 1; /* skip = */
86
87         while (*p == ' ' || *p == '\t')
88             p++;
89         p = strstr(v = p, "\r\n");
90         if (!p)
91             return -1;
92         for (vlen = p - v; v[vlen] == ' ' || v[vlen] == '\t'; vlen--);
93         p += 2; /* skip \r\n */
94
95         /* do sth with (k,v) */
96
97         assert (p[0] && p[1]);
98         if (p[0] == '\r' && p[1] == '\n')
99             break;
100     }
101
102     return -1;
103 }
104
105 static void postfix_process(job_t *job)
106 {
107     int nb;
108
109     switch (job->mode) {
110       case JOB_LISTEN:
111         if ((job = job_accept(job, JOB_READ))) {
112             job->jdata   = postfix_jpriv_new();
113             job->process = &postfix_process;
114             job->stop    = &postfix_stop;
115         }
116         return;
117
118       case JOB_WRITE:
119         nb = write(job->fd, job->jdata->obuf.data, job->jdata->obuf.len);
120         if (nb < 0) {
121             if ((job->error = errno != EINTR && errno != EAGAIN)) {
122                 syslog(LOG_ERR, "unexpected problem on the socket: %m");
123             }
124             return;
125         }
126
127         buffer_consume(&job->jdata->obuf, nb);
128         if (job->jdata->obuf.len)
129             return;
130
131         job_update_mode(job, JOB_READ);
132
133         /* fall through */
134
135       case JOB_READ:
136         nb = buffer_read(&job->jdata->ibuf, job->fd, -1);
137         if (nb < 0) {
138             if ((job->error = errno != EINTR && errno != EAGAIN)) {
139                 syslog(LOG_ERR, "unexpected problem on the socket: %m");
140             }
141             return;
142         }
143         if (nb == 0) {
144             syslog(LOG_ERR, "unexpected eof");
145             job->error = true;
146             return;
147         }
148
149         if (!strstr(skipspaces(job->jdata->ibuf.data), "\r\n\r\n")) {
150             if (job->jdata->ibuf.len > SHRT_MAX) {
151                 syslog(LOG_ERR, "too much data without CRLFCRLF");
152                 job->error = true;
153             }
154             return;
155         }
156
157         if (postfix_parsejob(job) < 0) {
158             syslog(LOG_ERR, "could not parse postfix request");
159             job->error = true;
160             return;
161         }
162
163         job_update_mode(job, JOB_IDLE);
164
165         /* TODO: run the scenario */
166         return;
167
168       default:
169         job->error = true;
170         return;
171     }
172 }