- safe_fd(-1, 0);
-
- amandad_path = vstralloc(libexecdir, "/", "amandad", versionsuffix(),
- NULL);
- execlp(RSH_PATH, RSH_PATH, RSH_ARGS, rc->hostname, amandad_path,
- "-auth=rsh", NULL);
- error("error: couldn't exec %s: %s", RSH_PATH, strerror(errno));
-
- /* should nerver go here, shut up compiler warning */
- return(-1);
-}
-
-/*
- * Transmit a packet.
- */
-static int
-rsh_sendpkt(cookie, pkt)
- void *cookie;
- pkt_t *pkt;
-{
- char buf[sizeof(pkt_t)];
- struct rsh_handle *rh = cookie;
- size_t len;
-
- assert(rh != NULL);
- assert(pkt != NULL);
-
- rshprintf(("rsh: sendpkt: enter\n"));
-
- len = strlen(pkt->body) + 2;
- buf[0] = (char)pkt->type;
- strcpy(&buf[1], pkt->body);
-
- rshprintf(("rsh: sendpkt: %s (%d) pkt_t (len %d) contains:\n\n\"%s\"\n\n",
- pkt_type2str(pkt->type), pkt->type, strlen(pkt->body), pkt->body));
-
- if (rsh_stream_write(rh->rs, buf, len) < 0) {
- security_seterror(&rh->sech, security_stream_geterror(&rh->rs->secstr));
- return (-1);
- }
- return (0);
-}
-
-/*
- * Set up to receive a packet asyncronously, and call back when
- * it has been read.
- */
-static void
-rsh_recvpkt(cookie, fn, arg, timeout)
- void *cookie, *arg;
- void (*fn) P((void *, pkt_t *, security_status_t));
- int timeout;
-{
- struct rsh_handle *rh = cookie;
-
- assert(rh != NULL);
-
- rshprintf(("rsh: recvpkt registered for %s\n", rh->hostname));
-
- /*
- * Reset any pending timeout on this handle
- */
- if (rh->ev_timeout != NULL)
- event_release(rh->ev_timeout);
-
- /*
- * Negative timeouts mean no timeout
- */
- if (timeout < 0)
- rh->ev_timeout = NULL;
- else
- rh->ev_timeout = event_register(timeout, EV_TIME, recvpkt_timeout, rh);
-
- rh->fn.recvpkt = fn;
- rh->arg = arg;
- rsh_stream_read(rh->rs, recvpkt_callback, rh);
-}
-
-/*
- * Remove a async receive request from the queue
- */
-static void
-rsh_recvpkt_cancel(cookie)
- void *cookie;
-{
- struct rsh_handle *rh = cookie;
-
- rshprintf(("rsh: cancelling recvpkt for %s\n", rh->hostname));
-
- assert(rh != NULL);
-
- rsh_stream_read_cancel(rh->rs);
- if (rh->ev_timeout != NULL) {
- event_release(rh->ev_timeout);
- rh->ev_timeout = NULL;
- }
-}
-
-/*
- * This is called when a handle is woken up because data read off of the
- * net is for it.
- */
-static void
-recvpkt_callback(cookie, buf, bufsize)
- void *cookie, *buf;
- ssize_t bufsize;
-{
- pkt_t pkt;
- struct rsh_handle *rh = cookie;
-
- assert(rh != NULL);
-
- /*
- * We need to cancel the recvpkt request before calling
- * the callback because the callback may reschedule us.
- */
- rsh_recvpkt_cancel(rh);
-
- switch (bufsize) {
- case 0:
- security_seterror(&rh->sech,
- "EOF on read from %s", rh->hostname);
- (*rh->fn.recvpkt)(rh->arg, NULL, S_ERROR);
- return;
- case -1:
- security_seterror(&rh->sech, security_stream_geterror(&rh->rs->secstr));
- (*rh->fn.recvpkt)(rh->arg, NULL, S_ERROR);
- return;
- default:
- break;
- }
-
- parse_pkt(&pkt, buf, bufsize);
- rshprintf(("rsh: received %s packet (%d) from %s, contains:\n\n\"%s\"\n\n",
- pkt_type2str(pkt.type), pkt.type, rh->hostname, pkt.body));
- (*rh->fn.recvpkt)(rh->arg, &pkt, S_OK);
-}
-
-/*
- * This is called when a handle times out before receiving a packet.
- */
-static void
-recvpkt_timeout(cookie)
- void *cookie;
-{
- struct rsh_handle *rh = cookie;
-
- assert(rh != NULL);
-
- rshprintf(("rsh: recvpkt timeout for %s\n", rh->hostname));
-
- rsh_recvpkt_cancel(rh);
- (*rh->fn.recvpkt)(rh->arg, NULL, S_TIMEOUT);
-}
-
-/*
- * Create the server end of a stream. For rsh, this means setup a stream
- * object and allocate a new handle for it.
- */
-static void *
-rsh_stream_server(h)
- void *h;
-{
- struct rsh_handle *rh = h;
- struct rsh_stream *rs;
-
- assert(rh != NULL);
-
- rs = alloc(sizeof(*rs));
- security_streaminit(&rs->secstr, &rsh_security_driver);
- rs->rc = conn_get(rh->hostname);
- /*
- * Stream should already be setup!
- */
- if (rs->rc->read < 0) {
- conn_put(rs->rc);
- amfree(rs);
- security_seterror(&rh->sech, "lost connection to %s", rh->hostname);
- return (NULL);
- }
- rh->hostname = rs->rc->hostname;
- /*
- * so as not to conflict with the amanda server's handle numbers,
- * we start at 5000 and work down
- */
- rs->handle = 5000 - newhandle++;
- rs->ev_read = NULL;
- rshprintf(("rsh: stream_server: created stream %d\n", rs->handle));
- return (rs);
-}
-
-/*
- * Accept an incoming connection on a stream_server socket
- * Nothing needed for rsh.
- */
-static int
-rsh_stream_accept(s)
- void *s;
-{
-
- return (0);
-}
-
-/*
- * Return a connected stream. For rsh, this means setup a stream
- * with the supplied handle.
- */
-static void *
-rsh_stream_client(h, id)
- void *h;
- int id;
-{
- struct rsh_handle *rh = h;
- struct rsh_stream *rs;
-
- assert(rh != NULL);
-
- if (id <= 0) {
- security_seterror(&rh->sech,
- "%d: invalid security stream id", id);
- return (NULL);
- }
-
- rs = alloc(sizeof(*rs));
- security_streaminit(&rs->secstr, &rsh_security_driver);
- rs->handle = id;
- rs->ev_read = NULL;
- rs->rc = conn_get(rh->hostname);
-
- rshprintf(("rsh: stream_client: connected to stream %d\n", id));
-
- return (rs);
-}
-
-/*
- * Close and unallocate resources for a stream.
- */
-static void
-rsh_stream_close(s)
- void *s;
-{
- struct rsh_stream *rs = s;
-
- assert(rs != NULL);
-
- rshprintf(("rsh: stream_close: closing stream %d\n", rs->handle));
-
- rsh_stream_read_cancel(rs);
- conn_put(rs->rc);
- amfree(rs);
-}
-
-/*
- * Authenticate a stream
- * Nothing needed for rsh. The connection is authenticated by rshd
- * on startup.
- */
-static int
-rsh_stream_auth(s)
- void *s;
-{
-
- return (0);
-}
-
-/*
- * Returns the stream id for this stream. This is just the local
- * port.
- */
-static int
-rsh_stream_id(s)
- void *s;
-{
- struct rsh_stream *rs = s;
-
- assert(rs != NULL);
-
- return (rs->handle);
-}
-
-/*
- * Write a chunk of data to a stream. Blocks until completion.
- */
-static int
-rsh_stream_write(s, buf, size)
- void *s;
- const void *buf;
- size_t size;
-{
- struct rsh_stream *rs = s;
-
- assert(rs != NULL);
-
- rshprintf(("rsh: stream_write: writing %d bytes to %s:%d\n", size,
- rs->rc->hostname, rs->handle));
-
- if (send_token(rs->rc, rs->handle, buf, size) < 0) {
- security_stream_seterror(&rs->secstr, rs->rc->errmsg);
- return (-1);
- }
- return (0);
-}
-
-/*
- * Submit a request to read some data. Calls back with the given
- * function and arg when completed.
- */
-static void
-rsh_stream_read(s, fn, arg)
- void *s, *arg;
- void (*fn) P((void *, void *, ssize_t));
-{
- struct rsh_stream *rs = s;
-
- assert(rs != NULL);
-
- /*
- * Only one read request can be active per stream.
- */
- if (rs->ev_read == NULL) {
- rs->ev_read = event_register((event_id_t)rs->rc, EV_WAIT,
- stream_read_callback, rs);
- conn_read(rs->rc);
- }
- rs->fn = fn;
- rs->arg = arg;
-}
-
-/*
- * Cancel a previous stream read request. It's ok if we didn't have a read
- * scheduled.
- */
-static void
-rsh_stream_read_cancel(s)
- void *s;
-{
- struct rsh_stream *rs = s;
-
- assert(rs != NULL);
-
- if (rs->ev_read != NULL) {
- event_release(rs->ev_read);
- rs->ev_read = NULL;
- conn_read_cancel(rs->rc);
- }
-}
-
-/*
- * Callback for rsh_stream_read
- */
-static void
-stream_read_callback(arg)
- void *arg;
-{
- struct rsh_stream *rs = arg;
- assert(rs != NULL);
-
- rshprintf(("rsh: stream_read_callback: handle %d\n", rs->handle));
-
- /*
- * Make sure this was for us. If it was, then blow away the handle
- * so it doesn't get claimed twice. Otherwise, leave it alone.
- *
- * If the handle is EOF, pass that up to our callback.
- */
- if (rs->rc->handle == rs->handle) {
- rshprintf(("rsh: stream_read_callback: it was for us\n"));
- rs->rc->handle = H_TAKEN;
- } else if (rs->rc->handle != H_EOF) {
- rshprintf(("rsh: stream_read_callback: not for us\n"));
- return;
- }
-
- /*
- * Remove the event first, and then call the callback.
- * We remove it first because we don't want to get in their
- * way if they reschedule it.
- */
- rsh_stream_read_cancel(rs);
-
- if (rs->rc->pktlen == 0) {
- rshprintf(("rsh: stream_read_callback: EOF\n"));
- (*rs->fn)(rs->arg, NULL, 0);
- return;
- }
- rshprintf(("rsh: stream_read_callback: read %ld bytes from %s:%d\n",
- rs->rc->pktlen, rs->rc->hostname, rs->handle));
- (*rs->fn)(rs->arg, rs->rc->pkt, rs->rc->pktlen);
-}
-
-/*
- * The callback for the netfd for the event handler
- * Determines if this packet is for this security handle,
- * and does the real callback if so.
- */
-static void
-conn_read_callback(cookie)
- void *cookie;
-{
- struct rsh_conn *rc = cookie;
- struct rsh_handle *rh;
- pkt_t pkt;
- int rval;
-
- assert(cookie != NULL);
-
- rshprintf(("rsh: conn_read_callback\n"));
-
- /* Read the data off the wire. If we get errors, shut down. */
- rval = recv_token(rc, 5);
- rshprintf(("rsh: conn_read_callback: recv_token returned %d\n", rval));
- if (rval <= 0) {
- rc->pktlen = 0;
- rc->handle = H_EOF;
- rval = event_wakeup((event_id_t)rc);
- rshprintf(("rsh: conn_read_callback: event_wakeup return %d\n", rval));
- /* delete our 'accept' reference */
- if (accept_fn != NULL)
- conn_put(rc);
- accept_fn = NULL;
- return;
- }
-
- /* If there are events waiting on this handle, we're done */
- rval = event_wakeup((event_id_t)rc);
- rshprintf(("rsh: conn_read_callback: event_wakeup return %d\n", rval));
- if (rval > 0)
- return;
-
- /* If there is no accept fn registered, then drop the packet */
- if (accept_fn == NULL)
- return;
-
- rh = alloc(sizeof(*rh));
- security_handleinit(&rh->sech, &rsh_security_driver);
- rh->hostname = rc->hostname;
- rh->rs = rsh_stream_client(rh, rc->handle);
- rh->ev_timeout = NULL;
-
- rshprintf(("rsh: new connection\n"));
- parse_pkt(&pkt, rc->pkt, rc->pktlen);
- rshprintf(("rsh: calling accept_fn\n"));
- (*accept_fn)(&rh->sech, &pkt);
-}
-
-static void
-parse_pkt(pkt, buf, bufsize)
- pkt_t *pkt;
- const void *buf;
- size_t bufsize;
-{
- const unsigned char *bufp = buf;
-
- rshprintf(("rsh: parse_pkt: parsing buffer of %d bytes\n", bufsize));
-
- pkt->type = (pktype_t)*bufp++;
- bufsize--;
-
- if (bufsize == 0) {
- pkt->body[0] = '\0';
- } else {
- if (bufsize > sizeof(pkt->body) - 1)
- bufsize = sizeof(pkt->body) - 1;
- memcpy(pkt->body, bufp, bufsize);
- pkt->body[sizeof(pkt->body) - 1] = '\0';
- }
-
- rshprintf(("rsh: parse_pkt: %s (%d): \"%s\"\n", pkt_type2str(pkt->type),
- pkt->type, pkt->body));
-}
-
-
-/*
- * Transmits a chunk of data over a rsh_handle, adding
- * the necessary headers to allow the remote end to decode it.
- */
-static int
-send_token(rc, handle, buf, len)
- struct rsh_conn *rc;
- int handle;
- const void *buf;
- size_t len;
-{
- unsigned int netlength, nethandle;
- struct iovec iov[3];
-
- rshprintf(("rsh: send_token: writing %d bytes to %s\n", len,
- rc->hostname));
-
- assert(sizeof(netlength) == 4);
-
- /*
- * Format is:
- * 32 bit length (network byte order)
- * 32 bit handle (network byte order)
- * data
- */
- netlength = htonl(len);
- iov[0].iov_base = (void *)&netlength;
- iov[0].iov_len = sizeof(netlength);
-
- nethandle = htonl(handle);
- iov[1].iov_base = (void *)&nethandle;
- iov[1].iov_len = sizeof(nethandle);
-
- iov[2].iov_base = (void *)buf;
- iov[2].iov_len = len;
-
- if (net_writev(rc->write, iov, 3) < 0) {
- rc->errmsg = newvstralloc(rc->errmsg, "rsh write error to ",
- rc->hostname, ": ", strerror(errno), NULL);
- return (-1);
- }
- return (0);
-}
-
-static int
-recv_token(rc, timeout)
- struct rsh_conn *rc;
- int timeout;
-{
- unsigned int netint;
-
- assert(sizeof(netint) == 4);
-
- assert(rc->read >= 0);
-
- rshprintf(("rsh: recv_token: reading from %s\n", rc->hostname));
-
- switch (net_read(rc, &netint, sizeof(netint), timeout)) {
- case -1:
- rc->errmsg = newvstralloc(rc->errmsg, "recv error: ", strerror(errno),
- NULL);
- return (-1);
- case 0:
- rc->pktlen = 0;
- return (0);
- default:
- break;
- }
- rc->pktlen = ntohl(netint);
- if (rc->pktlen > sizeof(rc->pkt)) {
- rc->errmsg = newstralloc(rc->errmsg, "recv error: huge packet");
- return (-1);
- }
-
- switch (net_read(rc, &netint, sizeof(netint), timeout)) {
- case -1:
- rc->errmsg = newvstralloc(rc->errmsg, "recv error: ", strerror(errno),
- NULL);
- return (-1);
- case 0:
- rc->pktlen = 0;
- return (0);
- default:
- break;
- }
- rc->handle = ntohl(netint);