blob: 80ab22f5093e7b27182730bb67baa70449e390c2 [file] [log] [blame]
Radek Krejciac6d3472015-10-22 15:47:18 +02001/**
2 * \file session_ssh.c
3 * \author Radek Krejci <rkrejci@cesnet.cz>
4 * \brief libnetconf2 - SSH specific session transport functions
5 *
6 * This source is compiled only with libssh.
7 *
8 * Copyright (c) 2015 CESNET, z.s.p.o.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of the Company nor the names of its contributors
20 * may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
22 *
23 */
24
25#include <assert.h>
26#include <errno.h>
27#include <pwd.h>
28#include <sys/types.h>
29#include <string.h>
30#include <unistd.h>
31
32#include <libyang/libyang.h>
33
34#include "libnetconf.h"
35
36/* seconds */
37#define SSH_TIMEOUT 10
38
39/* internal functions from session.c */
40struct nc_session *connect_init(struct ly_ctx *ctx);
41int connect_getsocket(const char* host, unsigned short port);
42int handshake(struct nc_session *session);
43
44static int
45connect_ssh_socket(struct nc_session *session, int sock)
46{
47 const int timeout = SSH_TIMEOUT;
48
49 if (sock == -1) {
50 return 1;
51 }
52
53 session->ti_type = NC_TI_LIBSSH;
54 session->ti.libssh.session = ssh_new();
55 if (!session->ti.libssh.session) {
56 ERR("Unable to initialize SSH session.");
57 return 1;
58 }
59
60 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, session->host);
61 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, session->username);
62 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
63 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout);
64
65 /* TODO - libssh magic with authentication and all other stuff */
66
67 return 0;
68}
69
70API struct nc_session *
71nc_connect_ssh(const char *host, unsigned short port, const char* username, struct ly_ctx *ctx)
72{
73 struct passwd *pw;
74 struct nc_session *session = NULL;
75
76 /* process parameters */
77 if (!host || strisempty(host)) {
78 host = "localhost";
79 }
80
81 if (!port) {
82 port = NC_PORT_SSH;
83 }
84
85 if (!username) {
86 pw = getpwuid(getuid());
87 if (!pw) {
88 ERR("Unknwon username for the SSH connection (%s).", strerror(errno));
89 return (NULL);
90 } else {
91 username = pw->pw_name;
92 }
93 }
94
95 /* prepare session structure */
96 session = connect_init(ctx);
97 if (!session) {
98 return NULL;
99 }
100
101 /* transport specific data */
102 session->username = lydict_insert(session->ctx, username, 0);
103 session->host = lydict_insert(session->ctx, host, 0);
104 session->port = port;
105
106 if (connect_ssh_socket(session, connect_getsocket(host, port))) {
107 goto error;
108 }
109
110 /* NETCONF handshake */
111 if (handshake(session)) {
112 goto error;
113 }
114
115 session->status = NC_STATUS_RUNNING;
116 return session;
117
118error:
119 nc_session_free(session);
120 return NULL;
121}
122
123API struct nc_session *
124nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx)
125{
126 (void) ssh_session;
127 (void) ctx;
128
129 return NULL;
130}
131
132API struct nc_session *
133nc_connect_ssh_channel(struct nc_session *session)
134{
135 (void) session;
136
137 return NULL;
138}