Codebase list xrdp / run/04343fb2-6aee-41d1-9c13-37faba2b665d/main common / file.c
run/04343fb2-6aee-41d1-9c13-37faba2b665d/main

Tree @run/04343fb2-6aee-41d1-9c13-37faba2b665d/main (Download .tar.gz)

file.c @run/04343fb2-6aee-41d1-9c13-37faba2b665d/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2004-2014
 *
 * 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
 */

#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif

#include "arch.h"
#include "os_calls.h"
#include "string_calls.h"
#include "list.h"
#include "file.h"
#include "parse.h"

#define FILE_MAX_LINE_BYTES 2048

static int
file_read_ini_line(struct stream *s, char *text, int text_bytes);

/*****************************************************************************/
/* 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. */
static int
line_lookup_for_section_name(char *str, int str_bytes)
{
    int name_index_start;
    int index;
    char c;

    name_index_start = -1;
    index = 0;
    while ((c = str[index]) != 0)
    {
        if (c == '[')
        {
            name_index_start = index + 1;
        }
        else if (c == ']' && name_index_start > 0)
        {
            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;
            return 1;
        }
        ++index;
    }
    return 0;
}


/*****************************************************************************/
/* returns error
   returns 0 if everything is ok
   returns 1 if problem reading file */
static int
l_file_read_sections(int fd, int max_file_size, struct list *names)
{
    struct stream *s;
    char text[FILE_MAX_LINE_BYTES];
    int len;
    int rv;

    rv = 0;
    g_file_seek(fd, 0);
    g_memset(text, 0, FILE_MAX_LINE_BYTES);
    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)
    {
        s->end = s->p + len;
        while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
        {
            if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
            {
                list_add_item(names, (tbus)g_strdup(text));
            }
        }
    }
    else if (len < 0)
    {
        rv = 1;
    }

    free_stream(s);
    return rv;
}

/*****************************************************************************/
/* Read a line in the stream 's', removing comments.
 * returns error
 * returns 0 if everything is ok
 * returns 1 if problem reading file */
static int
file_read_ini_line(struct stream *s, char *text, int text_bytes)
{
    int i;
    int skip_to_end;
    int at_end;
    char c;

    skip_to_end = 0;

    if (!s_check_rem(s, 1))
    {
        return 1;
    }

    i = 0;
    in_uint8(s, c);

    while (c != 10 && c != 13)
    {
        /* these mean skip the rest of the line */
        if (c == '#' || c == ';')
        {
            skip_to_end = 1;
        }

        if (!skip_to_end)
        {
            text[i] = c;
            i++;
            if (i >= text_bytes)
            {
                return 1;
            }
        }

        if (s_check_rem(s, 1))
        {
            in_uint8(s, c);
        }
        else
        {
            c = 0;
            break;
        }
    }

    if (c == 10 || c == 13)
    {
        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--;
        }
    }

    text[i] = 0;

    return 0;
}


/*****************************************************************************/
/* returns error */
static int
file_split_name_value(char *text, char *name, char *value)
{
    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++)
    {
        if (text[i] == '=' && !on_to)
        {
            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;
        }
    }

    g_strtrim(name, 3); /* trim both right and left */
    g_strtrim(value, 3); /* trim both right and left */
    return 0;
}

/*****************************************************************************/
/* return error */
static int
l_file_read_section(int fd, int max_file_size, const char *section,
                    struct list *names, struct list *values)
{
    struct stream *s;
    char *data;
    char *text;
    char *name;
    char *value;
    char *lvalue;
    int len;
    int file_size;

    data = (char *) g_malloc(FILE_MAX_LINE_BYTES * 3, 0);
    text = data;
    name = text + FILE_MAX_LINE_BYTES;
    value = name + FILE_MAX_LINE_BYTES;

    file_size = 32 * 1024; /* 32 K file size limit */
    g_file_seek(fd, 0);
    g_memset(text, 0, FILE_MAX_LINE_BYTES);
    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)
    {
        s->end = s->p + len;
        while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
        {
            if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
            {
                if (g_strcasecmp(section, text) == 0)
                {
                    while (file_read_ini_line(s, text,
                                              FILE_MAX_LINE_BYTES) == 0)
                    {
                        if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
                        {
                            break;
                        }

                        if (g_strlen(text) > 0)
                        {
                            file_split_name_value(text, name, value);
                            list_add_item(names, (tbus)g_strdup(name));

                            if (value[0] == '$')
                            {
                                lvalue = g_getenv(value + 1);

                                if (lvalue != 0)
                                {
                                    list_add_item(values, (tbus)g_strdup(lvalue));
                                }
                                else
                                {
                                    list_add_item(values, (tbus)g_strdup(""));
                                }
                            }
                            else
                            {
                                list_add_item(values, (tbus)g_strdup(value));
                            }
                        }
                    }
                    free_stream(s);
                    g_free(data);
                    return 0;
                }
            }
        }
    }

    free_stream(s);
    g_free(data);
    return 1;
}

/*****************************************************************************/
/* returns error
   returns 0 if everything is ok
   returns 1 if problem reading file */
/* 32 K file size limit */
int
file_read_sections(int fd, struct list *names)
{
    return l_file_read_sections(fd, 32 * 1024, names);
}

/*****************************************************************************/
/* return error */
/* this function should be preferred over file_read_sections because it can
   read any file size */
int
file_by_name_read_sections(const char *file_name, struct list *names)
{
    int fd;
    int file_size;
    int rv;

    file_size = g_file_get_size(file_name);

    if (file_size < 1)
    {
        return 1;
    }

    fd = g_file_open_ex(file_name, 1, 0, 0, 0);

    if (fd < 0)
    {
        return 1;
    }

    rv = l_file_read_sections(fd, file_size, names);
    g_file_close(fd);
    return rv;
}

/*****************************************************************************/
/* return error */
/* 32 K file size limit */
int
file_read_section(int fd, const char *section,
                  struct list *names, struct list *values)
{
    return l_file_read_section(fd, 32 * 1024, section, names, values);
}

/*****************************************************************************/
/* return error */
/* this function should be preferred over file_read_section because it can
   read any file size */
int
file_by_name_read_section(const char *file_name, const char *section,
                          struct list *names, struct list *values)
{
    int fd;
    int file_size;
    int rv;

    file_size = g_file_get_size(file_name);

    if (file_size < 1)
    {
        return 1;
    }

    fd = g_file_open_ex(file_name, 1, 0, 0, 0);

    if (fd < 0)
    {
        return 1;
    }

    rv = l_file_read_section(fd, file_size, section, names, values);
    g_file_close(fd);
    return rv;
}