X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/d73cbfe35a502847bab5d5f43500d0b877005d57..ce27af321f94d71fc810380c06f42a2c5943f6b3:/monitor_fdpass.c diff --git a/monitor_fdpass.c b/monitor_fdpass.c index fb97171f..4b9a066b 100644 --- a/monitor_fdpass.c +++ b/monitor_fdpass.c @@ -1,3 +1,4 @@ +/* $OpenBSD: monitor_fdpass.c,v 1.18 2008/11/30 11:59:26 dtucker Exp $ */ /* * Copyright 2001 Niels Provos * All rights reserved. @@ -24,34 +25,44 @@ */ #include "includes.h" -RCSID("$OpenBSD: monitor_fdpass.c,v 1.2 2002/03/24 17:53:16 stevesk Exp $"); +#include +#include #include +#ifdef HAVE_SYS_UN_H +#include +#endif + +#include +#include +#include #include "log.h" #include "monitor_fdpass.h" -void -mm_send_fd(int socket, int fd) +int +mm_send_fd(int sock, int fd) { #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) struct msghdr msg; - struct iovec vec; - char ch = '\0'; - int n; -#if !defined(HAVE_ACCRIGHTS_IN_MSGHDR) || \ - (defined(HAVE_ACCRIGHTS_IN_MSGHDR) && defined(HAVE_CONTROL_IN_MSGHDR)) - char tmp[CMSG_SPACE(sizeof(int))]; +#ifndef HAVE_ACCRIGHTS_IN_MSGHDR + union { + struct cmsghdr hdr; + char buf[CMSG_SPACE(sizeof(int))]; + } cmsgbuf; struct cmsghdr *cmsg; #endif + struct iovec vec; + char ch = '\0'; + ssize_t n; memset(&msg, 0, sizeof(msg)); -#if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR) +#ifdef HAVE_ACCRIGHTS_IN_MSGHDR msg.msg_accrights = (caddr_t)&fd; msg.msg_accrightslen = sizeof(fd); #else - msg.msg_control = (caddr_t)tmp; - msg.msg_controllen = CMSG_LEN(sizeof(int)); + msg.msg_control = (caddr_t)&cmsgbuf.buf; + msg.msg_controllen = sizeof(cmsgbuf.buf); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; @@ -64,64 +75,95 @@ mm_send_fd(int socket, int fd) msg.msg_iov = &vec; msg.msg_iovlen = 1; - if ((n = sendmsg(socket, &msg, 0)) == -1) - fatal("%s: sendmsg(%d): %s", __FUNCTION__, fd, + while ((n = sendmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN || + errno == EINTR)) + debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno)); + if (n == -1) { + error("%s: sendmsg(%d): %s", __func__, fd, strerror(errno)); - if (n != 1) - fatal("%s: sendmsg: expected sent 1 got %d", - __FUNCTION__, n); + return -1; + } + + if (n != 1) { + error("%s: sendmsg: expected sent 1 got %ld", + __func__, (long)n); + return -1; + } + return 0; #else - fatal("%s: UsePrivilegeSeparation=yes not supported", - __FUNCTION__); + error("%s: file descriptor passing not supported", __func__); + return -1; #endif } int -mm_receive_fd(int socket) +mm_receive_fd(int sock) { #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) struct msghdr msg; - struct iovec vec; - char ch; - int fd, n; -#if !defined(HAVE_ACCRIGHTS_IN_MSGHDR) || \ - (defined(HAVE_ACCRIGHTS_IN_MSGHDR) && defined(HAVE_CONTROL_IN_MSGHDR)) - char tmp[CMSG_SPACE(sizeof(int))]; +#ifndef HAVE_ACCRIGHTS_IN_MSGHDR + union { + struct cmsghdr hdr; + char buf[CMSG_SPACE(sizeof(int))]; + } cmsgbuf; struct cmsghdr *cmsg; #endif + struct iovec vec; + ssize_t n; + char ch; + int fd; memset(&msg, 0, sizeof(msg)); vec.iov_base = &ch; vec.iov_len = 1; msg.msg_iov = &vec; msg.msg_iovlen = 1; -#if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR) +#ifdef HAVE_ACCRIGHTS_IN_MSGHDR msg.msg_accrights = (caddr_t)&fd; msg.msg_accrightslen = sizeof(fd); #else - msg.msg_control = tmp; - msg.msg_controllen = sizeof(tmp); + msg.msg_control = &cmsgbuf.buf; + msg.msg_controllen = sizeof(cmsgbuf.buf); #endif - if ((n = recvmsg(socket, &msg, 0)) == -1) - fatal("%s: recvmsg: %s", __FUNCTION__, strerror(errno)); - if (n != 1) - fatal("%s: recvmsg: expected received 1 got %d", - __FUNCTION__, n); + while ((n = recvmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN || + errno == EINTR)) + debug3("%s: recvmsg: %s", __func__, strerror(errno)); + if (n == -1) { + error("%s: recvmsg: %s", __func__, strerror(errno)); + return -1; + } -#if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR) - if (msg.msg_accrightslen != sizeof(fd)) - fatal("%s: no fd", __FUNCTION__); + if (n != 1) { + error("%s: recvmsg: expected received 1 got %ld", + __func__, (long)n); + return -1; + } + +#ifdef HAVE_ACCRIGHTS_IN_MSGHDR + if (msg.msg_accrightslen != sizeof(fd)) { + error("%s: no fd", __func__); + return -1; + } #else cmsg = CMSG_FIRSTHDR(&msg); - if (cmsg->cmsg_type != SCM_RIGHTS) - fatal("%s: expected type %d got %d", __FUNCTION__, + if (cmsg == NULL) { + error("%s: no message header", __func__); + return -1; + } + +#ifndef BROKEN_CMSG_TYPE + if (cmsg->cmsg_type != SCM_RIGHTS) { + error("%s: expected type %d got %d", __func__, SCM_RIGHTS, cmsg->cmsg_type); + return -1; + } +#endif fd = (*(int *)CMSG_DATA(cmsg)); #endif return fd; #else - fatal("%s: UsePrivilegeSeparation=yes not supported", - __FUNCTION__); + error("%s: file descriptor passing not supported", __func__); + return -1; #endif }