]>
git.saurik.com Git - apple/network_cmds.git/blob - rlogin.tproj/kcmd.c
1482dda77eb6be6d30a99689ad615a3b624084f5
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #if !defined(__APPLE__) || defined(KERBEROS)
59 #include <sys/param.h>
61 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
67 #include <kerberosIV/des.h>
68 #include <kerberosIV/krb.h>
69 #include <kerberosIV/kparse.h>
83 #ifndef MAXHOSTNAMELEN
84 #define MAXHOSTNAMELEN 64
87 #define START_PORT 5120 /* arbitrary */
89 int getport
__P((int *));
92 kcmd(sock
, ahost
, rport
, locuser
, remuser
, cmd
, fd2p
, ticket
, service
, realm
,
93 cred
, schedule
, msg_data
, laddr
, faddr
, authopts
)
97 char *locuser
, *remuser
, *cmd
;
103 Key_schedule schedule
;
105 struct sockaddr_in
*laddr
, *faddr
;
108 int s
, timo
= 1, pid
;
110 struct sockaddr_in sin
, from
;
113 int lport
= IPPORT_RESERVED
- 1;
115 int lport
= START_PORT
;
123 hp
= gethostbyname(*ahost
);
125 /* fprintf(stderr, "%s: unknown host\n", *ahost); */
129 host_save
= malloc(strlen(hp
->h_name
) + 1);
130 strcpy(host_save
, hp
->h_name
);
134 /* If realm is null, look up from table */
135 if (realm
== NULL
|| realm
[0] == '\0')
136 realm
= krb_realmofhost(host_save
);
137 #endif /* KERBEROS */
139 oldmask
= sigblock(sigmask(SIGURG
));
145 "kcmd(socket): All ports in use\n");
147 perror("kcmd: socket");
151 fcntl(s
, F_SETOWN
, pid
);
152 sin
.sin_family
= hp
->h_addrtype
;
153 #if defined(ultrix) || defined(sun)
154 bcopy(hp
->h_addr
, (caddr_t
)&sin
.sin_addr
, hp
->h_length
);
156 bcopy(hp
->h_addr_list
[0], (caddr_t
)&sin
.sin_addr
, hp
->h_length
);
158 sin
.sin_port
= rport
;
159 if (connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
162 if (errno
== EADDRINUSE
) {
167 * don't wait very long for Kerberos rcmd.
169 if (errno
== ECONNREFUSED
&& timo
<= 4) {
170 /* sleep(timo); don't wait at all here */
174 #if !(defined(ultrix) || defined(sun))
175 if (hp
->h_addr_list
[1] != NULL
) {
179 "kcmd: connect to address %s: ",
180 inet_ntoa(sin
.sin_addr
));
184 bcopy(hp
->h_addr_list
[0], (caddr_t
)&sin
.sin_addr
,
186 fprintf(stderr
, "Trying %s...\n",
187 inet_ntoa(sin
.sin_addr
));
190 #endif /* !(defined(ultrix) || defined(sun)) */
191 if (errno
!= ECONNREFUSED
)
202 int s2
= getport(&lport
), s3
;
203 int len
= sizeof(from
);
210 (void) sprintf(num
, "%d", lport
);
211 if (write(s
, num
, strlen(num
) + 1) != strlen(num
) + 1) {
212 perror("kcmd(write): setting up stderr");
217 s3
= accept(s2
, (struct sockaddr
*)&from
, &len
);
220 perror("kcmd:accept");
226 from
.sin_port
= ntohs((u_short
)from
.sin_port
);
227 if (from
.sin_family
!= AF_INET
||
228 from
.sin_port
>= IPPORT_RESERVED
) {
230 "kcmd(socket): protocol failure in circuit setup.\n");
236 * Kerberos-authenticated service. Don't have to send locuser,
237 * since its already in the ticket, and we'll extract it on
240 /* (void) write(s, locuser, strlen(locuser)+1); */
242 /* set up the needed stuff for mutual auth, but only if necessary */
243 if (authopts
& KOPT_DO_MUTUAL
) {
247 sin_len
= sizeof(struct sockaddr_in
);
248 if (getsockname(s
, (struct sockaddr
*)laddr
, &sin_len
) < 0) {
249 perror("kcmd(getsockname)");
255 if ((status
= krb_sendauth(authopts
, s
, ticket
, service
, *ahost
,
256 realm
, (unsigned long) getpid(), msg_data
,
260 "KCMDV0.1")) != KSUCCESS
)
262 #endif /* KERBEROS */
264 (void) write(s
, remuser
, strlen(remuser
)+1);
265 (void) write(s
, cmd
, strlen(cmd
)+1);
267 if ((rc
= read(s
, &c
, 1)) != 1) {
271 fprintf(stderr
,"kcmd: bad connection with remote host\n");
276 while (read(s
, &c
, 1) == 1) {
277 (void) write(2, &c
, 1);
300 struct sockaddr_in sin
;
303 sin
.sin_family
= AF_INET
;
304 sin
.sin_addr
.s_addr
= INADDR_ANY
;
305 s
= socket(AF_INET
, SOCK_STREAM
, 0);
309 sin
.sin_port
= htons((u_short
)*alport
);
310 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) >= 0)
312 if (errno
!= EADDRINUSE
) {
318 if (*alport
== IPPORT_RESERVED
/2) {
320 if (*alport
== IPPORT_RESERVED
) {
323 errno
= EAGAIN
; /* close */
329 #endif /* !NeXT || KERBEROS */