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