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