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