示例#1
0
/*
  setup_table -- initialize the table of hosts allowed to contact the server,
                 by reading from the file specified by the GNU_SECURE
		 environment variable
                 Put in the local machine, and, if a security file is specifed,
                 add each host that is named in the file.
		 Return the number of hosts added.
*/
static int
setup_table (void)
{
  FILE *host_file;
  char *file_name;
  char hostname[HOSTNAMSZ];
  unsigned int host_addr;
  int i, hosts=0;

  /* Make sure every entry is null */
  for (i=0; i<TABLE_SIZE; i++)
    permitted_hosts[i] = NULL;

  gethostname(hostname,HOSTNAMSZ);

  if ((host_addr = internet_addr (hostname)) == (unsigned int) -1)
    {
      fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP",
	      progname,hostname);
      exit(1);
    } /* if */

#ifdef AUTH_MAGIC_COOKIE

  server_xauth = XauGetAuthByAddr (FamilyInternet,
				   sizeof(host_addr), (char *)&host_addr,
				   strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
				   strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
  hosts++;

#endif /* AUTH_MAGIC_COOKIE */


#if 0 /* Don't even want to allow access from the local host by default */
  add_host(host_addr);					/* add local host */
#endif

  if (((file_name = getenv("GNU_SECURE")) != NULL &&    /* security file  */
       (host_file = fopen(file_name,"r")) != NULL))	/* opened ok */
    {
      while ((fscanf(host_file,"%s",hostname) != EOF))	/* find a host */
	if ((host_addr = internet_addr(hostname)) != (unsigned int) -1)
	  /* get its addr */
	  {
	    add_host(host_addr);			/* add the addr */
	    hosts++;
	  }
      fclose(host_file);
    } /* if */

  return hosts;
} /* setup_table */
示例#2
0
/*
  connect_to_internet_server -- establish connection with server process via 
  				an internet domain socket. Returns socket
				descriptor for server if successful.
*/
static int
connect_to_internet_server (char *serverhost, unsigned short port)
{
  int s;				/* connected socket descriptor */
  struct servent *sp;			/* pointer to service information */
  struct sockaddr_in peeraddr_in;	/* for peer socket address */
  char buf[512];                        /* temporary buffer */

  int t;

  /* clear out address structures */
  memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in));
  
  /* Set up the peer address to which we will connect. */
  peeraddr_in.sin_family = AF_INET;

  /* look up the server host's internet address */
  if ((t = internet_addr(serverhost)) == -1) {
    fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP\n",
	    progname,serverhost);
    exit(1);
  } else {
    peeraddr_in.sin_addr.s_addr = t;
  }; /* if */
  
  if (port == 0) {
    if ((sp = getservbyname ("gnuserv","tcp")) == NULL)
      peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid());
    else
      peeraddr_in.sin_port = sp->s_port;
  } /* if */
  else
    peeraddr_in.sin_port = htons(port);
  
  /* Create the socket. */
  if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) {
    perror(progname);
    fprintf(stderr,"%s: unable to create socket\n",progname);
    exit(1);
  }; /* if */
  
  /* Try to connect to the remote server at the address
   * which was just built into peeraddr.
   */
  if (connect(s, (struct sockaddr *)&peeraddr_in,
	      sizeof(struct sockaddr_in)) == -1) {
    perror(progname);
    fprintf(stderr, "%s: unable to connect to remote\n",progname);
    exit(1);
  }; /* if */

#ifdef AUTH_MAGIC_COOKIE

  /* send credentials using MIT-MAGIC-COOKIE-1 protocol */
  
  server_xauth = 
    XauGetAuthByAddr(FamilyInternet, 
		     sizeof(peeraddr_in.sin_addr.s_addr), 
		     (char *) &peeraddr_in.sin_addr.s_addr,
		     strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
		     strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);

  if (server_xauth && server_xauth->data) {
    sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length);
    write (s, buf, strlen(buf));
    write (s, server_xauth->data, server_xauth->data_length);

    return (s);
  }

#endif /* AUTH_MAGIC_COOKIE */

  sprintf (buf, "%s\n", DEFAUTH_NAME);
  write (s, buf, strlen(buf));

  return(s);

} /* connect_to_internet_server */