2 ** addrsbr.c -- parse addresses 822-style
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/addrsbr.h>
14 ** High level parsing of addresses:
16 ** The routines in sbr/mf.c parse the syntactic representations of
17 ** addresses. The routines in sbr/addrsbr.c associate semantics with those
20 ** If #ifdef DUMB is in effect, a full 822-style parser is called
21 ** for syntax recongition. This breaks each address into its components.
22 ** Note however that no semantics are assumed about the parts or their
23 ** totality. This means that implicit hostnames aren't made explicit,
24 ** and explicit hostnames aren't expanded to their "official" represenations.
26 ** If DUMB is not in effect, then this module does some
27 ** high-level thinking about what the addresses are.
29 ** 1. for MMDF systems:
31 ** string%<uucp>@<local> -> string
33 ** 2. for non-MMDF systems:
35 ** string@host.<uucp> -> host!string
37 ** 3. for any system, an address interpreted relative to the local host:
39 ** string@<uucp> -> string
41 ** For cases (1) and (3) above, the leftmost host is extracted. If it's not
42 ** present, the local host is used. If the tests above fail, the address is
43 ** considered to be a real 822-style address.
45 ** If an explicit host is not present, then MH checks for a bang to indicate
46 ** an explicit UUCP-style address. If so, this is noted. If not, the host is
47 ** defaulted, typically to the local host. The lack of an explict host is
50 ** If an explicit 822-style host is present, then MH checks to see if it
51 ** can expand this to the official name for the host. If the hostname is
52 ** unknown, the address is so typed.
54 ** To summarize, when we're all done, here's what MH knows about the address:
56 ** DUMB - type: local, uucp, or network
57 ** host: not locally defaulted, not explicitly expanded
60 ** other - type: local, uucp, network, unknown
66 static char *pers = NULL;
67 static char *mbox = NULL;
68 static char *host = NULL;
69 static char *route = NULL;
70 static char *grp = NULL;
71 static char *note = NULL;
72 static char err[BUFSIZ];
73 static char adr[BUFSIZ];
77 ** external prototypes
79 char *getusername(void);
87 pers = mbox = host = route = grp = note = NULL;
90 if ((ap = getadrx(addrs ? addrs : "")) == NULL)
93 strncpy(adr, ap->text, sizeof(adr));
101 if (ap->err && *ap->err)
102 strncpy(err, ap->err, sizeof(err));
109 getm(char *str, char *dfhost, int dftype, int wanthost, char *eresult)
115 #endif /* not DUMB */
119 strcpy(eresult, err);
121 if (wanthost == AD_HOST)
122 admonish(NULL, "bad address '%s' - %s", str, err);
125 if (pers == NULL && mbox == NULL && host == NULL && route == NULL
128 strcpy(eresult, "null address");
130 if (wanthost == AD_HOST)
131 admonish(NULL, "null address '%s'", str);
134 if (mbox == NULL && grp == NULL) {
136 strcpy(eresult, "no mailbox in address");
137 else if (wanthost == AD_HOST)
138 admonish(NULL, "no mailbox in address '%s'", str);
142 if (dfhost == NULL) {
143 dfhost = LocalName();
147 mp = (struct mailname *) calloc((size_t) 1, sizeof(*mp));
150 strcpy(eresult, "insufficient memory to represent address");
151 else if (wanthost == AD_HOST)
152 adios(NULL, "insufficient memory to represent address");
157 mp->m_text = getcpy(str);
159 mp->m_pers = getcpy(pers);
162 mp->m_type = BADHOST;
165 mp->m_gname = getcpy(grp);
167 mp->m_note = getcpy(note);
172 mp->m_mbox = getcpy(mbox);
173 mp->m_host = getcpy(host);
175 if ((pp = strchr(mbox, '!'))) {
177 mp->m_mbox = getcpy(pp);
178 mp->m_host = getcpy(mbox);
179 mp->m_type = UUCPHOST;
182 mp->m_mbox = getcpy(mbox);
184 if (route == NULL && dftype == LOCALHOST) {
190 mp->m_host = route ? NULL : getcpy(dfhost);
191 mp->m_type = route ? NETHOST : dftype;
197 if (wanthost == AD_NHST)
198 mp->m_type = !mh_strcasecmp(LocalName(), mp->m_host)
199 ? LOCALHOST : NETHOST;
202 mp->m_type = mh_strcasecmp(LocalName(), mp->m_host) ? NETHOST : LOCALHOST;
205 if (pp = OfficialName(mp->m_host)) {
208 mp->m_host = getcpy(pp);
209 mp->m_type = mh_strcasecmp(LocalName(), mp->m_host) ? NETHOST : LOCALHOST;
211 if (dp = strchr(mp->m_host, '.')) {
213 if (pp = OfficialName(mp->m_host))
217 mp->m_type = BADHOST;
219 #endif /* not DUMB */
223 mp->m_path = getcpy(route);
226 mp->m_gname = getcpy(grp);
228 mp->m_note = getcpy(note);
235 mnfree(struct mailname *mp)
259 #define empty(s) ((s) ? (s) : "")
262 adrformat(struct mailname *mp)
264 static char addr[BUFSIZ];
265 static char buffer[BUFSIZ];
269 strncpy(addr, mp->m_mbox ? mp->m_mbox : "", sizeof(addr));
274 if (mp->m_type != UUCPHOST)
275 snprintf(addr, sizeof(addr), mp->m_host ? "%s%s@%s" : "%s%s",
276 empty(mp->m_path), empty(mp->m_mbox), mp->m_host);
278 #endif /* not BANG */
279 snprintf(addr, sizeof(addr), "%s!%s", mp->m_host, mp->m_mbox);
281 if (mp->m_pers || mp->m_path) {
283 snprintf(buffer, sizeof(buffer), "%s %s <%s>",
284 legal_person(mp->m_pers ? mp->m_pers : mp->m_mbox),
287 snprintf(buffer, sizeof(buffer), "%s <%s>",
288 legal_person(mp->m_pers ? mp->m_pers : mp->m_mbox),
290 } else if (mp->m_note)
291 snprintf(buffer, sizeof(buffer), "%s %s", addr, mp->m_note);
293 strncpy(buffer, addr, sizeof(buffer));
300 #define W_MBEG 0x0001
301 #define W_MEND 0x0002
302 #define W_MBOX (W_MBEG | W_MEND)
303 #define W_HBEG 0x0004
304 #define W_HEND 0x0008
305 #define W_HOST (W_HBEG | W_HEND)
306 #define WBITS "\020\01MBEG\02MEND\03HBEG\04HEND"
309 ** Check if this is my address
313 ismymbox(struct mailname *np)
321 static char *am = NULL;
322 static struct mailname mq={NULL};
325 ** If this is the first call, initialize
326 ** list of alternate mailboxes.
330 mq.m_mbox = getusername();
331 if ((am = context_find("alternate-mailboxes")) == NULL)
336 while ((cp = getname(am))) {
337 if ((mp->m_next = getm(cp, NULL, 0, AD_NAME, NULL)) == NULL) {
338 admonish(NULL, "illegal address: %s", cp);
343 if (*mp->m_mbox == '*') {
344 mp->m_type |= W_MBEG;
347 if (*(cp = mp->m_mbox + strlen(mp->m_mbox) - 1) == '*') {
348 mp->m_type |= W_MEND;
352 if (*mp->m_host == '*') {
353 mp->m_type |= W_HBEG;
356 if (*(cp = mp->m_host + strlen(mp->m_host) - 1) == '*') {
357 mp->m_type |= W_HEND;
361 if ((cp = getenv("MHWDEBUG")) && *cp)
362 fprintf(stderr, "mbox=\"%s\" host=\"%s\" %s\n",
363 mp->m_mbox, mp->m_host,
364 snprintb(buffer, sizeof(buffer), (unsigned) mp->m_type, WBITS));
368 advise(NULL, "please fix the profile entry %s",
369 "alternate-mailboxes");
373 if (np == NULL) /* XXX */
376 switch (np->m_type) {
378 len = strlen(cp = LocalName());
379 if (!uprf(np->m_host, cp) || np->m_host[len] != '.')
384 if (mh_strcasecmp(np->m_host, SystemName()))
388 if (!mh_strcasecmp(np->m_mbox, mq.m_mbox))
397 ** Now scan through list of alternate
398 ** mailboxes, and check for a match.
400 for (mp = &mq; mp->m_next;) {
404 if ((len = strlen(cp = np->m_mbox))
405 < (i = strlen(pp = mp->m_mbox)))
407 switch (mp->m_type & W_MBOX) {
409 if (mh_strcasecmp(cp, pp))
413 if (mh_strcasecmp(cp + len - i, pp))
420 case W_MBEG | W_MEND:
421 if (stringdex(pp, cp) < 0)
428 if (np->m_host == NULL)
430 if ((len = strlen(cp = np->m_host))
431 < (i = strlen(pp = mp->m_host)))
433 switch (mp->m_type & W_HOST) {
435 if (mh_strcasecmp(cp, pp))
439 if (mh_strcasecmp (cp + len - i, pp))
446 case W_HBEG | W_HEND:
447 if (stringdex(pp, cp) < 0)
459 ** Moved from hosts.c -- find out the official name of a host
463 ** In the SendMail world, we really don't know what the valid
464 ** hosts are. We could poke around in the sendmail.cf file, but
465 ** that still isn't a guarantee. As a result, we'll say that
466 ** everything is a valid host, and let SendMail worry about it.
474 OfficialName(char *name)
477 char *q, site[BUFSIZ];
478 struct addrinfo hints, *res;
480 static char buffer[BUFSIZ];
482 for (p = name, q = site; *p && (q - site < sizeof(site) - 1); p++, q++)
483 *q = isupper(*p) ? tolower(*p) : *p;
487 if (!mh_strcasecmp(LocalName(), site))
490 memset(&hints, 0, sizeof(hints));
491 hints.ai_flags = AI_CANONNAME;
492 hints.ai_family = PF_UNSPEC;
494 if (getaddrinfo(q, NULL, &hints, &res) == 0) {
495 strncpy(buffer, res->ai_canonname, sizeof(buffer));
496 buffer[sizeof(buffer) - 1] = '\0';
501 strncpy(buffer, site, sizeof(buffer));