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