*** postmaster.c.orig Wed Jul 22 13:03:42 1992 --- postmaster.c Wed Jul 22 13:03:39 1992 *************** *** 158,163 **** --- 158,164 ---- int ServerLoop ARGS((int serverFd )); int ConnStartup ARGS((Port *port )); int ConnCreate ARGS((int serverFd , int *newFdP )); + int chkhost ARGS ((sockaddr_in *sinp)); int PortDestroy ARGS((Port *port )); int pmdie ARGS((void )); int reaper ARGS((void )); *************** *** 175,180 **** --- 176,183 ---- int *CacheAlloc ARGS((unsigned int size )); #endif + char *hostName; + main(argc, argv) int argc; char *argv[]; *************** *** 183,189 **** char getopt(); char opt; int serverMask; - char *hostName; int status; int size; char sysLogPath[PATH_SIZE]; --- 186,191 ---- *************** *** 323,337 **** int newMask; status = ConnCreate(serverFd, &newFd); ! if (newFd >= nSockets) ! nSockets = newFd+1; ! /* read the new connection on the first pass */ ! newMask = 1 << newFd; ! nSelected--; ! /* add the new connection to the baseMask */ ! baseMask |= newMask; } ListForEach( curr, PortList, PortList) { --- 325,340 ---- int newMask; status = ConnCreate(serverFd, &newFd); + nSelected--; + if (status == STATUS_OK) { ! if (newFd >= nSockets) ! nSockets = newFd+1; ! /* add the new connection to the baseMask */ ! newMask = 1 << newFd; ! baseMask |= newMask; ! } } ListForEach( curr, PortList, PortList) { *************** *** 415,420 **** --- 418,424 ---- Connection *conn; int connId; int status; + int newstatus; long currTime; int newFd; Port *port = (Port *) zalloc(sizeof(Port)); *************** *** 422,436 **** status = StreamConnection(serverFd,&newFd,&port->addr); ! port->sock = newFd; ! port->mask = 1 << newFd; ! ListPush(PortList, port); ! *newFdP = newFd; ! /* in case of error message */ ! SendPort = port; /* * If there was an error in the port creation, the connection * struct should be freed again. --- 426,446 ---- status = StreamConnection(serverFd,&newFd,&port->addr); ! if (status == STATUS_OK) { ! status = chkhost(&port->addr); ! } ! if (status == STATUS_OK) { ! port->sock = newFd; ! port->mask = 1 << newFd; ! ListPush(PortList, port); ! *newFdP = newFd; + /* in case of error message */ + SendPort = port; + } + /* * If there was an error in the port creation, the connection * struct should be freed again. *************** *** 1032,1034 **** --- 1042,1104 ---- bzero(t, size); return(t); } + + + /* + * Check to see if the from host has access to the backend + */ + + #define OTHERHOSTFILE "/etc/hosts.postgres" + #define DUMMY ":nobody::" + + chkhost(sinp) + struct sockaddr_in *sinp; + { + register struct hostent *hp; + register FILE *hostf; + register char *cp; + char frombuf[50]; + char cbuf[50]; + char *from; + extern char *inet_ntoa(); + int baselen = -1; + + sinp->sin_port = ntohs(sinp->sin_port); + hp = gethostbyaddr(&sinp->sin_addr, sizeof(struct in_addr), + sinp->sin_family); + if (hp == 0) { + fprintf(stderr, "Unknown host attempting connection: %s", + inet_ntoa(sinp->sin_addr)); + return(STATUS_INVALID); + } + + strcpy(frombuf, hp->h_name); + from = frombuf; + if (!strcmp(from, hostName)) + return(STATUS_OK); + + cp = cbuf; + while (*from) { + if (*from == '.') { + if (baselen == -1) + baselen = from - frombuf; + *cp++ = *from++; + baselen = from - frombuf; + *cp++ = *from++; + } else { + *cp++ = isupper(*from) ? tolower(*from++) : *from++; + } + } + *cp = '\0'; + hostf = fopen(OTHERHOSTFILE, "r"); + if (hostf) { + if (!_validuser(hostf, cbuf, DUMMY, DUMMY, baselen)) { + (void) fclose(hostf); + return(STATUS_OK); + } + (void) fclose(hostf); + } + from = frombuf; + fprintf(stderr, "POSTGRES: Invalid host attempting access: %s\n", from); + return(STATUS_INVALID); + } +