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