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