Beispiel #1
0
int validate_socket(

  int          psock,
  std::string &external_err_msg)

  {
  int            rc = PBSE_NONE;
  char           tmp_buf[LOCAL_LOG_BUF];
  char           write_buf[1024];
  char          *read_buf = NULL;
  long long      read_buf_len = 0;
  uid_t          myrealuid;
  int            local_socket = 0;
  int            parent_client_socket = 0;
  struct passwd *pwent;
  char          *err_msg = NULL;
  char          *l_server = NULL;
  int            l_server_len = 0;
  unsigned short af_family;
  long long      code = -1;
  int            write_buf_len = 0;
  int            local_errno;
  pid_t          mypid;
  char           unix_sockname[MAXPATHLEN + 1];
  char           err_buf[MAXPATHLEN];

  myrealuid = getuid();

  if ((pwent = getpwuid(myrealuid)) == NULL)
    {
    snprintf(tmp_buf, LOCAL_LOG_BUF, "cannot get account info: uid %d, errno %d (%s)\n", (int)myrealuid, errno, strerror(errno));
    log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_SERVER, __func__, tmp_buf);
    }
  else if ((rc = get_hostaddr_hostent_af(&local_errno, (char *)AUTH_IP, &af_family, &l_server, &l_server_len)) != PBSE_NONE)
    {
    snprintf(err_buf, sizeof(err_buf), "get_hostaddr_hostend_af failed: %d", rc);
    external_err_msg = err_buf;
    }
  else if ((rc = get_parent_client_socket(psock, &parent_client_socket)) != PBSE_NONE)
    {
    snprintf(err_buf, sizeof(err_buf), "get_parent_client_socket failed: %d", rc);
    external_err_msg = err_buf;
    }
  else
    {
    snprintf(unix_sockname, sizeof(unix_sockname), "%s/%s", TRQAUTHD_SOCK_DIR, TRQAUTHD_SOCK_NAME);
    /* format is:
     * trq_system|trq_port|Validation_type|user|pid|psock|
     */
    mypid = getpid();
    sprintf(write_buf, "%d|%d|%s|%d|%d|%d|%s|%d|%d|", TRQ_AUTH_CONNECTION, (int)strlen(server_name), server_name, server_port, AUTH_TYPE_IFF, (int)strlen(pwent->pw_name), pwent->pw_name, mypid, parent_client_socket);
    /*
     * total_length|val
     */
    write_buf_len = strlen(write_buf);
    if ((local_socket = socket_get_unix()) <= 0)
      {
      external_err_msg = "qsub was unable to open a socket\n";
      rc = PBSE_SOCKET_FAULT;
      }
    else if ((rc = socket_connect_unix(local_socket, unix_sockname, &err_msg)) != PBSE_NONE)
      {
      external_err_msg = "qsub couldn't connect its socket to trqauthd: VERIFY THAT trqauthd IS RUNNING\n";
      }
    else if ((rc = socket_write(local_socket, write_buf, write_buf_len)) != write_buf_len)
      {
      rc = PBSE_SOCKET_WRITE;
      external_err_msg = "qsub couldn't write authentication information to trqauthd";
      }
    else if ((rc = socket_read_num(local_socket, &code)) != PBSE_NONE)
      {
      external_err_msg = "qsub couldn't read the size of information from trqauthd\n";
      }
    else if ((rc = socket_read_str(local_socket, &read_buf, &read_buf_len)) != PBSE_NONE)
      {
      external_err_msg = "qsub couldn't read the response from trqauthd\n";
      }
    else if ((rc = parse_daemon_response(code, read_buf_len, read_buf)) != PBSE_NONE)
      {
      snprintf(err_buf, sizeof(err_buf), "qsub received error code %lld ('%s') from trqauthd\n", code, pbse_to_txt(code));
      external_err_msg = err_buf;
      }
    else
      {
      if (getenv("PBSDEBUG"))
        {
        fprintf(stdout, "%s : Connection authorized (server socket %d)\n", __func__, parent_client_socket);
        }
      }

    if (local_socket >= 0)
      socket_close(local_socket);
    }

  if (rc != PBSE_NONE)
    {
    if (err_msg != NULL)
      {
      snprintf(err_buf, sizeof(err_buf), "Error in connection to trqauthd (%d)-[%s]\n", rc, err_msg);
      external_err_msg = err_buf;
      }
    }

  if (err_msg != NULL)
    free(err_msg);

  if (read_buf != NULL)
    free(read_buf);

  if (l_server != NULL)
    free(l_server);

  return(rc);
  }
Beispiel #2
0
int validate_socket(

  int psock)

  {
  int            rc = PBSE_NONE;
  static char    id[] = "validate_socket";
  char           tmp_buf[LOCAL_LOG_BUF];
  char           write_buf[1024];
  char          *read_buf = NULL;
  long long      read_buf_len = 0;
  uid_t          myrealuid;
  int            local_socket = 0;
  int            parent_client_socket = 0;
  struct passwd *pwent;
  char          *err_msg = NULL;
  char          *l_server = NULL;
  int            l_server_len = 0;
  unsigned short af_family;
  long long      code = -1;
  int            write_buf_len = 0;
  int            local_errno;

  myrealuid = getuid();

  if ((pwent = getpwuid(myrealuid)) == NULL)
    {
    snprintf(tmp_buf, LOCAL_LOG_BUF, "cannot get account info: uid %d, errno %d (%s)\n", (int)myrealuid, errno, strerror(errno));
    log_event(PBSEVENT_ADMIN,PBS_EVENTCLASS_SERVER,id,tmp_buf);
    }
  else if ((rc = get_hostaddr_hostent_af(&local_errno, AUTH_IP, &af_family, &l_server, &l_server_len)) != PBSE_NONE)
    {
    }
  else if ((rc = get_parent_client_socket(psock, &parent_client_socket)) != PBSE_NONE)
    {
    }
  else
    {
    /* format is:
     * trq_system|trq_port|Validation_type|user|psock|
     */
    sprintf(write_buf, "%d|%s|%d|%d|%d|%s|%d|", (int)strlen(server_name), server_name, server_port, AUTH_TYPE_IFF, (int)strlen(pwent->pw_name), pwent->pw_name, parent_client_socket);
    /*
     * total_length|val
     */
    write_buf_len = strlen(write_buf);
    if ((local_socket = socket_get_tcp()) <= 0)
      {
      fprintf(stderr, "socket_get_tcp error\n");
      rc = PBSE_SOCKET_FAULT;
      }
    else if ((rc = socket_connect(&local_socket, l_server, l_server_len, AUTH_PORT, AF_INET, 0, &err_msg)) != PBSE_NONE)
      {
      fprintf(stderr, "socket_connect error (VERIFY THAT trqauthd IS RUNNING)\n");
      }
    else if ((rc = socket_write(local_socket, write_buf, write_buf_len)) != write_buf_len)
      {
      rc = PBSE_SOCKET_WRITE;
      fprintf(stderr, "socket_write error\n");
      }
    else if ((rc = socket_read_num(local_socket, &code)) != PBSE_NONE)
      {
      fprintf(stderr, "socket_read_num error\n");
      }
    else if ((rc = socket_read_str(local_socket, &read_buf, &read_buf_len)) != PBSE_NONE)
      {
      fprintf(stderr, "socket_read_str error\n");
      }
    else if ((rc = parse_daemon_response(code, read_buf_len, read_buf)) != PBSE_NONE)
      {
      fprintf(stderr, "parse_daemon_response error\n");
      }
    else
      {
      if (getenv("PBSDEBUG"))
        {
        fprintf(stderr, "%s : Connection authorized (server socket %d)\n", id, parent_client_socket);
        }
      socket_close(local_socket);
      }
    }
  if (rc != PBSE_NONE)
    {
    if (err_msg != NULL)
      {
      fprintf(stderr, "Error in connection to trqauthd (%d)-[%s]\n", rc, err_msg);
      }
    }
  if (err_msg != NULL)
    free(err_msg);
  if (read_buf != NULL)
    free(read_buf);
  return rc;
  }