#include "kernel.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "oslib/os.h"
#include "sockets.h"

#include "syslogif.h"
#include "def.h"

#include "tcpd.h"

/* check an incoming connection with tcp_wrap to see if it should be allowed */

int tcpwrapif_check_connection(struct sockaddr *connection)
{
  struct request_info request;

  if ((!fexist(HOSTS_ALLOW)) && (!fexist(HOSTS_DENY)))
  {
    xsyslogf_irq(SYSLOG_FILE,LOG_CONNECTION,"   No HostsAllow/Deny files found - only allowing 127.0.0.1 access");
    /* if no HostsAllow/Deny files exist, only allow localhost to connect as before */
      if ((connection->sa_data[2] != 127) ||
          (connection->sa_data[3] != 0  ) ||
          (connection->sa_data[4] != 0  ) ||
          (connection->sa_data[5] != 1  ))
      {
        /* not 127.0.0.1 - disallow */
        return 0;
      }
      else
      {
        /* is 127.0.0.1 - allow */
        return 1;
      }
  }
  else
  {
    xsyslogf_irq(SYSLOG_FILE,LOG_CONNECTION_LOW,"Checking access rights with tcp_wrappers...");
    /* log any errors to our SysLog file */
    tcpd_context.use_syslog = 1;
    tcpd_context.syslog_file = SYSLOG_FILE;
    /* logging levels */
    tcpd_context.syslog_error_level = LOG_ERROR;
    tcpd_context.syslog_warning_level = LOG_WARNING;
    request_init(&request,RQ_DAEMON,"sshproxy",RQ_CLIENT_SIN,connection,0);

    sock_methods(&request);

    return hosts_access(&request);
  }

}
