]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/misc-progs/syslogdctrl.c
Added ability to change syslog to async logging.
[people/pmueller/ipfire-2.x.git] / src / misc-progs / syslogdctrl.c
CommitLineData
5b3962de
CS
1/* This file is part of the IPCop Firewall.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * Copyright (C) 2003-07-12 Robert Kerr <rkerr@go.to>
7 *
8 * $Id$
9 *
10 * Edited by the IPFire Team to change var log messages
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <string.h>
17#include <sys/stat.h>
18#include <sys/types.h>
19#include <fcntl.h>
20#include <signal.h>
21#include <errno.h>
22#include "libsmooth.h"
23#include "setuid.h"
24
25#define ERR_ANY 1
26#define ERR_SETTINGS 2 /* error in settings file */
d36e6241 27#define ERR_ETC 3 /* error with /etc permissions */
5b3962de
CS
28#define ERR_CONFIG 4 /* error updated sshd_config */
29#define ERR_SYSLOG 5 /* error restarting syslogd */
30
31int main(void)
32{
4c7fa778 33 char buffer[STRING_SIZE], command[STRING_SIZE], hostname[STRING_SIZE], varmessages[STRING_SIZE];
5b3962de
CS
34 int config_fd,rc,fd,pid;
35 struct stat st;
36 struct keyvalue *kv = NULL;
37 memset(buffer, 0, STRING_SIZE);
38 memset(hostname, 0, STRING_SIZE);
39 memset(varmessages, 0, STRING_SIZE);
40
41 if (!(initsetuid()))
42 exit(1);
43
44
45 /* Read in and verify config */
46 kv=initkeyvalues();
47
ca4c317c 48 if (!readkeyvalues(kv, "/var/ipfire/logging/settings"))
5b3962de
CS
49 {
50 fprintf(stderr, "Cannot read syslog settings\n");
51 exit(ERR_SETTINGS);
52 }
53
54 if (!findkey(kv, "ENABLE_REMOTELOG", buffer))
55 {
56 fprintf(stderr, "Cannot read ENABLE_REMOTELOG\n");
57 exit(ERR_SETTINGS);
58 }
59
60 if (!findkey(kv, "REMOTELOG_ADDR", hostname))
61 {
62 fprintf(stderr, "Cannot read REMOTELOG_ADDR\n");
63 exit(ERR_SETTINGS);
64 }
65
66 if (!findkey(kv, "VARMESSAGES", varmessages))
67 {
68 fprintf(stderr, "Cannot read VARMESSAGES\n");
69 exit(ERR_SETTINGS);
70 }
71
72 if (strspn(hostname, VALID_FQDN) != strlen(hostname))
73 {
74 fprintf(stderr, "Bad REMOTELOG_ADDR: %s\n", hostname);
75 exit(ERR_SETTINGS);
76 }
77
78 freekeyvalues(kv);
79
80
d36e6241
CS
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
5b3962de
CS
83 * change the file mode to give themselves or anyone else write access. */
84
d36e6241 85 if(lstat("/etc",&st))
5b3962de 86 {
d36e6241 87 perror("Unable to stat /etc");
5b3962de
CS
88 exit(ERR_ETC);
89 }
90 if(!S_ISDIR(st.st_mode))
91 {
d36e6241 92 fprintf(stderr, "/etc is not a directory?!\n");
5b3962de
CS
93 exit(ERR_ETC);
94 }
95 if ( st.st_uid != 0 || st.st_mode & S_IWOTH ||
96 ((st.st_gid != 0) && (st.st_mode & S_IWGRP)) )
97 {
d36e6241 98 fprintf(stderr, "/etc is owned/writable by non-root users\n");
5b3962de
CS
99 exit(ERR_ETC);
100 }
101
102 /* O_CREAT with O_EXCL will make open() fail if the file already exists -
103 * mostly to prevent 2 copies running at once */
d36e6241 104 if ((config_fd = open( "/etc/syslog.conf.new", O_WRONLY|O_CREAT|O_EXCL, 0644 )) == -1 )
5b3962de
CS
105 {
106 perror("Unable to open new config file");
107 exit(ERR_CONFIG);
108 }
109
110 if (!strcmp(buffer,"on"))
d36e6241 111 snprintf(buffer, STRING_SIZE - 1, "/bin/sed -e 's/^#\\?\\(\\*\\.\\*[[:blank:]]\\+@\\).\\+$/\\1%s/' /etc/syslog.conf >&%d", hostname, config_fd );
5b3962de 112 else
d36e6241 113 snprintf(buffer, STRING_SIZE - 1, "/bin/sed -e 's/^#\\?\\(\\*\\.\\*[[:blank:]]\\+@.\\+\\)$/#\\1/' /etc/syslog.conf >&%d", config_fd );
5b3962de 114
4c7fa778 115 /* if the return code isn't 0 failsafe */
5b3962de
CS
116 if ((rc = unpriv_system(buffer,99,99)) != 0)
117 {
118 fprintf(stderr, "sed returned bad exit code: %d\n", rc);
119 close(config_fd);
d36e6241 120 unlink("/etc/syslog.conf.new");
5b3962de
CS
121 exit(ERR_CONFIG);
122 }
123 close(config_fd);
4c7fa778
MT
124
125 /* Replace the logging option*/
4c7fa778 126 safe_system("grep -v '/var/log/messages' < /etc/syslog.conf.new > /etc/syslog.conf.tmp && mv /etc/syslog.conf.tmp /etc/syslog.conf.new");
6945083e
CS
127
128 if (strcmp(ENABLE_ASYNCLOG,"on"))
129 snprintf(command, STRING_SIZE-1, "printf '%s -/var/log/messages' >> /etc/syslog.conf.new", varmessages );
130 else
4c7fa778 131 snprintf(command, STRING_SIZE-1, "printf '%s /var/log/messages' >> /etc/syslog.conf.new", varmessages );
6945083e 132
4c7fa778
MT
133 safe_system(command);
134
a3d6c878 135 if (rename("/etc/syslog.conf.new", "/etc/syslog.conf") == -1)
5b3962de
CS
136 {
137 perror("Unable to replace old config file");
d36e6241 138 unlink("/etc/syslog.conf.new");
5b3962de
CS
139 exit(ERR_CONFIG);
140 }
141
142
143 /* Get syslogd to read the new config file */
144 if ((fd = open("/var/run/syslogd.pid", O_RDONLY)) == -1)
145 {
146 if(errno == ENOENT)
147 {
148 /* pid file doesn't exists.. restart syslog */
149 if((rc = safe_system("/usr/sbin/syslogd u syslogd -m 0")) == 0 )
150 return 0;
151 else
152 {
153 fprintf(stderr,
154 "Unable to restart syslogd - returned exit code %d\n", rc);
155 exit(ERR_SYSLOG);
156 }
157 } else {
158 /* Something odd is going on, failsafe */
159 perror("Unable to open pid file");
160 exit(ERR_SYSLOG);
161 }
162 }
163
164 memset(buffer, 0, STRING_SIZE);
165 if (read(fd, buffer, STRING_SIZE - 1) == -1)
166 {
167 close(fd);
168 perror("Couldn't read from pid file");
169 exit(ERR_SYSLOG);
170 }
171 close(fd);
172 /* strtol does sanity checks that atoi doesn't do */
173 errno = 0;
174 pid = (int)strtol(buffer, (char **)NULL, 10);
175 if (errno || pid <= 1)
176 {
177 fprintf(stderr, "Bad pid value\n");
178 exit(ERR_SYSLOG);
179 }
180 if (kill(pid, SIGHUP) == -1)
181 {
182 fprintf(stderr, "Unable to send SIGHUP\n");
183 exit(ERR_SYSLOG);
184 }
185
186 return 0;
187}