chiark / gitweb /
496c5ddf82dab27f95cc63ef11ecd360bd88e6a8
[qmail] / addrcheck.c
1 #include "cdb.h"
2 #include "stralloc.h"
3 #include "byte.h"
4 #include "str.h"
5 #include "addrcheck.h"
6 #include <errno.h>
7 #include <unistd.h>
8
9 /* #define DEBUG */
10 #ifdef DEBUG
11 #  define D(x) x
12 #  include <stdio.h>
13 #  include <sys/types.h>
14 #else
15 #  define D(x)
16 #endif
17
18 #define STRALLOC_INIT { 0 }
19
20 /* --- @probe@ --- *
21  *
22  * Arguments:   @int cdb@ = the CDB file descriptor
23  *              @int prefix@ = the prefix character (e.g., @'V'@)
24  *              @const char *key, int len@ = the key string and its length
25  *              @const char *suffix@ = a suffix to append to the key
26  *              @const char **kp@ = where to put the full key string
27  *              @uint32 *dlen@ = where to put the answer's length
28  *
29  * Returns:     Zero if the lookup failed, @+1@ on success, or @-1@ for an
30  *              error.
31  *
32  * Use:         Probes a CDB file.  The key looked up is the character
33  *              @prefix@ followed by the first @len@ bytes of @key@, followed
34  *              by @suffix@ (if non-null).
35  */
36
37 static int probe(int cdb, int prefix, const char *key, int len,
38                  const char *suffix, const char **kp, uint32 *dlen)
39 {
40   static stralloc k = STRALLOC_INIT;
41   char ch = prefix;
42   int rc;
43
44   k.len = 0;
45   if (!stralloc_append(&k, &ch) ||
46       !stralloc_catb(&k, key, len) ||
47       (suffix && !stralloc_cats(&k, suffix)) ||
48       !stralloc_0(&k))
49     return (-1);
50   if (kp) *kp = k.s;
51   D( fprintf(stderr, "*** `%.*s' -> ", k.len, k.s); )
52   rc = cdb_seek(cdb, k.s, k.len - 1, dlen);
53   D( if (rc == -1)
54        fprintf(stderr, "error: %s\n", strerror(errno));
55      else if (rc == 0)
56        fprintf(stderr, "not found\n");
57      else if (!*dlen)
58        fprintf(stderr, "empty\n");
59      else {
60        int n = *dlen;
61        int nn;
62        char buf[256];
63        off_t pos = lseek(cdb, 0, SEEK_CUR);
64        fprintf(stderr, "`");
65        while (n) {
66          nn = sizeof(buf); if (nn > n) nn = n;
67          read(cdb, buf, nn);
68          fwrite(buf, 1, nn, stderr);
69          n -= nn;
70        }
71        fprintf(stderr, "'\n");
72        lseek(cdb, pos, SEEK_SET);
73      } )
74   return (rc);
75 }
76
77 /* --- @localprobe@ --- *
78  *
79  * Arguments:   @int cdb@ = the CDB file descriptor
80  *              @const char *sender@ = the envelope-sender address
81  *              @const char *key, int len@ = the (possibly defaulted) mailbox
82  *                      name to look up
83  *              @const char *suffix@ = a suffix to append to the key (e.g.,
84  *                      @"-default"@)
85  *              @const char *tail@ = the tail of the local address matching
86  *                      any defaulted extension part
87  *              @int *rc@ = where to put the answer
88  *
89  * Returns:     Positive if found, zero if not found, negative on error.
90  *
91  * Use:         Looks up a local-part in the database.  This may involve
92  *              invoking a Userv service for the recipient's owner.
93  */
94
95 static int localprobe(int cdb, const char *sender,
96                       const char *key, int len,
97                       const char *suffix, const char *tail, int *rc)
98 {
99   int err;
100   uint32 dlen;
101   char ch;
102   const char *k;
103   static stralloc u =  STRALLOC_INIT;
104   static stralloc serv = STRALLOC_INIT;
105   int kid;
106   int n;
107   int wstat;
108   int p[2];
109
110   if ((err = probe(cdb, 'L', key, len, suffix, &k, &dlen)) < 0)
111     return (-1);
112   if (!err) { *rc = 0; return (0); }
113   if (!dlen) { errno = EINVAL; return (-1); }  
114   if (read(cdb, &ch, 1) != 1) { errno = EIO; return (-1); }
115   if (ch == '?') {
116     u.len = 0;
117     if (!stralloc_ready(&u, dlen - 1)) return (-1);
118     if (read(cdb, u.s, dlen - 1) != dlen - 1) { errno = EIO; return (-1); }
119     u.len = dlen - 1;
120     serv.len = 0;
121     if (!stralloc_cats(&serv, "addrcheck:") ||
122         !stralloc_cats(&serv, k + 1) ||
123         !stralloc_0(&serv) ||
124         !stralloc_0(&u))
125       return (-1);
126     D( fprintf(stderr, "asking user:\n\
127   user = %s; service = %s\n  tail = %s; sender = %s\n\
128   address = %s; key = %s\n",
129                u.s, serv.s, tail, sender, key, k + 1); )
130                
131     if (pipe(p) || (kid = fork()) == -1)
132       return (-1);
133     if (!kid) {
134       dup2(p[1], 1);
135       close(p[0]);
136       close(p[1]);
137       execl("/usr/bin/userv", "/usr/bin/userv",
138             "-f", "stdin=/dev/null",
139             u.s, serv.s,
140             tail, sender, key, k + 1,
141             (char *)0);
142       _exit(127);
143     }
144     close(p[1]);
145     n = read(p[0], &ch, 1);
146     close(p[0]);
147     if (wait_pid(&wstat, kid) < 0) { return (-1); }
148     D( fprintf(stderr, "userv exited with status %d\n", wstat); )
149     if (n != 1 || wstat) { errno = EAGAIN; return (-1); }
150     D( fprintf(stderr, "userv answer was `%c'\n", ch); )
151   } else if (dlen != 1) {
152     errno = EIO;
153     return (-1);
154   }
155   *rc = ch;
156   return (1);
157 }
158
159 /* --- @local@ --- *
160  *
161  * Arguments:   @int cdb@ = file descriptor for the CDB
162  *              @const char *l@ = pointer to local-part to look up
163  *              @int len@ = length of the local-part
164  *              @const char *sender@ = envelope sender address
165  *              @int *rc@ = where to put the answer
166  *
167  * Returns:     Positive if answered, zero if lookup failed, negative on
168  *              error.
169  *
170  * Use:         Finds out whether @l@ is a valid local-part for a non-virtual
171  *              mailbox name.  Handles defaulting of extension addresses and
172  *              suchlike.
173  */
174
175 static int local(int cdb, const char *l, int len,
176                  const char *sender, int *rc)
177 {
178   int code;
179   int err = 0;
180   int dash;
181
182   if ((err = localprobe(cdb, sender, l, len, 0, l + len, &code)) != 0)
183     goto done;
184
185   for (;;) {
186     dash = byte_rchr(l, len, '-');
187     if (dash == len) break;
188     if ((err = localprobe(cdb, sender,
189                           l, dash, "-default", l + dash + 1, &code)) != 0)
190       goto done;
191     len = dash;
192   }
193   *rc = 0;
194   return (0);
195   
196 done:
197   if (err >= 0) {
198     switch (code) {
199       case '+': *rc = 1; break;
200       case '-': *rc = 0; break;
201       default: errno = EINVAL; err = -1; break;
202     }
203   }
204   return (err);
205 }
206
207 /* --- @virt@ --- *
208  *
209  * Arguments:   @int cdb@ = file descriptor for CDB
210  *              @const char *u@ = the local-part of the recipient mailbox
211  *              @int ulen@ = length of the local-part
212  *              @const char *addr@ = the virtual domain to look up
213  *              @int alen@ = length of the virtual domain
214  *              @const char *sender@ = the envelope sender address
215  *              @int *rc@ = where to put the answer
216  *
217  * Returns:     Positive if found, zero if not found, negative on error.
218  *
219  * Use:         Looks up the address as a virtual domain.  If found, the
220  *              local-part is appended to the local mailbox handling the
221  *              virtual domain and passed to @local@.
222  */
223
224 static int virt(int cdb, const char *u, int ulen,
225                 const char *addr, int alen, const char *sender, int *rc)
226 {
227   static stralloc l = STRALLOC_INIT;
228   uint32 dlen;
229   int err;
230
231   if ((err = probe(cdb, 'V', addr, alen, 0, 0, &dlen)) <= 0)
232     return (err);
233   if (!stralloc_ready(&l, dlen + 1)) return (-1);
234   if (read(cdb, l.s, dlen) != dlen) { errno = EIO; return (-1); }
235   l.s[dlen] = '-';
236   l.len = dlen + 1;
237   if (!stralloc_catb(&l, u, ulen) || !stralloc_0(&l)) return (-1);
238   D( printf("*** virtual map -> `%s'\n", l.s); )
239   if (local(cdb, l.s, l.len - 1, sender, rc) < 0) return (-1);
240   return (1);
241 }
242
243 /* --- @addrcheck@ --- *
244  *
245  * Arguments:   @int cdb@ = file descriptor for CDB to look things up in
246  *              @const char *addr@ = the (full) recipient mailbox to look up
247  *              @const char *sender@ = the (full) envelope sender address
248  *              @int *rc@ = where to put the answer.
249  *
250  * Returns:     Nonnegative on success, or @-1@ on failure.
251  *
252  * Use:         Determines whether @addr@ is a valid mailbox on this system,
253  *              by examining the given CDB file.  On exit, @*rc@ is set to
254  *              @'+'@ if the mailbox is valid, or @'-'@ if not.
255  */
256
257 int addrcheck(int cdb, const char *addr, const char *sender, int *rc)
258 {
259   int at, len, dot;
260   int err = 0;
261   uint32 dlen;
262   static stralloc l = STRALLOC_INIT;
263
264   len = str_len(addr);
265   at = str_chr(addr, '@');
266   if (!addr[at])
267     return (local(cdb, addr, len, sender, rc));
268
269   if ((err = virt(cdb, addr, at, addr, len, sender, rc)) != 0)
270     return (err);
271   dot = at + 1;
272   while (addr[dot]) {
273     if ((err = virt(cdb, addr, at, addr + dot, len - dot, sender, rc)) != 0)
274       return (err);
275     dot += byte_chr(addr + dot + 1, len - dot - 1, '.') + 1;
276   }
277
278   if ((err = probe(cdb, '@', addr + at + 1, len - at - 1, 0, 0, &dlen)) < 0)
279     return (-1);
280   if (!err) { *rc = 1; return (0); }
281   if (dlen != 0) { errno = EINVAL; return (-1); }
282   l.len = 0;
283   if (!stralloc_catb(&l, addr, at) ||
284       !stralloc_0(&l))
285     return (-1);
286   return (local(cdb, l.s, l.len - 1, sender, rc));
287 }
288
289 #ifdef TEST
290
291 #include <sys/types.h>
292 #include <unistd.h>
293 #include <fcntl.h>
294 #include <stdio.h>
295 #include <errno.h>
296 #include <unistd.h>
297
298 int main(int argc, char *argv[])
299 {
300   int fd;
301   int rc;
302   int i;
303
304   if (argc < 4) {
305     fprintf(stderr, "usage: addrcheck CDB SENDER ADDR...\n");
306     return (1);
307   }
308   if ((fd = open(argv[1], O_RDONLY)) < 0) {
309     perror(argv[1]);
310     return (1);
311   }
312   for (i = 3; i < argc; i++) {
313     if (addrcheck(fd, argv[i], argv[2], &rc) < 0) {
314       perror("checking");
315       return (1);
316     }
317     printf("%s: %s\n", argv[i], rc ? "ok" : "bad");
318   }
319   return (0);
320 }
321
322 #endif