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