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
|
/*
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "gmid.h"
#include <sys/types.h>
#include <sys/uio.h>
#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <netdb.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include "log.h"
#include "proc.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
static FILE *log;
static void logger_init(struct privsep *, struct privsep_proc *, void *);
static void logger_shutdown(void);
static int logger_dispatch_parent(int, struct privsep_proc *, struct imsg *);
static int logger_dispatch_server(int, struct privsep_proc *, struct imsg *);
static struct privsep_proc procs[] = {
{ "parent", PROC_PARENT, logger_dispatch_parent },
{ "server", PROC_SERVER, logger_dispatch_server },
};
void
logger(struct privsep *ps, struct privsep_proc *p)
{
proc_run(ps, p, procs, nitems(procs), logger_init, NULL);
}
static void
logger_init(struct privsep *ps, struct privsep_proc *p, void *arg)
{
p->p_shutdown = logger_shutdown;
log = stderr;
sandbox_logger_process();
}
static void
logger_shutdown(void)
{
closelog();
if (log && log != stderr) {
fflush(log);
fclose(log);
}
}
static int
logger_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
{
switch (imsg->hdr.type) {
case IMSG_LOG_TYPE:
if (log != NULL && log != stderr) {
fflush(log);
fclose(log);
}
log = NULL;
if (imsg->fd != -1) {
if ((log = fdopen(imsg->fd, "a")) == NULL)
fatal("fdopen");
}
break;
default:
return -1;
}
return 0;
}
static int
logger_dispatch_server(int fd, struct privsep_proc *p, struct imsg *imsg)
{
char *msg;
size_t datalen;
switch (imsg->hdr.type) {
case IMSG_LOG_REQUEST:
msg = imsg->data;
datalen = IMSG_DATA_SIZE(imsg);
if (datalen == 0)
fatal("got invalid IMSG_LOG_REQUEST");
msg[datalen - 1] = '\0';
if (log != NULL)
fprintf(log, "%s\n", msg);
else
syslog(LOG_DAEMON | LOG_NOTICE, "%s", msg);
break;
default:
return -1;
}
return 0;
}
|