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

proftpd / proftpd / 26182518137

20 May 2026 06:49PM UTC coverage: 93.024% (+0.4%) from 92.635%
26182518137

push

github

51329 of 55178 relevant lines covered (93.02%)

226.63 hits per line

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

80.91
/src/ctrls.c
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 2001-2026 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, see <https://www.gnu.org/licenses/>.
17
 *
18
 * As a special exemption, The ProFTPD Project team and other respective
19
 * copyright holders give permission to link this program with OpenSSL, and
20
 * distribute the resulting executable, without including the source code for
21
 * OpenSSL in the source distribution.
22
 */
23

24
/* Controls API routines */
25

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

29
#if defined(HAVE_UCRED_H)
30
# include <ucred.h>
31
#endif /* !HAVE_UCRED_H */
32

33
#if defined(HAVE_SYS_UCRED_H)
34
# include <sys/ucred.h>
35
#endif /* !HAVE_SYS_UCRED_H */
36

37
#if defined(HAVE_SYS_UIO_H)
38
# include <sys/uio.h>
39
#endif /* !HAVE_SYS_UIO_H */
40

41
#if defined(PR_USE_CTRLS)
42

43
#include "mod_ctrls.h"
44

45
#define CTRLS_MAX_REQ_SIZE        1024
46
#define CTRLS_MAX_RESP_SIZE        (1024 * 1024)
47

48
#define CTRLS_REQ_ACTION_KEY        "action"
49
#define CTRLS_REQ_ARGS_KEY        "args"
50
#define CTRLS_RESP_STATUS_KEY        "status"
51
#define CTRLS_RESP_RESPS_KEY        "responses"
52

53
typedef struct ctrls_act_obj {
54
  struct ctrls_act_obj *prev, *next;
55
  pool *pool;
56
  unsigned int id;
57
  const char *action;
58
  const char *desc;
59
  const module *module;
60
  volatile unsigned int flags;
61
  int (*action_cb)(pr_ctrls_t *, int, char **);
62
} ctrls_action_t;
63

64
static unsigned char ctrls_blocked = FALSE;
65

66
static pool *ctrls_pool = NULL;
67
static ctrls_action_t *ctrls_action_list = NULL;
68

69
static pr_ctrls_t *ctrls_active_list = NULL;
70
static pr_ctrls_t *ctrls_free_list = NULL;
71

72
static int ctrls_use_isfifo = FALSE;
73

74
static const char *trace_channel = "ctrls";
75

76
/* lookup/lookup_next indices */
77
static ctrls_action_t *action_lookup_next = NULL;
78
static const char *action_lookup_action = NULL;
79
static module *action_lookup_module = NULL;
80

81
/* Logging */
82
static int ctrls_logfd = -1;
83

84
/* necessary prototypes */
85
static ctrls_action_t *ctrls_action_new(void);
86
static pr_ctrls_t *ctrls_lookup_action(module *, const char *, unsigned char);
87
static pr_ctrls_t *ctrls_lookup_next_action(module *, unsigned char);
7✔
88

7✔
89
static pr_ctrls_t *ctrls_prepare(ctrls_action_t *act) {
90
  pr_ctrls_t *ctrl = NULL;
7✔
91

92
  pr_block_ctrls();
93

14✔
94
  /* Get a blank ctrl object */
95
  ctrl = pr_ctrls_alloc();
96

7✔
97
  /* Fill in the fields from the action object. */
7✔
98
  ctrl->ctrls_id = act->id;
7✔
99
  ctrl->ctrls_module = act->module;
7✔
100
  ctrl->ctrls_action = act->action;
7✔
101
  ctrl->ctrls_desc = act->desc;
7✔
102
  ctrl->ctrls_cb = act->action_cb;
103
  ctrl->ctrls_flags = act->flags;
104

7✔
105
  /* Add this to the "in use" list */
7✔
106
  ctrl->ctrls_next = ctrls_active_list;
107
  ctrls_active_list = ctrl;
7✔
108

7✔
109
  pr_unblock_ctrls();
110
  return ctrl;
111
}
12✔
112

12✔
113
static ctrls_action_t *ctrls_action_new(void) {
12✔
114
  ctrls_action_t *act = NULL;
115
  pool *sub_pool = NULL;
12✔
116

12✔
117
  sub_pool = make_sub_pool(ctrls_pool);
118
  pr_pool_tag(sub_pool, "ctrls action subpool");
12✔
119

12✔
120
  act = pcalloc(sub_pool, sizeof(ctrls_action_t));
121
  act->pool = sub_pool;
12✔
122

123
  return act;
124
}
27✔
125

27✔
126
pr_ctrls_t *pr_ctrls_alloc(void) {
127
  pr_ctrls_t *ctrl = NULL;
128

27✔
129
  /* Check for a free ctrl first */
130
  if (ctrls_free_list != NULL) {
131

5✔
132
    /* Take one from the top */
5✔
133
    ctrl = ctrls_free_list;
134
    ctrls_free_list = ctrls_free_list->ctrls_next;
5✔
135

2✔
136
    if (ctrls_free_list != NULL) {
137
      ctrls_free_list->ctrls_prev = NULL;
138
    }
139

140
  } else {
22✔
141
    /* Have to allocate a new one. */
142
    ctrl = (pr_ctrls_t *) pcalloc(ctrls_pool, sizeof(pr_ctrls_t));
143

144
    /* It's important that a new ctrl object have the retval initialized
145
     * to 1; this tells the Controls layer that it is "pending", not yet
146
     * handled.
22✔
147
     */
148
    ctrl->ctrls_cb_retval = PR_CTRLS_STATUS_PENDING;
149
  }
27✔
150

151
  return ctrl;
152
}
9✔
153

9✔
154
int pr_ctrls_free(pr_ctrls_t *ctrl) {
1✔
155
  if (ctrl == NULL) {
1✔
156
    errno = EINVAL;
157
    return -1;
158
  }
159

8✔
160
  /* Make sure that ctrls are blocked while we're doing this */
161
  pr_block_ctrls();
162

8✔
163
  /* Remove this object from the active list */
×
164
  if (ctrl->ctrls_prev != NULL) {
165
    ctrl->ctrls_prev->ctrls_next = ctrl->ctrls_next;
166

8✔
167
  } else {
168
    ctrls_active_list = ctrl->ctrls_next;
169
  }
8✔
170

2✔
171
  if (ctrl->ctrls_next != NULL) {
172
    ctrl->ctrls_next->ctrls_prev = ctrl->ctrls_prev;
173
  }
174

8✔
175
  /* Clear its fields, and add it to the free list */
8✔
176
  ctrl->ctrls_next = NULL;
8✔
177
  ctrl->ctrls_prev = NULL;
8✔
178
  ctrl->ctrls_id = 0;
8✔
179
  ctrl->ctrls_module = NULL;
8✔
180
  ctrl->ctrls_action = NULL;
8✔
181
  ctrl->ctrls_cb = NULL;
8✔
182
  ctrl->ctrls_cb_retval = PR_CTRLS_STATUS_PENDING;
183
  ctrl->ctrls_flags = 0;
8✔
184

4✔
185
  if (ctrl->ctrls_tmp_pool != NULL) {
4✔
186
    destroy_pool(ctrl->ctrls_tmp_pool);
187
    ctrl->ctrls_tmp_pool = NULL;
188
  }
8✔
189

8✔
190
  ctrl->ctrls_cb_args = NULL;
8✔
191
  ctrl->ctrls_cb_resps = NULL;
192
  ctrl->ctrls_data = NULL;
8✔
193

8✔
194
  ctrl->ctrls_next = ctrls_free_list;
195
  ctrls_free_list = ctrl;
8✔
196

8✔
197
  pr_unblock_ctrls();
198
  return 0;
199
}
15✔
200

201
int pr_ctrls_register(const module *mod, const char *action,
15✔
202
    const char *desc, int (*cb)(pr_ctrls_t *, int, char **)) {
15✔
203
  ctrls_action_t *act = NULL, *acti = NULL;
204
  unsigned int act_id = 0;
205

15✔
206
  /* sanity checks */
15✔
207
  if (action == NULL ||
208
      desc == NULL ||
3✔
209
      cb == NULL) {
3✔
210
    errno = EINVAL;
211
    return -1;
212
  }
12✔
213

214
  pr_trace_msg("ctrls", 3,
215
    "module '%s' registering handler for ctrl action '%s' (at %p)",
216
    mod ? mod->name : "(none)", action, cb);
217

12✔
218
  /* Block ctrls while we're doing this */
219
  pr_block_ctrls();
220

12✔
221
  /* Get a ctrl action object */
222
  act = ctrls_action_new();
223

12✔
224
  /* Randomly generate a unique random ID for this object */
12✔
225
  while (TRUE) {
226
    int have_id = FALSE;
12✔
227

228
    act_id = (unsigned int) pr_random_next(1L, RAND_MAX);
229

16✔
230
    /* Check the list for this ID */
4✔
231
    for (acti = ctrls_action_list; acti; acti = acti->next) {
232
      if (acti->id == act_id) {
233
        have_id = TRUE;
234
        break;
235
      }
236
    }
12✔
237

238
    if (have_id == FALSE) {
239
      break;
240
    }
241
  }
12✔
242

12✔
243
  act->next = NULL;
12✔
244
  act->id = act_id;
12✔
245
  act->action = pstrdup(ctrls_pool, action);
12✔
246
  act->desc = desc;
12✔
247
  act->module = mod;
248
  act->action_cb = cb;
249

250
  /* Add this to the list of "registered" actions */
12✔
251

4✔
252
  if (ctrls_action_list != NULL) {
4✔
253
    act->next = ctrls_action_list;
254
    ctrls_action_list->prev = act;
255
  }
12✔
256

257
  ctrls_action_list = act;
12✔
258

12✔
259
  pr_unblock_ctrls();
260
  return act_id;
261
}
15✔
262

15✔
263
int pr_ctrls_unregister(module *mod, const char *action) {
15✔
264
  ctrls_action_t *act = NULL, *next_act = NULL;
265
  unsigned char have_action = FALSE;
266

15✔
267
  /* Make sure that ctrls are blocked while we're doing this */
268
  pr_block_ctrls();
27✔
269

12✔
270
  for (act = ctrls_action_list; act != NULL; act = next_act) {
271
    next_act = act->next;
12✔
272

12✔
273
    if ((action == NULL || strcmp(act->action, action) == 0) &&
12✔
274
        (act->module == mod || mod == ANY_MODULE || mod == NULL)) {
275
      have_action = TRUE;
276

12✔
277
      /* Remove this object from the list of registered actions */
×
278
      if (act->prev != NULL) {
279
        act->prev->next = act->next;
280

12✔
281
      } else {
282
        ctrls_action_list = act->next;
283
      }
12✔
284

4✔
285
      if (act->next != NULL) {
286
        act->next->prev = act->prev;
287
      }
12✔
288

289
      pr_trace_msg("ctrls", 3,
290
        "module '%s' unregistering handler for ctrl action '%s'",
291
        mod ? mod->name : "(none)", act->action);
292

12✔
293
      /* Destroy this action. */
294
      destroy_pool(act->pool);
295
    }
296
  }
15✔
297

298
  pr_unblock_ctrls();
15✔
299

7✔
300
  if (have_action == FALSE) {
7✔
301
    errno = ENOENT;
302
    return -1;
303
  }
304

305
  return 0;
306
}
11✔
307

11✔
308
int pr_ctrls_add_arg(pr_ctrls_t *ctrl, char *ctrls_arg, size_t ctrls_arglen) {
309
  register unsigned int i;
310

11✔
311
  /* Sanity checks */
11✔
312
  if (ctrl == NULL ||
2✔
313
      ctrls_arg == NULL) {
2✔
314
    errno = EINVAL;
315
    return -1;
316
  }
317

35✔
318
  /* Scan for non-printable characters. */
27✔
319
  for (i = 0; i < ctrls_arglen; i++) {
1✔
320
    if (!PR_ISPRINT((int) ctrls_arg[i])) {
1✔
321
      errno = EPERM;
322
      return -1;
323
    }
324
  }
325

326
  /* Make sure the pr_ctrls_t has a temporary pool, from which the args will
327
   * be allocated.
8✔
328
   */
6✔
329
  if (ctrl->ctrls_tmp_pool == NULL) {
6✔
330
    ctrl->ctrls_tmp_pool = make_sub_pool(ctrls_pool);
331
    pr_pool_tag(ctrl->ctrls_tmp_pool, "ctrls tmp pool");
332
  }
8✔
333

6✔
334
  if (ctrl->ctrls_cb_args == NULL) {
335
    ctrl->ctrls_cb_args = make_array(ctrl->ctrls_tmp_pool, 0, sizeof(char *));
336
  }
337

8✔
338
  /* Add the given argument */
339
  *((char **) push_array(ctrl->ctrls_cb_args)) = pstrndup(ctrl->ctrls_tmp_pool,
340
    ctrls_arg, ctrls_arglen);
8✔
341

342
  return 0;
343
}
6✔
344

6✔
345
int pr_ctrls_copy_args(pr_ctrls_t *src_ctrl, pr_ctrls_t *dst_ctrl) {
6✔
346
  if (src_ctrl == NULL ||
347
      dst_ctrl == NULL ||
3✔
348
      src_ctrl == dst_ctrl) {
3✔
349
    errno = EINVAL;
350
    return -1;
351
  }
352

353
  /* If source ctrl has no ctrls_cb_args member, there's nothing to be
354
   * done.
3✔
355
   */
356
  if (src_ctrl->ctrls_cb_args == NULL) {
357
    return 0;
358
  }
359

360
  /* Make sure the pr_ctrls_t has a temporary pool, from which the args will
361
   * be allocated.
2✔
362
   */
2✔
363
  if (dst_ctrl->ctrls_tmp_pool == NULL) {
2✔
364
    dst_ctrl->ctrls_tmp_pool = make_sub_pool(ctrls_pool);
365
    pr_pool_tag(dst_ctrl->ctrls_tmp_pool, "ctrls tmp pool");
366
  }
367

368
  /* Overwrite any existing dst_ctrl->ctrls_cb_args.  This is OK, as
369
   * the ctrl will be reset (cleared) once it has been processed.
4✔
370
   */
2✔
371
  dst_ctrl->ctrls_cb_args = copy_array(dst_ctrl->ctrls_tmp_pool,
372
    src_ctrl->ctrls_cb_args);
2✔
373

374
  return 0;
375
}
6✔
376

6✔
377
int pr_ctrls_copy_resps(pr_ctrls_t *src_ctrl, pr_ctrls_t *dst_ctrl) {
6✔
378
  if (src_ctrl == NULL ||
379
      dst_ctrl == NULL ||
3✔
380
      src_ctrl == dst_ctrl) {
3✔
381
    errno = EINVAL;
382
    return -1;
383
  }
384

385
  /* The source ctrl must have a ctrls_cb_resps member, and the destination
386
   * ctrl must not have a ctrls_cb_resps member.
3✔
387
   */
2✔
388
  if (src_ctrl->ctrls_cb_resps == NULL ||
2✔
389
      dst_ctrl->ctrls_cb_resps != NULL) {
2✔
390
    errno = EPERM;
391
    return -1;
392
  }
1✔
393

394
  dst_ctrl->ctrls_cb_resps = copy_array(dst_ctrl->ctrls_tmp_pool,
395
    src_ctrl->ctrls_cb_resps);
1✔
396

397
  return 0;
398
}
14✔
399

14✔
400
int pr_ctrls_add_response(pr_ctrls_t *ctrl, const char *fmt, ...) {
14✔
401
  char buf[PR_TUNABLE_BUFFER_SIZE] = {'\0'};
402
  va_list resp;
403

14✔
404
  /* Sanity check */
14✔
405
  if (ctrl == NULL ||
2✔
406
      fmt == NULL) {
2✔
407
    errno = EINVAL;
408
    return -1;
409
  }
410

411
  /* Make sure the pr_ctrls_t has a temporary pool, from which the responses
412
   * will be allocated
12✔
413
   */
6✔
414
  if (ctrl->ctrls_tmp_pool == NULL) {
6✔
415
    ctrl->ctrls_tmp_pool = make_sub_pool(ctrls_pool);
416
    pr_pool_tag(ctrl->ctrls_tmp_pool, "ctrls tmp pool");
417
  }
12✔
418

7✔
419
  if (ctrl->ctrls_cb_resps == NULL) {
420
    ctrl->ctrls_cb_resps = make_array(ctrl->ctrls_tmp_pool, 0,
421
      sizeof(char *));
422
  }
423

12✔
424
  /* Affix the message */
12✔
425
  va_start(resp, fmt);
12✔
426
  pr_vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, resp);
427
  va_end(resp);
12✔
428

429
  buf[sizeof(buf) - 1] = '\0';
430

24✔
431
  /* add the given response */
12✔
432
  *((char **) push_array(ctrl->ctrls_cb_resps)) =
433
    pstrdup(ctrl->ctrls_tmp_pool, buf);
12✔
434

435
  return 0;
436
}
5✔
437

5✔
438
int pr_ctrls_flush_response(pr_ctrls_t *ctrl) {
1✔
439
  if (ctrl == NULL) {
1✔
440
    errno = EINVAL;
441
    return -1;
442
  }
443

4✔
444
  /* Make sure the callback(s) added responses */
3✔
445
  if (ctrl->ctrls_cb_resps != NULL) {
446
    int res;
3✔
447

1✔
448
    if (ctrl->ctrls_cl == NULL) {
1✔
449
      errno = EPERM;
450
      return -1;
451
    }
4✔
452

453
    res = pr_ctrls_send_response(ctrl->ctrls_tmp_pool, ctrl->ctrls_cl->cl_fd,
2✔
454
      ctrl->ctrls_cb_retval, ctrl->ctrls_cb_resps->nelts,
2✔
455
      (char **) ctrl->ctrls_cb_resps->elts);
1✔
456
    if (res < 0) {
457
      return -1;
458
    }
459
  }
460

461
  return 0;
462
}
7✔
463

7✔
464
static int ctrls_send_msg(pool *p, int fd, pr_json_object_t *json) {
7✔
465
  uint32_t msglen;
7✔
466
  int res, xerrno;
467
  char *msg;
7✔
468

7✔
469
  msg = pr_json_object_to_text(p, json, "");
470
  if (msg == NULL) {
471
    return -1;
472
  }
7✔
473

474
  msglen = strlen(msg);
475

7✔
476
  /* No interruptions. */
477
  pr_signals_block();
7✔
478

7✔
479
  pr_trace_msg(trace_channel, 27,
480
    "sending Controls message (%lu bytes) to fd %d", (unsigned long) msglen,
7✔
481
    fd);
3✔
482

483
  res = write(fd, &msglen, sizeof(uint32_t));
3✔
484
  xerrno = errno;
3✔
485

486
  if ((size_t) res != sizeof(uint32_t)) {
487
    pr_signals_unblock();
4✔
488

4✔
489
    errno = xerrno;
4✔
490
    return -1;
491
  }
4✔
492

×
493
  while (TRUE) {
×
494
    res = write(fd, msg, msglen);
495
    xerrno = errno;
496

×
497
    if ((size_t) res != msglen) {
498
      if (xerrno == EAGAIN) {
×
499
        continue;
×
500
      }
501

502
      pr_signals_unblock();
4✔
503

504
      errno = xerrno;
505
      return -1;
4✔
506
    }
4✔
507

508
    break;
509
  }
6✔
510

511
  pr_signals_unblock();
6✔
512
  return 0;
6✔
513
}
6✔
514

6✔
515
int pr_ctrls_send_request(pool *p, int fd, const char *action,
6✔
516
    unsigned int argc, char **argv) {
517
  register unsigned int i;
6✔
518
  pool *tmp_pool;
6✔
519
  int res, xerrno;
520
  pr_json_object_t *json;
3✔
521
  pr_json_array_t *args;
3✔
522

523
  if (p == NULL ||
524
      fd < 0 ||
3✔
525
      action == NULL) {
3✔
526
    errno = EINVAL;
1✔
527
    return -1;
1✔
528
  }
529

530
  if (argc > 0 &&
2✔
531
      argv == NULL) {
2✔
532
    errno = EINVAL;
533
    return -1;
2✔
534
  }
535

2✔
536
  tmp_pool = make_sub_pool(p);
2✔
537
  pr_pool_tag(tmp_pool, "Controls API send_request pool");
538

2✔
539
  json = pr_json_object_alloc(tmp_pool);
×
540

×
541
  res = pr_json_object_set_string(tmp_pool, json, CTRLS_REQ_ACTION_KEY, action);
542
  xerrno = errno;
×
543

×
544
  if (res < 0) {
545
    pr_json_object_free(json);
546
    destroy_pool(tmp_pool);
2✔
547

548
    errno = xerrno;
6✔
549
    return -1;
2✔
550
  }
2✔
551

552
  args = pr_json_array_alloc(tmp_pool);
2✔
553

×
554
  for (i = 0; i < argc; i++) {
×
555
    res = pr_json_array_append_string(tmp_pool, args, argv[i]);
×
556
    xerrno = errno;
557

×
558
    if (res < 0) {
×
559
      pr_json_array_free(args);
560
      pr_json_object_free(json);
561
      destroy_pool(tmp_pool);
562

2✔
563
      errno = xerrno;
2✔
564
      return -1;
565
    }
2✔
566
  }
×
567

×
568
  res = pr_json_object_set_array(tmp_pool, json, CTRLS_REQ_ARGS_KEY, args);
×
569
  xerrno = errno;
570

×
571
  if (res < 0) {
×
572
    pr_json_array_free(args);
573
    pr_json_object_free(json);
574
    destroy_pool(tmp_pool);
2✔
575

2✔
576
    errno = xerrno;
577
    return -1;
2✔
578
  }
2✔
579

2✔
580
  res = ctrls_send_msg(tmp_pool, fd, json);
581
  xerrno = errno;
2✔
582

2✔
583
  pr_json_array_free(args);
584
  pr_json_object_free(json);
585
  destroy_pool(tmp_pool);
15✔
586

15✔
587
  errno = xerrno;
15✔
588
  return res;
15✔
589
}
15✔
590

15✔
591
int pr_ctrls_recv_request(pr_ctrls_cl_t *cl) {
15✔
592
  register int i = 0;
15✔
593
  pr_ctrls_t *ctrl = NULL, *next_ctrl = NULL;
15✔
594
  pool *tmp_pool = NULL;
595
  int nread, nreqargs = 0, res, xerrno;
15✔
596
  uint32_t msglen;
14✔
597
  char *msg = NULL, *reqaction = NULL;
2✔
598
  pr_json_object_t *json = NULL;
2✔
599
  pr_json_array_t *args = NULL;
600

601
  if (cl == NULL ||
13✔
602
      cl->cl_ctrls == NULL) {
1✔
603
    errno = EINVAL;
1✔
604
    return -1;
605
  }
606

607
  if (cl->cl_fd < 0) {
12✔
608
    errno = EBADF;
609
    return -1;
610
  }
611

12✔
612
  /* No interruptions */
12✔
613
  pr_signals_block();
614

12✔
615
  /* Read in the size of the message, as JSON text. */
1✔
616

617
  nread = read(cl->cl_fd, &msglen, sizeof(uint32_t));
618
  xerrno = errno;
1✔
619

620
  if (nread < 0) {
1✔
621
    pr_signals_unblock();
1✔
622

623
    pr_trace_msg(trace_channel, 3,
624
      "error reading %lu bytes of request message size: %s",
625
      sizeof(msglen), strerror(xerrno));
11✔
626
    errno = xerrno;
1✔
627
    return -1;
628
  }
629

1✔
630
  /* Watch for short reads. */
1✔
631
  if (nread != sizeof(uint32_t)) {
1✔
632
    pr_signals_unblock();
633

634
    (void) pr_trace_msg(trace_channel, 3,
10✔
635
      "short read (%d of %u bytes) of message size, unable to receive request",
10✔
636
      nread, (unsigned int) sizeof(uint32_t));
637
    errno = EPERM;
638
    return -1;
10✔
639
  }
640

10✔
641
  tmp_pool = make_sub_pool(cl->cl_pool);
10✔
642
  pr_pool_tag(tmp_pool, "Controls API recv_request pool");
643

10✔
644
  pr_trace_msg(trace_channel, 27,
×
645
    "receiving Controls request message (%lu bytes) from fd %d",
646
    (unsigned long) msglen, cl->cl_fd);
647

×
648
  /* Impose max request size limit here, for Issue #2036. */
×
649
  if (msglen > CTRLS_MAX_REQ_SIZE) {
650
    destroy_pool(tmp_pool);
×
651
    pr_signals_unblock();
×
652

653
    (void) pr_trace_msg(trace_channel, 3,
654
      "message size (%lu bytes) exceeds max (%lu bytes), unable to receive "
655
      "request", (unsigned long) msglen, (unsigned long) CTRLS_MAX_REQ_SIZE);
10✔
656
    errno = E2BIG;
1✔
657
    return -1;
658
  }
659

1✔
660
  /* Allocate one byte for the terminating NUL. */
1✔
661
  msg = pcalloc(tmp_pool, msglen + 1);
1✔
662

1✔
663
  nread = read(cl->cl_fd, msg, msglen);
664
  xerrno = errno;
665

9✔
666
  if (nread < 0) {
9✔
667
    destroy_pool(tmp_pool);
668
    pr_signals_unblock();
9✔
669

1✔
670
    pr_trace_msg(trace_channel, 3,
671
      "error reading %lu bytes of request message: %s",
672
      (unsigned long) msglen, strerror(xerrno));
673
    errno = xerrno;
1✔
674
    return -1;
1✔
675
  }
676

1✔
677
  /* Watch for short reads. */
1✔
678
  if ((unsigned int) nread != msglen) {
679
    destroy_pool(tmp_pool);
680
    pr_signals_unblock();
8✔
681

682
    (void) pr_trace_msg(trace_channel, 3,
8✔
683
      "short read (%d of %u bytes) of message text, unable to receive request",
684
      nread, (unsigned int) msglen);
8✔
685
    errno = EPERM;
1✔
686
    return -1;
687
  }
688

1✔
689
  json = pr_json_object_from_text(tmp_pool, msg);
1✔
690
  xerrno = errno;
1✔
691

692
  if (json == NULL) {
1✔
693
    destroy_pool(tmp_pool);
1✔
694
    pr_signals_unblock();
695

696
    (void) pr_trace_msg(trace_channel, 3,
7✔
697
      "read invalid JSON message text ('%.*s' [%lu bytes]), unable to "
7✔
698
      "receive request: %s", (int) msglen, msg, (unsigned long) msglen,
699
      strerror(xerrno));
7✔
700
    errno = EINVAL;
1✔
701
    return -1;
702
  }
703

1✔
704
  res = pr_json_object_get_string(tmp_pool, json, CTRLS_REQ_ACTION_KEY,
1✔
705
    &reqaction);
1✔
706
  xerrno = errno;
707

1✔
708
  if (res < 0) {
1✔
709
    pr_json_object_free(json);
710
    destroy_pool(tmp_pool);
711
    pr_signals_unblock();
6✔
712

6✔
713
    (void) pr_trace_msg(trace_channel, 3,
714
      "unable to read message action (%s), unable to receive request",
715
      strerror(xerrno));
716
    errno = EINVAL;
717
    return -1;
12✔
718
  }
6✔
719

1✔
720
  res = pr_json_object_get_array(tmp_pool, json, CTRLS_REQ_ARGS_KEY, &args);
721
  xerrno = errno;
1✔
722

1✔
723
  if (res < 0) {
1✔
724
    pr_json_object_free(json);
1✔
725
    destroy_pool(tmp_pool);
726
    pr_signals_unblock();
727

1✔
728
    (void) pr_trace_msg(trace_channel, 3,
1✔
729
      "unable to read message arguments (%s), unable to receive request",
730
      strerror(xerrno));
731
    errno = EINVAL;
5✔
732
    return -1;
733
  }
14✔
734

4✔
735
  nreqargs = pr_json_array_count(args);
4✔
736
  pr_trace_msg(trace_channel, 19, "received request argc: %u", nreqargs);
737

4✔
738
  /* Find a matching action object, and use it to populate a ctrl object,
4✔
739
   * preparing the ctrl object for dispatching to the action handlers.
740
   */
4✔
741
  ctrl = ctrls_lookup_action(NULL, reqaction, TRUE);
×
742
  if (ctrl == NULL) {
743
    (void) pr_trace_msg(trace_channel, 3,
744
      "unknown action requested '%s', unable to receive request", reqaction);
×
745
    pr_json_array_free(args);
×
746
    pr_json_object_free(json);
×
747
    destroy_pool(tmp_pool);
×
748
    pr_signals_unblock();
749

×
750
    /* XXX This is where we could also add "did you mean" functionality. */
×
751
    errno = EINVAL;
752
    return -1;
753
  }
4✔
754

4✔
755
  pr_trace_msg(trace_channel, 19, "known action '%s' requested", reqaction);
4✔
756

757
  for (i = 0; i < nreqargs; i++) {
4✔
758
    size_t reqarglen = 0;
×
759
    char *reqarg = NULL;
760

761
    res = pr_json_array_get_string(tmp_pool, args, i, &reqarg);
×
762
    xerrno = errno;
×
763

×
764
    if (res < 0) {
×
765
      (void) pr_trace_msg(trace_channel, 3,
766
        "unable to read message argument #%u (%s), unable to receive request",
×
767
        i+1, strerror(xerrno));
×
768
      pr_json_array_free(args);
769
      pr_json_object_free(json);
770
      destroy_pool(tmp_pool);
771
      pr_signals_unblock();
772

5✔
773
      errno = EINVAL;
774
      return -1;
775
    }
5✔
776

5✔
777
    reqarglen = strlen(reqarg);
778
    res = pr_ctrls_add_arg(ctrl, reqarg, reqarglen);
779
    xerrno = errno;
780

781
    if (res < 0) {
5✔
782
      pr_trace_msg(trace_channel, 3,
783
        "error adding message argument #%u (%s): %s", i+1, reqarg,
6✔
784
        strerror(xerrno));
1✔
785
      pr_json_array_free(args);
786
      pr_json_object_free(json);
787
      destroy_pool(tmp_pool);
1✔
788
      pr_signals_unblock();
789

790
      errno = xerrno;
1✔
791
      return -1;
1✔
792
    }
793
  }
1✔
794

795
  /* Add this ctrls object to the client object. */
796
  *((pr_ctrls_t **) push_array(cl->cl_ctrls)) = ctrl;
5✔
797

5✔
798
  /* Set the flag that this control is ready to go */
5✔
799
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
5✔
800
  ctrl->ctrls_cl = cl;
801

5✔
802
  /* Copy the populated ctrl object args to ctrl objects for all other
803
   * matching action objects.
804
   */
8✔
805
  next_ctrl = ctrls_lookup_next_action(NULL, TRUE);
806

8✔
807
  while (next_ctrl != NULL) {
8✔
808
    (void) pr_ctrls_copy_args(ctrl, next_ctrl);
8✔
809

8✔
810
    /* Add this ctrl object to the client object. */
8✔
811
    *((pr_ctrls_t **) push_array(cl->cl_ctrls)) = next_ctrl;
812

8✔
813
    /* Set the flag that this control is ready to go. */
8✔
814
    next_ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
2✔
815
    next_ctrl->ctrls_cl = cl;
2✔
816

817
    next_ctrl = ctrls_lookup_next_action(NULL, TRUE);
818
  }
6✔
819

6✔
820
  pr_json_array_free(args);
1✔
821
  pr_json_object_free(json);
1✔
822
  destroy_pool(tmp_pool);
823
  pr_signals_unblock();
824

5✔
825
  return 0;
5✔
826
}
827

5✔
828
int pr_ctrls_send_response(pool *p, int fd, int status, unsigned int argc,
829
    char **argv) {
5✔
830
  register unsigned int i;
831
  pool *tmp_pool;
5✔
832
  int res, xerrno;
833
  pr_json_object_t *json;
5✔
834
  pr_json_array_t *resps;
×
835

×
836
  if (p == NULL ||
837
      fd < 0) {
×
838
    errno = EINVAL;
×
839
    return -1;
840
  }
841

5✔
842
  if (argc > 0 &&
843
      argv == NULL) {
14✔
844
    errno = EINVAL;
4✔
845
    return -1;
4✔
846
  }
847

4✔
848
  tmp_pool = make_sub_pool(p);
×
849
  pr_pool_tag(tmp_pool, "Controls API send_response pool");
×
850

×
851
  json = pr_json_object_alloc(tmp_pool);
852

×
853
  res = pr_json_object_set_number(tmp_pool, json, CTRLS_RESP_STATUS_KEY,
×
854
    (double) status);
855
  xerrno = errno;
856

857
  if (res < 0) {
5✔
858
    pr_json_object_free(json);
5✔
859
    destroy_pool(tmp_pool);
860

5✔
861
    errno = xerrno;
×
862
    return -1;
×
863
  }
×
864

865
  resps = pr_json_array_alloc(tmp_pool);
×
866

×
867
  for (i = 0; i < argc; i++) {
868
    res = pr_json_array_append_string(tmp_pool, resps, argv[i]);
869
    xerrno = errno;
5✔
870

5✔
871
    if (res < 0) {
872
      pr_json_array_free(resps);
5✔
873
      pr_json_object_free(json);
5✔
874
      destroy_pool(tmp_pool);
5✔
875

876
      errno = xerrno;
5✔
877
      return -1;
5✔
878
    }
879
  }
880

10✔
881
  res = pr_json_object_set_array(tmp_pool, json, CTRLS_RESP_RESPS_KEY, resps);
10✔
882
  xerrno = errno;
10✔
883

10✔
884
  if (res < 0) {
10✔
885
    pr_json_array_free(resps);
10✔
886
    pr_json_object_free(json);
10✔
887
    destroy_pool(tmp_pool);
10✔
888

10✔
889
    errno = xerrno;
10✔
890
    return -1;
891
  }
892

10✔
893
  res = ctrls_send_msg(tmp_pool, fd, json);
10✔
894
  xerrno = errno;
895

3✔
896
  pr_json_array_free(resps);
3✔
897
  pr_json_object_free(json);
898
  destroy_pool(tmp_pool);
899

900
  errno = xerrno;
7✔
901
  return res;
902
}
903

904
int pr_ctrls_recv_response(pool *p, int fd, int *status, char ***respargv) {
7✔
905
  register int i = 0;
7✔
906
  pool *tmp_pool;
907
  int nread, res, respargc = 0, xerrno;
7✔
908
  uint32_t msglen = 0;
1✔
909
  char *msg = NULL;
910
  pr_json_object_t *json = NULL;
1✔
911
  pr_json_array_t *resps = NULL;
×
912
  double dv;
913
  array_header *resparr = NULL;
914

×
915
  /* Sanity checks */
×
916
  if (p == NULL ||
917
      fd < 0 ||
918
      status == NULL) {
1✔
919
    errno = EINVAL;
920
    return -1;
921
  }
1✔
922

1✔
923
  /* No interruptions. */
924
  pr_signals_block();
925

6✔
926
  /* Read in the size of the message, as JSON text. */
6✔
927

928
  nread = read(fd, &msglen, sizeof(uint32_t));
929
  xerrno = errno;
6✔
930

6✔
931
  if (nread < 0) {
6✔
932
    pr_signals_unblock();
933

6✔
934
    pr_trace_msg(trace_channel, 3,
×
935
      "error reading %lu bytes of response message size: %s",
936
      sizeof(msglen), strerror(xerrno));
937

×
938
    errno = xerrno;
×
939
    return -1;
940
  }
×
941

×
942
  /* Watch for short reads. */
943
  if (nread != sizeof(uint32_t)) {
944
    pr_signals_unblock();
945

6✔
946
    if (nread < 0) {
1✔
947
      (void) pr_trace_msg(trace_channel, 3,
948
        "error reading %u of response message size: %s",
949
        (unsigned int) sizeof(uint32_t), strerror(xerrno));
1✔
950
      errno = xerrno;
1✔
951
      return -1;
952
    }
1✔
953

1✔
954
    (void) pr_trace_msg(trace_channel, 3,
955
      "short read (%d of %u bytes) of response message, unable to receive "
956
      "response", nread, (unsigned int) sizeof(uint32_t));
5✔
957
    errno = EPERM;
5✔
958
    return -1;
959
  }
5✔
960

1✔
961
  tmp_pool = make_sub_pool(p);
962
  pr_pool_tag(tmp_pool, "Controls API recv_response pool");
963

964
  pr_trace_msg(trace_channel, 27,
1✔
965
    "receiving Controls response message (%lu bytes) from fd %d",
1✔
966
    (unsigned long) msglen, fd);
967

1✔
968
  /* Impose max response size limit here, for Issue #2036. */
1✔
969
  if (msglen > CTRLS_MAX_RESP_SIZE) {
970
    destroy_pool(tmp_pool);
971
    pr_signals_unblock();
4✔
972

4✔
973
    (void) pr_trace_msg(trace_channel, 3,
974
      "message size (%lu bytes) exceeds max (%lu bytes), unable to receive "
4✔
975
      "response", (unsigned long) msglen, (unsigned long) CTRLS_MAX_RESP_SIZE);
1✔
976
    errno = E2BIG;
977
    return -1;
978
  }
1✔
979

1✔
980
  /* Allocate one byte for the terminating NUL. */
1✔
981
  msg = pcalloc(tmp_pool, msglen + 1);
982
  nread = read(fd, msg, msglen);
1✔
983
  xerrno = errno;
1✔
984

985
  if (nread < 0) {
986
    destroy_pool(tmp_pool);
3✔
987
    pr_signals_unblock();
3✔
988

989
    pr_trace_msg(trace_channel, 3,
3✔
990
      "error reading %lu bytes of response message: %s",
3✔
991
      (unsigned long) msglen, strerror(xerrno));
992
    errno = xerrno;
3✔
993
    return -1;
1✔
994
  }
995

996
  /* Watch for short reads. */
1✔
997
  if ((unsigned int) nread != msglen) {
1✔
998
    destroy_pool(tmp_pool);
1✔
999
    pr_signals_unblock();
1000

1✔
1001
    (void) pr_trace_msg(trace_channel, 3,
1✔
1002
      "short read (%d of %u bytes) of message text, unable to receive response",
1003
      nread, (unsigned int) msglen);
1004
    errno = EPERM;
2✔
1005
    return -1;
2✔
1006
  }
1007

2✔
1008
  json = pr_json_object_from_text(tmp_pool, msg);
1009
  xerrno = errno;
1010

5✔
1011
  if (json == NULL) {
2✔
1012
    destroy_pool(tmp_pool);
1013
    pr_signals_unblock();
1014

1015
    (void) pr_trace_msg(trace_channel, 3,
1016
      "read invalid JSON message text ('%.*s' [%lu bytes]), unable to "
1017
      "receive response: %s", (int) msglen, msg, (unsigned long) msglen,
2✔
1018
      strerror(xerrno));
2✔
1019
    errno = EINVAL;
1020
    return -1;
2✔
1021
  }
1✔
1022

1023
  res = pr_json_object_get_number(tmp_pool, json, CTRLS_RESP_STATUS_KEY, &dv);
1024
  xerrno = errno;
1✔
1025

1✔
1026
  if (res < 0) {
1✔
1027
    pr_json_object_free(json);
1✔
1028
    destroy_pool(tmp_pool);
1029
    pr_signals_unblock();
1✔
1030

1✔
1031
    (void) pr_trace_msg(trace_channel, 3,
1032
      "unable to read response status (%s), unable to receive response",
1033
      strerror(xerrno));
1✔
1034
    errno = EINVAL;
1035
    return -1;
1036
  }
1✔
1037

×
1038
  *status = (int) dv;
1039
  pr_trace_msg(trace_channel, 19, "received response status: %d", *status);
1040

1✔
1041
  res = pr_json_object_get_array(tmp_pool, json, CTRLS_RESP_RESPS_KEY, &resps);
1✔
1042
  xerrno = errno;
1✔
1043

1✔
1044
  if (res < 0) {
1045
    (void) pr_trace_msg(trace_channel, 3,
1✔
1046
      "unable to read message responses (%s), unable to receive response",
1047
      strerror(xerrno));
1048
    pr_json_object_free(json);
7✔
1049
    destroy_pool(tmp_pool);
1050
    pr_signals_unblock();
1051

1052
    errno = EINVAL;
7✔
1053
    return -1;
7✔
1054
  }
7✔
1055

1056
  respargc = pr_json_array_count(resps);
1057
  pr_trace_msg(trace_channel, 19, "received response argc: %u", respargc);
6✔
1058

1059
  resparr = make_array(p, 0, sizeof(char *));
1060

13✔
1061
  /* Read each response, and add it to the array */
1062
  for (i = 0; i < respargc; i++) {
13✔
1063
    char *resp = NULL;
1064

1065
    /* TODO: Handle other response types, such as arrays or objects, for
13✔
1066
     * more complex responses.  Think of an action that dumps the memory
×
1067
     * pools, for example.
×
1068
     */
1069
    res = pr_json_array_get_string(tmp_pool, resps, i, &resp);
1070
    xerrno = errno;
13✔
1071

×
1072
    if (res < 0) {
1073
      (void) pr_trace_msg(trace_channel, 3,
1074
        "unable to read message response #%u (%s), unable to receive response",
13✔
1075
        i+1, strerror(xerrno));
7✔
1076
      pr_json_array_free(resps);
×
1077
      pr_json_object_free(json);
1078
      destroy_pool(tmp_pool);
1079
      pr_signals_unblock();
7✔
1080

7✔
1081
      errno = EINVAL;
7✔
1082
      return -1;
1083
    }
1084

7✔
1085
    *((char **) push_array(resparr)) = pstrdup(p, resp);
1086
  }
1087

1088
  if (respargv != NULL) {
1089
    *respargv = ((char **) resparr->elts);
1090
  }
1091

7✔
1092
  pr_json_array_free(resps);
7✔
1093
  pr_json_object_free(json);
7✔
1094
  destroy_pool(tmp_pool);
1095
  pr_signals_unblock();
7✔
1096

1✔
1097
  return respargc;
1✔
1098
}
1099

1100
static pr_ctrls_t *ctrls_lookup_action(module *mod, const char *action,
1101
    unsigned char skip_disabled) {
6✔
1102

1✔
1103
  /* (Re)set the current indices */
1✔
1104
  action_lookup_next = ctrls_action_list;
1105
  action_lookup_action = action;
1106
  action_lookup_module = mod;
13✔
1107

8✔
1108
  /* Wrapper around ctrls_lookup_next_action() */
2✔
1109
  return ctrls_lookup_next_action(mod, skip_disabled);
2✔
1110
}
1✔
1111

1✔
1112
static pr_ctrls_t *ctrls_lookup_next_action(module *mod,
1113
    unsigned char skip_disabled) {
1114
  register ctrls_action_t *act = NULL;
1✔
1115

1116
  /* Sanity check */
1117
  if (action_lookup_action == NULL) {
2✔
1118
    errno = EINVAL;
2✔
1119
    return NULL;
1120
  }
2✔
1121

2✔
1122
  if (mod != action_lookup_module) {
×
1123
    return ctrls_lookup_action(mod, action_lookup_action, skip_disabled);
1124
  }
1125

2✔
1126
  for (act = action_lookup_next; act; act = act->next) {
1✔
1127
    if (skip_disabled && (act->flags & PR_CTRLS_ACT_DISABLED)) {
1✔
1128
      continue;
1129
    }
1130

1✔
1131
    if (strcmp(act->action, action_lookup_action) == 0 &&
1132
        (act->module == mod || mod == ANY_MODULE || mod == NULL)) {
1133
      action_lookup_next = act->next;
2✔
1134

2✔
1135
      /* Use this action object to prepare a ctrl object. */
1136
      return ctrls_prepare(act);
2✔
1137
    }
2✔
1138
  }
1139

2✔
1140
  return NULL;
2✔
1141
}
1142

1143
int pr_get_registered_actions(pr_ctrls_t *ctrl, int flags) {
1144
  register ctrls_action_t *act = NULL;
1145
  int count = 0;
1146

1147
  if (ctrl == NULL) {
7✔
1148
    errno = EINVAL;
1149
    return -1;
7✔
1150
  }
7✔
1151

1152
  /* Are ctrls blocked? */
1153
  if (ctrls_blocked == TRUE) {
7✔
1154
    errno = EPERM;
7✔
1155
    return -1;
1✔
1156
  }
1✔
1157

1✔
1158
  for (act = ctrls_action_list; act; act = act->next) {
1✔
1159
    switch (flags) {
1160
      case CTRLS_GET_ACTION_ALL:
1161
        if (act->module != NULL) {
1162
          pr_ctrls_add_response(ctrl, "%s (mod_%s.c)", act->action,
6✔
1163
            act->module->name);
1✔
1164

1✔
1165
        } else {
1166
           pr_ctrls_add_response(ctrl, "%s (core)", act->action);
1167
        }
10✔
1168

5✔
1169
        count++;
×
1170
        break;
×
1171

1172
      case CTRLS_GET_ACTION_ENABLED:
1173
        if (act->flags & PR_CTRLS_ACT_DISABLED) {
5✔
1174
          continue;
4✔
1175
        }
3✔
1176

5✔
1177
        if (act->module != NULL) {
5✔
1178
          pr_ctrls_add_response(ctrl, "%s (mod_%s.c)", act->action,
5✔
1179
            act->module->name);
1180

1181
        } else {
1182
          pr_ctrls_add_response(ctrl, "%s (core)", act->action);
5✔
1183
        }
1✔
1184

1✔
1185
        count++;
1186
        break;
1187

1188
      case CTRLS_GET_DESC:
1189
        pr_ctrls_add_response(ctrl, "%s: %s", act->action,
1190
          act->desc);
1191
        count++;
1192
        break;
1193
    }
1194
  }
1195

1196
  return count;
1197
}
1198

1199
int pr_set_registered_actions(module *mod, const char *action,
1200
    unsigned char skip_disabled, unsigned int flags) {
1201
  register ctrls_action_t *act = NULL;
1202
  unsigned char have_action = FALSE;
1203

1204
  /* Is flags a valid combination of settable flags? */
1205
  if (flags > 0 &&
1206
      flags != PR_CTRLS_ACT_SOLITARY &&
1207
      flags != PR_CTRLS_ACT_DISABLED &&
1208
      flags != (PR_CTRLS_ACT_SOLITARY|PR_CTRLS_ACT_DISABLED)) {
1209
    errno = EINVAL;
1210
    return -1;
1211
  }
1212

1213
  /* Are ctrls blocked? */
1214
  if (ctrls_blocked == TRUE) {
1215
    errno = EPERM;
1216
    return -1;
1217
  }
1218

1219
  for (act = ctrls_action_list; act; act = act->next) {
1220
    if (skip_disabled == TRUE &&
3✔
1221
        (act->flags & PR_CTRLS_ACT_DISABLED)) {
3✔
1222
      continue;
3✔
1223
    }
1224

3✔
1225
    if ((action == NULL ||
1✔
1226
         strcmp(action, "all") == 0 ||
1✔
1227
         strcmp(act->action, action) == 0) &&
1228
        (act->module == mod || mod == ANY_MODULE || mod == NULL)) {
1229
      have_action = TRUE;
1230
      act->flags = flags;
2✔
1231
    }
1232
  }
1233

2✔
1234
  if (have_action == FALSE) {
2✔
1235
    errno = ENOENT;
×
1236
    return -1;
1237
  }
×
1238

1239
  return 0;
×
1240
}
×
1241

1242
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
1243
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
2✔
1244
static int ctrls_connect_local_creds(int fd) {
×
1245
  char buf[1] = {'\0'};
1246
  int res;
×
1247

×
1248
  /* The backend doesn't care what we send here, but it wants
1249
   * exactly one character to force recvmsg() to block and wait
×
1250
   * for us.
×
1251
   */
1252

1253
  res = write(fd, buf, 1);
1254
  while (res < 0) {
2✔
1255
    if (errno == EINTR) {
1256
      pr_signals_handle();
1257

1258
      res = write(fd, buf, 1);
1259
      continue;
1260
    }
1261

1262
    pr_trace_msg(trace_channel, 5,
1263
      "error writing credentials byte for LOCAL_CREDS to fd %d: %s", fd,
2✔
1264
      strerror(errno));
2✔
1265
    return -1;
2✔
1266
  }
2✔
1267

1268
  return res;
1269
}
2✔
1270
#endif /* !SCM_CREDS */
1271

1272
int pr_ctrls_connect(const char *socket_file) {
2✔
1273
  int fd = -1, len = 0;
×
1274
  struct sockaddr_un cl_sock, ctrl_sock;
1275

×
1276
  if (socket_file == NULL) {
1277
    errno = EINVAL;
×
1278
    return -1;
×
1279
  }
×
1280

1281
  /* No interruptions */
×
1282
  pr_signals_block();
×
1283

1284
  /* Create a Unix domain socket */
1285
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
1286
  if (fd < 0) {
2✔
1287
    int xerrno = errno;
×
1288

1289
    pr_signals_unblock();
×
1290

1291
    errno = xerrno;
×
1292
    return -1;
×
1293
  }
×
1294

1295
  if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
×
1296
    int xerrno = errno;
×
1297

1298
    (void) close(fd);
1299
    pr_signals_unblock();
1300

2✔
1301
    errno = xerrno;
1302
    return -1;
2✔
1303
  }
2✔
1304

2✔
1305
  /* Fill in the socket address */
1306
  memset(&cl_sock, 0, sizeof(cl_sock));
2✔
1307

1✔
1308
  /* This first part is clever.  First, this process creates a socket in
1309
   * the file system.  It _then_ connect()s to the server.  Upon accept()ing
1✔
1310
   * the connection, the server examines the created socket to see that it
1311
   * is indeed a socket, with the proper mode and time.  Clever, but not
1✔
1312
   * ideal.
1✔
1313
   */
1✔
1314

1315
  cl_sock.sun_family = AF_UNIX;
1✔
1316
  pr_snprintf(cl_sock.sun_path, sizeof(cl_sock.sun_path) - 1, "%s%05u",
1✔
1317
    "/tmp/ftp.cl", (unsigned int) getpid());
1318
  len = sizeof(cl_sock);
1319

1320
  /* Make sure the file doesn't already exist */
1321
  (void) unlink(cl_sock.sun_path);
1322

1323
  /* Make it a socket */
1324
  if (bind(fd, (struct sockaddr *) &cl_sock, len) < 0) {
1325
    int xerrno = errno;
1326

1327
    pr_trace_msg(trace_channel, 19, "error binding local socket to '%s': %s",
1328
      cl_sock.sun_path, strerror(xerrno));
1329
    (void) unlink(cl_sock.sun_path);
1330
    (void) close(fd);
1331
    pr_signals_unblock();
1332

1333
    errno = xerrno;
1334
    return -1;
1335
  }
1✔
1336

1✔
1337
  /* Set the proper mode */
1338
  if (chmod(cl_sock.sun_path, PR_CTRLS_CL_MODE) < 0) {
1339
    int xerrno = errno;
3✔
1340

1341
    pr_trace_msg(trace_channel, 19, "error setting local socket mode: %s",
3✔
1342
      strerror(xerrno));
1343
    (void) unlink(cl_sock.sun_path);
×
1344
    (void) close(fd);
1345
    pr_signals_unblock();
1346

1347
    errno = xerrno;
1348
    return -1;
1349
  }
3✔
1350

1351
  /* Now connect to the real server */
1352
  memset(&ctrl_sock, 0, sizeof(ctrl_sock));
1353

1354
  ctrl_sock.sun_family = AF_UNIX;
1355
  sstrncpy(ctrl_sock.sun_path, socket_file, sizeof(ctrl_sock.sun_path));
2✔
1356
  len = sizeof(ctrl_sock);
2✔
1357

1358
  if (connect(fd, (struct sockaddr *) &ctrl_sock, len) < 0) {
1359
    int xerrno = errno;
1360

×
1361
    pr_trace_msg(trace_channel, 19, "error connecting to local socket '%s': %s",
1362
      ctrl_sock.sun_path, strerror(xerrno));
1363
    (void) unlink(cl_sock.sun_path);
1364
    (void) close(fd);
1365
    pr_signals_unblock();
×
1366

1367
    errno = xerrno;
×
1368
    return -1;
1369
  }
×
1370

×
1371
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
×
1372
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
1373
  if (ctrls_connect_local_creds(fd) < 0) {
×
1374
    int xerrno = errno;
1375

1376
    pr_trace_msg(trace_channel, 19, "error sending creds to local socket: %s",
1377
      strerror(xerrno));
×
1378
    (void) unlink(cl_sock.sun_path);
×
1379
    (void) close(fd);
1380
    pr_signals_unblock();
1381

×
1382
    errno = xerrno;
×
1383
    return -1;
1384
  }
1385
#endif /* LOCAL_CREDS */
×
1386

×
1387
  pr_signals_unblock();
1388
  return fd;
1389
}
×
1390

×
1391
int pr_ctrls_issock_unix(mode_t sock_mode) {
1392

1393
  if (ctrls_use_isfifo == TRUE) {
1394
#if defined(S_ISFIFO)
1395
    if (S_ISFIFO(sock_mode)) {
1396
      return 0;
1397
    }
1398
#endif /* S_ISFIFO */
1399
  } else {
1400
#if defined(S_ISSOCK)
1401
    if (S_ISSOCK(sock_mode)) {
1402
      return 0;
1403
    }
1404
#endif /* S_ISSOCK */
1405
  }
1406

1407
  errno = ENOSYS;
1408
  return -1;
1409
}
1410

1411
#if defined(SO_PEERCRED)
1412
static int ctrls_get_creds_peercred(int fd, uid_t *uid, gid_t *gid,
1413
    pid_t *pid) {
1414
# if defined(HAVE_STRUCT_SOCKPEERCRED)
1415
  struct sockpeercred cred;
1416
# else
1417
  struct ucred cred;
1418
# endif /* HAVE_STRUCT_SOCKPEERCRED */
1419
  socklen_t cred_len;
1420

1421
  cred_len = sizeof(cred);
1422
  if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) {
1423
    int xerrno = errno;
1424

1425
    pr_trace_msg(trace_channel, 2,
1426
      "error obtaining peer credentials using SO_PEERCRED: %s",
1427
      strerror(xerrno));
1428

1429
    errno = EPERM;
1430
    return -1;
1431
  }
1432

1433
  if (uid != NULL) {
1434
    *uid = cred.uid;
1435
  }
1436

1437
  if (gid != NULL) {
1438
    *gid = cred.gid;
1439
  }
1440

1441
  if (pid != NULL) {
1442
    *pid = cred.pid;
1443
  }
1444

1445
  return 0;
1446
}
1447
#endif /* SO_PEERCRED */
1448

1449
#if !defined(SO_PEERCRED) && defined(HAVE_GETPEEREID)
1450
static int ctrls_get_creds_peereid(int fd, uid_t *uid, gid_t *gid) {
1451
  if (getpeereid(fd, uid, gid) < 0) {
1452
    int xerrno = errno;
1453

1454
    pr_trace_msg(trace_channel, 7, "error obtaining credentials using "
1455
      "getpeereid(2) on fd %d: %s", fd, strerror(xerrno));
1456

1457
    errno = xerrno;
1458
    return -1;
1459
  }
1460

1461
  return 0;
1462
}
1463
#endif /* !HAVE_GETPEEREID */
1464

1465
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
1466
    defined(HAVE_GETPEERUCRED)
1467
static int ctrls_get_creds_peerucred(int fd, uid_t *uid, gid_t *gid) {
1468
  ucred_t *cred = NULL;
1469

1470
  if (getpeerucred(fd, &cred) < 0) {
1471
    int xerrno = errno;
1472

1473
    pr_trace_msg(trace_channel, 7, "error obtaining credentials using "
1474
      "getpeerucred(3) on fd %d: %s", fd, strerror(xerrno));
1475

1476
    errno = xerrno;
1477
    return -1;
1478
  }
1479

1480
  if (uid != NULL) {
1481
    *uid = ucred_getruid(cred);
1482
  }
1483

1484
  if (gid != NULL) {
1485
    *gid = ucred_getrgid(cred);
1486
  }
1487

1488
  ucred_free(cred);
1489
  return 0;
1490
}
1491
#endif /* !HAVE_GETPEERUCRED */
1492

1493
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
1494
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
1495
static int ctrls_get_creds_local(int fd, uid_t *uid, gid_t *gid,
1496
    pid_t *pid) {
1497
  int res;
1498
  char buf[1];
1499
  struct iovec iov;
1500
  struct msghdr msg;
1501

1502
# if defined(SOCKCREDSIZE)
1503
#  define MINCREDSIZE                (sizeof(struct cmsghdr) + SOCKCREDSIZE(0))
1504
# else
1505
#  if defined(HAVE_STRUCT_CMSGCRED)
1506
#   define MINCREDSIZE                (sizeof(struct cmsghdr) + sizeof(struct cmsgcred))
1507
#  elif defined(HAVE_STRUCT_SOCKCRED)
1508
#   define MINCREDSIZE                (sizeof(struct cmsghdr) + sizeof(struct sockcred))
1509
#  endif
1510
# endif /* !SOCKCREDSIZE */
1511

1512
  char control[MINCREDSIZE];
1513

1514
  iov.iov_base = buf;
1515
  iov.iov_len = 1;
1516

1517
  memset(&msg, 0, sizeof(msg));
1518
  msg.msg_iov = &iov;
1519
  msg.msg_iovlen = 1;
1520
  msg.msg_control = control;
1521
  msg.msg_controllen = sizeof(control);
1522
  msg.msg_flags = 0;
1523

1524
  res = recvmsg(fd, &msg, 0);
1525
  while (res < 0) {
1526
    int xerrno = errno;
1527

1528
    if (xerrno == EINTR) {
1529
      pr_signals_handle();
1530

1531
      res = recvmsg(fd, &msg, 0);
1532
      continue;
1533
    }
1534

1535
    pr_trace_msg(trace_channel, 6,
1536
      "error calling recvmsg() on fd %d: %s", fd, strerror(xerrno));
1537

1538
    errno = xerrno;
1539
    return -1;
1540
  }
1541

1542
  if (msg.msg_controllen > 0) {
1543
#if defined(HAVE_STRUCT_CMSGCRED)
1544
    struct cmsgcred cred;
1545
#elif defined(HAVE_STRUCT_SOCKCRED)
1546
    struct sockcred cred;
1547
#endif /* !CMSGCRED and !SOCKCRED */
1548

1549
    struct cmsghdr *hdr = (struct cmsghdr *) control;
1550

1551
    if (hdr->cmsg_level != SOL_SOCKET) {
1552
      pr_trace_msg(trace_channel, 5,
1553
        "message received via recvmsg() on fd %d was not a SOL_SOCKET message",
1554
        fd);
1555

1556
      errno = EINVAL;
1557
      return -1;
1558
    }
1559

1560
    if (hdr->cmsg_len < MINCREDSIZE) {
×
1561
      pr_trace_msg(trace_channel, 5,
1562
        "message received via recvmsg() on fd %d was not of proper "
×
1563
        "length (%u bytes)", fd, MINCREDSIZE);
×
1564

×
1565
      errno = EINVAL;
×
1566
      return -1;
1567
    }
1568

×
1569
    if (hdr->cmsg_type != SCM_CREDS) {
×
1570
      pr_trace_msg(trace_channel, 5,
×
1571
        "message received via recvmsg() on fd %d was not of type SCM_CREDS",
1572
        fd);
×
1573

×
1574
      errno = EINVAL;
×
1575
      return -1;
1576
    }
1577

×
1578
#if defined(HAVE_STRUCT_CMSGCRED)
×
1579
    memcpy(&cred, CMSG_DATA(hdr), sizeof(struct cmsgcred));
1580

×
1581
    if (uid != NULL) {
1582
      *uid = cred.cmcred_uid;
×
1583
    }
×
1584

1585
    if (gid != NULL) {
×
1586
      *gid = cred.cmcred_gid;
1587
    }
1588

×
1589
    if (pid != NULL) {
×
1590
      *pid = cred.cmcred_pid;
×
1591
    }
×
1592

1593
#elif defined(HAVE_STRUCT_SOCKCRED)
1594
    memcpy(&cred, CMSG_DATA(hdr), sizeof(struct sockcred));
1595

×
1596
    if (uid != NULL) {
1597
      *uid = cred.sc_uid;
×
1598
    }
1599

×
1600
    if (gid != NULL) {
×
1601
      *gid = cred.sc_gid;
×
1602
    }
1603
#endif
1604

1605
    return 0;
×
1606
  }
1607

×
1608
  return -1;
×
1609
}
×
1610
#endif /* !SCM_CREDS */
×
1611

×
1612
static int ctrls_get_creds_basic(struct sockaddr_un *sock, int cl_fd,
1613
    unsigned int max_age, uid_t *uid, gid_t *gid, pid_t *pid) {
×
1614
  pid_t cl_pid = 0;
1615
  char *tmp = NULL;
1616
  time_t stale_time;
1617
  struct stat st;
×
1618

×
1619
  /* Check the path -- hmmm... */
1620
  PRIVS_ROOT
×
1621
  while (stat(sock->sun_path, &st) < 0) {
1622
    int xerrno = errno;
1623

1624
    if (xerrno == EINTR) {
1625
      pr_signals_handle();
×
1626
      continue;
×
1627
    }
1628

×
1629
    PRIVS_RELINQUISH
1630
    pr_trace_msg(trace_channel, 2, "error: unable to stat %s: %s",
1631
      sock->sun_path, strerror(xerrno));
1632
    (void) close(cl_fd);
1633

×
1634
    errno = xerrno;
×
1635
    return -1;
1636
  }
×
1637
  PRIVS_RELINQUISH
1638

1639
  /* Is it a socket? */
1640
  if (pr_ctrls_issock_unix(st.st_mode) < 0) {
1641
    (void) close(cl_fd);
×
1642
    errno = ENOTSOCK;
1643
    return -1;
×
1644
  }
×
1645

×
1646
  /* Are the perms _not_ rwx------? */
1647
  if (st.st_mode & (S_IRWXG|S_IRWXO) ||
1648
      ((st.st_mode & S_IRWXU) != PR_CTRLS_CL_MODE)) {
×
1649
    pr_trace_msg(trace_channel, 3,
×
1650
      "error: unable to accept connection: incorrect mode");
1651
    (void) close(cl_fd);
×
1652
    errno = EPERM;
×
1653
    return -1;
1654
  }
1655

1656
  /* Is it new enough? */
×
1657
  stale_time = time(NULL) - max_age;
×
1658

×
1659
  if (st.st_atime < stale_time ||
1660
      st.st_ctime < stale_time ||
1661
      st.st_mtime < stale_time) {
×
1662
    pool *tmp_pool;
×
1663
    char *msg = "error: stale connection";
1664

1665
    pr_trace_msg(trace_channel, 3,
×
1666
      "unable to accept connection: stale connection");
×
1667

1668
    /* Log the times being compared, to aid in debugging this situation. */
1669
    if (st.st_atime < stale_time) {
×
1670
      time_t age = stale_time - st.st_atime;
×
1671

1672
      pr_trace_msg(trace_channel, 3,
1673
        "last access time of '%s' is %lu secs old (must be less than %u secs)",
1674
        sock->sun_path, (unsigned long) age, max_age);
1675
    }
1676

2✔
1677
    if (st.st_ctime < stale_time) {
1678
      time_t age = stale_time - st.st_ctime;
2✔
1679

2✔
1680
      pr_trace_msg(trace_channel, 3,
2✔
1681
        "last change time of '%s' is %lu secs old (must be less than %u secs)",
1682
        sock->sun_path, (unsigned long) age, max_age);
2✔
1683
    }
1684

2✔
1685
    if (st.st_mtime < stale_time) {
2✔
1686
      time_t age = stale_time - st.st_mtime;
1687

2✔
1688
      pr_trace_msg(trace_channel, 3,
2✔
1689
        "last modified time of '%s' is %lu secs old (must be less than %u "
×
1690
        "secs)", sock->sun_path, (unsigned long) age, max_age);
1691
    }
×
1692

×
1693
    tmp_pool = make_sub_pool(permanent_pool);
×
1694

1695
    if (pr_ctrls_send_response(tmp_pool, cl_fd, -1, 1, &msg) < 0) {
1696
      pr_trace_msg(trace_channel, 2, "error sending message: %s",
2✔
1697
        strerror(errno));
1698
    }
1699

2✔
1700
    destroy_pool(tmp_pool);
2✔
1701
    (void) close(cl_fd);
1702

1703
    errno = ETIMEDOUT;
1704
    return -1;
×
1705
  }
1706

1707
  /* Parse the PID out of the path */
×
1708
  tmp = sock->sun_path;
1709
  tmp += strlen("/tmp/ftp.cl");
×
1710
  cl_pid = atol(tmp);
1711

1712
  /* Return the IDs of the caller */
1713
  if (uid != NULL) {
1714
    *uid = st.st_uid;
1715
  }
1716

1717
  if (gid != NULL) {
1718
    *gid = st.st_gid;
1719
  }
1720

1721
  if (pid != NULL) {
1722
    *pid = cl_pid;
1723
  }
1724

1725
  return 0;
1726
}
1727

1728
int pr_ctrls_accept(int fd, uid_t *uid, gid_t *gid, pid_t *pid,
1729
    unsigned int max_age) {
1730
  socklen_t len = 0;
1731
  struct sockaddr_un sock;
1732
  int cl_fd = -1, res = -1, xerrno;
×
1733

×
1734
  len = sizeof(sock);
1735

×
1736
  cl_fd = accept(fd, (struct sockaddr *) &sock, &len);
×
1737
  xerrno = errno;
1738

1739
  while (cl_fd < 0) {
1740
    if (xerrno == EINTR) {
1741
      pr_signals_handle();
1742

×
1743
      cl_fd = accept(fd, (struct sockaddr *) &sock, &len);
×
1744
      xerrno = errno;
×
1745
      continue;
1746
    }
×
1747

1748
    pr_trace_msg(trace_channel, 3,
1749
      "error: unable to accept on local socket: %s", strerror(xerrno));
46✔
1750

42✔
1751
    errno = xerrno;
3✔
1752
    return -1;
1753
  }
46✔
1754

35✔
1755
  /* NULL terminate the name */
3✔
1756
  sock.sun_path[sizeof(sock.sun_path)-1] = '\0';
1757

4✔
1758
#if defined(SO_PEERCRED)
4✔
1759
  pr_trace_msg(trace_channel, 5,
1760
    "checking client credentials using SO_PEERCRED");
7✔
1761
  res = ctrls_get_creds_peercred(cl_fd, uid, gid, pid);
4✔
1762

1763
#elif !defined(SO_PEERCRED) && defined(HAVE_GETPEEREID)
1✔
1764
  pr_trace_msg(trace_channel, 5,
1✔
1765
    "checking client credentials using getpeereid(2)");
1766
  res = ctrls_get_creds_peereid(cl_fd, uid, gid);
1767

1✔
1768
#elif !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
1✔
1769
      defined(HAVE_GETPEERUCRED)
1770
  pr_trace_msg(trace_channel, 5,
1771
    "checking client credentials using getpeerucred(3)");
1772
  res = ctrls_get_creds_peerucred(cl_fd, uid, gid);
1773

1774
#elif !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
1775
      !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
1776
  pr_trace_msg(trace_channel, 5,
12✔
1777
    "checking client credentials using SCM_CREDS");
12✔
1778
  res = ctrls_get_creds_local(cl_fd, uid, gid, pid);
12✔
1779
#endif
1780

1781
  /* Fallback to the Stevens method of determining connection credentials,
12✔
1782
   * if the kernel-enforced methods did not pan out.
1✔
1783
   */
1✔
1784
  if (res < 0) {
1785
    pr_trace_msg(trace_channel, 5,
1786
      "checking client credentials using Stevens' method");
11✔
1787
    res = ctrls_get_creds_basic(&sock, cl_fd, max_age, uid, gid, pid);
1788
    if (res < 0) {
18✔
1789
      return res;
7✔
1790
    }
1791
  }
7✔
1792

7✔
1793
  /* Done with the path now */
1794
  PRIVS_ROOT
1✔
1795
  (void) unlink(sock.sun_path);
1796
  PRIVS_RELINQUISH
1797

1✔
1798
  return cl_fd;
1799
}
1800

1801
void pr_block_ctrls(void) {
1802
  ctrls_blocked = TRUE;
1803
}
6✔
1804

1✔
1805
void pr_unblock_ctrls(void) {
1806
  ctrls_blocked = FALSE;
1✔
1807
}
1808

1809
int pr_ctrls_check_actions(void) {
1810
  register ctrls_action_t *act = NULL;
5✔
1811

1✔
1812
  for (act = ctrls_action_list; act; act = act->next) {
1813
    if (act->flags & PR_CTRLS_ACT_SOLITARY) {
1✔
1814
      /* This is a territorial action -- only one instance allowed */
1815
      if (ctrls_lookup_action(NULL, act->action, FALSE)) {
1816
        pr_log_pri(PR_LOG_NOTICE,
1817
          "duplicate controls for '%s' action not allowed",
4✔
1818
          act->action);
1✔
1819
        errno = EEXIST;
1820
        return -1;
1✔
1821
      }
1822
    }
1823
  }
3✔
1824

1✔
1825
  return 0;
1826
}
1827

1✔
1828
int pr_run_ctrls(module *mod, const char *action) {
1✔
1829
  register pr_ctrls_t *ctrl = NULL;
1✔
1830
  time_t now;
1831

1832
  /* Are ctrls blocked? */
2✔
1833
  if (ctrls_blocked == TRUE) {
2✔
1834
    errno = EPERM;
1✔
1835
    return -1;
1836
  }
1837

1838
  now = time(NULL);
1✔
1839

1840
  for (ctrl = ctrls_active_list; ctrl; ctrl = ctrl->ctrls_next) {
1841
    int res;
1✔
1842

2✔
1843
    if (mod != NULL &&
1✔
1844
        ctrl->ctrls_module != NULL &&
1✔
1845
        ctrl->ctrls_module != mod) {
1✔
1846
      pr_trace_msg(trace_channel, 19,
1847
        "skipping ctrl due to module mismatch: module = %p, ctrl module = %p",
1✔
1848
        mod, ctrl->ctrls_module);
1849
      continue;
1850
    }
1✔
1851

×
1852
    /* Be watchful of the various client-side flags.  Note: if
1853
     * ctrl->ctrls_cl is ever NULL, it means there's a bug in the code.
1854
     */
×
1855
    if (ctrl->ctrls_cl->cl_flags != PR_CTRLS_CL_HAVEREQ) {
1856
      pr_trace_msg(trace_channel, 19,
1857
        "skipping ctrl due to missing client HAVEREQ flag");
1✔
1858
      continue;
1✔
1859
    }
1✔
1860

1861
    /* Has this control been disabled? */
1✔
1862
    if (ctrl->ctrls_flags & PR_CTRLS_ACT_DISABLED) {
1863
      pr_trace_msg(trace_channel, 19,
1864
        "skipping ctrl due to ACT_DISABLED flag");
1865
      continue;
1866
    }
1867

2✔
1868
    /* Is it time to trigger this ctrl? */
2✔
1869
    if (!(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED)) {
1870
      pr_trace_msg(trace_channel, 19,
1871
        "skipping ctrl due to missing CTRLS_REQUESTED flag");
1872
      continue;
1873
    }
1874

1875
    if (ctrl->ctrls_when > now) {
1876
      pr_trace_msg(trace_channel, 19,
1877
        "skipping ctrl because it is still pending: now = %lu, ctrl when = %lu",
1878
        (unsigned long) now, (unsigned long) ctrl->ctrls_when);
1879
      ctrl->ctrls_flags |= PR_CTRLS_FL_PENDING;
1880
      pr_ctrls_add_response(ctrl, "request pending");
1881
      continue;
1882
    }
1883

1884
    if (action == NULL ||
1885
        strcmp(ctrl->ctrls_action, action) == 0) {
1886
      pr_trace_msg(trace_channel, 7, "calling '%s' control handler",
1887
        ctrl->ctrls_action);
1888

2✔
1889
    } else {
×
1890
      continue;
×
1891
    }
1892

1893
    pr_unblock_ctrls();
1894
    res = ctrl->ctrls_cb(ctrl,
2✔
1895
      (ctrl->ctrls_cb_args ? ctrl->ctrls_cb_args->nelts : 0),
1896
      (ctrl->ctrls_cb_args ? (char **) ctrl->ctrls_cb_args->elts : NULL));
1897
    pr_block_ctrls();
1898

1899
    pr_trace_msg(trace_channel, 19,
1900
      "ran '%s' ctrl, callback value = %d", ctrl->ctrls_action, res);
1901

1902
    if (res >= PR_CTRLS_STATUS_PENDING) {
1903
      pr_trace_msg(trace_channel, 1, "'%s' ctrl returned inappropriate "
10✔
1904
        "value %d, treating as GENERIC_ERROR (%d)", ctrl->ctrls_action, res,
10✔
1905
        PR_CTRLS_STATUS_GENERIC_ERROR);
1906
      res = PR_CTRLS_STATUS_GENERIC_ERROR;
10✔
1907
    }
1✔
1908

1✔
1909
    ctrl->ctrls_flags &= ~PR_CTRLS_FL_REQUESTED;
1910
    ctrl->ctrls_flags &= ~PR_CTRLS_FL_PENDING;
1911
    ctrl->ctrls_flags |= PR_CTRLS_FL_HANDLED;
1912

1913
    ctrl->ctrls_cb_retval = res;
1914
  }
9✔
1915

1916
  return 0;
1917
}
4✔
1918

2✔
1919
int pr_ctrls_reset(void) {
1✔
1920
  pr_ctrls_t *ctrl = NULL;
1921

1922
  /* NOTE: need a clean_ctrls() or somesuch that will, after sending any
1923
   * responses, iterate through the list and "free" any ctrls whose
7✔
1924
   * ctrls_cb_retval is zero.  This feature is used to handle things like
4✔
1925
   * shutdown requests in the future -- the request is only considered
1926
   * "processed" when the callback returns zero.  Any non-zero requests are
1927
   * not cleared, and are considered "pending".  However, this brings up the
9✔
1928
   * complication of an additional request for that action being issued by the
2✔
1929
   * client before the request is processed.  Simplest solution: remove the
1930
   * old request args, and replace them with the new ones.
1931
   *
1932
   * This requires that the return value of the ctrl callback be explicitly
1933
   * documented.
1934
   *
1935
   * How about: ctrls_cb_retval = 1  pending
1936
   *                              0  processed, OK    (reset)
1937
   *                             -1  processed, error (reset)
1938
   */
10✔
1939

10✔
1940
  for (ctrl = ctrls_active_list; ctrl; ctrl = ctrl->ctrls_next) {
1941
    if (ctrl->ctrls_cb_retval < PR_CTRLS_STATUS_PENDING) {
10✔
1942
      pr_ctrls_free(ctrl);
1✔
1943
    }
1✔
1944
  }
1945

1946
  return 0;
1947
}
1948

1949
/* From include/mod_ctrls.h */
9✔
1950

1951
/* Returns TRUE if the given cl_gid is allowed by the group ACL, FALSE
1952
 * otherwise. Note that the default is to deny everyone, unless an ACL has
4✔
1953
 * been configured.
2✔
1954
 */
1✔
1955
int pr_ctrls_check_group_acl(gid_t cl_gid, const ctrls_group_acl_t *group_acl) {
1956
  int res = FALSE;
1957

1958
  if (group_acl == NULL) {
7✔
1959
    errno = EINVAL;
3✔
1960
    return -1;
1961
  }
1962

9✔
1963
  /* Note: the special condition of ngids of 1 and gids of NULL signals
2✔
1964
   * that all groups are to be treated according to the allow member.
1965
   */
1966
  if (group_acl->gids != NULL) {
1967
    register unsigned int i = 0;
1968

1969
    for (i = 0; i < group_acl->ngids; i++) {
1970
      if ((group_acl->gids)[i] == cl_gid) {
9✔
1971
        res = TRUE;
1972
      }
9✔
1973
    }
1974

9✔
1975
  } else if (group_acl->ngids == 1) {
8✔
1976
    res = TRUE;
7✔
1977
  }
7✔
1978

4✔
1979
  if (!group_acl->allow) {
4✔
1980
    res = !res;
1981
  }
1982

8✔
1983
  return res;
5✔
1984
}
4✔
1985

1986
/* Returns TRUE if the given cl_uid is allowed by the user ACL, FALSE
4✔
1987
 * otherwise. Note that the default is to deny everyone, unless an ACL has
6✔
1988
 * been configured.
3✔
1989
 */
3✔
1990
int pr_ctrls_check_user_acl(uid_t cl_uid, const ctrls_user_acl_t *user_acl) {
3✔
1991
  int res = FALSE;
1992

1993
  if (user_acl == NULL) {
4✔
1994
    errno = EINVAL;
4✔
1995
    return -1;
1996
  }
1997

1998
  /* Note: the special condition of nuids of 1 and uids of NULL signals
1999
   * that all users are to be treated according to the allow member.
2000
   */
2001
  if (user_acl->uids != NULL) {
2002
    register unsigned int i = 0;
2003

7✔
2004
    for (i = 0; i < user_acl->nuids; i++) {
7✔
2005
      if ((user_acl->uids)[i] == cl_uid) {
1✔
2006
        res = TRUE;
1✔
2007
      }
2008
    }
2009

6✔
2010
  } else if (user_acl->nuids == 1) {
6✔
2011
    res = TRUE;
2012
  }
6✔
2013

2014
  if (!user_acl->allow) {
2015
    res = !res;
46✔
2016
  }
46✔
2017

46✔
2018
  return res;
2019
}
46✔
2020

46✔
2021
/* Returns TRUE for allowed, FALSE for denied. */
46✔
2022
int pr_ctrls_check_acl(const pr_ctrls_t *ctrl,
12✔
2023
    const ctrls_acttab_t *acttab, const char *action) {
12✔
2024
  register unsigned int i = 0;
2025

2026
  if (ctrl == NULL ||
34✔
2027
      ctrl->ctrls_cl == NULL ||
34✔
2028
      acttab == NULL ||
×
2029
      action == NULL) {
2030
    errno = EINVAL;
2031
    return -1;
34✔
2032
  }
2033

2034
  for (i = 0; acttab[i].act_action; i++) {
2035
    if (strcmp(acttab[i].act_action, action) == 0) {
34✔
2036
      int user_check = FALSE, group_check = FALSE;
2037

34✔
2038
      if (acttab[i].act_acl != NULL) {
×
2039
        user_check = pr_ctrls_check_user_acl(ctrl->ctrls_cl->cl_uid,
×
2040
          &(acttab[i].act_acl->acl_users));
2041
        group_check = pr_ctrls_check_group_acl(ctrl->ctrls_cl->cl_gid,
2042
          &(acttab[i].act_acl->acl_groups));
240✔
2043
      }
103✔
2044

2045
      if (user_check != TRUE &&
103✔
2046
          group_check != TRUE) {
2047
        return FALSE;
×
2048
      }
×
2049
    }
2050
  }
2051

2052
  return TRUE;
103✔
2053
}
103✔
2054

2055
int pr_ctrls_init_acl(ctrls_acl_t *acl) {
2056
  if (acl == NULL) {
34✔
2057
    errno = EINVAL;
22✔
2058
    return -1;
2059
  }
2060

34✔
2061
  memset(acl, 0, sizeof(ctrls_acl_t));
34✔
2062
  acl->acl_users.allow = acl->acl_groups.allow = TRUE;
2063

2064
  return 0;
14✔
2065
}
14✔
2066

14✔
2067
static char *ctrls_argsep(char **arg) {
14✔
2068
  char *ret = NULL, *dst = NULL;
2069
  char quote_mode = 0;
14✔
2070

14✔
2071
  if (arg == NULL ||
2✔
2072
      !*arg ||
2✔
2073
      !**arg) {
2074
    errno = EINVAL;
2075
    return NULL;
12✔
2076
  }
12✔
2077

2078
  while (**arg &&
2079
         PR_ISSPACE(**arg)) {
12✔
2080
    (*arg)++;
2081
  }
2082

46✔
2083
  if (!**arg) {
34✔
2084
    return NULL;
2085
  }
34✔
2086

2087
  ret = dst = *arg;
2088

34✔
2089
  if (**arg == '\"') {
2090
    quote_mode++;
2091
    (*arg)++;
2092
  }
12✔
2093

2094
  while (**arg && **arg != ',' &&
12✔
2095
      (quote_mode ? (**arg != '\"') : (!PR_ISSPACE(**arg)))) {
12✔
2096

2097
    if (**arg == '\\' && quote_mode) {
2098
      /* escaped char */
12✔
2099
      if (*((*arg) + 1)) {
2100
        *dst = *(++(*arg));
2101
      }
8✔
2102
    }
2103

8✔
2104
    *dst++ = **arg;
8✔
2105
    ++(*arg);
8✔
2106
  }
8✔
2107

2108
  if (**arg) {
8✔
2109
    (*arg)++;
8✔
2110
  }
6✔
2111

6✔
2112
  *dst = '\0';
4✔
2113
  return ret;
4✔
2114
}
2115

2116
char **pr_ctrls_parse_acl(pool *acl_pool, const char *acl_text) {
4✔
2117
  char *name = NULL, *acl_text_dup = NULL, **acl_list = NULL;
2118
  array_header *acl_arr = NULL;
4✔
2119
  pool *tmp_pool = NULL;
3✔
2120

2121
  if (acl_pool == NULL ||
2122
      acl_text == NULL) {
1✔
2123
    errno = EINVAL;
2124
    return NULL;
2125
  }
2126

2127
  tmp_pool = make_sub_pool(acl_pool);
2128
  acl_text_dup = pstrdup(tmp_pool, acl_text);
4✔
2129

2130
  /* Allocate an array */
2131
  acl_arr = make_array(acl_pool, 0, sizeof(char **));
4✔
2132

2133
  /* Add each name to the array */
15✔
2134
  while ((name = ctrls_argsep(&acl_text_dup)) != NULL) {
2135
    char *text;
2136

12✔
2137
    text = pstrdup(acl_pool, name);
1✔
2138

1✔
2139
    /* Push the name into the ACL array */
1✔
2140
    *((char **) push_array(acl_arr)) = text;
1✔
2141
  }
2142

2143
  /* Terminate the temp array with a NULL, as is proper. */
11✔
2144
  *((char **) push_array(acl_arr)) = NULL;
11✔
2145

11✔
2146
  acl_list = (char **) acl_arr->elts;
2147
  destroy_pool(tmp_pool);
2148

×
2149
  /* return the array of names */
2150
  return acl_list;
2151
}
3✔
2152

3✔
2153
int pr_ctrls_set_group_acl(pool *group_acl_pool, ctrls_group_acl_t *group_acl,
2154
    const char *allow, char *grouplist) {
3✔
2155
  char *group = NULL, **groups = NULL;
3✔
2156
  array_header *gid_list = NULL;
2157
  gid_t gid = 0;
2158
  pool *tmp_pool = NULL;
10✔
2159

2160
  if (group_acl_pool == NULL ||
10✔
2161
      group_acl == NULL ||
10✔
2162
      allow == NULL ||
10✔
2163
      grouplist == NULL) {
10✔
2164
    errno = EINVAL;
2165
    return -1;
2166
  }
10✔
2167

10✔
2168
  tmp_pool = make_sub_pool(group_acl_pool);
8✔
2169

8✔
2170
  if (strcasecmp(allow, "allow") == 0) {
4✔
2171
    group_acl->allow = TRUE;
4✔
2172

2173
  } else {
2174
    group_acl->allow = FALSE;
6✔
2175
  }
2176

6✔
2177
  /* Parse the given expression into an array, then retrieve the GID
5✔
2178
   * for each given name.
2179
   */
2180
  groups = pr_ctrls_parse_acl(group_acl_pool, grouplist);
1✔
2181

2182
  /* Allocate an array of gid_t's */
2183
  gid_list = make_array(group_acl_pool, 0, sizeof(gid_t));
2184

2185
  for (group = *groups; group != NULL; group = *++groups) {
2186

6✔
2187
    /* Handle a group name of "*" differently. */
2188
    if (strcmp(group, "*") == 0) {
2189
      group_acl->ngids = 1;
6✔
2190
      group_acl->gids = NULL;
2191
      destroy_pool(tmp_pool);
23✔
2192
      return 0;
2193
    }
2194

18✔
2195
    gid = pr_auth_name2gid(tmp_pool, group);
1✔
2196
    if (gid == (gid_t) -1) {
1✔
2197
      continue;
1✔
2198
    }
1✔
2199

2200
    *((gid_t *) push_array(gid_list)) = gid;
2201
  }
17✔
2202

17✔
2203
  group_acl->ngids = gid_list->nelts;
17✔
2204
  group_acl->gids = (gid_t *) gid_list->elts;
2205

2206
  destroy_pool(tmp_pool);
×
2207
  return 0;
2208
}
2209

5✔
2210
int pr_ctrls_set_user_acl(pool *user_acl_pool, ctrls_user_acl_t *user_acl,
5✔
2211
    const char *allow, char *userlist) {
2212
  char *user = NULL, **users = NULL;
5✔
2213
  array_header *uid_list = NULL;
5✔
2214
  uid_t uid = 0;
2215
  pool *tmp_pool = NULL;
2216

23✔
2217
  /* Sanity checks */
2218
  if (user_acl_pool == NULL ||
2219
      user_acl == NULL ||
23✔
2220
      allow == NULL ||
23✔
2221
      userlist == NULL) {
2222
    errno = EINVAL;
23✔
2223
    return -1;
23✔
2224
  }
19✔
2225

19✔
2226
  tmp_pool = make_sub_pool(user_acl_pool);
2227

12✔
2228
  if (strcasecmp(allow, "allow") == 0) {
12✔
2229
    user_acl->allow = TRUE;
2230

2231
  } else {
11✔
2232
    user_acl->allow = FALSE;
5✔
2233
  }
3✔
2234

3✔
2235
  /* Parse the given expression into an array, then retrieve the UID
2236
   * for each given name.
2237
   */
2238
  users = pr_ctrls_parse_acl(user_acl_pool, userlist);
2239

2240
  /* Allocate an array of uid_t's */
14✔
2241
  uid_list = make_array(user_acl_pool, 0, sizeof(uid_t));
8✔
2242

8✔
2243
  for (user = *users; user != NULL; user = *++users) {
2244

8✔
2245
    /* Handle a user name of "*" differently. */
2✔
2246
    if (strcmp(user, "*") == 0) {
2247
      user_acl->nuids = 1;
2248
      user_acl->uids = NULL;
8✔
2249
      destroy_pool(tmp_pool);
6✔
2250
      return 0;
2251
    }
2252

2253
    uid = pr_auth_name2uid(tmp_pool, user);
2254
    if (uid == (uid_t) -1) {
2255
      continue;
6✔
2256
    }
2✔
2257

2✔
2258
    *((uid_t *) push_array(uid_list)) = uid;
2✔
2259
  }
2260

2261
  user_acl->nuids = uid_list->nelts;
2262
  user_acl->uids = (uid_t *) uid_list->elts;
12✔
2263

6✔
2264
  destroy_pool(tmp_pool);
2265
  return 0;
6✔
2266
}
6✔
2267

2✔
2268
int pr_ctrls_set_module_acls2(ctrls_acttab_t *acttab, pool *acl_pool,
2269
    char **actions, const char *allow, const char *type, char *list,
2270
    const char **bad_action) {
12✔
2271
  register unsigned int i = 0;
6✔
2272
  int all_actions = FALSE;
2273

6✔
2274
  if (acttab == NULL ||
4✔
2275
      acl_pool == NULL ||
2276
      actions == NULL ||
2277
      type == NULL ||
2278
      bad_action == NULL) {
2279
    errno = EINVAL;
6✔
2280
    return -1;
4✔
2281
  }
4✔
2282

2283
  if (strcasecmp(type, "user") != 0 &&
2✔
2284
      strcasecmp(type, "group") != 0) {
2✔
2285
    errno = EINVAL;
2✔
2286
    return -1;
2287
  }
2288

6✔
2289
  /* First, sanity check the given list of actions against the actions
×
2290
   * in the given table.
×
2291
   */
2292
  for (i = 0; actions[i]; i++) {
2293
    register unsigned int j = 0;
2294
    int valid_action = FALSE;
2295

2296
    if (strcasecmp(actions[i], "all") == 0) {
2297
      continue;
2298
    }
2299

11✔
2300
    for (j = 0; acttab[j].act_action; j++) {
2301
      if (strcmp(actions[i], acttab[j].act_action) == 0) {
11✔
2302
        valid_action = TRUE;
11✔
2303
        break;
2304
      }
11✔
2305
    }
2306

11✔
2307
    if (valid_action == FALSE) {
8✔
2308
      *bad_action = actions[i];
2309
      errno = EPERM;
2310
      return -1;
2311
    }
2312
  }
2313

11✔
2314
  for (i = 0; actions[i]; i++) {
2315
    register unsigned int j = 0;
11✔
2316

2317
    if (all_actions == FALSE &&
11✔
2318
        strcasecmp(actions[i], "all") == 0) {
11✔
2319
      all_actions = TRUE;
7✔
2320
    }
7✔
2321

7✔
2322
    for (j = 0; acttab[j].act_action; j++) {
7✔
2323
      int res = 0;
2324

2325
      if (all_actions == TRUE ||
2326
          strcmp(actions[i], acttab[j].act_action) == 0) {
2327

2328
        /* Use the type parameter to determine whether the list is of users or
6✔
2329
         * of groups.
2330
         */
6✔
2331
        if (strcasecmp(type, "user") == 0) {
2332
          res = pr_ctrls_set_user_acl(acl_pool,
6✔
2333
            &(acttab[j].act_acl->acl_users), allow, list);
4✔
2334

2335
        } else if (strcasecmp(type, "group") == 0) {
2336
          res = pr_ctrls_set_group_acl(acl_pool,
2337
            &(acttab[j].act_acl->acl_groups), allow, list);
2338
        }
2339

4✔
2340
        if (res < 0) {
2✔
2341
          *bad_action = actions[i];
2✔
2342
          return -1;
2✔
2343
        }
2344
      }
2345
    }
2346
  }
2347

2348
  return 0;
2349
}
4✔
2350

2351
char *pr_ctrls_set_module_acls(ctrls_acttab_t *acttab, pool *acl_pool,
2✔
2352
    char **actions, const char *allow, const char *type, char *list) {
2353
  int res;
2✔
2354
  char *bad_action = NULL;
2✔
2355

2356
  res = pr_ctrls_set_module_acls2(acttab, acl_pool, actions, allow, type, list,
2357
    (const char **) &bad_action);
2358
  if (res < 0) {
2359
    return bad_action;
2360
  }
2✔
2361

2✔
2362
  return 0;
2363
}
2✔
2364

2✔
2365
int pr_ctrls_unregister_module_actions2(ctrls_acttab_t *acttab,
2366
    char **actions, module *mod, const char **bad_action) {
2367
  register unsigned int i = 0;
2368

2369
  if (acttab == NULL ||
2370
      actions == NULL ||
2371
      mod == NULL ||
5✔
2372
      bad_action == NULL) {
2373
    errno = EINVAL;
5✔
2374
    return -1;
5✔
2375
  }
2376

5✔
2377
  /* First, sanity check the given actions against the actions supported by
2378
   * this module.
5✔
2379
   */
4✔
2380
  for (i = 0; actions[i]; i++) {
2381
    register unsigned int j = 0;
2382
    int valid_action = FALSE;
2383

2384
    for (j = 0; acttab[j].act_action; j++) {
2385
      if (strcmp(actions[i], acttab[j].act_action) == 0) {
4✔
2386
        valid_action = TRUE;
2387
        break;
2388
      }
4✔
2389
    }
1✔
2390

2391
    if (valid_action == FALSE) {
2392
      *bad_action = actions[i];
4✔
2393
      errno = EPERM;
4✔
2394
      return -1;
2395
    }
2396
  }
4✔
2397

4✔
2398
  /* Next, iterate through both lists again, looking for actions of the
4✔
2399
   * module _not_ in the given list.
2400
   */
4✔
2401
  for (i = 0; acttab[i].act_action; i++) {
2402
    register unsigned int j = 0;
2403
    int have_action = FALSE;
2404

3✔
2405
    for (j = 0; actions[j]; j++) {
2406
      if (strcmp(acttab[i].act_action, actions[j]) == 0) {
2407
        have_action = TRUE;
2408
        break;
1✔
2409
      }
1✔
2410
    }
1✔
2411

2412
    if (have_action == TRUE) {
1✔
2413
      pr_trace_msg(trace_channel, 4, "mod_%s.c: removing '%s' control",
2414
        mod->name, acttab[i].act_action);
2415
      pr_ctrls_unregister(mod, acttab[i].act_action);
34✔
2416
      destroy_pool(acttab[i].act_acl->acl_pool);
34✔
2417
    }
34✔
2418
  }
34✔
2419

34✔
2420
  return 0;
2421
}
34✔
2422

34✔
2423
char *pr_ctrls_unregister_module_actions(ctrls_acttab_t *acttab,
34✔
2424
    char **actions, module *mod) {
34✔
2425
  int res;
2426
  char *bad_action = NULL;
2427

34✔
2428
  res = pr_ctrls_unregister_module_actions2(acttab, actions, mod,
34✔
2429
    (const char **) &bad_action);
34✔
2430
  if (res < 0) {
34✔
2431
    return bad_action;
34✔
2432
  }
2433

34✔
2434
  return 0;
×
2435
}
2436

2437
int pr_ctrls_set_logfd(int fd) {
34✔
2438

34✔
2439
  /* Close any existing log fd. */
34✔
2440
  if (ctrls_logfd >= 0) {
2441
    (void) close(ctrls_logfd);
2442
  }
34✔
2443

34✔
2444
  ctrls_logfd = fd;
34✔
2445
  return 0;
2446
}
2447

34✔
2448
int pr_ctrls_log(const char *module_version, const char *fmt, ...) {
34✔
2449
  va_list msg;
34✔
2450
  int res;
2451

2452
  if (ctrls_logfd < 0) {
2453
    return 0;
2454
  }
2455

2456
  if (fmt == NULL) {
34✔
2457
    return 0;
34✔
2458
  }
2459

34✔
2460
  va_start(msg, fmt);
×
2461
  res = pr_log_vwritefile(ctrls_logfd, module_version, fmt, msg);
2462
  va_end(msg);
2463

×
2464
  return res;
×
2465
}
2466

2467
static void ctrls_cleanup_cb(void *user_data) {
34✔
2468
  ctrls_pool = NULL;
34✔
2469
  ctrls_action_list = NULL;
34✔
2470
  ctrls_active_list = NULL;
34✔
2471
  ctrls_free_list = NULL;
2472

34✔
2473
  action_lookup_next = NULL;
×
2474
  action_lookup_action = NULL;
2475
  action_lookup_module = NULL;
×
2476
}
2477

×
2478
/* Initialize the Controls API. */
×
2479
int init_ctrls2(const char *socket_path) {
2480
  struct stat st;
×
2481
  int fd, xerrno;
×
2482
  struct sockaddr_un sockun;
2483
  size_t socklen;
2484

34✔
2485
  if (ctrls_pool != NULL) {
×
2486
    destroy_pool(ctrls_pool);
2487
  }
×
2488

2489
  ctrls_pool = make_sub_pool(permanent_pool);
×
2490
  pr_pool_tag(ctrls_pool, "Controls Pool");
×
2491
  register_cleanup2(ctrls_pool, NULL, ctrls_cleanup_cb);
2492

×
2493
  /* Make sure all of the lists are zero'd out. */
×
2494
  ctrls_action_list = NULL;
2495
  ctrls_active_list = NULL;
2496
  ctrls_free_list = NULL;
2497

34✔
2498
   /* And that the lookup indices are (re)set as well... */
34✔
2499
  action_lookup_next = NULL;
×
2500
  action_lookup_action = NULL;
2501
  action_lookup_module = NULL;
2502

2503
  /* Run-time check to find out whether this platform identifies a
2504
   * Unix domain socket file descriptor via the S_ISFIFO macro, or
2505
   * the S_ISSOCK macro.
2506
   */
2507

34✔
2508
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
34✔
2509
  xerrno = errno;
34✔
2510

2511
  if (fd < 0) {
2512
    pr_log_debug(DEBUG10, "unable to create Unix domain socket: %s",
2513
      strerror(xerrno));
2514

2515
    errno = xerrno;
2516
    return -1;
34✔
2517
  }
2518

34✔
2519
  memset(&sockun, 0, sizeof(sockun));
2520
  sockun.sun_family = AF_UNIX;
34✔
2521
  sstrncpy(sockun.sun_path, socket_path, sizeof(sockun.sun_path));
34✔
2522
  socklen = sizeof(struct sockaddr_un);
34✔
2523

2524
  if (bind(fd, (struct sockaddr *) &sockun, socklen) < 0) {
2525
    xerrno = errno;
×
2526

×
2527
    pr_log_debug(DEBUG10, "unable to bind to Unix domain socket at '%s': %s",
2528
      socket_path, strerror(xerrno));
×
2529
    (void) close(fd);
×
2530
    (void) unlink(socket_path);
2531

2532
    errno = xerrno;
2533
    return -1;
2534
  }
2535

2536
  if (fstat(fd, &st) < 0) {
2537
    xerrno = errno;
2538

2539
    pr_log_debug(DEBUG10, "unable to stat Unix domain socket at '%s': %s",
2540
      socket_path, strerror(xerrno));
2541
    (void) close(fd);
2542
    (void) unlink(socket_path);
2543

2544
    errno = xerrno;
2545
    return -1;
2546
  }
2547

2548
#if defined(S_ISFIFO)
2549
  pr_trace_msg(trace_channel, 9, "testing Unix domain socket using S_ISFIFO");
2550
  if (S_ISFIFO(st.st_mode)) {
2551
    ctrls_use_isfifo = TRUE;
2552
  }
2553
#else
2554
  pr_log_debug(DEBUG10, "cannot test Unix domain socket using S_ISFIFO: "
2555
    "macro undefined");
2556
#endif /* S_ISFIFO */
2557

2558
#if defined(S_ISSOCK)
2559
  pr_trace_msg(trace_channel, 9, "testing Unix domain socket using S_ISSOCK");
2560
  if (S_ISSOCK(st.st_mode)) {
2561
    ctrls_use_isfifo = FALSE;
2562
  }
2563
#else
2564
  pr_log_debug(DEBUG10, "cannot test Unix domain socket using S_ISSOCK: "
2565
    "macro undefined");
2566
#endif /* S_ISSOCK */
2567

2568
  pr_trace_msg(trace_channel, 9,
2569
    "using %s macro for Unix domain socket detection",
2570
    ctrls_use_isfifo ? "S_ISFIFO" : "S_ISSOCK");
2571

2572
  (void) close(fd);
2573
  (void) unlink(socket_path);
2574
  return 0;
2575
}
2576

2577
void init_ctrls(void) {
2578
  const char *socket_path = PR_RUN_DIR "/test.sock";
2579

2580
  (void) init_ctrls2(socket_path);
2581
}
2582
#endif /* PR_USE_CTRLS */
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