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