Simple program for finding and reading remote videos with libssh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

138 lines
3.2 KiB

#include <libssh/libssh.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int verify_host(ssh_session session) {
enum ssh_known_hosts_e state;
state = ssh_session_is_known_server(session);
// only handling known hosts for now
// will implement other responses later.
switch (state) {
case SSH_KNOWN_HOSTS_OK:
break;
default:
return -1;
}
return 0;
}
int get_movies(ssh_session session, char **buffer, size_t size) {
ssh_channel channel;
int rc;
char read_buffer[256];
int nbytes;
size_t len = 0;
char *temp;
channel = ssh_channel_new(session);
if (channel == NULL)
return SSH_ERROR;
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK) {
fprintf(stderr, "Failed to open channel");
ssh_channel_free(channel);
return rc;
}
rc = ssh_channel_request_exec(
channel, "find /var/www/public/media -type f -name \"*mp4\"");
if (rc != SSH_OK) {
fprintf(stderr, "Failed to request exec");
ssh_channel_close(channel);
ssh_channel_free(channel);
return rc;
}
nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0);
while (nbytes > 0) {
if ((len += nbytes) > size) {
size = len;
temp = realloc(*buffer, len);
if (!temp) {
fprintf(stderr, "Failed to reallocate char buffer");
ssh_channel_close(channel);
ssh_channel_free(channel);
return -1;
}
*buffer = temp;
}
memcpy(*buffer + len - nbytes, read_buffer, nbytes);
nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0);
if (nbytes < 0) {
fprintf(stderr, "Failed to read bytes from channel");
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
}
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_OK;
}
int main(int argc, char *argv[]) {
char *host = "localhost";
char *user = getlogin();
int opt;
int rc;
while ((opt = getopt(argc, argv, "h:u:")) != -1)
switch (opt) {
case 'h':
host = optarg;
break;
case 'u':
user = optarg;
break;
case '?':
printf("Unknown option\n");
break;
}
ssh_session session = ssh_new();
if (session == NULL) {
fprintf(stderr, "Failed to get new ssh_session");
exit(-1);
}
ssh_options_set(session, SSH_OPTIONS_HOST, host);
ssh_options_set(session, SSH_OPTIONS_USER, user);
if (ssh_connect(session) != SSH_OK) {
fprintf(stderr, "Unable to connect to host: %s\n", ssh_get_error(session));
exit(-1);
}
if (verify_host(session) != 0) {
fprintf(stderr, "Attempted to connect to unknown host\n");
ssh_disconnect(session);
ssh_free(session);
exit(-1);
}
rc = ssh_userauth_publickey_auto(session, NULL, NULL);
if (rc == SSH_AUTH_ERROR) {
fprintf(stderr, "Authentication failed: %s\n", ssh_get_error(session));
exit(-1);
}
int size = 16;
char *b = malloc(size);
memset(b, 0, size);
if (get_movies(session, &b, size) != SSH_OK) {
fprintf(stderr, "Unable to list movies: %s\n", ssh_get_error(session));
exit(-1);
}
ssh_disconnect(session);
ssh_free(session);
}