home *** CD-ROM | disk | FTP | other *** search
- /*
- * DOMAIN.C
- *
- * (C) Copyright 1989-1990 by Matthew Dillon, All Rights Reserved.
- *
- * Given the first machine in a path scan the domain list and
- * return the type, class, and address of the resource entry.
- *
- * AUTOMATIC HACKS: scans L.sys file and automatically deals
- * with machine[.UUCP] domain, returning
- * Type=MD Class=UU Addr=machine.UUCP
- */
-
- #include "defs.h"
-
- Prototype int DomainLookup (char *, int, char *, char *, char *, int);
- Prototype int CompareDomain (char **, int, char **, int);
-
- /*
- * DomainLookup() looks up a domain route for an address. Determination
- * of whether something will bounce or not is done here as well. For
- * INCOMING MAIL ONLY, if no route is found and bouncing is enabled AND
- * there is no alias (which essentially makes the mail outgoing again,
- * this is the 'nobounce' flag), then we return level 0 meaning an
- * error.
- */
-
- int
- DomainLookup (char *name, int len, char *tb, char *cb, char *ab, int nobounce)
- {
- char
- *dary [16],
- *tmp = malloc (len + 1),
- *tbase = tmp;
- int
- b,
- i,
- di,
- level;
- FILE
- *fi;
- static char
- buf [256];
-
- if (!tmp) {
- ulog (-1, "DomainLookup, no mem!");
- return 0;
- }
-
- for (b = i = di = 0; i < len && name[i] != '!'; ++i) {
- if (name [i] == '.') {
- if (di == sizeof (dary) / sizeof (dary [0]) - 1) {
- ulog (-1, "DomainLookup, too many domains! %s", name);
- break;
- }
- strncpy (tmp, name + b, i - b);
- tmp [i - b] = 0;
- dary [di] = tmp;
- tmp += i - b + 1;
- b = i + 1;
- ++di;
- }
- }
-
- strncpy (tmp, name + b, i - b);
- tmp [i - b] = 0;
- dary [di++] = tmp;
-
- /*
- * Check local mail. If local mail return a dummy UUCP domain
- * for ourself. This will cause the mail to be queued for
- * immediate execution. Sendmail may not run UUXQT as this can
- * cause mail loops. Since the user has not been run through
- * aliases we cannot simply post it locally, but must go through
- * another mail run.
- *
- * MBS UPDATE: 1/2/94 -- Expand aliases before calling here.
- * Thank you. (sendmail.c does.)
- */
-
- level = 0;
-
- if (stricmp (dary [0], NodeName) == 0) {
- strcpy (tb, "MD");
- strcpy (cb, "UU");
- strcpy (ab, NodeName);
- strcat (ab, ".uucp");
- level = 1;
- }
-
- if (level == 0 && (fi = fopen (MakeConfigPath (UULIB, "Domain"), "r"))) {
- while (fgets (buf, sizeof (buf), fi)) {
- int
- l2,
- di2 = 0;
- char
- *dary2 [16];
-
- if (buf [0] == ' ' || buf [0] == '\t' || buf [0] == '\n' || buf [0] == '#')
- continue;
- for (b = i = 0; buf [i] && buf [i] != ' ' && buf [i] != '\t'; ++i) {
- if (buf [i] == '.') {
- if (di2 == sizeof (dary2) / sizeof (dary2 [0]) - 1) {
- ulog (-1, "%s, entry has too many subdomains: %s", MakeConfigPath (UULIB, "Domain"), buf);
- break;
- }
- dary2 [di2++] = buf + b;
- buf [i] = 0;
- b = i + 1;
- }
- }
- buf [i] = 0;
- dary2 [di2++] = buf + b;
-
- buf[i] = 0; /* get domain name/wildcard */
-
- l2 = CompareDomain (dary, di, dary2, di2);
-
- if (l2 > level) { /* better domain then what we have */
- sscanf (buf + i + 1, "%s %s %s", tb, cb, ab);
- level = l2;
- }
- }
- fclose (fi);
- }
-
- /*
- * Couldn't find the appropriate domain entry, check L.sys
- * OR domain entry is a forwarder, check L.sys
- */
-
- if (strcmp (tb, "MF") == 0 || level == 0) {
- if (fi = fopen (MakeConfigPath (UULIB, "L.sys"), "r")) {
- while (fgets (buf, sizeof (buf), fi)) {
- if (buf [0] == ' ' || buf [0] == 9 || buf [0] == '#' || buf[0] == '\n')
- continue;
- for (i = 0; buf [i] && buf [i] != ' ' && buf [i] != 9; ++i);
- buf[i] = 0;
- if (stricmp (dary [0], buf) == 0) {
- strcpy (tb, "MD");
- strcpy (cb, "UU");
- strcpy (ab, buf);
- strcat (ab, ".uucp");
- level = 1;
- break;
- }
- }
- fclose (fi);
- }
- }
-
- /*
- * Couldn't find nothing, use DefaultNode for outgoing mail. If
- * Bounce's are enabled then bounce if incoming mail, else route it
- * to the default node (if we feed other Amiga's we may not want to
- * enable the bounce because they can then mail to a domain
- * address straight and it will be routed through everyone's
- * DefaultNode automatically)
- */
-
- if (level == 0) {
- if (ROpt && BounceOpt && !nobounce) {
- /*
- * If incoming mail and the bounce option is enabled then
- * return 0, signifying a failure
- */
-
- strcpy (tb, "MD");
- strcpy (cb, "UU");
- strcpy (ab, dary [0]);
- level = 0;
- }
- else {
- /*
- * outgoing mail, go to default node
- */
-
- if (DefaultNode == NULL) {
- ulog (-1, "Error, DefaultNode must exist if no Domain file");
- fprintf (stderr, "ERROR, no entry in Domain, L.sys, and\n");
- fprintf (stderr, "no DefaultNode config entry for %s\n", name);
- fprintf (stderr, "cannot send mail\n");
- free (tbase);
- return 0;
- }
- strcpy (tb, "MF");
- strcpy (cb, "UU");
- strcpy (ab, DefaultNode);
- level = 1;
- }
- }
-
- free (tbase);
- return level > 0;
- }
-
- /*
- * Compares a broken up address with a domain entry (buf).
- */
-
- int
- CompareDomain (char **da1, int di1, char **da2, int di2)
- {
- int
- i,
- j,
- level = 0;
-
- for (i = di1 - 1, j = di2 - 1; i >= 0 && j >= 0; --i, --j) {
- if (da2 [j] [0] == '*') {
- ++level;
- if (i && j == 0) /* so loop does not terminate */
- ++j;
- continue;
- }
- if (stricmp (da1 [i], da2 [j]) == 0)
- level += 2;
- else {
- if (j + 1 < di2 && da2 [j + 1] [0] == '*') {
- ++level;
- ++j;
- }
- else
- return 0;
- }
- }
-
- if (j >= 0) /* didn't exhaust domain entry */
- return 0;
-
- return level;
- }
-