]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/misc-progs/syslogdctrl.c
core76: Sort exclude file alphabetically.
[people/teissler/ipfire-2.x.git] / src / misc-progs / syslogdctrl.c
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
23 #include "libsmooth.h"
24 #include "setuid.h"
25 #include "netutil.h"
26
27 #define ERR_ANY 1
28 #define ERR_SETTINGS 2 /* error in settings file */
29 #define ERR_ETC 3 /* error with /etc permissions */
30 #define ERR_CONFIG 4 /* error updated sshd_config */
31 #define ERR_SYSLOG 5 /* error restarting syslogd */
32
33 int main(void)
34 {
35 char buffer[STRING_SIZE], command[STRING_SIZE], hostname[STRING_SIZE];
36 char varmessages[STRING_SIZE], asynclog[STRING_SIZE];
37 int config_fd,rc,fd,pid;
38 struct stat st;
39 struct keyvalue *kv = NULL;
40 memset(buffer, 0, STRING_SIZE);
41 memset(hostname, 0, STRING_SIZE);
42 memset(varmessages, 0, STRING_SIZE);
43 memset(asynclog, 0, STRING_SIZE);
44
45 if (!(initsetuid()))
46 exit(1);
47
48
49 /* Read in and verify config */
50 kv=initkeyvalues();
51
52 if (!readkeyvalues(kv, "/var/ipfire/logging/settings"))
53 {
54 fprintf(stderr, "Cannot read syslog settings\n");
55 exit(ERR_SETTINGS);
56 }
57
58 if (!findkey(kv, "ENABLE_REMOTELOG", buffer))
59 {
60 fprintf(stderr, "Cannot read ENABLE_REMOTELOG\n");
61 exit(ERR_SETTINGS);
62 }
63
64 if (!findkey(kv, "REMOTELOG_ADDR", hostname))
65 {
66 fprintf(stderr, "Cannot read REMOTELOG_ADDR\n");
67 exit(ERR_SETTINGS);
68 }
69
70 if (!findkey(kv, "ENABLE_ASYNCLOG", asynclog))
71 {
72 fprintf(stderr, "Cannot read ENABLE_ASYNCLOG\n");
73 exit(ERR_SETTINGS);
74 }
75
76
77 if (!findkey(kv, "VARMESSAGES", varmessages))
78 {
79 fprintf(stderr, "Cannot read VARMESSAGES\n");
80 exit(ERR_SETTINGS);
81 }
82
83 if (strspn(hostname, VALID_FQDN) != strlen(hostname))
84 {
85 fprintf(stderr, "Bad REMOTELOG_ADDR: %s\n", hostname);
86 exit(ERR_SETTINGS);
87 }
88
89 freekeyvalues(kv);
90
91
92 /* If anyone other than root can write to /etc this would be totally
93 * insecure - same if anyone other than root owns /etc, as they could
94 * change the file mode to give themselves or anyone else write access. */
95
96 if(lstat("/etc",&st))
97 {
98 perror("Unable to stat /etc");
99 exit(ERR_ETC);
100 }
101 if(!S_ISDIR(st.st_mode))
102 {
103 fprintf(stderr, "/etc is not a directory?!\n");
104 exit(ERR_ETC);
105 }
106 if ( st.st_uid != 0 || st.st_mode & S_IWOTH ||
107 ((st.st_gid != 0) && (st.st_mode & S_IWGRP)) )
108 {
109 fprintf(stderr, "/etc is owned/writable by non-root users\n");
110 exit(ERR_ETC);
111 }
112
113 /* O_CREAT with O_EXCL will make open() fail if the file already exists -
114 * mostly to prevent 2 copies running at once */
115 if ((config_fd = open( "/etc/syslog.conf.new", O_WRONLY|O_CREAT|O_EXCL, 0644 )) == -1 )
116 {
117 perror("Unable to open new config file");
118 exit(ERR_CONFIG);
119 }
120
121 if (!strcmp(buffer,"on"))
122 snprintf(buffer, STRING_SIZE - 1, "/bin/sed -e 's/^#\\?\\(\\*\\.\\*[[:blank:]]\\+@\\).\\+$/\\1%s/' /etc/syslog.conf >&%d", hostname, config_fd );
123 else
124 snprintf(buffer, STRING_SIZE - 1, "/bin/sed -e 's/^#\\?\\(\\*\\.\\*[[:blank:]]\\+@.\\+\\)$/#\\1/' /etc/syslog.conf >&%d", config_fd );
125
126 /* if the return code isn't 0 failsafe */
127 if ((rc = unpriv_system(buffer,99,99)) != 0)
128 {
129 fprintf(stderr, "sed returned bad exit code: %d\n", rc);
130 close(config_fd);
131 unlink("/etc/syslog.conf.new");
132 exit(ERR_CONFIG);
133 }
134 close(config_fd);
135
136 /* Replace the logging option*/
137 safe_system("grep -v '/var/log/messages' < /etc/syslog.conf.new > /etc/syslog.conf.tmp && mv /etc/syslog.conf.tmp /etc/syslog.conf.new");
138
139 if (!strcmp(asynclog,"on"))
140 snprintf(command, STRING_SIZE - 1, "printf '%s -/var/log/messages' >> /etc/syslog.conf.new", varmessages );
141 else
142 snprintf(command, STRING_SIZE - 1, "printf '%s /var/log/messages' >> /etc/syslog.conf.new", varmessages );
143
144 safe_system(command);
145
146 if (rename("/etc/syslog.conf.new", "/etc/syslog.conf") == -1)
147 {
148 perror("Unable to replace old config file");
149 unlink("/etc/syslog.conf.new");
150 exit(ERR_CONFIG);
151 }
152
153
154 /* Get syslogd to read the new config file */
155 if ((fd = open("/var/run/syslogd.pid", O_RDONLY)) == -1)
156 {
157 if(errno == ENOENT)
158 {
159 /* pid file doesn't exists.. restart syslog */
160 if((rc = safe_system("/usr/sbin/syslogd u syslogd -m 0")) == 0 )
161 return 0;
162 else
163 {
164 fprintf(stderr,
165 "Unable to restart syslogd - returned exit code %d\n", rc);
166 exit(ERR_SYSLOG);
167 }
168 } else {
169 /* Something odd is going on, failsafe */
170 perror("Unable to open pid file");
171 exit(ERR_SYSLOG);
172 }
173 }
174
175 memset(buffer, 0, STRING_SIZE);
176 if (read(fd, buffer, STRING_SIZE - 1) == -1)
177 {
178 close(fd);
179 perror("Couldn't read from pid file");
180 exit(ERR_SYSLOG);
181 }
182 close(fd);
183 /* strtol does sanity checks that atoi doesn't do */
184 errno = 0;
185 pid = (int)strtol(buffer, (char **)NULL, 10);
186 if (errno || pid <= 1)
187 {
188 fprintf(stderr, "Bad pid value\n");
189 exit(ERR_SYSLOG);
190 }
191 if (kill(pid, SIGHUP) == -1)
192 {
193 fprintf(stderr, "Unable to send SIGHUP\n");
194 exit(ERR_SYSLOG);
195 }
196
197 return 0;
198 }