/
proto_detail_file.c
727 lines (594 loc) · 18.2 KB
/
proto_detail_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
* @file proto_detail_file.c
* @brief Detail handler for files
*
* @copyright 2017 The FreeRADIUS server project.
* @copyright 2017 Alan DeKok (aland@deployingradius.com)
*/
#include <netdb.h>
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/protocol.h>
#include <freeradius-devel/radius/radius.h>
#include <freeradius-devel/io/io.h>
#include <freeradius-devel/io/application.h>
#include <freeradius-devel/io/listen.h>
#include <freeradius-devel/io/schedule.h>
#include <freeradius-devel/rad_assert.h>
#include "proto_detail.h"
#include <fcntl.h>
#include <sys/stat.h>
#ifdef HAVE_GLOB_H
#include <glob.h>
#else
#error proto_detail_file requires <glob.h>
#endif
#if 0
/*
* When we want detailed debugging here, without detailed server
* debugging.
*/
#define MPRINT DEBUG
#else
#define MPRINT DEBUG3
#endif
/*
* For talloc names, ".name = detail_file", and dl.c prepends "proto_", and appends "_t".
*/
typedef struct proto_detail_work_t proto_detail_file_t;
static void work_init(proto_detail_file_t *inst);
static void mod_vnode_delete(fr_event_list_t *el, int fd, UNUSED int fflags, void *ctx);
static const CONF_PARSER file_listen_config[] = {
{ FR_CONF_OFFSET("filename", FR_TYPE_STRING | FR_TYPE_REQUIRED, proto_detail_file_t, filename ) },
{ FR_CONF_OFFSET("filename.work", FR_TYPE_STRING, proto_detail_work_t, filename_work ) },
{ FR_CONF_OFFSET("track", FR_TYPE_BOOL, proto_detail_file_t, track_progress), .dflt = "yes" },
{ FR_CONF_OFFSET("poll_interval", FR_TYPE_UINT32, proto_detail_file_t, poll_interval), .dflt = "5" },
CONF_PARSER_TERMINATOR
};
/*
* All of the decoding is done by proto_detail and proto_detail_work
*/
static int mod_decode(void const *instance, REQUEST *request, uint8_t *const data, size_t data_len)
{
proto_detail_file_t const *inst = talloc_get_type_abort_const(instance, proto_detail_file_t);
return inst->parent->work_io->decode(inst->parent->work_io_instance, request, data, data_len);
}
static ssize_t mod_write(void *instance, void *packet_ctx,
fr_time_t request_time, uint8_t *buffer, size_t buffer_len)
{
proto_detail_file_t const *inst = talloc_get_type_abort_const(instance, proto_detail_file_t);
return inst->parent->work_io->write(inst->parent->work_io_instance, packet_ctx, request_time, buffer, buffer_len);
}
static void mod_vnode_extend(void *instance, UNUSED uint32_t fflags)
{
proto_detail_file_t *inst = talloc_get_type_abort(instance, proto_detail_file_t);
bool has_worker = false;
PTHREAD_MUTEX_LOCK(&inst->parent->worker_mutex);
has_worker = (inst->parent->num_workers != 0);
PTHREAD_MUTEX_UNLOCK(&inst->parent->worker_mutex);
if (has_worker) return;
if (inst->ev) fr_event_timer_delete(inst->el, &inst->ev);
work_init(inst);
}
/** Open a detail listener
*
* @param[in] instance of the detail worker.
* @return
* - <0 on error
* - 0 on success
*/
static int mod_open(void *instance)
{
proto_detail_file_t *inst = talloc_get_type_abort(instance, proto_detail_file_t);
int oflag;
#ifdef O_EVTONLY
oflag = O_EVTONLY;
#else
oflag = O_RDONLY;
#endif
inst->fd = open(inst->directory, oflag);
if (inst->fd < 0) {
cf_log_err(inst->cs, "Failed opening %s: %s", inst->directory, fr_syserror(errno));
return -1;
}
rad_assert(inst->name == NULL);
inst->name = talloc_typed_asprintf(inst, "detail directory %s", inst->directory);
DEBUG("Listening on %s bound to virtual server %s FD %d",
inst->name, cf_section_name2(inst->parent->server_cs), inst->fd);
return 0;
}
/** Get the file descriptor for this IO instance
*
* @param[in] instance of the detail worker
* @return the file descriptor
*/
static int mod_fd(void const *instance)
{
proto_detail_file_t const *inst = talloc_get_type_abort_const(instance, proto_detail_file_t);
return inst->fd;
}
/*
* The "detail.work" file doesn't exist. Let's see if we can rename one.
*/
static int work_rename(proto_detail_file_t *inst)
{
unsigned int i;
int found;
time_t chtime;
char const *filename;
glob_t files;
struct stat st;
DEBUG3("proto_detail (%s): polling for detail files in %s",
inst->name, inst->directory);
memset(&files, 0, sizeof(files));
if (glob(inst->filename, 0, NULL, &files) != 0) {
noop:
DEBUG3("proto_detail (%s): no matching files for %s",
inst->name, inst->filename);
globfree(&files);
return -1;
}
/*
* Loop over the glob'd files, looking for the
* oldest one.
*/
chtime = 0;
found = -1;
for (i = 0; i < files.gl_pathc; i++) {
if (stat(files.gl_pathv[i], &st) < 0) continue;
if ((i == 0) || (st.st_ctime < chtime)) {
chtime = st.st_ctime;
found = i;
}
}
/*
* No matching files, reset the timer and continue.
*/
if (found < 0) goto noop;
/*
* Rename detail to detail.work
*/
filename = files.gl_pathv[found];
DEBUG("proto_detail (%s): Renaming %s -> %s", inst->name, filename, inst->filename_work);
if (rename(filename, inst->filename_work) < 0) {
ERROR("detail (%s): Failed renaming %s to %s: %s",
inst->name, filename, inst->filename_work, fr_syserror(errno));
goto noop;
}
globfree(&files); /* Shouldn't be using anything in files now */
/*
* The file should now exist, return the open'd FD.
*/
return open(inst->filename_work, inst->mode);
}
/*
* Start polling again after a timeout.
*/
static void work_retry_timer(UNUSED fr_event_list_t *el, UNUSED struct timeval *now, void *uctx)
{
proto_detail_file_t *inst = talloc_get_type_abort(uctx, proto_detail_file_t);
work_init(inst);
}
/*
* The "detail.work" file exists.
*/
static int work_exists(proto_detail_file_t *inst, int fd)
{
bool opened = false;
proto_detail_work_t *work;
fr_listen_t *listen = NULL;
struct stat st;
fr_event_vnode_func_t funcs = { .delete = mod_vnode_delete };
DEBUG3("proto_detail (%s): Trying to lock %s", inst->name, inst->filename_work);
/*
* "detail.work" exists, try to lock it.
*/
if (rad_lockfd_nonblock(fd, 0) < 0) {
struct timeval when, now;
DEBUG3("proto_detail (%s): Failed locking %s: %s",
inst->name, inst->filename_work, fr_syserror(errno));
close(fd);
when.tv_usec = inst->lock_interval % USEC;
when.tv_sec = inst->lock_interval / USEC;
/*
* Ensure that we don't do massive busy-polling.
*/
inst->lock_interval += inst->lock_interval / 2;
if (inst->lock_interval > (30 * USEC)) inst->lock_interval = 30 * USEC;
DEBUG3("proto_detail (%s): Waiting %d.%06ds for lock on file %s",
inst->name, (int) when.tv_sec, (int) when.tv_usec, inst->filename_work);
gettimeofday(&now, NULL);
fr_timeval_add(&when, &when, &now);
if (fr_event_timer_insert(inst, inst->el, &inst->ev,
&when, work_retry_timer, inst) < 0) {
ERROR("Failed inserting retry timer for %s", inst->filename_work);
}
return 0;
}
DEBUG3("proto_detail (%s): Obtained lock and starting to process file %s",
inst->name, inst->filename_work);
/*
* Ignore empty files.
*/
if (fstat(fd, &st) < 0) {
ERROR("Failed opening %s: %s", inst->filename_work,
fr_syserror(errno));
unlink(inst->filename_work);
close(fd);
return -1;
}
if (!st.st_size) {
DEBUG3("proto_detail (%s): %s file is empty, ignoring it.",
inst->name, inst->filename_work);
unlink(inst->filename_work);
close(fd);
return -1;
}
MEM(listen = talloc_zero(NULL, fr_listen_t));
/*
* Create a new listener, and insert it into the
* scheduler. Shamelessly copied from proto_detail.c
* mod_open(), with changes.
*
* This listener is parented from the worker. So that
* when the worker goes away, so does the listener.
*/
listen->app_io = inst->parent->work_io;
listen->app = inst->parent->self;
listen->app_instance = inst->parent;
listen->server_cs = inst->parent->server_cs;
/*
* The worker may be in a different thread, so avoid
* talloc threading issues by using a NULL TALLOC_CTX.
*/
listen->app_io_instance = work = talloc(listen, proto_detail_work_t);
if (!work) {
talloc_free(listen);
close(fd);
DEBUG("Failed allocating memory");
return -1;
}
memcpy(work, inst->parent->work_submodule->data, sizeof(*work));
work->ev = NULL;
work->fd = dup(fd);
if (work->fd < 0) {
struct timeval when, now;
DEBUG("proto_detail (%s): Failed opening %s: %s",
inst->name, inst->filename_work, fr_syserror(errno));
close(fd);
talloc_free(listen);
when.tv_sec = 0;
when.tv_usec = 10; /* hard-code! */
DEBUG3("proto_detail (%s): Waiting %d.%06ds for lock on file %s",
inst->name, (int) when.tv_sec, (int) when.tv_usec, inst->filename_work);
gettimeofday(&now, NULL);
fr_timeval_add(&when, &when, &now);
if (fr_event_timer_insert(inst, inst->el, &inst->ev,
&when, work_retry_timer, inst) < 0) {
ERROR("Failed inserting retry timer for %s", inst->filename_work);
}
return 0;
}
/*
* Don't do anything until the file has been deleted.
*
* @todo - ensure that proto_detail_work is done the file...
* maybe by creating a new instance?
*/
if (fr_event_filter_insert(inst, inst->el, fd, FR_EVENT_FILTER_VNODE,
&funcs, NULL, inst) < 0) {
ERROR("Failed adding work socket to event loop: %s", fr_strerror());
close(fd);
goto detach;
}
/*
* Yuck.
*/
inst->parent->work_io_instance = work;
work->filename_work = talloc_strdup(work, inst->filename_work);
/*
* Set configurable parameters for message ring buffer.
*/
listen->default_message_size = inst->parent->max_packet_size;
listen->num_messages = inst->parent->num_messages;
PTHREAD_MUTEX_LOCK(&inst->parent->worker_mutex);
inst->parent->num_workers++;
PTHREAD_MUTEX_UNLOCK(&inst->parent->worker_mutex);
/*
* Instantiate the new worker.
*/
if (listen->app_io->instantiate &&
(listen->app_io->instantiate(listen->app_io_instance,
inst->parent->work_io_conf) < 0)) {
ERROR("Failed instantiating %s", listen->app_io->name);
goto error;
}
/*
* Open the detail.work file.
*/
if (listen->app_io->open(listen->app_io_instance) < 0) {
ERROR("Failed opening %s", listen->app_io->name);
goto error;
}
opened = true;
if (!fr_schedule_socket_add(inst->parent->sc, listen)) {
error:
if (fr_event_fd_delete(inst->el, fd, FR_EVENT_FILTER_VNODE) < 0) {
ERROR("Failed removing DELETE callback on add: %s", fr_strerror());
}
if (opened) {
(void) listen->app_io->close(listen->app_io_instance);
listen = NULL;
} else {
close(fd);
}
detach:
if (listen) (void) listen->app_io->detach(listen->app_io_instance);
talloc_free(listen);
return -1;
}
/*
* Tell the worker to clean itself up.
*/
work->free_on_close = true;
inst->vnode_fd = fd;
return 0;
}
static void mod_vnode_delete(fr_event_list_t *el, int fd, UNUSED int fflags, void *ctx)
{
proto_detail_file_t *inst = talloc_get_type_abort(ctx, proto_detail_file_t);
DEBUG("proto_detail (%s): Deleted %s", inst->name, inst->filename_work);
if (fd != inst->vnode_fd) {
ERROR("Received DELETE for FD %d, when we were expecting one on FD %d - ignoring it",
fd, inst->vnode_fd);
return;
}
if (fr_event_fd_delete(el, fd, FR_EVENT_FILTER_VNODE) < 0) {
ERROR("Failed removing DELETE callback after deletion: %s", fr_strerror());
}
close(fd);
inst->vnode_fd = -1;
/*
* Re-initialize the state machine.
*
* Note that a "delete" may be the result of an atomic
* "move", which both deletes the old file, and creates
* the new one.
*/
work_init(inst);
}
static void work_init(proto_detail_file_t *inst)
{
int fd;
bool has_worker;
PTHREAD_MUTEX_LOCK(&inst->parent->worker_mutex);
has_worker = (inst->parent->num_workers != 0);
PTHREAD_MUTEX_UNLOCK(&inst->parent->worker_mutex);
/*
* The worker is still processing the file, poll until
* it's done.
*/
if (has_worker) {
DEBUG3("proto_detail (%s): worker %s is still alive, waiting for it to finish.",
inst->name, inst->filename_work);
goto delay;
}
/*
* See if there is a "detail.work" file. If not, try to
* rename an existing file to "detail.work".
*/
DEBUG3("Trying to open %s", inst->filename_work);
fd = open(inst->filename_work, inst->mode);
/*
* If the work file didn't exist, try to rename detail* ->
* detail.work, and return the newly opened file.
*/
if (fd < 0) {
if (errno != ENOENT) {
DEBUG("proto_detail (%s): Failed opening %s: %s",
inst->name, inst->filename_work,
fr_syserror(errno));
goto delay;
}
fd = work_rename(inst);
}
/*
* The work file still doesn't exist. Go set up timers,
* or wait for an event which signals us that something
* in the directory changed.
*/
if (fd < 0) {
struct timeval when, now;
#ifdef __linux__
/*
* Wait for the directory to change before
* looking for another "detail" file.
*/
if (!inst->poll_interval) return;
#endif
delay:
/*
* Check every N seconds.
*/
when.tv_sec = inst->poll_interval;
when.tv_usec = 0;
DEBUG3("Waiting %d.%06ds for new files in %s",
(int) when.tv_sec, (int) when.tv_usec, inst->name);
gettimeofday(&now, NULL);
fr_timeval_add(&when, &when, &now);
if (fr_event_timer_insert(inst, inst->el, &inst->ev,
&when, work_retry_timer, inst) < 0) {
ERROR("Failed inserting poll timer for %s", inst->filename_work);
}
return;
}
inst->lock_interval = USEC / 10;
/*
* It exists, go process it!
*
* We will get back to the main loop when the
* "detail.work" file is deleted.
*/
if (work_exists(inst, fd) < 0) goto delay;
}
/** Set the event list for a new IO instance
*
* @param[in] instance of the detail worker
* @param[in] el the event list
* @param[in] nr context from the network side
*/
static void mod_event_list_set(void *instance, fr_event_list_t *el, UNUSED void *nr)
{
proto_detail_file_t *inst = talloc_get_type_abort(instance, proto_detail_file_t);
#ifdef __linux__
struct timeval when;
#endif
inst->el = el;
/*
* Initialize the work state machine.
*/
#ifndef __linux__
work_init(inst);
#else
/*
* We're not changing UID, etc. Start processing the
* detail files now.
*/
if (!main_config.allow_core_dumps) {
work_init(inst);
return;
}
/*
* Delay for a bit, before reading the detail files.
* This gives the server time to call
* rad_suid_down_permanent(), and for /proc/PID to
* therefore change permissions, so that libkqueue can
* read it.
*/
gettimeofday(&when, NULL);
when.tv_sec +=1;
if (fr_event_timer_insert(inst, inst->el, &inst->ev,
&when, work_retry_timer, inst) < 0) {
ERROR("Failed inserting poll timer for %s", inst->filename_work);
}
#endif
}
static int mod_instantiate(UNUSED void *instance, UNUSED CONF_SECTION *cs)
{
// proto_detail_file_t *inst = talloc_get_type_abort(instance, proto_detail_file_t);
return 0;
}
static int mod_bootstrap(void *instance, CONF_SECTION *cs)
{
proto_detail_file_t *inst = talloc_get_type_abort(instance, proto_detail_file_t);
dl_instance_t const *dl_inst;
char *p;
#ifdef __linux__
/*
* The kqueue API takes an FD, but inotify requires a filename.
* libkqueue uses /proc/PID/fd/# to look up the FD -> filename mapping.
*
* However, if you start the server as "root", and then swap to "radiusd",
* /proc/PID will be owned by "root" for security reasons. The only way
* to make /proc/PID owned by "radiusd" is to set the DUMPABLE flag.
*
* Instead of making the poor sysadmin figure this out,
* we check for this situation, and give them a
* descriptive message telling them what to do.
*/
if (!main_config.allow_core_dumps &&
main_config.uid_name && *main_config.uid_name &&
main_config.server_uid != 0) {
cf_log_err(cs, "Cannot start detail file reader due to Linux limitations.");
cf_log_err(cs, "Please set 'allow_core_dumps = true' in the main configuration file.");
return -1;
}
#endif
/*
* Find the dl_instance_t holding our instance data
* so we can find out what the parent of our instance
* was.
*/
dl_inst = dl_instance_find(instance);
rad_assert(dl_inst);
#ifndef __linux__
/*
* Linux inotify works. So we allow poll_interval==0
*/
FR_INTEGER_BOUND_CHECK("poll_interval", inst->poll_interval, >=, 1);
#endif
FR_INTEGER_BOUND_CHECK("poll_interval", inst->poll_interval, <=, 3600);
inst->parent = talloc_get_type_abort(dl_inst->parent->data, proto_detail_t);
inst->cs = cs;
inst->fd = -1;
inst->vnode = true;
inst->directory = p = talloc_strdup(inst, inst->filename);
p = strrchr(p, '/');
if (!p) {
cf_log_err(cs, "Filename must contain '/'");
return -1;
}
*p = '\0';
if (!inst->filename_work) {
inst->filename_work = talloc_typed_asprintf(inst, "%s/detail.work", inst->directory);
}
/*
* We need this for the lock.
*/
inst->mode = O_RDWR;
return 0;
}
static int mod_detach(void *instance)
{
proto_detail_file_t *inst = talloc_get_type_abort(instance, proto_detail_file_t);
/*
* @todo - have our OWN event loop for timers, and a
* "copy timer from -> to, which means we only have to
* delete our child event loop from the parent on close.
*/
close(inst->fd);
if (inst->vnode_fd >= 0) {
if (fr_event_fd_delete(inst->el, inst->vnode_fd, FR_EVENT_FILTER_VNODE) < 0) {
ERROR("Failed removing DELETE callback on detach: %s", fr_strerror());
}
close(inst->vnode_fd);
inst->vnode_fd = -1;
}
return 0;
}
/** Private interface for use by proto_detail_file
*
*/
extern fr_app_io_t proto_detail_file;
fr_app_io_t proto_detail_file = {
.magic = RLM_MODULE_INIT,
.name = "detail_file",
.config = file_listen_config,
.inst_size = sizeof(proto_detail_file_t),
.detach = mod_detach,
.bootstrap = mod_bootstrap,
.instantiate = mod_instantiate,
.default_message_size = 65536,
.default_reply_size = 32,
.open = mod_open,
.vnode = mod_vnode_extend,
.decode = mod_decode,
.write = mod_write,
.fd = mod_fd,
.event_list_set = mod_event_list_set,
};