Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1 | /*! |
Michal Vasko | 30aa382 | 2015-11-25 10:41:54 +0100 | [diff] [blame] | 2 | * \file netopeerguid.c |
| 3 | * \brief NetopeerGUI daemon |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 4 | * \author Tomas Cejka <cejkat@cesnet.cz> |
| 5 | * \author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 6 | * \author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 7 | * \date 2011 |
| 8 | * \date 2012 |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 9 | * \date 2013 |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 10 | * \date 2015 |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 11 | */ |
| 12 | /* |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 13 | * Copyright (C) 2011-2015 CESNET |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 14 | * |
| 15 | * LICENSE TERMS |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in |
| 24 | * the documentation and/or other materials provided with the |
| 25 | * distribution. |
| 26 | * 3. Neither the name of the Company nor the names of its contributors |
| 27 | * may be used to endorse or promote products derived from this |
| 28 | * software without specific prior written permission. |
| 29 | * |
| 30 | * ALTERNATIVELY, provided that this notice is retained in full, this |
| 31 | * product may be distributed under the terms of the GNU General Public |
| 32 | * License (GPL) version 2 or later, in which case the provisions |
| 33 | * of the GPL apply INSTEAD OF those given above. |
| 34 | * |
| 35 | * This software is provided ``as is'', and any express or implied |
| 36 | * warranties, including, but not limited to, the implied warranties of |
| 37 | * merchantability and fitness for a particular purpose are disclaimed. |
| 38 | * In no event shall the company or contributors be liable for any |
| 39 | * direct, indirect, incidental, special, exemplary, or consequential |
| 40 | * damages (including, but not limited to, procurement of substitute |
| 41 | * goods or services; loss of use, data, or profits; or business |
| 42 | * interruption) however caused and on any theory of liability, whether |
| 43 | * in contract, strict liability, or tort (including negligence or |
| 44 | * otherwise) arising in any way out of the use of this software, even |
| 45 | * if advised of the possibility of such damage. |
| 46 | * |
| 47 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 48 | #define _GNU_SOURCE |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 49 | |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 50 | #include <unistd.h> |
| 51 | #include <poll.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 52 | #include <time.h> |
Michal Vasko | b69dde2 | 2016-03-09 11:42:20 +0100 | [diff] [blame] | 53 | #include <fcntl.h> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 54 | #include <sys/types.h> |
| 55 | #include <sys/socket.h> |
| 56 | #include <sys/un.h> |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 57 | #include <sys/stat.h> |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 58 | #include <pwd.h> |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 59 | #include <syslog.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 60 | #include <errno.h> |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 61 | #include <limits.h> |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 62 | #include <grp.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 63 | #include <signal.h> |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 64 | #include <pthread.h> |
| 65 | #include <ctype.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 66 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 67 | #include <nc_client.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 68 | |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 69 | #include "../config.h" |
| 70 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 71 | #ifdef WITH_NOTIFICATIONS |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 72 | #include "notification_server.h" |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 73 | #endif |
| 74 | |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 75 | #include "message_type.h" |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 76 | #include "netopeerguid.h" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 77 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 78 | #define SCHEMA_DIR "/tmp/yang_models" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 79 | #define MAX_PROCS 5 |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 80 | #define SOCKET_FILENAME "/var/run/netopeerguid.sock" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 81 | #define MAX_SOCKET_CL 10 |
| 82 | #define BUFFER_SIZE 4096 |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 83 | #define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */ |
| 84 | #define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 85 | |
Michal Vasko | 7732dee | 2015-11-05 10:22:15 +0100 | [diff] [blame] | 86 | /* sleep in master process for non-blocking socket reading, in msec */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 87 | #define SLEEP_TIME 200 |
| 88 | |
| 89 | #ifndef offsetof |
| 90 | #define offsetof(type, member) ((size_t) ((type *) 0)->member) |
| 91 | #endif |
| 92 | |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 93 | /* timeout in msec */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 94 | struct timeval timeout = { 1, 0 }; |
| 95 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 96 | #define NCWITHDEFAULTS NCWD_MODE_NOTSET |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 97 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 98 | #define MSG_OK 0 |
| 99 | #define MSG_OPEN 1 |
| 100 | #define MSG_DATA 2 |
| 101 | #define MSG_CLOSE 3 |
| 102 | #define MSG_ERROR 4 |
| 103 | #define MSG_UNKNOWN 5 |
| 104 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 105 | pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */ |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 106 | pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 107 | pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */ |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 108 | pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */ |
| 109 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 110 | unsigned int session_key_generator = 1; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 111 | struct session_with_mutex *netconf_sessions_list = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 112 | static const char *sockname; |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 113 | static pthread_key_t notif_history_key; |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 114 | pthread_key_t err_reply_key; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 115 | volatile int isterminated = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 116 | static char* password; |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 117 | int daemonize; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 118 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 119 | json_object *create_ok_reply(void); |
| 120 | json_object *create_data_reply(const char *data); |
| 121 | static char *netconf_getschema(unsigned int session_key, const char *identifier, const char *version, |
| 122 | const char *format, json_object **err); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 123 | static void node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module, |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 124 | json_object *data_json_parent); |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 125 | static void node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 126 | |
| 127 | static void |
| 128 | signal_handler(int sign) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 129 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 130 | switch (sign) { |
| 131 | case SIGINT: |
| 132 | case SIGTERM: |
| 133 | isterminated = 1; |
| 134 | break; |
| 135 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 138 | int |
| 139 | netconf_callback_ssh_hostkey_check(const char* UNUSED(hostname), ssh_session UNUSED(session)) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 140 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 141 | /* always approve */ |
| 142 | return (EXIT_SUCCESS); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 145 | char * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 146 | netconf_callback_sshauth_passphrase(const char *UNUSED(priv_key_file)) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 147 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 148 | char *buf; |
| 149 | buf = strdup(password); |
| 150 | return (buf); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 153 | char * |
| 154 | netconf_callback_sshauth_password(const char *UNUSED(username), const char *UNUSED(hostname)) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 155 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 156 | char *buf; |
| 157 | buf = strdup(password); |
| 158 | return (buf); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 161 | char * |
| 162 | netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction), |
| 163 | const char *UNUSED(prompt), int UNUSED(echo)) |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 164 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 165 | char *buf; |
| 166 | buf = strdup(password); |
| 167 | return (buf); |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 170 | void |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 171 | netconf_callback_error_process(const char *message) |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 172 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 173 | json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); |
| 174 | if (err_reply_p == NULL) { |
| 175 | ERROR("Error message was not allocated. %s", __func__); |
| 176 | return; |
| 177 | } |
| 178 | json_object *err_reply = *err_reply_p; |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 179 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 180 | json_object *array = NULL; |
| 181 | if (err_reply == NULL) { |
| 182 | ERROR("error calback: empty error list"); |
| 183 | pthread_mutex_lock(&json_lock); |
| 184 | err_reply = json_object_new_object(); |
| 185 | array = json_object_new_array(); |
| 186 | json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR)); |
| 187 | json_object_object_add(err_reply, "errors", array); |
| 188 | if (message != NULL) { |
| 189 | json_object_array_add(array, json_object_new_string(message)); |
| 190 | } |
| 191 | pthread_mutex_unlock(&json_lock); |
| 192 | (*err_reply_p) = err_reply; |
| 193 | } else { |
| 194 | ERROR("error calback: nonempty error list"); |
| 195 | pthread_mutex_lock(&json_lock); |
| 196 | if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) { |
| 197 | if (message != NULL) { |
| 198 | json_object_array_add(array, json_object_new_string(message)); |
| 199 | } |
| 200 | } |
| 201 | pthread_mutex_unlock(&json_lock); |
| 202 | } |
| 203 | pthread_setspecific(err_reply_key, err_reply_p); |
| 204 | return; |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 207 | /** |
| 208 | * should be used in locked area |
| 209 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 210 | void |
| 211 | prepare_status_message(struct session_with_mutex *s, struct nc_session *session) |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 212 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 213 | int i; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 214 | json_object *json_obj = NULL; |
| 215 | json_object *js_tmp = NULL; |
| 216 | char *old_sid = NULL; |
| 217 | const char *j_old_sid = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 218 | char str_port[6]; |
| 219 | const char **cpblts; |
| 220 | struct lyd_node *yanglib, *module, *node; |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 221 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 222 | if (s == NULL) { |
| 223 | ERROR("No session given."); |
| 224 | return; |
| 225 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 226 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 227 | pthread_mutex_lock(&json_lock); |
| 228 | if (s->hello_message != NULL) { |
| 229 | ERROR("clean previous hello message"); |
| 230 | if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) { |
| 231 | j_old_sid = json_object_get_string(js_tmp); |
| 232 | if (j_old_sid != NULL) { |
| 233 | old_sid = strdup(j_old_sid); |
| 234 | } |
| 235 | } |
| 236 | json_object_put(s->hello_message); |
| 237 | s->hello_message = NULL; |
| 238 | } |
| 239 | s->hello_message = json_object_new_object(); |
| 240 | if (session != NULL) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 241 | if (!old_sid) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 242 | /* we don't have old sid */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 243 | asprintf(&old_sid, "%u", nc_session_get_id(session)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 244 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 245 | json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid)); |
| 246 | free(old_sid); |
| 247 | old_sid = NULL; |
| 248 | |
| 249 | json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) ? "1.1":"1.0"))); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 250 | json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session))); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 251 | sprintf(str_port, "%u", nc_session_get_port(session)); |
| 252 | json_object_object_add(s->hello_message, "port", json_object_new_string(str_port)); |
| 253 | json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_username(session))); |
| 254 | cpblts = nc_session_get_cpblts(session); |
| 255 | if (cpblts) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 256 | json_obj = json_object_new_array(); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 257 | for (i = 0; cpblts[i]; ++i) { |
| 258 | json_object_array_add(json_obj, json_object_new_string(cpblts[i])); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 259 | } |
| 260 | json_object_object_add(s->hello_message, "capabilities", json_obj); |
| 261 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 262 | |
| 263 | yanglib = ly_ctx_info(nc_session_get_ctx(session)); |
| 264 | if (yanglib) { |
| 265 | json_obj = json_object_new_array(); |
| 266 | LY_TREE_FOR(yanglib->child, module) { |
| 267 | if (!strcmp(module->schema->name, "module")) { |
| 268 | LY_TREE_FOR(module->child, node) { |
| 269 | if (!strcmp(node->schema->name, "name")) { |
| 270 | json_object_array_add(json_obj, json_object_new_string(((struct lyd_node_leaf_list *)node)->value_str)); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | json_object_object_add(s->hello_message, "models", json_obj); |
| 277 | |
Michal Vasko | c04900a | 2016-03-15 16:20:24 +0100 | [diff] [blame] | 278 | lyd_free(yanglib); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 281 | DEBUG("%s", json_object_to_json_string(s->hello_message)); |
| 282 | } else { |
| 283 | ERROR("Session was not given."); |
| 284 | json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR)); |
| 285 | json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier.")); |
| 286 | } |
| 287 | DEBUG("Status info from hello message prepared"); |
| 288 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 291 | void |
| 292 | create_err_reply_p() |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 293 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 294 | json_object **err_reply = calloc(1, sizeof(json_object **)); |
| 295 | if (err_reply == NULL) { |
| 296 | ERROR("Allocation of err_reply storage failed!"); |
| 297 | return; |
| 298 | } |
| 299 | if (pthread_setspecific(err_reply_key, err_reply) != 0) { |
| 300 | ERROR("cannot set thread-specific value."); |
| 301 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 304 | void |
| 305 | clean_err_reply() |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 306 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 307 | json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key); |
| 308 | if (err_reply != NULL) { |
| 309 | if (*err_reply != NULL) { |
| 310 | pthread_mutex_lock(&json_lock); |
| 311 | json_object_put(*err_reply); |
| 312 | pthread_mutex_unlock(&json_lock); |
| 313 | } |
| 314 | if (pthread_setspecific(err_reply_key, err_reply) != 0) { |
| 315 | ERROR("Cannot set thread-specific hash value."); |
| 316 | } |
| 317 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 320 | void |
| 321 | free_err_reply() |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 322 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 323 | json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key); |
| 324 | if (err_reply != NULL) { |
| 325 | if (*err_reply != NULL) { |
| 326 | pthread_mutex_lock(&json_lock); |
| 327 | json_object_put(*err_reply); |
| 328 | pthread_mutex_unlock(&json_lock); |
| 329 | } |
| 330 | free(err_reply); |
| 331 | err_reply = NULL; |
| 332 | if (pthread_setspecific(err_reply_key, err_reply) != 0) { |
| 333 | ERROR("Cannot set thread-specific hash value."); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static struct session_with_mutex * |
| 339 | session_get_locked(unsigned int session_key, json_object **err) |
| 340 | { |
| 341 | struct session_with_mutex *locked_session; |
| 342 | |
| 343 | /* get non-exclusive (read) access to sessions_list (conns) */ |
| 344 | DEBUG("LOCK wrlock %s", __func__); |
| 345 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 346 | if (*err) { |
| 347 | *err = create_error_reply("Locking failed."); |
| 348 | } |
| 349 | return NULL; |
| 350 | } |
| 351 | /* get session where send the RPC */ |
| 352 | for (locked_session = netconf_sessions_list; |
| 353 | locked_session && (locked_session->session_key != session_key); |
| 354 | locked_session = locked_session->next); |
| 355 | if (!locked_session) { |
| 356 | if (*err) { |
| 357 | *err = create_error_reply("Session not found."); |
| 358 | } |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | /* get exclusive access to session */ |
| 363 | DEBUG("LOCK mutex %s", __func__); |
| 364 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 365 | if (*err) { |
| 366 | *err = create_error_reply("Locking failed."); |
| 367 | } |
| 368 | goto wrlock_fail; |
| 369 | } |
| 370 | return locked_session; |
| 371 | |
| 372 | wrlock_fail: |
| 373 | DEBUG("UNLOCK wrlock %s", __func__); |
| 374 | pthread_rwlock_unlock(&session_lock); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | static void |
Michal Vasko | e32bcba | 2015-11-24 09:05:51 +0100 | [diff] [blame] | 379 | session_user_activity(const char *username) |
| 380 | { |
| 381 | struct session_with_mutex *sess; |
| 382 | |
| 383 | for (sess = netconf_sessions_list; sess; sess = sess->next) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 384 | if (!strcmp(nc_session_get_username(sess->session), username)) { |
Michal Vasko | e32bcba | 2015-11-24 09:05:51 +0100 | [diff] [blame] | 385 | sess->last_activity = time(NULL); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static void |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 391 | session_unlock(struct session_with_mutex *locked_session) |
| 392 | { |
| 393 | DEBUG("UNLOCK mutex %s", __func__); |
| 394 | pthread_mutex_unlock(&locked_session->lock); |
| 395 | DEBUG("UNLOCK wrlock %s", __func__); |
| 396 | pthread_rwlock_unlock(&session_lock); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 397 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 398 | |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 399 | static void |
| 400 | node_metadata_text(const char *text, const char *name, json_object *parent) |
| 401 | { |
| 402 | json_object *obj; |
| 403 | |
| 404 | if (!text) { |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | obj = json_object_new_string(text); |
| 409 | json_object_object_add(parent, name, obj); |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | node_metadata_restr(struct lys_restr *restr, const char *name, json_object *parent) |
| 414 | { |
| 415 | json_object *obj; |
| 416 | |
| 417 | if (!restr) { |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | obj = json_object_new_string(restr->expr); |
| 422 | json_object_object_add(parent, name, obj); |
| 423 | } |
| 424 | |
| 425 | static void |
| 426 | node_metadata_must(uint8_t must_size, struct lys_restr *must, json_object *parent) |
| 427 | { |
| 428 | uint8_t i; |
| 429 | json_object *array, *obj; |
| 430 | |
| 431 | if (!must_size || !must) { |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | array = json_object_new_array(); |
| 436 | |
| 437 | for (i = 0; i < must_size; ++i) { |
| 438 | obj = json_object_new_string(must[i].expr); |
| 439 | json_object_array_add(array, obj); |
| 440 | } |
| 441 | |
| 442 | json_object_object_add(parent, "must", array); |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | node_metadata_basic(struct lys_node *node, json_object *parent) |
| 447 | { |
| 448 | json_object *obj; |
| 449 | |
| 450 | /* description */ |
| 451 | node_metadata_text(node->dsc, "description", parent); |
| 452 | |
| 453 | /* reference */ |
| 454 | node_metadata_text(node->ref, "reference", parent); |
| 455 | |
| 456 | /* config */ |
| 457 | if (node->flags & LYS_CONFIG_R) { |
| 458 | obj = json_object_new_boolean(0); |
| 459 | } else { |
| 460 | obj = json_object_new_boolean(1); |
| 461 | } |
| 462 | json_object_object_add(parent, "config", obj); |
| 463 | |
| 464 | /* status */ |
| 465 | if (node->flags & LYS_STATUS_DEPRC) { |
| 466 | obj = json_object_new_string("deprecated"); |
| 467 | } else if (node->flags & LYS_STATUS_OBSLT) { |
| 468 | obj = json_object_new_string("obsolete"); |
| 469 | } else { |
| 470 | obj = json_object_new_string("current"); |
| 471 | } |
| 472 | json_object_object_add(parent, "status", obj); |
| 473 | |
| 474 | /* mandatory */ |
| 475 | if (node->flags & LYS_MAND_TRUE) { |
| 476 | obj = json_object_new_boolean(1); |
| 477 | } else { |
| 478 | obj = json_object_new_boolean(0); |
| 479 | } |
| 480 | json_object_object_add(parent, "mandatory", obj); |
| 481 | |
| 482 | /* NACM extensions */ |
| 483 | if (node->nacm) { |
| 484 | if (node->nacm & LYS_NACM_DENYW) { |
| 485 | obj = json_object_new_string("default-deny-write"); |
| 486 | } else { |
| 487 | obj = json_object_new_string("default-deny-all"); |
| 488 | } |
| 489 | json_object_object_add(parent, "ext", obj); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | node_metadata_when(struct lys_when *when, json_object *parent) |
| 495 | { |
| 496 | json_object *obj; |
| 497 | |
| 498 | if (!when) { |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | obj = json_object_new_string(when->cond); |
| 503 | json_object_object_add(parent, "when", obj); |
| 504 | } |
| 505 | |
| 506 | static void |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 507 | node_metadata_children_recursive(struct lys_node *node, json_object **child_array, json_object **choice_array) |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 508 | { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 509 | json_object *obj; |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 510 | struct lys_node *child; |
| 511 | |
| 512 | if (!node->child) { |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | LY_TREE_FOR(node->child, child) { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 517 | if (child->nodetype == LYS_USES) { |
| 518 | node_metadata_children_recursive(child, child_array, choice_array); |
| 519 | } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) { |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 520 | obj = json_object_new_string(child->name); |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 521 | if (!*child_array) { |
| 522 | *child_array = json_object_new_array(); |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 523 | } |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 524 | json_object_array_add(*child_array, obj); |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 525 | } else if (child->nodetype == LYS_CHOICE) { |
| 526 | obj = json_object_new_string(child->name); |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 527 | if (!*choice_array) { |
| 528 | *choice_array = json_object_new_array(); |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 529 | } |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 530 | json_object_array_add(*choice_array, obj); |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static void |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 536 | node_metadata_cases_recursive(struct lys_node_choice *choice, json_object *array) |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 537 | { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 538 | json_object *obj; |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 539 | struct lys_node *child; |
| 540 | |
| 541 | if (!choice->child) { |
| 542 | return; |
| 543 | } |
| 544 | |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 545 | LY_TREE_FOR(choice->child, child) { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 546 | if (child->nodetype == LYS_USES) { |
| 547 | node_metadata_cases_recursive((struct lys_node_choice *)child, array); |
| 548 | } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE)) { |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 549 | obj = json_object_new_string(child->name); |
| 550 | json_object_array_add(array, obj); |
| 551 | } |
| 552 | } |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void |
| 556 | node_metadata_min_max(uint32_t min, uint32_t max, json_object *parent) |
| 557 | { |
| 558 | json_object *obj; |
| 559 | |
| 560 | if (min) { |
| 561 | obj = json_object_new_int(min); |
| 562 | json_object_object_add(parent, "min-elements", obj); |
| 563 | } |
| 564 | |
| 565 | if (max) { |
| 566 | obj = json_object_new_int(max); |
| 567 | json_object_object_add(parent, "max-elements", obj); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static void |
| 572 | node_metadata_ident_recursive(struct lys_ident *ident, json_object *array) |
| 573 | { |
| 574 | struct lys_ident_der *cur; |
| 575 | json_object *obj; |
| 576 | |
| 577 | if (!ident) { |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | obj = json_object_new_string(ident->name); |
| 582 | json_object_array_add(array, obj); |
| 583 | |
| 584 | for (cur = ident->der; cur; cur = cur->next) { |
| 585 | node_metadata_ident_recursive(cur->ident, array); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static void |
| 590 | node_metadata_type(struct lys_type *type, struct lys_module *module, json_object *parent) |
| 591 | { |
| 592 | json_object *obj, *array, *item; |
| 593 | char *str; |
| 594 | int i; |
| 595 | |
| 596 | /* built-in YANG type */ |
| 597 | if (!type->der->module) { |
| 598 | switch (type->base) { |
| 599 | case LY_TYPE_BINARY: |
| 600 | node_metadata_text("binary", "type", parent); |
| 601 | node_metadata_restr(type->info.binary.length, "length", parent); |
| 602 | break; |
| 603 | case LY_TYPE_BITS: |
| 604 | node_metadata_text("bits", "type", parent); |
| 605 | |
| 606 | array = json_object_new_array(); |
| 607 | for (i = 0; i < type->info.bits.count; ++i) { |
| 608 | item = json_object_new_object(); |
| 609 | obj = json_object_new_string(type->info.bits.bit[i].name); |
| 610 | json_object_object_add(item, "name", obj); |
| 611 | obj = json_object_new_int(type->info.bits.bit[i].pos); |
| 612 | json_object_object_add(item, "position", obj); |
| 613 | json_object_array_add(array, item); |
| 614 | } |
| 615 | json_object_object_add(parent, "bits", array); |
| 616 | break; |
| 617 | case LY_TYPE_BOOL: |
| 618 | node_metadata_text("bool", "type", parent); |
| 619 | break; |
| 620 | case LY_TYPE_DEC64: |
| 621 | node_metadata_text("decimal64", "type", parent); |
| 622 | node_metadata_restr(type->info.dec64.range, "range", parent); |
| 623 | obj = json_object_new_int(type->info.dec64.dig); |
| 624 | json_object_object_add(parent, "fraction-digits", obj); |
| 625 | break; |
| 626 | case LY_TYPE_EMPTY: |
| 627 | node_metadata_text("empty", "type", parent); |
| 628 | break; |
| 629 | case LY_TYPE_ENUM: |
| 630 | node_metadata_text("enumeration", "type", parent); |
| 631 | |
| 632 | array = json_object_new_array(); |
| 633 | for (i = 0; i < type->info.enums.count; ++i) { |
| 634 | obj = json_object_new_string(type->info.enums.enm[i].name); |
| 635 | json_object_array_add(array, obj); |
| 636 | } |
| 637 | json_object_object_add(parent, "enumval", array); |
| 638 | break; |
| 639 | case LY_TYPE_IDENT: |
| 640 | node_metadata_text("identityref", "type", parent); |
| 641 | |
| 642 | array = json_object_new_array(); |
| 643 | node_metadata_ident_recursive(type->info.ident.ref, array); |
| 644 | json_object_object_add(parent, "identityval", array); |
| 645 | break; |
| 646 | case LY_TYPE_INST: |
| 647 | node_metadata_text("instance-identifier", "type", parent); |
| 648 | if (type->info.inst.req == -1) { |
| 649 | obj = json_object_new_boolean(0); |
| 650 | } else { |
| 651 | obj = json_object_new_boolean(1); |
| 652 | } |
| 653 | json_object_object_add(parent, "require-instance", obj); |
| 654 | break; |
| 655 | case LY_TYPE_LEAFREF: |
| 656 | node_metadata_text("leafref", "type", parent); |
| 657 | node_metadata_text(type->info.lref.path, "path", parent); |
| 658 | break; |
| 659 | case LY_TYPE_STRING: |
| 660 | node_metadata_text("string", "type", parent); |
| 661 | node_metadata_restr(type->info.str.length, "length", parent); |
| 662 | if (type->info.str.pat_count) { |
| 663 | array = json_object_new_array(); |
| 664 | for (i = 0; i < type->info.str.pat_count; ++i) { |
| 665 | obj = json_object_new_string(type->info.str.patterns[i].expr); |
| 666 | json_object_array_add(array, obj); |
| 667 | } |
| 668 | json_object_object_add(parent, "pattern", array); |
| 669 | } |
| 670 | break; |
| 671 | case LY_TYPE_UNION: |
| 672 | node_metadata_text("union", "type", parent); |
| 673 | array = json_object_new_array(); |
| 674 | for (i = 0; i < type->info.uni.count; ++i) { |
| 675 | obj = json_object_new_object(); |
| 676 | node_metadata_type(&type->info.uni.types[i], module, obj); |
| 677 | json_object_array_add(array, obj); |
| 678 | } |
| 679 | json_object_object_add(parent, "types", array); |
| 680 | break; |
| 681 | case LY_TYPE_INT8: |
| 682 | node_metadata_text("int8", "type", parent); |
| 683 | node_metadata_restr(type->info.num.range, "range", parent); |
| 684 | break; |
| 685 | case LY_TYPE_UINT8: |
| 686 | node_metadata_text("uint8", "type", parent); |
| 687 | node_metadata_restr(type->info.num.range, "range", parent); |
| 688 | break; |
| 689 | case LY_TYPE_INT16: |
| 690 | node_metadata_text("int16", "type", parent); |
| 691 | node_metadata_restr(type->info.num.range, "range", parent); |
| 692 | break; |
| 693 | case LY_TYPE_UINT16: |
| 694 | node_metadata_text("uint16", "type", parent); |
| 695 | node_metadata_restr(type->info.num.range, "range", parent); |
| 696 | break; |
| 697 | case LY_TYPE_INT32: |
| 698 | node_metadata_text("int32", "type", parent); |
| 699 | node_metadata_restr(type->info.num.range, "range", parent); |
| 700 | break; |
| 701 | case LY_TYPE_UINT32: |
| 702 | node_metadata_text("uint32", "type", parent); |
| 703 | node_metadata_restr(type->info.num.range, "range", parent); |
| 704 | break; |
| 705 | case LY_TYPE_INT64: |
| 706 | node_metadata_text("int64", "type", parent); |
| 707 | node_metadata_restr(type->info.num.range, "range", parent); |
| 708 | break; |
| 709 | case LY_TYPE_UINT64: |
| 710 | node_metadata_text("uint64", "type", parent); |
| 711 | node_metadata_restr(type->info.num.range, "range", parent); |
| 712 | break; |
| 713 | default: |
| 714 | ERROR("Internal: unknown type (%s:%d)", __FILE__, __LINE__); |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | /* typedef */ |
| 719 | } else { |
| 720 | if (!module || !type->module_name || !strcmp(type->module_name, module->name)) { |
| 721 | node_metadata_text(type->der->name, "type", parent); |
| 722 | } else { |
| 723 | asprintf(&str, "%s:%s", type->module_name, type->der->name); |
| 724 | node_metadata_text(str, "type", parent); |
| 725 | free(str); |
| 726 | } |
| 727 | obj = json_object_new_object(); |
| 728 | node_metadata_typedef(type->der, obj); |
| 729 | json_object_object_add(parent, "typedef", obj); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | static void |
| 734 | node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent) |
| 735 | { |
| 736 | json_object *obj; |
| 737 | |
| 738 | /* description */ |
| 739 | node_metadata_text(tpdf->dsc, "description", parent); |
| 740 | |
| 741 | /* reference */ |
| 742 | node_metadata_text(tpdf->ref, "reference", parent); |
| 743 | |
| 744 | /* status */ |
| 745 | if (tpdf->flags & LYS_STATUS_DEPRC) { |
| 746 | obj = json_object_new_string("deprecated"); |
| 747 | } else if (tpdf->flags & LYS_STATUS_OBSLT) { |
| 748 | obj = json_object_new_string("obsolete"); |
| 749 | } else { |
| 750 | obj = json_object_new_string("current"); |
| 751 | } |
| 752 | json_object_object_add(parent, "status", obj); |
| 753 | |
| 754 | /* type */ |
| 755 | node_metadata_type(&tpdf->type, tpdf->module, parent); |
| 756 | |
| 757 | /* units */ |
| 758 | node_metadata_text(tpdf->units, "units", parent); |
| 759 | |
| 760 | /* default */ |
| 761 | node_metadata_text(tpdf->dflt, "default", parent); |
| 762 | } |
| 763 | |
| 764 | static void |
| 765 | node_metadata_container(struct lys_node_container *cont, json_object *parent) |
| 766 | { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 767 | json_object *obj, *child_array = NULL, *choice_array = NULL; |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 768 | |
| 769 | /* element type */ |
| 770 | obj = json_object_new_string("container"); |
| 771 | json_object_object_add(parent, "eltype", obj); |
| 772 | |
| 773 | /* shared info */ |
| 774 | node_metadata_basic((struct lys_node *)cont, parent); |
| 775 | |
| 776 | /* must */ |
| 777 | node_metadata_must(cont->must_size, cont->must, parent); |
| 778 | |
| 779 | /* presence */ |
| 780 | node_metadata_text(cont->presence, "presence", parent); |
| 781 | |
| 782 | /* when */ |
| 783 | node_metadata_when(cont->when, parent); |
| 784 | |
| 785 | /* children & choice */ |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 786 | node_metadata_children_recursive((struct lys_node *)cont, &child_array, &choice_array); |
| 787 | if (child_array) { |
| 788 | json_object_object_add(parent, "children", child_array); |
| 789 | } |
| 790 | if (choice_array) { |
| 791 | json_object_object_add(parent, "choice", choice_array); |
| 792 | } |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static void |
| 796 | node_metadata_choice(struct lys_node_choice *choice, json_object *parent) |
| 797 | { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 798 | json_object *obj, *array; |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 799 | |
| 800 | /* element type */ |
| 801 | obj = json_object_new_string("choice"); |
| 802 | json_object_object_add(parent, "eltype", obj); |
| 803 | |
| 804 | /* shared info */ |
| 805 | node_metadata_basic((struct lys_node *)choice, parent); |
| 806 | |
| 807 | /* default */ |
Michal Vasko | 5e1c605 | 2016-03-17 15:43:33 +0100 | [diff] [blame] | 808 | if (choice->dflt) { |
| 809 | node_metadata_text(choice->dflt->name, "default", parent); |
| 810 | } |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 811 | |
| 812 | /* when */ |
| 813 | node_metadata_when(choice->when, parent); |
| 814 | |
| 815 | /* cases */ |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 816 | if (choice->child) { |
| 817 | array = json_object_new_array(); |
| 818 | node_metadata_cases_recursive(choice, array); |
| 819 | json_object_object_add(parent, "cases", array); |
| 820 | } |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void |
| 824 | node_metadata_leaf(struct lys_node_leaf *leaf, json_object *parent) |
| 825 | { |
| 826 | json_object *obj; |
| 827 | struct lys_node_list *list; |
| 828 | int is_key, i; |
| 829 | |
| 830 | /* element type */ |
| 831 | obj = json_object_new_string("leaf"); |
| 832 | json_object_object_add(parent, "eltype", obj); |
| 833 | |
| 834 | /* shared info */ |
| 835 | node_metadata_basic((struct lys_node *)leaf, parent); |
| 836 | |
| 837 | /* type */ |
| 838 | node_metadata_type(&leaf->type, leaf->module, parent); |
| 839 | |
| 840 | /* units */ |
| 841 | node_metadata_text(leaf->units, "units", parent); |
| 842 | |
| 843 | /* default */ |
| 844 | node_metadata_text(leaf->dflt, "default", parent); |
| 845 | |
| 846 | /* must */ |
| 847 | node_metadata_must(leaf->must_size, leaf->must, parent); |
| 848 | |
| 849 | /* when */ |
| 850 | node_metadata_when(leaf->when, parent); |
| 851 | |
| 852 | /* iskey */ |
| 853 | is_key = 0; |
| 854 | list = (struct lys_node_list *)lys_parent((struct lys_node *)leaf); |
| 855 | if (list && (list->nodetype == LYS_LIST)) { |
| 856 | for (i = 0; i < list->keys_size; ++i) { |
| 857 | if (list->keys[i] == leaf) { |
| 858 | is_key = 1; |
| 859 | break; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | obj = json_object_new_boolean(is_key); |
| 864 | json_object_object_add(parent, "iskey", obj); |
| 865 | } |
| 866 | |
| 867 | static void |
| 868 | node_metadata_leaflist(struct lys_node_leaflist *llist, json_object *parent) |
| 869 | { |
| 870 | json_object *obj; |
| 871 | |
| 872 | /* element type */ |
| 873 | obj = json_object_new_string("leaf-list"); |
| 874 | json_object_object_add(parent, "eltype", obj); |
| 875 | |
| 876 | /* shared info */ |
| 877 | node_metadata_basic((struct lys_node *)llist, parent); |
| 878 | |
| 879 | /* type */ |
| 880 | node_metadata_type(&llist->type, llist->module, parent); |
| 881 | |
| 882 | /* units */ |
| 883 | node_metadata_text(llist->units, "units", parent); |
| 884 | |
| 885 | /* must */ |
| 886 | node_metadata_must(llist->must_size, llist->must, parent); |
| 887 | |
| 888 | /* when */ |
| 889 | node_metadata_when(llist->when, parent); |
| 890 | |
| 891 | /* min/max-elements */ |
| 892 | node_metadata_min_max(llist->min, llist->max, parent); |
| 893 | } |
| 894 | |
| 895 | static void |
| 896 | node_metadata_list(struct lys_node_list *list, json_object *parent) |
| 897 | { |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 898 | json_object *obj, *array, *child_array = NULL, *choice_array = NULL;; |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 899 | int i; |
| 900 | unsigned int j; |
| 901 | |
| 902 | /* element type */ |
| 903 | obj = json_object_new_string("list"); |
| 904 | json_object_object_add(parent, "eltype", obj); |
| 905 | |
| 906 | /* shared info */ |
| 907 | node_metadata_basic((struct lys_node *)list, parent); |
| 908 | |
| 909 | /* must */ |
| 910 | node_metadata_must(list->must_size, list->must, parent); |
| 911 | |
| 912 | /* when */ |
| 913 | node_metadata_when(list->when, parent); |
| 914 | |
| 915 | /* min/max-elements */ |
| 916 | node_metadata_min_max(list->min, list->max, parent); |
| 917 | |
| 918 | /* keys */ |
| 919 | if (list->keys_size) { |
| 920 | array = json_object_new_array(); |
| 921 | for (i = 0; i < list->keys_size; ++i) { |
| 922 | obj = json_object_new_string(list->keys[i]->name); |
| 923 | json_object_array_add(array, obj); |
| 924 | } |
| 925 | json_object_object_add(parent, "keys", array); |
| 926 | } |
| 927 | |
| 928 | /* unique */ |
| 929 | if (list->unique_size) { |
| 930 | array = json_object_new_array(); |
| 931 | for (i = 0; i < list->unique_size; ++i) { |
| 932 | for (j = 0; j < list->unique[i].expr_size; ++j) { |
| 933 | obj = json_object_new_string(list->unique[i].expr[j]); |
| 934 | json_object_array_add(array, obj); |
| 935 | } |
| 936 | } |
| 937 | json_object_object_add(parent, "unique", array); |
| 938 | } |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 939 | |
| 940 | /* children & choice */ |
| 941 | node_metadata_children_recursive((struct lys_node *)list, &child_array, &choice_array); |
| 942 | if (child_array) { |
| 943 | json_object_object_add(parent, "children", child_array); |
| 944 | } |
| 945 | if (choice_array) { |
| 946 | json_object_object_add(parent, "choice", choice_array); |
| 947 | } |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static void |
| 951 | node_metadata_anyxml(struct lys_node_anyxml *anyxml, json_object *parent) |
| 952 | { |
| 953 | json_object *obj; |
| 954 | |
| 955 | /* element type */ |
| 956 | obj = json_object_new_string("anyxml"); |
| 957 | json_object_object_add(parent, "eltype", obj); |
| 958 | |
| 959 | /* shared info */ |
| 960 | node_metadata_basic((struct lys_node *)anyxml, parent); |
| 961 | |
| 962 | /* must */ |
| 963 | node_metadata_must(anyxml->must_size, anyxml->must, parent); |
| 964 | |
| 965 | /* when */ |
| 966 | node_metadata_when(anyxml->when, parent); |
| 967 | |
| 968 | } |
| 969 | |
| 970 | static void |
| 971 | node_metadata_case(struct lys_node_case *cas, json_object *parent) |
| 972 | { |
| 973 | json_object *obj; |
| 974 | |
| 975 | /* element type */ |
| 976 | obj = json_object_new_string("case"); |
| 977 | json_object_object_add(parent, "eltype", obj); |
| 978 | |
| 979 | /* shared info */ |
| 980 | node_metadata_basic((struct lys_node *)cas, parent); |
| 981 | |
| 982 | /* when */ |
| 983 | node_metadata_when(cas->when, parent); |
| 984 | } |
| 985 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 986 | static void |
| 987 | node_metadata_rpc(struct lys_node_rpc *rpc, json_object *parent) |
| 988 | { |
| 989 | json_object *obj; |
| 990 | |
| 991 | /* element type */ |
| 992 | obj = json_object_new_string("rpc"); |
| 993 | json_object_object_add(parent, "eltype", obj); |
| 994 | |
| 995 | /* description */ |
| 996 | node_metadata_text(rpc->dsc, "description", parent); |
| 997 | |
| 998 | /* reference */ |
| 999 | node_metadata_text(rpc->ref, "reference", parent); |
| 1000 | |
| 1001 | /* status */ |
| 1002 | if (rpc->flags & LYS_STATUS_DEPRC) { |
| 1003 | obj = json_object_new_string("deprecated"); |
| 1004 | } else if (rpc->flags & LYS_STATUS_OBSLT) { |
| 1005 | obj = json_object_new_string("obsolete"); |
| 1006 | } else { |
| 1007 | obj = json_object_new_string("current"); |
| 1008 | } |
| 1009 | json_object_object_add(parent, "status", obj); |
| 1010 | } |
| 1011 | |
| 1012 | static void |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1013 | node_metadata_model(const struct lys_module *module, json_object *parent) |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1014 | { |
| 1015 | json_object *obj, *array, *item; |
Michal Vasko | a4b8cdb | 2016-03-17 15:44:45 +0100 | [diff] [blame] | 1016 | const struct lys_node *node; |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1017 | int i; |
| 1018 | |
| 1019 | /* yang-version */ |
| 1020 | if (module->version == 2) { |
| 1021 | obj = json_object_new_string("1.1"); |
| 1022 | } else { |
| 1023 | obj = json_object_new_string("1.0"); |
| 1024 | } |
| 1025 | json_object_object_add(parent, "yang-version", obj); |
| 1026 | |
| 1027 | /* namespace */ |
| 1028 | node_metadata_text(module->ns, "namespace", parent); |
| 1029 | |
| 1030 | /* prefix */ |
| 1031 | node_metadata_text(module->prefix, "prefix", parent); |
| 1032 | |
| 1033 | /* contact */ |
| 1034 | node_metadata_text(module->contact, "contact", parent); |
| 1035 | |
| 1036 | /* organization */ |
| 1037 | node_metadata_text(module->org, "organization", parent); |
| 1038 | |
| 1039 | /* revision */ |
| 1040 | if (module->rev_size) { |
| 1041 | node_metadata_text(module->rev[0].date, "revision", parent); |
| 1042 | } |
| 1043 | |
| 1044 | /* description */ |
| 1045 | node_metadata_text(module->dsc, "description", parent); |
| 1046 | |
| 1047 | /* import */ |
| 1048 | if (module->imp_size) { |
| 1049 | array = json_object_new_array(); |
| 1050 | for (i = 0; i < module->imp_size; ++i) { |
| 1051 | item = json_object_new_object(); |
| 1052 | |
| 1053 | node_metadata_text(module->imp[i].module->name, "name", item); |
| 1054 | node_metadata_text(module->imp[i].prefix, "prefix", item); |
| 1055 | if (module->imp[i].rev && module->imp[i].rev[0]) { |
| 1056 | node_metadata_text(module->imp[i].rev, "revision", item); |
| 1057 | } |
| 1058 | |
| 1059 | json_object_array_add(array, item); |
| 1060 | } |
| 1061 | json_object_object_add(parent, "imports", array); |
| 1062 | } |
| 1063 | |
| 1064 | /* include */ |
| 1065 | if (module->inc_size) { |
| 1066 | array = json_object_new_array(); |
| 1067 | for (i = 0; i < module->inc_size; ++i) { |
| 1068 | item = json_object_new_object(); |
| 1069 | |
| 1070 | node_metadata_text(module->inc[i].submodule->name, "name", item); |
| 1071 | if (module->inc[i].rev && module->inc[i].rev[0]) { |
| 1072 | node_metadata_text(module->inc[i].rev, "revision", item); |
| 1073 | } |
| 1074 | |
| 1075 | json_object_array_add(array, item); |
| 1076 | } |
| 1077 | json_object_object_add(parent, "includes", array); |
| 1078 | } |
Michal Vasko | a4b8cdb | 2016-03-17 15:44:45 +0100 | [diff] [blame] | 1079 | |
| 1080 | /* top-nodes */ |
| 1081 | node = NULL; |
| 1082 | array = NULL; |
| 1083 | while ((node = lys_getnext(node, NULL, module, LYS_GETNEXT_WITHCHOICE))) { |
| 1084 | if (node->nodetype & (LYS_RPC | LYS_NOTIF)) { |
| 1085 | continue; |
| 1086 | } |
| 1087 | if (!array) { |
| 1088 | array = json_object_new_array(); |
| 1089 | } |
| 1090 | item = json_object_new_string(node->name); |
| 1091 | json_object_array_add(array, item); |
| 1092 | } |
| 1093 | if (array) { |
| 1094 | json_object_object_add(parent, "top-nodes", array); |
| 1095 | } |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 1098 | /** |
| 1099 | * \defgroup netconf_operations NETCONF operations |
| 1100 | * The list of NETCONF operations that mod_netconf supports. |
| 1101 | * @{ |
| 1102 | */ |
| 1103 | |
| 1104 | /** |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 1105 | * \brief Send RPC and wait for reply with timeout. |
| 1106 | * |
| 1107 | * \param[in] session libnetconf session |
| 1108 | * \param[in] rpc prepared RPC message |
| 1109 | * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking |
| 1110 | * \param[out] reply reply from the server |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1111 | * \return NC_MSG_WOULDBLOCK or NC_MSG_ERROR. |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 1112 | * On success, it returns NC_MSG_REPLY. |
| 1113 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1114 | NC_MSG_TYPE |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1115 | netconf_send_recv_timed(struct nc_session *session, struct nc_rpc *rpc, int timeout, int strict, struct nc_reply **reply) |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 1116 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1117 | uint64_t msgid; |
| 1118 | NC_MSG_TYPE ret; |
| 1119 | ret = nc_send_rpc(session, rpc, timeout, &msgid); |
| 1120 | if (ret != NC_MSG_RPC) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1121 | return ret; |
| 1122 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1123 | |
| 1124 | while ((ret = nc_recv_reply(session, rpc, msgid, timeout, (strict ? LYD_OPT_STRICT : 0), reply)) == NC_MSG_NOTIF); |
| 1125 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1126 | return ret; |
| 1127 | } |
| 1128 | |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 1129 | /** |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 1130 | * \brief Connect to NETCONF server |
| 1131 | * |
| 1132 | * \warning Session_key hash is not bound with caller identification. This could be potential security risk. |
| 1133 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1134 | static unsigned int |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1135 | netconf_connect(const char *host, const char *port, const char *user, const char *pass, const char *privkey) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1136 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1137 | struct nc_session* session = NULL; |
| 1138 | struct session_with_mutex *locked_session, *last_session; |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 1139 | char *pubkey; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1140 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1141 | /* connect to the requested NETCONF server */ |
| 1142 | password = (char*)pass; |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 1143 | if (privkey) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1144 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 3); |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 1145 | asprintf(&pubkey, "%s.pub", privkey); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1146 | nc_client_ssh_add_keypair(pubkey, privkey); |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 1147 | free(pubkey); |
| 1148 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1149 | nc_client_ssh_set_username(user); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1150 | DEBUG("prepare to connect %s@%s:%s", user, host, port); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1151 | session = nc_connect_ssh(host, (unsigned short)atoi(port), NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1152 | DEBUG("nc_session_connect done"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1153 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1154 | /* if connected successful, add session to the list */ |
| 1155 | if (session != NULL) { |
| 1156 | if ((locked_session = calloc(1, sizeof(struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) { |
Michal Vasko | a959005 | 2016-03-08 10:12:24 +0100 | [diff] [blame] | 1157 | nc_session_free(session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1158 | session = NULL; |
| 1159 | free(locked_session); |
| 1160 | locked_session = NULL; |
| 1161 | ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno)); |
| 1162 | return 0; |
| 1163 | } |
| 1164 | locked_session->session = session; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1165 | locked_session->hello_message = NULL; |
| 1166 | locked_session->closed = 0; |
| 1167 | pthread_mutex_init(&locked_session->lock, NULL); |
| 1168 | DEBUG("Before session_lock"); |
| 1169 | /* get exclusive access to sessions_list (conns) */ |
| 1170 | DEBUG("LOCK wrlock %s", __func__); |
| 1171 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
Michal Vasko | a959005 | 2016-03-08 10:12:24 +0100 | [diff] [blame] | 1172 | nc_session_free(session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1173 | free(locked_session); |
| 1174 | ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | locked_session->ntfc_subscribed = 0; |
| 1178 | DEBUG("Add connection to the list"); |
| 1179 | if (!netconf_sessions_list) { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1180 | netconf_sessions_list = locked_session; |
| 1181 | } else { |
| 1182 | for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next); |
| 1183 | last_session->next = locked_session; |
| 1184 | locked_session->prev = last_session; |
| 1185 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1186 | session_user_activity(nc_session_get_username(locked_session->session)); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1187 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1188 | /* no need to lock session, noone can read it while we have wrlock */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1189 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1190 | /* store information about session from hello message for future usage */ |
| 1191 | prepare_status_message(locked_session, session); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1192 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1193 | DEBUG("NETCONF session established"); |
| 1194 | locked_session->session_key = session_key_generator; |
| 1195 | ++session_key_generator; |
| 1196 | if (session_key_generator == UINT_MAX) { |
| 1197 | session_key_generator = 1; |
| 1198 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1199 | |
Michal Vasko | e32bcba | 2015-11-24 09:05:51 +0100 | [diff] [blame] | 1200 | DEBUG("Before session_unlock"); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1201 | /* unlock session list */ |
| 1202 | DEBUG("UNLOCK wrlock %s", __func__); |
| 1203 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1204 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1205 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1206 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1207 | return locked_session->session_key; |
| 1208 | } |
| 1209 | |
| 1210 | ERROR("Connection could not be established"); |
| 1211 | return 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1214 | static int |
| 1215 | close_and_free_session(struct session_with_mutex *locked_session) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1216 | { |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 1217 | int i; |
| 1218 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1219 | DEBUG("LOCK mutex %s", __func__); |
| 1220 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 1221 | ERROR("Error while locking rwlock"); |
| 1222 | } |
| 1223 | locked_session->ntfc_subscribed = 0; |
| 1224 | locked_session->closed = 1; |
| 1225 | if (locked_session->session != NULL) { |
Michal Vasko | a959005 | 2016-03-08 10:12:24 +0100 | [diff] [blame] | 1226 | nc_session_free(locked_session->session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1227 | locked_session->session = NULL; |
| 1228 | } |
| 1229 | DEBUG("session closed."); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1230 | DEBUG("UNLOCK mutex %s", __func__); |
| 1231 | if (pthread_mutex_unlock(&locked_session->lock) != 0) { |
| 1232 | ERROR("Error while locking rwlock"); |
| 1233 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1234 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1235 | DEBUG("closed session, disabled notif(?), wait 0.5s"); |
| 1236 | usleep(500000); /* let notification thread stop */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1237 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1238 | /* session shouldn't be used by now */ |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 1239 | for (i = 0; i < locked_session->notif_count; ++i) { |
| 1240 | free(locked_session->notifications[i].content); |
| 1241 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1242 | free(locked_session->notifications); |
| 1243 | pthread_mutex_destroy(&locked_session->lock); |
| 1244 | if (locked_session->hello_message != NULL) { |
| 1245 | json_object_put(locked_session->hello_message); |
| 1246 | locked_session->hello_message = NULL; |
| 1247 | } |
| 1248 | locked_session->session = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1249 | free(locked_session); |
| 1250 | locked_session = NULL; |
| 1251 | DEBUG("NETCONF session closed, everything cleared."); |
| 1252 | return (EXIT_SUCCESS); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1255 | static int |
| 1256 | netconf_close(unsigned int session_key, json_object **reply) |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1257 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1258 | struct session_with_mutex *locked_session; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1259 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1260 | DEBUG("Session to close: %u", session_key); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1261 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1262 | /* get exclusive (write) access to sessions_list (conns) */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1263 | DEBUG("LOCK wrlock %s", __func__); |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 1264 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1265 | ERROR("Error while locking rwlock"); |
| 1266 | (*reply) = create_error_reply("Internal: Error while locking."); |
| 1267 | return EXIT_FAILURE; |
| 1268 | } |
| 1269 | /* remove session from the active sessions list -> nobody new can now work with session */ |
| 1270 | for (locked_session = netconf_sessions_list; |
| 1271 | locked_session && (locked_session->session_key != session_key); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1272 | locked_session = locked_session->next); |
| 1273 | |
| 1274 | if (!locked_session) { |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 1275 | DEBUG("UNLOCK wrlock %s", __func__); |
Michal Vasko | 642cad0 | 2016-03-17 12:31:18 +0100 | [diff] [blame] | 1276 | pthread_rwlock_unlock(&session_lock); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1277 | ERROR("Could not find the session %u to close.", session_key); |
| 1278 | (*reply) = create_error_reply("Internal: Error while finding a session."); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1279 | return EXIT_FAILURE; |
| 1280 | } |
| 1281 | |
| 1282 | if (!locked_session->prev) { |
| 1283 | netconf_sessions_list = netconf_sessions_list->next; |
Michal Vasko | f0b6fbb | 2016-03-08 12:04:34 +0100 | [diff] [blame] | 1284 | if (netconf_sessions_list) { |
| 1285 | netconf_sessions_list->prev = NULL; |
| 1286 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1287 | } else { |
| 1288 | locked_session->prev->next = locked_session->next; |
| 1289 | if (locked_session->next) { |
| 1290 | locked_session->next->prev = locked_session->prev; |
| 1291 | } |
| 1292 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1293 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1294 | DEBUG("UNLOCK wrlock %s", __func__); |
| 1295 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 1296 | ERROR("Error while unlocking rwlock"); |
| 1297 | (*reply) = create_error_reply("Internal: Error while unlocking."); |
| 1298 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1299 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1300 | if ((locked_session != NULL) && (locked_session->session != NULL)) { |
| 1301 | return close_and_free_session(locked_session); |
| 1302 | } else { |
| 1303 | ERROR("Unknown session to close"); |
| 1304 | (*reply) = create_error_reply("Internal: Unkown session to close."); |
| 1305 | return (EXIT_FAILURE); |
| 1306 | } |
| 1307 | (*reply) = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1310 | /** |
| 1311 | * Test reply message type and return error message. |
| 1312 | * |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1313 | * \param[in] session nc_session internal struct |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1314 | * \param[in] session_key session ID, 0 to disable disconnect on error |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1315 | * \param[in] msgt RPC-REPLY message type |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1316 | * \param[out] data |
| 1317 | * \return NULL on success |
| 1318 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1319 | json_object * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1320 | netconf_test_reply(struct nc_session *session, unsigned int session_key, NC_MSG_TYPE msgt, struct nc_reply *reply, struct lyd_node **data) |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1321 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1322 | json_object *err = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1323 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1324 | /* process the result of the operation */ |
| 1325 | switch (msgt) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1326 | case NC_MSG_ERROR: |
| 1327 | if (nc_session_get_status(session) != NC_STATUS_RUNNING) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1328 | ERROR("mod_netconf: receiving rpc-reply failed"); |
| 1329 | if (session_key) { |
| 1330 | netconf_close(session_key, &err); |
| 1331 | } |
| 1332 | if (err != NULL) { |
| 1333 | return err; |
| 1334 | } |
| 1335 | return create_error_reply("Internal: Receiving RPC-REPLY failed."); |
| 1336 | } |
| 1337 | case NC_MSG_NONE: |
| 1338 | /* there is error handled by callback */ |
| 1339 | if (data != NULL) { |
| 1340 | free(*data); |
| 1341 | (*data) = NULL; |
| 1342 | } |
| 1343 | return NULL; |
| 1344 | case NC_MSG_REPLY: |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1345 | switch (reply->type) { |
| 1346 | case NC_RPL_OK: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1347 | if ((data != NULL) && (*data != NULL)) { |
| 1348 | free(*data); |
| 1349 | (*data) = NULL; |
| 1350 | } |
| 1351 | return create_ok_reply(); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1352 | case NC_RPL_DATA: |
| 1353 | if (((*data) = ((struct nc_reply_data *)reply)->data) == NULL) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1354 | ERROR("mod_netconf: no data from reply"); |
| 1355 | return create_error_reply("Internal: No data from reply received."); |
| 1356 | } else { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1357 | ((struct nc_reply_data *)reply)->data = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1358 | return NULL; |
| 1359 | } |
| 1360 | break; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1361 | case NC_RPL_ERROR: |
| 1362 | ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1363 | if (data != NULL) { |
| 1364 | free(*data); |
| 1365 | (*data) = NULL; |
| 1366 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1367 | return create_error_reply(((struct nc_reply_error *)reply)->err[0].message); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1368 | default: |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1369 | ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1370 | if (data != NULL) { |
| 1371 | free(*data); |
| 1372 | (*data) = NULL; |
| 1373 | } |
| 1374 | return create_error_reply("Unknown type of NETCONF reply."); |
| 1375 | } |
| 1376 | break; |
| 1377 | default: |
| 1378 | ERROR("mod_netconf: unexpected reply message received (%d)", msgt); |
| 1379 | if (data != NULL) { |
| 1380 | free(*data); |
| 1381 | (*data) = NULL; |
| 1382 | } |
| 1383 | return create_error_reply("Internal: Unexpected RPC-REPLY message type."); |
| 1384 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1387 | json_object * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1388 | netconf_unlocked_op(struct nc_session *session, struct nc_rpc *rpc) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1389 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1390 | struct nc_reply* reply = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1391 | NC_MSG_TYPE msgt; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1392 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1393 | /* check requests */ |
| 1394 | if (rpc == NULL) { |
| 1395 | ERROR("mod_netconf: rpc is not created"); |
| 1396 | return create_error_reply("Internal error: RPC is not created"); |
| 1397 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1398 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1399 | if (session != NULL) { |
| 1400 | /* send the request and get the reply */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1401 | msgt = netconf_send_recv_timed(session, rpc, 50000, 0, &reply); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1402 | /* process the result of the operation */ |
| 1403 | return netconf_test_reply(session, 0, msgt, reply, NULL); |
| 1404 | } else { |
| 1405 | ERROR("Unknown session to process."); |
| 1406 | return create_error_reply("Internal error: Unknown session to process."); |
| 1407 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1410 | /** |
| 1411 | * Perform RPC method that returns data. |
| 1412 | * |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1413 | * \param[in] session_id session identifier |
| 1414 | * \param[in] rpc RPC message to perform |
| 1415 | * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1416 | * \return NULL on success, json object with error otherwise |
| 1417 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1418 | static json_object * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1419 | netconf_op(unsigned int session_key, struct nc_rpc *rpc, int strict, struct lyd_node **received_data) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1420 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1421 | struct session_with_mutex * locked_session; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1422 | struct nc_reply* reply = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1423 | json_object *res = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1424 | struct lyd_node *data = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1425 | NC_MSG_TYPE msgt; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 1426 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1427 | /* check requests */ |
| 1428 | if (rpc == NULL) { |
| 1429 | ERROR("mod_netconf: rpc is not created"); |
| 1430 | res = create_error_reply("Internal: RPC could not be created."); |
| 1431 | data = NULL; |
| 1432 | goto finished; |
| 1433 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1434 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1435 | locked_session = session_get_locked(session_key, &res); |
| 1436 | if (!locked_session) { |
| 1437 | ERROR("Unknown session or locking failed."); |
| 1438 | goto finished; |
| 1439 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1440 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1441 | session_user_activity(nc_session_get_username(locked_session->session)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1442 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1443 | /* send the request and get the reply */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1444 | msgt = netconf_send_recv_timed(locked_session->session, rpc, 2000000, strict, &reply); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1445 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1446 | session_unlock(locked_session); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1447 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1448 | res = netconf_test_reply(locked_session->session, session_key, msgt, reply, &data); |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1449 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1450 | finished: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1451 | nc_reply_free(reply); |
| 1452 | if (received_data != NULL) { |
| 1453 | (*received_data) = data; |
| 1454 | } else { |
| 1455 | if (data != NULL) { |
| 1456 | free(data); |
| 1457 | data = NULL; |
| 1458 | } |
| 1459 | } |
| 1460 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1463 | static char * |
| 1464 | netconf_getconfig(unsigned int session_key, NC_DATASTORE source, const char *filter, int strict, json_object **err) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1465 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1466 | struct nc_rpc* rpc; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1467 | struct session_with_mutex *locked_session; |
Michal Vasko | 04245ee | 2016-03-16 09:32:59 +0100 | [diff] [blame] | 1468 | json_object *res = NULL, *data_cjson; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1469 | enum json_tokener_error tok_err; |
Michal Vasko | 04245ee | 2016-03-16 09:32:59 +0100 | [diff] [blame] | 1470 | char *data_json = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1471 | struct lyd_node *data, *sibling, *next; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1472 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1473 | /* tell server to show all elements even if they have default values */ |
| 1474 | #ifdef HAVE_WITHDEFAULTS_TAGGED |
| 1475 | rpc = nc_rpc_getconfig(source, filter, NC_WD_MODE_ALL_TAG, NC_PARAMTYPE_CONST); |
| 1476 | #else |
| 1477 | rpc = nc_rpc_getconfig(source, filter, 0, NC_PARAMTYPE_CONST); |
| 1478 | #endif |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1479 | if (rpc == NULL) { |
| 1480 | ERROR("mod_netconf: creating rpc request failed"); |
| 1481 | return (NULL); |
| 1482 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1483 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1484 | res = netconf_op(session_key, rpc, strict, &data); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1485 | nc_rpc_free(rpc); |
| 1486 | if (res != NULL) { |
| 1487 | (*err) = res; |
| 1488 | } else { |
| 1489 | (*err) = NULL; |
| 1490 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1491 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1492 | if (data) { |
| 1493 | for (locked_session = netconf_sessions_list; |
| 1494 | locked_session && (locked_session->session_key != session_key); |
| 1495 | locked_session = locked_session->next); |
| 1496 | /* won't fail */ |
| 1497 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1498 | /* print data into JSON */ |
| 1499 | if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1500 | ERROR("Printing JSON <get-config> data failed."); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1501 | lyd_free_withsiblings(data); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1502 | return NULL; |
| 1503 | } |
| 1504 | |
| 1505 | /* parse JSON data into cjson */ |
| 1506 | pthread_mutex_lock(&json_lock); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1507 | data_cjson = json_tokener_parse_verbose(data_json, &tok_err); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1508 | if (!data_cjson) { |
| 1509 | ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err)); |
| 1510 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1511 | lyd_free_withsiblings(data); |
| 1512 | free(data_json); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1513 | return NULL; |
| 1514 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1515 | free(data_json); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1516 | |
| 1517 | /* go simultaneously through both trees and add metadata */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1518 | LY_TREE_FOR_SAFE(data, next, sibling) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1519 | node_add_metadata_recursive(sibling, NULL, data_cjson); |
| 1520 | lyd_free(sibling); |
| 1521 | } |
| 1522 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1523 | data_json = strdup(json_object_to_json_string_ext(data_cjson, 0)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1524 | json_object_put(data_cjson); |
| 1525 | pthread_mutex_unlock(&json_lock); |
| 1526 | } |
| 1527 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1528 | return (data_json); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1531 | static char * |
| 1532 | netconf_getschema(unsigned int session_key, const char *identifier, const char *version, const char *format, json_object **err) |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1533 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1534 | struct nc_rpc *rpc; |
| 1535 | struct lyd_node *data = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1536 | json_object *res = NULL; |
Michal Vasko | 56ec595 | 2016-04-05 14:30:23 +0200 | [diff] [blame] | 1537 | char *model_data = NULL; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1538 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1539 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1540 | rpc = nc_rpc_getschema(identifier, version, format, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1541 | if (rpc == NULL) { |
| 1542 | ERROR("mod_netconf: creating rpc request failed"); |
| 1543 | return (NULL); |
| 1544 | } |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1545 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1546 | res = netconf_op(session_key, rpc, 0, &data); |
| 1547 | nc_rpc_free(rpc); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1548 | if (res != NULL) { |
| 1549 | (*err) = res; |
| 1550 | } else { |
| 1551 | (*err) = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1552 | |
| 1553 | if (data) { |
Michal Vasko | 56ec595 | 2016-04-05 14:30:23 +0200 | [diff] [blame] | 1554 | if (((struct lyd_node_anyxml *)data)->xml_struct) { |
| 1555 | lyxml_print_mem(&model_data, ((struct lyd_node_anyxml *)data)->value.xml, 0); |
| 1556 | } else { |
| 1557 | model_data = strdup(((struct lyd_node_anyxml *)data)->value.str); |
| 1558 | } |
| 1559 | if (!model_data) { |
| 1560 | ERROR("memory allocation fail (%s:%d)", __FILE__, __LINE__); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1561 | } |
| 1562 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1563 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1564 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1565 | return (model_data); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1568 | static char * |
| 1569 | netconf_get(unsigned int session_key, const char* filter, int strict, json_object **err) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1570 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1571 | struct nc_rpc* rpc; |
| 1572 | char* data_json = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1573 | json_object *res = NULL, *data_cjson; |
| 1574 | enum json_tokener_error tok_err; |
| 1575 | struct session_with_mutex *locked_session; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1576 | struct lyd_node *data, *sibling, *next; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1577 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1578 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1579 | rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1580 | if (rpc == NULL) { |
| 1581 | ERROR("mod_netconf: creating rpc request failed"); |
| 1582 | return (NULL); |
| 1583 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1584 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1585 | res = netconf_op(session_key, rpc, strict, &data); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1586 | nc_rpc_free(rpc); |
| 1587 | if (res != NULL) { |
| 1588 | (*err) = res; |
| 1589 | } else { |
| 1590 | (*err) = NULL; |
| 1591 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1592 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1593 | if (data) { |
| 1594 | for (locked_session = netconf_sessions_list; |
| 1595 | locked_session && (locked_session->session_key != session_key); |
| 1596 | locked_session = locked_session->next); |
| 1597 | /* won't fail */ |
| 1598 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1599 | /* print JSON data */ |
| 1600 | if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1601 | ERROR("Printing JSON <get> data failed."); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1602 | lyd_free_withsiblings(data); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1603 | return NULL; |
| 1604 | } |
| 1605 | |
| 1606 | /* parse JSON data into cjson */ |
| 1607 | pthread_mutex_lock(&json_lock); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1608 | data_cjson = json_tokener_parse_verbose(data_json, &tok_err); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1609 | if (!data_cjson) { |
| 1610 | ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err)); |
| 1611 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1612 | lyd_free_withsiblings(data); |
| 1613 | free(data_json); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1614 | return NULL; |
| 1615 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1616 | free(data_json); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1617 | |
| 1618 | /* go simultaneously through both trees and add metadata */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1619 | LY_TREE_FOR_SAFE(data, next, sibling) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1620 | node_add_metadata_recursive(sibling, NULL, data_cjson); |
| 1621 | lyd_free(sibling); |
| 1622 | } |
| 1623 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1624 | data_json = strdup(json_object_to_json_string_ext(data_cjson, 0)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1625 | json_object_put(data_cjson); |
| 1626 | pthread_mutex_unlock(&json_lock); |
| 1627 | } |
| 1628 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1629 | return data_json; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1632 | static json_object * |
| 1633 | netconf_copyconfig(unsigned int session_key, NC_DATASTORE source, NC_DATASTORE target, const char *config, |
| 1634 | const char *uri_src, const char *uri_trg) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1635 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1636 | struct nc_rpc* rpc; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1637 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1638 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1639 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1640 | rpc = nc_rpc_copy(target, uri_trg, source, (config ? config : uri_src), 0, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1641 | if (rpc == NULL) { |
| 1642 | ERROR("mod_netconf: creating rpc request failed"); |
| 1643 | return create_error_reply("Internal: Creating rpc request failed"); |
| 1644 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1645 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1646 | res = netconf_op(session_key, rpc, 0, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1647 | nc_rpc_free(rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1648 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1649 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 1650 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 1651 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1652 | static json_object * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1653 | netconf_editconfig(unsigned int session_key, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP defop, |
| 1654 | NC_RPC_EDIT_ERROPT erropt, NC_RPC_EDIT_TESTOPT testopt, const char *config_or_url) |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 1655 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1656 | struct nc_rpc* rpc; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1657 | json_object *res = NULL; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 1658 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1659 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1660 | rpc = nc_rpc_edit(target, defop, testopt, erropt, config_or_url, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1661 | if (rpc == NULL) { |
| 1662 | ERROR("mod_netconf: creating rpc request failed"); |
| 1663 | return create_error_reply("Internal: Creating rpc request failed"); |
| 1664 | } |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 1665 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1666 | res = netconf_op(session_key, rpc, 0, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1667 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1668 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1669 | return res; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1672 | static json_object * |
| 1673 | netconf_killsession(unsigned int session_key, const char *sid) |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 1674 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1675 | struct nc_rpc *rpc; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1676 | json_object *res = NULL; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 1677 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1678 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1679 | rpc = nc_rpc_kill(atoi(sid)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1680 | if (rpc == NULL) { |
| 1681 | ERROR("mod_netconf: creating rpc request failed"); |
| 1682 | return create_error_reply("Internal: Creating rpc request failed"); |
| 1683 | } |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 1684 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1685 | res = netconf_op(session_key, rpc, 0, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1686 | nc_rpc_free(rpc); |
| 1687 | return res; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 1688 | } |
| 1689 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1690 | static json_object * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1691 | netconf_onlytargetop(unsigned int session_key, NC_DATASTORE target, struct nc_rpc *(*op_func)(NC_DATASTORE)) |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 1692 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1693 | struct nc_rpc* rpc; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1694 | json_object *res = NULL; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 1695 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1696 | /* create requests */ |
| 1697 | rpc = op_func(target); |
| 1698 | if (rpc == NULL) { |
| 1699 | ERROR("mod_netconf: creating rpc request failed"); |
| 1700 | return create_error_reply("Internal: Creating rpc request failed"); |
| 1701 | } |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 1702 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1703 | res = netconf_op(session_key, rpc, 0, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1704 | nc_rpc_free (rpc); |
| 1705 | return res; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1708 | static json_object * |
| 1709 | netconf_deleteconfig(unsigned int session_key, NC_DATASTORE target, const char *url) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 1710 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1711 | struct nc_rpc *rpc = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1712 | json_object *res = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1713 | rpc = nc_rpc_delete(target, url, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1714 | if (rpc == NULL) { |
| 1715 | ERROR("mod_netconf: creating rpc request failed"); |
| 1716 | return create_error_reply("Internal: Creating rpc request failed"); |
| 1717 | } |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1718 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1719 | res = netconf_op(session_key, rpc, 0, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1720 | nc_rpc_free (rpc); |
| 1721 | return res; |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1724 | static json_object * |
| 1725 | netconf_lock(unsigned int session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 1726 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1727 | return (netconf_onlytargetop(session_key, target, nc_rpc_lock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 1728 | } |
| 1729 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1730 | static json_object * |
| 1731 | netconf_unlock(unsigned int session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 1732 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1733 | return (netconf_onlytargetop(session_key, target, nc_rpc_unlock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1736 | static json_object * |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1737 | netconf_generic(unsigned int session_key, const char *xml_content, struct lyd_node **data) |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 1738 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1739 | struct nc_rpc* rpc = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1740 | json_object *res = NULL; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 1741 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1742 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1743 | rpc = nc_rpc_generic_xml(xml_content, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1744 | if (rpc == NULL) { |
| 1745 | ERROR("mod_netconf: creating rpc request failed"); |
| 1746 | return create_error_reply("Internal: Creating rpc request failed"); |
| 1747 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 1748 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1749 | /* get session where send the RPC */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1750 | res = netconf_op(session_key, rpc, 0, data); |
| 1751 | nc_rpc_free(rpc); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1752 | return res; |
| 1753 | } |
| 1754 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1755 | static int |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1756 | node_add_metadata(const struct lys_node *node, const struct lys_module *module, json_object *parent) |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1757 | { |
| 1758 | struct lys_module *cur_module; |
| 1759 | json_object *meta_obj; |
| 1760 | char *obj_name; |
| 1761 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1762 | if (node->nodetype == LYS_INPUT) { |
| 1763 | /* silently skipped */ |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1767 | cur_module = node->module; |
| 1768 | if (cur_module->type) { |
| 1769 | cur_module = ((struct lys_submodule *)cur_module)->belongsto; |
| 1770 | } |
| 1771 | if (cur_module == module) { |
| 1772 | asprintf(&obj_name, "$@%s", node->name); |
| 1773 | } else { |
| 1774 | asprintf(&obj_name, "$@%s:%s", cur_module->name, node->name); |
| 1775 | } |
| 1776 | |
| 1777 | /* in (leaf-)lists the metadata could have already been added */ |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1778 | if ((node->nodetype & (LYS_LEAFLIST | LYS_LIST)) && (json_object_object_get_ex(parent, obj_name, NULL) == TRUE)) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1779 | free(obj_name); |
| 1780 | return 1; |
| 1781 | } |
| 1782 | |
| 1783 | meta_obj = json_object_new_object(); |
| 1784 | |
| 1785 | switch (node->nodetype) { |
| 1786 | case LYS_CONTAINER: |
| 1787 | node_metadata_container((struct lys_node_container *)node, meta_obj); |
| 1788 | break; |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 1789 | case LYS_CHOICE: |
| 1790 | node_metadata_choice((struct lys_node_choice *)node, meta_obj); |
| 1791 | break; |
| 1792 | case LYS_LEAF: |
| 1793 | node_metadata_leaf((struct lys_node_leaf *)node, meta_obj); |
| 1794 | break; |
| 1795 | case LYS_LEAFLIST: |
| 1796 | node_metadata_leaflist((struct lys_node_leaflist *)node, meta_obj); |
| 1797 | break; |
| 1798 | case LYS_LIST: |
| 1799 | node_metadata_list((struct lys_node_list *)node, meta_obj); |
| 1800 | break; |
| 1801 | case LYS_ANYXML: |
| 1802 | node_metadata_anyxml((struct lys_node_anyxml *)node, meta_obj); |
| 1803 | break; |
| 1804 | case LYS_CASE: |
| 1805 | node_metadata_case((struct lys_node_case *)node, meta_obj); |
| 1806 | break; |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1807 | case LYS_RPC: |
| 1808 | node_metadata_rpc((struct lys_node_rpc *)node, meta_obj); |
| 1809 | break; |
| 1810 | default: /* LYS_OUTPUT */ |
Michal Vasko | 3fda9a9 | 2015-11-23 10:10:57 +0100 | [diff] [blame] | 1811 | ERROR("Internal: unuxpected nodetype (%s:%d)", __FILE__, __LINE__); |
| 1812 | break; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | /* just a precaution */ |
| 1816 | if (json_object_get_type(parent) != json_type_object) { |
| 1817 | ERROR("Internal: wrong JSON type (%s:%d)", __FILE__, __LINE__); |
| 1818 | free(obj_name); |
| 1819 | return 1; |
| 1820 | } |
| 1821 | |
| 1822 | json_object_object_add(parent, obj_name, meta_obj); |
| 1823 | free(obj_name); |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | static void |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1828 | node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module, json_object *data_json_parent) |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1829 | { |
| 1830 | struct lys_module *cur_module; |
| 1831 | struct lys_node *list_schema; |
| 1832 | struct lyd_node *child, *list_item; |
| 1833 | json_object *child_json, *list_child_json; |
| 1834 | char *child_name; |
| 1835 | int list_idx; |
| 1836 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1837 | if (data_tree->schema->nodetype & (LYS_OUTPUT | LYS_GROUPING)) { |
| 1838 | return; |
| 1839 | } |
| 1840 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1841 | /* add data_tree metadata */ |
| 1842 | if (node_add_metadata(data_tree->schema, module, data_json_parent)) { |
| 1843 | return; |
| 1844 | } |
| 1845 | |
| 1846 | /* get data_tree module */ |
| 1847 | cur_module = data_tree->schema->module; |
| 1848 | if (cur_module->type) { |
| 1849 | cur_module = ((struct lys_submodule *)cur_module)->belongsto; |
| 1850 | } |
| 1851 | |
| 1852 | if (!(data_tree->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
| 1853 | /* print correct data_tree JSON name */ |
| 1854 | if (cur_module == module) { |
| 1855 | asprintf(&child_name, "%s", data_tree->schema->name); |
| 1856 | } else { |
| 1857 | asprintf(&child_name, "%s:%s", cur_module->name, data_tree->schema->name); |
| 1858 | } |
| 1859 | |
| 1860 | /* go down in JSON object */ |
| 1861 | if (json_object_object_get_ex(data_json_parent, child_name, &child_json) == FALSE) { |
| 1862 | ERROR("Internal: failed to get JSON object \"%s\".", child_name); |
| 1863 | free(child_name); |
| 1864 | return; |
| 1865 | } |
| 1866 | free(child_name); |
| 1867 | |
| 1868 | if (data_tree->schema->nodetype == LYS_LIST) { |
| 1869 | if (json_object_get_type(child_json) != json_type_array) { |
| 1870 | ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__); |
| 1871 | return; |
| 1872 | } |
| 1873 | /* go down in data tree for every item, we process them all now, skip later |
| 1874 | * (metadata duplicate will be detected at the beginning of this function) */ |
| 1875 | list_idx = 0; |
| 1876 | list_schema = data_tree->schema; |
| 1877 | |
| 1878 | LY_TREE_FOR(data_tree, list_item) { |
| 1879 | /* another list member */ |
| 1880 | if (list_item->schema == list_schema) { |
| 1881 | list_child_json = json_object_array_get_idx(child_json, list_idx); |
| 1882 | if (!list_child_json) { |
| 1883 | ERROR("Internal: list \"%s\" idx out-of-bounds", list_schema->name); |
| 1884 | return; |
| 1885 | } |
| 1886 | LY_TREE_FOR(list_item->child, child) { |
| 1887 | node_add_metadata_recursive(child, cur_module, list_child_json); |
| 1888 | } |
| 1889 | |
| 1890 | ++list_idx; |
| 1891 | } |
| 1892 | } |
| 1893 | } else { |
| 1894 | if (json_object_get_type(child_json) != json_type_object) { |
| 1895 | ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__); |
| 1896 | return; |
| 1897 | } |
| 1898 | /* go down in data tree */ |
| 1899 | LY_TREE_FOR(data_tree->child, child) { |
| 1900 | node_add_metadata_recursive(child, cur_module, child_json); |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | static void |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1907 | node_add_model_metadata(const struct lys_module *module, json_object *parent) |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1908 | { |
| 1909 | json_object *obj; |
| 1910 | char *str; |
| 1911 | |
| 1912 | obj = json_object_new_object(); |
| 1913 | node_metadata_model(module, obj); |
| 1914 | asprintf(&str, "$@@%s", module->name); |
| 1915 | json_object_object_add(parent, str, obj); |
| 1916 | free(str); |
| 1917 | } |
| 1918 | |
| 1919 | static void |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1920 | node_add_children_with_metadata_recursive(const struct lys_node *node, const struct lys_module *module, json_object *parent) |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1921 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1922 | const struct lys_module *cur_module; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1923 | struct lys_node *child; |
| 1924 | json_object *node_json; |
| 1925 | char *json_name; |
| 1926 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1927 | if (node->nodetype & (LYS_OUTPUT | LYS_GROUPING)) { |
| 1928 | return; |
| 1929 | } |
| 1930 | |
| 1931 | if (node->nodetype & LYS_USES) { |
| 1932 | cur_module = module; |
| 1933 | node_json = parent; |
| 1934 | goto children; |
| 1935 | } |
| 1936 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1937 | /* add node metadata */ |
| 1938 | if (node_add_metadata(node, module, parent)) { |
| 1939 | ERROR("Internal: metadata duplicate for \"%s\".", node->name); |
| 1940 | return; |
| 1941 | } |
| 1942 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1943 | /* no other metadata */ |
| 1944 | if (!node->child) { |
| 1945 | return; |
| 1946 | } |
| 1947 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1948 | /* get node module */ |
| 1949 | cur_module = node->module; |
| 1950 | if (cur_module->type) { |
| 1951 | cur_module = ((struct lys_submodule *)cur_module)->belongsto; |
| 1952 | } |
| 1953 | |
| 1954 | /* create JSON object for child metadata */ |
| 1955 | node_json = json_object_new_object(); |
| 1956 | if (cur_module == module) { |
| 1957 | json_object_object_add(parent, node->name, node_json); |
| 1958 | } else { |
| 1959 | asprintf(&json_name, "%s:%s", cur_module->name, node->name); |
| 1960 | json_object_object_add(parent, json_name, node_json); |
| 1961 | free(json_name); |
| 1962 | } |
| 1963 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1964 | children: |
Michal Vasko | ea2ddd9 | 2016-03-17 15:43:58 +0100 | [diff] [blame] | 1965 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
| 1966 | LY_TREE_FOR(node->child, child) { |
| 1967 | node_add_children_with_metadata_recursive(child, cur_module, node_json); |
| 1968 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | static json_object * |
| 1973 | libyang_query(unsigned int session_key, const char *filter, int load_children) |
| 1974 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1975 | const struct lys_node *node; |
| 1976 | const struct lys_module *module = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1977 | struct session_with_mutex *locked_session; |
| 1978 | json_object *ret = NULL, *data; |
| 1979 | |
| 1980 | locked_session = session_get_locked(session_key, &ret); |
| 1981 | if (!locked_session) { |
| 1982 | ERROR("Locking failed or session not found."); |
| 1983 | goto finish; |
| 1984 | } |
| 1985 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1986 | session_user_activity(nc_session_get_username(locked_session->session)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 1987 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1988 | if (filter[0] == '/') { |
Michal Vasko | 56ec595 | 2016-04-05 14:30:23 +0200 | [diff] [blame] | 1989 | node = ly_ctx_get_node(nc_session_get_ctx(locked_session->session), NULL, filter); |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1990 | if (!node) { |
| 1991 | ret = create_error_reply("Failed to resolve XPath filter node."); |
| 1992 | goto finish; |
| 1993 | } |
| 1994 | } else { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 1995 | module = ly_ctx_get_module(nc_session_get_ctx(locked_session->session), filter, NULL); |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 1996 | if (!module) { |
| 1997 | ret = create_error_reply("Failed to find model."); |
| 1998 | goto finish; |
| 1999 | } |
| 2000 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2001 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 2002 | pthread_mutex_lock(&json_lock); |
| 2003 | data = json_object_new_object(); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2004 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 2005 | if (module) { |
| 2006 | node_add_model_metadata(module, data); |
| 2007 | if (load_children) { |
| 2008 | LY_TREE_FOR(module->data, node) { |
| 2009 | node_add_children_with_metadata_recursive(node, NULL, data); |
| 2010 | } |
| 2011 | } |
| 2012 | } else { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2013 | if (load_children) { |
| 2014 | node_add_children_with_metadata_recursive(node, NULL, data); |
| 2015 | } else { |
| 2016 | node_add_metadata(node, NULL, data); |
| 2017 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2018 | } |
| 2019 | |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 2020 | pthread_mutex_unlock(&json_lock); |
| 2021 | ret = create_data_reply(json_object_to_json_string(data)); |
| 2022 | json_object_put(data); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2023 | |
| 2024 | finish: |
Michal Vasko | a45770b | 2015-11-23 15:49:41 +0100 | [diff] [blame] | 2025 | session_unlock(locked_session); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2026 | return ret; |
| 2027 | } |
| 2028 | |
| 2029 | static json_object * |
| 2030 | libyang_merge(unsigned int session_key, const char *config) |
| 2031 | { |
| 2032 | struct lyd_node *data_tree = NULL, *sibling; |
| 2033 | struct session_with_mutex *locked_session; |
| 2034 | json_object *ret = NULL, *data_json = NULL; |
| 2035 | enum json_tokener_error err = 0; |
| 2036 | |
| 2037 | locked_session = session_get_locked(session_key, &ret); |
| 2038 | if (!locked_session) { |
| 2039 | ERROR("Locking failed or session not found."); |
| 2040 | goto finish; |
| 2041 | } |
| 2042 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2043 | session_user_activity(nc_session_get_username(locked_session->session)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2044 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2045 | data_tree = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_STRICT); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2046 | if (!data_tree) { |
| 2047 | ERROR("Creating data tree failed."); |
| 2048 | ret = create_error_reply("Failed to create data tree from JSON config."); |
| 2049 | session_unlock(locked_session); |
| 2050 | goto finish; |
| 2051 | } |
| 2052 | |
| 2053 | session_unlock(locked_session); |
| 2054 | |
| 2055 | pthread_mutex_lock(&json_lock); |
| 2056 | data_json = json_tokener_parse_verbose(config, &err); |
| 2057 | if (!data_json) { |
| 2058 | ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(err)); |
| 2059 | pthread_mutex_unlock(&json_lock); |
| 2060 | ret = create_error_reply(json_tokener_error_desc(err)); |
| 2061 | goto finish; |
| 2062 | } |
| 2063 | |
| 2064 | /* go simultaneously through both trees and add metadata */ |
| 2065 | LY_TREE_FOR(data_tree, sibling) { |
| 2066 | node_add_metadata_recursive(sibling, NULL, data_json); |
| 2067 | } |
| 2068 | pthread_mutex_unlock(&json_lock); |
| 2069 | ret = create_data_reply(json_object_to_json_string(data_json)); |
| 2070 | |
| 2071 | finish: |
| 2072 | LY_TREE_FOR(data_tree, sibling) { |
| 2073 | lyd_free(sibling); |
| 2074 | } |
| 2075 | json_object_put(data_json); |
| 2076 | return ret; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 2079 | /** |
| 2080 | * @} |
| 2081 | *//* netconf_operations */ |
| 2082 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2083 | void |
| 2084 | clb_print(NC_VERB_LEVEL level, const char *msg) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2085 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2086 | switch (level) { |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 2087 | case NC_VERB_ERROR: |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 2088 | ERROR("lib ERROR: %s", msg); |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 2089 | break; |
| 2090 | case NC_VERB_WARNING: |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 2091 | ERROR("lib WARNING: %s", msg); |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 2092 | break; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2093 | case NC_VERB_VERBOSE: |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 2094 | ERROR("lib VERBOSE: %s", msg); |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 2095 | break; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2096 | case NC_VERB_DEBUG: |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 2097 | DEBUG("lib DEBUG: %s", msg); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2098 | break; |
| 2099 | } |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 2100 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2101 | if (level == NC_VERB_ERROR) { |
| 2102 | /* return global error */ |
Michal Vasko | 56c02b2 | 2016-04-06 09:59:09 +0200 | [diff] [blame] | 2103 | netconf_callback_error_process(msg); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2104 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2105 | } |
| 2106 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2107 | /** |
Tomas Cejka | 6e8f426 | 2013-07-10 09:20:19 +0200 | [diff] [blame] | 2108 | * Receive message from client over UNIX socket and return pointer to it. |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2109 | * Caller should free message memory. |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2110 | * \param[in] client socket descriptor of client |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2111 | * \return pointer to message |
| 2112 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2113 | char * |
| 2114 | get_framed_message(int client) |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2115 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2116 | /* read json in chunked framing */ |
| 2117 | unsigned int buffer_size = 0; |
| 2118 | ssize_t buffer_len = 0; |
| 2119 | char *buffer = NULL; |
| 2120 | char c; |
| 2121 | ssize_t ret; |
| 2122 | int i, chunk_len; |
| 2123 | char chunk_len_str[12]; |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2124 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2125 | while (1) { |
| 2126 | /* read chunk length */ |
| 2127 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 2128 | if (buffer != NULL) { |
| 2129 | free (buffer); |
| 2130 | buffer = NULL; |
| 2131 | } |
| 2132 | break; |
| 2133 | } |
| 2134 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 2135 | if (buffer != NULL) { |
| 2136 | free (buffer); |
| 2137 | buffer = NULL; |
| 2138 | } |
| 2139 | break; |
| 2140 | } |
| 2141 | i=0; |
| 2142 | memset (chunk_len_str, 0, 12); |
| 2143 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 2144 | if (i==0 && c == '#') { |
| 2145 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 2146 | /* end but invalid */ |
| 2147 | if (buffer != NULL) { |
| 2148 | free (buffer); |
| 2149 | buffer = NULL; |
| 2150 | } |
| 2151 | } |
| 2152 | /* end of message, double-loop break */ |
| 2153 | goto msg_complete; |
| 2154 | } |
| 2155 | chunk_len_str[i++] = c; |
| 2156 | if (i==11) { |
| 2157 | ERROR("Message is too long, buffer for length is not big enought!!!!"); |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
| 2161 | if (c != '\n') { |
| 2162 | if (buffer != NULL) { |
| 2163 | free (buffer); |
| 2164 | buffer = NULL; |
| 2165 | } |
| 2166 | break; |
| 2167 | } |
| 2168 | chunk_len_str[i] = 0; |
| 2169 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 2170 | if (buffer != NULL) { |
| 2171 | free (buffer); |
| 2172 | buffer = NULL; |
| 2173 | } |
| 2174 | break; |
| 2175 | } |
| 2176 | buffer_size += chunk_len+1; |
| 2177 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 2178 | memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1); |
| 2179 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 2180 | if (buffer != NULL) { |
| 2181 | free (buffer); |
| 2182 | buffer = NULL; |
| 2183 | } |
| 2184 | break; |
| 2185 | } |
| 2186 | buffer_len += ret; |
| 2187 | } |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2188 | msg_complete: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2189 | return buffer; |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2192 | NC_DATASTORE |
| 2193 | parse_datastore(const char *ds) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2194 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2195 | if (strcmp(ds, "running") == 0) { |
| 2196 | return NC_DATASTORE_RUNNING; |
| 2197 | } else if (strcmp(ds, "startup") == 0) { |
| 2198 | return NC_DATASTORE_STARTUP; |
| 2199 | } else if (strcmp(ds, "candidate") == 0) { |
| 2200 | return NC_DATASTORE_CANDIDATE; |
| 2201 | } else if (strcmp(ds, "url") == 0) { |
| 2202 | return NC_DATASTORE_URL; |
| 2203 | } else if (strcmp(ds, "config") == 0) { |
| 2204 | return NC_DATASTORE_CONFIG; |
| 2205 | } |
| 2206 | return -1; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2209 | NC_RPC_EDIT_TESTOPT |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2210 | parse_testopt(const char *t) |
Tomas Cejka | 5ae8dfb | 2014-02-14 23:42:17 +0100 | [diff] [blame] | 2211 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2212 | if (strcmp(t, "notset") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2213 | return NC_RPC_EDIT_TESTOPT_UNKNOWN; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2214 | } else if (strcmp(t, "testset") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2215 | return NC_RPC_EDIT_TESTOPT_TESTSET; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2216 | } else if (strcmp(t, "set") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2217 | return NC_RPC_EDIT_TESTOPT_SET; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2218 | } else if (strcmp(t, "test") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2219 | return NC_RPC_EDIT_TESTOPT_TEST; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2220 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2221 | return NC_RPC_EDIT_TESTOPT_UNKNOWN; |
Tomas Cejka | 5ae8dfb | 2014-02-14 23:42:17 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2224 | json_object * |
| 2225 | create_error_reply(const char *errmess) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2226 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2227 | json_object *reply, *array; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2228 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2229 | pthread_mutex_lock(&json_lock); |
| 2230 | reply = json_object_new_object(); |
| 2231 | array = json_object_new_array(); |
| 2232 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 2233 | json_object_array_add(array, json_object_new_string(errmess)); |
| 2234 | json_object_object_add(reply, "errors", array); |
| 2235 | pthread_mutex_unlock(&json_lock); |
| 2236 | |
| 2237 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2238 | } |
| 2239 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2240 | json_object * |
| 2241 | create_data_reply(const char *data) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2242 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2243 | pthread_mutex_lock(&json_lock); |
| 2244 | json_object *reply = json_object_new_object(); |
| 2245 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 2246 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 2247 | pthread_mutex_unlock(&json_lock); |
| 2248 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2249 | } |
| 2250 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2251 | json_object * |
| 2252 | create_ok_reply(void) |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2253 | { |
Michal Vasko | 365dc4c | 2016-03-17 10:03:58 +0100 | [diff] [blame] | 2254 | json_object *reply; |
| 2255 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2256 | pthread_mutex_lock(&json_lock); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2257 | reply = json_object_new_object(); |
| 2258 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 2259 | pthread_mutex_unlock(&json_lock); |
| 2260 | return reply; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2263 | json_object * |
| 2264 | create_replies(void) |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2265 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2266 | json_object *replies; |
| 2267 | |
| 2268 | pthread_mutex_lock(&json_lock); |
| 2269 | replies = json_object_new_object(); |
| 2270 | pthread_mutex_unlock(&json_lock); |
| 2271 | |
| 2272 | return replies; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2273 | } |
| 2274 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2275 | void |
| 2276 | add_reply(json_object *replies, json_object *reply, unsigned int session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2277 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2278 | char *str; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2279 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2280 | asprintf(&str, "%u", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2281 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2282 | pthread_mutex_lock(&json_lock); |
| 2283 | json_object_object_add(replies, str, reply); |
| 2284 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2285 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2286 | free(str); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2287 | } |
| 2288 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2289 | char * |
| 2290 | get_param_string(json_object *data, const char *name) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2291 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2292 | json_object *js_tmp = NULL; |
| 2293 | char *res = NULL; |
| 2294 | if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) { |
| 2295 | res = strdup(json_object_get_string(js_tmp)); |
| 2296 | } |
| 2297 | return res; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2300 | json_object * |
| 2301 | handle_op_connect(json_object *request) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2302 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2303 | char *host = NULL; |
| 2304 | char *port = NULL; |
| 2305 | char *user = NULL; |
| 2306 | char *pass = NULL; |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 2307 | char *privkey = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2308 | json_object *reply = NULL; |
| 2309 | unsigned int session_key = 0; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2310 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2311 | DEBUG("Request: connect"); |
| 2312 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2313 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2314 | host = get_param_string(request, "host"); |
| 2315 | port = get_param_string(request, "port"); |
| 2316 | user = get_param_string(request, "user"); |
| 2317 | pass = get_param_string(request, "pass"); |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 2318 | privkey = get_param_string(request, "privatekey"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2319 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2320 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2321 | |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 2322 | if (host == NULL) { |
| 2323 | host = "localhost"; |
| 2324 | } |
| 2325 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2326 | DEBUG("host: %s, port: %s, user: %s", host, port, user); |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 2327 | if (user == NULL) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2328 | ERROR("Cannot connect - insufficient input."); |
| 2329 | session_key = 0; |
| 2330 | } else { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2331 | session_key = netconf_connect(host, port, user, pass, privkey); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2332 | DEBUG("Session key: %u", session_key); |
| 2333 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2334 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2335 | GETSPEC_ERR_REPLY |
| 2336 | |
| 2337 | pthread_mutex_lock(&json_lock); |
| 2338 | if (session_key == 0) { |
| 2339 | /* negative reply */ |
| 2340 | if (err_reply == NULL) { |
| 2341 | reply = json_object_new_object(); |
| 2342 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 2343 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
| 2344 | ERROR("Connection failed."); |
| 2345 | } else { |
| 2346 | /* use filled err_reply from libnetconf's callback */ |
| 2347 | reply = err_reply; |
| 2348 | ERROR("Connect - error from libnetconf's callback."); |
| 2349 | } |
| 2350 | } else { |
| 2351 | /* positive reply */ |
| 2352 | reply = json_object_new_object(); |
| 2353 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 2354 | json_object_object_add(reply, "session", json_object_new_int(session_key)); |
| 2355 | } |
| 2356 | memset(pass, 0, strlen(pass)); |
| 2357 | pthread_mutex_unlock(&json_lock); |
| 2358 | CHECK_AND_FREE(host); |
| 2359 | CHECK_AND_FREE(user); |
| 2360 | CHECK_AND_FREE(port); |
| 2361 | CHECK_AND_FREE(pass); |
Michal Vasko | da5ccc8 | 2015-11-25 10:42:49 +0100 | [diff] [blame] | 2362 | CHECK_AND_FREE(privkey); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2363 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2364 | } |
| 2365 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2366 | json_object * |
| 2367 | handle_op_disconnect(json_object *UNUSED(request), unsigned int session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2368 | { |
Michal Vasko | 5326024 | 2016-04-06 11:18:18 +0200 | [diff] [blame] | 2369 | json_object *reply = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2370 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2371 | DEBUG("Request: disconnect (session %u)", session_key); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2372 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2373 | if (netconf_close(session_key, &reply) != EXIT_SUCCESS) { |
| 2374 | CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.") |
| 2375 | } else { |
| 2376 | reply = create_ok_reply(); |
| 2377 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2378 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2379 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2380 | } |
| 2381 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2382 | json_object * |
| 2383 | handle_op_get(json_object *request, unsigned int session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2384 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2385 | char *filter = NULL; |
| 2386 | char *data = NULL; |
| 2387 | json_object *reply = NULL, *obj; |
| 2388 | int strict; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2389 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2390 | DEBUG("Request: get (session %u)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2391 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2392 | pthread_mutex_lock(&json_lock); |
| 2393 | filter = get_param_string(request, "filter"); |
| 2394 | if (json_object_object_get_ex(request, "strict", &obj) == FALSE) { |
| 2395 | pthread_mutex_unlock(&json_lock); |
| 2396 | reply = create_error_reply("Missing strict parameter."); |
| 2397 | return reply; |
| 2398 | } |
| 2399 | strict = json_object_get_boolean(obj); |
| 2400 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2401 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2402 | if ((data = netconf_get(session_key, filter, strict, &reply)) == NULL) { |
| 2403 | CHECK_ERR_SET_REPLY_ERR("Get information failed.") |
| 2404 | } else { |
| 2405 | reply = create_data_reply(data); |
| 2406 | free(data); |
| 2407 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2408 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2409 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2410 | } |
| 2411 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2412 | json_object * |
| 2413 | handle_op_getconfig(json_object *request, unsigned int session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2414 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2415 | NC_DATASTORE ds_type_s = -1; |
| 2416 | char *filter = NULL; |
| 2417 | char *data = NULL; |
| 2418 | char *source = NULL; |
| 2419 | json_object *reply = NULL, *obj; |
| 2420 | int strict; |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 2421 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2422 | DEBUG("Request: get-config (session %u)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2423 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2424 | pthread_mutex_lock(&json_lock); |
| 2425 | filter = get_param_string(request, "filter"); |
| 2426 | source = get_param_string(request, "source"); |
| 2427 | if (source != NULL) { |
| 2428 | ds_type_s = parse_datastore(source); |
| 2429 | } |
| 2430 | if (json_object_object_get_ex(request, "strict", &obj) == FALSE) { |
| 2431 | pthread_mutex_unlock(&json_lock); |
| 2432 | reply = create_error_reply("Missing strict parameter."); |
| 2433 | return reply; |
| 2434 | } |
| 2435 | strict = json_object_get_boolean(obj); |
| 2436 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2437 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2438 | if ((int)ds_type_s == -1) { |
| 2439 | reply = create_error_reply("Invalid source repository type requested."); |
| 2440 | goto finalize; |
| 2441 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2442 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2443 | if ((data = netconf_getconfig(session_key, ds_type_s, filter, strict, &reply)) == NULL) { |
| 2444 | CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.") |
| 2445 | } else { |
| 2446 | reply = create_data_reply(data); |
| 2447 | free(data); |
| 2448 | } |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 2449 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2450 | finalize: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2451 | CHECK_AND_FREE(filter); |
| 2452 | CHECK_AND_FREE(source); |
| 2453 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2456 | json_object * |
| 2457 | handle_op_editconfig(json_object *request, unsigned int session_key, int idx) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2458 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2459 | NC_DATASTORE ds_type_t = -1; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2460 | NC_RPC_EDIT_DFLTOP defop_type = 0; |
| 2461 | NC_RPC_EDIT_ERROPT erropt_type = 0; |
| 2462 | NC_RPC_EDIT_TESTOPT testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2463 | char *defop = NULL; |
| 2464 | char *erropt = NULL; |
| 2465 | char *config = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2466 | char *target = NULL; |
| 2467 | char *testopt = NULL; |
| 2468 | char *urisource = NULL; |
| 2469 | json_object *reply = NULL, *configs, *obj; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2470 | struct lyd_node *content; |
| 2471 | struct session_with_mutex *locked_session; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2472 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2473 | DEBUG("Request: edit-config (session %u)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2474 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2475 | pthread_mutex_lock(&json_lock); |
| 2476 | /* get parameters */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2477 | if (json_object_object_get_ex(request, "configs", &configs) == FALSE) { |
| 2478 | pthread_mutex_unlock(&json_lock); |
| 2479 | reply = create_error_reply("Missing configs parameter."); |
| 2480 | goto finalize; |
| 2481 | } |
| 2482 | obj = json_object_array_get_idx(configs, idx); |
| 2483 | config = strdup(json_object_get_string(obj)); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2484 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2485 | target = get_param_string(request, "target"); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2486 | defop = get_param_string(request, "default-operation"); |
| 2487 | erropt = get_param_string(request, "error-option"); |
| 2488 | urisource = get_param_string(request, "uri-source"); |
| 2489 | testopt = get_param_string(request, "test-option"); |
| 2490 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2491 | |
Michal Vasko | b8f5d44 | 2016-04-05 14:48:37 +0200 | [diff] [blame] | 2492 | if (!target) { |
| 2493 | ERROR("Missing the target parameter."); |
| 2494 | goto finalize; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2495 | } |
Michal Vasko | b8f5d44 | 2016-04-05 14:48:37 +0200 | [diff] [blame] | 2496 | ds_type_t = parse_datastore(target); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2497 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2498 | if (defop != NULL) { |
| 2499 | if (strcmp(defop, "merge") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2500 | defop_type = NC_RPC_EDIT_DFLTOP_MERGE; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2501 | } else if (strcmp(defop, "replace") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2502 | defop_type = NC_RPC_EDIT_DFLTOP_REPLACE; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2503 | } else if (strcmp(defop, "none") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2504 | defop_type = NC_RPC_EDIT_DFLTOP_NONE; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2505 | } else { |
| 2506 | reply = create_error_reply("Invalid default-operation parameter."); |
| 2507 | goto finalize; |
| 2508 | } |
| 2509 | } else { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2510 | defop_type = NC_RPC_EDIT_DFLTOP_UNKNOWN; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2511 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2512 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2513 | if (erropt != NULL) { |
| 2514 | if (strcmp(erropt, "continue-on-error") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2515 | erropt_type = NC_RPC_EDIT_ERROPT_CONTINUE; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2516 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2517 | erropt_type = NC_RPC_EDIT_ERROPT_STOP; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2518 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2519 | erropt_type = NC_RPC_EDIT_ERROPT_ROLLBACK; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2520 | } else { |
| 2521 | reply = create_error_reply("Invalid error-option parameter."); |
| 2522 | goto finalize; |
| 2523 | } |
| 2524 | } else { |
| 2525 | erropt_type = 0; |
| 2526 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2527 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2528 | if ((config && urisource) || (!config && !urisource)) { |
| 2529 | reply = create_error_reply("Invalid config and uri-source data parameters."); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2530 | goto finalize; |
| 2531 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2532 | |
| 2533 | if (config) { |
| 2534 | locked_session = session_get_locked(session_key, NULL); |
| 2535 | if (!locked_session) { |
| 2536 | ERROR("Unknown session or locking failed."); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2537 | goto finalize; |
| 2538 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2539 | |
| 2540 | content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_EDIT); |
| 2541 | session_unlock(locked_session); |
| 2542 | |
Michal Vasko | c7a8f3c | 2016-04-05 14:53:33 +0200 | [diff] [blame] | 2543 | if (!content) { |
| 2544 | ERROR("Failed to parse edit-config content."); |
| 2545 | goto finalize; |
| 2546 | } |
| 2547 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2548 | free(config); |
Michal Vasko | c7a8f3c | 2016-04-05 14:53:33 +0200 | [diff] [blame] | 2549 | config = NULL; |
| 2550 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2551 | lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS); |
| 2552 | lyd_free_withsiblings(content); |
Michal Vasko | c7a8f3c | 2016-04-05 14:53:33 +0200 | [diff] [blame] | 2553 | if (!config) { |
| 2554 | ERROR("Failed to print edit-config content."); |
| 2555 | goto finalize; |
| 2556 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2557 | } else { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2558 | config = urisource; |
| 2559 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2560 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2561 | if (testopt != NULL) { |
| 2562 | testopt_type = parse_testopt(testopt); |
| 2563 | } else { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2564 | testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2565 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2566 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2567 | reply = netconf_editconfig(session_key, ds_type_t, defop_type, erropt_type, testopt_type, config); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2568 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2569 | CHECK_ERR_SET_REPLY |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 2570 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2571 | finalize: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2572 | CHECK_AND_FREE(defop); |
| 2573 | CHECK_AND_FREE(erropt); |
| 2574 | CHECK_AND_FREE(config); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2575 | CHECK_AND_FREE(urisource); |
| 2576 | CHECK_AND_FREE(target); |
| 2577 | CHECK_AND_FREE(testopt); |
| 2578 | |
| 2579 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2580 | } |
| 2581 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2582 | json_object * |
| 2583 | handle_op_copyconfig(json_object *request, unsigned int session_key, int idx) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2584 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2585 | NC_DATASTORE ds_type_s = -1; |
| 2586 | NC_DATASTORE ds_type_t = -1; |
| 2587 | char *config = NULL; |
| 2588 | char *target = NULL; |
| 2589 | char *source = NULL; |
| 2590 | char *uri_src = NULL; |
| 2591 | char *uri_trg = NULL; |
| 2592 | json_object *reply = NULL, *configs, *obj; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2593 | struct lyd_node *content; |
| 2594 | struct session_with_mutex *locked_session; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2595 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2596 | DEBUG("Request: copy-config (session %u)", session_key); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2597 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2598 | /* get parameters */ |
| 2599 | pthread_mutex_lock(&json_lock); |
| 2600 | target = get_param_string(request, "target"); |
| 2601 | source = get_param_string(request, "source"); |
| 2602 | uri_src = get_param_string(request, "uri-source"); |
| 2603 | uri_trg = get_param_string(request, "uri-target"); |
| 2604 | if (!strcmp(source, "config")) { |
| 2605 | if (json_object_object_get_ex(request, "configs", &configs) == FALSE) { |
| 2606 | pthread_mutex_unlock(&json_lock); |
| 2607 | reply = create_error_reply("Missing configs parameter."); |
| 2608 | goto finalize; |
| 2609 | } |
| 2610 | obj = json_object_array_get_idx(configs, idx); |
| 2611 | if (!obj) { |
| 2612 | pthread_mutex_unlock(&json_lock); |
| 2613 | reply = create_error_reply("Configs array parameter shorter than sessions."); |
| 2614 | goto finalize; |
| 2615 | } |
| 2616 | config = strdup(json_object_get_string(obj)); |
| 2617 | } |
| 2618 | pthread_mutex_unlock(&json_lock); |
| 2619 | |
| 2620 | if (target != NULL) { |
| 2621 | ds_type_t = parse_datastore(target); |
| 2622 | } |
| 2623 | if (source != NULL) { |
| 2624 | ds_type_s = parse_datastore(source); |
| 2625 | } |
| 2626 | |
| 2627 | if ((int)ds_type_s == -1) { |
| 2628 | /* invalid source datastore specified */ |
| 2629 | reply = create_error_reply("Invalid source repository type requested."); |
| 2630 | goto finalize; |
| 2631 | } |
| 2632 | |
| 2633 | if ((int)ds_type_t == -1) { |
| 2634 | /* invalid target datastore specified */ |
| 2635 | reply = create_error_reply("Invalid target repository type requested."); |
| 2636 | goto finalize; |
| 2637 | } |
| 2638 | |
| 2639 | if (ds_type_s == NC_DATASTORE_URL) { |
| 2640 | if (uri_src == NULL) { |
| 2641 | uri_src = ""; |
| 2642 | } |
| 2643 | } |
| 2644 | if (ds_type_t == NC_DATASTORE_URL) { |
| 2645 | if (uri_trg == NULL) { |
| 2646 | uri_trg = ""; |
| 2647 | } |
| 2648 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2649 | |
| 2650 | if (config) { |
| 2651 | locked_session = session_get_locked(session_key, NULL); |
| 2652 | if (!locked_session) { |
| 2653 | ERROR("Unknown session or locking failed."); |
| 2654 | goto finalize; |
| 2655 | } |
| 2656 | |
| 2657 | content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_CONFIG); |
| 2658 | session_unlock(locked_session); |
| 2659 | |
| 2660 | free(config); |
| 2661 | lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS); |
| 2662 | lyd_free_withsiblings(content); |
| 2663 | } |
| 2664 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2665 | reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg); |
| 2666 | |
| 2667 | CHECK_ERR_SET_REPLY |
| 2668 | |
| 2669 | finalize: |
| 2670 | CHECK_AND_FREE(config); |
| 2671 | CHECK_AND_FREE(target); |
| 2672 | CHECK_AND_FREE(source); |
| 2673 | CHECK_AND_FREE(uri_src); |
| 2674 | CHECK_AND_FREE(uri_trg); |
| 2675 | |
| 2676 | return reply; |
| 2677 | } |
| 2678 | |
| 2679 | json_object * |
| 2680 | handle_op_deleteconfig(json_object *request, unsigned int session_key) |
| 2681 | { |
| 2682 | json_object *reply; |
| 2683 | NC_DATASTORE ds_type = -1; |
| 2684 | char *target, *url; |
| 2685 | |
| 2686 | DEBUG("Request: delete-config (session %u)", session_key); |
| 2687 | |
| 2688 | pthread_mutex_lock(&json_lock); |
| 2689 | target = get_param_string(request, "target"); |
| 2690 | url = get_param_string(request, "url"); |
| 2691 | pthread_mutex_unlock(&json_lock); |
| 2692 | |
| 2693 | if (target != NULL) { |
| 2694 | ds_type = parse_datastore(target); |
| 2695 | } |
| 2696 | if ((int)ds_type == -1) { |
| 2697 | reply = create_error_reply("Invalid target repository type requested."); |
| 2698 | goto finalize; |
| 2699 | } |
| 2700 | if (ds_type == NC_DATASTORE_URL) { |
| 2701 | if (!url) { |
| 2702 | url = ""; |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | reply = netconf_deleteconfig(session_key, ds_type, url); |
| 2707 | |
| 2708 | CHECK_ERR_SET_REPLY |
| 2709 | if (reply == NULL) { |
| 2710 | reply = create_ok_reply(); |
| 2711 | } |
| 2712 | |
| 2713 | finalize: |
| 2714 | CHECK_AND_FREE(target); |
| 2715 | CHECK_AND_FREE(url); |
| 2716 | return reply; |
| 2717 | } |
| 2718 | |
| 2719 | json_object * |
| 2720 | handle_op_lock(json_object *request, unsigned int session_key) |
| 2721 | { |
| 2722 | json_object *reply; |
| 2723 | NC_DATASTORE ds_type = -1; |
| 2724 | char *target; |
| 2725 | |
| 2726 | DEBUG("Request: lock (session %u)", session_key); |
| 2727 | |
| 2728 | pthread_mutex_lock(&json_lock); |
| 2729 | target = get_param_string(request, "target"); |
| 2730 | pthread_mutex_unlock(&json_lock); |
| 2731 | |
| 2732 | if (target != NULL) { |
| 2733 | ds_type = parse_datastore(target); |
| 2734 | } |
| 2735 | if ((int)ds_type == -1) { |
| 2736 | reply = create_error_reply("Invalid target repository type requested."); |
| 2737 | goto finalize; |
| 2738 | } |
| 2739 | |
| 2740 | reply = netconf_lock(session_key, ds_type); |
| 2741 | |
| 2742 | CHECK_ERR_SET_REPLY |
| 2743 | if (reply == NULL) { |
| 2744 | reply = create_ok_reply(); |
| 2745 | } |
| 2746 | |
| 2747 | finalize: |
| 2748 | CHECK_AND_FREE(target); |
| 2749 | return reply; |
| 2750 | } |
| 2751 | |
| 2752 | json_object * |
| 2753 | handle_op_unlock(json_object *request, unsigned int session_key) |
| 2754 | { |
| 2755 | json_object *reply; |
| 2756 | NC_DATASTORE ds_type = -1; |
| 2757 | char *target; |
| 2758 | |
| 2759 | DEBUG("Request: unlock (session %u)", session_key); |
| 2760 | |
| 2761 | pthread_mutex_lock(&json_lock); |
| 2762 | target = get_param_string(request, "target"); |
| 2763 | pthread_mutex_unlock(&json_lock); |
| 2764 | |
| 2765 | if (target != NULL) { |
| 2766 | ds_type = parse_datastore(target); |
| 2767 | } |
| 2768 | if ((int)ds_type == -1) { |
| 2769 | reply = create_error_reply("Invalid target repository type requested."); |
| 2770 | goto finalize; |
| 2771 | } |
| 2772 | |
| 2773 | reply = netconf_unlock(session_key, ds_type); |
| 2774 | |
| 2775 | CHECK_ERR_SET_REPLY |
| 2776 | if (reply == NULL) { |
| 2777 | reply = create_ok_reply(); |
| 2778 | } |
| 2779 | |
| 2780 | finalize: |
| 2781 | CHECK_AND_FREE(target); |
| 2782 | return reply; |
| 2783 | } |
| 2784 | |
| 2785 | json_object * |
| 2786 | handle_op_kill(json_object *request, unsigned int session_key) |
| 2787 | { |
| 2788 | json_object *reply = NULL; |
| 2789 | char *sid = NULL; |
| 2790 | |
| 2791 | DEBUG("Request: kill-session (session %u)", session_key); |
| 2792 | |
| 2793 | pthread_mutex_lock(&json_lock); |
| 2794 | sid = get_param_string(request, "session-id"); |
| 2795 | pthread_mutex_unlock(&json_lock); |
| 2796 | |
| 2797 | if (sid == NULL) { |
| 2798 | reply = create_error_reply("Missing session-id parameter."); |
| 2799 | goto finalize; |
| 2800 | } |
| 2801 | |
| 2802 | reply = netconf_killsession(session_key, sid); |
| 2803 | |
| 2804 | CHECK_ERR_SET_REPLY |
| 2805 | |
| 2806 | finalize: |
| 2807 | CHECK_AND_FREE(sid); |
| 2808 | return reply; |
| 2809 | } |
| 2810 | |
| 2811 | json_object * |
| 2812 | handle_op_info(json_object *UNUSED(request), unsigned int session_key) |
| 2813 | { |
| 2814 | json_object *reply = NULL; |
| 2815 | struct session_with_mutex *locked_session = NULL; |
| 2816 | DEBUG("Request: get info about session %u", session_key); |
| 2817 | |
| 2818 | DEBUG("LOCK wrlock %s", __func__); |
| 2819 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 2820 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 2821 | } |
| 2822 | |
| 2823 | for (locked_session = netconf_sessions_list; |
| 2824 | locked_session && (locked_session->session_key != session_key); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2825 | locked_session = locked_session->next); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2826 | if (locked_session != NULL) { |
| 2827 | DEBUG("LOCK mutex %s", __func__); |
| 2828 | pthread_mutex_lock(&locked_session->lock); |
| 2829 | DEBUG("UNLOCK wrlock %s", __func__); |
| 2830 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 2831 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 2832 | } |
| 2833 | if (locked_session->hello_message != NULL) { |
| 2834 | reply = locked_session->hello_message; |
| 2835 | } else { |
| 2836 | reply = create_error_reply("Invalid session identifier."); |
| 2837 | } |
| 2838 | DEBUG("UNLOCK mutex %s", __func__); |
| 2839 | pthread_mutex_unlock(&locked_session->lock); |
| 2840 | } else { |
| 2841 | DEBUG("UNLOCK wrlock %s", __func__); |
| 2842 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 2843 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 2844 | } |
| 2845 | reply = create_error_reply("Invalid session identifier."); |
| 2846 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2847 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2848 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2851 | json_object * |
| 2852 | handle_op_generic(json_object *request, unsigned int session_key, int idx) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2853 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2854 | json_object *reply = NULL, *contents, *obj; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2855 | char *content = NULL, *str; |
| 2856 | struct lyd_node *data = NULL, *node_content; |
| 2857 | struct session_with_mutex *locked_session; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2858 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2859 | DEBUG("Request: generic request (session %u)", session_key); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2860 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2861 | pthread_mutex_lock(&json_lock); |
| 2862 | if (json_object_object_get_ex(request, "contents", &contents) == FALSE) { |
| 2863 | pthread_mutex_unlock(&json_lock); |
| 2864 | reply = create_error_reply("Missing contents parameter."); |
| 2865 | goto finalize; |
| 2866 | } |
| 2867 | obj = json_object_array_get_idx(contents, idx); |
| 2868 | if (!obj) { |
| 2869 | pthread_mutex_unlock(&json_lock); |
| 2870 | reply = create_error_reply("Contents array parameter shorter than sessions."); |
| 2871 | goto finalize; |
| 2872 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2873 | content = strdup(json_object_get_string(obj)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2874 | pthread_mutex_unlock(&json_lock); |
| 2875 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2876 | locked_session = session_get_locked(session_key, NULL); |
| 2877 | if (!locked_session) { |
| 2878 | ERROR("Unknown session or locking failed."); |
| 2879 | goto finalize; |
| 2880 | } |
| 2881 | |
| 2882 | node_content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), content, LYD_JSON, LYD_OPT_RPC); |
| 2883 | session_unlock(locked_session); |
| 2884 | |
| 2885 | free(content); |
| 2886 | lyd_print_mem(&content, node_content, LYD_XML, LYP_WITHSIBLINGS); |
| 2887 | lyd_free_withsiblings(node_content); |
| 2888 | |
| 2889 | reply = netconf_generic(session_key, content, &data); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2890 | if (reply == NULL) { |
| 2891 | GETSPEC_ERR_REPLY |
| 2892 | if (err_reply != NULL) { |
| 2893 | /* use filled err_reply from libnetconf's callback */ |
| 2894 | reply = err_reply; |
| 2895 | } |
| 2896 | } else { |
| 2897 | if (data == NULL) { |
| 2898 | pthread_mutex_lock(&json_lock); |
| 2899 | reply = json_object_new_object(); |
| 2900 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 2901 | pthread_mutex_unlock(&json_lock); |
| 2902 | } else { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2903 | lyd_print_mem(&str, data, LYD_JSON, LYP_WITHSIBLINGS); |
| 2904 | lyd_free_withsiblings(data); |
| 2905 | reply = create_data_reply(str); |
| 2906 | free(str); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | finalize: |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2911 | CHECK_AND_FREE(content); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2912 | return reply; |
| 2913 | } |
| 2914 | |
| 2915 | json_object * |
| 2916 | handle_op_getschema(json_object *request, unsigned int session_key) |
| 2917 | { |
| 2918 | char *data = NULL; |
| 2919 | char *identifier = NULL; |
| 2920 | char *version = NULL; |
| 2921 | char *format = NULL; |
| 2922 | json_object *reply = NULL; |
| 2923 | |
| 2924 | DEBUG("Request: get-schema (session %u)", session_key); |
| 2925 | |
| 2926 | pthread_mutex_lock(&json_lock); |
| 2927 | identifier = get_param_string(request, "identifier"); |
| 2928 | version = get_param_string(request, "version"); |
| 2929 | format = get_param_string(request, "format"); |
| 2930 | pthread_mutex_unlock(&json_lock); |
| 2931 | |
| 2932 | if (identifier == NULL) { |
| 2933 | reply = create_error_reply("No identifier for get-schema supplied."); |
| 2934 | goto finalize; |
| 2935 | } |
| 2936 | |
| 2937 | DEBUG("get-schema(version: %s, format: %s)", version, format); |
| 2938 | if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) { |
| 2939 | CHECK_ERR_SET_REPLY_ERR("Get models operation failed.") |
| 2940 | } else { |
| 2941 | reply = create_data_reply(data); |
| 2942 | free(data); |
| 2943 | } |
| 2944 | |
| 2945 | finalize: |
| 2946 | CHECK_AND_FREE(identifier); |
| 2947 | CHECK_AND_FREE(version); |
| 2948 | CHECK_AND_FREE(format); |
| 2949 | return reply; |
| 2950 | } |
| 2951 | |
| 2952 | json_object * |
| 2953 | handle_op_reloadhello(json_object *UNUSED(request), unsigned int session_key) |
| 2954 | { |
| 2955 | struct nc_session *temp_session = NULL; |
| 2956 | struct session_with_mutex * locked_session = NULL; |
| 2957 | json_object *reply = NULL; |
| 2958 | |
| 2959 | DEBUG("Request: reload hello (session %u)", session_key); |
| 2960 | |
| 2961 | DEBUG("LOCK wrlock %s", __func__); |
| 2962 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
| 2963 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 2964 | return NULL; |
| 2965 | } |
| 2966 | |
| 2967 | for (locked_session = netconf_sessions_list; |
| 2968 | locked_session && (locked_session->session_key != session_key); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2969 | locked_session = locked_session->next); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2970 | if ((locked_session != NULL) && (locked_session->hello_message != NULL)) { |
| 2971 | DEBUG("LOCK mutex %s", __func__); |
| 2972 | pthread_mutex_lock(&locked_session->lock); |
| 2973 | DEBUG("creating temporary NC session."); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 2974 | temp_session = nc_connect_ssh_channel(locked_session->session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2975 | if (temp_session != NULL) { |
| 2976 | prepare_status_message(locked_session, temp_session); |
| 2977 | DEBUG("closing temporal NC session."); |
Michal Vasko | a959005 | 2016-03-08 10:12:24 +0100 | [diff] [blame] | 2978 | nc_session_free(temp_session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2979 | temp_session = NULL; |
| 2980 | } else { |
| 2981 | DEBUG("Reload hello failed due to channel establishment"); |
| 2982 | reply = create_error_reply("Reload was unsuccessful, connection failed."); |
| 2983 | } |
| 2984 | DEBUG("UNLOCK mutex %s", __func__); |
| 2985 | pthread_mutex_unlock(&locked_session->lock); |
| 2986 | DEBUG("UNLOCK wrlock %s", __func__); |
| 2987 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 2988 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 2989 | } |
| 2990 | } else { |
| 2991 | DEBUG("UNLOCK wrlock %s", __func__); |
| 2992 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 2993 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 2994 | } |
| 2995 | reply = create_error_reply("Invalid session identifier."); |
| 2996 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2997 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 2998 | if ((reply == NULL) && (locked_session->hello_message != NULL)) { |
| 2999 | reply = locked_session->hello_message; |
| 3000 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 3001 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3002 | return reply; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3005 | void |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3006 | notification_history(struct nc_session *session, const struct nc_notif *notif) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3007 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3008 | time_t eventtime; |
| 3009 | char *content; |
| 3010 | (void)session; |
| 3011 | |
| 3012 | eventtime = nc_datetime2time(notif->datetime); |
| 3013 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3014 | json_object *notif_history_array = (json_object *)pthread_getspecific(notif_history_key); |
| 3015 | if (notif_history_array == NULL) { |
| 3016 | ERROR("No list of notification history found."); |
| 3017 | return; |
| 3018 | } |
| 3019 | DEBUG("Got notification from history %lu.", (long unsigned)eventtime); |
| 3020 | pthread_mutex_lock(&json_lock); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3021 | json_object *notif_obj = json_object_new_object(); |
| 3022 | if (notif_obj == NULL) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3023 | ERROR("Could not allocate memory for notification (json)."); |
| 3024 | goto failed; |
| 3025 | } |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3026 | lyd_print_mem(&content, notif->tree, LYD_JSON, 0); |
| 3027 | |
| 3028 | json_object_object_add(notif_obj, "eventtime", json_object_new_int64(eventtime)); |
| 3029 | json_object_object_add(notif_obj, "content", json_object_new_string(content)); |
| 3030 | |
| 3031 | free(content); |
| 3032 | |
| 3033 | json_object_array_add(notif_history_array, notif_obj); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 3034 | failed: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3035 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3036 | } |
| 3037 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3038 | json_object * |
| 3039 | handle_op_ntfgethistory(json_object *request, unsigned int session_key) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3040 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3041 | json_object *reply = NULL; |
| 3042 | json_object *js_tmp = NULL; |
| 3043 | struct session_with_mutex *locked_session = NULL; |
| 3044 | struct nc_session *temp_session = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3045 | struct nc_rpc *rpc = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3046 | time_t start = 0; |
| 3047 | time_t stop = 0; |
| 3048 | int64_t from = 0, to = 0; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3049 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3050 | DEBUG("Request: get notification history (session %u)", session_key); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3051 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3052 | pthread_mutex_lock(&json_lock); |
| 3053 | if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) { |
| 3054 | from = json_object_get_int64(js_tmp); |
| 3055 | } |
| 3056 | if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) { |
| 3057 | to = json_object_get_int64(js_tmp); |
| 3058 | } |
| 3059 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 3060 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3061 | start = time(NULL) + from; |
| 3062 | stop = time(NULL) + to; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3063 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3064 | DEBUG("notification history interval %li %li", (long int)from, (long int)to); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3065 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3066 | DEBUG("LOCK wrlock %s", __func__); |
| 3067 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 3068 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 3069 | reply = create_error_reply("Internal lock failed."); |
| 3070 | goto finalize; |
| 3071 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3072 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3073 | for (locked_session = netconf_sessions_list; |
| 3074 | locked_session && (locked_session->session_key != session_key); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3075 | locked_session = locked_session->next); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3076 | if (locked_session != NULL) { |
| 3077 | DEBUG("LOCK mutex %s", __func__); |
| 3078 | pthread_mutex_lock(&locked_session->lock); |
| 3079 | DEBUG("UNLOCK wrlock %s", __func__); |
| 3080 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 3081 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 3082 | } |
| 3083 | DEBUG("creating temporal NC session."); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3084 | temp_session = nc_connect_ssh_channel(locked_session->session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3085 | if (temp_session != NULL) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3086 | rpc = nc_rpc_subscribe(NULL, NULL, nc_time2datetime(start, NULL), nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3087 | if (rpc == NULL) { |
| 3088 | DEBUG("UNLOCK mutex %s", __func__); |
| 3089 | pthread_mutex_unlock(&locked_session->lock); |
| 3090 | DEBUG("notifications: creating an rpc request failed."); |
| 3091 | reply = create_error_reply("notifications: creating an rpc request failed."); |
| 3092 | goto finalize; |
| 3093 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3094 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3095 | DEBUG("Send NC subscribe."); |
| 3096 | /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */ |
| 3097 | json_object *res = netconf_unlocked_op(temp_session, rpc); |
| 3098 | if (res != NULL) { |
| 3099 | DEBUG("UNLOCK mutex %s", __func__); |
| 3100 | pthread_mutex_unlock(&locked_session->lock); |
| 3101 | DEBUG("Subscription RPC failed."); |
| 3102 | reply = res; |
| 3103 | goto finalize; |
| 3104 | } |
| 3105 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3106 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3107 | DEBUG("UNLOCK mutex %s", __func__); |
| 3108 | pthread_mutex_unlock(&locked_session->lock); |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 3109 | DEBUG("LOCK ntf mutex %s", __func__); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3110 | pthread_mutex_lock(&ntf_history_lock); |
| 3111 | pthread_mutex_lock(&json_lock); |
| 3112 | json_object *notif_history_array = json_object_new_array(); |
| 3113 | pthread_mutex_unlock(&json_lock); |
| 3114 | if (pthread_setspecific(notif_history_key, notif_history_array) != 0) { |
| 3115 | ERROR("notif_history: cannot set thread-specific hash value."); |
| 3116 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3117 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3118 | nc_recv_notif_dispatch(temp_session, notification_history); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3119 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3120 | pthread_mutex_lock(&json_lock); |
| 3121 | reply = json_object_new_object(); |
| 3122 | json_object_object_add(reply, "notifications", notif_history_array); |
| 3123 | //json_object_put(notif_history_array); |
| 3124 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3125 | |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 3126 | DEBUG("UNLOCK ntf mutex %s", __func__); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3127 | pthread_mutex_unlock(&ntf_history_lock); |
| 3128 | DEBUG("closing temporal NC session."); |
Michal Vasko | a959005 | 2016-03-08 10:12:24 +0100 | [diff] [blame] | 3129 | nc_session_free(temp_session, NULL); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3130 | temp_session = NULL; |
| 3131 | } else { |
| 3132 | DEBUG("UNLOCK mutex %s", __func__); |
| 3133 | pthread_mutex_unlock(&locked_session->lock); |
| 3134 | DEBUG("Get history of notification failed due to channel establishment"); |
| 3135 | reply = create_error_reply("Get history of notification was unsuccessful, connection failed."); |
| 3136 | } |
| 3137 | } else { |
| 3138 | DEBUG("UNLOCK wrlock %s", __func__); |
| 3139 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 3140 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 3141 | } |
| 3142 | reply = create_error_reply("Invalid session identifier."); |
| 3143 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3144 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 3145 | finalize: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3146 | return reply; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3149 | json_object * |
| 3150 | handle_op_validate(json_object *request, unsigned int session_key) |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3151 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3152 | json_object *reply = NULL; |
| 3153 | char *target = NULL; |
| 3154 | char *url = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3155 | struct nc_rpc *rpc = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3156 | NC_DATASTORE target_ds; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3157 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3158 | DEBUG("Request: validate datastore (session %u)", session_key); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3159 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3160 | pthread_mutex_lock(&json_lock); |
| 3161 | target = get_param_string(request, "target"); |
| 3162 | url = get_param_string(request, "url"); |
| 3163 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3164 | |
| 3165 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3166 | if (target == NULL) { |
| 3167 | reply = create_error_reply("Missing target parameter."); |
| 3168 | goto finalize; |
| 3169 | } |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3170 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3171 | /* validation */ |
| 3172 | target_ds = parse_datastore(target); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3173 | rpc = nc_rpc_validate(target_ds, url, NC_PARAMTYPE_CONST); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3174 | if (rpc == NULL) { |
| 3175 | DEBUG("mod_netconf: creating rpc request failed"); |
| 3176 | reply = create_error_reply("Creation of RPC request failed."); |
| 3177 | goto finalize; |
| 3178 | } |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 3179 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3180 | if ((reply = netconf_op(session_key, rpc, 0, NULL)) == NULL) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3181 | CHECK_ERR_SET_REPLY |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 3182 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3183 | if (reply == NULL) { |
| 3184 | DEBUG("Request: validation ok."); |
| 3185 | reply = create_ok_reply(); |
| 3186 | } |
| 3187 | } |
| 3188 | nc_rpc_free (rpc); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 3189 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 3190 | finalize: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3191 | CHECK_AND_FREE(target); |
| 3192 | CHECK_AND_FREE(url); |
| 3193 | return reply; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3196 | json_object * |
| 3197 | handle_op_query(json_object *request, unsigned int session_key, int idx) |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3198 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3199 | json_object *reply = NULL, *filters, *obj; |
| 3200 | char *filter = NULL; |
| 3201 | int load_children = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3202 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3203 | DEBUG("Request: query (session %u)", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3204 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3205 | pthread_mutex_lock(&json_lock); |
| 3206 | if (json_object_object_get_ex(request, "filters", &filters) == FALSE) { |
| 3207 | pthread_mutex_unlock(&json_lock); |
| 3208 | reply = create_error_reply("Missing filters parameter."); |
| 3209 | goto finalize; |
| 3210 | } |
| 3211 | obj = json_object_array_get_idx(filters, idx); |
| 3212 | if (!obj) { |
| 3213 | pthread_mutex_unlock(&json_lock); |
| 3214 | reply = create_error_reply("Filters array parameter shorter than sessions."); |
| 3215 | goto finalize; |
| 3216 | } |
| 3217 | filter = strdup(json_object_get_string(obj)); |
| 3218 | if (json_object_object_get_ex(request, "load_children", &obj) == TRUE) { |
| 3219 | load_children = json_object_get_boolean(obj); |
| 3220 | } |
| 3221 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 3222 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3223 | reply = libyang_query(session_key, filter, load_children); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3224 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3225 | CHECK_ERR_SET_REPLY |
| 3226 | if (!reply) { |
| 3227 | reply = create_error_reply("Query failed."); |
| 3228 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3229 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3230 | finalize: |
| 3231 | CHECK_AND_FREE(filter); |
| 3232 | return reply; |
| 3233 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3234 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3235 | json_object * |
| 3236 | handle_op_merge(json_object *request, unsigned int session_key, int idx) |
| 3237 | { |
| 3238 | json_object *reply = NULL, *configs, *obj; |
| 3239 | char *config = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3240 | struct lyd_node *content; |
| 3241 | struct session_with_mutex *locked_session; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3242 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3243 | DEBUG("Request: merge (session %u)", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3244 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3245 | pthread_mutex_lock(&json_lock); |
| 3246 | if (json_object_object_get_ex(request, "configurations", &configs) == FALSE) { |
| 3247 | pthread_mutex_unlock(&json_lock); |
| 3248 | reply = create_error_reply("Missing configurations parameter."); |
| 3249 | goto finalize; |
| 3250 | } |
| 3251 | obj = json_object_array_get_idx(configs, idx); |
| 3252 | if (!obj) { |
| 3253 | pthread_mutex_unlock(&json_lock); |
| 3254 | reply = create_error_reply("Filters array parameter shorter than sessions."); |
| 3255 | goto finalize; |
| 3256 | } |
| 3257 | config = strdup(json_object_get_string(obj)); |
| 3258 | pthread_mutex_unlock(&json_lock); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3259 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3260 | locked_session = session_get_locked(session_key, NULL); |
| 3261 | if (!locked_session) { |
| 3262 | ERROR("Unknown session or locking failed."); |
| 3263 | goto finalize; |
| 3264 | } |
| 3265 | |
| 3266 | content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_DATA); |
| 3267 | session_unlock(locked_session); |
| 3268 | |
| 3269 | free(config); |
| 3270 | lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS); |
| 3271 | lyd_free_withsiblings(content); |
| 3272 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3273 | reply = libyang_merge(session_key, config); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3274 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3275 | CHECK_ERR_SET_REPLY |
| 3276 | if (!reply) { |
| 3277 | reply = create_error_reply("Merge failed."); |
| 3278 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 3279 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3280 | finalize: |
| 3281 | CHECK_AND_FREE(config); |
| 3282 | return reply; |
| 3283 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 3284 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3285 | void * |
| 3286 | thread_routine(void *arg) |
| 3287 | { |
| 3288 | void *retval = NULL; |
| 3289 | struct pollfd fds; |
| 3290 | json_object *request = NULL, *replies = NULL, *reply, *sessions = NULL; |
| 3291 | json_object *js_tmp = NULL; |
Michal Vasko | df280a2 | 2016-03-17 09:19:53 +0100 | [diff] [blame] | 3292 | int operation = (-1), count, i, sent; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3293 | int status = 0; |
| 3294 | const char *msgtext; |
| 3295 | unsigned int session_key = 0; |
| 3296 | char *chunked_out_msg = NULL; |
| 3297 | int client = ((struct pass_to_thread *)arg)->client; |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 3298 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3299 | char *buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3300 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3301 | /* init thread specific err_reply memory */ |
| 3302 | create_err_reply_p(); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3303 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3304 | while (!isterminated) { |
| 3305 | fds.fd = client; |
| 3306 | fds.events = POLLIN; |
| 3307 | fds.revents = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3308 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3309 | status = poll(&fds, 1, 1000); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 3310 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3311 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 3312 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
| 3313 | continue; |
| 3314 | } else if (status < 0) { |
| 3315 | /* 0: poll time outed |
| 3316 | * close socket and ignore this request from the client, it can try it again |
| 3317 | * -1: poll failed |
| 3318 | * something wrong happend, close this socket and wait for another request |
| 3319 | */ |
| 3320 | close(client); |
| 3321 | break; |
| 3322 | } |
| 3323 | /* status > 0 */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3324 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3325 | /* check the status of the socket */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3326 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3327 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 3328 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 3329 | /* close client's socket (it's probably already closed by client */ |
| 3330 | close(client); |
| 3331 | break; |
| 3332 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 3333 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3334 | buffer = get_framed_message(client); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3335 | if (buffer != NULL) { |
Michal Vasko | e7d1bbc | 2016-04-05 16:09:29 +0200 | [diff] [blame] | 3336 | DEBUG("Received message:\n%s\n", buffer); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3337 | enum json_tokener_error jerr; |
| 3338 | pthread_mutex_lock(&json_lock); |
| 3339 | request = json_tokener_parse_verbose(buffer, &jerr); |
| 3340 | if (jerr != json_tokener_success) { |
| 3341 | ERROR("JSON parsing error"); |
| 3342 | pthread_mutex_unlock(&json_lock); |
| 3343 | continue; |
| 3344 | } |
| 3345 | |
| 3346 | if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) { |
| 3347 | operation = json_object_get_int(js_tmp); |
| 3348 | } |
| 3349 | pthread_mutex_unlock(&json_lock); |
| 3350 | if (operation == -1) { |
| 3351 | replies = create_replies(); |
| 3352 | add_reply(replies, create_error_reply("Missing operation type from frontend."), 0); |
| 3353 | goto send_reply; |
| 3354 | } |
| 3355 | |
| 3356 | if ((operation < 4) || ((operation > 19) && (operation < 100)) || (operation > 101)) { |
| 3357 | DEBUG("Unknown mod_netconf operation requested (%d)", operation); |
| 3358 | replies = create_replies(); |
| 3359 | add_reply(replies, create_error_reply("Operation not supported."), 0); |
| 3360 | goto send_reply; |
| 3361 | } |
| 3362 | |
| 3363 | DEBUG("operation %d", operation); |
| 3364 | |
| 3365 | /* null global JSON error-reply */ |
| 3366 | clean_err_reply(); |
| 3367 | |
| 3368 | /* clean replies envelope */ |
| 3369 | if (replies != NULL) { |
| 3370 | pthread_mutex_lock(&json_lock); |
| 3371 | json_object_put(replies); |
| 3372 | pthread_mutex_unlock(&json_lock); |
| 3373 | } |
| 3374 | replies = create_replies(); |
| 3375 | |
| 3376 | if (operation == MSG_CONNECT) { |
| 3377 | count = 1; |
| 3378 | } else { |
| 3379 | pthread_mutex_lock(&json_lock); |
| 3380 | if (json_object_object_get_ex(request, "sessions", &sessions) == FALSE) { |
Michal Vasko | 642cad0 | 2016-03-17 12:31:18 +0100 | [diff] [blame] | 3381 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3382 | add_reply(replies, create_error_reply("Operation missing \"sessions\" arg"), 0); |
| 3383 | goto send_reply; |
| 3384 | } |
| 3385 | count = json_object_array_length(sessions); |
| 3386 | pthread_mutex_unlock(&json_lock); |
| 3387 | } |
| 3388 | |
| 3389 | for (i = 0; i < count; ++i) { |
| 3390 | if (operation != MSG_CONNECT) { |
| 3391 | js_tmp = json_object_array_get_idx(sessions, i); |
| 3392 | session_key = json_object_get_int(js_tmp); |
| 3393 | } |
| 3394 | |
| 3395 | /* process required operation */ |
Michal Vasko | 977bad9 | 2016-03-15 16:20:51 +0100 | [diff] [blame] | 3396 | reply = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3397 | switch (operation) { |
| 3398 | case MSG_CONNECT: |
| 3399 | reply = handle_op_connect(request); |
| 3400 | break; |
| 3401 | case MSG_DISCONNECT: |
| 3402 | reply = handle_op_disconnect(request, session_key); |
| 3403 | break; |
| 3404 | case MSG_GET: |
| 3405 | reply = handle_op_get(request, session_key); |
| 3406 | break; |
| 3407 | case MSG_GETCONFIG: |
| 3408 | reply = handle_op_getconfig(request, session_key); |
| 3409 | break; |
| 3410 | case MSG_EDITCONFIG: |
| 3411 | reply = handle_op_editconfig(request, session_key, i); |
| 3412 | break; |
| 3413 | case MSG_COPYCONFIG: |
| 3414 | reply = handle_op_copyconfig(request, session_key, i); |
| 3415 | break; |
| 3416 | case MSG_DELETECONFIG: |
| 3417 | reply = handle_op_deleteconfig(request, session_key); |
| 3418 | break; |
| 3419 | case MSG_LOCK: |
| 3420 | reply = handle_op_lock(request, session_key); |
| 3421 | break; |
| 3422 | case MSG_UNLOCK: |
| 3423 | reply = handle_op_unlock(request, session_key); |
| 3424 | break; |
| 3425 | case MSG_KILL: |
| 3426 | reply = handle_op_kill(request, session_key); |
| 3427 | break; |
| 3428 | case MSG_INFO: |
| 3429 | reply = handle_op_info(request, session_key); |
| 3430 | break; |
| 3431 | case MSG_GENERIC: |
| 3432 | reply = handle_op_generic(request, session_key, i); |
| 3433 | break; |
| 3434 | case MSG_GETSCHEMA: |
| 3435 | reply = handle_op_getschema(request, session_key); |
| 3436 | break; |
| 3437 | case MSG_RELOADHELLO: |
| 3438 | reply = handle_op_reloadhello(request, session_key); |
| 3439 | break; |
| 3440 | case MSG_NTF_GETHISTORY: |
| 3441 | reply = handle_op_ntfgethistory(request, session_key); |
| 3442 | break; |
| 3443 | case MSG_VALIDATE: |
| 3444 | reply = handle_op_validate(request, session_key); |
| 3445 | break; |
| 3446 | case SCH_QUERY: |
| 3447 | reply = handle_op_query(request, session_key, i); |
| 3448 | break; |
| 3449 | case SCH_MERGE: |
| 3450 | reply = handle_op_merge(request, session_key, i); |
| 3451 | break; |
| 3452 | } |
| 3453 | |
| 3454 | add_reply(replies, reply, session_key); |
| 3455 | } |
| 3456 | |
| 3457 | /* free parameters */ |
| 3458 | operation = (-1); |
| 3459 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3460 | if (request != NULL) { |
| 3461 | pthread_mutex_lock(&json_lock); |
| 3462 | json_object_put(request); |
| 3463 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | 365dc4c | 2016-03-17 10:03:58 +0100 | [diff] [blame] | 3464 | request = NULL; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3465 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 3466 | |
| 3467 | send_reply: |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3468 | /* send reply to caller */ |
| 3469 | if (replies) { |
| 3470 | pthread_mutex_lock(&json_lock); |
| 3471 | msgtext = json_object_to_json_string(replies); |
Michal Vasko | 52c9177 | 2016-03-17 10:04:12 +0100 | [diff] [blame] | 3472 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | f849e4e | 2016-04-06 10:00:08 +0200 | [diff] [blame] | 3473 | DEBUG("Sending message:\n%s\n", msgtext); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3474 | if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) { |
| 3475 | if (buffer != NULL) { |
| 3476 | free(buffer); |
| 3477 | buffer = NULL; |
| 3478 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3479 | break; |
| 3480 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 3481 | |
Michal Vasko | df280a2 | 2016-03-17 09:19:53 +0100 | [diff] [blame] | 3482 | i = 0; |
| 3483 | sent = 0; |
| 3484 | count = strlen(chunked_out_msg) + 1; |
| 3485 | while (count && ((i = send(client, chunked_out_msg + sent, count, 0)) != -1)) { |
| 3486 | sent += i; |
| 3487 | count -= i; |
| 3488 | } |
| 3489 | if (i == -1) { |
| 3490 | ERROR("Sending message failed (%s).", strerror(errno)); |
| 3491 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3492 | pthread_mutex_lock(&json_lock); |
| 3493 | json_object_put(replies); |
Michal Vasko | 52c9177 | 2016-03-17 10:04:12 +0100 | [diff] [blame] | 3494 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3495 | replies = NULL; |
Michal Vasko | 2314d40 | 2016-04-06 13:24:06 +0200 | [diff] [blame] | 3496 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3497 | CHECK_AND_FREE(chunked_out_msg); |
| 3498 | chunked_out_msg = NULL; |
| 3499 | if (buffer) { |
| 3500 | free(buffer); |
| 3501 | buffer = NULL; |
| 3502 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3503 | clean_err_reply(); |
| 3504 | } else { |
| 3505 | ERROR("Reply is NULL, shouldn't be..."); |
| 3506 | continue; |
| 3507 | } |
| 3508 | } |
| 3509 | } |
| 3510 | free(arg); |
| 3511 | free_err_reply(); |
Michal Vasko | d020599 | 2016-03-17 10:04:49 +0100 | [diff] [blame] | 3512 | nc_thread_destroy(); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3513 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3514 | return retval; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3515 | } |
| 3516 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3517 | /** |
| 3518 | * \brief Close all open NETCONF sessions. |
| 3519 | * |
| 3520 | * During termination of mod_netconf, it is useful to close all remaining |
| 3521 | * sessions. This function iterates over the list of sessions and close them |
| 3522 | * all. |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3523 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3524 | static void |
| 3525 | close_all_nc_sessions(void) |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3526 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3527 | struct session_with_mutex *locked_session, *next_session; |
| 3528 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3529 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3530 | /* get exclusive access to sessions_list (conns) */ |
| 3531 | DEBUG("LOCK wrlock %s", __func__); |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 3532 | if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3533 | ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
| 3534 | return; |
| 3535 | } |
| 3536 | for (next_session = netconf_sessions_list; next_session;) { |
| 3537 | locked_session = next_session; |
| 3538 | next_session = locked_session->next; |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 3539 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3540 | /* close_and_free_session handles locking on its own */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3541 | DEBUG("Closing NETCONF session %u (SID %u).", locked_session->session_key, nc_session_get_id(locked_session->session)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3542 | close_and_free_session(locked_session); |
| 3543 | } |
| 3544 | netconf_sessions_list = NULL; |
| 3545 | |
| 3546 | /* get exclusive access to sessions_list (conns) */ |
| 3547 | DEBUG("UNLOCK wrlock %s", __func__); |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 3548 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3549 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 3550 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3551 | } |
| 3552 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3553 | static void |
| 3554 | check_timeout_and_close(void) |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3555 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3556 | struct nc_session *ns = NULL; |
| 3557 | struct session_with_mutex *locked_session = NULL; |
| 3558 | time_t current_time = time(NULL); |
| 3559 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3560 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3561 | /* get exclusive access to sessions_list (conns) */ |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 3562 | DEBUG("LOCK wrlock %s", __func__); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3563 | if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) { |
| 3564 | DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
| 3565 | return; |
| 3566 | } |
| 3567 | for (locked_session = netconf_sessions_list; locked_session; locked_session = locked_session->next) { |
| 3568 | ns = locked_session->session; |
| 3569 | if (ns == NULL) { |
| 3570 | continue; |
| 3571 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3572 | if ((current_time - locked_session->last_activity) > ACTIVITY_TIMEOUT) { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3573 | DEBUG("Closing NETCONF session %u (SID %u).", locked_session->session_key, nc_session_get_id(locked_session->session)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 3574 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3575 | /* close_and_free_session handles locking on its own */ |
| 3576 | close_and_free_session(locked_session); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3577 | } |
| 3578 | } |
Michal Vasko | dc40b3a | 2016-04-11 14:36:16 +0200 | [diff] [blame^] | 3579 | DEBUG("UNLOCK wrlock %s", __func__); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3580 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 3581 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 3582 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | |
| 3586 | /** |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 3587 | * This is actually implementation of NETCONF client |
| 3588 | * - requests are received from UNIX socket in the predefined format |
| 3589 | * - results are replied through the same way |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3590 | * - the daemon run as a separate process |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 3591 | * |
| 3592 | */ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3593 | static void |
| 3594 | forked_proc(void) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3595 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3596 | struct timeval tv; |
| 3597 | struct sockaddr_un local, remote; |
| 3598 | int lsock, client, ret, i, pthread_count = 0; |
| 3599 | unsigned int olds = 0, timediff = 0; |
| 3600 | socklen_t len; |
| 3601 | struct pass_to_thread *arg; |
| 3602 | pthread_t *ptids = calloc(1, sizeof(pthread_t)); |
| 3603 | struct timespec maxtime; |
| 3604 | pthread_rwlockattr_t lock_attrs; |
| 3605 | #ifdef WITH_NOTIFICATIONS |
| 3606 | char use_notifications = 0; |
| 3607 | #endif |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3608 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3609 | /* wait at most 5 seconds for every thread to terminate */ |
| 3610 | maxtime.tv_sec = 5; |
| 3611 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3612 | |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3613 | #ifdef HAVE_UNIXD_SETUP_CHILD |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3614 | /* change uid and gid of process for security reasons */ |
| 3615 | unixd_setup_child(); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3616 | #else |
| 3617 | # ifdef SU_GROUP |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3618 | if (strlen(SU_GROUP) > 0) { |
| 3619 | struct group *g = getgrnam(SU_GROUP); |
| 3620 | if (g == NULL) { |
| 3621 | ERROR("GID (%s) was not found.", SU_GROUP); |
| 3622 | return; |
| 3623 | } |
| 3624 | if (setgid(g->gr_gid) != 0) { |
| 3625 | ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno)); |
| 3626 | return; |
| 3627 | } |
| 3628 | } |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3629 | # else |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3630 | DEBUG("no SU_GROUP"); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3631 | # endif |
| 3632 | # ifdef SU_USER |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3633 | if (strlen(SU_USER) > 0) { |
| 3634 | struct passwd *p = getpwnam(SU_USER); |
| 3635 | if (p == NULL) { |
| 3636 | ERROR("UID (%s) was not found.", SU_USER); |
| 3637 | return; |
| 3638 | } |
| 3639 | if (setuid(p->pw_uid) != 0) { |
| 3640 | ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno)); |
| 3641 | return; |
| 3642 | } |
| 3643 | } |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3644 | # else |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3645 | DEBUG("no SU_USER"); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3646 | # endif |
| 3647 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3648 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3649 | /* try to remove if exists */ |
| 3650 | unlink(sockname); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3651 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3652 | /* create listening UNIX socket to accept incoming connections */ |
| 3653 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 3654 | ERROR("Creating socket failed (%s)", strerror(errno)); |
| 3655 | goto error_exit; |
| 3656 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3657 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3658 | local.sun_family = AF_UNIX; |
| 3659 | strncpy(local.sun_path, sockname, sizeof(local.sun_path)); |
| 3660 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3661 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3662 | if (bind(lsock, (struct sockaddr *)&local, len) == -1) { |
| 3663 | if (errno == EADDRINUSE) { |
| 3664 | ERROR("mod_netconf socket address already in use"); |
| 3665 | goto error_exit; |
| 3666 | } |
| 3667 | ERROR("Binding socket failed (%s)", strerror(errno)); |
| 3668 | goto error_exit; |
| 3669 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3670 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3671 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 3672 | ERROR("Setting up listen socket failed (%s)", strerror(errno)); |
| 3673 | goto error_exit; |
| 3674 | } |
| 3675 | chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3676 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3677 | uid_t user = -1; |
| 3678 | if (strlen(CHOWN_USER) > 0) { |
| 3679 | struct passwd *p = getpwnam(CHOWN_USER); |
| 3680 | if (p != NULL) { |
| 3681 | user = p->pw_uid; |
| 3682 | } |
| 3683 | } |
| 3684 | gid_t group = -1; |
| 3685 | if (strlen(CHOWN_GROUP) > 0) { |
| 3686 | struct group *g = getgrnam(CHOWN_GROUP); |
| 3687 | if (g != NULL) { |
| 3688 | group = g->gr_gid; |
| 3689 | } |
| 3690 | } |
| 3691 | if (chown(sockname, user, group) == -1) { |
| 3692 | ERROR("Chown on socket file failed (%s).", strerror(errno)); |
| 3693 | } |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 3694 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3695 | /* prepare internal lists */ |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 3696 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3697 | #ifdef WITH_NOTIFICATIONS |
| 3698 | if (notification_init() == -1) { |
| 3699 | ERROR("libwebsockets initialization failed"); |
| 3700 | use_notifications = 0; |
| 3701 | } else { |
| 3702 | use_notifications = 1; |
| 3703 | } |
| 3704 | #endif |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 3705 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3706 | /* setup libnetconf's callbacks */ |
Michal Vasko | d020599 | 2016-03-17 10:04:49 +0100 | [diff] [blame] | 3707 | nc_client_init(); |
Michal Vasko | dedf91b | 2016-04-06 10:00:31 +0200 | [diff] [blame] | 3708 | nc_verbosity(NC_VERB_VERBOSE); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3709 | nc_set_print_clb(clb_print); |
| 3710 | nc_client_ssh_set_auth_hostkey_check_clb(netconf_callback_ssh_hostkey_check); |
| 3711 | nc_client_ssh_set_auth_interactive_clb(netconf_callback_sshauth_interactive); |
| 3712 | nc_client_ssh_set_auth_password_clb(netconf_callback_sshauth_password); |
| 3713 | nc_client_ssh_set_auth_privkey_passphrase_clb(netconf_callback_sshauth_passphrase); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3714 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3715 | /* disable publickey authentication */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 3716 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3717 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3718 | /* create mutex protecting session list */ |
| 3719 | pthread_rwlockattr_init(&lock_attrs); |
| 3720 | /* rwlock is shared only with threads in this process */ |
| 3721 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 3722 | /* create rw lock */ |
| 3723 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
| 3724 | ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
| 3725 | goto error_exit; |
| 3726 | } |
| 3727 | pthread_mutex_init(&ntf_history_lock, NULL); |
| 3728 | pthread_mutex_init(&json_lock, NULL); |
| 3729 | DEBUG("Initialization of notification history."); |
| 3730 | if (pthread_key_create(¬if_history_key, NULL) != 0) { |
| 3731 | ERROR("Initialization of notification history failed."); |
| 3732 | } |
| 3733 | if (pthread_key_create(&err_reply_key, NULL) != 0) { |
| 3734 | ERROR("Initialization of reply key failed."); |
| 3735 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 3736 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3737 | fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK); |
| 3738 | while (isterminated == 0) { |
| 3739 | gettimeofday(&tv, NULL); |
| 3740 | timediff = (unsigned int)tv.tv_sec - olds; |
| 3741 | #ifdef WITH_NOTIFICATIONS |
| 3742 | if (use_notifications == 1) { |
| 3743 | notification_handle(); |
| 3744 | } |
| 3745 | #endif |
| 3746 | if (timediff > ACTIVITY_CHECK_INTERVAL) { |
| 3747 | check_timeout_and_close(); |
| 3748 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3749 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3750 | /* open incoming connection if any */ |
| 3751 | len = sizeof(remote); |
| 3752 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
| 3753 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 3754 | usleep(SLEEP_TIME * 1000); |
| 3755 | continue; |
| 3756 | } else if (client == -1 && (errno == EINTR)) { |
| 3757 | continue; |
| 3758 | } else if (client == -1) { |
| 3759 | ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 3760 | continue; |
| 3761 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3762 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3763 | /* set client's socket as non-blocking */ |
| 3764 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3765 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3766 | arg = malloc(sizeof(struct pass_to_thread)); |
| 3767 | arg->client = client; |
| 3768 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3769 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3770 | /* start new thread. It will serve this particular request and then terminate */ |
| 3771 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void *)arg)) != 0) { |
| 3772 | ERROR("Creating POSIX thread failed: %d\n", ret); |
| 3773 | } else { |
| 3774 | DEBUG("Thread %lu created", ptids[pthread_count]); |
| 3775 | pthread_count++; |
| 3776 | ptids = realloc (ptids, sizeof(pthread_t) * (pthread_count+1)); |
| 3777 | ptids[pthread_count] = 0; |
| 3778 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3779 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3780 | /* check if some thread already terminated, free some resources by joining it */ |
| 3781 | for (i = 0; i < pthread_count; i++) { |
| 3782 | if (pthread_tryjoin_np(ptids[i], (void **)&arg) == 0) { |
| 3783 | DEBUG("Thread %lu joined with retval %p", ptids[i], arg); |
| 3784 | pthread_count--; |
| 3785 | if (pthread_count > 0) { |
| 3786 | /* place last Thread ID on the place of joined one */ |
| 3787 | ptids[i] = ptids[pthread_count]; |
| 3788 | } |
| 3789 | } |
| 3790 | } |
| 3791 | DEBUG("Running %d threads", pthread_count); |
| 3792 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3793 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3794 | DEBUG("mod_netconf terminating..."); |
| 3795 | /* join all threads */ |
| 3796 | for (i = 0; i < pthread_count; i++) { |
| 3797 | pthread_timedjoin_np(ptids[i], (void **)&arg, &maxtime); |
| 3798 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3799 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3800 | #ifdef WITH_NOTIFICATIONS |
| 3801 | notification_close(); |
| 3802 | #endif |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 3803 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3804 | /* close all NETCONF sessions */ |
| 3805 | close_all_nc_sessions(); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 3806 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3807 | /* destroy rwlock */ |
| 3808 | pthread_rwlock_destroy(&session_lock); |
| 3809 | pthread_rwlockattr_destroy(&lock_attrs); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 3810 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3811 | DEBUG("Exiting from the mod_netconf daemon"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3812 | |
Michal Vasko | d020599 | 2016-03-17 10:04:49 +0100 | [diff] [blame] | 3813 | nc_client_destroy(); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3814 | free(ptids); |
| 3815 | close(lsock); |
| 3816 | exit(0); |
| 3817 | return; |
| 3818 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 3819 | error_exit: |
Michal Vasko | d020599 | 2016-03-17 10:04:49 +0100 | [diff] [blame] | 3820 | nc_client_destroy(); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3821 | close(lsock); |
| 3822 | free(ptids); |
| 3823 | return; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 3824 | } |
| 3825 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3826 | int |
| 3827 | main(int argc, char **argv) |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 3828 | { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3829 | struct sigaction action; |
| 3830 | sigset_t block_mask; |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 3831 | int i; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3832 | |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 3833 | if (argc > 3) { |
| 3834 | printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n"); |
| 3835 | return 1; |
| 3836 | } |
| 3837 | |
| 3838 | sockname = SOCKET_FILENAME; |
| 3839 | for (i = 1; i < argc; ++i) { |
| 3840 | if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { |
| 3841 | printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n"); |
| 3842 | return 0; |
| 3843 | } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")) { |
| 3844 | daemonize = 1; |
| 3845 | } else { |
| 3846 | sockname = argv[i]; |
| 3847 | } |
| 3848 | } |
| 3849 | |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 3850 | if (daemonize) { |
| 3851 | if (daemon(0, 0) == -1) { |
| 3852 | ERROR("daemon() failed (%s)", strerror(errno)); |
| 3853 | return 1; |
| 3854 | } |
| 3855 | openlog("netopeerguid", LOG_PID, LOG_DAEMON); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3856 | } |
| 3857 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3858 | sigfillset(&block_mask); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 3859 | action.sa_handler = signal_handler; |
| 3860 | action.sa_mask = block_mask; |
| 3861 | action.sa_flags = 0; |
| 3862 | sigaction(SIGINT, &action, NULL); |
| 3863 | sigaction(SIGTERM, &action, NULL); |
| 3864 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3865 | forked_proc(); |
| 3866 | DEBUG("Terminated"); |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 3867 | if (daemonize) { |
| 3868 | closelog(); |
| 3869 | } |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 3870 | return 0; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 3871 | } |