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