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