Subversion Repositories general

Rev

Rev 1163 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

/******************************************************************
 * rebootd.
 *
 * A daemon which waits on specified TCP port for special string
 * (password) and reboots the computer.
 *
 * Command line: rebootd [--version] [--help] [--interface IP] [--port PORT] [--config CONFIG_FILE]
 * Defaults are port 19 and config /etc/rebootd.conf.
 *
 * Config file looks like:
 * --------------------------
 * interface=192.168.0.1
 * 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 <ctype.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/reboot.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/* name version number */
#define APP_NAME          "rebootd"
#define VERSION           "1.0"
#define REVISION          "$Rev: 1164 $"

#define DEFAULT_PORT      19
#define DEFAULT_CFG_FILE  "/etc/rebootd.conf"
#define CMDSIZE           4096
#define BUFSIZE           4096
#define MAX_CONFIG_LINE   4096
#define PORT_MIN          1
#define PORT_MAX          65535

/* return values of functions */
#define RESULT_OK         0
#define RESULT_EXIT       1
#define RESULT_ERROR      2
#define RESULT_UNEXPECTED 3

/* return values for the whole program */
#define EXIT_OK           0
#define EXIT_USER_ERROR   1
#define EXIT_UNEXPECTED   2

/* command line and config file parameters */
#define PARAM_HELP_1      "-h"
#define PARAM_HELP_2      "--help"
#define PARAM_VERSION_1   "-v"
#define PARAM_VERSION_2   "--version"
#define PARAM_INTERFACE_1 "-i"
#define PARAM_INTERFACE_2 "--interface"
#define PARAM_PORT_1      "-p"
#define PARAM_PORT_2      "--port"
#define PARAM_CONFIG_1    "-c"
#define PARAM_CONFIG_2    "--config"
#define CONFIG_INTERFACE  "interface"
#define CONFIG_PORT       "port"
#define CONFIG_PASSWORD   "password"

struct config {
        char           config_file[FILENAME_MAX];
        struct in_addr interface;
        ushort         port;
        char           password[MAX_CONFIG_LINE];
};

static int parse_cmd_line(struct config *cfg, int argc, const char* argv[]);
static int parse_config_file(struct config *cfg, const char *config_name);
static int get_revision(void);

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

static int establish(const struct config *cfg)
{
        int s;
        struct sockaddr_in sa;

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

        sa.sin_family      = AF_INET;
        sa.sin_port        = htons(cfg->port);
        sa.sin_addr.s_addr = (cfg->interface.s_addr ? cfg->interface.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;
        }

        if(listen(s, 1) != 0) {
                close(s);
                return -1;
        }

        return s;
}

static void listen_socket(const struct config *cfg, int soc)
{
        int                t;
        char               cmd[CMDSIZE];
        char               buf[BUFSIZE];
        int                br;
        struct sockaddr_in sa;
        socklen_t          sa_len;
        int                cmdlen;

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

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

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

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

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

static void print_version(void)
{
        printf("%s %s.%d\n", APP_NAME, VERSION, get_revision());
}

static void pring_usage(int argc, const char* argv[])
{
        print_version();
        printf("\nA daemon which waits on specified TCP port for special");
        printf(" string (password) and reboots the computer.\n\n");
        printf("Usage: %s [--interface IP] [--port PORT] [--config CONFIG_FILE]\n", argv[0]);
        printf("   or: %s --version\n", argv[0]);
        printf("   or: %s --help\n\n", argv[0]);
        printf("Defaults are port 19 and config /etc/rebootd.conf.\n\n");
        printf("Config file looks like:\n--------------------------\n");
        printf("interface=192.168.0.1\nport=19\npassword=\"some password\"\n--------------------------\n\n");
        printf("Then run from any other host:\necho -n \"some password\" | nc host_to_rebot 19\n");
}

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

        pring_usage(argc, argv);

        return RESULT_ERROR;
}

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

        return RESULT_ERROR;
}

/* return: 0 - no error, continue; 1 - no error, but exit; 2 - user error, exit; 3 - unexpected error, exit */
static int read_config(struct config *cfg, int argc, const char* argv[])
{
        int    res_cmd_line;
        int    res_config_file;
        struct config cmd_cfg;
        struct config file_cfg;

        memset(cfg, 0, sizeof(struct config));
        res_cmd_line = parse_cmd_line(&cmd_cfg, argc, argv);
        if(res_cmd_line != RESULT_OK) return res_cmd_line;

        res_config_file = parse_config_file(&file_cfg,
                cmd_cfg.config_file[0] != '\0' ? cmd_cfg.config_file : DEFAULT_CFG_FILE);
        if(res_config_file != RESULT_OK) return res_config_file;

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

        /* save parsed values to general config */
        strncpy(cfg->config_file, cmd_cfg.config_file, sizeof(cfg->config_file));

        if(cmd_cfg.interface.s_addr)
                cfg->interface = cmd_cfg.interface;
        else if(file_cfg.interface.s_addr)
                cfg->interface = file_cfg.interface;

        if(cmd_cfg.port > 0)
                cfg->port = cmd_cfg.port;
        else if(file_cfg.port > 0)
                cfg->port = file_cfg.port;
        else
                cfg->port = DEFAULT_PORT;

        strncpy(cfg->password, file_cfg.password, sizeof(cfg->password));

        return RESULT_OK;
}

static int parse_interface(const char *s, char **end, struct in_addr *ip)
{
        char       buf[MAX_CONFIG_LINE];
        int        count;
        const char *c;

        c     = s;
        count = 0;
        while(c[0] != '\0' && !isspace(c[0])) c++, count++;
        if(count > MAX_CONFIG_LINE) return RESULT_ERROR;
        strncpy(buf, s, count);
        buf[count] = '\0';
        c++;

        if(end) *end = (char *)c;

        if(inet_aton(buf, ip) == 1)
                return RESULT_OK;
        else
                return RESULT_ERROR;
}

static int parse_cmd_line(struct config *cfg, int argc, const char* argv[])
{
        char *end;
        int  i;
        long port;

        memset(cfg, 0, sizeof(struct config));

        for(i = 1; i < argc; i++) {
                if(0 == strcmp(argv[i], PARAM_INTERFACE_1) || 0 == strcmp(argv[i], PARAM_INTERFACE_2)) {
                        if(cfg->interface.s_addr)
                                return print_cmd_error(argc, argv, "Interface is already set", NULL);

                        if(++i < argc) {
                                if(parse_interface(argv[i], (char **)NULL, &cfg->interface) != RESULT_OK)
                                        return print_cmd_error(argc, argv, 
                                                "Cannot parse interface", argv[i]);
                        }
                        else {
                                return print_cmd_error(argc, argv, "Interface expected", NULL);
                        }
                }
                else if(0 == strcmp(argv[i], PARAM_PORT_1) || 0 == strcmp(argv[i], PARAM_PORT_2)) {
                        if(cfg->port)
                                return print_cmd_error(argc, argv, "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(argc, argv, 
                                                "Port number must be integer between 1 and 65535", NULL);
                                cfg->port = (ushort)port;
                        }
                        else {
                                return print_cmd_error(argc, argv, "Port number expected", NULL);
                        }
                }
                else if(0 == strcmp(argv[i], PARAM_CONFIG_1) || 0 == strcmp(argv[i], PARAM_CONFIG_2)) {
                        if(cfg->config_file[0] != '\0')
                                return print_cmd_error(argc, argv, "Config file is already set", NULL);

                        if(++i < argc) {
                                strncpy(cfg->config_file, argv[i], MAX_CONFIG_LINE);
                                if(cfg->config_file[MAX_CONFIG_LINE - 1] != '\0')
                                        return print_cmd_error(argc, argv,
                                                "Config file name is too long", NULL);
                        }
                        else {
                                return print_cmd_error(argc, argv, "Config file expected", NULL);
                        }
                }
                else if(0 == strcmp(argv[i], PARAM_VERSION_1) || 0 == strcmp(argv[i], PARAM_VERSION_2)) {
                        print_version();
                        return RESULT_EXIT;
                }
                else if(0 == strcmp(argv[i], PARAM_HELP_1) || 0 == strcmp(argv[i], PARAM_HELP_2)) {
                        pring_usage(argc, argv);
                        return RESULT_EXIT;
                }
                else {
                        return print_cmd_error(argc, argv, "Unknown parameter", argv[i]);
                }
        }

        return RESULT_OK;
}

static int validate_equal_sign(const char *config_name, const int count, const char *line,
        uint name_len, char **subline)
{
        const char *c;

        c = line + name_len - 1;
        if('=' != c[0] && !isspace(c[0]))
                return print_config_error(config_name, count, "Unknown config parameter", line);
        while(c[0] != '\0' && isspace(c[0])) c++;
        if('=' != c[0])
                return print_config_error(config_name, count, "Equal sign expected", NULL);
        c++;

        if(subline) *subline = (char *)c;

        return RESULT_OK;
}

static int validate_eol(const char *config_name, const int count, const char *line)
{
        while(line[0] != '\0' && isspace(line[0])) line++;
        if('\0' != line[0] && '#' != line[0])
                return print_config_error(config_name, count, "End of line expected", NULL);

        return RESULT_OK;
}

static int extract_quoted(const char *config_name, const int count, char **subline, char *string, uint len)
{
        uint cur_len;

        while(*subline[0] != '\0' && isspace(*subline[0])) (*subline)++;
        if('"' != *subline[0])
                return print_config_error(config_name, count, "Open quot expected", NULL);
        (*subline)++;

        cur_len = 0;
        while(cur_len < len && *subline[0] != '\0' && *subline[0] != '"')
        {
                string[0] = *subline[0];
                string++;
                (*subline)++;
                cur_len++;
        }

        string[0] = '\0';
        if('"' != *subline[0])
                return print_config_error(config_name, count, "Close quot expected", NULL);
        (*subline)++;

        return RESULT_OK;
}

static int parse_config_file(struct config *cfg, const char *config_name)
{
        FILE *config_file;
        char buf[MAX_CONFIG_LINE];
        char *line;
        char *subline;
        int  count;
        uint len;
        long port;
        int  res;

        memset(cfg, 0, sizeof(struct config));

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

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

                /* skip end spaces */
                len = strlen(line);
                if(len == MAX_CONFIG_LINE-1)
                        return print_config_error(config_name, count, "Line is too long", NULL);
                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_INTERFACE, min(sizeof(CONFIG_INTERFACE) - 1, len)) == 0) {
                        if((res = validate_equal_sign(config_name, count, line,
                                sizeof(CONFIG_INTERFACE), &subline)) != RESULT_OK) return res;

                        if(parse_interface(subline, &subline, &cfg->interface) != RESULT_OK)
                                return print_config_error(config_name, count,
                                        "Cannot parse interface", NULL);

                        if((res = validate_eol(config_name, count, subline)) != RESULT_OK) return res;
                }
                else if(strncmp(line, CONFIG_PORT, min(sizeof(CONFIG_PORT) - 1, len)) == 0) {
                        if((res = validate_equal_sign(config_name, count, line, sizeof(CONFIG_PORT),
                                &subline)) != RESULT_OK) return res;

                        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);

                        if((res = validate_eol(config_name, count, subline)) != RESULT_OK) return res;
                        cfg->port = (ushort)port;
                }
                else if(strncmp(line, CONFIG_PASSWORD, min(sizeof(CONFIG_PASSWORD) - 1, len)) == 0) {
                        if((res = validate_equal_sign(config_name, count, line,
                                sizeof(CONFIG_PASSWORD), &subline)) != RESULT_OK) return res;

                        if((res = extract_quoted(config_name, count, &subline,
                                cfg->password, sizeof(cfg->password))) != RESULT_OK) return res;

                        if((res = validate_eol(config_name, count, subline)) != RESULT_OK) return res;
                }
                else {
                        return print_config_error(config_name, count, "Unknown config parameter", line);
                }
        }
        if(ferror(config_file)) {
                fprintf(stderr, "Config file %s reading failed\n", config_name);
        }

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

        return RESULT_OK;
}

static int get_revision(void)
{
        int        r;
        const char *begin;

        begin = REVISION;
        while(begin[0] != '\0' && begin[0] != ':') begin++;
        if(begin[0] == '\0') return 0;
        begin++;
        r = (int)strtol(begin, (char**)NULL, 10);

        return r;
}

static int create_child(int soc)
{
        pid_t child;

        child = fork();
        if(child == -1) {
                fprintf(stderr, "Cannot fork: %s\n", strerror(errno));
                return RESULT_UNEXPECTED; /* an unexpected error */
        }
        else if(child > 0) {
                if(close(soc) != 0) {
                        fprintf(stderr, "Cannot close socket: %s\n", strerror(errno));
                        return RESULT_UNEXPECTED; /* an unexpected error */
                }
                return RESULT_EXIT; /* we are the parent */
        }
        else {
                return RESULT_OK; /* we are the child */
        }
}

static int close_descr(int d)
{
        if(close(d) != 0) {
                syslog(LOG_ERR, "Cannot close descriptor %d: %s\n", d, strerror(errno));
                return RESULT_UNEXPECTED;
        }
        return RESULT_OK;
}

int main(int argc, const char* argv[])
{
        int           soc;
        struct config cfg;

        /* get config */
        switch(read_config(&cfg, argc, argv)) {
                case RESULT_EXIT:       return EXIT_OK;           /* no error but exit */
                case RESULT_ERROR:      return EXIT_USER_ERROR;   /* user error */
                case RESULT_UNEXPECTED: return EXIT_UNEXPECTED;   /* unexpected error */
        }

        /* try to listen the port */
        if((soc = establish(&cfg)) < 0) {
                fprintf(stderr, "Cannot listen to port %i\n", cfg.port);
                return EXIT_USER_ERROR;
        }

        /* fork and release the console */
        switch(create_child(soc)) {
                case RESULT_EXIT:       return EXIT_OK;           /* no error but exit */
                case RESULT_ERROR:      return EXIT_USER_ERROR;   /* user error */
                case RESULT_UNEXPECTED: return EXIT_UNEXPECTED;   /* unexpected error */
        }

        /* continue as first child */
        if(setsid() == -1) {
                fprintf(stderr, "Cannot create session: %s\n", strerror(errno));
                return EXIT_UNEXPECTED;
        }

        /* fork the second time */
        switch(create_child(soc)) {
                case RESULT_EXIT:       return EXIT_OK;           /* no error but exit */
                case RESULT_ERROR:      return EXIT_USER_ERROR;   /* user error */
                case RESULT_UNEXPECTED: return EXIT_UNEXPECTED;   /* unexpected error */
        }

        /* continue as final child, from now do not use console for error output */
        chdir("/");
        umask(0);
        if(close_descr(0) != RESULT_OK) return EXIT_UNEXPECTED;
        if(close_descr(1) != RESULT_OK) return EXIT_UNEXPECTED;
        if(close_descr(2) != RESULT_OK) return EXIT_UNEXPECTED;

        syslog(LOG_INFO, "listen on %d", cfg.port);
        listen_socket(&cfg, soc);

        return EXIT_OK; /* unreachedable */
}