xref: /freebsd/lib/libcompat/4.3/rexec.c (revision 12bae251fbfa33f375d134e0840d88c4363b8b94)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1980, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3258f0484fSRodney W. Grimes 
3358f0484fSRodney W. Grimes #include <sys/types.h>
34f06a5580SPoul-Henning Kamp #include <sys/uio.h>
3558f0484fSRodney W. Grimes #include <sys/socket.h>
36bfb50190SJoerg Wunsch #include <sys/param.h>
37bfb50190SJoerg Wunsch #include <sys/stat.h>
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #include <netinet/in.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <stdio.h>
42f06a5580SPoul-Henning Kamp #include <unistd.h>
43bfb50190SJoerg Wunsch #include <string.h>
4458f0484fSRodney W. Grimes #include <netdb.h>
4558f0484fSRodney W. Grimes #include <errno.h>
46bfb50190SJoerg Wunsch #include <ctype.h>
47bfb50190SJoerg Wunsch #include <err.h>
48bfb50190SJoerg Wunsch #include <stdlib.h>
49bfb50190SJoerg Wunsch #include <unistd.h>
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes int	rexecoptions;
5258f0484fSRodney W. Grimes 
53bfb50190SJoerg Wunsch /*
54bfb50190SJoerg Wunsch  * Options and other state info.
55bfb50190SJoerg Wunsch  */
56bfb50190SJoerg Wunsch struct macel {
57bfb50190SJoerg Wunsch 	char mac_name[9];	/* macro name */
58bfb50190SJoerg Wunsch 	char *mac_start;	/* start of macro in macbuf */
59bfb50190SJoerg Wunsch 	char *mac_end;		/* end of macro in macbuf */
60bfb50190SJoerg Wunsch };
61bfb50190SJoerg Wunsch 
62bfb50190SJoerg Wunsch int macnum;			/* number of defined macros */
63bfb50190SJoerg Wunsch struct macel macros[16];
64bfb50190SJoerg Wunsch char macbuf[4096];
65bfb50190SJoerg Wunsch 
66bfb50190SJoerg Wunsch static	FILE *cfile;
67bfb50190SJoerg Wunsch 
68bfb50190SJoerg Wunsch #define	DEFAULT	1
69bfb50190SJoerg Wunsch #define	LOGIN	2
70bfb50190SJoerg Wunsch #define	PASSWD	3
71bfb50190SJoerg Wunsch #define	ACCOUNT 4
72bfb50190SJoerg Wunsch #define MACDEF  5
73bfb50190SJoerg Wunsch #define	ID	10
74bfb50190SJoerg Wunsch #define	MACH	11
75bfb50190SJoerg Wunsch 
76bfb50190SJoerg Wunsch static char tokval[100];
77bfb50190SJoerg Wunsch 
78bfb50190SJoerg Wunsch static struct toktab {
79bfb50190SJoerg Wunsch 	char *tokstr;
80bfb50190SJoerg Wunsch 	int tval;
81bfb50190SJoerg Wunsch } toktab[]= {
82bfb50190SJoerg Wunsch 	{ "default",	DEFAULT },
83bfb50190SJoerg Wunsch 	{ "login",	LOGIN },
84bfb50190SJoerg Wunsch 	{ "password",	PASSWD },
85bfb50190SJoerg Wunsch 	{ "passwd",	PASSWD },
86bfb50190SJoerg Wunsch 	{ "account",	ACCOUNT },
87bfb50190SJoerg Wunsch 	{ "machine",	MACH },
88bfb50190SJoerg Wunsch 	{ "macdef",	MACDEF },
89bfb50190SJoerg Wunsch 	{ NULL,		0 }
90bfb50190SJoerg Wunsch };
91bfb50190SJoerg Wunsch 
92bfb50190SJoerg Wunsch static int
token()93bfb50190SJoerg Wunsch token()
94bfb50190SJoerg Wunsch {
95bfb50190SJoerg Wunsch 	char *cp;
96bfb50190SJoerg Wunsch 	int c;
97bfb50190SJoerg Wunsch 	struct toktab *t;
98bfb50190SJoerg Wunsch 
99bfb50190SJoerg Wunsch 	if (feof(cfile) || ferror(cfile))
100bfb50190SJoerg Wunsch 		return (0);
101bfb50190SJoerg Wunsch 	while ((c = getc(cfile)) != EOF &&
102bfb50190SJoerg Wunsch 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
103bfb50190SJoerg Wunsch 		continue;
104bfb50190SJoerg Wunsch 	if (c == EOF)
105bfb50190SJoerg Wunsch 		return (0);
106bfb50190SJoerg Wunsch 	cp = tokval;
107bfb50190SJoerg Wunsch 	if (c == '"') {
108bfb50190SJoerg Wunsch 		while ((c = getc(cfile)) != EOF && c != '"') {
109bfb50190SJoerg Wunsch 			if (c == '\\')
110bfb50190SJoerg Wunsch 				c = getc(cfile);
111bfb50190SJoerg Wunsch 			*cp++ = c;
112bfb50190SJoerg Wunsch 		}
113bfb50190SJoerg Wunsch 	} else {
114bfb50190SJoerg Wunsch 		*cp++ = c;
115bfb50190SJoerg Wunsch 		while ((c = getc(cfile)) != EOF
116bfb50190SJoerg Wunsch 		    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
117bfb50190SJoerg Wunsch 			if (c == '\\')
118bfb50190SJoerg Wunsch 				c = getc(cfile);
119bfb50190SJoerg Wunsch 			*cp++ = c;
120bfb50190SJoerg Wunsch 		}
121bfb50190SJoerg Wunsch 	}
122bfb50190SJoerg Wunsch 	*cp = 0;
123bfb50190SJoerg Wunsch 	if (tokval[0] == 0)
124bfb50190SJoerg Wunsch 		return (0);
125bfb50190SJoerg Wunsch 	for (t = toktab; t->tokstr; t++)
126bfb50190SJoerg Wunsch 		if (!strcmp(t->tokstr, tokval))
127bfb50190SJoerg Wunsch 			return (t->tval);
128bfb50190SJoerg Wunsch 	return (ID);
129bfb50190SJoerg Wunsch }
130bfb50190SJoerg Wunsch 
131bfb50190SJoerg Wunsch static int
ruserpass(char * host,char ** aname,char ** apass,char ** aacct)132*12bae251SPiotr Pawel Stefaniak ruserpass(char *host, char **aname, char **apass, char **aacct)
133bfb50190SJoerg Wunsch {
134bfb50190SJoerg Wunsch 	char *hdir, buf[BUFSIZ], *tmp;
135bfb50190SJoerg Wunsch 	char myname[MAXHOSTNAMELEN], *mydomain;
136bfb50190SJoerg Wunsch 	int t, i, c, usedefault = 0;
137bfb50190SJoerg Wunsch 	struct stat stb;
138bfb50190SJoerg Wunsch 
139bfb50190SJoerg Wunsch 	hdir = getenv("HOME");
140bfb50190SJoerg Wunsch 	if (hdir == NULL)
141bfb50190SJoerg Wunsch 		hdir = ".";
1429c9c8212SKris Kennaway 	if (strlen(hdir) + 8 > sizeof(buf))
1439c9c8212SKris Kennaway 		return (0);
144bfb50190SJoerg Wunsch 	(void) sprintf(buf, "%s/.netrc", hdir);
145bfb50190SJoerg Wunsch 	cfile = fopen(buf, "r");
146bfb50190SJoerg Wunsch 	if (cfile == NULL) {
147bfb50190SJoerg Wunsch 		if (errno != ENOENT)
148bfb50190SJoerg Wunsch 			warn("%s", buf);
149bfb50190SJoerg Wunsch 		return (0);
150bfb50190SJoerg Wunsch 	}
151bfb50190SJoerg Wunsch 	if (gethostname(myname, sizeof(myname)) < 0)
152bfb50190SJoerg Wunsch 		myname[0] = '\0';
153bfb50190SJoerg Wunsch 	if ((mydomain = strchr(myname, '.')) == NULL)
154bfb50190SJoerg Wunsch 		mydomain = "";
155bfb50190SJoerg Wunsch next:
156bfb50190SJoerg Wunsch 	while ((t = token())) switch(t) {
157bfb50190SJoerg Wunsch 
158bfb50190SJoerg Wunsch 	case DEFAULT:
159bfb50190SJoerg Wunsch 		usedefault = 1;
160bfb50190SJoerg Wunsch 		/* FALL THROUGH */
161bfb50190SJoerg Wunsch 
162bfb50190SJoerg Wunsch 	case MACH:
163bfb50190SJoerg Wunsch 		if (!usedefault) {
164bfb50190SJoerg Wunsch 			if (token() != ID)
165bfb50190SJoerg Wunsch 				continue;
166bfb50190SJoerg Wunsch 			/*
167bfb50190SJoerg Wunsch 			 * Allow match either for user's input host name
168bfb50190SJoerg Wunsch 			 * or official hostname.  Also allow match of
169bfb50190SJoerg Wunsch 			 * incompletely-specified host in local domain.
170bfb50190SJoerg Wunsch 			 */
171bfb50190SJoerg Wunsch 			if (strcasecmp(host, tokval) == 0)
172bfb50190SJoerg Wunsch 				goto match;
173bfb50190SJoerg Wunsch 			if ((tmp = strchr(host, '.')) != NULL &&
174bfb50190SJoerg Wunsch 			    strcasecmp(tmp, mydomain) == 0 &&
175bfb50190SJoerg Wunsch 			    strncasecmp(host, tokval, tmp - host) == 0 &&
176bfb50190SJoerg Wunsch 			    tokval[tmp - host] == '\0')
177bfb50190SJoerg Wunsch 				goto match;
178bfb50190SJoerg Wunsch 			continue;
179bfb50190SJoerg Wunsch 		}
180bfb50190SJoerg Wunsch 	match:
181bfb50190SJoerg Wunsch 		while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
182bfb50190SJoerg Wunsch 
183bfb50190SJoerg Wunsch 		case LOGIN:
184bfb50190SJoerg Wunsch 			if (token())
18550a7cc95SPedro F. Giffuni 				if (*aname == NULL) {
186bfb50190SJoerg Wunsch 					*aname = malloc((unsigned) strlen(tokval) + 1);
187bfb50190SJoerg Wunsch 					(void) strcpy(*aname, tokval);
188bfb50190SJoerg Wunsch 				} else {
189bfb50190SJoerg Wunsch 					if (strcmp(*aname, tokval))
190bfb50190SJoerg Wunsch 						goto next;
191bfb50190SJoerg Wunsch 				}
192bfb50190SJoerg Wunsch 			break;
193bfb50190SJoerg Wunsch 		case PASSWD:
19450a7cc95SPedro F. Giffuni 			if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
195bfb50190SJoerg Wunsch 			    fstat(fileno(cfile), &stb) >= 0 &&
196bfb50190SJoerg Wunsch 			    (stb.st_mode & 077) != 0) {
197bfb50190SJoerg Wunsch 	warnx("Error: .netrc file is readable by others.");
198bfb50190SJoerg Wunsch 	warnx("Remove password or make file unreadable by others.");
199bfb50190SJoerg Wunsch 				goto bad;
200bfb50190SJoerg Wunsch 			}
20150a7cc95SPedro F. Giffuni 			if (token() && *apass == NULL) {
202bfb50190SJoerg Wunsch 				*apass = malloc((unsigned) strlen(tokval) + 1);
203bfb50190SJoerg Wunsch 				(void) strcpy(*apass, tokval);
204bfb50190SJoerg Wunsch 			}
205bfb50190SJoerg Wunsch 			break;
206bfb50190SJoerg Wunsch 		case ACCOUNT:
207bfb50190SJoerg Wunsch 			if (fstat(fileno(cfile), &stb) >= 0
208bfb50190SJoerg Wunsch 			    && (stb.st_mode & 077) != 0) {
209bfb50190SJoerg Wunsch 	warnx("Error: .netrc file is readable by others.");
210bfb50190SJoerg Wunsch 	warnx("Remove account or make file unreadable by others.");
211bfb50190SJoerg Wunsch 				goto bad;
212bfb50190SJoerg Wunsch 			}
21350a7cc95SPedro F. Giffuni 			if (token() && *aacct == NULL) {
214bfb50190SJoerg Wunsch 				*aacct = malloc((unsigned) strlen(tokval) + 1);
215bfb50190SJoerg Wunsch 				(void) strcpy(*aacct, tokval);
216bfb50190SJoerg Wunsch 			}
217bfb50190SJoerg Wunsch 			break;
218bfb50190SJoerg Wunsch 		case MACDEF:
219bfb50190SJoerg Wunsch 			while ((c=getc(cfile)) != EOF &&
220bfb50190SJoerg Wunsch 						(c == ' ' || c == '\t'))
221bfb50190SJoerg Wunsch 				;
222bfb50190SJoerg Wunsch 			if (c == EOF || c == '\n') {
223bfb50190SJoerg Wunsch 				printf("Missing macdef name argument.\n");
224bfb50190SJoerg Wunsch 				goto bad;
225bfb50190SJoerg Wunsch 			}
226bfb50190SJoerg Wunsch 			if (macnum == 16) {
227bfb50190SJoerg Wunsch 				printf("Limit of 16 macros have already been defined\n");
228bfb50190SJoerg Wunsch 				goto bad;
229bfb50190SJoerg Wunsch 			}
230bfb50190SJoerg Wunsch 			tmp = macros[macnum].mac_name;
231bfb50190SJoerg Wunsch 			*tmp++ = c;
232bfb50190SJoerg Wunsch 			for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
233bfb50190SJoerg Wunsch 			    !isspace(c); ++i) {
234bfb50190SJoerg Wunsch 				*tmp++ = c;
235bfb50190SJoerg Wunsch 			}
236bfb50190SJoerg Wunsch 			if (c == EOF) {
237bfb50190SJoerg Wunsch 				printf("Macro definition missing null line terminator.\n");
238bfb50190SJoerg Wunsch 				goto bad;
239bfb50190SJoerg Wunsch 			}
240bfb50190SJoerg Wunsch 			*tmp = '\0';
241bfb50190SJoerg Wunsch 			if (c != '\n') {
242bfb50190SJoerg Wunsch 				while ((c=getc(cfile)) != EOF && c != '\n');
243bfb50190SJoerg Wunsch 			}
244bfb50190SJoerg Wunsch 			if (c == EOF) {
245bfb50190SJoerg Wunsch 				printf("Macro definition missing null line terminator.\n");
246bfb50190SJoerg Wunsch 				goto bad;
247bfb50190SJoerg Wunsch 			}
248bfb50190SJoerg Wunsch 			if (macnum == 0) {
249bfb50190SJoerg Wunsch 				macros[macnum].mac_start = macbuf;
250bfb50190SJoerg Wunsch 			}
251bfb50190SJoerg Wunsch 			else {
252bfb50190SJoerg Wunsch 				macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
253bfb50190SJoerg Wunsch 			}
254bfb50190SJoerg Wunsch 			tmp = macros[macnum].mac_start;
255bfb50190SJoerg Wunsch 			while (tmp != macbuf + 4096) {
256bfb50190SJoerg Wunsch 				if ((c=getc(cfile)) == EOF) {
257bfb50190SJoerg Wunsch 				printf("Macro definition missing null line terminator.\n");
258bfb50190SJoerg Wunsch 					goto bad;
259bfb50190SJoerg Wunsch 				}
260bfb50190SJoerg Wunsch 				*tmp = c;
261bfb50190SJoerg Wunsch 				if (*tmp == '\n') {
262bfb50190SJoerg Wunsch 					if (*(tmp-1) == '\0') {
263bfb50190SJoerg Wunsch 					   macros[macnum++].mac_end = tmp - 1;
264bfb50190SJoerg Wunsch 					   break;
265bfb50190SJoerg Wunsch 					}
266bfb50190SJoerg Wunsch 					*tmp = '\0';
267bfb50190SJoerg Wunsch 				}
268bfb50190SJoerg Wunsch 				tmp++;
269bfb50190SJoerg Wunsch 			}
270bfb50190SJoerg Wunsch 			if (tmp == macbuf + 4096) {
271bfb50190SJoerg Wunsch 				printf("4K macro buffer exceeded\n");
272bfb50190SJoerg Wunsch 				goto bad;
273bfb50190SJoerg Wunsch 			}
274bfb50190SJoerg Wunsch 			break;
275bfb50190SJoerg Wunsch 		default:
276bfb50190SJoerg Wunsch 			warnx("Unknown .netrc keyword %s", tokval);
277bfb50190SJoerg Wunsch 			break;
278bfb50190SJoerg Wunsch 		}
279bfb50190SJoerg Wunsch 		goto done;
280bfb50190SJoerg Wunsch 	}
281bfb50190SJoerg Wunsch done:
282bfb50190SJoerg Wunsch 	(void) fclose(cfile);
283bfb50190SJoerg Wunsch 	return (0);
284bfb50190SJoerg Wunsch bad:
285bfb50190SJoerg Wunsch 	(void) fclose(cfile);
286bfb50190SJoerg Wunsch 	return (-1);
287bfb50190SJoerg Wunsch }
288bfb50190SJoerg Wunsch 
28951295a4dSJordan K. Hubbard int
rexec(char ** ahost,int rport,char * name,char * pass,char * cmd,int * fd2p)290*12bae251SPiotr Pawel Stefaniak rexec(char **ahost, int rport, char *name, char *pass, char *cmd, int *fd2p)
29158f0484fSRodney W. Grimes {
29258f0484fSRodney W. Grimes 	struct sockaddr_in sin, sin2, from;
29358f0484fSRodney W. Grimes 	struct hostent *hp;
29458f0484fSRodney W. Grimes 	u_short port;
29558f0484fSRodney W. Grimes 	int s, timo = 1, s3;
296b9c4b6b4SRoman Divacky 	char c, *acct;
29758f0484fSRodney W. Grimes 
29858f0484fSRodney W. Grimes 	hp = gethostbyname(*ahost);
29950a7cc95SPedro F. Giffuni 	if (hp == NULL) {
30058f0484fSRodney W. Grimes 		herror(*ahost);
30158f0484fSRodney W. Grimes 		return (-1);
30258f0484fSRodney W. Grimes 	}
30358f0484fSRodney W. Grimes 	*ahost = hp->h_name;
304b9c4b6b4SRoman Divacky 	acct = NULL;
305b9c4b6b4SRoman Divacky 	ruserpass(hp->h_name, &name, &pass, &acct);
306b9c4b6b4SRoman Divacky 	free(acct);
30758f0484fSRodney W. Grimes retry:
30858f0484fSRodney W. Grimes 	s = socket(AF_INET, SOCK_STREAM, 0);
30958f0484fSRodney W. Grimes 	if (s < 0) {
31058f0484fSRodney W. Grimes 		perror("rexec: socket");
31158f0484fSRodney W. Grimes 		return (-1);
31258f0484fSRodney W. Grimes 	}
31358f0484fSRodney W. Grimes 	sin.sin_family = hp->h_addrtype;
31458f0484fSRodney W. Grimes 	sin.sin_port = rport;
31558f0484fSRodney W. Grimes 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
31658f0484fSRodney W. Grimes 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
31758f0484fSRodney W. Grimes 		if (errno == ECONNREFUSED && timo <= 16) {
31858f0484fSRodney W. Grimes 			(void) close(s);
31958f0484fSRodney W. Grimes 			sleep(timo);
32058f0484fSRodney W. Grimes 			timo *= 2;
32158f0484fSRodney W. Grimes 			goto retry;
32258f0484fSRodney W. Grimes 		}
32358f0484fSRodney W. Grimes 		perror(hp->h_name);
324d7807d51SDon Lewis 		(void) close(s);
32558f0484fSRodney W. Grimes 		return (-1);
32658f0484fSRodney W. Grimes 	}
32758f0484fSRodney W. Grimes 	port = 0;
32846bf2f53SPedro F. Giffuni 	if (fd2p == 0)
32946bf2f53SPedro F. Giffuni 		(void) write(s, "", 1);
33046bf2f53SPedro F. Giffuni 	else {
33158f0484fSRodney W. Grimes 		char num[8];
33258f0484fSRodney W. Grimes 		int s2, sin2len;
33358f0484fSRodney W. Grimes 
33458f0484fSRodney W. Grimes 		s2 = socket(AF_INET, SOCK_STREAM, 0);
33558f0484fSRodney W. Grimes 		if (s2 < 0) {
33658f0484fSRodney W. Grimes 			(void) close(s);
33758f0484fSRodney W. Grimes 			return (-1);
33858f0484fSRodney W. Grimes 		}
33958f0484fSRodney W. Grimes 		listen(s2, 1);
34058f0484fSRodney W. Grimes 		sin2len = sizeof (sin2);
34158f0484fSRodney W. Grimes 		if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
34258f0484fSRodney W. Grimes 		  sin2len != sizeof (sin2)) {
34358f0484fSRodney W. Grimes 			perror("getsockname");
34458f0484fSRodney W. Grimes 			(void) close(s2);
34558f0484fSRodney W. Grimes 			goto bad;
34658f0484fSRodney W. Grimes 		}
34758f0484fSRodney W. Grimes 		port = ntohs((u_short)sin2.sin_port);
3480ed20b78SPedro F. Giffuni 		(void) sprintf(num, "%hu", port);
34958f0484fSRodney W. Grimes 		(void) write(s, num, strlen(num)+1);
35058f0484fSRodney W. Grimes 		{ int len = sizeof (from);
35158f0484fSRodney W. Grimes 		  s3 = accept(s2, (struct sockaddr *)&from, &len);
35258f0484fSRodney W. Grimes 		  close(s2);
35358f0484fSRodney W. Grimes 		  if (s3 < 0) {
35458f0484fSRodney W. Grimes 			perror("accept");
35558f0484fSRodney W. Grimes 			port = 0;
35658f0484fSRodney W. Grimes 			goto bad;
35758f0484fSRodney W. Grimes 		  }
35858f0484fSRodney W. Grimes 		}
35958f0484fSRodney W. Grimes 		*fd2p = s3;
36058f0484fSRodney W. Grimes 	}
36158f0484fSRodney W. Grimes 	(void) write(s, name, strlen(name) + 1);
36258f0484fSRodney W. Grimes 	/* should public key encypt the password here */
36358f0484fSRodney W. Grimes 	(void) write(s, pass, strlen(pass) + 1);
36458f0484fSRodney W. Grimes 	(void) write(s, cmd, strlen(cmd) + 1);
36558f0484fSRodney W. Grimes 	if (read(s, &c, 1) != 1) {
36658f0484fSRodney W. Grimes 		perror(*ahost);
36758f0484fSRodney W. Grimes 		goto bad;
36858f0484fSRodney W. Grimes 	}
36958f0484fSRodney W. Grimes 	if (c != 0) {
37058f0484fSRodney W. Grimes 		while (read(s, &c, 1) == 1) {
37158f0484fSRodney W. Grimes 			(void) write(2, &c, 1);
37258f0484fSRodney W. Grimes 			if (c == '\n')
37358f0484fSRodney W. Grimes 				break;
37458f0484fSRodney W. Grimes 		}
37558f0484fSRodney W. Grimes 		goto bad;
37658f0484fSRodney W. Grimes 	}
37758f0484fSRodney W. Grimes 	return (s);
37858f0484fSRodney W. Grimes bad:
37958f0484fSRodney W. Grimes 	if (port)
38058f0484fSRodney W. Grimes 		(void) close(*fd2p);
38158f0484fSRodney W. Grimes 	(void) close(s);
38258f0484fSRodney W. Grimes 	return (-1);
38358f0484fSRodney W. Grimes }
384