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