Annotation of ansh/src/ansh3d.c, revision 1.1.1.1.2.2

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.2! misho       6:  * $Id: ansh3d.c,v 1.1.1.1.2.1 2011/10/07 13:41:26 misho Exp $
1.1       misho       7:  *
1.1.1.1.2.2! misho       8:  *************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
1.1       misho      46: #include "global.h"
                     47: #include "anshd.h"
                     48: #include "proc.h"
                     49: 
                     50: 
                     51: intptr_t Kill;
1.1.1.1.2.1  misho      52: int Verbose;
                     53: u_int Crypted = 1;
1.1       misho      54: proc_head_t pH;
                     55: int bpfLEN, Timeout, Daemon = 1;
                     56: 
                     57: extern char compiled[], compiledby[], compilehost[];
                     58: 
                     59: static void
                     60: Usage()
                     61: {
                     62:        printf( " -= anshd =- ELWIX Layer3 remote management service over ICMP\n"
                     63:                "=== %s === %s@%s ===\n\n"
                     64:                " Syntax: ansh3d [options]\n\n"
                     65:                "\t-a <host>\tBind to host address (default is *any*)\n"
                     66:                "\t-i <id>\tService ID (default is 42)\n"
                     67:                "\t-U <user>\tRun service with other user\n"
                     68:                "\t-C <dir>\tRun service into chroot directory\n"
                     69:                "\t-t <timeout>\tTimeout of login if no activity (default is 0 sec)\n"
                     70:                "\t-u\t\tSwitch to unencrypted traffic between hosts\n"
                     71:                "\t-b\t\tRun into batch mode (default is daemon mode)\n"
                     72:                "\t-v\t\tVerbose (more -v, more verbosity ...)\n"
                     73:                "\t-h\t\tThis help screen!\n"
                     74:                "\n", compiled, compiledby, compilehost);
                     75: }
                     76: 
                     77: static void
                     78: sig(int s)
                     79: {
                     80:        int state;
                     81: 
                     82:        switch (s) {
                     83:                case SIGHUP:
                     84:                        VERB(1) LOG("Got SIGHUP!\n");
                     85:                        break;
                     86:                case SIGTERM:
                     87:                        Kill++;
                     88:                        VERB(1) LOG("Got SIGTERM!\n");
                     89:                        break;
                     90:                case SIGPIPE:
                     91:                        VERB(1) LOG("Got SIGPIPE!\n");
                     92:                        break;
                     93:                case SIGCHLD:
                     94:                        VERB(1) LOG("Got SIGCHLD!\n");
                     95:                        while (waitpid(-1, &state, WNOHANG) > 0);
                     96:                        break;
                     97:        }
                     98: }
                     99: 
                    100: static void *
                    101: hook_error(void *root, void *arg)
                    102: {
                    103: /*     sched_root_task_t *r = root; */
                    104: 
                    105:        if (!root)
                    106:                return (void*) -1;
                    107: 
                    108:        if (arg == (void*) EINTR)
                    109:                return (void*) -1;
                    110: 
                    111:        return NULL;
                    112: }
                    113: 
                    114: int
                    115: main(int argc, char **argv)
                    116: {
                    117:        struct sockaddr sa = { 0 };
                    118:        struct sockaddr_in *sin4 = (struct sockaddr_in*) &sa;
                    119:        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
                    120:        struct hostent *host;
                    121:        struct passwd *pass;
                    122:        int fd, h = 0, uid = 0, gid = 0;
                    123:        long id = ANSH_ID;
                    124:        char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/";
                    125:        struct sigaction sact;
                    126:        sched_root_task_t *root = NULL;
                    127:        struct tagProc *proc;
                    128: 
                    129:        while ((ch = getopt(argc, argv, "hvubt:a:i:U:C:")) != -1)
                    130:                switch (ch) {
                    131:                        case 'U':
                    132:                                pass = getpwnam(optarg);
                    133:                                if (!pass) {
                    134:                                        printf("Error:: User %s not found!\n", optarg);
                    135:                                        return 1;
                    136:                                } else {
                    137:                                        strlcpy(szUser, optarg, sizeof szUser);
                    138:                                        uid = pass->pw_uid;
                    139:                                        gid = pass->pw_gid;
                    140:                                }
                    141:                                endpwent();
                    142:                                break;
                    143:                        case 'C':
                    144:                                if (access(optarg, R_OK)) {
                    145:                                        printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
                    146:                                        return 1;
                    147:                                } else
                    148:                                        strlcpy(szChroot, optarg, sizeof szChroot);
                    149:                                break;
                    150:                        case 'i':
                    151:                                id = strtol(optarg, NULL, 0);
                    152:                                break;
                    153:                        case 't':
                    154:                                Timeout = abs(strtol(optarg, NULL, 0));
                    155:                                break;
                    156:                        case 'a':
                    157:                                host = gethostbyname(optarg);
                    158:                                if (!host) {
                    159:                                        printf("Error:: in bind address '%s' #%d - %s\n", 
                    160:                                                        optarg, h_errno, hstrerror(h_errno));
                    161:                                        return 1;
                    162:                                }
                    163:                                switch (host->h_addrtype) {
                    164:                                        case AF_INET:
                    165:                                                sin4->sin_len = sizeof(struct sockaddr_in);
                    166:                                                sin4->sin_family = AF_INET;
                    167:                                                memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
                    168:                                                break;
                    169:                                        case AF_INET6:
                    170:                                                sin6->sin6_len = sizeof(struct sockaddr_in6);
                    171:                                                sin6->sin6_family = AF_INET6;
                    172:                                                memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
                    173:                                                break;
                    174:                                        default:
                    175:                                                printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
                    176:                                                return 1;
                    177:                                }
                    178:                                break;
                    179:                        case 'u':
                    180:                                Crypted ^= Crypted;
                    181:                                break;
                    182:                        case 'b':
                    183:                                Daemon ^= Daemon;
                    184:                                break;
                    185:                        case 'v':
                    186:                                Verbose++;
                    187:                                break;
                    188:                        case 'h':
                    189:                        default:
                    190:                                Usage();
                    191:                                return 1;
                    192:                }
                    193:        argc -= optind;
                    194:        argv += optind;
                    195: 
                    196:        /* sanity check for openned descriptor */
                    197:        if (!sa.sa_family) {
                    198:                sin4->sin_len = sizeof(struct sockaddr_in);
                    199:                sin4->sin_family = AF_INET;
                    200:        }
                    201: 
                    202:        /* catch signals */
                    203:        memset(&sact, 0, sizeof sact);
                    204:        sigemptyset(&sact.sa_mask);
                    205:        sact.sa_handler = sig;
                    206:        sigaction(SIGPIPE, &sact, NULL);
                    207:        sigaction(SIGCHLD, &sact, NULL);
                    208:        sigaction(SIGTERM, &sact, NULL);
                    209:        sigaction(SIGHUP, &sact, NULL);
                    210: 
                    211:        openlog("ansh3d", LOG_CONS | LOG_PID, LOG_DAEMON);
                    212: 
                    213:        if (Daemon) {
                    214:                switch (fork()) {
                    215:                        case -1:
                    216:                                ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
                    217:                                closelog();
                    218:                                return 1;
                    219:                        case 0:
                    220:                                VERB(1) LOG("Welcome to dark ...\n");
                    221: 
                    222:                                setsid();
                    223: 
                    224:                                fd = open("/dev/null", O_WRONLY);
                    225:                                if (fd) {
                    226:                                        dup2(fd, STDIN_FILENO);
                    227:                                        dup2(fd, STDOUT_FILENO);
                    228:                                        dup2(fd, STDERR_FILENO);
                    229:                                        if (fd > 2)
                    230:                                                close(fd);
                    231:                                }
                    232:                                break;
                    233:                        default:
                    234:                                VERB(1) LOG("Going to shadow land ...\n");
                    235:                                closelog();
                    236:                                return 0;
                    237:                }
                    238:        }
                    239: 
                    240:        if (ioCreatePIDFile(PIDFILE_ANSH3D, 42)) {
                    241:                ERR("Error:: already started ansh3d service ...\n");
                    242:                closelog();
                    243:                return 1;
                    244:        }
                    245: 
                    246:        h = PrepareL3(&sa, &bpfLEN);
                    247:        if (h == -1) {
                    248:                ERR("Error:: Descriptor not opened ... abort!\n");
                    249:                unlink(PIDFILE_ANSH3D);
                    250:                closelog();
                    251:                return 2;
                    252:        }
                    253: 
                    254:        SLIST_INIT(&pH);
                    255:        if (!(proc = InitProc(h, NULL, id, bpfLEN))) {
                    256:                ERR("Error:: Not enough memory ...\n");
                    257:                close(h);
                    258:                unlink(PIDFILE_ANSH3D);
                    259:                closelog();
                    260:                return 3;
                    261:        }
                    262: 
                    263:        root = schedBegin();
                    264:        if (!root) {
                    265:                ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
                    266:                DestroyProc(id);
                    267:                close(h);
                    268:                unlink(PIDFILE_ANSH3D);
                    269:                closelog();
                    270:                return 4;
                    271:        } else
                    272:                root->root_hooks.hook_root.error = hook_error;
                    273: 
                    274:        chdir("/");
                    275:        chroot(szChroot);
                    276: 
                    277:        setgid(gid);
                    278:        setuid(uid);
                    279: 
                    280:        if (schedRead(root, icmpRx, (void*) id, h)) {
                    281:                schedRun(root, &Kill);
                    282:        } else
                    283:                ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
                    284: 
                    285:        VERB(1) LOG("Finish process.");
                    286:        schedEnd(&root);
                    287:        DestroyProc(id);
                    288:        close(h);
                    289:        unlink(PIDFILE_ANSH3D);
                    290:        closelog();
                    291:        return 0;
                    292: }

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