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