File: mod_autohost.c

package info (click to toggle)
proftpd-mod-autohost 0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: perl: 1,700; ansic: 422; makefile: 16
file content (663 lines) | stat: -rw-r--r-- 18,727 bytes parent folder | download
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
 * ProFTPD: mod_autohost -- a module for mass virtual hosting
 * Copyright (c) 2004-2021 TJ Saunders
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 *
 * This is mod_autohost, contrib software for proftpd 1.3.x and above.
 * For more information contact TJ Saunders <tj@castaglia.org>.
 */

#include "conf.h"
#include "privs.h"

#define MOD_AUTOHOST_VERSION		"mod_autohost/0.6"

#if PROFTPD_VERSION_NUMBER < 0x0001030604
# error "ProFTPD 1.3.6 or later required"
#endif

module autohost_module;

static const char *autohost_config = NULL;
static int autohost_engine = FALSE;
static int autohost_logfd = -1;
static pool *autohost_pool = NULL;
static xaset_t *autohost_server_list = NULL;

static const char *trace_channel = "autohost";

static char *autohost_get_config(conn_t *conn, const char *server_name) {
  char *ipstr, *portstr, *path = (char *) autohost_config;
  int family;

  family = pr_netaddr_get_family(conn->local_addr);
  ipstr = (char *) pr_netaddr_get_ipstr(conn->local_addr);

  if (family == AF_INET) {
    char *oct1str, *oct2str, *oct3str, *oct4str;
    char *start, *end;

    start = ipstr;
    end = strchr(start, '.');
    *end = '\0';
    oct1str = pstrdup(autohost_pool, start);

    start = end + 1;
    *end = '.';
    end = strchr(start, '.');
    *end = '\0';
    oct2str = pstrdup(autohost_pool, start);

    start = end + 1;
    *end = '.';
    end = strchr(start, '.');
    *end = '\0';
    oct3str = pstrdup(autohost_pool, start);

    start = end + 1;
    *end = '.';
    oct4str = pstrdup(autohost_pool, start);

    if (strstr(path, "%1") != NULL) {
      path = (char *) sreplace(autohost_pool, path, "%1", oct1str, NULL);
    }

    if (strstr(path, "%2") != NULL) {
      path = (char *) sreplace(autohost_pool, path, "%2", oct2str, NULL);
    }

    if (strstr(path, "%3") != NULL) {
      path = (char *) sreplace(autohost_pool, path, "%3", oct3str, NULL);
    }

    if (strstr(path, "%4") != NULL) {
      path = (char *) sreplace(autohost_pool, path, "%4", oct4str, NULL);
    }
  }

  portstr = pcalloc(autohost_pool, 10);
  snprintf(portstr, 10, "%u", conn->local_port);

  if (strstr(path, "%0") != NULL) {
    path = (char *) sreplace(autohost_pool, path, "%0", ipstr, NULL);
  }

  if (server_name != NULL) {
    /* Note: How to handle the case-insensitive name of SNI/HOST, and the
     * case-sensitive nature of paths.  Should we always downcase the entire
     * server name, for purposes of config file lookup?
     */
    if (strstr(path, "%n") != NULL) {
      path = (char *) sreplace(autohost_pool, path, "%n", server_name, NULL);
    }
  }

  if (strstr(path, "%p") != NULL) {
    path = (char *) sreplace(autohost_pool, path, "%p", portstr, NULL);
  }

  return path;
}

/* Largely borrowed/copied from src/bindings.c. */
static unsigned int process_serveralias(server_rec *s, pr_ipbind_t *ipbind) {
  unsigned namebind_count = 0;
  config_rec *c;

  /* If there is no ipbind already for this server, we cannot associate
   * any ServerAlias-based namebinds to it.
   *
   * Keep in mind that there may be multiple ipbinds pointed at this server:
   *
   *  <VirtualHost 1.2.3.4 5.6.7.8>
   *    ServerAlias alias
   *  </VirtualHost>
   */

  if (pr_ipbind_get_server(s->addr, s->ServerPort) == NULL) {
    return 0;
  }

  c = find_config(s->conf, CONF_PARAM, "ServerAlias", FALSE);
  while (c != NULL) {
    int res;

#if PROFTPD_VERSION_NUMBER < 0x0001030707
    res = pr_namebind_create(s, c->argv[0], s->addr, s->ServerPort);
#else
    res = pr_namebind_create(s, c->argv[0], ipbind, s->addr, s->ServerPort);
#endif /* ProFTPD 1.3.7b and later */
    if (res == 0) {
      namebind_count++;

      res = pr_namebind_open(c->argv[0], s->addr, s->ServerPort);
      if (res < 0) {
        pr_trace_msg(trace_channel, 2,
          "notice: unable to open namebind '%s': %s", (char *) c->argv[0],
          strerror(errno));
      }

    } else {
      if (errno != ENOENT) {
        pr_trace_msg(trace_channel, 3,
          "unable to create namebind for '%s' to %s#%u: %s",
          (char *) c->argv[0], pr_netaddr_get_ipstr(s->addr), s->ServerPort,
          strerror(errno));
      }
    }

    c = find_config_next(c, c->next, CONF_PARAM, "ServerAlias", FALSE);
  }

  return namebind_count;
}

static int autohost_parse_config(conn_t *conn, const char *path) {
  server_rec *s;
  pr_ipbind_t *ipbind;

  /* We use session.pool here, rather than autohost_pool, because
   * we'll be destroying autohost_pool once the server_rec has
   * been created and bound.
   */
  pr_parser_prepare(session.pool, &autohost_server_list);
  pr_parser_server_ctxt_open(pr_netaddr_get_ipstr(conn->local_addr));

  /* XXX: some things, like Port, <VirtualHost>, etc in the autohost.conf
   * file will be ignored.
   */

  if (pr_parser_parse_file(session.pool, path, NULL, 0) < 0) {
    return -1;
  }

  pr_parser_server_ctxt_close();
  pr_parser_cleanup();

  if (fixup_servers(autohost_server_list) < 0) {
    int xerrno = errno;

    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error fixing up autohost config '%s': %s", path, strerror(xerrno));

    errno = xerrno;
    return -1;
  }

  s = (server_rec *) autohost_server_list->xas_list;
  s->ServerPort = conn->local_port;

  ipbind = pr_ipbind_find(conn->local_addr, conn->local_port, TRUE);

  /* Now that we have a valid server_rec, we need to bind it to
   * the address to which the client connected.
   */
  process_serveralias(s, ipbind);

  if (ipbind != NULL) {
    /* If we already have a binding in place, we need to replace the
     * server_rec to which that binding points with our new server_rec.
     */
    ipbind->ib_server = s;

    return 0;
  }

  if (pr_ipbind_create(s, conn->local_addr, conn->local_port) < 0) {
    int xerrno = errno;

    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error creating binding: %s", strerror(xerrno));

    errno = xerrno;
    return -1;
  }

  if (pr_ipbind_open(conn->local_addr, conn->local_port, main_server->listen,
      TRUE, TRUE, FALSE) < 0) {
    int xerrno = errno;

    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error opening binding for %s#%d: %s",
      pr_netaddr_get_ipstr(conn->local_addr), conn->local_port,
      strerror(xerrno));

    errno = xerrno;
    return -1;
  }

  return 0;
}

/* Configuration handlers
 */

/* usage: AutoHostConfig path */
MODRET set_autohostconfig(cmd_rec *cmd) {
  CHECK_ARGS(cmd, 1);
  CHECK_CONF(cmd, CONF_ROOT);

  if (pr_fs_valid_path(cmd->argv[1]) < 0) {
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", cmd->argv[1],
      "' is not a valid path", NULL));
  }

  (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
  return PR_HANDLED(cmd);
}

/* usage: AutoHostEngine on|off */
MODRET set_autohostengine(cmd_rec *cmd) {
  int engine;
  config_rec *c;

  CHECK_ARGS(cmd, 1);
  CHECK_CONF(cmd, CONF_ROOT);

  engine = get_boolean(cmd, 1);
  if (engine == -1) {
    CONF_ERROR(cmd, "expected Boolean parameter");
  }

  c = add_config_param(cmd->argv[0], 1, NULL);
  c->argv[0] = pcalloc(c->pool, sizeof(int));
  *((int *) c->argv[0]) = engine;

  return PR_HANDLED(cmd);
}

/* usage: AutoHostLog path */
MODRET set_autohostlog(cmd_rec *cmd) {
  CHECK_ARGS(cmd, 1);
  CHECK_CONF(cmd, CONF_ROOT);

  if (pr_fs_valid_path(cmd->argv[1]) < 0) {
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", cmd->argv[1],
      "' is not a valid path", NULL));
  }

  (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
  return PR_HANDLED(cmd);
}

/* usage: AutoHostPorts port1 ... portN */
MODRET set_autohostports(cmd_rec *cmd) {
  register unsigned int i;
  config_rec *c;
  array_header *port_list;

  if (cmd->argc < 2)
    CONF_ERROR(cmd, "wrong number of parameters");
  CHECK_CONF(cmd, CONF_ROOT);

  /* First, scan all of the configured ports to make sure that they are
   * all valid port numbers.
   */
  for (i = 1; i < cmd->argc; i++) {
    int port;

    port = atoi(cmd->argv[i]);
    if (port < 1 ||
        port > 65535) {
      CONF_ERROR(cmd, "port must be between 1 and 65535");
    }
  }

  c = add_config_param(cmd->argv[0], 1, NULL);

  port_list = make_array(c->pool, cmd->argc - 1, sizeof(int));
  for (i = 1; i < cmd->argc; i++) {
    *((int *) push_array(port_list)) = atoi(cmd->argv[i]);
  }

  c->argv[0] = port_list;
  return PR_HANDLED(cmd);
}

/* Command handlers
 */

MODRET autohost_pre_host(cmd_rec *cmd) {
  const char *path, *server_name;
  struct stat st;

  if (autohost_engine == FALSE) {
    return PR_DECLINED(cmd);
  }

  server_name = (const char *) cmd->argv[1];
  path = autohost_get_config(session.c, server_name);
  pr_trace_msg(trace_channel, 4, "using AutoHostConfig path '%s' for '%s %s'",
    path, (const char *) cmd->argv[0], server_name);

  if (pr_fsio_stat(path, &st) < 0) {
    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error checking for '%s': %s", path, strerror(errno));
    return PR_DECLINED(cmd);
  }

  if (autohost_parse_config(session.c, path) < 0) {
    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error parsing '%s': %s", path, strerror(errno));
    return PR_DECLINED(cmd);
  }

  pr_trace_msg(trace_channel, 9, "'%s %s' found using autohost for %s#%u",
    (const char *) cmd->argv[0], server_name,
    pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port);

  return PR_DECLINED(cmd);
}

/* Event listeners
 */

static void autohost_connect_ev(const void *event_data, void *user_data) {
  const char *path;
  struct stat st;
  conn_t *conn = (conn_t *) event_data;
 
  if (autohost_engine == FALSE) {
    return;
  }

  /* Autohost config files, if found, will take precedence over a matching
   * server config found in the main config file.
   *
   * To avoid this precedence, we could see if there is a binding already
   * configured for the incoming connection, e.g.:
   *
   *  if (pr_ipbind_get_server(conn->local_addr, conn->local_port) != NULL)
   *
   * but this would preclude us from being able to create multiple bindings
   * for the different AutoHostPorts.
   */

  /* Note that we need not necessarily worry about not destroying autohost_pool.
   * It is allocated after the fork().
   */

  path = autohost_get_config(conn, NULL);
  pr_trace_msg(trace_channel, 4, "using AutoHostConfig path '%s'", path);

  if (pr_fsio_stat(path, &st) < 0) {
    int xerrno = errno;

    /* If the given path contains '%n', and the path is not found, do not log
     * the error.  The file in question may be intended for name-based vhosts,
     * which can only be resolved at SNI/HOST time, not at TCP connect time.
     */
    if (xerrno == ENOENT &&
        strstr(path, "%n") != NULL) {
      pr_trace_msg(trace_channel, 19,
        "ignoring connect-time check of name-based config file '%s'", path);

    } else {
      (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
        "error checking for '%s': %s", path, strerror(xerrno));
    }

    return;
  }

  if (autohost_parse_config(conn, path) < 0) {
    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error parsing '%s': %s", path, strerror(errno));
    return;
  }

  pr_trace_msg(trace_channel, 9, "found using autohost for %s#%u",
    pr_netaddr_get_ipstr(conn->local_addr), conn->local_port);
}

#if defined(PR_SHARED_MODULE)
static void autohost_mod_unload_ev(const void *event_data, void *user_data) {
  if (strcmp("mod_autohost.c", (const char *) event_data) != 0) {
    return;
  }

  pr_event_unregister(&autohost_module, NULL, NULL);

  if (autohost_pool != NULL) {
    destroy_pool(autohost_pool);
    autohost_pool = NULL;
  }
}
#endif /* PR_SHARED_MODULE */

static void autohost_sni_ev(const void *event_data, void *user_data) {
  const char *path, *server_name;
  struct stat st;

  if (autohost_engine == FALSE) {
    return;
  }

  server_name = (const char *) event_data;
  path = autohost_get_config(session.c, server_name);
  pr_trace_msg(trace_channel, 4,
    "using AutoHostConfig path '%s' for TLS SNI '%s'", path, server_name);

  if (pr_fsio_stat(path, &st) < 0) {
    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error checking for '%s': %s", path, strerror(errno));
    return;
  }

  if (autohost_parse_config(session.c, path) < 0) {
    (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
      "error parsing '%s': %s", path, strerror(errno));
    return;
  }

  pr_trace_msg(trace_channel, 9, "TLS SNI '%s' found using autohost for %s#%u",
    server_name, pr_netaddr_get_ipstr(session.c->local_addr),
    session.c->local_port);
}

static void autohost_postparse_ev(const void *event_data, void *user_data) {
  config_rec *c;

  c = find_config(main_server->conf, CONF_PARAM, "AutoHostEngine", FALSE);
  if (c != NULL) {
    autohost_engine = *((int *) c->argv[0]);
  }

  if (autohost_engine == FALSE) {
    return;
  }

  autohost_pool = make_sub_pool(permanent_pool);
  pr_pool_tag(autohost_pool, MOD_AUTOHOST_VERSION);

  pr_event_register(&autohost_module, "core.connect", autohost_connect_ev,
    NULL);
  pr_event_register(&autohost_module, "mod_tls.sni", autohost_sni_ev, NULL);

  c = find_config(main_server->conf, CONF_PARAM, "AutoHostConfig", FALSE);
  if (c != NULL) {
    autohost_config = c->argv[0];

  } else {
    pr_log_debug(DEBUG0, MOD_AUTOHOST_VERSION
      ": missing required AutoHostConfig");
    pr_session_disconnect(&autohost_module, PR_SESS_DISCONNECT_BAD_CONFIG,
      "missing required AutoHostConfig directive");
  }

  c = find_config(main_server->conf, CONF_PARAM, "AutoHostLog", FALSE);
  if (c != NULL) {
    int res;
    char *autohost_log;

    autohost_log = c->argv[0];

    PRIVS_ROOT
    res = pr_log_openfile(autohost_log, &autohost_logfd, 0660);
    PRIVS_RELINQUISH

    switch (res) {
      case 0:
        break;

      case -1:
        pr_log_debug(DEBUG1, MOD_AUTOHOST_VERSION
          ": unable to open AutoHostLog '%s': %s", autohost_log,
          strerror(errno));
        break;

      case PR_LOG_SYMLINK:
        pr_log_debug(DEBUG1, MOD_AUTOHOST_VERSION
          ": unable to open AutoHostLog '%s': %s", autohost_log,
          "is a symlink");
        break;

      case PR_LOG_WRITABLE_DIR:
        pr_log_debug(DEBUG0, MOD_AUTOHOST_VERSION
          ": unable to open AutoHostLog '%s': %s", autohost_log,
          "parent directory is world-writable");
        break;
    }
  }

  autohost_server_list = xaset_create(autohost_pool, NULL);

  c = find_config(main_server->conf, CONF_PARAM, "AutoHostPorts", FALSE);
  if (c != NULL) {
    register unsigned int i;
    array_header *port_list;
    int *ports;

    port_list = c->argv[0];
    ports = port_list->elts;

    /* We need to open a binding for each of the specific ports, unless
     * such a binding already exists.
     */

    for (i = 0; i < port_list->nelts; i++) {
      /* If this port duplicates the existing Port, skip it. */
      if (ports[i] == main_server->ServerPort) {
        continue;
      }

      if (pr_ipbind_find(main_server->addr, ports[i], TRUE) == NULL) {
        int res;
        conn_t *listener;

        (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
          "adding socket for AutoHostPort %d", ports[i]);

        res = pr_ipbind_create(main_server, main_server->addr, ports[i]);
        if (res < 0) {
          (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
            "error creating binding for %s#%d: %s",
            pr_netaddr_get_ipstr(main_server->addr), ports[i], strerror(errno));
          continue;
        }

        /* Create a listening socket for this port. */
        listener = pr_inet_create_conn(autohost_pool, -1, main_server->addr,
          ports[i], FALSE);
        if (listener == NULL) {
          (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
            "error opening new listening socket for port %d: %s", ports[i],
            strerror(errno));
          continue;
        }

        res = pr_ipbind_open(main_server->addr, ports[i], listener, FALSE,
          FALSE, TRUE);
        if (res < 0) {
          (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
            "error opening binding for %s:%d: %s",
            pr_netaddr_get_ipstr(main_server->addr), ports[i], strerror(errno));
          continue;
        }

        (void) pr_log_writefile(autohost_logfd, MOD_AUTOHOST_VERSION,
          "opening listening socket for %s on AutoHostPort %d",
          pr_netaddr_get_ipstr(main_server->addr), ports[i]);
      }
    }
  }
}

/* Initialization routines
 */

static int autohost_init(void) {
#if defined(PR_SHARED_MODULE)
  pr_event_register(&autohost_module, "core.module-unload",
    autohost_mod_unload_ev, NULL);
#endif /* PR_SHARED_MODULE */

  pr_event_register(&autohost_module, "core.postparse", autohost_postparse_ev,
    NULL);

  return 0;
}

/* Module API tables
 */

static conftable autohost_conftab[] = {
  { "AutoHostConfig",	set_autohostconfig,	NULL },
  { "AutoHostEngine",	set_autohostengine,	NULL },
  { "AutoHostLog",	set_autohostlog,	NULL },
  { "AutoHostPorts",	set_autohostports,	NULL },
  { NULL }
};

static cmdtable autohost_cmdtab[] = {
#if defined(C_HOST)
  { PRE_CMD,	C_HOST,	G_NONE,	autohost_pre_host,	FALSE,	FALSE },
#endif /* FTP HOST support */
  { 0, NULL }
};

module autohost_module = {
  NULL, NULL,

  /* Module API version 2.0 */
  0x20,

  /* Module name */
  "autohost",

  /* Module configuration handler table */
  autohost_conftab,

  /* Module command handler table */
  autohost_cmdtab,

  /* Module authentication handler table */
  NULL,

  /* Module initialization function */
  autohost_init,

  /* Session initialization function */
  NULL,

  /* Module version */
  MOD_AUTOHOST_VERSION
};