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