2012-09-20 07:51:34 +04:00
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
2014-03-02 11:26:40 +04:00
|
|
|
* Copyright (C) Jay Sorg 2004-2014
|
2012-09-20 07:51:34 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* read a config file
|
|
|
|
*/
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2017-03-03 07:33:23 +03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config_ac.h>
|
|
|
|
#endif
|
|
|
|
|
2005-06-28 07:04:36 +04:00
|
|
|
#include "arch.h"
|
|
|
|
#include "os_calls.h"
|
2020-12-21 15:36:00 +03:00
|
|
|
#include "string_calls.h"
|
2005-06-28 07:04:36 +04:00
|
|
|
#include "list.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "parse.h"
|
|
|
|
|
2014-06-19 08:18:47 +04:00
|
|
|
#define FILE_MAX_LINE_BYTES 2048
|
|
|
|
|
2017-03-12 19:35:00 +03:00
|
|
|
static int
|
2015-08-20 00:02:50 +03:00
|
|
|
file_read_ini_line(struct stream *s, char *text, int text_bytes);
|
|
|
|
|
2016-02-10 05:29:52 +03:00
|
|
|
/*****************************************************************************/
|
2015-08-20 00:02:50 +03:00
|
|
|
/* look up for a section name within str (i.e. pattern [section_name])
|
|
|
|
* if a section name is found, this function return 1 and copy the section
|
|
|
|
* inplace of str. */
|
2017-03-12 19:35:00 +03:00
|
|
|
static int
|
2016-02-10 05:29:52 +03:00
|
|
|
line_lookup_for_section_name(char *str, int str_bytes)
|
|
|
|
{
|
2015-08-20 00:02:50 +03:00
|
|
|
int name_index_start;
|
|
|
|
int index;
|
|
|
|
char c;
|
2016-02-10 05:29:52 +03:00
|
|
|
|
2015-08-20 00:02:50 +03:00
|
|
|
name_index_start = -1;
|
|
|
|
index = 0;
|
2016-02-10 05:29:52 +03:00
|
|
|
while ((c = str[index]) != 0)
|
|
|
|
{
|
2015-08-20 00:02:50 +03:00
|
|
|
if (c == '[')
|
|
|
|
{
|
2016-02-10 05:29:52 +03:00
|
|
|
name_index_start = index + 1;
|
2015-08-20 00:02:50 +03:00
|
|
|
}
|
|
|
|
else if (c == ']' && name_index_start > 0)
|
|
|
|
{
|
2016-02-10 05:29:52 +03:00
|
|
|
if (name_index_start + index >= str_bytes)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (index = index - name_index_start; index > 0; index--)
|
|
|
|
{
|
|
|
|
str[0] = str[name_index_start];
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
str[0] = 0;
|
2015-08-20 00:02:50 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-28 07:04:36 +04:00
|
|
|
/*****************************************************************************/
|
2005-09-25 08:08:26 +04:00
|
|
|
/* returns error
|
|
|
|
returns 0 if everything is ok
|
|
|
|
returns 1 if problem reading file */
|
2017-03-12 19:35:00 +03:00
|
|
|
static int
|
2012-09-20 07:51:34 +04:00
|
|
|
l_file_read_sections(int fd, int max_file_size, struct list *names)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
struct stream *s;
|
2014-06-19 08:18:47 +04:00
|
|
|
char text[FILE_MAX_LINE_BYTES];
|
2012-09-20 07:51:34 +04:00
|
|
|
int len;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
g_file_seek(fd, 0);
|
2014-06-19 08:18:47 +04:00
|
|
|
g_memset(text, 0, FILE_MAX_LINE_BYTES);
|
2012-09-20 07:51:34 +04:00
|
|
|
list_clear(names);
|
|
|
|
make_stream(s);
|
|
|
|
init_stream(s, max_file_size);
|
|
|
|
len = g_file_read(fd, s->data, max_file_size);
|
|
|
|
|
|
|
|
if (len > 0)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
s->end = s->p + len;
|
2016-02-10 05:29:52 +03:00
|
|
|
while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
|
|
|
|
{
|
|
|
|
if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
|
|
|
|
{
|
2023-03-06 16:54:22 +03:00
|
|
|
list_add_strdup(names, text);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
}
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
else if (len < 0)
|
|
|
|
{
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_stream(s);
|
|
|
|
return rv;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2015-08-20 00:02:50 +03:00
|
|
|
/* Read a line in the stream 's', removing comments.
|
|
|
|
* returns error
|
|
|
|
* returns 0 if everything is ok
|
|
|
|
* returns 1 if problem reading file */
|
2017-03-12 19:35:00 +03:00
|
|
|
static int
|
2015-08-20 00:02:50 +03:00
|
|
|
file_read_ini_line(struct stream *s, char *text, int text_bytes)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int i;
|
|
|
|
int skip_to_end;
|
|
|
|
int at_end;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
skip_to_end = 0;
|
|
|
|
|
|
|
|
if (!s_check_rem(s, 1))
|
2007-01-15 12:02:21 +03:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
return 1;
|
2007-01-15 12:02:21 +03:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
in_uint8(s, c);
|
|
|
|
|
|
|
|
while (c != 10 && c != 13)
|
2007-01-15 12:02:21 +03:00
|
|
|
{
|
2014-06-19 08:18:47 +04:00
|
|
|
/* these mean skip the rest of the line */
|
2018-02-13 07:34:57 +03:00
|
|
|
if (c == '#' || c == ';')
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
skip_to_end = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skip_to_end)
|
|
|
|
{
|
|
|
|
text[i] = c;
|
|
|
|
i++;
|
2014-06-19 08:18:47 +04:00
|
|
|
if (i >= text_bytes)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s_check_rem(s, 1))
|
|
|
|
{
|
|
|
|
in_uint8(s, c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
}
|
2007-01-15 12:02:21 +03:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (c == 10 || c == 13)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
at_end = 0;
|
|
|
|
|
|
|
|
while (c == 10 || c == 13)
|
|
|
|
{
|
|
|
|
if (s_check_rem(s, 1))
|
|
|
|
{
|
|
|
|
in_uint8(s, c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
at_end = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!at_end)
|
|
|
|
{
|
|
|
|
s->p--;
|
|
|
|
}
|
2007-01-15 12:02:21 +03:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
text[i] = 0;
|
|
|
|
|
|
|
|
return 0;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2015-08-20 00:02:50 +03:00
|
|
|
|
2005-06-28 07:04:36 +04:00
|
|
|
/*****************************************************************************/
|
2007-10-28 08:49:08 +03:00
|
|
|
/* returns error */
|
2017-03-12 19:35:00 +03:00
|
|
|
static int
|
2012-09-20 07:51:34 +04:00
|
|
|
file_split_name_value(char *text, char *name, char *value)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
int value_index;
|
|
|
|
int name_index;
|
|
|
|
int on_to;
|
|
|
|
|
|
|
|
value_index = 0;
|
|
|
|
name_index = 0;
|
|
|
|
on_to = 0;
|
|
|
|
name[0] = 0;
|
|
|
|
value[0] = 0;
|
|
|
|
len = g_strlen(text);
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2017-06-22 08:41:33 +03:00
|
|
|
if (text[i] == '=' && !on_to)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
on_to = 1;
|
|
|
|
}
|
|
|
|
else if (on_to)
|
|
|
|
{
|
|
|
|
value[value_index] = text[i];
|
|
|
|
value_index++;
|
|
|
|
value[value_index] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name[name_index] = text[i];
|
|
|
|
name_index++;
|
|
|
|
name[name_index] = 0;
|
|
|
|
}
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
g_strtrim(name, 3); /* trim both right and left */
|
|
|
|
g_strtrim(value, 3); /* trim both right and left */
|
|
|
|
return 0;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2005-09-29 07:19:52 +04:00
|
|
|
/* return error */
|
2017-03-12 19:35:00 +03:00
|
|
|
static int
|
2012-09-20 07:51:34 +04:00
|
|
|
l_file_read_section(int fd, int max_file_size, const char *section,
|
|
|
|
struct list *names, struct list *values)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
struct stream *s;
|
2014-06-19 08:18:47 +04:00
|
|
|
char *data;
|
|
|
|
char *text;
|
|
|
|
char *name;
|
|
|
|
char *value;
|
2012-09-20 07:51:34 +04:00
|
|
|
char *lvalue;
|
|
|
|
int len;
|
|
|
|
int file_size;
|
2014-07-19 22:56:00 +04:00
|
|
|
|
2014-06-19 08:18:47 +04:00
|
|
|
data = (char *) g_malloc(FILE_MAX_LINE_BYTES * 3, 0);
|
|
|
|
text = data;
|
|
|
|
name = text + FILE_MAX_LINE_BYTES;
|
|
|
|
value = name + FILE_MAX_LINE_BYTES;
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
file_size = 32 * 1024; /* 32 K file size limit */
|
|
|
|
g_file_seek(fd, 0);
|
2014-06-19 08:18:47 +04:00
|
|
|
g_memset(text, 0, FILE_MAX_LINE_BYTES);
|
2012-09-20 07:51:34 +04:00
|
|
|
list_clear(names);
|
|
|
|
list_clear(values);
|
|
|
|
make_stream(s);
|
|
|
|
init_stream(s, file_size);
|
|
|
|
len = g_file_read(fd, s->data, file_size);
|
|
|
|
|
|
|
|
if (len > 0)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
s->end = s->p + len;
|
2016-02-10 05:29:52 +03:00
|
|
|
while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
|
|
|
|
{
|
|
|
|
if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
|
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
if (g_strcasecmp(section, text) == 0)
|
2012-03-17 02:38:13 +04:00
|
|
|
{
|
2016-02-10 05:29:52 +03:00
|
|
|
while (file_read_ini_line(s, text,
|
|
|
|
FILE_MAX_LINE_BYTES) == 0)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2016-02-10 05:29:52 +03:00
|
|
|
if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
|
|
|
|
{
|
|
|
|
break;
|
2015-08-20 00:02:50 +03:00
|
|
|
}
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (g_strlen(text) > 0)
|
|
|
|
{
|
|
|
|
file_split_name_value(text, name, value);
|
2023-03-06 16:54:22 +03:00
|
|
|
list_add_strdup(names, name);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (value[0] == '$')
|
|
|
|
{
|
|
|
|
lvalue = g_getenv(value + 1);
|
|
|
|
|
|
|
|
if (lvalue != 0)
|
|
|
|
{
|
2023-03-06 16:54:22 +03:00
|
|
|
list_add_strdup(values, lvalue);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-06 16:54:22 +03:00
|
|
|
list_add_strdup(values, "");
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-06 16:54:22 +03:00
|
|
|
list_add_strdup(values, value);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_stream(s);
|
2014-06-19 08:18:47 +04:00
|
|
|
g_free(data);
|
2012-09-20 07:51:34 +04:00
|
|
|
return 0;
|
2012-03-17 02:38:13 +04:00
|
|
|
}
|
2006-07-21 07:27:03 +04:00
|
|
|
}
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 00:02:50 +03:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
free_stream(s);
|
2014-06-19 08:18:47 +04:00
|
|
|
g_free(data);
|
2012-09-20 07:51:34 +04:00
|
|
|
return 1;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2007-10-28 08:49:08 +03:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* returns error
|
|
|
|
returns 0 if everything is ok
|
|
|
|
returns 1 if problem reading file */
|
|
|
|
/* 32 K file size limit */
|
2017-03-12 19:35:00 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
file_read_sections(int fd, struct list *names)
|
2007-10-28 08:49:08 +03:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
return l_file_read_sections(fd, 32 * 1024, names);
|
2007-10-28 08:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* return error */
|
2016-01-14 19:32:09 +03:00
|
|
|
/* this function should be preferred over file_read_sections because it can
|
2007-10-28 08:49:08 +03:00
|
|
|
read any file size */
|
2017-03-12 19:35:00 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
file_by_name_read_sections(const char *file_name, struct list *names)
|
2007-10-28 08:49:08 +03:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int fd;
|
|
|
|
int file_size;
|
|
|
|
int rv;
|
2007-10-28 08:49:08 +03:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
file_size = g_file_get_size(file_name);
|
|
|
|
|
|
|
|
if (file_size < 1)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
Split g_file_open() into _ro() and _rw() variants
Rename g_file_open() to g_file_open_rw(), and add a new g_file_open_ro()
call that wraps the common g_file_open_ex(file, 1, 0, 0, 0) idiom. This
will make the file access mode more explicit in the code.
Change all calls to g_file_open() to the _ro() or _rw() variant as
appropriate, and replace g_file_open_ex(file, 1, 0, 0, 0) with the _ro()
call.
Lastly, add tests for the two new calls to test_os_calls.c (code
courteously provided by matt335672).
2023-05-13 08:54:32 +03:00
|
|
|
fd = g_file_open_ro(file_name);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2014-07-19 22:56:00 +04:00
|
|
|
if (fd < 0)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = l_file_read_sections(fd, file_size, names);
|
|
|
|
g_file_close(fd);
|
|
|
|
return rv;
|
2007-10-28 08:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* return error */
|
|
|
|
/* 32 K file size limit */
|
2017-03-12 19:35:00 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
file_read_section(int fd, const char *section,
|
|
|
|
struct list *names, struct list *values)
|
2007-10-28 08:49:08 +03:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
return l_file_read_section(fd, 32 * 1024, section, names, values);
|
2007-10-28 08:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* return error */
|
2016-01-14 19:32:09 +03:00
|
|
|
/* this function should be preferred over file_read_section because it can
|
2007-10-28 08:49:08 +03:00
|
|
|
read any file size */
|
2017-03-12 19:35:00 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
file_by_name_read_section(const char *file_name, const char *section,
|
|
|
|
struct list *names, struct list *values)
|
2007-10-28 08:49:08 +03:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int fd;
|
|
|
|
int file_size;
|
|
|
|
int rv;
|
2007-10-28 08:49:08 +03:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
file_size = g_file_get_size(file_name);
|
|
|
|
|
|
|
|
if (file_size < 1)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
Split g_file_open() into _ro() and _rw() variants
Rename g_file_open() to g_file_open_rw(), and add a new g_file_open_ro()
call that wraps the common g_file_open_ex(file, 1, 0, 0, 0) idiom. This
will make the file access mode more explicit in the code.
Change all calls to g_file_open() to the _ro() or _rw() variant as
appropriate, and replace g_file_open_ex(file, 1, 0, 0, 0) with the _ro()
call.
Lastly, add tests for the two new calls to test_os_calls.c (code
courteously provided by matt335672).
2023-05-13 08:54:32 +03:00
|
|
|
fd = g_file_open_ro(file_name);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2014-07-19 22:56:00 +04:00
|
|
|
if (fd < 0)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = l_file_read_section(fd, file_size, section, names, values);
|
|
|
|
g_file_close(fd);
|
|
|
|
return rv;
|
2007-10-28 08:49:08 +03:00
|
|
|
}
|