blob: 9a18ed12d6b2592c2097c1ac1515fd7db46523a0 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file io.c
3 * \author Radek Krejci <rkrejci@cesnet.cz>
4 * \brief libnetconf2 - input/output functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Miroslav Mareš9563b812017-08-19 17:45:36 +020015#define _GNU_SOURCE /* asprintf, signals */
Radek Krejci206fcd62015-10-07 15:42:48 +020016#include <assert.h>
17#include <errno.h>
18#include <poll.h>
Michal Vasko3512e402016-01-28 16:22:34 +010019#include <inttypes.h>
Radek Krejcife0b3472015-10-12 13:43:42 +020020#include <stdarg.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010024#include <signal.h>
Michal Vasko36c7be82017-02-22 13:37:59 +010025#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020026
Michal Vasko964e1732016-09-23 13:39:33 +020027#ifdef NC_ENABLED_TLS
28# include <openssl/err.h>
29#endif
30
Radek Krejci206fcd62015-10-07 15:42:48 +020031#include <libyang/libyang.h>
32
Michal Vasko9e8ac262020-04-07 13:06:45 +020033#include "compat.h"
Radek Krejci206fcd62015-10-07 15:42:48 +020034#include "libnetconf.h"
Radek Krejci206fcd62015-10-07 15:42:48 +020035
Michal Vasko8fe604c2020-02-10 15:25:04 +010036const char *nc_msgtype2str[] = {
37 "error",
38 "would block",
39 "no message",
40 "hello message",
41 "bad hello message",
42 "RPC message",
43 "rpc-reply message",
44 "rpc-reply message with wrong ID",
45 "notification message",
46};
47
Radek Krejcife0b3472015-10-12 13:43:42 +020048#define BUFFERSIZE 512
Radek Krejci206fcd62015-10-07 15:42:48 +020049
Michal Vasko90a87d92018-12-10 15:53:44 +010050#ifdef NC_ENABLED_TLS
51
52static char *
53nc_ssl_error_get_reasons(void)
54{
55 unsigned int e;
56 int reason_size, reason_len;
57 char *reasons = NULL;
58
59 reason_size = 1;
60 reason_len = 0;
61 while ((e = ERR_get_error())) {
62 if (reason_len) {
63 /* add "; " */
64 reason_size += 2;
65 reasons = nc_realloc(reasons, reason_size);
66 if (!reasons) {
67 ERRMEM;
68 return NULL;
69 }
70 reason_len += sprintf(reasons + reason_len, "; ");
71 }
72 reason_size += strlen(ERR_reason_error_string(e));
73 reasons = nc_realloc(reasons, reason_size);
74 if (!reasons) {
75 ERRMEM;
76 return NULL;
77 }
Rosen Peneve38d7ef2019-07-15 18:18:03 -070078 reason_len += sprintf(reasons + reason_len, "%s", ERR_reason_error_string(e));
Michal Vasko90a87d92018-12-10 15:53:44 +010079 }
80
81 return reasons;
82}
83
84#endif
85
Radek Krejci206fcd62015-10-07 15:42:48 +020086static ssize_t
Michal Vasko36c7be82017-02-22 13:37:59 +010087nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_timeout, struct timespec *ts_act_timeout)
Radek Krejci206fcd62015-10-07 15:42:48 +020088{
Michal Vasko81b33fb2016-09-26 14:57:36 +020089 size_t readd = 0;
Michal Vasko9d8bee62016-03-03 10:58:24 +010090 ssize_t r = -1;
Robin Jarry7de4b8e2019-10-14 21:46:00 +020091 int fd, interrupted;
Michal Vasko36c7be82017-02-22 13:37:59 +010092 struct timespec ts_cur, ts_inact_timeout;
Radek Krejci206fcd62015-10-07 15:42:48 +020093
94 assert(session);
95 assert(buf);
96
Michal Vasko428087d2016-01-14 16:04:28 +010097 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
98 return -1;
99 }
100
Radek Krejci206fcd62015-10-07 15:42:48 +0200101 if (!count) {
102 return 0;
103 }
104
Michal Vasko77a6abe2017-10-05 10:02:20 +0200105 nc_gettimespec_mono(&ts_inact_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100106 nc_addtimespec(&ts_inact_timeout, inact_timeout);
Michal Vasko81b33fb2016-09-26 14:57:36 +0200107 do {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200108 interrupted = 0;
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100109 switch (session->ti_type) {
110 case NC_TI_NONE:
111 return 0;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100112
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100113 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200114 case NC_TI_UNIX:
115 fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock;
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100116 /* read via standard file descriptor */
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200117 r = read(fd, buf + readd, count - readd);
Radek Krejci206fcd62015-10-07 15:42:48 +0200118 if (r < 0) {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200119 if (errno == EAGAIN) {
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100120 r = 0;
121 break;
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200122 } else if (errno == EINTR) {
123 r = 0;
124 interrupted = 1;
125 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200126 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100127 ERR("Session %u: reading from file descriptor (%d) failed (%s).",
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200128 session->id, fd, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100129 session->status = NC_STATUS_INVALID;
130 session->term_reason = NC_SESSION_TERM_OTHER;
Radek Krejci206fcd62015-10-07 15:42:48 +0200131 return -1;
132 }
133 } else if (r == 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100134 ERR("Session %u: communication file descriptor (%d) unexpectedly closed.",
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200135 session->id, fd);
Michal Vasko428087d2016-01-14 16:04:28 +0100136 session->status = NC_STATUS_INVALID;
137 session->term_reason = NC_SESSION_TERM_DROPPED;
Radek Krejci206fcd62015-10-07 15:42:48 +0200138 return -1;
139 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100140 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200141
Radek Krejci53691be2016-02-22 13:58:37 +0100142#ifdef NC_ENABLED_SSH
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100143 case NC_TI_LIBSSH:
144 /* read via libssh */
Michal Vasko81b33fb2016-09-26 14:57:36 +0200145 r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0);
Radek Krejci206fcd62015-10-07 15:42:48 +0200146 if (r == SSH_AGAIN) {
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100147 r = 0;
148 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200149 } else if (r == SSH_ERROR) {
Michal Vasko051d35b2016-02-03 15:28:37 +0100150 ERR("Session %u: reading from the SSH channel failed (%s).", session->id,
151 ssh_get_error(session->ti.libssh.session));
Michal Vasko428087d2016-01-14 16:04:28 +0100152 session->status = NC_STATUS_INVALID;
153 session->term_reason = NC_SESSION_TERM_OTHER;
Radek Krejci206fcd62015-10-07 15:42:48 +0200154 return -1;
155 } else if (r == 0) {
156 if (ssh_channel_is_eof(session->ti.libssh.channel)) {
Michal Vasko454e22b2016-01-21 15:34:08 +0100157 ERR("Session %u: SSH channel unexpected EOF.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100158 session->status = NC_STATUS_INVALID;
159 session->term_reason = NC_SESSION_TERM_DROPPED;
Radek Krejci206fcd62015-10-07 15:42:48 +0200160 return -1;
161 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100162 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200163 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100164 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200165#endif
166
Radek Krejci53691be2016-02-22 13:58:37 +0100167#ifdef NC_ENABLED_TLS
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100168 case NC_TI_OPENSSL:
169 /* read via OpenSSL */
Michal Vasko90a87d92018-12-10 15:53:44 +0100170 ERR_clear_error();
Michal Vasko81b33fb2016-09-26 14:57:36 +0200171 r = SSL_read(session->ti.tls, buf + readd, count - readd);
Radek Krejcid0046592015-10-08 12:52:02 +0200172 if (r <= 0) {
Michal Vasko0abba6d2018-12-10 14:09:39 +0100173 int e;
Michal Vasko90a87d92018-12-10 15:53:44 +0100174 char *reasons;
175
Michal Vasko0abba6d2018-12-10 14:09:39 +0100176 switch (e = SSL_get_error(session->ti.tls, r)) {
Radek Krejcid0046592015-10-08 12:52:02 +0200177 case SSL_ERROR_WANT_READ:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100178 case SSL_ERROR_WANT_WRITE:
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100179 r = 0;
180 break;
Radek Krejcid0046592015-10-08 12:52:02 +0200181 case SSL_ERROR_ZERO_RETURN:
Michal Vaskod083db62016-01-19 10:31:29 +0100182 ERR("Session %u: communication socket unexpectedly closed (OpenSSL).", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100183 session->status = NC_STATUS_INVALID;
184 session->term_reason = NC_SESSION_TERM_DROPPED;
Radek Krejcid0046592015-10-08 12:52:02 +0200185 return -1;
Michal Vasko0abba6d2018-12-10 14:09:39 +0100186 case SSL_ERROR_SYSCALL:
187 ERR("Session %u: SSL socket error (%s).", session->id, strerror(errno));
188 session->status = NC_STATUS_INVALID;
189 session->term_reason = NC_SESSION_TERM_OTHER;
190 return -1;
191 case SSL_ERROR_SSL:
Michal Vasko90a87d92018-12-10 15:53:44 +0100192 reasons = nc_ssl_error_get_reasons();
193 ERR("Session %u: SSL error (%s).", session->id, reasons);
194 free(reasons);
Michal Vasko0abba6d2018-12-10 14:09:39 +0100195 session->status = NC_STATUS_INVALID;
196 session->term_reason = NC_SESSION_TERM_OTHER;
197 return -1;
Radek Krejcid0046592015-10-08 12:52:02 +0200198 default:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100199 ERR("Session %u: unknown SSL error occured (err code %d).", session->id, e);
Michal Vasko428087d2016-01-14 16:04:28 +0100200 session->status = NC_STATUS_INVALID;
201 session->term_reason = NC_SESSION_TERM_OTHER;
Radek Krejcid0046592015-10-08 12:52:02 +0200202 return -1;
203 }
Radek Krejci206fcd62015-10-07 15:42:48 +0200204 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100205 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200206#endif
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100207 }
208
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100209 if (r == 0) {
Michal Vaskof471fa02017-02-15 10:48:12 +0100210 /* nothing read */
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200211 if (!interrupted) {
212 usleep(NC_TIMEOUT_STEP);
213 }
Michal Vasko77a6abe2017-10-05 10:02:20 +0200214 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +0100215 if ((nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) || (nc_difftimespec(&ts_cur, ts_act_timeout) < 1)) {
216 if (nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) {
Michal Vaskof471fa02017-02-15 10:48:12 +0100217 ERR("Session %u: inactive read timeout elapsed.", session->id);
218 } else {
219 ERR("Session %u: active read timeout elapsed.", session->id);
220 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100221 session->status = NC_STATUS_INVALID;
222 session->term_reason = NC_SESSION_TERM_OTHER;
223 return -1;
224 }
Michal Vaskof471fa02017-02-15 10:48:12 +0100225 } else {
226 /* something read */
227 readd += r;
Michal Vasko36c7be82017-02-22 13:37:59 +0100228
229 /* reset inactive timeout */
Michal Vasko77a6abe2017-10-05 10:02:20 +0200230 nc_gettimespec_mono(&ts_inact_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100231 nc_addtimespec(&ts_inact_timeout, inact_timeout);
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100232 }
233
Michal Vasko81b33fb2016-09-26 14:57:36 +0200234 } while (readd < count);
235 buf[count] = '\0';
Radek Krejci206fcd62015-10-07 15:42:48 +0200236
Michal Vasko81b33fb2016-09-26 14:57:36 +0200237 return (ssize_t)readd;
Radek Krejci206fcd62015-10-07 15:42:48 +0200238}
239
240static ssize_t
Michal Vasko36c7be82017-02-22 13:37:59 +0100241nc_read_chunk(struct nc_session *session, size_t len, uint32_t inact_timeout, struct timespec *ts_act_timeout, char **chunk)
Radek Krejci206fcd62015-10-07 15:42:48 +0200242{
243 ssize_t r;
244
245 assert(session);
246 assert(chunk);
247
248 if (!len) {
249 return 0;
250 }
251
Michal Vasko4eb3c312016-03-01 14:09:37 +0100252 *chunk = malloc((len + 1) * sizeof **chunk);
Radek Krejci206fcd62015-10-07 15:42:48 +0200253 if (!*chunk) {
254 ERRMEM;
255 return -1;
256 }
257
Michal Vasko36c7be82017-02-22 13:37:59 +0100258 r = nc_read(session, *chunk, len, inact_timeout, ts_act_timeout);
Radek Krejci206fcd62015-10-07 15:42:48 +0200259 if (r <= 0) {
260 free(*chunk);
261 return -1;
262 }
263
264 /* terminating null byte */
Radek Krejcife0b3472015-10-12 13:43:42 +0200265 (*chunk)[r] = 0;
Radek Krejci206fcd62015-10-07 15:42:48 +0200266
267 return r;
268}
269
270static ssize_t
Michal Vasko36c7be82017-02-22 13:37:59 +0100271nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint32_t inact_timeout,
272 struct timespec *ts_act_timeout, char **result)
Radek Krejci206fcd62015-10-07 15:42:48 +0200273{
274 char *chunk = NULL;
David Sedlákfedbc792018-07-04 11:07:07 +0200275 size_t size, count = 0, r, len, i, matched = 0;
Radek Krejci206fcd62015-10-07 15:42:48 +0200276
277 assert(session);
278 assert(endtag);
279
280 if (limit && limit < BUFFERSIZE) {
281 size = limit;
282 } else {
283 size = BUFFERSIZE;
284 }
Michal Vasko4eb3c312016-03-01 14:09:37 +0100285 chunk = malloc((size + 1) * sizeof *chunk);
Radek Krejcib791b532015-10-08 15:29:34 +0200286 if (!chunk) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200287 ERRMEM;
288 return -1;
289 }
290
291 len = strlen(endtag);
Michal Vasko428087d2016-01-14 16:04:28 +0100292 while (1) {
David Sedlák15dad862018-07-04 11:25:55 +0200293 if (limit && count == limit) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200294 free(chunk);
Michal Vaskod083db62016-01-19 10:31:29 +0100295 WRN("Session %u: reading limit (%d) reached.", session->id, limit);
296 ERR("Session %u: invalid input data (missing \"%s\" sequence).", session->id, endtag);
Radek Krejci206fcd62015-10-07 15:42:48 +0200297 return -1;
298 }
299
300 /* resize buffer if needed */
David Sedlákfedbc792018-07-04 11:07:07 +0200301 if ((count + (len - matched)) >= size) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200302 /* get more memory */
303 size = size + BUFFERSIZE;
Radek Krejcif6d9aef2018-08-17 11:50:53 +0200304 chunk = nc_realloc(chunk, (size + 1) * sizeof *chunk);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100305 if (!chunk) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200306 ERRMEM;
Radek Krejci206fcd62015-10-07 15:42:48 +0200307 return -1;
308 }
Radek Krejci206fcd62015-10-07 15:42:48 +0200309 }
310
311 /* get another character */
David Sedlákfedbc792018-07-04 11:07:07 +0200312 r = nc_read(session, &(chunk[count]), len - matched, inact_timeout, ts_act_timeout);
313 if (r != len - matched) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200314 free(chunk);
315 return -1;
316 }
317
David Sedlákfedbc792018-07-04 11:07:07 +0200318 count += len - matched;
Radek Krejci206fcd62015-10-07 15:42:48 +0200319
David Sedlákfedbc792018-07-04 11:07:07 +0200320 for (i = len - matched; i > 0; i--) {
321 if (!strncmp(&endtag[matched], &(chunk[count - i]), i)) {
322 /*part of endtag found */
323 matched += i;
Radek Krejci206fcd62015-10-07 15:42:48 +0200324 break;
David Sedlákfedbc792018-07-04 11:07:07 +0200325 } else {
326 matched = 0;
Radek Krejci206fcd62015-10-07 15:42:48 +0200327 }
328 }
David Sedlákfedbc792018-07-04 11:07:07 +0200329
330 /* whole endtag found */
331 if (matched == len) {
332 break;
333 }
Radek Krejci206fcd62015-10-07 15:42:48 +0200334 }
335
336 /* terminating null byte */
337 chunk[count] = 0;
338
339 if (result) {
340 *result = chunk;
Radek Krejcife0b3472015-10-12 13:43:42 +0200341 } else {
342 free(chunk);
Radek Krejci206fcd62015-10-07 15:42:48 +0200343 }
344 return count;
345}
346
Michal Vasko77367452021-02-16 16:32:18 +0100347int
348nc_read_msg_io(struct nc_session *session, int io_timeout, struct ly_in **msg, int passing_io_lock)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100349{
Michal Vasko77367452021-02-16 16:32:18 +0100350 int ret = 1, r, io_locked = passing_io_lock;
351 char *data = NULL, *chunk;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100352 uint64_t chunk_len, len = 0;
Michal Vasko36c7be82017-02-22 13:37:59 +0100353 /* use timeout in milliseconds instead seconds */
354 uint32_t inact_timeout = NC_READ_INACT_TIMEOUT * 1000;
355 struct timespec ts_act_timeout;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100356
Michal Vasko77367452021-02-16 16:32:18 +0100357 assert(session && msg);
358 *msg = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +0100359
360 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100361 ERR("Session %u: invalid session to read from.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100362 ret = -1;
Michal Vasko131120a2018-05-29 15:44:02 +0200363 goto cleanup;
Michal Vasko428087d2016-01-14 16:04:28 +0100364 }
365
Michal Vasko77a6abe2017-10-05 10:02:20 +0200366 nc_gettimespec_mono(&ts_act_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100367 nc_addtimespec(&ts_act_timeout, NC_READ_ACT_TIMEOUT * 1000);
368
Michal Vasko131120a2018-05-29 15:44:02 +0200369 if (!io_locked) {
370 /* SESSION IO LOCK */
371 ret = nc_session_io_lock(session, io_timeout, __func__);
Michal Vasko77367452021-02-16 16:32:18 +0100372 if (ret < 1) {
Michal Vasko131120a2018-05-29 15:44:02 +0200373 goto cleanup;
374 }
375 io_locked = 1;
376 }
377
Michal Vasko05ba9df2016-01-13 14:40:27 +0100378 /* read the message */
379 switch (session->version) {
380 case NC_VERSION_10:
Michal Vasko77367452021-02-16 16:32:18 +0100381 r = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, inact_timeout, &ts_act_timeout, &data);
382 if (r == -1) {
383 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200384 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100385 }
386
387 /* cut off the end tag */
Michal Vasko77367452021-02-16 16:32:18 +0100388 data[r - NC_VERSION_10_ENDTAG_LEN] = '\0';
Michal Vasko05ba9df2016-01-13 14:40:27 +0100389 break;
390 case NC_VERSION_11:
391 while (1) {
Michal Vasko77367452021-02-16 16:32:18 +0100392 r = nc_read_until(session, "\n#", 0, inact_timeout, &ts_act_timeout, NULL);
393 if (r == -1) {
394 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200395 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100396 }
Michal Vasko77367452021-02-16 16:32:18 +0100397 r = nc_read_until(session, "\n", 0, inact_timeout, &ts_act_timeout, &chunk);
398 if (r == -1) {
399 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200400 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100401 }
402
403 if (!strcmp(chunk, "#\n")) {
404 /* end of chunked framing message */
405 free(chunk);
Michal Vasko77367452021-02-16 16:32:18 +0100406 if (!data) {
Michal Vasko79df3262016-07-13 13:42:17 +0200407 ERR("Session %u: invalid frame chunk delimiters.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100408 ret = -2;
409 goto cleanup;
Michal Vasko79df3262016-07-13 13:42:17 +0200410 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100411 break;
412 }
413
414 /* convert string to the size of the following chunk */
415 chunk_len = strtoul(chunk, (char **)NULL, 10);
416 free(chunk);
417 if (!chunk_len) {
Michal Vaskod083db62016-01-19 10:31:29 +0100418 ERR("Session %u: invalid frame chunk size detected, fatal error.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100419 ret = -2;
420 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100421 }
422
423 /* now we have size of next chunk, so read the chunk */
Michal Vasko77367452021-02-16 16:32:18 +0100424 r = nc_read_chunk(session, chunk_len, inact_timeout, &ts_act_timeout, &chunk);
425 if (r == -1) {
426 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200427 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100428 }
429
430 /* realloc message buffer, remember to count terminating null byte */
Michal Vasko77367452021-02-16 16:32:18 +0100431 data = nc_realloc(data, len + chunk_len + 1);
432 if (!data) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100433 ERRMEM;
Michal Vasko77367452021-02-16 16:32:18 +0100434 ret = -1;
Michal Vasko131120a2018-05-29 15:44:02 +0200435 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100436 }
Michal Vasko77367452021-02-16 16:32:18 +0100437 memcpy(data + len, chunk, chunk_len);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100438 len += chunk_len;
Michal Vasko77367452021-02-16 16:32:18 +0100439 data[len] = '\0';
Michal Vasko05ba9df2016-01-13 14:40:27 +0100440 free(chunk);
441 }
442
443 break;
444 }
Michal Vasko131120a2018-05-29 15:44:02 +0200445
446 /* SESSION IO UNLOCK */
447 assert(io_locked);
448 nc_session_io_unlock(session, __func__);
449 io_locked = 0;
450
Michal Vasko77367452021-02-16 16:32:18 +0100451 DBG("Session %u: received message:\n%s\n", session->id, data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100452
Michal Vasko77367452021-02-16 16:32:18 +0100453 /* build an input structure, eats data */
454 if (ly_in_new_memory(data, msg)) {
455 ret = -1;
456 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100457 }
Michal Vasko77367452021-02-16 16:32:18 +0100458 data = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100459
Michal Vasko131120a2018-05-29 15:44:02 +0200460cleanup:
461 if (io_locked) {
Michal Vasko77367452021-02-16 16:32:18 +0100462 /* SESSION IO UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200463 nc_session_io_unlock(session, __func__);
464 }
Michal Vasko77367452021-02-16 16:32:18 +0100465 free(data);
Michal Vasko131120a2018-05-29 15:44:02 +0200466 return ret;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100467}
468
Michal Vasko428087d2016-01-14 16:04:28 +0100469/* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */
470static int
Michal Vasko131120a2018-05-29 15:44:02 +0200471nc_read_poll(struct nc_session *session, int io_timeout)
Michal Vasko428087d2016-01-14 16:04:28 +0100472{
Radek Krejci5961c702016-07-15 09:15:18 +0200473 sigset_t sigmask, origmask;
Michal Vasko428087d2016-01-14 16:04:28 +0100474 int ret = -2;
475 struct pollfd fds;
Michal Vasko428087d2016-01-14 16:04:28 +0100476
477 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100478 ERR("Session %u: invalid session to poll.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100479 return -1;
480 }
481
482 switch (session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +0100483#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100484 case NC_TI_LIBSSH:
485 /* EINTR is handled, it resumes waiting */
Michal Vasko131120a2018-05-29 15:44:02 +0200486 ret = ssh_channel_poll_timeout(session->ti.libssh.channel, io_timeout, 0);
Michal Vasko428087d2016-01-14 16:04:28 +0100487 if (ret == SSH_ERROR) {
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100488 ERR("Session %u: SSH channel poll error (%s).", session->id,
Michal Vasko051d35b2016-02-03 15:28:37 +0100489 ssh_get_error(session->ti.libssh.session));
Michal Vasko428087d2016-01-14 16:04:28 +0100490 session->status = NC_STATUS_INVALID;
491 session->term_reason = NC_SESSION_TERM_OTHER;
492 return -1;
493 } else if (ret == SSH_EOF) {
Michal Vasko051d35b2016-02-03 15:28:37 +0100494 ERR("Session %u: SSH channel unexpected EOF.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100495 session->status = NC_STATUS_INVALID;
496 session->term_reason = NC_SESSION_TERM_DROPPED;
497 return -1;
498 } else if (ret > 0) {
499 /* fake it */
500 ret = 1;
501 fds.revents = POLLIN;
Michal Vasko5550cda2016-02-03 15:28:57 +0100502 } else { /* ret == 0 */
503 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100504 }
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100505 break;
Michal Vasko428087d2016-01-14 16:04:28 +0100506#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100507#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100508 case NC_TI_OPENSSL:
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100509 ret = SSL_pending(session->ti.tls);
510 if (ret) {
511 /* some buffered TLS data available */
512 ret = 1;
513 fds.revents = POLLIN;
514 break;
Michal Vasko428087d2016-01-14 16:04:28 +0100515 }
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100516
517 fds.fd = SSL_get_fd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100518#endif
Michal Vaskob983c002017-11-02 13:10:57 +0100519 /* fallthrough */
Michal Vasko428087d2016-01-14 16:04:28 +0100520 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200521 case NC_TI_UNIX:
522 if (session->ti_type == NC_TI_FD)
Michal Vasko428087d2016-01-14 16:04:28 +0100523 fds.fd = session->ti.fd.in;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200524 else if (session->ti_type == NC_TI_UNIX)
525 fds.fd = session->ti.unixsock.sock;
Michal Vasko428087d2016-01-14 16:04:28 +0100526
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100527 fds.events = POLLIN;
528 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100529
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100530 sigfillset(&sigmask);
531 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vasko131120a2018-05-29 15:44:02 +0200532 ret = poll(&fds, 1, io_timeout);
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100533 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
Michal Vasko428087d2016-01-14 16:04:28 +0100534
535 break;
536
537 default:
538 ERRINT;
539 return -1;
540 }
541
542 /* process the poll result, unified ret meaning for poll and ssh_channel poll */
543 if (ret < 0) {
544 /* poll failed - something really bad happened, close the session */
Radek Krejci5961c702016-07-15 09:15:18 +0200545 ERR("Session %u: poll error (%s).", session->id, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100546 session->status = NC_STATUS_INVALID;
547 session->term_reason = NC_SESSION_TERM_OTHER;
548 return -1;
549 } else { /* status > 0 */
550 /* in case of standard (non-libssh) poll, there still can be an error */
Michal Vasko428087d2016-01-14 16:04:28 +0100551 if (fds.revents & POLLERR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100552 ERR("Session %u: communication channel error.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100553 session->status = NC_STATUS_INVALID;
554 session->term_reason = NC_SESSION_TERM_OTHER;
555 return -1;
556 }
Robin Jarryf732adc2020-05-15 11:18:38 +0200557 /* Some poll() implementations may return POLLHUP|POLLIN when the other
558 * side has closed but there is data left to read in the buffer. */
559 if ((fds.revents & POLLHUP) && !(fds.revents & POLLIN)) {
560 ERR("Session %u: communication channel unexpectedly closed.", session->id);
561 session->status = NC_STATUS_INVALID;
562 session->term_reason = NC_SESSION_TERM_DROPPED;
563 return -1;
564 }
Michal Vasko428087d2016-01-14 16:04:28 +0100565 }
566
567 return ret;
568}
569
Michal Vasko77367452021-02-16 16:32:18 +0100570int
571nc_read_msg_poll_io(struct nc_session *session, int io_timeout, struct ly_in **msg)
Radek Krejci206fcd62015-10-07 15:42:48 +0200572{
Michal Vasko428087d2016-01-14 16:04:28 +0100573 int ret;
Radek Krejci206fcd62015-10-07 15:42:48 +0200574
Michal Vasko77367452021-02-16 16:32:18 +0100575 assert(msg);
576 *msg = NULL;
Radek Krejci206fcd62015-10-07 15:42:48 +0200577
Michal Vasko428087d2016-01-14 16:04:28 +0100578 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100579 ERR("Session %u: invalid session to read from.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100580 return -1;
Radek Krejci206fcd62015-10-07 15:42:48 +0200581 }
582
Michal Vasko131120a2018-05-29 15:44:02 +0200583 /* SESSION IO LOCK */
584 ret = nc_session_io_lock(session, io_timeout, __func__);
Michal Vasko77367452021-02-16 16:32:18 +0100585 if (ret < 1) {
586 return ret;
Michal Vasko131120a2018-05-29 15:44:02 +0200587 }
588
589 ret = nc_read_poll(session, io_timeout);
Michal Vasko77367452021-02-16 16:32:18 +0100590 if (ret < 1) {
591 /* timed out or error */
Michal Vasko131120a2018-05-29 15:44:02 +0200592
593 /* SESSION IO UNLOCK */
594 nc_session_io_unlock(session, __func__);
Michal Vasko77367452021-02-16 16:32:18 +0100595 return ret;
Radek Krejci206fcd62015-10-07 15:42:48 +0200596 }
597
Michal Vasko131120a2018-05-29 15:44:02 +0200598 /* SESSION IO LOCK passed down */
Michal Vasko77367452021-02-16 16:32:18 +0100599 return nc_read_msg_io(session, io_timeout, msg, 1);
Radek Krejci206fcd62015-10-07 15:42:48 +0200600}
Radek Krejcife0b3472015-10-12 13:43:42 +0200601
Michal Vasko428087d2016-01-14 16:04:28 +0100602/* does not really log, only fatal errors */
603int
604nc_session_is_connected(struct nc_session *session)
605{
606 int ret;
607 struct pollfd fds;
608
609 switch (session->ti_type) {
610 case NC_TI_FD:
611 fds.fd = session->ti.fd.in;
612 break;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200613 case NC_TI_UNIX:
614 fds.fd = session->ti.unixsock.sock;
615 break;
Radek Krejci53691be2016-02-22 13:58:37 +0100616#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100617 case NC_TI_LIBSSH:
Michal Vasko840a8a62017-02-07 10:56:34 +0100618 return ssh_is_connected(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100619#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100620#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100621 case NC_TI_OPENSSL:
622 fds.fd = SSL_get_fd(session->ti.tls);
623 break;
624#endif
Michal Vaskof945da52018-02-15 08:45:13 +0100625 default:
Michal Vasko428087d2016-01-14 16:04:28 +0100626 return 0;
627 }
628
Michal Vasko840a8a62017-02-07 10:56:34 +0100629 if (fds.fd == -1) {
630 return 0;
631 }
632
Michal Vasko428087d2016-01-14 16:04:28 +0100633 fds.events = POLLIN;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100634 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100635
636 errno = 0;
637 while (((ret = poll(&fds, 1, 0)) == -1) && (errno == EINTR));
638
639 if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100640 ERR("Session %u: poll failed (%s).", session->id, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100641 return 0;
642 } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) {
643 return 0;
644 }
645
646 return 1;
647}
648
Radek Krejcife0b3472015-10-12 13:43:42 +0200649#define WRITE_BUFSIZE (2 * BUFFERSIZE)
650struct wclb_arg {
651 struct nc_session *session;
652 char buf[WRITE_BUFSIZE];
653 size_t len;
654};
655
Michal Vasko964e1732016-09-23 13:39:33 +0200656static int
Michal Vasko428087d2016-01-14 16:04:28 +0100657nc_write(struct nc_session *session, const void *buf, size_t count)
Radek Krejcife0b3472015-10-12 13:43:42 +0200658{
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200659 int c, fd, interrupted;
Michal Vasko964e1732016-09-23 13:39:33 +0200660 size_t written = 0;
Michal Vaskoe2357e92016-10-05 14:20:47 +0200661#ifdef NC_ENABLED_TLS
662 unsigned long e;
663#endif
Michal Vasko964e1732016-09-23 13:39:33 +0200664
Michal Vasko428087d2016-01-14 16:04:28 +0100665 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
666 return -1;
667 }
668
669 /* prevent SIGPIPE this way */
670 if (!nc_session_is_connected(session)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100671 ERR("Session %u: communication socket unexpectedly closed.", session->id);
Michal Vasko2a7d4732016-01-15 09:24:46 +0100672 session->status = NC_STATUS_INVALID;
673 session->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko428087d2016-01-14 16:04:28 +0100674 return -1;
675 }
676
Michal Vasko81b33fb2016-09-26 14:57:36 +0200677 DBG("Session %u: sending message:\n%.*s\n", session->id, count, buf);
Michal Vasko160b7912016-06-20 10:00:53 +0200678
Michal Vasko81b33fb2016-09-26 14:57:36 +0200679 do {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200680 interrupted = 0;
Michal Vasko964e1732016-09-23 13:39:33 +0200681 switch (session->ti_type) {
Michal Vasko964e1732016-09-23 13:39:33 +0200682 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200683 case NC_TI_UNIX:
684 fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock;
685 c = write(fd, (char *)(buf + written), count - written);
686 if (c < 0 && errno == EAGAIN) {
687 c = 0;
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200688 } else if (c < 0 && errno == EINTR) {
689 c = 0;
690 interrupted = 1;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200691 } else if (c < 0) {
Michal Vaskoe2146a32016-09-23 14:20:36 +0200692 ERR("Session %u: socket error (%s).", session->id, strerror(errno));
Michal Vasko964e1732016-09-23 13:39:33 +0200693 return -1;
694 }
695 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200696
Radek Krejci53691be2016-02-22 13:58:37 +0100697#ifdef NC_ENABLED_SSH
Michal Vasko964e1732016-09-23 13:39:33 +0200698 case NC_TI_LIBSSH:
699 if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) {
700 if (ssh_channel_is_closed(session->ti.libssh.channel)) {
701 ERR("Session %u: SSH channel unexpectedly closed.", session->id);
702 } else {
703 ERR("Session %u: SSH channel unexpected EOF.", session->id);
704 }
705 session->status = NC_STATUS_INVALID;
706 session->term_reason = NC_SESSION_TERM_DROPPED;
707 return -1;
Michal Vasko454e22b2016-01-21 15:34:08 +0100708 }
Michal Vasko81b33fb2016-09-26 14:57:36 +0200709 c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written);
Michal Vasko964e1732016-09-23 13:39:33 +0200710 if ((c == SSH_ERROR) || (c == -1)) {
711 ERR("Session %u: SSH channel write failed.", session->id);
712 return -1;
713 }
714 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200715#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100716#ifdef NC_ENABLED_TLS
Michal Vasko964e1732016-09-23 13:39:33 +0200717 case NC_TI_OPENSSL:
Michal Vasko81b33fb2016-09-26 14:57:36 +0200718 c = SSL_write(session->ti.tls, (char *)(buf + written), count - written);
Michal Vasko964e1732016-09-23 13:39:33 +0200719 if (c < 1) {
Michal Vasko90a87d92018-12-10 15:53:44 +0100720 char *reasons;
721
Michal Vasko964e1732016-09-23 13:39:33 +0200722 switch ((e = SSL_get_error(session->ti.tls, c))) {
723 case SSL_ERROR_ZERO_RETURN:
724 ERR("Session %u: SSL connection was properly closed.", session->id);
725 return -1;
726 case SSL_ERROR_WANT_WRITE:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100727 case SSL_ERROR_WANT_READ:
Michal Vasko964e1732016-09-23 13:39:33 +0200728 c = 0;
729 break;
730 case SSL_ERROR_SYSCALL:
731 ERR("Session %u: SSL socket error (%s).", session->id, strerror(errno));
732 return -1;
733 case SSL_ERROR_SSL:
Michal Vasko90a87d92018-12-10 15:53:44 +0100734 reasons = nc_ssl_error_get_reasons();
735 ERR("Session %u: SSL error (%s).", session->id, reasons);
736 free(reasons);
Michal Vasko964e1732016-09-23 13:39:33 +0200737 return -1;
738 default:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100739 ERR("Session %u: unknown SSL error occured (err code %d).", session->id, e);
Michal Vasko964e1732016-09-23 13:39:33 +0200740 return -1;
741 }
742 }
743 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200744#endif
Michal Vasko339eea82016-09-29 11:42:36 +0200745 default:
746 ERRINT;
747 return -1;
Michal Vasko964e1732016-09-23 13:39:33 +0200748 }
749
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200750 if (c == 0 && !interrupted) {
Michal Vasko964e1732016-09-23 13:39:33 +0200751 /* we must wait */
752 usleep(NC_TIMEOUT_STEP);
753 }
754
755 written += c;
Michal Vasko81b33fb2016-09-26 14:57:36 +0200756 } while (written < count);
Radek Krejcife0b3472015-10-12 13:43:42 +0200757
Michal Vasko964e1732016-09-23 13:39:33 +0200758 return written;
Radek Krejcife0b3472015-10-12 13:43:42 +0200759}
760
Michal Vasko428087d2016-01-14 16:04:28 +0100761static int
762nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count)
Michal Vasko086311b2016-01-08 09:53:11 +0100763{
Michal Vaskofd2db9b2016-01-14 16:15:16 +0100764 int ret = 0, c;
Claus Klein22091912020-01-20 13:45:47 +0100765 char chunksize[24];
Michal Vasko086311b2016-01-08 09:53:11 +0100766
Claus Klein22091912020-01-20 13:45:47 +0100767 // warning: ‘%zu’ directive writing between 4 and 20 bytes into a region of size 18 [-Wformat-overflow=]
Michal Vasko086311b2016-01-08 09:53:11 +0100768 if (session->version == NC_VERSION_11) {
769 sprintf(chunksize, "\n#%zu\n", count);
Michal Vasko428087d2016-01-14 16:04:28 +0100770 ret = nc_write(session, chunksize, strlen(chunksize));
771 if (ret == -1) {
772 return -1;
773 }
Michal Vasko086311b2016-01-08 09:53:11 +0100774 }
Michal Vasko428087d2016-01-14 16:04:28 +0100775
776 c = nc_write(session, buf, count);
777 if (c == -1) {
778 return -1;
779 }
780 ret += c;
781
782 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100783}
784
Radek Krejcife0b3472015-10-12 13:43:42 +0200785static int
Michal Vasko428087d2016-01-14 16:04:28 +0100786nc_write_endtag(struct nc_session *session)
Radek Krejcife0b3472015-10-12 13:43:42 +0200787{
Michal Vasko428087d2016-01-14 16:04:28 +0100788 int ret;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100789
Michal Vasko428087d2016-01-14 16:04:28 +0100790 if (session->version == NC_VERSION_11) {
791 ret = nc_write(session, "\n##\n", 4);
792 } else {
793 ret = nc_write(session, "]]>]]>", 6);
Radek Krejcife0b3472015-10-12 13:43:42 +0200794 }
795
Michal Vasko428087d2016-01-14 16:04:28 +0100796 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200797}
798
Michal Vasko428087d2016-01-14 16:04:28 +0100799static int
800nc_write_clb_flush(struct wclb_arg *warg)
Radek Krejcife0b3472015-10-12 13:43:42 +0200801{
Michal Vasko428087d2016-01-14 16:04:28 +0100802 int ret = 0;
803
Radek Krejcife0b3472015-10-12 13:43:42 +0200804 /* flush current buffer */
805 if (warg->len) {
Michal Vasko428087d2016-01-14 16:04:28 +0100806 ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len);
Radek Krejcife0b3472015-10-12 13:43:42 +0200807 warg->len = 0;
808 }
Michal Vasko428087d2016-01-14 16:04:28 +0100809
810 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200811}
812
813static ssize_t
Radek Krejci047300e2016-03-08 16:46:58 +0100814nc_write_clb(void *arg, const void *buf, size_t count, int xmlcontent)
Radek Krejcife0b3472015-10-12 13:43:42 +0200815{
Michal Vasko428087d2016-01-14 16:04:28 +0100816 int ret = 0, c;
Radek Krejci047300e2016-03-08 16:46:58 +0100817 size_t l;
Radek Krejcife0b3472015-10-12 13:43:42 +0200818 struct wclb_arg *warg = (struct wclb_arg *)arg;
819
820 if (!buf) {
Michal Vasko428087d2016-01-14 16:04:28 +0100821 c = nc_write_clb_flush(warg);
822 if (c == -1) {
823 return -1;
824 }
825 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200826
827 /* endtag */
Michal Vasko428087d2016-01-14 16:04:28 +0100828 c = nc_write_endtag(warg->session);
829 if (c == -1) {
830 return -1;
831 }
832 ret += c;
833
834 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200835 }
836
837 if (warg->len && (warg->len + count > WRITE_BUFSIZE)) {
838 /* dump current buffer */
Michal Vasko428087d2016-01-14 16:04:28 +0100839 c = nc_write_clb_flush(warg);
840 if (c == -1) {
841 return -1;
842 }
843 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200844 }
Michal Vasko428087d2016-01-14 16:04:28 +0100845
Radek Krejci047300e2016-03-08 16:46:58 +0100846 if (!xmlcontent && count > WRITE_BUFSIZE) {
Radek Krejcife0b3472015-10-12 13:43:42 +0200847 /* write directly */
Michal Vasko428087d2016-01-14 16:04:28 +0100848 c = nc_write_starttag_and_msg(warg->session, buf, count);
849 if (c == -1) {
850 return -1;
851 }
852 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200853 } else {
854 /* keep in buffer and write later */
Radek Krejci047300e2016-03-08 16:46:58 +0100855 if (xmlcontent) {
856 for (l = 0; l < count; l++) {
857 if (warg->len + 5 >= WRITE_BUFSIZE) {
858 /* buffer is full */
859 c = nc_write_clb_flush(warg);
860 if (c == -1) {
861 return -1;
862 }
863 }
864
865 switch (((char *)buf)[l]) {
866 case '&':
867 ret += 5;
868 memcpy(&warg->buf[warg->len], "&amp;", 5);
869 warg->len += 5;
870 break;
871 case '<':
872 ret += 4;
873 memcpy(&warg->buf[warg->len], "&lt;", 4);
874 warg->len += 4;
875 break;
876 case '>':
877 /* not needed, just for readability */
878 ret += 4;
879 memcpy(&warg->buf[warg->len], "&gt;", 4);
880 warg->len += 4;
881 break;
882 default:
883 ret++;
884 memcpy(&warg->buf[warg->len], &((char *)buf)[l], 1);
885 warg->len++;
886 }
887 }
888 } else {
889 memcpy(&warg->buf[warg->len], buf, count);
890 warg->len += count; /* is <= WRITE_BUFSIZE */
891 ret += count;
892 }
Radek Krejcife0b3472015-10-12 13:43:42 +0200893 }
894
Michal Vasko428087d2016-01-14 16:04:28 +0100895 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200896}
897
Radek Krejci047300e2016-03-08 16:46:58 +0100898static ssize_t
899nc_write_xmlclb(void *arg, const void *buf, size_t count)
900{
Michal Vasko77367452021-02-16 16:32:18 +0100901 ssize_t r;
Radek Krejci047300e2016-03-08 16:46:58 +0100902
Michal Vasko77367452021-02-16 16:32:18 +0100903 r = nc_write_clb(arg, buf, count, 0);
904 if (r == -1) {
905 return -1;
Michal Vasko08611b32016-12-05 13:30:37 +0100906 }
907
Michal Vasko77367452021-02-16 16:32:18 +0100908 /* always return what libyang expects, simply that all the characters were printed */
909 return count;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100910}
911
Michal Vasko131120a2018-05-29 15:44:02 +0200912/* return NC_MSG_ERROR can change session status, acquires IO lock as needed */
913NC_MSG_TYPE
914nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...)
Radek Krejcife0b3472015-10-12 13:43:42 +0200915{
Radek Krejcid116db42016-01-08 15:36:30 +0100916 va_list ap;
Michal Vasko131120a2018-05-29 15:44:02 +0200917 int count, ret;
Michal Vasko77367452021-02-16 16:32:18 +0100918 const char *attrs;
919 struct lyd_node *op, *reply_envp, *node;
920 struct lyd_node_opaq *rpc_envp;
Radek Krejci93e80222016-10-03 13:34:25 +0200921 struct nc_server_notif *notif;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100922 struct nc_server_reply *reply;
Michal Vasko77367452021-02-16 16:32:18 +0100923 char *buf;
Radek Krejcid116db42016-01-08 15:36:30 +0100924 struct wclb_arg arg;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200925 const char **capabilities;
Michal Vasko77367452021-02-16 16:32:18 +0100926 uint32_t *sid = NULL, i, wd = 0;
927 LY_ERR lyrc;
Radek Krejcife0b3472015-10-12 13:43:42 +0200928
Michal Vasko428087d2016-01-14 16:04:28 +0100929 assert(session);
930
931 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100932 ERR("Session %u: invalid session to write to.", session->id);
Michal Vasko131120a2018-05-29 15:44:02 +0200933 return NC_MSG_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100934 }
935
Radek Krejcife0b3472015-10-12 13:43:42 +0200936 arg.session = session;
937 arg.len = 0;
938
Michal Vasko131120a2018-05-29 15:44:02 +0200939 /* SESSION IO LOCK */
940 ret = nc_session_io_lock(session, io_timeout, __func__);
941 if (ret < 0) {
942 return NC_MSG_ERROR;
943 } else if (!ret) {
944 return NC_MSG_WOULDBLOCK;
945 }
946
947 va_start(ap, type);
Radek Krejci127f8952016-10-12 14:57:16 +0200948
Radek Krejcife0b3472015-10-12 13:43:42 +0200949 switch (type) {
950 case NC_MSG_RPC:
Michal Vasko77367452021-02-16 16:32:18 +0100951 op = va_arg(ap, struct lyd_node *);
Radek Krejcife0b3472015-10-12 13:43:42 +0200952 attrs = va_arg(ap, const char *);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100953
Radek Krejcife0b3472015-10-12 13:43:42 +0200954 count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%"PRIu64"\"%s>",
Michal Vasko77367452021-02-16 16:32:18 +0100955 NC_NS_BASE, session->opts.client.msgid + 1, attrs ? attrs : "");
Michal Vasko4eb3c312016-03-01 14:09:37 +0100956 if (count == -1) {
957 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +0200958 ret = NC_MSG_ERROR;
959 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100960 }
Radek Krejci047300e2016-03-08 16:46:58 +0100961 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +0200962 free(buf);
Michal Vaskoe1708602016-10-18 12:17:22 +0200963
Michal Vasko77367452021-02-16 16:32:18 +0100964 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, op, LYD_XML, LYD_PRINT_SHRINK)) {
Michal Vasko131120a2018-05-29 15:44:02 +0200965 ret = NC_MSG_ERROR;
966 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +0200967 }
Radek Krejci047300e2016-03-08 16:46:58 +0100968 nc_write_clb((void *)&arg, "</rpc>", 6, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +0200969
Michal Vasko2e6defd2016-10-07 15:48:15 +0200970 session->opts.client.msgid++;
Radek Krejcife0b3472015-10-12 13:43:42 +0200971 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100972
Radek Krejcife0b3472015-10-12 13:43:42 +0200973 case NC_MSG_REPLY:
Michal Vasko77367452021-02-16 16:32:18 +0100974 rpc_envp = va_arg(ap, struct lyd_node_opaq *);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100975 reply = va_arg(ap, struct nc_server_reply *);
976
Michal Vasko77367452021-02-16 16:32:18 +0100977 if (!rpc_envp) {
978 /* can be NULL if replying with a malformed-message error */
979 nc_write_clb((void *)&arg, "<rpc-reply xmlns=\"" NC_NS_BASE "\">", 18 + strlen(NC_NS_BASE) + 2, 0);
980
981 assert(reply->type == NC_RPL_ERROR);
982 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, ((struct nc_server_reply_error *)reply)->err, LYD_XML,
983 LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS)) {
984 ret = NC_MSG_ERROR;
985 goto cleanup;
986 }
987
988 nc_write_clb((void *)&arg, "</rpc-reply>", 12, 0);
989 break;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +0100990 }
991
Michal Vasko77367452021-02-16 16:32:18 +0100992 /* build a rpc-reply opaque node that can be simply printed */
993 if (lyd_new_opaq2(NULL, session->ctx, "rpc-reply", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, &reply_envp)) {
994 ERRINT;
995 ret = NC_MSG_ERROR;
996 goto cleanup;
Michal Vaskoe7e534f2016-01-15 09:51:09 +0100997 }
Michal Vasko77367452021-02-16 16:32:18 +0100998
Michal Vasko05ba9df2016-01-13 14:40:27 +0100999 switch (reply->type) {
1000 case NC_RPL_OK:
Michal Vasko77367452021-02-16 16:32:18 +01001001 if (lyd_new_opaq2(reply_envp, NULL, "ok", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, NULL)) {
1002 lyd_free_tree(reply_envp);
1003
1004 ERRINT;
1005 ret = NC_MSG_ERROR;
1006 goto cleanup;
Michal Vasko08611b32016-12-05 13:30:37 +01001007 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001008 break;
1009 case NC_RPL_DATA:
Radek Krejci36dfdb32016-09-01 16:56:35 +02001010 switch(((struct nc_server_reply_data *)reply)->wd) {
1011 case NC_WD_UNKNOWN:
1012 case NC_WD_EXPLICIT:
Michal Vasko77367452021-02-16 16:32:18 +01001013 wd = LYD_PRINT_WD_EXPLICIT;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001014 break;
1015 case NC_WD_TRIM:
Michal Vasko77367452021-02-16 16:32:18 +01001016 wd = LYD_PRINT_WD_TRIM;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001017 break;
1018 case NC_WD_ALL:
Michal Vasko77367452021-02-16 16:32:18 +01001019 wd = LYD_PRINT_WD_ALL;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001020 break;
1021 case NC_WD_ALL_TAG:
Michal Vasko77367452021-02-16 16:32:18 +01001022 wd = LYD_PRINT_WD_ALL_TAG;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001023 break;
1024 }
Michal Vasko77367452021-02-16 16:32:18 +01001025
1026 node = ((struct nc_server_reply_data *)reply)->data;
1027 assert(node->schema->nodetype & (LYS_RPC | LYS_ACTION));
1028 if (lyd_child(node)) {
1029 /* temporary */
1030 lyd_child(node)->parent = NULL;
1031 lyd_insert_child(reply_envp, lyd_child(node));
1032 ((struct lyd_node_inner *)node)->child = NULL;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001033 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001034 break;
1035 case NC_RPL_ERROR:
Michal Vasko77367452021-02-16 16:32:18 +01001036 /* temporary */
1037 lyd_insert_child(reply_envp, ((struct nc_server_reply_error *)reply)->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001038 break;
1039 default:
1040 ERRINT;
Radek Krejci047300e2016-03-08 16:46:58 +01001041 nc_write_clb((void *)&arg, NULL, 0, 0);
Michal Vasko131120a2018-05-29 15:44:02 +02001042 ret = NC_MSG_ERROR;
1043 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001044 }
Michal Vasko77367452021-02-16 16:32:18 +01001045
1046 /* temporary */
1047 ((struct lyd_node_opaq *)reply_envp)->attr = rpc_envp->attr;
1048
1049 /* print */
1050 lyrc = lyd_print_clb(nc_write_xmlclb, (void *)&arg, reply_envp, LYD_XML, LYD_PRINT_SHRINK | wd);
1051 ((struct lyd_node_opaq *)reply_envp)->attr = NULL;
1052
1053 /* cleanup */
1054 switch (reply->type) {
1055 case NC_RPL_OK:
1056 /* just free everything */
1057 lyd_free_tree(reply_envp);
1058 break;
1059 case NC_RPL_DATA:
1060 if (lyd_child(reply_envp)) {
1061 /* connect back to the reply structure */
1062 lyd_child(reply_envp)->parent = NULL;
1063 lyd_insert_child(((struct nc_server_reply_data *)reply)->data, lyd_child(reply_envp));
1064 ((struct lyd_node_opaq *)reply_envp)->child = NULL;
1065 }
1066 lyd_free_tree(reply_envp);
1067 break;
1068 case NC_RPL_ERROR:
1069 /* unlink from the data reply */
1070 lyd_unlink_tree(lyd_child(reply_envp));
1071 lyd_free_tree(reply_envp);
1072 break;
1073 default:
1074 break;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001075 }
Michal Vasko77367452021-02-16 16:32:18 +01001076
1077 if (lyrc) {
1078 ret = NC_MSG_ERROR;
1079 goto cleanup;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001080 }
Radek Krejcife0b3472015-10-12 13:43:42 +02001081 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001082
Radek Krejcife0b3472015-10-12 13:43:42 +02001083 case NC_MSG_NOTIF:
Radek Krejci93e80222016-10-03 13:34:25 +02001084 notif = va_arg(ap, struct nc_server_notif *);
1085
1086 nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF"\">", 21 + 47 + 2, 0);
1087 nc_write_clb((void *)&arg, "<eventTime>", 11, 0);
1088 nc_write_clb((void *)&arg, notif->eventtime, strlen(notif->eventtime), 0);
1089 nc_write_clb((void *)&arg, "</eventTime>", 12, 0);
Michal Vasko77367452021-02-16 16:32:18 +01001090 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, notif->ntf, LYD_XML, LYD_PRINT_SHRINK)) {
Michal Vasko131120a2018-05-29 15:44:02 +02001091 ret = NC_MSG_ERROR;
1092 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001093 }
mohitarora24878b2962016-11-09 18:45:33 -05001094 nc_write_clb((void *)&arg, "</notification>", 15, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001095 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001096
Radek Krejcid116db42016-01-08 15:36:30 +01001097 case NC_MSG_HELLO:
1098 if (session->version != NC_VERSION_10) {
Michal Vasko131120a2018-05-29 15:44:02 +02001099 ret = NC_MSG_ERROR;
1100 goto cleanup;
Radek Krejcid116db42016-01-08 15:36:30 +01001101 }
1102 capabilities = va_arg(ap, const char **);
1103 sid = va_arg(ap, uint32_t*);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001104
Radek Krejcid116db42016-01-08 15:36:30 +01001105 count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001106 if (count == -1) {
1107 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001108 ret = NC_MSG_ERROR;
1109 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001110 }
Radek Krejci047300e2016-03-08 16:46:58 +01001111 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001112 free(buf);
1113 for (i = 0; capabilities[i]; i++) {
Radek Krejci047300e2016-03-08 16:46:58 +01001114 nc_write_clb((void *)&arg, "<capability>", 12, 0);
1115 nc_write_clb((void *)&arg, capabilities[i], strlen(capabilities[i]), 1);
1116 nc_write_clb((void *)&arg, "</capability>", 13, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001117 }
1118 if (sid) {
Michal Vasko05ba9df2016-01-13 14:40:27 +01001119 count = asprintf(&buf, "</capabilities><session-id>%u</session-id></hello>", *sid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001120 if (count == -1) {
1121 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001122 ret = NC_MSG_ERROR;
1123 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001124 }
Radek Krejci047300e2016-03-08 16:46:58 +01001125 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001126 free(buf);
1127 } else {
Radek Krejci047300e2016-03-08 16:46:58 +01001128 nc_write_clb((void *)&arg, "</capabilities></hello>", 23, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001129 }
Radek Krejcid116db42016-01-08 15:36:30 +01001130 break;
Michal Vaskoed462342016-01-12 12:33:48 +01001131
Radek Krejcife0b3472015-10-12 13:43:42 +02001132 default:
Michal Vasko131120a2018-05-29 15:44:02 +02001133 ret = NC_MSG_ERROR;
1134 goto cleanup;
Radek Krejcife0b3472015-10-12 13:43:42 +02001135 }
1136
1137 /* flush message */
Radek Krejci047300e2016-03-08 16:46:58 +01001138 nc_write_clb((void *)&arg, NULL, 0, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001139
Michal Vasko428087d2016-01-14 16:04:28 +01001140 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
1141 /* error was already written */
Michal Vasko131120a2018-05-29 15:44:02 +02001142 ret = NC_MSG_ERROR;
1143 } else {
1144 /* specific message successfully sent */
1145 ret = type;
Michal Vasko428087d2016-01-14 16:04:28 +01001146 }
1147
Michal Vasko131120a2018-05-29 15:44:02 +02001148cleanup:
1149 va_end(ap);
1150 nc_session_io_unlock(session, __func__);
1151 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +02001152}
Michal Vasko4eb3c312016-03-01 14:09:37 +01001153
1154void *
1155nc_realloc(void *ptr, size_t size)
1156{
1157 void *ret;
1158
1159 ret = realloc(ptr, size);
1160 if (!ret) {
1161 free(ptr);
1162 }
1163
1164 return ret;
1165}