POSIX signals
[unix-history] / usr / src / usr.bin / rlogin / kcmd.c
CommitLineData
ed878ac6
KF
1/*
2 * $Source: /mit/kerberos/src/appl/bsd/RCS/kcmd.c,v $
3 * $Header: kcmd.c,v 4.16 89/05/17 10:54:31 jtkohl Exp $
4 */
5
6#ifndef lint
7static char *rcsid_kcmd_c =
8"$Header: kcmd.c,v 4.16 89/05/17 10:54:31 jtkohl Exp $";
9#endif lint
10#define LIBC_SCCS
11
12/*
13 * Copyright (c) 1983 Regents of the University of California.
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms are permitted
17 * provided that the above copyright notice and this paragraph are
18 * duplicated in all such forms and that any documentation,
19 * advertising materials, and other materials related to such
20 * distribution and use acknowledge that the software was developed
21 * by the University of California, Berkeley. The name of the
22 * University may not be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
26 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 */
28
29#if defined(LIBC_SCCS) && !defined(lint)
30static char sccsid[] = "derived from @(#)rcmd.c 5.17 (Berkeley) 6/27/88";
31#endif /* LIBC_SCCS and not lint */
32
33#include <stdio.h>
34#include <ctype.h>
35#include <pwd.h>
36#include <sys/param.h>
37#include <sys/file.h>
38#include <sys/signal.h>
39#include <sys/socket.h>
40#include <sys/stat.h>
41
42#include <netinet/in.h>
43
44#include <netdb.h>
45#include <errno.h>
46#include <krb.h>
47#include <kparse.h>
48
49#ifndef MAXHOSTNAMELEN
50#define MAXHOSTNAMELEN 64
51#endif
52
53extern errno;
54char *index(), *malloc(), *krb_realmofhost();
55
56#define START_PORT 5120 /* arbitrary */
57
58kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, ticket, service, realm,
59 cred, schedule, msg_data, laddr, faddr, authopts)
60int *sock;
61char **ahost;
62u_short rport;
63char *locuser, *remuser, *cmd;
64int *fd2p;
65KTEXT ticket;
66char *service;
67char *realm;
68CREDENTIALS *cred;
69Key_schedule schedule;
70MSG_DAT *msg_data;
71struct sockaddr_in *laddr, *faddr;
72long authopts;
73{
74 int s, timo = 1, pid;
75 long oldmask;
76 struct sockaddr_in sin, from;
77 char c;
78#ifdef ATHENA_COMPAT
79 int lport = IPPORT_RESERVED - 1;
80#else
81 int lport = START_PORT;
82#endif ATHENA_COMPAT
83 struct hostent *hp;
84 int rc;
85 char *host_save;
86 int status;
87
88 pid = getpid();
89 hp = gethostbyname(*ahost);
90 if (hp == 0) {
91 /* fprintf(stderr, "%s: unknown host\n", *ahost); */
92 return (-1);
93 }
94
95 host_save = malloc(strlen(hp->h_name) + 1);
96 strcpy(host_save, hp->h_name);
97 *ahost = host_save;
98
99 /* If realm is null, look up from table */
100 if ((realm == NULL) || (realm[0] == '\0')) {
101 realm = krb_realmofhost(host_save);
102 }
103
104 oldmask = sigblock(sigmask(SIGURG));
105 for (;;) {
106 s = getport(&lport);
107 if (s < 0) {
108 if (errno == EAGAIN)
5b56bddb
KF
109 fprintf(stderr,
110 "kcmd(socket): All ports in use\n");
ed878ac6 111 else
5b56bddb 112 perror("kcmd: socket");
ed878ac6
KF
113 sigsetmask(oldmask);
114 return (-1);
115 }
116 fcntl(s, F_SETOWN, pid);
117 sin.sin_family = hp->h_addrtype;
118#if defined(ultrix) || defined(sun)
119 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
120#else
121 bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
122#endif /* defined(ultrix) || defined(sun) */
123 sin.sin_port = rport;
124 if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
125 break;
126 (void) close(s);
127 if (errno == EADDRINUSE) {
128 lport--;
129 continue;
130 }
131 /*
132 * don't wait very long for Kerberos rcmd.
133 */
134 if (errno == ECONNREFUSED && timo <= 4) {
5b56bddb 135 /* sleep(timo); don't wait at all here */
ed878ac6
KF
136 timo *= 2;
137 continue;
138 }
139#if !(defined(ultrix) || defined(sun))
140 if (hp->h_addr_list[1] != NULL) {
141 int oerrno = errno;
142
143 fprintf(stderr,
5b56bddb
KF
144 "kcmd: connect to address %s: ",
145 inet_ntoa(sin.sin_addr));
ed878ac6
KF
146 errno = oerrno;
147 perror(0);
148 hp->h_addr_list++;
149 bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
150 hp->h_length);
151 fprintf(stderr, "Trying %s...\n",
152 inet_ntoa(sin.sin_addr));
153 continue;
154 }
155#endif /* !(defined(ultrix) || defined(sun)) */
156 if (errno != ECONNREFUSED)
157 perror(hp->h_name);
158 sigsetmask(oldmask);
159 return (-1);
160 }
161 lport--;
162 if (fd2p == 0) {
163 write(s, "", 1);
164 lport = 0;
165 } else {
166 char num[8];
167 int s2 = getport(&lport), s3;
168 int len = sizeof (from);
169
170 if (s2 < 0) {
171 status = -1;
172 goto bad;
173 }
174 listen(s2, 1);
175 (void) sprintf(num, "%d", lport);
176 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
5b56bddb 177 perror("kcmd(write): setting up stderr");
ed878ac6
KF
178 (void) close(s2);
179 status = -1;
180 goto bad;
181 }
182 s3 = accept(s2, (struct sockaddr *)&from, &len);
183 (void) close(s2);
184 if (s3 < 0) {
5b56bddb 185 perror("kcmd:accept");
ed878ac6
KF
186 lport = 0;
187 status = -1;
188 goto bad;
189 }
190 *fd2p = s3;
191 from.sin_port = ntohs((u_short)from.sin_port);
192 if (from.sin_family != AF_INET ||
193 from.sin_port >= IPPORT_RESERVED) {
194 fprintf(stderr,
5b56bddb 195 "kcmd(socket): protocol failure in circuit setup.\n");
ed878ac6
KF
196 goto bad2;
197 }
198 }
199 /*
200 * Kerberos-authenticated service. Don't have to send locuser,
201 * since its already in the ticket, and we'll extract it on
202 * the other side.
203 */
204 /* (void) write(s, locuser, strlen(locuser)+1); */
205
206 /* set up the needed stuff for mutual auth, but only if necessary */
207 if (authopts & KOPT_DO_MUTUAL) {
208 int sin_len;
209 *faddr = sin;
210
211 sin_len = sizeof (struct sockaddr_in);
212 if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
5b56bddb 213 perror("kcmd(getsockname)");
ed878ac6
KF
214 status = -1;
215 goto bad2;
216 }
217 }
218 if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
219 realm, (unsigned long) getpid(), msg_data,
220 cred, schedule,
221 laddr,
222 faddr,
223 "KCMDV0.1")) != KSUCCESS)
224 goto bad2;
225
226 (void) write(s, remuser, strlen(remuser)+1);
227 (void) write(s, cmd, strlen(cmd)+1);
228
229 if ((rc=read(s, &c, 1)) != 1) {
230 if (rc==-1) {
231 perror(*ahost);
232 } else {
5b56bddb 233 fprintf(stderr,"kcmd: bad connection with remote host\n");
ed878ac6
KF
234 }
235 status = -1;
236 goto bad2;
237 }
238 if (c != 0) {
239 while (read(s, &c, 1) == 1) {
240 (void) write(2, &c, 1);
241 if (c == '\n')
242 break;
243 }
244 status = -1;
245 goto bad2;
246 }
247 sigsetmask(oldmask);
248 *sock = s;
249 return (KSUCCESS);
250bad2:
251 if (lport)
252 (void) close(*fd2p);
253bad:
254 (void) close(s);
255 sigsetmask(oldmask);
256 return (status);
257}
258
259getport(alport)
260 int *alport;
261{
262 struct sockaddr_in sin;
263 int s;
264
265 sin.sin_family = AF_INET;
266 sin.sin_addr.s_addr = INADDR_ANY;
267 s = socket(AF_INET, SOCK_STREAM, 0);
268 if (s < 0)
269 return (-1);
270 for (;;) {
271 sin.sin_port = htons((u_short)*alport);
272 if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
273 return (s);
274 if (errno != EADDRINUSE) {
275 (void) close(s);
276 return (-1);
277 }
278 (*alport)--;
279#ifdef ATHENA_COMPAT
280 if (*alport == IPPORT_RESERVED/2) {
281#else
282 if (*alport == IPPORT_RESERVED) {
283#endif ATHENA_COMPAT
284 (void) close(s);
285 errno = EAGAIN; /* close */
286 return (-1);
287 }
288 }
289}
290