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; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 388 | nc_reply* reply = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 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; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 741 | nc_reply* reply = NULL; |
| 742 | nc_rpc* rpc = NULL; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 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 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 834 | /** |
| 835 | * Receive message from client and return pointer to it. |
| 836 | * Caller should free message memory. |
| 837 | * \param[in] client socket descriptor of client |
| 838 | * \param[in] server httpd server for logging |
| 839 | * \return pointer to message |
| 840 | */ |
| 841 | char *get_framed_message(server_rec *server, int client) |
| 842 | { |
| 843 | /* read json in chunked framing */ |
| 844 | unsigned int buffer_size = 0; |
| 845 | ssize_t buffer_len = 0; |
| 846 | char *buffer = NULL; |
| 847 | char c; |
| 848 | ssize_t ret; |
| 849 | int i, chunk_len; |
| 850 | char chunk_len_str[12]; |
| 851 | |
| 852 | while (1) { |
| 853 | /* read chunk length */ |
| 854 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 855 | if (buffer != NULL) { |
| 856 | free (buffer); |
| 857 | buffer = NULL; |
| 858 | } |
| 859 | break; |
| 860 | } |
| 861 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 862 | if (buffer != NULL) { |
| 863 | free (buffer); |
| 864 | buffer = NULL; |
| 865 | } |
| 866 | break; |
| 867 | } |
| 868 | i=0; |
| 869 | memset (chunk_len_str, 0, 12); |
| 870 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 871 | if (i==0 && c == '#') { |
| 872 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 873 | /* end but invalid */ |
| 874 | if (buffer != NULL) { |
| 875 | free (buffer); |
| 876 | buffer = NULL; |
| 877 | } |
| 878 | } |
| 879 | /* end of message, double-loop break */ |
| 880 | goto msg_complete; |
| 881 | } |
| 882 | chunk_len_str[i++] = c; |
| 883 | if (i==11) { |
| 884 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!"); |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | if (c != '\n') { |
| 889 | if (buffer != NULL) { |
| 890 | free (buffer); |
| 891 | buffer = NULL; |
| 892 | } |
| 893 | break; |
| 894 | } |
| 895 | chunk_len_str[i] = 0; |
| 896 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 897 | if (buffer != NULL) { |
| 898 | free (buffer); |
| 899 | buffer = NULL; |
| 900 | } |
| 901 | break; |
| 902 | } |
| 903 | buffer_size += chunk_len+1; |
| 904 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 905 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 906 | if (buffer != NULL) { |
| 907 | free (buffer); |
| 908 | buffer = NULL; |
| 909 | } |
| 910 | break; |
| 911 | } |
| 912 | buffer_len += ret; |
| 913 | } |
| 914 | msg_complete: |
| 915 | return buffer; |
| 916 | } |
| 917 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 918 | NC_DATASTORE parse_datastore(const char *ds) |
| 919 | { |
| 920 | if (strcmp(ds, "running") == 0) { |
| 921 | return NC_DATASTORE_RUNNING; |
| 922 | } else if (strcmp(ds, "startup") == 0) { |
| 923 | return NC_DATASTORE_STARTUP; |
| 924 | } else if (strcmp(ds, "candidate") == 0) { |
| 925 | return NC_DATASTORE_CANDIDATE; |
| 926 | } |
| 927 | return -1; |
| 928 | } |
| 929 | |
| 930 | json_object *create_error(const char *errmess) |
| 931 | { |
| 932 | json_object *reply = json_object_new_object(); |
| 933 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 934 | json_object_object_add(reply, "error-message", json_object_new_string(errmess)); |
| 935 | return reply; |
| 936 | |
| 937 | } |
| 938 | |
| 939 | json_object *create_data(const char *data) |
| 940 | { |
| 941 | json_object *reply = json_object_new_object(); |
| 942 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 943 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 944 | return reply; |
| 945 | } |
| 946 | |
| 947 | json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list) |
| 948 | { |
| 949 | const char *host = NULL; |
| 950 | const char *port = NULL; |
| 951 | const char *user = NULL; |
| 952 | const char *pass = NULL; |
| 953 | json_object *capabilities = NULL; |
| 954 | json_object *reply = NULL; |
| 955 | char *session_key_hash = NULL; |
| 956 | struct nc_cpblts* cpblts = NULL; |
| 957 | unsigned int len, i; |
| 958 | |
| 959 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 960 | host = json_object_get_string(json_object_object_get((json_object *) request, "host")); |
| 961 | port = json_object_get_string(json_object_object_get((json_object *) request, "port")); |
| 962 | user = json_object_get_string(json_object_object_get((json_object *) request, "user")); |
| 963 | pass = json_object_get_string(json_object_object_get((json_object *) request, "pass")); |
| 964 | |
| 965 | capabilities = json_object_object_get((json_object *) request, "capabilities"); |
| 966 | if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) { |
| 967 | cpblts = nc_cpblts_new(NULL); |
| 968 | for (i=0; i<len; i++) { |
| 969 | nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i))); |
| 970 | } |
| 971 | } else { |
| 972 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified"); |
| 973 | } |
| 974 | |
| 975 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 976 | if ((host == NULL) || (user == NULL)) { |
| 977 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input."); |
| 978 | session_key_hash = NULL; |
| 979 | } else { |
| 980 | session_key_hash = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts); |
| 981 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash); |
| 982 | } |
| 983 | if (cpblts != NULL) { |
| 984 | nc_cpblts_free(cpblts); |
| 985 | } |
| 986 | |
| 987 | if (session_key_hash == NULL) { |
| 988 | /* negative reply */ |
| 989 | if (err_reply == NULL) { |
| 990 | reply = json_object_new_object(); |
| 991 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 992 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
| 993 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed."); |
| 994 | } else { |
| 995 | /* use filled err_reply from libnetconf's callback */ |
| 996 | reply = err_reply; |
| 997 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback."); |
| 998 | } |
| 999 | } else { |
| 1000 | /* positive reply */ |
| 1001 | reply = json_object_new_object(); |
| 1002 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1003 | json_object_object_add(reply, "session", json_object_new_string(session_key_hash)); |
| 1004 | |
| 1005 | free(session_key_hash); |
| 1006 | } |
| 1007 | return reply; |
| 1008 | } |
| 1009 | |
| 1010 | json_object *handle_op_get(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1011 | { |
| 1012 | const char *filter = NULL; |
| 1013 | const char *data = NULL; |
| 1014 | json_object *reply = NULL; |
| 1015 | |
| 1016 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key); |
| 1017 | |
| 1018 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1019 | |
| 1020 | if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) { |
| 1021 | if (err_reply == NULL) { |
| 1022 | reply = create_error("Get information from device failed."); |
| 1023 | } else { |
| 1024 | /* use filled err_reply from libnetconf's callback */ |
| 1025 | reply = err_reply; |
| 1026 | } |
| 1027 | } else { |
| 1028 | return create_data(data); |
| 1029 | } |
| 1030 | return reply; |
| 1031 | } |
| 1032 | |
| 1033 | json_object *handle_op_getconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1034 | { |
| 1035 | NC_DATASTORE ds_type_s = -1; |
| 1036 | NC_DATASTORE ds_type_t = -1; |
| 1037 | const char *filter = NULL; |
| 1038 | const char *data = NULL; |
| 1039 | const char *source = NULL; |
| 1040 | const char *target = NULL; |
| 1041 | json_object *reply = NULL; |
| 1042 | |
| 1043 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
| 1044 | |
| 1045 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1046 | |
| 1047 | /* get parameters */ |
| 1048 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1049 | ds_type_t = parse_datastore(target); |
| 1050 | } |
| 1051 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1052 | ds_type_s = parse_datastore(source); |
| 1053 | } |
| 1054 | if (ds_type_s == -1) { |
| 1055 | return create_error("Invalid source repository type requested."); |
| 1056 | } |
| 1057 | |
| 1058 | if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) { |
| 1059 | if (err_reply == NULL) { |
| 1060 | reply = create_error("Get configuration information from device failed."); |
| 1061 | } else { |
| 1062 | /* use filled err_reply from libnetconf's callback */ |
| 1063 | reply = err_reply; |
| 1064 | } |
| 1065 | } else { |
| 1066 | return create_data(data); |
| 1067 | } |
| 1068 | return reply; |
| 1069 | } |
| 1070 | |
| 1071 | json_object *handle_op_getschema(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1072 | { |
| 1073 | const char *data = NULL; |
| 1074 | const char *identifier = NULL; |
| 1075 | const char *version = NULL; |
| 1076 | const char *format = NULL; |
| 1077 | json_object *reply = NULL; |
| 1078 | |
| 1079 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key); |
| 1080 | identifier = json_object_get_string(json_object_object_get(request, "identifier")); |
| 1081 | if (identifier == NULL) { |
| 1082 | return create_error("No identifier for get-schema supplied."); |
| 1083 | } |
| 1084 | version = json_object_get_string(json_object_object_get(request, "version")); |
| 1085 | format = json_object_get_string(json_object_object_get(request, "format")); |
| 1086 | |
| 1087 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format); |
| 1088 | if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) { |
| 1089 | if (err_reply == NULL) { |
| 1090 | reply = create_error("Get schema failed."); |
| 1091 | } else { |
| 1092 | /* use filled err_reply from libnetconf's callback */ |
| 1093 | reply = err_reply; |
| 1094 | } |
| 1095 | } else { |
| 1096 | return create_data(data); |
| 1097 | } |
| 1098 | return reply; |
| 1099 | } |
| 1100 | |
| 1101 | json_object *handle_op_editconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1102 | { |
| 1103 | NC_DATASTORE ds_type_s = -1; |
| 1104 | NC_DATASTORE ds_type_t = -1; |
| 1105 | NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1106 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
| 1107 | const char *defop = NULL; |
| 1108 | const char *erropt = NULL; |
| 1109 | const char *config = NULL; |
| 1110 | const char *source = NULL; |
| 1111 | const char *target = NULL; |
| 1112 | json_object *reply = NULL; |
| 1113 | |
| 1114 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key); |
| 1115 | |
| 1116 | defop = json_object_get_string(json_object_object_get(request, "default-operation")); |
| 1117 | if (defop != NULL) { |
| 1118 | if (strcmp(defop, "merge") == 0) { |
| 1119 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 1120 | } else if (strcmp(defop, "replace") == 0) { |
| 1121 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 1122 | } else if (strcmp(defop, "none") == 0) { |
| 1123 | defop_type = NC_EDIT_DEFOP_NONE; |
| 1124 | } else { |
| 1125 | return create_error("Invalid default-operation parameter."); |
| 1126 | } |
| 1127 | } else { |
| 1128 | defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1129 | } |
| 1130 | |
| 1131 | erropt = json_object_get_string(json_object_object_get(request, "error-option")); |
| 1132 | if (erropt != NULL) { |
| 1133 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 1134 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 1135 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 1136 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 1137 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 1138 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 1139 | } else { |
| 1140 | return create_error("Invalid error-option parameter."); |
| 1141 | } |
| 1142 | } else { |
| 1143 | erropt_type = 0; |
| 1144 | } |
| 1145 | |
| 1146 | /* get parameters */ |
| 1147 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1148 | ds_type_t = parse_datastore(target); |
| 1149 | } |
| 1150 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1151 | ds_type_s = parse_datastore(source); |
| 1152 | } |
| 1153 | if (ds_type_t == -1) { |
| 1154 | return create_error("Invalid target repository type requested."); |
| 1155 | } |
| 1156 | |
| 1157 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1158 | if (config == NULL) { |
| 1159 | return create_error("Invalid config data parameter."); |
| 1160 | } |
| 1161 | |
| 1162 | if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) { |
| 1163 | if (err_reply == NULL) { |
| 1164 | reply = create_error("Edit-config failed."); |
| 1165 | } else { |
| 1166 | /* use filled err_reply from libnetconf's callback */ |
| 1167 | reply = err_reply; |
| 1168 | } |
| 1169 | } else { |
| 1170 | reply = json_object_new_object(); |
| 1171 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1172 | } |
| 1173 | return reply; |
| 1174 | } |
| 1175 | |
| 1176 | json_object *handle_op_copyconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1177 | { |
| 1178 | NC_DATASTORE ds_type_s = -1; |
| 1179 | NC_DATASTORE ds_type_t = -1; |
| 1180 | const char *config = NULL; |
| 1181 | const char *target = NULL; |
| 1182 | const char *source = NULL; |
| 1183 | json_object *reply = NULL; |
| 1184 | |
| 1185 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key); |
| 1186 | |
| 1187 | /* get parameters */ |
| 1188 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1189 | ds_type_t = parse_datastore(target); |
| 1190 | } |
| 1191 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1192 | ds_type_s = parse_datastore(source); |
| 1193 | } |
| 1194 | if (source == NULL) { |
| 1195 | /* no explicit source specified -> use config data */ |
| 1196 | ds_type_s = NC_DATASTORE_CONFIG; |
| 1197 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1198 | } else if (ds_type_s == -1) { |
| 1199 | /* source datastore specified, but it is invalid */ |
| 1200 | return create_error("Invalid source repository type requested."); |
| 1201 | } |
| 1202 | |
| 1203 | if (ds_type_t == -1) { |
| 1204 | /* invalid target datastore specified */ |
| 1205 | return create_error("Invalid target repository type requested."); |
| 1206 | } |
| 1207 | |
| 1208 | if (source == NULL && config == NULL) { |
| 1209 | reply = create_error("invalid input parameters - one of source and config is required."); |
| 1210 | } else { |
| 1211 | if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) { |
| 1212 | if (err_reply == NULL) { |
| 1213 | reply = create_error("Copying of configuration failed."); |
| 1214 | } else { |
| 1215 | /* use filled err_reply from libnetconf's callback */ |
| 1216 | reply = err_reply; |
| 1217 | } |
| 1218 | } else { |
| 1219 | reply = json_object_new_object(); |
| 1220 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1221 | } |
| 1222 | } |
| 1223 | return reply; |
| 1224 | } |
| 1225 | |
| 1226 | json_object *handle_op_generic(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1227 | { |
| 1228 | json_object *reply = NULL; |
| 1229 | const char *config = NULL; |
| 1230 | char *data = NULL; |
| 1231 | |
| 1232 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key); |
| 1233 | |
| 1234 | config = json_object_get_string(json_object_object_get(request, "content")); |
| 1235 | |
| 1236 | if (config == NULL) { |
| 1237 | return create_error("Missing content parameter."); |
| 1238 | } |
| 1239 | |
| 1240 | if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) { |
| 1241 | if (err_reply == NULL) { |
| 1242 | reply = create_error("Killing of session failed."); |
| 1243 | } else { |
| 1244 | /* use filled err_reply from libnetconf's callback */ |
| 1245 | reply = err_reply; |
| 1246 | } |
| 1247 | } else { |
| 1248 | if (data == NULL) { |
| 1249 | reply = json_object_new_object(); |
| 1250 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1251 | } else { |
| 1252 | return create_data(data); |
| 1253 | } |
| 1254 | } |
| 1255 | return reply; |
| 1256 | } |
| 1257 | |
| 1258 | json_object *handle_op_disconnect(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1259 | { |
| 1260 | json_object *reply = NULL; |
| 1261 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key); |
| 1262 | |
| 1263 | if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) { |
| 1264 | if (err_reply == NULL) { |
| 1265 | reply = create_error("Invalid session identifier."); |
| 1266 | } else { |
| 1267 | /* use filled err_reply from libnetconf's callback */ |
| 1268 | reply = err_reply; |
| 1269 | } |
| 1270 | } else { |
| 1271 | reply = json_object_new_object(); |
| 1272 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1273 | } |
| 1274 | return reply; |
| 1275 | } |
| 1276 | |
| 1277 | json_object *handle_op_kill(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1278 | { |
| 1279 | json_object *reply = NULL; |
| 1280 | const char *sid = NULL; |
| 1281 | |
| 1282 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key); |
| 1283 | |
| 1284 | sid = json_object_get_string(json_object_object_get(request, "session-id")); |
| 1285 | |
| 1286 | if (sid == NULL) { |
| 1287 | return create_error("Missing session-id parameter."); |
| 1288 | } |
| 1289 | |
| 1290 | if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) { |
| 1291 | if (err_reply == NULL) { |
| 1292 | reply = create_error("Killing of session failed."); |
| 1293 | } else { |
| 1294 | /* use filled err_reply from libnetconf's callback */ |
| 1295 | reply = err_reply; |
| 1296 | } |
| 1297 | } else { |
| 1298 | reply = json_object_new_object(); |
| 1299 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1300 | } |
| 1301 | return reply; |
| 1302 | } |
| 1303 | |
| 1304 | json_object *handle_op_reloadhello(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1305 | { |
| 1306 | struct session_with_mutex * locked_session = NULL; |
| 1307 | json_object *reply = NULL; |
| 1308 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1309 | |
| 1310 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1311 | if ((locked_session != NULL) && (locked_session->hello_message != NULL)) { |
| 1312 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session."); |
| 1313 | struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL); |
| 1314 | if (temp_session != NULL) { |
| 1315 | prepare_status_message(server, locked_session, temp_session); |
| 1316 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session."); |
| 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 | reply = create_error("Reload was unsuccessful, connection failed."); |
| 1321 | } |
| 1322 | } else { |
| 1323 | reply = create_error("Invalid session identifier."); |
| 1324 | } |
| 1325 | |
| 1326 | if ((reply == NULL) && (locked_session->hello_message != NULL)) { |
| 1327 | reply = locked_session->hello_message; |
| 1328 | } |
| 1329 | return reply; |
| 1330 | } |
| 1331 | |
| 1332 | json_object *handle_op_info(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key) |
| 1333 | { |
| 1334 | json_object *reply = NULL; |
| 1335 | struct session_with_mutex * locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1336 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1337 | |
| 1338 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1339 | if (locked_session != NULL) { |
| 1340 | if (locked_session->hello_message != NULL) { |
| 1341 | reply = locked_session->hello_message; |
| 1342 | } else { |
| 1343 | reply = create_error("Invalid session identifier."); |
| 1344 | } |
| 1345 | } else { |
| 1346 | reply = create_error("Invalid session identifier."); |
| 1347 | } |
| 1348 | |
| 1349 | return reply; |
| 1350 | } |
| 1351 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1352 | void * thread_routine (void * arg) |
| 1353 | { |
| 1354 | void * retval = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1355 | struct pollfd fds; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1356 | json_object *request = NULL; |
| 1357 | json_object *reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1358 | int operation; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1359 | int status = 0; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1360 | const char *msgtext; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1361 | const char *session_key; |
| 1362 | const char *target = NULL; |
| 1363 | const char *source = NULL; |
| 1364 | NC_DATASTORE ds_type_t = -1; |
| 1365 | NC_DATASTORE ds_type_s = -1; |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1366 | char *chunked_out_msg = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1367 | apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool; |
| 1368 | apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list; |
| 1369 | server_rec * server = ((struct pass_to_thread*)arg)->server; |
| 1370 | int client = ((struct pass_to_thread*)arg)->client; |
| 1371 | |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1372 | char *buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1373 | |
| 1374 | while (!isterminated) { |
| 1375 | fds.fd = client; |
| 1376 | fds.events = POLLIN; |
| 1377 | fds.revents = 0; |
| 1378 | |
| 1379 | status = poll(&fds, 1, 1000); |
| 1380 | |
| 1381 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 1382 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
| 1383 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
| 1384 | continue; |
| 1385 | } else if (status < 0) { |
| 1386 | /* 0: poll time outed |
| 1387 | * close socket and ignore this request from the client, it can try it again |
| 1388 | * -1: poll failed |
| 1389 | * something wrong happend, close this socket and wait for another request |
| 1390 | */ |
| 1391 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno)); |
| 1392 | close(client); |
| 1393 | break; |
| 1394 | } |
| 1395 | /* status > 0 */ |
| 1396 | |
| 1397 | /* check the status of the socket */ |
| 1398 | |
| 1399 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 1400 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 1401 | /* close client's socket (it's probably already closed by client */ |
| 1402 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents); |
| 1403 | close(client); |
| 1404 | break; |
| 1405 | } |
| 1406 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1407 | |
| 1408 | buffer = get_framed_message(server, client); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1409 | |
| 1410 | if (buffer != NULL) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1411 | enum json_tokener_error jerr; |
| 1412 | request = json_tokener_parse_verbose(buffer, &jerr); |
| 1413 | if (jerr != json_tokener_success) { |
| 1414 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error"); |
| 1415 | continue; |
| 1416 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1417 | operation = json_object_get_int(json_object_object_get(request, "type")); |
| 1418 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1419 | session_key = json_object_get_string(json_object_object_get(request, "session")); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1420 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 1421 | if (operation != MSG_CONNECT && session_key == NULL) { |
| 1422 | reply = json_object_new_object(); |
| 1423 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1424 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification.")); |
| 1425 | msgtext = json_object_to_json_string(reply); |
| 1426 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 1427 | json_object_put(reply); |
| 1428 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 1429 | close(client); |
| 1430 | break; |
| 1431 | } |
| 1432 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1433 | /* null global JSON error-reply */ |
| 1434 | err_reply = NULL; |
| 1435 | |
| 1436 | /* prepare reply envelope */ |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1437 | reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1438 | |
| 1439 | /* process required operation */ |
| 1440 | switch (operation) { |
| 1441 | case MSG_CONNECT: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1442 | reply = handle_op_connect(server, pool, request, netconf_sessions_list); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1443 | break; |
| 1444 | case MSG_GET: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1445 | reply = handle_op_get(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1446 | break; |
| 1447 | case MSG_GETCONFIG: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1448 | reply = handle_op_getconfig(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1449 | break; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1450 | case MSG_GETSCHEMA: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1451 | reply = handle_op_getschema(server, pool, request, netconf_sessions_list, session_key); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1452 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1453 | case MSG_EDITCONFIG: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1454 | reply = handle_op_editconfig(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1455 | break; |
| 1456 | case MSG_COPYCONFIG: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1457 | reply = handle_op_copyconfig(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1458 | break; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1459 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1460 | case MSG_DELETECONFIG: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1461 | case MSG_LOCK: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1462 | case MSG_UNLOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1463 | /* get parameters */ |
| 1464 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1465 | ds_type_t = parse_datastore(target); |
| 1466 | } |
| 1467 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1468 | ds_type_s = parse_datastore(source); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | if (ds_type_t == -1) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1472 | reply = create_error("Invalid target repository type requested."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1473 | break; |
| 1474 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1475 | switch(operation) { |
| 1476 | case MSG_DELETECONFIG: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1477 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1478 | status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t); |
| 1479 | break; |
| 1480 | case MSG_LOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1481 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1482 | status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t); |
| 1483 | break; |
| 1484 | case MSG_UNLOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1485 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1486 | status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t); |
| 1487 | break; |
| 1488 | default: |
| 1489 | status = -1; |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | if (status != EXIT_SUCCESS) { |
| 1494 | if (err_reply == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1495 | /** \todo more clever error message wanted */ |
| 1496 | reply = create_error("operation failed."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1497 | } else { |
| 1498 | /* use filled err_reply from libnetconf's callback */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1499 | reply = err_reply; |
| 1500 | } |
| 1501 | } else { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1502 | reply = json_object_new_object(); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1503 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1504 | } |
| 1505 | break; |
| 1506 | case MSG_KILL: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1507 | reply = handle_op_kill(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1508 | break; |
| 1509 | case MSG_DISCONNECT: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1510 | reply = handle_op_disconnect(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1511 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1512 | case MSG_RELOADHELLO: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1513 | reply = handle_op_reloadhello(server, pool, request, netconf_sessions_list, session_key); |
| 1514 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1515 | case MSG_INFO: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1516 | reply = handle_op_info(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1517 | break; |
| 1518 | case MSG_GENERIC: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1519 | reply = handle_op_generic(server, pool, request, netconf_sessions_list, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1520 | break; |
| 1521 | default: |
| 1522 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1523 | reply = create_error("Operation not supported."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1524 | break; |
| 1525 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1526 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1527 | json_object_put(request); |
| 1528 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame^] | 1529 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object."); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1530 | /* send reply to caller */ |
| 1531 | if (reply != NULL) { |
| 1532 | msgtext = json_object_to_json_string(reply); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1533 | if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1534 | if (buffer != NULL) { |
| 1535 | free(buffer); |
| 1536 | buffer = NULL; |
| 1537 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1538 | break; |
| 1539 | } |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1540 | send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1541 | json_object_put(reply); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1542 | free(chunked_out_msg); |
| 1543 | chunked_out_msg = NULL; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1544 | if (buffer != NULL) { |
| 1545 | free(buffer); |
| 1546 | buffer = NULL; |
| 1547 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1548 | } else { |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1549 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be..."); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1550 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1554 | free (arg); |
| 1555 | |
| 1556 | return retval; |
| 1557 | } |
| 1558 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1559 | /** |
| 1560 | * \brief Close all open NETCONF sessions. |
| 1561 | * |
| 1562 | * During termination of mod_netconf, it is useful to close all remaining |
| 1563 | * sessions. This function iterates over the list of sessions and close them |
| 1564 | * all. |
| 1565 | * |
| 1566 | * \param[in] server pointer to server_rec for logging |
| 1567 | * \param[in] p apr pool needed for hash table iterating |
| 1568 | * \param[in] ht hash table of session_with_mutex structs |
| 1569 | */ |
| 1570 | static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht) |
| 1571 | { |
| 1572 | apr_hash_index_t *hi; |
| 1573 | void *val = NULL; |
| 1574 | struct session_with_mutex *swm = NULL; |
| 1575 | struct nc_session *ns = NULL; |
| 1576 | const char *hashed_key = NULL; |
| 1577 | apr_ssize_t hashed_key_length; |
| 1578 | |
| 1579 | for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) { |
| 1580 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1581 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key); |
| 1582 | swm = (struct session_with_mutex *) val; |
| 1583 | if (swm != NULL) { |
| 1584 | pthread_mutex_lock(&swm->lock); |
| 1585 | if (swm->session != NULL) { |
| 1586 | ns = swm->session; |
| 1587 | nc_session_close(ns, NC_SESSION_TERM_CLOSED); |
| 1588 | nc_session_free(ns); |
| 1589 | swm->session = NULL; |
| 1590 | } |
| 1591 | pthread_mutex_unlock(&swm->lock); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht) |
| 1597 | { |
| 1598 | apr_hash_index_t *hi; |
| 1599 | void *val = NULL; |
| 1600 | struct nc_session *ns = NULL; |
| 1601 | struct session_with_mutex *swm = NULL; |
| 1602 | const char *hashed_key = NULL; |
| 1603 | apr_ssize_t hashed_key_length; |
| 1604 | apr_time_t current_time = apr_time_now(); |
| 1605 | |
| 1606 | for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) { |
| 1607 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1608 | swm = (struct session_with_mutex *) val; |
| 1609 | if (swm == NULL) { |
| 1610 | continue; |
| 1611 | } |
| 1612 | ns = swm->session; |
| 1613 | if (ns == NULL) { |
| 1614 | continue; |
| 1615 | } |
| 1616 | pthread_mutex_lock(&swm->lock); |
| 1617 | if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) { |
| 1618 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key); |
| 1619 | nc_session_close(ns, NC_SESSION_TERM_CLOSED); |
| 1620 | nc_session_free (ns); |
| 1621 | ns = NULL; |
| 1622 | /* remove session from the active sessions list */ |
| 1623 | apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL); |
| 1624 | } |
| 1625 | pthread_mutex_unlock(&swm->lock); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | |
| 1630 | /** |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1631 | * This is actually implementation of NETCONF client |
| 1632 | * - requests are received from UNIX socket in the predefined format |
| 1633 | * - results are replied through the same way |
| 1634 | * - the daemon run as a separate process, but it is started and stopped |
| 1635 | * automatically by Apache. |
| 1636 | * |
| 1637 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1638 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 1639 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1640 | struct timeval tv; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1641 | struct sockaddr_un local, remote; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1642 | int lsock, client, ret, i, pthread_count = 0; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1643 | unsigned int olds = 0, timediff = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1644 | socklen_t len; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1645 | mod_netconf_cfg *cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1646 | apr_hash_t *netconf_sessions_list; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1647 | struct pass_to_thread * arg; |
| 1648 | pthread_t * ptids = calloc (1,sizeof(pthread_t)); |
| 1649 | struct timespec maxtime; |
| 1650 | pthread_rwlockattr_t lock_attrs; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1651 | #ifdef WITH_NOTIFICATIONS |
| 1652 | char use_notifications = 0; |
| 1653 | #endif |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1654 | |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1655 | /* wait at most 5 seconds for every thread to terminate */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1656 | maxtime.tv_sec = 5; |
| 1657 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1658 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1659 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1660 | unixd_setup_child(); |
| 1661 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1662 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 1663 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1664 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 1665 | return; |
| 1666 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1667 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1668 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1669 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 1670 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 1671 | return; |
| 1672 | } |
| 1673 | |
| 1674 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1675 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1676 | unlink(local.sun_path); |
| 1677 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 1678 | |
| 1679 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 1680 | if (errno == EADDRINUSE) { |
| 1681 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 1682 | close(lsock); |
| 1683 | exit(0); |
| 1684 | } |
| 1685 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 1686 | close(lsock); |
| 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 1691 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 1692 | close(lsock); |
| 1693 | return; |
| 1694 | } |
| 1695 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 1696 | /* prepare internal lists */ |
| 1697 | netconf_sessions_list = apr_hash_make(pool); |
| 1698 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1699 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 1700 | if (notification_init(pool, server, netconf_sessions_list) == -1) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1701 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed"); |
| 1702 | use_notifications = 0; |
| 1703 | } else { |
| 1704 | use_notifications = 1; |
| 1705 | } |
| 1706 | #endif |
| 1707 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1708 | /* setup libnetconf's callbacks */ |
| 1709 | nc_verbosity(NC_VERB_DEBUG); |
| 1710 | clb_print_server = server; |
| 1711 | nc_callback_print(clb_print); |
| 1712 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 1713 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 1714 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 1715 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1716 | |
| 1717 | /* disable publickey authentication */ |
| 1718 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 1719 | |
Tomas Cejka | 44e71db | 2013-04-21 15:50:47 +0200 | [diff] [blame] | 1720 | ncdflt_set_basic_mode(NCWD_MODE_ALL); |
| 1721 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1722 | /* create mutex protecting session list */ |
| 1723 | pthread_rwlockattr_init(&lock_attrs); |
| 1724 | /* rwlock is shared only with threads in this process */ |
| 1725 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 1726 | /* create rw lock */ |
| 1727 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
| 1728 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
| 1729 | close (lsock); |
| 1730 | return; |
| 1731 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 1732 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1733 | fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1734 | while (isterminated == 0) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1735 | gettimeofday(&tv, NULL); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1736 | timediff = (unsigned int)tv.tv_sec - olds; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1737 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1738 | if (timediff > 60) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1739 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications"); |
| 1740 | } |
| 1741 | if (use_notifications == 1) { |
| 1742 | notification_handle(); |
| 1743 | } |
| 1744 | #endif |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1745 | if (timediff > ACTIVITY_CHECK_INTERVAL) { |
| 1746 | check_timeout_and_close(server, pool, netconf_sessions_list); |
| 1747 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* open incoming connection if any */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1750 | len = sizeof(remote); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1751 | if (((unsigned int)tv.tv_sec - olds) > 60) { |
| 1752 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client"); |
| 1753 | olds = tv.tv_sec; |
| 1754 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1755 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1756 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 1757 | apr_sleep(SLEEP_TIME); |
| 1758 | continue; |
| 1759 | } else if (client == -1 && (errno == EINTR)) { |
| 1760 | continue; |
| 1761 | } else if (client == -1) { |
| 1762 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 1763 | continue; |
| 1764 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1765 | |
| 1766 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1767 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1768 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1769 | arg = malloc (sizeof(struct pass_to_thread)); |
| 1770 | arg->client = client; |
| 1771 | arg->pool = pool; |
| 1772 | arg->server = server; |
| 1773 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1774 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1775 | /* start new thread. It will serve this particular request and then terminate */ |
| 1776 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) { |
| 1777 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret); |
| 1778 | } else { |
| 1779 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]); |
| 1780 | pthread_count++; |
| 1781 | ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1)); |
| 1782 | ptids[pthread_count] = 0; |
| 1783 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1784 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1785 | /* check if some thread already terminated, free some resources by joining it */ |
| 1786 | for (i=0; i<pthread_count; i++) { |
| 1787 | if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) { |
| 1788 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg); |
| 1789 | pthread_count--; |
| 1790 | if (pthread_count > 0) { |
| 1791 | /* place last Thread ID on the place of joined one */ |
| 1792 | ptids[i] = ptids[pthread_count]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1793 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1794 | } |
| 1795 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1796 | 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] | 1797 | } |
| 1798 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1799 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating..."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1800 | /* join all threads */ |
| 1801 | for (i=0; i<pthread_count; i++) { |
| 1802 | pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime); |
| 1803 | } |
| 1804 | free (ptids); |
| 1805 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1806 | close(lsock); |
| 1807 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1808 | #ifdef WITH_NOTIFICATIONS |
| 1809 | notification_close(); |
| 1810 | #endif |
| 1811 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1812 | /* close all NETCONF sessions */ |
| 1813 | close_all_nc_sessions(server, pool, netconf_sessions_list); |
| 1814 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1815 | /* destroy rwlock */ |
| 1816 | pthread_rwlock_destroy(&session_lock); |
| 1817 | pthread_rwlockattr_destroy(&lock_attrs); |
| 1818 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1819 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 1820 | |
| 1821 | exit(APR_SUCCESS); |
| 1822 | } |
| 1823 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1824 | 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] | 1825 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1826 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 1827 | |
| 1828 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 1829 | apr_pool_create(&config->pool, pool); |
| 1830 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1831 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1832 | |
| 1833 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 1837 | apr_pool_t * plog, server_rec * s) |
| 1838 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1839 | mod_netconf_cfg *config; |
| 1840 | apr_status_t res; |
| 1841 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1842 | /* These two help ensure that we only init once. */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1843 | void *data = NULL; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1844 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1845 | |
| 1846 | /* |
| 1847 | * The following checks if this routine has been called before. |
| 1848 | * This is necessary because the parent process gets initialized |
| 1849 | * a couple of times as the server starts up. |
| 1850 | */ |
| 1851 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 1852 | if (!data) { |
| 1853 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 1854 | return (OK); |
| 1855 | } |
| 1856 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1857 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 1858 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 1859 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1860 | if (config && config->forkproc == NULL) { |
| 1861 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 1862 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1863 | switch (res) { |
| 1864 | case APR_INCHILD: |
| 1865 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1866 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1867 | apr_signal(SIGTERM, signal_handler); |
| 1868 | |
| 1869 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1870 | 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] | 1871 | |
| 1872 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1873 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1874 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1875 | /* I never should be here, wtf?!? */ |
| 1876 | 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] | 1877 | exit(APR_EGENERAL); |
| 1878 | break; |
| 1879 | case APR_INPARENT: |
| 1880 | /* 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] | 1881 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1882 | break; |
| 1883 | default: |
| 1884 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 1885 | break; |
| 1886 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1887 | } else { |
| 1888 | 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] | 1889 | } |
| 1890 | |
| 1891 | return OK; |
| 1892 | } |
| 1893 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1894 | /** |
| 1895 | * Register module hooks |
| 1896 | */ |
| 1897 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 1898 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1899 | 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] | 1900 | } |
| 1901 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1902 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 1903 | { |
| 1904 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 1905 | return NULL; |
| 1906 | } |
| 1907 | |
| 1908 | static const command_rec netconf_cmds[] = { |
| 1909 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 1910 | {NULL} |
| 1911 | }; |
| 1912 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1913 | /* Dispatch list for API hooks */ |
| 1914 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 1915 | STANDARD20_MODULE_STUFF, |
| 1916 | NULL, /* create per-dir config structures */ |
| 1917 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1918 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1919 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1920 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1921 | mod_netconf_register_hooks /* register hooks */ |
| 1922 | }; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1923 | |