13 # include <sys/types.h>
18 #define STRALLOC_INIT { 0 }
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
29 * Returns: Zero if the lookup failed, @+1@ on success, or @-1@ for an
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).
37 static int probe(int cdb, int prefix, const char *key, int len,
38 const char *suffix, const char **kp, uint32 *dlen)
40 static stralloc k = STRALLOC_INIT;
45 if (!stralloc_append(&k, &ch) ||
46 !stralloc_catb(&k, key, len) ||
47 (suffix && !stralloc_cats(&k, suffix)) ||
51 D( fprintf(stderr, "*** `%.*s' -> ", k.len, k.s); )
52 rc = cdb_seek(cdb, k.s, k.len - 1, dlen);
54 fprintf(stderr, "error: %s\n", strerror(errno));
56 fprintf(stderr, "not found\n");
58 fprintf(stderr, "empty\n");
63 off_t pos = lseek(cdb, 0, SEEK_CUR);
66 nn = sizeof(buf); if (nn > n) nn = n;
68 fwrite(buf, 1, nn, stderr);
71 fprintf(stderr, "'\n");
72 lseek(cdb, pos, SEEK_SET);
77 /* --- @localprobe@ --- *
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
83 * @const char *suffix@ = a suffix to append to the key (e.g.,
85 * @const char *tail@ = the tail of the local address matching
86 * any defaulted extension part
87 * @int *rc@ = where to put the answer
89 * Returns: Positive if found, zero if not found, negative on error.
91 * Use: Looks up a local-part in the database. This may involve
92 * invoking a Userv service for the recipient's owner.
95 static int localprobe(int cdb, const char *sender,
96 const char *key, int len,
97 const char *suffix, const char *tail, int *rc)
103 static stralloc u = STRALLOC_INIT;
104 static stralloc serv = STRALLOC_INIT;
110 if ((err = probe(cdb, 'L', key, len, suffix, &k, &dlen)) < 0)
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); }
117 if (!stralloc_ready(&u, dlen - 1)) return (-1);
118 if (read(cdb, u.s, dlen - 1) != dlen - 1) { errno = EIO; return (-1); }
121 if (!stralloc_cats(&serv, "addrcheck:") ||
122 !stralloc_cats(&serv, k + 1) ||
123 !stralloc_0(&serv) ||
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); )
131 if (pipe(p) || (kid = fork()) == -1)
137 execl("/usr/bin/userv", "/usr/bin/userv",
138 "-f", "stdin=/dev/null",
140 tail, sender, key, k + 1,
145 n = read(p[0], &ch, 1);
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) {
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
167 * Returns: Positive if answered, zero if lookup failed, negative on
170 * Use: Finds out whether @l@ is a valid local-part for a non-virtual
171 * mailbox name. Handles defaulting of extension addresses and
175 static int local(int cdb, const char *l, int len,
176 const char *sender, int *rc)
182 if ((err = localprobe(cdb, sender, l, len, 0, l + len, &code)) != 0)
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)
199 case '+': *rc = 1; break;
200 case '-': *rc = 0; break;
201 default: errno = EINVAL; err = -1; break;
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
217 * Returns: Positive if found, zero if not found, negative on error.
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@.
224 static int virt(int cdb, const char *u, int ulen,
225 const char *addr, int alen, const char *sender, int *rc)
227 static stralloc l = STRALLOC_INIT;
231 if ((err = probe(cdb, 'V', addr, alen, 0, 0, &dlen)) <= 0)
233 if (!stralloc_ready(&l, dlen + 1)) return (-1);
234 if (read(cdb, l.s, dlen) != dlen) { errno = EIO; return (-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);
243 /* --- @addrcheck@ --- *
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.
250 * Returns: Nonnegative on success, or @-1@ on failure.
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.
257 int addrcheck(int cdb, const char *addr, const char *sender, int *rc)
264 at = str_chr(addr, '@');
266 return (local(cdb, addr, len, sender, rc));
268 if ((err = virt(cdb, addr, at, addr, len, sender, rc)) != 0)
272 if ((err = virt(cdb, addr, at, addr + dot, len - dot, sender, rc)) != 0)
274 dot += byte_chr(addr + dot + 1, len - dot - 1, '.') + 1;
277 if ((err = probe(cdb, '@', addr + at + 1, len - at - 1, 0, 0, &dlen)) < 0)
279 if (!err) { *rc = 1; return (0); }
280 if (dlen != 0) { errno = EINVAL; return (-1); }
282 return (local(cdb, addr, at, sender, rc));
287 #include <sys/types.h>
294 int main(int argc, char *argv[])
301 fprintf(stderr, "usage: addrcheck CDB SENDER ADDR...\n");
304 if ((fd = open(argv[1], O_RDONLY)) < 0) {
308 for (i = 3; i < argc; i++) {
309 if (addrcheck(fd, argv[i], argv[2], &rc) < 0) {
313 printf("%s: %s\n", argv[i], rc ? "ok" : "bad");