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