blob: 6def9682537961f78a25b12b4e5b45b8479153cf [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
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 Cejka94da2c52013-01-08 18:20:30 +01008 * \date 2013
Radek Krejci469aab82012-07-22 18:42:20 +02009 */
10/*
Tomas Cejkad340dbf2013-03-24 20:36:57 +010011 * Copyright (C) 2011-2013 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020012 *
13 * LICENSE TERMS
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 * 3. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * ALTERNATIVELY, provided that this notice is retained in full, this
29 * product may be distributed under the terms of the GNU General Public
30 * License (GPL) version 2 or later, in which case the provisions
31 * of the GPL apply INSTEAD OF those given above.
32 *
33 * This software is provided ``as is'', and any express or implied
34 * warranties, including, but not limited to, the implied warranties of
35 * merchantability and fitness for a particular purpose are disclaimed.
36 * In no event shall the company or contributors be liable for any
37 * direct, indirect, incidental, special, exemplary, or consequential
38 * damages (including, but not limited to, procurement of substitute
39 * goods or services; loss of use, data, or profits; or business
40 * interruption) however caused and on any theory of liability, whether
41 * in contract, strict liability, or tort (including negligence or
42 * otherwise) arising in any way out of the use of this software, even
43 * if advised of the possibility of such damage.
44 *
45 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +010046#ifdef ARCSID
Tomas Cejka689a1042013-01-16 15:08:25 +010047static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "ARCSID" $";
Tomas Cejkaef531ee2013-11-12 16:07:00 +010048#endif
Radek Krejci469aab82012-07-22 18:42:20 +020049
Radek Krejci7b4ddd02012-07-30 08:09:58 +020050#include <unistd.h>
51#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020052#include <sys/types.h>
53#include <sys/socket.h>
54#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010055#include <sys/fcntl.h>
David Kupka8e60a372012-09-04 09:15:20 +020056#include <pthread.h>
57#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020058
59#include <unixd.h>
60#include <httpd.h>
61#include <http_log.h>
62#include <http_config.h>
63
64#include <apr_sha1.h>
65#include <apr_hash.h>
66#include <apr_signal.h>
67#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020068
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020069#include <json/json.h>
70
Radek Krejci469aab82012-07-22 18:42:20 +020071#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020072#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020073
Tomas Cejkad340dbf2013-03-24 20:36:57 +010074#ifdef WITH_NOTIFICATIONS
75#include "notification_module.h"
76#endif
77
Tomas Cejka94da2c52013-01-08 18:20:30 +010078#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020079#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020080
81#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020082#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020083#define MAX_SOCKET_CL 10
84#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020085#define NOTIFICATION_QUEUE_SIZE 10
86#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020087#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020088
89/* sleep in master process for non-blocking socket reading */
90#define SLEEP_TIME 200
91
92#ifndef offsetof
93#define offsetof(type, member) ((size_t) ((type *) 0)->member)
94#endif
95
Tomas Cejkaef531ee2013-11-12 16:07:00 +010096server_rec *http_server = NULL;
97
Tomas Cejka027f3bc2012-11-10 20:28:36 +010098/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020099struct timeval timeout = { 1, 0 };
100
Tomas Cejka5064c232013-01-17 09:30:58 +0100101#define NCWITHDEFAULTS NCWD_MODE_NOTSET
102
103
Radek Krejci469aab82012-07-22 18:42:20 +0200104#define MSG_OK 0
105#define MSG_OPEN 1
106#define MSG_DATA 2
107#define MSG_CLOSE 3
108#define MSG_ERROR 4
109#define MSG_UNKNOWN 5
110
Radek Krejci469aab82012-07-22 18:42:20 +0200111module AP_MODULE_DECLARE_DATA netconf_module;
112
Tomas Cejka47387fd2013-06-10 20:37:46 +0200113pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200114pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100115pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200116apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200117
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200118static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200119
Radek Krejci469aab82012-07-22 18:42:20 +0200120volatile int isterminated = 0;
121
122static char* password;
123
Radek Krejci469aab82012-07-22 18:42:20 +0200124static void signal_handler(int sign)
125{
126 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100127 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200128 case SIGTERM:
129 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200130 break;
131 }
132}
133
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200134static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200135{
Radek Krejcif23850c2012-07-23 16:14:17 +0200136 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
137 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200138 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200139
Radek Krejci469aab82012-07-22 18:42:20 +0200140 apr_sha1_ctx_t sha1_ctx;
141 apr_sha1_init(&sha1_ctx);
142 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
143 apr_sha1_update(&sha1_ctx, port, strlen(port));
144 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200145 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200146
Radek Krejcif23850c2012-07-23 16:14:17 +0200147 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200148 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200149 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200150 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200151 }
152 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200153
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200154 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200155}
156
157int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
158{
159 /* always approve */
160 return (EXIT_SUCCESS);
161}
162
163char* netconf_callback_sshauth_password (const char* username, const char* hostname)
164{
165 char* buf;
166
167 buf = malloc ((strlen(password) + 1) * sizeof(char));
168 apr_cpystrn(buf, password, strlen(password) + 1);
169
170 return (buf);
171}
172
173void netconf_callback_sshauth_interactive (const char* name,
174 int name_len,
175 const char* instruction,
176 int instruction_len,
177 int num_prompts,
178 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
179 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
180 void** abstract)
181{
182 int i;
183
184 for (i=0; i<num_prompts; i++) {
185 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
186 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
187 responses[i].length = strlen(responses[i].text) + 1;
188 }
189
190 return;
191}
192
Radek Krejcic11fd862012-07-26 12:41:21 +0200193static json_object *err_reply = NULL;
194void netconf_callback_error_process(const char* tag,
195 const char* type,
196 const char* severity,
197 const char* apptag,
198 const char* path,
199 const char* message,
200 const char* attribute,
201 const char* element,
202 const char* ns,
203 const char* sid)
204{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100205 if (err_reply != NULL) {
206 json_object_put(err_reply);
207 err_reply = NULL;
208 }
Radek Krejcic11fd862012-07-26 12:41:21 +0200209 err_reply = json_object_new_object();
210 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
211 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
212 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
213 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
214 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
215 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
216 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
217 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
218 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
219 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
220 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
221}
222
Tomas Cejka47387fd2013-06-10 20:37:46 +0200223/**
224 * should be used in locked area
225 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100226void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200227{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200228 json_object *json_obj = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100229 char *old_sid = NULL;
230 const char *j_old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200231 const char *cpbltstr;
232 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200233
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200234 if (s == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100235 DEBUG("No session given.");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200236 return;
237 }
238
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200239 if (s->hello_message != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100240 DEBUG("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200241 //json_object_put(s->hello_message);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100242 j_old_sid = json_object_get_string(json_object_object_get(s->hello_message, "sid"));
243 if (j_old_sid != NULL) {
244 old_sid = strdup(j_old_sid);
245 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200246 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200247 }
248 s->hello_message = json_object_new_object();
249 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100250 if (old_sid != NULL) {
251 /* use previous sid */
252 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
253 free(old_sid);
254 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200255 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100256 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
257 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200258 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
259 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
260 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
261 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
262 cpblts = nc_session_get_cpblts (session);
263 if (cpblts != NULL) {
264 json_obj = json_object_new_array();
265 nc_cpblts_iter_start (cpblts);
266 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
267 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
268 }
269 json_object_object_add(s->hello_message, "capabilities", json_obj);
270 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100271 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200272 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100273 DEBUG("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200274 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
275 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
276 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100277 DEBUG("Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200278
279}
280
281
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200282/**
283 * \defgroup netconf_operations NETCONF operations
284 * The list of NETCONF operations that mod_netconf supports.
285 * @{
286 */
287
288/**
289 * \brief Connect to NETCONF server
290 *
291 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
292 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100293static char* netconf_connect(apr_pool_t* pool, const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200294{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200295 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200296 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200297 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200298
299 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200300 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100301 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200302 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200303 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100304 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200305
Radek Krejci469aab82012-07-22 18:42:20 +0200306 /* if connected successful, add session to the list */
307 if (session != NULL) {
308 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200309 session_key = gen_ncsession_hash(
310 (host==NULL) ? "localhost" : host,
311 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200312 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200313
Tomas Cejkaba21b382013-04-13 02:37:32 +0200314 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200315 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
316 nc_session_free(session);
317 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100318 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200319 return NULL;
320 }
321 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200322 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200323 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200324 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200325 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100326 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200327 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100328 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200329 if (pthread_rwlock_wrlock (&session_lock) != 0) {
330 nc_session_free(session);
331 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100332 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200333 return NULL;
334 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200335 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200336 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100337 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200338 apr_hash_set(netconf_sessions_list, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100339 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200340
Tomas Cejka47387fd2013-06-10 20:37:46 +0200341 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100342 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200343 pthread_mutex_lock(&locked_session->lock);
344
345 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100346 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200347 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100348 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200349 }
350
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200351 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100352 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200353
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100354 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200355 pthread_mutex_unlock(&locked_session->lock);
356
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100357 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200358 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200359 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100360 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200361 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200362 }
363
Radek Krejci469aab82012-07-22 18:42:20 +0200364}
365
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100366static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200367{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100368 DEBUG("lock private lock.");
369 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200370 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100371 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200372 }
373 locked_session->ntfc_subscribed = 0;
374 locked_session->closed = 1;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200375 nc_session_free(locked_session->session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100376 DEBUG("session closed.");
377 DEBUG("unlock private lock.");
378 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200379 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100380 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200381 }
382
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100383 DEBUG("unlock session lock.");
384 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200385 usleep(500000); /* let notification thread stop */
386
387 /* session shouldn't be used by now */
388 /** \todo free all notifications from queue */
389 apr_array_clear(locked_session->notifications);
390 pthread_mutex_destroy(&locked_session->lock);
391 if (locked_session->hello_message != NULL) {
392 /** \todo free hello_message */
393 //json_object_put(locked_session->hello_message);
394 locked_session->hello_message = NULL;
395 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200396 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200397 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200398 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100399 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200400 return (EXIT_SUCCESS);
401}
402
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100403static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200404{
David Kupka8e60a372012-09-04 09:15:20 +0200405 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200406
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100407 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200408
David Kupka8e60a372012-09-04 09:15:20 +0200409 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100410 DEBUG("lock session lock.");
411 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200412 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100413 DEBUG("Error while locking rwlock");
414 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200415 return EXIT_FAILURE;
416 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200417 /* get session to close */
418 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
419 /* remove session from the active sessions list -> nobody new can now work with session */
420 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200421
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100422 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200423 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100424 DEBUG("Error while unlocking rwlock");
425 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200426 }
427
428 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100429 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200430 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100431 DEBUG("Unknown session to close");
432 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200433 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200434 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100435 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200436}
437
Tomas Cejkac7929632013-10-24 19:25:15 +0200438/**
439 * Test reply message type and return error message.
440 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200441 * \param[in] session nc_session internal struct
442 * \param[in] session_key session key, NULL to disable disconnect on error
443 * \param[in] msgt RPC-REPLY message type
444 * \param[out] data
445 * \return NULL on success
446 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100447json_object *netconf_test_reply(struct nc_session *session, const char *session_key, NC_MSG_TYPE msgt, nc_reply *reply, char **data)
Tomas Cejkac7929632013-10-24 19:25:15 +0200448{
449 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100450 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200451
452 /* process the result of the operation */
453 switch (msgt) {
454 case NC_MSG_UNKNOWN:
455 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100456 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200457 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100458 netconf_close(session_key, &err);
459 }
460 if (err != NULL) {
461 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200462 }
463 return create_error("Internal: Receiving RPC-REPLY failed.");
464 }
465 case NC_MSG_NONE:
466 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100467 if (data != NULL) {
468 free(*data);
469 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200470 (*data) = NULL;
471 return NULL;
472 case NC_MSG_REPLY:
473 switch (replyt = nc_reply_get_type(reply)) {
474 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100475 if ((data != NULL) && (*data != NULL)) {
476 free(*data);
477 (*data) = NULL;
478 }
479 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200480 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100481 if (((*data) = nc_reply_get_data(reply)) == NULL) {
482 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200483 return create_error("Internal: No data from reply received.");
484 } else {
485 return NULL;
486 }
487 break;
488 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100489 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
490 if (data != NULL) {
491 free(*data);
492 (*data) = NULL;
493 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200494 return create_error(nc_reply_get_errormsg(reply));
495 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100496 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
497 if (data != NULL) {
498 free(*data);
499 (*data) = NULL;
500 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200501 return create_error(nc_reply_get_errormsg(reply));
502 }
503 break;
504 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100505 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
506 if (data != NULL) {
507 free(*data);
508 (*data) = NULL;
509 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200510 return create_error("Internal: Unexpected RPC-REPLY message type.");
511 }
512}
513
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100514json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200515{
516 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200517 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200518
519 /* check requests */
520 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100521 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200522 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200523 }
524
525 if (session != NULL) {
526 /* send the request and get the reply */
527 msgt = nc_session_send_recv(session, rpc, &reply);
528 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100529 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200530 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100531 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200532 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200533 }
534}
535
Tomas Cejkac7929632013-10-24 19:25:15 +0200536/**
537 * Perform RPC method that returns data.
538 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200539 * \param[in] session_key session identifier
540 * \param[in] rpc RPC message to perform
541 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
542 * \return NULL on success, json object with error otherwise
543 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100544static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200545{
546 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200547 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200548 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200549 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100550 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100551 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200552
Radek Krejci8e4632a2012-07-26 13:40:34 +0200553 /* check requests */
554 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100555 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200556 res = create_error("Internal: RPC could not be created.");
557 data = NULL;
558 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200559 }
560
David Kupka8e60a372012-09-04 09:15:20 +0200561 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100562 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200563 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100564 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200565 res = create_error("Internal: Lock failed.");
566 data = NULL;
567 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200568 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200569 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200570 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
David Kupka8e60a372012-09-04 09:15:20 +0200571 if (locked_session != NULL) {
572 session = locked_session->session;
573 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200574 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200575 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100576 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200577 if (pthread_mutex_lock(&locked_session->lock) != 0) {
578 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100579 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200580 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200581
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100582 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200583 res = create_error("Internal: Could not unlock.");
584 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200585 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200586 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200587 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100588 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200589 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200590
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100591 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200592 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200593 }
594
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200595 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200596
Radek Krejci035bf4e2012-07-25 10:59:09 +0200597 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100598 msgt = nc_session_send_recv(session, rpc, &reply);
599
David Kupka8e60a372012-09-04 09:15:20 +0200600 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100601 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200602 pthread_mutex_unlock(&locked_session->lock);
603 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200604
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100605 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200606 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100607 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100608 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100609 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100610 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100611 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100612 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200613 res = create_error("Unknown session to process.");
614 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200615 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200616finished:
617 nc_reply_free(reply);
618 if (received_data != NULL) {
619 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100620 } else {
621 if (data != NULL) {
622 free(data);
623 data = NULL;
624 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200625 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200626 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200627}
628
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100629static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200630{
631 nc_rpc* rpc;
632 struct nc_filter *f = NULL;
633 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200634 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200635
636 /* create filter if set */
637 if (filter != NULL) {
638 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
639 }
640
641 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100642 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200643 nc_filter_free(f);
644 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100645 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200646 return (NULL);
647 }
648
Tomas Cejka94674662013-09-13 15:55:24 +0200649 /* tell server to show all elements even if they have default values */
650 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100651 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200652 }
653
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100654 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200655 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100656 if (res != NULL) {
657 (*err) = res;
658 } else {
659 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200660 }
661
Radek Krejci8e4632a2012-07-26 13:40:34 +0200662 return (data);
663}
664
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100665static char* netconf_getschema(const char* session_key, const char* identifier, const char* version, const char* format, json_object **err)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100666{
667 nc_rpc* rpc;
668 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200669 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100670
671 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100672 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100673 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100674 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100675 return (NULL);
676 }
677
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100678 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100679 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100680 if (res != NULL) {
681 (*err) = res;
682 } else {
683 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200684 }
685
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100686 return (data);
687}
688
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100689static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200690{
691 nc_rpc* rpc;
692 struct nc_filter *f = NULL;
693 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200694 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200695
696 /* create filter if set */
697 if (filter != NULL) {
698 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
699 }
700
701 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100702 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200703 nc_filter_free(f);
704 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100705 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200706 return (NULL);
707 }
708
Tomas Cejka94674662013-09-13 15:55:24 +0200709 /* tell server to show all elements even if they have default values */
710 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100711 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200712 }
713
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100714 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200715 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200716 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100717 (*err) = res;
718 } else {
719 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200720 }
721
Radek Krejci8e4632a2012-07-26 13:40:34 +0200722 return (data);
723}
724
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100725static json_object *netconf_copyconfig(const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *url)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200726{
727 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200728 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200729
730 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100731 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
732 if (target == NC_DATASTORE_URL) {
733 rpc = nc_rpc_copyconfig(source, target, config, url);
734 } else {
735 rpc = nc_rpc_copyconfig(source, target, config);
736 }
737 } else {
738 if (target == NC_DATASTORE_URL) {
739 rpc = nc_rpc_copyconfig(source, target, url);
740 } else {
741 rpc = nc_rpc_copyconfig(source, target);
742 }
743 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200744 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100745 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200746 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200747 }
748
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100749 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200750 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200751
752 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200753}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200754
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100755static json_object *netconf_editconfig(const char* session_key, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, NC_EDIT_TESTOPT_TYPE testopt, const char* config)
Radek Krejci62ab34b2012-07-26 13:42:05 +0200756{
757 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200758 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200759
760 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100761 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
762 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200763 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100764 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200765 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200766 }
767
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100768 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200769 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200770
771 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200772}
773
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100774static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200775{
776 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200777 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200778
779 /* create requests */
780 rpc = nc_rpc_killsession(sid);
781 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100782 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200783 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200784 }
785
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100786 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200787 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200788 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200789}
790
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100791static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200792{
793 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200794 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200795
796 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200797 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200798 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100799 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200800 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200801 }
802
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100803 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200804 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200805 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200806}
807
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100808static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200809{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200810 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200811 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200812 if (target != NC_DATASTORE_URL) {
813 rpc = nc_rpc_deleteconfig(target);
814 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200815 rpc = nc_rpc_deleteconfig(target, url);
816 }
817 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100818 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200819 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200820 }
821
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100822 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200823 nc_rpc_free (rpc);
824 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200825}
826
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100827static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200828{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100829 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200830}
831
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100832static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200833{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100834 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200835}
836
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100837static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200838{
Tomas Cejka00635972013-06-03 15:10:52 +0200839 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200840 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200841
842 /* create requests */
843 rpc = nc_rpc_generic(content);
844 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100845 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200846 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200847 }
848
Radek Krejcia332b692012-11-12 16:15:54 +0100849 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200850 // TODO ?free(*data);
851 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100852 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200853
854 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100855 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200856 nc_rpc_free (rpc);
857 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200858}
859
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200860/**
861 * @}
862 *//* netconf_operations */
863
Radek Krejci7338bde2012-08-10 12:57:30 +0200864void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200865{
Radek Krejci7338bde2012-08-10 12:57:30 +0200866 switch (level) {
867 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100868 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200869 break;
870 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100871 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200872 break;
873 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100874 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200875 break;
876 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100877 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200878 break;
879 }
Radek Krejci469aab82012-07-22 18:42:20 +0200880}
881
Tomas Cejka64b87482013-06-03 16:30:53 +0200882/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200883 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200884 * Caller should free message memory.
885 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200886 * \return pointer to message
887 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100888char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200889{
890 /* read json in chunked framing */
891 unsigned int buffer_size = 0;
892 ssize_t buffer_len = 0;
893 char *buffer = NULL;
894 char c;
895 ssize_t ret;
896 int i, chunk_len;
897 char chunk_len_str[12];
898
899 while (1) {
900 /* read chunk length */
901 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
902 if (buffer != NULL) {
903 free (buffer);
904 buffer = NULL;
905 }
906 break;
907 }
908 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
909 if (buffer != NULL) {
910 free (buffer);
911 buffer = NULL;
912 }
913 break;
914 }
915 i=0;
916 memset (chunk_len_str, 0, 12);
917 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
918 if (i==0 && c == '#') {
919 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
920 /* end but invalid */
921 if (buffer != NULL) {
922 free (buffer);
923 buffer = NULL;
924 }
925 }
926 /* end of message, double-loop break */
927 goto msg_complete;
928 }
929 chunk_len_str[i++] = c;
930 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100931 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +0200932 break;
933 }
934 }
935 if (c != '\n') {
936 if (buffer != NULL) {
937 free (buffer);
938 buffer = NULL;
939 }
940 break;
941 }
942 chunk_len_str[i] = 0;
943 if ((chunk_len = atoi (chunk_len_str)) == 0) {
944 if (buffer != NULL) {
945 free (buffer);
946 buffer = NULL;
947 }
948 break;
949 }
950 buffer_size += chunk_len+1;
951 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100952 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +0200953 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
954 if (buffer != NULL) {
955 free (buffer);
956 buffer = NULL;
957 }
958 break;
959 }
960 buffer_len += ret;
961 }
962msg_complete:
963 return buffer;
964}
965
Tomas Cejkad5b53772013-06-08 23:01:07 +0200966NC_DATASTORE parse_datastore(const char *ds)
967{
968 if (strcmp(ds, "running") == 0) {
969 return NC_DATASTORE_RUNNING;
970 } else if (strcmp(ds, "startup") == 0) {
971 return NC_DATASTORE_STARTUP;
972 } else if (strcmp(ds, "candidate") == 0) {
973 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +0200974 } else if (strcmp(ds, "url") == 0) {
975 return NC_DATASTORE_URL;
Tomas Cejkad5b53772013-06-08 23:01:07 +0200976 }
977 return -1;
978}
979
980json_object *create_error(const char *errmess)
981{
982 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100983 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +0200984 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100985 json_object_array_add(array, json_object_new_string(errmess));
986 json_object_object_add(reply, "errors", array);
Tomas Cejkad5b53772013-06-08 23:01:07 +0200987 return reply;
988
989}
990
991json_object *create_data(const char *data)
992{
993 json_object *reply = json_object_new_object();
994 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
995 json_object_object_add(reply, "data", json_object_new_string(data));
996 return reply;
997}
998
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100999json_object *create_ok()
1000{
1001 json_object *reply = json_object_new_object();
1002 reply = json_object_new_object();
1003 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1004 return reply;
1005}
1006
1007json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001008{
1009 const char *host = NULL;
1010 const char *port = NULL;
1011 const char *user = NULL;
1012 const char *pass = NULL;
1013 json_object *capabilities = NULL;
1014 json_object *reply = NULL;
1015 char *session_key_hash = NULL;
1016 struct nc_cpblts* cpblts = NULL;
1017 unsigned int len, i;
1018
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001019 DEBUG("Request: Connect");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001020 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1021 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1022 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1023 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1024
1025 capabilities = json_object_object_get((json_object *) request, "capabilities");
1026 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1027 cpblts = nc_cpblts_new(NULL);
1028 for (i=0; i<len; i++) {
1029 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1030 }
1031 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001032 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001033 }
1034
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001035 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001036 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001037 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001038 session_key_hash = NULL;
1039 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001040 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1041 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001042 }
1043 if (cpblts != NULL) {
1044 nc_cpblts_free(cpblts);
1045 }
1046
1047 if (session_key_hash == NULL) {
1048 /* negative reply */
1049 if (err_reply == NULL) {
1050 reply = json_object_new_object();
1051 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1052 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001053 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001054 } else {
1055 /* use filled err_reply from libnetconf's callback */
1056 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001057 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001058 }
1059 } else {
1060 /* positive reply */
1061 reply = json_object_new_object();
1062 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1063 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1064
1065 free(session_key_hash);
1066 }
1067 return reply;
1068}
1069
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001070json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001071{
1072 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001073 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001074 json_object *reply = NULL;
1075
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001076 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001077
1078 filter = json_object_get_string(json_object_object_get(request, "filter"));
1079
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001080 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
1081 if (reply == NULL) {
1082 if (err_reply == NULL) {
1083 reply = create_error("Get information failed.");
1084 } else {
1085 /* use filled err_reply from libnetconf's callback */
1086 reply = err_reply;
1087 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001088 }
1089 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001090 reply = create_data(data);
1091 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001092 }
1093 return reply;
1094}
1095
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001096json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001097{
1098 NC_DATASTORE ds_type_s = -1;
1099 NC_DATASTORE ds_type_t = -1;
1100 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001101 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001102 const char *source = NULL;
1103 const char *target = NULL;
1104 json_object *reply = NULL;
1105
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001106 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001107
1108 filter = json_object_get_string(json_object_object_get(request, "filter"));
1109
1110 /* get parameters */
1111 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1112 ds_type_t = parse_datastore(target);
1113 }
1114 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1115 ds_type_s = parse_datastore(source);
1116 }
1117 if (ds_type_s == -1) {
1118 return create_error("Invalid source repository type requested.");
1119 }
1120
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001121 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
1122 if (reply == NULL) {
1123 if (err_reply == NULL) {
1124 reply = create_error("Get configuration operation failed.");
1125 } else {
1126 /* use filled err_reply from libnetconf's callback */
1127 reply = err_reply;
1128 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001129 }
1130 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001131 reply = create_data(data);
1132 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001133 }
1134 return reply;
1135}
1136
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001137json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001138{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001139 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001140 const char *identifier = NULL;
1141 const char *version = NULL;
1142 const char *format = NULL;
1143 json_object *reply = NULL;
1144
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001145 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001146 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1147 if (identifier == NULL) {
1148 return create_error("No identifier for get-schema supplied.");
1149 }
1150 version = json_object_get_string(json_object_object_get(request, "version"));
1151 format = json_object_get_string(json_object_object_get(request, "format"));
1152
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001153 DEBUG("get-schema(version: %s, format: %s)", version, format);
1154 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
1155 if (reply == NULL) {
1156 if (err_reply == NULL) {
1157 reply = create_error("Get models operation failed.");
1158 } else {
1159 /* use filled err_reply from libnetconf's callback */
1160 reply = err_reply;
1161 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001162 }
1163 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001164 reply = create_data(data);
1165 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001166 }
1167 return reply;
1168}
1169
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001170json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001171{
1172 NC_DATASTORE ds_type_s = -1;
1173 NC_DATASTORE ds_type_t = -1;
1174 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1175 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1176 const char *defop = NULL;
1177 const char *erropt = NULL;
1178 const char *config = NULL;
1179 const char *source = NULL;
1180 const char *target = NULL;
1181 json_object *reply = NULL;
1182
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001183 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001184
1185 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1186 if (defop != NULL) {
1187 if (strcmp(defop, "merge") == 0) {
1188 defop_type = NC_EDIT_DEFOP_MERGE;
1189 } else if (strcmp(defop, "replace") == 0) {
1190 defop_type = NC_EDIT_DEFOP_REPLACE;
1191 } else if (strcmp(defop, "none") == 0) {
1192 defop_type = NC_EDIT_DEFOP_NONE;
1193 } else {
1194 return create_error("Invalid default-operation parameter.");
1195 }
1196 } else {
1197 defop_type = NC_EDIT_DEFOP_NOTSET;
1198 }
1199
1200 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1201 if (erropt != NULL) {
1202 if (strcmp(erropt, "continue-on-error") == 0) {
1203 erropt_type = NC_EDIT_ERROPT_CONT;
1204 } else if (strcmp(erropt, "stop-on-error") == 0) {
1205 erropt_type = NC_EDIT_ERROPT_STOP;
1206 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1207 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1208 } else {
1209 return create_error("Invalid error-option parameter.");
1210 }
1211 } else {
1212 erropt_type = 0;
1213 }
1214
1215 /* get parameters */
1216 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1217 ds_type_t = parse_datastore(target);
1218 }
1219 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1220 ds_type_s = parse_datastore(source);
1221 }
1222 if (ds_type_t == -1) {
1223 return create_error("Invalid target repository type requested.");
1224 }
1225
1226 config = json_object_get_string(json_object_object_get(request, "config"));
1227 if (config == NULL) {
1228 return create_error("Invalid config data parameter.");
1229 }
1230
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001231 reply = netconf_editconfig(session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_TESTSET, config);
1232 if (reply == NULL) {
1233 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001234 /* use filled err_reply from libnetconf's callback */
1235 reply = err_reply;
1236 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001237 }
1238 return reply;
1239}
1240
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001241json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001242{
1243 NC_DATASTORE ds_type_s = -1;
1244 NC_DATASTORE ds_type_t = -1;
1245 const char *config = NULL;
1246 const char *target = NULL;
1247 const char *source = NULL;
1248 json_object *reply = NULL;
1249
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001250 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001251
1252 /* get parameters */
1253 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1254 ds_type_t = parse_datastore(target);
1255 }
1256 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1257 ds_type_s = parse_datastore(source);
1258 }
1259 if (source == NULL) {
1260 /* no explicit source specified -> use config data */
1261 ds_type_s = NC_DATASTORE_CONFIG;
1262 config = json_object_get_string(json_object_object_get(request, "config"));
1263 } else if (ds_type_s == -1) {
1264 /* source datastore specified, but it is invalid */
1265 return create_error("Invalid source repository type requested.");
1266 }
1267
1268 if (ds_type_t == -1) {
1269 /* invalid target datastore specified */
1270 return create_error("Invalid target repository type requested.");
1271 }
1272
1273 if (source == NULL && config == NULL) {
1274 reply = create_error("invalid input parameters - one of source and config is required.");
1275 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001276 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, "");
1277 if (reply == NULL) {
1278 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001279 /* use filled err_reply from libnetconf's callback */
1280 reply = err_reply;
1281 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001282 }
1283 }
1284 return reply;
1285}
1286
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001287json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001288{
1289 json_object *reply = NULL;
1290 const char *config = NULL;
1291 char *data = NULL;
1292
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001293 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001294
1295 config = json_object_get_string(json_object_object_get(request, "content"));
1296
1297 if (config == NULL) {
1298 return create_error("Missing content parameter.");
1299 }
1300
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001301 /* TODO */
1302 reply = netconf_generic(session_key, config, &data);
1303 if (reply == NULL) {
1304 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001305 /* use filled err_reply from libnetconf's callback */
1306 reply = err_reply;
1307 }
1308 } else {
1309 if (data == NULL) {
1310 reply = json_object_new_object();
1311 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1312 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001313 reply = create_data(data);
1314 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001315 }
1316 }
1317 return reply;
1318}
1319
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001320json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001321{
1322 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001323 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001324
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001325 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
1326 if (reply == NULL) {
1327 if (err_reply == NULL) {
1328 reply = create_error("Get configuration information from device failed.");
1329 } else {
1330 /* use filled err_reply from libnetconf's callback */
1331 reply = err_reply;
1332 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001333 }
1334 } else {
1335 reply = json_object_new_object();
1336 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1337 }
1338 return reply;
1339}
1340
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001341json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001342{
1343 json_object *reply = NULL;
1344 const char *sid = NULL;
1345
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001346 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001347
1348 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1349
1350 if (sid == NULL) {
1351 return create_error("Missing session-id parameter.");
1352 }
1353
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001354 reply = netconf_killsession(session_key, sid);
1355 if (reply == NULL) {
1356 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001357 /* use filled err_reply from libnetconf's callback */
1358 reply = err_reply;
1359 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001360 }
1361 return reply;
1362}
1363
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001364json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001365{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001366 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001367 struct session_with_mutex * locked_session = NULL;
1368 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001369 DEBUG("Request: get info about session %s", session_key);
1370 return create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001371
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001372 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001373 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001374 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001375 return NULL;
1376 }
1377
Tomas Cejkad5b53772013-06-08 23:01:07 +02001378 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1379 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001380 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001381 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001382 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001383 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001384 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001385 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001386 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001387 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001388 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001389 prepare_status_message(locked_session, temp_session);
1390 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001391 nc_session_free(temp_session);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001392 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001393 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001394 reply = create_error("Reload was unsuccessful, connection failed.");
1395 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001396 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001397 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001398 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001399 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001400 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001401 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001402 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001403 reply = create_error("Invalid session identifier.");
1404 }
1405
1406 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1407 reply = locked_session->hello_message;
1408 }
1409 return reply;
1410}
1411
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001412json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001413{
1414 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001415 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001416 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001417
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001418 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001419 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001420 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001421 }
1422
Tomas Cejkad5b53772013-06-08 23:01:07 +02001423 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1424 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001425 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001426 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001427 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001428 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001429 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001430 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001431 if (locked_session->hello_message != NULL) {
1432 reply = locked_session->hello_message;
1433 } else {
1434 reply = create_error("Invalid session identifier.");
1435 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001436 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001437 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001438 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001439 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001440 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001441 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001442 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001443 reply = create_error("Invalid session identifier.");
1444 }
1445
Tomas Cejka47387fd2013-06-10 20:37:46 +02001446
Tomas Cejkad5b53772013-06-08 23:01:07 +02001447 return reply;
1448}
1449
Tomas Cejka6b886e02013-07-05 09:53:17 +02001450void notification_history(time_t eventtime, const char *content)
1451{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001452 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1453 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001454 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001455 return;
1456 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001457 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001458 json_object *notif = json_object_new_object();
1459 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001460 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001461 return;
1462 }
1463 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1464 json_object_object_add(notif, "content", json_object_new_string(content));
1465 json_object_array_add(notif_history_array, notif);
1466}
1467
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001468json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001469{
1470 json_object *reply = NULL;
1471 const char *sid = NULL;
1472 struct session_with_mutex *locked_session = NULL;
1473 struct nc_session *temp_session = NULL;
1474 nc_rpc *rpc = NULL;
1475 time_t start = 0;
1476 time_t stop = 0;
1477 int64_t from, to;
1478
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001479 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001480
1481 sid = json_object_get_string(json_object_object_get(request, "session"));
1482 from = json_object_get_int64(json_object_object_get(request, "from"));
1483 to = json_object_get_int64(json_object_object_get(request, "to"));
1484
1485 start = time(NULL) + from;
1486 stop = time(NULL) + to;
1487
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001488 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001489
1490 if (sid == NULL) {
1491 return create_error("Missing session parameter.");
1492 }
1493
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001494 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001495 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001496 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001497 return NULL;
1498 }
1499
1500 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1501 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001502 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001503 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001504 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001505 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001506 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001507 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001508 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001509 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1510 if (temp_session != NULL) {
1511 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1512 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001513 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001514 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001515 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001516 return create_error("notifications: creating an rpc request failed.");
1517 }
1518
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001519 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001520 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001521 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001522 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001523 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001524 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001525 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001526 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001527 }
1528 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1529
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001530 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001531 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001532 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001533 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001534 json_object *notif_history_array = json_object_new_array();
1535 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001536 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001537 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001538
1539 ncntf_dispatch_receive(temp_session, notification_history);
1540
1541 reply = json_object_new_object();
1542 json_object_object_add(reply, "notifications", notif_history_array);
1543 //json_object_put(notif_history_array);
1544
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001545 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001546 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001547 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001548 nc_session_free(temp_session);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001549 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001550 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001551 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001552 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001553 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1554 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001555 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001556 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001557 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001558 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001559 }
1560 reply = create_error("Invalid session identifier.");
1561 }
1562
Tomas Cejka4003a702013-10-01 00:02:45 +02001563 return reply;
1564}
1565
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001566json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001567{
1568 json_object *reply = NULL;
1569 const char *sid = NULL;
1570 const char *target = NULL;
1571 const char *url = NULL;
1572 struct session_with_mutex *locked_session = NULL;
1573 nc_rpc *rpc = NULL;
1574 NC_DATASTORE target_ds;
1575
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001576 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001577
1578 sid = json_object_get_string(json_object_object_get(request, "session"));
1579 target = json_object_get_string(json_object_object_get(request, "target"));
1580 url = json_object_get_string(json_object_object_get(request, "url"));
1581
1582
1583 if ((sid == NULL) || (target == NULL)) {
1584 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001585 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001586
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001587 /* validation */
1588 target_ds = parse_datastore(target);
1589 if (target_ds == NC_DATASTORE_URL) {
1590 if (url != NULL) {
1591 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001592 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001593 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001594 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001595 rpc = nc_rpc_validate(target_ds);
1596 }
1597 if (rpc == NULL) {
1598 DEBUG("mod_netconf: creating rpc request failed");
1599 reply = create_error("Creation of RPC request failed.");
1600 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka4003a702013-10-01 00:02:45 +02001601 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001602 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001603 }
1604
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001605 DEBUG("Request: validate datastore");
1606 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
1607 reply = json_object_new_object();
1608 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1609 }
1610 nc_rpc_free (rpc);
1611
1612 DEBUG("Request: validate datastore");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001613 return reply;
1614}
1615
David Kupka8e60a372012-09-04 09:15:20 +02001616void * thread_routine (void * arg)
1617{
1618 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001619 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001620 json_object *request = NULL;
1621 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001622 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001623 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001624 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001625 const char *session_key;
1626 const char *target = NULL;
1627 const char *source = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001628 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001629 NC_DATASTORE ds_type_t = -1;
1630 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001631 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001632 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001633 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001634 int client = ((struct pass_to_thread*)arg)->client;
1635
Tomas Cejka00635972013-06-03 15:10:52 +02001636 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001637
1638 while (!isterminated) {
1639 fds.fd = client;
1640 fds.events = POLLIN;
1641 fds.revents = 0;
1642
1643 status = poll(&fds, 1, 1000);
1644
1645 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1646 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001647 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001648 continue;
1649 } else if (status < 0) {
1650 /* 0: poll time outed
1651 * close socket and ignore this request from the client, it can try it again
1652 * -1: poll failed
1653 * something wrong happend, close this socket and wait for another request
1654 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001655 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001656 close(client);
1657 break;
1658 }
1659 /* status > 0 */
1660
1661 /* check the status of the socket */
1662
1663 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1664 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1665 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001666 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001667 close(client);
1668 break;
1669 }
1670
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001671 DEBUG("Get framed message...");
1672 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001673
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001674 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001675 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001676 enum json_tokener_error jerr;
1677 request = json_tokener_parse_verbose(buffer, &jerr);
1678 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001679 DEBUG("JSON parsing error");
Tomas Cejka00635972013-06-03 15:10:52 +02001680 continue;
1681 }
David Kupka8e60a372012-09-04 09:15:20 +02001682 operation = json_object_get_int(json_object_object_get(request, "type"));
1683
Tomas Cejka64b87482013-06-03 16:30:53 +02001684 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001685 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001686 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1687 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001688 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001689 msgtext = json_object_to_json_string(reply);
1690 send(client, msgtext, strlen(msgtext) + 1, 0);
1691 json_object_put(reply);
1692 /* there is some stupid client, so close the connection to give a chance to some other client */
1693 close(client);
1694 break;
1695 }
1696
David Kupka8e60a372012-09-04 09:15:20 +02001697 /* null global JSON error-reply */
1698 err_reply = NULL;
1699
1700 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001701 if (reply != NULL) {
1702 json_object_put(reply);
1703 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001704 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001705
1706 /* process required operation */
1707 switch (operation) {
1708 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001709 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001710 break;
1711 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001712 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001713 break;
1714 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001715 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001716 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001717 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001718 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001719 break;
David Kupka8e60a372012-09-04 09:15:20 +02001720 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001721 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001722 break;
1723 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001724 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001725 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001726
David Kupka8e60a372012-09-04 09:15:20 +02001727 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001728 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001729 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001730 /* get parameters */
1731 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1732 ds_type_t = parse_datastore(target);
1733 }
1734 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1735 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001736 }
1737
1738 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001739 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001740 break;
1741 }
David Kupka8e60a372012-09-04 09:15:20 +02001742 switch(operation) {
1743 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001744 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejkac7929632013-10-24 19:25:15 +02001745 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001746 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001747 break;
1748 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001749 DEBUG("Request: lock (session %s)", session_key);
1750 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001751 break;
1752 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001753 DEBUG("Request: unlock (session %s)", session_key);
1754 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001755 break;
1756 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001757 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001758 break;
1759 }
1760
Tomas Cejkac7929632013-10-24 19:25:15 +02001761 if (reply == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001762 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001763 /** \todo more clever error message wanted */
Tomas Cejkac7929632013-10-24 19:25:15 +02001764 reply = json_object_new_object();
1765 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
David Kupka8e60a372012-09-04 09:15:20 +02001766 } else {
1767 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001768 reply = err_reply;
1769 }
David Kupka8e60a372012-09-04 09:15:20 +02001770 }
1771 break;
1772 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001773 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001774 break;
1775 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001776 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001777 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001778 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001779 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001780 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001781 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001782 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001783 break;
1784 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001785 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001786 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001787 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001788 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001789 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001790 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001791 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001792 break;
David Kupka8e60a372012-09-04 09:15:20 +02001793 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001794 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001795 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001796 break;
1797 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001798 DEBUG("Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001799 json_object_put(request);
1800
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001801 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001802 /* send reply to caller */
1803 if (reply != NULL) {
1804 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001805 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001806 if (buffer != NULL) {
1807 free(buffer);
1808 buffer = NULL;
1809 }
David Kupka8e60a372012-09-04 09:15:20 +02001810 break;
1811 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001812 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001813 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001814 DEBUG("Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001815 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001816 reply = NULL;
1817 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001818 free(chunked_out_msg);
1819 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001820 if (buffer != NULL) {
1821 free(buffer);
1822 buffer = NULL;
1823 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001824 if (err_reply != NULL) {
1825 json_object_put(err_reply);
1826 err_reply = NULL;
1827 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001828 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001829 DEBUG("Reply is NULL, shouldn't be...");
1830 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001831 }
1832 }
1833 }
David Kupka8e60a372012-09-04 09:15:20 +02001834 free (arg);
1835
1836 return retval;
1837}
1838
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001839/**
1840 * \brief Close all open NETCONF sessions.
1841 *
1842 * During termination of mod_netconf, it is useful to close all remaining
1843 * sessions. This function iterates over the list of sessions and close them
1844 * all.
1845 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001846 * \param[in] p apr pool needed for hash table iterating
1847 * \param[in] ht hash table of session_with_mutex structs
1848 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001849static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001850{
1851 apr_hash_index_t *hi;
1852 void *val = NULL;
1853 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001854 const char *hashed_key = NULL;
1855 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001856 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001857
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001858 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001859 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001860 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001861 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001862 return;
1863 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001864 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001865 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001866 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001867 swm = (struct session_with_mutex *) val;
1868 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001869 DEBUG("LOCK mutex %s", __func__);
1870 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001871 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001872 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001873 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001874
1875 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001876 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001877 }
1878 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001879 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001880 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001881 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001882 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001883 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001884}
1885
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001886static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001887{
1888 apr_hash_index_t *hi;
1889 void *val = NULL;
1890 struct nc_session *ns = NULL;
1891 struct session_with_mutex *swm = NULL;
1892 const char *hashed_key = NULL;
1893 apr_ssize_t hashed_key_length;
1894 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001895 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001896
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001897 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001898//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001899 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001900 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001901 return;
1902 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001903 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001904 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1905 swm = (struct session_with_mutex *) val;
1906 if (swm == NULL) {
1907 continue;
1908 }
1909 ns = swm->session;
1910 if (ns == NULL) {
1911 continue;
1912 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001913//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001914 pthread_mutex_lock(&swm->lock);
1915 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001916 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001917 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001918 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001919//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001920 pthread_mutex_unlock(&swm->lock);
1921
1922 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001923 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001924 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001925//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001926 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001927 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001928 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001929 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001930//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001931 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001932 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001933 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001934}
1935
1936
1937/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001938 * This is actually implementation of NETCONF client
1939 * - requests are received from UNIX socket in the predefined format
1940 * - results are replied through the same way
1941 * - the daemon run as a separate process, but it is started and stopped
1942 * automatically by Apache.
1943 *
1944 */
Radek Krejci469aab82012-07-22 18:42:20 +02001945static void forked_proc(apr_pool_t * pool, server_rec * server)
1946{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001947 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001948 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001949 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001950 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001951 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001952 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02001953 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001954 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02001955 struct timespec maxtime;
1956 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001957 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001958 #ifdef WITH_NOTIFICATIONS
1959 char use_notifications = 0;
1960 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001961
Tomas Cejka6b886e02013-07-05 09:53:17 +02001962 http_server = server;
1963
Tomas Cejka404d37e2013-04-13 02:31:35 +02001964 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001965 maxtime.tv_sec = 5;
1966 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001967
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001968 #ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02001969 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001970 unixd_setup_child();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001971 #endif
Radek Krejci469aab82012-07-22 18:42:20 +02001972
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001973 if (server != NULL) {
1974 cfg = ap_get_module_config(server->module_config, &netconf_module);
1975 if (cfg == NULL) {
1976 DEBUG("Getting mod_netconf configuration failed");
1977 return;
1978 }
1979 sockname = cfg->sockname;
1980 } else {
1981 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001982 }
Radek Krejci469aab82012-07-22 18:42:20 +02001983
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001984 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001985 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001986 DEBUG("Creating socket failed (%s)", strerror(errno));
1987 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02001988 }
1989
1990 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001991 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001992 unlink(local.sun_path);
1993 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1994
1995 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1996 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001997 DEBUG("mod_netconf socket address already in use");
1998 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02001999 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002000 DEBUG("Binding socket failed (%s)", strerror(errno));
2001 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002002 }
2003
2004 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002005 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2006 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002007 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002008 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002009
Tomas Cejkaba21b382013-04-13 02:37:32 +02002010 /* prepare internal lists */
2011 netconf_sessions_list = apr_hash_make(pool);
2012
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002013 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002014 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002015 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002016 use_notifications = 0;
2017 } else {
2018 use_notifications = 1;
2019 }
2020 #endif
2021
Radek Krejci469aab82012-07-22 18:42:20 +02002022 /* setup libnetconf's callbacks */
2023 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002024 nc_callback_print(clb_print);
2025 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2026 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2027 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002028 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002029
2030 /* disable publickey authentication */
2031 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2032
David Kupka8e60a372012-09-04 09:15:20 +02002033 /* create mutex protecting session list */
2034 pthread_rwlockattr_init(&lock_attrs);
2035 /* rwlock is shared only with threads in this process */
2036 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2037 /* create rw lock */
2038 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002039 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2040 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002041 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002042 pthread_mutex_init(&ntf_history_lock, NULL);
2043 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002044 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002045 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002046 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002047
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002048 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002049 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002050 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002051 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002052 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002053 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002054 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002055 }
2056 if (use_notifications == 1) {
2057 notification_handle();
2058 }
2059 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002060 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002061 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002062 }
Radek Krejci469aab82012-07-22 18:42:20 +02002063
2064 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002065 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002066 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002067 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002068 olds = tv.tv_sec;
2069 }
David Kupka8e60a372012-09-04 09:15:20 +02002070 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002071 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2072 apr_sleep(SLEEP_TIME);
2073 continue;
2074 } else if (client == -1 && (errno == EINTR)) {
2075 continue;
2076 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002077 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002078 continue;
2079 }
Radek Krejci469aab82012-07-22 18:42:20 +02002080
2081 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002082 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002083
David Kupka8e60a372012-09-04 09:15:20 +02002084 arg = malloc (sizeof(struct pass_to_thread));
2085 arg->client = client;
2086 arg->pool = pool;
2087 arg->server = server;
2088 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002089
David Kupka8e60a372012-09-04 09:15:20 +02002090 /* start new thread. It will serve this particular request and then terminate */
2091 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002092 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002093 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002094 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002095 pthread_count++;
2096 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2097 ptids[pthread_count] = 0;
2098 }
Radek Krejci469aab82012-07-22 18:42:20 +02002099
David Kupka8e60a372012-09-04 09:15:20 +02002100 /* check if some thread already terminated, free some resources by joining it */
2101 for (i=0; i<pthread_count; i++) {
2102 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002103 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002104 pthread_count--;
2105 if (pthread_count > 0) {
2106 /* place last Thread ID on the place of joined one */
2107 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002108 }
Radek Krejci469aab82012-07-22 18:42:20 +02002109 }
2110 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002111 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002112 }
2113
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002114 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002115 /* join all threads */
2116 for (i=0; i<pthread_count; i++) {
2117 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2118 }
Radek Krejci469aab82012-07-22 18:42:20 +02002119
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002120 #ifdef WITH_NOTIFICATIONS
2121 notification_close();
2122 #endif
2123
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002124 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002125 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002126
David Kupka8e60a372012-09-04 09:15:20 +02002127 /* destroy rwlock */
2128 pthread_rwlock_destroy(&session_lock);
2129 pthread_rwlockattr_destroy(&lock_attrs);
2130
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002131 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002132
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002133 free(ptids);
2134 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002135 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002136 return;
2137error_exit:
2138 close(lsock);
2139 free(ptids);
2140 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002141}
2142
Radek Krejcif23850c2012-07-23 16:14:17 +02002143static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002144{
Radek Krejcif23850c2012-07-23 16:14:17 +02002145 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2146 apr_pool_create(&config->pool, pool);
2147 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002148 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002149
2150 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002151}
2152
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002153#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002154static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2155 apr_pool_t * plog, server_rec * s)
2156{
Radek Krejcif23850c2012-07-23 16:14:17 +02002157 mod_netconf_cfg *config;
2158 apr_status_t res;
2159
Radek Krejci469aab82012-07-22 18:42:20 +02002160 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002161 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002162 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002163
2164 /*
2165 * The following checks if this routine has been called before.
2166 * This is necessary because the parent process gets initialized
2167 * a couple of times as the server starts up.
2168 */
2169 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2170 if (!data) {
2171 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2172 return (OK);
2173 }
2174
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002175 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002176 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002177
Radek Krejcif23850c2012-07-23 16:14:17 +02002178 if (config && config->forkproc == NULL) {
2179 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2180 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002181 switch (res) {
2182 case APR_INCHILD:
2183 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002184 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002185 apr_signal(SIGTERM, signal_handler);
2186
2187 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002188 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002189
2190 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002191 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002192
Radek Krejcif23850c2012-07-23 16:14:17 +02002193 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002194 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002195 exit(APR_EGENERAL);
2196 break;
2197 case APR_INPARENT:
2198 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002199 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002200 break;
2201 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002202 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002203 break;
2204 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002205 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002206 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002207 }
2208
2209 return OK;
2210}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002211#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002212
Radek Krejci469aab82012-07-22 18:42:20 +02002213/**
2214 * Register module hooks
2215 */
2216static void mod_netconf_register_hooks(apr_pool_t * p)
2217{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002218#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002219 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002220#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002221}
2222
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002223static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2224{
2225 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2226 return NULL;
2227}
2228
2229static const command_rec netconf_cmds[] = {
2230 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2231 {NULL}
2232};
2233
Radek Krejci469aab82012-07-22 18:42:20 +02002234/* Dispatch list for API hooks */
2235module AP_MODULE_DECLARE_DATA netconf_module = {
2236 STANDARD20_MODULE_STUFF,
2237 NULL, /* create per-dir config structures */
2238 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002239 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002240 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002241 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002242 mod_netconf_register_hooks /* register hooks */
2243};
Radek Krejcia332b692012-11-12 16:15:54 +01002244
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002245int main(int argc, char **argv)
2246{
2247 apr_pool_t *pool;
2248 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2249 apr_signal(SIGTERM, signal_handler);
2250 apr_signal(SIGINT, signal_handler);
2251 apr_pool_create(&pool, NULL);
2252 forked_proc(pool, NULL);
2253 apr_pool_destroy(pool);
2254 apr_terminate();
2255 DEBUG("Terminated");
2256 return 0;
2257}