00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <config.h>
00036
00037 #include <assert.h>
00038 #include <errno.h>
00039 #include <signal.h>
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #ifndef _WIN32
00043 #include <sys/time.h>
00044 #include <sys/types.h>
00045 #include <unistd.h>
00046 #else
00047 #include <io.h>
00048 #endif
00049 #include "linebuf.h"
00050 #include "misc.h"
00051 #include "proto.h"
00052 #include "ringbuf.h"
00053 #include "secure.h"
00054
00055 #ifdef _WIN32
00056 static CRITICAL_SECTION signal_critical_section;
00057 static LPCRITICAL_SECTION signal_critical_section_ptr = NULL;
00058
00059 static unsigned char bounce_buf[RING_SIZE];
00060
00061
00062
00063
00064 static HANDLE bounce_empty;
00065
00066
00067
00068
00069 static HANDLE bounce_full;
00070
00071 static HANDLE ctrl_c_event;
00072 static int bounce_status, bounce_error;
00073
00074 #define SIGPIPE -1
00075 static void (*ctrl_handler)(int sig) = { SIG_DFL };
00076
00077
00078
00079
00080 static BOOL WINAPI
00081 w32_signal_handler(DWORD ctrl_type)
00082 {
00083 if (ctrl_type == CTRL_C_EVENT) {
00084 EnterCriticalSection(signal_critical_section_ptr);
00085 if (ctrl_handler != SIG_DFL) {
00086 ctrl_handler(SIGINT);
00087 LeaveCriticalSection(signal_critical_section_ptr);
00088 SetEvent(ctrl_c_event);
00089 return TRUE;
00090 } else
00091 LeaveCriticalSection(signal_critical_section_ptr);
00092 }
00093 return FALSE;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 static int
00103 sigaction(int signal, struct sigaction *action, struct sigaction *oaction)
00104 {
00105 assert(!oaction);
00106 assert(action);
00107
00108 if (signal == SIGPIPE)
00109 assert(action->sa_handler == SIG_IGN);
00110 else {
00111 assert(signal == SIGINT && action->sa_handler != SIG_IGN);
00112 if (ctrl_handler == action->sa_handler)
00113 return 0;
00114 if (signal_critical_section_ptr == NULL) {
00115 signal_critical_section_ptr = &signal_critical_section;
00116 InitializeCriticalSection(signal_critical_section_ptr);
00117 }
00118 EnterCriticalSection(signal_critical_section_ptr);
00119 if (!SetConsoleCtrlHandler(w32_signal_handler,
00120 action->sa_handler != SIG_DFL)) {
00121 errno = GetLastError();
00122 LeaveCriticalSection(signal_critical_section_ptr);
00123 return -1;
00124 }
00125 ctrl_handler = action->sa_handler;
00126 LeaveCriticalSection(signal_critical_section_ptr);
00127 }
00128 return 0;
00129 }
00130
00131
00132
00133
00134
00135
00136 static DWORD WINAPI
00137 stdin_read_thread(LPVOID lpParam)
00138 {
00139 for (;;) {
00140 if (WaitForSingleObject(bounce_empty, INFINITE) != WAIT_OBJECT_0)
00141 break;
00142 bounce_status = _read(0, bounce_buf, sizeof(bounce_buf));
00143 bounce_error = errno;
00144 if (bounce_status == 0) {
00145 if (_isatty(0)) {
00146 SetEvent(bounce_empty);
00147 continue;
00148 } else
00149 break;
00150 }
00151 SetEvent(bounce_full);
00152 }
00153 SetEvent(bounce_full);
00154 return 0;
00155 }
00156
00157
00158
00159
00160 static void
00161 sysdep_stdin_init(void)
00162 {
00163 bounce_empty = CreateEvent(NULL, FALSE, TRUE, "bounce_empty");
00164 bounce_full = CreateEvent(NULL, TRUE, FALSE, "bounce_full");
00165 ctrl_c_event = CreateEvent(NULL, FALSE, FALSE, "Ctrl_C");
00166 CreateThread(NULL, 0, stdin_read_thread, NULL, 0, NULL);
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 static int
00182 w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval* time)
00183 {
00184 HANDLE handles[3];
00185 SOCKET sock;
00186 int result, s_result, num_handles = 0;
00187 struct timeval tv_time = {0, 0};
00188 fd_set rdfd2;
00189
00190 if (rdfd->fd_count > 1) {
00191 sock = rdfd->fd_array[1];
00192 if (rdfd->fd_array[0])
00193 handles[num_handles++] = (HANDLE)rdfd->fd_array[0];
00194 else {
00195 handles[num_handles++] = ctrl_c_event;
00196 handles[num_handles++] = bounce_full;
00197 }
00198 } else {
00199 assert(rdfd->fd_count == 1);
00200 sock = rdfd->fd_array[0];
00201 }
00202 assert(wrfd->fd_count == 0 ||
00203 (wrfd->fd_count == 1 && wrfd->fd_array[0] == sock));
00204
00205 handles[num_handles++] = WSACreateEvent();
00206
00207 if (wrfd->fd_count > 0)
00208 WSAEventSelect(sock, handles[num_handles - 1],
00209 FD_READ | FD_WRITE | FD_CLOSE);
00210 else
00211 WSAEventSelect(sock, handles[num_handles - 1],
00212 FD_READ | FD_CLOSE);
00213
00214 result = WaitForMultipleObjects(num_handles, handles, 0, INFINITE);
00215 if (result < 0) {
00216 errno = GetLastError();
00217 WSACloseEvent(handles[num_handles - 1]);
00218 return -1;
00219 }
00220 WSACloseEvent(handles[num_handles - 1]);
00221
00222 if (num_handles == 3 && result == WAIT_OBJECT_0) {
00223 errno = EINTR;
00224 return -1;
00225 }
00226
00227 FD_ZERO(&rdfd2);
00228 FD_SET(sock, &rdfd2);
00229 s_result = select(sock + 1, &rdfd2, wrfd, NULL, &tv_time);
00230
00231 if (s_result < 0) {
00232 errno = WSAGetLastError();
00233 return s_result;
00234 }
00235
00236 *rdfd = rdfd2;
00237 if (num_handles == 3 && result == WAIT_OBJECT_0 + 1) {
00238 FD_SET((SOCKET)0, rdfd);
00239 s_result++;
00240 }
00241 if (num_handles == 2 && result == WAIT_OBJECT_0) {
00242 FD_SET((SOCKET)handles[0], rdfd);
00243 s_result++;
00244 }
00245 return s_result;
00246 }
00247
00248
00249
00250
00251
00252
00253 static int
00254 w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
00255 {
00256 int i, res;
00257
00258 if (fd)
00259 return ring_from_file(r, fd);
00260
00261 if (bounce_status < 0) {
00262 errno = bounce_error;
00263 res = bounce_status;
00264 } else {
00265 for (i = 0; i < bounce_status; i++) {
00266 if (ring_putc(r, bounce_buf[i]) == EOF) {
00267
00268 memmove(bounce_buf, bounce_buf + i, bounce_status - i);
00269 bounce_status -= i;
00270 return i;
00271 }
00272 }
00273 res = i;
00274 }
00275
00276 ResetEvent(bounce_full);
00277 SetEvent(bounce_empty);
00278 return res;
00279 }
00280 #define ring_from_file w32_ring_from_file_or_bounce_buf
00281 #define close(fd) w32_close_handle((fd))
00282 #define read(sock, buffer, buf_size) \
00283 w32_recv((sock), (buffer), (buf_size), 0)
00284 #define select(nfds, rd, wr, error, time) \
00285 w32_select((nfds), (rd), (wr), (error), (time))
00286 #define sigemptyset(mask) ((void)0)
00287 #else
00288 #define sysdep_stdin_init() ((void)0)
00289 #endif
00290
00291 #define EOF_COOKIE "ctld\n"
00292 #define INTR_COOKIE "\naborted\n"
00293
00294 int input_fd;
00295 int send_eof;
00296 static volatile sig_atomic_t send_intr;
00297
00298
00299
00300
00301
00302 static int
00303 recv_output(int sock)
00304 {
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 static enum {
00319 SCANNING_ID, BUFFERING, COPYING
00320 } state = SCANNING_ID;
00321 static int id;
00322 static struct lbuf lbuf;
00323 char buf[4096];
00324 ssize_t n;
00325 int i, ch, len;
00326 char *line;
00327
00328 n = read(sock, buf, sizeof(buf));
00329 if (n < 0)
00330 return -1;
00331
00332 for (i = 0; i < n; i++) {
00333 ch = buf[i];
00334 switch (state) {
00335 case SCANNING_ID:
00336 if (ch == '\n') {
00337
00338 lbuf_init(&lbuf);
00339 break;
00340 }
00341 lbuf_putc(&lbuf, ch);
00342 if (ch != ' ')
00343 break;
00344 line = lbuf_line(&lbuf);
00345 id = parseid(line);
00346 lbuf_init(&lbuf);
00347
00348 switch (id) {
00349 case C_PROMPT:
00350 case C_FLUSH:
00351 case C_EXECUTE:
00352 case C_EXIT:
00353 case C_FLASH:
00354 case C_INFORM:
00355 case C_PIPE:
00356 case C_REDIR:
00357 state = BUFFERING;
00358 break;
00359 default:
00360
00361 case C_DATA:
00362 state = COPYING;
00363 break;
00364 }
00365 break;
00366
00367 case BUFFERING:
00368 len = lbuf_putc(&lbuf, ch);
00369 if (len) {
00370 line = lbuf_line(&lbuf);
00371 servercmd(id, line, len);
00372 lbuf_init(&lbuf);
00373 state = SCANNING_ID;
00374 }
00375 break;
00376
00377 case COPYING:
00378 outch(ch);
00379 if (ch == '\n')
00380 state = SCANNING_ID;
00381 }
00382 }
00383
00384 return n;
00385 }
00386
00387
00388
00389
00390
00391 static int
00392 recv_input(int fd, struct ring *inbuf)
00393 {
00394 static struct lbuf cmdbuf;
00395 int n, i, ch;
00396 char *line;
00397 int res = 1;
00398
00399 n = ring_from_file(inbuf, fd);
00400 if (n < 0)
00401 return -1;
00402 if (n == 0) {
00403
00404 if (lbuf_len(&cmdbuf)) {
00405
00406 ring_putc(inbuf, '\n');
00407 n++;
00408 }
00409
00410
00411
00412
00413 res = 0;
00414 }
00415
00416
00417 for (i = -n; i < 0; i++) {
00418 ch = ring_peek(inbuf, i);
00419 assert(ch != EOF);
00420 if (ch != '\r' && lbuf_putc(&cmdbuf, ch)) {
00421 line = lbuf_line(&cmdbuf);
00422 if (auxfp)
00423 fputs(line, auxfp);
00424 save_input(line);
00425 lbuf_init(&cmdbuf);
00426 }
00427 }
00428
00429 return res;
00430 }
00431
00432 static void
00433 intr(int sig)
00434 {
00435 send_intr = 1;
00436 }
00437
00438
00439
00440
00441
00442
00443 int
00444 play(int sock)
00445 {
00446
00447
00448
00449
00450
00451
00452 struct sigaction sa;
00453 struct ring inbuf;
00454 int eof_fd0;
00455 fd_set rdfd, wrfd;
00456 int n;
00457
00458 sa.sa_flags = 0;
00459 sigemptyset(&sa.sa_mask);
00460 sa.sa_handler = intr;
00461 sigaction(SIGINT, &sa, NULL);
00462 sa.sa_handler = SIG_IGN;
00463 sigaction(SIGPIPE, &sa, NULL);
00464
00465 ring_init(&inbuf);
00466 eof_fd0 = send_eof = send_intr = 0;
00467 input_fd = 0;
00468 sysdep_stdin_init();
00469
00470 for (;;) {
00471 FD_ZERO(&rdfd);
00472 FD_ZERO(&wrfd);
00473
00474
00475
00476
00477
00478
00479 if (!send_intr && !send_eof && !eof_fd0 && ring_space(&inbuf))
00480 FD_SET(input_fd, &rdfd);
00481
00482 if (send_intr || send_eof || ring_len(&inbuf))
00483 FD_SET(sock, &wrfd);
00484
00485 FD_SET(sock, &rdfd);
00486
00487 n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
00488 if (n < 0) {
00489 if (errno != EINTR) {
00490 perror("select");
00491 return -1;
00492 }
00493 }
00494
00495 if (send_eof
00496 && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
00497 send_eof--;
00498 if (send_intr
00499 && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0)
00500 send_intr = 0;
00501
00502 if (n < 0)
00503 continue;
00504
00505
00506 if (FD_ISSET(input_fd, &rdfd)) {
00507 n = recv_input(input_fd, &inbuf);
00508 if (n < 0) {
00509 perror("read stdin");
00510 n = 0;
00511 }
00512 if (n == 0) {
00513
00514 send_eof++;
00515 if (input_fd) {
00516
00517 close(input_fd);
00518 input_fd = 0;
00519 } else {
00520
00521 eof_fd0 = 1;
00522 sa.sa_handler = SIG_DFL;
00523 sigaction(SIGINT, &sa, NULL);
00524 }
00525 }
00526 }
00527
00528
00529 if (FD_ISSET(sock, &wrfd)) {
00530 n = ring_to_file(&inbuf, sock);
00531 if (n < 0) {
00532 perror("write socket");
00533 return -1;
00534 }
00535 }
00536
00537
00538 if (FD_ISSET(sock, &rdfd)) {
00539 n = recv_output(sock);
00540 if (n < 0) {
00541 perror("read socket");
00542 return -1;
00543 }
00544 if (n == 0)
00545 return 0;
00546 }
00547 }
00548 }