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