]>
git.saurik.com Git - apple/network_cmds.git/blob - rlogin.tproj/kcmd.c
0f8e6e3eada2414bf7ef453593a1eaf31d39b834
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #if !defined(__APPLE__) || defined(KERBEROS)
60 #include <sys/param.h>
62 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
68 #include <kerberosIV/des.h>
69 #include <kerberosIV/krb.h>
70 #include <kerberosIV/kparse.h>
84 #ifndef MAXHOSTNAMELEN
85 #define MAXHOSTNAMELEN 64
88 #define START_PORT 5120 /* arbitrary */
90 int getport
__P((int *));
93 kcmd(sock
, ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, ticket
, service
, realm
,
94 cred
, schedule
, msg_data
, laddr
, faddr
, authopts
)
98 char *locuser
, *remuser
, *cmd
;
104 Key_schedule schedule
;
106 struct sockaddr_in
*laddr
, *faddr
;
109 int s
, timo
= 1, pid
;
111 struct sockaddr_in sin
, from
;
114 int lport
= IPPORT_RESERVED
- 1;
116 int lport
= START_PORT
;
124 hp
= gethostbyname(*ahost
);
126 /* fprintf(stderr, "%s: unknown host\n", *ahost); */
130 host_save
= malloc(strlen(hp
->h_name
) + 1);
131 strcpy(host_save
, hp
->h_name
);
135 /* If realm is null, look up from table */
136 if (realm
== NULL
|| realm
[0] == '\0')
137 realm
= krb_realmofhost(host_save
);
138 #endif /* KERBEROS */
140 oldmask
= sigblock(sigmask(SIGURG
));
146 "kcmd(socket): All ports in use\n");
148 perror("kcmd: socket");
152 fcntl(s
, F_SETOWN
, pid
);
153 sin
.sin_family
= hp
->h_addrtype
;
154 #if defined(ultrix) || defined(sun)
155 bcopy(hp
->h_addr
, (caddr_t
)&sin
.sin_addr
, hp
->h_length
);
157 bcopy(hp
->h_addr_list
[0], (caddr_t
)&sin
.sin_addr
, hp
->h_length
);
159 sin
.sin_port
= rport
;
160 if (connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
163 if (errno
== EADDRINUSE
) {
168 * don't wait very long for Kerberos rcmd.
170 if (errno
== ECONNREFUSED
&& timo
<= 4) {
171 /* sleep(timo); don't wait at all here */
175 #if !(defined(ultrix) || defined(sun))
176 if (hp
->h_addr_list
[1] != NULL
) {
180 "kcmd: connect to address %s: ",
181 inet_ntoa(sin
.sin_addr
));
185 bcopy(hp
->h_addr_list
[0], (caddr_t
)&sin
.sin_addr
,
187 fprintf(stderr
, "Trying %s...\n",
188 inet_ntoa(sin
.sin_addr
));
191 #endif /* !(defined(ultrix) || defined(sun)) */
192 if (errno
!= ECONNREFUSED
)
203 int s2
= getport(&lport
), s3
;
204 int len
= sizeof(from
);
211 (void) sprintf(num
, "%d", lport
);
212 if (write(s
, num
, strlen(num
) + 1) != strlen(num
) + 1) {
213 perror("kcmd(write): setting up stderr");
218 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
221 perror("kcmd:accept");
227 from
.sin_port
= ntohs((u_short
)from
.sin_port
);
228 if (from
.sin_family
!= AF_INET
||
229 from
.sin_port
>= IPPORT_RESERVED
) {
231 "kcmd(socket): protocol failure in circuit setup.\n");
237 * Kerberos-authenticated service. Don't have to send locuser,
238 * since its already in the ticket, and we'll extract it on
241 /* (void) write(s, locuser, strlen(locuser)+1); */
243 /* set up the needed stuff for mutual auth, but only if necessary */
244 if (authopts
& KOPT_DO_MUTUAL
) {
248 sin_len
= sizeof(struct sockaddr_in
);
249 if (getsockname(s
, (struct sockaddr
*)laddr
, &sin_len
) < 0) {
250 perror("kcmd(getsockname)");
256 if ((status
= krb_sendauth(authopts
, s
, ticket
, service
, *ahost
,
257 realm
, (unsigned long) getpid(), msg_data
,
261 "KCMDV0.1")) != KSUCCESS
)
263 #endif /* KERBEROS */
265 (void) write(s
, remuser
, strlen(remuser
)+1);
266 (void) write(s
, cmd
, strlen(cmd
)+1);
268 if ((rc
= read(s
, &c
, 1)) != 1) {
272 fprintf(stderr
,"kcmd: bad connection with remote host\n");
277 while (read(s
, &c
, 1) == 1) {
278 (void) write(2, &c
, 1);
301 struct sockaddr_in sin
;
304 sin
.sin_family
= AF_INET
;
305 sin
.sin_addr
.s_addr
= INADDR_ANY
;
306 s
= socket(AF_INET
, SOCK_STREAM
, 0);
310 sin
.sin_port
= htons((u_short
)*alport
);
311 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
313 if (errno
!= EADDRINUSE
) {
319 if (*alport
== IPPORT_RESERVED
/2) {
321 if (*alport
== IPPORT_RESERVED
) {
324 errno
= EAGAIN
; /* close */
330 #endif /* !NeXT || KERBEROS */