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