#include "str.h"
#include "addrcheck.h"
#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
/* #define DEBUG */
#ifdef DEBUG
# define D(x) x
# include <stdio.h>
# include <sys/types.h>
-# include <unistd.h>
#else
# define D(x)
#endif
#define STRALLOC_INIT { 0 }
+/* --- @probe@ --- *
+ *
+ * Arguments: @int cdb@ = the CDB file descriptor
+ * @int prefix@ = the prefix character (e.g., @'V'@)
+ * @const char *key, int len@ = the key string and its length
+ * @const char *suffix@ = a suffix to append to the key
+ * @const char **kp@ = where to put the full key string
+ * @uint32 *dlen@ = where to put the answer's length
+ *
+ * Returns: Zero if the lookup failed, @+1@ on success, or @-1@ for an
+ * error.
+ *
+ * Use: Probes a CDB file. The key looked up is the character
+ * @prefix@ followed by the first @len@ bytes of @key@, followed
+ * by @suffix@ (if non-null).
+ */
+
static int probe(int cdb, int prefix, const char *key, int len,
- const char *suffix, uint32 *dlen)
+ const char *suffix, const char **kp, uint32 *dlen)
{
static stralloc k = STRALLOC_INIT;
char ch = prefix;
k.len = 0;
if (!stralloc_append(&k, &ch) ||
!stralloc_catb(&k, key, len) ||
- (suffix && !stralloc_cats(&k, suffix)))
+ (suffix && !stralloc_cats(&k, suffix)) ||
+ !stralloc_0(&k))
return (-1);
+ if (kp) *kp = k.s;
D( fprintf(stderr, "*** `%.*s' -> ", k.len, k.s); )
- rc = cdb_seek(cdb, k.s, k.len, dlen);
+ rc = cdb_seek(cdb, k.s, k.len - 1, dlen);
D( if (rc == -1)
fprintf(stderr, "error: %s\n", strerror(errno));
else if (rc == 0)
return (rc);
}
-static int localprobe(int cdb, const char *key, int len,
- const char *suffix, int *rc)
+/* --- @localprobe@ --- *
+ *
+ * Arguments: @int cdb@ = the CDB file descriptor
+ * @const char *sender@ = the envelope-sender address
+ * @const char *key, int len@ = the (possibly defaulted) mailbox
+ * name to look up
+ * @const char *suffix@ = a suffix to append to the key (e.g.,
+ * @"-default"@)
+ * @const char *tail@ = the tail of the local address matching
+ * any defaulted extension part
+ * @int *rc@ = where to put the answer
+ *
+ * Returns: Positive if found, zero if not found, negative on error.
+ *
+ * Use: Looks up a local-part in the database. This may involve
+ * invoking a Userv service for the recipient's owner.
+ */
+
+static int localprobe(int cdb, const char *sender,
+ const char *key, int len,
+ const char *suffix, const char *tail, int *rc)
{
int err;
uint32 dlen;
char ch;
+ const char *k;
+ static stralloc u = STRALLOC_INIT;
+ static stralloc serv = STRALLOC_INIT;
+ int kid;
+ int n;
+ int wstat;
+ int p[2];
- if ((err = probe(cdb, 'L', key, len, suffix, &dlen)) < 0)
+ if ((err = probe(cdb, 'L', key, len, suffix, &k, &dlen)) < 0)
return (-1);
if (!err) { *rc = 0; return (0); }
- if (dlen != 1) { errno = EINVAL; return (-1); }
+ if (!dlen) { errno = EINVAL; return (-1); }
if (read(cdb, &ch, 1) != 1) { errno = EIO; return (-1); }
+ if (ch == '?') {
+ u.len = 0;
+ if (!stralloc_ready(&u, dlen - 1)) return (-1);
+ if (read(cdb, u.s, dlen - 1) != dlen - 1) { errno = EIO; return (-1); }
+ u.len = dlen - 1;
+ serv.len = 0;
+ if (!stralloc_cats(&serv, "addrcheck:") ||
+ !stralloc_cats(&serv, k + 1) ||
+ !stralloc_0(&serv) ||
+ !stralloc_0(&u))
+ return (-1);
+ D( fprintf(stderr, "asking user:\n\
+ user = %s; service = %s\n tail = %s; sender = %s\n\
+ address = %s; key = %s\n",
+ u.s, serv.s, tail, sender, key, k + 1); )
+
+ if (pipe(p) || (kid = fork()) == -1)
+ return (-1);
+ if (!kid) {
+ close(0); open("/dev/null", O_RDONLY);
+ dup2(p[1], 1);
+ close(2); open("/dev/null", O_WRONLY);
+ close(p[0]); close(p[1]);
+ execl("/usr/bin/userv", "/usr/bin/userv",
+ "-f", "stdin=/dev/null",
+ u.s, serv.s,
+ tail, sender, key, k + 1,
+ (char *)0);
+ _exit(127);
+ }
+ close(p[1]);
+ n = read(p[0], &ch, 1);
+ close(p[0]);
+ if (wait_pid(&wstat, kid) < 0) { return (-1); }
+ D( fprintf(stderr, "userv exited with status %d\n", wstat); )
+ if (n != 1 || wstat) { errno = EAGAIN; return (-1); }
+ D( fprintf(stderr, "userv answer was `%c'\n", ch); )
+ } else if (dlen != 1) {
+ errno = EIO;
+ return (-1);
+ }
*rc = ch;
return (1);
}
-static int local(int cdb, const char *l, int len, int *rc)
+/* --- @local@ --- *
+ *
+ * Arguments: @int cdb@ = file descriptor for the CDB
+ * @const char *l@ = pointer to local-part to look up
+ * @int len@ = length of the local-part
+ * @const char *sender@ = envelope sender address
+ * @int *rc@ = where to put the answer
+ *
+ * Returns: Positive if answered, zero if lookup failed, negative on
+ * error.
+ *
+ * Use: Finds out whether @l@ is a valid local-part for a non-virtual
+ * mailbox name. Handles defaulting of extension addresses and
+ * suchlike.
+ */
+
+static int local(int cdb, const char *l, int len,
+ const char *sender, int *rc)
{
int code;
int err = 0;
int dash;
- if ((err = localprobe(cdb, l, len, 0, &code)) != 0) goto done;
+ if ((err = localprobe(cdb, sender, l, len, 0, l + len, &code)) != 0)
+ goto done;
for (;;) {
dash = byte_rchr(l, len, '-');
if (dash == len) break;
- if ((err = localprobe(cdb, l, dash, "-default", &code)) != 0) goto done;
+ if ((err = localprobe(cdb, sender,
+ l, dash, "-default", l + dash + 1, &code)) != 0)
+ goto done;
len = dash;
}
*rc = 0;
return (err);
}
+/* --- @virt@ --- *
+ *
+ * Arguments: @int cdb@ = file descriptor for CDB
+ * @const char *u@ = the local-part of the recipient mailbox
+ * @int ulen@ = length of the local-part
+ * @const char *addr@ = the virtual domain to look up
+ * @int alen@ = length of the virtual domain
+ * @const char *sender@ = the envelope sender address
+ * @int *rc@ = where to put the answer
+ *
+ * Returns: Positive if found, zero if not found, negative on error.
+ *
+ * Use: Looks up the address as a virtual domain. If found, the
+ * local-part is appended to the local mailbox handling the
+ * virtual domain and passed to @local@.
+ */
+
static int virt(int cdb, const char *u, int ulen,
- const char *addr, int alen, int *rc)
+ const char *addr, int alen, const char *sender, int *rc)
{
static stralloc l = STRALLOC_INIT;
uint32 dlen;
int err;
- if ((err = probe(cdb, 'V', addr, alen, 0, &dlen)) <= 0)
+ if ((err = probe(cdb, 'V', addr, alen, 0, 0, &dlen)) <= 0)
return (err);
if (!stralloc_ready(&l, dlen + 1)) return (-1);
if (read(cdb, l.s, dlen) != dlen) { errno = EIO; return (-1); }
l.s[dlen] = '-';
l.len = dlen + 1;
- if (!stralloc_catb(&l, u, ulen)) return (-1);
- D( printf("*** virtual map -> `%.*s'\n", l.len, l.s); )
- if (local(cdb, l.s, l.len, rc) < 0) return (-1);
+ if (!stralloc_catb(&l, u, ulen) || !stralloc_0(&l)) return (-1);
+ D( printf("*** virtual map -> `%s'\n", l.s); )
+ if (local(cdb, l.s, l.len - 1, sender, rc) < 0) return (-1);
return (1);
}
-int addrcheck(int cdb, const char *addr, int *rc)
+/* --- @addrcheck@ --- *
+ *
+ * Arguments: @int cdb@ = file descriptor for CDB to look things up in
+ * @const char *addr@ = the (full) recipient mailbox to look up
+ * @const char *sender@ = the (full) envelope sender address
+ * @int *rc@ = where to put the answer.
+ *
+ * Returns: Nonnegative on success, or @-1@ on failure.
+ *
+ * Use: Determines whether @addr@ is a valid mailbox on this system,
+ * by examining the given CDB file. On exit, @*rc@ is set to
+ * @'+'@ if the mailbox is valid, or @'-'@ if not.
+ */
+
+int addrcheck(int cdb, const char *addr, const char *sender, int *rc)
{
int at, len, dot;
int err = 0;
uint32 dlen;
+ static stralloc l = STRALLOC_INIT;
len = str_len(addr);
- at = str_chr(addr, '@');
+ at = str_rchr(addr, '@');
if (!addr[at])
- return (local(cdb, addr, len, rc));
+ return (local(cdb, addr, len, sender, rc));
- if ((err = virt(cdb, addr, at, addr, len, rc)) != 0)
+ if ((err = virt(cdb, addr, at, addr, len, sender, rc)) != 0)
return (err);
dot = at + 1;
while (addr[dot]) {
- if ((err = virt(cdb, addr, at, addr + dot, len - dot, rc)) != 0)
+ if ((err = virt(cdb, addr, at, addr + dot, len - dot, sender, rc)) != 0)
return (err);
dot += byte_chr(addr + dot + 1, len - dot - 1, '.') + 1;
}
- if ((err = probe(cdb, '@', addr + at + 1, len - at - 1, 0, &dlen)) < 0)
+ if ((err = probe(cdb, '@', addr + at + 1, len - at - 1, 0, 0, &dlen)) < 0)
return (-1);
if (!err) { *rc = 1; return (0); }
if (dlen != 0) { errno = EINVAL; return (-1); }
-
- return (local(cdb, addr, at, rc));
+ l.len = 0;
+ if (!stralloc_catb(&l, addr, at) ||
+ !stralloc_0(&l))
+ return (-1);
+ return (local(cdb, l.s, l.len - 1, sender, rc));
}
#ifdef TEST
+
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int rc;
int i;
- if (argc < 3) {
- fprintf(stderr, "usage: addrcheck CDB ADDR...\n");
+ if (argc < 4) {
+ fprintf(stderr, "usage: addrcheck CDB SENDER ADDR...\n");
return (1);
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror(argv[1]);
return (1);
}
- for (i = 2; i < argc; i++) {
- if (addrcheck(fd, argv[i], &rc) < 0) {
+ for (i = 3; i < argc; i++) {
+ if (addrcheck(fd, argv[i], argv[2], &rc) < 0) {
perror("checking");
return (1);
}