13 # include <sys/types.h>
18 #define STRALLOC_INIT { 0 }
20 static int probe(int cdb, int prefix, const char *key, int len,
21 const char *suffix, const char **kp, uint32 *dlen)
23 static stralloc k = STRALLOC_INIT;
28 if (!stralloc_append(&k, &ch) ||
29 !stralloc_catb(&k, key, len) ||
30 (suffix && !stralloc_cats(&k, suffix)) ||
34 D( fprintf(stderr, "*** `%.*s' -> ", k.len, k.s); )
35 rc = cdb_seek(cdb, k.s, k.len - 1, dlen);
37 fprintf(stderr, "error: %s\n", strerror(errno));
39 fprintf(stderr, "not found\n");
41 fprintf(stderr, "empty\n");
46 off_t pos = lseek(cdb, 0, SEEK_CUR);
49 nn = sizeof(buf); if (nn > n) nn = n;
51 fwrite(buf, 1, nn, stderr);
54 fprintf(stderr, "'\n");
55 lseek(cdb, pos, SEEK_SET);
60 static int localprobe(int cdb, const char *sender,
61 const char *key, int len,
62 const char *suffix, const char *tail, int *rc)
68 static stralloc u = STRALLOC_INIT;
69 static stralloc serv = STRALLOC_INIT;
75 if ((err = probe(cdb, 'L', key, len, suffix, &k, &dlen)) < 0)
77 if (!err) { *rc = 0; return (0); }
78 if (!dlen) { errno = EINVAL; return (-1); }
79 if (read(cdb, &ch, 1) != 1) { errno = EIO; return (-1); }
82 if (!stralloc_ready(&u, dlen - 1)) return (-1);
83 if (read(cdb, u.s, dlen - 1) != dlen - 1) { errno = EIO; return (-1); }
86 if (!stralloc_cats(&serv, "addrcheck:") ||
87 !stralloc_cats(&serv, k + 1) ||
91 D( fprintf(stderr, "asking user:\n\
92 user = %s; service = %s\n tail = %s; sender = %s\n\
93 address = %s; key = %s\n",
94 u.s, serv.s, tail, sender, key, k + 1); )
96 if (pipe(p) || (kid = fork()) == -1)
102 execl("/usr/bin/userv", "/usr/bin/userv",
103 "-f", "stdin=/dev/null",
105 tail, sender, key, k + 1,
110 n = read(p[0], &ch, 1);
112 if (wait_pid(&wstat, kid) < 0) { return (-1); }
113 D( fprintf(stderr, "userv exited with status %d\n", wstat); )
114 if (n != 1 || wstat) { errno = EAGAIN; return (-1); }
115 D( fprintf(stderr, "userv answer was `%c'\n", ch); )
116 } else if (dlen != 1) {
124 static int local(int cdb, const char *l, int len,
125 const char *sender, int *rc)
131 if ((err = localprobe(cdb, sender, l, len, 0, l + len, &code)) != 0)
135 dash = byte_rchr(l, len, '-');
136 if (dash == len) break;
137 if ((err = localprobe(cdb, sender,
138 l, dash, "-default", l + dash + 1, &code)) != 0)
148 case '+': *rc = 1; break;
149 case '-': *rc = 0; break;
150 default: errno = EINVAL; err = -1; break;
156 static int virt(int cdb, const char *u, int ulen,
157 const char *addr, int alen, const char *sender, int *rc)
159 static stralloc l = STRALLOC_INIT;
163 if ((err = probe(cdb, 'V', addr, alen, 0, 0, &dlen)) <= 0)
165 if (!stralloc_ready(&l, dlen + 1)) return (-1);
166 if (read(cdb, l.s, dlen) != dlen) { errno = EIO; return (-1); }
169 if (!stralloc_catb(&l, u, ulen) || !stralloc_0(&l)) return (-1);
170 D( printf("*** virtual map -> `%s'\n", l.s); )
171 if (local(cdb, l.s, l.len - 1, sender, rc) < 0) return (-1);
175 int addrcheck(int cdb, const char *addr, const char *sender, int *rc)
182 at = str_chr(addr, '@');
184 return (local(cdb, addr, len, sender, rc));
186 if ((err = virt(cdb, addr, at, addr, len, sender, rc)) != 0)
190 if ((err = virt(cdb, addr, at, addr + dot, len - dot, sender, rc)) != 0)
192 dot += byte_chr(addr + dot + 1, len - dot - 1, '.') + 1;
195 if ((err = probe(cdb, '@', addr + at + 1, len - at - 1, 0, 0, &dlen)) < 0)
197 if (!err) { *rc = 1; return (0); }
198 if (dlen != 0) { errno = EINVAL; return (-1); }
200 return (local(cdb, addr, at, sender, rc));
205 #include <sys/types.h>
212 int main(int argc, char *argv[])
219 fprintf(stderr, "usage: addrcheck CDB SENDER ADDR...\n");
222 if ((fd = open(argv[1], O_RDONLY)) < 0) {
226 for (i = 3; i < argc; i++) {
227 if (addrcheck(fd, argv[i], argv[2], &rc) < 0) {
231 printf("%s: %s\n", argv[i], rc ? "ok" : "bad");