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