usched: Allow process to change self cpu affinity
[dragonfly.git] / libexec / rlogind / rlogind.c
blob30be1ea65a39770dd4b3bbc907c631c1216484a4
1 /*-
2 * Copyright (c) 1983, 1988, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1983, 1988, 1989, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)rlogind.c 8.1 (Berkeley) 6/4/93
31 * $FreeBSD: src/libexec/rlogind/rlogind.c,v 1.29.2.5 2000/12/07 15:02:31 ru Exp $
35 * remote login server:
36 * \0
37 * remuser\0
38 * locuser\0
39 * terminal_type/speed\0
40 * data
43 #define FD_SETSIZE 16 /* don't need many bits for select */
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/ioctl.h>
48 #include <signal.h>
49 #include <termios.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <arpa/inet.h>
57 #include <netdb.h>
59 #include <errno.h>
60 #include <libutil.h>
61 #include <pwd.h>
62 #include <syslog.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include "pathnames.h"
70 #ifndef TIOCPKT_WINDOW
71 #define TIOCPKT_WINDOW 0x80
72 #endif
74 #define ARGSTR "Dalnx"
76 /* wrapper for KAME-special getnameinfo() */
77 #ifndef NI_WITHSCOPEID
78 #define NI_WITHSCOPEID 0
79 #endif
81 extern int __check_rhosts_file;
82 extern char **environ;
84 char *env[2];
85 #define NMAX 30
86 char lusername[NMAX+1], rusername[NMAX+1];
87 static char term[64] = "TERM=";
88 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
89 int keepalive = 1;
90 int check_all = 0;
91 int no_delay;
93 struct passwd *pwd;
95 union sockunion {
96 struct sockinet {
97 u_char si_len;
98 u_char si_family;
99 u_short si_port;
100 } su_si;
101 struct sockaddr_in su_sin;
102 struct sockaddr_in6 su_sin6;
104 #define su_len su_si.si_len
105 #define su_family su_si.si_family
106 #define su_port su_si.si_port
108 void doit(int, union sockunion *);
109 int control(int, char *, int);
110 void protocol(int, int);
111 void cleanup(int);
112 void fatal(int, const char *, int);
113 int do_rlogin(union sockunion *);
114 void getstr(char *, int, const char *);
115 void setup_term(int);
116 int do_krb_login(struct sockaddr_in *);
117 void usage(void);
121 main(int argc, char *argv[])
123 union sockunion from;
124 socklen_t fromlen;
125 int ch, on;
127 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
129 opterr = 0;
130 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
131 switch (ch) {
132 case 'D':
133 no_delay = 1;
134 break;
135 case 'a':
136 check_all = 1;
137 break;
138 case 'l':
139 __check_rhosts_file = 0;
140 break;
141 case 'n':
142 keepalive = 0;
143 break;
144 #ifdef CRYPT
145 case 'x':
146 doencrypt = 1;
147 break;
148 #endif
149 case '?':
150 default:
151 usage();
152 break;
154 argc -= optind;
155 argv += optind;
157 fromlen = sizeof (from);
158 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
159 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
160 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
162 on = 1;
163 if (keepalive &&
164 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
165 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
166 if (no_delay &&
167 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
168 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
169 if (from.su_family == AF_INET)
171 on = IPTOS_LOWDELAY;
172 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
173 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
176 doit(0, &from);
177 return 0;
180 int child;
181 int netf;
182 char line[MAXPATHLEN];
183 int confirmed;
185 struct winsize win = { 0, 0, 0, 0 };
188 void
189 doit(int f, union sockunion *fromp)
191 int master, pid, on = 1;
192 int authenticated = 0;
193 char hostname[2 * MAXHOSTNAMELEN + 1];
194 char nameinfo[2 * INET6_ADDRSTRLEN + 1];
195 u_char c;
197 alarm(60);
198 read(f, &c, 1);
200 if (c != 0)
201 exit(1);
203 alarm(0);
205 realhostname_sa(hostname, sizeof(hostname) - 1,
206 (struct sockaddr *)fromp, fromp->su_len);
207 /* error check ? */
208 fromp->su_port = ntohs((u_short)fromp->su_port);
209 hostname[sizeof(hostname) - 1] = '\0';
212 if ((fromp->su_family != AF_INET
213 #ifdef INET6
214 && fromp->su_family != AF_INET6
215 #endif
216 ) ||
217 fromp->su_port >= IPPORT_RESERVED ||
218 fromp->su_port < IPPORT_RESERVED/2) {
219 getnameinfo((struct sockaddr *)fromp,
220 fromp->su_len,
221 nameinfo, sizeof(nameinfo), NULL, 0,
222 NI_NUMERICHOST|NI_WITHSCOPEID);
223 /* error check ? */
224 syslog(LOG_NOTICE, "Connection from %s on illegal port",
225 nameinfo);
226 fatal(f, "Permission denied", 0);
228 #ifdef IP_OPTIONS
229 if (fromp->su_family == AF_INET)
231 u_char optbuf[BUFSIZ/3];
232 socklen_t optsize = sizeof(optbuf), i;
233 int ipproto;
234 struct protoent *ip;
236 if ((ip = getprotobyname("ip")) != NULL)
237 ipproto = ip->p_proto;
238 else
239 ipproto = IPPROTO_IP;
240 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
241 &optsize) == 0 && optsize != 0) {
242 for (i = 0; i < optsize; ) {
243 c = optbuf[i];
244 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
245 syslog(LOG_NOTICE,
246 "Connection refused from %s with IP option %s",
247 inet_ntoa(fromp->su_sin.sin_addr),
248 c == IPOPT_LSRR ? "LSRR" : "SSRR");
249 exit(1);
251 if (c == IPOPT_EOL)
252 break;
253 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
257 #endif
258 if (do_rlogin(fromp) == 0)
259 authenticated++;
261 if (confirmed == 0) {
262 write(f, "", 1);
263 confirmed = 1; /* we sent the null! */
265 #ifdef CRYPT
266 if (doencrypt) {
267 des_enc_write(f, SECURE_MESSAGE, strlen(SECURE_MESSAGE),
268 schedule, &kdata->session);
270 #endif
271 netf = f;
273 pid = forkpty(&master, line, NULL, &win);
274 if (pid < 0) {
275 if (errno == ENOENT)
276 fatal(f, "Out of ptys", 0);
277 else
278 fatal(f, "Forkpty", 1);
280 if (pid == 0) {
281 if (f > 2) /* f should always be 0, but... */
282 close(f);
283 setup_term(0);
284 if (*lusername=='-') {
285 syslog(LOG_ERR, "tried to pass user \"%s\" to login",
286 lusername);
287 fatal(STDERR_FILENO, "invalid user", 0);
289 if (authenticated) {
290 execl(_PATH_LOGIN, "login", "-p",
291 "-h", hostname, "-f", lusername, NULL);
292 } else
293 execl(_PATH_LOGIN, "login", "-p",
294 "-h", hostname, lusername, NULL);
295 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
296 /*NOTREACHED*/
298 #ifdef CRYPT
300 * If encrypted, don't turn on NBIO or the des read/write
301 * routines will croak.
304 if (!doencrypt)
305 #endif
306 ioctl(f, FIONBIO, &on);
307 ioctl(master, FIONBIO, &on);
308 ioctl(master, TIOCPKT, &on);
309 signal(SIGCHLD, cleanup);
310 protocol(f, master);
311 signal(SIGCHLD, SIG_IGN);
312 cleanup(0);
315 char magic[2] = { 0377, 0377 };
316 char oobdata[] = {TIOCPKT_WINDOW};
319 * Handle a "control" request (signaled by magic being present)
320 * in the data stream. For now, we are only willing to handle
321 * window size changes.
324 control(int pty, char *cp, int n)
326 struct winsize w;
328 if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
329 return (0);
330 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
331 bcopy(cp+4, (char *)&w, sizeof(w));
332 w.ws_row = ntohs(w.ws_row);
333 w.ws_col = ntohs(w.ws_col);
334 w.ws_xpixel = ntohs(w.ws_xpixel);
335 w.ws_ypixel = ntohs(w.ws_ypixel);
336 ioctl(pty, TIOCSWINSZ, &w);
337 return (4+sizeof (w));
341 * rlogin "protocol" machine.
343 void
344 protocol(int f, int p)
346 char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
347 int pcc = 0, fcc = 0;
348 int cc, nfd, n;
349 char cntl;
352 * Must ignore SIGTTOU, otherwise we'll stop
353 * when we try and set slave pty's window shape
354 * (our controlling tty is the master pty).
356 signal(SIGTTOU, SIG_IGN);
357 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
358 if (f > p)
359 nfd = f + 1;
360 else
361 nfd = p + 1;
362 if (nfd > FD_SETSIZE) {
363 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
364 fatal(f, "internal error (select mask too small)", 0);
366 for (;;) {
367 fd_set ibits, obits, ebits, *omask;
369 FD_ZERO(&ebits);
370 FD_ZERO(&ibits);
371 FD_ZERO(&obits);
372 omask = NULL;
373 if (fcc) {
374 FD_SET(p, &obits);
375 omask = &obits;
376 } else
377 FD_SET(f, &ibits);
378 if (pcc >= 0) {
379 if (pcc) {
380 FD_SET(f, &obits);
381 omask = &obits;
382 } else
383 FD_SET(p, &ibits);
385 FD_SET(p, &ebits);
386 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
387 if (errno == EINTR)
388 continue;
389 fatal(f, "select", 1);
391 if (n == 0) {
392 /* shouldn't happen... */
393 sleep(5);
394 continue;
396 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
397 if (FD_ISSET(p, &ebits)) {
398 cc = read(p, &cntl, 1);
399 if (cc == 1 && pkcontrol(cntl)) {
400 cntl |= oobdata[0];
401 send(f, &cntl, 1, MSG_OOB);
402 if (cntl & TIOCPKT_FLUSHWRITE) {
403 pcc = 0;
404 FD_CLR(p, &ibits);
408 if (FD_ISSET(f, &ibits)) {
409 #ifdef CRYPT
410 if (doencrypt)
411 fcc = des_enc_read(f, fibuf, sizeof(fibuf),
412 schedule, &kdata->session);
413 else
414 #endif
415 fcc = read(f, fibuf, sizeof(fibuf));
416 if (fcc < 0 && errno == EWOULDBLOCK)
417 fcc = 0;
418 else {
419 register char *cp;
420 int left;
422 if (fcc <= 0)
423 break;
424 fbp = fibuf;
426 top:
427 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
428 if (cp[0] == magic[0] &&
429 cp[1] == magic[1]) {
430 left = fcc - (cp-fibuf);
431 n = control(p, cp, left);
432 if (n) {
433 left -= n;
434 if (left > 0)
435 bcopy(cp+n, cp, left);
436 fcc -= n;
437 goto top; /* n^2 */
440 FD_SET(p, &obits); /* try write */
444 if (FD_ISSET(p, &obits) && fcc > 0) {
445 cc = write(p, fbp, fcc);
446 if (cc > 0) {
447 fcc -= cc;
448 fbp += cc;
452 if (FD_ISSET(p, &ibits)) {
453 pcc = read(p, pibuf, sizeof (pibuf));
454 pbp = pibuf;
455 if (pcc < 0 && errno == EWOULDBLOCK)
456 pcc = 0;
457 else if (pcc <= 0)
458 break;
459 else if (pibuf[0] == 0) {
460 pbp++, pcc--;
461 #ifdef CRYPT
462 if (!doencrypt)
463 #endif
464 FD_SET(f, &obits); /* try write */
465 } else {
466 if (pkcontrol(pibuf[0])) {
467 pibuf[0] |= oobdata[0];
468 send(f, &pibuf[0], 1, MSG_OOB);
470 pcc = 0;
473 if ((FD_ISSET(f, &obits)) && pcc > 0) {
474 #ifdef CRYPT
475 if (doencrypt)
476 cc = des_enc_write(f, pbp, pcc,
477 schedule, &kdata->session);
478 else
479 #endif
480 cc = write(f, pbp, pcc);
481 if (cc < 0 && errno == EWOULDBLOCK) {
483 * This happens when we try write after read
484 * from p, but some old kernels balk at large
485 * writes even when select returns true.
487 if (!FD_ISSET(p, &ibits))
488 sleep(5);
489 continue;
491 if (cc > 0) {
492 pcc -= cc;
493 pbp += cc;
499 void
500 cleanup(int signo __unused)
502 char *p;
504 p = line + sizeof(_PATH_DEV) - 1;
505 if (logout(p))
506 logwtmp(p, "", "");
507 chflags(line, 0);
508 chmod(line, 0666);
509 chown(line, 0, 0);
510 *p = 'p';
511 chflags(line, 0);
512 chmod(line, 0666);
513 chown(line, 0, 0);
514 shutdown(netf, SHUT_RDWR);
515 exit(1);
518 void
519 fatal(int f, const char *msg, int syserr)
521 int len;
522 char buf[BUFSIZ], *bp = buf;
525 * Prepend binary one to message if we haven't sent
526 * the magic null as confirmation.
528 if (!confirmed)
529 *bp++ = '\01'; /* error indicator */
530 if (syserr)
531 len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
532 msg, strerror(errno));
533 else
534 len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
535 write(f, buf, bp + len - buf);
536 exit(1);
540 do_rlogin(union sockunion *dest)
543 getstr(rusername, sizeof(rusername), "remuser too long");
544 getstr(lusername, sizeof(lusername), "locuser too long");
545 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
547 pwd = getpwnam(lusername);
548 if (pwd == NULL)
549 return (-1);
550 /* XXX why don't we syslog() failure? */
552 return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
553 lusername));
556 void
557 getstr(char *buf, int cnt, const char *errmsg)
559 char c;
561 do {
562 if (read(0, &c, 1) != 1)
563 exit(1);
564 if (--cnt < 0)
565 fatal(STDOUT_FILENO, errmsg, 0);
566 *buf++ = c;
567 } while (c != 0);
570 void
571 setup_term(int fd)
573 register char *cp = index(term+ENVSIZE, '/');
574 char *speed;
575 struct termios tt;
577 #ifndef notyet
578 tcgetattr(fd, &tt);
579 if (cp) {
580 *cp++ = '\0';
581 speed = cp;
582 cp = index(speed, '/');
583 if (cp)
584 *cp++ = '\0';
585 cfsetspeed(&tt, atoi(speed));
588 tt.c_iflag = TTYDEF_IFLAG;
589 tt.c_oflag = TTYDEF_OFLAG;
590 tt.c_lflag = TTYDEF_LFLAG;
591 tcsetattr(fd, TCSAFLUSH, &tt);
592 #else
593 if (cp) {
594 *cp++ = '\0';
595 speed = cp;
596 cp = index(speed, '/');
597 if (cp)
598 *cp++ = '\0';
599 tcgetattr(fd, &tt);
600 cfsetspeed(&tt, atoi(speed));
601 tcsetattr(fd, TCSAFLUSH, &tt);
603 #endif
605 env[0] = term;
606 env[1] = NULL;
607 environ = env;
610 void
611 usage(void)
613 syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");