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
|
/*
* sysrqd.c - Daemon to control sysrq over network
*
* (c) 2005-2006 Julien Danjou <julien@danjou.info>
*
* 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
*
* $Id: sysrqd.c 1702 2007-01-19 14:33:11Z jd $
*/
#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 "sysrqd.h"
char pwd[PASS_MAX_LEN];
char bindip[BIND_MAX_LEN];
int sock_serv;
/* Macro used to write the prompt */
#define write_prompt(s) \
write (s, PROMPT, sizeof(char) * strlen(PROMPT));
/* Macro used to write a string to the client */
#define write_cli(sock, s) \
write (sock, s, sizeof(char) * strlen(s));
#define errmsg(s) \
fprintf(stderr, "%s (%s:%d)\n", s, __FILE__, __LINE__);
/* Authenticate the connection */
int
auth (int sock_client)
{
char buf[PASS_MAX_LEN];
write_cli (sock_client, "sysrqd password: ");
read (sock_client, buf, PASS_MAX_LEN);
if(!strncmp(buf, pwd, strlen(pwd)))
return 1;
write_cli (sock_client, "Go away!\r\n");
return 0;
}
/* Read the ip address */
int
read_bindip (void)
{
int fd_bindip;
char * tmp;
if((fd_bindip = open (BINDIP_FILE, O_RDONLY)) == -1)
return 1;
read (fd_bindip, bindip, BIND_MAX_LEN);
close (fd_bindip);
/* Strip last \n */
if((tmp = strchr(bindip, '\n')))
*tmp = '\0';
return 0;
}
/* Read commands */
void
read_cmd (int sock_client, int fd_sysrq)
{
char buf;
write_prompt (sock_client);
do
{
if(buf == '\n')
write_prompt (sock_client);
if((buf >= 48 && buf <= 57) ||
(buf >= 97 && buf <= 122 ))
write(fd_sysrq, &buf, 1);
}
while(read (sock_client, &buf, 1) && buf != 'q');
}
/*
* Listen to a port,
* authenticate connection
* and execute commands
*/
int
start_listen (int fd_sysrq)
{
int sock_client;
socklen_t size_addr;
struct sockaddr_in addr;
struct sockaddr_in addr_client;
int opt;
addr.sin_family = AF_INET;
addr.sin_port = htons(SYSRQD_LISTEN_PORT);
if (read_bindip())
addr.sin_addr.s_addr = INADDR_ANY;
else
inet_aton(bindip, &addr.sin_addr);
if(!(sock_serv = socket (PF_INET, SOCK_STREAM, 0)))
{
perror("Error while creating server socket");
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)))
{
perror("Unable to bind()");
return 1;
}
if(listen(sock_serv, 2))
{
perror("Unable to listen()");
return 1;
}
size_addr = sizeof (addr_client);
syslog(LOG_PID||LOG_DAEMON, "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;
}
int
open_sysrq_trigger (void)
{
return open (SYSRQ_TRIGGER_PATH, O_SYNC|O_WRONLY);
}
/* Read the sysrqd password */
int
read_pwd (void)
{
int fd_pwd;
char * tmp;
if((fd_pwd = open (AUTH_FILE, O_RDONLY)) == -1)
return 1;
read (fd_pwd, pwd, PASS_MAX_LEN);
close (fd_pwd);
/* Strip last \n */
if((tmp = strchr(pwd, '\n')))
*tmp = '\0';
return 0;
}
void
signal_handler (void)
{
close (sock_serv);
exit (EXIT_FAILURE);
}
int
catch_signals ()
{
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);
return 0;
}
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;
}
int
main (void)
{
int fd_sysrq;
/* 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_pwd ())
{
errmsg ("Error while reading password file ("AUTH_FILE").");
return 1;
}
/* mlock, we want this to always run */
mlockall(MCL_FUTURE);
/* We daemonize */
daemon(0, 0);
openlog ("sysrqd", LOG_PID, LOG_DAEMON);
syslog (LOG_PID||LOG_DAEMON, "sysrqd started");
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.");
return 1;
}
/* Catch some signals */
catch_signals ();
/* We keep the sysrq-trigger file opened */
if(!(fd_sysrq = open_sysrq_trigger ()))
{
errmsg ("Error while opening sysrq trigger.");
return 1;
}
/* Now listen */
if(!start_listen (fd_sysrq))
return 1;
/* If we quit, close the trigger */
close (fd_sysrq);
closelog();
return (EXIT_SUCCESS);
}
|