Annotation of src/usr.bin/rlogin/kcmd.c, Revision 1.6
1.5 millert 1: /* $OpenBSD: kcmd.c,v 1.4 1996/08/18 18:21:24 tholo 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.5 millert 42: static char rcsid[] = "$OpenBSD: kcmd.c,v 1.4 1996/08/18 18:21:24 tholo 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:
54: #include <kerberosIV/des.h>
55: #include <kerberosIV/krb.h>
56: #include <kerberosIV/kparse.h>
57:
58: #include <ctype.h>
59: #include <errno.h>
60: #include <netdb.h>
61: #include <pwd.h>
62: #include <signal.h>
63: #include <stdio.h>
64: #include <stdlib.h>
65: #include <string.h>
66: #include <unistd.h>
67:
68: #ifndef MAXHOSTNAMELEN
69: #define MAXHOSTNAMELEN 64
70: #endif
71:
72: #define START_PORT 5120 /* arbitrary */
73:
74: int
75: kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
76: cred, schedule, msg_data, laddr, faddr, authopts)
77: int *sock;
78: char **ahost;
79: u_short rport;
80: char *locuser, *remuser, *cmd;
81: int *fd2p;
82: KTEXT ticket;
83: char *service;
84: char *realm;
85: CREDENTIALS *cred;
86: Key_schedule schedule;
87: MSG_DAT *msg_data;
88: struct sockaddr_in *laddr, *faddr;
89: long authopts;
90: {
91: int s, timo = 1, pid;
92: long oldmask;
93: struct sockaddr_in sin, from;
94: char c;
95: #ifdef ATHENA_COMPAT
96: int lport = IPPORT_RESERVED - 1;
97: #else
98: int lport = START_PORT;
99: #endif
100: struct hostent *hp;
101: int rc;
102: char *host_save;
103: int status;
104:
105: pid = getpid();
106: hp = gethostbyname(*ahost);
107: if (hp == NULL) {
1.4 tholo 108: herror(*ahost);
1.1 deraadt 109: return (-1);
110: }
1.5 millert 111: if ((host_save = strdup(hp->h_name)) == NULL) {
112: warn("can't allocate memory");
113: return (-1);
114: }
1.1 deraadt 115: *ahost = host_save;
116:
117: /* If realm is null, look up from table */
118: if (realm == NULL || realm[0] == '\0')
119: realm = krb_realmofhost(host_save);
120:
121: oldmask = sigblock(sigmask(SIGURG));
122: for (;;) {
1.4 tholo 123: s = rresvport(&lport);
1.1 deraadt 124: if (s < 0) {
125: if (errno == EAGAIN)
126: fprintf(stderr,
127: "kcmd(socket): All ports in use\n");
128: else
129: perror("kcmd: socket");
130: sigsetmask(oldmask);
131: return (-1);
132: }
133: fcntl(s, F_SETOWN, pid);
1.4 tholo 134: bzero(&sin, sizeof sin);
135: sin.sin_len = sizeof(struct sockaddr_in);
1.1 deraadt 136: sin.sin_family = hp->h_addrtype;
137: sin.sin_port = rport;
1.4 tholo 138: bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
1.1 deraadt 139: if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
140: break;
141: (void) close(s);
142: if (errno == EADDRINUSE) {
143: lport--;
144: continue;
145: }
146: /*
147: * don't wait very long for Kerberos rcmd.
148: */
149: if (errno == ECONNREFUSED && timo <= 4) {
150: /* sleep(timo); don't wait at all here */
151: timo *= 2;
152: continue;
153: }
154: if (hp->h_addr_list[1] != NULL) {
155: int oerrno = errno;
156:
157: fprintf(stderr,
158: "kcmd: connect to address %s: ",
159: inet_ntoa(sin.sin_addr));
160: errno = oerrno;
161: perror(NULL);
162: hp->h_addr_list++;
1.4 tholo 163: bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
1.1 deraadt 164: fprintf(stderr, "Trying %s...\n",
165: inet_ntoa(sin.sin_addr));
166: continue;
167: }
168: if (errno != ECONNREFUSED)
169: perror(hp->h_name);
170: sigsetmask(oldmask);
171: return (-1);
172: }
173: if (fd2p == 0) {
174: write(s, "", 1);
175: lport = 0;
176: } else {
177: char num[8];
1.4 tholo 178: int s2 = rresvport(&lport), s3;
1.1 deraadt 179: int len = sizeof(from);
180:
181: if (s2 < 0) {
182: status = -1;
183: goto bad;
184: }
185: listen(s2, 1);
1.4 tholo 186: (void) snprintf(num, sizeof(num), "%d", lport);
1.1 deraadt 187: if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
188: perror("kcmd(write): setting up stderr");
189: (void) close(s2);
190: status = -1;
191: goto bad;
192: }
1.4 tholo 193: again:
194: s3 = accept(s2, (struct sockaddr *)&from, &len);
195: /*
196: * XXX careful for ftp bounce attacks. If discovered, shut them
197: * down and check for the real auxiliary channel to connect.
198: */
199: if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
200: (void) close(s3);
201: goto again;
202: }
1.1 deraadt 203: (void) close(s2);
204: if (s3 < 0) {
205: perror("kcmd:accept");
206: lport = 0;
207: status = -1;
208: goto bad;
209: }
210: *fd2p = s3;
1.4 tholo 211: from.sin_port = ntohs(from.sin_port);
1.1 deraadt 212: if (from.sin_family != AF_INET ||
1.4 tholo 213: from.sin_port >= IPPORT_RESERVED ||
214: from.sin_port < IPPORT_RESERVED / 2) {
1.1 deraadt 215: fprintf(stderr,
216: "kcmd(socket): protocol failure in circuit setup.\n");
217: status = -1;
218: goto bad2;
219: }
220: }
221: /*
222: * Kerberos-authenticated service. Don't have to send locuser,
223: * since its already in the ticket, and we'll extract it on
224: * the other side.
225: */
226: /* (void) write(s, locuser, strlen(locuser)+1); */
227:
228: /* set up the needed stuff for mutual auth, but only if necessary */
229: if (authopts & KOPT_DO_MUTUAL) {
230: int sin_len;
231: *faddr = sin;
232:
233: sin_len = sizeof(struct sockaddr_in);
234: if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
235: perror("kcmd(getsockname)");
236: status = -1;
237: goto bad2;
238: }
239: }
240: if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
241: realm, (unsigned long) getpid(), msg_data,
242: cred, schedule,
243: laddr,
244: faddr,
245: "KCMDV0.1")) != KSUCCESS)
246: goto bad2;
247:
248: (void) write(s, remuser, strlen(remuser)+1);
249: (void) write(s, cmd, strlen(cmd)+1);
250:
251: if ((rc = read(s, &c, 1)) != 1) {
252: if (rc == -1)
253: perror(*ahost);
254: else
255: fprintf(stderr,"kcmd: bad connection with remote host\n");
256: status = -1;
257: goto bad2;
258: }
259: if (c != '\0') {
260: while (read(s, &c, 1) == 1) {
261: (void) write(2, &c, 1);
262: if (c == '\n')
263: break;
264: }
265: status = -1;
266: goto bad2;
267: }
268: sigsetmask(oldmask);
269: *sock = s;
270: return (KSUCCESS);
271: bad2:
272: if (lport)
273: (void) close(*fd2p);
274: bad:
275: (void) close(s);
276: sigsetmask(oldmask);
277: return (status);
278: }