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