Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * \file mod_netconf.c |
| 3 | * \brief NETCONF Apache modul for Netopeer |
| 4 | * \author Tomas Cejka <cejkat@cesnet.cz> |
| 5 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 6 | * \date 2011 |
| 7 | * \date 2012 |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 8 | * \date 2013 |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 9 | */ |
| 10 | /* |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 11 | * Copyright (C) 2011-2013 CESNET |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 12 | * |
| 13 | * LICENSE TERMS |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in |
| 22 | * the documentation and/or other materials provided with the |
| 23 | * distribution. |
| 24 | * 3. Neither the name of the Company nor the names of its contributors |
| 25 | * may be used to endorse or promote products derived from this |
| 26 | * software without specific prior written permission. |
| 27 | * |
| 28 | * ALTERNATIVELY, provided that this notice is retained in full, this |
| 29 | * product may be distributed under the terms of the GNU General Public |
| 30 | * License (GPL) version 2 or later, in which case the provisions |
| 31 | * of the GPL apply INSTEAD OF those given above. |
| 32 | * |
| 33 | * This software is provided ``as is'', and any express or implied |
| 34 | * warranties, including, but not limited to, the implied warranties of |
| 35 | * merchantability and fitness for a particular purpose are disclaimed. |
| 36 | * In no event shall the company or contributors be liable for any |
| 37 | * direct, indirect, incidental, special, exemplary, or consequential |
| 38 | * damages (including, but not limited to, procurement of substitute |
| 39 | * goods or services; loss of use, data, or profits; or business |
| 40 | * interruption) however caused and on any theory of liability, whether |
| 41 | * in contract, strict liability, or tort (including negligence or |
| 42 | * otherwise) arising in any way out of the use of this software, even |
| 43 | * if advised of the possibility of such damage. |
| 44 | * |
| 45 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 46 | #define _GNU_SOURCE |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 47 | |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 48 | #include <unistd.h> |
| 49 | #include <poll.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 50 | #include <time.h> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 51 | #include <sys/types.h> |
| 52 | #include <sys/socket.h> |
| 53 | #include <sys/un.h> |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 54 | #include <sys/fcntl.h> |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 55 | #include <pwd.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 56 | #include <errno.h> |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 57 | #include <grp.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 58 | #include <signal.h> |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 59 | #include <pthread.h> |
| 60 | #include <ctype.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 61 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 62 | #include <libnetconf.h> |
Tomas Cejka | b3cc64f | 2013-05-03 19:44:54 +0200 | [diff] [blame] | 63 | #include <libnetconf_ssh.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 64 | |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 65 | #include "../config.h" |
| 66 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 67 | #ifdef WITH_NOTIFICATIONS |
| 68 | #include "notification_module.h" |
| 69 | #endif |
| 70 | |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 71 | #include "message_type.h" |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 72 | #include "mod_netconf.h" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 73 | |
| 74 | #define MAX_PROCS 5 |
Tomas Cejka | 86f0fc1 | 2014-09-17 15:09:38 +0200 | [diff] [blame] | 75 | #define SOCKET_FILENAME "/var/run/mod_netconf.sock" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 76 | #define MAX_SOCKET_CL 10 |
| 77 | #define BUFFER_SIZE 4096 |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 78 | #define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */ |
Tomas Cejka | 04e3995 | 2013-04-19 11:49:38 +0200 | [diff] [blame] | 79 | #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] | 80 | |
Michal Vasko | 7732dee | 2015-11-05 10:22:15 +0100 | [diff] [blame] | 81 | /* sleep in master process for non-blocking socket reading, in msec */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 82 | #define SLEEP_TIME 200 |
| 83 | |
| 84 | #ifndef offsetof |
| 85 | #define offsetof(type, member) ((size_t) ((type *) 0)->member) |
| 86 | #endif |
| 87 | |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 88 | /* timeout in msec */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 89 | struct timeval timeout = { 1, 0 }; |
| 90 | |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 91 | #define NCWITHDEFAULTS NCWD_MODE_NOTSET |
| 92 | |
| 93 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 94 | #define MSG_OK 0 |
| 95 | #define MSG_OPEN 1 |
| 96 | #define MSG_DATA 2 |
| 97 | #define MSG_CLOSE 3 |
| 98 | #define MSG_ERROR 4 |
| 99 | #define MSG_UNKNOWN 5 |
| 100 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 101 | 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] | 102 | pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 103 | pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */ |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 104 | pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */ |
| 105 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 106 | struct session_with_mutex *netconf_sessions_list = NULL; |
| 107 | |
| 108 | static const char *sockname; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 109 | |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 110 | static pthread_key_t notif_history_key; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 111 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 112 | pthread_key_t err_reply_key; |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 113 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 114 | volatile int isterminated = 0; |
| 115 | |
| 116 | static char* password; |
| 117 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 118 | static void signal_handler(int sign) |
| 119 | { |
| 120 | switch (sign) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 121 | case SIGINT: |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 122 | case SIGTERM: |
| 123 | isterminated = 1; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 128 | int 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] | 129 | { |
| 130 | /* always approve */ |
| 131 | return (EXIT_SUCCESS); |
| 132 | } |
| 133 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 134 | char *netconf_callback_sshauth_passphrase(const char *UNUSED(username), const char *UNUSED(hostname), const char *UNUSED(priv_key_file)) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 135 | { |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 136 | char *buf; |
| 137 | buf = strdup(password); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 138 | return (buf); |
| 139 | } |
| 140 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 141 | char *netconf_callback_sshauth_password(const char* UNUSED(username), const char* UNUSED(hostname)) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 142 | { |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 143 | char *buf; |
| 144 | buf = strdup(password); |
| 145 | return (buf); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 148 | char *netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction), |
| 149 | const char *UNUSED(prompt), int UNUSED(echo)) |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 150 | { |
| 151 | char *buf; |
| 152 | buf = strdup(password); |
| 153 | return (buf); |
| 154 | } |
| 155 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 156 | void netconf_callback_error_process(const char *UNUSED(tag), |
| 157 | const char *UNUSED(type), |
| 158 | const char *UNUSED(severity), |
| 159 | const char *UNUSED(apptag), |
| 160 | const char *UNUSED(path), |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 161 | const char *message, |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 162 | const char *UNUSED(attribute), |
| 163 | const char *UNUSED(element), |
| 164 | const char *UNUSED(ns), |
| 165 | const char *UNUSED(sid)) |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 166 | { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 167 | json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 168 | if (err_reply_p == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 169 | ERROR("Error message was not allocated. %s", __func__); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 170 | return; |
| 171 | } |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 172 | json_object *err_reply = *err_reply_p; |
| 173 | |
Tomas Cejka | 0858dbb | 2013-11-19 15:11:00 +0100 | [diff] [blame] | 174 | json_object *array = NULL; |
| 175 | if (err_reply == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 176 | ERROR("error calback: empty error list"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 177 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0858dbb | 2013-11-19 15:11:00 +0100 | [diff] [blame] | 178 | err_reply = json_object_new_object(); |
| 179 | array = json_object_new_array(); |
| 180 | json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR)); |
| 181 | json_object_object_add(err_reply, "errors", array); |
| 182 | if (message != NULL) { |
| 183 | json_object_array_add(array, json_object_new_string(message)); |
| 184 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 185 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 186 | (*err_reply_p) = err_reply; |
Tomas Cejka | 0858dbb | 2013-11-19 15:11:00 +0100 | [diff] [blame] | 187 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 188 | ERROR("error calback: nonempty error list"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 189 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 190 | if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) { |
Tomas Cejka | 0858dbb | 2013-11-19 15:11:00 +0100 | [diff] [blame] | 191 | if (message != NULL) { |
| 192 | json_object_array_add(array, json_object_new_string(message)); |
| 193 | } |
| 194 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 195 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 196 | } |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 197 | pthread_setspecific(err_reply_key, err_reply_p); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 198 | return; |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 201 | /** |
| 202 | * should be used in locked area |
| 203 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 204 | void prepare_status_message(struct session_with_mutex *s, struct nc_session *session) |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 205 | { |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 206 | json_object *json_obj = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 207 | json_object *js_tmp = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 208 | char *old_sid = NULL; |
| 209 | const char *j_old_sid = NULL; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 210 | const char *cpbltstr; |
| 211 | struct nc_cpblts* cpblts = NULL; |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 212 | |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 213 | if (s == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 214 | ERROR("No session given."); |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 218 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 219 | if (s->hello_message != NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 220 | ERROR("clean previous hello message"); |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 221 | //json_object_put(s->hello_message); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 222 | if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) { |
| 223 | j_old_sid = json_object_get_string(js_tmp); |
| 224 | if (j_old_sid != NULL) { |
| 225 | old_sid = strdup(j_old_sid); |
| 226 | } |
| 227 | json_object_put(s->hello_message); |
| 228 | json_object_put(js_tmp); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 229 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 230 | s->hello_message = NULL; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 231 | } |
Tomas Cejka | dd5cb45 | 2014-03-26 15:22:00 +0100 | [diff] [blame] | 232 | s->hello_message = json_object_get(json_object_new_object()); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 233 | if (session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 234 | if (old_sid != NULL) { |
| 235 | /* use previous sid */ |
| 236 | json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid)); |
| 237 | free(old_sid); |
Tomas Cejka | a3ffdba | 2014-03-27 15:12:21 +0100 | [diff] [blame] | 238 | old_sid = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 239 | } else { |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 240 | /* we don't have old sid */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 241 | json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session))); |
| 242 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 243 | json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1")); |
| 244 | json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session))); |
| 245 | json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session))); |
| 246 | json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session))); |
| 247 | cpblts = nc_session_get_cpblts (session); |
| 248 | if (cpblts != NULL) { |
| 249 | json_obj = json_object_new_array(); |
| 250 | nc_cpblts_iter_start (cpblts); |
| 251 | while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) { |
| 252 | json_object_array_add(json_obj, json_object_new_string(cpbltstr)); |
| 253 | } |
| 254 | json_object_object_add(s->hello_message, "capabilities", json_obj); |
| 255 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 256 | DEBUG("%s", json_object_to_json_string(s->hello_message)); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 257 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 258 | ERROR("Session was not given."); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 259 | json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR)); |
| 260 | json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier.")); |
| 261 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 262 | DEBUG("Status info from hello message prepared"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 263 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 264 | |
| 265 | } |
| 266 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 267 | void create_err_reply_p() |
| 268 | { |
| 269 | json_object **err_reply = calloc(1, sizeof(json_object **)); |
| 270 | if (err_reply == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 271 | ERROR("Allocation of err_reply storage failed!"); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | if (pthread_setspecific(err_reply_key, err_reply) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 275 | ERROR("cannot set thread-specific value."); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | void clean_err_reply() |
| 280 | { |
| 281 | json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key); |
| 282 | if (err_reply != NULL) { |
| 283 | if (*err_reply != NULL) { |
| 284 | pthread_mutex_lock(&json_lock); |
| 285 | json_object_put(*err_reply); |
| 286 | pthread_mutex_unlock(&json_lock); |
| 287 | } |
| 288 | if (pthread_setspecific(err_reply_key, err_reply) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 289 | ERROR("Cannot set thread-specific hash value."); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void free_err_reply() |
| 295 | { |
| 296 | json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key); |
| 297 | if (err_reply != NULL) { |
| 298 | if (*err_reply != NULL) { |
| 299 | pthread_mutex_lock(&json_lock); |
| 300 | json_object_put(*err_reply); |
| 301 | pthread_mutex_unlock(&json_lock); |
| 302 | } |
| 303 | free(err_reply); |
| 304 | err_reply = NULL; |
| 305 | if (pthread_setspecific(err_reply_key, err_reply) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 306 | ERROR("Cannot set thread-specific hash value."); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 310 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 311 | /** |
| 312 | * \defgroup netconf_operations NETCONF operations |
| 313 | * The list of NETCONF operations that mod_netconf supports. |
| 314 | * @{ |
| 315 | */ |
| 316 | |
| 317 | /** |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 318 | * \brief Send RPC and wait for reply with timeout. |
| 319 | * |
| 320 | * \param[in] session libnetconf session |
| 321 | * \param[in] rpc prepared RPC message |
| 322 | * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking |
| 323 | * \param[out] reply reply from the server |
| 324 | * \return Value from nc_session_recv_reply() or NC_MSG_UNKNOWN when send_rpc() fails. |
| 325 | * On success, it returns NC_MSG_REPLY. |
| 326 | */ |
| 327 | NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc, |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 328 | int timeout, nc_reply **reply) |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 329 | { |
| 330 | const nc_msgid msgid = NULL; |
| 331 | NC_MSG_TYPE ret = NC_MSG_UNKNOWN; |
| 332 | msgid = nc_session_send_rpc(session, rpc); |
| 333 | if (msgid == NULL) { |
| 334 | return ret; |
| 335 | } |
Tomas Cejka | 756d129 | 2015-05-06 09:31:46 +0200 | [diff] [blame] | 336 | do { |
| 337 | ret = nc_session_recv_reply(session, timeout, reply); |
| 338 | if (ret == NC_MSG_HELLO) { |
| 339 | ERROR("<hello> received instead reply, it will be lost."); |
| 340 | nc_reply_free(*reply); |
| 341 | } |
| 342 | if (ret == NC_MSG_WOULDBLOCK) { |
| 343 | ERROR("Timeout for receiving RPC reply expired."); |
| 344 | break; |
| 345 | } |
| 346 | } while (ret == NC_MSG_HELLO || ret == NC_MSG_NOTIFICATION); |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | /** |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 351 | * \brief Connect to NETCONF server |
| 352 | * |
| 353 | * \warning Session_key hash is not bound with caller identification. This could be potential security risk. |
| 354 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 355 | static const char *netconf_connect(const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 356 | { |
Tomas Cejka | ae9efe5 | 2013-04-23 13:37:36 +0200 | [diff] [blame] | 357 | struct nc_session* session = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 358 | struct session_with_mutex * locked_session, *last_session; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 359 | |
| 360 | /* connect to the requested NETCONF server */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 361 | password = (char*)pass; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 362 | DEBUG("prepare to connect %s@%s:%s", user, host, port); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 363 | session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 364 | DEBUG("nc_session_connect done"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 365 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 366 | /* if connected successful, add session to the list */ |
| 367 | if (session != NULL) { |
Tomas Cejka | 73496bf | 2014-03-26 15:31:09 +0100 | [diff] [blame] | 368 | if ((locked_session = calloc(1, sizeof(struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 369 | nc_session_free(session); |
Tomas Cejka | 73496bf | 2014-03-26 15:31:09 +0100 | [diff] [blame] | 370 | session = NULL; |
| 371 | free(locked_session); |
| 372 | locked_session = NULL; |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 373 | ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno)); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 374 | return 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 375 | } |
| 376 | locked_session->session = session; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 377 | locked_session->last_activity = time(NULL); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 378 | locked_session->hello_message = NULL; |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 379 | locked_session->closed = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 380 | pthread_mutex_init (&locked_session->lock, NULL); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 381 | DEBUG("Before session_lock"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 382 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 383 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 384 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 385 | nc_session_free(session); |
| 386 | free (locked_session); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 387 | ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 388 | return 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 389 | } |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 390 | locked_session->ntfc_subscribed = 0; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 391 | DEBUG("Add connection to the list"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 392 | if (!netconf_sessions_list) { |
| 393 | netconf_sessions_list = locked_session; |
| 394 | } else { |
| 395 | for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next); |
| 396 | last_session->next = locked_session; |
| 397 | locked_session->prev = last_session; |
| 398 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 399 | DEBUG("Before session_unlock"); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 400 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 401 | /* lock session */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 402 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 403 | pthread_mutex_lock(&locked_session->lock); |
| 404 | |
| 405 | /* unlock session list */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 406 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 407 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 408 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 411 | /* store information about session from hello message for future usage */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 412 | prepare_status_message(locked_session, session); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 413 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 414 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 415 | pthread_mutex_unlock(&locked_session->lock); |
| 416 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 417 | DEBUG("NETCONF session established"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 418 | return nc_session_get_id(session); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 419 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 420 | ERROR("Connection could not be established"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 421 | return 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 426 | static int close_and_free_session(struct session_with_mutex *locked_session) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 427 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 428 | DEBUG("lock private lock."); |
| 429 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 430 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 431 | ERROR("Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 432 | } |
| 433 | locked_session->ntfc_subscribed = 0; |
| 434 | locked_session->closed = 1; |
Tomas Cejka | 73496bf | 2014-03-26 15:31:09 +0100 | [diff] [blame] | 435 | if (locked_session->session != NULL) { |
| 436 | nc_session_free(locked_session->session); |
| 437 | locked_session->session = NULL; |
| 438 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 439 | DEBUG("session closed."); |
| 440 | DEBUG("unlock private lock."); |
| 441 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 442 | if (pthread_mutex_unlock(&locked_session->lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 443 | ERROR("Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 446 | DEBUG("unlock session lock."); |
| 447 | DEBUG("closed session, disabled notif(?), wait 2s"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 448 | usleep(500000); /* let notification thread stop */ |
| 449 | |
| 450 | /* session shouldn't be used by now */ |
| 451 | /** \todo free all notifications from queue */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 452 | free(locked_session->notifications); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 453 | pthread_mutex_destroy(&locked_session->lock); |
| 454 | if (locked_session->hello_message != NULL) { |
| 455 | /** \todo free hello_message */ |
| 456 | //json_object_put(locked_session->hello_message); |
| 457 | locked_session->hello_message = NULL; |
| 458 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 459 | locked_session->session = NULL; |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 460 | free(locked_session); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 461 | locked_session = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 462 | DEBUG("NETCONF session closed, everything cleared."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 463 | return (EXIT_SUCCESS); |
| 464 | } |
| 465 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 466 | static int netconf_close(const char *session_id, json_object **reply) |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 467 | { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 468 | struct session_with_mutex *locked_session; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 469 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 470 | DEBUG("Session to close: %s", session_id); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 471 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 472 | /* get exclusive (write) access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 473 | DEBUG("lock session lock."); |
| 474 | DEBUG("LOCK wrlock %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 475 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 476 | ERROR("Error while locking rwlock"); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 477 | (*reply) = create_error("Internal: Error while locking."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 478 | return EXIT_FAILURE; |
| 479 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 480 | /* remove session from the active sessions list -> nobody new can now work with session */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 481 | for (locked_session = netconf_sessions_list; |
Michal Vasko | 59ccb36 | 2015-11-05 10:21:44 +0100 | [diff] [blame] | 482 | locked_session && strcmp(nc_session_get_id(locked_session->session), session_id); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 483 | locked_session = locked_session->next); |
| 484 | |
| 485 | if (!locked_session) { |
| 486 | ERROR("Could not find the session \"%s\" to close.", session_id); |
| 487 | (*reply) = create_error("Internal: Error while finding a session."); |
| 488 | return EXIT_FAILURE; |
| 489 | } |
| 490 | |
| 491 | if (!locked_session->prev) { |
| 492 | netconf_sessions_list = netconf_sessions_list->next; |
| 493 | netconf_sessions_list->prev = NULL; |
| 494 | } else { |
| 495 | locked_session->prev->next = locked_session->next; |
| 496 | if (locked_session->next) { |
| 497 | locked_session->next->prev = locked_session->prev; |
| 498 | } |
| 499 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 500 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 501 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 502 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 503 | ERROR("Error while unlocking rwlock"); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 504 | (*reply) = create_error("Internal: Error while unlocking."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if ((locked_session != NULL) && (locked_session->session != NULL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 508 | return close_and_free_session(locked_session); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 509 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 510 | ERROR("Unknown session to close"); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 511 | (*reply) = create_error("Internal: Unkown session to close."); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 512 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 513 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 514 | (*reply) = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 517 | /** |
| 518 | * Test reply message type and return error message. |
| 519 | * |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 520 | * \param[in] session nc_session internal struct |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 521 | * \param[in] session_key session ID, 0 to disable disconnect on error |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 522 | * \param[in] msgt RPC-REPLY message type |
| 523 | * \param[out] data |
| 524 | * \return NULL on success |
| 525 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 526 | json_object *netconf_test_reply(struct nc_session *session, const char *session_id, NC_MSG_TYPE msgt, nc_reply *reply, char **data) |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 527 | { |
| 528 | NC_REPLY_TYPE replyt; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 529 | json_object *err = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 530 | |
| 531 | /* process the result of the operation */ |
| 532 | switch (msgt) { |
| 533 | case NC_MSG_UNKNOWN: |
| 534 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 535 | ERROR("mod_netconf: receiving rpc-reply failed"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 536 | if (session_id) { |
| 537 | netconf_close(session_id, &err); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 538 | } |
| 539 | if (err != NULL) { |
| 540 | return err; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 541 | } |
| 542 | return create_error("Internal: Receiving RPC-REPLY failed."); |
| 543 | } |
| 544 | case NC_MSG_NONE: |
| 545 | /* there is error handled by callback */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 546 | if (data != NULL) { |
| 547 | free(*data); |
Tomas Cejka | 7b1e3bd | 2014-04-08 14:34:28 +0200 | [diff] [blame] | 548 | (*data) = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 549 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 550 | return NULL; |
| 551 | case NC_MSG_REPLY: |
| 552 | switch (replyt = nc_reply_get_type(reply)) { |
| 553 | case NC_REPLY_OK: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 554 | if ((data != NULL) && (*data != NULL)) { |
| 555 | free(*data); |
| 556 | (*data) = NULL; |
| 557 | } |
| 558 | return create_ok(); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 559 | case NC_REPLY_DATA: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 560 | if (((*data) = nc_reply_get_data(reply)) == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 561 | ERROR("mod_netconf: no data from reply"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 562 | return create_error("Internal: No data from reply received."); |
| 563 | } else { |
| 564 | return NULL; |
| 565 | } |
| 566 | break; |
| 567 | case NC_REPLY_ERROR: |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 568 | ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 569 | if (data != NULL) { |
| 570 | free(*data); |
| 571 | (*data) = NULL; |
| 572 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 573 | return create_error(nc_reply_get_errormsg(reply)); |
| 574 | default: |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 575 | ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 576 | if (data != NULL) { |
| 577 | free(*data); |
| 578 | (*data) = NULL; |
| 579 | } |
Tomas Cejka | 6088525 | 2014-03-26 15:45:47 +0100 | [diff] [blame] | 580 | return create_error("Unknown type of NETCONF reply."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 581 | } |
| 582 | break; |
| 583 | default: |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 584 | ERROR("mod_netconf: unexpected reply message received (%d)", msgt); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 585 | if (data != NULL) { |
| 586 | free(*data); |
| 587 | (*data) = NULL; |
| 588 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 589 | return create_error("Internal: Unexpected RPC-REPLY message type."); |
| 590 | } |
| 591 | } |
| 592 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 593 | json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 594 | { |
| 595 | nc_reply* reply = NULL; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 596 | NC_MSG_TYPE msgt; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 597 | |
| 598 | /* check requests */ |
| 599 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 600 | ERROR("mod_netconf: rpc is not created"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 601 | return create_error("Internal error: RPC is not created"); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | if (session != NULL) { |
| 605 | /* send the request and get the reply */ |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 606 | msgt = netconf_send_recv_timed(session, rpc, 5000, &reply); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 607 | /* process the result of the operation */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 608 | return netconf_test_reply(session, 0, msgt, reply, NULL); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 609 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 610 | ERROR("Unknown session to process."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 611 | return create_error("Internal error: Unknown session to process."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 615 | /** |
| 616 | * Perform RPC method that returns data. |
| 617 | * |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 618 | * \param[in] session_id session identifier |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 619 | * \param[in] rpc RPC message to perform |
| 620 | * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received |
| 621 | * \return NULL on success, json object with error otherwise |
| 622 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 623 | static json_object *netconf_op(const char *session_id, nc_rpc* rpc, char **received_data) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 624 | { |
| 625 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 626 | struct session_with_mutex * locked_session; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 627 | nc_reply* reply = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 628 | json_object *res = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 629 | char *data = NULL; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 630 | NC_MSG_TYPE msgt; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 631 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 632 | /* check requests */ |
| 633 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 634 | ERROR("mod_netconf: rpc is not created"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 635 | res = create_error("Internal: RPC could not be created."); |
| 636 | data = NULL; |
| 637 | goto finished; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 638 | } |
| 639 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 640 | /* get non-exclusive (read) access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 641 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 642 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 643 | ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 644 | res = create_error("Internal: Lock failed."); |
| 645 | data = NULL; |
| 646 | goto finished; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 647 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 648 | /* get session where send the RPC */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 649 | for (locked_session = netconf_sessions_list; |
Michal Vasko | 59ccb36 | 2015-11-05 10:21:44 +0100 | [diff] [blame] | 650 | locked_session && strcmp(nc_session_get_id(locked_session->session), session_id); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 651 | locked_session = locked_session->next); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 652 | if (locked_session != NULL) { |
| 653 | session = locked_session->session; |
| 654 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 655 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 656 | /* get exclusive access to session */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 657 | DEBUG("LOCK mutex %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 658 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 659 | /* unlock before returning error */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 660 | DEBUG("UNLOCK wrlock %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 661 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 662 | |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 663 | ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 664 | res = create_error("Internal: Could not unlock."); |
| 665 | goto finished; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 666 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 667 | res = create_error("Internal: Could not unlock."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 668 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 669 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 670 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 671 | |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 672 | ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 673 | res = create_error("Internal: Could not unlock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 676 | locked_session->last_activity = time(NULL); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 677 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 678 | /* send the request and get the reply */ |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 679 | msgt = netconf_send_recv_timed(session, rpc, 5000, &reply); |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 680 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 681 | /* first release exclusive lock for this session */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 682 | DEBUG("UNLOCK mutex %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 683 | pthread_mutex_unlock(&locked_session->lock); |
| 684 | /* end of critical section */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 685 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 686 | res = netconf_test_reply(session, session_id, msgt, reply, &data); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 687 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 688 | /* release lock on failure */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 689 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 690 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 691 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 692 | } |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 693 | ERROR("Unknown session to process."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 694 | res = create_error("Unknown session to process."); |
| 695 | data = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 696 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 697 | finished: |
| 698 | nc_reply_free(reply); |
| 699 | if (received_data != NULL) { |
| 700 | (*received_data) = data; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 701 | } else { |
| 702 | if (data != NULL) { |
| 703 | free(data); |
| 704 | data = NULL; |
| 705 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 706 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 707 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 710 | static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 711 | { |
| 712 | nc_rpc* rpc; |
| 713 | struct nc_filter *f = NULL; |
| 714 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 715 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 716 | |
| 717 | /* create filter if set */ |
| 718 | if (filter != NULL) { |
| 719 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 720 | } |
| 721 | |
| 722 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 723 | rpc = nc_rpc_getconfig (source, f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 724 | nc_filter_free(f); |
| 725 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 726 | ERROR("mod_netconf: creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 727 | return (NULL); |
| 728 | } |
| 729 | |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 730 | /* tell server to show all elements even if they have default values */ |
Tomas Cejka | e8bd27c | 2014-03-27 15:16:31 +0100 | [diff] [blame] | 731 | #ifdef HAVE_WITHDEFAULTS_TAGGED |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 732 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) |
Tomas Cejka | e8bd27c | 2014-03-27 15:16:31 +0100 | [diff] [blame] | 733 | #else |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 734 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) |
| 735 | //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) |
Tomas Cejka | e8bd27c | 2014-03-27 15:16:31 +0100 | [diff] [blame] | 736 | #endif |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 737 | { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 738 | ERROR("mod_netconf: setting withdefaults failed"); |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 741 | res = netconf_op(session_key, rpc, &data); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 742 | nc_rpc_free (rpc); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 743 | if (res != NULL) { |
| 744 | (*err) = res; |
| 745 | } else { |
| 746 | (*err) = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 749 | return (data); |
| 750 | } |
| 751 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 752 | static char* netconf_getschema(const char* 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] | 753 | { |
| 754 | nc_rpc* rpc; |
| 755 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 756 | json_object *res = NULL; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 757 | |
| 758 | /* create requests */ |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 759 | rpc = nc_rpc_getschema(identifier, version, format); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 760 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 761 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 762 | return (NULL); |
| 763 | } |
| 764 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 765 | res = netconf_op(session_key, rpc, &data); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 766 | nc_rpc_free (rpc); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 767 | if (res != NULL) { |
| 768 | (*err) = res; |
| 769 | } else { |
| 770 | (*err) = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 773 | return (data); |
| 774 | } |
| 775 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 776 | static char* netconf_get(const char* session_key, const char* filter, json_object **err) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 777 | { |
| 778 | nc_rpc* rpc; |
| 779 | struct nc_filter *f = NULL; |
| 780 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 781 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 782 | |
| 783 | /* create filter if set */ |
| 784 | if (filter != NULL) { |
| 785 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 786 | } |
| 787 | |
| 788 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 789 | rpc = nc_rpc_get (f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 790 | nc_filter_free(f); |
| 791 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 792 | ERROR("mod_netconf: creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 793 | return (NULL); |
| 794 | } |
| 795 | |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 796 | /* tell server to show all elements even if they have default values */ |
Tomas Cejka | c0e166b | 2014-07-08 16:04:58 +0200 | [diff] [blame] | 797 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) { |
| 798 | //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) { |
| 799 | //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 800 | ERROR("mod_netconf: setting withdefaults failed"); |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 803 | res = netconf_op(session_key, rpc, &data); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 804 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 805 | if (res == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 806 | (*err) = res; |
| 807 | } else { |
| 808 | (*err) = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 811 | return (data); |
| 812 | } |
| 813 | |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 814 | static json_object *netconf_copyconfig(const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *uri_src, const char *uri_trg) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 815 | { |
| 816 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 817 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 818 | |
| 819 | /* create requests */ |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 820 | if (source == NC_DATASTORE_CONFIG) { |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 821 | if (target == NC_DATASTORE_URL) { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 822 | /* config, url */ |
| 823 | rpc = nc_rpc_copyconfig(source, target, config, uri_trg); |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 824 | } else { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 825 | /* config, datastore */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 826 | rpc = nc_rpc_copyconfig(source, target, config); |
| 827 | } |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 828 | } else if (source == NC_DATASTORE_URL) { |
| 829 | if (target == NC_DATASTORE_URL) { |
| 830 | /* url, url */ |
| 831 | rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg); |
| 832 | } else { |
| 833 | /* url, datastore */ |
| 834 | rpc = nc_rpc_copyconfig(source, target, uri_src); |
| 835 | } |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 836 | } else { |
| 837 | if (target == NC_DATASTORE_URL) { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 838 | /* datastore, url */ |
| 839 | rpc = nc_rpc_copyconfig(source, target, uri_trg); |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 840 | } else { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 841 | /* datastore, datastore */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 842 | rpc = nc_rpc_copyconfig(source, target); |
| 843 | } |
| 844 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 845 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 846 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 847 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 850 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 851 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 852 | |
| 853 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 854 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 855 | |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 856 | static json_object *netconf_editconfig(const char* session_key, NC_DATASTORE source, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, NC_EDIT_TESTOPT_TYPE testopt, const char* config_or_url) |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 857 | { |
| 858 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 859 | json_object *res = NULL; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 860 | |
| 861 | /* create requests */ |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 862 | rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 863 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 864 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 865 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 866 | } |
| 867 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 868 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 869 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 870 | |
| 871 | return res; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 872 | } |
| 873 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 874 | static json_object *netconf_killsession(const char* session_key, const char* sid) |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 875 | { |
| 876 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 877 | json_object *res = NULL; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 878 | |
| 879 | /* create requests */ |
| 880 | rpc = nc_rpc_killsession(sid); |
| 881 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 882 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 883 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 886 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 887 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 888 | return res; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 889 | } |
| 890 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 891 | static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE)) |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 892 | { |
| 893 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 894 | json_object *res = NULL; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 895 | |
| 896 | /* create requests */ |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 897 | rpc = op_func(target); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 898 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 899 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 900 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 903 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 904 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 905 | return res; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 906 | } |
| 907 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 908 | static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 909 | { |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 910 | nc_rpc *rpc = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 911 | json_object *res = NULL; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 912 | if (target != NC_DATASTORE_URL) { |
| 913 | rpc = nc_rpc_deleteconfig(target); |
| 914 | } else { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 915 | rpc = nc_rpc_deleteconfig(target, url); |
| 916 | } |
| 917 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 918 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 919 | return create_error("Internal: Creating rpc request failed"); |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 922 | res = netconf_op(session_key, rpc, NULL); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 923 | nc_rpc_free (rpc); |
| 924 | return res; |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 927 | static json_object *netconf_lock(const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 928 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 929 | return (netconf_onlytargetop(session_key, target, nc_rpc_lock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 932 | static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 933 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 934 | return (netconf_onlytargetop(session_key, target, nc_rpc_unlock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 937 | static json_object *netconf_generic(const char* session_key, const char* content, char** data) |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 938 | { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 939 | nc_rpc* rpc = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 940 | json_object *res = NULL; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 941 | |
| 942 | /* create requests */ |
| 943 | rpc = nc_rpc_generic(content); |
| 944 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 945 | ERROR("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 946 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 949 | if (data != NULL) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 950 | // TODO ?free(*data); |
| 951 | (*data) = NULL; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 952 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 953 | |
| 954 | /* get session where send the RPC */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 955 | res = netconf_op(session_key, rpc, data); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 956 | nc_rpc_free (rpc); |
| 957 | return res; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 960 | /** |
| 961 | * @} |
| 962 | *//* netconf_operations */ |
| 963 | |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 964 | void clb_print(NC_VERB_LEVEL level, const char* msg) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 965 | { |
Tomas Cejka | 8a86cc2 | 2014-09-18 15:35:07 +0200 | [diff] [blame] | 966 | #define FOREACH(I) \ |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 967 | I(NC_VERB_ERROR) I(NC_VERB_WARNING) |
| 968 | |
| 969 | #define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \ |
Tomas Cejka | 8a86cc2 | 2014-09-18 15:35:07 +0200 | [diff] [blame] | 970 | break; |
| 971 | |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 972 | switch (level) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 973 | FOREACH(CASE); |
| 974 | case NC_VERB_VERBOSE: |
| 975 | case NC_VERB_DEBUG: |
| 976 | DEBUG("DEBUG: %s", msg); |
| 977 | break; |
Tomas Cejka | 8a86cc2 | 2014-09-18 15:35:07 +0200 | [diff] [blame] | 978 | } |
| 979 | if (level == NC_VERB_ERROR) { |
| 980 | /* return global error */ |
| 981 | netconf_callback_error_process(NULL /* tag */, NULL /* type */, |
| 982 | NULL /* severity */, NULL /* apptag */, |
| 983 | NULL /* path */, msg, NULL /* attribute */, |
| 984 | NULL /* element */, NULL /* ns */, NULL /* sid */); |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 985 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 988 | /** |
Tomas Cejka | 6e8f426 | 2013-07-10 09:20:19 +0200 | [diff] [blame] | 989 | * Receive message from client over UNIX socket and return pointer to it. |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 990 | * Caller should free message memory. |
| 991 | * \param[in] client socket descriptor of client |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 992 | * \return pointer to message |
| 993 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 994 | char *get_framed_message(int client) |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 995 | { |
| 996 | /* read json in chunked framing */ |
| 997 | unsigned int buffer_size = 0; |
| 998 | ssize_t buffer_len = 0; |
| 999 | char *buffer = NULL; |
| 1000 | char c; |
| 1001 | ssize_t ret; |
| 1002 | int i, chunk_len; |
| 1003 | char chunk_len_str[12]; |
| 1004 | |
| 1005 | while (1) { |
| 1006 | /* read chunk length */ |
| 1007 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 1008 | if (buffer != NULL) { |
| 1009 | free (buffer); |
| 1010 | buffer = NULL; |
| 1011 | } |
| 1012 | break; |
| 1013 | } |
| 1014 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 1015 | if (buffer != NULL) { |
| 1016 | free (buffer); |
| 1017 | buffer = NULL; |
| 1018 | } |
| 1019 | break; |
| 1020 | } |
| 1021 | i=0; |
| 1022 | memset (chunk_len_str, 0, 12); |
| 1023 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 1024 | if (i==0 && c == '#') { |
| 1025 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 1026 | /* end but invalid */ |
| 1027 | if (buffer != NULL) { |
| 1028 | free (buffer); |
| 1029 | buffer = NULL; |
| 1030 | } |
| 1031 | } |
| 1032 | /* end of message, double-loop break */ |
| 1033 | goto msg_complete; |
| 1034 | } |
| 1035 | chunk_len_str[i++] = c; |
| 1036 | if (i==11) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1037 | ERROR("Message is too long, buffer for length is not big enought!!!!"); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | } |
| 1041 | if (c != '\n') { |
| 1042 | if (buffer != NULL) { |
| 1043 | free (buffer); |
| 1044 | buffer = NULL; |
| 1045 | } |
| 1046 | break; |
| 1047 | } |
| 1048 | chunk_len_str[i] = 0; |
| 1049 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 1050 | if (buffer != NULL) { |
| 1051 | free (buffer); |
| 1052 | buffer = NULL; |
| 1053 | } |
| 1054 | break; |
| 1055 | } |
| 1056 | buffer_size += chunk_len+1; |
| 1057 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1058 | memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1059 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 1060 | if (buffer != NULL) { |
| 1061 | free (buffer); |
| 1062 | buffer = NULL; |
| 1063 | } |
| 1064 | break; |
| 1065 | } |
| 1066 | buffer_len += ret; |
| 1067 | } |
| 1068 | msg_complete: |
| 1069 | return buffer; |
| 1070 | } |
| 1071 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1072 | NC_DATASTORE parse_datastore(const char *ds) |
| 1073 | { |
| 1074 | if (strcmp(ds, "running") == 0) { |
| 1075 | return NC_DATASTORE_RUNNING; |
| 1076 | } else if (strcmp(ds, "startup") == 0) { |
| 1077 | return NC_DATASTORE_STARTUP; |
| 1078 | } else if (strcmp(ds, "candidate") == 0) { |
| 1079 | return NC_DATASTORE_CANDIDATE; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1080 | } else if (strcmp(ds, "url") == 0) { |
| 1081 | return NC_DATASTORE_URL; |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 1082 | } else if (strcmp(ds, "config") == 0) { |
| 1083 | return NC_DATASTORE_CONFIG; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1084 | } |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
Tomas Cejka | 5ae8dfb | 2014-02-14 23:42:17 +0100 | [diff] [blame] | 1088 | NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t) |
| 1089 | { |
| 1090 | if (strcmp(t, "notset") == 0) { |
| 1091 | return NC_EDIT_TESTOPT_NOTSET; |
| 1092 | } else if (strcmp(t, "testset") == 0) { |
| 1093 | return NC_EDIT_TESTOPT_TESTSET; |
| 1094 | } else if (strcmp(t, "set") == 0) { |
| 1095 | return NC_EDIT_TESTOPT_SET; |
| 1096 | } else if (strcmp(t, "test") == 0) { |
| 1097 | return NC_EDIT_TESTOPT_TEST; |
| 1098 | } |
| 1099 | return NC_EDIT_TESTOPT_ERROR; |
| 1100 | } |
| 1101 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1102 | json_object *create_error(const char *errmess) |
| 1103 | { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1104 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1105 | json_object *reply = json_object_new_object(); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1106 | json_object *array = json_object_new_array(); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1107 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1108 | json_object_array_add(array, json_object_new_string(errmess)); |
| 1109 | json_object_object_add(reply, "errors", array); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1110 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1111 | return reply; |
| 1112 | |
| 1113 | } |
| 1114 | |
| 1115 | json_object *create_data(const char *data) |
| 1116 | { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1117 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1118 | json_object *reply = json_object_new_object(); |
| 1119 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1120 | json_object_object_add(reply, "data", json_object_new_string(data)); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1121 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1122 | return reply; |
| 1123 | } |
| 1124 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1125 | json_object *create_ok() |
| 1126 | { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1127 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1128 | json_object *reply = json_object_new_object(); |
| 1129 | reply = json_object_new_object(); |
| 1130 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1131 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1132 | return reply; |
| 1133 | } |
| 1134 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1135 | char *get_param_string(json_object *data, const char *name) |
| 1136 | { |
| 1137 | json_object *js_tmp = NULL; |
| 1138 | char *res = NULL; |
| 1139 | if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) { |
| 1140 | res = strdup(json_object_get_string(js_tmp)); |
| 1141 | json_object_put(js_tmp); |
| 1142 | } |
| 1143 | return res; |
| 1144 | } |
| 1145 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1146 | json_object *handle_op_connect(json_object *request) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1147 | { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1148 | char *host = NULL; |
| 1149 | char *port = NULL; |
| 1150 | char *user = NULL; |
| 1151 | char *pass = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1152 | json_object *capabilities = NULL; |
| 1153 | json_object *reply = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1154 | const char *session_id = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1155 | struct nc_cpblts* cpblts = NULL; |
| 1156 | unsigned int len, i; |
| 1157 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1158 | DEBUG("Request: Connect"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1159 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1160 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1161 | host = get_param_string(request, "host"); |
| 1162 | port = get_param_string(request, "port"); |
| 1163 | user = get_param_string(request, "user"); |
| 1164 | pass = get_param_string(request, "pass"); |
| 1165 | |
| 1166 | if (json_object_object_get_ex(request, "capabilities", &capabilities) == TRUE) { |
| 1167 | if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) { |
| 1168 | cpblts = nc_cpblts_new(NULL); |
| 1169 | for (i=0; i<len; i++) { |
| 1170 | nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i))); |
| 1171 | } |
| 1172 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1173 | ERROR("no capabilities specified"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1174 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1175 | json_object_put(capabilities); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1176 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1177 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1178 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1179 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1180 | DEBUG("host: %s, port: %s, user: %s", host, port, user); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1181 | if ((host == NULL) || (user == NULL)) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1182 | ERROR("Cannot connect - insufficient input."); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1183 | session_id = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1184 | } else { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1185 | session_id = netconf_connect(host, port, user, pass, cpblts); |
| 1186 | DEBUG("SID: %s", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1187 | } |
| 1188 | if (cpblts != NULL) { |
| 1189 | nc_cpblts_free(cpblts); |
| 1190 | } |
| 1191 | |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1192 | GETSPEC_ERR_REPLY |
| 1193 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1194 | pthread_mutex_lock(&json_lock); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1195 | if (session_id == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1196 | /* negative reply */ |
| 1197 | if (err_reply == NULL) { |
| 1198 | reply = json_object_new_object(); |
| 1199 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1200 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1201 | ERROR("Connection failed."); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1202 | } else { |
| 1203 | /* use filled err_reply from libnetconf's callback */ |
| 1204 | reply = err_reply; |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1205 | ERROR("Connect - error from libnetconf's callback."); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1206 | } |
| 1207 | } else { |
| 1208 | /* positive reply */ |
| 1209 | reply = json_object_new_object(); |
| 1210 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1211 | json_object_object_add(reply, "session", json_object_new_string(session_id)); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1212 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1213 | memset(pass, 0, strlen(pass)); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1214 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1215 | CHECK_AND_FREE(host); |
| 1216 | CHECK_AND_FREE(user); |
| 1217 | CHECK_AND_FREE(port); |
| 1218 | CHECK_AND_FREE(pass); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1219 | return reply; |
| 1220 | } |
| 1221 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1222 | json_object *handle_op_get(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1223 | { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1224 | char *filter = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1225 | char *data = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1226 | json_object *reply = NULL; |
| 1227 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1228 | DEBUG("Request: get (session %s)", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1229 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1230 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1231 | filter = get_param_string(request, "filter"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1232 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1233 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1234 | if ((data = netconf_get(session_id, filter, &reply)) == NULL) { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1235 | CHECK_ERR_SET_REPLY_ERR("Get information failed.") |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1236 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1237 | reply = create_data(data); |
| 1238 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1239 | } |
| 1240 | return reply; |
| 1241 | } |
| 1242 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1243 | json_object *handle_op_getconfig(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1244 | { |
| 1245 | NC_DATASTORE ds_type_s = -1; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1246 | char *filter = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1247 | char *data = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1248 | char *source = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1249 | json_object *reply = NULL; |
| 1250 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1251 | DEBUG("Request: get-config (session %s)", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1252 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1253 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1254 | filter = get_param_string(request, "filter"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1255 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1256 | source = get_param_string(request, "source"); |
| 1257 | if (source != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1258 | ds_type_s = parse_datastore(source); |
| 1259 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1260 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1261 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1262 | if ((int)ds_type_s == -1) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1263 | reply = create_error("Invalid source repository type requested."); |
| 1264 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1267 | if ((data = netconf_getconfig(session_id, ds_type_s, filter, &reply)) == NULL) { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1268 | CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.") |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1269 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1270 | reply = create_data(data); |
| 1271 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1272 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1273 | finalize: |
| 1274 | CHECK_AND_FREE(filter); |
| 1275 | CHECK_AND_FREE(source); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1276 | return reply; |
| 1277 | } |
| 1278 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1279 | json_object *handle_op_getschema(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1280 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1281 | char *data = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1282 | char *identifier = NULL; |
| 1283 | char *version = NULL; |
| 1284 | char *format = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1285 | json_object *reply = NULL; |
| 1286 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1287 | DEBUG("Request: get-schema (session %s)", session_id); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1288 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1289 | identifier = get_param_string(request, "identifier"); |
| 1290 | version = get_param_string(request, "version"); |
| 1291 | format = get_param_string(request, "format"); |
Tomas Cejka | b9e7efe | 2014-03-28 21:09:04 +0100 | [diff] [blame] | 1292 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1293 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1294 | if (identifier == NULL) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1295 | reply = create_error("No identifier for get-schema supplied."); |
| 1296 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1297 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1298 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1299 | DEBUG("get-schema(version: %s, format: %s)", version, format); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1300 | if ((data = netconf_getschema(session_id, identifier, version, format, &reply)) == NULL) { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1301 | CHECK_ERR_SET_REPLY_ERR("Get models operation failed.") |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1302 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1303 | reply = create_data(data); |
| 1304 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1305 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1306 | finalize: |
| 1307 | CHECK_AND_FREE(identifier); |
| 1308 | CHECK_AND_FREE(version); |
| 1309 | CHECK_AND_FREE(format); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1310 | return reply; |
| 1311 | } |
| 1312 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1313 | json_object *handle_op_editconfig(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1314 | { |
| 1315 | NC_DATASTORE ds_type_s = -1; |
| 1316 | NC_DATASTORE ds_type_t = -1; |
| 1317 | NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1318 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
Tomas Cejka | 5ae8dfb | 2014-02-14 23:42:17 +0100 | [diff] [blame] | 1319 | NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1320 | char *defop = NULL; |
| 1321 | char *erropt = NULL; |
| 1322 | char *config = NULL; |
| 1323 | char *source = NULL; |
| 1324 | char *target = NULL; |
| 1325 | char *testopt = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1326 | json_object *reply = NULL; |
| 1327 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1328 | DEBUG("Request: edit-config (session %s)", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1329 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1330 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1331 | /* get parameters */ |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1332 | defop = get_param_string(request, "default-operation"); |
| 1333 | erropt = get_param_string(request, "error-option"); |
| 1334 | target = get_param_string(request, "target"); |
| 1335 | source = get_param_string(request, "source"); |
| 1336 | config = get_param_string(request, "config"); |
| 1337 | testopt = get_param_string(request, "test-option"); |
| 1338 | pthread_mutex_unlock(&json_lock); |
| 1339 | |
| 1340 | if (target != NULL) { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1341 | ds_type_t = parse_datastore(target); |
| 1342 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1343 | if (source != NULL) { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1344 | ds_type_s = parse_datastore(source); |
| 1345 | } else { |
| 1346 | /* source is optional, default value is config */ |
| 1347 | ds_type_s = NC_DATASTORE_CONFIG; |
| 1348 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1349 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1350 | if (defop != NULL) { |
| 1351 | if (strcmp(defop, "merge") == 0) { |
| 1352 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 1353 | } else if (strcmp(defop, "replace") == 0) { |
| 1354 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 1355 | } else if (strcmp(defop, "none") == 0) { |
| 1356 | defop_type = NC_EDIT_DEFOP_NONE; |
| 1357 | } else { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1358 | reply = create_error("Invalid default-operation parameter."); |
| 1359 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1360 | } |
| 1361 | } else { |
| 1362 | defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1363 | } |
| 1364 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1365 | if (erropt != NULL) { |
| 1366 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 1367 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 1368 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 1369 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 1370 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 1371 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 1372 | } else { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1373 | reply = create_error("Invalid error-option parameter."); |
| 1374 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1375 | } |
| 1376 | } else { |
| 1377 | erropt_type = 0; |
| 1378 | } |
| 1379 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1380 | if ((int)ds_type_t == -1) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1381 | reply = create_error("Invalid target repository type requested."); |
| 1382 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1383 | } |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 1384 | if (ds_type_s == NC_DATASTORE_CONFIG) { |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 1385 | if (config == NULL) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1386 | reply = create_error("Invalid config data parameter."); |
| 1387 | goto finalize; |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 1388 | } |
| 1389 | } else if (ds_type_s == NC_DATASTORE_URL){ |
Tomas Cejka | 8f3031e | 2014-02-14 23:15:08 +0100 | [diff] [blame] | 1390 | if (config == NULL) { |
| 1391 | config = ""; |
| 1392 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
Tomas Cejka | 5ae8dfb | 2014-02-14 23:42:17 +0100 | [diff] [blame] | 1395 | if (testopt != NULL) { |
| 1396 | testopt_type = parse_testopt(testopt); |
| 1397 | } else { |
| 1398 | testopt_type = NC_EDIT_TESTOPT_TESTSET; |
| 1399 | } |
| 1400 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1401 | reply = netconf_editconfig(session_id, ds_type_s, ds_type_t, defop_type, erropt_type, testopt_type, config); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1402 | |
| 1403 | CHECK_ERR_SET_REPLY |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1404 | finalize: |
| 1405 | CHECK_AND_FREE(defop); |
| 1406 | CHECK_AND_FREE(erropt); |
| 1407 | CHECK_AND_FREE(config); |
| 1408 | CHECK_AND_FREE(source); |
| 1409 | CHECK_AND_FREE(target); |
| 1410 | CHECK_AND_FREE(testopt); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1411 | return reply; |
| 1412 | } |
| 1413 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1414 | json_object *handle_op_copyconfig(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1415 | { |
| 1416 | NC_DATASTORE ds_type_s = -1; |
| 1417 | NC_DATASTORE ds_type_t = -1; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1418 | char *config = NULL; |
| 1419 | char *target = NULL; |
| 1420 | char *source = NULL; |
| 1421 | char *uri_src = NULL; |
| 1422 | char *uri_trg = NULL; |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 1423 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1424 | json_object *reply = NULL; |
| 1425 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1426 | DEBUG("Request: copy-config (session %s)", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1427 | |
| 1428 | /* get parameters */ |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1429 | pthread_mutex_lock(&json_lock); |
| 1430 | target = get_param_string(request, "target"); |
| 1431 | source = get_param_string(request, "source"); |
| 1432 | config = get_param_string(request, "config"); |
| 1433 | uri_src = get_param_string(request, "uri-source"); |
| 1434 | uri_trg = get_param_string(request, "uri-target"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1435 | pthread_mutex_unlock(&json_lock); |
| 1436 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1437 | if (target != NULL) { |
| 1438 | ds_type_t = parse_datastore(target); |
| 1439 | } |
| 1440 | if (source != NULL) { |
| 1441 | ds_type_s = parse_datastore(source); |
| 1442 | } else { |
| 1443 | /* source == NULL *//* no explicit source specified -> use config data */ |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1444 | ds_type_s = NC_DATASTORE_CONFIG; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1445 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1446 | if ((int)ds_type_s == -1) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1447 | /* source datastore specified, but it is invalid */ |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1448 | reply = create_error("Invalid source repository type requested."); |
| 1449 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1452 | if ((int)ds_type_t == -1) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1453 | /* invalid target datastore specified */ |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1454 | reply = create_error("Invalid target repository type requested."); |
| 1455 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Tomas Cejka | 55c6df5 | 2014-02-20 12:59:33 +0100 | [diff] [blame] | 1458 | /* source can be missing when config is given */ |
| 1459 | if (source == NULL && config == NULL) { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 1460 | reply = create_error("invalid input parameters - source and config is required."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1461 | goto finalize; |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | if (ds_type_s == NC_DATASTORE_URL) { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 1465 | if (uri_src == NULL) { |
| 1466 | uri_src = ""; |
| 1467 | } |
| 1468 | } |
| 1469 | if (ds_type_t == NC_DATASTORE_URL) { |
Tomas Cejka | b4d0587 | 2014-02-14 22:44:38 +0100 | [diff] [blame] | 1470 | if (uri_trg == NULL) { |
| 1471 | uri_trg = ""; |
| 1472 | } |
| 1473 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1474 | reply = netconf_copyconfig(session_id, ds_type_s, ds_type_t, config, uri_src, uri_trg); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1475 | |
| 1476 | CHECK_ERR_SET_REPLY |
| 1477 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1478 | finalize: |
| 1479 | CHECK_AND_FREE(config); |
| 1480 | CHECK_AND_FREE(target); |
| 1481 | CHECK_AND_FREE(source); |
| 1482 | CHECK_AND_FREE(uri_src); |
| 1483 | CHECK_AND_FREE(uri_trg); |
| 1484 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1485 | return reply; |
| 1486 | } |
| 1487 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1488 | json_object *handle_op_generic(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1489 | { |
| 1490 | json_object *reply = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1491 | char *config = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1492 | char *data = NULL; |
| 1493 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1494 | DEBUG("Request: generic request for session %s", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1495 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1496 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1497 | config = get_param_string(request, "content"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1498 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1499 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1500 | reply = netconf_generic(session_id, config, &data); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1501 | if (reply == NULL) { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1502 | GETSPEC_ERR_REPLY |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1503 | if (err_reply != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1504 | /* use filled err_reply from libnetconf's callback */ |
| 1505 | reply = err_reply; |
| 1506 | } |
| 1507 | } else { |
| 1508 | if (data == NULL) { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1509 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1510 | reply = json_object_new_object(); |
| 1511 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1512 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1513 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1514 | reply = create_data(data); |
| 1515 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1516 | } |
| 1517 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1518 | CHECK_AND_FREE(config); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1519 | return reply; |
| 1520 | } |
| 1521 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1522 | json_object *handle_op_disconnect(json_object *UNUSED(request), const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1523 | { |
| 1524 | json_object *reply = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1525 | DEBUG("Request: Disconnect session %s", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1526 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1527 | if (netconf_close(session_id, &reply) != EXIT_SUCCESS) { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1528 | CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.") |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1529 | } else { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1530 | reply = create_ok(); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1531 | } |
| 1532 | return reply; |
| 1533 | } |
| 1534 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1535 | json_object *handle_op_kill(json_object *request, const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1536 | { |
| 1537 | json_object *reply = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1538 | char *sid = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1539 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1540 | DEBUG("Request: kill-session, session %s", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1541 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1542 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1543 | sid = get_param_string(request, "session-id"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1544 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1545 | |
| 1546 | if (sid == NULL) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1547 | reply = create_error("Missing session-id parameter."); |
| 1548 | goto finalize; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1551 | reply = netconf_killsession(session_id, sid); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1552 | |
| 1553 | CHECK_ERR_SET_REPLY |
| 1554 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1555 | finalize: |
| 1556 | CHECK_AND_FREE(sid); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1557 | return reply; |
| 1558 | } |
| 1559 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1560 | json_object *handle_op_reloadhello(json_object *UNUSED(request), const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1561 | { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1562 | struct nc_session *temp_session = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1563 | struct session_with_mutex * locked_session = NULL; |
| 1564 | json_object *reply = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1565 | DEBUG("Request: get info about session %s", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1566 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1567 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 93b0e49 | 2014-03-26 15:47:45 +0100 | [diff] [blame] | 1568 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1569 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1570 | return NULL; |
| 1571 | } |
| 1572 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1573 | for (locked_session = netconf_sessions_list; |
Michal Vasko | 59ccb36 | 2015-11-05 10:21:44 +0100 | [diff] [blame] | 1574 | locked_session && strcmp(nc_session_get_id(locked_session->session), session_id); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1575 | locked_session = locked_session->next); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1576 | if ((locked_session != NULL) && (locked_session->hello_message != NULL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1577 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1578 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1579 | DEBUG("creating temporal NC session."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1580 | temp_session = nc_session_connect_channel(locked_session->session, NULL); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1581 | if (temp_session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1582 | prepare_status_message(locked_session, temp_session); |
| 1583 | DEBUG("closing temporal NC session."); |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 1584 | nc_session_free(temp_session); |
Tomas Cejka | 73496bf | 2014-03-26 15:31:09 +0100 | [diff] [blame] | 1585 | temp_session = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1586 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1587 | DEBUG("Reload hello failed due to channel establishment"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1588 | reply = create_error("Reload was unsuccessful, connection failed."); |
| 1589 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1590 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1591 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | 93b0e49 | 2014-03-26 15:47:45 +0100 | [diff] [blame] | 1592 | DEBUG("UNLOCK wrlock %s", __func__); |
| 1593 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1594 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 93b0e49 | 2014-03-26 15:47:45 +0100 | [diff] [blame] | 1595 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1596 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1597 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1598 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1599 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1600 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1601 | reply = create_error("Invalid session identifier."); |
| 1602 | } |
| 1603 | |
| 1604 | if ((reply == NULL) && (locked_session->hello_message != NULL)) { |
| 1605 | reply = locked_session->hello_message; |
| 1606 | } |
| 1607 | return reply; |
| 1608 | } |
| 1609 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1610 | json_object *handle_op_info(json_object *UNUSED(request), const char *session_id) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1611 | { |
| 1612 | json_object *reply = NULL; |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1613 | struct session_with_mutex * locked_session = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1614 | DEBUG("Request: get info about session %s", session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1615 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1616 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1617 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1618 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1619 | } |
| 1620 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1621 | for (locked_session = netconf_sessions_list; |
Michal Vasko | 59ccb36 | 2015-11-05 10:21:44 +0100 | [diff] [blame] | 1622 | locked_session && strcmp(nc_session_get_id(locked_session->session), session_id); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1623 | locked_session = locked_session->next); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1624 | if (locked_session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1625 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1626 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1627 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1628 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1629 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1630 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1631 | if (locked_session->hello_message != NULL) { |
| 1632 | reply = locked_session->hello_message; |
| 1633 | } else { |
| 1634 | reply = create_error("Invalid session identifier."); |
| 1635 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1636 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1637 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1638 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1639 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1640 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1641 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1642 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1643 | reply = create_error("Invalid session identifier."); |
| 1644 | } |
| 1645 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1646 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1647 | return reply; |
| 1648 | } |
| 1649 | |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1650 | void notification_history(time_t eventtime, const char *content) |
| 1651 | { |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1652 | json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key); |
| 1653 | if (notif_history_array == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1654 | ERROR("No list of notification history found."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1655 | return; |
| 1656 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1657 | DEBUG("Got notification from history %lu.", (long unsigned) eventtime); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1658 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1659 | json_object *notif = json_object_new_object(); |
| 1660 | if (notif == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1661 | ERROR("Could not allocate memory for notification (json)."); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1662 | goto failed; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1663 | } |
| 1664 | json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime)); |
| 1665 | json_object_object_add(notif, "content", json_object_new_string(content)); |
| 1666 | json_object_array_add(notif_history_array, notif); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1667 | failed: |
| 1668 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1669 | } |
| 1670 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1671 | json_object *handle_op_ntfgethistory(json_object *request, const char *session_id) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1672 | { |
| 1673 | json_object *reply = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1674 | json_object *js_tmp = NULL; |
| 1675 | char *sid = NULL; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1676 | struct session_with_mutex *locked_session = NULL; |
| 1677 | struct nc_session *temp_session = NULL; |
| 1678 | nc_rpc *rpc = NULL; |
| 1679 | time_t start = 0; |
| 1680 | time_t stop = 0; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1681 | int64_t from = 0, to = 0; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1682 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1683 | DEBUG("Request: get notification history, session %s", session_id); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1684 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1685 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1686 | sid = get_param_string(request, "session"); |
| 1687 | |
| 1688 | if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) { |
| 1689 | from = json_object_get_int64(js_tmp); |
| 1690 | json_object_put(js_tmp); |
| 1691 | } |
| 1692 | if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) { |
| 1693 | to = json_object_get_int64(js_tmp); |
| 1694 | json_object_put(js_tmp); |
| 1695 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1696 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1697 | |
| 1698 | start = time(NULL) + from; |
| 1699 | stop = time(NULL) + to; |
| 1700 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1701 | DEBUG("notification history interval %li %li", (long int) from, (long int) to); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1702 | |
| 1703 | if (sid == NULL) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1704 | reply = create_error("Missing session parameter."); |
| 1705 | goto finalize; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1708 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1709 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1710 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1711 | reply = create_error("Internal lock failed."); |
| 1712 | goto finalize; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1715 | for (locked_session = netconf_sessions_list; |
Michal Vasko | 59ccb36 | 2015-11-05 10:21:44 +0100 | [diff] [blame] | 1716 | locked_session && strcmp(nc_session_get_id(locked_session->session), session_id); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1717 | locked_session = locked_session->next); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1718 | if (locked_session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1719 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1720 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1721 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1722 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1723 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1724 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1725 | DEBUG("creating temporal NC session."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1726 | temp_session = nc_session_connect_channel(locked_session->session, NULL); |
| 1727 | if (temp_session != NULL) { |
| 1728 | rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop); |
| 1729 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1730 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1731 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1732 | DEBUG("notifications: creating an rpc request failed."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1733 | reply = create_error("notifications: creating an rpc request failed."); |
| 1734 | goto finalize; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1737 | DEBUG("Send NC subscribe."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1738 | /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1739 | json_object *res = netconf_unlocked_op(temp_session, rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1740 | if (res != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1741 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1742 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1743 | DEBUG("Subscription RPC failed."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1744 | reply = res; |
| 1745 | goto finalize; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1746 | } |
| 1747 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
| 1748 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1749 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1750 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1751 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1752 | pthread_mutex_lock(&ntf_history_lock); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1753 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1754 | json_object *notif_history_array = json_object_new_array(); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1755 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1756 | if (pthread_setspecific(notif_history_key, notif_history_array) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1757 | ERROR("notif_history: cannot set thread-specific hash value."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1758 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1759 | |
| 1760 | ncntf_dispatch_receive(temp_session, notification_history); |
| 1761 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1762 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1763 | reply = json_object_new_object(); |
| 1764 | json_object_object_add(reply, "notifications", notif_history_array); |
| 1765 | //json_object_put(notif_history_array); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1766 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1767 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1768 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1769 | pthread_mutex_unlock(&ntf_history_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1770 | DEBUG("closing temporal NC session."); |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 1771 | nc_session_free(temp_session); |
Tomas Cejka | 73496bf | 2014-03-26 15:31:09 +0100 | [diff] [blame] | 1772 | temp_session = NULL; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1773 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1774 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1775 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1776 | DEBUG("Get history of notification failed due to channel establishment"); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1777 | reply = create_error("Get history of notification was unsuccessful, connection failed."); |
| 1778 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1779 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1780 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1781 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1782 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1783 | } |
| 1784 | reply = create_error("Invalid session identifier."); |
| 1785 | } |
| 1786 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1787 | finalize: |
| 1788 | CHECK_AND_FREE(sid); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1789 | return reply; |
| 1790 | } |
| 1791 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1792 | json_object *handle_op_validate(json_object *request, const char *session_id) |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1793 | { |
| 1794 | json_object *reply = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1795 | char *sid = NULL; |
| 1796 | char *target = NULL; |
| 1797 | char *url = NULL; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1798 | nc_rpc *rpc = NULL; |
| 1799 | NC_DATASTORE target_ds; |
| 1800 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1801 | DEBUG("Request: validate datastore, session %s", session_id); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1802 | |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1803 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1804 | sid = get_param_string(request, "session"); |
| 1805 | target = get_param_string(request, "target"); |
| 1806 | url = get_param_string(request, "url"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1807 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1808 | |
| 1809 | |
| 1810 | if ((sid == NULL) || (target == NULL)) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1811 | reply = create_error("Missing session parameter."); |
| 1812 | goto finalize; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1813 | } |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1814 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1815 | /* validation */ |
| 1816 | target_ds = parse_datastore(target); |
| 1817 | if (target_ds == NC_DATASTORE_URL) { |
| 1818 | if (url != NULL) { |
| 1819 | rpc = nc_rpc_validate(target_ds, url); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1820 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1821 | } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP) |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1822 | || (target_ds == NC_DATASTORE_CANDIDATE)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1823 | rpc = nc_rpc_validate(target_ds); |
| 1824 | } |
| 1825 | if (rpc == NULL) { |
| 1826 | DEBUG("mod_netconf: creating rpc request failed"); |
| 1827 | reply = create_error("Creation of RPC request failed."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1828 | goto finalize; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1829 | } |
| 1830 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1831 | DEBUG("Request: validate datastore"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1832 | if ((reply = netconf_op(session_id, rpc, NULL)) == NULL) { |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1833 | |
| 1834 | CHECK_ERR_SET_REPLY |
| 1835 | |
| 1836 | if (reply == NULL) { |
Tomas Cejka | 4ad470b | 2014-03-20 15:30:50 +0100 | [diff] [blame] | 1837 | DEBUG("Request: validation ok."); |
| 1838 | reply = create_ok(); |
Tomas Cejka | 4ad470b | 2014-03-20 15:30:50 +0100 | [diff] [blame] | 1839 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1840 | } |
| 1841 | nc_rpc_free (rpc); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1842 | finalize: |
| 1843 | CHECK_AND_FREE(sid); |
| 1844 | CHECK_AND_FREE(target); |
| 1845 | CHECK_AND_FREE(url); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1846 | return reply; |
| 1847 | } |
| 1848 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1849 | void * thread_routine (void * arg) |
| 1850 | { |
| 1851 | void * retval = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1852 | struct pollfd fds; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1853 | json_object *request = NULL; |
| 1854 | json_object *reply = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1855 | json_object *js_tmp = NULL; |
| 1856 | int operation = (-1); |
| 1857 | NC_DATASTORE ds_type_t = -1; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1858 | int status = 0; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1859 | const char *msgtext; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1860 | char *session_id = NULL; |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1861 | char *target = NULL; |
| 1862 | char *url = NULL; |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1863 | char *chunked_out_msg = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1864 | //server_rec * server = ((struct pass_to_thread*)arg)->server; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1865 | int client = ((struct pass_to_thread*)arg)->client; |
| 1866 | |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1867 | char *buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1868 | |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 1869 | /* init thread specific err_reply memory */ |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 1870 | create_err_reply_p(); |
| 1871 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1872 | while (!isterminated) { |
| 1873 | fds.fd = client; |
| 1874 | fds.events = POLLIN; |
| 1875 | fds.revents = 0; |
| 1876 | |
| 1877 | status = poll(&fds, 1, 1000); |
| 1878 | |
| 1879 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 1880 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1881 | continue; |
| 1882 | } else if (status < 0) { |
| 1883 | /* 0: poll time outed |
| 1884 | * close socket and ignore this request from the client, it can try it again |
| 1885 | * -1: poll failed |
| 1886 | * something wrong happend, close this socket and wait for another request |
| 1887 | */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1888 | close(client); |
| 1889 | break; |
| 1890 | } |
| 1891 | /* status > 0 */ |
| 1892 | |
| 1893 | /* check the status of the socket */ |
| 1894 | |
| 1895 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 1896 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 1897 | /* close client's socket (it's probably already closed by client */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1898 | close(client); |
| 1899 | break; |
| 1900 | } |
| 1901 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1902 | DEBUG("Get framed message..."); |
| 1903 | buffer = get_framed_message(client); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1904 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1905 | DEBUG("Check read buffer."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1906 | if (buffer != NULL) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1907 | enum json_tokener_error jerr; |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1908 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1909 | request = json_tokener_parse_verbose(buffer, &jerr); |
| 1910 | if (jerr != json_tokener_success) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 1911 | ERROR("JSON parsing error"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1912 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1913 | continue; |
| 1914 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1915 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1916 | session_id = get_param_string(request, "session"); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1917 | if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) { |
| 1918 | operation = json_object_get_int(js_tmp); |
| 1919 | json_object_put(js_tmp); |
| 1920 | js_tmp = NULL; |
| 1921 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1922 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1923 | if (operation == -1) { |
| 1924 | reply = create_error("Missing operation type form frontend."); |
| 1925 | goto send_reply; |
| 1926 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1927 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1928 | DEBUG("operation %d session_id %s.", operation, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1929 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1930 | if (operation != MSG_CONNECT && session_id == NULL) { |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1931 | reply = create_error("Missing session specification."); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1932 | pthread_mutex_lock(&json_lock); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1933 | msgtext = json_object_to_json_string(reply); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1934 | pthread_mutex_unlock(&json_lock); |
| 1935 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1936 | send(client, msgtext, strlen(msgtext) + 1, 0); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1937 | |
| 1938 | pthread_mutex_lock(&json_lock); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1939 | json_object_put(reply); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1940 | pthread_mutex_unlock(&json_lock); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1941 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 1942 | close(client); |
| 1943 | break; |
| 1944 | } |
| 1945 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1946 | /* null global JSON error-reply */ |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 1947 | clean_err_reply(); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1948 | |
| 1949 | /* prepare reply envelope */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1950 | if (reply != NULL) { |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1951 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1952 | json_object_put(reply); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1953 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1954 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1955 | reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1956 | |
| 1957 | /* process required operation */ |
| 1958 | switch (operation) { |
| 1959 | case MSG_CONNECT: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1960 | reply = handle_op_connect(request); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1961 | break; |
| 1962 | case MSG_GET: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1963 | reply = handle_op_get(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1964 | break; |
| 1965 | case MSG_GETCONFIG: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1966 | reply = handle_op_getconfig(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1967 | break; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1968 | case MSG_GETSCHEMA: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1969 | reply = handle_op_getschema(request, session_id); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1970 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1971 | case MSG_EDITCONFIG: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1972 | reply = handle_op_editconfig(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1973 | break; |
| 1974 | case MSG_COPYCONFIG: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1975 | reply = handle_op_copyconfig(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1976 | break; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1977 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1978 | case MSG_DELETECONFIG: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1979 | case MSG_LOCK: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1980 | case MSG_UNLOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1981 | /* get parameters */ |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1982 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1983 | target = get_param_string(request, "target"); |
| 1984 | pthread_mutex_unlock(&json_lock); |
| 1985 | if (target != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1986 | ds_type_t = parse_datastore(target); |
| 1987 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1988 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1989 | if ((int)ds_type_t == -1) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1990 | reply = create_error("Invalid target repository type requested."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1991 | break; |
| 1992 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1993 | switch(operation) { |
| 1994 | case MSG_DELETECONFIG: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1995 | DEBUG("Request: delete-config (session %s)", session_id); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1996 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 1997 | url = get_param_string(request, "url"); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 1998 | pthread_mutex_unlock(&json_lock); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 1999 | reply = netconf_deleteconfig(session_id, ds_type_t, url); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2000 | break; |
| 2001 | case MSG_LOCK: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2002 | DEBUG("Request: lock (session %s)", session_id); |
| 2003 | reply = netconf_lock(session_id, ds_type_t); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2004 | break; |
| 2005 | case MSG_UNLOCK: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2006 | DEBUG("Request: unlock (session %s)", session_id); |
| 2007 | reply = netconf_unlock(session_id, ds_type_t); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2008 | break; |
| 2009 | default: |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 2010 | reply = create_error("Internal: Unknown request type."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2011 | break; |
| 2012 | } |
| 2013 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 2014 | CHECK_ERR_SET_REPLY |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 2015 | if (reply == NULL) { |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2016 | reply = create_ok(); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2017 | } |
| 2018 | break; |
| 2019 | case MSG_KILL: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2020 | reply = handle_op_kill(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2021 | break; |
| 2022 | case MSG_DISCONNECT: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2023 | reply = handle_op_disconnect(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2024 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 2025 | case MSG_RELOADHELLO: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2026 | reply = handle_op_reloadhello(request, session_id); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2027 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 2028 | case MSG_INFO: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2029 | reply = handle_op_info(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2030 | break; |
| 2031 | case MSG_GENERIC: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2032 | reply = handle_op_generic(request, session_id); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2033 | break; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 2034 | case MSG_NTF_GETHISTORY: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2035 | reply = handle_op_ntfgethistory(request, session_id); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 2036 | break; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 2037 | case MSG_VALIDATE: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2038 | reply = handle_op_validate(request, session_id); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 2039 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2040 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2041 | DEBUG("Unknown mod_netconf operation requested (%d)", operation); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 2042 | reply = create_error("Operation not supported."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2043 | break; |
| 2044 | } |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2045 | /* free parameters */ |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2046 | CHECK_AND_FREE(url); |
| 2047 | CHECK_AND_FREE(target); |
| 2048 | request = NULL; |
| 2049 | operation = (-1); |
| 2050 | ds_type_t = (-1); |
| 2051 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2052 | DEBUG("Clean request json object."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2053 | if (request != NULL) { |
Tomas Cejka | 74dd7a9 | 2014-09-18 15:58:45 +0200 | [diff] [blame] | 2054 | pthread_mutex_lock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2055 | json_object_put(request); |
Tomas Cejka | 74dd7a9 | 2014-09-18 15:58:45 +0200 | [diff] [blame] | 2056 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2057 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2058 | DEBUG("Send reply json object."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2059 | |
| 2060 | |
| 2061 | send_reply: |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 2062 | /* send reply to caller */ |
| 2063 | if (reply != NULL) { |
Tomas Cejka | 74dd7a9 | 2014-09-18 15:58:45 +0200 | [diff] [blame] | 2064 | pthread_mutex_lock(&json_lock); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 2065 | msgtext = json_object_to_json_string(reply); |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 2066 | if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int) strlen(msgtext), msgtext) == -1) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 2067 | if (buffer != NULL) { |
| 2068 | free(buffer); |
| 2069 | buffer = NULL; |
| 2070 | } |
Tomas Cejka | 74dd7a9 | 2014-09-18 15:58:45 +0200 | [diff] [blame] | 2071 | pthread_mutex_unlock(&json_lock); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2072 | break; |
| 2073 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2074 | pthread_mutex_unlock(&json_lock); |
| 2075 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2076 | DEBUG("Send framed reply json object."); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2077 | send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2078 | DEBUG("Clean reply json object."); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2079 | pthread_mutex_lock(&json_lock); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 2080 | json_object_put(reply); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2081 | reply = NULL; |
| 2082 | DEBUG("Clean message buffer."); |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 2083 | CHECK_AND_FREE(chunked_out_msg); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 2084 | chunked_out_msg = NULL; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 2085 | if (buffer != NULL) { |
| 2086 | free(buffer); |
| 2087 | buffer = NULL; |
| 2088 | } |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2089 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 2090 | clean_err_reply(); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 2091 | } else { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2092 | ERROR("Reply is NULL, shouldn't be..."); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2093 | continue; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2097 | free (arg); |
| 2098 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 2099 | free_err_reply(); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 2100 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2101 | return retval; |
| 2102 | } |
| 2103 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2104 | /** |
| 2105 | * \brief Close all open NETCONF sessions. |
| 2106 | * |
| 2107 | * During termination of mod_netconf, it is useful to close all remaining |
| 2108 | * sessions. This function iterates over the list of sessions and close them |
| 2109 | * all. |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2110 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2111 | static void close_all_nc_sessions(void) |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2112 | { |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2113 | struct session_with_mutex *swm = NULL; |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2114 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2115 | |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2116 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2117 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2118 | if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2119 | ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2120 | return; |
| 2121 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2122 | for (swm = netconf_sessions_list; swm; swm = swm->next) { |
| 2123 | DEBUG("Closing NETCONF session (%s).", nc_session_get_id(swm->session)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2124 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2125 | /* close_and_free_session handles locking on its own */ |
| 2126 | close_and_free_session(swm); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2127 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2128 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2129 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2130 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2131 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2132 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2133 | } |
| 2134 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2135 | static void check_timeout_and_close(void) |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2136 | { |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2137 | struct nc_session *ns = NULL; |
| 2138 | struct session_with_mutex *swm = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2139 | time_t current_time = time(NULL); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2140 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2141 | |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2142 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 2143 | if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2144 | DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2145 | return; |
| 2146 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2147 | for (swm = netconf_sessions_list; swm; swm = swm->next) { |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2148 | ns = swm->session; |
| 2149 | if (ns == NULL) { |
| 2150 | continue; |
| 2151 | } |
| 2152 | pthread_mutex_lock(&swm->lock); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2153 | if ((current_time - swm->last_activity) > ACTIVITY_TIMEOUT) { |
| 2154 | DEBUG("Closing NETCONF session (%s).", nc_session_get_id(swm->session)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2155 | |
| 2156 | /* close_and_free_session handles locking on its own */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2157 | close_and_free_session(swm); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2158 | } else { |
| 2159 | pthread_mutex_unlock(&swm->lock); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2160 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2161 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2162 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 2163 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2164 | ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 2165 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | |
| 2169 | /** |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2170 | * This is actually implementation of NETCONF client |
| 2171 | * - requests are received from UNIX socket in the predefined format |
| 2172 | * - results are replied through the same way |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2173 | * - the daemon run as a separate process |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2174 | * |
| 2175 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2176 | static void forked_proc(void) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2177 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2178 | struct timeval tv; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2179 | struct sockaddr_un local, remote; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2180 | int lsock, client, ret, i, pthread_count = 0; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2181 | unsigned int olds = 0, timediff = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2182 | socklen_t len; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2183 | struct pass_to_thread * arg; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2184 | pthread_t * ptids = calloc(1, sizeof(pthread_t)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2185 | struct timespec maxtime; |
| 2186 | pthread_rwlockattr_t lock_attrs; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 2187 | #ifdef WITH_NOTIFICATIONS |
| 2188 | char use_notifications = 0; |
| 2189 | #endif |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2190 | |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 2191 | /* wait at most 5 seconds for every thread to terminate */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2192 | maxtime.tv_sec = 5; |
| 2193 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2194 | |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2195 | #ifdef HAVE_UNIXD_SETUP_CHILD |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2196 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2197 | unixd_setup_child(); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2198 | #else |
| 2199 | # ifdef SU_GROUP |
| 2200 | if (strlen(SU_GROUP) > 0) { |
| 2201 | struct group *g = getgrnam(SU_GROUP); |
| 2202 | if (g == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2203 | ERROR("GID (%s) was not found.", SU_GROUP); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2204 | return; |
| 2205 | } |
| 2206 | if (setgid(g->gr_gid) != 0) { |
| 2207 | |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2208 | ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno)); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2209 | return; |
| 2210 | } |
| 2211 | } |
| 2212 | # else |
| 2213 | DEBUG("no SU_GROUP"); |
| 2214 | # endif |
| 2215 | # ifdef SU_USER |
| 2216 | if (strlen(SU_USER) > 0) { |
| 2217 | struct passwd *p = getpwnam(SU_USER); |
| 2218 | if (p == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2219 | ERROR("UID (%s) was not found.", SU_USER); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2220 | return; |
| 2221 | } |
| 2222 | if (setuid(p->pw_uid) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2223 | ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno)); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2224 | return; |
| 2225 | } |
| 2226 | } |
| 2227 | # else |
| 2228 | DEBUG("no SU_USER"); |
| 2229 | # endif |
| 2230 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2231 | |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2232 | /* try to remove if exists */ |
| 2233 | unlink(sockname); |
| 2234 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2235 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2236 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2237 | ERROR("Creating socket failed (%s)", strerror(errno)); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2238 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | local.sun_family = AF_UNIX; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2242 | strncpy(local.sun_path, sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2243 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 2244 | |
| 2245 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 2246 | if (errno == EADDRINUSE) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2247 | ERROR("mod_netconf socket address already in use"); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2248 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2249 | } |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2250 | ERROR("Binding socket failed (%s)", strerror(errno)); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2251 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2255 | ERROR("Setting up listen socket failed (%s)", strerror(errno)); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2256 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2257 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2258 | 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] | 2259 | |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2260 | uid_t user = -1; |
| 2261 | if (strlen(CHOWN_USER) > 0) { |
| 2262 | struct passwd *p = getpwnam(CHOWN_USER); |
| 2263 | if (p != NULL) { |
| 2264 | user = p->pw_uid; |
| 2265 | } |
| 2266 | } |
| 2267 | gid_t group = -1; |
| 2268 | if (strlen(CHOWN_GROUP) > 0) { |
| 2269 | struct group *g = getgrnam(CHOWN_GROUP); |
| 2270 | if (g != NULL) { |
| 2271 | group = g->gr_gid; |
| 2272 | } |
| 2273 | } |
| 2274 | if (chown(sockname, user, group) == -1) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2275 | ERROR("Chown on socket file failed (%s).", strerror(errno)); |
Tomas Cejka | 04e08f4 | 2014-03-27 19:52:34 +0100 | [diff] [blame] | 2276 | } |
| 2277 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 2278 | /* prepare internal lists */ |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 2279 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2280 | #ifdef WITH_NOTIFICATIONS |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2281 | if (notification_init() == -1) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2282 | ERROR("libwebsockets initialization failed"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2283 | use_notifications = 0; |
| 2284 | } else { |
| 2285 | use_notifications = 1; |
| 2286 | } |
| 2287 | #endif |
| 2288 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2289 | /* setup libnetconf's callbacks */ |
| 2290 | nc_verbosity(NC_VERB_DEBUG); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2291 | nc_callback_print(clb_print); |
| 2292 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 2293 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 2294 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame] | 2295 | nc_callback_sshauth_passphrase(netconf_callback_sshauth_passphrase); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 2296 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2297 | |
| 2298 | /* disable publickey authentication */ |
| 2299 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 2300 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2301 | /* create mutex protecting session list */ |
| 2302 | pthread_rwlockattr_init(&lock_attrs); |
| 2303 | /* rwlock is shared only with threads in this process */ |
| 2304 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 2305 | /* create rw lock */ |
| 2306 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2307 | ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2308 | goto error_exit; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2309 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2310 | pthread_mutex_init(&ntf_history_lock, NULL); |
Tomas Cejka | 9a23f6e | 2014-03-27 14:57:00 +0100 | [diff] [blame] | 2311 | pthread_mutex_init(&json_lock, NULL); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2312 | DEBUG("Initialization of notification history."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 2313 | if (pthread_key_create(¬if_history_key, NULL) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2314 | ERROR("Initialization of notification history failed."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 2315 | } |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 2316 | if (pthread_key_create(&err_reply_key, NULL) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2317 | ERROR("Initialization of reply key failed."); |
Tomas Cejka | edb3ab4 | 2014-03-27 15:04:00 +0100 | [diff] [blame] | 2318 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 2319 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2320 | fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2321 | while (isterminated == 0) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2322 | gettimeofday(&tv, NULL); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2323 | timediff = (unsigned int)tv.tv_sec - olds; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2324 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2325 | if (use_notifications == 1) { |
| 2326 | notification_handle(); |
| 2327 | } |
| 2328 | #endif |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2329 | if (timediff > ACTIVITY_CHECK_INTERVAL) { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2330 | check_timeout_and_close(); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2331 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2332 | |
| 2333 | /* open incoming connection if any */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2334 | len = sizeof(remote); |
| 2335 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2336 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
Michal Vasko | 7732dee | 2015-11-05 10:22:15 +0100 | [diff] [blame] | 2337 | usleep(SLEEP_TIME * 1000); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2338 | continue; |
| 2339 | } else if (client == -1 && (errno == EINTR)) { |
| 2340 | continue; |
| 2341 | } else if (client == -1) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2342 | ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2343 | continue; |
| 2344 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2345 | |
| 2346 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2347 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2348 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2349 | arg = malloc(sizeof(struct pass_to_thread)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2350 | arg->client = client; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2351 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2352 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2353 | /* start new thread. It will serve this particular request and then terminate */ |
| 2354 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 2355 | ERROR("Creating POSIX thread failed: %d\n", ret); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2356 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2357 | DEBUG("Thread %lu created", ptids[pthread_count]); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2358 | pthread_count++; |
| 2359 | ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1)); |
| 2360 | ptids[pthread_count] = 0; |
| 2361 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2362 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2363 | /* check if some thread already terminated, free some resources by joining it */ |
| 2364 | for (i=0; i<pthread_count; i++) { |
| 2365 | if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2366 | DEBUG("Thread %lu joined with retval %p", ptids[i], arg); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2367 | pthread_count--; |
| 2368 | if (pthread_count > 0) { |
| 2369 | /* place last Thread ID on the place of joined one */ |
| 2370 | ptids[i] = ptids[pthread_count]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2371 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2372 | } |
| 2373 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2374 | DEBUG("Running %d threads", pthread_count); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2375 | } |
| 2376 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2377 | DEBUG("mod_netconf terminating..."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2378 | /* join all threads */ |
| 2379 | for (i=0; i<pthread_count; i++) { |
| 2380 | pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime); |
| 2381 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2382 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2383 | #ifdef WITH_NOTIFICATIONS |
| 2384 | notification_close(); |
| 2385 | #endif |
| 2386 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2387 | /* close all NETCONF sessions */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2388 | close_all_nc_sessions(); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2389 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2390 | /* destroy rwlock */ |
| 2391 | pthread_rwlock_destroy(&session_lock); |
| 2392 | pthread_rwlockattr_destroy(&lock_attrs); |
| 2393 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2394 | DEBUG("Exiting from the mod_netconf daemon"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2395 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2396 | free(ptids); |
| 2397 | close(lsock); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2398 | exit(0); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2399 | return; |
| 2400 | error_exit: |
| 2401 | close(lsock); |
| 2402 | free(ptids); |
| 2403 | return; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2406 | int main(int argc, char **argv) |
| 2407 | { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 2408 | struct sigaction action; |
| 2409 | sigset_t block_mask; |
| 2410 | |
| 2411 | if (argc > 1) { |
| 2412 | sockname = argv[1]; |
| 2413 | } else { |
| 2414 | sockname = SOCKET_FILENAME; |
| 2415 | } |
| 2416 | |
| 2417 | sigfillset (&block_mask); |
| 2418 | action.sa_handler = signal_handler; |
| 2419 | action.sa_mask = block_mask; |
| 2420 | action.sa_flags = 0; |
| 2421 | sigaction(SIGINT, &action, NULL); |
| 2422 | sigaction(SIGTERM, &action, NULL); |
| 2423 | |
| 2424 | forked_proc(); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2425 | DEBUG("Terminated"); |
| 2426 | return 0; |
| 2427 | } |