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; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 304 | pthread_mutex_init (&locked_session->lock, NULL); |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 305 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 306 | /* get exclusive access to sessions_list (conns) */ |
| 307 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
| 308 | nc_session_free(session); |
| 309 | free (locked_session); |
| 310 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 311 | return NULL; |
| 312 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 313 | 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] | 314 | locked_session->ntfc_subscribed = 0; |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 315 | 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] | 316 | 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] | 317 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock"); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 318 | |
| 319 | /* store information about session from hello message for future usage */ |
Tomas Cejka | a72dfdb | 2013-05-15 13:26:21 +0200 | [diff] [blame] | 320 | prepare_status_message(server, locked_session, session); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 321 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 322 | /* end of critical section */ |
| 323 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 324 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 325 | return NULL; |
| 326 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 327 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 328 | return (session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 329 | } else { |
| 330 | 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] | 331 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 336 | 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] | 337 | { |
| 338 | struct nc_session *ns = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 339 | struct session_with_mutex * locked_session; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 340 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 341 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 342 | /* get exclusive (write) access to sessions_list (conns) */ |
| 343 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
| 344 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 345 | return EXIT_FAILURE; |
| 346 | } |
| 347 | locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 348 | if (locked_session != NULL) { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 349 | /** \todo free all notifications from queue */ |
| 350 | apr_array_clear(locked_session->notifications); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 351 | pthread_mutex_destroy(&locked_session->lock); |
| 352 | ns = locked_session->session; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 353 | if (locked_session->hello_message != NULL) { |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 354 | json_object_put(locked_session->hello_message); |
| 355 | locked_session->hello_message = NULL; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 356 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 357 | free (locked_session); |
| 358 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 359 | if (ns != NULL) { |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 360 | nc_session_close (ns, NC_SESSION_TERM_CLOSED); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 361 | nc_session_free (ns); |
| 362 | ns = NULL; |
| 363 | |
| 364 | /* remove session from the active sessions list */ |
| 365 | apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 366 | /* end of critical section */ |
| 367 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 368 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 369 | return EXIT_FAILURE; |
| 370 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 371 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed"); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 372 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 373 | return (EXIT_SUCCESS); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 374 | } else { |
Tomas Cejka | 6c4609b | 2012-10-12 22:29:47 +0200 | [diff] [blame] | 375 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 376 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 377 | return EXIT_FAILURE; |
| 378 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 379 | 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] | 380 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 384 | 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] | 385 | { |
| 386 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 387 | struct session_with_mutex * locked_session; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 388 | nc_reply* reply; |
| 389 | int retval = EXIT_SUCCESS; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 390 | NC_MSG_TYPE msgt; |
| 391 | NC_REPLY_TYPE replyt; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 392 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 393 | /* check requests */ |
| 394 | if (rpc == NULL) { |
| 395 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
| 396 | return (EXIT_FAILURE); |
| 397 | } |
| 398 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 399 | /* get non-exclusive (read) access to sessions_list (conns) */ |
| 400 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
| 401 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 402 | return EXIT_FAILURE; |
| 403 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 404 | /* get session where send the RPC */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 405 | locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 406 | if (locked_session != NULL) { |
| 407 | session = locked_session->session; |
| 408 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 409 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 410 | /* get exclusive access to session */ |
| 411 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 412 | /* unlock before returning error */ |
| 413 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 414 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 415 | return EXIT_FAILURE; |
| 416 | } |
| 417 | return EXIT_FAILURE; |
| 418 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 419 | locked_session->last_activity = apr_time_now(); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 420 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 421 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 422 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 423 | /* first release exclusive lock for this session */ |
| 424 | pthread_mutex_unlock(&locked_session->lock); |
| 425 | /* end of critical section */ |
| 426 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 427 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 428 | return EXIT_FAILURE; |
| 429 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 430 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 431 | /* process the result of the operation */ |
| 432 | switch (msgt) { |
| 433 | case NC_MSG_UNKNOWN: |
| 434 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 435 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 436 | netconf_close(server, conns, session_key); |
| 437 | return (EXIT_FAILURE); |
| 438 | } |
| 439 | /* no break */ |
| 440 | case NC_MSG_NONE: |
| 441 | /* there is error handled by callback */ |
| 442 | return (EXIT_FAILURE); |
| 443 | break; |
| 444 | case NC_MSG_REPLY: |
| 445 | switch (replyt = nc_reply_get_type(reply)) { |
| 446 | case NC_REPLY_OK: |
| 447 | retval = EXIT_SUCCESS; |
| 448 | break; |
| 449 | default: |
| 450 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 451 | retval = EXIT_FAILURE; |
| 452 | break; |
| 453 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 454 | break; |
| 455 | default: |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 456 | 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] | 457 | retval = EXIT_FAILURE; |
| 458 | break; |
| 459 | } |
| 460 | nc_reply_free(reply); |
| 461 | return (retval); |
| 462 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 463 | /* release lock on failure */ |
| 464 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 465 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 466 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 467 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 468 | return (EXIT_FAILURE); |
| 469 | } |
| 470 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 471 | |
| 472 | 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] | 473 | { |
| 474 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 475 | struct session_with_mutex * locked_session; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 476 | nc_reply* reply = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 477 | char* data; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 478 | NC_MSG_TYPE msgt; |
| 479 | NC_REPLY_TYPE replyt; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 480 | |
| 481 | /* check requests */ |
| 482 | if (rpc == NULL) { |
| 483 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
| 484 | return (NULL); |
| 485 | } |
| 486 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 487 | /* get non-exclusive (read) access to sessions_list (conns) */ |
| 488 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
| 489 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 490 | return NULL; |
| 491 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 492 | /* get session where send the RPC */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 493 | locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 494 | if (locked_session != NULL) { |
| 495 | session = locked_session->session; |
| 496 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 497 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 498 | /* get exclusive access to session */ |
| 499 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 500 | /* unlock before returning error */ |
| 501 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 502 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 503 | return NULL; |
| 504 | } |
| 505 | return NULL; |
| 506 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 507 | locked_session->last_activity = apr_time_now(); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 508 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 509 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 510 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 511 | /* first release exclusive lock for this session */ |
| 512 | pthread_mutex_unlock(&locked_session->lock); |
| 513 | /* end of critical section */ |
| 514 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 515 | 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] | 516 | return (NULL); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 517 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 518 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 519 | /* process the result of the operation */ |
| 520 | switch (msgt) { |
| 521 | case NC_MSG_UNKNOWN: |
| 522 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 523 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 524 | netconf_close(server, conns, session_key); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 525 | return (NULL); |
| 526 | } |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 527 | /* no break */ |
| 528 | case NC_MSG_NONE: |
| 529 | /* there is error handled by callback */ |
| 530 | return (NULL); |
| 531 | break; |
| 532 | case NC_MSG_REPLY: |
| 533 | switch (replyt = nc_reply_get_type(reply)) { |
| 534 | case NC_REPLY_DATA: |
| 535 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 536 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 537 | data = NULL; |
| 538 | } |
| 539 | break; |
| 540 | default: |
| 541 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 542 | data = NULL; |
| 543 | break; |
| 544 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 545 | break; |
| 546 | default: |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 547 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt); |
| 548 | data = NULL; |
| 549 | break; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 550 | } |
| 551 | nc_reply_free(reply); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 552 | return (data); |
| 553 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 554 | /* release lock on failure */ |
| 555 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 556 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 557 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 558 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 559 | return (NULL); |
| 560 | } |
| 561 | } |
| 562 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 563 | 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] | 564 | { |
| 565 | nc_rpc* rpc; |
| 566 | struct nc_filter *f = NULL; |
| 567 | char* data = NULL; |
| 568 | |
| 569 | /* create filter if set */ |
| 570 | if (filter != NULL) { |
| 571 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 572 | } |
| 573 | |
| 574 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 575 | rpc = nc_rpc_getconfig (source, f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 576 | nc_filter_free(f); |
| 577 | if (rpc == NULL) { |
| 578 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 579 | return (NULL); |
| 580 | } |
| 581 | |
| 582 | data = netconf_opdata(server, conns, session_key, rpc); |
| 583 | nc_rpc_free (rpc); |
| 584 | return (data); |
| 585 | } |
| 586 | |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 587 | static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format) |
| 588 | { |
| 589 | nc_rpc* rpc; |
| 590 | char* data = NULL; |
| 591 | |
| 592 | /* create requests */ |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 593 | rpc = nc_rpc_getschema(identifier, version, format); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 594 | if (rpc == NULL) { |
| 595 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 596 | return (NULL); |
| 597 | } |
| 598 | |
| 599 | data = netconf_opdata(server, conns, session_key, rpc); |
| 600 | nc_rpc_free (rpc); |
| 601 | return (data); |
| 602 | } |
| 603 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 604 | 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] | 605 | { |
| 606 | nc_rpc* rpc; |
| 607 | struct nc_filter *f = NULL; |
| 608 | char* data = NULL; |
| 609 | |
| 610 | /* create filter if set */ |
| 611 | if (filter != NULL) { |
| 612 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 613 | } |
| 614 | |
| 615 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 616 | rpc = nc_rpc_get (f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 617 | nc_filter_free(f); |
| 618 | if (rpc == NULL) { |
| 619 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 620 | return (NULL); |
| 621 | } |
| 622 | |
| 623 | data = netconf_opdata(server, conns, session_key, rpc); |
| 624 | nc_rpc_free (rpc); |
| 625 | return (data); |
| 626 | } |
| 627 | |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 628 | 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] | 629 | { |
| 630 | nc_rpc* rpc; |
| 631 | int retval = EXIT_SUCCESS; |
| 632 | |
| 633 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 634 | if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) { |
| 635 | if (target == NC_DATASTORE_URL) { |
| 636 | rpc = nc_rpc_copyconfig(source, target, config, url); |
| 637 | } else { |
| 638 | rpc = nc_rpc_copyconfig(source, target, config); |
| 639 | } |
| 640 | } else { |
| 641 | if (target == NC_DATASTORE_URL) { |
| 642 | rpc = nc_rpc_copyconfig(source, target, url); |
| 643 | } else { |
| 644 | rpc = nc_rpc_copyconfig(source, target); |
| 645 | } |
| 646 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 647 | if (rpc == NULL) { |
| 648 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 649 | return (EXIT_FAILURE); |
| 650 | } |
| 651 | |
| 652 | retval = netconf_op(server, conns, session_key, rpc); |
| 653 | nc_rpc_free (rpc); |
| 654 | return (retval); |
| 655 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 656 | |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 657 | 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] | 658 | { |
| 659 | nc_rpc* rpc; |
| 660 | int retval = EXIT_SUCCESS; |
| 661 | |
| 662 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 663 | /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */ |
| 664 | rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 665 | if (rpc == NULL) { |
| 666 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 667 | return (EXIT_FAILURE); |
| 668 | } |
| 669 | |
| 670 | retval = netconf_op(server, conns, session_key, rpc); |
| 671 | nc_rpc_free (rpc); |
| 672 | return (retval); |
| 673 | } |
| 674 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 675 | 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] | 676 | { |
| 677 | nc_rpc* rpc; |
| 678 | int retval = EXIT_SUCCESS; |
| 679 | |
| 680 | /* create requests */ |
| 681 | rpc = nc_rpc_killsession(sid); |
| 682 | if (rpc == NULL) { |
| 683 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 684 | return (EXIT_FAILURE); |
| 685 | } |
| 686 | |
| 687 | retval = netconf_op(server, conns, session_key, rpc); |
| 688 | nc_rpc_free (rpc); |
| 689 | return (retval); |
| 690 | } |
| 691 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 692 | 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] | 693 | { |
| 694 | nc_rpc* rpc; |
| 695 | int retval = EXIT_SUCCESS; |
| 696 | |
| 697 | /* create requests */ |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 698 | rpc = op_func(target); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 699 | if (rpc == NULL) { |
| 700 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 701 | return (EXIT_FAILURE); |
| 702 | } |
| 703 | |
| 704 | retval = netconf_op(server, conns, session_key, rpc); |
| 705 | nc_rpc_free (rpc); |
| 706 | return (retval); |
| 707 | } |
| 708 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 709 | 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] | 710 | { |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 711 | nc_rpc *rpc = NULL; |
| 712 | if (target != NC_DATASTORE_URL) { |
| 713 | rpc = nc_rpc_deleteconfig(target); |
| 714 | } else { |
| 715 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 716 | /* rpc = nc_rpc_deleteconfig(target, const char *url); */ |
| 717 | return (EXIT_FAILURE); |
| 718 | } |
| 719 | |
| 720 | return netconf_op(server, conns, session_key, rpc); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 723 | 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] | 724 | { |
| 725 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock)); |
| 726 | } |
| 727 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 728 | 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] | 729 | { |
| 730 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock)); |
| 731 | } |
| 732 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 733 | /** |
| 734 | * @return REPLY_OK: 0, *data == NULL |
| 735 | * REPLY_DATA: 0, *data != NULL |
| 736 | * REPLY_ERROR: 1, *data == NULL |
| 737 | */ |
| 738 | static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data) |
| 739 | { |
| 740 | struct nc_session *session = NULL; |
| 741 | nc_reply* reply; |
| 742 | nc_rpc* rpc; |
| 743 | int retval = EXIT_SUCCESS; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 744 | NC_MSG_TYPE msgt; |
| 745 | NC_REPLY_TYPE replyt; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 746 | |
| 747 | /* create requests */ |
| 748 | rpc = nc_rpc_generic(content); |
| 749 | if (rpc == NULL) { |
| 750 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 751 | return (EXIT_FAILURE); |
| 752 | } |
| 753 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 754 | if (data != NULL) { |
| 755 | *data = NULL; |
| 756 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 757 | |
| 758 | /* get session where send the RPC */ |
| 759 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 760 | if (session != NULL) { |
| 761 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 762 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 763 | nc_rpc_free (rpc); |
| 764 | |
| 765 | /* process the result of the operation */ |
| 766 | switch (msgt) { |
| 767 | case NC_MSG_UNKNOWN: |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 768 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 769 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 770 | netconf_close(server, conns, session_key); |
| 771 | return (EXIT_FAILURE); |
| 772 | } |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 773 | /* no break */ |
| 774 | case NC_MSG_NONE: |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 775 | /* there is error handled by callback */ |
| 776 | return (EXIT_FAILURE); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 777 | break; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 778 | case NC_MSG_REPLY: |
| 779 | switch (replyt = nc_reply_get_type(reply)) { |
| 780 | case NC_REPLY_DATA: |
| 781 | if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) { |
| 782 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 783 | nc_reply_free(reply); |
| 784 | return (EXIT_FAILURE); |
| 785 | } |
| 786 | retval = EXIT_SUCCESS; |
| 787 | break; |
| 788 | case NC_REPLY_OK: |
| 789 | retval = EXIT_SUCCESS; |
| 790 | break; |
| 791 | default: |
| 792 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 793 | retval = EXIT_FAILURE; |
| 794 | break; |
| 795 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 796 | break; |
| 797 | default: |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 798 | 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] | 799 | retval = EXIT_FAILURE; |
| 800 | break; |
| 801 | } |
| 802 | nc_reply_free(reply); |
| 803 | |
| 804 | return (retval); |
| 805 | } else { |
| 806 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 807 | return (EXIT_FAILURE); |
| 808 | } |
| 809 | } |
| 810 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 811 | /** |
| 812 | * @} |
| 813 | *//* netconf_operations */ |
| 814 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 815 | server_rec* clb_print_server; |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 816 | void clb_print(NC_VERB_LEVEL level, const char* msg) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 817 | { |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 818 | switch (level) { |
| 819 | case NC_VERB_ERROR: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 820 | 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] | 821 | break; |
| 822 | case NC_VERB_WARNING: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 823 | 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] | 824 | break; |
| 825 | case NC_VERB_VERBOSE: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 826 | 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] | 827 | break; |
| 828 | case NC_VERB_DEBUG: |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 829 | 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] | 830 | break; |
| 831 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 832 | } |
| 833 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 834 | void * thread_routine (void * arg) |
| 835 | { |
| 836 | void * retval = NULL; |
| 837 | |
| 838 | ssize_t buffer_len; |
| 839 | struct pollfd fds; |
| 840 | int status, buffer_size, ret; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 841 | json_object *request, *reply, *capabilities; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 842 | int operation; |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 843 | int i, chunk_len, len = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 844 | char* session_key, *data; |
| 845 | const char *host, *port, *user, *pass; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 846 | const char *msgtext; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 847 | const char *target, *source, *filter, *config, *defop, *erropt, *sid; |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 848 | const char *identifier, *version, *format; |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 849 | struct nc_cpblts* cpblts = NULL; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 850 | struct session_with_mutex * locked_session; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 851 | NC_DATASTORE ds_type_s, ds_type_t; |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 852 | NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 853 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
| 854 | |
| 855 | apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool; |
| 856 | apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list; |
| 857 | server_rec * server = ((struct pass_to_thread*)arg)->server; |
| 858 | int client = ((struct pass_to_thread*)arg)->client; |
| 859 | |
| 860 | char * buffer, chunk_len_str[12], *chunked_msg; |
| 861 | char c; |
| 862 | |
| 863 | while (!isterminated) { |
| 864 | fds.fd = client; |
| 865 | fds.events = POLLIN; |
| 866 | fds.revents = 0; |
| 867 | |
| 868 | status = poll(&fds, 1, 1000); |
| 869 | |
| 870 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 871 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
| 872 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
| 873 | continue; |
| 874 | } else if (status < 0) { |
| 875 | /* 0: poll time outed |
| 876 | * close socket and ignore this request from the client, it can try it again |
| 877 | * -1: poll failed |
| 878 | * something wrong happend, close this socket and wait for another request |
| 879 | */ |
| 880 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno)); |
| 881 | close(client); |
| 882 | break; |
| 883 | } |
| 884 | /* status > 0 */ |
| 885 | |
| 886 | /* check the status of the socket */ |
| 887 | |
| 888 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 889 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 890 | /* close client's socket (it's probably already closed by client */ |
| 891 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents); |
| 892 | close(client); |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | /* read json in chunked framing */ |
| 897 | buffer_size = 0; |
| 898 | buffer_len = 0; |
| 899 | buffer = NULL; |
| 900 | while (1) { |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 901 | /* read chunk length */ |
| 902 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 903 | free (buffer); |
| 904 | buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 905 | break; |
| 906 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 907 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 908 | free (buffer); |
| 909 | buffer = NULL; |
| 910 | break; |
| 911 | } |
| 912 | i=0; |
| 913 | memset (chunk_len_str, 0, 12); |
| 914 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 915 | if (i==0 && c == '#') { |
| 916 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 917 | /* end but invalid */ |
| 918 | free (buffer); |
| 919 | buffer = NULL; |
| 920 | } |
| 921 | /* end of message, double-loop break */ |
| 922 | goto msg_complete; |
| 923 | } |
| 924 | chunk_len_str[i++] = c; |
| 925 | } |
| 926 | if (c != '\n') { |
| 927 | free (buffer); |
| 928 | buffer = NULL; |
| 929 | break; |
| 930 | } |
| 931 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 932 | free (buffer); |
| 933 | buffer = NULL; |
| 934 | break; |
| 935 | } |
| 936 | buffer_size += chunk_len+1; |
| 937 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 938 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 939 | free (buffer); |
| 940 | buffer = NULL; |
| 941 | break; |
| 942 | } |
| 943 | buffer_len += ret; |
| 944 | } |
| 945 | msg_complete: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 946 | |
| 947 | if (buffer != NULL) { |
| 948 | request = json_tokener_parse(buffer); |
| 949 | operation = json_object_get_int(json_object_object_get(request, "type")); |
| 950 | |
| 951 | session_key = (char*) json_object_get_string(json_object_object_get(request, "session")); |
| 952 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 953 | if (operation != MSG_CONNECT && session_key == NULL) { |
| 954 | reply = json_object_new_object(); |
| 955 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 956 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification.")); |
| 957 | msgtext = json_object_to_json_string(reply); |
| 958 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 959 | json_object_put(reply); |
| 960 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 961 | close(client); |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | /* get parameters */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 966 | /* TODO NC_DATASTORE_URL */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 967 | ds_type_t = -1; |
| 968 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 969 | if (strcmp(target, "running") == 0) { |
| 970 | ds_type_t = NC_DATASTORE_RUNNING; |
| 971 | } else if (strcmp(target, "startup") == 0) { |
| 972 | ds_type_t = NC_DATASTORE_STARTUP; |
| 973 | } else if (strcmp(target, "candidate") == 0) { |
| 974 | ds_type_t = NC_DATASTORE_CANDIDATE; |
| 975 | } |
| 976 | } |
| 977 | ds_type_s = -1; |
| 978 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 979 | if (strcmp(source, "running") == 0) { |
| 980 | ds_type_s = NC_DATASTORE_RUNNING; |
| 981 | } else if (strcmp(source, "startup") == 0) { |
| 982 | ds_type_s = NC_DATASTORE_STARTUP; |
| 983 | } else if (strcmp(source, "candidate") == 0) { |
| 984 | ds_type_s = NC_DATASTORE_CANDIDATE; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* null global JSON error-reply */ |
| 989 | err_reply = NULL; |
| 990 | |
| 991 | /* prepare reply envelope */ |
| 992 | reply = json_object_new_object(); |
| 993 | |
| 994 | /* process required operation */ |
| 995 | switch (operation) { |
| 996 | case MSG_CONNECT: |
| 997 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 998 | |
| 999 | host = json_object_get_string(json_object_object_get(request, "host")); |
| 1000 | port = json_object_get_string(json_object_object_get(request, "port")); |
| 1001 | user = json_object_get_string(json_object_object_get(request, "user")); |
| 1002 | pass = json_object_get_string(json_object_object_get(request, "pass")); |
Tomas Cejka | f34f391 | 2012-09-05 18:14:06 +0200 | [diff] [blame] | 1003 | capabilities = json_object_object_get(request, "capabilities"); |
Tomas Cejka | ae9efe5 | 2013-04-23 13:37:36 +0200 | [diff] [blame] | 1004 | if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) { |
| 1005 | cpblts = nc_cpblts_new (NULL); |
| 1006 | for (i=0; i<len; i++) { |
| 1007 | nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i))); |
| 1008 | } |
| 1009 | } else { |
| 1010 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified"); |
| 1011 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1012 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 1013 | if ((host == NULL) || (user == NULL)) { |
| 1014 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input."); |
| 1015 | session_key = NULL; |
| 1016 | } else { |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 1017 | session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts); |
| 1018 | nc_cpblts_free (cpblts); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1019 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key); |
| 1020 | } |
| 1021 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1022 | if (session_key == NULL) { |
| 1023 | /* negative reply */ |
| 1024 | if (err_reply == NULL) { |
| 1025 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1026 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
Tomas Cejka | 1490ef1 | 2012-12-10 00:16:13 +0100 | [diff] [blame] | 1027 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1028 | } else { |
| 1029 | /* use filled err_reply from libnetconf's callback */ |
| 1030 | json_object_put(reply); |
| 1031 | reply = err_reply; |
Tomas Cejka | 1490ef1 | 2012-12-10 00:16:13 +0100 | [diff] [blame] | 1032 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1033 | } |
| 1034 | } else { |
| 1035 | /* positive reply */ |
| 1036 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1037 | json_object_object_add(reply, "session", json_object_new_string(session_key)); |
| 1038 | |
| 1039 | free(session_key); |
| 1040 | } |
| 1041 | |
| 1042 | break; |
| 1043 | case MSG_GET: |
| 1044 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key); |
| 1045 | |
| 1046 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1047 | |
| 1048 | //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter); |
| 1049 | |
Tomas Cejka | cdc274e | 2012-09-05 18:15:33 +0200 | [diff] [blame] | 1050 | if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1051 | if (err_reply == NULL) { |
| 1052 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1053 | json_object_object_add(reply, "error-message", json_object_new_string("get failed.")); |
| 1054 | } else { |
| 1055 | /* use filled err_reply from libnetconf's callback */ |
| 1056 | json_object_put(reply); |
| 1057 | reply = err_reply; |
| 1058 | } |
| 1059 | } else { |
| 1060 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1061 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 1062 | } |
| 1063 | break; |
| 1064 | case MSG_GETCONFIG: |
| 1065 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
| 1066 | |
| 1067 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1068 | |
| 1069 | if (ds_type_s == -1) { |
| 1070 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1071 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) { |
| 1076 | if (err_reply == NULL) { |
| 1077 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1078 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 1079 | } else { |
| 1080 | /* use filled err_reply from libnetconf's callback */ |
| 1081 | json_object_put(reply); |
| 1082 | reply = err_reply; |
| 1083 | } |
| 1084 | } else { |
| 1085 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1086 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 1087 | } |
| 1088 | break; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1089 | case MSG_GETSCHEMA: |
| 1090 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key); |
| 1091 | identifier = json_object_get_string(json_object_object_get(request, "identifier")); |
| 1092 | if (identifier == NULL) { |
| 1093 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1094 | json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied.")); |
| 1095 | break; |
| 1096 | } |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 1097 | version = json_object_get_string(json_object_object_get(request, "version")); |
| 1098 | format = json_object_get_string(json_object_object_get(request, "format")); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1099 | |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 1100 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format); |
Tomas Cejka | afe4607 | 2013-01-09 16:55:58 +0100 | [diff] [blame] | 1101 | if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) { |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1102 | if (err_reply == NULL) { |
| 1103 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1104 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 1105 | } else { |
| 1106 | /* use filled err_reply from libnetconf's callback */ |
| 1107 | json_object_put(reply); |
| 1108 | reply = err_reply; |
| 1109 | } |
| 1110 | } else { |
| 1111 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1112 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 1113 | } |
| 1114 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1115 | case MSG_EDITCONFIG: |
| 1116 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key); |
| 1117 | |
| 1118 | defop = json_object_get_string(json_object_object_get(request, "default-operation")); |
| 1119 | if (defop != NULL) { |
| 1120 | if (strcmp(defop, "merge") == 0) { |
| 1121 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 1122 | } else if (strcmp(defop, "replace") == 0) { |
| 1123 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 1124 | } else if (strcmp(defop, "none") == 0) { |
| 1125 | defop_type = NC_EDIT_DEFOP_NONE; |
| 1126 | } else { |
| 1127 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1128 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter.")); |
| 1129 | break; |
| 1130 | } |
| 1131 | } else { |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 1132 | defop_type = NC_EDIT_DEFOP_NOTSET; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | erropt = json_object_get_string(json_object_object_get(request, "error-option")); |
| 1136 | if (erropt != NULL) { |
| 1137 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 1138 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 1139 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 1140 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 1141 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 1142 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 1143 | } else { |
| 1144 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1145 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter.")); |
| 1146 | break; |
| 1147 | } |
| 1148 | } else { |
| 1149 | erropt_type = 0; |
| 1150 | } |
| 1151 | |
| 1152 | if (ds_type_t == -1) { |
| 1153 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1154 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1159 | if (config == NULL) { |
| 1160 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1161 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter.")); |
| 1162 | break; |
| 1163 | } |
| 1164 | |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 1165 | /* TODO url capability see netconf_editconfig */ |
| 1166 | /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */ |
| 1167 | if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1168 | if (err_reply == NULL) { |
| 1169 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1170 | json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed.")); |
| 1171 | } else { |
| 1172 | /* use filled err_reply from libnetconf's callback */ |
| 1173 | json_object_put(reply); |
| 1174 | reply = err_reply; |
| 1175 | } |
| 1176 | } else { |
| 1177 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1178 | } |
| 1179 | break; |
| 1180 | case MSG_COPYCONFIG: |
| 1181 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key); |
| 1182 | config = NULL; |
| 1183 | |
| 1184 | if (source == NULL) { |
| 1185 | /* no explicit source specified -> use config data */ |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 1186 | ds_type_s = NC_DATASTORE_CONFIG; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1187 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1188 | } else if (ds_type_s == -1) { |
| 1189 | /* source datastore specified, but it is invalid */ |
| 1190 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1191 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
| 1192 | break; |
| 1193 | } |
| 1194 | |
| 1195 | if (ds_type_t == -1) { |
| 1196 | /* invalid target datastore specified */ |
| 1197 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1198 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 1199 | break; |
| 1200 | } |
| 1201 | |
| 1202 | if (source == NULL && config == NULL) { |
| 1203 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1204 | json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required.")); |
| 1205 | } else { |
Tomas Cejka | 4ce5d0a | 2013-01-17 19:23:54 +0100 | [diff] [blame] | 1206 | if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1207 | if (err_reply == NULL) { |
| 1208 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1209 | json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed.")); |
| 1210 | } else { |
| 1211 | /* use filled err_reply from libnetconf's callback */ |
| 1212 | json_object_put(reply); |
| 1213 | reply = err_reply; |
| 1214 | } |
| 1215 | } else { |
| 1216 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1217 | } |
| 1218 | } |
| 1219 | break; |
| 1220 | case MSG_DELETECONFIG: |
| 1221 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key); |
| 1222 | /* no break - unifying code */ |
| 1223 | case MSG_LOCK: |
| 1224 | if (operation == MSG_LOCK) { |
| 1225 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key); |
| 1226 | } |
| 1227 | /* no break - unifying code */ |
| 1228 | case MSG_UNLOCK: |
| 1229 | if (operation == MSG_UNLOCK) { |
| 1230 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key); |
| 1231 | } |
| 1232 | |
| 1233 | if (ds_type_t == -1) { |
| 1234 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1235 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | switch(operation) { |
| 1240 | case MSG_DELETECONFIG: |
| 1241 | status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t); |
| 1242 | break; |
| 1243 | case MSG_LOCK: |
| 1244 | status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t); |
| 1245 | break; |
| 1246 | case MSG_UNLOCK: |
| 1247 | status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t); |
| 1248 | break; |
| 1249 | default: |
| 1250 | status = -1; |
| 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | if (status != EXIT_SUCCESS) { |
| 1255 | if (err_reply == NULL) { |
| 1256 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1257 | json_object_object_add(reply, "error-message", json_object_new_string("operation failed.")); |
| 1258 | } else { |
| 1259 | /* use filled err_reply from libnetconf's callback */ |
| 1260 | json_object_put(reply); |
| 1261 | reply = err_reply; |
| 1262 | } |
| 1263 | } else { |
| 1264 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1265 | } |
| 1266 | break; |
| 1267 | case MSG_KILL: |
| 1268 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key); |
| 1269 | |
| 1270 | sid = json_object_get_string(json_object_object_get(request, "session-id")); |
| 1271 | |
| 1272 | if (sid == NULL) { |
| 1273 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1274 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter.")); |
| 1275 | break; |
| 1276 | } |
| 1277 | |
| 1278 | if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) { |
| 1279 | if (err_reply == NULL) { |
| 1280 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1281 | json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed.")); |
| 1282 | } else { |
| 1283 | /* use filled err_reply from libnetconf's callback */ |
| 1284 | json_object_put(reply); |
| 1285 | reply = err_reply; |
| 1286 | } |
| 1287 | } else { |
| 1288 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1289 | } |
| 1290 | break; |
| 1291 | case MSG_DISCONNECT: |
| 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 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1297 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1298 | } else { |
| 1299 | /* use filled err_reply from libnetconf's callback */ |
| 1300 | json_object_put(reply); |
| 1301 | reply = err_reply; |
| 1302 | } |
| 1303 | } else { |
| 1304 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1305 | } |
| 1306 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1307 | case MSG_RELOADHELLO: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1308 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1309 | |
Kupka David | da134a1 | 2012-09-06 14:12:16 +0200 | [diff] [blame] | 1310 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1311 | if ((locked_session != NULL) && (locked_session->hello_message != NULL)) { |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 1312 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session."); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1313 | struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL); |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 1314 | if (temp_session != NULL) { |
| 1315 | prepare_status_message(server, locked_session, temp_session); |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 1316 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session."); |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 1317 | nc_session_close(temp_session, NC_SESSION_TERM_CLOSED); |
| 1318 | } else { |
| 1319 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment"); |
| 1320 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1321 | } else { |
| 1322 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1323 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1324 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1325 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1326 | /* do NOT insert "break" here, we want to give new info back */; |
| 1327 | case MSG_INFO: |
| 1328 | if (operation != MSG_INFO) { |
Tomas Cejka | a72dfdb | 2013-05-15 13:26:21 +0200 | [diff] [blame] | 1329 | if (locked_session->hello_message != NULL) { |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 1330 | json_object_put(reply); |
Tomas Cejka | a72dfdb | 2013-05-15 13:26:21 +0200 | [diff] [blame] | 1331 | reply = locked_session->hello_message; |
| 1332 | } else { |
| 1333 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1334 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1335 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1336 | } else { |
| 1337 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1338 | |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1339 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
Tomas Cejka | a72dfdb | 2013-05-15 13:26:21 +0200 | [diff] [blame] | 1340 | if (locked_session != NULL) { |
| 1341 | if (locked_session->hello_message != NULL) { |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 1342 | json_object_put(reply); |
Tomas Cejka | a72dfdb | 2013-05-15 13:26:21 +0200 | [diff] [blame] | 1343 | reply = locked_session->hello_message; |
| 1344 | } else { |
| 1345 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1346 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1347 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1348 | } else { |
| 1349 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1350 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1351 | } |
| 1352 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1353 | break; |
| 1354 | case MSG_GENERIC: |
| 1355 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key); |
| 1356 | |
| 1357 | config = json_object_get_string(json_object_object_get(request, "content")); |
| 1358 | |
| 1359 | if (config == NULL) { |
| 1360 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1361 | json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter.")); |
| 1362 | break; |
| 1363 | } |
| 1364 | |
| 1365 | if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) { |
| 1366 | if (err_reply == NULL) { |
| 1367 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1368 | json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed.")); |
| 1369 | } else { |
| 1370 | /* use filled err_reply from libnetconf's callback */ |
| 1371 | json_object_put(reply); |
| 1372 | reply = err_reply; |
| 1373 | } |
| 1374 | } else { |
| 1375 | if (data == NULL) { |
| 1376 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1377 | } else { |
| 1378 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1379 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 1380 | } |
| 1381 | } |
| 1382 | break; |
| 1383 | default: |
| 1384 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1385 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1386 | json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported.")); |
| 1387 | break; |
| 1388 | } |
| 1389 | json_object_put(request); |
| 1390 | |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1391 | /* send reply to caller */ |
| 1392 | if (reply != NULL) { |
| 1393 | msgtext = json_object_to_json_string(reply); |
| 1394 | if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) { |
| 1395 | free (buffer); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1396 | break; |
| 1397 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1398 | send(client, chunked_msg, strlen(chunked_msg) + 1, 0); |
| 1399 | json_object_put(reply); |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 1400 | reply = NULL; |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1401 | free (chunked_msg); |
| 1402 | free (buffer); |
| 1403 | } else { |
| 1404 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1408 | free (arg); |
| 1409 | |
| 1410 | return retval; |
| 1411 | } |
| 1412 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1413 | /** |
| 1414 | * \brief Close all open NETCONF sessions. |
| 1415 | * |
| 1416 | * During termination of mod_netconf, it is useful to close all remaining |
| 1417 | * sessions. This function iterates over the list of sessions and close them |
| 1418 | * all. |
| 1419 | * |
| 1420 | * \param[in] server pointer to server_rec for logging |
| 1421 | * \param[in] p apr pool needed for hash table iterating |
| 1422 | * \param[in] ht hash table of session_with_mutex structs |
| 1423 | */ |
| 1424 | static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht) |
| 1425 | { |
| 1426 | apr_hash_index_t *hi; |
| 1427 | void *val = NULL; |
| 1428 | struct session_with_mutex *swm = NULL; |
| 1429 | struct nc_session *ns = NULL; |
| 1430 | const char *hashed_key = NULL; |
| 1431 | apr_ssize_t hashed_key_length; |
| 1432 | |
| 1433 | for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) { |
| 1434 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1435 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key); |
| 1436 | swm = (struct session_with_mutex *) val; |
| 1437 | if (swm != NULL) { |
| 1438 | pthread_mutex_lock(&swm->lock); |
| 1439 | if (swm->session != NULL) { |
| 1440 | ns = swm->session; |
| 1441 | nc_session_close(ns, NC_SESSION_TERM_CLOSED); |
| 1442 | nc_session_free(ns); |
| 1443 | swm->session = NULL; |
| 1444 | } |
| 1445 | pthread_mutex_unlock(&swm->lock); |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht) |
| 1451 | { |
| 1452 | apr_hash_index_t *hi; |
| 1453 | void *val = NULL; |
| 1454 | struct nc_session *ns = NULL; |
| 1455 | struct session_with_mutex *swm = NULL; |
| 1456 | const char *hashed_key = NULL; |
| 1457 | apr_ssize_t hashed_key_length; |
| 1458 | apr_time_t current_time = apr_time_now(); |
| 1459 | |
| 1460 | for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) { |
| 1461 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1462 | swm = (struct session_with_mutex *) val; |
| 1463 | if (swm == NULL) { |
| 1464 | continue; |
| 1465 | } |
| 1466 | ns = swm->session; |
| 1467 | if (ns == NULL) { |
| 1468 | continue; |
| 1469 | } |
| 1470 | pthread_mutex_lock(&swm->lock); |
| 1471 | if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) { |
| 1472 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key); |
| 1473 | nc_session_close(ns, NC_SESSION_TERM_CLOSED); |
| 1474 | nc_session_free (ns); |
| 1475 | ns = NULL; |
| 1476 | /* remove session from the active sessions list */ |
| 1477 | apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL); |
| 1478 | } |
| 1479 | pthread_mutex_unlock(&swm->lock); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | |
| 1484 | /** |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1485 | * This is actually implementation of NETCONF client |
| 1486 | * - requests are received from UNIX socket in the predefined format |
| 1487 | * - results are replied through the same way |
| 1488 | * - the daemon run as a separate process, but it is started and stopped |
| 1489 | * automatically by Apache. |
| 1490 | * |
| 1491 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1492 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 1493 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1494 | struct timeval tv; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1495 | struct sockaddr_un local, remote; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1496 | int lsock, client, ret, i, pthread_count = 0; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1497 | unsigned int olds = 0, timediff = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1498 | socklen_t len; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1499 | mod_netconf_cfg *cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1500 | apr_hash_t *netconf_sessions_list; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1501 | struct pass_to_thread * arg; |
| 1502 | pthread_t * ptids = calloc (1,sizeof(pthread_t)); |
| 1503 | struct timespec maxtime; |
| 1504 | pthread_rwlockattr_t lock_attrs; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1505 | #ifdef WITH_NOTIFICATIONS |
| 1506 | char use_notifications = 0; |
| 1507 | #endif |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1508 | |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1509 | /* wait at most 5 seconds for every thread to terminate */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1510 | maxtime.tv_sec = 5; |
| 1511 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1512 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1513 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1514 | unixd_setup_child(); |
| 1515 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1516 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 1517 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1518 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 1519 | return; |
| 1520 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1521 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1522 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1523 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 1524 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 1525 | return; |
| 1526 | } |
| 1527 | |
| 1528 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1529 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1530 | unlink(local.sun_path); |
| 1531 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 1532 | |
| 1533 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 1534 | if (errno == EADDRINUSE) { |
| 1535 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 1536 | close(lsock); |
| 1537 | exit(0); |
| 1538 | } |
| 1539 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 1540 | close(lsock); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
| 1544 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 1545 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 1546 | close(lsock); |
| 1547 | return; |
| 1548 | } |
| 1549 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 1550 | /* prepare internal lists */ |
| 1551 | netconf_sessions_list = apr_hash_make(pool); |
| 1552 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1553 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 1554 | if (notification_init(pool, server, netconf_sessions_list) == -1) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1555 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed"); |
| 1556 | use_notifications = 0; |
| 1557 | } else { |
| 1558 | use_notifications = 1; |
| 1559 | } |
| 1560 | #endif |
| 1561 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1562 | /* setup libnetconf's callbacks */ |
| 1563 | nc_verbosity(NC_VERB_DEBUG); |
| 1564 | clb_print_server = server; |
| 1565 | nc_callback_print(clb_print); |
| 1566 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 1567 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 1568 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 1569 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1570 | |
| 1571 | /* disable publickey authentication */ |
| 1572 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 1573 | |
Tomas Cejka | 44e71db | 2013-04-21 15:50:47 +0200 | [diff] [blame] | 1574 | ncdflt_set_basic_mode(NCWD_MODE_ALL); |
| 1575 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1576 | /* create mutex protecting session list */ |
| 1577 | pthread_rwlockattr_init(&lock_attrs); |
| 1578 | /* rwlock is shared only with threads in this process */ |
| 1579 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 1580 | /* create rw lock */ |
| 1581 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
| 1582 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
| 1583 | close (lsock); |
| 1584 | return; |
| 1585 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame^] | 1586 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1587 | fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1588 | while (isterminated == 0) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1589 | gettimeofday(&tv, NULL); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1590 | timediff = (unsigned int)tv.tv_sec - olds; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1591 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1592 | if (timediff > 60) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1593 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications"); |
| 1594 | } |
| 1595 | if (use_notifications == 1) { |
| 1596 | notification_handle(); |
| 1597 | } |
| 1598 | #endif |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1599 | if (timediff > ACTIVITY_CHECK_INTERVAL) { |
| 1600 | check_timeout_and_close(server, pool, netconf_sessions_list); |
| 1601 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1602 | |
| 1603 | /* open incoming connection if any */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1604 | len = sizeof(remote); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1605 | if (((unsigned int)tv.tv_sec - olds) > 60) { |
| 1606 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client"); |
| 1607 | olds = tv.tv_sec; |
| 1608 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1609 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1610 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 1611 | apr_sleep(SLEEP_TIME); |
| 1612 | continue; |
| 1613 | } else if (client == -1 && (errno == EINTR)) { |
| 1614 | continue; |
| 1615 | } else if (client == -1) { |
| 1616 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 1617 | continue; |
| 1618 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1619 | |
| 1620 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1621 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1622 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1623 | arg = malloc (sizeof(struct pass_to_thread)); |
| 1624 | arg->client = client; |
| 1625 | arg->pool = pool; |
| 1626 | arg->server = server; |
| 1627 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1628 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1629 | /* start new thread. It will serve this particular request and then terminate */ |
| 1630 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) { |
| 1631 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret); |
| 1632 | } else { |
| 1633 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]); |
| 1634 | pthread_count++; |
| 1635 | ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1)); |
| 1636 | ptids[pthread_count] = 0; |
| 1637 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1638 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1639 | /* check if some thread already terminated, free some resources by joining it */ |
| 1640 | for (i=0; i<pthread_count; i++) { |
| 1641 | if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) { |
| 1642 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg); |
| 1643 | pthread_count--; |
| 1644 | if (pthread_count > 0) { |
| 1645 | /* place last Thread ID on the place of joined one */ |
| 1646 | ptids[i] = ptids[pthread_count]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1647 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1648 | } |
| 1649 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1650 | 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] | 1651 | } |
| 1652 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1653 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating..."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1654 | /* join all threads */ |
| 1655 | for (i=0; i<pthread_count; i++) { |
| 1656 | pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime); |
| 1657 | } |
| 1658 | free (ptids); |
| 1659 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1660 | close(lsock); |
| 1661 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1662 | #ifdef WITH_NOTIFICATIONS |
| 1663 | notification_close(); |
| 1664 | #endif |
| 1665 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1666 | /* close all NETCONF sessions */ |
| 1667 | close_all_nc_sessions(server, pool, netconf_sessions_list); |
| 1668 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1669 | /* destroy rwlock */ |
| 1670 | pthread_rwlock_destroy(&session_lock); |
| 1671 | pthread_rwlockattr_destroy(&lock_attrs); |
| 1672 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1673 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 1674 | |
| 1675 | exit(APR_SUCCESS); |
| 1676 | } |
| 1677 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1678 | 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] | 1679 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1680 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 1681 | |
| 1682 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 1683 | apr_pool_create(&config->pool, pool); |
| 1684 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1685 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1686 | |
| 1687 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 1691 | apr_pool_t * plog, server_rec * s) |
| 1692 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1693 | mod_netconf_cfg *config; |
| 1694 | apr_status_t res; |
| 1695 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1696 | /* These two help ensure that we only init once. */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1697 | void *data = NULL; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1698 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* |
| 1701 | * The following checks if this routine has been called before. |
| 1702 | * This is necessary because the parent process gets initialized |
| 1703 | * a couple of times as the server starts up. |
| 1704 | */ |
| 1705 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 1706 | if (!data) { |
| 1707 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 1708 | return (OK); |
| 1709 | } |
| 1710 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1711 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 1712 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 1713 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1714 | if (config && config->forkproc == NULL) { |
| 1715 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 1716 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1717 | switch (res) { |
| 1718 | case APR_INCHILD: |
| 1719 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1720 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1721 | apr_signal(SIGTERM, signal_handler); |
| 1722 | |
| 1723 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1724 | 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] | 1725 | |
| 1726 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1727 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1728 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1729 | /* I never should be here, wtf?!? */ |
| 1730 | 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] | 1731 | exit(APR_EGENERAL); |
| 1732 | break; |
| 1733 | case APR_INPARENT: |
| 1734 | /* 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] | 1735 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1736 | break; |
| 1737 | default: |
| 1738 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 1739 | break; |
| 1740 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1741 | } else { |
| 1742 | 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] | 1743 | } |
| 1744 | |
| 1745 | return OK; |
| 1746 | } |
| 1747 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1748 | /** |
| 1749 | * Register module hooks |
| 1750 | */ |
| 1751 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 1752 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1753 | 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] | 1754 | } |
| 1755 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1756 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 1757 | { |
| 1758 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | |
| 1762 | static const command_rec netconf_cmds[] = { |
| 1763 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 1764 | {NULL} |
| 1765 | }; |
| 1766 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1767 | /* Dispatch list for API hooks */ |
| 1768 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 1769 | STANDARD20_MODULE_STUFF, |
| 1770 | NULL, /* create per-dir config structures */ |
| 1771 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1772 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1773 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1774 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1775 | mod_netconf_register_hooks /* register hooks */ |
| 1776 | }; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1777 | |