summaryrefslogtreecommitdiffstats
path: root/source3/smbd/vt_mode.c
blob: 0a4d50c217f4f1b3e3ddf03869a3a3ebb1a35b72 (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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* vt_mode.c */
/*
support vtp-sessions

written by Christian A. Lademann <cal@zls.com>
*/

/*
02.05.95:cal:ported to samba-1.9.13
*/

#define	__vt_mode_c__


/* #include	<stdio.h> */
/* #include	<fcntl.h> */
/* #include	<sys/types.h> */
/* #include	<unistd.h> */
/* #include	<signal.h> */
/* #include	<errno.h> */
/* #include	<ctype.h> */
/* #include	<utmp.h> */
/* #include	<sys/param.h> */
/* #include	<sys/ioctl.h> */
/* #include	<stdlib.h> */
/* #include	<string.h> */

#include	"includes.h"
#include	"vt_mode.h"
#include	<utmp.h>

#ifdef SCO
	extern char	*strdup();
#endif

extern int Client;

#ifdef LINUX
#	define	HAS_VTY
#endif

#ifdef SCO
#	define	HAS_PTY
#	define	HAS_VTY

#	include	<sys/tty.h>
#endif

extern int	DEBUGLEVEL;
extern char	*InBuffer, *OutBuffer;
extern int	done_become_user;

char	master_name [64], slave_name [64];
int		master, slave, i, o, e;

int		ms_type = MS_NONE,
		ms_poll = 0;


/*
VT_Check: test incoming packet for "vtp" or "iVT1\0"
*/
int	VT_Check(char	*buffer)
{
	DEBUG(3,("Checking packet: <%10s...>\n", buffer+4));
	if((strncmp(buffer+4, "vtp", 3) == 0 && smb_len(buffer) == 3) || (strncmp(buffer+4, "iVT1\0", 5) == 0 && smb_len(buffer) == 5))
		return(1);
	else
		return(0);
}


/*
VT_Start_utmp: prepare /etc/utmp for /bin/login
*/
int VT_Start_utmp(void)
{
	struct utmp	u, *v;
	char		*tt;


	setutent();

	strcpy(u.ut_line, VT_Line);

	if((v = getutline(&u)) == NULL) {
		if(strncmp(VT_Line, "tty", 3) == 0)
			tt = VT_Line + 3;
		else if(strlen(VT_Line) > 4)
			tt = VT_Line + strlen(VT_Line) - 4;
		else
			tt = VT_Line;

		strcpy(u.ut_id, tt);
		u.ut_time = time((time_t*)0);
	}

	strcpy(u.ut_user, "LOGIN");
	strcpy(u.ut_line, VT_Line);
	u.ut_pid = getpid();
	u.ut_type = LOGIN_PROCESS;
	pututline(&u);

	endutent();

	return(0);
}


/*
VT_Stop_utmp: prepare /etc/utmp for other processes
*/
int VT_Stop_utmp(void)
{
	struct utmp	u, *v;


	if(VT_Line != NULL) {
		setutent();

		strcpy(u.ut_line, VT_Line);

		if((v = getutline(&u)) != NULL) {
			strcpy(v->ut_user, "");
			v->ut_type = DEAD_PROCESS;
			v->ut_time = time((time_t*)0);
			pututline(v);
		}

		endutent();
	}

	return(0);
}


/*
VT_AtExit: Things to do when the program exits
*/
void	VT_AtExit(void)
{
	if(VT_ChildPID > 0) {
		kill(VT_ChildPID, SIGHUP);
		(void)wait(NULL);
	}

	VT_Stop_utmp();
}


/*
VT_SigCLD: signalhandler for SIGCLD: set flag if child-process died
*/
void	VT_SigCLD(int	sig)
{
	if(wait(NULL) == VT_ChildPID)
		VT_ChildDied = True;
	else
		signal(SIGCLD, VT_SigCLD);
}


/*
VT_SigEXIT: signalhandler for signals that cause the process to exit
*/
void	VT_SigEXIT(int	sig)
{
	VT_AtExit();

	exit(1);
}


/*
VT_Start: initialize vt-specific data, alloc pty, spawn shell and send ACK
*/
int	VT_Start(void)
{
	char	OutBuf [64], *X, *Y;


	ms_type = MS_NONE;
	master = slave = -1;

#ifdef HAS_VTY
#ifdef LINUX
#	define	MASTER_TMPL	"/dev/pty  "
#	define	SLAVE_TMPL	"/dev/tty  "
#	define	LETTER1		"pqrs"
#	define	POS1		8
#	define	LETTER2		"0123456789abcdef"
#	define	POS2		9
#endif

#ifdef SCO
#	define	MASTER_TMPL	"/dev/ptyp_  "
#	define	SLAVE_TMPL	"/dev/ttyp_  "
#	define	LETTER1		"0123456"
#	define	POS1		10
#	define	LETTER2		"0123456789abcdef"
#	define	POS2		11
#endif

	if(ms_poll == MS_VTY || ms_poll == 0) {
		strcpy(master_name, MASTER_TMPL);
		strcpy(slave_name, SLAVE_TMPL);

		for(X = LETTER1; *X && master < 0; X++)
			for(Y = LETTER2; *Y && master < 0; Y++) {
				master_name [POS1] = *X;
				master_name [POS2] = *Y;
				if((master = open(master_name, O_RDWR)) >= 0) {
					slave_name [POS1] = *X;
					slave_name [POS2] = *Y;
					if((slave = open(slave_name, O_RDWR)) < 0)
						close(master);
				}
			}

		if(master >= 0 && slave >= 0)
			ms_type = MS_VTY;
	}

#	undef	MASTER_TMPL
#	undef	SLAVE_TMPL
#	undef	LETTER1
#	undef	LETTER2
#	undef	POS1
#	undef	POS2
#endif


#ifdef HAS_PTY
#ifdef SCO
#	define	MASTER_TMPL	"/dev/ptyp%d"
#	define	SLAVE_TMPL	"/dev/ttyp%d"
#	define	MIN_I		0
#	define	MAX_I		63
#endif

	if(ms_poll == MS_PTY || ms_poll == 0) {
		int	i;

		for(i = MIN_I; i <= MAX_I && master < 0; i++) {
			sprintf(master_name, MASTER_TMPL, i);
			if((master = open(master_name, O_RDWR)) >= 0) {
				sprintf(slave_name, SLAVE_TMPL, i);
				if((slave = open(slave_name, O_RDWR)) < 0)
					close(master);
			}
		}

		if(master >= 0 && slave >= 0)
			ms_type = MS_PTY;
	}

#	undef	MASTER_TMPL
#	undef	SLAVE_TMPL
#	undef	MIN_I
#	undef	MAX_I
#endif


	if(! ms_type)
		return(-1);

	VT_Line = strdup(strrchr(slave_name, '/') + 1);

	switch((VT_ChildPID = fork())) {
	case -1:
		return(-1);
		break;

	case 0:
#ifdef SCO
		setsid();
#endif
		close(0);
		close(1);
		close(2);

		i = open(slave_name, O_RDWR);
		o = open(slave_name, O_RDWR);
		e = open(slave_name, O_RDWR);

#ifdef LINUX
		setsid();
		if (ioctl(slave, TIOCSCTTY, (char *)NULL) == -1)
			exit(1);
#endif
#ifdef SCO
		tcsetpgrp(0, getpid());
#endif

		VT_Start_utmp();

		system("stty sane");
		execlp("/bin/login", "login", "-c", (char*)0);
		exit(1);
		break;

	default:
		VT_Mode = True;
		VT_Status = VT_OPEN;
		VT_ChildDied = False;
		VT_Fd = master;

		signal(SIGCLD, VT_SigCLD);

		signal(SIGHUP, VT_SigEXIT);
		signal(SIGTERM, VT_SigEXIT);
		signal(SIGINT, VT_SigEXIT);
		signal(SIGQUIT, VT_SigEXIT);

		memset(OutBuf, 0, sizeof(OutBuf));
		OutBuf [4] = 0x06;
		_smb_setlen(OutBuf, 1);

		send_smb(Client,OutBuf);

		return(0);
		break;
	}
}


/*
VT_Output: transport data from socket to pty
*/
int	VT_Output(char	*Buffer)
{
	int		i, len, nb;


	if(VT_Status != VT_OPEN)
		return(-1);

	len = smb_len(Buffer);

	nb = write(VT_Fd, Buffer + 4, len);

	return((nb == len) ? 0 : -1);
}


/*
VT_Input: transport data from pty to socket
*/
int	VT_Input(char	*Buffer,int		Size)
{
	int		len;


	if(VT_Status != VT_OPEN)
		return(-1);

	memset(Buffer, 0, Size);
	len = read(VT_Fd, Buffer + 4, MIN(VT_MAXREAD, Size));

	_smb_setlen(Buffer, len);

	return(len + 4);
}


/*
VT_Process: main loop while in vt-mode
*/
void VT_Process(void)
{
	static int	trans_num = 0;
	extern int	Client;
	int			nread;


	VT_Start();

	atexit(VT_AtExit);

	while (True) {
		int32			len;      
		int				msg_type;
		int				msg_flags;
		int				counter;
		int				last_keepalive=0;
		struct fd_set	si;
		struct timeval	to, *top;
		int				n, ret, t;


		errno = 0;
		t = SMBD_SELECT_LOOP*1000;


		FD_ZERO(&si);
		FD_SET(Client, &si);

		FD_SET(VT_Fd, &si);

		if(t >= 0) {
			to.tv_sec = t / 1000;
			to.tv_usec = t - (to.tv_sec * 1000);

			top = &to;
		} else
			top = NULL;

		if(VT_ChildDied)
			goto leave_VT_Process;

		n = select(MAX(VT_Fd, Client) + 1, &si, NULL, NULL, top);

		if(VT_ChildDied)
			goto leave_VT_Process;
	
		if(n == 0) {
			int i;
			time_t t;
			BOOL allidle = True;
			extern int keepalive;
	
			counter += SMBD_SELECT_LOOP;

			t = time(NULL);
	
			if (keepalive && (counter-last_keepalive)>keepalive) {
				if (!send_keepalive(Client))
					goto leave_VT_Process;
				last_keepalive = counter;
			}
		} else if(n > 0) {
			counter = 0;

			if(FD_ISSET(VT_Fd, &si)) {
				/* got input from vt */
				nread = VT_Input(OutBuffer, MIN(BUFFER_SIZE,lp_maxxmit()));

				if(nread > 0)
					send_smb(Client,OutBuffer);
			}

			if(FD_ISSET(Client, &si)) {
				/* got input from socket */

				if(receive_smb(Client,InBuffer, 0)) {
					msg_type = CVAL(InBuffer,0);
					msg_flags = CVAL(InBuffer,1);
	
					len = smb_len(InBuffer);
	
					DEBUG(6,("got message type 0x%x of len 0x%x\n",msg_type,len));
	
					nread = len + 4;
     	 
					DEBUG(3,("%s Transaction %d of length %d\n",timestring(),trans_num,nread));
	
					if(msg_type == 0)
						VT_Output(InBuffer);
					else {
						nread = construct_reply(InBuffer,OutBuffer,nread,MIN(BUFFER_SIZE,lp_maxxmit()));
     	
						if(nread > 0) {
							if (nread != smb_len(OutBuffer) + 4) {
								DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
									nread,
									smb_len(OutBuffer)));
							} else
								send_smb(Client,OutBuffer);
						}
					}
				} else
					if(errno == EBADF)
						goto leave_VT_Process;
			}
		}

		trans_num++;
	}

	leave_VT_Process:
/*
		if(VT_ChildPID > 0)
			kill(VT_ChildPID, SIGHUP);

		VT_Stop_utmp(VT_Line);
		return;
*/
		close_sockets();
		exit(0);
}