Annotation of ansh/src/daemon3.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: daemon3.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: 
                     12: 
                     13: void *
                     14: icmpTx(sched_task_t *task)
                     15: {
                     16:        struct tagProc *proc;
                     17:        int wlen;
1.1.1.1.2.1! misho      18:        u_char *str;
1.1       misho      19: 
                     20:        FTRACE(3);
                     21: 
                     22:        /* not found argument, drop data */
                     23:        if (!(proc = TASK_ARG(task)))
                     24:                return (void*) -1;
                     25: 
1.1.1.1.2.1! misho      26:        if (Crypted) {
        !            27:                str = cryptBuffer(proc->proc_buf_[FD2NET], proc->proc_rlen_[FD2NET], Crypted);
        !            28:                if (str) {
        !            29:                        memcpy(proc->proc_buf_[FD2NET], str, proc->proc_rlen_[FD2NET]);
        !            30:                        free(str);
        !            31:                }
        !            32:        }
        !            33: 
        !            34:        if ((wlen = icmpSend(TASK_FD(task), proc->proc_id, proc->proc_flg, Crypted, proc->proc_buf_[FD2NET], 
1.1       misho      35:                        proc->proc_rlen_[FD2NET], &proc->proc_cli, sizeof proc->proc_cli)) != ANSH_FLG_ERR) {
                     36:                proc->proc_flg = ANSH_FLG_OK;
                     37:                proc->proc_rlen_[FD2NET] = 0;
                     38:        }
                     39:        VERB(5) LOG("Sended %d bytes", wlen);
                     40: 
                     41:        return NULL;
                     42: }
                     43: 
                     44: void *
                     45: icmpRx(sched_task_t *task)
                     46: {
1.1.1.1.2.1! misho      47:        u_char *buf, *str;
1.1       misho      48:        struct sockaddr sa;
                     49:        int rlen, n = 0, salen = sizeof sa;
                     50:        struct tagProc *proc = NULL;
                     51:        char ret;
                     52:        u_short id, *b;
                     53: 
                     54:        FTRACE(3);
                     55: 
                     56:        rlen = bpfLEN;
                     57:        if (!(buf = malloc(rlen)))
                     58:                goto end;
                     59: 
1.1.1.1.2.1! misho      60:        if ((ret = icmpRecv(TASK_FD(task), &id, &Crypted, buf, &rlen, &sa, (socklen_t *) &salen)) == ANSH_FLG_ERR)
1.1       misho      61:                goto end;
                     62:        VERB(5) LOG("Received %d bytes", rlen);
                     63:        if (!(ret & ANSH_FLG_CPOUT))
                     64:                goto end;
                     65: 
                     66:        /* packet is ok find active session */
                     67:        SLIST_FOREACH(proc, &pH, proc_next)
                     68:                if (proc->proc_id == id) {
                     69:                        n = ANSH_CODE;
                     70:                        break;
                     71:                }
                     72:        /* not found in sessions, drop packet */
                     73:        if (n != ANSH_CODE) {
                     74:                proc = NULL;
                     75:                goto end;
                     76:        }
                     77: 
1.1.1.1.2.1! misho      78:        if (Crypted) {
        !            79:                str = cryptBuffer(buf, rlen, Crypted);
        !            80:                if (str) {
        !            81:                        memcpy(buf, str, rlen);
        !            82:                        free(str);
        !            83:                }
        !            84:        }
        !            85: 
1.1       misho      86:        switch (ret) {
                     87:                case ANSH_FLG_EOF:
                     88:                case ANSH_FLG_CPOUT:
                     89:                        break;
                     90:                case ANSH_FLG_WINZ:
                     91:                        b = (u_short*) buf;
                     92:                        ioChgWinPTY(proc->proc_pty, ntohs(b[0]), ntohs(b[1]), ntohs(b[2]), ntohs(b[3]));
                     93:                        /* if not started login, lets start & go! */
                     94:                        if (!proc->proc_pid) {
                     95:                                memcpy(&proc->proc_cli, &sa, sizeof sa);
                     96:                                spawnLogin(task, proc);
                     97:                        }
                     98:                default:
                     99:                        goto end;
                    100:        }
                    101: 
                    102:        proc->proc_flg = ret;
                    103:        proc->proc_rlen_[NET2FD] = rlen;
                    104:        memset(proc->proc_buf_[NET2FD], 0, proc->proc_blen);
                    105:        memcpy(proc->proc_buf_[NET2FD], buf, proc->proc_rlen_[NET2FD]);
                    106:        schedWrite(TASK_ROOT(task), fdTx, proc, proc->proc_pty);
                    107: end:
                    108:        free(buf);
                    109:        schedRead(TASK_ROOT(task), icmpRx, NULL, proc ? proc->proc_sock : TASK_FD(task));
                    110:        return NULL;
                    111: }
                    112: 
                    113: void *
                    114: fdTx(sched_task_t *task)
                    115: {
                    116:        struct tagProc *proc;
                    117:        struct timeval tv = { 0 };
                    118:        int wlen;
                    119: 
                    120:        FTRACE(3);
                    121: 
                    122:        /* not found argument, drop data */
                    123:        if (!(proc = TASK_ARG(task)))
                    124:                return (void*) -1;
                    125: 
                    126:        /* if != ANSH_FLG_CPOUT isnt received from client */
                    127:        if (proc->proc_flg != ANSH_FLG_CPOUT || !proc->proc_pid)
                    128:                return NULL;
                    129: 
                    130:        if (waitpid(proc->proc_pid, &wlen, WNOHANG)) {
                    131:                ioFreePTY(TASK_FD(task), proc->proc_ttyname);
                    132:                schedCancelby(TASK_ROOT(task), NULL, CRITERIA_FD, (void*) TASK_FD(task), NULL);
                    133: 
                    134:                proc->proc_pid = 0;
                    135:                proc->proc_flg = ANSH_FLG_EOF;
                    136:                proc->proc_rlen_[FD2NET] = 0;
                    137: 
                    138:                schedWrite(TASK_ROOT(task), icmpTx, proc, proc->proc_sock);
                    139:                return NULL;
                    140:        }
                    141: 
                    142:        /* if Timeout defined, disarm timer */
                    143:        if (Timeout)
                    144:                schedCancelby(TASK_ROOT(task), &TASK_ROOT(task)->root_timer, CRITERIA_CALL, TOfunc, NULL);
                    145: 
                    146:        wlen = write(TASK_FD(task), proc->proc_buf_[NET2FD], proc->proc_rlen_[NET2FD]);
                    147:        switch (wlen) {
                    148:                case -1:
                    149:                        ERR("write2tty #%d - %s", errno, strerror(errno));
                    150:                        /* exit from shell and release tty */
                    151:                        waitpid(proc->proc_pid, &wlen, 0);
                    152:                        ioFreePTY(TASK_FD(task), proc->proc_ttyname);
                    153:                        schedCancelby(TASK_ROOT(task), NULL, CRITERIA_FD, (void*) TASK_FD(task), NULL);
                    154: 
                    155:                        proc->proc_pid = 0;
                    156:                        proc->proc_flg = ANSH_FLG_EOF;
                    157:                        proc->proc_rlen_[FD2NET] = 0;
                    158: 
                    159:                        schedWrite(TASK_ROOT(task), icmpTx, proc, proc->proc_sock);
                    160:                        return NULL;
                    161:                default:
                    162:                        proc->proc_flg = ANSH_FLG_OK;
                    163:                        proc->proc_rlen_[NET2FD] = 0;
                    164:        }
                    165:        VERB(3) LOG("Writed %d bytes - %s", wlen, proc->proc_buf_[NET2FD]);
                    166: 
                    167:        /* if Timeout defined, go arm timer */
                    168:        if (Timeout) {
                    169:                tv.tv_sec = Timeout;
                    170:                schedTimer(TASK_ROOT(task), TOfunc, proc, tv);
                    171:        }
                    172:        return NULL;
                    173: }
                    174: 
                    175: void *
                    176: fdRx(sched_task_t *task)
                    177: {
                    178:        struct tagProc *proc;
                    179:        struct timeval tv = { 0 };
                    180:        int rlen;
                    181: 
                    182:        FTRACE(3);
                    183: 
                    184:        /* not found argument, drop data */
                    185:        if (!(proc = TASK_ARG(task)))
                    186:                return (void*) -1;
                    187:        if (!proc->proc_pid)
                    188:                return NULL;
                    189: 
                    190:        if (waitpid(proc->proc_pid, &rlen, WNOHANG)) {
                    191:                ioFreePTY(TASK_FD(task), proc->proc_ttyname);
                    192:                schedCancelby(TASK_ROOT(task), NULL, CRITERIA_FD, (void*) TASK_FD(task), NULL);
                    193: 
                    194:                proc->proc_pid = 0;
                    195:                proc->proc_flg = ANSH_FLG_EOF;
                    196:                proc->proc_rlen_[FD2NET] = 0;
                    197: 
                    198:                schedWrite(TASK_ROOT(task), icmpTx, proc, proc->proc_sock);
                    199:                return NULL;
                    200:        }
                    201: 
                    202:        /* if Timeout defined, disarm timer */
                    203:        if (Timeout)
                    204:                schedCancelby(TASK_ROOT(task), &TASK_ROOT(task)->root_timer, CRITERIA_CALL, TOfunc, NULL);
                    205: 
                    206:        memset(proc->proc_buf_[FD2NET], 0, proc->proc_blen);
                    207:        rlen = read(TASK_FD(task), proc->proc_buf_[FD2NET], proc->proc_blen);
                    208:        switch (rlen) {
                    209:                case -1:
                    210:                        ERR("readtty #%d - %s", errno, strerror(errno));
                    211:                case 0:
                    212:                        /* exit from shell and release tty */
                    213:                        waitpid(proc->proc_pid, &rlen, 0);
                    214:                        ioFreePTY(TASK_FD(task), proc->proc_ttyname);
                    215:                        schedCancelby(TASK_ROOT(task), NULL, CRITERIA_FD, (void*) TASK_FD(task), NULL);
                    216:                        VERB(3) LOG("EOF process status %d", rlen);
                    217: 
                    218:                        proc->proc_pid = 0;
                    219:                        proc->proc_flg = ANSH_FLG_EOF;
                    220:                        proc->proc_rlen_[FD2NET] = 0;
                    221: 
                    222:                        schedWrite(TASK_ROOT(task), icmpTx, proc, proc->proc_sock);
                    223:                        return NULL;
                    224:                default:
                    225:                        proc->proc_flg = ANSH_FLG_OK;
                    226:                        proc->proc_rlen_[FD2NET] = rlen;
                    227:        }
                    228:        VERB(3) LOG("Readed %d bytes - %s", rlen, proc->proc_buf_[FD2NET]);
                    229: 
                    230:        schedWrite(TASK_ROOT(task), icmpTx, proc, proc->proc_sock);
                    231:        schedRead(TASK_ROOT(task), fdRx, proc, proc->proc_pty);
                    232: 
                    233:        /* if Timeout defined, go arm timer */
                    234:        if (Timeout) {
                    235:                tv.tv_sec = Timeout;
                    236:                schedTimer(TASK_ROOT(task), TOfunc, proc, tv);
                    237:        }
                    238:        return NULL;
                    239: }
                    240: 
                    241: int
                    242: spawnLogin(sched_task_t *task, struct tagProc *proc)
                    243: {
                    244:        int flg;
                    245:        struct timeval tv = { 0 };
                    246:        char str[STRSIZ] = { 0 };
                    247:        struct sockaddr_in *sin;
                    248:        struct sockaddr_in6 *sin6;
                    249: 
                    250:        FTRACE(3);
                    251: 
                    252:        assert(proc);
                    253: 
                    254:        switch ((proc->proc_pid = ioForkPTY(&proc->proc_pty, proc->proc_ttyname, 
                    255:                                        sizeof proc->proc_ttyname, NULL, NULL, NULL))) {
                    256:                case -1:
                    257:                        ERR("ioForkPTY() #%d - %s", io_GetErrno(), io_GetError());
                    258:                        return -1;
                    259:                case 0:
                    260:                        printf("ansh3d ELWIX remote management system over ICMP (%s)\n\n", 
                    261:                                        proc->proc_ttyname);
                    262:                        strlcpy(str, "-hansh3@", sizeof str);
                    263:                        if (proc->proc_cli.sa_family == AF_INET) {
                    264:                                sin = (struct sockaddr_in*) &proc->proc_cli;
                    265:                                inet_ntop(AF_INET, &sin->sin_addr, str + 8, INET_ADDRSTRLEN);
                    266:                        } else if (proc->proc_cli.sa_family == AF_INET) {
                    267:                                sin6 = (struct sockaddr_in6*) &proc->proc_cli;
                    268:                                inet_ntop(AF_INET6, &sin6->sin6_addr, str + 8, INET6_ADDRSTRLEN);
                    269:                        }
                    270:                        execl("/usr/bin/login", "login", str, NULL);
                    271:                        /* never reached */
                    272:                        return -1;
                    273:                default:
                    274:                        flg = fcntl(proc->proc_pty, F_GETFL);
                    275:                        fcntl(proc->proc_pty, F_SETFL, flg | O_NONBLOCK);
                    276: 
                    277:                        VERB(3) LOG("Parent know child pid %d", proc->proc_pid);
                    278:                        schedRead(TASK_ROOT(task), fdRx, proc, proc->proc_pty);
                    279: 
                    280:                        /* if Timeout defined, go arm timer */
                    281:                        if (Timeout) {
                    282:                                tv.tv_sec = Timeout;
                    283:                                schedTimer(TASK_ROOT(task), TOfunc, proc, tv);
                    284:                        }
                    285:                        break;
                    286:        }
                    287: 
                    288:        return 0;
                    289: }

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