END_TEST START_TEST(test_socket_get_unix) { int rc; socket_success = true; close_success = true; connect_success = true; rc = socket_get_unix(); fail_unless(rc >= 0, "socket_get_unix failed", rc); socket_success = false; rc = socket_get_unix(); fail_unless(rc < 0, "socket_get_unix failed", rc); }
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); }
int get_active_pbs_server( char **active_server, int *port) { char *err_msg = NULL; char *current_server = NULL; char unix_sockname[MAXPATHLEN + 1]; char write_buf[MAX_LINE]; int write_buf_len; char *read_buf = NULL; long long read_buf_len = MAX_LINE; int local_socket; int rc = PBSE_NONE; long long ret_code = PBSE_NONE; char *timeout_ptr; if ((timeout_ptr = getenv("PBSAPITIMEOUT")) != NULL) { time_t tmp_timeout = strtol(timeout_ptr, NULL, 0); if (tmp_timeout > 0) { pbs_tcp_timeout = tmp_timeout; } } /* the syntax for this call is a number followed by a | (pipe). The pipe indicates the end of a number */ sprintf(write_buf, "%d|", TRQ_GET_ACTIVE_SERVER); write_buf_len = strlen(write_buf); snprintf(unix_sockname, sizeof(unix_sockname), "%s/%s", TRQAUTHD_SOCK_DIR, TRQAUTHD_SOCK_NAME); local_socket = socket_get_unix(); if (local_socket < 0) return(local_socket * -1); /* socket_get_unix returns a negative PBSE error on failure. make it positive */ rc = socket_connect_unix(local_socket, unix_sockname, &err_msg); if (err_msg != NULL) free(err_msg); if (rc != PBSE_NONE) { close(local_socket); fprintf(stderr, "socket_connect_unix failed: %d\n", rc); return(rc); } rc = socket_write(local_socket, write_buf, write_buf_len); if (rc <= 0 ) { close(local_socket); fprintf(stderr, "socket_write failed: %d\n", rc); return(PBSE_SYSTEM); } /* get the server name */ if ((rc = socket_read_num(local_socket, &ret_code)) != PBSE_NONE) { close(local_socket); return(rc); } else if ((rc = socket_read_str(local_socket, &read_buf, &read_buf_len)) != PBSE_NONE) { if (read_buf != NULL) free(read_buf); close(local_socket); return(rc); } else if ((rc = socket_read_num(local_socket, (long long *)port)) != PBSE_NONE) { if (read_buf != NULL) free(read_buf); close(local_socket); return(rc); } close(local_socket); if (read_buf_len == 0) { if (read_buf != NULL) free(read_buf); return(PBSE_SOCKET_READ); } // read_buf has been allocated in socket_read_str() current_server = read_buf; *active_server = current_server; return(rc); } /* END get_active_pbs_server() */
int validate_active_pbs_server( char **active_server) { char *err_msg = NULL; char unix_sockname[MAXPATHLEN + 1]; char write_buf[MAX_LINE]; int write_buf_len; char *read_buf = NULL; long long read_buf_len = MAX_LINE; int local_socket; int rc; long long ret_code; /* the format is TRQ command */ sprintf(write_buf, "%d|", TRQ_VALIDATE_ACTIVE_SERVER); write_buf_len = strlen(write_buf); snprintf(unix_sockname, sizeof(unix_sockname), "%s/%s", TRQAUTHD_SOCK_DIR, TRQAUTHD_SOCK_NAME); local_socket = socket_get_unix(); if (local_socket < 0) { fprintf(stderr, "could not allocate unix domain socket: %d\n", local_socket); return(local_socket * -1); /*socket_get_unix returns a negative PBSE error value Change it back */ } rc = socket_connect_unix(local_socket, unix_sockname, &err_msg); if (err_msg != NULL) free(err_msg); if (rc != PBSE_NONE) { close(local_socket); fprintf(stderr, "socket_connect_unix failed: %d\n", rc); return(rc); } rc = socket_write(local_socket, write_buf, write_buf_len); if (rc <= 0 ) { close(local_socket); fprintf(stderr, "socket_write failed: %d\n", rc); return(PBSE_SYSTEM); } rc = socket_read_num(local_socket, &ret_code); if (rc != PBSE_NONE) { close(local_socket); return(rc); } rc = socket_read_str(local_socket, &read_buf, &read_buf_len); close(local_socket); if ((rc == PBSE_NONE) && (read_buf_len != 0)) { *active_server = read_buf; } else { if (read_buf != NULL) free(read_buf); if (rc != PBSE_NONE) return(rc); if (read_buf_len == 0) return(PBSE_SOCKET_READ); } return(rc); } /* END validate_active_pbs_server() */