insecure.c revision 29964
10SN/A/* insecure.c: The opieinsecure() library function.
2157SN/A
30SN/A%%% portions-copyright-cmetz-96
40SN/APortions of this software are Copyright 1996-1997 by Craig Metz, All Rights
50SN/AReserved. The Inner Net License Version 2 applies to these portions of
60SN/Athe software.
7157SN/AYou should have received a copy of the license with this software. If
80SN/Ayou didn't get a copy, you may request one from <license@inner.net>.
9157SN/A
100SN/APortions of this software are Copyright 1995 by Randall Atkinson and Dan
110SN/AMcDonald, All Rights Reserved. All Rights under this copyright are assigned
120SN/Ato the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
130SN/ALicense Agreement applies to this software.
140SN/A
150SN/A        History:
160SN/A
170SN/A	Modified by cmetz for OPIE 2.31. Fixed a logic bug. Call endut[x]ent().
180SN/A	Modified by cmetz for OPIE 2.3. Added result caching. Use
190SN/A	     __opiegetutmpentry(). Ifdef around ut_host check. Eliminate
200SN/A	     unused variable.
21157SN/A	Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
22157SN/A             Allow IP loopback. DISPLAY and ut_host must match exactly,
23157SN/A             not just the part before the colon. Added work-around for
240SN/A             Sun CDE dtterm bug. Leave the environment as it was
250SN/A             found. Use uname().
260SN/A        Created at NRL for OPIE 2.2 from opiesubr.c. Fixed pointer
270SN/A             assignment that should have been a comparison.
280SN/A*/
290SN/A#include "opie_cfg.h"
300SN/A
310SN/A#include <stdio.h>
320SN/A#include <string.h>
330SN/A#include <stdlib.h>	/* ANSI C standard library */
340SN/A#include <sys/param.h>
350SN/A#include <unistd.h>
360SN/A
370SN/A#include <utmp.h>
380SN/A#if DOUTMPX
390SN/A#include <utmpx.h>
400SN/A#define utmp utmpx
410SN/A#define endutent endutxent
420SN/A#endif	/* DOUTMPX */
430SN/A
440SN/A#if HAVE_SYS_UTSNAME_H
450SN/A#include <sys/utsname.h>
460SN/A#endif /* HAVE_SYS_UTSNAME_H */
470SN/A
480SN/A#include "opie.h"
490SN/A
50char *remote_terms[] = { "xterm", "xterms", "kterm", NULL };
51
52int opieinsecure FUNCTION_NOARGS
53{
54#ifndef NO_INSECURE_CHECK
55  char *display_name;
56  char *s;
57  char *term_name;
58  int  insecure = 0;
59#if HAVE_UT_HOST
60  struct utmp utmp;
61#endif /* HAVE_UT_HOST */
62  static int result = -1;
63
64  if (result != -1)
65    return result;
66
67  display_name = (char *) getenv("DISPLAY");
68  term_name = (char *) getenv("TERM");
69
70  if (display_name) {
71    insecure = 1;
72    if (s = strchr(display_name, ':')) {
73      int n = s - display_name;
74      if (!n)
75	insecure = 0;
76      else {
77	if (!strncmp("unix", display_name, n))
78	  insecure = 0;
79        else if (!strncmp("localhost", display_name, n))
80	    insecure = 0;
81        else if (!strncmp("loopback", display_name, n))
82	    insecure = 0;
83        else if (!strncmp("127.0.0.1", display_name, n))
84	    insecure = 0;
85	else {
86          struct utsname utsname;
87
88	  if (!uname(&utsname)) {
89	    if (!strncmp(utsname.nodename, display_name, n))
90	      insecure = 0;
91	    else {
92	      if (s = strchr(display_name, '.')) {
93		int n2 = s - display_name;
94                if (n < n2)
95                  n2 = n;
96		if (!strncmp(utsname.nodename, display_name, n2))
97		  insecure = 0;
98	      } /* endif display_name is '.' */
99	    } /* endif hostname != display_name */
100	  } /* endif was able to get hostname */
101	} /* endif display_name == UNIX */
102      }
103    }
104    } /* endif display_name == ":" */
105    if (insecure)
106      return (result = 1);
107
108  /* If no DISPLAY variable exists and TERM=xterm,
109     then we probably have an xterm executing on a remote system
110     with an rlogin or telnet to our system.  If it were a local
111     xterm, then the DISPLAY environment variable would
112     have to exist. rja */
113  if (!display_name && term_name) {
114    int i;
115    for (i = 0; remote_terms[i]; i++)
116      if (!strcmp(term_name, remote_terms[i]))
117        return (result = 1);
118  };
119
120#if HAVE_UT_HOST
121  memset(&utmp, 0, sizeof(struct utmp));
122  {
123  int i = __opiegetutmpentry(ttyname(0), &utmp);
124  endutent();
125  if (!i && utmp.ut_host[0]) {
126    insecure = 1;
127
128    if (s = strchr(utmp.ut_host, ':')) {
129      int n = s - utmp.ut_host;
130      if (!n)
131	insecure = 0;
132      else
133        if (display_name) {
134          if (!strncmp(utmp.ut_host, display_name, n))
135            insecure = 0;
136#ifdef SOLARIS
137          else
138            if (s = strchr(utmp.ut_host, ' ')) {
139              *s = ':';
140              if (s = strchr(s + 1, ' '))
141                *s = '.';
142              if (!strncmp(utmp.ut_host, display_name, n))
143                insecure = 0;
144            }
145#endif /* SOLARIS */
146        }
147    }
148  }
149  };
150#endif /* HAVE_UT_HOST */
151  if (insecure)
152    return (result = 1);
153
154  return (result = 0);
155#else /* NO_INSECURE_CHECK */
156  return 0;
157#endif /* NO_INSECURE_CHECK */
158}
159