[BACK]Return to monitor_fdpass.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor_fdpass.c, Revision 1.22

1.22    ! djm         1: /* $OpenBSD: monitor_fdpass.c,v 1.21 2016/02/29 20:22:36 jca Exp $ */
1.1       provos      2: /*
                      3:  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.9       stevesk    27: #include <sys/types.h>
                     28: #include <sys/socket.h>
1.1       provos     29: #include <sys/uio.h>
1.10      stevesk    30:
                     31: #include <errno.h>
1.19      djm        32: #include <poll.h>
1.11      stevesk    33: #include <string.h>
1.12      deraadt    34: #include <stdarg.h>
1.1       provos     35:
                     36: #include "log.h"
                     37: #include "monitor_fdpass.h"
                     38:
1.13      djm        39: int
1.5       avsm       40: mm_send_fd(int sock, int fd)
1.1       provos     41: {
                     42:        struct msghdr msg;
1.14      deraadt    43:        union {
                     44:                struct cmsghdr hdr;
1.15      deraadt    45:                char buf[CMSG_SPACE(sizeof(int))];
                     46:        } cmsgbuf;
1.1       provos     47:        struct cmsghdr *cmsg;
                     48:        struct iovec vec;
1.2       stevesk    49:        char ch = '\0';
1.4       deraadt    50:        ssize_t n;
1.19      djm        51:        struct pollfd pfd;
1.1       provos     52:
                     53:        memset(&msg, 0, sizeof(msg));
1.20      djm        54:        memset(&cmsgbuf, 0, sizeof(cmsgbuf));
1.15      deraadt    55:        msg.msg_control = (caddr_t)&cmsgbuf.buf;
1.17      deraadt    56:        msg.msg_controllen = sizeof(cmsgbuf.buf);
1.1       provos     57:        cmsg = CMSG_FIRSTHDR(&msg);
                     58:        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
                     59:        cmsg->cmsg_level = SOL_SOCKET;
                     60:        cmsg->cmsg_type = SCM_RIGHTS;
                     61:        *(int *)CMSG_DATA(cmsg) = fd;
                     62:
                     63:        vec.iov_base = &ch;
                     64:        vec.iov_len = 1;
                     65:        msg.msg_iov = &vec;
                     66:        msg.msg_iovlen = 1;
                     67:
1.19      djm        68:        pfd.fd = sock;
                     69:        pfd.events = POLLOUT;
                     70:        while ((n = sendmsg(sock, &msg, 0)) == -1 &&
                     71:            (errno == EAGAIN || errno == EINTR)) {
1.22    ! djm        72:                debug3_f("sendmsg(%d): %s", fd, strerror(errno));
1.19      djm        73:                (void)poll(&pfd, 1, -1);
                     74:        }
1.18      dtucker    75:        if (n == -1) {
1.22    ! djm        76:                error_f("sendmsg(%d): %s", fd, strerror(errno));
1.13      djm        77:                return -1;
                     78:        }
                     79:
                     80:        if (n != 1) {
1.22    ! djm        81:                error_f("sendmsg: expected sent 1 got %zd", n);
1.13      djm        82:                return -1;
                     83:        }
                     84:        return 0;
1.1       provos     85: }
                     86:
                     87: int
1.5       avsm       88: mm_receive_fd(int sock)
1.1       provos     89: {
                     90:        struct msghdr msg;
1.14      deraadt    91:        union {
                     92:                struct cmsghdr hdr;
1.15      deraadt    93:                char buf[CMSG_SPACE(sizeof(int))];
                     94:        } cmsgbuf;
1.1       provos     95:        struct cmsghdr *cmsg;
                     96:        struct iovec vec;
1.4       deraadt    97:        ssize_t n;
1.1       provos     98:        char ch;
1.4       deraadt    99:        int fd;
1.19      djm       100:        struct pollfd pfd;
1.1       provos    101:
                    102:        memset(&msg, 0, sizeof(msg));
1.20      djm       103:        memset(&cmsgbuf, 0, sizeof(cmsgbuf));
1.1       provos    104:        vec.iov_base = &ch;
                    105:        vec.iov_len = 1;
                    106:        msg.msg_iov = &vec;
                    107:        msg.msg_iovlen = 1;
1.15      deraadt   108:        msg.msg_control = &cmsgbuf.buf;
1.17      deraadt   109:        msg.msg_controllen = sizeof(cmsgbuf.buf);
1.1       provos    110:
1.19      djm       111:        pfd.fd = sock;
                    112:        pfd.events = POLLIN;
                    113:        while ((n = recvmsg(sock, &msg, 0)) == -1 &&
                    114:            (errno == EAGAIN || errno == EINTR)) {
1.22    ! djm       115:                debug3_f("recvmsg: %s", strerror(errno));
1.19      djm       116:                (void)poll(&pfd, 1, -1);
                    117:        }
1.18      dtucker   118:        if (n == -1) {
1.22    ! djm       119:                error_f("recvmsg: %s", strerror(errno));
1.13      djm       120:                return -1;
                    121:        }
                    122:
                    123:        if (n != 1) {
1.22    ! djm       124:                error_f("recvmsg: expected received 1 got %zd", n);
1.13      djm       125:                return -1;
                    126:        }
1.1       provos    127:
                    128:        cmsg = CMSG_FIRSTHDR(&msg);
1.13      djm       129:        if (cmsg == NULL) {
1.22    ! djm       130:                error_f("no message header");
1.13      djm       131:                return -1;
                    132:        }
                    133:
                    134:        if (cmsg->cmsg_type != SCM_RIGHTS) {
1.22    ! djm       135:                error_f("expected %d got %d", SCM_RIGHTS, cmsg->cmsg_type);
1.13      djm       136:                return -1;
                    137:        }
1.2       stevesk   138:        fd = (*(int *)CMSG_DATA(cmsg));
                    139:        return fd;
1.1       provos    140: }