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