Coverage Report

Created: 2026-11-11 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proftpd/modules/mod_procfs.c
Line
Count
Source
1
/*
2
 * ProFTPD: mod_procfs -- a module for hiding the /proc filesystem
3
 * Copyright (c) 2026 TJ Saunders
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, see <https://www.gnu.org/licenses/>.
17
 *
18
 * As a special exemption, TJ Saunders and other respective copyright holders
19
 * give permission to link this program with OpenSSL, and distribute the
20
 * resulting executable, without including the source code for OpenSSL in the
21
 * source distribution.
22
 *
23
 * This is mod_procfs contrib software for proftpd 1.3.x and above.
24
 * For more information contact TJ Saunders <tj@castaglia.org>.
25
 *
26
 * -----DO NOT CHANGE THE LINES BELOW-----
27
 */
28
29
#include "conf.h"
30
#include "privs.h"
31
32
#if !defined(HAVE_MNTENT_H)
33
/* Older ProFTPD versions did not check for the <mntent.h> header, so we
34
 * will use heuristics to guess whether it is present.  Some platforms,
35
 * such as Mac OSX, do not have this header.
36
 *
37
 * If/when I get access to BSD platforms, I can add heuristics to handle them
38
 * appropriately in the future.
39
 */
40
# if defined(__GLIBC__)
41
#   define HAVE_MNTENT_H 1
42
# elif defined(LINUX)
43
#   define HAVE_MNTENT_H 1
44
# endif /* LINUX */
45
#endif /* HAVE_MNTENT_H */
46
47
#if defined(HAVE_MNTENT_H)
48
# include <mntent.h>
49
#endif /* HAVE_MNTENT_H */
50
51
#if PROFTPD_VERSION_NUMBER < 0x0001030602
52
# error "ProFTPD 1.3.6rc2 or later required"
53
#endif
54
55
0
#define MOD_PROCFS_VERSION  "mod_procfs/0.4"
56
57
module procfs_module;
58
59
static int procfs_engine = FALSE;
60
static int procfs_logfd = -1;
61
static pool *procfs_pool = NULL;
62
63
static const char *trace_channel = "procfs";
64
65
struct procfs_mount {
66
  const char *path;
67
  size_t path_len;
68
  const char *type;
69
};
70
71
static array_header *procfs_mounts = NULL;
72
73
0
static int get_procfs_mounts(pool *p) {
74
0
#if defined(HAVE_MNTENT_H)
75
0
  FILE *mountf = NULL;
76
0
  struct mntent *mnt = NULL;
77
78
0
  mountf = setmntent("/etc/mtab", "r");
79
0
  if (mountf == NULL) {
80
0
    int xerrno = errno;
81
82
0
    pr_trace_msg(trace_channel, 1, "unable to read /etc/mtab: %s",
83
0
      strerror(xerrno));
84
0
    errno = xerrno;
85
0
    return -1;
86
0
  }
87
88
0
  mnt = getmntent(mountf);
89
0
  while (mnt != NULL) {
90
0
    struct procfs_mount *mount = NULL;
91
0
    size_t path_len = 0;
92
93
0
    pr_signals_handle();
94
95
0
    if (strcmp(mnt->mnt_type, "proc") == 0) {
96
0
      pr_log_debug(DEBUG0, MOD_PROCFS_VERSION
97
0
        ": discovered procfs mounted at '%s'", mnt->mnt_dir);
98
0
      mount = palloc(p, sizeof(struct procfs_mount));
99
0
      mount->type = pstrdup(p, "procfs");
100
101
0
    } else if (strcmp(mnt->mnt_type, "sysfs") == 0) {
102
0
      pr_log_debug(DEBUG0, MOD_PROCFS_VERSION
103
0
        ": discovered sysfs mounted at '%s'", mnt->mnt_dir);
104
0
      mount = palloc(p, sizeof(struct procfs_mount));
105
0
      mount->type = pstrdup(p, "sysfs");
106
0
    }
107
108
0
    if (mount != NULL) {
109
      /* If the mount point path does not end with a trailing slash, add it.
110
       * We use this property when checking paths that reference this mount
111
       * point.
112
       */
113
0
      path_len = strlen(mnt->mnt_dir);
114
0
      if (mnt->mnt_dir[path_len-1] != '/') {
115
0
        mount->path = pstrcat(p, mnt->mnt_dir, "/", NULL);
116
0
        mount->path_len = path_len + 1;
117
118
0
      } else {
119
0
        mount->path = pstrdup(p, mnt->mnt_dir);
120
0
        mount->path_len = path_len;
121
0
      }
122
123
0
      if (procfs_mounts == NULL) {
124
0
        procfs_mounts = make_array(p, 0, sizeof(struct procfs_mount *));
125
0
      }
126
127
0
      *((struct procfs_mount **) push_array(procfs_mounts)) = mount;
128
0
    }
129
130
0
    mnt = getmntent(mountf);
131
0
  }
132
133
0
  if (endmntent(mountf) != 1) {
134
0
    pr_trace_msg(trace_channel, 1, "error closing /etc/mtab: %s",
135
0
      strerror(errno));
136
0
  }
137
138
#else
139
  struct stat st;
140
141
  /* Check for the presence of the /proc filesystem on this host.  If it
142
   * is not present, then we need do nothing else.
143
   */
144
  if (lstat("/proc/", &st) == 0) {
145
    pr_log_debug(DEBUG10, MOD_PROCFS_VERSION ": found /proc/ filesystem");
146
147
    if (S_ISDIR(st.st_mode)) {
148
      struct procfs_mount *mount;
149
150
      mount = palloc(p, sizeof(struct procfs_mount));
151
      mount->path = pstrdup(p, "/proc/");
152
      mount->path_len = 6;
153
154
      procfs_mounts = make_array(p, 0, sizeof(struct procfs_mount *));
155
      *((struct procfs_mount **) push_array(procfs_mounts)) = mount;
156
157
    } else {
158
      pr_log_debug(DEBUG10, MOD_PROCFS_VERSION ": /proc/ is not a directory");
159
    }
160
  }
161
#endif /* HAVE_MNTENT_H */
162
163
0
  if (procfs_mounts != NULL) {
164
    /* Automatically enable ProcfsEngine on in such cases. */
165
0
    procfs_engine = TRUE;
166
167
0
  } else {
168
0
    pr_log_debug(DEBUG5, MOD_PROCFS_VERSION
169
0
      ": did not find /proc filesystem: %s", strerror(errno));
170
0
  }
171
172
0
  return 0;
173
0
}
174
175
0
static int is_blocked_path(pool *p, const char *path) {
176
0
  register unsigned int i;
177
0
  int res = FALSE;
178
0
  char *abs_path;
179
0
  size_t abs_pathlen;
180
0
  struct procfs_mount **mounts;
181
182
0
  abs_path = dir_abs_path(p, path, FALSE);
183
0
  abs_pathlen = strlen(abs_path);
184
185
0
  mounts = procfs_mounts->elts;
186
0
  for (i = 0; i < procfs_mounts->nelts; i++) {
187
0
    struct procfs_mount *mount;
188
189
0
    pr_signals_handle();
190
191
0
    mount = mounts[i];
192
193
0
    pr_trace_msg(trace_channel, 19,
194
0
      "checking path '%s' against %s mount '%s'", abs_path, mount->type,
195
0
      mount->path);
196
197
0
    if (abs_pathlen >= mount->path_len &&
198
0
        strncmp(abs_path, mount->path, mount->path_len) == 0) {
199
0
      res = TRUE;
200
201
0
    } else if (abs_pathlen == (mount->path_len - 1) &&
202
0
               strncmp(abs_path, mount->path, mount->path_len - 1) == 0) {
203
0
      res = TRUE;
204
0
    }
205
206
0
    if (res == TRUE) {
207
0
      break;
208
0
    }
209
0
  }
210
211
0
  return res;
212
0
}
213
214
/* Configuration handlers
215
 */
216
217
/* usage: ProcfsEngine on|off */
218
0
MODRET set_procfsengine(cmd_rec *cmd) {
219
0
  int engine = -1;
220
0
  config_rec *c = NULL;
221
222
0
  CHECK_ARGS(cmd, 1);
223
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
224
225
0
  engine = get_boolean(cmd, 1);
226
0
  if (engine == -1) {
227
0
    CONF_ERROR(cmd, "expected Boolean parameter");
228
0
  }
229
230
0
  c = add_config_param(cmd->argv[0], 1, NULL);
231
0
  c->argv[0] = palloc(c->pool, sizeof(int));
232
0
  *((int *) c->argv[0]) = engine;
233
234
0
  return PR_HANDLED(cmd);
235
0
}
236
237
/* usage: ProcfsLog path|"none" */
238
0
MODRET set_procfslog(cmd_rec *cmd) {
239
0
  CHECK_ARGS(cmd, 1);
240
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
241
242
0
  if (pr_fs_valid_path(cmd->argv[1]) < 0) {
243
0
    CONF_ERROR(cmd, "must be an absolute path");
244
0
  }
245
246
0
  (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
247
0
  return PR_HANDLED(cmd);
248
0
}
249
250
0
static const char *get_cmd_resp_code(cmd_rec *cmd) {
251
0
  const char *resp_code = R_550;
252
253
0
  if (pr_cmd_cmp(cmd, PR_CMD_LIST_ID) == 0 ||
254
0
      pr_cmd_cmp(cmd, PR_CMD_NLST_ID) == 0 ||
255
0
      pr_cmd_cmp(cmd, PR_CMD_STAT_ID) == 0) {
256
0
    resp_code = R_450;
257
0
  }
258
259
0
  return resp_code;
260
0
}
261
262
static modret_t *handle_path(cmd_rec *cmd, const char *cmd_name,
263
0
    const char *path) {
264
0
  pr_trace_msg(trace_channel, 19, "checking path '%s' for %s", path, cmd_name);
265
266
0
  if (is_blocked_path(cmd->tmp_pool, path) == TRUE) {
267
0
    const char *proto, *resp_code;
268
269
0
    proto = pr_session_get_protocol(0);
270
271
0
    (void) pr_log_writefile(procfs_logfd, MOD_PROCFS_VERSION,
272
0
      "%s %s denied by mod_procfs for user '%s', client IP %s, protocol %s",
273
0
      cmd_name, path, session.user,
274
0
      pr_netaddr_get_ipstr(session.c->remote_addr), proto);
275
0
    pr_log_pri(PR_LOG_NOTICE, "%s %s denied by mod_procfs", cmd_name, path);
276
277
    /* The response code to use depends on the command. */
278
0
    resp_code = get_cmd_resp_code(cmd);
279
0
    pr_response_add_err(resp_code, _("%s: %s"), path, strerror(ENOENT));
280
281
0
    pr_cmd_set_errno(cmd, ENOENT);
282
0
    errno = ENOENT;
283
0
    return PR_ERROR(cmd);
284
0
  }
285
286
0
  return PR_DECLINED(cmd);
287
0
}
288
289
/* Command handlers
290
 */
291
292
0
MODRET procfs_pre_mfmt(cmd_rec *cmd) {
293
0
  const char *path, *ptr;
294
295
0
  if (procfs_engine == FALSE) {
296
0
    return PR_DECLINED(cmd);
297
0
  }
298
299
0
  if (cmd->argc < 3) {
300
0
    return PR_DECLINED(cmd);
301
0
  }
302
303
  /* The path can contain spaces.  Thus we need to use cmd->arg, not cmd->argv,
304
   * to find the path.  But cmd->arg contains the facts as well.  Thus we
305
   * find the FIRST space in cmd->arg; the path is everything past that space.
306
   */
307
0
  ptr = strchr(cmd->arg, ' ');
308
0
  if (ptr == NULL) {
309
0
    return PR_DECLINED(cmd);
310
0
  }
311
312
0
  path = ptr + 1;
313
0
  return handle_path(cmd, cmd->argv[0], path);
314
0
}
315
316
0
MODRET procfs_pre_path(cmd_rec *cmd) {
317
0
  const char *path;
318
319
0
  if (procfs_engine == FALSE) {
320
0
    return PR_DECLINED(cmd);
321
0
  }
322
323
0
  if (cmd->argc < 2) {
324
0
    return PR_DECLINED(cmd);
325
0
  }
326
327
  /* TODO: Add similar decoding, handling of spaces as done by mod_core. */
328
329
0
  path = cmd->arg;
330
0
  return handle_path(cmd, cmd->argv[0], path);
331
0
}
332
333
0
MODRET procfs_pre_site(cmd_rec *cmd) {
334
0
  char *cmd_name, *path;
335
336
0
  if (procfs_engine == FALSE) {
337
0
    return PR_DECLINED(cmd);
338
0
  }
339
340
0
  if (cmd->argc < 3) {
341
0
    return PR_DECLINED(cmd);
342
0
  }
343
344
  /* These are the SITE commands we know about:
345
   *
346
   *  SITE CHGRP <group> <path>
347
   *  SITE CHMOD <mode> <path>
348
   *
349
   *  SITE MKDIR <path> (mod_site_misc)
350
   *  SITE RMDIR <path> (mod_site_misc)
351
   *  SITE SYMLINK <from> <to> (mod_site_misc)
352
   *  SITE UTIME <timestamp> <path> (mod_site_misc)
353
   *
354
   *  SITE CPFR <path> (mod_copy)
355
   *  SITE CPTO <path> (mod_copy)
356
   *  SITE COPY <from> <to> (mod_copy)
357
   */
358
359
0
  if (strcmp(cmd->argv[1], "CHGRP") == 0 ||
360
0
      strcmp(cmd->argv[1], "CHMOD") == 0 ||
361
0
      strcmp(cmd->argv[1], "UTIME") == 0) {
362
0
    register unsigned int i;
363
0
    char *arg = "";
364
365
0
    if (cmd->argc < 4) {
366
0
      return PR_DECLINED(cmd);
367
0
    }
368
369
    /* Construct the path by concatenating all of the parameter after the
370
     * operational data, separating them with spaces.
371
     */
372
373
0
    for (i = 3; i < cmd->argc; i++) {
374
0
      arg = pstrcat(cmd->tmp_pool, arg, *arg ? " " : "", cmd->argv[i], NULL);
375
0
    }
376
377
0
    cmd_name = pstrcat(cmd->tmp_pool, cmd->argv[0], " ", cmd->argv[1], NULL);
378
0
    path = arg;
379
0
    return handle_path(cmd, cmd_name, path);
380
381
0
  } else if (strcmp(cmd->argv[1], "CPFR") == 0 ||
382
0
             strcmp(cmd->argv[1], "CPTO") == 0 ||
383
0
             strcmp(cmd->argv[1], "MKDIR") == 0 ||
384
0
             strcmp(cmd->argv[1], "RMDIR") == 0) {
385
386
0
    cmd_name = pstrcat(cmd->tmp_pool, cmd->argv[0], " ", cmd->argv[1], NULL);
387
0
    path = cmd->argv[2];
388
0
    return handle_path(cmd, cmd_name, path);
389
390
0
  } else if (strcmp(cmd->argv[1], "COPY") == 0 ||
391
0
             strcmp(cmd->argv[1], "SYMLINK") == 0) {
392
0
    char *from, *to;
393
0
    modret_t *mr;
394
395
0
    if (cmd->argc < 4) {
396
0
      return PR_DECLINED(cmd);
397
0
    }
398
399
0
    cmd_name = pstrcat(cmd->tmp_pool, cmd->argv[0], " ", cmd->argv[1], NULL);
400
0
    from = cmd->argv[2];
401
0
    to = cmd->argv[3];
402
403
0
    mr = handle_path(cmd, cmd_name, from);
404
0
    if (MODRET_ISERROR(mr)) {
405
0
      return mr;
406
0
    }
407
408
0
    return handle_path(cmd, cmd_name, to);
409
410
0
  } else {
411
0
    pr_trace_msg(trace_channel, 7,
412
0
      "unknown/unsupported SITE '%s' command, ignoring", (char *) cmd->argv[1]);
413
0
  }
414
415
0
  return PR_DECLINED(cmd);
416
0
}
417
418
0
MODRET procfs_sftp_pre_path(cmd_rec *cmd) {
419
0
  const char *path, *proto;
420
421
0
  if (procfs_engine == FALSE) {
422
0
    return PR_DECLINED(cmd);
423
0
  }
424
425
0
  proto = pr_session_get_protocol(0);
426
0
  if (strcmp(proto, "sftp") != 0) {
427
0
    return PR_DECLINED(cmd);
428
0
  }
429
430
0
  path = cmd->argv[1];
431
0
  return handle_path(cmd, cmd->argv[0], path);
432
0
}
433
434
0
MODRET procfs_sftp_pre_extended(cmd_rec *cmd) {
435
0
  const char *src_path = NULL, *dst_path = NULL, *proto;
436
0
  modret_t *mr;
437
438
0
  if (procfs_engine == FALSE) {
439
0
    return PR_DECLINED(cmd);
440
0
  }
441
442
0
  proto = pr_session_get_protocol(0);
443
0
  if (strcmp(proto, "sftp") != 0) {
444
0
    return PR_DECLINED(cmd);
445
0
  }
446
447
  /* Not all SFTP extensions use paths to be checked. */
448
0
  if (cmd->argv[2] == NULL &&
449
0
      cmd->argv[3] == NULL) {
450
0
    return PR_DECLINED(cmd);
451
0
  }
452
453
0
  if (cmd->argv[2] != NULL) {
454
0
    src_path = cmd->argv[2];
455
456
0
    mr = handle_path(cmd, cmd->argv[1], src_path);
457
0
    if (MODRET_ISERROR(mr)) {
458
0
      return mr;
459
0
    }
460
0
  }
461
462
0
  if (cmd->argv[3] == NULL) {
463
0
    return PR_DECLINED(cmd);
464
0
  }
465
466
0
  dst_path = cmd->argv[3];
467
0
  return handle_path(cmd, cmd->argv[1], dst_path);
468
0
}
469
470
0
MODRET procfs_sftp_pre_hardlink(cmd_rec *cmd) {
471
0
  const char *src_path, *dst_path, *proto;
472
0
  char *ptr;
473
0
  modret_t *mr;
474
475
0
  if (procfs_engine == FALSE) {
476
0
    return PR_DECLINED(cmd);
477
0
  }
478
479
0
  proto = pr_session_get_protocol(0);
480
0
  if (strcmp(proto, "sftp") != 0) {
481
0
    return PR_DECLINED(cmd);
482
0
  }
483
484
  /* Unfortunately, mod_sftp currently does NOT break the two paths into
485
   * the cmd->argv array; it only populates cmd->arg.
486
   *
487
   * In the future, if/when mod_sftp behavior changes, we can look at the
488
   * cmd->argc to determine which style is being used by mod_sftp.
489
   */
490
0
  ptr = strchr(cmd->arg, ' ');
491
0
  if (ptr == NULL) {
492
0
    return PR_DECLINED(cmd);
493
0
  }
494
495
0
  src_path = pstrndup(cmd->tmp_pool, cmd->arg, ptr - cmd->arg);
496
0
  dst_path = pstrdup(cmd->tmp_pool, ptr + 1);
497
498
0
  mr = handle_path(cmd, cmd->argv[0], src_path);
499
0
  if (MODRET_ISERROR(mr)) {
500
0
    return mr;
501
0
  }
502
503
0
  return handle_path(cmd, cmd->argv[0], dst_path);
504
0
}
505
506
0
MODRET procfs_sftp_pre_symlink(cmd_rec *cmd) {
507
0
  const char *src_path, *dst_path, *proto;
508
0
  char *ptr;
509
0
  modret_t *mr;
510
511
0
  if (procfs_engine == FALSE) {
512
0
    return PR_DECLINED(cmd);
513
0
  }
514
515
0
  proto = pr_session_get_protocol(0);
516
0
  if (strcmp(proto, "sftp") != 0) {
517
0
    return PR_DECLINED(cmd);
518
0
  }
519
520
  /* Unfortunately, mod_sftp currently does NOT break the two paths into
521
   * the cmd->argv array; it only populates cmd->arg.
522
   *
523
   * In the future, if/when mod_sftp behavior changes, we can look at the
524
   * cmd->argc to determine which style is being used by mod_sftp.
525
   */
526
0
  ptr = strchr(cmd->arg, '\t');
527
0
  if (ptr == NULL) {
528
0
    return PR_DECLINED(cmd);
529
0
  }
530
531
0
  src_path = pstrndup(cmd->tmp_pool, cmd->arg, ptr - cmd->arg);
532
0
  dst_path = pstrdup(cmd->tmp_pool, ptr + 1);
533
534
0
  mr = handle_path(cmd, cmd->argv[0], src_path);
535
0
  if (MODRET_ISERROR(mr)) {
536
0
    return mr;
537
0
  }
538
539
0
  return handle_path(cmd, cmd->argv[0], dst_path);
540
0
}
541
542
0
MODRET procfs_post_pass(cmd_rec *cmd) {
543
0
  config_rec *c;
544
545
0
  if (procfs_mounts == NULL) {
546
0
    procfs_engine = FALSE;
547
0
    return PR_DECLINED(cmd);
548
0
  }
549
550
0
  c = find_config(main_server->conf, CONF_PARAM, "ProcfsEngine", FALSE);
551
0
  if (c != NULL) {
552
0
    procfs_engine = *((int *) c->argv[0]);
553
0
  }
554
555
0
  if (procfs_engine == FALSE) {
556
0
    return PR_DECLINED(cmd);
557
0
  }
558
559
  /* Check whether we are chrooted.  If so, then we need not do anything. */
560
0
  if (session.chroot_path != NULL) {
561
0
    if (strcmp(session.chroot_path, "/") != 0) {
562
0
      pr_trace_msg(trace_channel, 3,
563
0
        "session is chrooted to '%s', disabling mod_procfs",
564
0
        session.chroot_path);
565
0
      procfs_engine = FALSE;
566
0
    }
567
0
  }
568
569
0
  return PR_DECLINED(cmd);
570
0
}
571
572
/* Event Listeners
573
 */
574
575
#if defined(PR_SHARED_MODULE)
576
static void procfs_mod_unload_ev(const void *event_data, void *user_data) {
577
  if (strcmp("mod_procfs.c", (const char *) event_data) != 0) {
578
    return;
579
  }
580
581
  pr_event_unregister(&procfs_module, NULL, NULL);
582
583
  (void) close(procfs_logfd);
584
  procfs_logfd = -1;
585
586
  if (procfs_pool != NULL) {
587
    destroy_pool(procfs_pool);
588
    procfs_pool = NULL;
589
    procfs_mounts = NULL;
590
  }
591
592
  procfs_engine = FALSE;
593
}
594
#endif /* PR_SHARED_MODULE */
595
596
0
static void procfs_restart_ev(const void *event_data, void *user_data) {
597
0
  (void) close(procfs_logfd);
598
0
  procfs_logfd = -1;
599
600
0
  if (procfs_pool != NULL) {
601
0
    destroy_pool(procfs_pool);
602
0
  }
603
604
0
  procfs_pool = make_sub_pool(permanent_pool);
605
0
  pr_pool_tag(procfs_pool, MOD_PROCFS_VERSION);
606
0
}
607
608
0
static void procfs_shutdown_ev(const void *event_data, void *user_data) {
609
0
  (void) close(procfs_logfd);
610
0
  procfs_logfd = -1;
611
612
0
  if (procfs_pool != NULL) {
613
0
    destroy_pool(procfs_pool);
614
0
    procfs_pool = NULL;
615
0
    procfs_mounts = NULL;
616
0
  }
617
0
}
618
619
/* Initialization functions
620
 */
621
622
0
static int procfs_init(void) {
623
0
  if (procfs_pool != NULL) {
624
0
    destroy_pool(procfs_pool);
625
0
  }
626
627
0
  procfs_pool = make_sub_pool(permanent_pool);
628
0
  pr_pool_tag(procfs_pool, MOD_PROCFS_VERSION);
629
630
#if defined(PR_SHARED_MODULE)
631
  pr_event_register(&procfs_module, "core.module-unload", procfs_mod_unload_ev,
632
    NULL);
633
#endif /* PR_SHARED_MODULE */
634
0
  pr_event_register(&procfs_module, "core.restart", procfs_restart_ev, NULL);
635
0
  pr_event_register(&procfs_module, "core.shutdown", procfs_shutdown_ev, NULL);
636
637
0
  if (get_procfs_mounts(procfs_pool) < 0) {
638
0
    pr_trace_msg(trace_channel, 1, "unable to discover procfs mounts: %s",
639
0
      strerror(errno));
640
0
  }
641
642
0
  return 0;
643
0
}
644
645
0
static int procfs_sess_init(void) {
646
0
  config_rec *c;
647
0
  const char *path;
648
0
  int res, xerrno;
649
650
0
  c = find_config(main_server->conf, CONF_PARAM, "ProcfsLog", FALSE);
651
0
  if (c == NULL) {
652
0
    return 0;
653
0
  }
654
655
0
  path = c->argv[0];
656
0
  if (strcasecmp(path, "none") == 0) {
657
0
    return 0;
658
0
  }
659
660
0
  PRIVS_ROOT
661
0
  res = pr_log_openfile(path, &procfs_logfd, 0660);
662
0
  xerrno = errno;
663
0
  PRIVS_RELINQUISH
664
665
0
  switch (res) {
666
0
    case 0:
667
0
      break;
668
669
0
    case -1:
670
0
      pr_log_debug(DEBUG1, MOD_PROCFS_VERSION
671
0
        ": unable to open ProcfsLog '%s': %s", path, strerror(xerrno));
672
0
      break;
673
674
0
    case PR_LOG_SYMLINK:
675
0
      pr_log_debug(DEBUG1, MOD_PROCFS_VERSION
676
0
        ": unable to open ProcfsLog '%s': %s", path, "is a symlink");
677
0
      break;
678
679
0
    case PR_LOG_WRITABLE_DIR:
680
0
      pr_log_debug(DEBUG1, MOD_PROCFS_VERSION
681
0
        ": unable to open ProcfsLog '%s': %s", path,
682
0
        "parent directory is world-writable");
683
0
      break;
684
0
  }
685
686
0
  return 0;
687
0
}
688
689
/* Module API tables
690
 */
691
692
static conftable procfs_conftab[] = {
693
  { "ProcfsEngine", set_procfsengine, NULL },
694
  { "ProcfsLog",  set_procfslog,    NULL },
695
  { NULL }
696
};
697
698
static cmdtable procfs_cmdtab[] = {
699
700
  /* FTP */
701
  { PRE_CMD,    C_APPE, G_NONE, procfs_pre_path,  FALSE, FALSE },
702
  { PRE_CMD,    C_CWD,  G_NONE, procfs_pre_path,  FALSE, FALSE },
703
  { PRE_CMD,    C_XCWD, G_NONE, procfs_pre_path,  FALSE, FALSE },
704
  { PRE_CMD,    C_DELE, G_NONE, procfs_pre_path,  FALSE, FALSE },
705
  { PRE_CMD,    C_LIST, G_NONE, procfs_pre_path,  FALSE, FALSE },
706
  { PRE_CMD,    C_MDTM, G_NONE, procfs_pre_path,  FALSE, FALSE },
707
  { PRE_CMD,    C_MFF,  G_NONE, procfs_pre_mfmt,  FALSE, FALSE },
708
  { PRE_CMD,    C_MFMT, G_NONE, procfs_pre_mfmt,  FALSE, FALSE },
709
  { PRE_CMD,    C_MKD,  G_NONE, procfs_pre_path,  FALSE, FALSE },
710
  { PRE_CMD,    C_XMKD, G_NONE, procfs_pre_path,  FALSE, FALSE },
711
  { PRE_CMD,    C_MLSD, G_NONE, procfs_pre_path,  FALSE, FALSE },
712
  { PRE_CMD,    C_MLST, G_NONE, procfs_pre_path,  FALSE, FALSE },
713
  { PRE_CMD,    C_NLST, G_NONE, procfs_pre_path,  FALSE, FALSE },
714
  { PRE_CMD,    C_RETR, G_NONE, procfs_pre_path,  FALSE, FALSE },
715
  { PRE_CMD,    C_RMD,  G_NONE, procfs_pre_path,  FALSE, FALSE },
716
  { PRE_CMD,    C_XRMD, G_NONE, procfs_pre_path,  FALSE, FALSE },
717
  { PRE_CMD,    C_RNFR, G_NONE, procfs_pre_path,  FALSE, FALSE },
718
  { PRE_CMD,    C_RNTO, G_NONE, procfs_pre_path,  FALSE, FALSE },
719
  { PRE_CMD,    C_SITE, G_NONE, procfs_pre_site,  FALSE, FALSE },
720
  { PRE_CMD,    C_SIZE, G_NONE, procfs_pre_path,  FALSE, FALSE },
721
  { PRE_CMD,    C_STAT, G_NONE, procfs_pre_path,  FALSE, FALSE },
722
  { PRE_CMD,    C_STOR, G_NONE, procfs_pre_path,  FALSE, FALSE },
723
724
  /* mod_digest FTP commands */
725
  { PRE_CMD,  "HASH",   G_NONE, procfs_pre_path,  FALSE, FALSE },
726
  { PRE_CMD,  "MD5",    G_NONE, procfs_pre_path,  FALSE, FALSE },
727
  { PRE_CMD,  "XCRC",   G_NONE, procfs_pre_path,  FALSE, FALSE },
728
  { PRE_CMD,  "XMD5",   G_NONE, procfs_pre_path,  FALSE, FALSE },
729
  { PRE_CMD,  "XSHA",   G_NONE, procfs_pre_path,  FALSE, FALSE },
730
  { PRE_CMD,  "XSHA1",  G_NONE, procfs_pre_path,  FALSE, FALSE },
731
  { PRE_CMD,  "XSHA256",  G_NONE, procfs_pre_path,  FALSE, FALSE },
732
  { PRE_CMD,  "XSHA512",  G_NONE, procfs_pre_path,  FALSE, FALSE },
733
734
  /* SFTP */
735
  { PRE_CMD, "EXTENDED",  G_NONE, procfs_sftp_pre_extended, FALSE, FALSE },
736
  { PRE_CMD, "LINK",    G_NONE, procfs_sftp_pre_hardlink, FALSE, FALSE },
737
  { PRE_CMD, "LSTAT",   G_NONE, procfs_sftp_pre_path, FALSE, FALSE },
738
  { PRE_CMD, "OPENDIR",   G_NONE, procfs_sftp_pre_path, FALSE, FALSE },
739
  { PRE_CMD, "READLINK",  G_NONE, procfs_sftp_pre_path, FALSE, FALSE },
740
  { PRE_CMD, "REALPATH",  G_NONE, procfs_sftp_pre_path, FALSE, FALSE },
741
  { PRE_CMD, "SETSTAT",   G_NONE, procfs_sftp_pre_path, FALSE, FALSE },
742
  { PRE_CMD, "SYMLINK",   G_NONE, procfs_sftp_pre_symlink, FALSE, FALSE },
743
744
  { POST_CMD,   C_PASS, G_NONE, procfs_post_pass, FALSE, FALSE },
745
  { 0, NULL }
746
};
747
748
module procfs_module = {
749
  NULL, NULL,
750
751
  /* Module API version 2.0 */
752
  0x20,
753
754
  /* Module name */
755
  "procfs",
756
757
  /* Module configuration handler table */
758
  procfs_conftab,
759
760
  /* Module command handler table */
761
  procfs_cmdtab,
762
763
  /* Module authentication handler table */
764
  NULL,
765
766
  /* Module initialization function */
767
  procfs_init,
768
769
  /* Session initialization function */
770
  procfs_sess_init,
771
772
  /* Module version */
773
  MOD_PROCFS_VERSION
774
};