Rev 1144 | Rev 1146 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1140 | dev | 1 | /****************************************************************** |
1145 | dev | 2 | * RebootDaemon. |
1140 | dev | 3 | * |
1145 | dev | 4 | * A daemon which waits on specified TCP port for special string |
1143 | dev | 5 | * and reboots the computer. |
1140 | dev | 6 | * |
7 | * Command line: rebootdaemon [--port PORT] [--config CONFIG_FILE] |
||
8 | * Defaults are port 19 and config /etc/rebootdaemon.conf. |
||
9 | * |
||
10 | * Config file looks like: |
||
11 | * -------------------------- |
||
12 | * port=19 |
||
13 | * password="some password" |
||
14 | * -------------------------- |
||
15 | * |
||
16 | * Usage example (from any other host): |
||
17 | * echo -n "some password" | nc host_to_rebot 19 |
||
18 | * |
||
19 | * Copyleft 2005 Anatoli Klassen |
||
20 | * |
||
21 | ******************************************************************/ |
||
22 | |||
23 | #include <stdlib.h> |
||
24 | #include <stdio.h> |
||
25 | #include <string.h> |
||
26 | #include <stdarg.h> |
||
1144 | dev | 27 | #include <ctype.h> |
1140 | dev | 28 | #include <errno.h> |
29 | #include <signal.h> |
||
30 | #include <unistd.h> |
||
31 | #include <netdb.h> |
||
32 | #include <syslog.h> |
||
33 | #include <sys/types.h> |
||
1144 | dev | 34 | #include <sys/stat.h> |
1140 | dev | 35 | #include <sys/socket.h> |
36 | #include <sys/wait.h> |
||
37 | #include <sys/reboot.h> |
||
38 | #include <netinet/in.h> |
||
39 | #include <arpa/inet.h> |
||
40 | |||
1145 | dev | 41 | /* name version number */ |
42 | #define APP_NAME "RebootDaemon" |
||
43 | #define VERSION "1.0" |
||
44 | #define REVISION "$Rev: 1145 $" |
||
1144 | dev | 45 | |
1145 | dev | 46 | #define DEFAULT_PORT 19 |
47 | #define DEFAULT_CFG_FILE "/etc/rebootdaemon.conf" |
||
48 | #define CMDSIZE 4096 |
||
49 | #define BUFSIZE 4096 |
||
50 | #define MAX_CONFIG_LINE 4096 |
||
51 | #define PORT_MIN 1 |
||
52 | #define PORT_MAX 65535 |
||
1140 | dev | 53 | |
1145 | dev | 54 | /* return values of functions */ |
55 | #define RESULT_OK 0 |
||
56 | #define RESULT_EXIT 1 |
||
57 | #define RESULT_ERROR 2 |
||
58 | #define RESULT_UNEXPECTED 3 |
||
1140 | dev | 59 | |
1145 | dev | 60 | /* return values for the whole program */ |
61 | #define EXIT_OK 0 |
||
62 | #define EXIT_USER_ERROR 1 |
||
63 | #define EXIT_UNEXPECTED 2 |
||
64 | |||
65 | /* command line and config file parameters */ |
||
66 | #define PARAM_HELP_1 "-h" |
||
67 | #define PARAM_HELP_2 "--help" |
||
68 | #define PARAM_VERSION_1 "-v" |
||
69 | #define PARAM_VERSION_2 "--version" |
||
70 | #define PARAM_PORT_1 "-p" |
||
71 | #define PARAM_PORT_2 "--port" |
||
72 | #define PARAM_CONFIG_1 "-c" |
||
73 | #define PARAM_CONFIG_2 "--config" |
||
74 | #define CONFIG_PORT "port" |
||
75 | #define CONFIG_PASSWORD "password" |
||
76 | |||
1140 | dev | 77 | struct config { |
78 | int port; |
||
79 | char password[MAX_CONFIG_LINE]; |
||
80 | }; |
||
81 | |||
82 | static int parse_cmd_line(int argc, char* argv[], long *port, char *config_name); |
||
83 | static int parse_config_file(char *config_name, long *port, char *password); |
||
1145 | dev | 84 | static int get_revision(); |
1140 | dev | 85 | |
86 | static int min(int a, int b) |
||
87 | { |
||
88 | return (a < b) ? a : b; |
||
89 | } |
||
90 | |||
91 | static int establish(unsigned short portnum) |
||
92 | { |
||
93 | int s; |
||
94 | struct sockaddr_in sa; |
||
95 | |||
96 | memset(&sa, 0, sizeof(struct sockaddr_in)); |
||
97 | |||
98 | sa.sin_family = AF_INET; |
||
99 | sa.sin_port = htons(portnum); |
||
100 | sa.sin_addr.s_addr = INADDR_ANY; |
||
101 | |||
102 | if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
||
103 | return -1; |
||
104 | |||
105 | if(bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { |
||
106 | close(s); |
||
107 | return -1; |
||
108 | } |
||
109 | |||
110 | listen(s, 1); |
||
111 | |||
112 | return s; |
||
113 | } |
||
114 | |||
1143 | dev | 115 | static void listen_socket(struct config *cfg, int socket) |
1142 | dev | 116 | { |
117 | int t; |
||
118 | char cmd[CMDSIZE]; |
||
119 | char buf[BUFSIZE]; |
||
120 | char* msg; |
||
121 | int br; |
||
122 | struct sockaddr_in sa; |
||
123 | socklen_t sa_len; |
||
124 | |||
125 | for(;;) { |
||
126 | sa_len = sizeof(sa); |
||
127 | if((t = accept(socket, (struct sockaddr *)&sa, &sa_len)) < 0) { |
||
128 | if(errno == EINTR) /* EINTR might happen on accept(), */ |
||
129 | continue; /* try again */ |
||
130 | |||
131 | msg = strerror(errno); |
||
132 | syslog(LOG_ERR, "cannot get connection, %s", msg); |
||
133 | continue; |
||
134 | } |
||
135 | |||
136 | msg = inet_ntoa(sa.sin_addr); |
||
137 | syslog(LOG_INFO, "connect from %s", msg); |
||
138 | |||
139 | cmd[0] = '\0'; |
||
140 | while((br = recv(t, buf, BUFSIZE, 0)) > 0) { |
||
141 | strncat(cmd, buf, min(br, sizeof(cmd) - strlen(cmd) - 1)); |
||
142 | } |
||
143 | sleep(1); |
||
144 | close(t); |
||
145 | |||
146 | syslog(LOG_INFO, "got command [%s]", cmd); |
||
147 | |||
1143 | dev | 148 | if(0 == strncmp(cmd, cfg->password, sizeof(cmd))) { |
1142 | dev | 149 | syslog(LOG_EMERG, "REBOOT"); |
150 | sleep(5); |
||
151 | if(reboot(RB_AUTOBOOT) < 0) { |
||
152 | msg = strerror(errno); |
||
153 | syslog(LOG_ERR, "cannot reboot, %s", msg); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
1145 | dev | 159 | static void print_version() |
1140 | dev | 160 | { |
1145 | dev | 161 | printf("%s %s.%d\n", APP_NAME, VERSION, get_revision()); |
162 | } |
||
163 | |||
164 | static void pring_usage(int argc, char* argv[]) |
||
165 | { |
||
166 | print_version(); |
||
167 | printf("\nA daemon which waits on specified TCP port for special string and reboots the computer.\n\n"); |
||
168 | printf("Usage: %s [--port PORT] [--config CONFIG_FILE]\n", argv[0]); |
||
169 | printf(" or: %s --version\n", argv[0]); |
||
170 | printf(" or: %s --help\n\n", argv[0]); |
||
171 | printf("Defaults are port 19 and config /etc/rebootdaemon.conf.\n\n"); |
||
172 | printf("Config file looks like:\n--------------------------\n"); |
||
173 | printf("port=19\npassword=\"some password\"\n--------------------------\n\n"); |
||
174 | printf("Then run from any other host:\necho -n \"some password\" | nc host_to_rebot 19\n"); |
||
175 | } |
||
176 | |||
177 | static int print_cmd_error(int argc, char* argv[], char *msg, char *value) |
||
178 | { |
||
1140 | dev | 179 | fprintf(stderr, "%s", msg); |
180 | if(value) fprintf(stderr, " - %s\n", value); |
||
181 | fprintf(stderr, "\n"); |
||
182 | |||
1145 | dev | 183 | pring_usage(argc, argv); |
184 | |||
185 | return RESULT_ERROR; |
||
1140 | dev | 186 | } |
187 | |||
188 | static int print_config_error(char *config_name, int line, char *msg, char *value) |
||
189 | { |
||
190 | fprintf(stderr, "Error in %s, line %d: %s", config_name, line, msg); |
||
191 | if(value) fprintf(stderr, " - %s", value); |
||
192 | fprintf(stderr, "\n"); |
||
193 | |||
1145 | dev | 194 | return RESULT_ERROR; |
1140 | dev | 195 | } |
196 | |||
1145 | dev | 197 | /* return: 0 - no error, continue; 1 - no error, but exit; 2 - user error, exit; 3 - unexpected error, exit */ |
1143 | dev | 198 | static int read_config(struct config *cfg, int argc, char* argv[]) |
1140 | dev | 199 | { |
1145 | dev | 200 | int res_cmd_line; |
201 | int res_config_file; |
||
1140 | dev | 202 | long cmd_port; |
203 | long file_port; |
||
204 | char cmd_config[MAX_CONFIG_LINE]; |
||
205 | |||
1145 | dev | 206 | res_cmd_line = parse_cmd_line(argc, argv, &cmd_port, cmd_config); |
207 | if(res_cmd_line != RESULT_OK) return res_cmd_line; |
||
1140 | dev | 208 | |
1145 | dev | 209 | res_config_file = parse_config_file(cmd_config[0] != '\0' ? cmd_config : DEFAULT_CFG_FILE, |
210 | &file_port, cfg->password); |
||
211 | if(res_config_file != RESULT_OK) return res_config_file; |
||
1140 | dev | 212 | |
1143 | dev | 213 | if(cfg->password[0] == '\0') { |
1140 | dev | 214 | fprintf(stderr, "Password is not set\n"); |
1145 | dev | 215 | return RESULT_ERROR; |
1140 | dev | 216 | } |
217 | |||
218 | if(cmd_port > 0) |
||
1143 | dev | 219 | cfg->port = (int)cmd_port; |
1140 | dev | 220 | else if(file_port > 0) |
1143 | dev | 221 | cfg->port = (int)file_port; |
1140 | dev | 222 | else |
1143 | dev | 223 | cfg->port = DEFAULT_PORT; |
1140 | dev | 224 | |
1145 | dev | 225 | return RESULT_OK; |
1140 | dev | 226 | } |
227 | |||
228 | static int parse_cmd_line(int argc, char* argv[], long *port, char *config_name) |
||
229 | { |
||
230 | char *end; |
||
231 | int i; |
||
232 | |||
233 | *port = 0; |
||
234 | config_name[0] = '\0'; |
||
235 | |||
236 | for(i = 1; i < argc; i++) { |
||
237 | if(0 == strcmp(argv[i], PARAM_PORT_1) || 0 == strcmp(argv[i], PARAM_PORT_2)) { |
||
238 | if(*port > 0) |
||
1145 | dev | 239 | return print_cmd_error(argc, argv, "Port is already set", NULL); |
1140 | dev | 240 | |
241 | if(++i < argc) { |
||
242 | *port = strtol(argv[i], &end, 10); |
||
243 | if(*end != '\0' || *port < PORT_MIN || *port > PORT_MAX) |
||
1145 | dev | 244 | return print_cmd_error(argc, argv, |
245 | "Port number must be integer between 1 and 65535", NULL); |
||
1140 | dev | 246 | } |
247 | else { |
||
1145 | dev | 248 | return print_cmd_error(argc, argv, "Port number expected", NULL); |
1140 | dev | 249 | } |
250 | } |
||
251 | else if(0 == strcmp(argv[i], PARAM_CONFIG_1) || 0 == strcmp(argv[i], PARAM_CONFIG_2)) { |
||
252 | if(config_name[0] != '\0') |
||
1145 | dev | 253 | return print_cmd_error(argc, argv, "Config file is already set", NULL); |
1140 | dev | 254 | |
255 | if(++i < argc) { |
||
256 | strncpy(config_name, argv[i], MAX_CONFIG_LINE); |
||
257 | if(config_name[MAX_CONFIG_LINE - 1] != '\0') |
||
1145 | dev | 258 | return print_cmd_error(argc, argv, "Config file name is too long", NULL); |
1140 | dev | 259 | } |
260 | else { |
||
1145 | dev | 261 | return print_cmd_error(argc, argv, "Config file expected", NULL); |
1140 | dev | 262 | } |
263 | } |
||
1145 | dev | 264 | else if(0 == strcmp(argv[i], PARAM_VERSION_1) || 0 == strcmp(argv[i], PARAM_VERSION_2)) { |
265 | print_version(); |
||
266 | return RESULT_EXIT; |
||
267 | } |
||
268 | else if(0 == strcmp(argv[i], PARAM_HELP_1) || 0 == strcmp(argv[i], PARAM_HELP_2)) { |
||
269 | pring_usage(argc, argv); |
||
270 | return RESULT_EXIT; |
||
271 | } |
||
1140 | dev | 272 | else { |
1145 | dev | 273 | return print_cmd_error(argc, argv, "Unknown parameter", argv[i]); |
1140 | dev | 274 | } |
275 | } |
||
276 | |||
1145 | dev | 277 | return RESULT_OK; |
1140 | dev | 278 | } |
279 | |||
280 | |||
281 | static int parse_config_file(char *config_name, long *port, char *password) |
||
282 | { |
||
283 | FILE *config_file; |
||
284 | char buf[MAX_CONFIG_LINE]; |
||
285 | char *line; |
||
286 | char *subline; |
||
287 | char *cur; |
||
288 | int count; |
||
289 | |||
290 | config_file = fopen(config_name, "r"); |
||
291 | if(!config_file) { |
||
292 | fprintf(stderr, "Can not open config file %s: %s\n", config_name, strerror(errno)); |
||
1145 | dev | 293 | return RESULT_ERROR; |
1140 | dev | 294 | } |
295 | |||
296 | count = 0; |
||
297 | while(fgets(buf, sizeof(buf), config_file)) { |
||
298 | count++; |
||
299 | line = buf; |
||
300 | |||
301 | /* skip end spaces */ |
||
302 | int len = strlen(line); |
||
303 | while(len && isspace(line[len-1])) --len; |
||
304 | if(!len) continue; |
||
305 | line[len] = '\0'; |
||
306 | |||
307 | /* skip begin spaces */ |
||
308 | while(line[0] != '\0' && isspace(line[0])) line++; |
||
309 | |||
310 | if('#' == line[0]) { /* skip comment lines */ |
||
311 | continue; |
||
312 | } |
||
313 | else if(strncmp(line, CONFIG_PORT, min(sizeof(CONFIG_PORT) - 1, len)) == 0) { |
||
1145 | dev | 314 | if('=' != subline[0] && !isspace(subline[0])) |
315 | return print_config_error(config_name, count, "Unknown config parameter", line); |
||
1140 | dev | 316 | subline = line + sizeof(CONFIG_PORT) - 1; |
317 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
318 | if('=' != subline[0]) |
||
319 | return print_config_error(config_name, count, "Equal sign expected", NULL); |
||
320 | subline++; |
||
321 | *port = strtol(subline, &subline, 10); |
||
322 | if(*port < PORT_MIN || *port > PORT_MAX) |
||
323 | return print_config_error(config_name, count, |
||
324 | "Port number must be integer between 1 and 65535", NULL); |
||
325 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
326 | if('\0' != subline[0] && '#' != subline[0]) |
||
327 | return print_config_error(config_name, count, "End of line expected", NULL); |
||
328 | } |
||
329 | else if(strncmp(line, CONFIG_PASSWORD, min(sizeof(CONFIG_PASSWORD) - 1, len)) == 0) { |
||
1145 | dev | 330 | if('=' != subline[0] && !isspace(subline[0])) |
331 | return print_config_error(config_name, count, "Unknown config parameter", line); |
||
1140 | dev | 332 | subline = line + sizeof(CONFIG_PASSWORD) - 1; |
333 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
334 | if('=' != subline[0]) |
||
335 | return print_config_error(config_name, count, "Equal sign expected", NULL); |
||
336 | subline++; |
||
337 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
338 | if('"' != subline[0]) |
||
339 | return print_config_error(config_name, count, "Open quot expected", NULL); |
||
340 | subline++; |
||
341 | |||
342 | cur = password; |
||
343 | while(subline[0] != '\0' && subline[0] != '"') { |
||
344 | cur[0] = subline[0]; |
||
345 | cur++; |
||
346 | subline++; |
||
347 | } |
||
348 | |||
349 | cur[0] = '\0'; |
||
350 | if('"' != subline[0]) |
||
351 | return print_config_error(config_name, count, "Close quot expected", NULL); |
||
352 | subline++; |
||
353 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
354 | if('\0' != subline[0] && '#' != subline[0]) |
||
355 | return print_config_error(config_name, count, "End of line expected", NULL); |
||
356 | } |
||
357 | else { |
||
358 | return print_config_error(config_name, count, "Unknown config parameter", line); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | if(fclose(config_file) != 0) { |
||
363 | fprintf(stderr, "Can not close config file %s: %s\n", config_name, strerror(errno)); |
||
1145 | dev | 364 | return RESULT_UNEXPECTED; |
1140 | dev | 365 | } |
366 | |||
1145 | dev | 367 | return RESULT_OK; |
1140 | dev | 368 | } |
369 | |||
1145 | dev | 370 | static int get_revision() |
371 | { |
||
372 | int r; |
||
373 | char *begin; |
||
374 | |||
375 | begin = REVISION; |
||
376 | while(begin[0] != '\0' && begin[0] != ':') begin++; |
||
377 | if(begin[0] == '\0') return 0; |
||
378 | begin++; |
||
379 | r = strtol(begin, (char**)NULL, 10); |
||
380 | |||
381 | return r; |
||
382 | } |
||
383 | |||
1143 | dev | 384 | static int create_child(int socket) |
385 | { |
||
386 | pid_t child; |
||
387 | |||
388 | child = fork(); |
||
389 | if(child == -1) { |
||
390 | fprintf(stderr, "Cannot fork: %s\n", strerror(errno)); |
||
1145 | dev | 391 | return RESULT_UNEXPECTED; /* an unexpected error */ |
1143 | dev | 392 | } |
393 | else if(child > 0) { |
||
394 | if(close(socket) != 0) { |
||
395 | fprintf(stderr, "Cannot close socket: %s\n", strerror(errno)); |
||
1145 | dev | 396 | return RESULT_UNEXPECTED; /* an unexpected error */ |
1143 | dev | 397 | } |
1145 | dev | 398 | return RESULT_EXIT; /* we are the parent */ |
1143 | dev | 399 | } |
400 | else { |
||
1145 | dev | 401 | return RESULT_OK; /* we are the child */ |
1143 | dev | 402 | } |
403 | } |
||
404 | |||
405 | static int close_descr(int d) |
||
406 | { |
||
407 | if(close(d) != 0) { |
||
408 | syslog(LOG_ERR, "Cannot close descriptor %d: %s\n", d, strerror(errno)); |
||
1145 | dev | 409 | return RESULT_UNEXPECTED; |
1143 | dev | 410 | } |
1145 | dev | 411 | return RESULT_OK; |
1143 | dev | 412 | } |
413 | |||
1140 | dev | 414 | int main(int argc, char* argv[]) |
415 | { |
||
1143 | dev | 416 | int socket; |
417 | struct config cfg; |
||
1140 | dev | 418 | |
1143 | dev | 419 | /* get config */ |
1145 | dev | 420 | switch(read_config(&cfg, argc, argv)) { |
421 | case RESULT_EXIT: return EXIT_OK; // no error but exit |
||
422 | case RESULT_ERROR: return EXIT_USER_ERROR; // user error |
||
423 | case RESULT_UNEXPECTED: return EXIT_UNEXPECTED; // unexpected error |
||
424 | } |
||
1143 | dev | 425 | |
426 | /* try to listen the port */ |
||
427 | if((socket = establish(cfg.port)) < 0) { |
||
1140 | dev | 428 | fprintf(stderr, "Cannot listen to port %i\n", cfg.port); |
1145 | dev | 429 | return EXIT_USER_ERROR; |
1140 | dev | 430 | } |
431 | |||
1143 | dev | 432 | /* fork and release the console */ |
433 | switch(create_child(socket)) { |
||
1145 | dev | 434 | case RESULT_EXIT: return EXIT_OK; // no error but exit |
435 | case RESULT_ERROR: return EXIT_USER_ERROR; // user error |
||
436 | case RESULT_UNEXPECTED: return EXIT_UNEXPECTED; // unexpected error |
||
1143 | dev | 437 | } |
1140 | dev | 438 | |
1143 | dev | 439 | /* continue as first child */ |
440 | if(setsid() == -1) { |
||
441 | fprintf(stderr, "Cannot create session: %s\n", strerror(errno)); |
||
1145 | dev | 442 | return EXIT_UNEXPECTED; |
1143 | dev | 443 | } |
444 | |||
445 | /* fork the second time */ |
||
446 | switch(create_child(socket)) { |
||
1145 | dev | 447 | case RESULT_EXIT: return EXIT_OK; // no error but exit |
448 | case RESULT_ERROR: return EXIT_USER_ERROR; // user error |
||
449 | case RESULT_UNEXPECTED: return EXIT_UNEXPECTED; // unexpected error |
||
1143 | dev | 450 | } |
451 | |||
452 | /* continue as final child, from now do not use console for error output */ |
||
453 | chdir("/"); |
||
454 | umask(0); |
||
1145 | dev | 455 | if(close_descr(0) != RESULT_OK) return EXIT_UNEXPECTED; |
456 | if(close_descr(1) != RESULT_OK) return EXIT_UNEXPECTED; |
||
457 | if(close_descr(2) != RESULT_OK) return EXIT_UNEXPECTED; |
||
1143 | dev | 458 | |
459 | syslog(LOG_INFO, "listen on %d", cfg.port); |
||
460 | listen_socket(&cfg, socket); |
||
461 | |||
1145 | dev | 462 | return EXIT_OK; // unreachedable |
1140 | dev | 463 | } |
464 |