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