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