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