Skip to content

Commit 4f4ad32

Browse files
authored
Merge pull request #15 from jovanlanik/master
Add setting log permissions to configuration
2 parents 2de5a3b + 211c137 commit 4f4ad32

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed

README

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ Defaults to 0 (i.e. repeated messages will be summarized)
225225
directory doesn't exist, it will be automatically created when the first
226226
matching message will be logged (the parent directory has to exist, though) .
227227

228+
* perms = <mode> : permissions for the log directory. Defaults to 0700
229+
230+
Example : Let those in the group with the GID of the process read the log.
231+
Don't forget to run metalog as the group.
232+
233+
perms = 0770
234+
228235
* command = <path/to/command> : run a program or a shell-script when all
229236
conditions are met. This directive is not incompatible with logdir : a
230237
message can be both logged and passed to an external command. When the
@@ -354,6 +361,9 @@ Linux. Valid values are from 0 to 7. The default is 7.
354361
- '-C <configuration file>' or '--configfile=<configuration file>' : use an
355362
alternative configuration file.
356363

364+
- '-g <group>' or '--group=<group>' : change the GID of the metalog process.
365+
Created files will be owned by this group.
366+
357367
- '-h' or '--help' : show help and version number.
358368

359369
- '-p <filename>' or '--pidfile=<filename>' : set the name of the file

man/metalog.8.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Set the console log level (Linux only).
3232
.BR \-C ", " \-\-configfile = \fR\fIfile\fR
3333
Use an alternate configuration file.
3434
.TP
35+
.BR \-g ", " \-\-group = \fR\fIgroup\fR
36+
Change the GID of the metalog process.
37+
Created files will be owned by this group.
38+
.TP
3539
.BR \-h ", " \-\-help
3640
Output help information and exit.
3741
.TP

man/metalog.conf.5.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ By default, \fBmaximum\fR is the largest possible level.
9191
Files will be written under the specified directory. The special value \fI"NONE"\fR will
9292
skip the log message.
9393
.TP
94+
\fBperms\fR = \fI<mode>\fR
95+
Permissions for the log directory. Defaults to \fI0700\fR
96+
.TP
9497
\fBprogram\fR = \fI"name"\fR
9598
Can be used to do filtering instead of \fBfacility\fR.
9699
Remember to use the executable name.

metalog.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ maxsize = 1048576 # size in bytes (1048576 = 1 megabyte)
44
maxtime = 86400 # time in seconds (86400 = 1 day)
55
maxfiles = 5 # num files per directory
66

7+
# Permissions for log directories. 0750 allows group to read logs. 0700 is default.
8+
#perms = 0750
9+
710
# Format of the timestamp: YYYY-MM-DD HH:MM:SS.NNN
811
#stamp_fmt = "%F %T.%3N"
912

@@ -57,6 +60,10 @@ Password failures :
5760
logdir = "/var/log/pwdfail"
5861
# command = "/usr/local/sbin/mail_pwd_failures.sh"
5962

63+
# If you changed default permissions it may be a good idea to set more
64+
# restrictive permissions on sensitive logs.
65+
# perms = 0700
66+
6067
Kernel messages :
6168

6269
facility = "kern"

src/metalog.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
189189
if ((*cur_block)->output != NULL) {
190190
(*cur_block)->output->showrepeats = (*cur_block)->showrepeats;
191191
}
192+
} else if (strcasecmp(keyword, "perms") == 0) {
193+
(*cur_block)->perms = (mode_t) strtoul(value, NULL, 8);
194+
if ((*cur_block)->output != NULL) {
195+
(*cur_block)->output->perms = (*cur_block)->perms;
196+
}
192197
} else if (strcasecmp(keyword, "logdir") == 0) {
193198
char *logdir = NULL;
194199
Output *outputs_scan = outputs;
@@ -215,6 +220,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
215220
else
216221
free(logdir);
217222
new_output->fp = NULL;
223+
new_output->perms = (*cur_block)->perms;
218224
new_output->size = (off_t) 0;
219225
new_output->maxsize = (*cur_block)->maxsize;
220226
new_output->maxfiles = (*cur_block)->maxfiles;
@@ -353,6 +359,7 @@ static int configParser(const char * const file)
353359
(off_t) DEFAULT_MAXSIZE, /* maxsize */
354360
DEFAULT_MAXFILES, /* maxfiles */
355361
(time_t) DEFAULT_MAXTIME, /* maxtime */
362+
(mode_t) DEFAULT_PERMS, /* perms */
356363
NULL, /* output */
357364
NULL, /* command */
358365
NULL, /* program */
@@ -849,7 +856,7 @@ static int writeLogLine(Output * const output, const char * const date,
849856

850857
testdir:
851858
if (stat(output->directory, &st) < 0) {
852-
if (mkdir(output->directory, OUTPUT_DIR_PERMS) < 0) {
859+
if (mkdir(output->directory, output->perms) < 0) {
853860
warnp("Can't create [%s]", output->directory);
854861
return -1;
855862
}
@@ -1638,6 +1645,21 @@ static void dodaemonize(void)
16381645
}
16391646
}
16401647

1648+
static void setgroup(void)
1649+
{
1650+
if (group_name == NULL) return;
1651+
struct group *g;
1652+
errno = 0;
1653+
if ((g = getgrnam(group_name)) == NULL) {
1654+
if(errno == 0)
1655+
err("Failed to set group: group '%s' not found", group_name);
1656+
else
1657+
errp("Failed to set group");
1658+
}
1659+
if (setgid(g->gr_gid) == -1)
1660+
errp("Failed to set group");
1661+
}
1662+
16411663
__attribute__ ((noreturn))
16421664
static void help(void)
16431665
{
@@ -1678,6 +1700,9 @@ static void parseOptions(int argc, char *argv[])
16781700
case 'C' :
16791701
config_file = xstrdup(optarg);
16801702
break;
1703+
case 'g' :
1704+
group_name = xstrdup(optarg);
1705+
break;
16811706
case 'v' :
16821707
++verbose;
16831708
break;
@@ -1722,6 +1747,7 @@ int main(int argc, char *argv[])
17221747
if (configParser(config_file) < 0)
17231748
err("Bad configuration file");
17241749
checkRoot();
1750+
setgroup();
17251751
dodaemonize();
17261752
setsignals();
17271753
if (update_pid_file(pid_file))

src/metalog.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <sys/wait.h>
3838
#include <netinet/in.h>
3939
#include <netdb.h>
40+
#include <grp.h>
4041

4142
#ifndef HAVE_SYSLOG_NAMES
4243
# include "syslognames.h"
@@ -96,6 +97,7 @@ typedef struct RemoteHost_ {
9697

9798
typedef struct Output_ {
9899
char *directory;
100+
mode_t perms;
99101
FILE *fp;
100102
off_t size;
101103
off_t maxsize;
@@ -130,6 +132,7 @@ typedef struct ConfigBlock_ {
130132
off_t maxsize;
131133
int maxfiles;
132134
time_t maxtime;
135+
mode_t perms;
133136
Output *output;
134137
const char *command;
135138
const char *program;
@@ -168,7 +171,7 @@ typedef enum LogLineType_ {
168171
#define CF_PROGNAME_KERNEL "kernel"
169172
#define OUTPUT_DIR_TIMESTAMP ".timestamp"
170173
#define OUTPUT_DIR_CURRENT "current"
171-
#define OUTPUT_DIR_PERMS 0700
174+
#define DEFAULT_PERMS 0700
172175
#define OUTPUT_DIR_LOGFILES_PREFIX "log-"
173176
#define OUTPUT_DIR_LOGFILES_SUFFIX "%Y-%m-%d-%H:%M:%S"
174177
#define DEFAULT_CONFIG_FILE CONFDIR "/metalog.conf"

src/metalog_p.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#else
77
# define KLOGCTL_OPTIONS ""
88
#endif
9-
#define GETOPT_OPTIONS KLOGCTL_OPTIONS "aBC:hp:sVvN"
9+
#define GETOPT_OPTIONS KLOGCTL_OPTIONS "aBC:g:hp:sVvN"
1010

1111
static struct option long_options[] = {
1212
{ "async", 0, NULL, 'a' },
@@ -15,6 +15,7 @@ static struct option long_options[] = {
1515
{ "consolelevel", 1, NULL, 'c' },
1616
#endif
1717
{ "configfile", 1, NULL, 'C' },
18+
{ "group", 1, NULL, 'g' },
1819
{ "help", 0, NULL, 'h' },
1920
{ "no-kernel", 0, NULL, 'N' },
2021
{ "pidfile", 1, NULL, 'p' },
@@ -42,5 +43,6 @@ static bool do_kernel_log = true;
4243
static signed char daemonize;
4344
static const char *pid_file = DEFAULT_PID_FILE;
4445
static const char *config_file = DEFAULT_CONFIG_FILE;
46+
static const char *group_name = NULL;
4547

4648
#endif

0 commit comments

Comments
 (0)