Deleted Added
full compact
rshd.c (31490) rshd.c (35728)
1/*-
2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
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#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
43#endif
44static const char rcsid[] =
1/*-
2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
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#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
43#endif
44static const char rcsid[] =
45 "$Id$";
45 "$Id: rshd.c,v 1.20 1997/12/02 12:30:04 charnier Exp $";
46#endif /* not lint */
47
48/*
49 * remote shell server:
50 * [port]\0
51 * remuser\0
52 * locuser\0
53 * command\0
54 * data
55 */
56#include <sys/param.h>
57#include <sys/ioctl.h>
58#include <sys/time.h>
59#include <sys/socket.h>
60
61#include <netinet/in_systm.h>
62#include <netinet/in.h>
63#include <netinet/ip.h>
64#include <arpa/inet.h>
65#include <netdb.h>
66
67#include <errno.h>
68#include <fcntl.h>
69#include <paths.h>
70#include <pwd.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <syslog.h>
76#include <unistd.h>
77#ifdef LOGIN_CAP
78#include <login_cap.h>
79#endif
80
81int keepalive = 1;
82int check_all;
83int log_success; /* If TRUE, log all successful accesses */
84int sent_null;
85
86void doit __P((struct sockaddr_in *));
87void error __P((const char *, ...));
88void getstr __P((char *, int, char *));
89int local_domain __P((char *));
90char *topdomain __P((char *));
91void usage __P((void));
92
93#ifdef KERBEROS
94#include <des.h>
95#include <krb.h>
96#define VERSION_SIZE 9
97#define SECURE_MESSAGE "This rsh session is using DES encryption for all transmissions.\r\n"
98#define OPTIONS "alnkvxL"
99char authbuf[sizeof(AUTH_DAT)];
100char tickbuf[sizeof(KTEXT_ST)];
101int doencrypt, use_kerberos, vacuous;
102Key_schedule schedule;
103#else
104#define OPTIONS "alnL"
105#endif
106
107int
108main(argc, argv)
109 int argc;
110 char *argv[];
111{
112 extern int __check_rhosts_file;
113 struct linger linger;
114 int ch, on = 1, fromlen;
115 struct sockaddr_in from;
116
117 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
118
119 opterr = 0;
120 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
121 switch (ch) {
122 case 'a':
123 check_all = 1;
124 break;
125 case 'l':
126 __check_rhosts_file = 0;
127 break;
128 case 'n':
129 keepalive = 0;
130 break;
131#ifdef KERBEROS
132 case 'k':
133 use_kerberos = 1;
134 break;
135
136 case 'v':
137 vacuous = 1;
138 break;
139
140#ifdef CRYPT
141 case 'x':
142 doencrypt = 1;
143 break;
144#endif
145#endif
146 case 'L':
147 log_success = 1;
148 break;
149 case '?':
150 default:
151 usage();
152 break;
153 }
154
155 argc -= optind;
156 argv += optind;
157
158#ifdef KERBEROS
159 if (use_kerberos && vacuous) {
160 syslog(LOG_ERR, "only one of -k and -v allowed");
161 exit(2);
162 }
163#ifdef CRYPT
164 if (doencrypt && !use_kerberos) {
165 syslog(LOG_ERR, "-k is required for -x");
166 exit(2);
167 }
168#endif
169#endif
170
171 fromlen = sizeof (from);
172 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
173 syslog(LOG_ERR, "getpeername: %m");
46#endif /* not lint */
47
48/*
49 * remote shell server:
50 * [port]\0
51 * remuser\0
52 * locuser\0
53 * command\0
54 * data
55 */
56#include <sys/param.h>
57#include <sys/ioctl.h>
58#include <sys/time.h>
59#include <sys/socket.h>
60
61#include <netinet/in_systm.h>
62#include <netinet/in.h>
63#include <netinet/ip.h>
64#include <arpa/inet.h>
65#include <netdb.h>
66
67#include <errno.h>
68#include <fcntl.h>
69#include <paths.h>
70#include <pwd.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <syslog.h>
76#include <unistd.h>
77#ifdef LOGIN_CAP
78#include <login_cap.h>
79#endif
80
81int keepalive = 1;
82int check_all;
83int log_success; /* If TRUE, log all successful accesses */
84int sent_null;
85
86void doit __P((struct sockaddr_in *));
87void error __P((const char *, ...));
88void getstr __P((char *, int, char *));
89int local_domain __P((char *));
90char *topdomain __P((char *));
91void usage __P((void));
92
93#ifdef KERBEROS
94#include <des.h>
95#include <krb.h>
96#define VERSION_SIZE 9
97#define SECURE_MESSAGE "This rsh session is using DES encryption for all transmissions.\r\n"
98#define OPTIONS "alnkvxL"
99char authbuf[sizeof(AUTH_DAT)];
100char tickbuf[sizeof(KTEXT_ST)];
101int doencrypt, use_kerberos, vacuous;
102Key_schedule schedule;
103#else
104#define OPTIONS "alnL"
105#endif
106
107int
108main(argc, argv)
109 int argc;
110 char *argv[];
111{
112 extern int __check_rhosts_file;
113 struct linger linger;
114 int ch, on = 1, fromlen;
115 struct sockaddr_in from;
116
117 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
118
119 opterr = 0;
120 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
121 switch (ch) {
122 case 'a':
123 check_all = 1;
124 break;
125 case 'l':
126 __check_rhosts_file = 0;
127 break;
128 case 'n':
129 keepalive = 0;
130 break;
131#ifdef KERBEROS
132 case 'k':
133 use_kerberos = 1;
134 break;
135
136 case 'v':
137 vacuous = 1;
138 break;
139
140#ifdef CRYPT
141 case 'x':
142 doencrypt = 1;
143 break;
144#endif
145#endif
146 case 'L':
147 log_success = 1;
148 break;
149 case '?':
150 default:
151 usage();
152 break;
153 }
154
155 argc -= optind;
156 argv += optind;
157
158#ifdef KERBEROS
159 if (use_kerberos && vacuous) {
160 syslog(LOG_ERR, "only one of -k and -v allowed");
161 exit(2);
162 }
163#ifdef CRYPT
164 if (doencrypt && !use_kerberos) {
165 syslog(LOG_ERR, "-k is required for -x");
166 exit(2);
167 }
168#endif
169#endif
170
171 fromlen = sizeof (from);
172 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
173 syslog(LOG_ERR, "getpeername: %m");
174 _exit(1);
174 exit(1);
175 }
176 if (keepalive &&
177 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
178 sizeof(on)) < 0)
179 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
180 linger.l_onoff = 1;
181 linger.l_linger = 60; /* XXX */
182 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
183 sizeof (linger)) < 0)
184 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
185 doit(&from);
186 /* NOTREACHED */
187 return(0);
188}
189
190char username[20] = "USER=";
191char homedir[64] = "HOME=";
192char shell[64] = "SHELL=";
193char path[100] = "PATH=";
194char *envinit[] =
195 {homedir, shell, path, username, 0};
196char **environ;
197
198void
199doit(fromp)
200 struct sockaddr_in *fromp;
201{
202 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
203 struct hostent *hp;
204 struct passwd *pwd;
205 u_short port;
206 fd_set ready, readfrom;
207 int cc, nfd, pv[2], pid, s;
208 int one = 1;
209 char *hostname, *errorstr, *errorhost;
210 char *cp, sig, buf[BUFSIZ];
211 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
212 char remotehost[2 * MAXHOSTNAMELEN + 1];
213 char fromhost[2 * MAXHOSTNAMELEN + 1];
214#ifdef LOGIN_CAP
215 login_cap_t *lc;
216#endif
217
218#ifdef KERBEROS
219 AUTH_DAT *kdata = (AUTH_DAT *) NULL;
220 KTEXT ticket = (KTEXT) NULL;
221 char instance[INST_SZ], version[VERSION_SIZE];
222 struct sockaddr_in fromaddr;
223 int rc;
224 long authopts;
225 int pv1[2], pv2[2];
226 fd_set wready, writeto;
227
228 fromaddr = *fromp;
229#endif
230
231 (void) signal(SIGINT, SIG_DFL);
232 (void) signal(SIGQUIT, SIG_DFL);
233 (void) signal(SIGTERM, SIG_DFL);
234#ifdef DEBUG
235 { int t = open(_PATH_TTY, 2);
236 if (t >= 0) {
237 ioctl(t, TIOCNOTTY, (char *)0);
238 (void) close(t);
239 }
240 }
241#endif
242 fromp->sin_port = ntohs((u_short)fromp->sin_port);
243 if (fromp->sin_family != AF_INET) {
244 syslog(LOG_ERR, "malformed \"from\" address (af %d)",
245 fromp->sin_family);
246 exit(1);
247 }
248#ifdef IP_OPTIONS
249 {
250 u_char optbuf[BUFSIZ/3];
251 int optsize = sizeof(optbuf), ipproto, i;
252 struct protoent *ip;
253
254 if ((ip = getprotobyname("ip")) != NULL)
255 ipproto = ip->p_proto;
256 else
257 ipproto = IPPROTO_IP;
258 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
259 optsize != 0) {
260 for (i = 0; i < optsize; ) {
261 u_char c = optbuf[i];
262 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
263 syslog(LOG_NOTICE,
264 "connection refused from %s with IP option %s",
265 inet_ntoa(fromp->sin_addr),
266 c == IPOPT_LSRR ? "LSRR" : "SSRR");
267 exit(1);
268 }
269 if (c == IPOPT_EOL)
270 break;
271 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
272 }
273 }
274 }
275#endif
276
277#ifdef KERBEROS
278 if (!use_kerberos)
279#endif
280 if (fromp->sin_port >= IPPORT_RESERVED ||
281 fromp->sin_port < IPPORT_RESERVED/2) {
282 syslog(LOG_NOTICE|LOG_AUTH,
283 "connection from %s on illegal port %u",
284 inet_ntoa(fromp->sin_addr),
285 fromp->sin_port);
286 exit(1);
287 }
288
289 (void) alarm(60);
290 port = 0;
291 for (;;) {
292 char c;
293 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
294 if (cc < 0)
295 syslog(LOG_NOTICE, "read: %m");
296 shutdown(0, 1+1);
297 exit(1);
298 }
299 if (c== 0)
300 break;
301 port = port * 10 + c - '0';
302 }
303
304 (void) alarm(0);
305 if (port != 0) {
306 int lport = IPPORT_RESERVED - 1;
307 s = rresvport(&lport);
308 if (s < 0) {
309 syslog(LOG_ERR, "can't get stderr port: %m");
310 exit(1);
311 }
312#ifdef KERBEROS
313 if (!use_kerberos)
314#endif
315 if (port >= IPPORT_RESERVED ||
316 port < IPPORT_RESERVED/2) {
317 syslog(LOG_NOTICE|LOG_AUTH,
318 "2nd socket from %s on unreserved port %u",
319 inet_ntoa(fromp->sin_addr),
320 port);
321 exit(1);
322 }
323 fromp->sin_port = htons(port);
324 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
325 syslog(LOG_INFO, "connect second port %d: %m", port);
326 exit(1);
327 }
328 }
329
330#ifdef KERBEROS
331 if (vacuous) {
332 error("rshd: remote host requires Kerberos authentication\n");
333 exit(1);
334 }
335#endif
336
337#ifdef notdef
338 /* from inetd, socket is already on 0, 1, 2 */
339 dup2(f, 0);
340 dup2(f, 1);
341 dup2(f, 2);
342#endif
343 errorstr = NULL;
344 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
345 fromp->sin_family);
346 if (hp) {
347 /*
348 * If name returned by gethostbyaddr is in our domain,
349 * attempt to verify that we haven't been fooled by someone
350 * in a remote net; look up the name and check that this
351 * address corresponds to the name.
352 */
353 strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1);
354 fromhost[sizeof(fromhost) - 1] = 0;
355 hostname = fromhost;
356#ifdef KERBEROS
357 if (!use_kerberos)
358#endif
359 if (check_all || local_domain(hp->h_name)) {
360 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
361 remotehost[sizeof(remotehost) - 1] = 0;
362 errorhost = remotehost;
363 hp = gethostbyname(remotehost);
364 if (hp == NULL) {
365 syslog(LOG_INFO,
366 "couldn't look up address for %s",
367 remotehost);
368 errorstr =
369 "Couldn't look up address for your host (%s)\n";
370 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
371 sizeof(fromhost) - 1);
372 fromhost[sizeof(fromhost) - 1] = 0;
373 hostname = fromhost;
374 } else for (; ; hp->h_addr_list++) {
375 if (hp->h_addr_list[0] == NULL) {
376 syslog(LOG_NOTICE,
377 "host addr %s not listed for host %s",
378 inet_ntoa(fromp->sin_addr),
379 hp->h_name);
380 errorstr =
381 "Host address mismatch for %s\n";
382 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
383 sizeof(fromhost) - 1);
384 fromhost[sizeof(fromhost) - 1] = 0;
385 hostname = fromhost;
386 break;
387 }
388 if (!bcmp(hp->h_addr_list[0],
389 (caddr_t)&fromp->sin_addr,
390 sizeof(fromp->sin_addr))) {
391 hostname = remotehost;
392 break;
393 }
394 }
395 }
396 } else {
397 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
398 sizeof(fromhost) - 1);
399 fromhost[sizeof(fromhost) - 1] = 0;
400 errorhost = hostname = fromhost;
401 }
402
403#ifdef KERBEROS
404 if (use_kerberos) {
405 kdata = (AUTH_DAT *) authbuf;
406 ticket = (KTEXT) tickbuf;
407 authopts = 0L;
408 strcpy(instance, "*");
409 version[VERSION_SIZE - 1] = '\0';
410#ifdef CRYPT
411 if (doencrypt) {
412 struct sockaddr_in local_addr;
413 rc = sizeof(local_addr);
414 if (getsockname(0, (struct sockaddr *)&local_addr,
415 &rc) < 0) {
416 syslog(LOG_ERR, "getsockname: %m");
417 error("rlogind: getsockname: %m");
418 exit(1);
419 }
420 authopts = KOPT_DO_MUTUAL;
421 rc = krb_recvauth(authopts, 0, ticket,
422 "rcmd", instance, &fromaddr,
423 &local_addr, kdata, "", schedule,
424 version);
425 des_set_key(&kdata->session, schedule);
426 } else
427#endif
428 rc = krb_recvauth(authopts, 0, ticket, "rcmd",
429 instance, &fromaddr,
430 (struct sockaddr_in *) 0,
431 kdata, "", NULL, version);
432 if (rc != KSUCCESS) {
433 error("Kerberos authentication failure: %s\n",
434 krb_err_txt[rc]);
435 exit(1);
436 }
437 } else
438#endif
439 getstr(remuser, sizeof(remuser), "remuser");
440
441 getstr(locuser, sizeof(locuser), "locuser");
442 getstr(cmdbuf, sizeof(cmdbuf), "command");
443 setpwent();
444 pwd = getpwnam(locuser);
445 if (pwd == NULL) {
446 syslog(LOG_INFO|LOG_AUTH,
447 "%s@%s as %s: unknown login. cmd='%.80s'",
448 remuser, hostname, locuser, cmdbuf);
449 if (errorstr == NULL)
450 errorstr = "Login incorrect.\n";
451 goto fail;
452 }
453#ifdef LOGIN_CAP
454 lc = login_getpwclass(pwd);
455#endif
456 if (chdir(pwd->pw_dir) < 0) {
457#ifdef LOGIN_CAP
458 if (chdir("/") < 0 ||
459 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
460 syslog(LOG_INFO|LOG_AUTH,
461 "%s@%s as %s: no home directory. cmd='%.80s'",
462 remuser, hostname, locuser, cmdbuf);
463 error("No remote home directory.\n");
464 exit(0);
465 }
466#else
467 (void) chdir("/");
468#ifdef notdef
469 syslog(LOG_INFO|LOG_AUTH,
470 "%s@%s as %s: no home directory. cmd='%.80s'",
471 remuser, hostname, locuser, cmdbuf);
472 error("No remote directory.\n");
473 exit(1);
474#endif
475#endif
476 pwd->pw_dir = "/";
477 }
478
479#ifdef KERBEROS
480 if (use_kerberos) {
481 if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
482 if (kuserok(kdata, locuser) != 0) {
483 syslog(LOG_INFO|LOG_AUTH,
484 "Kerberos rsh denied to %s.%s@%s",
485 kdata->pname, kdata->pinst, kdata->prealm);
486 error("Login incorrect.\n");
487 exit(1);
488 }
489 }
490 } else
491#endif
492
493 if (errorstr ||
494 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
495 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
496 iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
497 remuser, locuser) < 0)) {
498 if (__rcmd_errstr)
499 syslog(LOG_INFO|LOG_AUTH,
500 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
501 remuser, hostname, locuser, __rcmd_errstr,
502 cmdbuf);
503 else
504 syslog(LOG_INFO|LOG_AUTH,
505 "%s@%s as %s: permission denied. cmd='%.80s'",
506 remuser, hostname, locuser, cmdbuf);
507fail:
508 if (errorstr == NULL)
509 errorstr = "Login incorrect.\n";
510 error(errorstr, errorhost);
511 exit(1);
512 }
513
514 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
515 error("Logins currently disabled.\n");
516 exit(1);
517 }
518#ifdef LOGIN_CAP
519 if (lc != NULL) {
520 char remote_ip[MAXHOSTNAMELEN];
521
522 strncpy(remote_ip, inet_ntoa(fromp->sin_addr),
523 sizeof(remote_ip) - 1);
524 remote_ip[sizeof(remote_ip) - 1] = 0;
525 if (!auth_hostok(lc, fromhost, remote_ip)) {
526 syslog(LOG_INFO|LOG_AUTH,
527 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
528 remuser, hostname, locuser, __rcmd_errstr,
529 cmdbuf);
530 error("Login incorrect.\n");
531 exit(1);
532 }
533 if (!auth_timeok(lc, time(NULL))) {
534 error("Logins not available right now\n");
535 exit(1);
536 }
537 }
538#endif /* !LOGIN_CAP */
539#if BSD > 43
540 /* before fork, while we're session leader */
541 if (setlogin(pwd->pw_name) < 0)
542 syslog(LOG_ERR, "setlogin() failed: %m");
543#endif
544
545 (void) write(STDERR_FILENO, "\0", 1);
546 sent_null = 1;
547
548 if (port) {
549 if (pipe(pv) < 0) {
550 error("Can't make pipe.\n");
551 exit(1);
552 }
553#ifdef CRYPT
554#ifdef KERBEROS
555 if (doencrypt) {
556 if (pipe(pv1) < 0) {
557 error("Can't make 2nd pipe.\n");
558 exit(1);
559 }
560 if (pipe(pv2) < 0) {
561 error("Can't make 3rd pipe.\n");
562 exit(1);
563 }
564 }
565#endif
566#endif
567 pid = fork();
568 if (pid == -1) {
569 error("Can't fork; try again.\n");
570 exit(1);
571 }
572 if (pid) {
573#ifdef CRYPT
574#ifdef KERBEROS
575 if (doencrypt) {
576 static char msg[] = SECURE_MESSAGE;
577 (void) close(pv1[1]);
578 (void) close(pv2[1]);
579 des_enc_write(s, msg, sizeof(msg) - 1,
580 schedule, &kdata->session);
581
582 } else
583#endif
584#endif
585 {
586 (void) close(0);
587 (void) close(1);
588 }
589 (void) close(2);
590 (void) close(pv[1]);
591
592 FD_ZERO(&readfrom);
593 FD_SET(s, &readfrom);
594 FD_SET(pv[0], &readfrom);
595 if (pv[0] > s)
596 nfd = pv[0];
597 else
598 nfd = s;
599#ifdef CRYPT
600#ifdef KERBEROS
601 if (doencrypt) {
602 FD_ZERO(&writeto);
603 FD_SET(pv2[0], &writeto);
604 FD_SET(pv1[0], &readfrom);
605
606 nfd = MAX(nfd, pv2[0]);
607 nfd = MAX(nfd, pv1[0]);
608 } else
609#endif
610#endif
611 ioctl(pv[0], FIONBIO, (char *)&one);
612
613 /* should set s nbio! */
614 nfd++;
615 do {
616 ready = readfrom;
617#ifdef CRYPT
618#ifdef KERBEROS
619 if (doencrypt) {
620 wready = writeto;
621 if (select(nfd, &ready,
622 &wready, (fd_set *) 0,
623 (struct timeval *) 0) < 0)
624 break;
625 } else
626#endif
627#endif
628 if (select(nfd, &ready, (fd_set *)0,
629 (fd_set *)0, (struct timeval *)0) < 0)
630 break;
631 if (FD_ISSET(s, &ready)) {
632 int ret;
633#ifdef CRYPT
634#ifdef KERBEROS
635 if (doencrypt)
636 ret = des_enc_read(s, &sig, 1,
637 schedule, &kdata->session);
638 else
639#endif
640#endif
641 ret = read(s, &sig, 1);
642 if (ret <= 0)
643 FD_CLR(s, &readfrom);
644 else
645 killpg(pid, sig);
646 }
647 if (FD_ISSET(pv[0], &ready)) {
648 errno = 0;
649 cc = read(pv[0], buf, sizeof(buf));
650 if (cc <= 0) {
651 shutdown(s, 1+1);
652 FD_CLR(pv[0], &readfrom);
653 } else {
654#ifdef CRYPT
655#ifdef KERBEROS
656 if (doencrypt)
657 (void)
658 des_enc_write(s, buf, cc,
659 schedule, &kdata->session);
660 else
661#endif
662#endif
663 (void)
664 write(s, buf, cc);
665 }
666 }
667#ifdef CRYPT
668#ifdef KERBEROS
669 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
670 errno = 0;
671 cc = read(pv1[0], buf, sizeof(buf));
672 if (cc <= 0) {
673 shutdown(pv1[0], 1+1);
674 FD_CLR(pv1[0], &readfrom);
675 } else
676 (void) des_enc_write(STDOUT_FILENO,
677 buf, cc,
678 schedule, &kdata->session);
679 }
680
681 if (doencrypt && FD_ISSET(pv2[0], &wready)) {
682 errno = 0;
683 cc = des_enc_read(STDIN_FILENO,
684 buf, sizeof(buf),
685 schedule, &kdata->session);
686 if (cc <= 0) {
687 shutdown(pv2[0], 1+1);
688 FD_CLR(pv2[0], &writeto);
689 } else
690 (void) write(pv2[0], buf, cc);
691 }
692#endif
693#endif
694
695 } while (FD_ISSET(s, &readfrom) ||
696#ifdef CRYPT
697#ifdef KERBEROS
698 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
699#endif
700#endif
701 FD_ISSET(pv[0], &readfrom));
702 exit(0);
703 }
704 setpgrp(0, getpid());
705 (void) close(s);
706 (void) close(pv[0]);
707#ifdef CRYPT
708#ifdef KERBEROS
709 if (doencrypt) {
710 close(pv1[0]); close(pv2[0]);
711 dup2(pv1[1], 1);
712 dup2(pv2[1], 0);
713 close(pv1[1]);
714 close(pv2[1]);
715 }
716#endif
717#endif
718 dup2(pv[1], 2);
719 close(pv[1]);
720 }
721 if (*pwd->pw_shell == '\0')
722 pwd->pw_shell = _PATH_BSHELL;
723 environ = envinit;
724 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
725 strcat(path, _PATH_DEFPATH);
726 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
727 strncat(username, pwd->pw_name, sizeof(username)-6);
728 cp = strrchr(pwd->pw_shell, '/');
729 if (cp)
730 cp++;
731 else
732 cp = pwd->pw_shell;
733#ifdef LOGIN_CAP
734 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
735 syslog(LOG_ERR, "setusercontext: %m");
736 exit(1);
737 }
738 login_close(lc);
739#else
740 (void) setgid((gid_t)pwd->pw_gid);
741 initgroups(pwd->pw_name, pwd->pw_gid);
742 (void) setuid((uid_t)pwd->pw_uid);
743#endif
744 endpwent();
745 if (log_success || pwd->pw_uid == 0) {
746#ifdef KERBEROS
747 if (use_kerberos)
748 syslog(LOG_INFO|LOG_AUTH,
749 "Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
750 kdata->pname, kdata->pinst, kdata->prealm,
751 hostname, locuser, cmdbuf);
752 else
753#endif
754 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
755 remuser, hostname, locuser, cmdbuf);
756 }
757 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
758 perror(pwd->pw_shell);
759 exit(1);
760}
761
762/*
763 * Report error to client. Note: can't be used until second socket has
764 * connected to client, or older clients will hang waiting for that
765 * connection first.
766 */
767#if __STDC__
768#include <stdarg.h>
769#else
770#include <varargs.h>
771#endif
772
773void
774#if __STDC__
775error(const char *fmt, ...)
776#else
777error(fmt, va_alist)
778 char *fmt;
779 va_dcl
780#endif
781{
782 va_list ap;
783 int len;
784 char *bp, buf[BUFSIZ];
785#if __STDC__
786 va_start(ap, fmt);
787#else
788 va_start(ap);
789#endif
790 bp = buf;
791 if (sent_null == 0) {
792 *bp++ = 1;
793 len = 1;
794 } else
795 len = 0;
796 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
797 (void)write(STDERR_FILENO, buf, len + strlen(bp));
798}
799
800void
801getstr(buf, cnt, err)
802 char *buf, *err;
803 int cnt;
804{
805 char c;
806
807 do {
808 if (read(STDIN_FILENO, &c, 1) != 1)
809 exit(1);
810 *buf++ = c;
811 if (--cnt == 0) {
812 error("%s too long\n", err);
813 exit(1);
814 }
815 } while (c != 0);
816}
817
818/*
819 * Check whether host h is in our local domain,
820 * defined as sharing the last two components of the domain part,
821 * or the entire domain part if the local domain has only one component.
822 * If either name is unqualified (contains no '.'),
823 * assume that the host is local, as it will be
824 * interpreted as such.
825 */
826int
827local_domain(h)
828 char *h;
829{
830 char localhost[MAXHOSTNAMELEN];
831 char *p1, *p2;
832
833 localhost[0] = 0;
834 (void) gethostname(localhost, sizeof(localhost));
835 p1 = topdomain(localhost);
836 p2 = topdomain(h);
837 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
838 return (1);
839 return (0);
840}
841
842char *
843topdomain(h)
844 char *h;
845{
846 char *p, *maybe = NULL;
847 int dots = 0;
848
849 for (p = h + strlen(h); p >= h; p--) {
850 if (*p == '.') {
851 if (++dots == 2)
852 return (p);
853 maybe = p;
854 }
855 }
856 return (maybe);
857}
858
859void
860usage()
861{
862
863 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
864 exit(2);
865}
175 }
176 if (keepalive &&
177 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
178 sizeof(on)) < 0)
179 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
180 linger.l_onoff = 1;
181 linger.l_linger = 60; /* XXX */
182 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
183 sizeof (linger)) < 0)
184 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
185 doit(&from);
186 /* NOTREACHED */
187 return(0);
188}
189
190char username[20] = "USER=";
191char homedir[64] = "HOME=";
192char shell[64] = "SHELL=";
193char path[100] = "PATH=";
194char *envinit[] =
195 {homedir, shell, path, username, 0};
196char **environ;
197
198void
199doit(fromp)
200 struct sockaddr_in *fromp;
201{
202 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
203 struct hostent *hp;
204 struct passwd *pwd;
205 u_short port;
206 fd_set ready, readfrom;
207 int cc, nfd, pv[2], pid, s;
208 int one = 1;
209 char *hostname, *errorstr, *errorhost;
210 char *cp, sig, buf[BUFSIZ];
211 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
212 char remotehost[2 * MAXHOSTNAMELEN + 1];
213 char fromhost[2 * MAXHOSTNAMELEN + 1];
214#ifdef LOGIN_CAP
215 login_cap_t *lc;
216#endif
217
218#ifdef KERBEROS
219 AUTH_DAT *kdata = (AUTH_DAT *) NULL;
220 KTEXT ticket = (KTEXT) NULL;
221 char instance[INST_SZ], version[VERSION_SIZE];
222 struct sockaddr_in fromaddr;
223 int rc;
224 long authopts;
225 int pv1[2], pv2[2];
226 fd_set wready, writeto;
227
228 fromaddr = *fromp;
229#endif
230
231 (void) signal(SIGINT, SIG_DFL);
232 (void) signal(SIGQUIT, SIG_DFL);
233 (void) signal(SIGTERM, SIG_DFL);
234#ifdef DEBUG
235 { int t = open(_PATH_TTY, 2);
236 if (t >= 0) {
237 ioctl(t, TIOCNOTTY, (char *)0);
238 (void) close(t);
239 }
240 }
241#endif
242 fromp->sin_port = ntohs((u_short)fromp->sin_port);
243 if (fromp->sin_family != AF_INET) {
244 syslog(LOG_ERR, "malformed \"from\" address (af %d)",
245 fromp->sin_family);
246 exit(1);
247 }
248#ifdef IP_OPTIONS
249 {
250 u_char optbuf[BUFSIZ/3];
251 int optsize = sizeof(optbuf), ipproto, i;
252 struct protoent *ip;
253
254 if ((ip = getprotobyname("ip")) != NULL)
255 ipproto = ip->p_proto;
256 else
257 ipproto = IPPROTO_IP;
258 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
259 optsize != 0) {
260 for (i = 0; i < optsize; ) {
261 u_char c = optbuf[i];
262 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
263 syslog(LOG_NOTICE,
264 "connection refused from %s with IP option %s",
265 inet_ntoa(fromp->sin_addr),
266 c == IPOPT_LSRR ? "LSRR" : "SSRR");
267 exit(1);
268 }
269 if (c == IPOPT_EOL)
270 break;
271 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
272 }
273 }
274 }
275#endif
276
277#ifdef KERBEROS
278 if (!use_kerberos)
279#endif
280 if (fromp->sin_port >= IPPORT_RESERVED ||
281 fromp->sin_port < IPPORT_RESERVED/2) {
282 syslog(LOG_NOTICE|LOG_AUTH,
283 "connection from %s on illegal port %u",
284 inet_ntoa(fromp->sin_addr),
285 fromp->sin_port);
286 exit(1);
287 }
288
289 (void) alarm(60);
290 port = 0;
291 for (;;) {
292 char c;
293 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
294 if (cc < 0)
295 syslog(LOG_NOTICE, "read: %m");
296 shutdown(0, 1+1);
297 exit(1);
298 }
299 if (c== 0)
300 break;
301 port = port * 10 + c - '0';
302 }
303
304 (void) alarm(0);
305 if (port != 0) {
306 int lport = IPPORT_RESERVED - 1;
307 s = rresvport(&lport);
308 if (s < 0) {
309 syslog(LOG_ERR, "can't get stderr port: %m");
310 exit(1);
311 }
312#ifdef KERBEROS
313 if (!use_kerberos)
314#endif
315 if (port >= IPPORT_RESERVED ||
316 port < IPPORT_RESERVED/2) {
317 syslog(LOG_NOTICE|LOG_AUTH,
318 "2nd socket from %s on unreserved port %u",
319 inet_ntoa(fromp->sin_addr),
320 port);
321 exit(1);
322 }
323 fromp->sin_port = htons(port);
324 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
325 syslog(LOG_INFO, "connect second port %d: %m", port);
326 exit(1);
327 }
328 }
329
330#ifdef KERBEROS
331 if (vacuous) {
332 error("rshd: remote host requires Kerberos authentication\n");
333 exit(1);
334 }
335#endif
336
337#ifdef notdef
338 /* from inetd, socket is already on 0, 1, 2 */
339 dup2(f, 0);
340 dup2(f, 1);
341 dup2(f, 2);
342#endif
343 errorstr = NULL;
344 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
345 fromp->sin_family);
346 if (hp) {
347 /*
348 * If name returned by gethostbyaddr is in our domain,
349 * attempt to verify that we haven't been fooled by someone
350 * in a remote net; look up the name and check that this
351 * address corresponds to the name.
352 */
353 strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1);
354 fromhost[sizeof(fromhost) - 1] = 0;
355 hostname = fromhost;
356#ifdef KERBEROS
357 if (!use_kerberos)
358#endif
359 if (check_all || local_domain(hp->h_name)) {
360 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
361 remotehost[sizeof(remotehost) - 1] = 0;
362 errorhost = remotehost;
363 hp = gethostbyname(remotehost);
364 if (hp == NULL) {
365 syslog(LOG_INFO,
366 "couldn't look up address for %s",
367 remotehost);
368 errorstr =
369 "Couldn't look up address for your host (%s)\n";
370 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
371 sizeof(fromhost) - 1);
372 fromhost[sizeof(fromhost) - 1] = 0;
373 hostname = fromhost;
374 } else for (; ; hp->h_addr_list++) {
375 if (hp->h_addr_list[0] == NULL) {
376 syslog(LOG_NOTICE,
377 "host addr %s not listed for host %s",
378 inet_ntoa(fromp->sin_addr),
379 hp->h_name);
380 errorstr =
381 "Host address mismatch for %s\n";
382 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
383 sizeof(fromhost) - 1);
384 fromhost[sizeof(fromhost) - 1] = 0;
385 hostname = fromhost;
386 break;
387 }
388 if (!bcmp(hp->h_addr_list[0],
389 (caddr_t)&fromp->sin_addr,
390 sizeof(fromp->sin_addr))) {
391 hostname = remotehost;
392 break;
393 }
394 }
395 }
396 } else {
397 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
398 sizeof(fromhost) - 1);
399 fromhost[sizeof(fromhost) - 1] = 0;
400 errorhost = hostname = fromhost;
401 }
402
403#ifdef KERBEROS
404 if (use_kerberos) {
405 kdata = (AUTH_DAT *) authbuf;
406 ticket = (KTEXT) tickbuf;
407 authopts = 0L;
408 strcpy(instance, "*");
409 version[VERSION_SIZE - 1] = '\0';
410#ifdef CRYPT
411 if (doencrypt) {
412 struct sockaddr_in local_addr;
413 rc = sizeof(local_addr);
414 if (getsockname(0, (struct sockaddr *)&local_addr,
415 &rc) < 0) {
416 syslog(LOG_ERR, "getsockname: %m");
417 error("rlogind: getsockname: %m");
418 exit(1);
419 }
420 authopts = KOPT_DO_MUTUAL;
421 rc = krb_recvauth(authopts, 0, ticket,
422 "rcmd", instance, &fromaddr,
423 &local_addr, kdata, "", schedule,
424 version);
425 des_set_key(&kdata->session, schedule);
426 } else
427#endif
428 rc = krb_recvauth(authopts, 0, ticket, "rcmd",
429 instance, &fromaddr,
430 (struct sockaddr_in *) 0,
431 kdata, "", NULL, version);
432 if (rc != KSUCCESS) {
433 error("Kerberos authentication failure: %s\n",
434 krb_err_txt[rc]);
435 exit(1);
436 }
437 } else
438#endif
439 getstr(remuser, sizeof(remuser), "remuser");
440
441 getstr(locuser, sizeof(locuser), "locuser");
442 getstr(cmdbuf, sizeof(cmdbuf), "command");
443 setpwent();
444 pwd = getpwnam(locuser);
445 if (pwd == NULL) {
446 syslog(LOG_INFO|LOG_AUTH,
447 "%s@%s as %s: unknown login. cmd='%.80s'",
448 remuser, hostname, locuser, cmdbuf);
449 if (errorstr == NULL)
450 errorstr = "Login incorrect.\n";
451 goto fail;
452 }
453#ifdef LOGIN_CAP
454 lc = login_getpwclass(pwd);
455#endif
456 if (chdir(pwd->pw_dir) < 0) {
457#ifdef LOGIN_CAP
458 if (chdir("/") < 0 ||
459 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
460 syslog(LOG_INFO|LOG_AUTH,
461 "%s@%s as %s: no home directory. cmd='%.80s'",
462 remuser, hostname, locuser, cmdbuf);
463 error("No remote home directory.\n");
464 exit(0);
465 }
466#else
467 (void) chdir("/");
468#ifdef notdef
469 syslog(LOG_INFO|LOG_AUTH,
470 "%s@%s as %s: no home directory. cmd='%.80s'",
471 remuser, hostname, locuser, cmdbuf);
472 error("No remote directory.\n");
473 exit(1);
474#endif
475#endif
476 pwd->pw_dir = "/";
477 }
478
479#ifdef KERBEROS
480 if (use_kerberos) {
481 if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
482 if (kuserok(kdata, locuser) != 0) {
483 syslog(LOG_INFO|LOG_AUTH,
484 "Kerberos rsh denied to %s.%s@%s",
485 kdata->pname, kdata->pinst, kdata->prealm);
486 error("Login incorrect.\n");
487 exit(1);
488 }
489 }
490 } else
491#endif
492
493 if (errorstr ||
494 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
495 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
496 iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
497 remuser, locuser) < 0)) {
498 if (__rcmd_errstr)
499 syslog(LOG_INFO|LOG_AUTH,
500 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
501 remuser, hostname, locuser, __rcmd_errstr,
502 cmdbuf);
503 else
504 syslog(LOG_INFO|LOG_AUTH,
505 "%s@%s as %s: permission denied. cmd='%.80s'",
506 remuser, hostname, locuser, cmdbuf);
507fail:
508 if (errorstr == NULL)
509 errorstr = "Login incorrect.\n";
510 error(errorstr, errorhost);
511 exit(1);
512 }
513
514 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
515 error("Logins currently disabled.\n");
516 exit(1);
517 }
518#ifdef LOGIN_CAP
519 if (lc != NULL) {
520 char remote_ip[MAXHOSTNAMELEN];
521
522 strncpy(remote_ip, inet_ntoa(fromp->sin_addr),
523 sizeof(remote_ip) - 1);
524 remote_ip[sizeof(remote_ip) - 1] = 0;
525 if (!auth_hostok(lc, fromhost, remote_ip)) {
526 syslog(LOG_INFO|LOG_AUTH,
527 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
528 remuser, hostname, locuser, __rcmd_errstr,
529 cmdbuf);
530 error("Login incorrect.\n");
531 exit(1);
532 }
533 if (!auth_timeok(lc, time(NULL))) {
534 error("Logins not available right now\n");
535 exit(1);
536 }
537 }
538#endif /* !LOGIN_CAP */
539#if BSD > 43
540 /* before fork, while we're session leader */
541 if (setlogin(pwd->pw_name) < 0)
542 syslog(LOG_ERR, "setlogin() failed: %m");
543#endif
544
545 (void) write(STDERR_FILENO, "\0", 1);
546 sent_null = 1;
547
548 if (port) {
549 if (pipe(pv) < 0) {
550 error("Can't make pipe.\n");
551 exit(1);
552 }
553#ifdef CRYPT
554#ifdef KERBEROS
555 if (doencrypt) {
556 if (pipe(pv1) < 0) {
557 error("Can't make 2nd pipe.\n");
558 exit(1);
559 }
560 if (pipe(pv2) < 0) {
561 error("Can't make 3rd pipe.\n");
562 exit(1);
563 }
564 }
565#endif
566#endif
567 pid = fork();
568 if (pid == -1) {
569 error("Can't fork; try again.\n");
570 exit(1);
571 }
572 if (pid) {
573#ifdef CRYPT
574#ifdef KERBEROS
575 if (doencrypt) {
576 static char msg[] = SECURE_MESSAGE;
577 (void) close(pv1[1]);
578 (void) close(pv2[1]);
579 des_enc_write(s, msg, sizeof(msg) - 1,
580 schedule, &kdata->session);
581
582 } else
583#endif
584#endif
585 {
586 (void) close(0);
587 (void) close(1);
588 }
589 (void) close(2);
590 (void) close(pv[1]);
591
592 FD_ZERO(&readfrom);
593 FD_SET(s, &readfrom);
594 FD_SET(pv[0], &readfrom);
595 if (pv[0] > s)
596 nfd = pv[0];
597 else
598 nfd = s;
599#ifdef CRYPT
600#ifdef KERBEROS
601 if (doencrypt) {
602 FD_ZERO(&writeto);
603 FD_SET(pv2[0], &writeto);
604 FD_SET(pv1[0], &readfrom);
605
606 nfd = MAX(nfd, pv2[0]);
607 nfd = MAX(nfd, pv1[0]);
608 } else
609#endif
610#endif
611 ioctl(pv[0], FIONBIO, (char *)&one);
612
613 /* should set s nbio! */
614 nfd++;
615 do {
616 ready = readfrom;
617#ifdef CRYPT
618#ifdef KERBEROS
619 if (doencrypt) {
620 wready = writeto;
621 if (select(nfd, &ready,
622 &wready, (fd_set *) 0,
623 (struct timeval *) 0) < 0)
624 break;
625 } else
626#endif
627#endif
628 if (select(nfd, &ready, (fd_set *)0,
629 (fd_set *)0, (struct timeval *)0) < 0)
630 break;
631 if (FD_ISSET(s, &ready)) {
632 int ret;
633#ifdef CRYPT
634#ifdef KERBEROS
635 if (doencrypt)
636 ret = des_enc_read(s, &sig, 1,
637 schedule, &kdata->session);
638 else
639#endif
640#endif
641 ret = read(s, &sig, 1);
642 if (ret <= 0)
643 FD_CLR(s, &readfrom);
644 else
645 killpg(pid, sig);
646 }
647 if (FD_ISSET(pv[0], &ready)) {
648 errno = 0;
649 cc = read(pv[0], buf, sizeof(buf));
650 if (cc <= 0) {
651 shutdown(s, 1+1);
652 FD_CLR(pv[0], &readfrom);
653 } else {
654#ifdef CRYPT
655#ifdef KERBEROS
656 if (doencrypt)
657 (void)
658 des_enc_write(s, buf, cc,
659 schedule, &kdata->session);
660 else
661#endif
662#endif
663 (void)
664 write(s, buf, cc);
665 }
666 }
667#ifdef CRYPT
668#ifdef KERBEROS
669 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
670 errno = 0;
671 cc = read(pv1[0], buf, sizeof(buf));
672 if (cc <= 0) {
673 shutdown(pv1[0], 1+1);
674 FD_CLR(pv1[0], &readfrom);
675 } else
676 (void) des_enc_write(STDOUT_FILENO,
677 buf, cc,
678 schedule, &kdata->session);
679 }
680
681 if (doencrypt && FD_ISSET(pv2[0], &wready)) {
682 errno = 0;
683 cc = des_enc_read(STDIN_FILENO,
684 buf, sizeof(buf),
685 schedule, &kdata->session);
686 if (cc <= 0) {
687 shutdown(pv2[0], 1+1);
688 FD_CLR(pv2[0], &writeto);
689 } else
690 (void) write(pv2[0], buf, cc);
691 }
692#endif
693#endif
694
695 } while (FD_ISSET(s, &readfrom) ||
696#ifdef CRYPT
697#ifdef KERBEROS
698 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
699#endif
700#endif
701 FD_ISSET(pv[0], &readfrom));
702 exit(0);
703 }
704 setpgrp(0, getpid());
705 (void) close(s);
706 (void) close(pv[0]);
707#ifdef CRYPT
708#ifdef KERBEROS
709 if (doencrypt) {
710 close(pv1[0]); close(pv2[0]);
711 dup2(pv1[1], 1);
712 dup2(pv2[1], 0);
713 close(pv1[1]);
714 close(pv2[1]);
715 }
716#endif
717#endif
718 dup2(pv[1], 2);
719 close(pv[1]);
720 }
721 if (*pwd->pw_shell == '\0')
722 pwd->pw_shell = _PATH_BSHELL;
723 environ = envinit;
724 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
725 strcat(path, _PATH_DEFPATH);
726 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
727 strncat(username, pwd->pw_name, sizeof(username)-6);
728 cp = strrchr(pwd->pw_shell, '/');
729 if (cp)
730 cp++;
731 else
732 cp = pwd->pw_shell;
733#ifdef LOGIN_CAP
734 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
735 syslog(LOG_ERR, "setusercontext: %m");
736 exit(1);
737 }
738 login_close(lc);
739#else
740 (void) setgid((gid_t)pwd->pw_gid);
741 initgroups(pwd->pw_name, pwd->pw_gid);
742 (void) setuid((uid_t)pwd->pw_uid);
743#endif
744 endpwent();
745 if (log_success || pwd->pw_uid == 0) {
746#ifdef KERBEROS
747 if (use_kerberos)
748 syslog(LOG_INFO|LOG_AUTH,
749 "Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
750 kdata->pname, kdata->pinst, kdata->prealm,
751 hostname, locuser, cmdbuf);
752 else
753#endif
754 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
755 remuser, hostname, locuser, cmdbuf);
756 }
757 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
758 perror(pwd->pw_shell);
759 exit(1);
760}
761
762/*
763 * Report error to client. Note: can't be used until second socket has
764 * connected to client, or older clients will hang waiting for that
765 * connection first.
766 */
767#if __STDC__
768#include <stdarg.h>
769#else
770#include <varargs.h>
771#endif
772
773void
774#if __STDC__
775error(const char *fmt, ...)
776#else
777error(fmt, va_alist)
778 char *fmt;
779 va_dcl
780#endif
781{
782 va_list ap;
783 int len;
784 char *bp, buf[BUFSIZ];
785#if __STDC__
786 va_start(ap, fmt);
787#else
788 va_start(ap);
789#endif
790 bp = buf;
791 if (sent_null == 0) {
792 *bp++ = 1;
793 len = 1;
794 } else
795 len = 0;
796 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
797 (void)write(STDERR_FILENO, buf, len + strlen(bp));
798}
799
800void
801getstr(buf, cnt, err)
802 char *buf, *err;
803 int cnt;
804{
805 char c;
806
807 do {
808 if (read(STDIN_FILENO, &c, 1) != 1)
809 exit(1);
810 *buf++ = c;
811 if (--cnt == 0) {
812 error("%s too long\n", err);
813 exit(1);
814 }
815 } while (c != 0);
816}
817
818/*
819 * Check whether host h is in our local domain,
820 * defined as sharing the last two components of the domain part,
821 * or the entire domain part if the local domain has only one component.
822 * If either name is unqualified (contains no '.'),
823 * assume that the host is local, as it will be
824 * interpreted as such.
825 */
826int
827local_domain(h)
828 char *h;
829{
830 char localhost[MAXHOSTNAMELEN];
831 char *p1, *p2;
832
833 localhost[0] = 0;
834 (void) gethostname(localhost, sizeof(localhost));
835 p1 = topdomain(localhost);
836 p2 = topdomain(h);
837 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
838 return (1);
839 return (0);
840}
841
842char *
843topdomain(h)
844 char *h;
845{
846 char *p, *maybe = NULL;
847 int dots = 0;
848
849 for (p = h + strlen(h); p >= h; p--) {
850 if (*p == '.') {
851 if (++dots == 2)
852 return (p);
853 maybe = p;
854 }
855 }
856 return (maybe);
857}
858
859void
860usage()
861{
862
863 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
864 exit(2);
865}