-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sysrqd.c
331 lines (270 loc) · 7.09 KB
/
sysrqd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* sysrqd.c - Daemon to control sysrq over network
*
* © 2005-2009 Julien Danjou <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 dated June, 1991.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/mman.h>
#include <errno.h>
#include <crypt.h>
#define PASS_MAX_LEN 256
#define BIND_MAX_LEN 16
#define PROMPT "sysrq> "
#define SYSRQ_TRIGGER_PATH "/proc/sysrq-trigger"
#define AUTH_FILE "/etc/sysrqd.secret"
#define BINDIP_FILE "/etc/sysrqd.bind"
#define PID_FILE "/var/run/sysrqd.pid"
#define SYSRQD_PRIO -19
#define SYSRQD_LISTEN_PORT 4094
char pwd[PASS_MAX_LEN];
int sock_serv;
/* Macro used to write a string to the client */
#define write_cli(s) \
write(sock_client, s, sizeof(s) - 1);
/* Macro used to write the prompt */
#define write_prompt(s) \
write_cli(PROMPT);
#define errmsg(s) \
fprintf(stderr, "%s (%s:%d)\n", s, __FILE__, __LINE__);
/* Authenticate the connection */
static int
auth (int sock_client)
{
char buf[PASS_MAX_LEN];
size_t len;
char *crypt_result;
write_cli("sysrqd password: ");
memset(buf, 0, sizeof(buf));
/* Read password */
len = read(sock_client, buf, sizeof(buf) - 1);
/* remove \r and \n at end */
while(len > 0
&& (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
len--;
/* If password has been sent */
if(len > 0)
{
buf[len] = '\0';
/* Check if our stored password is crypted, i.e. starts with $[0-9]$ */
if(strlen(pwd) >= 34 && pwd[0] == '$' && pwd[2] == '$' && pwd[1] >= '0' && pwd[1] <= '9')
{
crypt_result = crypt(buf, pwd);
if(!strcmp(crypt_result, pwd))
return 1;
else
write_cli("Go away!\r\n");
} else {
if(!strcmp(buf, pwd))
return 1;
else
write_cli("Go away!\r\n");
}
}
return 0;
}
/* Read a configuration file */
static int
read_conffile (const char *file, char* buf, size_t buflen)
{
int fd;
if((fd = open (file, O_RDONLY)) == -1)
{
syslog(LOG_ERR, "Unable to open file %s: %s",
file,
strerror(errno));
return 1;
}
memset(buf, 0, buflen);
read (fd, buf, buflen - 1);
close (fd);
buf[buflen - 1] = '\0';
/* Strip last \n */
char *tmp;
if((tmp = strchr(buf, '\n')))
*tmp = '\0';
return 0;
}
/* Read commands */
static void
read_cmd (int sock_client, int fd_sysrq)
{
char buf = 0;
write_prompt();
do
{
if(buf == '\n')
write_prompt();
if((buf >= 48 && buf <= 57) ||
(buf >= 97 && buf <= 122 ))
write(fd_sysrq, &buf, 1);
}
while(read (sock_client, &buf, 1) == 1 && buf != 'q');
}
/*
* Listen to a port,
* authenticate connection
* and execute commands
*/
static int
start_listen (int fd_sysrq)
{
int sock_client;
socklen_t size_addr;
struct sockaddr_in addr;
struct sockaddr_in addr_client;
int opt;
char bindip[BIND_MAX_LEN];
addr.sin_family = AF_INET;
addr.sin_port = htons(SYSRQD_LISTEN_PORT);
if(read_conffile(BINDIP_FILE, bindip, sizeof(bindip)))
addr.sin_addr.s_addr = INADDR_ANY;
else if(!inet_aton(bindip, &addr.sin_addr))
{
syslog(LOG_ERR, "Unable to convert IP: %s, using INADDR_ANY",
strerror(errno));
addr.sin_addr.s_addr = INADDR_ANY;
}
if(!(sock_serv = socket (PF_INET, SOCK_STREAM, 0)))
{
syslog(LOG_ERR, "Error while creating server socket: %s",
strerror(errno));
return 1;
}
/* We tries to avoid "socket already in use" errors */
opt = 1;
setsockopt(sock_serv, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
if(bind (sock_serv, (struct sockaddr *) &addr, sizeof (addr)))
{
syslog(LOG_ERR, "Unable to bind(): %s",
strerror(errno));
return 1;
}
if(listen(sock_serv, 2))
{
syslog(LOG_ERR, "Unable to listen(): %s",
strerror(errno));
return 1;
}
size_addr = sizeof (addr_client);
syslog(LOG_INFO, "Listening on port tcp/%d", SYSRQD_LISTEN_PORT);
while((sock_client = accept (sock_serv, (struct sockaddr *) &addr_client, &size_addr)))
{
if(auth (sock_client))
read_cmd (sock_client, fd_sysrq);
close(sock_client);
}
close (sock_serv);
return 0;
}
static void __attribute__ ((noreturn))
signal_handler (void)
{
close (sock_serv);
exit (EXIT_FAILURE);
}
static int
catch_signals (void)
{
struct sigaction sa;
sigset_t mask;
sigfillset (&mask);
sa.sa_handler = (void *) &signal_handler;
sa.sa_mask = mask;
sigaction (SIGINT, &sa, NULL);
sigaction (SIGTERM, &sa, NULL);
/* ignore sigpipe */
sigemptyset(&mask);
sigaddset(&mask, SIGPIPE);
sigprocmask(SIG_BLOCK, &mask, NULL);
return 0;
}
static int
write_pidfile(pid_t pid)
{
FILE *pidf = fopen(PID_FILE, "w");
if (pidf == NULL)
return 1;
fprintf(pidf, "%d\n", pid);
fclose(pidf);
return 0;
}
static void
do_on_exit(void)
{
syslog(LOG_NOTICE, "Exiting");
}
int
main (void)
{
int fd_sysrq;
atexit(do_on_exit);
/* We test if it is worth the pain to fork if setpriority would fail */
if(getuid() != 0)
{
errmsg ("Only root can run this program.");
return 1;
}
/* We read our password */
if(read_conffile (AUTH_FILE, pwd, sizeof(pwd)))
return EXIT_FAILURE;
/* mlock, we want this to always run */
mlockall(MCL_CURRENT | MCL_FUTURE);
/* We daemonize */
daemon(0, 0);
openlog ("sysrqd", LOG_PID, LOG_DAEMON);
syslog(LOG_PID | LOG_DAEMON, "Starting");
if(write_pidfile(getpid()))
syslog (LOG_PID | LOG_DAEMON, "Unable to write pidfile");
/* We set our priority */
if(setpriority (PRIO_PROCESS, 0, SYSRQD_PRIO))
{
syslog (LOG_PID | LOG_DAEMON, "Unable to set priority: %s",
strerror(errno));
return EXIT_FAILURE;
}
/* Catch some signals */
catch_signals ();
/* We keep the sysrq-trigger file opened */
fd_sysrq = open(SYSRQ_TRIGGER_PATH, O_SYNC|O_WRONLY);
if(fd_sysrq == -1)
{
syslog(LOG_ERR, "Error while opening sysrq trigger: %s",
strerror(errno));
return EXIT_FAILURE;
}
/* Now listen */
if(!start_listen (fd_sysrq))
return EXIT_FAILURE;
/* If we quit, close the trigger */
close (fd_sysrq);
closelog();
return EXIT_SUCCESS;
}