Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / kerberosIV / appl / bsd / kcmd.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "bsd_locl.h"
35
36 RCSID("$Id: kcmd.c,v 1.20.4.1 2000/10/10 12:55:55 assar Exp $");
37
38 #define START_PORT      5120     /* arbitrary */
39
40 static int
41 getport(int *alport)
42 {
43         struct sockaddr_in sin;
44         int s;
45
46         sin.sin_family = AF_INET;
47         sin.sin_addr.s_addr = INADDR_ANY;
48         s = socket(AF_INET, SOCK_STREAM, 0);
49         if (s < 0)
50                 return (-1);
51         for (;;) {
52                 sin.sin_port = htons((u_short)*alport);
53                 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
54                         return (s);
55                 if (errno != EADDRINUSE) {
56                         close(s);
57                         return (-1);
58                 }
59                 (*alport)--;
60 #ifdef ATHENA_COMPAT
61                 if (*alport == IPPORT_RESERVED/2) {
62 #else
63                 if (*alport == IPPORT_RESERVED) {
64 #endif
65                         close(s);
66                         errno = EAGAIN;         /* close */
67                         return (-1);
68                 }
69         }
70 }
71
72 int
73 kcmd(int *sock,
74      char **ahost,
75      u_int16_t rport, 
76      char *locuser,
77      char *remuser,
78      char *cmd,
79      int *fd2p,
80      KTEXT ticket,
81      char *service,
82      char *realm,
83      CREDENTIALS *cred,
84      Key_schedule schedule,
85      MSG_DAT *msg_data,
86      struct sockaddr_in *laddr,
87      struct sockaddr_in *faddr,
88      int32_t authopts)
89 {
90         int s, timo = 1;
91         pid_t pid;
92         struct sockaddr_in sin, from;
93         char c;
94 #ifdef ATHENA_COMPAT
95         int lport = IPPORT_RESERVED - 1;
96 #else
97         int lport = START_PORT;
98 #endif
99         struct hostent *hp;
100         int rc;
101         char *host_save;
102         int status;
103         char **h_addr_list;
104
105         pid = getpid();
106         hp = gethostbyname(*ahost);
107         if (hp == NULL) {
108                 /* fprintf(stderr, "%s: unknown host\n", *ahost); */
109                 return (-1);
110         }
111
112         host_save = strdup(hp->h_name);
113         if (host_save == NULL)
114                 return -1;
115         *ahost = host_save;
116         h_addr_list = hp->h_addr_list;
117
118         /* If realm is null, look up from table */
119         if (realm == NULL || realm[0] == '\0')
120                 realm = krb_realmofhost(host_save);
121
122         for (;;) {
123                 s = getport(&lport);
124                 if (s < 0) {
125                         if (errno == EAGAIN)
126                                 warnx("kcmd(socket): All ports in use\n");
127                         else
128                                 warn("kcmd: socket");
129                         return (-1);
130                 }
131                 sin.sin_family = hp->h_addrtype;
132                 memcpy (&sin.sin_addr, h_addr_list[0], sizeof(sin.sin_addr));
133                 sin.sin_port = rport;
134                 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
135                         break;
136                 close(s);
137                 if (errno == EADDRINUSE) {
138                         lport--;
139                         continue;
140                 }
141                 /*
142                  * don't wait very long for Kerberos rcmd.
143                  */
144                 if (errno == ECONNREFUSED && timo <= 4) {
145                         /* sleep(timo); don't wait at all here */
146                         timo *= 2;
147                         continue;
148                 }
149                 if (h_addr_list[1] != NULL) {
150                         warn ("kcmd: connect (%s)",
151                               inet_ntoa(sin.sin_addr));
152                         h_addr_list++;
153                         memcpy(&sin.sin_addr,
154                                *h_addr_list, 
155                                sizeof(sin.sin_addr));
156                         fprintf(stderr, "Trying %s...\n",
157                                 inet_ntoa(sin.sin_addr));
158                         continue;
159                 }
160                 if (errno != ECONNREFUSED)
161                         warn ("connect(%s)", hp->h_name);
162                 return (-1);
163         }
164         lport--;
165         if (fd2p == 0) {
166                 write(s, "", 1);
167                 lport = 0;
168         } else {
169                 char num[8];
170                 int s2 = getport(&lport), s3;
171                 int len = sizeof(from);
172
173                 if (s2 < 0) {
174                         status = -1;
175                         goto bad;
176                 }
177                 listen(s2, 1);
178                 snprintf(num, sizeof(num), "%d", lport);
179                 if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
180                         warn("kcmd(write): setting up stderr");
181                         close(s2);
182                         status = -1;
183                         goto bad;
184                 }
185                 {
186                     fd_set fds;
187                     FD_ZERO(&fds);
188                     if (s >= FD_SETSIZE || s2 >= FD_SETSIZE) {
189                         warnx("file descriptor too large");
190                         close(s);
191                         close(s2);
192                         status = -1;
193                         goto bad;
194                     }
195
196                     FD_SET(s, &fds);
197                     FD_SET(s2, &fds);
198                     status = select(FD_SETSIZE, &fds, NULL, NULL, NULL);
199                     if(FD_ISSET(s, &fds)){
200                         warnx("kcmd: connection unexpectedly closed.");
201                         close(s2);
202                         status = -1;
203                         goto bad;
204                     }
205                 }
206                 s3 = accept(s2, (struct sockaddr *)&from, &len);
207                 close(s2);
208                 if (s3 < 0) {
209                         warn ("kcmd: accept");
210                         lport = 0;
211                         status = -1;
212                         goto bad;
213                 }
214                 
215                 *fd2p = s3;
216                 from.sin_port = ntohs((u_short)from.sin_port);
217                 if (from.sin_family != AF_INET ||
218                     from.sin_port >= IPPORT_RESERVED) {
219                         warnx("kcmd(socket): "
220                               "protocol failure in circuit setup.");
221                         status = -1;
222                         goto bad2;
223                 }
224         }
225         /*
226          * Kerberos-authenticated service.  Don't have to send locuser,
227          * since its already in the ticket, and we'll extract it on
228          * the other side.
229          */
230         /* write(s, locuser, strlen(locuser)+1); */
231
232         /* set up the needed stuff for mutual auth, but only if necessary */
233         if (authopts & KOPT_DO_MUTUAL) {
234                 int sin_len;
235                 *faddr = sin;
236
237                 sin_len = sizeof(struct sockaddr_in);
238                 if (getsockname(s, (struct sockaddr *)laddr, &sin_len) < 0) {
239                         warn("kcmd(getsockname)");
240                         status = -1;
241                         goto bad2;
242                 }
243         }
244         if ((status = krb_sendauth(authopts, s, ticket, service, *ahost,
245                                realm, (unsigned long) getpid(), msg_data,
246                                cred, schedule,
247                                laddr,
248                                faddr,
249                                "KCMDV0.1")) != KSUCCESS)
250                 goto bad2;
251
252         write(s, remuser, strlen(remuser)+1);
253         write(s, cmd, strlen(cmd)+1);
254
255         if ((rc = read(s, &c, 1)) != 1) {
256                 if (rc == -1)
257                         warn("read(%s)", *ahost);
258                 else
259                         warnx("kcmd: bad connection with remote host");
260                 status = -1;
261                 goto bad2;
262         }
263         if (c != '\0') {
264                 while (read(s, &c, 1) == 1) {
265                         write(2, &c, 1);
266                         if (c == '\n')
267                                 break;
268                 }
269                 status = -1;
270                 goto bad2;
271         }
272         *sock = s;
273         return (KSUCCESS);
274 bad2:
275         if (lport)
276                 close(*fd2p);
277 bad:
278         close(s);
279         return (status);
280 }