1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* Amanda, The Advanced Maryland Automatic Network Disk Archiver
* Copyright (c) 2016-2016 Carbonite, Inc. All Rights Reserved.
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of U.M. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. U.M. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: the Amanda Development Team. Its members are listed in a
* file named AUTHORS, in the root directory of this distribution.
*/
#include <config.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "sockaddr-util.h"
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "security-file.h"
int
main(
int argc,
char ** argv)
{
int s;
int r;
int rc;
struct msghdr msg;
struct msghdr msg_socket;
struct msghdr msg_ambind_data;
struct cmsghdr *cmsg;
char cmsg_socket[CMSG_SPACE(sizeof(s))];
char cmsgbuf[CMSG_SPACE(sizeof(s))];
ambind_t ambind;
struct iovec iov[2];
int sockfd;
if (argc < 2) {
}
sockfd = atoi(argv[1]);
do {
struct timeval timeout = { 5, 0 };
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(sockfd, &readSet);
rc = select(sockfd+1, &readSet, NULL, NULL, &timeout);
} while (rc < 0 && errno == EINTR);
if (rc <= 0) {
fprintf(stderr, "ambind: select failed: %s\n", strerror(errno));
shutdown(sockfd, SHUT_RDWR);
return -1;
}
// read the message socket msg
memset(&msg_socket, 0, sizeof(msg_socket));
msg_socket.msg_control = cmsg_socket;
msg_socket.msg_controllen = sizeof(cmsg_socket);
rc = recvmsg(sockfd, &msg_socket, 0);
if (rc == -1) {
fprintf(stderr, "ambind: first recvmsg failed: %s\n", strerror(errno));
exit(1);
}
cmsg = CMSG_FIRSTHDR(&msg_socket);
if (cmsg == NULL || cmsg -> cmsg_type != SCM_RIGHTS) {
fprintf(stderr, "ambind: The first control structure contains no file descriptor.\n");
exit(2);
}
memcpy(&s, CMSG_DATA(cmsg), sizeof(s));
do {
struct timeval timeout = { 5, 0 };
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(sockfd, &readSet);
rc = select(sockfd+1, &readSet, NULL, NULL, &timeout);
} while (rc < 0 && errno == EINTR);
if (rc <= 0) {
fprintf(stderr, "ambind: select failed: %s\n", strerror(errno));
shutdown(sockfd, SHUT_RDWR);
return -1;
}
// read the ambind msg
memset(&msg_ambind_data, 0, sizeof(msg_ambind_data));
iov[0].iov_base = &ambind;
iov[0].iov_len = sizeof(ambind_t);
iov[1].iov_base = NULL;
iov[1].iov_len = 0;
msg_ambind_data.msg_iov = iov;
msg_ambind_data.msg_iovlen = 1;
rc = recvmsg(sockfd, &msg_ambind_data, 0);
if (rc == -1) {
fprintf(stderr, "ambind: second recvmsg failed: %s\n", strerror(errno));
shutdown(sockfd, SHUT_RDWR);
exit(2);
}
if (rc != sizeof(ambind_t)) {
fprintf(stderr, "ambind: recvmsg failed: size == %d\n", rc);
shutdown(sockfd, SHUT_RDWR);
exit(2);
}
if (!security_allow_bind(s, &ambind.addr)) {
shutdown(sockfd, SHUT_RDWR);
exit(2);
}
r = bind(s, (struct sockaddr *)&ambind.addr, ambind.socklen);
if (r != 0) {
if (errno != EADDRINUSE) {
fprintf(stderr, "ERROR %s: ambind: bind failed A: %s\n", get_errno_string(errno), strerror(errno));
shutdown(sockfd, SHUT_RDWR);
exit(2);
}
fprintf(stderr, "WARNING %s: ambind: bind failed B: %s\n", get_errno_string(errno), strerror(errno));
if (shutdown(sockfd, SHUT_RDWR) < 0) {
fprintf(stderr, "ambind: shutdown failed B: %s\n", strerror(errno));
exit(1);
}
exit(1);
}
memset(&msg,0, sizeof(msg));
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(s));
memcpy(CMSG_DATA(cmsg), &s, sizeof(s));
msg.msg_controllen = cmsg->cmsg_len;
// send the socket
if ((sendmsg(sockfd, &msg, 0)) < 0) {
fprintf(stderr, "WARNING %s: ambind: sendmsg failed: %s\n", get_errno_string(errno), strerror(errno));
exit(1);
}
if (shutdown(sockfd, SHUT_RDWR) < 0) {
fprintf(stderr, "WARNING %s: ambind: shutdown failed C: %s\n", get_errno_string(errno), strerror(errno));
exit(1);
}
exit(0);
}
|