File: rmtermd.c

package info (click to toggle)
dnprogs 2.43.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,936 kB
  • ctags: 3,872
  • sloc: ansic: 24,686; cpp: 10,608; makefile: 769; sh: 551; awk: 13
file content (344 lines) | stat: -rw-r--r-- 7,340 bytes parent folder | download | duplicates (3)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

/******************************************************************************
    rmtermd -- old decnet remote terminal protocol daemon

    (c) 2000 Paul Koning	ni1d@arrl.net

    Based on ctermd by E.M. Serrat and Patrick Caulfield
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*******************************************************************************/

/* 
 * This program implements the old (object 23, pre-Cterm) remote terminal
 * protocol.  That protocol is OS-dependent; there are actually four flavors
 * of it, for RSTS/E, RSX, VMS, and TOPS-20.  The one we have here, 
 * following the example of Ultrix, is the TOPS-20 variant -- which is also
 * the simplest of the bunch.
 * The purpose of this daemon is to allow decnet remote terminal access
 * to Linux from DECnet nodes that do not implement Cterm.  That includes
 * the PDP-11 implementations and possibly TOPS-20 as well.
 */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <ctype.h>
#include <regex.h>
#include <stdlib.h>
#include <utmp.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#ifdef DNETUSE_DEVPTS
#include <pty.h>
#endif

#include "dn_endian.h"

static	struct	sockaddr_dn		sockaddr;
static	char				*line;
static	int				s,t,net,pty,len;
static	int				read_present;
/*-----------------------------------------------------------------------*/
void rmterm_child(int s)
{
	int	sts;
	wait (&sts);
}
/*-----------------------------------------------------------------------*/
void rmterm_bind(void)
{
	/* bind message says we're Ultrix, and speak Tops-20 protocol */
	char	rmterm_bind_msg[] = {0x01,0x01,0x01,0x00,18,0x00,0x08,0x00};

	if (write(net,rmterm_bind_msg,sizeof(rmterm_bind_msg)) < 0)
	{
		perror("Rmterm bind request failed");
		exit (-1);
	}
}
/*-----------------------------------------------------------------------*/
void rmterm_reset(int s)
{
	struct	utmp	entry;
	struct	utmp	*lentry;
	char	*p;

	p=line+sizeof("/dev/")-1;

	setutent();
	memcpy(entry.ut_line,p,strlen(p));
	entry.ut_line[strlen(p)]='\0';
	lentry=getutline(&entry);
	lentry->ut_type=DEAD_PROCESS;
	
	memset(lentry->ut_line,0,UT_LINESIZE);
	memset(lentry->ut_user,0,UT_NAMESIZE);
	lentry->ut_time=0;
	pututline(lentry);

	(void)chmod(line,0666);
	(void)chown(line,0,0);
	*p='p';
	(void)chmod(line,0666);
	(void)chown(line,0,0);
	close(pty);
	close(net);
	exit(1);
}
/*-----------------------------------------------------------------------*/
void rmterm_purge(void)
{
	char	buf[4000];
	char	Control_c = 0x03;
	long	numbytes;

	write(pty,&Control_c,1);
	ioctl(pty,FIONREAD,&numbytes);
	while (numbytes)
	{
	 	read(pty,buf,numbytes);
		ioctl(pty,FIONREAD,&numbytes);
	}
}
/*-----------------------------------------------------------------------*/
void rmterm_write (char *buf)
{
	if (write(net,buf,strlen(buf)) < 0)
	{
		perror("rmterm_write");
		exit(-1);
	}
	
}
/*-----------------------------------------------------------------------*/
void rmterm (void)
{
	char	 buf[100];
	struct	 timeval tv;
	fd_set	 rdfs;
	int	 cnt;
	struct   sigaction siga;
	sigset_t ss;

	setsid();

	sigemptyset(&ss);
	siga.sa_handler=rmterm_reset;
	siga.sa_mask  = ss;
	siga.sa_flags = 0;
	sigaction(SIGCHLD, &siga, NULL);
	
	signal(SIGTSTP, SIG_IGN);
	signal(SIGTTOU, SIG_IGN);

	rmterm_write("RMTERM Version 1.0.3\r\nDECnet for Linux\r\n\n");
	
	for (;;)
	{
		FD_ZERO(&rdfs);
		FD_SET(pty,&rdfs);
		tv.tv_sec = 0;
		tv.tv_usec= 200;
		
		if (select(pty+1,&rdfs,NULL,NULL,&tv) > 0);
		{
			if (FD_ISSET(pty,&rdfs))
			{
				cnt=read(pty,buf,sizeof(buf)-1);
				buf[cnt]='\0';
				rmterm_write(buf);
			}
		}
		FD_ZERO(&rdfs);
		FD_SET(net,&rdfs);
		tv.tv_sec = 0;
		tv.tv_usec= 200;
		
		if (select(net+1,&rdfs,NULL,NULL,&tv) > 0);
		{
			if (FD_ISSET(net,&rdfs))
			{
				cnt=read(net,buf,sizeof(buf));
				if (buf[0] == 3)
					rmterm_purge();
				write(pty,buf,cnt);
			}
		}
	}
	rmterm_reset(0);
}
/*-----------------------------------------------------------------------*/
void doit  (void)
{
	int	i,c;
	struct	stat	stb;
	char	ptyname[] = "/dev/ptyCP";
	int	gotpty =0;

	rmterm_bind();

#ifdef DNETUSE_DEVPTS
 	if (openpty(&pty, &t,NULL, NULL, NULL) == 0)
		gotpty = 1;
	else
	{
		rmterm_write("No ptys available for connection");
		exit(-1);
	}
#else
	for (c='p'; c <= 'z'; c++)
	{
		line = ptyname;
		line[strlen("/dev/pty")] = c;
		line[strlen("/dev/ptyC")] = '0';
		if (stat(line,&stb) < 0)
			break; 
		for (i=0; i < 16; i++)
		{
			line[strlen("/dev/ptyC")]= "0123456789abcdef"[i];
			if ( (pty=open(line,O_RDWR)) > 0)	
			{
				gotpty = 1;
				break;
			}
		}
		if (gotpty) break;
	}
	if (!gotpty) 
	{
		rmterm_write("No ptys available for connection");
		exit(-1);
	}


	line[strlen("/dev/")] = 't';
	if ( (t=open(line,O_RDWR)) < 0) 
	{
		rmterm_write("Error connecting to physical terminal");
		exit(-1);
	}
#endif
	if ( fchmod(t,0) ) 
	{
		rmterm_write("Error setting terminal mode");
		exit(-1);
	}

	if ( ( i=fork() ) < 0)
	{
		rmterm_write("Error forking");
		exit(-1);
	}
	if (i) 
	{
		rmterm();
	}

	setsid();
	
	close(pty); close(net);
	if (t != 0) dup2 (t,0);
	if (t != 1) dup2 (t,1);
	if (t != 2) dup2 (t,2);

	if (t > 2) close(t);

	putenv("TERM=vt100");
	execlp("/bin/login","login",(char *)0);
	rmterm_write("Error executing login command");
	exit(-1);
}

void usage(char *prog, FILE *f)
{
	fprintf(f,"\n%s options:\n", prog);
	fprintf(f," -v        Verbose messages\n");
	fprintf(f," -h        Show this help text\n");
	fprintf(f," -d        Run in debug mode - don't do initial fork\n");
	fprintf(f," -l<type>  Logging type(s:syslog, e:stderr, m:mono)\n");
	fprintf(f," -V        Show version number\n\n");
}


/*-----------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
	int verbosity;
	int debug = 0;
	char log_char = 'l';
	char opt;
    
	// Deal with command-line arguments.
	opterr = 0;
	optind = 0;
	while ((opt=getopt(argc,argv,"?vVdhl:")) != EOF)
	{
		switch(opt) 
		{
		case 'h': 
			usage(argv[0], stdout);
			exit(0);

		case '?':
			usage(argv[0], stderr);
			exit(0);

		case 'v':
			verbosity++;
			break;

		case 'd':
			debug++;
			break;

		case 'V':
			printf("\nrmtermd from dnprogs version %s\n\n", VERSION);
			exit(1);
			break;

		case 'l':
			if (optarg[0] != 's' &&
			    optarg[0] != 'm' &&
			    optarg[0] != 'e')
			{
				usage(argv[0], stderr);
				exit(2);
			}
			log_char = optarg[0];
			break;

		}
	}

	// Initialise logging
	init_daemon_logging("rmtermd", log_char);
    
	net = dnet_daemon(DNOBJECT_DTERM, NULL, verbosity, !debug);

	if (net > -1)
	{
		dnet_accept(net, 0, NULL, 0);
		doit();
	}
	return 0;
}
/*-------------------------------------------------------------------------*/