Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ssh_create_session(Message *message, int new_id) {
- unsigned long host_addr;
- char host_ip[IP_LENGTH];
- struct sockaddr_in sin;
- int rc;
- char *password = "";
- char *username = getenv("USER");
- hostname_to_ip(message->host_name, host_ip);
- host_addr = inet_addr(host_ip);
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- player_ssh_sessions[new_id].sock = sock;
- sin.sin_family = AF_INET;
- sin.sin_port = htons(22);
- sin.sin_addr.s_addr = host_addr;
- if (connect(sock, (struct sockaddr*)(&sin),
- sizeof(struct sockaddr_in)) != 0) {
- fprintf(stderr, "Failed to connect!\n");
- return 0;
- }
- if (!(player_ssh_sessions[new_id].session = libssh2_session_init()))
- return 0;
- libssh2_session_set_blocking(player_ssh_sessions[new_id].session, 0);
- // łączenie się z ssh_player_sessions[new_id];
- while ((rc = libssh2_session_handshake(player_ssh_sessions[new_id].session,
- sock)) == LIBSSH2_ERROR_EAGAIN);
- if (rc) {
- fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
- return 0;
- }
- if (strlen(password) != 0) {
- // We could authenticate via password
- while ((rc = libssh2_userauth_password(player_ssh_sessions[new_id].session,
- username, password)) == LIBSSH2_ERROR_EAGAIN);
- if (rc) {
- fprintf(stderr, "Authentication by password failed.\n");
- return 0;
- }
- }
- else {
- char path_pub[MAX_COMMAND_SIZE];
- char home_path[MAX_COMMAND_SIZE];
- memset(&path_pub, 0, sizeof(path_pub));
- char *home = getenv("HOME");
- strcpy(home_path, home);
- strcat(path_pub, home_path);
- strcat(path_pub, "/.ssh/id_rsa.pub");
- char path[MAX_COMMAND_SIZE];
- memset(&path, 0, sizeof(path));
- strcat(path, home_path);
- strcat(path, "/.ssh/id_rsa");
- // Or by public key
- while ((rc = libssh2_userauth_publickey_fromfile(player_ssh_sessions[new_id].session,
- username, path_pub, path,
- NULL)) == LIBSSH2_ERROR_EAGAIN);
- if (rc) {
- fprintf(stderr, "\tAuthentication by public key failed\n");
- return 0;
- }
- }
- /* Exec non-blocking on the remove host */
- while((player_ssh_sessions[new_id].channel = libssh2_channel_open_session(player_ssh_sessions[new_id].session)) == NULL &&
- libssh2_session_last_error(player_ssh_sessions[new_id].session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN )
- {
- waitsocket(sock, player_ssh_sessions[new_id].session);
- }
- if (player_ssh_sessions[new_id].channel == NULL)
- {
- fprintf(stderr,"Error in ssh create_session!\n");
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement