Subversion Repositories general

Rev

Rev 1142 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/******************************************************************
 *
 * Daemon which waiting on specified TCP port for special string
 * and reboot the computer.
 *
 * Command line: rebootdaemon [--port PORT] [--config CONFIG_FILE]
 * Defaults are port 19 and config /etc/rebootdaemon.conf.
 *
 * Config file looks like:
 * --------------------------
 * port=19
 * password="some password"
 * --------------------------
 *
 * Usage example (from any other host):
 * echo -n "some password" | nc host_to_rebot 19
 *
 * Copyleft 2005 Anatoli Klassen
 *
 ******************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/reboot.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT             19
#define CFG_FILE         "/etc/rebootdaemon.conf"
#define CMDSIZE          4096
#define BUFSIZE          4096
#define MAX_CONFIG_LINE  4096
#define PORT_MIN         1
#define PORT_MAX         65535

#define PARAM_PORT_1     "-p"
#define PARAM_PORT_2     "--port"
#define PARAM_CONFIG_1   "-c"
#define PARAM_CONFIG_2   "--config"
#define CONFIG_PORT      "port"
#define CONFIG_PASSWORD  "password"

struct config {
        int  port;
        char password[MAX_CONFIG_LINE];
};

static struct config cfg;

static int parse_cmd_line(int argc, char* argv[], long *port, char *config_name);
static int parse_config_file(char *config_name, long *port, char *password);

static int min(int a, int b)
{
        return (a < b) ? a : b;
}

static int establish(unsigned short portnum)
{
        int s;
        struct sockaddr_in sa;

        memset(&sa, 0, sizeof(struct sockaddr_in));

        sa.sin_family      = AF_INET;
        sa.sin_port        = htons(portnum);
        sa.sin_addr.s_addr = INADDR_ANY;

        if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
                return -1;

        if(bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
                close(s);
                return -1;
        }

        listen(s, 1);

        return s;
}

static int print_cmd_error(char *msg, char *value)
{
        fprintf(stderr, "%s", msg);
        if(value) fprintf(stderr, " - %s\n", value);
        fprintf(stderr, "\n");

        return 0;
}

static int print_config_error(char *config_name, int line, char *msg, char *value)
{
        fprintf(stderr, "Error in %s, line %d: %s", config_name, line, msg);
        if(value) fprintf(stderr, " - %s", value);
        fprintf(stderr, "\n");

        return 0;
}

static int read_config(int argc, char* argv[])
{
        long cmd_port;
        long file_port;
        char cmd_config[MAX_CONFIG_LINE];

        if(!parse_cmd_line(argc, argv, &cmd_port, cmd_config)) return 0;

        if(!parse_config_file(cmd_config[0] != '\0' ? cmd_config : CFG_FILE, &file_port, cfg.password)) return 0;

        if(cfg.password[0] == '\0') {
                fprintf(stderr, "Password is not set\n");
                return 0;
        }

        if(cmd_port > 0)
                cfg.port = (int)cmd_port;
        else if(file_port > 0)
                cfg.port = (int)file_port;
        else
                cfg.port = PORT;

        return 1;
}

static int parse_cmd_line(int argc, char* argv[], long *port, char *config_name)
{
        char *end;
        int  i;

        *port          = 0;
        config_name[0] = '\0';

        for(i = 1; i < argc; i++) {
                if(0 == strcmp(argv[i], PARAM_PORT_1) || 0 == strcmp(argv[i], PARAM_PORT_2)) {
                        if(*port > 0)
                                return print_cmd_error("Port is already set", NULL);

                        if(++i < argc) {
                                *port = strtol(argv[i], &end, 10);
                                if(*end != '\0' || *port < PORT_MIN || *port > PORT_MAX)
                                        return print_cmd_error("Port number must be integer between 1 and 65535", NULL);
                        }
                        else {
                                return print_cmd_error("Port number expected", NULL);
                        }
                }
                else if(0 == strcmp(argv[i], PARAM_CONFIG_1) || 0 == strcmp(argv[i], PARAM_CONFIG_2)) {
                        if(config_name[0] != '\0')
                                return print_cmd_error("Config file is already set", NULL);

                        if(++i < argc) {
                                strncpy(config_name, argv[i], MAX_CONFIG_LINE);
                                if(config_name[MAX_CONFIG_LINE - 1] != '\0')
                                        return print_cmd_error("Config file name is too long", NULL);
                        }
                        else {
                                return print_cmd_error("Config file expected", NULL);
                        }
                }
                else {
                        return print_cmd_error("Unknown parameter", argv[i]);
                }
        }

        return 1;
}


static int parse_config_file(char *config_name, long *port, char *password)
{
        FILE *config_file;
        char buf[MAX_CONFIG_LINE];
        char *line;
        char *subline;
        char *cur;
        int  count;

        config_file = fopen(config_name, "r");
        if(!config_file) {
                fprintf(stderr, "Can not open config file %s: %s\n", config_name, strerror(errno));
                return 0;
        }

        count = 0;
        while(fgets(buf, sizeof(buf), config_file)) {
                count++;
                line = buf;

                /* skip end spaces */
                int len = strlen(line);
                while(len && isspace(line[len-1])) --len;
                if(!len) continue;
                line[len] = '\0';

                /* skip begin spaces */
                while(line[0] != '\0' && isspace(line[0])) line++;

                if('#' == line[0]) { /* skip comment lines */
                        continue;
                }
                else if(strncmp(line, CONFIG_PORT, min(sizeof(CONFIG_PORT) - 1, len)) == 0) {
                        subline = line + sizeof(CONFIG_PORT) - 1;
                        while(subline[0] != '\0' && isspace(subline[0])) subline++;
                        if('=' != subline[0])
                                return print_config_error(config_name, count, "Equal sign expected", NULL);
                        subline++;
                        *port = strtol(subline, &subline, 10);
                        if(*port < PORT_MIN || *port > PORT_MAX)
                                return print_config_error(config_name, count, 
                                        "Port number must be integer between 1 and 65535", NULL);
                        while(subline[0] != '\0' && isspace(subline[0])) subline++;
                        if('\0' != subline[0] && '#' != subline[0])
                                return print_config_error(config_name, count, "End of line expected", NULL);
                }
                else if(strncmp(line, CONFIG_PASSWORD, min(sizeof(CONFIG_PASSWORD) - 1, len)) == 0) {
                        subline = line + sizeof(CONFIG_PASSWORD) - 1;
                        while(subline[0] != '\0' && isspace(subline[0])) subline++;
                        if('=' != subline[0])
                                return print_config_error(config_name, count, "Equal sign expected", NULL);
                        subline++;
                        while(subline[0] != '\0' && isspace(subline[0])) subline++;
                        if('"' != subline[0])
                                return print_config_error(config_name, count, "Open quot expected", NULL);
                        subline++;

                        cur = password;
                        while(subline[0] != '\0' && subline[0] != '"') {
                                cur[0] = subline[0];
                                cur++;
                                subline++;
                        }

                        cur[0] = '\0';
                        if('"' != subline[0])
                                return print_config_error(config_name, count, "Close quot expected", NULL);
                        subline++;
                        while(subline[0] != '\0' && isspace(subline[0])) subline++;
                        if('\0' != subline[0] && '#' != subline[0])
                                return print_config_error(config_name, count, "End of line expected", NULL);
                }
                else {
                        return print_config_error(config_name, count, "Unknown config parameter", line);
                }
        }

        if(fclose(config_file) != 0) {
                fprintf(stderr, "Can not close config file %s: %s\n", config_name, strerror(errno));
                return 0;
        }

        return 1;
}

int main(int argc, char* argv[])
{
        int                s, t;
        char               cmd[CMDSIZE];
        char               buf[BUFSIZE];
        char*              msg;
        int                br;
        struct sockaddr_in sa;
        socklen_t          sa_len;

        if(!read_config(argc, argv)) return 1;

        if((s = establish(cfg.port)) < 0) {
                fprintf(stderr, "Cannot listen to port %i\n", cfg.port);
                return 1;
        }

        for(;;) {
                sa_len = sizeof(sa);
                if((t = accept(s, (struct sockaddr *)&sa, &sa_len)) < 0) {
                        if(errno == EINTR) /* EINTR might happen on accept(), */
                                continue;        /* try again */

                        msg = strerror(errno);
                        syslog(LOG_ERR, "cannot get connection, %s", msg);
                        continue;
                }

                msg = inet_ntoa(sa.sin_addr);
                syslog(LOG_INFO, "connect from %s", msg);

                cmd[0] = '\0';
                while((br = recv(t, buf, BUFSIZE, 0)) > 0) {
                        strncat(cmd, buf, min(br, sizeof(cmd) - strlen(cmd) - 1));
                }
                sleep(1);
                close(t);

                syslog(LOG_INFO, "got command [%s]", cmd);
                
                if(0 == strncmp(cmd, cfg.password, sizeof(cmd))) {
                        syslog(LOG_EMERG, "REBOOT");
                        sleep(5);
                        if(reboot(RB_AUTOBOOT) < 0) {
                                msg = strerror(errno);
                                syslog(LOG_ERR, "cannot reboot, %s", msg);
                        }
                }
        }

        return 0;
}