summaryrefslogtreecommitdiffstats
path: root/src/kadmin/kpasswd/networked.c
blob: 18a3269a66e5d5ea1f5897c03ff85398006f573b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*			Networked				*/
/*								*/
/* Written by:	Glenn Machin 2931				*/
/* Originated:  Nov 12, 1990					*/
/* Description:							*/
/*								*/
/*	This program/routine exits/returns with a status 1 if	*/
/*	the terminal associated with the current process is	*/
/*	connected from a remote host, otherwise exits/returns	*/
/*	with a value of 0.					*/
/*								*/
/*	This program/routine makes some basic assumptions about	*/
/*      utmp:							*/
/*      	*The login process, rcmd, or window application */
/*		 makes an entry into utmp for all currents 	*/
/*		 users.						*/
/*		*For entries in which the users have logged in  */
/*		 locally. The line name is not a pseudo tty     */
/*		 device. 					*/
/*		*For X window application in which 	       */
/*		 the device is a pseudo tty device but the      */
/*               display is the local system, then the ut_host  */
/*	         has the format system_name:0.0 or :0.0.        */
/*		 All other entries will be assumed to be        */
/*		 networked.				        */
/*								*/
/*	Changes:   11/15/90  Check for file /etc/krb.secure.    */
/*			     If it exists then perform network  */
/*			     check, otherwise return 0.		*/
/****************************************************************/
/* 
 * Sandia National Laboratories also makes no representations about the 
 * suitability of the modifications, or additions to this software for 
 * any purpose.  It is provided "as is" without express or implied warranty.
 */
#ifndef _TYPES_
#include <sys/types.h>
#ifndef _TYPES_
#define _TYPES_
#endif
#endif
#include <utmp.h>
#include <pwd.h>

#ifndef MAXHOSTNAME
#define MAXHOSTNAME 64
#endif

#if defined(HAVE_GETUTENT) && !defined(NO_UT_PID)

static int utfile;

#include <fcntl.h>

static void kadmin_setutent()
{
  utfile = open("/etc/utmp",O_RDONLY);
}

static struct utmp * kadmin_getutline(utmpent)
struct utmp *utmpent;
{
 static struct utmp tmputmpent;
 int found = 0;
 while ( read(utfile,&tmputmpent,sizeof(struct utmp)) > 0 ){
	if ( strcmp(tmputmpent.ut_line,utmpent->ut_line) == 0){
#ifdef NO_UT_HOST
		if ( ( 1) &&
#else
		if ( (strcmp(tmputmpent.ut_host,"") == 0) && 
#endif
	     	   (strcmp(tmputmpent.ut_name,"") == 0)) continue;
		found = 1;
		break;
	}
 }
 if (found) 
	return(&tmputmpent);
 return((struct utmp *) 0);
}

static void kadmin_endutent()
{
  close(utfile);
}
#else
#define kadmin_setutent  setutent
#define kadmin_getutline getutline
#define kadmin_endutent  endutent
#endif /* defined(HAVE_GETUTENT) && !defined(NO_UT_PID) */


int network_connected()
{
struct utmp utmpent;
struct utmp retutent, *tmpptr;
char *display_indx;
char currenthost[MAXHOSTNAME];
char *username,*tmpname;


/* Macro for pseudo_tty */
#define pseudo_tty(ut) \
        ((strncmp((ut).ut_line, "tty", 3) == 0 && ((ut).ut_line[3] == 'p' \
                                                || (ut).ut_line[3] == 'q' \
                                                || (ut).ut_line[3] == 'r' \
                                                || (ut).ut_line[3] == 's'))\
				|| (strncmp((ut).ut_line, "pty", 3) == 0))

    /* Check to see if getlogin returns proper name */
    if ( (tmpname = (char *) getlogin()) == (char *) 0) return(1);
    username = (char *) malloc(strlen(tmpname) + 1);
    if ( username == (char *) 0) return(1);
    strcpy(username,tmpname);
    
    /* Obtain tty device for controlling tty of current process.*/
    strncpy(utmpent.ut_line,ttyname(0) + strlen("/dev/"),
	    sizeof(utmpent.ut_line));

    /* See if this device is currently listed in /etc/utmp under
       calling user */
#ifndef NO_UT_TYPE
    utmpent.ut_type = USER_PROCESS;
#define ut_name ut_user
#endif
    kadmin_setutent();
    while ( (tmpptr = (struct utmp *) kadmin_getutline(&utmpent)) 
            != ( struct utmp *) 0) {

	/* If logged out name and host will be empty */
	if ((strcmp(tmpptr->ut_name,"") == 0) &&
#ifdef NO_UT_HOST
	    ( 1)) continue;
#else
	    (strcmp(tmpptr->ut_host,"") == 0)) continue;
#endif
	else break;
    }
    if (  tmpptr   == (struct utmp *) 0) {
	kadmin_endutent();
	return(1);
    }
    byte_copy((char *)tmpptr,(char *)&retutent,sizeof(struct utmp));
    kadmin_endutent();
#ifdef DEBUG
#ifdef NO_UT_HOST
    printf("User %s on line %s :\n",
		retutent.ut_name,retutent.ut_line);
#else
    printf("User %s on line %s connected from host :%s:\n",
		retutent.ut_name,retutent.ut_line,retutent.ut_host);
#endif
#endif
    if  (strcmp(retutent.ut_name,username) != 0) {
	 return(1);
    }


    /* If this is not a pseudo tty then everything is OK */
    if (! pseudo_tty(retutent)) return(0);

    /* OK now the work begins there is an entry in utmp and
       the device is a pseudo tty. */

    /* Check if : is in hostname if so this is xwindow display */

    if (gethostname(currenthost,sizeof(currenthost))) return(1);
#ifdef NO_UT_HOST
    display_indx = (char *) 0;
#else
    display_indx = (char *) strchr(retutent.ut_host,':');
#endif
    if ( display_indx != (char *) 0) {
        /* 
           We have X window application here. The host field should have
	   the form => local_system_name:0.0 or :0.0  
           if the window is being displayed on the local system.
         */
#ifdef NO_UT_HOST
	return(1);
#else
        if (strncmp(currenthost,retutent.ut_host,
                (display_indx - retutent.ut_host)) != 0) return(1);
        else return(0);
#endif
    }
    
    /* Host field is empty or is not X window entry. At this point
       we can't trust that the pseudo tty is not connected to a 
       networked process so let's return 1.
     */
    return(1);
}

byte_copy(str1,str2,len)
char *str1, *str2;
int len;
{
 int i;
 for (i=0;i < len; i++) *(str2 + i) = *(str1 + i);
 return;
}


#ifdef NOTKERBEROS
main(argc,argv)
int argc;
char **argv;
{
  if (network_connected()){
#ifdef DEBUG
	 printf("Networked\n");
#endif
	exit(1);
  }
  else {
#ifdef DEBUG
	printf("Not networked\n");
#endif
	exit(0);
  }
}
#else
int networked()
{
  return(network_connected());
}
#endif