int parse_request_client( int sock, char **server_name, int *server_port, int *auth_type, char **user, int *user_sock) { int rc = PBSE_NONE; long long tmp_val = 0, tmp_port = 0, tmp_auth_type = 0, tmp_sock = 0; if ((rc = socket_read_str(sock, server_name, &tmp_val)) != PBSE_NONE) { } else if ((rc = socket_read_num(sock, &tmp_port)) != PBSE_NONE) { } else if ((rc = socket_read_num(sock, &tmp_auth_type)) != PBSE_NONE) { } else if ((rc = socket_read_str(sock, user, &tmp_val)) != PBSE_NONE) { } else if ((rc = socket_read_num(sock, &tmp_sock)) != PBSE_NONE) { } else { *server_port = (int)tmp_port; *auth_type = (int)tmp_auth_type; *user_sock = (int)tmp_sock; } return rc; }
int parse_terminate_request( int sock, char **username, int *pid) { int rc = PBSE_NONE; long long tmp_val; long long tmp_pid; if ((rc = socket_read_str(sock, username, &tmp_val)) != PBSE_NONE) { } else if ((rc = socket_read_num(sock, &tmp_pid)) != PBSE_NONE) { } else { *pid = tmp_pid; } return(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 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; }
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() */
int terminate_trqauthd() { int rc = PBSE_NONE; int sock = -1; char write_buf[MAX_LINE]; char *read_buf = NULL; char log_buf[MAX_BUF]; long long read_buf_len = MAX_LINE; long long ret_code; uid_t myrealuid; pid_t mypid; struct passwd *pwent; myrealuid = getuid(); pwent = getpwuid(myrealuid); if (pwent == NULL) { snprintf(log_buf, MAX_BUF, "cannot get account info: uid %d, errno %d (%s)\n", (int)myrealuid, errno, strerror(errno)); log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_SERVER, __func__, log_buf); return(PBSE_SYSTEM); } mypid = getpid(); sprintf(write_buf, "%d|%d|%s|%d|", TRQ_DOWN_TRQAUTHD, (int )strlen(pwent->pw_name), pwent->pw_name, mypid); if((rc = connect_to_trqauthd(&sock)) != PBSE_NONE) { fprintf(stderr, "Could not connect to trqauthd. trqauthd may already be down\n"); } else if ((rc = socket_write(sock, write_buf, strlen(write_buf))) < 0) { fprintf(stderr, "Failed to send termnation request to trqauthd: %d\n", rc); } else if ((rc = socket_read_num(sock, &ret_code)) != PBSE_NONE) { fprintf(stderr, "trqauthd did not give proper response. Check to see if traquthd has terminated: %d\n", rc); } else if (ret_code != PBSE_NONE) { rc = socket_read_str(sock, &read_buf, &read_buf_len); fprintf(stderr, "trqauthd not shutdown. %s\n", read_buf); } else if ((rc = socket_read_str(sock, &read_buf, &read_buf_len)) != PBSE_NONE) { fprintf(stderr, "trqauthd did not respond. Check to see if trqauthd has terminated: %d\n", rc); } else if( (rc = connect_to_trqauthd(&sock)) != PBSE_NONE) /* We do this because the accept loop on trqauthd is still waiting for a command before it realizes it is terminated */ { fprintf(stdout, "\ntrqauthd has been terminated\n"); } else { fprintf(stdout, "\ntrqauthd has been terminated\n"); } if (sock != -1) close(sock); if (read_buf != NULL) { free(read_buf); } return(rc); }