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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
rangitaki-sync/rsl-download-test.c

134 lines
3.1 KiB

/*
* Rangitaki Sync Library
*
* Download Test / Example
*
* A program for downloading and uploading files over ssh.
* Written for the Rangitaki blogging engine.
*
* Proudly written in C and with use of libssh (libssh.org)
*
* Version: 0.1
*
* COPYRIGHT (c) 2015 - 2016 The Rangitaki Project
* COPYRIGHT (c) 2015 - 2016 Marcel Kapfer (mmk2410)
* <marcelmichaelkapfer@yahoo.co.nz>
*
* MIT License
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libssh/libssh.h>
#include "rangitaki-sync.h"
#include "dbg.h"
#define MAX_DATA 512
int run(const char *host, const char *user, const char *password,
const char *remote_dir, const int port)
{
// Print the ssh struct
ssh_data *data = malloc(sizeof(ssh_data));
ssh_session ssh;
ssh_scp scp;
int rc;
ssh_initialize(host, user, password,
remote_dir, "/tmp/rangitaki-sync/", port, data);
mkdir(data->local_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
ssh = ssh_open(data);
check(ssh != NULL, "Error while connecting to the server.");
scp = scp_open(ssh, data, SSH_SCP_READ | SSH_SCP_RECURSIVE);
check(scp != NULL, "Error while creating a scp connection");
rc = scp_download(ssh, scp, data);
check(rc == 0, "Error while downloading");
scp_close(scp);
ssh_close(ssh);
free(data->host);
free(data->user);
free(data->password);
free(data->remote_dir);
free(data->local_dir);
free(data);
return 0;
error:
if(scp) scp_close(scp);
if(ssh) ssh_close(ssh);
free(data->host);
free(data->user);
free(data->password);
free(data->remote_dir);
free(data->local_dir);
free(data);
return -1;
}
int main(int argc, char *argv[])
{
check(argc == 1, "Too much arguments");
printf("RANGITAKI SYNC LIBRARY\n");
printf("Version: 0.1\n");
printf("COPYRIGHT (c) 2015 - 2016 The Rangitaki Project\n");
printf("COPYRIGHT (c) 2015 - 2016 Marcel Kapfer (mmk2410)"
" <marcelmichaelkapfer@yahoo.co.nz>\n");
printf("MIT License\n\n");
printf("RSL Download test\n\n");
char *in = NULL;
int rc = 0;
char host[MAX_DATA];
char user[MAX_DATA];
char password[MAX_DATA];
char remote_dir[MAX_DATA];
int port;
printf("Host: ");
in = fgets(host, MAX_DATA - 1, stdin);
strtok(host, "\n");
check(in != NULL, "Failed to read host.");
printf("User: ");
in = fgets(user, MAX_DATA - 1, stdin);
strtok(user, "\n");
check(in != NULL, "Failed to read host.");
printf("Password: ");
in = fgets(password, MAX_DATA - 1, stdin);
strtok(password, "\n");
check(in != NULL, "Failed to read password.");
printf("Remote Directory: ");
in = fgets(remote_dir, MAX_DATA - 1, stdin);
strtok(remote_dir, "\n");
check(in != NULL, "Failed to read directory.");
printf("Port: ");
rc = fscanf(stdin, "%i", &port);
check(rc > 0, "Failed to read port.");
printf("\nRunning Download now.\n\n");
run(host, user, password, remote_dir, port);
printf("\nFile downloaded to /tmp/rangitaki-sync.\n");
return 0;
error:
return -1;
}