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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* rpcmisc Miscellaneous functions for RPC startup and shutdown.
* This code is partially snarfed from rpcgen -s tcp -s udp,
* partly written by Mark Shand, Donald Becker, and Rick
* Sladkey. It was tweaked slightly by Olaf Kirch to be
* useable by both nfsd and mountd.
*
* This software may be used for any purpose provided
* the above copyright notice is retained. It is supplied
* as is, with no warranty expressed or implied.
*
* Auth daemon code by Olaf Kirch.
*/
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "rpcmisc.h"
#include "logging.h"
/* Another undefined function in RPC */
extern SVCXPRT *svcfd_create(int sock, u_int ssize, u_int rsize);
static int makesock(int port, int proto, int socksz);
#define _RPCSVC_CLOSEDOWN 120
time_t closedown = 0;
int _rpcpmstart = 0;
int _rpcfdtype = 0;
int _rpcsvcdirty = 0;
const char * auth_daemon = 0;
#ifdef AUTH_DAEMON
static bool_t (*tcp_rendevouser)(SVCXPRT *, struct rpc_msg *);
static bool_t (*tcp_receiver)(SVCXPRT *, struct rpc_msg *);
static bool_t auth_rendevouser(SVCXPRT *, struct rpc_msg *);
static bool_t auth_receiver(SVCXPRT *, struct rpc_msg *);
static void auth_handler(int sock);
#endif
void
rpc_init(char *name, int prog, int *verstbl, void (*dispatch)(),
int defport, int bufsiz)
{
struct sockaddr_in saddr;
SVCXPRT *transp;
int sock, i, vers;
int asize;
/* When started from inetd, initialize only once */
if (_rpcpmstart)
return;
asize = sizeof(saddr);
sock = 0;
if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
int ssize = sizeof (int);
if (saddr.sin_family != AF_INET)
goto not_inetd;
if (getsockopt(0, SOL_SOCKET, SO_TYPE, &_rpcfdtype, &ssize) < 0)
goto not_inetd;
background_logging(); /* no more logging to stderr */
closedown = time(NULL) + _RPCSVC_CLOSEDOWN;
_rpcpmstart = 1;
} else {
not_inetd:
_rpcfdtype = 0;
for (i = 0; (vers = verstbl[i]) != 0; i++)
pmap_unset(prog, vers);
sock = RPC_ANYSOCK;
}
if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
if (_rpcfdtype == 0 && defport != 0 &&
((sock = makesock(defport, IPPROTO_UDP, bufsiz)) < 0)) {
Dprintf(L_FATAL, "could not make a udp socket\n");
}
transp = svcudp_create(sock);
if (transp == NULL) {
Dprintf(L_FATAL, "cannot create udp service.");
}
for (i = 0; (vers = verstbl[i]) != 0; i++) {
if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
Dprintf(L_FATAL,
"unable to register (%s, %d, udp).",
name, vers);
}
}
}
if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
if (_rpcfdtype == 0 && defport != 0 &&
((sock = makesock(defport, IPPROTO_TCP, bufsiz)) < 0)) {
Dprintf(L_FATAL, "could not make a tcp socket\n");
}
transp = svctcp_create(sock, 0, 0);
if (transp == NULL) {
Dprintf(L_FATAL, "cannot create tcp service.");
}
#ifdef AUTH_DAEMON
tcp_rendevouser = transp->xp_ops->xp_recv;
transp->xp_ops->xp_recv = auth_rendevouser;
#endif
for (i = 0; (vers = verstbl[i]) != 0; i++) {
if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
Dprintf(L_FATAL,
"unable to register (%s, %d, tcp).",
name, vers);
}
}
}
/* We ignore SIGPIPE. SIGPIPE is being sent to a daemon when trying
* to do a sendmsg() on a TCP socket whose peer has disconnected.
*/
if (!_rpcpmstart) {
struct sigaction pipeact;
memset(&pipeact, 0, sizeof(pipeact));
pipeact.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &pipeact, NULL);
}
}
void
rpc_exit(int prog, int *verstbl)
{
int i, vers;
if (_rpcpmstart)
return;
for (i = 0; (vers = verstbl[i]) != 0; i++)
pmap_unset(prog, vers);
}
void
rpc_closedown(void)
{
struct sockaddr_in sin;
static int size = 0;
time_t now = time(NULL);
int i, len;
if (!_rpcpmstart || now < closedown)
return;
if (_rpcsvcdirty == 0) {
if (_rpcfdtype == SOCK_DGRAM)
exit(0);
/* Okay, this is a TCP socket. Check whether we're still
* connected */
if (size == 0) {
size = getdtablesize();
}
for (i = 0; i < size; i++) {
if (!FD_ISSET(i, &svc_fdset))
continue;
len = sizeof(sin);
if (getpeername(i, (struct sockaddr *) &sin, &len) >= 0)
exit(0);
}
}
closedown = now + _RPCSVC_CLOSEDOWN;
}
static int
makesock(int port, int proto, int socksz)
{
struct sockaddr_in sin;
int s;
int sock_type;
sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
s = socket(AF_INET, sock_type, proto);
if (s < 0) {
Dprintf(L_ERROR, "Could not make a socket: %s\n",
strerror(errno));
return (-1);
}
memset((char *) &sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
#ifdef DEBUG
{
int val = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
Dprintf(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
}
#endif
#ifdef SO_SNDBUF
if (socksz != 0) {
int sblen, rblen;
/* 1024 for rpc & transport overheads */
sblen = rblen = 8 * (socksz + 1024);
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
Dprintf(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
}
#endif /* SO_SNDBUF */
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
Dprintf(L_ERROR, "Could not bind name to socket %s:%d: %s\n",
inet_ntoa(sin.sin_addr),
ntohs(sin.sin_port),
strerror(errno));
return (-1);
}
return (s);
}
#ifdef AUTH_DAEMON
static bool_t
auth_rendevouser(SVCXPRT *xprt, struct rpc_msg *rpcmsg)
{
struct sockaddr_in sin;
int sock, slen = sizeof(sin);
do {
sock = accept(xprt->xp_sock, (struct sockaddr *) &sin, &slen);
} while (sock < 0 && errno == EINTR);
if (sock >= 0) {
xprt = svcfd_create(sock, 0, 0);
xprt->xp_raddr = sin;
xprt->xp_addrlen = slen;
/* Swap the receive handler */
if (auth_daemon) {
tcp_receiver = xprt->xp_ops->xp_recv;
xprt->xp_ops->xp_recv = auth_receiver;
}
}
/* Always return false -- there's never an rpcmsg to be processed */
return 0;
}
static bool_t
auth_receiver(SVCXPRT *xprt, struct rpc_msg *rpcmsg)
{
__u32 buffer[10];
struct msghdr msg;
struct iovec iov;
int len;
msg.msg_name = 0;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = 0;
msg.msg_controllen = 0;
iov.iov_base = buffer;
iov.iov_len = sizeof(buffer);
len = recvmsg(xprt->xp_sock, &msg, MSG_PEEK);
if (len < 16 /* Initial packet too small */
|| buffer[2] != htonl(2) /* RPC version 2 */
|| buffer[3] != htonl(0)) { /* CALL */
auth_handler(xprt->xp_sock);
svc_destroy(xprt);
return (0);
}
/* Okay--convert this back into an ordinary TCP socket */
xprt->xp_ops->xp_recv = tcp_receiver;
return svc_recv(xprt, rpcmsg);
}
static void
auth_handler(int sock)
{
int fds[2], pid, fd;
if (socketpair(AF_INET, SOCK_STREAM, IPPROTO_TCP, fds) < 0) {
Dprintf(L_ERROR, "Cannot create socket pair: %m");
return;
}
if ((pid = fork()) < 0) {
Dprintf(L_ERROR, "cannot fork: %m");
return;
}
if (pid == 0) {
/* Parent: create a new transport for this socket */
svcfd_create(fds[0], 0, 0);
return;
}
/* We're the child process. Set up the server socket on fd 0,
* and the client socket on fd 1.
*/
if (dup2(fds[1], 0) < 0 || dup2(sock, 1) < 0) {
Dprintf(L_ERROR, "unable to dup: %m");
exit(1);
}
log_close();
for (fd = 2; fd < OPEN_MAX; fd++)
close(fd);
execl(auth_daemon, auth_daemon, 0);
log_open(auth_daemon, 0);
Dprintf(L_ERROR, "unable to execute: %m");
exit(1);
}
#endif
|