Annotation of ansh/src/ansh3d.c, revision 1.1.1.1.2.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 $
1.1.1.1.2.1! misho       6:  * $Id: ansh3d.c,v 1.1.1.1 2011/10/04 22:37:46 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.1! misho      15: int Verbose;
        !            16: u_int Crypted = 1;
1.1       misho      17: proc_head_t pH;
                     18: int bpfLEN, Timeout, Daemon = 1;
                     19: 
                     20: extern char compiled[], compiledby[], compilehost[];
                     21: 
                     22: static void
                     23: Usage()
                     24: {
                     25:        printf( " -= anshd =- ELWIX Layer3 remote management service over ICMP\n"
                     26:                "=== %s === %s@%s ===\n\n"
                     27:                " Syntax: ansh3d [options]\n\n"
                     28:                "\t-a <host>\tBind to host address (default is *any*)\n"
                     29:                "\t-i <id>\tService ID (default is 42)\n"
                     30:                "\t-U <user>\tRun service with other user\n"
                     31:                "\t-C <dir>\tRun service into chroot directory\n"
                     32:                "\t-t <timeout>\tTimeout of login if no activity (default is 0 sec)\n"
                     33:                "\t-u\t\tSwitch to unencrypted traffic between hosts\n"
                     34:                "\t-b\t\tRun into batch mode (default is daemon mode)\n"
                     35:                "\t-v\t\tVerbose (more -v, more verbosity ...)\n"
                     36:                "\t-h\t\tThis help screen!\n"
                     37:                "\n", compiled, compiledby, compilehost);
                     38: }
                     39: 
                     40: static void
                     41: sig(int s)
                     42: {
                     43:        int state;
                     44: 
                     45:        switch (s) {
                     46:                case SIGHUP:
                     47:                        VERB(1) LOG("Got SIGHUP!\n");
                     48:                        break;
                     49:                case SIGTERM:
                     50:                        Kill++;
                     51:                        VERB(1) LOG("Got SIGTERM!\n");
                     52:                        break;
                     53:                case SIGPIPE:
                     54:                        VERB(1) LOG("Got SIGPIPE!\n");
                     55:                        break;
                     56:                case SIGCHLD:
                     57:                        VERB(1) LOG("Got SIGCHLD!\n");
                     58:                        while (waitpid(-1, &state, WNOHANG) > 0);
                     59:                        break;
                     60:        }
                     61: }
                     62: 
                     63: static void *
                     64: hook_error(void *root, void *arg)
                     65: {
                     66: /*     sched_root_task_t *r = root; */
                     67: 
                     68:        if (!root)
                     69:                return (void*) -1;
                     70: 
                     71:        if (arg == (void*) EINTR)
                     72:                return (void*) -1;
                     73: 
                     74:        return NULL;
                     75: }
                     76: 
                     77: int
                     78: main(int argc, char **argv)
                     79: {
                     80:        struct sockaddr sa = { 0 };
                     81:        struct sockaddr_in *sin4 = (struct sockaddr_in*) &sa;
                     82:        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
                     83:        struct hostent *host;
                     84:        struct passwd *pass;
                     85:        int fd, h = 0, uid = 0, gid = 0;
                     86:        long id = ANSH_ID;
                     87:        char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/";
                     88:        struct sigaction sact;
                     89:        sched_root_task_t *root = NULL;
                     90:        struct tagProc *proc;
                     91: 
                     92:        while ((ch = getopt(argc, argv, "hvubt:a:i:U:C:")) != -1)
                     93:                switch (ch) {
                     94:                        case 'U':
                     95:                                pass = getpwnam(optarg);
                     96:                                if (!pass) {
                     97:                                        printf("Error:: User %s not found!\n", optarg);
                     98:                                        return 1;
                     99:                                } else {
                    100:                                        strlcpy(szUser, optarg, sizeof szUser);
                    101:                                        uid = pass->pw_uid;
                    102:                                        gid = pass->pw_gid;
                    103:                                }
                    104:                                endpwent();
                    105:                                break;
                    106:                        case 'C':
                    107:                                if (access(optarg, R_OK)) {
                    108:                                        printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
                    109:                                        return 1;
                    110:                                } else
                    111:                                        strlcpy(szChroot, optarg, sizeof szChroot);
                    112:                                break;
                    113:                        case 'i':
                    114:                                id = strtol(optarg, NULL, 0);
                    115:                                break;
                    116:                        case 't':
                    117:                                Timeout = abs(strtol(optarg, NULL, 0));
                    118:                                break;
                    119:                        case 'a':
                    120:                                host = gethostbyname(optarg);
                    121:                                if (!host) {
                    122:                                        printf("Error:: in bind address '%s' #%d - %s\n", 
                    123:                                                        optarg, h_errno, hstrerror(h_errno));
                    124:                                        return 1;
                    125:                                }
                    126:                                switch (host->h_addrtype) {
                    127:                                        case AF_INET:
                    128:                                                sin4->sin_len = sizeof(struct sockaddr_in);
                    129:                                                sin4->sin_family = AF_INET;
                    130:                                                memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
                    131:                                                break;
                    132:                                        case AF_INET6:
                    133:                                                sin6->sin6_len = sizeof(struct sockaddr_in6);
                    134:                                                sin6->sin6_family = AF_INET6;
                    135:                                                memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
                    136:                                                break;
                    137:                                        default:
                    138:                                                printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
                    139:                                                return 1;
                    140:                                }
                    141:                                break;
                    142:                        case 'u':
                    143:                                Crypted ^= Crypted;
                    144:                                break;
                    145:                        case 'b':
                    146:                                Daemon ^= Daemon;
                    147:                                break;
                    148:                        case 'v':
                    149:                                Verbose++;
                    150:                                break;
                    151:                        case 'h':
                    152:                        default:
                    153:                                Usage();
                    154:                                return 1;
                    155:                }
                    156:        argc -= optind;
                    157:        argv += optind;
                    158: 
                    159:        /* sanity check for openned descriptor */
                    160:        if (!sa.sa_family) {
                    161:                sin4->sin_len = sizeof(struct sockaddr_in);
                    162:                sin4->sin_family = AF_INET;
                    163:        }
                    164: 
                    165:        /* catch signals */
                    166:        memset(&sact, 0, sizeof sact);
                    167:        sigemptyset(&sact.sa_mask);
                    168:        sact.sa_handler = sig;
                    169:        sigaction(SIGPIPE, &sact, NULL);
                    170:        sigaction(SIGCHLD, &sact, NULL);
                    171:        sigaction(SIGTERM, &sact, NULL);
                    172:        sigaction(SIGHUP, &sact, NULL);
                    173: 
                    174:        openlog("ansh3d", LOG_CONS | LOG_PID, LOG_DAEMON);
                    175: 
                    176:        if (Daemon) {
                    177:                switch (fork()) {
                    178:                        case -1:
                    179:                                ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
                    180:                                closelog();
                    181:                                return 1;
                    182:                        case 0:
                    183:                                VERB(1) LOG("Welcome to dark ...\n");
                    184: 
                    185:                                setsid();
                    186: 
                    187:                                fd = open("/dev/null", O_WRONLY);
                    188:                                if (fd) {
                    189:                                        dup2(fd, STDIN_FILENO);
                    190:                                        dup2(fd, STDOUT_FILENO);
                    191:                                        dup2(fd, STDERR_FILENO);
                    192:                                        if (fd > 2)
                    193:                                                close(fd);
                    194:                                }
                    195:                                break;
                    196:                        default:
                    197:                                VERB(1) LOG("Going to shadow land ...\n");
                    198:                                closelog();
                    199:                                return 0;
                    200:                }
                    201:        }
                    202: 
                    203:        if (ioCreatePIDFile(PIDFILE_ANSH3D, 42)) {
                    204:                ERR("Error:: already started ansh3d service ...\n");
                    205:                closelog();
                    206:                return 1;
                    207:        }
                    208: 
                    209:        h = PrepareL3(&sa, &bpfLEN);
                    210:        if (h == -1) {
                    211:                ERR("Error:: Descriptor not opened ... abort!\n");
                    212:                unlink(PIDFILE_ANSH3D);
                    213:                closelog();
                    214:                return 2;
                    215:        }
                    216: 
                    217:        SLIST_INIT(&pH);
                    218:        if (!(proc = InitProc(h, NULL, id, bpfLEN))) {
                    219:                ERR("Error:: Not enough memory ...\n");
                    220:                close(h);
                    221:                unlink(PIDFILE_ANSH3D);
                    222:                closelog();
                    223:                return 3;
                    224:        }
                    225: 
                    226:        root = schedBegin();
                    227:        if (!root) {
                    228:                ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
                    229:                DestroyProc(id);
                    230:                close(h);
                    231:                unlink(PIDFILE_ANSH3D);
                    232:                closelog();
                    233:                return 4;
                    234:        } else
                    235:                root->root_hooks.hook_root.error = hook_error;
                    236: 
                    237:        chdir("/");
                    238:        chroot(szChroot);
                    239: 
                    240:        setgid(gid);
                    241:        setuid(uid);
                    242: 
                    243:        if (schedRead(root, icmpRx, (void*) id, h)) {
                    244:                schedRun(root, &Kill);
                    245:        } else
                    246:                ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
                    247: 
                    248:        VERB(1) LOG("Finish process.");
                    249:        schedEnd(&root);
                    250:        DestroyProc(id);
                    251:        close(h);
                    252:        unlink(PIDFILE_ANSH3D);
                    253:        closelog();
                    254:        return 0;
                    255: }

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