Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int ssh_create_session(Message *message, int new_id) {
  2.  
  3.     unsigned long host_addr;
  4.     char host_ip[IP_LENGTH];
  5.     struct sockaddr_in sin;
  6.     int rc;
  7.     char *password = "";
  8.     char *username = getenv("USER");
  9.  
  10.     hostname_to_ip(message->host_name, host_ip);
  11.  
  12.     host_addr = inet_addr(host_ip);
  13.     int sock = socket(AF_INET, SOCK_STREAM, 0);
  14.     player_ssh_sessions[new_id].sock = sock;
  15.  
  16.     sin.sin_family = AF_INET;
  17.     sin.sin_port = htons(22);
  18.     sin.sin_addr.s_addr = host_addr;
  19.     if (connect(sock, (struct sockaddr*)(&sin),
  20.                 sizeof(struct sockaddr_in)) != 0) {
  21.         fprintf(stderr, "Failed to connect!\n");
  22.         return 0;
  23.     }
  24.  
  25.     if (!(player_ssh_sessions[new_id].session = libssh2_session_init()))
  26.         return 0;
  27.  
  28.     libssh2_session_set_blocking(player_ssh_sessions[new_id].session, 0);
  29.     // łączenie się z ssh_player_sessions[new_id];
  30.  
  31.  
  32.     while ((rc = libssh2_session_handshake(player_ssh_sessions[new_id].session,
  33.                                            sock)) == LIBSSH2_ERROR_EAGAIN);
  34.  
  35.     if (rc) {
  36.         fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  37.         return 0;
  38.     }
  39.  
  40.     if (strlen(password) != 0) {
  41.         // We could authenticate via password
  42.         while ((rc = libssh2_userauth_password(player_ssh_sessions[new_id].session,
  43.                                                username, password)) == LIBSSH2_ERROR_EAGAIN);
  44.         if (rc) {
  45.             fprintf(stderr, "Authentication by password failed.\n");
  46.             return 0;
  47.         }
  48.     }
  49.     else {
  50.         char path_pub[MAX_COMMAND_SIZE];
  51.         char home_path[MAX_COMMAND_SIZE];
  52.         memset(&path_pub, 0, sizeof(path_pub));
  53.         char *home = getenv("HOME");
  54.         strcpy(home_path, home);
  55.         strcat(path_pub, home_path);
  56.         strcat(path_pub, "/.ssh/id_rsa.pub");
  57.         char path[MAX_COMMAND_SIZE];
  58.         memset(&path, 0, sizeof(path));
  59.         strcat(path, home_path);
  60.         strcat(path, "/.ssh/id_rsa");
  61.         // Or by public key
  62.         while ((rc = libssh2_userauth_publickey_fromfile(player_ssh_sessions[new_id].session,
  63.                                                          username, path_pub, path,
  64.                                                          NULL)) == LIBSSH2_ERROR_EAGAIN);
  65.  
  66.         if (rc) {
  67.             fprintf(stderr, "\tAuthentication by public key failed\n");
  68.             return 0;
  69.         }
  70.     }
  71.  
  72.     /* Exec non-blocking on the remove host */
  73.     while((player_ssh_sessions[new_id].channel = libssh2_channel_open_session(player_ssh_sessions[new_id].session)) == NULL &&
  74.           libssh2_session_last_error(player_ssh_sessions[new_id].session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN )
  75.     {
  76.         waitsocket(sock, player_ssh_sessions[new_id].session);
  77.     }
  78.     if (player_ssh_sessions[new_id].channel == NULL)
  79.     {
  80.         fprintf(stderr,"Error in ssh create_session!\n");
  81.         return 0;
  82.     }
  83.  
  84.     return 1;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement