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

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

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