17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * on - user interface program for remote execution service
247c478bd9Sstevel@tonic-gate *
25*16ab6e0bSsm26363 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26*16ab6e0bSsm26363 * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
29*16ab6e0bSsm26363 #pragma ident "%Z%%M% %I% %E% SMI"
30*16ab6e0bSsm26363
317c478bd9Sstevel@tonic-gate #define BSD_COMP
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
447c478bd9Sstevel@tonic-gate #include <rpc/clnt_soc.h>
457c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
467c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
477c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/socket.h>
507c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/time.h>
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #include "rex.h"
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #include <stropts.h>
617c478bd9Sstevel@tonic-gate #include <sys/stream.h>
627c478bd9Sstevel@tonic-gate #include <sys/ttcompat.h>
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate #define bcmp(b1, b2, len) memcmp(b1, b2, len)
667c478bd9Sstevel@tonic-gate #define bzero(b, len) memset(b, '\0', len)
677c478bd9Sstevel@tonic-gate #define bcopy(b1, b2, len) memcpy(b2, b1, len)
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #define CommandName "on" /* given as argv[0] */
707c478bd9Sstevel@tonic-gate #define AltCommandName "dbon"
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate extern int errno;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate * Note - the following must be long enough for at least two portmap
767c478bd9Sstevel@tonic-gate * timeouts on the other side.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate struct timeval LongTimeout = { 123, 0 };
797c478bd9Sstevel@tonic-gate struct timeval testtimeout = { 5, 0 };
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate int Debug = 0; /* print extra debugging information */
827c478bd9Sstevel@tonic-gate int Only2 = 0; /* stdout and stderr are the same */
837c478bd9Sstevel@tonic-gate int Interactive = 0; /* use a pty on server */
847c478bd9Sstevel@tonic-gate int NoInput = 0; /* don't read standard input */
857c478bd9Sstevel@tonic-gate int child = 0; /* pid of the executed process */
867c478bd9Sstevel@tonic-gate int ChildDied = 0; /* true when above is valid */
877c478bd9Sstevel@tonic-gate int HasHelper = 0; /* must kill helpers (interactive mode) */
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate int InOut; /* socket for stdin/stdout */
907c478bd9Sstevel@tonic-gate int Err; /* socket for stderr */
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate struct sgttyb OldFlags; /* saved tty flags */
937c478bd9Sstevel@tonic-gate struct sgttyb NewFlags; /* for stop/continue job control */
947c478bd9Sstevel@tonic-gate CLIENT *Client; /* RPC client handle */
957c478bd9Sstevel@tonic-gate struct rex_ttysize WindowSize; /* saved window size */
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate static int Argc;
987c478bd9Sstevel@tonic-gate static char **Argv; /* saved argument vector (for ps) */
997c478bd9Sstevel@tonic-gate static char *LastArgv; /* saved end-of-argument vector */
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate void usage(void);
1027c478bd9Sstevel@tonic-gate void Die(int stat);
1037c478bd9Sstevel@tonic-gate void doaccept(int *fdp);
1047c478bd9Sstevel@tonic-gate u_short makeport(int *fdp);
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * window change handler - propagate to remote server
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate void
sigwinch(int junk)1117c478bd9Sstevel@tonic-gate sigwinch(int junk)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate struct winsize newsize; /* the modern way to get row and col */
1147c478bd9Sstevel@tonic-gate struct rex_ttysize size; /* the old way no body */
1157c478bd9Sstevel@tonic-gate /* bothered to change */
1167c478bd9Sstevel@tonic-gate enum clnt_stat clstat;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate ioctl(0, TIOCGWINSZ, &newsize);
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * compensate for the struct change
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate size.ts_lines = (int)newsize.ws_row; /* typecast important! */
1247c478bd9Sstevel@tonic-gate size.ts_cols = (int)newsize.ws_col;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate if (bcmp(&size, &WindowSize, sizeof (size)) == 0)
1277c478bd9Sstevel@tonic-gate return;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate WindowSize = size;
1307c478bd9Sstevel@tonic-gate if (clstat = clnt_call(Client, REXPROC_WINCH,
1317c478bd9Sstevel@tonic-gate xdr_rex_ttysize, (caddr_t)&size, xdr_void,
1327c478bd9Sstevel@tonic-gate NULL, LongTimeout)) {
1337c478bd9Sstevel@tonic-gate fprintf(stderr, "on (size): ");
1347c478bd9Sstevel@tonic-gate clnt_perrno(clstat);
1357c478bd9Sstevel@tonic-gate fprintf(stderr, "\r\n");
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate * signal handler - propagate to remote server
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate void
sendsig(int sig)1437c478bd9Sstevel@tonic-gate sendsig(int sig)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate enum clnt_stat clstat;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if (clstat = clnt_call(Client, REXPROC_SIGNAL,
1487c478bd9Sstevel@tonic-gate xdr_int, (caddr_t) &sig, xdr_void,
1497c478bd9Sstevel@tonic-gate NULL, LongTimeout)) {
1507c478bd9Sstevel@tonic-gate fprintf(stderr, "on (signal): ");
1517c478bd9Sstevel@tonic-gate clnt_perrno(clstat);
1527c478bd9Sstevel@tonic-gate fprintf(stderr, "\r\n");
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate void
cont(int junk)1587c478bd9Sstevel@tonic-gate cont(int junk)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * Put tty modes back the way they were and tell the rexd server
1627c478bd9Sstevel@tonic-gate * to send the command a SIGCONT signal.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate if (Interactive) {
1657c478bd9Sstevel@tonic-gate ioctl(0, TIOCSETN, &NewFlags);
1667c478bd9Sstevel@tonic-gate (void) send(InOut, "", 1, MSG_OOB);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate * oob -- called when the command invoked by the rexd server is stopped
1727c478bd9Sstevel@tonic-gate * with a SIGTSTP or SIGSTOP signal.
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate void
oob(int junk)1757c478bd9Sstevel@tonic-gate oob(int junk)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate int atmark;
1787c478bd9Sstevel@tonic-gate char waste[BUFSIZ], mark;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate for (;;) {
1817c478bd9Sstevel@tonic-gate if (ioctl(InOut, SIOCATMARK, &atmark) < 0) {
1827c478bd9Sstevel@tonic-gate perror("ioctl");
1837c478bd9Sstevel@tonic-gate break;
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate if (atmark)
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate (void) read(InOut, waste, sizeof (waste));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate (void) recv(InOut, &mark, 1, MSG_OOB);
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * Reset tty modes to something sane and stop myself
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate if (Interactive) {
1947c478bd9Sstevel@tonic-gate ioctl(0, TIOCSETN, &OldFlags);
1957c478bd9Sstevel@tonic-gate printf("\r\n");
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate kill(getpid(), SIGSTOP);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate
202*16ab6e0bSsm26363 int
main(int argc,char ** argv)2037c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate struct winsize newsize; /* the modern way to get row and col */
2067c478bd9Sstevel@tonic-gate char *rhost, **cmdp;
2077c478bd9Sstevel@tonic-gate char curdir[MAXPATHLEN];
2087c478bd9Sstevel@tonic-gate char wdhost[MAXHOSTNAMELEN];
2097c478bd9Sstevel@tonic-gate char fsname[MAXPATHLEN];
2107c478bd9Sstevel@tonic-gate char dirwithin[MAXPATHLEN];
2117c478bd9Sstevel@tonic-gate struct rex_start rst;
2127c478bd9Sstevel@tonic-gate struct rex_result result;
2137c478bd9Sstevel@tonic-gate extern char **environ;
2147c478bd9Sstevel@tonic-gate enum clnt_stat clstat;
2157c478bd9Sstevel@tonic-gate struct hostent *hp;
2167c478bd9Sstevel@tonic-gate struct sockaddr_in server_addr;
2177c478bd9Sstevel@tonic-gate int sock = RPC_ANYSOCK;
2187c478bd9Sstevel@tonic-gate fd_set selmask, zmask, remmask;
2197c478bd9Sstevel@tonic-gate int nfds, cc;
2207c478bd9Sstevel@tonic-gate char *chi, *cho;
2217c478bd9Sstevel@tonic-gate int trying_authdes;
2227c478bd9Sstevel@tonic-gate char netname[MAXNETNAMELEN+1];
2237c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN+1];
2247c478bd9Sstevel@tonic-gate char publickey[HEXKEYBYTES+1];
2257c478bd9Sstevel@tonic-gate int i;
2267c478bd9Sstevel@tonic-gate char *domain;
2277c478bd9Sstevel@tonic-gate static char buf[4096];
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate * we check the invoked command name to see if it should
2317c478bd9Sstevel@tonic-gate * really be a host name.
2327c478bd9Sstevel@tonic-gate */
2337c478bd9Sstevel@tonic-gate if ((rhost = strrchr(argv[0], '/')) == NULL) {
2347c478bd9Sstevel@tonic-gate rhost = argv[0];
2357c478bd9Sstevel@tonic-gate } else {
2367c478bd9Sstevel@tonic-gate rhost++;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * argv start and extent for setproctitle()
2417c478bd9Sstevel@tonic-gate */
2427c478bd9Sstevel@tonic-gate Argc = argc;
2437c478bd9Sstevel@tonic-gate Argv = argv;
2447c478bd9Sstevel@tonic-gate if (argc > 0)
2457c478bd9Sstevel@tonic-gate LastArgv = argv[argc-1] + strlen(argv[argc-1]);
2467c478bd9Sstevel@tonic-gate else
2477c478bd9Sstevel@tonic-gate LastArgv = NULL;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-') {
2507c478bd9Sstevel@tonic-gate switch (argv[1][1]) {
2517c478bd9Sstevel@tonic-gate case 'd': Debug = 1;
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate case 'i': Interactive = 1;
2547c478bd9Sstevel@tonic-gate break;
2557c478bd9Sstevel@tonic-gate case 'n': NoInput = 1;
2567c478bd9Sstevel@tonic-gate break;
2577c478bd9Sstevel@tonic-gate default:
2587c478bd9Sstevel@tonic-gate printf("Unknown option %s\n", argv[1]);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate argv++;
2617c478bd9Sstevel@tonic-gate argc--;
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate if (strcmp(rhost, CommandName) && strcmp(rhost, AltCommandName)) {
2657c478bd9Sstevel@tonic-gate cmdp = &argv[1];
2667c478bd9Sstevel@tonic-gate Interactive = 1;
2677c478bd9Sstevel@tonic-gate } else {
2687c478bd9Sstevel@tonic-gate if (argc < 2)
2697c478bd9Sstevel@tonic-gate usage();
2707c478bd9Sstevel@tonic-gate rhost = argv[1];
2717c478bd9Sstevel@tonic-gate cmdp = &argv[2];
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * Can only have one of these
2767c478bd9Sstevel@tonic-gate */
2777c478bd9Sstevel@tonic-gate if (Interactive && NoInput)
2787c478bd9Sstevel@tonic-gate usage();
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate if ((hp = gethostbyname(rhost)) == NULL) {
2817c478bd9Sstevel@tonic-gate fprintf(stderr, "on: unknown host %s\n", rhost);
2827c478bd9Sstevel@tonic-gate exit(1);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate bcopy(hp->h_addr, (caddr_t)&server_addr.sin_addr, hp->h_length);
2867c478bd9Sstevel@tonic-gate server_addr.sin_family = AF_INET;
2877c478bd9Sstevel@tonic-gate server_addr.sin_port = 0; /* use pmapper */
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate if (Debug)
2907c478bd9Sstevel@tonic-gate printf("Got the host named %s (%s)\n",
2917c478bd9Sstevel@tonic-gate rhost, inet_ntoa(server_addr.sin_addr));
2927c478bd9Sstevel@tonic-gate trying_authdes = 1;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate try_auth_unix:
2957c478bd9Sstevel@tonic-gate sock = RPC_ANYSOCK;
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate if (Debug)
2987c478bd9Sstevel@tonic-gate printf("clnt_create: Server_Addr %u Prog %d Vers %d Sock %d\n",
2997c478bd9Sstevel@tonic-gate &server_addr, REXPROG, REXVERS, sock);
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if ((Client = clnttcp_create(&server_addr, REXPROG, REXVERS, &sock,
3027c478bd9Sstevel@tonic-gate 0, 0)) == NULL) {
3037c478bd9Sstevel@tonic-gate fprintf(stderr, "on: cannot connect to server on %s\n",
3047c478bd9Sstevel@tonic-gate rhost);
3057c478bd9Sstevel@tonic-gate clnt_pcreateerror("on:");
3067c478bd9Sstevel@tonic-gate exit(1);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate if (Debug)
3107c478bd9Sstevel@tonic-gate printf("TCP RPC connection created\n");
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate if (trying_authdes) {
3137c478bd9Sstevel@tonic-gate yp_get_default_domain(&domain);
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate cho = hostname;
3167c478bd9Sstevel@tonic-gate *cho = 0;
3177c478bd9Sstevel@tonic-gate chi = hp->h_name;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate for (i = 0; (*chi && (i < MAXHOSTNAMELEN)); i++)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate if (isupper(*chi))
3227c478bd9Sstevel@tonic-gate *cho = tolower(*chi);
3237c478bd9Sstevel@tonic-gate else
3247c478bd9Sstevel@tonic-gate *cho = *chi;
3257c478bd9Sstevel@tonic-gate cho++;
3267c478bd9Sstevel@tonic-gate chi++;
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate *cho = 0;
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate if (domain != NULL) {
3317c478bd9Sstevel@tonic-gate if (host2netname(netname, hostname, domain) == 0) {
3327c478bd9Sstevel@tonic-gate trying_authdes = 0;
3337c478bd9Sstevel@tonic-gate if (Debug)
3347c478bd9Sstevel@tonic-gate printf("host2netname failed %s\n",
3357c478bd9Sstevel@tonic-gate hp->h_name);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate /* #ifdef NOWAY */
3387c478bd9Sstevel@tonic-gate else {
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate if (getpublickey(netname, publickey) == 0) {
3417c478bd9Sstevel@tonic-gate trying_authdes = 0;
3427c478bd9Sstevel@tonic-gate cho = strchr(hostname, '.');
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate if (cho) {
3457c478bd9Sstevel@tonic-gate *cho = 0;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if (!host2netname(netname,
3487c478bd9Sstevel@tonic-gate hostname,
3497c478bd9Sstevel@tonic-gate domain)) {
3507c478bd9Sstevel@tonic-gate if (Debug)
3517c478bd9Sstevel@tonic-gate printf("host2netname failed %s\n", hp->h_name);
3527c478bd9Sstevel@tonic-gate } else {
3537c478bd9Sstevel@tonic-gate if (getpublickey(
3547c478bd9Sstevel@tonic-gate netname,
3557c478bd9Sstevel@tonic-gate publickey) != 0)
3567c478bd9Sstevel@tonic-gate trying_authdes = 1;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate } else {
3627c478bd9Sstevel@tonic-gate trying_authdes = 0;
3637c478bd9Sstevel@tonic-gate if (Debug)
3647c478bd9Sstevel@tonic-gate printf("yp_get_default_domain failed \n");
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate if (trying_authdes) {
3697c478bd9Sstevel@tonic-gate Client->cl_auth = (AUTH *)authdes_create(netname, 60*60,
3707c478bd9Sstevel@tonic-gate &server_addr, NULL);
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate if (Client->cl_auth == NULL) {
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate if (Debug)
3757c478bd9Sstevel@tonic-gate printf("authdes_create failed %s\n", netname);
3767c478bd9Sstevel@tonic-gate trying_authdes = 0;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate if (trying_authdes == 0)
3827c478bd9Sstevel@tonic-gate if ((Client->cl_auth = authsys_create_default()) == NULL) {
3837c478bd9Sstevel@tonic-gate clnt_destroy(Client);
3847c478bd9Sstevel@tonic-gate fprintf(stderr,"on: can't create authunix structure.\n");
3857c478bd9Sstevel@tonic-gate exit(1);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate * Now that we have created the TCP connection, we do some
3917c478bd9Sstevel@tonic-gate * work while the server daemon is being swapped in.
3927c478bd9Sstevel@tonic-gate */
3937c478bd9Sstevel@tonic-gate if (getcwd(curdir, MAXPATHLEN) == (char *)NULL) {
3947c478bd9Sstevel@tonic-gate fprintf(stderr, "on: can't find . (%s)\n", curdir);
3957c478bd9Sstevel@tonic-gate exit(1);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate if (findmount(curdir, wdhost, fsname, dirwithin) == 0) {
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate if (Debug) {
4017c478bd9Sstevel@tonic-gate fprintf(stderr,
4027c478bd9Sstevel@tonic-gate "findmount failed: curdir %s\twdhost %s\t",
4037c478bd9Sstevel@tonic-gate curdir, wdhost);
4047c478bd9Sstevel@tonic-gate fprintf(stderr, "fsname %s\tdirwithin %s\n",
4057c478bd9Sstevel@tonic-gate fsname, dirwithin);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate fprintf(stderr, "on: can't locate mount point for %s (%s)\n",
4097c478bd9Sstevel@tonic-gate curdir, dirwithin);
4107c478bd9Sstevel@tonic-gate exit(1);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate if (Debug) {
4147c478bd9Sstevel@tonic-gate printf("findmount suceeds: cwd= %s, wd host %s, fs %s,",
4157c478bd9Sstevel@tonic-gate curdir, wdhost, fsname);
4167c478bd9Sstevel@tonic-gate printf("dir within %s\n", dirwithin);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate Only2 = samefd(1, 2);
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate rst.rst_cmd = (void *)(cmdp);
4227c478bd9Sstevel@tonic-gate rst.rst_host = (void *)wdhost;
4237c478bd9Sstevel@tonic-gate rst.rst_fsname = (void *)fsname;
4247c478bd9Sstevel@tonic-gate rst.rst_dirwithin = (void *)dirwithin;
4257c478bd9Sstevel@tonic-gate rst.rst_env = (void *)environ;
4267c478bd9Sstevel@tonic-gate rst.rst_port0 = makeport(&InOut);
4277c478bd9Sstevel@tonic-gate rst.rst_port1 = rst.rst_port0; /* same port for stdin */
4287c478bd9Sstevel@tonic-gate rst.rst_flags = 0;
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate if (Debug)
4317c478bd9Sstevel@tonic-gate printf("before Interactive flags\n");
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate if (Interactive) {
4347c478bd9Sstevel@tonic-gate rst.rst_flags |= REX_INTERACTIVE;
4357c478bd9Sstevel@tonic-gate ioctl(0, TIOCGETP, &OldFlags);
4367c478bd9Sstevel@tonic-gate NewFlags = OldFlags;
4377c478bd9Sstevel@tonic-gate NewFlags.sg_flags |= (u_int)RAW;
4387c478bd9Sstevel@tonic-gate NewFlags.sg_flags &= (u_int)~ECHO;
4397c478bd9Sstevel@tonic-gate ioctl(0, TIOCSETN, &NewFlags);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate if (Only2) {
4437c478bd9Sstevel@tonic-gate rst.rst_port2 = rst.rst_port1;
4447c478bd9Sstevel@tonic-gate } else {
4457c478bd9Sstevel@tonic-gate rst.rst_port2 = makeport(&Err);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate if (Debug)
4497c478bd9Sstevel@tonic-gate printf("before client call REXPROC_START\n");
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate (void) memset(&result, '\0', sizeof(result));
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate if (clstat = clnt_call(Client, REXPROC_START,
4547c478bd9Sstevel@tonic-gate xdr_rex_start, (caddr_t)&rst,
4557c478bd9Sstevel@tonic-gate xdr_rex_result, (caddr_t)&result, LongTimeout)) {
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate if (Debug)
4587c478bd9Sstevel@tonic-gate printf("Client call failed for REXPROC_START\r\n");
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate if (trying_authdes) {
4617c478bd9Sstevel@tonic-gate auth_destroy(Client->cl_auth);
4627c478bd9Sstevel@tonic-gate clnt_destroy(Client);
4637c478bd9Sstevel@tonic-gate trying_authdes = 0;
4647c478bd9Sstevel@tonic-gate if (Interactive)
4657c478bd9Sstevel@tonic-gate ioctl(0, TIOCSETN, &OldFlags);
4667c478bd9Sstevel@tonic-gate goto try_auth_unix;
4677c478bd9Sstevel@tonic-gate } else {
4687c478bd9Sstevel@tonic-gate fprintf(stderr, "on %s: ", rhost);
4697c478bd9Sstevel@tonic-gate clnt_perrno(clstat);
4707c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
4717c478bd9Sstevel@tonic-gate Die(1);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate if (result.rlt_stat != 0) {
4767c478bd9Sstevel@tonic-gate fprintf(stderr, "on %s: %s\n\r", rhost, result.rlt_message);
4777c478bd9Sstevel@tonic-gate Die(1);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate clnt_freeres(Client, xdr_rex_result, (caddr_t)&result);
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate if (Debug)
4837c478bd9Sstevel@tonic-gate printf("Client call suceeded for REXPROC_START\r\n");
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate if (Interactive) {
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate * Pass the tty modes along to the server
4887c478bd9Sstevel@tonic-gate */
4897c478bd9Sstevel@tonic-gate struct rex_ttymode mode;
4907c478bd9Sstevel@tonic-gate int err;
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate mode.basic.sg_ispeed = OldFlags.sg_ispeed;
4937c478bd9Sstevel@tonic-gate mode.basic.sg_ospeed = OldFlags.sg_ospeed;
4947c478bd9Sstevel@tonic-gate mode.basic.sg_erase = OldFlags.sg_erase;
4957c478bd9Sstevel@tonic-gate mode.basic.sg_kill = OldFlags.sg_kill;
4967c478bd9Sstevel@tonic-gate mode.basic.sg_flags = (short) (OldFlags.sg_flags & 0xFFFF);
4977c478bd9Sstevel@tonic-gate err = (ioctl(0, TIOCGETC, &mode.more) < 0 ||
4987c478bd9Sstevel@tonic-gate ioctl(0, TIOCGLTC, &mode.yetmore) < 0 ||
4997c478bd9Sstevel@tonic-gate ioctl(0, TIOCLGET, &mode.andmore) < 0);
5007c478bd9Sstevel@tonic-gate if (Debug)
5017c478bd9Sstevel@tonic-gate printf("Before clnt_call(REXPROC_MODES) err=%d\n", err);
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate if (!err && (clstat = clnt_call(Client, REXPROC_MODES,
5047c478bd9Sstevel@tonic-gate xdr_rex_ttymode, (caddr_t)&mode,
5057c478bd9Sstevel@tonic-gate xdr_void, NULL, LongTimeout))) {
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate fprintf(stderr, "on (modes) %s: ", rhost);
5087c478bd9Sstevel@tonic-gate clnt_perrno(clstat);
5097c478bd9Sstevel@tonic-gate fprintf(stderr, "\r\n");
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate err = ioctl(0, TIOCGWINSZ, &newsize) < 0;
5137c478bd9Sstevel@tonic-gate /* typecast important in following lines */
5147c478bd9Sstevel@tonic-gate WindowSize.ts_lines = (int)newsize.ws_row;
5157c478bd9Sstevel@tonic-gate WindowSize.ts_cols = (int)newsize.ws_col;
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate if (Debug)
5187c478bd9Sstevel@tonic-gate printf("Before client call REXPROC_WINCH\n");
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate if (!err && (clstat = clnt_call(Client, REXPROC_WINCH,
5217c478bd9Sstevel@tonic-gate xdr_rex_ttysize, (caddr_t)&WindowSize,
5227c478bd9Sstevel@tonic-gate xdr_void, NULL, LongTimeout))) {
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate fprintf(stderr, "on (size) %s: ", rhost);
5257c478bd9Sstevel@tonic-gate clnt_perrno(clstat);
5267c478bd9Sstevel@tonic-gate fprintf(stderr, "\r\n");
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate sigset(SIGWINCH, sigwinch);
5307c478bd9Sstevel@tonic-gate sigset(SIGINT, sendsig);
5317c478bd9Sstevel@tonic-gate sigset(SIGQUIT, sendsig);
5327c478bd9Sstevel@tonic-gate sigset(SIGTERM, sendsig);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate sigset(SIGCONT, cont);
5357c478bd9Sstevel@tonic-gate sigset(SIGURG, oob);
5367c478bd9Sstevel@tonic-gate doaccept(&InOut);
5377c478bd9Sstevel@tonic-gate (void) fcntl(InOut, F_SETOWN, getpid());
5387c478bd9Sstevel@tonic-gate FD_ZERO(&remmask);
5397c478bd9Sstevel@tonic-gate FD_SET(InOut, &remmask);
5407c478bd9Sstevel@tonic-gate if (Debug)
5417c478bd9Sstevel@tonic-gate printf("accept on stdout\r\n");
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate if (!Only2) {
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate doaccept(&Err);
5467c478bd9Sstevel@tonic-gate shutdown(Err, 1); /* 1=> further sends disallowed */
5477c478bd9Sstevel@tonic-gate if (Debug)
5487c478bd9Sstevel@tonic-gate printf("accept on stderr\r\n");
5497c478bd9Sstevel@tonic-gate FD_SET(Err, &remmask);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate FD_ZERO(&zmask);
5537c478bd9Sstevel@tonic-gate if (NoInput) {
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate * no input - simulate end-of-file instead
5577c478bd9Sstevel@tonic-gate */
5587c478bd9Sstevel@tonic-gate shutdown(InOut, 1); /* 1=> further sends disallowed */
5597c478bd9Sstevel@tonic-gate } else {
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate * set up to read standard input, send to remote
5627c478bd9Sstevel@tonic-gate */
5637c478bd9Sstevel@tonic-gate FD_SET(0, &zmask);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate FD_ZERO(&selmask);
5677c478bd9Sstevel@tonic-gate while (FD_ISSET(InOut, &remmask) || FD_ISSET(Err, &remmask)) {
5687c478bd9Sstevel@tonic-gate if (FD_ISSET(InOut, &remmask))
5697c478bd9Sstevel@tonic-gate FD_SET(InOut, &selmask);
5707c478bd9Sstevel@tonic-gate else
5717c478bd9Sstevel@tonic-gate FD_CLR(InOut, &selmask);
5727c478bd9Sstevel@tonic-gate if (FD_ISSET(Err, &remmask))
5737c478bd9Sstevel@tonic-gate FD_SET(Err, &selmask);
5747c478bd9Sstevel@tonic-gate else
5757c478bd9Sstevel@tonic-gate FD_CLR(Err, &selmask);
5767c478bd9Sstevel@tonic-gate if (FD_ISSET(0, &zmask))
5777c478bd9Sstevel@tonic-gate FD_SET(0, &selmask);
5787c478bd9Sstevel@tonic-gate else
5797c478bd9Sstevel@tonic-gate FD_CLR(0, &selmask);
5807c478bd9Sstevel@tonic-gate nfds = select(FD_SETSIZE, &selmask, (fd_set *) 0, (fd_set *) 0,
5817c478bd9Sstevel@tonic-gate (struct timeval *) 0);
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate if (nfds <= 0) {
5857c478bd9Sstevel@tonic-gate if (errno == EINTR) continue;
5867c478bd9Sstevel@tonic-gate perror("on: select");
5877c478bd9Sstevel@tonic-gate Die(1);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate if (FD_ISSET(InOut, &selmask)) {
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate cc = read(InOut, buf, sizeof buf);
5927c478bd9Sstevel@tonic-gate if (cc > 0)
5937c478bd9Sstevel@tonic-gate write(1, buf, cc);
5947c478bd9Sstevel@tonic-gate else
5957c478bd9Sstevel@tonic-gate FD_CLR(InOut, &remmask);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate if (!Only2 && FD_ISSET(Err, &selmask)) {
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate cc = read(Err, buf, sizeof buf);
6017c478bd9Sstevel@tonic-gate if (cc > 0)
6027c478bd9Sstevel@tonic-gate write(2, buf, cc);
6037c478bd9Sstevel@tonic-gate else
6047c478bd9Sstevel@tonic-gate FD_CLR(Err, &remmask);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate if (!NoInput && FD_ISSET(0, &selmask)) {
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate cc = read(0, buf, sizeof buf);
6107c478bd9Sstevel@tonic-gate if (cc > 0)
6117c478bd9Sstevel@tonic-gate write(InOut, buf, cc);
6127c478bd9Sstevel@tonic-gate else {
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * End of standard input - shutdown outgoing
6157c478bd9Sstevel@tonic-gate * direction of the TCP connection.
6167c478bd9Sstevel@tonic-gate */
6177c478bd9Sstevel@tonic-gate if (Debug)
6187c478bd9Sstevel@tonic-gate printf("Got EOF - shutting down connection\n");
6197c478bd9Sstevel@tonic-gate FD_CLR(0, &zmask);
6207c478bd9Sstevel@tonic-gate shutdown(InOut, 1); /* further sends disallowed */
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate close(InOut);
6267c478bd9Sstevel@tonic-gate if (!Only2)
6277c478bd9Sstevel@tonic-gate close(Err);
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate (void) memset(&result, '\0', sizeof(result));
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate if (clstat = clnt_call(Client, REXPROC_WAIT,
6327c478bd9Sstevel@tonic-gate xdr_void, 0, xdr_rex_result, (caddr_t)&result,
6337c478bd9Sstevel@tonic-gate LongTimeout)) {
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate fprintf(stderr, "on: ");
6367c478bd9Sstevel@tonic-gate clnt_perrno(clstat);
6377c478bd9Sstevel@tonic-gate fprintf(stderr, "\r\n");
6387c478bd9Sstevel@tonic-gate Die(1);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate Die(result.rlt_stat);
641*16ab6e0bSsm26363 return (0); /* Should never get here. */
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate * like exit, but resets the terminal state first
6467c478bd9Sstevel@tonic-gate */
6477c478bd9Sstevel@tonic-gate void
Die(int stat)6487c478bd9Sstevel@tonic-gate Die(int stat)
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate if (Interactive) {
6527c478bd9Sstevel@tonic-gate ioctl(0, TIOCSETN, &OldFlags);
6537c478bd9Sstevel@tonic-gate printf("\r\n");
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate exit(stat);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate void
remstop()6607c478bd9Sstevel@tonic-gate remstop()
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate Die(23);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate * returns true if we can safely say that the two file descriptors
6687c478bd9Sstevel@tonic-gate * are the "same" (both are same file).
6697c478bd9Sstevel@tonic-gate */
6707c478bd9Sstevel@tonic-gate int
samefd(a,b)6717c478bd9Sstevel@tonic-gate samefd(a, b)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate struct stat astat, bstat;
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate if (fstat(a, &astat) || fstat(b, &bstat))
6767c478bd9Sstevel@tonic-gate return (0);
6777c478bd9Sstevel@tonic-gate if (astat.st_ino == 0 || bstat.st_ino == 0)
6787c478bd9Sstevel@tonic-gate return (0);
6797c478bd9Sstevel@tonic-gate return (!bcmp(&astat, &bstat, sizeof (astat)));
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate
6827c478bd9Sstevel@tonic-gate
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate * accept the incoming connection on the given
6857c478bd9Sstevel@tonic-gate * file descriptor, and return the new file descritpor
6867c478bd9Sstevel@tonic-gate */
6877c478bd9Sstevel@tonic-gate void
doaccept(fdp)6887c478bd9Sstevel@tonic-gate doaccept(fdp)
6897c478bd9Sstevel@tonic-gate int *fdp;
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate int fd;
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate fd = accept(*fdp, 0, 0);
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate if (fd < 0) {
6967c478bd9Sstevel@tonic-gate perror("accept");
6977c478bd9Sstevel@tonic-gate remstop();
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate close(*fdp);
7007c478bd9Sstevel@tonic-gate *fdp = fd;
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate * create a socket, and return its the port number.
7057c478bd9Sstevel@tonic-gate */
7067c478bd9Sstevel@tonic-gate u_short
makeport(fdp)7077c478bd9Sstevel@tonic-gate makeport(fdp)
7087c478bd9Sstevel@tonic-gate int *fdp;
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate struct sockaddr_in sin;
7117c478bd9Sstevel@tonic-gate socklen_t len = (socklen_t)sizeof (sin);
7127c478bd9Sstevel@tonic-gate int fd;
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate fd = socket(AF_INET, SOCK_STREAM, 0);
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate if (fd < 0) {
7177c478bd9Sstevel@tonic-gate perror("socket");
7187c478bd9Sstevel@tonic-gate exit(1);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate bzero((char *)&sin, sizeof (sin));
7227c478bd9Sstevel@tonic-gate sin.sin_family = AF_INET;
7237c478bd9Sstevel@tonic-gate bind(fd, (struct sockaddr *)&sin, sizeof (sin));
7247c478bd9Sstevel@tonic-gate getsockname(fd, (struct sockaddr *)&sin, &len);
7257c478bd9Sstevel@tonic-gate listen(fd, 1);
7267c478bd9Sstevel@tonic-gate *fdp = fd;
7277c478bd9Sstevel@tonic-gate return (htons(sin.sin_port));
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate void
usage(void)7317c478bd9Sstevel@tonic-gate usage(void)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate fprintf(stderr, "Usage: on [-i|-n] [-d] machine cmd [args]...\n");
7347c478bd9Sstevel@tonic-gate exit(1);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate * SETPROCTITLE -- set the title of this process for "ps"
7397c478bd9Sstevel@tonic-gate *
7407c478bd9Sstevel@tonic-gate * Does nothing if there were not enough arguments on the command
7417c478bd9Sstevel@tonic-gate * line for the information.
7427c478bd9Sstevel@tonic-gate *
7437c478bd9Sstevel@tonic-gate * Side Effects:
7447c478bd9Sstevel@tonic-gate * Clobbers argv[] of our main procedure.
7457c478bd9Sstevel@tonic-gate */
7467c478bd9Sstevel@tonic-gate void
setproctitle(user,host)7477c478bd9Sstevel@tonic-gate setproctitle(user, host)
7487c478bd9Sstevel@tonic-gate char *user, *host;
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate register char *tohere;
7517c478bd9Sstevel@tonic-gate
7527c478bd9Sstevel@tonic-gate tohere = Argv[0];
7537c478bd9Sstevel@tonic-gate if ((int)LastArgv == (int)((char *)NULL) ||
7547c478bd9Sstevel@tonic-gate (int)(strlen(user) + strlen(host)+3) > (int)(LastArgv - tohere))
7557c478bd9Sstevel@tonic-gate return;
7567c478bd9Sstevel@tonic-gate *tohere++ = '-'; /* So ps prints (rpc.rexd) */
7577c478bd9Sstevel@tonic-gate sprintf(tohere, "%s@%s", user, host);
7587c478bd9Sstevel@tonic-gate while (*tohere++) /* Skip to end of printf output */
7597c478bd9Sstevel@tonic-gate ;
7607c478bd9Sstevel@tonic-gate while (tohere < LastArgv) /* Avoid confusing ps */
7617c478bd9Sstevel@tonic-gate *tohere++ = ' ';
7627c478bd9Sstevel@tonic-gate }
763