Annotation of ansh/src/anshd.c, revision 1.1.1.1

1.1       misho       1: /*************************************************************************
                      2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
                      3:  *  by Michael Pounov <misho@elwix.org>
                      4:  *
                      5:  * $Author: misho $
                      6:  * $Id: global.h,v 1.2 2011/06/08 12:45:40 misho Exp $
                      7:  *
                      8:  *************************************************************************/
                      9: #include "global.h"
                     10: #include "anshd.h"
                     11: #include "proc.h"
                     12: 
                     13: 
                     14: intptr_t Kill;
                     15: int bpfLEN, Verbose, Crypted = 1;
                     16: proc_head_t pH;
                     17: int Daemon = 1;
                     18: 
                     19: extern char compiled[], compiledby[], compilehost[];
                     20: 
                     21: static void
                     22: Usage()
                     23: {
                     24:        printf( " -= anshd =- ELWIX Layer2 remote management service\n"
                     25:                "=== %s === %s@%s ===\n\n"
                     26:                " Syntax: anshd [options]\n\n"
                     27:                "\t-d <dev>\tBind to host interface, like 'em0' (default is first host interface)\n"
                     28:                "\t-i <id>\tService ID (default is 42)\n"
                     29:                "\t-U <user>\tRun service with other user\n"
                     30:                "\t-C <dir>\tRun service into chroot directory\n"
                     31:                "\t-u\t\tSwitch to unencrypted traffic between hosts\n"
                     32:                "\t-b\t\tRun into batch mode (default is daemon mode)\n"
                     33:                "\t-v\t\tVerbose (more -v, more verbosity ...)\n"
                     34:                "\t-h\t\tThis help screen!\n"
                     35:                "\n", compiled, compiledby, compilehost);
                     36: }
                     37: 
                     38: static void
                     39: sig(int s)
                     40: {
                     41:        int state;
                     42: 
                     43:        switch (s) {
                     44:                case SIGHUP:
                     45:                        LOG("Got SIGHUP!\n");
                     46:                        break;
                     47:                case SIGTERM:
                     48:                        LOG("Got SIGTERM!\n");
                     49:                        Kill++;
                     50:                        break;
                     51:                case SIGPIPE:
                     52:                        LOG("Got SIGPIPE!\n");
                     53:                        break;
                     54:                case SIGCHLD:
                     55:                        while (waitpid(-1, &state, WNOHANG) > 0);
                     56:                        break;
                     57:        }
                     58: }
                     59: 
                     60: int
                     61: main(int argc, char **argv)
                     62: {
                     63:        struct passwd *pass;
                     64:        int fd, h = 0, uid = 0, gid = 0;
                     65:        long id = ANSH_ID;
                     66:        char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/", szDev[STRSIZ] = { 0 };
                     67:        struct sigaction sact;
                     68:        sched_root_task_t *root = NULL;
                     69:        struct tagProc *proc;
                     70: 
                     71:        Get1stEth(szDev, STRSIZ);
                     72: 
                     73:        while ((ch = getopt(argc, argv, "hvulbd:U:C:")) != -1)
                     74:                switch (ch) {
                     75:                        case 'U':
                     76:                                pass = getpwnam(optarg);
                     77:                                if (!pass) {
                     78:                                        printf("Error:: User %s not found!\n", optarg);
                     79:                                        return 1;
                     80:                                } else {
                     81:                                        strlcpy(szUser, optarg, sizeof szUser);
                     82:                                        uid = pass->pw_uid;
                     83:                                        gid = pass->pw_gid;
                     84:                                }
                     85:                                endpwent();
                     86:                                break;
                     87:                        case 'C':
                     88:                                if (access(optarg, R_OK)) {
                     89:                                        printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
                     90:                                        return 1;
                     91:                                } else
                     92:                                        strlcpy(szChroot, optarg, sizeof szChroot);
                     93:                                break;
                     94:                        case 'i':
                     95:                                id = strtol(optarg, NULL, 0);
                     96:                                break;
                     97:                        case 'd':
                     98:                                strlcpy(szDev, optarg, sizeof szDev);
                     99:                                break;
                    100:                        case 'u':
                    101:                                Crypted ^= Crypted;
                    102:                                break;
                    103:                        case 'b':
                    104:                                Daemon ^= Daemon;
                    105:                                break;
                    106:                        case 'v':
                    107:                                Verbose++;
                    108:                                break;
                    109:                        case 'h':
                    110:                        default:
                    111:                                Usage();
                    112:                                return 1;
                    113:                }
                    114:        argc -= optind;
                    115:        argv += optind;
                    116: 
                    117:        /* catch signals */
                    118:        memset(&sact, 0, sizeof sact);
                    119:        sigemptyset(&sact.sa_mask);
                    120:        sact.sa_handler = sig;
                    121:        sigaction(SIGPIPE, &sact, NULL);
                    122:        sigaction(SIGCHLD, &sact, NULL);
                    123:        sigaction(SIGTERM, &sact, NULL);
                    124:        sigaction(SIGHUP, &sact, NULL);
                    125: 
                    126:        openlog("anshd", LOG_CONS | LOG_PID, LOG_DAEMON);
                    127: 
                    128:        if (Daemon) {
                    129:                switch (fork()) {
                    130:                        case -1:
                    131:                                ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
                    132:                                closelog();
                    133:                                return 1;
                    134:                        case 0:
                    135:                                VERB(1) LOG("Welcome to dark ...\n");
                    136: 
                    137:                                setsid();
                    138: 
                    139:                                fd = open("/dev/null", O_WRONLY);
                    140:                                if (fd) {
                    141:                                        dup2(fd, STDIN_FILENO);
                    142:                                        dup2(fd, STDOUT_FILENO);
                    143:                                        dup2(fd, STDERR_FILENO);
                    144:                                        if (fd > 2)
                    145:                                                close(fd);
                    146:                                }
                    147:                                break;
                    148:                        default:
                    149:                                VERB(1) LOG("Going to shadow land ...\n");
                    150:                                closelog();
                    151:                                return 0;
                    152:                }
                    153:        }
                    154: 
                    155:        h = PrepareL2(szDev, &bpfLEN);
                    156:        if (h == -1) {
                    157:                ERR("Error:: Descriptor not opened ... abort!\n");
                    158:                closelog();
                    159:                return 2;
                    160:        }
                    161: 
                    162:        SLIST_INIT(&pH);
                    163:        if (!(proc = InitProc(h, NULL, id, bpfLEN))) {
                    164:                ERR("Error:: Not enough memory ...\n");
                    165:                close(h);
                    166:                closelog();
                    167:                return 3;
                    168:        }
                    169: 
                    170:        root = schedBegin();
                    171:        if (!root) {
                    172:                ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
                    173:                DestroyProc(id);
                    174:                close(h);
                    175:                closelog();
                    176:                return 4;
                    177:        }
                    178: 
                    179:        chdir("/");
                    180:        chroot(szChroot);
                    181: 
                    182:        setgid(gid);
                    183:        setuid(uid);
                    184: 
                    185:        if (schedRead(root, pktRx, (void*) id, h)) {
                    186:                schedRun(root, &Kill);
                    187:        } else
                    188:                ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
                    189: 
                    190:        VERB(1) LOG("Finish process.");
                    191:        schedEnd(&root);
                    192:        DestroyProc(id);
                    193:        close(h);
                    194:        closelog();
                    195:        return 0;
                    196: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>