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 | |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 81 | /* timeout in msec */ |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 82 | #define NCWITHDEFAULTS NCWD_MODE_DISABLED |
| 83 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 84 | struct timeval timeout = { 1, 0 }; |
| 85 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 86 | typedef enum MSG_TYPE { |
| 87 | REPLY_OK, |
| 88 | REPLY_DATA, |
| 89 | REPLY_ERROR, |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 90 | REPLY_INFO, |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 91 | MSG_CONNECT, |
| 92 | MSG_DISCONNECT, |
| 93 | MSG_GET, |
| 94 | MSG_GETCONFIG, |
| 95 | MSG_EDITCONFIG, |
| 96 | MSG_COPYCONFIG, |
| 97 | MSG_DELETECONFIG, |
| 98 | MSG_LOCK, |
| 99 | MSG_UNLOCK, |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 100 | MSG_KILL, |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 101 | MSG_INFO, |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame^] | 102 | MSG_GENERIC, |
| 103 | MSG_GETSCHEMA |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 104 | } MSG_TYPE; |
| 105 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 106 | #define MSG_OK 0 |
| 107 | #define MSG_OPEN 1 |
| 108 | #define MSG_DATA 2 |
| 109 | #define MSG_CLOSE 3 |
| 110 | #define MSG_ERROR 4 |
| 111 | #define MSG_UNKNOWN 5 |
| 112 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 113 | module AP_MODULE_DECLARE_DATA netconf_module; |
| 114 | |
| 115 | typedef struct { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 116 | apr_pool_t *pool; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 117 | apr_proc_t *forkproc; |
| 118 | char* sockname; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 119 | } mod_netconf_cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 120 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 121 | struct pass_to_thread { |
| 122 | int client; /**< opened socket */ |
| 123 | apr_pool_t * pool; /**< ?? */ |
| 124 | server_rec * server; /**< ?? */ |
| 125 | apr_hash_t * netconf_sessions_list; /**< ?? */ |
| 126 | }; |
| 127 | |
| 128 | struct session_with_mutex { |
| 129 | struct nc_session * session; /**< netconf session */ |
| 130 | pthread_mutex_t lock; /**< mutex protecting the session from multiple access */ |
| 131 | }; |
| 132 | |
| 133 | pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */ |
| 134 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 135 | volatile int isterminated = 0; |
| 136 | |
| 137 | static char* password; |
| 138 | |
| 139 | |
| 140 | static void signal_handler(int sign) |
| 141 | { |
| 142 | switch (sign) { |
| 143 | case SIGTERM: |
| 144 | isterminated = 1; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 149 | 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] | 150 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 151 | unsigned char hash_raw[APR_SHA1_DIGESTSIZE]; |
| 152 | int i; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 153 | char* hash; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 154 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 155 | apr_sha1_ctx_t sha1_ctx; |
| 156 | apr_sha1_init(&sha1_ctx); |
| 157 | apr_sha1_update(&sha1_ctx, hostname, strlen(hostname)); |
| 158 | apr_sha1_update(&sha1_ctx, port, strlen(port)); |
| 159 | apr_sha1_update(&sha1_ctx, sid, strlen(sid)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 160 | apr_sha1_final(hash_raw, &sha1_ctx); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 161 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 162 | /* convert binary hash into hex string, which is printable */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 163 | hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 164 | for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 165 | snprintf(hash + (2*i), 3, "%02x", hash_raw[i]); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 166 | } |
| 167 | //hash[2*APR_SHA1_DIGESTSIZE] = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 168 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 169 | return (hash); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint) |
| 173 | { |
| 174 | /* always approve */ |
| 175 | return (EXIT_SUCCESS); |
| 176 | } |
| 177 | |
| 178 | char* netconf_callback_sshauth_password (const char* username, const char* hostname) |
| 179 | { |
| 180 | char* buf; |
| 181 | |
| 182 | buf = malloc ((strlen(password) + 1) * sizeof(char)); |
| 183 | apr_cpystrn(buf, password, strlen(password) + 1); |
| 184 | |
| 185 | return (buf); |
| 186 | } |
| 187 | |
| 188 | void netconf_callback_sshauth_interactive (const char* name, |
| 189 | int name_len, |
| 190 | const char* instruction, |
| 191 | int instruction_len, |
| 192 | int num_prompts, |
| 193 | const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, |
| 194 | LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, |
| 195 | void** abstract) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i=0; i<num_prompts; i++) { |
| 200 | responses[i].text = malloc ((strlen(password) + 1) * sizeof(char)); |
| 201 | apr_cpystrn(responses[i].text, password, strlen(password) + 1); |
| 202 | responses[i].length = strlen(responses[i].text) + 1; |
| 203 | } |
| 204 | |
| 205 | return; |
| 206 | } |
| 207 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 208 | static json_object *err_reply = NULL; |
| 209 | void netconf_callback_error_process(const char* tag, |
| 210 | const char* type, |
| 211 | const char* severity, |
| 212 | const char* apptag, |
| 213 | const char* path, |
| 214 | const char* message, |
| 215 | const char* attribute, |
| 216 | const char* element, |
| 217 | const char* ns, |
| 218 | const char* sid) |
| 219 | { |
| 220 | err_reply = json_object_new_object(); |
| 221 | json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR)); |
| 222 | if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag)); |
| 223 | if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type)); |
| 224 | if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity)); |
| 225 | if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag)); |
| 226 | if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path)); |
| 227 | if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message)); |
| 228 | if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute)); |
| 229 | if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element)); |
| 230 | if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns)); |
| 231 | if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid)); |
| 232 | } |
| 233 | |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 234 | 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] | 235 | { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 236 | struct nc_session* session; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 237 | struct session_with_mutex * locked_session; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 238 | char *session_key; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 239 | |
| 240 | /* connect to the requested NETCONF server */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 241 | password = (char*)pass; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 242 | session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts); |
| 243 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 244 | /* if connected successful, add session to the list */ |
| 245 | if (session != NULL) { |
| 246 | /* generate hash for the session */ |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 247 | session_key = gen_ncsession_hash( |
| 248 | (host==NULL) ? "localhost" : host, |
| 249 | (port==NULL) ? "830" : port, |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 250 | nc_session_get_id(session)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 251 | |
| 252 | if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) { |
| 253 | nc_session_free(session); |
| 254 | free (locked_session); |
| 255 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno)); |
| 256 | return NULL; |
| 257 | } |
| 258 | locked_session->session = session; |
| 259 | pthread_mutex_init (&locked_session->lock, NULL); |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 260 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 261 | /* get exclusive access to sessions_list (conns) */ |
| 262 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
| 263 | nc_session_free(session); |
| 264 | free (locked_session); |
| 265 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 266 | return NULL; |
| 267 | } |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 268 | 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] | 269 | 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] | 270 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 271 | /* end of critical section */ |
| 272 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 273 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 274 | return NULL; |
| 275 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 276 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 277 | return (session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 278 | } else { |
| 279 | 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] | 280 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 285 | 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] | 286 | { |
| 287 | struct nc_session *ns = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 288 | struct session_with_mutex * locked_session; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 289 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 290 | 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] | 291 | /* get exclusive (write) access to sessions_list (conns) */ |
| 292 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
| 293 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 294 | return EXIT_FAILURE; |
| 295 | } |
| 296 | locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 297 | if (locked_session != NULL) { |
| 298 | pthread_mutex_destroy(&locked_session->lock); |
| 299 | ns = locked_session->session; |
| 300 | free (locked_session); |
| 301 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 302 | if (ns != NULL) { |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 303 | nc_session_close (ns, NC_SESSION_TERM_CLOSED); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 304 | nc_session_free (ns); |
| 305 | ns = NULL; |
| 306 | |
| 307 | /* remove session from the active sessions list */ |
| 308 | apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 309 | /* end of critical section */ |
| 310 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 311 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 312 | return EXIT_FAILURE; |
| 313 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 314 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed"); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 315 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 316 | return (EXIT_SUCCESS); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 317 | } else { |
Tomas Cejka | 6c4609b | 2012-10-12 22:29:47 +0200 | [diff] [blame] | 318 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 319 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 320 | return EXIT_FAILURE; |
| 321 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 322 | 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] | 323 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 327 | 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] | 328 | { |
| 329 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 330 | struct session_with_mutex * locked_session; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 331 | nc_reply* reply; |
| 332 | int retval = EXIT_SUCCESS; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 333 | NC_MSG_TYPE msgt; |
| 334 | NC_REPLY_TYPE replyt; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 335 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 336 | /* check requests */ |
| 337 | if (rpc == NULL) { |
| 338 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
| 339 | return (EXIT_FAILURE); |
| 340 | } |
| 341 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 342 | /* get non-exclusive (read) access to sessions_list (conns) */ |
| 343 | if (pthread_rwlock_rdlock (&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 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 347 | /* get session where send the RPC */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 348 | locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 349 | if (locked_session != NULL) { |
| 350 | session = locked_session->session; |
| 351 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 352 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 353 | /* get exclusive access to session */ |
| 354 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 355 | /* unlock before returning error */ |
| 356 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 357 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 358 | return EXIT_FAILURE; |
| 359 | } |
| 360 | return EXIT_FAILURE; |
| 361 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 362 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 363 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 364 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 365 | /* first release exclusive lock for this session */ |
| 366 | pthread_mutex_unlock(&locked_session->lock); |
| 367 | /* end of critical section */ |
| 368 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 369 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 370 | return EXIT_FAILURE; |
| 371 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 372 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 373 | /* process the result of the operation */ |
| 374 | switch (msgt) { |
| 375 | case NC_MSG_UNKNOWN: |
| 376 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 377 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 378 | netconf_close(server, conns, session_key); |
| 379 | return (EXIT_FAILURE); |
| 380 | } |
| 381 | /* no break */ |
| 382 | case NC_MSG_NONE: |
| 383 | /* there is error handled by callback */ |
| 384 | return (EXIT_FAILURE); |
| 385 | break; |
| 386 | case NC_MSG_REPLY: |
| 387 | switch (replyt = nc_reply_get_type(reply)) { |
| 388 | case NC_REPLY_OK: |
| 389 | retval = EXIT_SUCCESS; |
| 390 | break; |
| 391 | default: |
| 392 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 393 | retval = EXIT_FAILURE; |
| 394 | break; |
| 395 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 396 | break; |
| 397 | default: |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 398 | 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] | 399 | retval = EXIT_FAILURE; |
| 400 | break; |
| 401 | } |
| 402 | nc_reply_free(reply); |
| 403 | return (retval); |
| 404 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 405 | /* release lock on failure */ |
| 406 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 407 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 408 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 409 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 410 | return (EXIT_FAILURE); |
| 411 | } |
| 412 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 413 | |
| 414 | 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] | 415 | { |
| 416 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 417 | struct session_with_mutex * locked_session; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 418 | nc_reply* reply = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 419 | char* data; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 420 | NC_MSG_TYPE msgt; |
| 421 | NC_REPLY_TYPE replyt; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 422 | |
| 423 | /* check requests */ |
| 424 | if (rpc == NULL) { |
| 425 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
| 426 | return (NULL); |
| 427 | } |
| 428 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 429 | /* get non-exclusive (read) access to sessions_list (conns) */ |
| 430 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
| 431 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 432 | return NULL; |
| 433 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 434 | /* get session where send the RPC */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 435 | locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 436 | if (locked_session != NULL) { |
| 437 | session = locked_session->session; |
| 438 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 439 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 440 | /* get exclusive access to session */ |
| 441 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 442 | /* unlock before returning error */ |
| 443 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 444 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 445 | return NULL; |
| 446 | } |
| 447 | return NULL; |
| 448 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 449 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 450 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 451 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 452 | /* first release exclusive lock for this session */ |
| 453 | pthread_mutex_unlock(&locked_session->lock); |
| 454 | /* end of critical section */ |
| 455 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 456 | 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] | 457 | return (NULL); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 458 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 459 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 460 | /* process the result of the operation */ |
| 461 | switch (msgt) { |
| 462 | case NC_MSG_UNKNOWN: |
| 463 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 464 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 465 | netconf_close(server, conns, session_key); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 466 | return (NULL); |
| 467 | } |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 468 | /* no break */ |
| 469 | case NC_MSG_NONE: |
| 470 | /* there is error handled by callback */ |
| 471 | return (NULL); |
| 472 | break; |
| 473 | case NC_MSG_REPLY: |
| 474 | switch (replyt = nc_reply_get_type(reply)) { |
| 475 | case NC_REPLY_DATA: |
| 476 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 477 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 478 | data = NULL; |
| 479 | } |
| 480 | break; |
| 481 | default: |
| 482 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 483 | data = NULL; |
| 484 | break; |
| 485 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 486 | break; |
| 487 | default: |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 488 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt); |
| 489 | data = NULL; |
| 490 | break; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 491 | } |
| 492 | nc_reply_free(reply); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 493 | return (data); |
| 494 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 495 | /* release lock on failure */ |
| 496 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 497 | ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 498 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 499 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 500 | return (NULL); |
| 501 | } |
| 502 | } |
| 503 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 504 | 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] | 505 | { |
| 506 | nc_rpc* rpc; |
| 507 | struct nc_filter *f = NULL; |
| 508 | char* data = NULL; |
| 509 | |
| 510 | /* create filter if set */ |
| 511 | if (filter != NULL) { |
| 512 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 513 | } |
| 514 | |
| 515 | /* create requests */ |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 516 | rpc = nc_rpc_getconfig (source, f, NCWITHDEFAULTS); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 517 | nc_filter_free(f); |
| 518 | if (rpc == NULL) { |
| 519 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 520 | return (NULL); |
| 521 | } |
| 522 | |
| 523 | data = netconf_opdata(server, conns, session_key, rpc); |
| 524 | nc_rpc_free (rpc); |
| 525 | return (data); |
| 526 | } |
| 527 | |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame^] | 528 | static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format) |
| 529 | { |
| 530 | nc_rpc* rpc; |
| 531 | char* data = NULL; |
| 532 | |
| 533 | /* create requests */ |
| 534 | rpc = nc_rpc_getschema (identifier, version, format); |
| 535 | if (rpc == NULL) { |
| 536 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 537 | return (NULL); |
| 538 | } |
| 539 | |
| 540 | data = netconf_opdata(server, conns, session_key, rpc); |
| 541 | nc_rpc_free (rpc); |
| 542 | return (data); |
| 543 | } |
| 544 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 545 | 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] | 546 | { |
| 547 | nc_rpc* rpc; |
| 548 | struct nc_filter *f = NULL; |
| 549 | char* data = NULL; |
| 550 | |
| 551 | /* create filter if set */ |
| 552 | if (filter != NULL) { |
| 553 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 554 | } |
| 555 | |
| 556 | /* create requests */ |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 557 | rpc = nc_rpc_get (f, NCWITHDEFAULTS); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 558 | nc_filter_free(f); |
| 559 | if (rpc == NULL) { |
| 560 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 561 | return (NULL); |
| 562 | } |
| 563 | |
| 564 | data = netconf_opdata(server, conns, session_key, rpc); |
| 565 | nc_rpc_free (rpc); |
| 566 | return (data); |
| 567 | } |
| 568 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 569 | 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] | 570 | { |
| 571 | nc_rpc* rpc; |
| 572 | int retval = EXIT_SUCCESS; |
| 573 | |
| 574 | /* create requests */ |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 575 | rpc = nc_rpc_copyconfig(source, target, NCWITHDEFAULTS, config); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 576 | if (rpc == NULL) { |
| 577 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 578 | return (EXIT_FAILURE); |
| 579 | } |
| 580 | |
| 581 | retval = netconf_op(server, conns, session_key, rpc); |
| 582 | nc_rpc_free (rpc); |
| 583 | return (retval); |
| 584 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 585 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 586 | 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] | 587 | { |
| 588 | nc_rpc* rpc; |
| 589 | int retval = EXIT_SUCCESS; |
| 590 | |
| 591 | /* create requests */ |
| 592 | rpc = nc_rpc_editconfig(target, defop, erropt, config); |
| 593 | if (rpc == NULL) { |
| 594 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 595 | return (EXIT_FAILURE); |
| 596 | } |
| 597 | |
| 598 | retval = netconf_op(server, conns, session_key, rpc); |
| 599 | nc_rpc_free (rpc); |
| 600 | return (retval); |
| 601 | } |
| 602 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 603 | 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] | 604 | { |
| 605 | nc_rpc* rpc; |
| 606 | int retval = EXIT_SUCCESS; |
| 607 | |
| 608 | /* create requests */ |
| 609 | rpc = nc_rpc_killsession(sid); |
| 610 | if (rpc == NULL) { |
| 611 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 612 | return (EXIT_FAILURE); |
| 613 | } |
| 614 | |
| 615 | retval = netconf_op(server, conns, session_key, rpc); |
| 616 | nc_rpc_free (rpc); |
| 617 | return (retval); |
| 618 | } |
| 619 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 620 | 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] | 621 | { |
| 622 | nc_rpc* rpc; |
| 623 | int retval = EXIT_SUCCESS; |
| 624 | |
| 625 | /* create requests */ |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 626 | rpc = op_func(target); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 627 | if (rpc == NULL) { |
| 628 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 629 | return (EXIT_FAILURE); |
| 630 | } |
| 631 | |
| 632 | retval = netconf_op(server, conns, session_key, rpc); |
| 633 | nc_rpc_free (rpc); |
| 634 | return (retval); |
| 635 | } |
| 636 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 637 | 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] | 638 | { |
| 639 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig)); |
| 640 | } |
| 641 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 642 | 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] | 643 | { |
| 644 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock)); |
| 645 | } |
| 646 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 647 | 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] | 648 | { |
| 649 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock)); |
| 650 | } |
| 651 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 652 | /** |
| 653 | * @return REPLY_OK: 0, *data == NULL |
| 654 | * REPLY_DATA: 0, *data != NULL |
| 655 | * REPLY_ERROR: 1, *data == NULL |
| 656 | */ |
| 657 | static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data) |
| 658 | { |
| 659 | struct nc_session *session = NULL; |
| 660 | nc_reply* reply; |
| 661 | nc_rpc* rpc; |
| 662 | int retval = EXIT_SUCCESS; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 663 | NC_MSG_TYPE msgt; |
| 664 | NC_REPLY_TYPE replyt; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 665 | |
| 666 | /* create requests */ |
| 667 | rpc = nc_rpc_generic(content); |
| 668 | if (rpc == NULL) { |
| 669 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 670 | return (EXIT_FAILURE); |
| 671 | } |
| 672 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 673 | if (data != NULL) { |
| 674 | *data = NULL; |
| 675 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 676 | |
| 677 | /* get session where send the RPC */ |
| 678 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 679 | if (session != NULL) { |
| 680 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 681 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 682 | nc_rpc_free (rpc); |
| 683 | |
| 684 | /* process the result of the operation */ |
| 685 | switch (msgt) { |
| 686 | case NC_MSG_UNKNOWN: |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 687 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 688 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 689 | netconf_close(server, conns, session_key); |
| 690 | return (EXIT_FAILURE); |
| 691 | } |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 692 | /* no break */ |
| 693 | case NC_MSG_NONE: |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 694 | /* there is error handled by callback */ |
| 695 | return (EXIT_FAILURE); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 696 | break; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 697 | case NC_MSG_REPLY: |
| 698 | switch (replyt = nc_reply_get_type(reply)) { |
| 699 | case NC_REPLY_DATA: |
| 700 | if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) { |
| 701 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 702 | nc_reply_free(reply); |
| 703 | return (EXIT_FAILURE); |
| 704 | } |
| 705 | retval = EXIT_SUCCESS; |
| 706 | break; |
| 707 | case NC_REPLY_OK: |
| 708 | retval = EXIT_SUCCESS; |
| 709 | break; |
| 710 | default: |
| 711 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 712 | retval = EXIT_FAILURE; |
| 713 | break; |
| 714 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 715 | break; |
| 716 | default: |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 717 | 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] | 718 | retval = EXIT_FAILURE; |
| 719 | break; |
| 720 | } |
| 721 | nc_reply_free(reply); |
| 722 | |
| 723 | return (retval); |
| 724 | } else { |
| 725 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 726 | return (EXIT_FAILURE); |
| 727 | } |
| 728 | } |
| 729 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 730 | server_rec* clb_print_server; |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 731 | void clb_print(NC_VERB_LEVEL level, const char* msg) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 732 | { |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 733 | switch (level) { |
| 734 | case NC_VERB_ERROR: |
| 735 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg); |
| 736 | break; |
| 737 | case NC_VERB_WARNING: |
| 738 | ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg); |
| 739 | break; |
| 740 | case NC_VERB_VERBOSE: |
| 741 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg); |
| 742 | break; |
| 743 | case NC_VERB_DEBUG: |
| 744 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg); |
| 745 | break; |
| 746 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 747 | } |
| 748 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 749 | void * thread_routine (void * arg) |
| 750 | { |
| 751 | void * retval = NULL; |
| 752 | |
| 753 | ssize_t buffer_len; |
| 754 | struct pollfd fds; |
| 755 | int status, buffer_size, ret; |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 756 | json_object *request, *reply, *json_obj, *capabilities; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 757 | int operation; |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 758 | int i, chunk_len, len = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 759 | char* session_key, *data; |
| 760 | const char *host, *port, *user, *pass; |
| 761 | const char *msgtext, *cpbltstr; |
| 762 | const char *target, *source, *filter, *config, *defop, *erropt, *sid; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame^] | 763 | char *identifier, *version, *format; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 764 | struct nc_session *session = NULL; |
Kupka David | da134a1 | 2012-09-06 14:12:16 +0200 | [diff] [blame] | 765 | struct session_with_mutex * locked_session; |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 766 | struct nc_cpblts* cpblts = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 767 | NC_DATASTORE ds_type_s, ds_type_t; |
| 768 | NC_EDIT_DEFOP_TYPE defop_type = 0; |
| 769 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
| 770 | |
| 771 | apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool; |
| 772 | apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list; |
| 773 | server_rec * server = ((struct pass_to_thread*)arg)->server; |
| 774 | int client = ((struct pass_to_thread*)arg)->client; |
| 775 | |
| 776 | char * buffer, chunk_len_str[12], *chunked_msg; |
| 777 | char c; |
| 778 | |
| 779 | while (!isterminated) { |
| 780 | fds.fd = client; |
| 781 | fds.events = POLLIN; |
| 782 | fds.revents = 0; |
| 783 | |
| 784 | status = poll(&fds, 1, 1000); |
| 785 | |
| 786 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 787 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
| 788 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
| 789 | continue; |
| 790 | } else if (status < 0) { |
| 791 | /* 0: poll time outed |
| 792 | * close socket and ignore this request from the client, it can try it again |
| 793 | * -1: poll failed |
| 794 | * something wrong happend, close this socket and wait for another request |
| 795 | */ |
| 796 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno)); |
| 797 | close(client); |
| 798 | break; |
| 799 | } |
| 800 | /* status > 0 */ |
| 801 | |
| 802 | /* check the status of the socket */ |
| 803 | |
| 804 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 805 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 806 | /* close client's socket (it's probably already closed by client */ |
| 807 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents); |
| 808 | close(client); |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | /* read json in chunked framing */ |
| 813 | buffer_size = 0; |
| 814 | buffer_len = 0; |
| 815 | buffer = NULL; |
| 816 | while (1) { |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 817 | /* read chunk length */ |
| 818 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 819 | free (buffer); |
| 820 | buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 821 | break; |
| 822 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 823 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 824 | free (buffer); |
| 825 | buffer = NULL; |
| 826 | break; |
| 827 | } |
| 828 | i=0; |
| 829 | memset (chunk_len_str, 0, 12); |
| 830 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 831 | if (i==0 && c == '#') { |
| 832 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 833 | /* end but invalid */ |
| 834 | free (buffer); |
| 835 | buffer = NULL; |
| 836 | } |
| 837 | /* end of message, double-loop break */ |
| 838 | goto msg_complete; |
| 839 | } |
| 840 | chunk_len_str[i++] = c; |
| 841 | } |
| 842 | if (c != '\n') { |
| 843 | free (buffer); |
| 844 | buffer = NULL; |
| 845 | break; |
| 846 | } |
| 847 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 848 | free (buffer); |
| 849 | buffer = NULL; |
| 850 | break; |
| 851 | } |
| 852 | buffer_size += chunk_len+1; |
| 853 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 854 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 855 | free (buffer); |
| 856 | buffer = NULL; |
| 857 | break; |
| 858 | } |
| 859 | buffer_len += ret; |
| 860 | } |
| 861 | msg_complete: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 862 | |
| 863 | if (buffer != NULL) { |
| 864 | request = json_tokener_parse(buffer); |
| 865 | operation = json_object_get_int(json_object_object_get(request, "type")); |
| 866 | |
| 867 | session_key = (char*) json_object_get_string(json_object_object_get(request, "session")); |
| 868 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 869 | if (operation != MSG_CONNECT && session_key == NULL) { |
| 870 | reply = json_object_new_object(); |
| 871 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 872 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification.")); |
| 873 | msgtext = json_object_to_json_string(reply); |
| 874 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 875 | json_object_put(reply); |
| 876 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 877 | close(client); |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | /* get parameters */ |
| 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 | } |
| 1013 | version = json_object_get_string(json_object_object_get(request, "identifier")); |
| 1014 | format = json_object_get_string(json_object_object_get(request, "identifier")); |
| 1015 | |
| 1016 | |
| 1017 | if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) { |
| 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 { |
| 1048 | defop_type = 0; |
| 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 | |
| 1081 | if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) { |
| 1082 | if (err_reply == NULL) { |
| 1083 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1084 | json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed.")); |
| 1085 | } else { |
| 1086 | /* use filled err_reply from libnetconf's callback */ |
| 1087 | json_object_put(reply); |
| 1088 | reply = err_reply; |
| 1089 | } |
| 1090 | } else { |
| 1091 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1092 | } |
| 1093 | break; |
| 1094 | case MSG_COPYCONFIG: |
| 1095 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key); |
| 1096 | config = NULL; |
| 1097 | |
| 1098 | if (source == NULL) { |
| 1099 | /* no explicit source specified -> use config data */ |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 1100 | ds_type_s = NC_DATASTORE_CONFIG; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1101 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1102 | } else if (ds_type_s == -1) { |
| 1103 | /* source datastore specified, but it is invalid */ |
| 1104 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1105 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
| 1106 | break; |
| 1107 | } |
| 1108 | |
| 1109 | if (ds_type_t == -1) { |
| 1110 | /* invalid target datastore specified */ |
| 1111 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1112 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | if (source == NULL && config == NULL) { |
| 1117 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1118 | json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required.")); |
| 1119 | } else { |
| 1120 | if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) { |
| 1121 | if (err_reply == NULL) { |
| 1122 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1123 | json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed.")); |
| 1124 | } else { |
| 1125 | /* use filled err_reply from libnetconf's callback */ |
| 1126 | json_object_put(reply); |
| 1127 | reply = err_reply; |
| 1128 | } |
| 1129 | } else { |
| 1130 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1131 | } |
| 1132 | } |
| 1133 | break; |
| 1134 | case MSG_DELETECONFIG: |
| 1135 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key); |
| 1136 | /* no break - unifying code */ |
| 1137 | case MSG_LOCK: |
| 1138 | if (operation == MSG_LOCK) { |
| 1139 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key); |
| 1140 | } |
| 1141 | /* no break - unifying code */ |
| 1142 | case MSG_UNLOCK: |
| 1143 | if (operation == MSG_UNLOCK) { |
| 1144 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key); |
| 1145 | } |
| 1146 | |
| 1147 | if (ds_type_t == -1) { |
| 1148 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1149 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | switch(operation) { |
| 1154 | case MSG_DELETECONFIG: |
| 1155 | status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t); |
| 1156 | break; |
| 1157 | case MSG_LOCK: |
| 1158 | status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t); |
| 1159 | break; |
| 1160 | case MSG_UNLOCK: |
| 1161 | status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t); |
| 1162 | break; |
| 1163 | default: |
| 1164 | status = -1; |
| 1165 | break; |
| 1166 | } |
| 1167 | |
| 1168 | if (status != EXIT_SUCCESS) { |
| 1169 | if (err_reply == NULL) { |
| 1170 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1171 | json_object_object_add(reply, "error-message", json_object_new_string("operation failed.")); |
| 1172 | } else { |
| 1173 | /* use filled err_reply from libnetconf's callback */ |
| 1174 | json_object_put(reply); |
| 1175 | reply = err_reply; |
| 1176 | } |
| 1177 | } else { |
| 1178 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1179 | } |
| 1180 | break; |
| 1181 | case MSG_KILL: |
| 1182 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key); |
| 1183 | |
| 1184 | sid = json_object_get_string(json_object_object_get(request, "session-id")); |
| 1185 | |
| 1186 | if (sid == NULL) { |
| 1187 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1188 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter.")); |
| 1189 | break; |
| 1190 | } |
| 1191 | |
| 1192 | if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) { |
| 1193 | if (err_reply == NULL) { |
| 1194 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1195 | json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed.")); |
| 1196 | } else { |
| 1197 | /* use filled err_reply from libnetconf's callback */ |
| 1198 | json_object_put(reply); |
| 1199 | reply = err_reply; |
| 1200 | } |
| 1201 | } else { |
| 1202 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1203 | } |
| 1204 | break; |
| 1205 | case MSG_DISCONNECT: |
| 1206 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key); |
| 1207 | |
| 1208 | if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) { |
| 1209 | if (err_reply == NULL) { |
| 1210 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1211 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1212 | } else { |
| 1213 | /* use filled err_reply from libnetconf's callback */ |
| 1214 | json_object_put(reply); |
| 1215 | reply = err_reply; |
| 1216 | } |
| 1217 | } else { |
| 1218 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1219 | } |
| 1220 | break; |
| 1221 | case MSG_INFO: |
| 1222 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1223 | |
Kupka David | da134a1 | 2012-09-06 14:12:16 +0200 | [diff] [blame] | 1224 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1225 | if (locked_session != NULL) { |
| 1226 | session = locked_session->session; |
| 1227 | } else { |
| 1228 | session = NULL; |
| 1229 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1230 | if (session != NULL) { |
| 1231 | json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session))); |
| 1232 | json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1")); |
| 1233 | json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session))); |
| 1234 | json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session))); |
| 1235 | json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session))); |
| 1236 | cpblts = nc_session_get_cpblts (session); |
| 1237 | if (cpblts != NULL) { |
| 1238 | json_obj = json_object_new_array(); |
| 1239 | nc_cpblts_iter_start (cpblts); |
| 1240 | while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) { |
| 1241 | json_object_array_add(json_obj, json_object_new_string(cpbltstr)); |
| 1242 | } |
| 1243 | json_object_object_add(reply, "capabilities", json_obj); |
| 1244 | } |
| 1245 | } else { |
| 1246 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1247 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1248 | } |
| 1249 | |
| 1250 | break; |
| 1251 | case MSG_GENERIC: |
| 1252 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key); |
| 1253 | |
| 1254 | config = json_object_get_string(json_object_object_get(request, "content")); |
| 1255 | |
| 1256 | if (config == NULL) { |
| 1257 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1258 | json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter.")); |
| 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) { |
| 1263 | if (err_reply == NULL) { |
| 1264 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1265 | json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed.")); |
| 1266 | } else { |
| 1267 | /* use filled err_reply from libnetconf's callback */ |
| 1268 | json_object_put(reply); |
| 1269 | reply = err_reply; |
| 1270 | } |
| 1271 | } else { |
| 1272 | if (data == NULL) { |
| 1273 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1274 | } else { |
| 1275 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1276 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 1277 | } |
| 1278 | } |
| 1279 | break; |
| 1280 | default: |
| 1281 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
| 1282 | reply = json_object_new_object(); |
| 1283 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1284 | json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported.")); |
| 1285 | break; |
| 1286 | } |
| 1287 | json_object_put(request); |
| 1288 | |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1289 | /* send reply to caller */ |
| 1290 | if (reply != NULL) { |
| 1291 | msgtext = json_object_to_json_string(reply); |
| 1292 | if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) { |
| 1293 | free (buffer); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1294 | break; |
| 1295 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1296 | send(client, chunked_msg, strlen(chunked_msg) + 1, 0); |
| 1297 | json_object_put(reply); |
| 1298 | free (chunked_msg); |
| 1299 | free (buffer); |
| 1300 | } else { |
| 1301 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | free (arg); |
| 1307 | |
| 1308 | return retval; |
| 1309 | } |
| 1310 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1311 | /* |
| 1312 | * This is actually implementation of NETCONF client |
| 1313 | * - requests are received from UNIX socket in the predefined format |
| 1314 | * - results are replied through the same way |
| 1315 | * - the daemon run as a separate process, but it is started and stopped |
| 1316 | * automatically by Apache. |
| 1317 | * |
| 1318 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1319 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 1320 | { |
| 1321 | struct sockaddr_un local, remote; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1322 | int lsock, client, ret, i, pthread_count = 0; |
| 1323 | socklen_t len; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1324 | mod_netconf_cfg *cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1325 | apr_hash_t *netconf_sessions_list; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1326 | struct pass_to_thread * arg; |
| 1327 | pthread_t * ptids = calloc (1,sizeof(pthread_t)); |
| 1328 | struct timespec maxtime; |
| 1329 | pthread_rwlockattr_t lock_attrs; |
| 1330 | |
| 1331 | /* wait at most 5 secons for every thread to terminate */ |
| 1332 | maxtime.tv_sec = 5; |
| 1333 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1334 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1335 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1336 | unixd_setup_child(); |
| 1337 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1338 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 1339 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1340 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 1341 | return; |
| 1342 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1343 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1344 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1345 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 1346 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 1347 | return; |
| 1348 | } |
| 1349 | |
| 1350 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1351 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1352 | unlink(local.sun_path); |
| 1353 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 1354 | |
| 1355 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 1356 | if (errno == EADDRINUSE) { |
| 1357 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 1358 | close(lsock); |
| 1359 | exit(0); |
| 1360 | } |
| 1361 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 1362 | close(lsock); |
| 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 1367 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 1368 | close(lsock); |
| 1369 | return; |
| 1370 | } |
| 1371 | |
| 1372 | /* prepare internal lists */ |
| 1373 | netconf_sessions_list = apr_hash_make(pool); |
| 1374 | |
| 1375 | /* setup libnetconf's callbacks */ |
| 1376 | nc_verbosity(NC_VERB_DEBUG); |
| 1377 | clb_print_server = server; |
| 1378 | nc_callback_print(clb_print); |
| 1379 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 1380 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 1381 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 1382 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1383 | |
| 1384 | /* disable publickey authentication */ |
| 1385 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 1386 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1387 | /* create mutex protecting session list */ |
| 1388 | pthread_rwlockattr_init(&lock_attrs); |
| 1389 | /* rwlock is shared only with threads in this process */ |
| 1390 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 1391 | /* create rw lock */ |
| 1392 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
| 1393 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
| 1394 | close (lsock); |
| 1395 | return; |
| 1396 | } |
| 1397 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1398 | while (isterminated == 0) { |
| 1399 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request"); |
| 1400 | |
| 1401 | /* open incoming connection if any */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1402 | len = sizeof(remote); |
| 1403 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1404 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 1405 | apr_sleep(SLEEP_TIME); |
| 1406 | continue; |
| 1407 | } else if (client == -1 && (errno == EINTR)) { |
| 1408 | continue; |
| 1409 | } else if (client == -1) { |
| 1410 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 1411 | continue; |
| 1412 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1413 | |
| 1414 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1415 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1416 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1417 | arg = malloc (sizeof(struct pass_to_thread)); |
| 1418 | arg->client = client; |
| 1419 | arg->pool = pool; |
| 1420 | arg->server = server; |
| 1421 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1422 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1423 | /* start new thread. It will serve this particular request and then terminate */ |
| 1424 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) { |
| 1425 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret); |
| 1426 | } else { |
| 1427 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]); |
| 1428 | pthread_count++; |
| 1429 | ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1)); |
| 1430 | ptids[pthread_count] = 0; |
| 1431 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1432 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1433 | /* check if some thread already terminated, free some resources by joining it */ |
| 1434 | for (i=0; i<pthread_count; i++) { |
| 1435 | if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) { |
| 1436 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg); |
| 1437 | pthread_count--; |
| 1438 | if (pthread_count > 0) { |
| 1439 | /* place last Thread ID on the place of joined one */ |
| 1440 | ptids[i] = ptids[pthread_count]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1441 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1442 | } |
| 1443 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1444 | 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] | 1445 | } |
| 1446 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1447 | /* join all threads */ |
| 1448 | for (i=0; i<pthread_count; i++) { |
| 1449 | pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime); |
| 1450 | } |
| 1451 | free (ptids); |
| 1452 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1453 | close(lsock); |
| 1454 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1455 | /* destroy rwlock */ |
| 1456 | pthread_rwlock_destroy(&session_lock); |
| 1457 | pthread_rwlockattr_destroy(&lock_attrs); |
| 1458 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1459 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 1460 | |
| 1461 | exit(APR_SUCCESS); |
| 1462 | } |
| 1463 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1464 | 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] | 1465 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1466 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 1467 | |
| 1468 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 1469 | apr_pool_create(&config->pool, pool); |
| 1470 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1471 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1472 | |
| 1473 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 1477 | apr_pool_t * plog, server_rec * s) |
| 1478 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1479 | mod_netconf_cfg *config; |
| 1480 | apr_status_t res; |
| 1481 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1482 | /* These two help ensure that we only init once. */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1483 | void *data = NULL; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1484 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1485 | |
| 1486 | /* |
| 1487 | * The following checks if this routine has been called before. |
| 1488 | * This is necessary because the parent process gets initialized |
| 1489 | * a couple of times as the server starts up. |
| 1490 | */ |
| 1491 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 1492 | if (!data) { |
| 1493 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 1494 | return (OK); |
| 1495 | } |
| 1496 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1497 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 1498 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 1499 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1500 | if (config && config->forkproc == NULL) { |
| 1501 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 1502 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1503 | switch (res) { |
| 1504 | case APR_INCHILD: |
| 1505 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1506 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1507 | apr_signal(SIGTERM, signal_handler); |
| 1508 | |
| 1509 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1510 | 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] | 1511 | |
| 1512 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1513 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1514 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1515 | /* I never should be here, wtf?!? */ |
| 1516 | 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] | 1517 | exit(APR_EGENERAL); |
| 1518 | break; |
| 1519 | case APR_INPARENT: |
| 1520 | /* 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] | 1521 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1522 | break; |
| 1523 | default: |
| 1524 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 1525 | break; |
| 1526 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1527 | } else { |
| 1528 | 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] | 1529 | } |
| 1530 | |
| 1531 | return OK; |
| 1532 | } |
| 1533 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1534 | /** |
| 1535 | * Register module hooks |
| 1536 | */ |
| 1537 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 1538 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1539 | 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] | 1540 | } |
| 1541 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1542 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 1543 | { |
| 1544 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | |
| 1548 | static const command_rec netconf_cmds[] = { |
| 1549 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 1550 | {NULL} |
| 1551 | }; |
| 1552 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1553 | /* Dispatch list for API hooks */ |
| 1554 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 1555 | STANDARD20_MODULE_STUFF, |
| 1556 | NULL, /* create per-dir config structures */ |
| 1557 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1558 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1559 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1560 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1561 | mod_netconf_register_hooks /* register hooks */ |
| 1562 | }; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 1563 | |