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