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 | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 46 | #ifdef ARCSID |
Tomas Cejka | 689a104 | 2013-01-16 15:08:25 +0100 | [diff] [blame] | 47 | static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "ARCSID" $"; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 48 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 49 | |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 50 | #include <unistd.h> |
| 51 | #include <poll.h> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 52 | #include <sys/types.h> |
| 53 | #include <sys/socket.h> |
| 54 | #include <sys/un.h> |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 55 | #include <sys/fcntl.h> |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 56 | #include <pthread.h> |
| 57 | #include <ctype.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 58 | |
| 59 | #include <unixd.h> |
| 60 | #include <httpd.h> |
| 61 | #include <http_log.h> |
| 62 | #include <http_config.h> |
| 63 | |
| 64 | #include <apr_sha1.h> |
| 65 | #include <apr_hash.h> |
| 66 | #include <apr_signal.h> |
| 67 | #include <apr_strings.h> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 68 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 69 | #include <json/json.h> |
| 70 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 71 | #include <libnetconf.h> |
Tomas Cejka | b3cc64f | 2013-05-03 19:44:54 +0200 | [diff] [blame] | 72 | #include <libnetconf_ssh.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 73 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 74 | #ifdef WITH_NOTIFICATIONS |
| 75 | #include "notification_module.h" |
| 76 | #endif |
| 77 | |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 78 | #include "message_type.h" |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 79 | #include "mod_netconf.h" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 80 | |
| 81 | #define MAX_PROCS 5 |
Radek Krejci | 6cb0898 | 2012-07-25 18:01:06 +0200 | [diff] [blame] | 82 | #define SOCKET_FILENAME "/tmp/mod_netconf.sock" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 83 | #define MAX_SOCKET_CL 10 |
| 84 | #define BUFFER_SIZE 4096 |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 85 | #define NOTIFICATION_QUEUE_SIZE 10 |
| 86 | #define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */ |
Tomas Cejka | 04e3995 | 2013-04-19 11:49:38 +0200 | [diff] [blame] | 87 | #define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 88 | |
| 89 | /* sleep in master process for non-blocking socket reading */ |
| 90 | #define SLEEP_TIME 200 |
| 91 | |
| 92 | #ifndef offsetof |
| 93 | #define offsetof(type, member) ((size_t) ((type *) 0)->member) |
| 94 | #endif |
| 95 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 96 | server_rec *http_server = NULL; |
| 97 | |
Tomas Cejka | 027f3bc | 2012-11-10 20:28:36 +0100 | [diff] [blame] | 98 | /* timeout in msec */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 99 | struct timeval timeout = { 1, 0 }; |
| 100 | |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 101 | #define NCWITHDEFAULTS NCWD_MODE_NOTSET |
| 102 | |
| 103 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 104 | #define MSG_OK 0 |
| 105 | #define MSG_OPEN 1 |
| 106 | #define MSG_DATA 2 |
| 107 | #define MSG_CLOSE 3 |
| 108 | #define MSG_ERROR 4 |
| 109 | #define MSG_UNKNOWN 5 |
| 110 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 111 | module AP_MODULE_DECLARE_DATA netconf_module; |
| 112 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 113 | pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */ |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 114 | pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 115 | pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 116 | apr_hash_t *netconf_sessions_list = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 117 | |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 118 | static pthread_key_t notif_history_key; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 119 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 120 | volatile int isterminated = 0; |
| 121 | |
| 122 | static char* password; |
| 123 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 124 | static void signal_handler(int sign) |
| 125 | { |
| 126 | switch (sign) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 127 | case SIGINT: |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 128 | case SIGTERM: |
| 129 | isterminated = 1; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 134 | 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] | 135 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 136 | unsigned char hash_raw[APR_SHA1_DIGESTSIZE]; |
| 137 | int i; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 138 | char* hash; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 139 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 140 | apr_sha1_ctx_t sha1_ctx; |
| 141 | apr_sha1_init(&sha1_ctx); |
| 142 | apr_sha1_update(&sha1_ctx, hostname, strlen(hostname)); |
| 143 | apr_sha1_update(&sha1_ctx, port, strlen(port)); |
| 144 | apr_sha1_update(&sha1_ctx, sid, strlen(sid)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 145 | apr_sha1_final(hash_raw, &sha1_ctx); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 146 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 147 | /* convert binary hash into hex string, which is printable */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 148 | hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 149 | for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 150 | snprintf(hash + (2*i), 3, "%02x", hash_raw[i]); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 151 | } |
| 152 | //hash[2*APR_SHA1_DIGESTSIZE] = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 153 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 154 | return (hash); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint) |
| 158 | { |
| 159 | /* always approve */ |
| 160 | return (EXIT_SUCCESS); |
| 161 | } |
| 162 | |
| 163 | char* netconf_callback_sshauth_password (const char* username, const char* hostname) |
| 164 | { |
| 165 | char* buf; |
| 166 | |
| 167 | buf = malloc ((strlen(password) + 1) * sizeof(char)); |
| 168 | apr_cpystrn(buf, password, strlen(password) + 1); |
| 169 | |
| 170 | return (buf); |
| 171 | } |
| 172 | |
| 173 | void netconf_callback_sshauth_interactive (const char* name, |
| 174 | int name_len, |
| 175 | const char* instruction, |
| 176 | int instruction_len, |
| 177 | int num_prompts, |
| 178 | const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, |
| 179 | LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, |
| 180 | void** abstract) |
| 181 | { |
| 182 | int i; |
| 183 | |
| 184 | for (i=0; i<num_prompts; i++) { |
| 185 | responses[i].text = malloc ((strlen(password) + 1) * sizeof(char)); |
| 186 | apr_cpystrn(responses[i].text, password, strlen(password) + 1); |
| 187 | responses[i].length = strlen(responses[i].text) + 1; |
| 188 | } |
| 189 | |
| 190 | return; |
| 191 | } |
| 192 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 193 | static json_object *err_reply = NULL; |
| 194 | void netconf_callback_error_process(const char* tag, |
| 195 | const char* type, |
| 196 | const char* severity, |
| 197 | const char* apptag, |
| 198 | const char* path, |
| 199 | const char* message, |
| 200 | const char* attribute, |
| 201 | const char* element, |
| 202 | const char* ns, |
| 203 | const char* sid) |
| 204 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 205 | if (err_reply != NULL) { |
| 206 | json_object_put(err_reply); |
| 207 | err_reply = NULL; |
| 208 | } |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 209 | err_reply = json_object_new_object(); |
| 210 | json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR)); |
| 211 | if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag)); |
| 212 | if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type)); |
| 213 | if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity)); |
| 214 | if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag)); |
| 215 | if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path)); |
| 216 | if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message)); |
| 217 | if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute)); |
| 218 | if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element)); |
| 219 | if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns)); |
| 220 | if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid)); |
| 221 | } |
| 222 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 223 | /** |
| 224 | * should be used in locked area |
| 225 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 226 | void prepare_status_message(struct session_with_mutex *s, struct nc_session *session) |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 227 | { |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 228 | json_object *json_obj = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 229 | char *old_sid = NULL; |
| 230 | const char *j_old_sid = NULL; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 231 | const char *cpbltstr; |
| 232 | struct nc_cpblts* cpblts = NULL; |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 233 | |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 234 | if (s == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 235 | DEBUG("No session given."); |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 239 | if (s->hello_message != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 240 | DEBUG("clean previous hello message"); |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 241 | //json_object_put(s->hello_message); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 242 | j_old_sid = json_object_get_string(json_object_object_get(s->hello_message, "sid")); |
| 243 | if (j_old_sid != NULL) { |
| 244 | old_sid = strdup(j_old_sid); |
| 245 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 246 | s->hello_message = NULL; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 247 | } |
| 248 | s->hello_message = json_object_new_object(); |
| 249 | if (session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 250 | if (old_sid != NULL) { |
| 251 | /* use previous sid */ |
| 252 | json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid)); |
| 253 | free(old_sid); |
| 254 | } else { |
Tomas Cejka | f38a54c | 2013-05-27 21:57:35 +0200 | [diff] [blame] | 255 | /* we don't have old sid */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 256 | json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session))); |
| 257 | } |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 258 | json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1")); |
| 259 | json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session))); |
| 260 | json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session))); |
| 261 | json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session))); |
| 262 | cpblts = nc_session_get_cpblts (session); |
| 263 | if (cpblts != NULL) { |
| 264 | json_obj = json_object_new_array(); |
| 265 | nc_cpblts_iter_start (cpblts); |
| 266 | while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) { |
| 267 | json_object_array_add(json_obj, json_object_new_string(cpbltstr)); |
| 268 | } |
| 269 | json_object_object_add(s->hello_message, "capabilities", json_obj); |
| 270 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 271 | DEBUG("%s", json_object_to_json_string(s->hello_message)); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 272 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 273 | DEBUG("Session was not given."); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 274 | json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR)); |
| 275 | json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier.")); |
| 276 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 277 | DEBUG("Status info from hello message prepared"); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 278 | |
| 279 | } |
| 280 | |
| 281 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 282 | /** |
| 283 | * \defgroup netconf_operations NETCONF operations |
| 284 | * The list of NETCONF operations that mod_netconf supports. |
| 285 | * @{ |
| 286 | */ |
| 287 | |
| 288 | /** |
| 289 | * \brief Connect to NETCONF server |
| 290 | * |
| 291 | * \warning Session_key hash is not bound with caller identification. This could be potential security risk. |
| 292 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 293 | static char* netconf_connect(apr_pool_t* pool, 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] | 294 | { |
Tomas Cejka | ae9efe5 | 2013-04-23 13:37:36 +0200 | [diff] [blame] | 295 | struct nc_session* session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 296 | struct session_with_mutex * locked_session; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 297 | char *session_key; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 298 | |
| 299 | /* connect to the requested NETCONF server */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 300 | password = (char*)pass; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 301 | DEBUG("prepare to connect %s@%s:%s", user, host, port); |
Tomas Cejka | ae9efe5 | 2013-04-23 13:37:36 +0200 | [diff] [blame] | 302 | nc_verbosity(NC_VERB_DEBUG); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 303 | session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 304 | DEBUG("nc_session_connect done"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 305 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 306 | /* if connected successful, add session to the list */ |
| 307 | if (session != NULL) { |
| 308 | /* generate hash for the session */ |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 309 | session_key = gen_ncsession_hash( |
| 310 | (host==NULL) ? "localhost" : host, |
| 311 | (port==NULL) ? "830" : port, |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 312 | nc_session_get_id(session)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 313 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 314 | /** \todo allocate from apr_pool */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 315 | if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) { |
| 316 | nc_session_free(session); |
| 317 | free (locked_session); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 318 | DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | locked_session->session = session; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 322 | locked_session->last_activity = apr_time_now(); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 323 | locked_session->hello_message = NULL; |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 324 | locked_session->closed = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 325 | pthread_mutex_init (&locked_session->lock, NULL); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 326 | DEBUG("Before session_lock"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 327 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 328 | DEBUG("LOCK wrlock %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 329 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
| 330 | nc_session_free(session); |
| 331 | free (locked_session); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 332 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 335 | locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t)); |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 336 | locked_session->ntfc_subscribed = 0; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 337 | DEBUG("Add connection to the list"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 338 | apr_hash_set(netconf_sessions_list, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 339 | DEBUG("Before session_unlock"); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 340 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 341 | /* lock session */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 342 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 343 | pthread_mutex_lock(&locked_session->lock); |
| 344 | |
| 345 | /* unlock session list */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 346 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 347 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 348 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 351 | /* store information about session from hello message for future usage */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 352 | prepare_status_message(locked_session, session); |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 353 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 354 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 355 | pthread_mutex_unlock(&locked_session->lock); |
| 356 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 357 | DEBUG("NETCONF session established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 358 | return (session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 359 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 360 | DEBUG("Connection could not be established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 361 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 366 | static int close_and_free_session(struct session_with_mutex *locked_session) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 367 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 368 | DEBUG("lock private lock."); |
| 369 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 370 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 371 | DEBUG("Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 372 | } |
| 373 | locked_session->ntfc_subscribed = 0; |
| 374 | locked_session->closed = 1; |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 375 | nc_session_free(locked_session->session); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 376 | DEBUG("session closed."); |
| 377 | DEBUG("unlock private lock."); |
| 378 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 379 | if (pthread_mutex_unlock(&locked_session->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 380 | DEBUG("Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 383 | DEBUG("unlock session lock."); |
| 384 | DEBUG("closed session, disabled notif(?), wait 2s"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 385 | usleep(500000); /* let notification thread stop */ |
| 386 | |
| 387 | /* session shouldn't be used by now */ |
| 388 | /** \todo free all notifications from queue */ |
| 389 | apr_array_clear(locked_session->notifications); |
| 390 | pthread_mutex_destroy(&locked_session->lock); |
| 391 | if (locked_session->hello_message != NULL) { |
| 392 | /** \todo free hello_message */ |
| 393 | //json_object_put(locked_session->hello_message); |
| 394 | locked_session->hello_message = NULL; |
| 395 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 396 | locked_session->session = NULL; |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 397 | free(locked_session); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 398 | locked_session = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 399 | DEBUG("NETCONF session closed, everything cleared."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 400 | return (EXIT_SUCCESS); |
| 401 | } |
| 402 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 403 | static int netconf_close(const char* session_key, json_object **reply) |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 404 | { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 405 | struct session_with_mutex * locked_session; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 406 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 407 | DEBUG("Key in hash to close: %s", session_key); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 408 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 409 | /* get exclusive (write) access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 410 | DEBUG("lock session lock."); |
| 411 | DEBUG("LOCK wrlock %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 412 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 413 | DEBUG("Error while locking rwlock"); |
| 414 | (*reply) = create_error("Internal: Error while locking."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 415 | return EXIT_FAILURE; |
| 416 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 417 | /* get session to close */ |
| 418 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 419 | /* remove session from the active sessions list -> nobody new can now work with session */ |
| 420 | apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 421 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 422 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 423 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 424 | DEBUG("Error while unlocking rwlock"); |
| 425 | (*reply) = create_error("Internal: Error while unlocking."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if ((locked_session != NULL) && (locked_session->session != NULL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 429 | return close_and_free_session(locked_session); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 430 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 431 | DEBUG("Unknown session to close"); |
| 432 | (*reply) = create_error("Internal: Unkown session to close."); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 433 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 434 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 435 | (*reply) = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 438 | /** |
| 439 | * Test reply message type and return error message. |
| 440 | * |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 441 | * \param[in] session nc_session internal struct |
| 442 | * \param[in] session_key session key, NULL to disable disconnect on error |
| 443 | * \param[in] msgt RPC-REPLY message type |
| 444 | * \param[out] data |
| 445 | * \return NULL on success |
| 446 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 447 | json_object *netconf_test_reply(struct nc_session *session, const char *session_key, NC_MSG_TYPE msgt, nc_reply *reply, char **data) |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 448 | { |
| 449 | NC_REPLY_TYPE replyt; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 450 | json_object *err = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 451 | |
| 452 | /* process the result of the operation */ |
| 453 | switch (msgt) { |
| 454 | case NC_MSG_UNKNOWN: |
| 455 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 456 | DEBUG("mod_netconf: receiving rpc-reply failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 457 | if (session_key != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 458 | netconf_close(session_key, &err); |
| 459 | } |
| 460 | if (err != NULL) { |
| 461 | return err; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 462 | } |
| 463 | return create_error("Internal: Receiving RPC-REPLY failed."); |
| 464 | } |
| 465 | case NC_MSG_NONE: |
| 466 | /* there is error handled by callback */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 467 | if (data != NULL) { |
| 468 | free(*data); |
| 469 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 470 | (*data) = NULL; |
| 471 | return NULL; |
| 472 | case NC_MSG_REPLY: |
| 473 | switch (replyt = nc_reply_get_type(reply)) { |
| 474 | case NC_REPLY_OK: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 475 | if ((data != NULL) && (*data != NULL)) { |
| 476 | free(*data); |
| 477 | (*data) = NULL; |
| 478 | } |
| 479 | return create_ok(); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 480 | case NC_REPLY_DATA: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 481 | if (((*data) = nc_reply_get_data(reply)) == NULL) { |
| 482 | DEBUG("mod_netconf: no data from reply"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 483 | return create_error("Internal: No data from reply received."); |
| 484 | } else { |
| 485 | return NULL; |
| 486 | } |
| 487 | break; |
| 488 | case NC_REPLY_ERROR: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 489 | DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 490 | if (data != NULL) { |
| 491 | free(*data); |
| 492 | (*data) = NULL; |
| 493 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 494 | return create_error(nc_reply_get_errormsg(reply)); |
| 495 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 496 | DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt); |
| 497 | if (data != NULL) { |
| 498 | free(*data); |
| 499 | (*data) = NULL; |
| 500 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 501 | return create_error(nc_reply_get_errormsg(reply)); |
| 502 | } |
| 503 | break; |
| 504 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 505 | DEBUG("mod_netconf: unexpected reply message received (%d)", msgt); |
| 506 | if (data != NULL) { |
| 507 | free(*data); |
| 508 | (*data) = NULL; |
| 509 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 510 | return create_error("Internal: Unexpected RPC-REPLY message type."); |
| 511 | } |
| 512 | } |
| 513 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 514 | json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 515 | { |
| 516 | nc_reply* reply = NULL; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 517 | NC_MSG_TYPE msgt; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 518 | |
| 519 | /* check requests */ |
| 520 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 521 | DEBUG("mod_netconf: rpc is not created"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 522 | return create_error("Internal error: RPC is not created"); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (session != NULL) { |
| 526 | /* send the request and get the reply */ |
| 527 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 528 | /* process the result of the operation */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 529 | return netconf_test_reply(session, NULL, msgt, reply, NULL); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 530 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 531 | DEBUG("Unknown session to process."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 532 | return create_error("Internal error: Unknown session to process."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 536 | /** |
| 537 | * Perform RPC method that returns data. |
| 538 | * |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 539 | * \param[in] session_key session identifier |
| 540 | * \param[in] rpc RPC message to perform |
| 541 | * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received |
| 542 | * \return NULL on success, json object with error otherwise |
| 543 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 544 | static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 545 | { |
| 546 | struct nc_session *session = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 547 | struct session_with_mutex * locked_session; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 548 | nc_reply* reply = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 549 | json_object *res = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 550 | char *data = NULL; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 551 | NC_MSG_TYPE msgt; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 552 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 553 | /* check requests */ |
| 554 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 555 | DEBUG("mod_netconf: rpc is not created"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 556 | res = create_error("Internal: RPC could not be created."); |
| 557 | data = NULL; |
| 558 | goto finished; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 559 | } |
| 560 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 561 | /* get non-exclusive (read) access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 562 | DEBUG("LOCK wrlock %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 563 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 564 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 565 | res = create_error("Internal: Lock failed."); |
| 566 | data = NULL; |
| 567 | goto finished; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 568 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 569 | /* get session where send the RPC */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 570 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 571 | if (locked_session != NULL) { |
| 572 | session = locked_session->session; |
| 573 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 574 | if (session != NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 575 | /* get exclusive access to session */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 576 | DEBUG("LOCK mutex %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 577 | if (pthread_mutex_lock(&locked_session->lock) != 0) { |
| 578 | /* unlock before returning error */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 579 | DEBUG("UNLOCK wrlock %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 580 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 581 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 582 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 583 | res = create_error("Internal: Could not unlock."); |
| 584 | goto finished; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 585 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 586 | res = create_error("Internal: Could not unlock."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 587 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 588 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 589 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 590 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 591 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 592 | res = create_error("Internal: Could not unlock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 595 | locked_session->last_activity = apr_time_now(); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 596 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 597 | /* send the request and get the reply */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 598 | msgt = nc_session_send_recv(session, rpc, &reply); |
| 599 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 600 | /* first release exclusive lock for this session */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 601 | DEBUG("UNLOCK mutex %s", __func__); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 602 | pthread_mutex_unlock(&locked_session->lock); |
| 603 | /* end of critical section */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 604 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 605 | res = netconf_test_reply(session, session_key, msgt, reply, &data); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 606 | } else { |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 607 | /* release lock on failure */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 608 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 609 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 610 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bcdc114 | 2012-11-14 01:12:43 +0100 | [diff] [blame] | 611 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 612 | DEBUG("Unknown session to process."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 613 | res = create_error("Unknown session to process."); |
| 614 | data = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 615 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 616 | finished: |
| 617 | nc_reply_free(reply); |
| 618 | if (received_data != NULL) { |
| 619 | (*received_data) = data; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 620 | } else { |
| 621 | if (data != NULL) { |
| 622 | free(data); |
| 623 | data = NULL; |
| 624 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 625 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 626 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 629 | static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 630 | { |
| 631 | nc_rpc* rpc; |
| 632 | struct nc_filter *f = NULL; |
| 633 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 634 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 635 | |
| 636 | /* create filter if set */ |
| 637 | if (filter != NULL) { |
| 638 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 639 | } |
| 640 | |
| 641 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 642 | rpc = nc_rpc_getconfig (source, f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 643 | nc_filter_free(f); |
| 644 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 645 | DEBUG("mod_netconf: creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 646 | return (NULL); |
| 647 | } |
| 648 | |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 649 | /* tell server to show all elements even if they have default values */ |
| 650 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 651 | DEBUG("mod_netconf: setting withdefaults failed"); |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 654 | res = netconf_op(session_key, rpc, &data); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 655 | nc_rpc_free (rpc); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 656 | if (res != NULL) { |
| 657 | (*err) = res; |
| 658 | } else { |
| 659 | (*err) = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 662 | return (data); |
| 663 | } |
| 664 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 665 | static char* netconf_getschema(const char* session_key, const char* identifier, const char* version, const char* format, json_object **err) |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 666 | { |
| 667 | nc_rpc* rpc; |
| 668 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 669 | json_object *res = NULL; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 670 | |
| 671 | /* create requests */ |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 672 | rpc = nc_rpc_getschema(identifier, version, format); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 673 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 674 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 675 | return (NULL); |
| 676 | } |
| 677 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 678 | res = netconf_op(session_key, rpc, &data); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 679 | nc_rpc_free (rpc); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 680 | if (res != NULL) { |
| 681 | (*err) = res; |
| 682 | } else { |
| 683 | (*err) = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 686 | return (data); |
| 687 | } |
| 688 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 689 | static char* netconf_get(const char* session_key, const char* filter, json_object **err) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 690 | { |
| 691 | nc_rpc* rpc; |
| 692 | struct nc_filter *f = NULL; |
| 693 | char* data = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 694 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 695 | |
| 696 | /* create filter if set */ |
| 697 | if (filter != NULL) { |
| 698 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 699 | } |
| 700 | |
| 701 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 702 | rpc = nc_rpc_get (f); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 703 | nc_filter_free(f); |
| 704 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 705 | DEBUG("mod_netconf: creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 706 | return (NULL); |
| 707 | } |
| 708 | |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 709 | /* tell server to show all elements even if they have default values */ |
| 710 | if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 711 | DEBUG("mod_netconf: setting withdefaults failed"); |
Tomas Cejka | 9467466 | 2013-09-13 15:55:24 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 714 | res = netconf_op(session_key, rpc, &data); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 715 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 716 | if (res == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 717 | (*err) = res; |
| 718 | } else { |
| 719 | (*err) = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 722 | return (data); |
| 723 | } |
| 724 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 725 | static json_object *netconf_copyconfig(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] | 726 | { |
| 727 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 728 | json_object *res = NULL; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 729 | |
| 730 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 731 | if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) { |
| 732 | if (target == NC_DATASTORE_URL) { |
| 733 | rpc = nc_rpc_copyconfig(source, target, config, url); |
| 734 | } else { |
| 735 | rpc = nc_rpc_copyconfig(source, target, config); |
| 736 | } |
| 737 | } else { |
| 738 | if (target == NC_DATASTORE_URL) { |
| 739 | rpc = nc_rpc_copyconfig(source, target, url); |
| 740 | } else { |
| 741 | rpc = nc_rpc_copyconfig(source, target); |
| 742 | } |
| 743 | } |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 744 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 745 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 746 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 749 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 750 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 751 | |
| 752 | return res; |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 753 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 754 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 755 | static json_object *netconf_editconfig(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] | 756 | { |
| 757 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 758 | json_object *res = NULL; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 759 | |
| 760 | /* create requests */ |
Tomas Cejka | 5064c23 | 2013-01-17 09:30:58 +0100 | [diff] [blame] | 761 | /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */ |
| 762 | rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 763 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 764 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 765 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 768 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 769 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 770 | |
| 771 | return res; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 774 | static json_object *netconf_killsession(const char* session_key, const char* sid) |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 775 | { |
| 776 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 777 | json_object *res = NULL; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 778 | |
| 779 | /* create requests */ |
| 780 | rpc = nc_rpc_killsession(sid); |
| 781 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 782 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 783 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 784 | } |
| 785 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 786 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 787 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 788 | return res; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 791 | static json_object *netconf_onlytargetop(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] | 792 | { |
| 793 | nc_rpc* rpc; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 794 | json_object *res = NULL; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 795 | |
| 796 | /* create requests */ |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 797 | rpc = op_func(target); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 798 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 799 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 800 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 803 | res = netconf_op(session_key, rpc, NULL); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 804 | nc_rpc_free (rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 805 | return res; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 806 | } |
| 807 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 808 | static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 809 | { |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 810 | nc_rpc *rpc = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 811 | json_object *res = NULL; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 812 | if (target != NC_DATASTORE_URL) { |
| 813 | rpc = nc_rpc_deleteconfig(target); |
| 814 | } else { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 815 | rpc = nc_rpc_deleteconfig(target, url); |
| 816 | } |
| 817 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 818 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 819 | return create_error("Internal: Creating rpc request failed"); |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 822 | res = netconf_op(session_key, rpc, NULL); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 823 | nc_rpc_free (rpc); |
| 824 | return res; |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 827 | static json_object *netconf_lock(const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 828 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 829 | return (netconf_onlytargetop(session_key, target, nc_rpc_lock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 832 | static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 833 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 834 | return (netconf_onlytargetop(session_key, target, nc_rpc_unlock)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 837 | static json_object *netconf_generic(const char* session_key, const char* content, char** data) |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 838 | { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 839 | nc_rpc* rpc = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 840 | json_object *res = NULL; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 841 | |
| 842 | /* create requests */ |
| 843 | rpc = nc_rpc_generic(content); |
| 844 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 845 | DEBUG("mod_netconf: creating rpc request failed"); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 846 | return create_error("Internal: Creating rpc request failed"); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 849 | if (data != NULL) { |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 850 | // TODO ?free(*data); |
| 851 | (*data) = NULL; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 852 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 853 | |
| 854 | /* get session where send the RPC */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 855 | res = netconf_op(session_key, rpc, data); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 856 | nc_rpc_free (rpc); |
| 857 | return res; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Tomas Cejka | 0a4bba8 | 2013-04-19 11:51:28 +0200 | [diff] [blame] | 860 | /** |
| 861 | * @} |
| 862 | *//* netconf_operations */ |
| 863 | |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 864 | void clb_print(NC_VERB_LEVEL level, const char* msg) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 865 | { |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 866 | switch (level) { |
| 867 | case NC_VERB_ERROR: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 868 | DEBUG("%s", msg); |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 869 | break; |
| 870 | case NC_VERB_WARNING: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 871 | DEBUG("%s", msg); |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 872 | break; |
| 873 | case NC_VERB_VERBOSE: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 874 | DEBUG("%s", msg); |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 875 | break; |
| 876 | case NC_VERB_DEBUG: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 877 | DEBUG("%s", msg); |
Radek Krejci | 7338bde | 2012-08-10 12:57:30 +0200 | [diff] [blame] | 878 | break; |
| 879 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 882 | /** |
Tomas Cejka | 6e8f426 | 2013-07-10 09:20:19 +0200 | [diff] [blame] | 883 | * Receive message from client over UNIX socket and return pointer to it. |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 884 | * Caller should free message memory. |
| 885 | * \param[in] client socket descriptor of client |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 886 | * \return pointer to message |
| 887 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 888 | char *get_framed_message(int client) |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 889 | { |
| 890 | /* read json in chunked framing */ |
| 891 | unsigned int buffer_size = 0; |
| 892 | ssize_t buffer_len = 0; |
| 893 | char *buffer = NULL; |
| 894 | char c; |
| 895 | ssize_t ret; |
| 896 | int i, chunk_len; |
| 897 | char chunk_len_str[12]; |
| 898 | |
| 899 | while (1) { |
| 900 | /* read chunk length */ |
| 901 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') { |
| 902 | if (buffer != NULL) { |
| 903 | free (buffer); |
| 904 | buffer = NULL; |
| 905 | } |
| 906 | break; |
| 907 | } |
| 908 | if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') { |
| 909 | if (buffer != NULL) { |
| 910 | free (buffer); |
| 911 | buffer = NULL; |
| 912 | } |
| 913 | break; |
| 914 | } |
| 915 | i=0; |
| 916 | memset (chunk_len_str, 0, 12); |
| 917 | while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 918 | if (i==0 && c == '#') { |
| 919 | if (recv (client, &c, 1, 0) != 1 || c != '\n') { |
| 920 | /* end but invalid */ |
| 921 | if (buffer != NULL) { |
| 922 | free (buffer); |
| 923 | buffer = NULL; |
| 924 | } |
| 925 | } |
| 926 | /* end of message, double-loop break */ |
| 927 | goto msg_complete; |
| 928 | } |
| 929 | chunk_len_str[i++] = c; |
| 930 | if (i==11) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 931 | DEBUG("Message is too long, buffer for length is not big enought!!!!"); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 932 | break; |
| 933 | } |
| 934 | } |
| 935 | if (c != '\n') { |
| 936 | if (buffer != NULL) { |
| 937 | free (buffer); |
| 938 | buffer = NULL; |
| 939 | } |
| 940 | break; |
| 941 | } |
| 942 | chunk_len_str[i] = 0; |
| 943 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 944 | if (buffer != NULL) { |
| 945 | free (buffer); |
| 946 | buffer = NULL; |
| 947 | } |
| 948 | break; |
| 949 | } |
| 950 | buffer_size += chunk_len+1; |
| 951 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 952 | memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 953 | if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 954 | if (buffer != NULL) { |
| 955 | free (buffer); |
| 956 | buffer = NULL; |
| 957 | } |
| 958 | break; |
| 959 | } |
| 960 | buffer_len += ret; |
| 961 | } |
| 962 | msg_complete: |
| 963 | return buffer; |
| 964 | } |
| 965 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 966 | NC_DATASTORE parse_datastore(const char *ds) |
| 967 | { |
| 968 | if (strcmp(ds, "running") == 0) { |
| 969 | return NC_DATASTORE_RUNNING; |
| 970 | } else if (strcmp(ds, "startup") == 0) { |
| 971 | return NC_DATASTORE_STARTUP; |
| 972 | } else if (strcmp(ds, "candidate") == 0) { |
| 973 | return NC_DATASTORE_CANDIDATE; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 974 | } else if (strcmp(ds, "url") == 0) { |
| 975 | return NC_DATASTORE_URL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 976 | } |
| 977 | return -1; |
| 978 | } |
| 979 | |
| 980 | json_object *create_error(const char *errmess) |
| 981 | { |
| 982 | json_object *reply = json_object_new_object(); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 983 | json_object *array = json_object_new_array(); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 984 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 985 | json_object_array_add(array, json_object_new_string(errmess)); |
| 986 | json_object_object_add(reply, "errors", array); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 987 | return reply; |
| 988 | |
| 989 | } |
| 990 | |
| 991 | json_object *create_data(const char *data) |
| 992 | { |
| 993 | json_object *reply = json_object_new_object(); |
| 994 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 995 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 996 | return reply; |
| 997 | } |
| 998 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 999 | json_object *create_ok() |
| 1000 | { |
| 1001 | json_object *reply = json_object_new_object(); |
| 1002 | reply = json_object_new_object(); |
| 1003 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1004 | return reply; |
| 1005 | } |
| 1006 | |
| 1007 | json_object *handle_op_connect(apr_pool_t *pool, json_object *request) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1008 | { |
| 1009 | const char *host = NULL; |
| 1010 | const char *port = NULL; |
| 1011 | const char *user = NULL; |
| 1012 | const char *pass = NULL; |
| 1013 | json_object *capabilities = NULL; |
| 1014 | json_object *reply = NULL; |
| 1015 | char *session_key_hash = NULL; |
| 1016 | struct nc_cpblts* cpblts = NULL; |
| 1017 | unsigned int len, i; |
| 1018 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1019 | DEBUG("Request: Connect"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1020 | host = json_object_get_string(json_object_object_get((json_object *) request, "host")); |
| 1021 | port = json_object_get_string(json_object_object_get((json_object *) request, "port")); |
| 1022 | user = json_object_get_string(json_object_object_get((json_object *) request, "user")); |
| 1023 | pass = json_object_get_string(json_object_object_get((json_object *) request, "pass")); |
| 1024 | |
| 1025 | capabilities = json_object_object_get((json_object *) request, "capabilities"); |
| 1026 | if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) { |
| 1027 | cpblts = nc_cpblts_new(NULL); |
| 1028 | for (i=0; i<len; i++) { |
| 1029 | nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i))); |
| 1030 | } |
| 1031 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1032 | DEBUG("no capabilities specified"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1035 | DEBUG("host: %s, port: %s, user: %s", host, port, user); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1036 | if ((host == NULL) || (user == NULL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1037 | DEBUG("Cannot connect - insufficient input."); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1038 | session_key_hash = NULL; |
| 1039 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1040 | session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts); |
| 1041 | DEBUG("hash: %s", session_key_hash); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1042 | } |
| 1043 | if (cpblts != NULL) { |
| 1044 | nc_cpblts_free(cpblts); |
| 1045 | } |
| 1046 | |
| 1047 | if (session_key_hash == NULL) { |
| 1048 | /* negative reply */ |
| 1049 | if (err_reply == NULL) { |
| 1050 | reply = json_object_new_object(); |
| 1051 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1052 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1053 | DEBUG("Connection failed."); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1054 | } else { |
| 1055 | /* use filled err_reply from libnetconf's callback */ |
| 1056 | reply = err_reply; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1057 | DEBUG("Connect - error from libnetconf's callback."); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1058 | } |
| 1059 | } else { |
| 1060 | /* positive reply */ |
| 1061 | reply = json_object_new_object(); |
| 1062 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1063 | json_object_object_add(reply, "session", json_object_new_string(session_key_hash)); |
| 1064 | |
| 1065 | free(session_key_hash); |
| 1066 | } |
| 1067 | return reply; |
| 1068 | } |
| 1069 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1070 | json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1071 | { |
| 1072 | const char *filter = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1073 | char *data = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1074 | json_object *reply = NULL; |
| 1075 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1076 | DEBUG("Request: get (session %s)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1077 | |
| 1078 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1079 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1080 | if ((data = netconf_get(session_key, filter, &reply)) == NULL) { |
| 1081 | if (reply == NULL) { |
| 1082 | if (err_reply == NULL) { |
| 1083 | reply = create_error("Get information failed."); |
| 1084 | } else { |
| 1085 | /* use filled err_reply from libnetconf's callback */ |
| 1086 | reply = err_reply; |
| 1087 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1088 | } |
| 1089 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1090 | reply = create_data(data); |
| 1091 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1092 | } |
| 1093 | return reply; |
| 1094 | } |
| 1095 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1096 | json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1097 | { |
| 1098 | NC_DATASTORE ds_type_s = -1; |
| 1099 | NC_DATASTORE ds_type_t = -1; |
| 1100 | const char *filter = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1101 | char *data = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1102 | const char *source = NULL; |
| 1103 | const char *target = NULL; |
| 1104 | json_object *reply = NULL; |
| 1105 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1106 | DEBUG("Request: get-config (session %s)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1107 | |
| 1108 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 1109 | |
| 1110 | /* get parameters */ |
| 1111 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1112 | ds_type_t = parse_datastore(target); |
| 1113 | } |
| 1114 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1115 | ds_type_s = parse_datastore(source); |
| 1116 | } |
| 1117 | if (ds_type_s == -1) { |
| 1118 | return create_error("Invalid source repository type requested."); |
| 1119 | } |
| 1120 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1121 | if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) { |
| 1122 | if (reply == NULL) { |
| 1123 | if (err_reply == NULL) { |
| 1124 | reply = create_error("Get configuration operation failed."); |
| 1125 | } else { |
| 1126 | /* use filled err_reply from libnetconf's callback */ |
| 1127 | reply = err_reply; |
| 1128 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1129 | } |
| 1130 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1131 | reply = create_data(data); |
| 1132 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1133 | } |
| 1134 | return reply; |
| 1135 | } |
| 1136 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1137 | json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1138 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1139 | char *data = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1140 | const char *identifier = NULL; |
| 1141 | const char *version = NULL; |
| 1142 | const char *format = NULL; |
| 1143 | json_object *reply = NULL; |
| 1144 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1145 | DEBUG("Request: get-schema (session %s)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1146 | identifier = json_object_get_string(json_object_object_get(request, "identifier")); |
| 1147 | if (identifier == NULL) { |
| 1148 | return create_error("No identifier for get-schema supplied."); |
| 1149 | } |
| 1150 | version = json_object_get_string(json_object_object_get(request, "version")); |
| 1151 | format = json_object_get_string(json_object_object_get(request, "format")); |
| 1152 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1153 | DEBUG("get-schema(version: %s, format: %s)", version, format); |
| 1154 | if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) { |
| 1155 | if (reply == NULL) { |
| 1156 | if (err_reply == NULL) { |
| 1157 | reply = create_error("Get models operation failed."); |
| 1158 | } else { |
| 1159 | /* use filled err_reply from libnetconf's callback */ |
| 1160 | reply = err_reply; |
| 1161 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1162 | } |
| 1163 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1164 | reply = create_data(data); |
| 1165 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1166 | } |
| 1167 | return reply; |
| 1168 | } |
| 1169 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1170 | json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1171 | { |
| 1172 | NC_DATASTORE ds_type_s = -1; |
| 1173 | NC_DATASTORE ds_type_t = -1; |
| 1174 | NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1175 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
| 1176 | const char *defop = NULL; |
| 1177 | const char *erropt = NULL; |
| 1178 | const char *config = NULL; |
| 1179 | const char *source = NULL; |
| 1180 | const char *target = NULL; |
| 1181 | json_object *reply = NULL; |
| 1182 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1183 | DEBUG("Request: edit-config (session %s)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1184 | |
| 1185 | defop = json_object_get_string(json_object_object_get(request, "default-operation")); |
| 1186 | if (defop != NULL) { |
| 1187 | if (strcmp(defop, "merge") == 0) { |
| 1188 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 1189 | } else if (strcmp(defop, "replace") == 0) { |
| 1190 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 1191 | } else if (strcmp(defop, "none") == 0) { |
| 1192 | defop_type = NC_EDIT_DEFOP_NONE; |
| 1193 | } else { |
| 1194 | return create_error("Invalid default-operation parameter."); |
| 1195 | } |
| 1196 | } else { |
| 1197 | defop_type = NC_EDIT_DEFOP_NOTSET; |
| 1198 | } |
| 1199 | |
| 1200 | erropt = json_object_get_string(json_object_object_get(request, "error-option")); |
| 1201 | if (erropt != NULL) { |
| 1202 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 1203 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 1204 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 1205 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 1206 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 1207 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 1208 | } else { |
| 1209 | return create_error("Invalid error-option parameter."); |
| 1210 | } |
| 1211 | } else { |
| 1212 | erropt_type = 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* get parameters */ |
| 1216 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1217 | ds_type_t = parse_datastore(target); |
| 1218 | } |
| 1219 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1220 | ds_type_s = parse_datastore(source); |
| 1221 | } |
| 1222 | if (ds_type_t == -1) { |
| 1223 | return create_error("Invalid target repository type requested."); |
| 1224 | } |
| 1225 | |
| 1226 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1227 | if (config == NULL) { |
| 1228 | return create_error("Invalid config data parameter."); |
| 1229 | } |
| 1230 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1231 | reply = netconf_editconfig(session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_TESTSET, config); |
| 1232 | if (reply == NULL) { |
| 1233 | if (err_reply != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1234 | /* use filled err_reply from libnetconf's callback */ |
| 1235 | reply = err_reply; |
| 1236 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1237 | } |
| 1238 | return reply; |
| 1239 | } |
| 1240 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1241 | json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1242 | { |
| 1243 | NC_DATASTORE ds_type_s = -1; |
| 1244 | NC_DATASTORE ds_type_t = -1; |
| 1245 | const char *config = NULL; |
| 1246 | const char *target = NULL; |
| 1247 | const char *source = NULL; |
| 1248 | json_object *reply = NULL; |
| 1249 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1250 | DEBUG("Request: copy-config (session %s)", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1251 | |
| 1252 | /* get parameters */ |
| 1253 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1254 | ds_type_t = parse_datastore(target); |
| 1255 | } |
| 1256 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1257 | ds_type_s = parse_datastore(source); |
| 1258 | } |
| 1259 | if (source == NULL) { |
| 1260 | /* no explicit source specified -> use config data */ |
| 1261 | ds_type_s = NC_DATASTORE_CONFIG; |
| 1262 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 1263 | } else if (ds_type_s == -1) { |
| 1264 | /* source datastore specified, but it is invalid */ |
| 1265 | return create_error("Invalid source repository type requested."); |
| 1266 | } |
| 1267 | |
| 1268 | if (ds_type_t == -1) { |
| 1269 | /* invalid target datastore specified */ |
| 1270 | return create_error("Invalid target repository type requested."); |
| 1271 | } |
| 1272 | |
| 1273 | if (source == NULL && config == NULL) { |
| 1274 | reply = create_error("invalid input parameters - one of source and config is required."); |
| 1275 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1276 | reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, ""); |
| 1277 | if (reply == NULL) { |
| 1278 | if (err_reply != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1279 | /* use filled err_reply from libnetconf's callback */ |
| 1280 | reply = err_reply; |
| 1281 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | return reply; |
| 1285 | } |
| 1286 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1287 | json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1288 | { |
| 1289 | json_object *reply = NULL; |
| 1290 | const char *config = NULL; |
| 1291 | char *data = NULL; |
| 1292 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1293 | DEBUG("Request: generic request for session %s", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1294 | |
| 1295 | config = json_object_get_string(json_object_object_get(request, "content")); |
| 1296 | |
| 1297 | if (config == NULL) { |
| 1298 | return create_error("Missing content parameter."); |
| 1299 | } |
| 1300 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1301 | /* TODO */ |
| 1302 | reply = netconf_generic(session_key, config, &data); |
| 1303 | if (reply == NULL) { |
| 1304 | if (err_reply != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1305 | /* use filled err_reply from libnetconf's callback */ |
| 1306 | reply = err_reply; |
| 1307 | } |
| 1308 | } else { |
| 1309 | if (data == NULL) { |
| 1310 | reply = json_object_new_object(); |
| 1311 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1312 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1313 | reply = create_data(data); |
| 1314 | free(data); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | return reply; |
| 1318 | } |
| 1319 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1320 | json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1321 | { |
| 1322 | json_object *reply = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1323 | DEBUG("Request: Disconnect session %s", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1324 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1325 | if (netconf_close(session_key, &reply) != EXIT_SUCCESS) { |
| 1326 | if (reply == NULL) { |
| 1327 | if (err_reply == NULL) { |
| 1328 | reply = create_error("Get configuration information from device failed."); |
| 1329 | } else { |
| 1330 | /* use filled err_reply from libnetconf's callback */ |
| 1331 | reply = err_reply; |
| 1332 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1333 | } |
| 1334 | } else { |
| 1335 | reply = json_object_new_object(); |
| 1336 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1337 | } |
| 1338 | return reply; |
| 1339 | } |
| 1340 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1341 | json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1342 | { |
| 1343 | json_object *reply = NULL; |
| 1344 | const char *sid = NULL; |
| 1345 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1346 | DEBUG("Request: kill-session, session %s", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1347 | |
| 1348 | sid = json_object_get_string(json_object_object_get(request, "session-id")); |
| 1349 | |
| 1350 | if (sid == NULL) { |
| 1351 | return create_error("Missing session-id parameter."); |
| 1352 | } |
| 1353 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1354 | reply = netconf_killsession(session_key, sid); |
| 1355 | if (reply == NULL) { |
| 1356 | if (err_reply != NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1357 | /* use filled err_reply from libnetconf's callback */ |
| 1358 | reply = err_reply; |
| 1359 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1360 | } |
| 1361 | return reply; |
| 1362 | } |
| 1363 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1364 | json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1365 | { |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1366 | struct nc_session *temp_session = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1367 | struct session_with_mutex * locked_session = NULL; |
| 1368 | json_object *reply = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1369 | DEBUG("Request: get info about session %s", session_key); |
| 1370 | return create_ok(); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1371 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1372 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1373 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1374 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1375 | return NULL; |
| 1376 | } |
| 1377 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1378 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1379 | if ((locked_session != NULL) && (locked_session->hello_message != NULL)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1380 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1381 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1382 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1383 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1384 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1385 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1386 | DEBUG("creating temporal NC session."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1387 | temp_session = nc_session_connect_channel(locked_session->session, NULL); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1388 | if (temp_session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1389 | prepare_status_message(locked_session, temp_session); |
| 1390 | DEBUG("closing temporal NC session."); |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 1391 | nc_session_free(temp_session); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1392 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1393 | DEBUG("Reload hello failed due to channel establishment"); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1394 | reply = create_error("Reload was unsuccessful, connection failed."); |
| 1395 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1396 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1397 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1398 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1399 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1400 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1401 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1402 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1403 | reply = create_error("Invalid session identifier."); |
| 1404 | } |
| 1405 | |
| 1406 | if ((reply == NULL) && (locked_session->hello_message != NULL)) { |
| 1407 | reply = locked_session->hello_message; |
| 1408 | } |
| 1409 | return reply; |
| 1410 | } |
| 1411 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1412 | json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1413 | { |
| 1414 | json_object *reply = NULL; |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1415 | struct session_with_mutex * locked_session = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1416 | DEBUG("Request: get info about session %s", session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1417 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1418 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1419 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1420 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1423 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1424 | if (locked_session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1425 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1426 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1427 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1428 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1429 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1430 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1431 | if (locked_session->hello_message != NULL) { |
| 1432 | reply = locked_session->hello_message; |
| 1433 | } else { |
| 1434 | reply = create_error("Invalid session identifier."); |
| 1435 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1436 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1437 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1438 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1439 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1440 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1441 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1442 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1443 | reply = create_error("Invalid session identifier."); |
| 1444 | } |
| 1445 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1446 | |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1447 | return reply; |
| 1448 | } |
| 1449 | |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1450 | void notification_history(time_t eventtime, const char *content) |
| 1451 | { |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1452 | json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key); |
| 1453 | if (notif_history_array == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1454 | DEBUG("No list of notification history found."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1455 | return; |
| 1456 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1457 | DEBUG("Got notification from history %lu.", (long unsigned) eventtime); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1458 | json_object *notif = json_object_new_object(); |
| 1459 | if (notif == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1460 | DEBUG("Could not allocate memory for notification (json)."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1461 | return; |
| 1462 | } |
| 1463 | json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime)); |
| 1464 | json_object_object_add(notif, "content", json_object_new_string(content)); |
| 1465 | json_object_array_add(notif_history_array, notif); |
| 1466 | } |
| 1467 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1468 | json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1469 | { |
| 1470 | json_object *reply = NULL; |
| 1471 | const char *sid = NULL; |
| 1472 | struct session_with_mutex *locked_session = NULL; |
| 1473 | struct nc_session *temp_session = NULL; |
| 1474 | nc_rpc *rpc = NULL; |
| 1475 | time_t start = 0; |
| 1476 | time_t stop = 0; |
| 1477 | int64_t from, to; |
| 1478 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1479 | DEBUG("Request: get notification history, session %s", session_key); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1480 | |
| 1481 | sid = json_object_get_string(json_object_object_get(request, "session")); |
| 1482 | from = json_object_get_int64(json_object_object_get(request, "from")); |
| 1483 | to = json_object_get_int64(json_object_object_get(request, "to")); |
| 1484 | |
| 1485 | start = time(NULL) + from; |
| 1486 | stop = time(NULL) + to; |
| 1487 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1488 | DEBUG("notification history interval %li %li", (long int) from, (long int) to); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1489 | |
| 1490 | if (sid == NULL) { |
| 1491 | return create_error("Missing session parameter."); |
| 1492 | } |
| 1493 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1494 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1495 | if (pthread_rwlock_rdlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1496 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1497 | return NULL; |
| 1498 | } |
| 1499 | |
| 1500 | locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1501 | if (locked_session != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1502 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1503 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1504 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1505 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1506 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1507 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1508 | DEBUG("creating temporal NC session."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1509 | temp_session = nc_session_connect_channel(locked_session->session, NULL); |
| 1510 | if (temp_session != NULL) { |
| 1511 | rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop); |
| 1512 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1513 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1514 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1515 | DEBUG("notifications: creating an rpc request failed."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1516 | return create_error("notifications: creating an rpc request failed."); |
| 1517 | } |
| 1518 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1519 | DEBUG("Send NC subscribe."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1520 | /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1521 | json_object *res = netconf_unlocked_op(temp_session, rpc); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1522 | if (res != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1523 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1524 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1525 | DEBUG("Subscription RPC failed."); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1526 | return res; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1527 | } |
| 1528 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
| 1529 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1530 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1531 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1532 | DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1533 | pthread_mutex_lock(&ntf_history_lock); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1534 | json_object *notif_history_array = json_object_new_array(); |
| 1535 | if (pthread_setspecific(notif_history_key, notif_history_array) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1536 | DEBUG("notif_history: cannot set thread-specific hash value."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 1537 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1538 | |
| 1539 | ncntf_dispatch_receive(temp_session, notification_history); |
| 1540 | |
| 1541 | reply = json_object_new_object(); |
| 1542 | json_object_object_add(reply, "notifications", notif_history_array); |
| 1543 | //json_object_put(notif_history_array); |
| 1544 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1545 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1546 | pthread_mutex_unlock(&ntf_history_lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1547 | DEBUG("closing temporal NC session."); |
Tomas Cejka | aecc5f7 | 2013-10-01 00:03:50 +0200 | [diff] [blame] | 1548 | nc_session_free(temp_session); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1549 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1550 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1551 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1552 | DEBUG("Get history of notification failed due to channel establishment"); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1553 | reply = create_error("Get history of notification was unsuccessful, connection failed."); |
| 1554 | } |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1555 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1556 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1557 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1558 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1559 | } |
| 1560 | reply = create_error("Invalid session identifier."); |
| 1561 | } |
| 1562 | |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1563 | return reply; |
| 1564 | } |
| 1565 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1566 | json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key) |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1567 | { |
| 1568 | json_object *reply = NULL; |
| 1569 | const char *sid = NULL; |
| 1570 | const char *target = NULL; |
| 1571 | const char *url = NULL; |
| 1572 | struct session_with_mutex *locked_session = NULL; |
| 1573 | nc_rpc *rpc = NULL; |
| 1574 | NC_DATASTORE target_ds; |
| 1575 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1576 | DEBUG("Request: validate datastore, session %s", session_key); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1577 | |
| 1578 | sid = json_object_get_string(json_object_object_get(request, "session")); |
| 1579 | target = json_object_get_string(json_object_object_get(request, "target")); |
| 1580 | url = json_object_get_string(json_object_object_get(request, "url")); |
| 1581 | |
| 1582 | |
| 1583 | if ((sid == NULL) || (target == NULL)) { |
| 1584 | return create_error("Missing session parameter."); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1585 | } |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1586 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1587 | /* validation */ |
| 1588 | target_ds = parse_datastore(target); |
| 1589 | if (target_ds == NC_DATASTORE_URL) { |
| 1590 | if (url != NULL) { |
| 1591 | rpc = nc_rpc_validate(target_ds, url); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1592 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1593 | } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP) |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1594 | || (target_ds == NC_DATASTORE_CANDIDATE)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1595 | rpc = nc_rpc_validate(target_ds); |
| 1596 | } |
| 1597 | if (rpc == NULL) { |
| 1598 | DEBUG("mod_netconf: creating rpc request failed"); |
| 1599 | reply = create_error("Creation of RPC request failed."); |
| 1600 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1601 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1602 | return reply; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1605 | DEBUG("Request: validate datastore"); |
| 1606 | if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) { |
| 1607 | reply = json_object_new_object(); |
| 1608 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1609 | } |
| 1610 | nc_rpc_free (rpc); |
| 1611 | |
| 1612 | DEBUG("Request: validate datastore"); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1613 | return reply; |
| 1614 | } |
| 1615 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1616 | void * thread_routine (void * arg) |
| 1617 | { |
| 1618 | void * retval = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1619 | struct pollfd fds; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1620 | json_object *request = NULL; |
| 1621 | json_object *reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1622 | int operation; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1623 | int status = 0; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1624 | const char *msgtext; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1625 | const char *session_key; |
| 1626 | const char *target = NULL; |
| 1627 | const char *source = NULL; |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1628 | const char *url = NULL; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1629 | NC_DATASTORE ds_type_t = -1; |
| 1630 | NC_DATASTORE ds_type_s = -1; |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1631 | char *chunked_out_msg = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1632 | apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1633 | //server_rec * server = ((struct pass_to_thread*)arg)->server; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1634 | int client = ((struct pass_to_thread*)arg)->client; |
| 1635 | |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1636 | char *buffer = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1637 | |
| 1638 | while (!isterminated) { |
| 1639 | fds.fd = client; |
| 1640 | fds.events = POLLIN; |
| 1641 | fds.revents = 0; |
| 1642 | |
| 1643 | status = poll(&fds, 1, 1000); |
| 1644 | |
| 1645 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
| 1646 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1647 | //DEBUG("poll interrupted"); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1648 | continue; |
| 1649 | } else if (status < 0) { |
| 1650 | /* 0: poll time outed |
| 1651 | * close socket and ignore this request from the client, it can try it again |
| 1652 | * -1: poll failed |
| 1653 | * something wrong happend, close this socket and wait for another request |
| 1654 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1655 | //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1656 | close(client); |
| 1657 | break; |
| 1658 | } |
| 1659 | /* status > 0 */ |
| 1660 | |
| 1661 | /* check the status of the socket */ |
| 1662 | |
| 1663 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 1664 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 1665 | /* close client's socket (it's probably already closed by client */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1666 | //DEBUG("socket error (%d)", fds.revents); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1667 | close(client); |
| 1668 | break; |
| 1669 | } |
| 1670 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1671 | DEBUG("Get framed message..."); |
| 1672 | buffer = get_framed_message(client); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1673 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1674 | DEBUG("Check read buffer."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1675 | if (buffer != NULL) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1676 | enum json_tokener_error jerr; |
| 1677 | request = json_tokener_parse_verbose(buffer, &jerr); |
| 1678 | if (jerr != json_tokener_success) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1679 | DEBUG("JSON parsing error"); |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1680 | continue; |
| 1681 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1682 | operation = json_object_get_int(json_object_object_get(request, "type")); |
| 1683 | |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1684 | session_key = json_object_get_string(json_object_object_get(request, "session")); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1685 | DEBUG("operation %d session_key %s.", operation, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1686 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 1687 | if (operation != MSG_CONNECT && session_key == NULL) { |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1688 | reply = create_error("Missing session specification."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1689 | msgtext = json_object_to_json_string(reply); |
| 1690 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 1691 | json_object_put(reply); |
| 1692 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 1693 | close(client); |
| 1694 | break; |
| 1695 | } |
| 1696 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1697 | /* null global JSON error-reply */ |
| 1698 | err_reply = NULL; |
| 1699 | |
| 1700 | /* prepare reply envelope */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1701 | if (reply != NULL) { |
| 1702 | json_object_put(reply); |
| 1703 | } |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1704 | reply = NULL; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1705 | |
| 1706 | /* process required operation */ |
| 1707 | switch (operation) { |
| 1708 | case MSG_CONNECT: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1709 | reply = handle_op_connect(pool, request); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1710 | break; |
| 1711 | case MSG_GET: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1712 | reply = handle_op_get(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1713 | break; |
| 1714 | case MSG_GETCONFIG: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1715 | reply = handle_op_getconfig(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1716 | break; |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1717 | case MSG_GETSCHEMA: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1718 | reply = handle_op_getschema(pool, request, session_key); |
Tomas Cejka | 0aeca8b | 2012-12-22 19:56:03 +0100 | [diff] [blame] | 1719 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1720 | case MSG_EDITCONFIG: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1721 | reply = handle_op_editconfig(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1722 | break; |
| 1723 | case MSG_COPYCONFIG: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1724 | reply = handle_op_copyconfig(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1725 | break; |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1726 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1727 | case MSG_DELETECONFIG: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1728 | case MSG_LOCK: |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1729 | case MSG_UNLOCK: |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1730 | /* get parameters */ |
| 1731 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 1732 | ds_type_t = parse_datastore(target); |
| 1733 | } |
| 1734 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 1735 | ds_type_s = parse_datastore(source); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | if (ds_type_t == -1) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1739 | reply = create_error("Invalid target repository type requested."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1740 | break; |
| 1741 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1742 | switch(operation) { |
| 1743 | case MSG_DELETECONFIG: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1744 | DEBUG("Request: delete-config (session %s)", session_key); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1745 | url = json_object_get_string(json_object_object_get(request, "url")); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1746 | reply = netconf_deleteconfig(session_key, ds_type_t, url); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1747 | break; |
| 1748 | case MSG_LOCK: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1749 | DEBUG("Request: lock (session %s)", session_key); |
| 1750 | reply = netconf_lock(session_key, ds_type_t); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1751 | break; |
| 1752 | case MSG_UNLOCK: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1753 | DEBUG("Request: unlock (session %s)", session_key); |
| 1754 | reply = netconf_unlock(session_key, ds_type_t); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1755 | break; |
| 1756 | default: |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1757 | reply = create_error("Internal: Unknown request type."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1761 | if (reply == NULL) { |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1762 | if (err_reply == NULL) { |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1763 | /** \todo more clever error message wanted */ |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 1764 | reply = json_object_new_object(); |
| 1765 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1766 | } else { |
| 1767 | /* use filled err_reply from libnetconf's callback */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1768 | reply = err_reply; |
| 1769 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1770 | } |
| 1771 | break; |
| 1772 | case MSG_KILL: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1773 | reply = handle_op_kill(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1774 | break; |
| 1775 | case MSG_DISCONNECT: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1776 | reply = handle_op_disconnect(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1777 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1778 | case MSG_RELOADHELLO: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1779 | reply = handle_op_reloadhello(pool, request, session_key); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1780 | break; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 1781 | case MSG_INFO: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1782 | reply = handle_op_info(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1783 | break; |
| 1784 | case MSG_GENERIC: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1785 | reply = handle_op_generic(pool, request, session_key); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1786 | break; |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1787 | case MSG_NTF_GETHISTORY: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1788 | reply = handle_op_ntfgethistory(pool, request, session_key); |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1789 | break; |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1790 | case MSG_VALIDATE: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1791 | reply = handle_op_validate(pool, request, session_key); |
Tomas Cejka | 4003a70 | 2013-10-01 00:02:45 +0200 | [diff] [blame] | 1792 | break; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1793 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1794 | DEBUG("Unknown mod_netconf operation requested (%d)", operation); |
Tomas Cejka | d5b5377 | 2013-06-08 23:01:07 +0200 | [diff] [blame] | 1795 | reply = create_error("Operation not supported."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1796 | break; |
| 1797 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1798 | DEBUG("Clean request json object."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1799 | json_object_put(request); |
| 1800 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1801 | DEBUG("Send reply json object."); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1802 | /* send reply to caller */ |
| 1803 | if (reply != NULL) { |
| 1804 | msgtext = json_object_to_json_string(reply); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1805 | if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) { |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1806 | if (buffer != NULL) { |
| 1807 | free(buffer); |
| 1808 | buffer = NULL; |
| 1809 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1810 | break; |
| 1811 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1812 | DEBUG("Send framed reply json object."); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1813 | send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1814 | DEBUG("Clean reply json object."); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1815 | json_object_put(reply); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1816 | reply = NULL; |
| 1817 | DEBUG("Clean message buffer."); |
Tomas Cejka | 64b8748 | 2013-06-03 16:30:53 +0200 | [diff] [blame] | 1818 | free(chunked_out_msg); |
| 1819 | chunked_out_msg = NULL; |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 1820 | if (buffer != NULL) { |
| 1821 | free(buffer); |
| 1822 | buffer = NULL; |
| 1823 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1824 | if (err_reply != NULL) { |
| 1825 | json_object_put(err_reply); |
| 1826 | err_reply = NULL; |
| 1827 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 1828 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1829 | DEBUG("Reply is NULL, shouldn't be..."); |
| 1830 | continue; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1834 | free (arg); |
| 1835 | |
| 1836 | return retval; |
| 1837 | } |
| 1838 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1839 | /** |
| 1840 | * \brief Close all open NETCONF sessions. |
| 1841 | * |
| 1842 | * During termination of mod_netconf, it is useful to close all remaining |
| 1843 | * sessions. This function iterates over the list of sessions and close them |
| 1844 | * all. |
| 1845 | * |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1846 | * \param[in] p apr pool needed for hash table iterating |
| 1847 | * \param[in] ht hash table of session_with_mutex structs |
| 1848 | */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1849 | static void close_all_nc_sessions(apr_pool_t *p) |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1850 | { |
| 1851 | apr_hash_index_t *hi; |
| 1852 | void *val = NULL; |
| 1853 | struct session_with_mutex *swm = NULL; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1854 | const char *hashed_key = NULL; |
| 1855 | apr_ssize_t hashed_key_length; |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1856 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1857 | |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1858 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1859 | DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1860 | if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1861 | DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1862 | return; |
| 1863 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1864 | for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) { |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1865 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1866 | DEBUG("Closing NETCONF session (%s).", hashed_key); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1867 | swm = (struct session_with_mutex *) val; |
| 1868 | if (swm != NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1869 | DEBUG("LOCK mutex %s", __func__); |
| 1870 | pthread_mutex_lock(&swm->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1871 | apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1872 | DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1873 | pthread_mutex_unlock(&swm->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1874 | |
| 1875 | /* close_and_free_session handles locking on its own */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1876 | close_and_free_session(swm); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1877 | } |
| 1878 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1879 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1880 | DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1881 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1882 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1883 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1884 | } |
| 1885 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1886 | static void check_timeout_and_close(apr_pool_t *p) |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1887 | { |
| 1888 | apr_hash_index_t *hi; |
| 1889 | void *val = NULL; |
| 1890 | struct nc_session *ns = NULL; |
| 1891 | struct session_with_mutex *swm = NULL; |
| 1892 | const char *hashed_key = NULL; |
| 1893 | apr_ssize_t hashed_key_length; |
| 1894 | apr_time_t current_time = apr_time_now(); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1895 | int ret; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1896 | |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1897 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1898 | //DEBUG("LOCK wrlock %s", __func__); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1899 | if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1900 | DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1901 | return; |
| 1902 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1903 | for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) { |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1904 | apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val); |
| 1905 | swm = (struct session_with_mutex *) val; |
| 1906 | if (swm == NULL) { |
| 1907 | continue; |
| 1908 | } |
| 1909 | ns = swm->session; |
| 1910 | if (ns == NULL) { |
| 1911 | continue; |
| 1912 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1913 | //DEBUG("LOCK mutex %s", __func__); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1914 | pthread_mutex_lock(&swm->lock); |
| 1915 | if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1916 | DEBUG("Closing NETCONF session (%s).", hashed_key); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1917 | /* remove session from the active sessions list */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1918 | apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1919 | //DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1920 | pthread_mutex_unlock(&swm->lock); |
| 1921 | |
| 1922 | /* close_and_free_session handles locking on its own */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1923 | close_and_free_session(swm); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1924 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1925 | //DEBUG("UNLOCK mutex %s", __func__); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 1926 | pthread_mutex_unlock(&swm->lock); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1927 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1928 | } |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1929 | /* get exclusive access to sessions_list (conns) */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1930 | //DEBUG("UNLOCK wrlock %s", __func__); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1931 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1932 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 1933 | } |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | |
| 1937 | /** |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1938 | * This is actually implementation of NETCONF client |
| 1939 | * - requests are received from UNIX socket in the predefined format |
| 1940 | * - results are replied through the same way |
| 1941 | * - the daemon run as a separate process, but it is started and stopped |
| 1942 | * automatically by Apache. |
| 1943 | * |
| 1944 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1945 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 1946 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1947 | struct timeval tv; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1948 | struct sockaddr_un local, remote; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1949 | int lsock, client, ret, i, pthread_count = 0; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1950 | unsigned int olds = 0, timediff = 0; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1951 | socklen_t len; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 1952 | mod_netconf_cfg *cfg; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1953 | struct pass_to_thread * arg; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1954 | pthread_t * ptids = calloc(1, sizeof(pthread_t)); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1955 | struct timespec maxtime; |
| 1956 | pthread_rwlockattr_t lock_attrs; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1957 | char *sockname = NULL; |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1958 | #ifdef WITH_NOTIFICATIONS |
| 1959 | char use_notifications = 0; |
| 1960 | #endif |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1961 | |
Tomas Cejka | 6b886e0 | 2013-07-05 09:53:17 +0200 | [diff] [blame] | 1962 | http_server = server; |
| 1963 | |
Tomas Cejka | 404d37e | 2013-04-13 02:31:35 +0200 | [diff] [blame] | 1964 | /* wait at most 5 seconds for every thread to terminate */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 1965 | maxtime.tv_sec = 5; |
| 1966 | maxtime.tv_nsec = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1967 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1968 | #ifndef HTTPD_INDEPENDENT |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1969 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1970 | unixd_setup_child(); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1971 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1972 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1973 | if (server != NULL) { |
| 1974 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 1975 | if (cfg == NULL) { |
| 1976 | DEBUG("Getting mod_netconf configuration failed"); |
| 1977 | return; |
| 1978 | } |
| 1979 | sockname = cfg->sockname; |
| 1980 | } else { |
| 1981 | sockname = SOCKET_FILENAME; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1982 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1983 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1984 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1985 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1986 | DEBUG("Creating socket failed (%s)", strerror(errno)); |
| 1987 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | local.sun_family = AF_UNIX; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1991 | strncpy(local.sun_path, sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1992 | unlink(local.sun_path); |
| 1993 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 1994 | |
| 1995 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 1996 | if (errno == EADDRINUSE) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 1997 | DEBUG("mod_netconf socket address already in use"); |
| 1998 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1999 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2000 | DEBUG("Binding socket failed (%s)", strerror(errno)); |
| 2001 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2005 | DEBUG("Setting up listen socket failed (%s)", strerror(errno)); |
| 2006 | goto error_exit; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2007 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2008 | chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2009 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 2010 | /* prepare internal lists */ |
| 2011 | netconf_sessions_list = apr_hash_make(pool); |
| 2012 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2013 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 2014 | if (notification_init(pool, server) == -1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2015 | DEBUG("libwebsockets initialization failed"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2016 | use_notifications = 0; |
| 2017 | } else { |
| 2018 | use_notifications = 1; |
| 2019 | } |
| 2020 | #endif |
| 2021 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2022 | /* setup libnetconf's callbacks */ |
| 2023 | nc_verbosity(NC_VERB_DEBUG); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2024 | nc_callback_print(clb_print); |
| 2025 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 2026 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 2027 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 2028 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2029 | |
| 2030 | /* disable publickey authentication */ |
| 2031 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 2032 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2033 | /* create mutex protecting session list */ |
| 2034 | pthread_rwlockattr_init(&lock_attrs); |
| 2035 | /* rwlock is shared only with threads in this process */ |
| 2036 | pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE); |
| 2037 | /* create rw lock */ |
| 2038 | if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2039 | DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno)); |
| 2040 | goto error_exit; |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2041 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2042 | pthread_mutex_init(&ntf_history_lock, NULL); |
| 2043 | DEBUG("init of notif_history_key."); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 2044 | if (pthread_key_create(¬if_history_key, NULL) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2045 | DEBUG("init of notif_history_key failed"); |
Tomas Cejka | d016f9c | 2013-07-10 09:16:16 +0200 | [diff] [blame] | 2046 | } |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 2047 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2048 | fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2049 | while (isterminated == 0) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2050 | gettimeofday(&tv, NULL); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2051 | timediff = (unsigned int)tv.tv_sec - olds; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2052 | #ifdef WITH_NOTIFICATIONS |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2053 | if (timediff > 60) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2054 | DEBUG("handling notifications"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2055 | } |
| 2056 | if (use_notifications == 1) { |
| 2057 | notification_handle(); |
| 2058 | } |
| 2059 | #endif |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2060 | if (timediff > ACTIVITY_CHECK_INTERVAL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2061 | check_timeout_and_close(pool); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2062 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2063 | |
| 2064 | /* open incoming connection if any */ |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2065 | len = sizeof(remote); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2066 | if (((unsigned int)tv.tv_sec - olds) > 60) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2067 | DEBUG("accepting another client"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2068 | olds = tv.tv_sec; |
| 2069 | } |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2070 | client = accept(lsock, (struct sockaddr *) &remote, &len); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2071 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 2072 | apr_sleep(SLEEP_TIME); |
| 2073 | continue; |
| 2074 | } else if (client == -1 && (errno == EINTR)) { |
| 2075 | continue; |
| 2076 | } else if (client == -1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2077 | DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2078 | continue; |
| 2079 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2080 | |
| 2081 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2082 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2083 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2084 | arg = malloc (sizeof(struct pass_to_thread)); |
| 2085 | arg->client = client; |
| 2086 | arg->pool = pool; |
| 2087 | arg->server = server; |
| 2088 | arg->netconf_sessions_list = netconf_sessions_list; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2089 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2090 | /* start new thread. It will serve this particular request and then terminate */ |
| 2091 | if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2092 | DEBUG("Creating POSIX thread failed: %d\n", ret); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2093 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2094 | DEBUG("Thread %lu created", ptids[pthread_count]); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2095 | pthread_count++; |
| 2096 | ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1)); |
| 2097 | ptids[pthread_count] = 0; |
| 2098 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2099 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2100 | /* check if some thread already terminated, free some resources by joining it */ |
| 2101 | for (i=0; i<pthread_count; i++) { |
| 2102 | if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2103 | DEBUG("Thread %lu joined with retval %p", ptids[i], arg); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2104 | pthread_count--; |
| 2105 | if (pthread_count > 0) { |
| 2106 | /* place last Thread ID on the place of joined one */ |
| 2107 | ptids[i] = ptids[pthread_count]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2108 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2109 | } |
| 2110 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2111 | DEBUG("Running %d threads", pthread_count); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2114 | DEBUG("mod_netconf terminating..."); |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2115 | /* join all threads */ |
| 2116 | for (i=0; i<pthread_count; i++) { |
| 2117 | pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime); |
| 2118 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2119 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 2120 | #ifdef WITH_NOTIFICATIONS |
| 2121 | notification_close(); |
| 2122 | #endif |
| 2123 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2124 | /* close all NETCONF sessions */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2125 | close_all_nc_sessions(pool); |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 2126 | |
David Kupka | 8e60a37 | 2012-09-04 09:15:20 +0200 | [diff] [blame] | 2127 | /* destroy rwlock */ |
| 2128 | pthread_rwlock_destroy(&session_lock); |
| 2129 | pthread_rwlockattr_destroy(&lock_attrs); |
| 2130 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2131 | DEBUG("Exiting from the mod_netconf daemon"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2132 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2133 | free(ptids); |
| 2134 | close(lsock); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2135 | exit(APR_SUCCESS); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2136 | return; |
| 2137 | error_exit: |
| 2138 | close(lsock); |
| 2139 | free(ptids); |
| 2140 | return; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2143 | 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] | 2144 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2145 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 2146 | apr_pool_create(&config->pool, pool); |
| 2147 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2148 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2149 | |
| 2150 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2151 | } |
| 2152 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2153 | #ifndef HTTPD_INDEPENDENT |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2154 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 2155 | apr_pool_t * plog, server_rec * s) |
| 2156 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2157 | mod_netconf_cfg *config; |
| 2158 | apr_status_t res; |
| 2159 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2160 | /* These two help ensure that we only init once. */ |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 2161 | void *data = NULL; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2162 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2163 | |
| 2164 | /* |
| 2165 | * The following checks if this routine has been called before. |
| 2166 | * This is necessary because the parent process gets initialized |
| 2167 | * a couple of times as the server starts up. |
| 2168 | */ |
| 2169 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 2170 | if (!data) { |
| 2171 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 2172 | return (OK); |
| 2173 | } |
| 2174 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2175 | DEBUG("creating mod_netconf daemon"); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2176 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 2177 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2178 | if (config && config->forkproc == NULL) { |
| 2179 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 2180 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2181 | switch (res) { |
| 2182 | case APR_INCHILD: |
| 2183 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2184 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2185 | apr_signal(SIGTERM, signal_handler); |
| 2186 | |
| 2187 | /* log start of the separated NETCONF communication process */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2188 | DEBUG("mod_netconf daemon started (PID %d)", getpid()); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2189 | |
| 2190 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2191 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2192 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2193 | /* I never should be here, wtf?!? */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2194 | DEBUG("mod_netconf daemon unexpectedly stopped"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2195 | exit(APR_EGENERAL); |
| 2196 | break; |
| 2197 | case APR_INPARENT: |
| 2198 | /* 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] | 2199 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2200 | break; |
| 2201 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2202 | DEBUG("apr_proc_fork() failed"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2203 | break; |
| 2204 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2205 | } else { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2206 | DEBUG("mod_netconf misses configuration structure"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | return OK; |
| 2210 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2211 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2212 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2213 | /** |
| 2214 | * Register module hooks |
| 2215 | */ |
| 2216 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 2217 | { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2218 | #ifndef HTTPD_INDEPENDENT |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2219 | ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2220 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2223 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 2224 | { |
| 2225 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 2226 | return NULL; |
| 2227 | } |
| 2228 | |
| 2229 | static const command_rec netconf_cmds[] = { |
| 2230 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 2231 | {NULL} |
| 2232 | }; |
| 2233 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2234 | /* Dispatch list for API hooks */ |
| 2235 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 2236 | STANDARD20_MODULE_STUFF, |
| 2237 | NULL, /* create per-dir config structures */ |
| 2238 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 2239 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2240 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 2241 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 2242 | mod_netconf_register_hooks /* register hooks */ |
| 2243 | }; |
Radek Krejci | a332b69 | 2012-11-12 16:15:54 +0100 | [diff] [blame] | 2244 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 2245 | int main(int argc, char **argv) |
| 2246 | { |
| 2247 | apr_pool_t *pool; |
| 2248 | apr_app_initialize(&argc, (char const *const **) &argv, NULL); |
| 2249 | apr_signal(SIGTERM, signal_handler); |
| 2250 | apr_signal(SIGINT, signal_handler); |
| 2251 | apr_pool_create(&pool, NULL); |
| 2252 | forked_proc(pool, NULL); |
| 2253 | apr_pool_destroy(pool); |
| 2254 | apr_terminate(); |
| 2255 | DEBUG("Terminated"); |
| 2256 | return 0; |
| 2257 | } |