home *** CD-ROM | disk | FTP | other *** search
- /*
- h o s t a b l e . c
-
- Remote host table routines for UUPC/extended
-
- Copyright (C) 1989, 1990 by Andrew H. Derbyshire
-
- See file README.SCR for restrictions on re-distribution.
-
- History:
-
- 18 Mar 1990 Create hostable.c from router.c ahd
- Move code to generate localdomain to here ahd
- 22 Apr 90 Perform check for full host name before examining
- name without domain. ahd
- 29 Jul 90 Only load host table based on first six characters
- of host name. ahd
- */
-
- #include <ctype.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
-
- #include "lib.h"
- #include "hlib.h"
- #include "hostable.h"
- #include "security.h"
-
- currentfile();
-
- static struct HostTable *hosts = NULL;
-
- static size_t HostElements = 0;
-
- static size_t loadhost( void );
-
- static int hostcmp( const void *a , const void *b );
-
- static size_t localdomainl; /* Length of localdomain */
-
- /*--------------------------------------------------------------------*/
- /* c h e c k n a m e */
- /* */
- /* Perform a search for a single host name */
- /* */
- /* Rewritten for release 1.10a. The old release had */
- /* most of the same logic, but nested it inside the search */
- /* loop; adding new cases (specifically, the wildcard domain */
- /* search) was difficult. This version is slower because it */
- /* makes multiple passes through the host table, but this */
- /* isn't really performance code for a small (under 100 hosts) */
- /* table. ahd 26 April 1991 */
- /* */
- /* Note because we save the arguments and use a static variable */
- /* to save the result of searches, this function is not */
- /* recursive! */
- /*--------------------------------------------------------------------*/
-
- struct HostTable *checkname(const char *name)
- {
- char hostname[MAXADDR]; /* Local copy of name to process */
- char *period; /* Pointer "." in hostname */
- size_t namel; /* Length of the name input */
- size_t column; /* Length of the name input */
-
- static char savename[MAXADDR] = "";
- /* Saved copy of name to make function
- reducible */
- static struct HostTable *hostz;
-
- /*--------------------------------------------------------------------*/
- /* Validate the argument */
- /*--------------------------------------------------------------------*/
-
- if ((name == NULL) || ((namel = strlen(name)) == 0))
- {
- printmsg(0,"checkname: Invalid (missing) hostname passed");
- panic();
- }
-
- /*--------------------------------------------------------------------*/
- /* If same argument as last time, return same result; otherwise */
- /* save input for next pass */
- /*--------------------------------------------------------------------*/
-
- if (equal(name, savename))
- return hostz;
- strcpy( savename, name); /* Save for next pass */
-
- /*--------------------------------------------------------------------*/
- /* Search for the full name */
- /*--------------------------------------------------------------------*/
-
- if ((hostz = searchname(name, MAXADDR)) != BADHOST)
- return hostz;
-
- /*--------------------------------------------------------------------*/
- /* If the name already has the local domain attached, search for */
- /* the host name without the domain. */
- /*--------------------------------------------------------------------*/
-
- column = namel - localdomainl;
- if ((namel > localdomainl) && equal(localdomain, &name[column]) &&
- (name[ column - 1] == '.'))
- {
- if ((hostz = searchname(name,column-1 )) != BADHOST)
- return hostz;
- } /* if */
-
- /*--------------------------------------------------------------------*/
- /* Search for the name in the local domain */
- /*--------------------------------------------------------------------*/
-
- if ((namel + localdomainl + 2) < MAXADDR)
- {
- sprintf(hostname,"%s.%s",name,localdomain);
- if ((hostz = searchname(hostname, MAXADDR)) != BADHOST)
- return hostz;
- } /* if */
-
- /*--------------------------------------------------------------------*/
- /* If a simple name and not found, return search for truncated */
- /* UNIX name. */
- /*--------------------------------------------------------------------*/
-
- if ( strchr(name,'.') == NULL )
- return checkreal( name );
-
- /*--------------------------------------------------------------------*/
- /* Perform a wildcard domain name search */
- /*--------------------------------------------------------------------*/
-
- period = (char *) name; /* Begin at front of name */
- while( period != NULL )
- {
- sprintf( hostname,(*period == '.') ? "*%s" : "*.%s",period);
- /* We add the missing period for the
- first pass through the loop */
- if ((hostz = searchname(hostname, MAXADDR)) != BADHOST)
- return hostz;
- period = strchr(++period,'.'); /* Not found, search for next
- higher domain */
- }
-
- /*--------------------------------------------------------------------*/
- /* We didn't find the host. Return failure to caller */
- /*--------------------------------------------------------------------*/
-
- return BADHOST;
-
- } /* checkname */
-
- /*--------------------------------------------------------------------*/
- /* c h e c k r e a l */
- /* */
- /* Perform a search for a real (connected) simple host name */
- /*--------------------------------------------------------------------*/
-
- struct HostTable *checkreal(const char *name)
- {
- size_t namel = max( strlen(name), HOSTLEN);
- struct HostTable *hostz = searchname( name, namel );
-
- /*--------------------------------------------------------------------*/
- /* If we didn't find the host, return failure */
- /*--------------------------------------------------------------------*/
-
- if ((hostz == BADHOST) || (hostz->hstatus >= nocall))
- return hostz; /* Return raw information */
- else
- return BADHOST; /* Not a real host, invalid for our
- purposes */
-
- } /* searchreal */
-
- /*--------------------------------------------------------------------*/
- /* s e a r c h n a m e */
- /* */
- /* Look up a system name in our systems (L.sys) file. */
- /* Only the first 7 characters of a system name is significant. */
- /*--------------------------------------------------------------------*/
-
- struct HostTable *searchname(const char *name, const size_t namel)
- {
- int lower;
- int upper;
-
- /*------------------------------------------------------------------*/
- /* Initialize the host name table if needed */
- /*------------------------------------------------------------------*/
-
- if (HostElements == 0) /* host table initialized yet? */
- HostElements = loadhost(); /* No --> load it */
-
- lower = 0;
- upper = HostElements - 1;
- while ( lower <= upper )
- {
- int midpoint = (lower + upper) / 2;
- int hit;
-
- /*--------------------------------------------------------------------*/
- /* Compare for up to the specified length of the host name, but */
- /* never less than the length of the item we are comparing it */
- /* to. In other words, the search key can be shorter than the */
- /* table entry for a simple host name. */
- /* */
- /* This mostly affects simple host names, as domain names */
- /* have a Very Large Number (VLN) passed into to insure we */
- /* compare the entire length of the name. */
- /*--------------------------------------------------------------------*/
-
- hit = strnicmp(name,hosts[midpoint].hostname,namel);
- if (hit > 0)
- lower = midpoint + 1;
- else if ((hit < 0) || (strlen(hosts[midpoint].hostname) > namel))
- upper = midpoint - 1;
- else {
- printmsg(8,"searchname: Looking for \"%s\" of length %d,\
- found \"%s\" via \"%s\"",
- name, namel, hosts[midpoint].hostname, hosts[midpoint].via);
- return &hosts[midpoint];
- }
- }
-
- /*--------------------------------------------------------------------*/
- /* We didn't find the host. Return failure to caller */
- /*--------------------------------------------------------------------*/
-
- printmsg(8,"searchname: Looking for \"%s\", did not find it",
- name);
- return BADHOST;
-
- } /* searchname */
-
-
- /*--------------------------------------------------------------------*/
- /* n e x t h o s t */
- /* */
- /* Returns next host in table with requested attribute */
- /*--------------------------------------------------------------------*/
-
- struct HostTable *nexthost( const boolean start , const hostatus status )
- {
- static size_t current = 0;
-
- if (HostElements == 0) /* host table initialized yet? */
- HostElements = loadhost(); /* No --> load it */
-
- if (start)
- current = 0;
- else
- current ++;
-
- while ( current < HostElements )
- {
- if (hosts[current].hstatus > status)
- return &hosts[current];
- else
- current++;
- }
-
- return BADHOST;
-
- } /* nexthost */
-
-
- /*
- i n i t h o s t
-
- Intializes a host table entry for for loadhost
- */
-
- struct HostTable *inithost(char *name)
- {
-
- size_t hit = HostElements;
- size_t element = 0;
- static size_t max_elements = 32; /* This is automatically
- raised if we run out of room */
-
- if (hosts == NULL)
- {
- hosts = calloc(max_elements, sizeof(*hosts));
- printmsg(5,"inithost: Allocated room for %d host entries",
- max_elements);
- }
- else if ( max_elements == HostElements )
- {
- max_elements = max_elements * 2;
- hosts = realloc(hosts , max_elements * sizeof(*hosts));
- printmsg(5,"inithost: reallocated room for %d host entries",
- max_elements);
- }
- checkref(hosts);
-
- /*--------------------------------------------------------------------*/
- /* Add the host to the table. Note that we must add the host */
- /* to the table ourselves (rather than use lsearch) because we */
- /* must make a copy of the string; the *token we use for the */
- /* search is in the middle of our I/O buffer! */
- /*--------------------------------------------------------------------*/
-
- while ( element < hit )
- {
- if (equali( hosts[element].hostname , name ))
- hit = element;
- else
- element++;
- }
-
- if (hit == HostElements)
- {
- hosts[hit].via = hosts[hit].hostname = strdup(name);
- hosts[hit].called = FALSE;
- hosts[hit].hstats = NULL;
- hosts[hit].hstatus = phantom;
- hosts[hit].hsecure = NULL;
- HostElements ++ ;
- } /* if */
- return &hosts[hit];
- } /* inithost */
-
-
- /*--------------------------------------------------------------------*/
- /* l o a d h o s t */
- /* */
- /* Initializes table of known host names for checkname */
- /*--------------------------------------------------------------------*/
-
- static size_t loadhost()
- {
- FILE *ff;
- char buf[BUFSIZ];
- char *token;
- char s_systems[FILENAME_MAX]; /* full-name of systems file */
- size_t hit;
- boolean hidden; /* Hidden via "fromdomain" keyword */
- struct HostTable *hostp;
-
- /*--------------------------------------------------------------------*/
- /* Load the local host information */
- /*--------------------------------------------------------------------*/
-
- hostp = inithost(nodename);
- hostp->hstatus = localhost;
-
- /*--------------------------------------------------------------------*/
- /* If the user is using a fromdomain entry, our true domain */
- /* name must be unique; load it first to insure this. */
- /*--------------------------------------------------------------------*/
-
- hidden = fdomain != NULL;
- if (hidden) /* If using fromdomain ... */
- { /* domain is forced to be true domain */
- hostp = inithost(domain);
- if (hostp->hstatus == phantom) /* Uninitilized? */
- {
- hostp->hstatus = aliasof;
- hostp->via = nodename;
- }
- } /* if (hidden) */
-
- /*--------------------------------------------------------------------*/
- /* If we allow anonymous UUCP, load the dummy host we use for */
- /* connections to such hosts */
- /*--------------------------------------------------------------------*/
-
- if ( anonymous != NULL )
- {
- hostp = inithost( ANONYMOUS_HOST );
- hostp->hstatus = localhost;
- hostp->hstatus = nocall;
- hostp->hstats = malloc( sizeof *(hostp->hstats) );
- checkref( hostp->hstats );
- memset( hostp->hstats, 0, sizeof *(hostp->hstats) );
- } /* if */
-
- /*--------------------------------------------------------------------*/
- /* Load names from the systems file */
- /*--------------------------------------------------------------------*/
-
- mkfilename(s_systems, confdir, SYSTEMS);
-
- ff = FOPEN(s_systems, "r", TEXT);
- if (ff == NULL)
- {
- printerr(s_systems);
- panic();
- }
-
- while (! feof(ff)) {
- if (fgets(buf,BUFSIZ,ff) == NULL) /* Try to read a line */
- break; /* Exit if end of file */
- token = strtok(buf,WHITESPACE);
- if (token == NULL) /* Any data? */
- continue; /* No --> read another line */
- if (token[0] == '#')
- continue; /* Line is a comment; loop again */
- hostp = inithost(token);
-
- if (hostp->hstatus == phantom)
- {
- hostp->hstatus = nocall;
- hostp->hstats = malloc( sizeof *(hostp->hstats) );
- checkref( hostp->hstats );
- memset( hostp->hstats, 0, sizeof *(hostp->hstats) );
- }
- else
- printmsg(2,"loadhost: Duplicate entry in %s for \"%s\" ignored",
- s_systems,
- hostp->hostname);
- } /* while */
-
- fclose(ff);
-
- /*--------------------------------------------------------------------*/
- /* Now the load the routing file, if any. */
- /*--------------------------------------------------------------------*/
-
- mkfilename(s_systems, confdir, PATHS);
-
- if ((ff = FOPEN(s_systems, "r", TEXT)) != NULL)
- {
-
- while (! feof(ff)) {
- if (fgets(buf,BUFSIZ,ff) == NULL) /* Try to read a line */
- break; /* Exit if end of file */
- token = strtok(buf,WHITESPACE);
-
- if (token == NULL) /* Any data? */
- continue; /* No --> read another line */
-
- if (token[0] == '#')
- continue; /* Line is a comment; loop again */
-
- hostp = inithost(token);
- token = strtok(NULL,WHITESPACE);
-
- if (hostp->hstatus == phantom) /* Uninitilized? */
- {
- if ((token != NULL) && equal(token,"="))
- {
- hostp->hstatus = aliasof;
- token = strtok(NULL,WHITESPACE);
- }
- else if ((token != NULL) && equal(token,"|"))
- {
- hostp->hstatus = gatewayed;
- token = strtok(NULL,"\n");
- while(isspace( *token )) /* Drop leading white space only */
- token++;
- if (*token == '\0') /* Empty string? */
- token = NULL; /* Yes --> Flag for error */
- }
- else
- hostp->hstatus = routed; /* Flag as routing entry */
- if ( token == NULL )
- {
- printmsg(0,"loadhost: Missing path/alias name for host \"%s\"",
- hostp->hostname);
- HostElements--; /* Ignore the routing entry */
- }
- else if (*token == '*') /* Wildcard on right side? */
- {
- printmsg(0,
- "loadhost: Wildcard \"%s\" not allowed for real name of host \"%s\"",
- token, hostp->hostname);
- HostElements--; /* Ignore the routing entry */
- }
- else
- hostp->via = strdup(token);
- }
- else
- printmsg(0,"Entry \"%s\" in routing table is real host",
- hostp->hostname);
- } /* end while */
-
- fclose(ff);
- }
- else
- printmsg(2,"loadhost: Cannot open host routing file %s",s_systems);
-
-
- /*--------------------------------------------------------------------*/
- /* Provide default for fromdomain */
- /*--------------------------------------------------------------------*/
-
- if (!hidden) /* If no fromdomain provided ... */
- fdomain = domain; /* Use domain as fromdomain */
-
- hostp = inithost(fdomain);
- if (hostp->hstatus == phantom) /* Uninitilized? */
- {
- hostp->hstatus = aliasof;
- hostp->via = hidden ? mailserv : nodename;
- }
-
- /*--------------------------------------------------------------------*/
- /* Shrink the table to whatever we actually need, then sort it */
- /*--------------------------------------------------------------------*/
-
- hosts = realloc(hosts, HostElements * sizeof(*hosts));
- checkref(hosts);
-
- qsort(hosts, HostElements ,sizeof(hosts[0]) , hostcmp);
-
- /*--------------------------------------------------------------------*/
- /* If the user did not define a local domain, then generate one */
- /* based on our own domain name; the generated name will either */
- /* be of the format ".a.b.c" (incuding the trailing period) or */
- /* a null string. */
- /*--------------------------------------------------------------------*/
-
- if ( localdomain == NULL )
- {
- localdomain = strchr(domain,'.');
- if (localdomain == NULL)
- localdomain = "UUCP";
- else
- localdomain ++; /* Step past the period */
- printmsg(2,"loadhost: local domain defined as \"%s\"",localdomain);
- } /* if */
- localdomainl = strlen(localdomain);
-
- /*--------------------------------------------------------------------*/
- /* Amend for the sin of typing the domain wrong in the sample */
- /* files for the old releases of UUPC/extended. */
- /*--------------------------------------------------------------------*/
-
- if (equal(localdomain,"UUPC"))
- {
- printmsg(0,"inithost: UUPC is an invalid domain name! \
- Change it to UUCP");
- panic();
- }
-
- /*--------------------------------------------------------------------*/
- /* Display the final table */
- /*--------------------------------------------------------------------*/
-
- for (hit = 0; hit < HostElements; hit++)
- {
- printmsg(8,"loadhost: final entry %d is \"%s\" via \"%s\"",
- hit,
- hosts[hit].hostname,hosts[hit].via);
- } /* for */
-
- /*--------------------------------------------------------------------*/
- /* Return to caller */
- /*--------------------------------------------------------------------*/
-
- return (HostElements) ;
- } /*loadhost*/
-
-
- /*
- h o s t c m p
-
- Accepts indirect pointers to two strings and compares
- them using stricmp (case insensitive string compare)
- */
-
- int hostcmp( const void *a , const void *b )
- {
- return stricmp(((struct HostTable*) a)->hostname,
- ((struct HostTable*) b)->hostname);
- } /*hostcmp*/
-