14 # include <sys/types.h>
19 #define STRALLOC_INIT { 0 }
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
30 * Returns: Zero if the lookup failed, @+1@ on success, or @-1@ for an
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).
38 static int probe(int cdb
, int prefix
, const char *key
, int len
,
39 const char *suffix
, const char **kp
, uint32
*dlen
)
41 static stralloc k
= STRALLOC_INIT
;
46 if (!stralloc_append(&k
, &ch
) ||
47 !stralloc_catb(&k
, key
, len
) ||
48 (suffix
&& !stralloc_cats(&k
, suffix
)) ||
52 D( fprintf(stderr
, "*** `%.*s' -> ", k
.len
, k
.s
); )
53 rc
= cdb_seek(cdb
, k
.s
, k
.len
- 1, dlen
);
55 fprintf(stderr
, "error: %s\n", strerror(errno
));
57 fprintf(stderr
, "not found\n");
59 fprintf(stderr
, "empty\n");
64 off_t pos
= lseek(cdb
, 0, SEEK_CUR
);
67 nn
= sizeof(buf
); if (nn
> n
) nn
= n
;
69 fwrite(buf
, 1, nn
, stderr
);
72 fprintf(stderr
, "'\n");
73 lseek(cdb
, pos
, SEEK_SET
);
78 /* --- @localprobe@ --- *
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
84 * @const char *suffix@ = a suffix to append to the key (e.g.,
86 * @const char *tail@ = the tail of the local address matching
87 * any defaulted extension part
88 * @int *rc@ = where to put the answer
90 * Returns: Positive if found, zero if not found, negative on error.
92 * Use: Looks up a local-part in the database. This may involve
93 * invoking a Userv service for the recipient's owner.
96 static int localprobe(int cdb
, const char *sender
,
97 const char *key
, int len
,
98 const char *suffix
, const char *tail
, int *rc
)
104 static stralloc u
= STRALLOC_INIT
;
105 static stralloc serv
= STRALLOC_INIT
;
111 if ((err
= probe(cdb
, 'L', key
, len
, suffix
, &k
, &dlen
)) < 0)
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); }
118 if (!stralloc_ready(&u
, dlen
- 1)) return (-1);
119 if (read(cdb
, u
.s
, dlen
- 1) != dlen
- 1) { errno
= EIO
; return (-1); }
122 if (!stralloc_cats(&serv
, "addrcheck:") ||
123 !stralloc_cats(&serv
, k
+ 1) ||
124 !stralloc_0(&serv
) ||
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); )
132 if (pipe(p
) || (kid
= fork()) == -1)
135 close(0); open("/dev/null", O_RDONLY
);
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",
142 tail
, sender
, key
, k
+ 1,
147 n
= read(p
[0], &ch
, 1);
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) {
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
169 * Returns: Positive if answered, zero if lookup failed, negative on
172 * Use: Finds out whether @l@ is a valid local-part for a non-virtual
173 * mailbox name. Handles defaulting of extension addresses and
177 static int local(int cdb
, const char *l
, int len
,
178 const char *sender
, int *rc
)
184 if ((err
= localprobe(cdb
, sender
, l
, len
, 0, l
+ len
, &code
)) != 0)
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)
201 case '+': *rc
= 1; break;
202 case '-': *rc
= 0; break;
203 default: errno
= EINVAL
; err
= -1; break;
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
219 * Returns: Positive if found, zero if not found, negative on error.
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@.
226 static int virt(int cdb
, const char *u
, int ulen
,
227 const char *addr
, int alen
, const char *sender
, int *rc
)
229 static stralloc l
= STRALLOC_INIT
;
233 if ((err
= probe(cdb
, 'V', addr
, alen
, 0, 0, &dlen
)) <= 0)
235 if (!stralloc_ready(&l
, dlen
+ 1)) return (-1);
236 if (read(cdb
, l
.s
, dlen
) != dlen
) { errno
= EIO
; return (-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);
245 /* --- @addrcheck@ --- *
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.
252 * Returns: Nonnegative on success, or @-1@ on failure.
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.
259 int addrcheck(int cdb
, const char *addr
, const char *sender
, int *rc
)
264 static stralloc l
= STRALLOC_INIT
;
267 at
= str_chr(addr
, '@');
269 return (local(cdb
, addr
, len
, sender
, rc
));
271 if ((err
= virt(cdb
, addr
, at
, addr
, len
, sender
, rc
)) != 0)
275 if ((err
= virt(cdb
, addr
, at
, addr
+ dot
, len
- dot
, sender
, rc
)) != 0)
277 dot
+= byte_chr(addr
+ dot
+ 1, len
- dot
- 1, '.') + 1;
280 if ((err
= probe(cdb
, '@', addr
+ at
+ 1, len
- at
- 1, 0, 0, &dlen
)) < 0)
282 if (!err
) { *rc
= 1; return (0); }
283 if (dlen
!= 0) { errno
= EINVAL
; return (-1); }
285 if (!stralloc_catb(&l
, addr
, at
) ||
288 return (local(cdb
, l
.s
, l
.len
- 1, sender
, rc
));
293 #include <sys/types.h>
300 int main(int argc
, char *argv
[])
307 fprintf(stderr
, "usage: addrcheck CDB SENDER ADDR...\n");
310 if ((fd
= open(argv
[1], O_RDONLY
)) < 0) {
314 for (i
= 3; i
< argc
; i
++) {
315 if (addrcheck(fd
, argv
[i
], argv
[2], &rc
) < 0) {
319 printf("%s: %s\n", argv
[i
], rc ?
"ok" : "bad");