Coverage Report

Created: 2026-12-01 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proftpd/modules/mod_ctrls.c
Line
Count
Source
1
/*
2
 * ProFTPD: mod_ctrls -- a module implementing the ftpdctl local socket
3
 *          server, as well as several utility functions for other Controls
4
 *          modules
5
 * Copyright (c) 2000-2026 TJ Saunders
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * As a special exemption, TJ Saunders and other respective copyright holders
21
 * give permission to link this program with OpenSSL, and distribute the
22
 * resulting executable, without including the source code for OpenSSL in the
23
 * source distribution.
24
 *
25
 * This is mod_ctrls, contrib software for proftpd 1.3.x and above.
26
 * For more information contact TJ Saunders <tj@castaglia.org>.
27
 */
28
29
#include "conf.h"
30
#include "privs.h"
31
#include "mod_ctrls.h"
32
33
0
#define MOD_CTRLS_VERSION "mod_ctrls/0.9.5"
34
35
#ifndef PR_USE_CTRLS
36
# error "Controls support required (use --enable-ctrls)"
37
#endif
38
39
/* Master daemon in standalone mode? (from src/main.c) */
40
extern unsigned char is_master;
41
42
module ctrls_module;
43
static ctrls_acttab_t ctrls_acttab[];
44
45
static const char *trace_channel = "ctrls";
46
47
/* Hard-coded Controls timer IDs.  Need two, one for the initial timer, one
48
 * to identify the user-configured-interval timer
49
 */
50
0
#define CTRLS_TIMER_ID       24075
51
52
static unsigned int ctrls_interval = 10;
53
54
/* Controls listening socket fd */
55
static int ctrls_sockfd = -1;
56
57
0
#define MOD_CTRLS_DEFAULT_SOCK    PR_RUN_DIR "/proftpd.sock"
58
static char *ctrls_sock_file = MOD_CTRLS_DEFAULT_SOCK;
59
60
/* User/group ownership of the control socket */
61
static uid_t ctrls_sock_uid = 0;
62
static gid_t ctrls_sock_gid = 0;
63
64
/* Pool for this module's use */
65
static pool *ctrls_pool = NULL;
66
67
/* Required "freshness" of client credential sockets */
68
static unsigned int ctrls_cl_freshness = 10;
69
70
/* Start of the client list */
71
static pr_ctrls_cl_t *cl_list = NULL;
72
static unsigned int cl_listlen = 0;
73
static unsigned int cl_maxlistlen = 5;
74
75
/* Controls access control list.  This is for ACLs on the control socket
76
 * itself, rather than on individual actions.
77
 */
78
static ctrls_acl_t ctrls_sock_acl;
79
80
static unsigned char ctrls_engine = TRUE;
81
82
/* Necessary prototypes */
83
static int ctrls_setblock(int sockfd);
84
static int ctrls_setnonblock(int sockfd);
85
86
static const char *ctrls_logname = NULL;
87
88
/* Support routines
89
 */
90
91
/* Controls logging routines
92
 */
93
94
0
static int ctrls_closelog(void) {
95
0
  if (ctrls_logname != NULL) {
96
0
    pr_ctrls_set_logfd(-1);
97
0
    ctrls_logname = NULL;
98
0
  }
99
100
0
  return 0;
101
0
}
102
103
0
static int ctrls_openlog(void) {
104
0
  int logfd, res = 0, xerrno = 0;
105
106
  /* Sanity check */
107
0
  if (ctrls_logname == NULL) {
108
0
    return 0;
109
0
  }
110
111
0
  PRIVS_ROOT
112
0
  res = pr_log_openfile(ctrls_logname, &logfd, PR_LOG_SYSTEM_MODE);
113
0
  xerrno = errno;
114
0
  PRIVS_RELINQUISH
115
116
0
  if (res == 0) {
117
0
    pr_ctrls_set_logfd(logfd);
118
119
0
  } else {
120
0
    if (res == -1) {
121
0
      pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
122
0
        ": unable to open ControlsLog '%s': %s", ctrls_logname,
123
0
        strerror(xerrno));
124
125
0
    } else if (res == PR_LOG_WRITABLE_DIR) {
126
0
      pr_log_pri(PR_LOG_WARNING, MOD_CTRLS_VERSION
127
0
        ": unable to open ControlsLog '%s': "
128
0
        "parent directory is world-writable", ctrls_logname);
129
130
0
    } else if (res == PR_LOG_SYMLINK) {
131
0
      pr_log_pri(PR_LOG_WARNING, MOD_CTRLS_VERSION
132
0
        ": unable to open ControlsLog '%s': %s is a symbolic link",
133
0
        ctrls_logname, ctrls_logname);
134
0
    }
135
0
  }
136
137
0
  return res;
138
0
}
139
140
/* Controls client routines
141
 */
142
143
0
static pr_ctrls_cl_t *ctrls_new_cl(void) {
144
0
  pool *cl_pool = NULL;
145
146
0
  if (cl_list == NULL) {
147
148
    /* Our first client */
149
0
    cl_pool = make_sub_pool(ctrls_pool);
150
0
    pr_pool_tag(cl_pool, "Controls client pool");
151
152
0
    cl_list = (pr_ctrls_cl_t *) pcalloc(cl_pool, sizeof(pr_ctrls_cl_t));
153
154
0
    cl_list->cl_pool = cl_pool;
155
0
    cl_list->cl_fd = -1;
156
0
    cl_list->cl_uid = 0;
157
0
    cl_list->cl_user = NULL;
158
0
    cl_list->cl_gid = 0;
159
0
    cl_list->cl_group = NULL;
160
0
    cl_list->cl_pid = 0;
161
0
    cl_list->cl_ctrls = make_array(cl_pool, 0, sizeof(pr_ctrls_t *));
162
163
0
    cl_list->cl_next = NULL;
164
0
    cl_list->cl_prev = NULL;
165
166
0
    cl_listlen = 1;
167
168
0
  } else {
169
0
    pr_ctrls_cl_t *cl = NULL;
170
171
    /* Add another victim to the list */
172
0
    cl_pool = make_sub_pool(ctrls_pool);
173
0
    pr_pool_tag(cl_pool, "Controls client pool");
174
175
0
    cl = (pr_ctrls_cl_t *) pcalloc(cl_pool, sizeof(pr_ctrls_cl_t));
176
177
0
    cl->cl_pool = cl_pool;
178
0
    cl->cl_fd = -1;
179
0
    cl->cl_uid = 0;
180
0
    cl->cl_user = NULL;
181
0
    cl->cl_gid = 0;
182
0
    cl->cl_group = NULL;
183
0
    cl->cl_pid = 0;
184
0
    cl->cl_ctrls = make_array(cl->cl_pool, 0, sizeof(pr_ctrls_t *));
185
186
0
    cl->cl_next = cl_list;
187
0
    cl->cl_prev = NULL;
188
189
0
    cl_list->cl_prev = cl;
190
0
    cl_list = cl;
191
192
0
    cl_listlen++;
193
0
  }
194
195
0
  return cl_list;
196
0
}
197
198
/* Add a new client to the set */
199
static pr_ctrls_cl_t *ctrls_add_cl(int cl_fd, uid_t cl_uid, gid_t cl_gid,
200
0
    pid_t cl_pid, unsigned long cl_flags) {
201
0
  pr_ctrls_cl_t *cl = NULL;
202
203
  /* Make sure there's an empty entry available */
204
0
  cl = ctrls_new_cl();
205
206
0
  cl->cl_fd = cl_fd;
207
0
  cl->cl_uid = cl_uid;
208
0
  cl->cl_user = pr_auth_uid2name(cl->cl_pool, cl->cl_uid);
209
0
  cl->cl_gid = cl_gid;
210
0
  cl->cl_group = pr_auth_gid2name(cl->cl_pool, cl->cl_gid);
211
0
  cl->cl_pid = cl_pid;
212
0
  cl->cl_flags = cl_flags;
213
214
0
  pr_ctrls_log(MOD_CTRLS_VERSION,
215
0
    "accepted connection from %s/%s client", cl->cl_user, cl->cl_group);
216
217
0
  return cl;
218
0
}
219
220
/* Remove a client from the set */
221
0
static void ctrls_del_cl(pr_ctrls_cl_t *cl) {
222
223
  /* Remove this ctr_cl_t from the list, and free it */
224
0
  if (cl->cl_next) {
225
0
    cl->cl_next->cl_prev = cl->cl_prev;
226
0
  }
227
228
0
  if (cl->cl_prev) {
229
0
    cl->cl_prev->cl_next = cl->cl_next;
230
231
0
  } else {
232
0
    cl_list = cl->cl_next;
233
0
  }
234
235
0
  pr_ctrls_remove_cl(cl);
236
237
0
  (void) close(cl->cl_fd);
238
0
  cl->cl_fd = -1;
239
240
0
  destroy_pool(cl->cl_pool);
241
0
  cl_listlen--;
242
0
}
243
244
/* Controls socket routines
245
 */
246
247
/* Iterate through any readable descriptors, reading each into appropriate
248
 * client objects
249
 */
250
0
static void ctrls_cls_read(void) {
251
0
  pr_ctrls_cl_t *cl;
252
253
0
  cl = cl_list;
254
0
  while (cl != NULL) {
255
0
    int res, xerrno;
256
257
0
    pr_signals_handle();
258
259
0
    res = pr_ctrls_recv_request(cl);
260
0
    xerrno = errno;
261
262
0
    if (res < 0) {
263
0
      switch (xerrno) {
264
0
        case EOF:
265
0
          break;
266
267
0
        case EINVAL:
268
          /* Unsupported action requested */
269
0
          if (cl->cl_flags == 0) {
270
0
            cl->cl_flags = PR_CTRLS_CL_NOACTION;
271
0
          }
272
273
0
          pr_ctrls_log(MOD_CTRLS_VERSION,
274
0
            "recvd from %s/%s client: (invalid action)", cl->cl_user,
275
0
            cl->cl_group);
276
0
          break;
277
278
0
        case EAGAIN:
279
#if defined(EAGAIN) && \
280
    defined(EWOULDBLOCK) && \
281
    EWOULDBLOCK != EAGAIN
282
        case EWOULDBLOCK:
283
#endif
284
          /* Malicious/blocked client */
285
0
          if (cl->cl_flags == 0) {
286
0
            cl->cl_flags = PR_CTRLS_CL_BLOCKED;
287
0
          }
288
0
          break;
289
290
0
        default:
291
0
          pr_ctrls_log(MOD_CTRLS_VERSION,
292
0
            "error: unable to receive client request: %s", strerror(errno));
293
0
          break;
294
0
      }
295
296
0
    } else {
297
0
      pr_ctrls_t *ctrl;
298
0
      char *request;
299
300
0
      ctrl = *((pr_ctrls_t **) cl->cl_ctrls->elts);
301
0
      request = (char *) ctrl->ctrls_action;
302
303
      /* Request successfully read.  Flag this client as being in such a
304
       * state.
305
       */
306
0
      if (cl->cl_flags == 0) {
307
0
        cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
308
0
      }
309
310
0
      if (ctrl->ctrls_cb_args != NULL) {
311
0
        unsigned int reqargc;
312
0
        char **reqargv;
313
314
        /* Reconstruct the original request string from the client for
315
         * logging.
316
         */
317
318
0
        reqargc = ctrl->ctrls_cb_args->nelts;
319
0
        reqargv = ctrl->ctrls_cb_args->elts;
320
321
0
        while (reqargc--) {
322
0
          request = pstrcat(cl->cl_pool, request, " ", *reqargv++, NULL);
323
0
        }
324
325
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
326
0
          "recvd from %s/%s client: '%s'", cl->cl_user, cl->cl_group,
327
0
          request);
328
0
      }
329
0
    }
330
331
0
    cl = cl->cl_next;
332
0
  }
333
0
}
334
335
/* Iterate through any writable descriptors, writing out the responses to the
336
 * appropriate client objects
337
 */
338
0
static int ctrls_cls_write(void) {
339
0
  pr_ctrls_cl_t *cl;
340
341
0
  cl = cl_list;
342
0
  while (cl != NULL) {
343
0
    pr_ctrls_cl_t *tmp_cl;
344
345
    /* Necessary to keep track of the next client in the list while
346
     * the list is being modified.
347
     */
348
0
    tmp_cl = cl->cl_next;
349
350
0
    pr_signals_handle();
351
352
    /* This client has something to hear */
353
0
    if (cl->cl_flags == PR_CTRLS_CL_NOACCESS) {
354
0
      char *msg = "access denied";
355
356
      /* ACL-denied access */
357
0
      if (pr_ctrls_send_response(ctrls_pool, cl->cl_fd,
358
0
          PR_CTRLS_STATUS_ACCESS_DENIED, 1, &msg) < 0) {
359
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
360
0
          "error: unable to send response to %s/%s client: %s",
361
0
          cl->cl_user, cl->cl_group, strerror(errno));
362
363
0
      } else {
364
0
        pr_ctrls_log(MOD_CTRLS_VERSION, "sent to %s/%s client: '%s'",
365
0
          cl->cl_user, cl->cl_group, msg);
366
0
      }
367
368
0
    } else if (cl->cl_flags == PR_CTRLS_CL_NOACTION) {
369
0
      char *msg = "unsupported action requested";
370
371
      /* Unsupported action -- no matching controls */
372
0
      if (pr_ctrls_send_response(ctrls_pool, cl->cl_fd,
373
0
          PR_CTRLS_STATUS_UNSUPPORTED_OPERATION, 1, &msg) < 0) {
374
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
375
0
          "error: unable to send response to %s/%s client: %s",
376
0
          cl->cl_user, cl->cl_group, strerror(errno));
377
378
0
      } else {
379
0
        pr_ctrls_log(MOD_CTRLS_VERSION, "sent to %s/%s client: '%s'",
380
0
          cl->cl_user, cl->cl_group, msg);
381
0
      }
382
383
0
    } else if (cl->cl_flags == PR_CTRLS_CL_BLOCKED) {
384
0
      char *msg = "blocked connection";
385
386
0
      if (pr_ctrls_send_response(ctrls_pool, cl->cl_fd,
387
0
          PR_CTRLS_STATUS_INTERNAL_ERROR, 1, &msg) < 0) {
388
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
389
0
          "error: unable to send response to %s/%s client: %s",
390
0
          cl->cl_user, cl->cl_group, strerror(errno));
391
392
0
      } else {
393
0
        pr_ctrls_log(MOD_CTRLS_VERSION, "sent to %s/%s client: '%s'",
394
0
          cl->cl_user, cl->cl_group, msg);
395
0
      }
396
397
0
    } else if (cl->cl_flags == PR_CTRLS_CL_HAVEREQ) {
398
0
      if (cl->cl_ctrls != NULL &&
399
0
          cl->cl_ctrls->nelts > 0) {
400
0
        register unsigned int i = 0;
401
0
        pr_ctrls_t **ctrlv = NULL;
402
403
0
        ctrlv = (pr_ctrls_t **) cl->cl_ctrls->elts;
404
405
0
        for (i = 0; i < cl->cl_ctrls->nelts; i++) {
406
0
          if ((ctrlv[i])->ctrls_cb_retval < 1) {
407
408
            /* Make sure the callback(s) added responses */
409
0
            if ((ctrlv[i])->ctrls_cb_resps != NULL) {
410
0
              if (pr_ctrls_send_response(ctrls_pool, cl->cl_fd,
411
0
                  (ctrlv[i])->ctrls_cb_retval,
412
0
                  (ctrlv[i])->ctrls_cb_resps->nelts,
413
0
                  (char **) (ctrlv[i])->ctrls_cb_resps->elts) < 0) {
414
0
                pr_ctrls_log(MOD_CTRLS_VERSION,
415
0
                  "error: unable to send response to %s/%s "
416
0
                  "client: %s", cl->cl_user, cl->cl_group, strerror(errno));
417
418
0
              } else {
419
                /* For logging/accounting purposes */
420
0
                register unsigned int j = 0;
421
0
                int respval = (ctrlv[i])->ctrls_cb_retval;
422
0
                unsigned int respargc = (ctrlv[i])->ctrls_cb_resps->nelts;
423
0
                char **respargv = (ctrlv[i])->ctrls_cb_resps->elts;
424
425
0
                pr_ctrls_log(MOD_CTRLS_VERSION,
426
0
                  "sent to %s/%s client: return value: %d",
427
0
                  cl->cl_user, cl->cl_group, respval);
428
429
0
                for (j = 0; j < respargc; j++) {
430
0
                  pr_ctrls_log(MOD_CTRLS_VERSION,
431
0
                    "sent to %s/%s client: '%s'", cl->cl_user, cl->cl_group,
432
0
                    respargv[j]);
433
0
                }
434
0
              }
435
436
0
            } else {
437
              /* No responses added by callbacks */
438
0
              pr_ctrls_log(MOD_CTRLS_VERSION,
439
0
                "notice: no responses given for %s/%s client: "
440
0
                "check controls handlers", cl->cl_user, cl->cl_group);
441
0
            }
442
0
          }
443
0
        }
444
0
      }
445
0
    }
446
447
0
    pr_ctrls_log(MOD_CTRLS_VERSION,
448
0
      "closed connection to %s/%s client", cl->cl_user, cl->cl_group);
449
450
    /* Remove the client from the list */
451
0
    ctrls_del_cl(cl);
452
0
    cl = tmp_cl;
453
0
  }
454
455
0
  return 0;
456
0
}
457
458
/* Create a listening local socket */
459
0
static int ctrls_listen(const char *sock_file) {
460
0
  int sockfd = -1, len = 0;
461
0
  struct sockaddr_un sock;
462
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
463
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
464
  int opt = 1;
465
  socklen_t optlen = sizeof(opt);
466
#endif /* !LOCAL_CREDS */
467
468
  /* No interruptions */
469
0
  pr_signals_block();
470
471
  /* Create the Unix domain socket */
472
0
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
473
0
  if (sockfd < 0) {
474
0
    int xerrno = errno;
475
476
0
    pr_signals_unblock();
477
0
    pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
478
0
      ": error: unable to create local socket: %s", strerror(xerrno));
479
480
0
    errno = xerrno;
481
0
    return -1;
482
0
  }
483
484
  /* Ensure that the socket used is not one of the major three fds (stdin,
485
   * stdout, or stderr).
486
   */
487
0
  if (sockfd <= STDERR_FILENO) {
488
0
    int res;
489
490
0
    res = pr_fs_get_usable_fd(sockfd);
491
0
    if (res < 0) {
492
0
      int xerrno = errno;
493
494
0
      pr_signals_unblock();
495
0
      pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
496
0
        ": error duplicating ctrls socket: %s", strerror(xerrno));
497
0
      (void) close(sockfd);
498
499
0
      errno = xerrno;
500
0
      return -1;
501
0
    }
502
503
0
    (void) close(sockfd);
504
0
    sockfd = res;
505
0
  }
506
507
0
  if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0) {
508
0
    int xerrno = errno;
509
510
0
    pr_signals_unblock();
511
0
    pr_log_pri(PR_LOG_WARNING,
512
0
      "unable to set CLO_EXEC on ctrls socket fd %d: %s", sockfd,
513
0
      strerror(xerrno));
514
515
0
    (void) close(sockfd);
516
0
    errno = xerrno;
517
0
    return -1;
518
0
  }
519
520
  /* Make sure the path to which we want to bind this socket doesn't already
521
   * exist.
522
   */
523
0
  (void) unlink(sock_file);
524
525
  /* Fill in the socket structure fields */
526
0
  memset(&sock, 0, sizeof(sock));
527
528
0
  sock.sun_family = AF_UNIX;
529
0
  sstrncpy(sock.sun_path, sock_file, sizeof(sock.sun_path));
530
531
0
  len = sizeof(sock);
532
533
  /* Bind the name to the descriptor */
534
0
  pr_trace_msg(trace_channel, 1, "binding ctrls socket fd %d to path '%s'",
535
0
    sockfd, sock.sun_path);
536
0
  if (bind(sockfd, (struct sockaddr *) &sock, len) < 0) {
537
0
    int xerrno = errno;
538
539
0
    pr_signals_unblock();
540
0
    (void) close(sockfd);
541
542
0
    errno = xerrno;
543
0
    pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
544
0
      ": error: unable to bind to local socket: %s", strerror(xerrno));
545
0
    pr_trace_msg(trace_channel, 1, "unable to bind to local socket: %s",
546
0
      strerror(xerrno));
547
548
0
    errno = xerrno;
549
0
    return -1;
550
0
  }
551
552
  /* Start listening to the socket */
553
0
  if (listen(sockfd, 5) < 0) {
554
0
    int xerrno = errno;
555
556
0
    pr_signals_unblock();
557
0
    (void) close(sockfd);
558
559
0
    errno = xerrno;
560
0
    pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
561
0
      ": error: unable to listen on local socket '%s': %s", sock.sun_path,
562
0
      strerror(xerrno));
563
0
    pr_trace_msg(trace_channel, 1, "unable to listen on local socket '%s': %s",
564
0
      sock.sun_path, strerror(xerrno));
565
566
0
    errno = xerrno;
567
0
    return -1;
568
0
  }
569
570
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
571
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
572
  /* Set the LOCAL_CREDS socket option. */
573
  if (setsockopt(sockfd, 0, LOCAL_CREDS, &opt, optlen) < 0) {
574
    pr_log_debug(DEBUG0, MOD_CTRLS_VERSION ": error enabling LOCAL_CREDS: %s",
575
      strerror(errno));
576
  }
577
#endif /* !LOCAL_CREDS */
578
579
  /* Change the permissions on the socket, so that users can connect */
580
0
  if (chmod(sock.sun_path, (mode_t) PR_CTRLS_MODE) < 0) {
581
0
    int xerrno = errno;
582
583
0
    pr_signals_unblock();
584
0
    (void) close(sockfd);
585
586
0
    errno = xerrno;
587
0
    pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
588
0
      ": error: unable to chmod local socket: %s", strerror(xerrno));
589
0
    pr_trace_msg(trace_channel, 1, "unable to chmod local socket: %s",
590
0
      strerror(xerrno));
591
592
0
    errno = xerrno;
593
0
    return -1;
594
0
  }
595
596
0
  pr_signals_unblock();
597
0
  return sockfd;
598
0
}
599
600
0
static int ctrls_recv_cl_reqs(void) {
601
0
  fd_set cl_rset;
602
0
  struct timeval timeout;
603
0
  uid_t cl_uid;
604
0
  gid_t cl_gid;
605
0
  pid_t cl_pid;
606
0
  unsigned long cl_flags = 0;
607
0
  int cl_fd, max_fd;
608
609
0
  timeout.tv_usec = 500L;
610
0
  timeout.tv_sec = 0L;
611
612
  /* look for any pending client connections */
613
0
  while (cl_listlen < cl_maxlistlen) {
614
0
    int res = 0, xerrno;
615
616
0
    pr_signals_handle();
617
618
0
    if (ctrls_sockfd < 0) {
619
0
      break;
620
0
    }
621
622
0
    FD_ZERO(&cl_rset);
623
0
    FD_SET(ctrls_sockfd, &cl_rset);
624
0
    max_fd = ctrls_sockfd + 1;
625
626
0
    res = select(max_fd + 1, &cl_rset, NULL, NULL, &timeout);
627
0
    xerrno = errno;
628
629
0
    if (res == 0) {
630
      /* Go through the client list */
631
0
      ctrls_cls_read();
632
633
0
      return 0;
634
0
    }
635
636
0
    if (res < 0) {
637
0
      if (xerrno == EINTR) {
638
0
        pr_signals_handle();
639
0
        continue;
640
0
      }
641
642
0
      pr_ctrls_log(MOD_CTRLS_VERSION,
643
0
        "error: unable to select on local socket: %s", strerror(xerrno));
644
645
0
      errno = xerrno;
646
0
      return res;
647
0
    }
648
649
0
    if (FD_ISSET(ctrls_sockfd, &cl_rset)) {
650
      /* Make sure the ctrl socket is non-blocking */
651
0
      if (ctrls_setnonblock(ctrls_sockfd) < 0) {
652
0
        xerrno = errno;
653
654
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
655
0
          "error: unable to set nonblocking on local socket: %s",
656
0
          strerror(xerrno));
657
658
0
        errno = xerrno;
659
0
        return -1;
660
0
      }
661
662
      /* Accept pending connections */
663
0
      cl_fd = pr_ctrls_accept(ctrls_sockfd, &cl_uid, &cl_gid, &cl_pid,
664
0
        ctrls_cl_freshness);
665
0
      xerrno = errno;
666
667
      /* Restore blocking mode to the ctrl socket */
668
0
      if (ctrls_setblock(ctrls_sockfd) < 0) {
669
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
670
0
          "error: unable to set blocking on local socket: %s",
671
0
          strerror(errno));
672
0
      }
673
674
0
      if (cl_fd < 0) {
675
0
        if (xerrno != ETIMEDOUT) {
676
0
          pr_ctrls_log(MOD_CTRLS_VERSION,
677
0
            "error: unable to accept connection: %s", strerror(xerrno));
678
0
        }
679
680
0
        continue;
681
0
      }
682
683
      /* Set this socket as non-blocking */
684
0
      if (ctrls_setnonblock(cl_fd) < 0) {
685
0
        pr_ctrls_log(MOD_CTRLS_VERSION,
686
0
          "error: unable to set nonblocking on client socket: %s",
687
0
          strerror(errno));
688
0
        (void) close(cl_fd);
689
0
        continue;
690
0
      }
691
692
0
      if (pr_ctrls_check_user_acl(cl_uid, &ctrls_sock_acl.acl_users) != TRUE &&
693
0
          pr_ctrls_check_group_acl(cl_gid, &ctrls_sock_acl.acl_groups) != TRUE) {
694
0
        cl_flags = PR_CTRLS_CL_NOACCESS;
695
0
      }
696
697
      /* Add the client to the list */
698
0
      ctrls_add_cl(cl_fd, cl_uid, cl_gid, cl_pid, cl_flags);
699
0
    }
700
0
  }
701
702
  /* Go through the client list */
703
0
  ctrls_cls_read();
704
705
0
  return 0;
706
0
}
707
708
0
static int ctrls_send_cl_resps(void) {
709
710
  /* Go through the client list */
711
0
  ctrls_cls_write();
712
713
0
  return 0;
714
0
}
715
716
0
static int ctrls_setblock(int sockfd) {
717
0
  int flags = 0;
718
0
  int res = -1;
719
720
  /* default error */
721
0
  errno = EBADF;
722
723
0
  flags = fcntl(sockfd, F_GETFL);
724
0
  res = fcntl(sockfd, F_SETFL, flags & (U32BITS ^ O_NONBLOCK));
725
726
0
  return res;
727
0
}
728
729
0
static int ctrls_setnonblock(int sockfd) {
730
0
  int flags = 0;
731
0
  int res = -1;
732
733
  /* default error */
734
0
  errno = EBADF;
735
736
0
  flags = fcntl(sockfd, F_GETFL);
737
0
  res = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
738
739
0
  return res;
740
0
}
741
742
0
static int ctrls_timer_cb(CALLBACK_FRAME) {
743
0
  static unsigned char first_time = TRUE;
744
745
  /* If the ControlsEngine is not to run, do nothing from here on out */
746
0
  if (ctrls_engine == FALSE) {
747
0
    (void) close(ctrls_sockfd);
748
0
    ctrls_sockfd = -1;
749
750
0
    if (is_master) {
751
      /* Remove the local socket path as well */
752
0
      (void) unlink(ctrls_sock_file);
753
0
    }
754
755
0
    return 0;
756
0
  }
757
758
0
  if (first_time == TRUE) {
759
0
    int res, xerrno;
760
761
    /* Change the ownership on the socket to that configured by the admin. */
762
0
    PRIVS_ROOT
763
0
    res = chown(ctrls_sock_file, ctrls_sock_uid, ctrls_sock_gid);
764
0
    xerrno = errno;
765
0
    PRIVS_RELINQUISH
766
767
0
    if (res < 0) {
768
0
      pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
769
0
        ": unable to chown local socket %s: %s", ctrls_sock_file,
770
0
        strerror(xerrno));
771
0
    }
772
773
0
    first_time = FALSE;
774
0
  }
775
776
  /* Please no alarms while doing this. */
777
0
  pr_alarms_block();
778
779
  /* Process pending requests. */
780
0
  ctrls_recv_cl_reqs();
781
782
  /* Run through the controls */
783
0
  pr_run_ctrls(NULL, NULL);
784
785
  /* Process pending responses */
786
0
  ctrls_send_cl_resps();
787
788
  /* Reset controls */
789
0
  pr_ctrls_reset();
790
791
0
  pr_alarms_unblock();
792
0
  return 1;
793
0
}
794
795
/* Controls handlers
796
 */
797
798
0
static int respcmp(const void *a, const void *b) {
799
0
  return strcmp(*((char **) a), *((char **) b));
800
0
}
801
802
static int ctrls_handle_help(pr_ctrls_t *ctrl, int reqargc,
803
0
    char **reqargv) {
804
805
  /* Run through the list of registered controls, and add them to the
806
   * response, including the module in which they appear.
807
   */
808
809
0
  if (reqargc != 0) {
810
0
    pr_ctrls_add_response(ctrl, "wrong number of parameters");
811
0
    return PR_CTRLS_STATUS_WRONG_PARAMETERS;
812
0
  }
813
814
0
  if (pr_get_registered_actions(ctrl, CTRLS_GET_DESC) < 0) {
815
0
    pr_ctrls_add_response(ctrl, "unable to get actions: %s", strerror(errno));
816
0
    return PR_CTRLS_STATUS_INTERNAL_ERROR;
817
0
  }
818
819
  /* Be nice, and sort the directives lexicographically */
820
0
  qsort(ctrl->ctrls_cb_resps->elts, ctrl->ctrls_cb_resps->nelts,
821
0
    sizeof(char *), respcmp);
822
823
0
  return PR_CTRLS_STATUS_OK;
824
0
}
825
826
static int ctrls_handle_insctrl(pr_ctrls_t *ctrl, int reqargc,
827
0
    char **reqargv) {
828
0
  module *m = ANY_MODULE;
829
0
  int res, xerrno;
830
831
  /* Enable a control into the registered controls list. This requires the
832
   * action and, optionally, the module of the control to be enabled.
833
   */
834
835
  /* Check the insctrl ACL */
836
0
  if (pr_ctrls_check_acl(ctrl, ctrls_acttab, "insctrl") != TRUE) {
837
838
    /* Access denied */
839
0
    pr_ctrls_add_response(ctrl, "access denied");
840
0
    return PR_CTRLS_STATUS_ACCESS_DENIED;
841
0
  }
842
843
0
  if (reqargc < 1 ||
844
0
      reqargc > 2) {
845
0
    pr_ctrls_add_response(ctrl, "wrong number of parameters");
846
0
    return PR_CTRLS_STATUS_WRONG_PARAMETERS;
847
0
  }
848
849
  /* If the optional second parameter, a module name, is used, lookup
850
   * the module pointer matching the name.
851
   */
852
0
  if (reqargc == 2) {
853
0
    m = pr_module_get(reqargv[1]);
854
0
  }
855
856
0
  res = pr_set_registered_actions(m, reqargv[0], FALSE, 0);
857
0
  xerrno = errno;
858
859
0
  if (res < 0) {
860
0
    if (xerrno == ENOENT) {
861
0
      pr_ctrls_add_response(ctrl, "no such control: '%s'", reqargv[0]);
862
0
      return PR_CTRLS_STATUS_SUBJECT_NOT_FOUND;
863
0
    }
864
865
0
    pr_ctrls_add_response(ctrl, "unable to enable '%s': %s", reqargv[0],
866
0
      strerror(errno));
867
0
    return PR_CTRLS_STATUS_INTERNAL_ERROR;
868
0
  }
869
870
0
  pr_ctrls_add_response(ctrl, "'%s' control enabled", reqargv[0]);
871
0
  return PR_CTRLS_STATUS_OK;
872
0
}
873
874
static int ctrls_handle_lsctrl(pr_ctrls_t *ctrl, int reqargc,
875
0
    char **reqargv) {
876
877
  /* Run through the list of registered controls, and add them to the
878
   * response, including the module in which they appear.
879
   */
880
881
  /* Check the lsctrl ACL */
882
0
  if (pr_ctrls_check_acl(ctrl, ctrls_acttab, "lsctrl") != TRUE) {
883
884
    /* Access denied */
885
0
    pr_ctrls_add_response(ctrl, "access denied");
886
0
    return PR_CTRLS_STATUS_ACCESS_DENIED;
887
0
  }
888
889
0
  if (reqargc != 0) {
890
0
    pr_ctrls_add_response(ctrl, "wrong number of parameters");
891
0
    return PR_CTRLS_STATUS_WRONG_PARAMETERS;
892
0
  }
893
894
0
  if (pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ENABLED) < 0) {
895
0
    pr_ctrls_add_response(ctrl, "unable to get actions: %s", strerror(errno));
896
0
    return PR_CTRLS_STATUS_INTERNAL_ERROR;
897
0
  }
898
899
  /* Be nice, and sort the actions lexicographically */
900
0
  qsort(ctrl->ctrls_cb_resps->elts, ctrl->ctrls_cb_resps->nelts,
901
0
    sizeof(char *), respcmp);
902
903
0
  return PR_CTRLS_STATUS_OK;
904
0
}
905
906
static int ctrls_handle_rmctrl(pr_ctrls_t *ctrl, int reqargc,
907
0
    char **reqargv) {
908
0
  module *m = ANY_MODULE;
909
0
  int res, xerrno;
910
911
  /* Disable a control from the registered controls list. This requires the
912
   * action and, optionally, the module of the control to be removed.
913
   */
914
915
  /* Check the rmctrl ACL */
916
0
  if (pr_ctrls_check_acl(ctrl, ctrls_acttab, "rmctrl") != TRUE) {
917
918
    /* Access denied */
919
0
    pr_ctrls_add_response(ctrl, "access denied");
920
0
    return PR_CTRLS_STATUS_ACCESS_DENIED;
921
0
  }
922
923
0
  if (reqargc < 1 ||
924
0
      reqargc > 2) {
925
0
    pr_ctrls_add_response(ctrl, "wrong number of parameters");
926
0
    return PR_CTRLS_STATUS_WRONG_PARAMETERS;
927
0
  }
928
929
  /* The three controls added by this module _cannot_ be removed (at least
930
   * not via this control handler).
931
   */
932
0
  if (strcmp(reqargv[0], "insctrl") == 0 ||
933
0
      strcmp(reqargv[0], "lsctrl") == 0 ||
934
0
      strcmp(reqargv[0], "rmctrl") == 0) {
935
0
    pr_ctrls_add_response(ctrl, "'%s' control cannot be removed", reqargv[0]);
936
0
    return PR_CTRLS_STATUS_OPERATION_DENIED;
937
0
  }
938
939
  /* If the optional second parameter, a module name, is used, lookup
940
   * the module pointer matching the name.
941
   */
942
0
  if (reqargc == 2) {
943
0
    m = pr_module_get(reqargv[1]);
944
0
  }
945
946
0
  res = pr_set_registered_actions(m, reqargv[0], FALSE, PR_CTRLS_ACT_DISABLED);
947
0
  xerrno = errno;
948
949
0
  if (res < 0) {
950
0
    if (xerrno == ENOENT) {
951
0
      pr_ctrls_add_response(ctrl, "no such control: '%s'", reqargv[0]);
952
0
      return PR_CTRLS_STATUS_SUBJECT_NOT_FOUND;
953
0
    }
954
955
0
    pr_ctrls_add_response(ctrl, "unable to disable '%s': %s", reqargv[0],
956
0
      strerror(xerrno));
957
0
    return PR_CTRLS_STATUS_INTERNAL_ERROR;
958
0
  }
959
960
0
  if (strcmp(reqargv[0], "all") != 0) {
961
0
    pr_ctrls_add_response(ctrl, "'%s' control disabled", reqargv[0]);
962
963
0
  } else {
964
    /* If all actions have been disabled, stop listening on the local
965
     * socket, and turn off this module's engine.
966
     */
967
0
    pr_ctrls_add_response(ctrl, "all controls disabled");
968
0
    pr_ctrls_add_response(ctrl, "restart the daemon to re-enable controls");
969
970
0
    (void) close(ctrls_sockfd);
971
0
    ctrls_sockfd = -1;
972
973
0
    ctrls_engine = FALSE;
974
0
  }
975
976
0
  return PR_CTRLS_STATUS_OK;
977
0
}
978
979
/* Configuration handlers
980
 */
981
982
/* Default behavior is to deny everyone unless an ACL has been configured */
983
0
MODRET set_ctrlsacls(cmd_rec *cmd) {
984
0
  int res;
985
0
  char **actions = NULL;
986
0
  const char *bad_action = NULL;
987
988
0
  CHECK_ARGS(cmd, 4);
989
0
  CHECK_CONF(cmd, CONF_ROOT);
990
991
  /* Parse the given string of actions into a char **.  Then iterate
992
   * through the acttab, checking to see if a given control is _not_ in
993
   * the list.  If not in the list, unregister that control.
994
   */
995
996
0
  actions = pr_ctrls_parse_acl(cmd->tmp_pool, cmd->argv[1]);
997
998
  /* Check the second parameter to make sure it is "allow" or "deny" */
999
0
  if (strcmp(cmd->argv[2], "allow") != 0 &&
1000
0
      strcmp(cmd->argv[2], "deny") != 0) {
1001
0
    CONF_ERROR(cmd, "second parameter must be 'allow' or 'deny'");
1002
0
  }
1003
1004
  /* Check the third parameter to make sure it is "user" or "group" */
1005
0
  if (strcmp(cmd->argv[3], "user") != 0 &&
1006
0
      strcmp(cmd->argv[3], "group") != 0) {
1007
0
    CONF_ERROR(cmd, "third parameter must be 'user' or 'group'");
1008
0
  }
1009
1010
0
  res = pr_ctrls_set_module_acls2(ctrls_acttab, ctrls_pool, actions,
1011
0
    cmd->argv[2], cmd->argv[3], cmd->argv[4], &bad_action);
1012
0
  if (res < 0) {
1013
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown action: '",
1014
0
      bad_action, "'", NULL));
1015
0
  }
1016
1017
0
  return PR_HANDLED(cmd);
1018
0
}
1019
1020
/* default: 10 secs */
1021
0
MODRET set_ctrlsauthfreshness(cmd_rec *cmd) {
1022
0
  int freshness = 0;
1023
1024
0
  CHECK_ARGS(cmd, 1);
1025
0
  CHECK_CONF(cmd, CONF_ROOT);
1026
1027
0
  freshness = atoi(cmd->argv[1]);
1028
0
  if (freshness <= 0) {
1029
0
    CONF_ERROR(cmd, "must be a positive number");
1030
0
  }
1031
1032
0
  ctrls_cl_freshness = freshness;
1033
0
  return PR_HANDLED(cmd);
1034
0
}
1035
1036
0
MODRET set_ctrlsengine(cmd_rec *cmd) {
1037
0
  int engine = -1;
1038
1039
0
  CHECK_ARGS(cmd, 1);
1040
0
  CHECK_CONF(cmd, CONF_ROOT);
1041
1042
0
  engine = get_boolean(cmd, 1);
1043
0
  if (engine == -1) {
1044
0
    CONF_ERROR(cmd, "expected Boolean parameter");
1045
0
  }
1046
1047
0
  ctrls_engine = engine;
1048
0
  return PR_HANDLED(cmd);
1049
0
}
1050
1051
/* default: 10 secs */
1052
0
MODRET set_ctrlsinterval(cmd_rec *cmd) {
1053
0
  int nsecs = 0;
1054
1055
0
  CHECK_ARGS(cmd, 1);
1056
0
  CHECK_CONF(cmd, CONF_ROOT);
1057
1058
0
  nsecs = atoi(cmd->argv[1]);
1059
0
  if (nsecs <= 0) {
1060
0
    CONF_ERROR(cmd, "must be a positive number");
1061
0
  }
1062
1063
  /* Remove the existing timer, and re-install it with this new interval. */
1064
0
  ctrls_interval = nsecs;
1065
1066
0
  pr_timer_remove(CTRLS_TIMER_ID, &ctrls_module);
1067
0
  pr_timer_add(ctrls_interval, CTRLS_TIMER_ID, &ctrls_module, ctrls_timer_cb,
1068
0
    "Controls polling");
1069
1070
0
  return PR_HANDLED(cmd);
1071
0
}
1072
1073
0
MODRET set_ctrlslog(cmd_rec *cmd) {
1074
0
  int res = 0;
1075
1076
0
  CHECK_ARGS(cmd, 1);
1077
0
  CHECK_CONF(cmd, CONF_ROOT);
1078
1079
0
  ctrls_logname = pstrdup(ctrls_pool, cmd->argv[1]);
1080
1081
0
  res = ctrls_openlog();
1082
0
  if (res < 0) {
1083
0
    if (res == -1) {
1084
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to open '",
1085
0
        (char *) cmd->argv[1], "': ", strerror(errno), NULL));
1086
0
    }
1087
1088
0
    if (res == -2) {
1089
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1090
0
        "unable to log to a world-writable directory", NULL));
1091
0
    }
1092
0
  }
1093
1094
0
  return PR_HANDLED(cmd);
1095
0
}
1096
1097
/* Default: 5 max clients */
1098
0
MODRET set_ctrlsmaxclients(cmd_rec *cmd) {
1099
0
  int nclients = 0;
1100
1101
0
  CHECK_ARGS(cmd, 1);
1102
0
  CHECK_CONF(cmd, CONF_ROOT);
1103
1104
0
  nclients = atoi(cmd->argv[1]);
1105
0
  if (nclients <= 0) {
1106
0
    CONF_ERROR(cmd, "must be a positive number");
1107
0
  }
1108
1109
0
  cl_maxlistlen = nclients;
1110
0
  return PR_HANDLED(cmd);
1111
0
}
1112
1113
/* Default: var/run/proftpd.sock */
1114
0
MODRET set_ctrlssocket(cmd_rec *cmd) {
1115
0
  char *path;
1116
1117
0
  CHECK_ARGS(cmd, 1);
1118
0
  CHECK_CONF(cmd, CONF_ROOT);
1119
1120
0
  path = cmd->argv[1];
1121
0
  if (*path != '/') {
1122
0
    CONF_ERROR(cmd, "must be an absolute path");
1123
0
  }
1124
1125
  /* Close the socket. */
1126
0
  if (ctrls_sockfd >= 0) {
1127
0
    pr_trace_msg(trace_channel, 3, "closing ctrls socket '%s' (fd %d)",
1128
0
      ctrls_sock_file, ctrls_sockfd);
1129
0
    (void) unlink(ctrls_sock_file);
1130
0
    (void) close(ctrls_sockfd);
1131
0
    ctrls_sockfd = -1;
1132
0
  }
1133
1134
  /* Change the path. */
1135
0
  if (strcmp(path, ctrls_sock_file) != 0) {
1136
0
    ctrls_sock_file = pstrdup(ctrls_pool, path);
1137
0
  }
1138
1139
0
  return PR_HANDLED(cmd);
1140
0
}
1141
1142
/* Default behavior is to deny everyone unless an ACL has been configured */
1143
0
MODRET set_ctrlssocketacl(cmd_rec *cmd) {
1144
0
  CHECK_ARGS(cmd, 3);
1145
0
  CHECK_CONF(cmd, CONF_ROOT);
1146
1147
0
  pr_ctrls_init_acl(&ctrls_sock_acl);
1148
1149
  /* Check the first argument to make sure it either "allow" or "deny" */
1150
0
  if (strcasecmp(cmd->argv[1], "allow") != 0 &&
1151
0
      strcasecmp(cmd->argv[1], "deny") != 0) {
1152
0
    CONF_ERROR(cmd, "first parameter must be either 'allow' or 'deny'");
1153
0
  }
1154
1155
  /* Check the second argument to see how to handle the directive */
1156
0
  if (strcasecmp(cmd->argv[2], "user") == 0) {
1157
0
    if (pr_ctrls_set_user_acl(ctrls_pool, &ctrls_sock_acl.acl_users,
1158
0
        cmd->argv[1], cmd->argv[3]) < 0) {
1159
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1160
0
        "error configuring user socket ACL: ", strerror(errno), NULL));
1161
0
    }
1162
1163
0
  } else if (strcasecmp(cmd->argv[2], "group") == 0) {
1164
0
    if (pr_ctrls_set_group_acl(ctrls_pool, &ctrls_sock_acl.acl_groups,
1165
0
        cmd->argv[1], cmd->argv[3]) < 0) {
1166
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1167
0
        "error configuring group socket ACL: ", strerror(errno), NULL));
1168
0
    }
1169
1170
0
  } else {
1171
0
    CONF_ERROR(cmd, "second parameter must be either 'user' or 'group'");
1172
0
  }
1173
1174
0
  return PR_HANDLED(cmd);
1175
0
}
1176
1177
/* Default: root root */
1178
0
MODRET set_ctrlssocketowner(cmd_rec *cmd) {
1179
0
  gid_t gid = 0;
1180
0
  uid_t uid = 0;
1181
1182
0
  CHECK_ARGS(cmd, 2);
1183
0
  CHECK_CONF(cmd, CONF_ROOT);
1184
1185
0
  uid = pr_auth_name2uid(cmd->tmp_pool, cmd->argv[1]);
1186
0
  if (uid == (uid_t) -1) {
1187
0
    if (errno != EINVAL) {
1188
0
      pr_log_debug(DEBUG0, "%s: %s has UID of -1", (char *) cmd->argv[0],
1189
0
        (char *) cmd->argv[1]);
1190
1191
0
    } else {
1192
0
      pr_log_debug(DEBUG0, "%s: no such user '%s'", (char *) cmd->argv[0],
1193
0
        (char *) cmd->argv[1]);
1194
0
    }
1195
1196
0
  } else {
1197
0
    ctrls_sock_uid = uid;
1198
0
  }
1199
1200
0
  gid = pr_auth_name2gid(cmd->tmp_pool, cmd->argv[2]);
1201
0
  if (gid == (gid_t) -1) {
1202
0
    if (errno != EINVAL) {
1203
0
      pr_log_debug(DEBUG0, "%s: %s has GID of -1", (char *) cmd->argv[0],
1204
0
        (char *) cmd->argv[2]);
1205
1206
0
    } else {
1207
0
      pr_log_debug(DEBUG0, "%s: no such group '%s'", (char *) cmd->argv[0],
1208
0
        (char *) cmd->argv[2]);
1209
0
    }
1210
1211
0
  } else {
1212
0
    ctrls_sock_gid = gid;
1213
0
  }
1214
1215
0
  return PR_HANDLED(cmd);
1216
0
}
1217
1218
/* Event handlers
1219
 */
1220
1221
0
static void ctrls_shutdown_ev(const void *event_data, void *user_data) {
1222
0
  if (!is_master || !ctrls_engine)
1223
0
    return;
1224
1225
  /* Close any connected clients */
1226
0
  if (cl_list != NULL) {
1227
0
    pr_ctrls_cl_t *cl = NULL;
1228
1229
0
    for (cl = cl_list; cl; cl = cl->cl_next) {
1230
0
      if (cl->cl_fd >= 0) {
1231
0
        (void) close(cl->cl_fd);
1232
0
        cl->cl_fd = -1;
1233
0
      }
1234
0
    }
1235
0
  }
1236
1237
0
  (void) close(ctrls_sockfd);
1238
0
  ctrls_sockfd = -1;
1239
1240
  /* Remove the local socket path as well */
1241
0
  (void) unlink(ctrls_sock_file);
1242
0
  return;
1243
0
}
1244
1245
0
static void ctrls_postparse_ev(const void *event_data, void *user_data) {
1246
0
  if (ctrls_engine == FALSE ||
1247
0
      ServerType == SERVER_INETD) {
1248
0
    return;
1249
0
  }
1250
1251
  /* Start listening on the ctrl socket */
1252
0
  PRIVS_ROOT
1253
0
  ctrls_sockfd = ctrls_listen(ctrls_sock_file);
1254
0
  PRIVS_RELINQUISH
1255
1256
  /* Start a timer for the checking/processing of the ctrl socket.  */
1257
0
  pr_timer_remove(CTRLS_TIMER_ID, &ctrls_module);
1258
0
  pr_timer_add(ctrls_interval, CTRLS_TIMER_ID, &ctrls_module, ctrls_timer_cb,
1259
0
    "Controls polling");
1260
0
}
1261
1262
0
static void ctrls_restart_ev(const void *event_data, void *user_data) {
1263
0
  register unsigned int i;
1264
1265
  /* Block alarms while we're preparing for the restart. */
1266
0
  pr_alarms_block();
1267
1268
  /* Close any connected clients */
1269
0
  if (cl_list != NULL) {
1270
0
    pr_ctrls_cl_t *cl = NULL;
1271
1272
0
    for (cl = cl_list; cl; cl = cl->cl_next) {
1273
0
      if (cl->cl_fd >= 0) {
1274
0
        (void) close(cl->cl_fd);
1275
0
        cl->cl_fd = -1;
1276
0
      }
1277
0
    }
1278
0
  }
1279
1280
  /* Reset the client list */
1281
0
  cl_list = NULL;
1282
0
  cl_listlen = 0;
1283
1284
0
  pr_trace_msg(trace_channel, 3, "closing ctrls socket '%s' (fd %d)",
1285
0
    ctrls_sock_file, ctrls_sockfd);
1286
0
  (void) close(ctrls_sockfd);
1287
0
  ctrls_sockfd = -1;
1288
1289
0
  ctrls_closelog();
1290
1291
  /* Clear the existing pool */
1292
0
  if (ctrls_pool != NULL) {
1293
0
    destroy_pool(ctrls_pool);
1294
1295
0
    ctrls_logname = NULL;
1296
0
    ctrls_sock_file = MOD_CTRLS_DEFAULT_SOCK;
1297
0
  }
1298
1299
  /* Allocate the pool for this module's use */
1300
0
  ctrls_pool = make_sub_pool(permanent_pool);
1301
0
  pr_pool_tag(ctrls_pool, MOD_CTRLS_VERSION);
1302
1303
  /* Register the control handlers */
1304
0
  for (i = 0; ctrls_acttab[i].act_action; i++) {
1305
1306
    /* Allocate and initialize the ACL for this control. */
1307
0
    ctrls_acttab[i].act_acl = pcalloc(ctrls_pool, sizeof(ctrls_acl_t));
1308
0
    pr_ctrls_init_acl(ctrls_acttab[i].act_acl);
1309
0
  }
1310
1311
0
  pr_timer_remove(CTRLS_TIMER_ID, &ctrls_module);
1312
0
  pr_alarms_unblock();
1313
0
  return;
1314
0
}
1315
1316
/* Initialization routines
1317
 */
1318
1319
0
static int ctrls_init(void) {
1320
0
  register unsigned int i = 0;
1321
1322
  /* Allocate the pool for this module's use */
1323
0
  ctrls_pool = make_sub_pool(permanent_pool);
1324
0
  pr_pool_tag(ctrls_pool, MOD_CTRLS_VERSION);
1325
1326
  /* Register the control handlers */
1327
0
  for (i = 0; ctrls_acttab[i].act_action; i++) {
1328
1329
    /* Allocate and initialize the ACL for this control. */
1330
0
    ctrls_acttab[i].act_acl = pcalloc(ctrls_pool, sizeof(ctrls_acl_t));
1331
0
    pr_ctrls_init_acl(ctrls_acttab[i].act_acl);
1332
1333
0
    if (pr_ctrls_register(&ctrls_module, ctrls_acttab[i].act_action,
1334
0
        ctrls_acttab[i].act_desc, ctrls_acttab[i].act_cb) < 0) {
1335
0
      pr_log_pri(PR_LOG_NOTICE, MOD_CTRLS_VERSION
1336
0
        ": error registering '%s' control: %s",
1337
0
        ctrls_acttab[i].act_action, strerror(errno));
1338
0
    }
1339
0
  }
1340
1341
  /* Make certain the socket ACL is initialized. */
1342
0
  pr_ctrls_init_acl(&ctrls_sock_acl);
1343
1344
0
  pr_event_register(&ctrls_module, "core.restart", ctrls_restart_ev, NULL);
1345
0
  pr_event_register(&ctrls_module, "core.shutdown", ctrls_shutdown_ev, NULL);
1346
0
  pr_event_register(&ctrls_module, "core.postparse", ctrls_postparse_ev, NULL);
1347
1348
0
  return 0;
1349
0
}
1350
1351
0
static int ctrls_sess_init(void) {
1352
1353
  /* Children are not to listen for or handle control requests */
1354
0
  ctrls_engine = FALSE;
1355
0
  pr_timer_remove(CTRLS_TIMER_ID, &ctrls_module);
1356
1357
0
  pr_event_unregister(&ctrls_module, "core.restart", ctrls_restart_ev);
1358
1359
  /* Close the inherited socket */
1360
0
  close(ctrls_sockfd);
1361
0
  ctrls_sockfd = -1;
1362
1363
0
  return 0;
1364
0
}
1365
1366
static ctrls_acttab_t ctrls_acttab[] = {
1367
  { "help", "describe all registered controls", NULL,
1368
    ctrls_handle_help },
1369
  { "insctrl",  "enable a disabled control", NULL,
1370
    ctrls_handle_insctrl },
1371
  { "lsctrl", "list all registered controls", NULL,
1372
    ctrls_handle_lsctrl },
1373
  { "rmctrl", "disable a registered control", NULL,
1374
    ctrls_handle_rmctrl },
1375
  { NULL, NULL, NULL, NULL }
1376
};
1377
1378
/* Module API tables
1379
 */
1380
1381
static conftable ctrls_conftab[] = {
1382
  { "ControlsACLs",   set_ctrlsacls,    NULL },
1383
  { "ControlsAuthFreshness",  set_ctrlsauthfreshness, NULL },
1384
  { "ControlsEngine",   set_ctrlsengine,  NULL },
1385
  { "ControlsInterval",   set_ctrlsinterval,  NULL },
1386
  { "ControlsLog",    set_ctrlslog,   NULL },
1387
  { "ControlsMaxClients", set_ctrlsmaxclients,  NULL },
1388
  { "ControlsSocket",   set_ctrlssocket,  NULL },
1389
  { "ControlsSocketACL",  set_ctrlssocketacl, NULL },
1390
  { "ControlsSocketOwner",  set_ctrlssocketowner, NULL },
1391
  { NULL }
1392
};
1393
1394
module ctrls_module = {
1395
  NULL, NULL,
1396
1397
  /* Module API version 2.0 */
1398
  0x20,
1399
1400
  /* Module name */
1401
  "ctrls",
1402
1403
  /* Module configuration handler table */
1404
  ctrls_conftab,
1405
1406
  /* Module command handler table */
1407
  NULL,
1408
1409
  /* Module authentication handler table */
1410
  NULL,
1411
1412
  /* Module initialization function */
1413
  ctrls_init,
1414
1415
  /* Session initialization function */
1416
  ctrls_sess_init,
1417
1418
  /* Module version */
1419
  MOD_CTRLS_VERSION
1420
};