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];
76 extern boolean username_extension_masquerading; /* defined in mts.c */
82 char *getusername (void);
90 pers = mbox = host = route = grp = note = NULL;
93 if ((ap = getadrx (addrs ? addrs : "")) == NULL)
96 strncpy (adr, ap->text, sizeof(adr));
104 if (ap->err && *ap->err)
105 strncpy (err, ap->err, sizeof(err));
112 getm (char *str, char *dfhost, int dftype, int wanthost, char *eresult)
118 #endif /* not DUMB */
122 strcpy (eresult, err);
124 if (wanthost == AD_HOST)
125 admonish (NULL, "bad address '%s' - %s", str, err);
128 if (pers == NULL && mbox == NULL && host == NULL && route == NULL
131 strcpy (eresult, "null address");
133 if (wanthost == AD_HOST)
134 admonish (NULL, "null address '%s'", str);
137 if (mbox == NULL && grp == NULL) {
139 strcpy (eresult, "no mailbox in address");
140 else if (wanthost == AD_HOST)
141 admonish (NULL, "no mailbox in address '%s'", str);
145 if (dfhost == NULL) {
146 dfhost = LocalName ();
150 mp = (struct mailname *) calloc ((size_t) 1, sizeof(*mp));
153 strcpy (eresult, "insufficient memory to represent address");
154 else if (wanthost == AD_HOST)
155 adios (NULL, "insufficient memory to represent address");
160 mp->m_text = getcpy (str);
162 mp->m_pers = getcpy (pers);
165 mp->m_type = BADHOST;
168 mp->m_gname = getcpy (grp);
170 mp->m_note = getcpy (note);
175 mp->m_mbox = getcpy (mbox);
176 mp->m_host = getcpy (host);
178 if ((pp = strchr(mbox, '!'))) {
180 mp->m_mbox = getcpy (pp);
181 mp->m_host = getcpy (mbox);
182 mp->m_type = UUCPHOST;
185 mp->m_mbox = getcpy (mbox);
187 if (route == NULL && dftype == LOCALHOST) {
193 mp->m_host = route ? NULL : getcpy (dfhost);
194 mp->m_type = route ? NETHOST : dftype;
200 if (wanthost == AD_NHST)
201 mp->m_type = !mh_strcasecmp (LocalName (), mp->m_host)
202 ? LOCALHOST : NETHOST;
205 mp->m_type = mh_strcasecmp (LocalName(), mp->m_host) ? NETHOST : LOCALHOST;
208 if (pp = OfficialName (mp->m_host)) {
211 mp->m_host = getcpy (pp);
212 mp->m_type = mh_strcasecmp (LocalName(), mp->m_host) ? NETHOST : LOCALHOST;
214 if (dp = strchr(mp->m_host, '.')) {
216 if (pp = OfficialName (mp->m_host))
220 mp->m_type = BADHOST;
222 #endif /* not DUMB */
226 mp->m_path = getcpy (route);
229 mp->m_gname = getcpy (grp);
231 mp->m_note = getcpy (note);
238 mnfree (struct mailname *mp)
262 #define empty(s) ((s) ? (s) : "")
265 adrformat (struct mailname *mp)
267 static char addr[BUFSIZ];
268 static char buffer[BUFSIZ];
272 strncpy (addr, mp->m_mbox ? mp->m_mbox : "", sizeof(addr));
277 if (mp->m_type != UUCPHOST)
278 snprintf (addr, sizeof(addr), mp->m_host ? "%s%s@%s" : "%s%s",
279 empty(mp->m_path), empty(mp->m_mbox), mp->m_host);
281 #endif /* not BANG */
282 snprintf (addr, sizeof(addr), "%s!%s", mp->m_host, mp->m_mbox);
284 if (mp->m_pers || mp->m_path) {
286 snprintf (buffer, sizeof(buffer), "%s %s <%s>",
287 legal_person (mp->m_pers ? mp->m_pers : mp->m_mbox),
290 snprintf (buffer, sizeof(buffer), "%s <%s>",
291 legal_person (mp->m_pers ? mp->m_pers : mp->m_mbox),
293 } else if (mp->m_note)
294 snprintf (buffer, sizeof(buffer), "%s %s", addr, mp->m_note);
296 strncpy (buffer, addr, sizeof(buffer));
303 #define W_MBEG 0x0001
304 #define W_MEND 0x0002
305 #define W_MBOX (W_MBEG | W_MEND)
306 #define W_HBEG 0x0004
307 #define W_HEND 0x0008
308 #define W_HOST (W_HBEG | W_HEND)
309 #define WBITS "\020\01MBEG\02MEND\03HBEG\04HEND"
312 * Check if this is my address
316 ismymbox (struct mailname *np)
324 static char *am = NULL;
325 static struct mailname mq={NULL};
328 * If this is the first call, initialize
329 * list of alternate mailboxes.
333 mq.m_mbox = getusername ();
334 if ((am = context_find ("alternate-mailboxes")) == NULL)
339 while ((cp = getname (am))) {
340 if ((mp->m_next = getm (cp, NULL, 0, AD_NAME, NULL)) == NULL) {
341 admonish (NULL, "illegal address: %s", cp);
346 if (*mp->m_mbox == '*') {
347 mp->m_type |= W_MBEG;
350 if (*(cp = mp->m_mbox + strlen (mp->m_mbox) - 1) == '*') {
351 mp->m_type |= W_MEND;
355 if (*mp->m_host == '*') {
356 mp->m_type |= W_HBEG;
359 if (*(cp = mp->m_host + strlen (mp->m_host) - 1) == '*') {
360 mp->m_type |= W_HEND;
364 if ((cp = getenv ("MHWDEBUG")) && *cp)
365 fprintf (stderr, "mbox=\"%s\" host=\"%s\" %s\n",
366 mp->m_mbox, mp->m_host,
367 snprintb (buffer, sizeof(buffer), (unsigned) mp->m_type, WBITS));
371 advise (NULL, "please fix the %s: entry in your %s file",
372 "alternate-mailboxes", mh_profile);
376 if (np == NULL) /* XXX */
379 switch (np->m_type) {
381 len = strlen (cp = LocalName ());
382 if (!uprf (np->m_host, cp) || np->m_host[len] != '.')
387 if (mh_strcasecmp (np->m_host, SystemName()))
391 if (!mh_strcasecmp (np->m_mbox, mq.m_mbox))
400 * Now scan through list of alternate
401 * mailboxes, and check for a match.
403 for (mp = &mq; mp->m_next;) {
407 if ((len = strlen (cp = np->m_mbox))
408 < (i = strlen (pp = mp->m_mbox)))
410 switch (mp->m_type & W_MBOX) {
412 if (mh_strcasecmp (cp, pp))
416 if (mh_strcasecmp (cp + len - i, pp))
423 case W_MBEG | W_MEND:
424 if (stringdex (pp, cp) < 0)
431 if (np->m_host == NULL)
433 if ((len = strlen (cp = np->m_host))
434 < (i = strlen (pp = mp->m_host)))
436 switch (mp->m_type & W_HOST) {
438 if (mh_strcasecmp (cp, pp))
442 if (mh_strcasecmp (cp + len - i, pp))
449 case W_HBEG | W_HEND:
450 if (stringdex (pp, cp) < 0)
462 * Moved from hosts.c -- find out the official name of a host
466 * In the SendMail world, we really don't know what the valid
467 * hosts are. We could poke around in the sendmail.cf file, but
468 * that still isn't a guarantee. As a result, we'll say that
469 * everything is a valid host, and let SendMail worry about it.
477 OfficialName (char *name)
480 char *q, site[BUFSIZ];
481 struct addrinfo hints, *res;
483 static char buffer[BUFSIZ];
485 for (p = name, q = site; *p && (q - site < sizeof(site) - 1); p++, q++)
486 *q = isupper (*p) ? tolower (*p) : *p;
490 if (!mh_strcasecmp (LocalName(), site))
493 memset(&hints, 0, sizeof(hints));
494 hints.ai_flags = AI_CANONNAME;
495 hints.ai_family = PF_UNSPEC;
497 if (getaddrinfo(q, NULL, &hints, &res) == 0) {
498 strncpy (buffer, res->ai_canonname, sizeof(buffer));
499 buffer[sizeof(buffer) - 1] = '\0';
504 strncpy (buffer, site, sizeof(buffer));