authenticate on SSH via BSD socket
I'd like to authenticate on ssh-server via BSD socket. I know how to initiate a connection but don't know how to actually authenticate. Thanks for your time when pointing me to the right direction.
Here is the source code:
//
#include <stdio.h> // printf()
#include <开发者_运维技巧sys/types.h> // socket data types
#include <sys/socket.h> // socket(), connect(), send(), recv()
#include <arpa/inet.h> // sockaddr_in, inet_addr()
#include <stdlib.h> // free()
#include <unistd.h> // close()
int *ssh(char *host, int port, char *user, char *pass);
int main(void)
{
// create socket
int *ssh_socket = ssh("127.0.0.1", 22, "root", "password");
// close and free
close(*ssh_socket);
free(ssh_socket);
return 0;
}
int *ssh(char *host, int port, char *user, char *pass)
{
int *sock = calloc(sizeof(int), 1);
struct sockaddr_in addr = {.sin_family=AF_INET, \
.sin_port=htons(port), \
.sin_addr.s_addr=inet_addr(host)};
*sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // create socket
connect(*sock, (struct sockaddr *)&addr, sizeof(addr)); // init connection
// here is the problem
// how do I authenticate on this socket?
return sock;
}
Use libssh for adding SSH functionality to your program.
SSH is a quite complex protocol with several layers.
Before getting to the user authentication you have to initiate the protocol, check remote host credentials and start an encrypted connection.
And after that, there are several ways to authenticate an user you may want to support (public key, passwd, keyboard-interactive, etc.).
The Wikipedia page for SSH has links to all the related RFCs.
Really, use libssh or libssh2 or the code from OpenSSH!
精彩评论