5 written by Christian A. Lademann <cal@zls.com>
9 02.05.95:cal:ported to samba-1.9.13
15 /* #include <stdio.h> */
16 /* #include <fcntl.h> */
17 /* #include <sys/types.h> */
18 /* #include <unistd.h> */
19 /* #include <signal.h> */
20 /* #include <errno.h> */
21 /* #include <ctype.h> */
22 /* #include <utmp.h> */
23 /* #include <sys/param.h> */
24 /* #include <sys/ioctl.h> */
25 /* #include <stdlib.h> */
26 /* #include <string.h> */
33 extern char *strdup();
49 extern int DEBUGLEVEL
;
50 extern char *InBuffer
, *OutBuffer
;
51 extern int done_become_user
;
53 char master_name
[64], slave_name
[64];
54 int master
, slave
, i
, o
, e
;
56 int ms_type
= MS_NONE
,
61 VT_Check: test incoming packet for "vtp" or "iVT1\0"
66 DEBUG(3,("Checking packet: <%10s...>\n", buffer
+4));
67 if((strncmp(buffer
+4, "vtp", 3) == 0 && smb_len(buffer
) == 3) || (strncmp(buffer
+4, "iVT1\0", 5) == 0 && smb_len(buffer
) == 5))
75 VT_Start_utmp: prepare /etc/utmp for /bin/login
85 strcpy(u
.ut_line
, VT_Line
);
87 if((v
= getutline(&u
)) == NULL
) {
88 if(strncmp(VT_Line
, "tty", 3) == 0)
90 else if(strlen(VT_Line
) > 4)
91 tt
= VT_Line
+ strlen(VT_Line
) - 4;
96 u
.ut_time
= time((time_t*)0);
99 strcpy(u
.ut_user
, "LOGIN");
100 strcpy(u
.ut_line
, VT_Line
);
102 u
.ut_type
= LOGIN_PROCESS
;
112 VT_Stop_utmp: prepare /etc/utmp for other processes
119 if(VT_Line
!= NULL
) {
122 strcpy(u
.ut_line
, VT_Line
);
124 if((v
= getutline(&u
)) != NULL
) {
125 strcpy(v
->ut_user
, "");
126 v
->ut_type
= DEAD_PROCESS
;
127 v
->ut_time
= time((time_t*)0);
139 VT_AtExit: Things to do when the program exits
143 if(VT_ChildPID
> 0) {
144 kill(VT_ChildPID
, SIGHUP
);
153 VT_SigCLD: signalhandler for SIGCLD: set flag if child-process died
158 if(wait(NULL
) == VT_ChildPID
)
161 signal(SIGCLD
, VT_SigCLD
);
166 VT_SigEXIT: signalhandler for signals that cause the process to exit
178 VT_Start: initialize vt-specific data, alloc pty, spawn shell and send ACK
182 char OutBuf
[64], *X
, *Y
;
190 # define MASTER_TMPL "/dev/pty "
191 # define SLAVE_TMPL "/dev/tty "
192 # define LETTER1 "pqrs"
194 # define LETTER2 "0123456789abcdef"
199 # define MASTER_TMPL "/dev/ptyp_ "
200 # define SLAVE_TMPL "/dev/ttyp_ "
201 # define LETTER1 "0123456"
203 # define LETTER2 "0123456789abcdef"
207 if(ms_poll
== MS_VTY
|| ms_poll
== 0) {
208 strcpy(master_name
, MASTER_TMPL
);
209 strcpy(slave_name
, SLAVE_TMPL
);
211 for(X
= LETTER1
; *X
&& master
< 0; X
++)
212 for(Y
= LETTER2
; *Y
&& master
< 0; Y
++) {
213 master_name
[POS1
] = *X
;
214 master_name
[POS2
] = *Y
;
215 if((master
= open(master_name
, O_RDWR
)) >= 0) {
216 slave_name
[POS1
] = *X
;
217 slave_name
[POS2
] = *Y
;
218 if((slave
= open(slave_name
, O_RDWR
)) < 0)
223 if(master
>= 0 && slave
>= 0)
238 # define MASTER_TMPL "/dev/ptyp%d"
239 # define SLAVE_TMPL "/dev/ttyp%d"
244 if(ms_poll
== MS_PTY
|| ms_poll
== 0) {
247 for(i
= MIN_I
; i
<= MAX_I
&& master
< 0; i
++) {
248 sprintf(master_name
, MASTER_TMPL
, i
);
249 if((master
= open(master_name
, O_RDWR
)) >= 0) {
250 sprintf(slave_name
, SLAVE_TMPL
, i
);
251 if((slave
= open(slave_name
, O_RDWR
)) < 0)
256 if(master
>= 0 && slave
>= 0)
270 VT_Line
= strdup(strrchr(slave_name
, '/') + 1);
272 switch((VT_ChildPID
= fork())) {
285 i
= open(slave_name
, O_RDWR
);
286 o
= open(slave_name
, O_RDWR
);
287 e
= open(slave_name
, O_RDWR
);
291 if (ioctl(slave
, TIOCSCTTY
, (char *)NULL
) == -1)
295 tcsetpgrp(0, getpid());
301 execlp("/bin/login", "login", "-c", (char*)0);
308 VT_ChildDied
= False
;
311 signal(SIGCLD
, VT_SigCLD
);
313 signal(SIGHUP
, VT_SigEXIT
);
314 signal(SIGTERM
, VT_SigEXIT
);
315 signal(SIGINT
, VT_SigEXIT
);
316 signal(SIGQUIT
, VT_SigEXIT
);
318 memset(OutBuf
, 0, sizeof(OutBuf
));
320 _smb_setlen(OutBuf
, 1);
322 send_smb(Client
,OutBuf
);
331 VT_Output: transport data from socket to pty
333 int VT_Output(Buffer
)
339 if(VT_Status
!= VT_OPEN
)
342 len
= smb_len(Buffer
);
344 nb
= write(VT_Fd
, Buffer
+ 4, len
);
346 return((nb
== len
) ? 0 : -1);
351 VT_Input: transport data from pty to socket
353 int VT_Input(Buffer
, Size
)
360 if(VT_Status
!= VT_OPEN
)
363 memset(Buffer
, 0, Size
);
364 len
= read(VT_Fd
, Buffer
+ 4, MIN(VT_MAXREAD
, Size
));
366 _smb_setlen(Buffer
, len
);
373 VT_Process: main loop while in vt-mode
377 static int trans_num
= 0;
391 int last_keepalive
=0;
393 struct timeval to
, *top
;
398 t
= SMBD_SELECT_LOOP
*1000;
407 to
.tv_sec
= t
/ 1000;
408 to
.tv_usec
= t
- (to
.tv_sec
* 1000);
415 goto leave_VT_Process
;
417 n
= select(MAX(VT_Fd
, Client
) + 1, &si
, NULL
, NULL
, top
);
420 goto leave_VT_Process
;
426 extern int keepalive
;
428 counter
+= SMBD_SELECT_LOOP
;
432 if (keepalive
&& (counter
-last_keepalive
)>keepalive
) {
433 if (!send_keepalive(Client
))
434 goto leave_VT_Process
;
435 last_keepalive
= counter
;
440 if(FD_ISSET(VT_Fd
, &si
)) {
441 /* got input from vt */
442 nread
= VT_Input(OutBuffer
, MIN(BUFFER_SIZE
,lp_maxxmit()));
445 send_smb(Client
,OutBuffer
);
448 if(FD_ISSET(Client
, &si
)) {
449 /* got input from socket */
451 if(receive_smb(Client
,InBuffer
, 0)) {
452 msg_type
= CVAL(InBuffer
,0);
453 msg_flags
= CVAL(InBuffer
,1);
455 len
= smb_len(InBuffer
);
457 DEBUG(6,("got message type 0x%x of len 0x%x\n",msg_type
,len
));
461 DEBUG(3,("%s Transaction %d of length %d\n",timestring(),trans_num
,nread
));
466 nread
= construct_reply(InBuffer
,OutBuffer
,nread
,MIN(BUFFER_SIZE
,lp_maxxmit()));
469 if (nread
!= smb_len(OutBuffer
) + 4) {
470 DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
472 smb_len(OutBuffer
)));
474 send_smb(Client
,OutBuffer
);
479 goto leave_VT_Process
;
489 kill(VT_ChildPID, SIGHUP);
491 VT_Stop_utmp(VT_Line);