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