]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/syslogdctrl.c
db2659ecb42ddee0d1f88bb5d75070bf756b98f2
1 /* This file is part of the IPCop Firewall.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * Copyright (C) 2003-07-12 Robert Kerr <rkerr@go.to>
10 * Edited by the IPFire Team to change var log messages
18 #include <sys/types.h>
22 #include "libsmooth.h"
26 #define ERR_SETTINGS 2 /* error in settings file */
27 #define ERR_ETC 3 /* error with /etc permissions */
28 #define ERR_CONFIG 4 /* error updated sshd_config */
29 #define ERR_SYSLOG 5 /* error restarting syslogd */
33 char buffer
[STRING_SIZE
], hostname
[STRING_SIZE
], varmessages
[STRING_SIZE
];
34 int config_fd
,rc
,fd
,pid
;
36 struct keyvalue
*kv
= NULL
;
37 memset(buffer
, 0, STRING_SIZE
);
38 memset(hostname
, 0, STRING_SIZE
);
39 memset(varmessages
, 0, STRING_SIZE
);
45 /* Read in and verify config */
48 if (!readkeyvalues(kv
, "/var/ipfire/logging/settings"))
50 fprintf(stderr
, "Cannot read syslog settings\n");
54 if (!findkey(kv
, "ENABLE_REMOTELOG", buffer
))
56 fprintf(stderr
, "Cannot read ENABLE_REMOTELOG\n");
60 if (!findkey(kv
, "REMOTELOG_ADDR", hostname
))
62 fprintf(stderr
, "Cannot read REMOTELOG_ADDR\n");
66 if (!findkey(kv
, "VARMESSAGES", varmessages
))
68 fprintf(stderr
, "Cannot read VARMESSAGES\n");
72 if (strspn(hostname
, VALID_FQDN
) != strlen(hostname
))
74 fprintf(stderr
, "Bad REMOTELOG_ADDR: %s\n", hostname
);
81 /* If anyone other than root can write to /etc this would be totally
82 * insecure - same if anyone other than root owns /etc, as they could
83 * change the file mode to give themselves or anyone else write access. */
87 perror("Unable to stat /etc");
90 if(!S_ISDIR(st
.st_mode
))
92 fprintf(stderr
, "/etc is not a directory?!\n");
95 if ( st
.st_uid
!= 0 || st
.st_mode
& S_IWOTH
||
96 ((st
.st_gid
!= 0) && (st
.st_mode
& S_IWGRP
)) )
98 fprintf(stderr
, "/etc is owned/writable by non-root users\n");
102 /* O_CREAT with O_EXCL will make open() fail if the file already exists -
103 * mostly to prevent 2 copies running at once */
104 if ((config_fd
= open( "/etc/syslog.conf.new", O_WRONLY
|O_CREAT
|O_EXCL
, 0644 )) == -1 )
106 perror("Unable to open new config file");
110 if (!strcmp(buffer
,"on"))
111 snprintf(buffer
, STRING_SIZE
- 1, "/bin/sed -e 's/^#\\?\\(\\*\\.\\*[[:blank:]]\\+@\\).\\+$/\\1%s/' /etc/syslog.conf >&%d", hostname
, config_fd
);
113 snprintf(buffer
, STRING_SIZE
- 1, "/bin/sed -e 's/^#\\?\\(\\*\\.\\*[[:blank:]]\\+@.\\+\\)$/#\\1/' /etc/syslog.conf >&%d", config_fd
);
115 snprintf(buffer
, STRING_SIZE
- 1, "/bin/sed 's/.*\\/var\\/log\\/messages.*/%s \\/var\\/log\\/messages/' /etc/syslog.conf >&%d", varmessages
, config_fd
);
117 /* if the return code isn't 0 failsafe */
118 if ((rc
= unpriv_system(buffer
,99,99)) != 0)
120 fprintf(stderr
, "sed returned bad exit code: %d\n", rc
);
122 unlink("/etc/syslog.conf.new");
126 if (rename("/etc/syslog.conf.new", "/etc/syslog.conf") == -1)
128 perror("Unable to replace old config file");
129 unlink("/etc/syslog.conf.new");
134 /* Get syslogd to read the new config file */
135 if ((fd
= open("/var/run/syslogd.pid", O_RDONLY
)) == -1)
139 /* pid file doesn't exists.. restart syslog */
140 if((rc
= safe_system("/usr/sbin/syslogd u syslogd -m 0")) == 0 )
145 "Unable to restart syslogd - returned exit code %d\n", rc
);
149 /* Something odd is going on, failsafe */
150 perror("Unable to open pid file");
155 memset(buffer
, 0, STRING_SIZE
);
156 if (read(fd
, buffer
, STRING_SIZE
- 1) == -1)
159 perror("Couldn't read from pid file");
163 /* strtol does sanity checks that atoi doesn't do */
165 pid
= (int)strtol(buffer
, (char **)NULL
, 10);
166 if (errno
|| pid
<= 1)
168 fprintf(stderr
, "Bad pid value\n");
171 if (kill(pid
, SIGHUP
) == -1)
173 fprintf(stderr
, "Unable to send SIGHUP\n");