Debian Codegraph @

libinetutils/kcmd.c

raw
1 /*
2 Copyright (C) 1997-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1983, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #if defined KRB5 || defined SHISHI
51
52 # include <sys/param.h>
53 # include <sys/file.h>
54 # include <sys/socket.h>
55 # include <sys/stat.h>
56 # include <fcntl.h>
57
58 # include <netinet/in.h>
59 # include <arpa/inet.h>
60
61 # if defined KRB5
62 # ifdef HAVE_KRB5_H
63 # include <krb5.h>
64 # endif
65 # include "kerberos5_def.h"
66 # elif defined(SHISHI) /* !KRB5 */
67 # include <shishi.h>
68 # include "shishi_def.h"
69 # endif/* SHISHI && !KRB5 */
70
71 # include <ctype.h>
72 # include <errno.h>
73 # include <netdb.h>
74 # include <pwd.h>
75 # include <signal.h>
76 # include <stdio.h>
77 # include <stdlib.h>
78 # include <string.h>
79 # include <unistd.h>
80
81 # ifndef MAXHOSTNAMELEN
82 # define MAXHOSTNAMELEN 64
83 # endif
84
85 # define START_PORT 5120 /* arbitrary */
86
87 int getport (int *, int);
88
89 # if defined KRB5
90 int
91 kcmd (krb5_context *ctx, int *sock, char **ahost, unsigned short rport,
92 char *locuser, char **remuser, char *cmd, int *fd2p,
93 char *service, const char *realm, krb5_keyblock **key,
94 struct sockaddr_in *laddr, struct sockaddr_in *faddr, long authopts)
95 # elif defined(SHISHI) /* !KRB5 */
96 int
97 kcmd (Shishi **h, int *sock, char **ahost, unsigned short rport,
98 char *locuser, char **remuser, char *cmd, int *fd2p,
99 char *service, const char *realm, Shishi_key **key,
100 struct sockaddr_storage *laddr, struct sockaddr_storage *faddr,
101 long authopts, int af)
102 # endif /* SHISHI && !KRB5 */
103 {
104 int s, timo = 1, pid;
105 # ifdef HAVE_SIGACTION
106 sigset_t sigs, osigs;
107 # else
108 long oldmask;
109 # endif/* !HAVE_SIGACTION */
110 struct sockaddr_storage sin, from;
111 socklen_t len;
112 char c;
113
114 # ifdef ATHENA_COMPAT
115 int lport = IPPORT_RESERVED - 1;
116 # else
117 int lport = START_PORT;
118 # endif
119 # if HAVE_DECL_GETADDRINFO
120 struct addrinfo hints, *ai, *res;
121 char portstr[8], fqdn[NI_MAXHOST];
122 # else/* !HAVE_DECL_GETADDRINFO */
123 struct hostent *hp;
124 # endif
125 int rc;
126 char *host_save, *host;
127 int status;
128
129 pid = getpid ();
130
131 /* Extract Kerberos instance name. */
132 host = strchr (*ahost, '/');
133 if (host)
134 ++host;
135 else
136 host = *ahost;
137
138 # if HAVE_DECL_GETADDRINFO
139 memset (&hints, 0, sizeof (hints));
140 # ifdef KRB5
141 hints.ai_family = AF_INET;
142 # else
143 /* SHISHI && !KRB5 */
144 hints.ai_family = af;
145 # endif
146 hints.ai_socktype = SOCK_STREAM;
147 hints.ai_flags = AI_CANONNAME;
148 snprintf (portstr, sizeof (portstr), "%hu", ntohs (rport));
149
150 rc = getaddrinfo (host, portstr, &hints, &res);
151 if (rc)
152 {
153 fprintf (stderr, "kcmd: host %s: %s\n", host, gai_strerror (rc));
154 return (-1);
155 }
156
157 ai = res;
158
159 /* Attempt back resolving into the official host name. */
160 rc = getnameinfo (ai->ai_addr, ai->ai_addrlen, fqdn, sizeof (fqdn),
161 NULL, 0, NI_NAMEREQD);
162 if (!rc)
163 {
164 host_save = malloc (strlen (fqdn) + 1);
165 if (host_save == NULL)
166 return (-1);
167 strcpy (host_save, fqdn);
168 }
169 else
170 {
171 host_save = malloc (strlen (ai->ai_canonname) + 1);
172 if (host_save == NULL)
173 return (-1);
174 strcpy (host_save, ai->ai_canonname);
175 }
176
177 # else/* !HAVE_DECL_GETADDRINFO */
178 /* Often the following rejects non-IPv4.
179 * This is dependent on system implementation. */
180 hp = gethostbyname (host);
181 if (hp == NULL)
182 {
183 /* fprintf(stderr, "%s: unknown host\n", host); */
184 return (-1);
185 }
186
187 host_save = malloc (strlen (hp->h_name) + 1);
188 if (host_save == NULL)
189 return -1;
190 strcpy (host_save, hp->h_name);
191 # endif/* !HAVE_DECL_GETADDRINFO */
192
193 if (host == *ahost)
194 *ahost = host_save; /* Simple host name string. */
195 else
196 {
197 /* Server name `*ahost' is a Kerberized name. */
198 char *p;
199
200 p = malloc ((host - *ahost) + strlen (host_save) + 1);
201 if (p == NULL)
202 return (-1);
203
204 /* Extract prefix from `*ahost', excluding slash,
205 * and concatenate the host's canonical name, but
206 * preceded by a slash.
207 */
208 sprintf (p, "%.*s/%s", (int) (host - *ahost - 1), *ahost, host_save);
209 *ahost = p;
210 }
211
212 # ifdef HAVE_SIGACTION
213 sigemptyset (&sigs);
214 sigaddset (&sigs, SIGURG);
215 sigprocmask (SIG_BLOCK, &sigs, &osigs);
216 # else
217 oldmask = sigblock (sigmask (SIGURG));
218 # endif/* !HAVE_SIGACTION */
219 for (;;)
220 {
221 # if HAVE_DECL_GETADDRINFO
222 s = getport (&lport, ai->ai_family);
223 # else/* !HAVE_DECL_GETADDRINFO */
224 s = getport (&lport, hp->h_addrtype);
225 # endif
226 if (s < 0)
227 {
228 if (errno == EAGAIN)
229 fprintf (stderr, "kcmd(socket): All ports in use\n");
230 else
231 perror ("kcmd: socket");
232 # if HAVE_SIGACTION
233 sigprocmask (SIG_SETMASK, &osigs, NULL);
234 # else
235 sigsetmask (oldmask);
236 # endif/* !HAVE_SIGACTION */
237 return (-1);
238 }
239 fcntl (s, F_SETOWN, pid);
240
241 # if HAVE_DECL_GETADDRINFO
242 len = ai->ai_addrlen;
243 memcpy (&sin, ai->ai_addr, ai->ai_addrlen);
244 # else/* !HAVE_DECL_GETADDRINFO */
245 sin.ss_family = hp->h_addrtype;
246 switch (hp->h_addrtype)
247 {
248 case AF_INET6:
249 len = sizeof (struct sockaddr_in6);
250 # ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
251 sin.ss_len = len;
252 # endif
253 memcpy (&((struct sockaddr_in6 *) &sin)->sin6_addr,
254 hp->h_addr, hp->h_length);
255 ((struct sockaddr_in6 *) &sin)->sin6_port = rport;
256 break;
257 case AF_INET:
258 default:
259 len = sizeof (struct sockaddr_in);
260 # ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
261 sin.ss_len = len;
262 # endif
263 memcpy (&((struct sockaddr_in *) &sin)->sin_addr,
264 hp->h_addr, hp->h_length);
265 ((struct sockaddr_in *) &sin)->sin_port = rport;
266 }
267 # endif/* !HAVE_DECL_GETADDRINFO */
268
269 if (connect (s, (struct sockaddr *) &sin, len) >= 0)
270 break;
271 close (s);
272 if (errno == EADDRINUSE)
273 {
274 lport--;
275 continue;
276 }
277 /*
278 * don't wait very long for Kerberos rcmd.
279 */
280 if (errno == ECONNREFUSED && timo <= 4)
281 {
282 /* sleep(timo); don't wait at all here */
283 timo *= 2;
284 continue;
285 }
286 # if ! defined ultrix || defined sun
287 # if HAVE_DECL_GETADDRINFO
288 if (ai->ai_next)
289 # else
290 /* !HAVE_DECL_GETADDRINFO */
291 if (hp->h_addr_list[1] != NULL)
292 # endif
293 {
294 int oerrno = errno;
295 char addrstr[INET6_ADDRSTRLEN];
296
297 # if HAVE_DECL_GETADDRINFO
298 getnameinfo (ai->ai_addr, ai->ai_addrlen,
299 addrstr, sizeof (addrstr), NULL, 0, NI_NUMERICHOST);
300 fprintf (stderr, "kcmd: connect to address %s: ", addrstr);
301 errno = oerrno;
302 perror (NULL);
303 ai = ai->ai_next;
304 getnameinfo (ai->ai_addr, ai->ai_addrlen,
305 addrstr, sizeof (addrstr), NULL, 0, NI_NUMERICHOST);
306 fprintf (stderr, "Trying %s...\n", addrstr);
307 # else
308 /* !HAVE_DECL_GETADDRINFO */
309 fprintf (stderr, "kcmd: connect to address %s: ",
310 inet_ntop (hp->h_addrtype, hp->h_addr_list[0],
311 addrstr, sizeof (addrstr)));
312 errno = oerrno;
313 perror (NULL);
314 hp->h_addr_list++;
315 fprintf (stderr, "Trying %s...\n",
316 inet_ntop (hp->h_addrtype, hp->h_addr_list[0],
317 addrstr, sizeof (addrstr)));
318 # endif
319 /* !HAVE_DECL_GETADDRINFO */
320 continue;
321 }
322 # endif/* !(defined(ultrix) || defined(sun)) */
323 # if HAVE_DECL_GETADDRINFO
324 if (errno != ECONNREFUSED)
325 perror (res->ai_canonname);
326
327 if (res)
328 freeaddrinfo (res);
329 # else/* !HAVE_DECL_GETADDRINFO */
330 if (errno != ECONNREFUSED)
331 perror (hp->h_name);
332 # endif
333
334 # if HAVE_SIGACTION
335 sigprocmask (SIG_SETMASK, &osigs, NULL);
336 # else
337 sigsetmask (oldmask);
338 # endif/* !HAVE_SIGACTION */
339
340 return (-1);
341 }
342 # if HAVE_DECL_GETADDRINFO
343 if (res)
344 freeaddrinfo (res);
345 # endif
346
347 lport--;
348 if (fd2p == 0)
349 {
350 write (s, "", 1);
351 lport = 0;
352 }
353 else
354 {
355 char num[8];
356 int port, s2, s3;
357
358 s2 = getport (&lport, sin.ss_family);
359 len = sizeof (from);
360
361 if (s2 < 0)
362 {
363 status = -1;
364 goto bad;
365 }
366 listen (s2, 1);
367 sprintf (num, "%d", lport);
368 if (write (s, num, strlen (num) + 1) != (ssize_t) strlen (num) + 1)
369 {
370 perror ("kcmd(write): setting up stderr");
371 close (s2);
372 status = -1;
373 goto bad;
374 }
375 s3 = accept (s2, (struct sockaddr *) &from, &len);
376 close (s2);
377 if (s3 < 0)
378 {
379 perror ("kcmd:accept");
380 lport = 0;
381 status = -1;
382 goto bad;
383 }
384 *fd2p = s3;
385 port = (from.ss_family == AF_INET6)
386 ? ntohs (((struct sockaddr_in6 *) &from)->sin6_port)
387 : ntohs (((struct sockaddr_in *) &from)->sin_port);
388
389 if (port >= IPPORT_RESERVED
390 || (from.ss_family != AF_INET && from.ss_family != AF_INET6))
391 {
392 fprintf (stderr,
393 "kcmd(socket): protocol failure in circuit setup.\n");
394 status = -1;
395 goto bad2;
396 }
397 }
398 /*
399 * Kerberos-authenticated service. Don't have to send locuser,
400 * since its already in the ticket, and we'll extract it on
401 * the other side.
402 */
403 /* write(s, locuser, strlen(locuser)+1); */
404
405 /* set up the needed stuff for mutual auth, but only if necessary */
406 # ifdef KRB5
407 if (authopts & AP_OPTS_MUTUAL_REQUIRED)
408 # elif defined(SHISHI) /* !KRB5 */
409 if (authopts & SHISHI_APOPTIONS_MUTUAL_REQUIRED)
410 # endif
411 {
412 socklen_t sin_len;
413
414 memcpy (faddr, &sin, sizeof (*faddr));
415
416 sin_len = sizeof (*laddr);
417 if (getsockname (s, (struct sockaddr *) laddr, &sin_len) < 0)
418 {
419 perror ("kcmd(getsockname)");
420 status = -1;
421 goto bad2;
422 }
423 }
424
425 (void) service; /* Silence warning. XXX: Implicit use? */
426
427 # ifdef KRB5
428 status = kerberos_auth (ctx, 0, remuser, *ahost, s, cmd, rport, key, realm);
429 if (status != 0)
430 goto bad2;
431
432 # elif defined(SHISHI) /* !KRB5 */
433 status = shishi_auth (h, 0, remuser, *ahost, s, cmd, rport, key, realm);
434 if (status != SHISHI_OK)
435 goto bad2;
436
437 # endif/* SHISHI && !KRB5 */
438
439 write (s, *remuser, strlen (*remuser) + 1);
440
441 write (s, cmd, strlen (cmd) + 1);
442
443 if (locuser && locuser[0])
444 write (s, locuser, strlen (locuser) + 1);
445 else
446 write (s, *remuser, strlen (*remuser) + 1);
447
448 {
449 int zero = 0; /* No forwarding of credentials. */
450
451 write (s, &zero, sizeof (zero));
452 }
453
454 rc = read (s, &c, sizeof (c));
455 if (rc != sizeof (c))
456 {
457 if (rc == -1)
458 perror (*ahost);
459 else
460 fprintf (stderr, "kcmd: bad connection with remote host\n");
461 status = -1;
462 goto bad2;
463 }
464 if (c != '\0')
465 {
466 while (read (s, &c, 1) == 1)
467 {
468 write (2, &c, 1);
469 if (c == '\n')
470 break;
471 }
472 status = -1;
473 errno = ENOENT;
474 goto bad2;
475 }
476 # if HAVE_SIGACTION
477 sigprocmask (SIG_SETMASK, &osigs, NULL);
478 # else
479 sigsetmask (oldmask);
480 # endif/* !HAVE_SIGACTION */
481 *sock = s;
482 # if defined KRB5
483 return (0);
484 # elif defined(SHISHI) /* !KRB5 */
485 return (SHISHI_OK);
486 # endif/* SHISHI && !KRB5 */
487 bad2:
488 if (lport)
489 close (*fd2p);
490 bad:
491 close (s);
492 # if HAVE_SIGACTION
493 sigprocmask (SIG_SETMASK, &osigs, NULL);
494 # else
495 sigsetmask (oldmask);
496 # endif/* !HAVE_SIGACTION */
497 return (status);
498 }
499
500 int
501 getport (int *alport, int af)
502 {
503 struct sockaddr_storage sin;
504 socklen_t len;
505 int s;
506
507 memset (&sin, 0, sizeof (sin));
508 sin.ss_family = af;
509 len = (af == AF_INET6) ? sizeof (struct sockaddr_in6)
510 : sizeof (struct sockaddr_in);
511 # ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
512 sin.ss_len = len;
513 # endif
514
515 s = socket (sin.ss_family, SOCK_STREAM, 0);
516 if (s < 0)
517 return (-1);
518 for (;;)
519 {
520 switch (af)
521 {
522 case AF_INET6:
523 ((struct sockaddr_in6 *) &sin)->sin6_port =
524 htons ((unsigned short) *alport);
525 break;
526 case AF_INET:
527 default:
528 ((struct sockaddr_in *) &sin)->sin_port =
529 htons ((unsigned short) *alport);
530 }
531
532 if (bind (s, (struct sockaddr *) &sin, len) >= 0)
533 return (s);
534 if (errno != EADDRINUSE)
535 {
536 close (s);
537 return (-1);
538 }
539 (*alport)--;
540 # ifdef ATHENA_COMPAT
541 if (*alport == IPPORT_RESERVED / 2)
542 {
543 # else
544 if (*alport == IPPORT_RESERVED)
545 {
546 # endif
547 close (s);
548 errno = EAGAIN; /* close */
549 return (-1);
550 }
551 }
552 }
553
554 #endif /* KRB5 || SHISHI */