rshd.c revision 1.14 1 1.14 mrg /* $NetBSD: rshd.c,v 1.14 1998/07/06 06:48:56 mrg 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.14 mrg __RCSID("$NetBSD: rshd.c,v 1.14 1998/07/06 06:48:56 mrg 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.1 cgd
75 1.1 cgd int keepalive = 1;
76 1.7 cgd int check_all;
77 1.7 cgd int log_success; /* If TRUE, log all successful accesses */
78 1.1 cgd int sent_null;
79 1.1 cgd
80 1.7 cgd void doit __P((struct sockaddr_in *));
81 1.7 cgd void error __P((const char *, ...));
82 1.7 cgd void getstr __P((char *, int, char *));
83 1.7 cgd int local_domain __P((char *));
84 1.7 cgd char *topdomain __P((char *));
85 1.7 cgd void usage __P((void));
86 1.10 mrg int main __P((int, char *[]));
87 1.7 cgd
88 1.3 cgd #define OPTIONS "alnL"
89 1.1 cgd
90 1.7 cgd int
91 1.1 cgd main(argc, argv)
92 1.1 cgd int argc;
93 1.7 cgd char *argv[];
94 1.1 cgd {
95 1.6 pk extern int __check_rhosts_file;
96 1.1 cgd struct linger linger;
97 1.1 cgd int ch, on = 1, fromlen;
98 1.1 cgd struct sockaddr_in from;
99 1.1 cgd
100 1.1 cgd openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
101 1.1 cgd
102 1.1 cgd opterr = 0;
103 1.11 enami while ((ch = getopt(argc, argv, OPTIONS)) != -1)
104 1.1 cgd switch (ch) {
105 1.1 cgd case 'a':
106 1.1 cgd check_all = 1;
107 1.1 cgd break;
108 1.1 cgd case 'l':
109 1.6 pk __check_rhosts_file = 0;
110 1.1 cgd break;
111 1.1 cgd case 'n':
112 1.1 cgd keepalive = 0;
113 1.1 cgd break;
114 1.3 cgd case 'L':
115 1.7 cgd log_success = 1;
116 1.3 cgd break;
117 1.1 cgd case '?':
118 1.1 cgd default:
119 1.1 cgd usage();
120 1.7 cgd break;
121 1.1 cgd }
122 1.1 cgd
123 1.1 cgd argc -= optind;
124 1.1 cgd argv += optind;
125 1.1 cgd
126 1.1 cgd
127 1.1 cgd fromlen = sizeof (from);
128 1.1 cgd if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
129 1.1 cgd syslog(LOG_ERR, "getpeername: %m");
130 1.1 cgd _exit(1);
131 1.1 cgd }
132 1.1 cgd if (keepalive &&
133 1.1 cgd setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
134 1.1 cgd sizeof(on)) < 0)
135 1.1 cgd syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
136 1.1 cgd linger.l_onoff = 1;
137 1.1 cgd linger.l_linger = 60; /* XXX */
138 1.1 cgd if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
139 1.1 cgd sizeof (linger)) < 0)
140 1.1 cgd syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
141 1.1 cgd doit(&from);
142 1.7 cgd /* NOTREACHED */
143 1.10 mrg #ifdef __GNUC__
144 1.10 mrg exit(0);
145 1.10 mrg #endif
146 1.1 cgd }
147 1.1 cgd
148 1.1 cgd char username[20] = "USER=";
149 1.1 cgd char homedir[64] = "HOME=";
150 1.1 cgd char shell[64] = "SHELL=";
151 1.1 cgd char path[100] = "PATH=";
152 1.1 cgd char *envinit[] =
153 1.1 cgd {homedir, shell, path, username, 0};
154 1.1 cgd char **environ;
155 1.1 cgd
156 1.7 cgd void
157 1.1 cgd doit(fromp)
158 1.1 cgd struct sockaddr_in *fromp;
159 1.1 cgd {
160 1.7 cgd extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
161 1.7 cgd struct hostent *hp;
162 1.1 cgd struct passwd *pwd;
163 1.14 mrg in_port_t port;
164 1.1 cgd fd_set ready, readfrom;
165 1.10 mrg int cc, nfd, pv[2], pid, s = -1; /* XXX gcc */
166 1.1 cgd int one = 1;
167 1.10 mrg char *hostname, *errorstr, *errorhost = NULL; /* XXX gcc */
168 1.7 cgd char *cp, sig, buf[BUFSIZ];
169 1.7 cgd char cmdbuf[NCARGS+1], locuser[16], remuser[16];
170 1.1 cgd char remotehost[2 * MAXHOSTNAMELEN + 1];
171 1.9 christos char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
172 1.1 cgd
173 1.1 cgd
174 1.1 cgd (void) signal(SIGINT, SIG_DFL);
175 1.1 cgd (void) signal(SIGQUIT, SIG_DFL);
176 1.1 cgd (void) signal(SIGTERM, SIG_DFL);
177 1.1 cgd #ifdef DEBUG
178 1.1 cgd { int t = open(_PATH_TTY, 2);
179 1.1 cgd if (t >= 0) {
180 1.1 cgd ioctl(t, TIOCNOTTY, (char *)0);
181 1.1 cgd (void) close(t);
182 1.1 cgd }
183 1.1 cgd }
184 1.1 cgd #endif
185 1.14 mrg fromp->sin_port = ntohs((in_port_t)fromp->sin_port);
186 1.1 cgd if (fromp->sin_family != AF_INET) {
187 1.1 cgd syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
188 1.1 cgd fromp->sin_family);
189 1.1 cgd exit(1);
190 1.1 cgd }
191 1.1 cgd #ifdef IP_OPTIONS
192 1.1 cgd {
193 1.1 cgd u_char optbuf[BUFSIZ/3], *cp;
194 1.1 cgd char lbuf[BUFSIZ], *lp;
195 1.1 cgd int optsize = sizeof(optbuf), ipproto;
196 1.1 cgd struct protoent *ip;
197 1.1 cgd
198 1.1 cgd if ((ip = getprotobyname("ip")) != NULL)
199 1.1 cgd ipproto = ip->p_proto;
200 1.1 cgd else
201 1.1 cgd ipproto = IPPROTO_IP;
202 1.1 cgd if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
203 1.1 cgd optsize != 0) {
204 1.1 cgd lp = lbuf;
205 1.1 cgd for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
206 1.1 cgd sprintf(lp, " %2.2x", *cp);
207 1.1 cgd syslog(LOG_NOTICE,
208 1.1 cgd "Connection received from %s using IP options (ignored):%s",
209 1.1 cgd inet_ntoa(fromp->sin_addr), lbuf);
210 1.1 cgd if (setsockopt(0, ipproto, IP_OPTIONS,
211 1.1 cgd (char *)NULL, optsize) != 0) {
212 1.1 cgd syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
213 1.1 cgd exit(1);
214 1.1 cgd }
215 1.1 cgd }
216 1.1 cgd }
217 1.1 cgd #endif
218 1.1 cgd
219 1.13 enami if (fromp->sin_port >= IPPORT_RESERVED ||
220 1.13 enami fromp->sin_port < IPPORT_RESERVED/2) {
221 1.13 enami syslog(LOG_NOTICE|LOG_AUTH,
222 1.13 enami "Connection from %s on illegal port %u",
223 1.13 enami inet_ntoa(fromp->sin_addr), fromp->sin_port);
224 1.13 enami exit(1);
225 1.13 enami }
226 1.1 cgd
227 1.1 cgd (void) alarm(60);
228 1.1 cgd port = 0;
229 1.1 cgd for (;;) {
230 1.1 cgd char c;
231 1.13 enami
232 1.7 cgd if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
233 1.1 cgd if (cc < 0)
234 1.1 cgd syslog(LOG_NOTICE, "read: %m");
235 1.1 cgd shutdown(0, 1+1);
236 1.1 cgd exit(1);
237 1.1 cgd }
238 1.13 enami if (c == 0)
239 1.1 cgd break;
240 1.1 cgd port = port * 10 + c - '0';
241 1.1 cgd }
242 1.1 cgd
243 1.1 cgd (void) alarm(0);
244 1.1 cgd if (port != 0) {
245 1.1 cgd int lport = IPPORT_RESERVED - 1;
246 1.1 cgd s = rresvport(&lport);
247 1.1 cgd if (s < 0) {
248 1.1 cgd syslog(LOG_ERR, "can't get stderr port: %m");
249 1.1 cgd exit(1);
250 1.1 cgd }
251 1.12 lukem if (port >= IPPORT_RESERVED) {
252 1.12 lukem syslog(LOG_ERR, "2nd port not reserved\n");
253 1.12 lukem exit(1);
254 1.12 lukem }
255 1.1 cgd fromp->sin_port = htons(port);
256 1.1 cgd if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
257 1.7 cgd syslog(LOG_INFO, "connect second port %d: %m", port);
258 1.1 cgd exit(1);
259 1.1 cgd }
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd
263 1.1 cgd #ifdef notdef
264 1.1 cgd /* from inetd, socket is already on 0, 1, 2 */
265 1.1 cgd dup2(f, 0);
266 1.1 cgd dup2(f, 1);
267 1.1 cgd dup2(f, 2);
268 1.1 cgd #endif
269 1.7 cgd errorstr = NULL;
270 1.1 cgd hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
271 1.13 enami fromp->sin_family);
272 1.1 cgd if (hp) {
273 1.1 cgd /*
274 1.1 cgd * If name returned by gethostbyaddr is in our domain,
275 1.1 cgd * attempt to verify that we haven't been fooled by someone
276 1.1 cgd * in a remote net; look up the name and check that this
277 1.1 cgd * address corresponds to the name.
278 1.1 cgd */
279 1.1 cgd hostname = hp->h_name;
280 1.1 cgd if (check_all || local_domain(hp->h_name)) {
281 1.1 cgd strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
282 1.1 cgd remotehost[sizeof(remotehost) - 1] = 0;
283 1.1 cgd errorhost = remotehost;
284 1.1 cgd hp = gethostbyname(remotehost);
285 1.1 cgd if (hp == NULL) {
286 1.1 cgd syslog(LOG_INFO,
287 1.1 cgd "Couldn't look up address for %s",
288 1.1 cgd remotehost);
289 1.1 cgd errorstr =
290 1.1 cgd "Couldn't look up address for your host (%s)\n";
291 1.1 cgd hostname = inet_ntoa(fromp->sin_addr);
292 1.1 cgd } else for (; ; hp->h_addr_list++) {
293 1.1 cgd if (hp->h_addr_list[0] == NULL) {
294 1.1 cgd syslog(LOG_NOTICE,
295 1.1 cgd "Host addr %s not listed for host %s",
296 1.1 cgd inet_ntoa(fromp->sin_addr),
297 1.1 cgd hp->h_name);
298 1.1 cgd errorstr =
299 1.1 cgd "Host address mismatch for %s\n";
300 1.1 cgd hostname = inet_ntoa(fromp->sin_addr);
301 1.1 cgd break;
302 1.1 cgd }
303 1.1 cgd if (!bcmp(hp->h_addr_list[0],
304 1.1 cgd (caddr_t)&fromp->sin_addr,
305 1.1 cgd sizeof(fromp->sin_addr))) {
306 1.1 cgd hostname = hp->h_name;
307 1.1 cgd break;
308 1.1 cgd }
309 1.1 cgd }
310 1.1 cgd }
311 1.9 christos hostname = strncpy(hostnamebuf, hostname,
312 1.13 enami sizeof(hostnamebuf) - 1);
313 1.1 cgd } else
314 1.9 christos errorhost = hostname = strncpy(hostnamebuf,
315 1.13 enami inet_ntoa(fromp->sin_addr), sizeof(hostnamebuf) - 1);
316 1.1 cgd
317 1.9 christos hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
318 1.1 cgd
319 1.9 christos getstr(remuser, sizeof(remuser), "remuser");
320 1.1 cgd getstr(locuser, sizeof(locuser), "locuser");
321 1.1 cgd getstr(cmdbuf, sizeof(cmdbuf), "command");
322 1.1 cgd setpwent();
323 1.1 cgd pwd = getpwnam(locuser);
324 1.1 cgd if (pwd == NULL) {
325 1.7 cgd syslog(LOG_INFO|LOG_AUTH,
326 1.7 cgd "%s@%s as %s: unknown login. cmd='%.80s'",
327 1.7 cgd remuser, hostname, locuser, cmdbuf);
328 1.1 cgd if (errorstr == NULL)
329 1.1 cgd errorstr = "Login incorrect.\n";
330 1.1 cgd goto fail;
331 1.1 cgd }
332 1.1 cgd if (chdir(pwd->pw_dir) < 0) {
333 1.1 cgd (void) chdir("/");
334 1.1 cgd #ifdef notdef
335 1.7 cgd syslog(LOG_INFO|LOG_AUTH,
336 1.7 cgd "%s@%s as %s: no home directory. cmd='%.80s'",
337 1.7 cgd remuser, hostname, locuser, cmdbuf);
338 1.1 cgd error("No remote directory.\n");
339 1.1 cgd exit(1);
340 1.1 cgd #endif
341 1.1 cgd }
342 1.1 cgd
343 1.1 cgd
344 1.13 enami if (errorstr ||
345 1.13 enami (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
346 1.13 enami iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
347 1.10 mrg remuser, locuser) < 0)) {
348 1.13 enami if (__rcmd_errstr)
349 1.13 enami syslog(LOG_INFO|LOG_AUTH,
350 1.7 cgd "%s@%s as %s: permission denied (%s). cmd='%.80s'",
351 1.13 enami remuser, hostname, locuser, __rcmd_errstr,
352 1.13 enami cmdbuf);
353 1.13 enami else
354 1.13 enami syslog(LOG_INFO|LOG_AUTH,
355 1.7 cgd "%s@%s as %s: permission denied. cmd='%.80s'",
356 1.13 enami remuser, hostname, locuser, cmdbuf);
357 1.1 cgd fail:
358 1.13 enami if (errorstr == NULL)
359 1.13 enami errorstr = "Permission denied.\n";
360 1.13 enami error(errorstr, errorhost);
361 1.13 enami exit(1);
362 1.13 enami }
363 1.1 cgd
364 1.1 cgd if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
365 1.1 cgd error("Logins currently disabled.\n");
366 1.1 cgd exit(1);
367 1.1 cgd }
368 1.1 cgd
369 1.7 cgd (void) write(STDERR_FILENO, "\0", 1);
370 1.1 cgd sent_null = 1;
371 1.1 cgd
372 1.1 cgd if (port) {
373 1.1 cgd if (pipe(pv) < 0) {
374 1.1 cgd error("Can't make pipe.\n");
375 1.1 cgd exit(1);
376 1.1 cgd }
377 1.1 cgd pid = fork();
378 1.1 cgd if (pid == -1) {
379 1.1 cgd error("Can't fork; try again.\n");
380 1.1 cgd exit(1);
381 1.1 cgd }
382 1.1 cgd if (pid) {
383 1.1 cgd {
384 1.7 cgd (void) close(0);
385 1.7 cgd (void) close(1);
386 1.1 cgd }
387 1.7 cgd (void) close(2);
388 1.7 cgd (void) close(pv[1]);
389 1.1 cgd
390 1.1 cgd FD_ZERO(&readfrom);
391 1.1 cgd FD_SET(s, &readfrom);
392 1.1 cgd FD_SET(pv[0], &readfrom);
393 1.1 cgd if (pv[0] > s)
394 1.1 cgd nfd = pv[0];
395 1.1 cgd else
396 1.1 cgd nfd = s;
397 1.13 enami ioctl(pv[0], FIONBIO, (char *)&one);
398 1.1 cgd
399 1.1 cgd /* should set s nbio! */
400 1.1 cgd nfd++;
401 1.1 cgd do {
402 1.1 cgd ready = readfrom;
403 1.13 enami if (select(nfd, &ready, (fd_set *)0,
404 1.13 enami (fd_set *)0, (struct timeval *)0) < 0)
405 1.13 enami break;
406 1.1 cgd if (FD_ISSET(s, &ready)) {
407 1.1 cgd int ret;
408 1.13 enami
409 1.13 enami ret = read(s, &sig, 1);
410 1.1 cgd if (ret <= 0)
411 1.1 cgd FD_CLR(s, &readfrom);
412 1.1 cgd else
413 1.1 cgd killpg(pid, sig);
414 1.1 cgd }
415 1.1 cgd if (FD_ISSET(pv[0], &ready)) {
416 1.1 cgd errno = 0;
417 1.1 cgd cc = read(pv[0], buf, sizeof(buf));
418 1.1 cgd if (cc <= 0) {
419 1.1 cgd shutdown(s, 1+1);
420 1.1 cgd FD_CLR(pv[0], &readfrom);
421 1.1 cgd } else {
422 1.13 enami (void) write(s, buf, cc);
423 1.1 cgd }
424 1.1 cgd }
425 1.1 cgd
426 1.1 cgd } while (FD_ISSET(s, &readfrom) ||
427 1.1 cgd FD_ISSET(pv[0], &readfrom));
428 1.1 cgd exit(0);
429 1.1 cgd }
430 1.1 cgd setpgrp(0, getpid());
431 1.7 cgd (void) close(s);
432 1.7 cgd (void) close(pv[0]);
433 1.1 cgd dup2(pv[1], 2);
434 1.1 cgd close(pv[1]);
435 1.1 cgd }
436 1.1 cgd if (*pwd->pw_shell == '\0')
437 1.1 cgd pwd->pw_shell = _PATH_BSHELL;
438 1.1 cgd #if BSD > 43
439 1.1 cgd if (setlogin(pwd->pw_name) < 0)
440 1.1 cgd syslog(LOG_ERR, "setlogin() failed: %m");
441 1.1 cgd #endif
442 1.1 cgd (void) setgid((gid_t)pwd->pw_gid);
443 1.1 cgd initgroups(pwd->pw_name, pwd->pw_gid);
444 1.1 cgd (void) setuid((uid_t)pwd->pw_uid);
445 1.1 cgd environ = envinit;
446 1.1 cgd strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
447 1.1 cgd strcat(path, _PATH_DEFPATH);
448 1.1 cgd strncat(shell, pwd->pw_shell, sizeof(shell)-7);
449 1.1 cgd strncat(username, pwd->pw_name, sizeof(username)-6);
450 1.7 cgd cp = strrchr(pwd->pw_shell, '/');
451 1.1 cgd if (cp)
452 1.1 cgd cp++;
453 1.1 cgd else
454 1.1 cgd cp = pwd->pw_shell;
455 1.1 cgd endpwent();
456 1.7 cgd if (log_success || pwd->pw_uid == 0) {
457 1.13 enami syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
458 1.13 enami remuser, hostname, locuser, cmdbuf);
459 1.1 cgd }
460 1.1 cgd execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
461 1.1 cgd perror(pwd->pw_shell);
462 1.1 cgd exit(1);
463 1.1 cgd }
464 1.1 cgd
465 1.1 cgd /*
466 1.7 cgd * Report error to client. Note: can't be used until second socket has
467 1.7 cgd * connected to client, or older clients will hang waiting for that
468 1.7 cgd * connection first.
469 1.1 cgd */
470 1.7 cgd #if __STDC__
471 1.7 cgd #include <stdarg.h>
472 1.7 cgd #else
473 1.7 cgd #include <varargs.h>
474 1.7 cgd #endif
475 1.7 cgd
476 1.7 cgd void
477 1.7 cgd #if __STDC__
478 1.7 cgd error(const char *fmt, ...)
479 1.7 cgd #else
480 1.7 cgd error(fmt, va_alist)
481 1.1 cgd char *fmt;
482 1.7 cgd va_dcl
483 1.7 cgd #endif
484 1.1 cgd {
485 1.7 cgd va_list ap;
486 1.7 cgd int len;
487 1.7 cgd char *bp, buf[BUFSIZ];
488 1.7 cgd #if __STDC__
489 1.7 cgd va_start(ap, fmt);
490 1.7 cgd #else
491 1.7 cgd va_start(ap);
492 1.7 cgd #endif
493 1.7 cgd bp = buf;
494 1.7 cgd if (sent_null == 0) {
495 1.1 cgd *bp++ = 1;
496 1.7 cgd len = 1;
497 1.7 cgd } else
498 1.7 cgd len = 0;
499 1.7 cgd (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
500 1.7 cgd (void)write(STDERR_FILENO, buf, len + strlen(bp));
501 1.1 cgd }
502 1.1 cgd
503 1.7 cgd void
504 1.1 cgd getstr(buf, cnt, err)
505 1.7 cgd char *buf, *err;
506 1.1 cgd int cnt;
507 1.1 cgd {
508 1.1 cgd char c;
509 1.1 cgd
510 1.1 cgd do {
511 1.7 cgd if (read(STDIN_FILENO, &c, 1) != 1)
512 1.1 cgd exit(1);
513 1.1 cgd *buf++ = c;
514 1.1 cgd if (--cnt == 0) {
515 1.1 cgd error("%s too long\n", err);
516 1.1 cgd exit(1);
517 1.1 cgd }
518 1.1 cgd } while (c != 0);
519 1.1 cgd }
520 1.1 cgd
521 1.1 cgd /*
522 1.1 cgd * Check whether host h is in our local domain,
523 1.1 cgd * defined as sharing the last two components of the domain part,
524 1.1 cgd * or the entire domain part if the local domain has only one component.
525 1.1 cgd * If either name is unqualified (contains no '.'),
526 1.1 cgd * assume that the host is local, as it will be
527 1.1 cgd * interpreted as such.
528 1.1 cgd */
529 1.7 cgd int
530 1.1 cgd local_domain(h)
531 1.1 cgd char *h;
532 1.1 cgd {
533 1.14 mrg char localhost[MAXHOSTNAMELEN + 1];
534 1.7 cgd char *p1, *p2;
535 1.1 cgd
536 1.1 cgd localhost[0] = 0;
537 1.14 mrg (void)gethostname(localhost, sizeof(localhost));
538 1.14 mrg localhost[sizeof(localhost) - 1] = '\0';
539 1.1 cgd p1 = topdomain(localhost);
540 1.1 cgd p2 = topdomain(h);
541 1.1 cgd if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
542 1.7 cgd return (1);
543 1.7 cgd return (0);
544 1.1 cgd }
545 1.1 cgd
546 1.1 cgd char *
547 1.1 cgd topdomain(h)
548 1.1 cgd char *h;
549 1.1 cgd {
550 1.7 cgd char *p, *maybe = NULL;
551 1.1 cgd int dots = 0;
552 1.1 cgd
553 1.1 cgd for (p = h + strlen(h); p >= h; p--) {
554 1.1 cgd if (*p == '.') {
555 1.1 cgd if (++dots == 2)
556 1.1 cgd return (p);
557 1.1 cgd maybe = p;
558 1.1 cgd }
559 1.1 cgd }
560 1.1 cgd return (maybe);
561 1.1 cgd }
562 1.1 cgd
563 1.7 cgd void
564 1.1 cgd usage()
565 1.1 cgd {
566 1.7 cgd
567 1.1 cgd syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
568 1.7 cgd exit(2);
569 1.1 cgd }
570