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