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 | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 414 | /** |
| 415 | * Test reply message type and return error message. |
| 416 | * |
| 417 | * \param[in] server httpd server for logging |
| 418 | * \param[in] session nc_session internal struct |
| 419 | * \param[in] session_key session key, NULL to disable disconnect on error |
| 420 | * \param[in] msgt RPC-REPLY message type |
| 421 | * \param[out] data |
| 422 | * \return NULL on success |
| 423 | */ |
| 424 | json_object *netconf_test_reply(server_rec *server, struct nc_session *session, const char *session_key, NC_MSG_TYPE msgt, nc_reply *reply, char **data) |
| 425 | { |
| 426 | NC_REPLY_TYPE replyt; |
| 427 | |
| 428 | /* process the result of the operation */ |
| 429 | switch (msgt) { |
| 430 | case NC_MSG_UNKNOWN: |
| 431 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 432 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 433 | if (session_key != NULL) { |
| 434 | netconf_close(server, session_key); |
| 435 | } |
| 436 | return create_error("Internal: Receiving RPC-REPLY failed."); |
| 437 | } |
| 438 | case NC_MSG_NONE: |
| 439 | /* there is error handled by callback */ |
| 440 | (*data) = NULL; |
| 441 | return NULL; |
| 442 | case NC_MSG_REPLY: |
| 443 | switch (replyt = nc_reply_get_type(reply)) { |
| 444 | case NC_REPLY_OK: |
| 445 | (*data) = NULL; |
| 446 | return NULL; |
| 447 | case NC_REPLY_DATA: |
| 448 | if (((*data) = nc_reply_get_data (reply)) == NULL) { |
| 449 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 450 | return create_error("Internal: No data from reply received."); |
| 451 | } else { |
| 452 | return NULL; |
| 453 | } |
| 454 | break; |
| 455 | case NC_REPLY_ERROR: |
| 456 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 457 | (*data) = NULL; |
| 458 | return create_error(nc_reply_get_errormsg(reply)); |
| 459 | default: |
| 460 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 461 | (*data) = NULL; |
| 462 | return create_error(nc_reply_get_errormsg(reply)); |
| 463 | } |
| 464 | break; |
| 465 | default: |
| 466 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt); |
| 467 | (*data) = NULL; |
| 468 | return create_error("Internal: Unexpected RPC-REPLY message type."); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | json_object *netconf_unlocked_op(server_rec* server, struct nc_session *session, nc_rpc* rpc) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 473 | { |
| 474 | nc_reply* reply = NULL; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 475 | NC_MSG_TYPE msgt; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 476 | |
| 477 | /* check requests */ |
| 478 | if (rpc == NULL) { |
| 479 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 480 | return create_error("Internal error: RPC is not created"); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if (session != NULL) { |
| 484 | /* send the request and get the reply */ |
| 485 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 486 | /* process the result of the operation */ |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 487 | return netconf_test_reply(server, session, NULL, msgt, reply, NULL); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 488 | } else { |
| 489 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 490 | return create_error("Internal error: Unknown session to process."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 494 | /** |
| 495 | * Perform RPC method that returns data. |
| 496 | * |
| 497 | * \param[in] server httpd server |
| 498 | * \param[in] session_key session identifier |
| 499 | * \param[in] rpc RPC message to perform |
| 500 | * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received |
| 501 | * \return NULL on success, json object with error otherwise |
| 502 | */ |
| 503 | static json_object *netconf_op(server_rec* server, const char* session_key, nc_rpc* rpc, char **received_data) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 504 | { |
| 505 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 506 | struct session_with_mutex * locked_session; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 507 | nc_reply* reply = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 508 | json_object *res = NULL; |
| 509 | char* data; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 510 | NC_MSG_TYPE msgt; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 511 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 512 | /* check requests */ |
| 513 | if (rpc == NULL) { |
| 514 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 515 | res = create_error("Internal: RPC could not be created."); |
| 516 | data = NULL; |
| 517 | goto finished; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 518 | } |
| 519 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 520 | /* get non-exclusive (read) access to sessions_list (conns) */ |
| 521 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 522 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 523 | res = create_error("Internal: Lock failed."); |
| 524 | data = NULL; |
| 525 | goto finished; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 526 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 527 | /* get session where send the RPC */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 528 | 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] | 529 | if (locked_session != NULL) { |
| 530 | session = locked_session->session; |
| 531 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 532 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 533 | /* get exclusive access to session */ |
| 534 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 535 | /* unlock before returning error */ |
| 536 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 537 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 538 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 539 | res = create_error("Internal: Could not unlock."); |
| 540 | goto finished; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 541 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 542 | res = create_error("Internal: Could not unlock."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 543 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 544 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 545 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 546 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 547 | res = create_error("Internal: Could not unlock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 550 | locked_session->last_activity = apr_time_now(); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 551 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 552 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 553 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 554 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 555 | /* first release exclusive lock for this session */ |
| 556 | pthread_mutex_unlock(&locked_session->lock); |
| 557 | /* end of critical section */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 558 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 559 | res = netconf_test_reply(server, session, session_key, msgt, reply, &data); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 560 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 561 | /* release lock on failure */ |
| 562 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 563 | 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] | 564 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 565 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 566 | res = create_error("Unknown session to process."); |
| 567 | data = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 568 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 569 | finished: |
| 570 | nc_reply_free(reply); |
| 571 | if (received_data != NULL) { |
| 572 | (*received_data) = data; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 573 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 574 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 577 | 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] | 578 | { |
| 579 | nc_rpc* rpc; |
| 580 | struct nc_filter *f = NULL; |
| 581 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 582 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 583 | |
| 584 | /* create filter if set */ |
| 585 | if (filter != NULL) { |
| 586 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 587 | } |
| 588 | |
| 589 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 590 | rpc = nc_rpc_getconfig (source, f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 591 | nc_filter_free(f); |
| 592 | if (rpc == NULL) { |
| 593 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 594 | return (NULL); |
| 595 | } |
| 596 | |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 597 | /* tell server to show all elements even if they have default values */ |
| 598 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) { |
| 599 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed"); |
| 600 | } |
| 601 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 602 | res = netconf_op(server, session_key, rpc, &data); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 603 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 604 | if (res == NULL) { |
| 605 | /* TODO return error message somehow */ |
| 606 | } |
| 607 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 608 | return (data); |
| 609 | } |
| 610 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 611 | 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] | 612 | { |
| 613 | nc_rpc* rpc; |
| 614 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 615 | json_object *res = NULL; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 616 | |
| 617 | /* create requests */ |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 618 | rpc = nc_rpc_getschema(identifier, version, format); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 619 | if (rpc == NULL) { |
| 620 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 621 | return (NULL); |
| 622 | } |
| 623 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 624 | res = netconf_op(server, session_key, rpc, &data); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 625 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 626 | if (res == NULL) { |
| 627 | /* TODO return error message somehow */ |
| 628 | } |
| 629 | |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 630 | return (data); |
| 631 | } |
| 632 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 633 | 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] | 634 | { |
| 635 | nc_rpc* rpc; |
| 636 | struct nc_filter *f = NULL; |
| 637 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 638 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 639 | |
| 640 | /* create filter if set */ |
| 641 | if (filter != NULL) { |
| 642 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 643 | } |
| 644 | |
| 645 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 646 | rpc = nc_rpc_get (f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 647 | nc_filter_free(f); |
| 648 | if (rpc == NULL) { |
| 649 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 650 | return (NULL); |
| 651 | } |
| 652 | |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 653 | /* tell server to show all elements even if they have default values */ |
| 654 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) { |
| 655 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed"); |
| 656 | } |
| 657 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 658 | res = netconf_op(server, session_key, rpc, &data); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 659 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 660 | if (res == NULL) { |
| 661 | /* TODO return error message somehow */ |
| 662 | } |
| 663 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 664 | return (data); |
| 665 | } |
| 666 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 667 | static json_object *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] | 668 | { |
| 669 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 670 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 671 | |
| 672 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 673 | if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) { |
| 674 | if (target == NC_DATASTORE_URL) { |
| 675 | rpc = nc_rpc_copyconfig(source, target, config, url); |
| 676 | } else { |
| 677 | rpc = nc_rpc_copyconfig(source, target, config); |
| 678 | } |
| 679 | } else { |
| 680 | if (target == NC_DATASTORE_URL) { |
| 681 | rpc = nc_rpc_copyconfig(source, target, url); |
| 682 | } else { |
| 683 | rpc = nc_rpc_copyconfig(source, target); |
| 684 | } |
| 685 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 686 | if (rpc == NULL) { |
| 687 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 688 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 689 | } |
| 690 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 691 | res = netconf_op(server, session_key, rpc, NULL); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 692 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 693 | |
| 694 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 695 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 696 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 697 | static json_object *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] | 698 | { |
| 699 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 700 | json_object *res = NULL; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 701 | |
| 702 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 703 | /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */ |
| 704 | rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 705 | if (rpc == NULL) { |
| 706 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 707 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 710 | res = netconf_op(server, session_key, rpc, NULL); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 711 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 712 | |
| 713 | return res; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 716 | static json_object *netconf_killsession(server_rec* server, const char* session_key, const char* sid) |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 717 | { |
| 718 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 719 | json_object *res = NULL; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 720 | |
| 721 | /* create requests */ |
| 722 | rpc = nc_rpc_killsession(sid); |
| 723 | if (rpc == NULL) { |
| 724 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 725 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 728 | res = netconf_op(server, session_key, rpc, NULL); |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 729 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 730 | return res; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 733 | static json_object *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] | 734 | { |
| 735 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 736 | json_object *res = NULL; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 737 | |
| 738 | /* create requests */ |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 739 | rpc = op_func(target); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 740 | if (rpc == NULL) { |
| 741 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 742 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 745 | res = netconf_op(server, session_key, rpc, NULL); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 746 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 747 | return res; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 750 | static json_object *netconf_deleteconfig(server_rec* server, const char* session_key, NC_DATASTORE target, const char *url) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 751 | { |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 752 | nc_rpc *rpc = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 753 | json_object *res = NULL; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 754 | if (target != NC_DATASTORE_URL) { |
| 755 | rpc = nc_rpc_deleteconfig(target); |
| 756 | } else { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 757 | rpc = nc_rpc_deleteconfig(target, url); |
| 758 | } |
| 759 | if (rpc == NULL) { |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 760 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 761 | return create_error("Internal: Creating rpc request failed"); |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 764 | res = netconf_op(server, session_key, rpc, NULL); |
| 765 | nc_rpc_free (rpc); |
| 766 | return res; |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 769 | static json_object *netconf_lock(server_rec* server, const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 770 | { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 771 | return (netconf_onlytargetop(server, session_key, target, nc_rpc_lock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 774 | static json_object *netconf_unlock(server_rec* server, const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 775 | { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 776 | return (netconf_onlytargetop(server, session_key, target, nc_rpc_unlock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 777 | } |
| 778 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 779 | static json_object *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] | 780 | { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 781 | nc_rpc* rpc = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 782 | json_object *res = NULL; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 783 | |
| 784 | /* create requests */ |
| 785 | rpc = nc_rpc_generic(content); |
| 786 | if (rpc == NULL) { |
| 787 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 788 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 791 | if (data != NULL) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 792 | // TODO ?free(*data); |
| 793 | (*data) = NULL; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 794 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 795 | |
| 796 | /* get session where send the RPC */ |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 797 | res = netconf_op(server, session_key, rpc, data); |
| 798 | nc_rpc_free (rpc); |
| 799 | return res; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 802 | /** |
| 803 | * @} |
| 804 | *//* netconf_operations */ |
| 805 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 806 | server_rec* clb_print_server; |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 807 | void clb_print(NC_VERB_LEVEL level, const char* msg) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 808 | { |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 809 | switch (level) { |
| 810 | case NC_VERB_ERROR: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 811 | 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] | 812 | break; |
| 813 | case NC_VERB_WARNING: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 814 | 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] | 815 | break; |
| 816 | case NC_VERB_VERBOSE: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 817 | 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] | 818 | break; |
| 819 | case NC_VERB_DEBUG: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 820 | 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] | 821 | break; |
| 822 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 825 | /** |
Tomas Cejka | 6e8f426 | 2013-07-10 09:20:19 +0200 | [diff] [blame] | 826 | * Receive message from client over UNIX socket and return pointer to it. |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 827 | * Caller should free message memory. |
| 828 | * \param[in] client socket descriptor of client |
| 829 | * \param[in] server httpd server for logging |
| 830 | * \return pointer to message |
| 831 | */ |
| 832 | char *get_framed_message(server_rec *server, int client) |
| 833 | { |
| 834 | /* read json in chunked framing */ |
| 835 | unsigned int buffer_size = 0; |
| 836 | ssize_t buffer_len = 0; |
| 837 | char *buffer = NULL; |
| 838 | char c; |
| 839 | ssize_t ret; |
| 840 | int i, chunk_len; |
| 841 | char chunk_len_str[12]; |
| 842 | |
| 843 | while (1) { |
| 844 | /* read chunk length */ |
| 845 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 846 | if (buffer != NULL) { |
| 847 | free (buffer); |
| 848 | buffer = NULL; |
| 849 | } |
| 850 | break; |
| 851 | } |
| 852 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 853 | if (buffer != NULL) { |
| 854 | free (buffer); |
| 855 | buffer = NULL; |
| 856 | } |
| 857 | break; |
| 858 | } |
| 859 | i=0; |
| 860 | memset (chunk_len_str, 0, 12); |
| 861 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 862 | if (i==0 && c == '#') { |
| 863 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 864 | /* end but invalid */ |
| 865 | if (buffer != NULL) { |
| 866 | free (buffer); |
| 867 | buffer = NULL; |
| 868 | } |
| 869 | } |
| 870 | /* end of message, double-loop break */ |
| 871 | goto msg_complete; |
| 872 | } |
| 873 | chunk_len_str[i++] = c; |
| 874 | if (i==11) { |
| 875 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!"); |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | if (c != '\n') { |
| 880 | if (buffer != NULL) { |
| 881 | free (buffer); |
| 882 | buffer = NULL; |
| 883 | } |
| 884 | break; |
| 885 | } |
| 886 | chunk_len_str[i] = 0; |
| 887 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 888 | if (buffer != NULL) { |
| 889 | free (buffer); |
| 890 | buffer = NULL; |
| 891 | } |
| 892 | break; |
| 893 | } |
| 894 | buffer_size += chunk_len+1; |
| 895 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 896 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 897 | if (buffer != NULL) { |
| 898 | free (buffer); |
| 899 | buffer = NULL; |
| 900 | } |
| 901 | break; |
| 902 | } |
| 903 | buffer_len += ret; |
| 904 | } |
| 905 | msg_complete: |
| 906 | return buffer; |
| 907 | } |
| 908 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 909 | NC_DATASTORE parse_datastore(const char *ds) |
| 910 | { |
| 911 | if (strcmp(ds, "running") == 0) { |
| 912 | return NC_DATASTORE_RUNNING; |
| 913 | } else if (strcmp(ds, "startup") == 0) { |
| 914 | return NC_DATASTORE_STARTUP; |
| 915 | } else if (strcmp(ds, "candidate") == 0) { |
| 916 | return NC_DATASTORE_CANDIDATE; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 917 | } else if (strcmp(ds, "url") == 0) { |
| 918 | return NC_DATASTORE_URL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 919 | } |
| 920 | return -1; |
| 921 | } |
| 922 | |
| 923 | json_object *create_error(const char *errmess) |
| 924 | { |
| 925 | json_object *reply = json_object_new_object(); |
| 926 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 927 | json_object_object_add(reply, "error-message", json_object_new_string(errmess)); |
| 928 | return reply; |
| 929 | |
| 930 | } |
| 931 | |
| 932 | json_object *create_data(const char *data) |
| 933 | { |
| 934 | json_object *reply = json_object_new_object(); |
| 935 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 936 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 937 | return reply; |
| 938 | } |
| 939 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 940 | 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] | 941 | { |
| 942 | const char *host = NULL; |
| 943 | const char *port = NULL; |
| 944 | const char *user = NULL; |
| 945 | const char *pass = NULL; |
| 946 | json_object *capabilities = NULL; |
| 947 | json_object *reply = NULL; |
| 948 | char *session_key_hash = NULL; |
| 949 | struct nc_cpblts* cpblts = NULL; |
| 950 | unsigned int len, i; |
| 951 | |
| 952 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 953 | host = json_object_get_string(json_object_object_get((json_object *) request, "host")); |
| 954 | port = json_object_get_string(json_object_object_get((json_object *) request, "port")); |
| 955 | user = json_object_get_string(json_object_object_get((json_object *) request, "user")); |
| 956 | pass = json_object_get_string(json_object_object_get((json_object *) request, "pass")); |
| 957 | |
| 958 | capabilities = json_object_object_get((json_object *) request, "capabilities"); |
| 959 | if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) { |
| 960 | cpblts = nc_cpblts_new(NULL); |
| 961 | for (i=0; i<len; i++) { |
| 962 | nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i))); |
| 963 | } |
| 964 | } else { |
| 965 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified"); |
| 966 | } |
| 967 | |
| 968 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 969 | if ((host == NULL) || (user == NULL)) { |
| 970 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input."); |
| 971 | session_key_hash = NULL; |
| 972 | } else { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 973 | session_key_hash = netconf_connect(server, pool, host, port, user, pass, cpblts); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 974 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash); |
| 975 | } |
| 976 | if (cpblts != NULL) { |
| 977 | nc_cpblts_free(cpblts); |
| 978 | } |
| 979 | |
| 980 | if (session_key_hash == NULL) { |
| 981 | /* negative reply */ |
| 982 | if (err_reply == NULL) { |
| 983 | reply = json_object_new_object(); |
| 984 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 985 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
| 986 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed."); |
| 987 | } else { |
| 988 | /* use filled err_reply from libnetconf's callback */ |
| 989 | reply = err_reply; |
| 990 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback."); |
| 991 | } |
| 992 | } else { |
| 993 | /* positive reply */ |
| 994 | reply = json_object_new_object(); |
| 995 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 996 | json_object_object_add(reply, "session", json_object_new_string(session_key_hash)); |
| 997 | |
| 998 | free(session_key_hash); |
| 999 | } |
| 1000 | return reply; |
| 1001 | } |
| 1002 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1003 | 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] | 1004 | { |
| 1005 | const char *filter = NULL; |
| 1006 | const char *data = NULL; |
| 1007 | json_object *reply = NULL; |
| 1008 | |
| 1009 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key); |
| 1010 | |
| 1011 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1012 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1013 | if ((data = netconf_get(server, session_key, filter)) == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1014 | if (err_reply == NULL) { |
| 1015 | reply = create_error("Get information from device failed."); |
| 1016 | } else { |
| 1017 | /* use filled err_reply from libnetconf's callback */ |
| 1018 | reply = err_reply; |
| 1019 | } |
| 1020 | } else { |
| 1021 | return create_data(data); |
| 1022 | } |
| 1023 | return reply; |
| 1024 | } |
| 1025 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1026 | 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] | 1027 | { |
| 1028 | NC_DATASTORE ds_type_s = -1; |
| 1029 | NC_DATASTORE ds_type_t = -1; |
| 1030 | const char *filter = NULL; |
| 1031 | const char *data = NULL; |
| 1032 | const char *source = NULL; |
| 1033 | const char *target = NULL; |
| 1034 | json_object *reply = NULL; |
| 1035 | |
| 1036 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
| 1037 | |
| 1038 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1039 | |
| 1040 | /* get parameters */ |
| 1041 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1042 | ds_type_t = parse_datastore(target); |
| 1043 | } |
| 1044 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1045 | ds_type_s = parse_datastore(source); |
| 1046 | } |
| 1047 | if (ds_type_s == -1) { |
| 1048 | return create_error("Invalid source repository type requested."); |
| 1049 | } |
| 1050 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1051 | if ((data = netconf_getconfig(server, session_key, ds_type_s, filter)) == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1052 | if (err_reply == NULL) { |
| 1053 | reply = create_error("Get configuration information from device failed."); |
| 1054 | } else { |
| 1055 | /* use filled err_reply from libnetconf's callback */ |
| 1056 | reply = err_reply; |
| 1057 | } |
| 1058 | } else { |
| 1059 | return create_data(data); |
| 1060 | } |
| 1061 | return reply; |
| 1062 | } |
| 1063 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1064 | 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] | 1065 | { |
| 1066 | const char *data = NULL; |
| 1067 | const char *identifier = NULL; |
| 1068 | const char *version = NULL; |
| 1069 | const char *format = NULL; |
| 1070 | json_object *reply = NULL; |
| 1071 | |
| 1072 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key); |
| 1073 | identifier = json_object_get_string(json_object_object_get(request, "identifier")); |
| 1074 | if (identifier == NULL) { |
| 1075 | return create_error("No identifier for get-schema supplied."); |
| 1076 | } |
| 1077 | version = json_object_get_string(json_object_object_get(request, "version")); |
| 1078 | format = json_object_get_string(json_object_object_get(request, "format")); |
| 1079 | |
| 1080 | 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] | 1081 | if ((data = netconf_getschema(server, session_key, identifier, version, format)) == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1082 | if (err_reply == NULL) { |
| 1083 | reply = create_error("Get schema failed."); |
| 1084 | } else { |
| 1085 | /* use filled err_reply from libnetconf's callback */ |
| 1086 | reply = err_reply; |
| 1087 | } |
| 1088 | } else { |
| 1089 | return create_data(data); |
| 1090 | } |
| 1091 | return reply; |
| 1092 | } |
| 1093 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1094 | 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] | 1095 | { |
| 1096 | NC_DATASTORE ds_type_s = -1; |
| 1097 | NC_DATASTORE ds_type_t = -1; |
| 1098 | NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1099 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
| 1100 | const char *defop = NULL; |
| 1101 | const char *erropt = NULL; |
| 1102 | const char *config = NULL; |
| 1103 | const char *source = NULL; |
| 1104 | const char *target = NULL; |
| 1105 | json_object *reply = NULL; |
| 1106 | |
| 1107 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key); |
| 1108 | |
| 1109 | defop = json_object_get_string(json_object_object_get(request, "default-operation")); |
| 1110 | if (defop != NULL) { |
| 1111 | if (strcmp(defop, "merge") == 0) { |
| 1112 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 1113 | } else if (strcmp(defop, "replace") == 0) { |
| 1114 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 1115 | } else if (strcmp(defop, "none") == 0) { |
| 1116 | defop_type = NC_EDIT_DEFOP_NONE; |
| 1117 | } else { |
| 1118 | return create_error("Invalid default-operation parameter."); |
| 1119 | } |
| 1120 | } else { |
| 1121 | defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1122 | } |
| 1123 | |
| 1124 | erropt = json_object_get_string(json_object_object_get(request, "error-option")); |
| 1125 | if (erropt != NULL) { |
| 1126 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 1127 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 1128 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 1129 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 1130 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 1131 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 1132 | } else { |
| 1133 | return create_error("Invalid error-option parameter."); |
| 1134 | } |
| 1135 | } else { |
| 1136 | erropt_type = 0; |
| 1137 | } |
| 1138 | |
| 1139 | /* get parameters */ |
| 1140 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1141 | ds_type_t = parse_datastore(target); |
| 1142 | } |
| 1143 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1144 | ds_type_s = parse_datastore(source); |
| 1145 | } |
| 1146 | if (ds_type_t == -1) { |
| 1147 | return create_error("Invalid target repository type requested."); |
| 1148 | } |
| 1149 | |
| 1150 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1151 | if (config == NULL) { |
| 1152 | return create_error("Invalid config data parameter."); |
| 1153 | } |
| 1154 | |
Tomas Cejka | 114b55d | 2013-10-01 00:04:37 +0200 | [diff] [blame] | 1155 | 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] | 1156 | if (err_reply == NULL) { |
| 1157 | reply = create_error("Edit-config failed."); |
| 1158 | } else { |
| 1159 | /* use filled err_reply from libnetconf's callback */ |
| 1160 | reply = err_reply; |
| 1161 | } |
| 1162 | } else { |
| 1163 | reply = json_object_new_object(); |
| 1164 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1165 | } |
| 1166 | return reply; |
| 1167 | } |
| 1168 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1169 | 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] | 1170 | { |
| 1171 | NC_DATASTORE ds_type_s = -1; |
| 1172 | NC_DATASTORE ds_type_t = -1; |
| 1173 | const char *config = NULL; |
| 1174 | const char *target = NULL; |
| 1175 | const char *source = NULL; |
| 1176 | json_object *reply = NULL; |
| 1177 | |
| 1178 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key); |
| 1179 | |
| 1180 | /* get parameters */ |
| 1181 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1182 | ds_type_t = parse_datastore(target); |
| 1183 | } |
| 1184 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1185 | ds_type_s = parse_datastore(source); |
| 1186 | } |
| 1187 | if (source == NULL) { |
| 1188 | /* no explicit source specified -> use config data */ |
| 1189 | ds_type_s = NC_DATASTORE_CONFIG; |
| 1190 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1191 | } else if (ds_type_s == -1) { |
| 1192 | /* source datastore specified, but it is invalid */ |
| 1193 | return create_error("Invalid source repository type requested."); |
| 1194 | } |
| 1195 | |
| 1196 | if (ds_type_t == -1) { |
| 1197 | /* invalid target datastore specified */ |
| 1198 | return create_error("Invalid target repository type requested."); |
| 1199 | } |
| 1200 | |
| 1201 | if (source == NULL && config == NULL) { |
| 1202 | reply = create_error("invalid input parameters - one of source and config is required."); |
| 1203 | } else { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1204 | 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] | 1205 | if (err_reply == NULL) { |
| 1206 | reply = create_error("Copying of configuration failed."); |
| 1207 | } else { |
| 1208 | /* use filled err_reply from libnetconf's callback */ |
| 1209 | reply = err_reply; |
| 1210 | } |
| 1211 | } else { |
| 1212 | reply = json_object_new_object(); |
| 1213 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1214 | } |
| 1215 | } |
| 1216 | return reply; |
| 1217 | } |
| 1218 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1219 | 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] | 1220 | { |
| 1221 | json_object *reply = NULL; |
| 1222 | const char *config = NULL; |
| 1223 | char *data = NULL; |
| 1224 | |
| 1225 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key); |
| 1226 | |
| 1227 | config = json_object_get_string(json_object_object_get(request, "content")); |
| 1228 | |
| 1229 | if (config == NULL) { |
| 1230 | return create_error("Missing content parameter."); |
| 1231 | } |
| 1232 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1233 | if (netconf_generic(server, session_key, config, &data) != EXIT_SUCCESS) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1234 | if (err_reply == NULL) { |
| 1235 | reply = create_error("Killing of session failed."); |
| 1236 | } else { |
| 1237 | /* use filled err_reply from libnetconf's callback */ |
| 1238 | reply = err_reply; |
| 1239 | } |
| 1240 | } else { |
| 1241 | if (data == NULL) { |
| 1242 | reply = json_object_new_object(); |
| 1243 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1244 | } else { |
| 1245 | return create_data(data); |
| 1246 | } |
| 1247 | } |
| 1248 | return reply; |
| 1249 | } |
| 1250 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1251 | 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] | 1252 | { |
| 1253 | json_object *reply = NULL; |
| 1254 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key); |
| 1255 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1256 | if (netconf_close(server, session_key) != EXIT_SUCCESS) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1257 | if (err_reply == NULL) { |
| 1258 | reply = create_error("Invalid session identifier."); |
| 1259 | } else { |
| 1260 | /* use filled err_reply from libnetconf's callback */ |
| 1261 | reply = err_reply; |
| 1262 | } |
| 1263 | } else { |
| 1264 | reply = json_object_new_object(); |
| 1265 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1266 | } |
| 1267 | return reply; |
| 1268 | } |
| 1269 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1270 | 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] | 1271 | { |
| 1272 | json_object *reply = NULL; |
| 1273 | const char *sid = NULL; |
| 1274 | |
| 1275 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key); |
| 1276 | |
| 1277 | sid = json_object_get_string(json_object_object_get(request, "session-id")); |
| 1278 | |
| 1279 | if (sid == NULL) { |
| 1280 | return create_error("Missing session-id parameter."); |
| 1281 | } |
| 1282 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1283 | if (netconf_killsession(server, session_key, sid) != EXIT_SUCCESS) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1284 | if (err_reply == NULL) { |
| 1285 | reply = create_error("Killing of session failed."); |
| 1286 | } else { |
| 1287 | /* use filled err_reply from libnetconf's callback */ |
| 1288 | reply = err_reply; |
| 1289 | } |
| 1290 | } else { |
| 1291 | reply = json_object_new_object(); |
| 1292 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1293 | } |
| 1294 | return reply; |
| 1295 | } |
| 1296 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1297 | 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] | 1298 | { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1299 | struct nc_session *temp_session = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1300 | struct session_with_mutex * locked_session = NULL; |
| 1301 | json_object *reply = NULL; |
| 1302 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1303 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1304 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 1305 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1309 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1310 | if ((locked_session != NULL) && (locked_session->hello_message != NULL)) { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1311 | pthread_mutex_lock(&locked_session->lock); |
| 1312 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1313 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1314 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1315 | 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] | 1316 | temp_session = nc_session_connect_channel(locked_session->session, NULL); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1317 | if (temp_session != NULL) { |
| 1318 | prepare_status_message(server, locked_session, temp_session); |
| 1319 | 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] | 1320 | nc_session_free(temp_session); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1321 | } else { |
| 1322 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment"); |
| 1323 | reply = create_error("Reload was unsuccessful, connection failed."); |
| 1324 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1325 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1326 | } else { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1327 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1328 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1329 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1330 | reply = create_error("Invalid session identifier."); |
| 1331 | } |
| 1332 | |
| 1333 | if ((reply == NULL) && (locked_session->hello_message != NULL)) { |
| 1334 | reply = locked_session->hello_message; |
| 1335 | } |
| 1336 | return reply; |
| 1337 | } |
| 1338 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1339 | 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] | 1340 | { |
| 1341 | json_object *reply = NULL; |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1342 | struct session_with_mutex * locked_session = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1343 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1344 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1345 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 1346 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1347 | } |
| 1348 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1349 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1350 | if (locked_session != NULL) { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1351 | pthread_mutex_lock(&locked_session->lock); |
| 1352 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1353 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1354 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1355 | if (locked_session->hello_message != NULL) { |
| 1356 | reply = locked_session->hello_message; |
| 1357 | } else { |
| 1358 | reply = create_error("Invalid session identifier."); |
| 1359 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1360 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1361 | } else { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1362 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1363 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1364 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1365 | reply = create_error("Invalid session identifier."); |
| 1366 | } |
| 1367 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1368 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1369 | return reply; |
| 1370 | } |
| 1371 | |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1372 | void notification_history(time_t eventtime, const char *content) |
| 1373 | { |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1374 | json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key); |
| 1375 | if (notif_history_array == NULL) { |
| 1376 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "No list of notification history found."); |
| 1377 | return; |
| 1378 | } |
| 1379 | 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] | 1380 | json_object *notif = json_object_new_object(); |
| 1381 | if (notif == NULL) { |
| 1382 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Could not allocate memory for notification (json)."); |
| 1383 | return; |
| 1384 | } |
| 1385 | json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime)); |
| 1386 | json_object_object_add(notif, "content", json_object_new_string(content)); |
| 1387 | json_object_array_add(notif_history_array, notif); |
| 1388 | } |
| 1389 | |
| 1390 | json_object *handle_op_ntfgethistory(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key) |
| 1391 | { |
| 1392 | json_object *reply = NULL; |
| 1393 | const char *sid = NULL; |
| 1394 | struct session_with_mutex *locked_session = NULL; |
| 1395 | struct nc_session *temp_session = NULL; |
| 1396 | nc_rpc *rpc = NULL; |
| 1397 | time_t start = 0; |
| 1398 | time_t stop = 0; |
| 1399 | int64_t from, to; |
| 1400 | |
| 1401 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get notification history, session %s", session_key); |
| 1402 | |
| 1403 | sid = json_object_get_string(json_object_object_get(request, "session")); |
| 1404 | from = json_object_get_int64(json_object_object_get(request, "from")); |
| 1405 | to = json_object_get_int64(json_object_object_get(request, "to")); |
| 1406 | |
| 1407 | start = time(NULL) + from; |
| 1408 | stop = time(NULL) + to; |
| 1409 | |
| 1410 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "notification history interval %li %li", (long int) from, (long int) to); |
| 1411 | |
| 1412 | if (sid == NULL) { |
| 1413 | return create_error("Missing session parameter."); |
| 1414 | } |
| 1415 | |
| 1416 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 1417 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1418 | return NULL; |
| 1419 | } |
| 1420 | |
| 1421 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1422 | if (locked_session != NULL) { |
| 1423 | pthread_mutex_lock(&locked_session->lock); |
| 1424 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1425 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1426 | } |
| 1427 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session."); |
| 1428 | temp_session = nc_session_connect_channel(locked_session->session, NULL); |
| 1429 | if (temp_session != NULL) { |
| 1430 | rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop); |
| 1431 | if (rpc == NULL) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1432 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1433 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "notifications: creating an rpc request failed."); |
| 1434 | return create_error("notifications: creating an rpc request failed."); |
| 1435 | } |
| 1436 | |
| 1437 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Send NC subscribe."); |
| 1438 | /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */ |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1439 | json_object *res = netconf_unlocked_op(server, temp_session, rpc); |
| 1440 | if (res != NULL) { |
| 1441 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1442 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Subscription RPC failed."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1443 | return res; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1444 | } |
| 1445 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
| 1446 | |
| 1447 | pthread_mutex_unlock(&locked_session->lock); |
| 1448 | pthread_mutex_lock(&ntf_history_lock); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1449 | json_object *notif_history_array = json_object_new_array(); |
| 1450 | if (pthread_setspecific(notif_history_key, notif_history_array) != 0) { |
| 1451 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notif_history: cannot set thread-specific hash value."); |
| 1452 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1453 | |
| 1454 | ncntf_dispatch_receive(temp_session, notification_history); |
| 1455 | |
| 1456 | reply = json_object_new_object(); |
| 1457 | json_object_object_add(reply, "notifications", notif_history_array); |
| 1458 | //json_object_put(notif_history_array); |
| 1459 | |
| 1460 | pthread_mutex_unlock(&ntf_history_lock); |
| 1461 | 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] | 1462 | nc_session_free(temp_session); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1463 | } else { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1464 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1465 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get history of notification failed due to channel establishment"); |
| 1466 | reply = create_error("Get history of notification was unsuccessful, connection failed."); |
| 1467 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1468 | } else { |
| 1469 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1470 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1471 | } |
| 1472 | reply = create_error("Invalid session identifier."); |
| 1473 | } |
| 1474 | |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1475 | return reply; |
| 1476 | } |
| 1477 | |
| 1478 | json_object *handle_op_validate(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key) |
| 1479 | { |
| 1480 | json_object *reply = NULL; |
| 1481 | const char *sid = NULL; |
| 1482 | const char *target = NULL; |
| 1483 | const char *url = NULL; |
| 1484 | struct session_with_mutex *locked_session = NULL; |
| 1485 | nc_rpc *rpc = NULL; |
| 1486 | NC_DATASTORE target_ds; |
| 1487 | |
| 1488 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: validate datastore, session %s", session_key); |
| 1489 | |
| 1490 | sid = json_object_get_string(json_object_object_get(request, "session")); |
| 1491 | target = json_object_get_string(json_object_object_get(request, "target")); |
| 1492 | url = json_object_get_string(json_object_object_get(request, "url")); |
| 1493 | |
| 1494 | |
| 1495 | if ((sid == NULL) || (target == NULL)) { |
| 1496 | return create_error("Missing session parameter."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1497 | } |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1498 | |
| 1499 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
| 1500 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1501 | return NULL; |
| 1502 | } |
| 1503 | |
| 1504 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1505 | if (locked_session != NULL) { |
| 1506 | pthread_mutex_lock(&locked_session->lock); |
| 1507 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 1508 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1509 | } |
| 1510 | /* validation */ |
| 1511 | target_ds = parse_datastore(target); |
| 1512 | if (target_ds == NC_DATASTORE_URL) { |
| 1513 | if (url != NULL) { |
| 1514 | rpc = nc_rpc_validate(target_ds, url); |
| 1515 | } |
| 1516 | } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP) |
| 1517 | || (target_ds == NC_DATASTORE_CANDIDATE)) { |
| 1518 | rpc = nc_rpc_validate(target_ds); |
| 1519 | } |
| 1520 | if (rpc == NULL) { |
| 1521 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 1522 | reply = create_error("Creation of RPC request failed."); |
| 1523 | pthread_mutex_unlock(&locked_session->lock); |
| 1524 | return reply; |
| 1525 | } |
| 1526 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1527 | if ((reply = netconf_op(server, session_key, rpc, NULL)) == NULL) { |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1528 | reply = json_object_new_object(); |
| 1529 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1530 | } |
| 1531 | nc_rpc_free (rpc); |
| 1532 | |
| 1533 | pthread_mutex_unlock(&locked_session->lock); |
| 1534 | } else { |
| 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 | reply = create_error("Invalid session identifier."); |
| 1539 | } |
| 1540 | |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1541 | return reply; |
| 1542 | } |
| 1543 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1544 | void * thread_routine (void * arg) |
| 1545 | { |
| 1546 | void * retval = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1547 | struct pollfd fds; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1548 | json_object *request = NULL; |
| 1549 | json_object *reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1550 | int operation; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1551 | int status = 0; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1552 | const char *msgtext; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1553 | const char *session_key; |
| 1554 | const char *target = NULL; |
| 1555 | const char *source = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1556 | const char *url = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1557 | NC_DATASTORE ds_type_t = -1; |
| 1558 | NC_DATASTORE ds_type_s = -1; |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1559 | char *chunked_out_msg = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1560 | apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1561 | server_rec * server = ((struct pass_to_thread*)arg)->server; |
| 1562 | int client = ((struct pass_to_thread*)arg)->client; |
| 1563 | |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1564 | char *buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1565 | |
| 1566 | while (!isterminated) { |
| 1567 | fds.fd = client; |
| 1568 | fds.events = POLLIN; |
| 1569 | fds.revents = 0; |
| 1570 | |
| 1571 | status = poll(&fds, 1, 1000); |
| 1572 | |
| 1573 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 1574 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
| 1575 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
| 1576 | continue; |
| 1577 | } else if (status < 0) { |
| 1578 | /* 0: poll time outed |
| 1579 | * close socket and ignore this request from the client, it can try it again |
| 1580 | * -1: poll failed |
| 1581 | * something wrong happend, close this socket and wait for another request |
| 1582 | */ |
| 1583 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno)); |
| 1584 | close(client); |
| 1585 | break; |
| 1586 | } |
| 1587 | /* status > 0 */ |
| 1588 | |
| 1589 | /* check the status of the socket */ |
| 1590 | |
| 1591 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 1592 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 1593 | /* close client's socket (it's probably already closed by client */ |
| 1594 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents); |
| 1595 | close(client); |
| 1596 | break; |
| 1597 | } |
| 1598 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1599 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get framed message..."); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1600 | buffer = get_framed_message(server, client); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1601 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1602 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Check read buffer."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1603 | if (buffer != NULL) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1604 | enum json_tokener_error jerr; |
| 1605 | request = json_tokener_parse_verbose(buffer, &jerr); |
| 1606 | if (jerr != json_tokener_success) { |
| 1607 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error"); |
| 1608 | continue; |
| 1609 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1610 | operation = json_object_get_int(json_object_object_get(request, "type")); |
| 1611 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1612 | session_key = json_object_get_string(json_object_object_get(request, "session")); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1613 | 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] | 1614 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 1615 | if (operation != MSG_CONNECT && session_key == NULL) { |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1616 | reply = create_error("Missing session specification."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1617 | msgtext = json_object_to_json_string(reply); |
| 1618 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 1619 | json_object_put(reply); |
| 1620 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 1621 | close(client); |
| 1622 | break; |
| 1623 | } |
| 1624 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1625 | /* null global JSON error-reply */ |
| 1626 | err_reply = NULL; |
| 1627 | |
| 1628 | /* prepare reply envelope */ |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1629 | reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1630 | |
| 1631 | /* process required operation */ |
| 1632 | switch (operation) { |
| 1633 | case MSG_CONNECT: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1634 | reply = handle_op_connect(server, pool, request); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1635 | break; |
| 1636 | case MSG_GET: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1637 | reply = handle_op_get(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1638 | break; |
| 1639 | case MSG_GETCONFIG: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1640 | reply = handle_op_getconfig(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1641 | break; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1642 | case MSG_GETSCHEMA: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1643 | reply = handle_op_getschema(server, pool, request, session_key); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1644 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1645 | case MSG_EDITCONFIG: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1646 | reply = handle_op_editconfig(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1647 | break; |
| 1648 | case MSG_COPYCONFIG: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1649 | reply = handle_op_copyconfig(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1650 | break; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1651 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1652 | case MSG_DELETECONFIG: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1653 | case MSG_LOCK: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1654 | case MSG_UNLOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1655 | /* get parameters */ |
| 1656 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1657 | ds_type_t = parse_datastore(target); |
| 1658 | } |
| 1659 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1660 | ds_type_s = parse_datastore(source); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | if (ds_type_t == -1) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1664 | reply = create_error("Invalid target repository type requested."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1665 | break; |
| 1666 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1667 | switch(operation) { |
| 1668 | case MSG_DELETECONFIG: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1669 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1670 | url = json_object_get_string(json_object_object_get(request, "url")); |
| 1671 | reply = netconf_deleteconfig(server, session_key, ds_type_t, url); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1672 | break; |
| 1673 | case MSG_LOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1674 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1675 | reply = netconf_lock(server, session_key, ds_type_t); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1676 | break; |
| 1677 | case MSG_UNLOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1678 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1679 | reply = netconf_unlock(server, session_key, ds_type_t); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1680 | break; |
| 1681 | default: |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1682 | reply = create_error("Internal: Unknown request type."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1683 | break; |
| 1684 | } |
| 1685 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1686 | if (reply == NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1687 | if (err_reply == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1688 | /** \todo more clever error message wanted */ |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame^] | 1689 | reply = json_object_new_object(); |
| 1690 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1691 | } else { |
| 1692 | /* use filled err_reply from libnetconf's callback */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1693 | reply = err_reply; |
| 1694 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1695 | } |
| 1696 | break; |
| 1697 | case MSG_KILL: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1698 | reply = handle_op_kill(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1699 | break; |
| 1700 | case MSG_DISCONNECT: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1701 | reply = handle_op_disconnect(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1702 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1703 | case MSG_RELOADHELLO: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1704 | reply = handle_op_reloadhello(server, pool, request, session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1705 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1706 | case MSG_INFO: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1707 | reply = handle_op_info(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1708 | break; |
| 1709 | case MSG_GENERIC: |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1710 | reply = handle_op_generic(server, pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1711 | break; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1712 | case MSG_NTF_GETHISTORY: |
| 1713 | reply = handle_op_ntfgethistory(server, pool, request, session_key); |
| 1714 | break; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1715 | case MSG_VALIDATE: |
| 1716 | reply = handle_op_validate(server, pool, request, session_key); |
| 1717 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1718 | default: |
| 1719 | 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] | 1720 | reply = create_error("Operation not supported."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1721 | break; |
| 1722 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1723 | 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] | 1724 | json_object_put(request); |
| 1725 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1726 | 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] | 1727 | /* send reply to caller */ |
| 1728 | if (reply != NULL) { |
| 1729 | msgtext = json_object_to_json_string(reply); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1730 | 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] | 1731 | if (buffer != NULL) { |
| 1732 | free(buffer); |
| 1733 | buffer = NULL; |
| 1734 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1735 | break; |
| 1736 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1737 | 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] | 1738 | send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1739 | 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] | 1740 | json_object_put(reply); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1741 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean message buffer."); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1742 | free(chunked_out_msg); |
| 1743 | chunked_out_msg = NULL; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1744 | if (buffer != NULL) { |
| 1745 | free(buffer); |
| 1746 | buffer = NULL; |
| 1747 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1748 | } else { |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1749 | 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] | 1750 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1754 | free (arg); |
| 1755 | |
| 1756 | return retval; |
| 1757 | } |
| 1758 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1759 | /** |
| 1760 | * \brief Close all open NETCONF sessions. |
| 1761 | * |
| 1762 | * During termination of mod_netconf, it is useful to close all remaining |
| 1763 | * sessions. This function iterates over the list of sessions and close them |
| 1764 | * all. |
| 1765 | * |
| 1766 | * \param[in] server pointer to server_rec for logging |
| 1767 | * \param[in] p apr pool needed for hash table iterating |
| 1768 | * \param[in] ht hash table of session_with_mutex structs |
| 1769 | */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1770 | 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] | 1771 | { |
| 1772 | apr_hash_index_t *hi; |
| 1773 | void *val = NULL; |
| 1774 | struct session_with_mutex *swm = NULL; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1775 | const char *hashed_key = NULL; |
| 1776 | apr_ssize_t hashed_key_length; |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1777 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1778 | |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1779 | /* get exclusive access to sessions_list (conns) */ |
| 1780 | if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) { |
| 1781 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
| 1782 | return; |
| 1783 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1784 | 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] | 1785 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1786 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key); |
| 1787 | swm = (struct session_with_mutex *) val; |
| 1788 | if (swm != NULL) { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1789 | 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] | 1790 | pthread_mutex_unlock(&swm->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* close_and_free_session handles locking on its own */ |
| 1793 | close_and_free_session(server, swm); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1794 | } |
| 1795 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1796 | /* get exclusive access to sessions_list (conns) */ |
| 1797 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 1798 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1799 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1802 | 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] | 1803 | { |
| 1804 | apr_hash_index_t *hi; |
| 1805 | void *val = NULL; |
| 1806 | struct nc_session *ns = NULL; |
| 1807 | struct session_with_mutex *swm = NULL; |
| 1808 | const char *hashed_key = NULL; |
| 1809 | apr_ssize_t hashed_key_length; |
| 1810 | apr_time_t current_time = apr_time_now(); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1811 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1812 | |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1813 | /* get exclusive access to sessions_list (conns) */ |
| 1814 | if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) { |
| 1815 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
| 1816 | return; |
| 1817 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1818 | 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] | 1819 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1820 | swm = (struct session_with_mutex *) val; |
| 1821 | if (swm == NULL) { |
| 1822 | continue; |
| 1823 | } |
| 1824 | ns = swm->session; |
| 1825 | if (ns == NULL) { |
| 1826 | continue; |
| 1827 | } |
| 1828 | pthread_mutex_lock(&swm->lock); |
| 1829 | if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) { |
| 1830 | 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] | 1831 | /* remove session from the active sessions list */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1832 | apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL); |
| 1833 | pthread_mutex_unlock(&swm->lock); |
| 1834 | |
| 1835 | /* close_and_free_session handles locking on its own */ |
| 1836 | close_and_free_session(server, swm); |
| 1837 | } else { |
| 1838 | pthread_mutex_unlock(&swm->lock); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1839 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1840 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1841 | /* get exclusive access to sessions_list (conns) */ |
| 1842 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 1843 | ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 1844 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | /** |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1849 | * This is actually implementation of NETCONF client |
| 1850 | * - requests are received from UNIX socket in the predefined format |
| 1851 | * - results are replied through the same way |
| 1852 | * - the daemon run as a separate process, but it is started and stopped |
| 1853 | * automatically by Apache. |
| 1854 | * |
| 1855 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1856 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 1857 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1858 | struct timeval tv; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1859 | struct sockaddr_un local, remote; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1860 | int lsock, client, ret, i, pthread_count = 0; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1861 | unsigned int olds = 0, timediff = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1862 | socklen_t len; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1863 | mod_netconf_cfg *cfg; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1864 | struct pass_to_thread * arg; |
| 1865 | pthread_t * ptids = calloc (1,sizeof(pthread_t)); |
| 1866 | struct timespec maxtime; |
| 1867 | pthread_rwlockattr_t lock_attrs; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1868 | #ifdef WITH_NOTIFICATIONS |
| 1869 | char use_notifications = 0; |
| 1870 | #endif |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1871 | |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1872 | http_server = server; |
| 1873 | |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1874 | /* wait at most 5 seconds for every thread to terminate */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1875 | maxtime.tv_sec = 5; |
| 1876 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1877 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1878 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1879 | unixd_setup_child(); |
| 1880 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1881 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 1882 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1883 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 1884 | return; |
| 1885 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1886 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1887 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1888 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 1889 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1894 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1895 | unlink(local.sun_path); |
| 1896 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 1897 | |
| 1898 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 1899 | if (errno == EADDRINUSE) { |
| 1900 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 1901 | close(lsock); |
| 1902 | exit(0); |
| 1903 | } |
| 1904 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 1905 | close(lsock); |
| 1906 | return; |
| 1907 | } |
| 1908 | |
| 1909 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 1910 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 1911 | close(lsock); |
| 1912 | return; |
| 1913 | } |
| 1914 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 1915 | /* prepare internal lists */ |
| 1916 | netconf_sessions_list = apr_hash_make(pool); |
| 1917 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1918 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1919 | if (notification_init(pool, server) == -1) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1920 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed"); |
| 1921 | use_notifications = 0; |
| 1922 | } else { |
| 1923 | use_notifications = 1; |
| 1924 | } |
| 1925 | #endif |
| 1926 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1927 | /* setup libnetconf's callbacks */ |
| 1928 | nc_verbosity(NC_VERB_DEBUG); |
| 1929 | clb_print_server = server; |
| 1930 | nc_callback_print(clb_print); |
| 1931 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 1932 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 1933 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 1934 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1935 | |
| 1936 | /* disable publickey authentication */ |
| 1937 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 1938 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1939 | /* create mutex protecting session list */ |
| 1940 | pthread_rwlockattr_init(&lock_attrs); |
| 1941 | /* rwlock is shared only with threads in this process */ |
| 1942 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 1943 | /* create rw lock */ |
| 1944 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
| 1945 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
| 1946 | close (lsock); |
| 1947 | return; |
| 1948 | } |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1949 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "init of notif_history_key."); |
| 1950 | if (pthread_key_create(¬if_history_key, NULL) != 0) { |
| 1951 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "init of notif_history_key failed"); |
| 1952 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 1953 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1954 | fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1955 | while (isterminated == 0) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1956 | gettimeofday(&tv, NULL); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1957 | timediff = (unsigned int)tv.tv_sec - olds; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1958 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1959 | if (timediff > 60) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1960 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications"); |
| 1961 | } |
| 1962 | if (use_notifications == 1) { |
| 1963 | notification_handle(); |
| 1964 | } |
| 1965 | #endif |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1966 | if (timediff > ACTIVITY_CHECK_INTERVAL) { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1967 | check_timeout_and_close(server, pool); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1968 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1969 | |
| 1970 | /* open incoming connection if any */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1971 | len = sizeof(remote); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1972 | if (((unsigned int)tv.tv_sec - olds) > 60) { |
| 1973 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client"); |
| 1974 | olds = tv.tv_sec; |
| 1975 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1976 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1977 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 1978 | apr_sleep(SLEEP_TIME); |
| 1979 | continue; |
| 1980 | } else if (client == -1 && (errno == EINTR)) { |
| 1981 | continue; |
| 1982 | } else if (client == -1) { |
| 1983 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 1984 | continue; |
| 1985 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1986 | |
| 1987 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1988 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1989 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1990 | arg = malloc (sizeof(struct pass_to_thread)); |
| 1991 | arg->client = client; |
| 1992 | arg->pool = pool; |
| 1993 | arg->server = server; |
| 1994 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1995 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1996 | /* start new thread. It will serve this particular request and then terminate */ |
| 1997 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) { |
| 1998 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret); |
| 1999 | } else { |
| 2000 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]); |
| 2001 | pthread_count++; |
| 2002 | ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1)); |
| 2003 | ptids[pthread_count] = 0; |
| 2004 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2005 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2006 | /* check if some thread already terminated, free some resources by joining it */ |
| 2007 | for (i=0; i<pthread_count; i++) { |
| 2008 | if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) { |
| 2009 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg); |
| 2010 | pthread_count--; |
| 2011 | if (pthread_count > 0) { |
| 2012 | /* place last Thread ID on the place of joined one */ |
| 2013 | ptids[i] = ptids[pthread_count]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2014 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2015 | } |
| 2016 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2017 | 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] | 2018 | } |
| 2019 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2020 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating..."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2021 | /* join all threads */ |
| 2022 | for (i=0; i<pthread_count; i++) { |
| 2023 | pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime); |
| 2024 | } |
| 2025 | free (ptids); |
| 2026 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2027 | close(lsock); |
| 2028 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2029 | #ifdef WITH_NOTIFICATIONS |
| 2030 | notification_close(); |
| 2031 | #endif |
| 2032 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2033 | /* close all NETCONF sessions */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2034 | close_all_nc_sessions(server, pool); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2035 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2036 | /* destroy rwlock */ |
| 2037 | pthread_rwlock_destroy(&session_lock); |
| 2038 | pthread_rwlockattr_destroy(&lock_attrs); |
| 2039 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2040 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 2041 | |
| 2042 | exit(APR_SUCCESS); |
| 2043 | } |
| 2044 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2045 | 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] | 2046 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2047 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 2048 | |
| 2049 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 2050 | apr_pool_create(&config->pool, pool); |
| 2051 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2052 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2053 | |
| 2054 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 2058 | apr_pool_t * plog, server_rec * s) |
| 2059 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2060 | mod_netconf_cfg *config; |
| 2061 | apr_status_t res; |
| 2062 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2063 | /* These two help ensure that we only init once. */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 2064 | void *data = NULL; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2065 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2066 | |
| 2067 | /* |
| 2068 | * The following checks if this routine has been called before. |
| 2069 | * This is necessary because the parent process gets initialized |
| 2070 | * a couple of times as the server starts up. |
| 2071 | */ |
| 2072 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 2073 | if (!data) { |
| 2074 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 2075 | return (OK); |
| 2076 | } |
| 2077 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2078 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 2079 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 2080 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2081 | if (config && config->forkproc == NULL) { |
| 2082 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 2083 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2084 | switch (res) { |
| 2085 | case APR_INCHILD: |
| 2086 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2087 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2088 | apr_signal(SIGTERM, signal_handler); |
| 2089 | |
| 2090 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2091 | 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] | 2092 | |
| 2093 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2094 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2095 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2096 | /* I never should be here, wtf?!? */ |
| 2097 | 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] | 2098 | exit(APR_EGENERAL); |
| 2099 | break; |
| 2100 | case APR_INPARENT: |
| 2101 | /* 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] | 2102 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2103 | break; |
| 2104 | default: |
| 2105 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 2106 | break; |
| 2107 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2108 | } else { |
| 2109 | 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] | 2110 | } |
| 2111 | |
| 2112 | return OK; |
| 2113 | } |
| 2114 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2115 | /** |
| 2116 | * Register module hooks |
| 2117 | */ |
| 2118 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 2119 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2120 | 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] | 2121 | } |
| 2122 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2123 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 2124 | { |
| 2125 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | |
| 2129 | static const command_rec netconf_cmds[] = { |
| 2130 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 2131 | {NULL} |
| 2132 | }; |
| 2133 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2134 | /* Dispatch list for API hooks */ |
| 2135 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 2136 | STANDARD20_MODULE_STUFF, |
| 2137 | NULL, /* create per-dir config structures */ |
| 2138 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2139 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2140 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2141 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2142 | mod_netconf_register_hooks /* register hooks */ |
| 2143 | }; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 2144 | |