1
1

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>
Этот коммит содержится в:
Stanislav Zidek 2020-10-31 21:53:34 +01:00 коммит произвёл Anderson Toshiyuki Sasaki
родитель 635edc8adb
Коммит b25feb3386
2 изменённых файлов: 55 добавлений и 0 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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;
}