unix_interact.c 8.69 KB
Newer Older
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
/* unix_interact.c
 *
 * Interact with the user.
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1999 Niels Mller
 *
 * 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 (at your option) 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "interact.h"

#include "format.h"
#include "io.h"
#include "resource.h"
31
#include "suspend.h"
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
#include "tty.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>
#include <errno.h>
#include <string.h>

#include <signal.h>

#if HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "unix_interact.c.x"

/* Depends on the tty being line buffered */
static int
read_line(int fd, UINT32 size, UINT8 *buffer)
{
  UINT32 i = 0;

  while (i < size)
    {
      int res = read(fd, buffer + i, size - i);
      if (!res)
	/* User pressed EOF (^D) */
	return i;
      else if (res < 0)
	switch(errno)
	  {
	  case EAGAIN:
	  case EINTR:
	    break;
	  default:
	    /* I/O error */
	    werror("unix_interact.c: read_line: %z (errno = %i)\n",
		   errno, STRERROR(errno));
	    return 0;
	  }
      else
	{
	  UINT32 j;
	  for (j = 0; j < (unsigned) res; j++, i++)
	    if (buffer[i] == '\n')
	      return i;
	}
    }
  /* We have filled our buffer already; continue reading until end of line */
#define BUFSIZE 512
  for (;;)
    {
      UINT8 b[BUFSIZE];
      int res = read(fd, b, BUFSIZE);
      if (!res)
	/* EOF */
	return size;
      else if (res < 0)
	switch(errno)
	  {
	  case EAGAIN:
	  case EINTR:
	    break;
	  default:
	    /* I/O error */
	    werror("tty_read_line: %z (errno = %i)\n",
		   errno, strerror(errno));
	    return 0;
	  }
      else
	{
	  UINT32 j;
	  for (j = 0; j < (unsigned) res; j++)
	    if (b[j] == '\n')
	      return res;
	}
    }
#undef BUFSIZE
}


/* NOTE: If there are more than one instance of this class, window
 * changes can be missed, as only one of them will have its signal
 * handler installed properly. */

/* GABA:
   (class
     (name window_subscriber)
     (super resource)
     (vars
126
       (interact object unix_interact)
127
128
129
130
       (next object window_subscriber)
       (callback object window_change_callback)))
*/

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
static void
do_kill_window_subscriber(struct resource *s)
{
  CAST(window_subscriber, self, s);

  if (self->super.alive)
    {
      self->super.alive = 0;
      assert(self->interact->nsubscribers);
      assert(self->interact->winch_handler);
      
      if (!--self->interact->nsubscribers)
        {
          KILL_RESOURCE(self->interact->winch_handler);
          self->interact->winch_handler = NULL;
        }
    }
}

150
151
/* FIXME: Remove subscribers list, and register multiple liboop signal
 * handlers instead? */
152
153
154
155
156
157
/* GABA:
   (class
     (name unix_interact)
     (super interact)
     (vars
       (tty_fd . int)
158
159
160
       ; Signal handler
       (winch_handler object resource)
       (nsubscribers . unsigned)
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
       (subscribers object window_subscriber)))
*/

#define IS_TTY(self) ((self)->tty_fd >= 0)

static int
unix_is_tty(struct interact *s)
{
  CAST(unix_interact, self, s);

  return IS_TTY(self);
}

/* FIXME: Rewrite to operate on tty_fd. */
static struct lsh_string *
unix_read_password(struct interact *s UNUSED,
		   UINT32 max_length UNUSED,
178
		   const struct lsh_string *prompt, int free)
179
{
180
  /* NOTE: Ignores max_length; instead getpass's limit applies. */
181
  
182
183
  char *password;
  const char *cprompt = lsh_get_cstring(prompt);
184

185
186
187
188
189
190
  if (!cprompt)
    {
      if (free)
	lsh_string_free(prompt);
      return NULL;
    }
191
  /* NOTE: This function uses a static buffer. */
192
  password = getpass(cprompt);
193

194
195
196
  if (free)
    lsh_string_free(prompt);
  
197
198
199
  if (!password)
    return NULL;
  
200
  return make_string(password);
201
202
203
204
205
}


static int
unix_yes_or_no(struct interact *s,
206
207
	       const struct lsh_string *prompt,
	       int def, int free)
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
{
#define TTY_BUFSIZE 10

  CAST(unix_interact, self, s);
  if (!IS_TTY(self) || quiet_flag)
    {
      if (free)
	lsh_string_free(prompt);
      return def;
    }
  else    
    {
      UINT8 buffer[TTY_BUFSIZE];
      const struct exception *e;
  
      e = write_raw(self->tty_fd, prompt->length, prompt->data);

      if (free)
	lsh_string_free(prompt);

      if (e)
	return def;

      if (!read_line(self->tty_fd, TTY_BUFSIZE, buffer))
	return def;

      switch (buffer[0])
	{
	case 'y':
	case 'Y':
	  return 1;
	default:
	  return 0;
	}
#undef TTY_BUFSIZE
    }
}

/* GABA:
   (class
     (name unix_termios)
     (super terminal_attributes)
     (vars
       (ios . "struct termios")))
*/

static struct terminal_attributes *
do_make_raw(struct terminal_attributes *s)
{
  CAST(unix_termios, self, s);
  CLONED(unix_termios, res, self);

  CFMAKERAW(&res->ios);

262
263
264
265
266
267
  /* Modify VMIN and VTIME, to save some bandwidth and make traffic
   * analysis of interactive sessions a little harder. */
  res->ios.c_cc[VMIN] = 4;
  /* Inter-character timer, in units of 0.1s */
  res->ios.c_cc[VTIME] = 1;
  
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
  return &res->super;
}

static struct lsh_string *
do_encode(struct terminal_attributes *s)
{
  CAST(unix_termios, self, s);
  return tty_encode_term_mode(&self->ios);
}

static struct terminal_attributes *
unix_get_attributes(struct interact *s)
{
  CAST(unix_interact, self, s);

  if (!IS_TTY(self))
    return NULL;
  else
    {
      NEW(unix_termios, res);
      res->super.make_raw = do_make_raw;
      res->super.encode = do_encode;
      
      if (!tty_getattr(self->tty_fd, &res->ios) < 0)
	{
	  KILL(res);
	  return NULL;
	}
      return &res->super;
    }
}

static int
unix_set_attributes(struct interact *s,
		    struct terminal_attributes *a)
{
  CAST(unix_interact, self, s);
  CAST(unix_termios, attr, a);

  return IS_TTY(self)
    && tty_setattr(self->tty_fd, &attr->ios);
}

static int
unix_window_size(struct interact *s,
		 struct terminal_dimensions *d)
{
  CAST(unix_interact, self, s);

  return IS_TTY(self)
318
    && tty_getwinsize(self->tty_fd, d);
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
}


/* GABA:
   (class
     (name winch_handler)
     (super lsh_callback)
     (vars
       (interact object unix_interact)))
*/

static void
do_winch_handler(struct lsh_callback *s)
{
  CAST(winch_handler, self, s);
  struct unix_interact *i = self->interact;
335
336

  assert(!!i->nsubscribers == !!i->winch_handler);
337
338
339
340
341
  
  if (i->subscribers)
    {
      struct window_subscriber *s;
      struct window_subscriber **s_p;
342
      unsigned alive;
343

344
      for (alive = 0, s_p = &i->subscribers; (s = *s_p) ;)
345
346
347
348
349
350
351
	{
	  if (!s->super.alive)
	    *s_p = s->next;
	  else
	    {
	      WINDOW_CHANGE_CALLBACK(s->callback, &i->super);
	      s_p = &s->next;
352
              alive++;
353
354
	    }
	}
355
356

      assert(alive == i->nsubscribers);
357
358
359
360
361
362
363
364
365
366
367
368
369
    }
}

static struct lsh_callback *
make_winch_handler(struct unix_interact *i)
{
  NEW(winch_handler, self);
  self->super.f = do_winch_handler;
  self->interact = i;

  return &self->super;
}

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
static struct resource *
unix_window_change_subscribe(struct interact *s,
			     struct window_change_callback *callback)
{
  CAST(unix_interact, self, s);

  NEW(window_subscriber, subscriber);

  init_resource(&subscriber->super, do_kill_window_subscriber);

  subscriber->interact = self;
  subscriber->next = self->subscribers;
  subscriber->callback = callback;

  self->subscribers = subscriber;
  self->nsubscribers++;
  
  if (!self->winch_handler)
    {
      /* This is the first subscriber */
      self->winch_handler
391
        = io_signal_handler(SIGWINCH,
392
393
394
395
396
397
                            make_winch_handler(self));
    }
  
  return &subscriber->super;
}

398
struct interact *
399
make_unix_interact(void)
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
{
  NEW(unix_interact, self);
  
  self->super.is_tty = unix_is_tty;
  self->super.read_password = unix_read_password;
  self->super.yes_or_no = unix_yes_or_no;
  self->super.get_attributes = unix_get_attributes;
  self->super.set_attributes = unix_set_attributes;
  self->super.window_size = unix_window_size;
  self->super.window_change_subscribe = unix_window_change_subscribe;

  self->tty_fd = -1;
  
#if HAVE_STDTTY_FILENO
  if (isatty(STDTTY_FILENO))
    self->tty_fd = STDTTY_FILENO;
#else /* ! HAVE_STDTTY_FILENO */
  self->tty_fd = open("/dev/tty", O_RDWR);
#endif

420
  if (self->tty_fd >= 0)
Niels Möller's avatar
Niels Möller committed
421
422
    /* Restore and reset tty if process is suspended. */      
    suspend_handle_tty(self->tty_fd);
423

424
425
  return &self->super;
}