aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanislav Zidek <szidek@redhat.com>2020-10-31 21:53:34 +0100
committerAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2020-11-03 18:23:47 +0100
commitb25feb3386a1435d78d3918742cff1df291cfcdb (patch)
tree7b159e602fda4df128e5dde6774293699e8bd320
parent635edc8adbbd5441cf070ae92ac448ae2c58293b (diff)
downloadlibssh-b25feb3386a1435d78d3918742cff1df291cfcdb.tar.gz
libssh-b25feb3386a1435d78d3918742cff1df291cfcdb.tar.xz
libssh-b25feb3386a1435d78d3918742cff1df291cfcdb.zip
New API for parsing configuration from string
Fixes T248 Signed-off-by: Stanislav Zidek <szidek@redhat.com> Reviewed-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
-rw-r--r--include/libssh/options.h1
-rw-r--r--src/config.c54
2 files changed, 55 insertions, 0 deletions
diff --git a/include/libssh/options.h b/include/libssh/options.h
index 5b1cb9f0..e8dc6c69 100644
--- a/include/libssh/options.h
+++ b/include/libssh/options.h
@@ -22,6 +22,7 @@
#define _OPTIONS_H
int ssh_config_parse_file(ssh_session session, const char *filename);
+int ssh_config_parse_string(ssh_session session, const char *input);
int ssh_options_set_algo(ssh_session session,
enum ssh_kex_types_e algo,
const char *list);
diff --git a/src/config.c b/src/config.c
index 79a64339..198a4726 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1170,3 +1170,57 @@ int ssh_config_parse_file(ssh_session session, const char *filename)
fclose(f);
return 0;
}
+
+/* @brief Parse configuration string and set the options to the given session
+ *
+ * @params[in] session The ssh session
+ * @params[in] input Null terminated string containing the configuration
+ *
+ * @returns SSH_OK on successful parsing the configuration string,
+ * SSH_ERROR on error
+ */
+int ssh_config_parse_string(ssh_session session, const char *input)
+{
+ char line[MAX_LINE_SIZE] = {0};
+ const char *c = input, *line_start = input;
+ unsigned int line_num = 0, line_len;
+ int parsing, rv;
+
+ SSH_LOG(SSH_LOG_DEBUG, "Reading configuration data from string:");
+ SSH_LOG(SSH_LOG_DEBUG, "START\n%s\nEND", input);
+
+ parsing = 1;
+ while (1) {
+ line_num++;
+ line_start = c;
+ c = strchr(line_start, '\n');
+ if (c == NULL) {
+ /* if there is no newline in the end of the string */
+ c = strchr(line_start, '\0');
+ }
+ if (c == NULL) {
+ /* should not happen, would mean a string without trailing '\0' */
+ SSH_LOG(SSH_LOG_WARN, "No trailing '\\0' in config string");
+ return SSH_ERROR;
+ }
+ line_len = c - line_start;
+ if (line_len > MAX_LINE_SIZE - 1) {
+ SSH_LOG(SSH_LOG_WARN, "Line %u too long: %u characters",
+ line_num, line_len);
+ return SSH_ERROR;
+ }
+ memcpy(line, line_start, line_len);
+ line[line_len] = '\0';
+ SSH_LOG(SSH_LOG_DEBUG, "Line %u: %s", line_num, line);
+ rv = ssh_config_parse_line(session, line, line_num, &parsing);
+ if (rv < 0) {
+ return SSH_ERROR;
+ }
+ if (*c == '\0') {
+ break;
+ }
+ c++;
+ }
+
+ return SSH_OK;
+}