Annotation of src/usr.bin/rlogin/kcmd.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: kcmd.c,v 1.10 1998/03/25 19:51:05 art Exp $ */
1.1 deraadt 2: /* $NetBSD: kcmd.c,v 1.2 1995/03/21 07:58:32 cgd Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char Xsccsid[] = "derived from @(#)rcmd.c 5.17 (Berkeley) 6/27/88";
40: static char sccsid[] = "@(#)kcmd.c 8.2 (Berkeley) 8/19/93";
41: #else
1.11 ! deraadt 42: static char rcsid[] = "$OpenBSD: kcmd.c,v 1.10 1998/03/25 19:51:05 art Exp $";
1.1 deraadt 43: #endif
44: #endif /* not lint */
45:
46: #include <sys/param.h>
47: #include <sys/file.h>
48: #include <sys/socket.h>
49: #include <sys/stat.h>
50:
51: #include <netinet/in.h>
52: #include <arpa/inet.h>
53:
1.9 provos 54: #include <des.h>
1.1 deraadt 55: #include <kerberosIV/krb.h>
56:
57: #include <ctype.h>
58: #include <errno.h>
59: #include <netdb.h>
60: #include <pwd.h>
61: #include <signal.h>
62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
65: #include <unistd.h>
1.10 art 66:
67: #include <err.h>
1.1 deraadt 68:
69: #ifndef MAXHOSTNAMELEN
70: #define MAXHOSTNAMELEN 64
71: #endif
72:
73: #define START_PORT 5120 /* arbitrary */
74:
1.7 rees 75: int getport __P((int *));
76:
1.1 deraadt 77: int
78: kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
79: cred, schedule, msg_data, laddr, faddr, authopts)
80: int *sock;
81: char **ahost;
82: u_short rport;
83: char *locuser, *remuser, *cmd;
84: int *fd2p;
85: KTEXT ticket;
86: char *service;
87: char *realm;
88: CREDENTIALS *cred;
89: Key_schedule schedule;
90: MSG_DAT *msg_data;
91: struct sockaddr_in *laddr, *faddr;
92: long authopts;
93: {
94: int s, timo = 1, pid;
1.8 deraadt 95: int oldmask;
1.1 deraadt 96: struct sockaddr_in sin, from;
97: char c;
98: int lport = IPPORT_RESERVED - 1;
99: struct hostent *hp;
100: int rc;
101: char *host_save;
102: int status;
103:
104: pid = getpid();
105: hp = gethostbyname(*ahost);
106: if (hp == NULL) {
1.4 tholo 107: herror(*ahost);
1.1 deraadt 108: return (-1);
109: }
1.5 millert 110: if ((host_save = strdup(hp->h_name)) == NULL) {
111: warn("can't allocate memory");
112: return (-1);
113: }
1.1 deraadt 114: *ahost = host_save;
115:
116: /* If realm is null, look up from table */
117: if (realm == NULL || realm[0] == '\0')
118: realm = krb_realmofhost(host_save);
119:
120: oldmask = sigblock(sigmask(SIGURG));
121: for (;;) {
1.7 rees 122: s = getport(&lport);
1.1 deraadt 123: if (s < 0) {
124: if (errno == EAGAIN)
125: fprintf(stderr,
126: "kcmd(socket): All ports in use\n");
127: else
128: perror("kcmd: socket");
129: sigsetmask(oldmask);
130: return (-1);
131: }
132: fcntl(s, F_SETOWN, pid);
1.4 tholo 133: bzero(&sin, sizeof sin);
134: sin.sin_len = sizeof(struct sockaddr_in);
1.1 deraadt 135: sin.sin_family = hp->h_addrtype;
136: sin.sin_port = rport;
1.4 tholo 137: bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
1.1 deraadt 138: if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
139: break;
140: (void) close(s);
141: if (errno == EADDRINUSE) {
142: lport--;
143: continue;
144: }
145: /*
146: * don't wait very long for Kerberos rcmd.
147: */
148: if (errno == ECONNREFUSED && timo <= 4) {
149: /* sleep(timo); don't wait at all here */
150: timo *= 2;
151: continue;
152: }
153: if (hp->h_addr_list[1] != NULL) {
154: int oerrno = errno;
155:
156: fprintf(stderr,
157: "kcmd: connect to address %s: ",
158: inet_ntoa(sin.sin_addr));
159: errno = oerrno;
160: perror(NULL);
161: hp->h_addr_list++;
1.4 tholo 162: bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
1.1 deraadt 163: fprintf(stderr, "Trying %s...\n",
164: inet_ntoa(sin.sin_addr));
165: continue;
166: }
167: if (errno != ECONNREFUSED)
168: perror(hp->h_name);
169: sigsetmask(oldmask);
170: return (-1);
171: }
172: if (fd2p == 0) {
173: write(s, "", 1);
174: lport = 0;
175: } else {
176: char num[8];
1.7 rees 177: int s2 = getport(&lport), s3;
1.1 deraadt 178: int len = sizeof(from);
179:
180: if (s2 < 0) {
181: status = -1;
182: goto bad;
183: }
184: listen(s2, 1);
1.4 tholo 185: (void) snprintf(num, sizeof(num), "%d", lport);
1.1 deraadt 186: if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
187: perror("kcmd(write): setting up stderr");
188: (void) close(s2);
189: status = -1;
190: goto bad;
191: }
1.4 tholo 192: again:
193: s3 = accept(s2, (struct sockaddr *)&from, &len);
194: /*
195: * XXX careful for ftp bounce attacks. If discovered, shut them
196: * down and check for the real auxiliary channel to connect.
197: */
198: if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
199: (void) close(s3);
200: goto again;
201: }
1.1 deraadt 202: (void) close(s2);
203: if (s3 < 0) {
204: perror("kcmd:accept");
205: lport = 0;
206: status = -1;
207: goto bad;
208: }
209: *fd2p = s3;
1.4 tholo 210: from.sin_port = ntohs(from.sin_port);
1.1 deraadt 211: if (from.sin_family != AF_INET ||
1.4 tholo 212: from.sin_port >= IPPORT_RESERVED ||
213: from.sin_port < IPPORT_RESERVED / 2) {
1.1 deraadt 214: fprintf(stderr,
215: "kcmd(socket): protocol failure in circuit setup.\n");
216: status = -1;
217: goto bad2;
218: }
219: }
220: /*
221: * Kerberos-authenticated service. Don't have to send locuser,
222: * since its already in the ticket, and we'll extract it on
223: * the other side.
224: */
225: /* (void) write(s, locuser, strlen(locuser)+1); */
226:
227: /* set up the needed stuff for mutual auth, but only if necessary */
228: if (authopts & KOPT_DO_MUTUAL) {
229: int sin_len;
230: *faddr = sin;
231:
232: sin_len = sizeof(struct sockaddr_in);
233: if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
234: perror("kcmd(getsockname)");
235: status = -1;
236: goto bad2;
237: }
238: }
239: if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
240: realm, (unsigned long) getpid(), msg_data,
241: cred, schedule,
242: laddr,
243: faddr,
244: "KCMDV0.1")) != KSUCCESS)
245: goto bad2;
246:
247: (void) write(s, remuser, strlen(remuser)+1);
248: (void) write(s, cmd, strlen(cmd)+1);
249:
250: if ((rc = read(s, &c, 1)) != 1) {
251: if (rc == -1)
252: perror(*ahost);
253: else
254: fprintf(stderr,"kcmd: bad connection with remote host\n");
255: status = -1;
256: goto bad2;
257: }
258: if (c != '\0') {
259: while (read(s, &c, 1) == 1) {
260: (void) write(2, &c, 1);
261: if (c == '\n')
262: break;
263: }
264: status = -1;
265: goto bad2;
266: }
267: sigsetmask(oldmask);
268: *sock = s;
269: return (KSUCCESS);
270: bad2:
271: if (lport)
272: (void) close(*fd2p);
273: bad:
274: (void) close(s);
275: sigsetmask(oldmask);
276: return (status);
1.7 rees 277: }
278:
279: int
280: getport(alport)
281: int *alport;
282: {
283: struct sockaddr_in sin;
284: int s;
285:
286: /* First try to get a "reserved" [sic] port, for interoperability with
287: broken klogind (aix, e.g.) */
288:
289: s = rresvport(alport);
290: if (s >= 0)
291: return s;
292:
293: /* Failed; if EACCES, we're not root, so just get an unreserved port
294: and hope that's good enough */
295:
296: if (errno != EACCES)
297: return -1;
298:
299: if (*alport < IPPORT_RESERVED)
300: *alport = START_PORT;
301: sin.sin_family = AF_INET;
302: sin.sin_addr.s_addr = INADDR_ANY;
303: s = socket(AF_INET, SOCK_STREAM, 0);
304: if (s < 0)
305: return (-1);
306: for (;;) {
307: sin.sin_port = htons((u_short)*alport);
308: if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
309: return (s);
310: if (errno != EADDRINUSE) {
311: (void) close(s);
312: return (-1);
313: }
314: (*alport)--;
315: if (*alport == IPPORT_RESERVED) {
316: (void) close(s);
317: errno = EAGAIN; /* close */
318: return (-1);
319: }
320: }
1.1 deraadt 321: }