• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

proftpd / proftpd / 20382970665

19 Dec 2025 09:31PM UTC coverage: 92.996% (+0.001%) from 92.995%
20382970665

push

github

51434 of 55308 relevant lines covered (93.0%)

223.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.8
/src/parser.c
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 2004-2025 The ProFTPD Project team
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18
 *
19
 * As a special exemption, The ProFTPD Project team and other respective
20
 * copyright holders give permission to link this program with OpenSSL, and
21
 * distribute the resulting executable, without including the source code
22
 * for OpenSSL in the source distribution.
23
 */
24

25
/* Configuration parser */
26

27
#include "conf.h"
28
#include "privs.h"
29

30
/* Maximum depth of Include patterns/files. */
31
#define PR_PARSER_INCLUDE_MAX_DEPTH        64
32

33
extern xaset_t *server_list;
34
extern pool *global_config_pool;
35

36
static pool *parser_pool = NULL;
37
static unsigned long parser_include_opts = 0UL;
38

39
static array_header *parser_confstack = NULL;
40
static config_rec **parser_curr_config = NULL;
41

42
static array_header *parser_servstack = NULL;
43
static server_rec **parser_curr_server = NULL;
44
static unsigned int parser_sid = 1;
45

46
static xaset_t **parser_server_list = NULL;
47

48
static const char *trace_channel = "config";
49

50
struct config_src {
51
  struct config_src *cs_next;
52
  pool *cs_pool;
53
  pr_fh_t *cs_fh;
54
  unsigned int cs_lineno;
55
};
56

57
static unsigned int parser_curr_lineno = 0;
58

59
/* Note: the parser seems to be touchy about this particular value.  If
60
 * you see strange segfaults occurring in the mergedown() function, it
61
 * might be because this pool size is too small.
62
 */
63
#define PARSER_CONFIG_SRC_POOL_SZ        512
64

65
static struct config_src *parser_sources = NULL;
66

67
/* Private functions
68
 */
69

70
static struct config_src *add_config_source(pr_fh_t *fh) {
6✔
71
  pool *p;
6✔
72
  struct config_src *cs;
6✔
73

74
  p = pr_pool_create_sz(parser_pool, PARSER_CONFIG_SRC_POOL_SZ);
6✔
75
  pr_pool_tag(p, "configuration source pool");
6✔
76

77
  cs = pcalloc(p, sizeof(struct config_src));
6✔
78
  cs->cs_pool = p;
6✔
79
  cs->cs_next = NULL;
6✔
80
  cs->cs_fh = fh;
6✔
81
  cs->cs_lineno = 0;
6✔
82

83
  if (parser_sources == NULL) {
6✔
84
    parser_sources = cs;
6✔
85

86
  } else {
87
    cs->cs_next = parser_sources;
×
88
    parser_sources = cs;
×
89
  }
90

91
  return cs;
6✔
92
}
93

94
static char *get_config_word(pool *p, char *word, int *resolved_var) {
37✔
95
  size_t wordlen;
37✔
96

97
  /* Should this word be replaced with a value from the environment?
98
   * If so, tmp will contain the expanded value, otherwise tmp will
99
   * contain a string duped from the given pool.
100
   */
101

102
  wordlen = strlen(word);
37✔
103
  if (wordlen > 7) {
37✔
104
    char *ptr = NULL;
15✔
105

106
    pr_trace_msg(trace_channel, 27, "word '%s' long enough for environment "
15✔
107
      "variable (%lu > minimum required 7)", word, (unsigned long) wordlen);
108

109
    /* Does the given word use the environment syntax? We handle this in a
110
     * while loop in order to handle a) multiple different variables, and b)
111
     * cases where the substituted value is itself a variable.  Hopefully no
112
     * one is so clever as to want to actually _use_ the latter approach.
113
     */
114
    ptr = strstr(word, "%{env:");
15✔
115
    while (ptr != NULL) {
22✔
116
      char *env, *key, *ptr2, *var;
7✔
117
      unsigned int keylen;
7✔
118

119
      pr_signals_handle();
7✔
120

121
      ptr2 = strchr(ptr + 6, '}');
7✔
122
      if (ptr2 == NULL) {
7✔
123
        /* No terminating marker; continue on to the next potential
124
         * variable in the word.
125
         */
126
        ptr2 = ptr + 6;
1✔
127
        ptr = strstr(ptr2, "%{env:");
1✔
128
        continue;
1✔
129
      }
130

131
      keylen = (ptr2 - ptr - 6);
6✔
132
      var = pstrndup(p, ptr, (ptr2 - ptr) + 1);
6✔
133

134
      key = pstrndup(p, ptr + 6, keylen);
6✔
135

136
      pr_trace_msg(trace_channel, 17,
6✔
137
        "word '%s' uses environment variable '%s'", word, key);
138
      env = pr_env_get(p, key);
6✔
139
      if (env == NULL) {
6✔
140
        /* No value in the environment; continue on to the next potential
141
         * variable in the word.
142
         */
143
        pr_trace_msg(trace_channel, 17, "no value found for environment "
1✔
144
          "variable '%s' for word '%s', ignoring", key, word);
145

146
        word = (char *) sreplace(p, word, var, "", NULL);
1✔
147
        ptr = strstr(word, "%{env:");
1✔
148
        continue;
1✔
149
      }
150

151
      pr_trace_msg(trace_channel, 17,
5✔
152
        "resolved environment variable '%s' to value '%s' for word '%s'", key,
153
        env, word);
154

155
      word = (char *) sreplace(p, word, var, env, NULL);
5✔
156
      ptr = strstr(word, "%{env:");
5✔
157

158
      if (resolved_var != NULL) {
5✔
159
        *resolved_var = TRUE;
5✔
160
      }
161
    }
162

163
  } else {
164
    pr_trace_msg(trace_channel, 27, "word '%s' not long enough for environment "
22✔
165
      "variable (%lu < minimum required 7)", word, (unsigned long) wordlen);
166
  }
167

168
  return pstrdup(p, word);
37✔
169
}
170

171
static void remove_config_source(void) {
5✔
172
  struct config_src *cs = parser_sources;
5✔
173

174
  if (cs != NULL) {
5✔
175
    parser_sources = cs->cs_next;
5✔
176
    destroy_pool(cs->cs_pool);
5✔
177
  }
178
}
5✔
179

180
/* Public API
181
 */
182

183
int pr_parser_cleanup(void) {
60✔
184
  if (parser_pool != NULL) {
60✔
185
    if (parser_servstack->nelts > 1 ||
3✔
186
        (parser_curr_config && *parser_curr_config)) {
2✔
187
      errno = EPERM;
1✔
188
      return -1;
1✔
189
    }
190

191
    destroy_pool(parser_pool);
2✔
192
    parser_pool = NULL;
2✔
193
  }
194

195
  parser_servstack = NULL;
59✔
196
  parser_curr_server = NULL;
59✔
197

198
  parser_confstack = NULL;
59✔
199
  parser_curr_config = NULL;
59✔
200

201
  /* Reset the SID counter. */
202
  parser_sid = 1;
59✔
203

204
  return 0;
59✔
205
}
206

207
config_rec *pr_parser_config_ctxt_close(int *empty) {
3✔
208
  config_rec *c = *parser_curr_config;
3✔
209

210
  /* Note that if the current config is empty, it should simply be removed.
211
   * Such empty configs can happen for <Directory> sections that
212
   * contain no directives, for example.
213
   */
214

215
  if (parser_curr_config == (config_rec **) parser_confstack->elts) {
3✔
216
    if (c != NULL &&
3✔
217
        (!c->subset || !c->subset->xas_list)) {
3✔
218
      xaset_remove(c->set, (xasetmember_t *) c);
3✔
219
      destroy_pool(c->pool);
3✔
220

221
      if (empty) {
3✔
222
        *empty = TRUE;
2✔
223
      }
224
    }
225

226
    if (*parser_curr_config) {
3✔
227
      *parser_curr_config = NULL;
3✔
228
    }
229

230
    return NULL;
3✔
231
  }
232

233
  if (c != NULL &&
×
234
      (!c->subset || !c->subset->xas_list)) {
×
235
    xaset_remove(c->set, (xasetmember_t *) c);
×
236
    destroy_pool(c->pool);
×
237

238
    if (empty) {
×
239
      *empty = TRUE;
×
240
    }
241
  }
242

243
  parser_curr_config--;
×
244
  parser_confstack->nelts--;
×
245

246
  return *parser_curr_config;
×
247
}
248

249
config_rec *pr_parser_config_ctxt_get(void) {
9✔
250
  if (parser_curr_config) {
9✔
251
    return *parser_curr_config;
8✔
252
  }
253

254
  errno = ENOENT;
1✔
255
  return NULL;
1✔
256
}
257

258
config_rec *pr_parser_config_ctxt_open(const char *name) {
3✔
259
  config_rec *c = NULL, *parent = *parser_curr_config;
3✔
260
  pool *c_pool = NULL, *parent_pool = NULL;
3✔
261
  xaset_t **set = NULL;
3✔
262

263
  if (name == NULL) {
3✔
264
    errno = EINVAL;
1✔
265
    return NULL;
1✔
266
  }
267

268
  if (parent != NULL) {
2✔
269
    parent_pool = parent->pool;
×
270
    set = &parent->subset;
×
271

272
  } else {
273
    parent_pool = (*parser_curr_server)->pool;
2✔
274
    set = &(*parser_curr_server)->conf;
2✔
275
  }
276

277
  /* Allocate a sub-pool for this config_rec.
278
   *
279
   * Note: special exception for <Global> configs: the parent pool is
280
   * 'global_config_pool' (a pool just for that context), not the pool of the
281
   * parent server.  This keeps <Global> config recs from being freed
282
   * prematurely, and helps to avoid memory leaks.
283
   */
284
  if (strcasecmp(name, "<Global>") == 0) {
2✔
285
    if (global_config_pool == NULL) {
1✔
286
      global_config_pool = make_sub_pool(permanent_pool);
1✔
287
      pr_pool_tag(global_config_pool, "<Global> Pool");
1✔
288
    }
289

290
    parent_pool = global_config_pool;
1✔
291
  }
292

293
  c_pool = make_sub_pool(parent_pool);
2✔
294
  pr_pool_tag(c_pool, "sub-config pool");
2✔
295

296
  c = (config_rec *) pcalloc(c_pool, sizeof(config_rec));
2✔
297

298
  if (!*set) {
2✔
299
    pool *set_pool = make_sub_pool(parent_pool);
1✔
300
    *set = xaset_create(set_pool, NULL);
1✔
301
    (*set)->pool = set_pool;
1✔
302
  }
303

304
  xaset_insert(*set, (xasetmember_t *) c);
2✔
305

306
  c->pool = c_pool;
2✔
307
  c->set = *set;
2✔
308
  c->parent = parent;
2✔
309
  c->name = pstrdup(c->pool, name);
2✔
310

311
  if (parent != NULL) {
2✔
312
    if (parent->config_type == CONF_DYNDIR) {
×
313
      c->flags |= CF_DYNAMIC;
×
314
    }
315
  }
316

317
  (void) pr_parser_config_ctxt_push(c);
2✔
318
  return c;
2✔
319
}
320

321
int pr_parser_config_ctxt_push(config_rec *c) {
5✔
322
  if (c == NULL) {
5✔
323
    errno = EINVAL;
1✔
324
    return -1;
1✔
325
  }
326

327
  if (parser_confstack == NULL) {
4✔
328
    errno = EPERM;
1✔
329
    return -1;
1✔
330
  }
331

332
  if (!*parser_curr_config) {
3✔
333
    *parser_curr_config = c;
3✔
334

335
  } else {
336
    parser_curr_config = (config_rec **) push_array(parser_confstack);
×
337
    *parser_curr_config = c;
×
338
  }
339

340
  return 0;
341
}
342

343
unsigned int pr_parser_get_lineno(void) {
11✔
344
  return parser_curr_lineno;
11✔
345
}
346

347
/* Return an array of all supported/known configuration directives. */
348
static array_header *get_all_directives(pool *p) {
1✔
349
  array_header *names;
1✔
350
  conftable *tab;
1✔
351
  int idx;
1✔
352
  unsigned int hash;
1✔
353

354
  names = make_array(p, 1, sizeof(const char *));
1✔
355

356
  idx = -1;
1✔
357
  hash = 0;
1✔
358
  tab = pr_stash_get_symbol2(PR_SYM_CONF, NULL, NULL, &idx, &hash);
1✔
359
  while (idx != -1) {
43✔
360
    pr_signals_handle();
42✔
361

362
    if (tab != NULL) {
42✔
363
      *((const char **) push_array(names)) = pstrdup(p, tab->directive);
2✔
364

365
    } else {
366
      idx++;
40✔
367
    }
368

369
    tab = pr_stash_get_symbol2(PR_SYM_CONF, NULL, tab, &idx, &hash);
42✔
370
  }
371

372
  return names;
1✔
373
}
374

375
int pr_parser_parse_file(pool *p, const char *path, config_rec *start,
10✔
376
    int flags) {
377
  pr_fh_t *fh;
10✔
378
  struct stat st;
10✔
379
  struct config_src *cs;
10✔
380
  cmd_rec *cmd;
10✔
381
  pool *tmp_pool;
10✔
382
  char *buf, *report_path;
10✔
383
  size_t bufsz;
10✔
384

385
  if (path == NULL) {
10✔
386
    errno = EINVAL;
1✔
387
    return -1;
1✔
388
  }
389

390
  if (parser_servstack == NULL) {
9✔
391
    errno = EPERM;
1✔
392
    return -1;
1✔
393
  }
394

395
  tmp_pool = make_sub_pool(p ? p : permanent_pool);
8✔
396
  pr_pool_tag(tmp_pool, "parser file pool");
8✔
397

398
  report_path = (char *) path;
8✔
399
  if (session.chroot_path) {
8✔
400
    report_path = pdircat(tmp_pool, session.chroot_path, path, NULL);
×
401
  }
402

403
  if (!(flags & PR_PARSER_FL_DYNAMIC_CONFIG)) {
8✔
404
    pr_trace_msg(trace_channel, 3, "parsing '%s' configuration", report_path);
7✔
405
  }
406

407
  fh = pr_fsio_open(path, O_RDONLY);
8✔
408
  if (fh == NULL) {
8✔
409
    int xerrno = errno;
1✔
410

411
    destroy_pool(tmp_pool);
1✔
412

413
    errno = xerrno;
1✔
414
    return -1;
1✔
415
  }
416

417
  /* Stat the opened file to determine the optimal buffer size for IO. */
418
  memset(&st, 0, sizeof(st));
7✔
419
  if (pr_fsio_fstat(fh, &st) < 0) {
7✔
420
    int xerrno = errno;
×
421

422
    pr_fsio_close(fh);
×
423
    destroy_pool(tmp_pool);
×
424

425
    errno = xerrno;
×
426
    return -1;
×
427
  }
428

429
  if (S_ISDIR(st.st_mode)) {
7✔
430
    pr_fsio_close(fh);
1✔
431
    destroy_pool(tmp_pool);
1✔
432

433
    errno = EISDIR;
1✔
434
    return -1;
1✔
435
  }
436

437
  /* Advise the platform that we will be only reading this file
438
   * sequentially.
439
   */
440
  pr_fs_fadvise(PR_FH_FD(fh), 0, 0, PR_FS_FADVISE_SEQUENTIAL);
6✔
441

442
  /* Check for world-writable files (and later, files in world-writable
443
   * directories).
444
   *
445
   * For now, just warn about these; later, we will be more draconian.
446
   */
447
  if (st.st_mode & S_IWOTH) {
6✔
448
    pr_log_pri(PR_LOG_WARNING, "warning: config file '%s' is world-writable",
×
449
     path);
450
  }
451

452
  fh->fh_iosz = st.st_blksize;
6✔
453

454
  /* Push the configuration information onto the stack of configuration
455
   * sources.
456
   */
457
  cs = add_config_source(fh);
6✔
458

459
  if (start != NULL) {
6✔
460
    (void) pr_parser_config_ctxt_push(start);
×
461
  }
462

463
  bufsz = PR_TUNABLE_PARSER_BUFFER_SIZE;
6✔
464
  buf = pcalloc(tmp_pool, bufsz + 1);
6✔
465

466
  while (pr_parser_read_line(buf, bufsz) != NULL) {
21✔
467
    pool *parsed_pool;
10✔
468
    pr_parsed_line_t *parsed_line;
10✔
469

470
    pr_signals_handle();
10✔
471

472
    /* Note that pr_parser_parse_line modifies the contents of the buffer,
473
     * so we want to make copy beforehand.
474
     */
475

476
    parsed_pool = make_sub_pool(tmp_pool);
10✔
477
    parsed_line = pcalloc(parsed_pool, sizeof(pr_parsed_line_t));
10✔
478
    parsed_line->text = pstrdup(parsed_pool, buf);
10✔
479
    parsed_line->source_file = report_path;
10✔
480
    parsed_line->source_lineno = cs->cs_lineno;
10✔
481

482
    cmd = pr_parser_parse_line(tmp_pool, buf, 0);
10✔
483
    if (cmd == NULL) {
10✔
484
      destroy_pool(parsed_pool);
×
485
      continue;
×
486
    }
487

488
    /* Generate an event about the parsed line of text, for any interested
489
     * parties.
490
     */
491
    parsed_line->cmd = cmd;
10✔
492
    pr_event_generate("core.parsed-line", parsed_line);
10✔
493
    destroy_pool(parsed_pool);
10✔
494

495
    if (cmd->argc) {
10✔
496
      conftable *conftab;
10✔
497
      char found = FALSE;
10✔
498

499
      cmd->server = *parser_curr_server;
10✔
500
      cmd->config = *parser_curr_config;
10✔
501

502
      conftab = pr_stash_get_symbol2(PR_SYM_CONF, cmd->argv[0], NULL,
10✔
503
        &cmd->stash_index, &cmd->stash_hash);
504
      while (conftab != NULL) {
18✔
505
        modret_t *mr;
8✔
506

507
        pr_signals_handle();
8✔
508

509
        cmd->argv[0] = conftab->directive;
8✔
510

511
        pr_trace_msg(trace_channel, 7,
8✔
512
          "dispatching directive '%s' to module mod_%s", conftab->directive,
513
          conftab->m->name);
8✔
514

515
        mr = pr_module_call(conftab->m, conftab->handler, cmd);
8✔
516
        if (mr != NULL) {
8✔
517
          if (MODRET_ISERROR(mr)) {
8✔
518
            if (!(flags & PR_PARSER_FL_DYNAMIC_CONFIG)) {
×
519
              pr_log_pri(PR_LOG_WARNING, "fatal: %s on line %u of '%s'",
×
520
                MODRET_ERRMSG(mr), cs->cs_lineno, report_path);
521
              destroy_pool(tmp_pool);
×
522
              errno = EPERM;
×
523
              return -1;
×
524
            }
525

526
            pr_log_pri(PR_LOG_WARNING, "warning: %s on line %u of '%s'",
×
527
              MODRET_ERRMSG(mr), cs->cs_lineno, report_path);
528
          }
529
        }
530

531
        if (!MODRET_ISDECLINED(mr)) {
8✔
532
          found = TRUE;
8✔
533
        }
534

535
        conftab = pr_stash_get_symbol2(PR_SYM_CONF, cmd->argv[0], conftab,
8✔
536
          &cmd->stash_index, &cmd->stash_hash);
537
      }
538

539
      if (cmd->tmp_pool != NULL) {
10✔
540
        destroy_pool(cmd->tmp_pool);
8✔
541
        cmd->tmp_pool = NULL;
8✔
542
      }
543

544
      if (found == FALSE) {
10✔
545
        register unsigned int i;
2✔
546
        char *name;
2✔
547
        size_t namelen;
2✔
548
        int non_ascii = FALSE;
2✔
549

550
        /* I encountered a case where a particular configuration file had
551
         * what APPEARED to be a valid directive, but the parser kept reporting
552
         * that the directive was unknown.  I now suspect that the file in
553
         * question had embedded UTF8 characters (spaces, perhaps), which
554
         * would appear as normal spaces in e.g. UTF8-aware editors/terminals,
555
         * but which the parser would rightly refuse.
556
         *
557
         * So to indicate that this might be the case, check for any non-ASCII
558
         * characters in the "unknown" directive name, and if found, log
559
         * about them.
560
         */
561

562
        name = cmd->argv[0];
2✔
563
        namelen = strlen(name);
2✔
564

565
        for (i = 0; i < namelen; i++) {
16✔
566
          if (!isascii((int) name[i])) {
14✔
567
            non_ascii = TRUE;
568
            break;
569
          }
570
        }
571

572
        if (!(flags & PR_PARSER_FL_DYNAMIC_CONFIG)) {
2✔
573
          pr_log_pri(PR_LOG_WARNING, "fatal: unknown configuration directive "
1✔
574
            "'%s' on line %u of '%s'", name, cs->cs_lineno, report_path);
575
          if (non_ascii) {
1✔
576
            pr_log_pri(PR_LOG_WARNING, "fatal: malformed directive name "
×
577
              "'%s' (contains non-ASCII characters)", name);
578

579
          } else {
580
            array_header *directives, *similars;
1✔
581

582
            directives = get_all_directives(tmp_pool);
1✔
583
            similars = pr_str_get_similars(tmp_pool, name, directives, 0,
1✔
584
              PR_STR_FL_IGNORE_CASE);
585
            if (similars != NULL &&
1✔
586
                similars->nelts > 0) {
1✔
587
              unsigned int nelts;
×
588
              const char **names, *msg;
×
589

590
              names = similars->elts;
×
591
              nelts = similars->nelts;
×
592
              if (nelts > 4) {
×
593
                nelts = 4;
594
              }
595

596
              msg = "fatal: Did you mean:";
×
597

598
              if (nelts == 1) {
×
599
                msg = pstrcat(tmp_pool, msg, " ", names[0], NULL);
×
600

601
              } else {
602
                for (i = 0; i < nelts; i++) {
×
603
                  msg = pstrcat(tmp_pool, msg, "\n  ", names[i], NULL);
×
604
                }
605
              }
606

607
              pr_log_pri(PR_LOG_WARNING, "%s", msg);
×
608
            }
609
          }
610

611
          destroy_pool(tmp_pool);
1✔
612
          errno = EPERM;
1✔
613
          return -1;
1✔
614
        }
615

616
        pr_log_pri(PR_LOG_WARNING, "warning: unknown configuration directive "
1✔
617
          "'%s' on line %u of '%s'", name, cs->cs_lineno, report_path);
618
        if (non_ascii) {
1✔
619
          pr_log_pri(PR_LOG_WARNING, "warning: malformed directive name "
×
620
            "'%s' (contains non-ASCII characters)", name);
621
        }
622
      }
623
    }
624

625
    destroy_pool(cmd->pool);
9✔
626
    memset(buf, '\0', bufsz);
24✔
627
  }
628

629
  /* Pop this configuration stream from the stack. */
630
  remove_config_source();
5✔
631

632
  pr_fsio_close(fh);
5✔
633

634
  destroy_pool(tmp_pool);
5✔
635
  return 0;
5✔
636
}
637

638
cmd_rec *pr_parser_parse_line(pool *p, const char *text, size_t text_len) {
22✔
639
  register unsigned int i;
22✔
640
  char *arg = "", *ptr, *word = NULL;
22✔
641
  cmd_rec *cmd = NULL;
22✔
642
  pool *sub_pool = NULL;
22✔
643
  array_header *arr = NULL;
22✔
644

645
  if (p == NULL ||
22✔
646
      text == NULL) {
22✔
647
    errno = EINVAL;
2✔
648
    return NULL;
2✔
649
  }
650

651
  if (text_len == 0) {
20✔
652
    text_len = strlen(text);
20✔
653
  }
654

655
  if (text_len == 0) {
20✔
656
    errno = ENOENT;
1✔
657
    return NULL;
1✔
658
  }
659

660
  ptr = (char *) text;
19✔
661

662
  /* Build a new pool for the command structure and array */
663
  sub_pool = make_sub_pool(p);
19✔
664
  pr_pool_tag(sub_pool, "parser cmd subpool");
19✔
665

666
  cmd = pcalloc(sub_pool, sizeof(cmd_rec));
19✔
667
  cmd->pool = sub_pool;
19✔
668
  cmd->stash_index = -1;
19✔
669
  cmd->stash_hash = 0;
19✔
670

671
  /* Add each word to the array */
672
  arr = make_array(cmd->pool, 4, sizeof(char **));
19✔
673
  while ((word = pr_str_get_word(&ptr, 0)) != NULL) {
56✔
674
    char *ptr2;
37✔
675
    int resolved_var = FALSE;
37✔
676

677
    pr_signals_handle();
37✔
678
    ptr2 = get_config_word(cmd->pool, word, &resolved_var);
37✔
679

680
    /* What if the retrieved word is a resolved variable, which itself
681
     * separated by whitespace, as from an environment variable whose value
682
     * contains multiple words (Issue #2002)?
683
     */
684
    if (resolved_var == TRUE &&
37✔
685
        strchr(ptr2, ' ') != NULL) {
4✔
686
      char *word2 = NULL;
4✔
687

688
      while ((word2 = pr_str_get_word(&ptr2, 0)) != NULL) {
4✔
689
        pr_signals_handle();
3✔
690

691
        *((char **) push_array(arr)) = pstrdup(cmd->pool, word2);
3✔
692
        cmd->argc++;
3✔
693
      }
694

695
    } else {
696
      *((char **) push_array(arr)) = ptr2;
36✔
697
      cmd->argc++;
36✔
698
    }
699
  }
700

701
  /* Terminate the array with a NULL. */
702
  *((char **) push_array(arr)) = NULL;
19✔
703

704
  /* The array header's job is done, we can forget about it and
705
   * it will get purged when the command's pool is destroyed.
706
   */
707

708
  cmd->argv = (void **) arr->elts;
19✔
709

710
  /* Perform a fixup on configuration directives so that:
711
   *
712
   *   -argv[0]--  -argv[1]-- ----argv[2]-----
713
   *   <Option     /etc/adir  /etc/anotherdir>
714
   *
715
   *  becomes:
716
   *
717
   *   -argv[0]--  -argv[1]-  ----argv[2]----
718
   *   <Option>    /etc/adir  /etc/anotherdir
719
   */
720

721
  if (cmd->argc &&
19✔
722
      *((char *) cmd->argv[0]) == '<') {
19✔
723
    char *cp;
1✔
724
    size_t cp_len;
1✔
725

726
    cp = cmd->argv[cmd->argc-1];
1✔
727
    cp_len = strlen(cp);
1✔
728

729
    if (*(cp + cp_len-1) == '>' &&
1✔
730
        cmd->argc > 1) {
731

732
      if (strcmp(cp, ">") == 0) {
1✔
733
        cmd->argv[cmd->argc-1] = NULL;
×
734
        cmd->argc--;
×
735

736
      } else {
737
        *(cp + cp_len-1) = '\0';
1✔
738
      }
739

740
      cp = cmd->argv[0];
1✔
741
      cp_len = strlen(cp);
1✔
742
      if (*(cp + cp_len-1) != '>') {
1✔
743
        cmd->argv[0] = pstrcat(cmd->pool, cp, ">", NULL);
1✔
744
      }
745
    }
746
  }
747

748
  if (cmd->argc < 2) {
19✔
749
    arg = pstrdup(cmd->pool, arg);
2✔
750
  }
751

752
  for (i = 1; i < cmd->argc; i++) {
39✔
753
    arg = pstrcat(cmd->pool, arg, *arg ? " " : "", cmd->argv[i], NULL);
37✔
754
  }
755

756
  cmd->arg = arg;
19✔
757
  return cmd;
19✔
758
}
759

760
int pr_parser_prepare(pool *p, xaset_t **parsed_servers) {
48✔
761

762
  if (p == NULL) {
48✔
763
    if (parser_pool == NULL) {
3✔
764
      parser_pool = make_sub_pool(permanent_pool);
2✔
765
      pr_pool_tag(parser_pool, "Parser Pool");
2✔
766
    }
767

768
    p = parser_pool;
3✔
769
  }
770

771
  if (parsed_servers == NULL) {
48✔
772
    parser_server_list = &server_list;
47✔
773

774
  } else {
775
    parser_server_list = parsed_servers;
1✔
776
  }
777

778
  parser_servstack = make_array(p, 1, sizeof(server_rec *));
48✔
779
  parser_curr_server = (server_rec **) push_array(parser_servstack);
48✔
780
  *parser_curr_server = main_server;
48✔
781

782
  parser_confstack = make_array(p, 10, sizeof(config_rec *));
48✔
783
  parser_curr_config = (config_rec **) push_array(parser_confstack);
48✔
784
  *parser_curr_config = NULL;
48✔
785

786
  return 0;
48✔
787
}
788

789
/* This functions returns the next line from the configuration stream,
790
 * skipping commented-out lines and trimming trailing and leading whitespace,
791
 * returning, in effect, the next line of configuration data on which to
792
 * act.  This function has the advantage that it can be called by functions
793
 * that don't have access to configuration file handle, such as the
794
 * <IfDefine> and <IfModule> configuration handlers.
795
 */
796
char *pr_parser_read_line(char *buf, size_t bufsz) {
16✔
797
  struct config_src *cs;
16✔
798

799
  /* Always use the config stream at the top of the stack. */
800
  cs = parser_sources;
16✔
801

802
  if (buf == NULL ||
16✔
803
      cs == NULL) {
16✔
804
    errno = EINVAL;
1✔
805
    return NULL;
1✔
806
  }
807

808
  if (cs->cs_fh == NULL) {
15✔
809
    errno = EPERM;
×
810
    return NULL;
×
811
  }
812

813
  parser_curr_lineno = cs->cs_lineno;
15✔
814

815
  /* Check for error conditions. */
816

817
  while ((pr_fsio_getline(buf, bufsz, cs->cs_fh, &(cs->cs_lineno))) != NULL) {
15✔
818
    int have_eol = FALSE;
10✔
819
    char *bufp = NULL;
10✔
820
    size_t buflen;
10✔
821

822
    pr_signals_handle();
10✔
823

824
    buflen = strlen(buf);
10✔
825
    parser_curr_lineno = cs->cs_lineno;
10✔
826

827
    /* Trim off the trailing newline, if present. */
828
    if (buflen &&
10✔
829
        buf[buflen - 1] == '\n') {
10✔
830
      have_eol = TRUE;
10✔
831
      buf[buflen-1] = '\0';
10✔
832
      buflen--;
10✔
833
    }
834

835
    if (buflen &&
10✔
836
        buf[buflen - 1] == '\r') {
10✔
837
      buf[buflen-1] = '\0';
6✔
838
      buflen--;
6✔
839
    }
840

841
    if (have_eol == FALSE) {
10✔
842
      pr_log_pri(PR_LOG_WARNING,
×
843
        "warning: handling possibly truncated configuration data at "
844
        "line %u of '%s'", cs->cs_lineno, cs->cs_fh->fh_path);
×
845
    }
846

847
    /* Advance past any leading whitespace. */
848
    for (bufp = buf; *bufp && PR_ISSPACE(*bufp); bufp++) {
10✔
849
    }
×
850

851
    /* Check for commented or blank lines at this point, and just continue on
852
     * to the next configuration line if found.  If not, return the
853
     * configuration line.
854
     */
855
    if (*bufp == '#' || !*bufp) {
10✔
856
      continue;
×
857
    }
858

859
    /* Copy the value of bufp back into the pointer passed in
860
     * and return it.
861
     */
862
    buf = bufp;
16✔
863

864
    return buf;
865
  }
866

867
  return NULL;
868
}
869

870
server_rec *pr_parser_server_ctxt_close(void) {
8✔
871
  if (!parser_curr_server) {
8✔
872
    errno = ENOENT;
×
873
    return NULL;
×
874
  }
875

876
  /* Disallow underflows. */
877
  if (parser_curr_server == (server_rec **) parser_servstack->elts) {
8✔
878
    errno = EPERM;
1✔
879
    return NULL;
1✔
880
  }
881

882
  parser_curr_server--;
7✔
883
  parser_servstack->nelts--;
7✔
884

885
  return *parser_curr_server;
7✔
886
}
887

888
server_rec *pr_parser_server_ctxt_get(void) {
27✔
889
  if (parser_curr_server) {
27✔
890
    return *parser_curr_server;
21✔
891
  }
892

893
  errno = ENOENT;
6✔
894
  return NULL;
6✔
895
}
896

897
int pr_parser_server_ctxt_push(server_rec *s) {
14✔
898
  if (s == NULL) {
14✔
899
    errno = EINVAL;
1✔
900
    return -1;
1✔
901
  }
902

903
  if (parser_servstack == NULL) {
13✔
904
    errno = EPERM;
1✔
905
    return -1;
1✔
906
  }
907

908
  parser_curr_server = (server_rec **) push_array(parser_servstack);
12✔
909
  *parser_curr_server = s;
12✔
910

911
  return 0;
12✔
912
}
913

914
server_rec *pr_parser_server_ctxt_open(const char *addrstr) {
11✔
915
  server_rec *s;
11✔
916
  pool *p;
11✔
917

918
  p = make_sub_pool(permanent_pool);
11✔
919
  pr_pool_tag(p, "<VirtualHost> Pool");
11✔
920

921
  s = (server_rec *) pcalloc(p, sizeof(server_rec));
11✔
922
  s->pool = p;
11✔
923
  s->config_type = CONF_VIRTUAL;
11✔
924
  s->sid = ++parser_sid;
11✔
925
  s->notes = pr_table_nalloc(p, 0, 8);
11✔
926

927
  /* TCP port reuse is disabled by default. */
928
  s->tcp_reuse_port = -1;
11✔
929

930
  /* TCP KeepAlive is enabled by default, with the system defaults. */
931
  s->tcp_keepalive = palloc(s->pool, sizeof(struct tcp_keepalive));
11✔
932
  s->tcp_keepalive->keepalive_enabled = TRUE;
11✔
933
  s->tcp_keepalive->keepalive_idle = -1;
11✔
934
  s->tcp_keepalive->keepalive_count = -1;
11✔
935
  s->tcp_keepalive->keepalive_intvl = -1;
11✔
936

937
  /* Have to make sure it ends up on the end of the chain, otherwise
938
   * main_server becomes useless.
939
   */
940
  xaset_insert_end(*parser_server_list, (xasetmember_t *) s);
11✔
941
  s->set = *parser_server_list;
11✔
942
  if (addrstr) {
11✔
943
    s->ServerAddress = pstrdup(s->pool, addrstr);
11✔
944
  }
945

946
  /* Default server port */
947
  s->ServerPort = pr_inet_getservport(s->pool, "ftp", "tcp");
11✔
948

949
  (void) pr_parser_server_ctxt_push(s);
11✔
950
  return s;
11✔
951
}
952

953
unsigned long pr_parser_set_include_opts(unsigned long opts) {
6✔
954
  unsigned long prev_opts;
6✔
955

956
  prev_opts = parser_include_opts;
6✔
957
  parser_include_opts = opts;
6✔
958

959
  return prev_opts;
6✔
960
}
961

962
static const char *tmpfile_patterns[] = {
963
  "*~",
964
  "*.sw?",
965
  NULL
966
};
967

968
static int is_tmp_file(const char *file) {
30✔
969
  register unsigned int i;
30✔
970

971
  for (i = 0; tmpfile_patterns[i]; i++) {
86✔
972
    if (pr_fnmatch(tmpfile_patterns[i], file, PR_FNM_PERIOD) == 0) {
58✔
973
      return TRUE;
974
    }
975
  }
976

977
  return FALSE;
978
}
979

980
static int config_filename_cmp(const void *a, const void *b) {
×
981
  return strcmp(*((char **) a), *((char **) b));
×
982
}
983

984
static int parse_wildcard_config_path(pool *p, const char *path,
1✔
985
    unsigned int depth) {
986
  register unsigned int i;
1✔
987
  int res, xerrno;
1✔
988
  pool *tmp_pool;
1✔
989
  array_header *globbed_dirs = NULL;
1✔
990
  const char *component = NULL, *parent_path = NULL, *suffix_path = NULL;
1✔
991
  struct stat st;
1✔
992
  size_t path_len, component_len;
1✔
993
  char *name_pattern = NULL;
1✔
994
  void *dirh = NULL;
1✔
995
  struct dirent *dent = NULL;
1✔
996

997
  if (depth > PR_PARSER_INCLUDE_MAX_DEPTH) {
1✔
998
    pr_log_pri(PR_LOG_WARNING, "error: resolving wildcard pattern in '%s' "
×
999
      "exceeded maximum filesystem depth (%u)", path,
1000
      (unsigned int) PR_PARSER_INCLUDE_MAX_DEPTH);
1001
    errno = EINVAL;
×
1002
    return -1;
×
1003
  }
1004

1005
  path_len = strlen(path);
1✔
1006
  if (path_len < 2) {
1✔
1007
    pr_trace_msg(trace_channel, 7, "path '%s' too short to be wildcard path",
×
1008
      path);
1009

1010
    /* The first character must be a slash, and we need at least one more
1011
     * character in the path as a glob character.
1012
     */
1013
    errno = EINVAL;
×
1014
    return -1;
×
1015
  }
1016

1017
  tmp_pool = make_sub_pool(p);
1✔
1018
  pr_pool_tag(tmp_pool, "Include sub-pool");
1✔
1019

1020
  /* We need to find the first component of the path which contains glob
1021
   * characters.  We then use the path up to the previous component as the
1022
   * parent directory to open, and the glob-bearing component as the filter
1023
   * for directories within the parent.
1024
   */
1025

1026
  parent_path = pstrdup(tmp_pool, "/");
1✔
1027
  component = path + 1;
1✔
1028

1029
  while (TRUE) {
1✔
1030
    int last_component = FALSE;
1✔
1031
    char *ptr;
1✔
1032

1033
    pr_signals_handle();
1✔
1034

1035
    ptr = strchr(component, '/');
1✔
1036
    if (ptr != NULL) {
1✔
1037
      component_len = ptr - component;
1✔
1038

1039
    } else {
1040
      component_len = strlen(component);
×
1041
      last_component = TRUE;
×
1042
    }
1043

1044
    if (memchr(component, (int) '*', component_len) != NULL ||
1✔
1045
        memchr(component, (int) '?', component_len) != NULL ||
×
1046
        memchr(component, (int) '[', component_len) != NULL) {
×
1047

1048
      name_pattern = pstrndup(tmp_pool, component, component_len);
1✔
1049

1050
      if (ptr != NULL) {
1✔
1051
        suffix_path = pstrdup(tmp_pool, ptr + 1);
1✔
1052
      }
1053

1054
      break;
1055
    }
1056

1057
    parent_path = pdircat(tmp_pool, parent_path,
×
1058
      pstrndup(tmp_pool, component, component_len), NULL);
1059

1060
    if (last_component == TRUE) {
×
1061
      break;
1062
    }
1063

1064
    component = ptr + 1;
×
1065
  }
1066

1067
  if (name_pattern == NULL) {
1✔
1068
    pr_trace_msg(trace_channel, 4,
×
1069
      "unable to process invalid, non-globbed path '%s'", path);
1070
    errno = ENOENT;
×
1071
    return -1;
×
1072
  }
1073

1074
  pr_trace_msg(trace_channel, 19, "generated globbed name pattern '%s/%s'",
1✔
1075
    parent_path, name_pattern);
1076

1077
  pr_fs_clear_cache2(parent_path);
1✔
1078
  res = pr_fsio_lstat(parent_path, &st);
1✔
1079
  xerrno = errno;
1✔
1080

1081
  if (res < 0) {
1✔
1082
    pr_log_pri(PR_LOG_WARNING,
×
1083
      "error: failed to check configuration path '%s': %s", parent_path,
1084
      strerror(xerrno));
1085

1086
    destroy_pool(tmp_pool);
×
1087
    errno = xerrno;
×
1088
    return -1;
×
1089
  }
1090

1091
  if (S_ISLNK(st.st_mode) &&
1✔
1092
      !(parser_include_opts & PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS)) {
×
1093
    pr_log_pri(PR_LOG_WARNING,
×
1094
      "error: cannot read configuration path '%s': Symbolic link", parent_path);
1095
    destroy_pool(tmp_pool);
×
1096
    errno = ENOTDIR;
×
1097
    return -1;
×
1098
  }
1099

1100
  pr_log_pri(PR_LOG_DEBUG,
1✔
1101
    "processing configuration directory '%s' using pattern '%s', suffix '%s'",
1102
    parent_path, name_pattern, suffix_path);
1103

1104
  dirh = pr_fsio_opendir(parent_path);
1✔
1105
  if (dirh == NULL) {
1✔
1106
    pr_log_pri(PR_LOG_WARNING,
×
1107
      "error: unable to open configuration directory '%s': %s", parent_path,
1108
      strerror(errno));
1109
    destroy_pool(tmp_pool);
×
1110
    errno = EINVAL;
×
1111
    return -1;
×
1112
  }
1113

1114
  globbed_dirs = make_array(tmp_pool, 0, sizeof(char *));
1✔
1115

1116
  while ((dent = pr_fsio_readdir(dirh)) != NULL) {
29✔
1117
    pr_signals_handle();
28✔
1118

1119
    if (strcmp(dent->d_name, ".") == 0 ||
28✔
1120
        strcmp(dent->d_name, "..") == 0) {
27✔
1121
      continue;
2✔
1122
    }
1123

1124
    if (parser_include_opts & PR_PARSER_INCLUDE_OPT_IGNORE_TMP_FILES) {
26✔
1125
      if (is_tmp_file(dent->d_name) == TRUE) {
26✔
1126
        pr_trace_msg(trace_channel, 19,
×
1127
          "ignoring temporary file '%s' found in directory '%s'", dent->d_name,
1128
          parent_path);
1129
        continue;
×
1130
      }
1131
    }
1132

1133
    if (pr_fnmatch(name_pattern, dent->d_name, PR_FNM_PERIOD) == 0) {
26✔
1134
      pr_trace_msg(trace_channel, 17,
1✔
1135
        "matched '%s/%s' path with wildcard pattern '%s/%s'", parent_path,
1136
        dent->d_name, parent_path, name_pattern);
1137

1138
      *((char **) push_array(globbed_dirs)) = pdircat(tmp_pool, parent_path,
1✔
1139
        dent->d_name, suffix_path, NULL);
1140
    }
1141
  }
1142

1143
  pr_fsio_closedir(dirh);
1✔
1144

1145
  if (globbed_dirs->nelts == 0) {
1✔
1146
    pr_log_pri(PR_LOG_WARNING,
×
1147
      "error: no matches found for wildcard directory '%s'", path);
1148
    destroy_pool(tmp_pool);
×
1149
    errno = ENOENT;
×
1150
    return -1;
×
1151
  }
1152

1153
  depth++;
1✔
1154

1155
  qsort((void *) globbed_dirs->elts, globbed_dirs->nelts, sizeof(char *),
1✔
1156
    config_filename_cmp);
1157

1158
  for (i = 0; i < globbed_dirs->nelts; i++) {
3✔
1159
    const char *globbed_dir;
1✔
1160

1161
    globbed_dir = ((const char **) globbed_dirs->elts)[i];
1✔
1162
    res = parse_config_path2(p, globbed_dir, depth);
1✔
1163
    if (res < 0) {
1✔
1164
      xerrno = errno;
×
1165

1166
      pr_trace_msg(trace_channel, 7, "error parsing wildcard path '%s': %s",
×
1167
        globbed_dir, strerror(xerrno));
1168

1169
      destroy_pool(tmp_pool);
×
1170
      errno = xerrno;
×
1171
      return -1;
×
1172
    }
1173
  }
1174

1175
  destroy_pool(tmp_pool);
1✔
1176
  return 0;
1✔
1177
}
1178

1179
int parse_config_path2(pool *p, const char *path, unsigned int depth) {
15✔
1180
  struct stat st;
15✔
1181
  int have_glob;
15✔
1182
  void *dirh;
15✔
1183
  struct dirent *dent;
15✔
1184
  array_header *file_list;
15✔
1185
  char *dup_path, *ptr;
15✔
1186
  pool *tmp_pool;
15✔
1187

1188
  if (p == NULL ||
15✔
1189
      path == NULL ||
15✔
1190
      (depth > PR_PARSER_INCLUDE_MAX_DEPTH)) {
1191
    errno = EINVAL;
3✔
1192
    return -1;
3✔
1193
  }
1194

1195
  if (pr_fs_valid_path(path) < 0) {
12✔
1196
    errno = EINVAL;
1✔
1197
    return -1;
1✔
1198
  }
1199

1200
  have_glob = pr_str_is_fnmatch(path);
11✔
1201
  if (have_glob) {
11✔
1202
    /* Even though the path may be valid, it also may not be a filesystem
1203
     * path; consider custom FSIO modules.  Thus if the path does not start
1204
     * with a slash, it should not be treated as having globs.
1205
     */
1206
    if (*path != '/') {
7✔
1207
      have_glob = FALSE;
×
1208
    }
1209
  }
1210

1211
  pr_fs_clear_cache2(path);
11✔
1212

1213
  if (have_glob) {
11✔
1214
    pr_trace_msg(trace_channel, 19, "parsing '%s' as a globbed path", path);
7✔
1215
  }
1216

1217
  if (!have_glob &&
15✔
1218
      pr_fsio_lstat(path, &st) < 0) {
4✔
1219
    return -1;
1220
  }
1221

1222
  /* If path is not a glob pattern, and is a symlink OR is not a directory,
1223
   * then use the normal parsing function for the file.
1224
   */
1225
  if (have_glob == FALSE &&
10✔
1226
      (S_ISLNK(st.st_mode) ||
3✔
1227
       !S_ISDIR(st.st_mode))) {
1228
    int res, xerrno;
1✔
1229

1230
    PRIVS_ROOT
1✔
1231
    res = pr_parser_parse_file(p, path, NULL, 0);
1✔
1232
    xerrno = errno;
1✔
1233
    PRIVS_RELINQUISH
1✔
1234

1235
    errno = xerrno;
1✔
1236
    return res;
1✔
1237
  }
1238

1239
  tmp_pool = make_sub_pool(p);
9✔
1240
  pr_pool_tag(tmp_pool, "Include sub-pool");
9✔
1241

1242
  /* Handle the glob/directory. */
1243
  dup_path = pstrdup(tmp_pool, path);
9✔
1244

1245
  ptr = strrchr(dup_path, '/');
9✔
1246

1247
  if (have_glob) {
9✔
1248
    int have_glob_dir;
7✔
1249

1250
    /* Note that we know, by definition, that ptr CANNOT be null here; dup_path
1251
     * is a duplicate of path, and the first character (if nothing else) of
1252
     * path MUST be a slash, per earlier checks.
1253
     */
1254
    *ptr = '\0';
7✔
1255

1256
    /* We just changed ptr, thus we DO need to check whether the now-modified
1257
     * path contains fnmatch(3) characters again.
1258
     */
1259
    have_glob_dir = pr_str_is_fnmatch(dup_path);
7✔
1260
    if (have_glob_dir) {
7✔
1261
      const char *glob_dir;
3✔
1262

1263
      if (parser_include_opts & PR_PARSER_INCLUDE_OPT_IGNORE_WILDCARDS) {
3✔
1264
        pr_log_pri(PR_LOG_WARNING, "error: wildcard patterns not allowed in "
2✔
1265
          "configuration directory name '%s'", dup_path);
1266
        destroy_pool(tmp_pool);
2✔
1267
        errno = EINVAL;
2✔
1268
        return -1;
2✔
1269
      }
1270

1271
      *ptr = '/';
1✔
1272
      glob_dir = pstrdup(p, dup_path);
1✔
1273
      destroy_pool(tmp_pool);
1✔
1274

1275
      return parse_wildcard_config_path(p, glob_dir, depth);
1✔
1276
    }
1277

1278
    ptr++;
4✔
1279

1280
    /* Check the directory component. */
1281
    pr_fs_clear_cache2(dup_path);
4✔
1282
    if (pr_fsio_lstat(dup_path, &st) < 0) {
4✔
1283
      int xerrno = errno;
×
1284

1285
      pr_log_pri(PR_LOG_WARNING,
×
1286
        "error: failed to check configuration path '%s': %s", dup_path,
1287
        strerror(xerrno));
1288

1289
      destroy_pool(tmp_pool);
×
1290
      errno = xerrno;
×
1291
      return -1;
×
1292
    }
1293

1294
    if (S_ISLNK(st.st_mode) &&
4✔
1295
        !(parser_include_opts & PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS)) {
×
1296
      pr_log_pri(PR_LOG_WARNING,
×
1297
        "error: cannot read configuration path '%s': Symbolic link", path);
1298
      destroy_pool(tmp_pool);
×
1299
      errno = ENOTDIR;
×
1300
      return -1;
×
1301
    }
1302

1303
    if (have_glob_dir == FALSE &&
4✔
1304
        pr_str_is_fnmatch(ptr) == FALSE) {
4✔
1305
      pr_log_pri(PR_LOG_WARNING,
×
1306
        "error: wildcard pattern required for file '%s'", ptr);
1307
      destroy_pool(tmp_pool);
×
1308
      errno = EINVAL;
×
1309
      return -1;
×
1310
    }
1311
  }
1312

1313
  pr_trace_msg(trace_channel, 3, "processing configuration directory '%s'",
6✔
1314
    dup_path);
1315

1316
  dirh = pr_fsio_opendir(dup_path);
6✔
1317
  if (dirh == NULL) {
6✔
1318
    pr_log_pri(PR_LOG_WARNING,
×
1319
      "error: unable to open configuration directory '%s': %s", dup_path,
1320
      strerror(errno));
×
1321
    destroy_pool(tmp_pool);
×
1322
    errno = EINVAL;
×
1323
    return -1;
×
1324
  }
1325

1326
  file_list = make_array(tmp_pool, 0, sizeof(char *));
6✔
1327

1328
  while ((dent = pr_fsio_readdir(dirh)) != NULL) {
23✔
1329
    pr_signals_handle();
17✔
1330

1331
    if (strcmp(dent->d_name, ".") == 0 ||
17✔
1332
        strcmp(dent->d_name, "..") == 0) {
11✔
1333
      continue;
12✔
1334
    }
1335

1336
    if (parser_include_opts & PR_PARSER_INCLUDE_OPT_IGNORE_TMP_FILES) {
5✔
1337
      if (is_tmp_file(dent->d_name) == TRUE) {
4✔
1338
        pr_trace_msg(trace_channel, 19,
2✔
1339
          "ignoring temporary file '%s' found in directory '%s'", dent->d_name,
1340
          dup_path);
1341
        continue;
2✔
1342
      }
1343
    }
1344

1345
    if (have_glob == FALSE ||
3✔
1346
        (ptr != NULL &&
3✔
1347
         pr_fnmatch(ptr, dent->d_name, PR_FNM_PERIOD) == 0)) {
3✔
1348
      *((char **) push_array(file_list)) = pdircat(tmp_pool, dup_path,
3✔
1349
        dent->d_name, NULL);
1350
    }
1351
  }
1352

1353
  pr_fsio_closedir(dirh);
6✔
1354

1355
  if (file_list->nelts) {
6✔
1356
    register unsigned int i;
3✔
1357

1358
    qsort((void *) file_list->elts, file_list->nelts, sizeof(char *),
3✔
1359
      config_filename_cmp);
1360

1361
    for (i = 0; i < file_list->nelts; i++) {
9✔
1362
      int res, xerrno;
3✔
1363
      char *file;
3✔
1364

1365
      file = ((char **) file_list->elts)[i];
3✔
1366

1367
      /* Make sure we always parse the files with root privs.  The
1368
       * previously parsed file might have had root privs relinquished
1369
       * (e.g. by its directive handlers), but when we first start up,
1370
       * we have root privs.  See Bug#3855.
1371
       */
1372
      PRIVS_ROOT
3✔
1373
      res = pr_parser_parse_file(tmp_pool, file, NULL, 0);
3✔
1374
      xerrno = errno;
3✔
1375
      PRIVS_RELINQUISH
3✔
1376

1377
      if (res < 0) {
3✔
1378
        pr_log_pri(PR_LOG_WARNING,
×
1379
          "error: unable to parse file '%s': %s", file, strerror(xerrno));
1380
        pr_log_pri(PR_LOG_WARNING, "%s",
×
1381
          "error: check `proftpd --configtest -d10` for details");
1382

1383
        destroy_pool(tmp_pool);
×
1384

1385
        /* Any error other than EINVAL is logged as a warning, but ignored,
1386
         * by the Include directive handler.  Thus we always return EINVAL
1387
         * here to halt further parsing (Issue #1721).
1388
         */
1389
        errno = EINVAL;
×
1390
        return -1;
×
1391
      }
1392
    }
1393
  }
1394

1395
  destroy_pool(tmp_pool);
6✔
1396
  return 0;
6✔
1397
}
1398

1399
int parse_config_path(pool *p, const char *path) {
1✔
1400
  return parse_config_path2(p, path, 0);
1✔
1401
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc