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