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