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];
75 /* static prototype */
76 char *OfficialName(char *);
84 pers = mbox = host = route = grp = note = NULL;
87 if ((ap = getadrx(addrs ? addrs : "")) == NULL)
90 strncpy(adr, ap->text, sizeof(adr));
98 if (ap->err && *ap->err)
99 strncpy(err, ap->err, sizeof(err));
106 getm(char *str, char *dfhost, int dftype, int wanthost, char *eresult)
112 #endif /* not DUMB */
116 strcpy(eresult, err);
118 if (wanthost == AD_HOST)
119 admonish(NULL, "bad address '%s' - %s", str, err);
122 if (pers == NULL && mbox == NULL && host == NULL && route == NULL
125 strcpy(eresult, "null address");
127 if (wanthost == AD_HOST)
128 admonish(NULL, "null address '%s'", str);
131 if (mbox == NULL && grp == NULL) {
133 strcpy(eresult, "no mailbox in address");
134 else if (wanthost == AD_HOST)
135 admonish(NULL, "no mailbox in address '%s'", str);
139 if (dfhost == NULL) {
140 dfhost = LocalName();
144 mp = (struct mailname *) calloc((size_t) 1, sizeof(*mp));
147 strcpy(eresult, "insufficient memory to represent address");
148 else if (wanthost == AD_HOST)
149 adios(NULL, "insufficient memory to represent address");
154 mp->m_text = getcpy(str);
156 mp->m_pers = getcpy(pers);
159 mp->m_type = BADHOST;
162 mp->m_gname = getcpy(grp);
164 mp->m_note = getcpy(note);
169 mp->m_mbox = getcpy(mbox);
170 mp->m_host = getcpy(host);
172 if ((pp = strchr(mbox, '!'))) {
174 mp->m_mbox = getcpy(pp);
175 mp->m_host = getcpy(mbox);
176 mp->m_type = UUCPHOST;
179 mp->m_mbox = getcpy(mbox);
181 if (route == NULL && dftype == LOCALHOST) {
187 mp->m_host = route ? NULL : getcpy(dfhost);
188 mp->m_type = route ? NETHOST : dftype;
194 if (wanthost == AD_NHST)
195 mp->m_type = !mh_strcasecmp(LocalName(), mp->m_host)
196 ? LOCALHOST : NETHOST;
199 mp->m_type = mh_strcasecmp(LocalName(), mp->m_host) ? NETHOST : LOCALHOST;
202 if (pp = OfficialName(mp->m_host)) {
205 mp->m_host = getcpy(pp);
206 mp->m_type = mh_strcasecmp(LocalName(), mp->m_host) ? NETHOST : LOCALHOST;
208 if (dp = strchr(mp->m_host, '.')) {
210 if (pp = OfficialName(mp->m_host))
214 mp->m_type = BADHOST;
216 #endif /* not DUMB */
220 mp->m_path = getcpy(route);
223 mp->m_gname = getcpy(grp);
225 mp->m_note = getcpy(note);
232 mnfree(struct mailname *mp)
256 #define empty(s) ((s) ? (s) : "")
259 adrformat(struct mailname *mp)
261 static char addr[BUFSIZ];
262 static char buffer[BUFSIZ];
266 strncpy(addr, mp->m_mbox ? mp->m_mbox : "", sizeof(addr));
271 if (mp->m_type != UUCPHOST)
272 snprintf(addr, sizeof(addr), mp->m_host ? "%s%s@%s" : "%s%s",
273 empty(mp->m_path), empty(mp->m_mbox), mp->m_host);
275 #endif /* not BANG */
276 snprintf(addr, sizeof(addr), "%s!%s", mp->m_host, mp->m_mbox);
278 if (mp->m_pers || mp->m_path) {
280 snprintf(buffer, sizeof(buffer), "%s %s <%s>",
281 legal_person(mp->m_pers ? mp->m_pers : mp->m_mbox),
284 snprintf(buffer, sizeof(buffer), "%s <%s>",
285 legal_person(mp->m_pers ? mp->m_pers : mp->m_mbox),
287 } else if (mp->m_note)
288 snprintf(buffer, sizeof(buffer), "%s %s", addr, mp->m_note);
290 strncpy(buffer, addr, sizeof(buffer));
297 #define W_MBEG 0x0001
298 #define W_MEND 0x0002
299 #define W_MBOX (W_MBEG | W_MEND)
300 #define W_HBEG 0x0004
301 #define W_HEND 0x0008
302 #define W_HOST (W_HBEG | W_HEND)
303 #define WBITS "\020\01MBEG\02MEND\03HBEG\04HEND"
306 ** Check if this is my address
310 ismymbox(struct mailname *np)
318 static char *am = NULL;
319 static struct mailname mq={NULL};
322 ** If this is the first call, initialize
323 ** list of alternate mailboxes.
327 mq.m_mbox = getusername();
328 if ((am = context_find("alternate-mailboxes")) == NULL)
333 while ((cp = getname(am))) {
334 if ((mp->m_next = getm(cp, NULL, 0, AD_NAME, NULL)) == NULL) {
335 admonish(NULL, "illegal address: %s", cp);
340 if (*mp->m_mbox == '*') {
341 mp->m_type |= W_MBEG;
344 if (*(cp = mp->m_mbox + strlen(mp->m_mbox) - 1) == '*') {
345 mp->m_type |= W_MEND;
349 if (*mp->m_host == '*') {
350 mp->m_type |= W_HBEG;
353 if (*(cp = mp->m_host + strlen(mp->m_host) - 1) == '*') {
354 mp->m_type |= W_HEND;
358 if ((cp = getenv("MHWDEBUG")) && *cp)
359 fprintf(stderr, "mbox=\"%s\" host=\"%s\" %s\n",
360 mp->m_mbox, mp->m_host,
361 snprintb(buffer, sizeof(buffer), (unsigned) mp->m_type, WBITS));
365 advise(NULL, "please fix the profile entry %s",
366 "alternate-mailboxes");
370 if (np == NULL) /* XXX */
373 switch (np->m_type) {
375 len = strlen(cp = LocalName());
376 if (!uprf(np->m_host, cp) || np->m_host[len] != '.')
381 if (mh_strcasecmp(np->m_host, SystemName()))
385 if (!mh_strcasecmp(np->m_mbox, mq.m_mbox))
394 ** Now scan through list of alternate
395 ** mailboxes, and check for a match.
397 for (mp = &mq; mp->m_next;) {
401 if ((len = strlen(cp = np->m_mbox))
402 < (i = strlen(pp = mp->m_mbox)))
404 switch (mp->m_type & W_MBOX) {
406 if (mh_strcasecmp(cp, pp))
410 if (mh_strcasecmp(cp + len - i, pp))
417 case W_MBEG | W_MEND:
418 if (stringdex(pp, cp) < 0)
425 if (np->m_host == NULL)
427 if ((len = strlen(cp = np->m_host))
428 < (i = strlen(pp = mp->m_host)))
430 switch (mp->m_type & W_HOST) {
432 if (mh_strcasecmp(cp, pp))
436 if (mh_strcasecmp (cp + len - i, pp))
443 case W_HBEG | W_HEND:
444 if (stringdex(pp, cp) < 0)
456 ** Moved from hosts.c -- find out the official name of a host
460 ** In the SendMail world, we really don't know what the valid
461 ** hosts are. We could poke around in the sendmail.cf file, but
462 ** that still isn't a guarantee. As a result, we'll say that
463 ** everything is a valid host, and let SendMail worry about it.
470 OfficialName(char *name)
473 char *q, site[BUFSIZ];
474 struct addrinfo hints, *res;
476 static char buffer[BUFSIZ];
478 for (p = name, q = site; *p && (q - site < sizeof(site) - 1); p++, q++)
479 *q = isupper(*p) ? tolower(*p) : *p;
483 if (!mh_strcasecmp(LocalName(), site))
486 memset(&hints, 0, sizeof(hints));
487 hints.ai_flags = AI_CANONNAME;
488 hints.ai_family = PF_UNSPEC;
490 if (getaddrinfo(q, NULL, &hints, &res) == 0) {
491 strncpy(buffer, res->ai_canonname, sizeof(buffer));
492 buffer[sizeof(buffer) - 1] = '\0';
497 strncpy(buffer, site, sizeof(buffer));