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