Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file io.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libnetconf2 - input/output functions |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Miroslav Mareš | 9563b81 | 2017-08-19 17:45:36 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE /* asprintf, signals */ |
Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | #include <errno.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 20 | #include <inttypes.h> |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 21 | #include <pwd.h> |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 26 | #include <sys/types.h> |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 27 | #include <time.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 28 | #include <unistd.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 29 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 30 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 31 | # include <openssl/err.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 32 | # include <openssl/ssl.h> |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 33 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 34 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 35 | #include <libyang/libyang.h> |
| 36 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 37 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 38 | #include "config.h" |
| 39 | #include "log_p.h" |
| 40 | #include "messages_p.h" |
| 41 | #include "netconf.h" |
| 42 | #include "session.h" |
| 43 | #include "session_p.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 44 | |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 45 | const char *nc_msgtype2str[] = { |
| 46 | "error", |
| 47 | "would block", |
| 48 | "no message", |
| 49 | "hello message", |
| 50 | "bad hello message", |
| 51 | "RPC message", |
| 52 | "rpc-reply message", |
| 53 | "rpc-reply message with wrong ID", |
| 54 | "notification message", |
| 55 | }; |
| 56 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 57 | #define BUFFERSIZE 512 |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 58 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 59 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 60 | |
| 61 | static char * |
| 62 | nc_ssl_error_get_reasons(void) |
| 63 | { |
| 64 | unsigned int e; |
| 65 | int reason_size, reason_len; |
| 66 | char *reasons = NULL; |
| 67 | |
| 68 | reason_size = 1; |
| 69 | reason_len = 0; |
| 70 | while ((e = ERR_get_error())) { |
| 71 | if (reason_len) { |
| 72 | /* add "; " */ |
| 73 | reason_size += 2; |
| 74 | reasons = nc_realloc(reasons, reason_size); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 75 | NC_CHECK_ERRMEM_RET(!reasons, NULL); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 76 | reason_len += sprintf(reasons + reason_len, "; "); |
| 77 | } |
| 78 | reason_size += strlen(ERR_reason_error_string(e)); |
| 79 | reasons = nc_realloc(reasons, reason_size); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 80 | NC_CHECK_ERRMEM_RET(!reasons, NULL); |
Rosen Penev | e38d7ef | 2019-07-15 18:18:03 -0700 | [diff] [blame] | 81 | reason_len += sprintf(reasons + reason_len, "%s", ERR_reason_error_string(e)); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return reasons; |
| 85 | } |
| 86 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 87 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 88 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 89 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 90 | nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_timeout, struct timespec *ts_act_timeout) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 91 | { |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 92 | size_t readd = 0; |
Michal Vasko | 9d8bee6 | 2016-03-03 10:58:24 +0100 | [diff] [blame] | 93 | ssize_t r = -1; |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 94 | int fd, interrupted; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 95 | struct timespec ts_inact_timeout; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 96 | |
| 97 | assert(session); |
| 98 | assert(buf); |
| 99 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 100 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 101 | return -1; |
| 102 | } |
| 103 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 104 | if (!count) { |
| 105 | return 0; |
| 106 | } |
| 107 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 108 | nc_timeouttime_get(&ts_inact_timeout, inact_timeout); |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 109 | do { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 110 | interrupted = 0; |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 111 | switch (session->ti_type) { |
| 112 | case NC_TI_NONE: |
| 113 | return 0; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 114 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 115 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 116 | case NC_TI_UNIX: |
| 117 | fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock; |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 118 | /* read via standard file descriptor */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 119 | r = read(fd, buf + readd, count - readd); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 120 | if (r < 0) { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 121 | if (errno == EAGAIN) { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 122 | r = 0; |
| 123 | break; |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 124 | } else if (errno == EINTR) { |
| 125 | r = 0; |
| 126 | interrupted = 1; |
| 127 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 128 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 129 | ERR(session, "Reading from file descriptor (%d) failed (%s).", fd, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 130 | session->status = NC_STATUS_INVALID; |
| 131 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | } else if (r == 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 135 | ERR(session, "Communication file descriptor (%d) unexpectedly closed.", fd); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 136 | session->status = NC_STATUS_INVALID; |
| 137 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 138 | return -1; |
| 139 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 140 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 141 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 142 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 143 | case NC_TI_LIBSSH: |
| 144 | /* read via libssh */ |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 145 | r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 146 | if (r == SSH_AGAIN) { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 147 | r = 0; |
| 148 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 149 | } else if (r == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 150 | ERR(session, "Reading from the SSH channel failed (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 151 | session->status = NC_STATUS_INVALID; |
| 152 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 153 | return -1; |
| 154 | } else if (r == 0) { |
| 155 | if (ssh_channel_is_eof(session->ti.libssh.channel)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 156 | ERR(session, "SSH channel unexpected EOF."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 157 | session->status = NC_STATUS_INVALID; |
| 158 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 159 | return -1; |
| 160 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 161 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 162 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 163 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 164 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 165 | case NC_TI_OPENSSL: |
| 166 | /* read via OpenSSL */ |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 167 | ERR_clear_error(); |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 168 | r = SSL_read(session->ti.tls, buf + readd, count - readd); |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 169 | if (r <= 0) { |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 170 | int e; |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 171 | char *reasons; |
| 172 | |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 173 | switch (e = SSL_get_error(session->ti.tls, r)) { |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 174 | case SSL_ERROR_WANT_READ: |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 175 | case SSL_ERROR_WANT_WRITE: |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 176 | r = 0; |
| 177 | break; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 178 | case SSL_ERROR_ZERO_RETURN: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 179 | ERR(session, "Communication socket unexpectedly closed (OpenSSL)."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 180 | session->status = NC_STATUS_INVALID; |
| 181 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 182 | return -1; |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 183 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 0272dc3 | 2021-09-03 13:02:20 +0200 | [diff] [blame] | 184 | ERR(session, "SSL socket error (%s).", errno ? strerror(errno) : "unexpected EOF"); |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 185 | session->status = NC_STATUS_INVALID; |
| 186 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 187 | return -1; |
| 188 | case SSL_ERROR_SSL: |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 189 | reasons = nc_ssl_error_get_reasons(); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 190 | ERR(session, "SSL error (%s).", reasons); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 191 | free(reasons); |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 192 | session->status = NC_STATUS_INVALID; |
| 193 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 194 | return -1; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 195 | default: |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 196 | ERR(session, "Unknown SSL error occurred (err code %d).", e); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 197 | session->status = NC_STATUS_INVALID; |
| 198 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 199 | return -1; |
| 200 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 201 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 202 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 203 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 206 | if (r == 0) { |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 207 | /* nothing read */ |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 208 | if (!interrupted) { |
| 209 | usleep(NC_TIMEOUT_STEP); |
| 210 | } |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 211 | if ((nc_timeouttime_cur_diff(&ts_inact_timeout) < 1) || (nc_timeouttime_cur_diff(ts_act_timeout) < 1)) { |
| 212 | if (nc_timeouttime_cur_diff(&ts_inact_timeout) < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 213 | ERR(session, "Inactive read timeout elapsed."); |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 214 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 215 | ERR(session, "Active read timeout elapsed."); |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 216 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 217 | session->status = NC_STATUS_INVALID; |
| 218 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 219 | return -1; |
| 220 | } |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 221 | } else { |
| 222 | /* something read */ |
| 223 | readd += r; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 224 | |
| 225 | /* reset inactive timeout */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 226 | nc_timeouttime_get(&ts_inact_timeout, inact_timeout); |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 229 | } while (readd < count); |
| 230 | buf[count] = '\0'; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 231 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 232 | return (ssize_t)readd; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 236 | nc_read_chunk(struct nc_session *session, size_t len, uint32_t inact_timeout, struct timespec *ts_act_timeout, char **chunk) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 237 | { |
| 238 | ssize_t r; |
| 239 | |
| 240 | assert(session); |
| 241 | assert(chunk); |
| 242 | |
| 243 | if (!len) { |
| 244 | return 0; |
| 245 | } |
| 246 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 247 | *chunk = malloc((len + 1) * sizeof **chunk); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 248 | NC_CHECK_ERRMEM_RET(!*chunk, -1); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 249 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 250 | r = nc_read(session, *chunk, len, inact_timeout, ts_act_timeout); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 251 | if (r <= 0) { |
| 252 | free(*chunk); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | /* terminating null byte */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 257 | (*chunk)[r] = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 258 | |
| 259 | return r; |
| 260 | } |
| 261 | |
| 262 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 263 | nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint32_t inact_timeout, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 264 | struct timespec *ts_act_timeout, char **result) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 265 | { |
| 266 | char *chunk = NULL; |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 267 | size_t size, count = 0, r, len, i, matched = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 268 | |
| 269 | assert(session); |
| 270 | assert(endtag); |
| 271 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 272 | if (limit && (limit < BUFFERSIZE)) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 273 | size = limit; |
| 274 | } else { |
| 275 | size = BUFFERSIZE; |
| 276 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 277 | chunk = malloc((size + 1) * sizeof *chunk); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 278 | NC_CHECK_ERRMEM_RET(!chunk, -1); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 279 | |
| 280 | len = strlen(endtag); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 281 | while (1) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 282 | if (limit && (count == limit)) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 283 | free(chunk); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 284 | WRN(session, "Reading limit (%d) reached.", limit); |
| 285 | ERR(session, "Invalid input data (missing \"%s\" sequence).", endtag); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | /* resize buffer if needed */ |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 290 | if ((count + (len - matched)) >= size) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 291 | /* get more memory */ |
| 292 | size = size + BUFFERSIZE; |
Radek Krejci | f6d9aef | 2018-08-17 11:50:53 +0200 | [diff] [blame] | 293 | chunk = nc_realloc(chunk, (size + 1) * sizeof *chunk); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 294 | NC_CHECK_ERRMEM_RET(!chunk, -1); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* get another character */ |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 298 | r = nc_read(session, &(chunk[count]), len - matched, inact_timeout, ts_act_timeout); |
| 299 | if (r != len - matched) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 300 | free(chunk); |
| 301 | return -1; |
| 302 | } |
| 303 | |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 304 | count += len - matched; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 305 | |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 306 | for (i = len - matched; i > 0; i--) { |
| 307 | if (!strncmp(&endtag[matched], &(chunk[count - i]), i)) { |
| 308 | /*part of endtag found */ |
| 309 | matched += i; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 310 | break; |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 311 | } else { |
| 312 | matched = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 313 | } |
| 314 | } |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 315 | |
| 316 | /* whole endtag found */ |
| 317 | if (matched == len) { |
| 318 | break; |
| 319 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* terminating null byte */ |
| 323 | chunk[count] = 0; |
| 324 | |
| 325 | if (result) { |
| 326 | *result = chunk; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 327 | } else { |
| 328 | free(chunk); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 329 | } |
| 330 | return count; |
| 331 | } |
| 332 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 333 | int |
| 334 | nc_read_msg_io(struct nc_session *session, int io_timeout, struct ly_in **msg, int passing_io_lock) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 335 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 336 | int ret = 1, r, io_locked = passing_io_lock; |
| 337 | char *data = NULL, *chunk; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 338 | uint64_t chunk_len, len = 0; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 339 | /* use timeout in milliseconds instead seconds */ |
| 340 | uint32_t inact_timeout = NC_READ_INACT_TIMEOUT * 1000; |
| 341 | struct timespec ts_act_timeout; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 342 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 343 | assert(session && msg); |
| 344 | *msg = NULL; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 345 | |
| 346 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 347 | ERR(session, "Invalid session to read from."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 348 | ret = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 349 | goto cleanup; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 352 | nc_timeouttime_get(&ts_act_timeout, NC_READ_ACT_TIMEOUT * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 353 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 354 | if (!io_locked) { |
| 355 | /* SESSION IO LOCK */ |
| 356 | ret = nc_session_io_lock(session, io_timeout, __func__); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 357 | if (ret < 1) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 358 | goto cleanup; |
| 359 | } |
| 360 | io_locked = 1; |
| 361 | } |
| 362 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 363 | /* read the message */ |
| 364 | switch (session->version) { |
| 365 | case NC_VERSION_10: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 366 | r = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, inact_timeout, &ts_act_timeout, &data); |
| 367 | if (r == -1) { |
| 368 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 369 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* cut off the end tag */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 373 | data[r - NC_VERSION_10_ENDTAG_LEN] = '\0'; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 374 | break; |
| 375 | case NC_VERSION_11: |
| 376 | while (1) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 377 | r = nc_read_until(session, "\n#", 0, inact_timeout, &ts_act_timeout, NULL); |
| 378 | if (r == -1) { |
| 379 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 380 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 381 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 382 | r = nc_read_until(session, "\n", 0, inact_timeout, &ts_act_timeout, &chunk); |
| 383 | if (r == -1) { |
| 384 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 385 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (!strcmp(chunk, "#\n")) { |
| 389 | /* end of chunked framing message */ |
| 390 | free(chunk); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 391 | if (!data) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 392 | ERR(session, "Invalid frame chunk delimiters."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 393 | ret = -2; |
| 394 | goto cleanup; |
Michal Vasko | 79df326 | 2016-07-13 13:42:17 +0200 | [diff] [blame] | 395 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | |
| 399 | /* convert string to the size of the following chunk */ |
| 400 | chunk_len = strtoul(chunk, (char **)NULL, 10); |
| 401 | free(chunk); |
| 402 | if (!chunk_len) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 403 | ERR(session, "Invalid frame chunk size detected, fatal error."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 404 | ret = -2; |
| 405 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* now we have size of next chunk, so read the chunk */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 409 | r = nc_read_chunk(session, chunk_len, inact_timeout, &ts_act_timeout, &chunk); |
| 410 | if (r == -1) { |
| 411 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 412 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* realloc message buffer, remember to count terminating null byte */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 416 | data = nc_realloc(data, len + chunk_len + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 417 | NC_CHECK_ERRMEM_GOTO(!data, ret = -1, cleanup); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 418 | memcpy(data + len, chunk, chunk_len); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 419 | len += chunk_len; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 420 | data[len] = '\0'; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 421 | free(chunk); |
| 422 | } |
| 423 | |
| 424 | break; |
| 425 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 426 | |
| 427 | /* SESSION IO UNLOCK */ |
| 428 | assert(io_locked); |
| 429 | nc_session_io_unlock(session, __func__); |
| 430 | io_locked = 0; |
| 431 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 432 | DBG(session, "Received message:\n%s\n", data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 433 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 434 | /* build an input structure, eats data */ |
| 435 | if (ly_in_new_memory(data, msg)) { |
| 436 | ret = -1; |
| 437 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 438 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 439 | data = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 440 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 441 | cleanup: |
| 442 | if (io_locked) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 443 | /* SESSION IO UNLOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 444 | nc_session_io_unlock(session, __func__); |
| 445 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 446 | free(data); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 447 | return ret; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 450 | /* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */ |
| 451 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 452 | nc_read_poll(struct nc_session *session, int io_timeout) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 453 | { |
| 454 | int ret = -2; |
| 455 | struct pollfd fds; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 456 | |
| 457 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 458 | ERR(session, "Invalid session to poll."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | switch (session->ti_type) { |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 463 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 464 | case NC_TI_LIBSSH: |
| 465 | /* EINTR is handled, it resumes waiting */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 466 | ret = ssh_channel_poll_timeout(session->ti.libssh.channel, io_timeout, 0); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 467 | if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 468 | ERR(session, "SSH channel poll error (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 469 | session->status = NC_STATUS_INVALID; |
| 470 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 471 | return -1; |
| 472 | } else if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 473 | ERR(session, "SSH channel unexpected EOF."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 474 | session->status = NC_STATUS_INVALID; |
| 475 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 476 | return -1; |
| 477 | } else if (ret > 0) { |
| 478 | /* fake it */ |
| 479 | ret = 1; |
| 480 | fds.revents = POLLIN; |
Michal Vasko | 5550cda | 2016-02-03 15:28:57 +0100 | [diff] [blame] | 481 | } else { /* ret == 0 */ |
| 482 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 483 | } |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 484 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 485 | case NC_TI_OPENSSL: |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 486 | ret = SSL_pending(session->ti.tls); |
| 487 | if (ret) { |
| 488 | /* some buffered TLS data available */ |
| 489 | ret = 1; |
| 490 | fds.revents = POLLIN; |
| 491 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 492 | } |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 493 | |
| 494 | fds.fd = SSL_get_fd(session->ti.tls); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 495 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 496 | /* fallthrough */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 497 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 498 | case NC_TI_UNIX: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 499 | if (session->ti_type == NC_TI_FD) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 500 | fds.fd = session->ti.fd.in; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 501 | } else if (session->ti_type == NC_TI_UNIX) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 502 | fds.fd = session->ti.unixsock.sock; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 503 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 504 | |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 505 | fds.events = POLLIN; |
| 506 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 507 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 508 | ret = nc_poll(&fds, 1, io_timeout); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 509 | break; |
| 510 | |
| 511 | default: |
| 512 | ERRINT; |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | /* process the poll result, unified ret meaning for poll and ssh_channel poll */ |
| 517 | if (ret < 0) { |
| 518 | /* poll failed - something really bad happened, close the session */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 519 | ERR(session, "poll error (%s).", strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 520 | session->status = NC_STATUS_INVALID; |
| 521 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 522 | return -1; |
| 523 | } else { /* status > 0 */ |
| 524 | /* in case of standard (non-libssh) poll, there still can be an error */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 525 | if (fds.revents & POLLERR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 526 | ERR(session, "Communication channel error."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 527 | session->status = NC_STATUS_INVALID; |
| 528 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 529 | return -1; |
| 530 | } |
Robin Jarry | f732adc | 2020-05-15 11:18:38 +0200 | [diff] [blame] | 531 | /* Some poll() implementations may return POLLHUP|POLLIN when the other |
| 532 | * side has closed but there is data left to read in the buffer. */ |
| 533 | if ((fds.revents & POLLHUP) && !(fds.revents & POLLIN)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 534 | ERR(session, "Communication channel unexpectedly closed."); |
Robin Jarry | f732adc | 2020-05-15 11:18:38 +0200 | [diff] [blame] | 535 | session->status = NC_STATUS_INVALID; |
| 536 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 537 | return -1; |
| 538 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | return ret; |
| 542 | } |
| 543 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 544 | int |
| 545 | nc_read_msg_poll_io(struct nc_session *session, int io_timeout, struct ly_in **msg) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 546 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 547 | int ret; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 548 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 549 | assert(msg); |
| 550 | *msg = NULL; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 551 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 552 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 553 | ERR(session, "Invalid session to read from."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 554 | return -1; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 557 | /* SESSION IO LOCK */ |
| 558 | ret = nc_session_io_lock(session, io_timeout, __func__); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 559 | if (ret < 1) { |
| 560 | return ret; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | ret = nc_read_poll(session, io_timeout); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 564 | if (ret < 1) { |
| 565 | /* timed out or error */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 566 | |
| 567 | /* SESSION IO UNLOCK */ |
| 568 | nc_session_io_unlock(session, __func__); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 569 | return ret; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 572 | /* SESSION IO LOCK passed down */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 573 | return nc_read_msg_io(session, io_timeout, msg, 1); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 574 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 575 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 576 | /* does not really log, only fatal errors */ |
| 577 | int |
roman | e5675b1 | 2024-03-05 14:26:23 +0100 | [diff] [blame] | 578 | nc_session_is_connected(const struct nc_session *session) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 579 | { |
| 580 | int ret; |
| 581 | struct pollfd fds; |
| 582 | |
| 583 | switch (session->ti_type) { |
| 584 | case NC_TI_FD: |
| 585 | fds.fd = session->ti.fd.in; |
| 586 | break; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 587 | case NC_TI_UNIX: |
| 588 | fds.fd = session->ti.unixsock.sock; |
| 589 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 590 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 591 | case NC_TI_LIBSSH: |
Michal Vasko | 840a8a6 | 2017-02-07 10:56:34 +0100 | [diff] [blame] | 592 | return ssh_is_connected(session->ti.libssh.session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 593 | case NC_TI_OPENSSL: |
| 594 | fds.fd = SSL_get_fd(session->ti.tls); |
| 595 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 596 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | f945da5 | 2018-02-15 08:45:13 +0100 | [diff] [blame] | 597 | default: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Michal Vasko | 840a8a6 | 2017-02-07 10:56:34 +0100 | [diff] [blame] | 601 | if (fds.fd == -1) { |
| 602 | return 0; |
| 603 | } |
| 604 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 605 | fds.events = POLLIN; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 606 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 607 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 608 | ret = nc_poll(&fds, 1, 0); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 609 | if (ret == -1) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 610 | return 0; |
| 611 | } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) { |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | return 1; |
| 616 | } |
| 617 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 618 | #define WRITE_BUFSIZE (2 * BUFFERSIZE) |
| 619 | struct wclb_arg { |
| 620 | struct nc_session *session; |
| 621 | char buf[WRITE_BUFSIZE]; |
| 622 | size_t len; |
| 623 | }; |
| 624 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 625 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 626 | nc_write(struct nc_session *session, const void *buf, size_t count) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 627 | { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 628 | int c, fd, interrupted; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 629 | size_t written = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 630 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 631 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | e2357e9 | 2016-10-05 14:20:47 +0200 | [diff] [blame] | 632 | unsigned long e; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 633 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 634 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 635 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | /* prevent SIGPIPE this way */ |
| 640 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 641 | ERR(session, "Communication socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 642 | session->status = NC_STATUS_INVALID; |
| 643 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 644 | return -1; |
| 645 | } |
| 646 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 647 | DBG(session, "Sending message:\n%.*s\n", count, buf); |
Michal Vasko | 160b791 | 2016-06-20 10:00:53 +0200 | [diff] [blame] | 648 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 649 | do { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 650 | interrupted = 0; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 651 | switch (session->ti_type) { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 652 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 653 | case NC_TI_UNIX: |
| 654 | fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock; |
| 655 | c = write(fd, (char *)(buf + written), count - written); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 656 | if ((c < 0) && (errno == EAGAIN)) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 657 | c = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 658 | } else if ((c < 0) && (errno == EINTR)) { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 659 | c = 0; |
| 660 | interrupted = 1; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 661 | } else if (c < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 662 | ERR(session, "socket error (%s).", strerror(errno)); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 666 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 667 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 668 | case NC_TI_LIBSSH: |
| 669 | if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) { |
| 670 | if (ssh_channel_is_closed(session->ti.libssh.channel)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 671 | ERR(session, "SSH channel unexpectedly closed."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 672 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 673 | ERR(session, "SSH channel unexpected EOF."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 674 | } |
| 675 | session->status = NC_STATUS_INVALID; |
| 676 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 677 | return -1; |
Michal Vasko | 454e22b | 2016-01-21 15:34:08 +0100 | [diff] [blame] | 678 | } |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 679 | c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 680 | if ((c == SSH_ERROR) || (c == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 681 | ERR(session, "SSH channel write failed."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 682 | return -1; |
| 683 | } |
| 684 | break; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 685 | case NC_TI_OPENSSL: |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 686 | c = SSL_write(session->ti.tls, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 687 | if (c < 1) { |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 688 | char *reasons; |
| 689 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 690 | switch ((e = SSL_get_error(session->ti.tls, c))) { |
| 691 | case SSL_ERROR_ZERO_RETURN: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 692 | ERR(session, "SSL connection was properly closed."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 693 | return -1; |
| 694 | case SSL_ERROR_WANT_WRITE: |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 695 | case SSL_ERROR_WANT_READ: |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 696 | c = 0; |
| 697 | break; |
| 698 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 699 | ERR(session, "SSL socket error (%s).", strerror(errno)); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 700 | return -1; |
| 701 | case SSL_ERROR_SSL: |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 702 | reasons = nc_ssl_error_get_reasons(); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 703 | ERR(session, "SSL error (%s).", reasons); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 704 | free(reasons); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 705 | return -1; |
| 706 | default: |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 707 | ERR(session, "Unknown SSL error occurred (err code %d).", e); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 708 | return -1; |
| 709 | } |
| 710 | } |
| 711 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 712 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 339eea8 | 2016-09-29 11:42:36 +0200 | [diff] [blame] | 713 | default: |
| 714 | ERRINT; |
| 715 | return -1; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 718 | if ((c == 0) && !interrupted) { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 719 | /* we must wait */ |
| 720 | usleep(NC_TIMEOUT_STEP); |
| 721 | } |
| 722 | |
| 723 | written += c; |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 724 | } while (written < count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 725 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 726 | return written; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 729 | static int |
| 730 | nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 731 | { |
Michal Vasko | fd2db9b | 2016-01-14 16:15:16 +0100 | [diff] [blame] | 732 | int ret = 0, c; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 733 | char chunksize[24]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 734 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 735 | // warning: ‘%zu’ directive writing between 4 and 20 bytes into a region of size 18 [-Wformat-overflow=] |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 736 | if (session->version == NC_VERSION_11) { |
| 737 | sprintf(chunksize, "\n#%zu\n", count); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 738 | ret = nc_write(session, chunksize, strlen(chunksize)); |
| 739 | if (ret == -1) { |
| 740 | return -1; |
| 741 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 742 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 743 | |
| 744 | c = nc_write(session, buf, count); |
| 745 | if (c == -1) { |
| 746 | return -1; |
| 747 | } |
| 748 | ret += c; |
| 749 | |
| 750 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 753 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 754 | nc_write_endtag(struct nc_session *session) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 755 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 756 | int ret; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 757 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 758 | if (session->version == NC_VERSION_11) { |
| 759 | ret = nc_write(session, "\n##\n", 4); |
| 760 | } else { |
| 761 | ret = nc_write(session, "]]>]]>", 6); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 764 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 767 | static int |
| 768 | nc_write_clb_flush(struct wclb_arg *warg) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 769 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 770 | int ret = 0; |
| 771 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 772 | /* flush current buffer */ |
| 773 | if (warg->len) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 774 | ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 775 | warg->len = 0; |
| 776 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 777 | |
| 778 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static ssize_t |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 782 | nc_write_clb(void *arg, const void *buf, size_t count, int xmlcontent) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 783 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 784 | int ret = 0, c; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 785 | size_t l; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 786 | struct wclb_arg *warg = (struct wclb_arg *)arg; |
| 787 | |
| 788 | if (!buf) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 789 | c = nc_write_clb_flush(warg); |
| 790 | if (c == -1) { |
| 791 | return -1; |
| 792 | } |
| 793 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 794 | |
| 795 | /* endtag */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 796 | c = nc_write_endtag(warg->session); |
| 797 | if (c == -1) { |
| 798 | return -1; |
| 799 | } |
| 800 | ret += c; |
| 801 | |
| 802 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (warg->len && (warg->len + count > WRITE_BUFSIZE)) { |
| 806 | /* dump current buffer */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 807 | c = nc_write_clb_flush(warg); |
| 808 | if (c == -1) { |
| 809 | return -1; |
| 810 | } |
| 811 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 812 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 813 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 814 | if (!xmlcontent && (count > WRITE_BUFSIZE)) { |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 815 | /* write directly */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 816 | c = nc_write_starttag_and_msg(warg->session, buf, count); |
| 817 | if (c == -1) { |
| 818 | return -1; |
| 819 | } |
| 820 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 821 | } else { |
| 822 | /* keep in buffer and write later */ |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 823 | if (xmlcontent) { |
| 824 | for (l = 0; l < count; l++) { |
| 825 | if (warg->len + 5 >= WRITE_BUFSIZE) { |
| 826 | /* buffer is full */ |
| 827 | c = nc_write_clb_flush(warg); |
| 828 | if (c == -1) { |
| 829 | return -1; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | switch (((char *)buf)[l]) { |
| 834 | case '&': |
| 835 | ret += 5; |
| 836 | memcpy(&warg->buf[warg->len], "&", 5); |
| 837 | warg->len += 5; |
| 838 | break; |
| 839 | case '<': |
| 840 | ret += 4; |
| 841 | memcpy(&warg->buf[warg->len], "<", 4); |
| 842 | warg->len += 4; |
| 843 | break; |
| 844 | case '>': |
| 845 | /* not needed, just for readability */ |
| 846 | ret += 4; |
| 847 | memcpy(&warg->buf[warg->len], ">", 4); |
| 848 | warg->len += 4; |
| 849 | break; |
| 850 | default: |
| 851 | ret++; |
| 852 | memcpy(&warg->buf[warg->len], &((char *)buf)[l], 1); |
| 853 | warg->len++; |
| 854 | } |
| 855 | } |
| 856 | } else { |
| 857 | memcpy(&warg->buf[warg->len], buf, count); |
| 858 | warg->len += count; /* is <= WRITE_BUFSIZE */ |
| 859 | ret += count; |
| 860 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 863 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 866 | static ssize_t |
| 867 | nc_write_xmlclb(void *arg, const void *buf, size_t count) |
| 868 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 869 | ssize_t r; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 870 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 871 | r = nc_write_clb(arg, buf, count, 0); |
| 872 | if (r == -1) { |
| 873 | return -1; |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 876 | /* always return what libyang expects, simply that all the characters were printed */ |
| 877 | return count; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 878 | } |
| 879 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 880 | /* return NC_MSG_ERROR can change session status, acquires IO lock as needed */ |
| 881 | NC_MSG_TYPE |
| 882 | nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 883 | { |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 884 | va_list ap; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 885 | int count, ret; |
Michal Vasko | 70bf222 | 2021-10-26 10:45:15 +0200 | [diff] [blame] | 886 | const char *attrs, *str; |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 887 | struct lyd_node *op, *reply_envp, *node, *next; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 888 | struct lyd_node_opaq *rpc_envp; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 889 | struct nc_server_notif *notif; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 890 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 891 | char *buf; |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 892 | struct wclb_arg arg; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 893 | const char **capabilities; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 894 | uint32_t *sid = NULL, i, wd = 0; |
| 895 | LY_ERR lyrc; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 896 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 897 | assert(session); |
| 898 | |
| 899 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 900 | ERR(session, "Invalid session to write to."); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 901 | return NC_MSG_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 902 | } |
| 903 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 904 | arg.session = session; |
| 905 | arg.len = 0; |
| 906 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 907 | /* SESSION IO LOCK */ |
| 908 | ret = nc_session_io_lock(session, io_timeout, __func__); |
| 909 | if (ret < 0) { |
| 910 | return NC_MSG_ERROR; |
| 911 | } else if (!ret) { |
| 912 | return NC_MSG_WOULDBLOCK; |
| 913 | } |
| 914 | |
| 915 | va_start(ap, type); |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 916 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 917 | switch (type) { |
| 918 | case NC_MSG_RPC: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 919 | op = va_arg(ap, struct lyd_node *); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 920 | attrs = va_arg(ap, const char *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 921 | |
Michal Vasko | 70bf222 | 2021-10-26 10:45:15 +0200 | [diff] [blame] | 922 | /* <rpc> open */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 923 | count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%" PRIu64 "\"%s>", |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 924 | NC_NS_BASE, session->opts.client.msgid + 1, attrs ? attrs : ""); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 925 | NC_CHECK_ERRMEM_GOTO(count == -1, ret = NC_MSG_ERROR, cleanup); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 926 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 927 | free(buf); |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 928 | |
Michal Vasko | 7e1f5fb | 2021-11-10 10:14:45 +0100 | [diff] [blame] | 929 | if (op->schema && (op->schema->nodetype & (LYS_CONTAINER | LYS_LIST))) { |
Michal Vasko | 70bf222 | 2021-10-26 10:45:15 +0200 | [diff] [blame] | 930 | /* <action> open */ |
| 931 | str = "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"; |
| 932 | nc_write_clb((void *)&arg, str, strlen(str), 0); |
| 933 | } |
| 934 | |
| 935 | /* rpc data */ |
Michal Vasko | 6b70b82 | 2022-01-21 08:39:16 +0100 | [diff] [blame] | 936 | if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, op, LYD_XML, LYD_PRINT_SHRINK | LYD_PRINT_KEEPEMPTYCONT)) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 937 | ret = NC_MSG_ERROR; |
| 938 | goto cleanup; |
Michal Vasko | 5a91ce7 | 2017-10-19 11:30:02 +0200 | [diff] [blame] | 939 | } |
Michal Vasko | 70bf222 | 2021-10-26 10:45:15 +0200 | [diff] [blame] | 940 | |
Michal Vasko | 7e1f5fb | 2021-11-10 10:14:45 +0100 | [diff] [blame] | 941 | if (op->schema && (op->schema->nodetype & (LYS_CONTAINER | LYS_LIST))) { |
Michal Vasko | 70bf222 | 2021-10-26 10:45:15 +0200 | [diff] [blame] | 942 | /* <action> close */ |
| 943 | str = "</action>"; |
| 944 | nc_write_clb((void *)&arg, str, strlen(str), 0); |
| 945 | } |
| 946 | |
| 947 | /* <rpc> close */ |
| 948 | str = "</rpc>"; |
| 949 | nc_write_clb((void *)&arg, str, strlen(str), 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 950 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 951 | session->opts.client.msgid++; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 952 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 953 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 954 | case NC_MSG_REPLY: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 955 | rpc_envp = va_arg(ap, struct lyd_node_opaq *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 956 | reply = va_arg(ap, struct nc_server_reply *); |
| 957 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 958 | if (!rpc_envp) { |
| 959 | /* can be NULL if replying with a malformed-message error */ |
| 960 | nc_write_clb((void *)&arg, "<rpc-reply xmlns=\"" NC_NS_BASE "\">", 18 + strlen(NC_NS_BASE) + 2, 0); |
| 961 | |
| 962 | assert(reply->type == NC_RPL_ERROR); |
| 963 | if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, ((struct nc_server_reply_error *)reply)->err, LYD_XML, |
| 964 | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS)) { |
| 965 | ret = NC_MSG_ERROR; |
| 966 | goto cleanup; |
| 967 | } |
| 968 | |
| 969 | nc_write_clb((void *)&arg, "</rpc-reply>", 12, 0); |
| 970 | break; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 973 | /* build a rpc-reply opaque node that can be simply printed */ |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 974 | if (lyd_new_opaq2(NULL, session->ctx, "rpc-reply", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, |
| 975 | &reply_envp)) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 976 | ERRINT; |
| 977 | ret = NC_MSG_ERROR; |
| 978 | goto cleanup; |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 979 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 980 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 981 | switch (reply->type) { |
| 982 | case NC_RPL_OK: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 983 | if (lyd_new_opaq2(reply_envp, NULL, "ok", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, NULL)) { |
| 984 | lyd_free_tree(reply_envp); |
| 985 | |
| 986 | ERRINT; |
| 987 | ret = NC_MSG_ERROR; |
| 988 | goto cleanup; |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 989 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 990 | break; |
| 991 | case NC_RPL_DATA: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 992 | switch (((struct nc_server_reply_data *)reply)->wd) { |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 993 | case NC_WD_UNKNOWN: |
| 994 | case NC_WD_EXPLICIT: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 995 | wd = LYD_PRINT_WD_EXPLICIT; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 996 | break; |
| 997 | case NC_WD_TRIM: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 998 | wd = LYD_PRINT_WD_TRIM; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 999 | break; |
| 1000 | case NC_WD_ALL: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1001 | wd = LYD_PRINT_WD_ALL; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1002 | break; |
| 1003 | case NC_WD_ALL_TAG: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1004 | wd = LYD_PRINT_WD_ALL_TAG; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1005 | break; |
| 1006 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1007 | |
| 1008 | node = ((struct nc_server_reply_data *)reply)->data; |
| 1009 | assert(node->schema->nodetype & (LYS_RPC | LYS_ACTION)); |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1010 | LY_LIST_FOR_SAFE(lyd_child(node), next, node) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1011 | /* temporary */ |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1012 | lyd_insert_child(reply_envp, node); |
Michal Vasko | 5a91ce7 | 2017-10-19 11:30:02 +0200 | [diff] [blame] | 1013 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1014 | break; |
| 1015 | case NC_RPL_ERROR: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1016 | /* temporary */ |
| 1017 | lyd_insert_child(reply_envp, ((struct nc_server_reply_error *)reply)->err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1018 | break; |
| 1019 | default: |
| 1020 | ERRINT; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1021 | nc_write_clb((void *)&arg, NULL, 0, 0); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1022 | ret = NC_MSG_ERROR; |
| 1023 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1024 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1025 | |
| 1026 | /* temporary */ |
| 1027 | ((struct lyd_node_opaq *)reply_envp)->attr = rpc_envp->attr; |
| 1028 | |
| 1029 | /* print */ |
| 1030 | lyrc = lyd_print_clb(nc_write_xmlclb, (void *)&arg, reply_envp, LYD_XML, LYD_PRINT_SHRINK | wd); |
| 1031 | ((struct lyd_node_opaq *)reply_envp)->attr = NULL; |
| 1032 | |
| 1033 | /* cleanup */ |
| 1034 | switch (reply->type) { |
| 1035 | case NC_RPL_OK: |
| 1036 | /* just free everything */ |
| 1037 | lyd_free_tree(reply_envp); |
| 1038 | break; |
| 1039 | case NC_RPL_DATA: |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1040 | LY_LIST_FOR_SAFE(lyd_child(reply_envp), next, node) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1041 | /* connect back to the reply structure */ |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1042 | lyd_insert_child(((struct nc_server_reply_data *)reply)->data, node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1043 | } |
| 1044 | lyd_free_tree(reply_envp); |
| 1045 | break; |
| 1046 | case NC_RPL_ERROR: |
| 1047 | /* unlink from the data reply */ |
| 1048 | lyd_unlink_tree(lyd_child(reply_envp)); |
| 1049 | lyd_free_tree(reply_envp); |
| 1050 | break; |
| 1051 | default: |
| 1052 | break; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1053 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1054 | |
| 1055 | if (lyrc) { |
| 1056 | ret = NC_MSG_ERROR; |
| 1057 | goto cleanup; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1058 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1059 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1060 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1061 | case NC_MSG_NOTIF: |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1062 | notif = va_arg(ap, struct nc_server_notif *); |
| 1063 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1064 | nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF "\">", 21 + 47 + 2, 0); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1065 | nc_write_clb((void *)&arg, "<eventTime>", 11, 0); |
| 1066 | nc_write_clb((void *)&arg, notif->eventtime, strlen(notif->eventtime), 0); |
| 1067 | nc_write_clb((void *)&arg, "</eventTime>", 12, 0); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1068 | if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, notif->ntf, LYD_XML, LYD_PRINT_SHRINK)) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1069 | ret = NC_MSG_ERROR; |
| 1070 | goto cleanup; |
Michal Vasko | 5a91ce7 | 2017-10-19 11:30:02 +0200 | [diff] [blame] | 1071 | } |
mohitarora24 | 878b296 | 2016-11-09 18:45:33 -0500 | [diff] [blame] | 1072 | nc_write_clb((void *)&arg, "</notification>", 15, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1073 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1074 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1075 | case NC_MSG_HELLO: |
| 1076 | if (session->version != NC_VERSION_10) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1077 | ret = NC_MSG_ERROR; |
| 1078 | goto cleanup; |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1079 | } |
| 1080 | capabilities = va_arg(ap, const char **); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1081 | sid = va_arg(ap, uint32_t *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1082 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1083 | count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1084 | NC_CHECK_ERRMEM_GOTO(count == -1, ret = NC_MSG_ERROR, cleanup); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1085 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1086 | free(buf); |
| 1087 | for (i = 0; capabilities[i]; i++) { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1088 | nc_write_clb((void *)&arg, "<capability>", 12, 0); |
| 1089 | nc_write_clb((void *)&arg, capabilities[i], strlen(capabilities[i]), 1); |
| 1090 | nc_write_clb((void *)&arg, "</capability>", 13, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1091 | } |
| 1092 | if (sid) { |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 1093 | count = asprintf(&buf, "</capabilities><session-id>%" PRIu32 "</session-id></hello>", *sid); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1094 | NC_CHECK_ERRMEM_GOTO(count == -1, ret = NC_MSG_ERROR, cleanup); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1095 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1096 | free(buf); |
| 1097 | } else { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1098 | nc_write_clb((void *)&arg, "</capabilities></hello>", 23, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1099 | } |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1100 | break; |
Michal Vasko | ed46234 | 2016-01-12 12:33:48 +0100 | [diff] [blame] | 1101 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1102 | default: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1103 | ret = NC_MSG_ERROR; |
| 1104 | goto cleanup; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* flush message */ |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1108 | nc_write_clb((void *)&arg, NULL, 0, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1109 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1110 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 1111 | /* error was already written */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1112 | ret = NC_MSG_ERROR; |
| 1113 | } else { |
| 1114 | /* specific message successfully sent */ |
| 1115 | ret = type; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1118 | cleanup: |
| 1119 | va_end(ap); |
| 1120 | nc_session_io_unlock(session, __func__); |
| 1121 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1122 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1123 | |
| 1124 | void * |
| 1125 | nc_realloc(void *ptr, size_t size) |
| 1126 | { |
| 1127 | void *ret; |
| 1128 | |
| 1129 | ret = realloc(ptr, size); |
| 1130 | if (!ret) { |
| 1131 | free(ptr); |
| 1132 | } |
| 1133 | |
| 1134 | return ret; |
| 1135 | } |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1136 | |
| 1137 | struct passwd * |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1138 | nc_getpw(uid_t uid, const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size) |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1139 | { |
| 1140 | struct passwd *pwd = NULL; |
Michal Vasko | 7e06ee5 | 2021-11-02 08:53:05 +0100 | [diff] [blame] | 1141 | long sys_size; |
| 1142 | int ret; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1143 | |
| 1144 | do { |
Michal Vasko | 7e06ee5 | 2021-11-02 08:53:05 +0100 | [diff] [blame] | 1145 | if (!*buf_size) { |
| 1146 | /* learn suitable buffer size */ |
| 1147 | sys_size = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 1148 | *buf_size = (sys_size == -1) ? 2048 : sys_size; |
| 1149 | } else { |
| 1150 | /* enlarge buffer */ |
| 1151 | *buf_size += 2048; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Michal Vasko | 7e06ee5 | 2021-11-02 08:53:05 +0100 | [diff] [blame] | 1154 | /* allocate some buffer */ |
| 1155 | *buf = nc_realloc(*buf, *buf_size); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1156 | NC_CHECK_ERRMEM_RET(!*buf, NULL); |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1157 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1158 | if (username) { |
| 1159 | ret = getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd); |
| 1160 | } else { |
| 1161 | ret = getpwuid_r(uid, pwd_buf, *buf, *buf_size, &pwd); |
| 1162 | } |
Michal Vasko | 7e06ee5 | 2021-11-02 08:53:05 +0100 | [diff] [blame] | 1163 | } while (ret && (ret == ERANGE)); |
| 1164 | |
| 1165 | if (ret) { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1166 | if (username) { |
| 1167 | ERR(NULL, "Retrieving username \"%s\" passwd entry failed (%s).", username, strerror(ret)); |
| 1168 | } else { |
| 1169 | ERR(NULL, "Retrieving UID \"%lu\" passwd entry failed (%s).", (unsigned long)uid, strerror(ret)); |
| 1170 | } |
Michal Vasko | 7e06ee5 | 2021-11-02 08:53:05 +0100 | [diff] [blame] | 1171 | } |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1172 | return pwd; |
| 1173 | } |