Rev 1147 | Rev 1156 | 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: 1155 $" |
||
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 | |||
1155 | dev | 110 | if(listen(s, 1) != 0) { |
111 | close(s); |
||
112 | return -1; |
||
113 | } |
||
1140 | dev | 114 | |
115 | return s; |
||
116 | } |
||
117 | |||
1143 | dev | 118 | static void listen_socket(struct config *cfg, int socket) |
1142 | dev | 119 | { |
120 | int t; |
||
121 | char cmd[CMDSIZE]; |
||
122 | char buf[BUFSIZE]; |
||
123 | int br; |
||
124 | struct sockaddr_in sa; |
||
125 | socklen_t sa_len; |
||
1155 | dev | 126 | int cmdlen; |
1142 | dev | 127 | |
128 | for(;;) { |
||
129 | sa_len = sizeof(sa); |
||
130 | if((t = accept(socket, (struct sockaddr *)&sa, &sa_len)) < 0) { |
||
131 | if(errno == EINTR) /* EINTR might happen on accept(), */ |
||
132 | continue; /* try again */ |
||
133 | |||
1155 | dev | 134 | syslog(LOG_ERR, "cannot get connection, %s", strerror(errno)); |
1142 | dev | 135 | continue; |
136 | } |
||
137 | |||
1155 | dev | 138 | syslog(LOG_INFO, "connect from %s", inet_ntoa(sa.sin_addr)); |
1142 | dev | 139 | |
140 | cmd[0] = '\0'; |
||
1155 | dev | 141 | cmdlen = 0; |
1142 | dev | 142 | while((br = recv(t, buf, BUFSIZE, 0)) > 0) { |
1155 | dev | 143 | strncat(cmd, buf, min(br, sizeof(cmd) - cmdlen - 1)); |
144 | cmdlen += min(br, sizeof(cmd) - cmdlen - 1); |
||
1142 | dev | 145 | } |
146 | sleep(1); |
||
147 | close(t); |
||
148 | |||
1147 | dev | 149 | syslog(LOG_INFO, "got command"); |
1142 | dev | 150 | |
1143 | dev | 151 | if(0 == strncmp(cmd, cfg->password, sizeof(cmd))) { |
1142 | dev | 152 | syslog(LOG_EMERG, "REBOOT"); |
153 | sleep(5); |
||
154 | if(reboot(RB_AUTOBOOT) < 0) { |
||
1155 | dev | 155 | syslog(LOG_ERR, "cannot reboot, %s", strerror(errno)); |
1142 | dev | 156 | } |
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
1145 | dev | 161 | static void print_version() |
1140 | dev | 162 | { |
1145 | dev | 163 | printf("%s %s.%d\n", APP_NAME, VERSION, get_revision()); |
164 | } |
||
165 | |||
166 | static void pring_usage(int argc, char* argv[]) |
||
167 | { |
||
168 | print_version(); |
||
169 | printf("\nA daemon which waits on specified TCP port for special string and reboots the computer.\n\n"); |
||
170 | printf("Usage: %s [--port PORT] [--config CONFIG_FILE]\n", argv[0]); |
||
171 | printf(" or: %s --version\n", argv[0]); |
||
172 | printf(" or: %s --help\n\n", argv[0]); |
||
173 | printf("Defaults are port 19 and config /etc/rebootdaemon.conf.\n\n"); |
||
174 | printf("Config file looks like:\n--------------------------\n"); |
||
175 | printf("port=19\npassword=\"some password\"\n--------------------------\n\n"); |
||
176 | printf("Then run from any other host:\necho -n \"some password\" | nc host_to_rebot 19\n"); |
||
177 | } |
||
178 | |||
179 | static int print_cmd_error(int argc, char* argv[], char *msg, char *value) |
||
180 | { |
||
1140 | dev | 181 | fprintf(stderr, "%s", msg); |
182 | if(value) fprintf(stderr, " - %s\n", value); |
||
183 | fprintf(stderr, "\n"); |
||
184 | |||
1145 | dev | 185 | pring_usage(argc, argv); |
186 | |||
187 | return RESULT_ERROR; |
||
1140 | dev | 188 | } |
189 | |||
190 | static int print_config_error(char *config_name, int line, char *msg, char *value) |
||
191 | { |
||
192 | fprintf(stderr, "Error in %s, line %d: %s", config_name, line, msg); |
||
193 | if(value) fprintf(stderr, " - %s", value); |
||
194 | fprintf(stderr, "\n"); |
||
195 | |||
1145 | dev | 196 | return RESULT_ERROR; |
1140 | dev | 197 | } |
198 | |||
1145 | dev | 199 | /* return: 0 - no error, continue; 1 - no error, but exit; 2 - user error, exit; 3 - unexpected error, exit */ |
1143 | dev | 200 | static int read_config(struct config *cfg, int argc, char* argv[]) |
1140 | dev | 201 | { |
1145 | dev | 202 | int res_cmd_line; |
203 | int res_config_file; |
||
1140 | dev | 204 | long cmd_port; |
205 | long file_port; |
||
206 | char cmd_config[MAX_CONFIG_LINE]; |
||
207 | |||
1145 | dev | 208 | res_cmd_line = parse_cmd_line(argc, argv, &cmd_port, cmd_config); |
209 | if(res_cmd_line != RESULT_OK) return res_cmd_line; |
||
1140 | dev | 210 | |
1145 | dev | 211 | res_config_file = parse_config_file(cmd_config[0] != '\0' ? cmd_config : DEFAULT_CFG_FILE, |
212 | &file_port, cfg->password); |
||
213 | if(res_config_file != RESULT_OK) return res_config_file; |
||
1140 | dev | 214 | |
1143 | dev | 215 | if(cfg->password[0] == '\0') { |
1140 | dev | 216 | fprintf(stderr, "Password is not set\n"); |
1145 | dev | 217 | return RESULT_ERROR; |
1140 | dev | 218 | } |
219 | |||
220 | if(cmd_port > 0) |
||
1143 | dev | 221 | cfg->port = (int)cmd_port; |
1140 | dev | 222 | else if(file_port > 0) |
1143 | dev | 223 | cfg->port = (int)file_port; |
1140 | dev | 224 | else |
1143 | dev | 225 | cfg->port = DEFAULT_PORT; |
1140 | dev | 226 | |
1145 | dev | 227 | return RESULT_OK; |
1140 | dev | 228 | } |
229 | |||
230 | static int parse_cmd_line(int argc, char* argv[], long *port, char *config_name) |
||
231 | { |
||
232 | char *end; |
||
233 | int i; |
||
234 | |||
235 | *port = 0; |
||
236 | config_name[0] = '\0'; |
||
237 | |||
238 | for(i = 1; i < argc; i++) { |
||
239 | if(0 == strcmp(argv[i], PARAM_PORT_1) || 0 == strcmp(argv[i], PARAM_PORT_2)) { |
||
240 | if(*port > 0) |
||
1145 | dev | 241 | return print_cmd_error(argc, argv, "Port is already set", NULL); |
1140 | dev | 242 | |
243 | if(++i < argc) { |
||
244 | *port = strtol(argv[i], &end, 10); |
||
245 | if(*end != '\0' || *port < PORT_MIN || *port > PORT_MAX) |
||
1145 | dev | 246 | return print_cmd_error(argc, argv, |
247 | "Port number must be integer between 1 and 65535", NULL); |
||
1140 | dev | 248 | } |
249 | else { |
||
1145 | dev | 250 | return print_cmd_error(argc, argv, "Port number expected", NULL); |
1140 | dev | 251 | } |
252 | } |
||
253 | else if(0 == strcmp(argv[i], PARAM_CONFIG_1) || 0 == strcmp(argv[i], PARAM_CONFIG_2)) { |
||
254 | if(config_name[0] != '\0') |
||
1145 | dev | 255 | return print_cmd_error(argc, argv, "Config file is already set", NULL); |
1140 | dev | 256 | |
257 | if(++i < argc) { |
||
258 | strncpy(config_name, argv[i], MAX_CONFIG_LINE); |
||
259 | if(config_name[MAX_CONFIG_LINE - 1] != '\0') |
||
1155 | dev | 260 | return print_cmd_error(argc, argv, |
261 | "Config file name is too long", NULL); |
||
1140 | dev | 262 | } |
263 | else { |
||
1145 | dev | 264 | return print_cmd_error(argc, argv, "Config file expected", NULL); |
1140 | dev | 265 | } |
266 | } |
||
1145 | dev | 267 | else if(0 == strcmp(argv[i], PARAM_VERSION_1) || 0 == strcmp(argv[i], PARAM_VERSION_2)) { |
268 | print_version(); |
||
269 | return RESULT_EXIT; |
||
270 | } |
||
271 | else if(0 == strcmp(argv[i], PARAM_HELP_1) || 0 == strcmp(argv[i], PARAM_HELP_2)) { |
||
272 | pring_usage(argc, argv); |
||
273 | return RESULT_EXIT; |
||
274 | } |
||
1140 | dev | 275 | else { |
1145 | dev | 276 | return print_cmd_error(argc, argv, "Unknown parameter", argv[i]); |
1140 | dev | 277 | } |
278 | } |
||
279 | |||
1145 | dev | 280 | return RESULT_OK; |
1140 | dev | 281 | } |
282 | |||
283 | |||
284 | static int parse_config_file(char *config_name, long *port, char *password) |
||
285 | { |
||
286 | FILE *config_file; |
||
287 | char buf[MAX_CONFIG_LINE]; |
||
288 | char *line; |
||
289 | char *subline; |
||
290 | char *cur; |
||
291 | int count; |
||
292 | |||
293 | config_file = fopen(config_name, "r"); |
||
294 | if(!config_file) { |
||
295 | fprintf(stderr, "Can not open config file %s: %s\n", config_name, strerror(errno)); |
||
1145 | dev | 296 | return RESULT_ERROR; |
1140 | dev | 297 | } |
298 | |||
299 | count = 0; |
||
300 | while(fgets(buf, sizeof(buf), config_file)) { |
||
301 | count++; |
||
302 | line = buf; |
||
303 | |||
304 | /* skip end spaces */ |
||
305 | int len = strlen(line); |
||
1146 | dev | 306 | if(len == MAX_CONFIG_LINE-1) |
307 | return print_config_error(config_name, count, "Line is too long", NULL); |
||
1140 | dev | 308 | while(len && isspace(line[len-1])) --len; |
309 | if(!len) continue; |
||
310 | line[len] = '\0'; |
||
311 | |||
312 | /* skip begin spaces */ |
||
313 | while(line[0] != '\0' && isspace(line[0])) line++; |
||
314 | |||
315 | if('#' == line[0]) { /* skip comment lines */ |
||
316 | continue; |
||
317 | } |
||
318 | else if(strncmp(line, CONFIG_PORT, min(sizeof(CONFIG_PORT) - 1, len)) == 0) { |
||
1146 | dev | 319 | subline = line + sizeof(CONFIG_PORT) - 1; |
1145 | dev | 320 | if('=' != subline[0] && !isspace(subline[0])) |
321 | return print_config_error(config_name, count, "Unknown config parameter", line); |
||
1140 | dev | 322 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
323 | if('=' != subline[0]) |
||
324 | return print_config_error(config_name, count, "Equal sign expected", NULL); |
||
325 | subline++; |
||
326 | *port = strtol(subline, &subline, 10); |
||
327 | if(*port < PORT_MIN || *port > PORT_MAX) |
||
328 | return print_config_error(config_name, count, |
||
329 | "Port number must be integer between 1 and 65535", NULL); |
||
330 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
331 | if('\0' != subline[0] && '#' != subline[0]) |
||
332 | return print_config_error(config_name, count, "End of line expected", NULL); |
||
333 | } |
||
334 | else if(strncmp(line, CONFIG_PASSWORD, min(sizeof(CONFIG_PASSWORD) - 1, len)) == 0) { |
||
1146 | dev | 335 | subline = line + sizeof(CONFIG_PASSWORD) - 1; |
1145 | dev | 336 | if('=' != subline[0] && !isspace(subline[0])) |
337 | return print_config_error(config_name, count, "Unknown config parameter", line); |
||
1140 | dev | 338 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
339 | if('=' != subline[0]) |
||
340 | return print_config_error(config_name, count, "Equal sign expected", NULL); |
||
341 | subline++; |
||
342 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
343 | if('"' != subline[0]) |
||
344 | return print_config_error(config_name, count, "Open quot expected", NULL); |
||
345 | subline++; |
||
346 | |||
347 | cur = password; |
||
348 | while(subline[0] != '\0' && subline[0] != '"') { |
||
349 | cur[0] = subline[0]; |
||
350 | cur++; |
||
351 | subline++; |
||
352 | } |
||
353 | |||
354 | cur[0] = '\0'; |
||
355 | if('"' != subline[0]) |
||
356 | return print_config_error(config_name, count, "Close quot expected", NULL); |
||
357 | subline++; |
||
358 | while(subline[0] != '\0' && isspace(subline[0])) subline++; |
||
359 | if('\0' != subline[0] && '#' != subline[0]) |
||
360 | return print_config_error(config_name, count, "End of line expected", NULL); |
||
361 | } |
||
362 | else { |
||
363 | return print_config_error(config_name, count, "Unknown config parameter", line); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | if(fclose(config_file) != 0) { |
||
368 | fprintf(stderr, "Can not close config file %s: %s\n", config_name, strerror(errno)); |
||
1145 | dev | 369 | return RESULT_UNEXPECTED; |
1140 | dev | 370 | } |
371 | |||
1145 | dev | 372 | return RESULT_OK; |
1140 | dev | 373 | } |
374 | |||
1145 | dev | 375 | static int get_revision() |
376 | { |
||
377 | int r; |
||
378 | char *begin; |
||
379 | |||
380 | begin = REVISION; |
||
381 | while(begin[0] != '\0' && begin[0] != ':') begin++; |
||
382 | if(begin[0] == '\0') return 0; |
||
383 | begin++; |
||
384 | r = strtol(begin, (char**)NULL, 10); |
||
385 | |||
386 | return r; |
||
387 | } |
||
388 | |||
1143 | dev | 389 | static int create_child(int socket) |
390 | { |
||
391 | pid_t child; |
||
392 | |||
393 | child = fork(); |
||
394 | if(child == -1) { |
||
395 | fprintf(stderr, "Cannot fork: %s\n", strerror(errno)); |
||
1145 | dev | 396 | return RESULT_UNEXPECTED; /* an unexpected error */ |
1143 | dev | 397 | } |
398 | else if(child > 0) { |
||
399 | if(close(socket) != 0) { |
||
400 | fprintf(stderr, "Cannot close socket: %s\n", strerror(errno)); |
||
1145 | dev | 401 | return RESULT_UNEXPECTED; /* an unexpected error */ |
1143 | dev | 402 | } |
1145 | dev | 403 | return RESULT_EXIT; /* we are the parent */ |
1143 | dev | 404 | } |
405 | else { |
||
1145 | dev | 406 | return RESULT_OK; /* we are the child */ |
1143 | dev | 407 | } |
408 | } |
||
409 | |||
410 | static int close_descr(int d) |
||
411 | { |
||
412 | if(close(d) != 0) { |
||
413 | syslog(LOG_ERR, "Cannot close descriptor %d: %s\n", d, strerror(errno)); |
||
1145 | dev | 414 | return RESULT_UNEXPECTED; |
1143 | dev | 415 | } |
1145 | dev | 416 | return RESULT_OK; |
1143 | dev | 417 | } |
418 | |||
1140 | dev | 419 | int main(int argc, char* argv[]) |
420 | { |
||
1143 | dev | 421 | int socket; |
422 | struct config cfg; |
||
1140 | dev | 423 | |
1143 | dev | 424 | /* get config */ |
1145 | dev | 425 | switch(read_config(&cfg, argc, argv)) { |
426 | case RESULT_EXIT: return EXIT_OK; // no error but exit |
||
427 | case RESULT_ERROR: return EXIT_USER_ERROR; // user error |
||
428 | case RESULT_UNEXPECTED: return EXIT_UNEXPECTED; // unexpected error |
||
429 | } |
||
1143 | dev | 430 | |
431 | /* try to listen the port */ |
||
432 | if((socket = establish(cfg.port)) < 0) { |
||
1140 | dev | 433 | fprintf(stderr, "Cannot listen to port %i\n", cfg.port); |
1145 | dev | 434 | return EXIT_USER_ERROR; |
1140 | dev | 435 | } |
436 | |||
1143 | dev | 437 | /* fork and release the console */ |
438 | switch(create_child(socket)) { |
||
1145 | dev | 439 | case RESULT_EXIT: return EXIT_OK; // no error but exit |
440 | case RESULT_ERROR: return EXIT_USER_ERROR; // user error |
||
441 | case RESULT_UNEXPECTED: return EXIT_UNEXPECTED; // unexpected error |
||
1143 | dev | 442 | } |
1140 | dev | 443 | |
1143 | dev | 444 | /* continue as first child */ |
445 | if(setsid() == -1) { |
||
446 | fprintf(stderr, "Cannot create session: %s\n", strerror(errno)); |
||
1145 | dev | 447 | return EXIT_UNEXPECTED; |
1143 | dev | 448 | } |
449 | |||
450 | /* fork the second time */ |
||
451 | switch(create_child(socket)) { |
||
1145 | dev | 452 | case RESULT_EXIT: return EXIT_OK; // no error but exit |
453 | case RESULT_ERROR: return EXIT_USER_ERROR; // user error |
||
454 | case RESULT_UNEXPECTED: return EXIT_UNEXPECTED; // unexpected error |
||
1143 | dev | 455 | } |
456 | |||
457 | /* continue as final child, from now do not use console for error output */ |
||
458 | chdir("/"); |
||
459 | umask(0); |
||
1145 | dev | 460 | if(close_descr(0) != RESULT_OK) return EXIT_UNEXPECTED; |
461 | if(close_descr(1) != RESULT_OK) return EXIT_UNEXPECTED; |
||
462 | if(close_descr(2) != RESULT_OK) return EXIT_UNEXPECTED; |
||
1143 | dev | 463 | |
464 | syslog(LOG_INFO, "listen on %d", cfg.port); |
||
465 | listen_socket(&cfg, socket); |
||
466 | |||
1145 | dev | 467 | return EXIT_OK; // unreachedable |
1140 | dev | 468 | } |
469 |