]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/misc-progs/syslogdctrl.c
Fix vnstat directory creation
[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*/
126
127 safe_system("grep -v '/var/log/messages' < /etc/syslog.conf.new > /etc/syslog.conf.tmp && mv /etc/syslog.conf.tmp /etc/syslog.conf.new");
128 snprintf(command, STRING_SIZE-1, "printf '%s /var/log/messages' >> /etc/syslog.conf.new", varmessages );
129 safe_system(command);
130
a3d6c878 131 if (rename("/etc/syslog.conf.new", "/etc/syslog.conf") == -1)
5b3962de
CS
132 {
133 perror("Unable to replace old config file");
d36e6241 134 unlink("/etc/syslog.conf.new");
5b3962de
CS
135 exit(ERR_CONFIG);
136 }
137
138
139 /* Get syslogd to read the new config file */
140 if ((fd = open("/var/run/syslogd.pid", O_RDONLY)) == -1)
141 {
142 if(errno == ENOENT)
143 {
144 /* pid file doesn't exists.. restart syslog */
145 if((rc = safe_system("/usr/sbin/syslogd u syslogd -m 0")) == 0 )
146 return 0;
147 else
148 {
149 fprintf(stderr,
150 "Unable to restart syslogd - returned exit code %d\n", rc);
151 exit(ERR_SYSLOG);
152 }
153 } else {
154 /* Something odd is going on, failsafe */
155 perror("Unable to open pid file");
156 exit(ERR_SYSLOG);
157 }
158 }
159
160 memset(buffer, 0, STRING_SIZE);
161 if (read(fd, buffer, STRING_SIZE - 1) == -1)
162 {
163 close(fd);
164 perror("Couldn't read from pid file");
165 exit(ERR_SYSLOG);
166 }
167 close(fd);
168 /* strtol does sanity checks that atoi doesn't do */
169 errno = 0;
170 pid = (int)strtol(buffer, (char **)NULL, 10);
171 if (errno || pid <= 1)
172 {
173 fprintf(stderr, "Bad pid value\n");
174 exit(ERR_SYSLOG);
175 }
176 if (kill(pid, SIGHUP) == -1)
177 {
178 fprintf(stderr, "Unable to send SIGHUP\n");
179 exit(ERR_SYSLOG);
180 }
181
182 return 0;
183}