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