10 #if defined(TEST) && !defined(DEBUG)
16 # include <sys/types.h>
21 #define STRALLOC_INIT { 0 }
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
32 * Returns: Zero if the lookup failed, @+1@ on success, or @-1@ for an
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).
40 static int probe(int cdb, int prefix, const char *key, int len,
41 const char *suffix, const char **kp, uint32 *dlen)
43 static stralloc k = STRALLOC_INIT;
48 if (!stralloc_append(&k, &ch) ||
49 !stralloc_catb(&k, key, len) ||
50 (suffix && !stralloc_cats(&k, suffix)) ||
54 D( fprintf(stderr, "*** `%.*s' -> ", k.len, k.s); )
55 rc = cdb_seek(cdb, k.s, k.len - 1, dlen);
57 fprintf(stderr, "error: %s\n", strerror(errno));
59 fprintf(stderr, "not found\n");
61 fprintf(stderr, "empty\n");
66 off_t pos = lseek(cdb, 0, SEEK_CUR);
69 nn = sizeof(buf); if (nn > n) nn = n;
71 fwrite(buf, 1, nn, stderr);
74 fprintf(stderr, "'\n");
75 lseek(cdb, pos, SEEK_SET);
80 /* --- @localprobe@ --- *
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
86 * @const char *suffix@ = a suffix to append to the key (e.g.,
88 * @const char *tail@ = the tail of the local address matching
89 * any defaulted extension part
90 * @int *rc@ = where to put the answer
92 * Returns: Positive if found, zero if not found, negative on error.
94 * Use: Looks up a local-part in the database. This may involve
95 * invoking a Userv service for the recipient's owner.
98 static int localprobe(int cdb, const char *sender,
99 const char *key, int len,
100 const char *suffix, const char *tail, int *rc)
106 static stralloc u = STRALLOC_INIT;
107 static stralloc serv = STRALLOC_INIT;
113 if ((err = probe(cdb, 'L', key, len, suffix, &k, &dlen)) < 0)
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); }
120 if (!stralloc_ready(&u, dlen - 1)) return (-1);
121 if (read(cdb, u.s, dlen - 1) != dlen - 1) { errno = EIO; return (-1); }
124 if (!stralloc_cats(&serv, "addrcheck:") ||
125 !stralloc_cats(&serv, k + 1) ||
126 !stralloc_0(&serv) ||
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); )
134 if (pipe(p) || (kid = fork()) == -1)
137 close(0); open("/dev/null", O_RDONLY);
140 close(2); open("/dev/null", O_WRONLY);
142 close(p[0]); close(p[1]);
143 execl("/usr/bin/userv", "/usr/bin/userv",
144 "-f", "stdin=/dev/null", "-t", "10",
146 tail, sender, key, k + 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); }
157 D( fprintf(stderr, "userv answer was `%c'\n", ch); )
158 } else if (dlen != 1) {
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
174 * Returns: Positive if answered, zero if lookup failed, negative on
177 * Use: Finds out whether @l@ is a valid local-part for a non-virtual
178 * mailbox name. Handles defaulting of extension addresses and
182 static int local(int cdb, const char *l, int len,
183 const char *sender, int *rc)
189 if ((err = localprobe(cdb, sender, l, len, 0, l + len, &code)) != 0)
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)
206 case '+': *rc = 1; break;
207 case '-': *rc = 0; break;
208 default: errno = EINVAL; err = -1; break;
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
224 * Returns: Positive if found, zero if not found, negative on error.
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@.
231 static int virt(int cdb, const char *u, int ulen,
232 const char *addr, int alen, const char *sender, int *rc)
234 static stralloc l = STRALLOC_INIT;
238 if ((err = probe(cdb, 'V', addr, alen, 0, 0, &dlen)) <= 0)
240 if (!stralloc_ready(&l, dlen + 1)) return (-1);
241 if (read(cdb, l.s, dlen) != dlen) { errno = EIO; return (-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);
250 /* --- @addrcheck@ --- *
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.
257 * Returns: Nonnegative on success, or @-1@ on failure.
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.
264 int addrcheck(int cdb, const char *addr, const char *sender, int *rc)
269 static stralloc l = STRALLOC_INIT;
272 at = str_rchr(addr, '@');
274 return (local(cdb, addr, len, sender, rc));
276 if ((err = virt(cdb, addr, at, addr, len, sender, rc)) != 0)
280 if ((err = virt(cdb, addr, at, addr + dot, len - dot, sender, rc)) != 0)
282 dot += byte_chr(addr + dot + 1, len - dot - 1, '.') + 1;
285 if ((err = probe(cdb, '@', addr + at + 1, len - at - 1, 0, 0, &dlen)) < 0)
287 if (!err) { *rc = 1; return (0); }
288 if (dlen != 0) { errno = EINVAL; return (-1); }
290 if (!stralloc_catb(&l, addr, at) ||
293 return (local(cdb, l.s, l.len - 1, sender, rc));
298 #include <sys/types.h>
305 int main(int argc, char *argv[])
312 fprintf(stderr, "usage: addrcheck CDB SENDER ADDR...\n");
315 if ((fd = open(argv[1], O_RDONLY)) < 0) {
319 for (i = 3; i < argc; i++) {
320 if (addrcheck(fd, argv[i], argv[2], &rc) < 0) {
324 printf("%s: %s\n", argv[i], rc ? "ok" : "bad");