monitor_fdpass.c (181110) | monitor_fdpass.c (181111) |
---|---|
1/* $OpenBSD: monitor_fdpass.c,v 1.12 2006/08/03 03:34:42 deraadt Exp $ */ | 1/* $OpenBSD: monitor_fdpass.c,v 1.17 2008/03/24 16:11:07 deraadt Exp $ */ |
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 --- 25 unchanged lines hidden (view full) --- 35 36#include <errno.h> 37#include <string.h> 38#include <stdarg.h> 39 40#include "log.h" 41#include "monitor_fdpass.h" 42 | 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 --- 25 unchanged lines hidden (view full) --- 35 36#include <errno.h> 37#include <string.h> 38#include <stdarg.h> 39 40#include "log.h" 41#include "monitor_fdpass.h" 42 |
43void | 43int |
44mm_send_fd(int sock, int fd) 45{ 46#if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) 47 struct msghdr msg; 48 struct iovec vec; 49 char ch = '\0'; 50 ssize_t n; 51#ifndef HAVE_ACCRIGHTS_IN_MSGHDR 52 union { 53 struct cmsghdr hdr; | 44mm_send_fd(int sock, int fd) 45{ 46#if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) 47 struct msghdr msg; 48 struct iovec vec; 49 char ch = '\0'; 50 ssize_t n; 51#ifndef HAVE_ACCRIGHTS_IN_MSGHDR 52 union { 53 struct cmsghdr hdr; |
54 char tmp[CMSG_SPACE(sizeof(int))]; |
|
54 char buf[CMSG_SPACE(sizeof(int))]; 55 } cmsgbuf; 56 struct cmsghdr *cmsg; 57#endif 58 59 memset(&msg, 0, sizeof(msg)); 60#ifdef HAVE_ACCRIGHTS_IN_MSGHDR 61 msg.msg_accrights = (caddr_t)&fd; --- 8 unchanged lines hidden (view full) --- 70 *(int *)CMSG_DATA(cmsg) = fd; 71#endif 72 73 vec.iov_base = &ch; 74 vec.iov_len = 1; 75 msg.msg_iov = &vec; 76 msg.msg_iovlen = 1; 77 | 55 char buf[CMSG_SPACE(sizeof(int))]; 56 } cmsgbuf; 57 struct cmsghdr *cmsg; 58#endif 59 60 memset(&msg, 0, sizeof(msg)); 61#ifdef HAVE_ACCRIGHTS_IN_MSGHDR 62 msg.msg_accrights = (caddr_t)&fd; --- 8 unchanged lines hidden (view full) --- 71 *(int *)CMSG_DATA(cmsg) = fd; 72#endif 73 74 vec.iov_base = &ch; 75 vec.iov_len = 1; 76 msg.msg_iov = &vec; 77 msg.msg_iovlen = 1; 78 |
78 if ((n = sendmsg(sock, &msg, 0)) == -1) 79 fatal("%s: sendmsg(%d): %s", __func__, fd, | 79 if ((n = sendmsg(sock, &msg, 0)) == -1) { 80 error("%s: sendmsg(%d): %s", __func__, fd, |
80 strerror(errno)); | 81 strerror(errno)); |
81 if (n != 1) 82 fatal("%s: sendmsg: expected sent 1 got %ld", | 82 return -1; 83 } 84 85 if (n != 1) { 86 error("%s: sendmsg: expected sent 1 got %ld", |
83 __func__, (long)n); | 87 __func__, (long)n); |
88 return -1; 89 } 90 return 0; |
|
84#else | 91#else |
85 fatal("%s: UsePrivilegeSeparation=yes not supported", 86 __func__); | 92 error("%s: file descriptor passing not supported", __func__); 93 return -1; |
87#endif 88} 89 90int 91mm_receive_fd(int sock) 92{ 93#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) 94 struct msghdr msg; --- 17 unchanged lines hidden (view full) --- 112#ifdef HAVE_ACCRIGHTS_IN_MSGHDR 113 msg.msg_accrights = (caddr_t)&fd; 114 msg.msg_accrightslen = sizeof(fd); 115#else 116 msg.msg_control = &cmsgbuf.buf; 117 msg.msg_controllen = sizeof(cmsgbuf.buf); 118#endif 119 | 94#endif 95} 96 97int 98mm_receive_fd(int sock) 99{ 100#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) 101 struct msghdr msg; --- 17 unchanged lines hidden (view full) --- 119#ifdef HAVE_ACCRIGHTS_IN_MSGHDR 120 msg.msg_accrights = (caddr_t)&fd; 121 msg.msg_accrightslen = sizeof(fd); 122#else 123 msg.msg_control = &cmsgbuf.buf; 124 msg.msg_controllen = sizeof(cmsgbuf.buf); 125#endif 126 |
120 if ((n = recvmsg(sock, &msg, 0)) == -1) 121 fatal("%s: recvmsg: %s", __func__, strerror(errno)); 122 if (n != 1) 123 fatal("%s: recvmsg: expected received 1 got %ld", | 127 if ((n = recvmsg(sock, &msg, 0)) == -1) { 128 error("%s: recvmsg: %s", __func__, strerror(errno)); 129 return -1; 130 } 131 if (n != 1) { 132 error("%s: recvmsg: expected received 1 got %ld", |
124 __func__, (long)n); | 133 __func__, (long)n); |
134 return -1; 135 } |
|
125 126#ifdef HAVE_ACCRIGHTS_IN_MSGHDR | 136 137#ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
127 if (msg.msg_accrightslen != sizeof(fd)) 128 fatal("%s: no fd", __func__); | 138 if (msg.msg_accrightslen != sizeof(fd)) { 139 error("%s: no fd", __func__); 140 return -1; 141 } |
129#else 130 cmsg = CMSG_FIRSTHDR(&msg); | 142#else 143 cmsg = CMSG_FIRSTHDR(&msg); |
131 if (cmsg == NULL) 132 fatal("%s: no message header", __func__); | 144 if (cmsg == NULL) { 145 error("%s: no message header", __func__); 146 return -1; 147 } |
133#ifndef BROKEN_CMSG_TYPE | 148#ifndef BROKEN_CMSG_TYPE |
134 if (cmsg->cmsg_type != SCM_RIGHTS) 135 fatal("%s: expected type %d got %d", __func__, | 149 if (cmsg->cmsg_type != SCM_RIGHTS) { 150 error("%s: expected type %d got %d", __func__, |
136 SCM_RIGHTS, cmsg->cmsg_type); | 151 SCM_RIGHTS, cmsg->cmsg_type); |
152 return -1; 153 } |
|
137#endif 138 fd = (*(int *)CMSG_DATA(cmsg)); 139#endif 140 return fd; 141#else | 154#endif 155 fd = (*(int *)CMSG_DATA(cmsg)); 156#endif 157 return fd; 158#else |
142 fatal("%s: UsePrivilegeSeparation=yes not supported", 143 __func__); | 159 error("%s: file descriptor passing not supported", __func__); 160 return -1; |
144#endif 145} | 161#endif 162} |