Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / opie / libopie / insecure.c
1 /* insecure.c: The opieinsecure() library function.
2
3 %%% portions-copyright-cmetz-96
4 Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
5 Reserved. The Inner Net License Version 2 applies to these portions of
6 the software.
7 You should have received a copy of the license with this software. If
8 you didn't get a copy, you may request one from <license@inner.net>.
9
10 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
11 McDonald, All Rights Reserved. All Rights under this copyright are assigned
12 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
13 License Agreement applies to this software.
14
15         History:
16
17         Modified by cmetz for OPIE 2.4. Do utmp checks on utmpx systems.
18              Handle unterminated ut_host.
19         Modified by cmetz for OPIE 2.31. Fixed a logic bug. Call endut[x]ent().
20         Modified by cmetz for OPIE 2.3. Added result caching. Use
21              __opiegetutmpentry(). Ifdef around ut_host check. Eliminate
22              unused variable.
23         Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
24              Allow IP loopback. DISPLAY and ut_host must match exactly,
25              not just the part before the colon. Added work-around for 
26              Sun CDE dtterm bug. Leave the environment as it was
27              found. Use uname().
28         Created at NRL for OPIE 2.2 from opiesubr.c. Fixed pointer
29              assignment that should have been a comparison.
30
31 $FreeBSD: src/contrib/opie/libopie/insecure.c,v 1.1.1.2.6.2 2002/07/15 14:48:47 des Exp $
32
33 */
34 #include "opie_cfg.h"
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>     /* ANSI C standard library */
39 #include <sys/param.h>
40 #include <unistd.h>
41
42 #include <utmp.h>
43 #if DOUTMPX
44 #include <utmpx.h>
45 #define utmp utmpx
46 #define endutent endutxent
47 #endif  /* DOUTMPX */
48
49 #if HAVE_SYS_UTSNAME_H
50 #include <sys/utsname.h>
51 #endif /* HAVE_SYS_UTSNAME_H */
52
53 #include "opie.h"
54
55 char *remote_terms[] = { "xterm", "xterms", "kterm", NULL };
56
57 int opieinsecure FUNCTION_NOARGS
58 {
59 #ifndef NO_INSECURE_CHECK
60   char *display_name;
61   char *s;
62   char *term_name;
63   int  insecure = 0;
64 #if HAVE_UT_HOST || DOUTMPX
65   struct utmp utmp;
66 #endif /* HAVE_UT_HOST || DOUTMPX */
67   static int result = -1;
68
69   if (result != -1)
70     return result;
71
72   if (getenv("SSH_CLIENT") != NULL)
73         return (result = 0);
74   display_name = (char *) getenv("DISPLAY");
75   term_name = (char *) getenv("TERM");
76
77   if (display_name) {
78     insecure = 1;
79     if (s = strchr(display_name, ':')) {
80       int n = s - display_name;
81       if (!n)
82         insecure = 0;
83       else {
84         if (!strncmp("unix", display_name, n))
85           insecure = 0;
86         else if (!strncmp("localhost", display_name, n))
87             insecure = 0;
88         else if (!strncmp("loopback", display_name, n))
89             insecure = 0;
90         else if (!strncmp("127.0.0.1", display_name, n))
91             insecure = 0;
92         else {
93           struct utsname utsname;
94
95           if (!uname(&utsname)) {
96             if (!strncmp(utsname.nodename, display_name, n))
97               insecure = 0;
98             else {
99               if (s = strchr(display_name, '.')) {
100                 int n2 = s - display_name;
101                 if (n < n2)
102                   n2 = n;
103                 if (!strncmp(utsname.nodename, display_name, n2))
104                   insecure = 0;
105               } /* endif display_name is '.' */
106             } /* endif hostname != display_name */
107           } /* endif was able to get hostname */
108         } /* endif display_name == UNIX */
109       }
110     }
111     } /* endif display_name == ":" */ 
112     if (insecure)
113       return (result = 1);
114
115   /* If no DISPLAY variable exists and TERM=xterm, 
116      then we probably have an xterm executing on a remote system 
117      with an rlogin or telnet to our system.  If it were a local
118      xterm, then the DISPLAY environment variable would
119      have to exist. rja */
120   if (!display_name && term_name) {
121     int i;
122     for (i = 0; remote_terms[i]; i++)
123       if (!strcmp(term_name, remote_terms[i]))
124         return (result = 1);
125   };
126
127 #if HAVE_UT_HOST || DOUTMPX
128   if (isatty(0)) {
129     memset(&utmp, 0, sizeof(struct utmp));
130     {
131       int i = __opiegetutmpentry(ttyname(0), &utmp);
132       endutent();
133       if (!i && utmp.ut_host[0]) {
134         char host[sizeof(utmp.ut_host) + 1];
135         insecure = 1;
136
137         strncpy(host, utmp.ut_host, sizeof(utmp.ut_host));
138         host[sizeof(utmp.ut_host)] = 0;
139
140         if (s = strchr(host, ':')) {
141           int n = s - host;
142           if (!n)
143             insecure = 0;
144           else
145             if (display_name) {
146               if (!strncmp(host, display_name, n))
147                 insecure = 0;
148 #if 1 /* def SOLARIS */
149               else
150                 if (s = strchr(host, ' ')) {
151                   *s = ':';
152                   if (s = strchr(s + 1, ' '))
153                     *s = '.';
154                   if (!strncmp(host, display_name, n))
155                     insecure = 0; 
156                 }
157 #endif /* SOLARIS */
158             }
159         }
160       }
161     };
162   };
163 #endif /* HAVE_UT_HOST || DOUTMPX */
164   if (insecure)
165     return (result = 1);
166
167   return (result = 0);
168 #else /* NO_INSECURE_CHECK */
169   return 0;
170 #endif /* NO_INSECURE_CHECK */
171 }