Annotation of src/usr.bin/rsh/kcmd.c, Revision 1.3
1.3 ! mpech 1: /* $OpenBSD: kcmd.c,v 1.2 2002/06/02 19:06:39 deraadt 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.3 ! mpech 42: static char rcsid[] = "$OpenBSD: kcmd.c,v 1.2 2002/06/02 19:06:39 deraadt 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 <des.h>
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>
66:
67: #include <err.h>
68:
69: #ifndef MAXHOSTNAMELEN
70: #define MAXHOSTNAMELEN 64
71: #endif
72:
73: #define START_PORT 5120 /* arbitrary */
74:
75: int getport(int *);
76: int kcmd(int *, char **, u_short, char *, char *, char *,
77: int *, KTEXT, char *, char *, CREDENTIALS *,
78: Key_schedule, MSG_DAT *, struct sockaddr_in *,
79: struct sockaddr_in *, long);
80:
81: int
82: kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
83: cred, schedule, msg_data, laddr, faddr, authopts)
84: int *sock;
85: char **ahost;
86: u_short rport;
87: char *locuser, *remuser, *cmd;
88: int *fd2p;
89: KTEXT ticket;
90: char *service;
91: char *realm;
92: CREDENTIALS *cred;
93: Key_schedule schedule;
94: MSG_DAT *msg_data;
95: struct sockaddr_in *laddr, *faddr;
96: long authopts;
97: {
1.3 ! mpech 98: int s, timo = 1;
1.1 deraadt 99: sigset_t mask, oldmask;
100: struct sockaddr_in sin, from;
101: char c;
102: int lport = IPPORT_RESERVED - 1;
103: struct hostent *hp;
104: int rc;
105: char *host_save;
106: int status;
1.3 ! mpech 107: pid_t pid;
1.1 deraadt 108:
109: pid = getpid();
110: hp = gethostbyname(*ahost);
111: if (hp == NULL) {
112: herror(*ahost);
113: return (-1);
114: }
115: if ((host_save = strdup(hp->h_name)) == NULL) {
116: warn("can't allocate memory");
117: return (-1);
118: }
119: *ahost = host_save;
120:
121: /* If realm is null, look up from table */
122: if (realm == NULL || realm[0] == '\0')
123: realm = krb_realmofhost(host_save);
124:
125: sigemptyset(&mask);
126: sigaddset(&mask, SIGURG);
127: sigprocmask(SIG_BLOCK, &mask, &oldmask);
128: for (;;) {
129: s = getport(&lport);
130: if (s < 0) {
131: if (errno == EAGAIN)
132: fprintf(stderr,
133: "kcmd(socket): All ports in use\n");
134: else
135: perror("kcmd: socket");
136: sigprocmask(SIG_SETMASK, &oldmask, NULL);
137: return (-1);
138: }
139: fcntl(s, F_SETOWN, pid);
140: bzero(&sin, sizeof sin);
141: sin.sin_len = sizeof(struct sockaddr_in);
142: sin.sin_family = hp->h_addrtype;
143: sin.sin_port = rport;
144: bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
145: if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
146: break;
147: (void) close(s);
148: if (errno == EADDRINUSE) {
149: lport--;
150: continue;
151: }
152: /*
153: * don't wait very long for Kerberos rcmd.
154: */
155: if (errno == ECONNREFUSED && timo <= 4) {
156: /* sleep(timo); don't wait at all here */
157: timo *= 2;
158: continue;
159: }
160: if (hp->h_addr_list[1] != NULL) {
161: int oerrno = errno;
162:
163: fprintf(stderr,
164: "kcmd: connect to address %s: ",
165: inet_ntoa(sin.sin_addr));
166: errno = oerrno;
167: perror(NULL);
168: hp->h_addr_list++;
169: bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
170: fprintf(stderr, "Trying %s...\n",
171: inet_ntoa(sin.sin_addr));
172: continue;
173: }
174: if (errno != ECONNREFUSED)
175: perror(hp->h_name);
176: sigprocmask(SIG_SETMASK, &oldmask, NULL);
177: return (-1);
178: }
179: if (fd2p == 0) {
180: write(s, "", 1);
181: lport = 0;
182: } else {
183: char num[8];
184: int s2 = getport(&lport), s3;
185: int len = sizeof(from);
186:
187: if (s2 < 0) {
188: status = -1;
189: goto bad;
190: }
191: listen(s2, 1);
192: (void) snprintf(num, sizeof(num), "%d", lport);
193: if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
194: perror("kcmd(write): setting up stderr");
195: (void) close(s2);
196: status = -1;
197: goto bad;
198: }
199: again:
200: s3 = accept(s2, (struct sockaddr *)&from, &len);
201: /*
202: * XXX careful for ftp bounce attacks. If discovered, shut them
203: * down and check for the real auxiliary channel to connect.
204: */
205: if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
206: (void) close(s3);
207: goto again;
208: }
209: (void) close(s2);
210: if (s3 < 0) {
211: perror("kcmd:accept");
212: lport = 0;
213: status = -1;
214: goto bad;
215: }
216: *fd2p = s3;
217: from.sin_port = ntohs(from.sin_port);
218: if (from.sin_family != AF_INET ||
219: from.sin_port >= IPPORT_RESERVED ||
220: from.sin_port < IPPORT_RESERVED / 2) {
221: fprintf(stderr,
222: "kcmd(socket): protocol failure in circuit setup.\n");
223: status = -1;
224: goto bad2;
225: }
226: }
227: /*
228: * Kerberos-authenticated service. Don't have to send locuser,
229: * since its already in the ticket, and we'll extract it on
230: * the other side.
231: */
232: /* (void) write(s, locuser, strlen(locuser)+1); */
233:
234: /* set up the needed stuff for mutual auth, but only if necessary */
235: if (authopts & KOPT_DO_MUTUAL) {
236: int sin_len;
237: *faddr = sin;
238:
239: sin_len = sizeof(struct sockaddr_in);
240: if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
241: perror("kcmd(getsockname)");
242: status = -1;
243: goto bad2;
244: }
245: }
246: if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
247: realm, (unsigned long) getpid(), msg_data,
248: cred, schedule,
249: laddr,
250: faddr,
251: "KCMDV0.1")) != KSUCCESS)
252: goto bad2;
253:
254: (void) write(s, remuser, strlen(remuser)+1);
255: (void) write(s, cmd, strlen(cmd)+1);
256:
257: if ((rc = read(s, &c, 1)) != 1) {
258: if (rc == -1)
259: perror(*ahost);
260: else
261: fprintf(stderr,"kcmd: bad connection with remote host\n");
262: status = -1;
263: goto bad2;
264: }
265: if (c != '\0') {
266: while (read(s, &c, 1) == 1) {
267: (void) write(2, &c, 1);
268: if (c == '\n')
269: break;
270: }
271: status = -1;
272: goto bad2;
273: }
274: sigprocmask(SIG_SETMASK, &oldmask, NULL);
275: *sock = s;
276: return (KSUCCESS);
277: bad2:
278: if (lport)
279: (void) close(*fd2p);
280: bad:
281: (void) close(s);
282: sigprocmask(SIG_SETMASK, &oldmask, NULL);
283: return (status);
284: }
285:
286: int
287: getport(alport)
288: int *alport;
289: {
290: struct sockaddr_in sin;
291: int s;
292:
293: /* First try to get a "reserved" [sic] port, for interoperability with
294: broken klogind (aix, e.g.) */
295:
296: s = rresvport(alport);
297: if (s >= 0)
298: return s;
299:
300: /* Failed; if EACCES, we're not root, so just get an unreserved port
301: and hope that's good enough */
302:
303: if (errno != EACCES)
304: return -1;
305:
306: if (*alport < IPPORT_RESERVED)
307: *alport = START_PORT;
1.2 deraadt 308: memset(&sin, 0, sizeof sin);
1.1 deraadt 309: sin.sin_family = AF_INET;
310: sin.sin_addr.s_addr = INADDR_ANY;
311: s = socket(AF_INET, SOCK_STREAM, 0);
312: if (s < 0)
313: return (-1);
314: for (;;) {
315: sin.sin_port = htons((u_short)*alport);
316: if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
317: return (s);
318: if (errno != EADDRINUSE) {
319: (void) close(s);
320: return (-1);
321: }
322: (*alport)--;
323: if (*alport == IPPORT_RESERVED) {
324: (void) close(s);
325: errno = EAGAIN; /* close */
326: return (-1);
327: }
328: }
329: }