lshd-pty-helper.c 12.7 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
/* lshd-pty-helper.c
 *
 * Helper program for managing pty:s. An unprivileged process
 * communicates with this program via a AF_UNIX socket. Either a named
 * socket, or socket pair(s) provided as stdin and stdout.
 */

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 2006 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
 */

Niels Möller's avatar
Niels Möller committed
27
28
29
30
#if HAVE_CONFIG_H
#include "config.h"
#endif

31
#include <assert.h>
Niels Möller's avatar
Niels Möller committed
32
33
34
35
36
37
38
39
40
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <unistd.h>

#include <sys/types.h>
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
#include <sys/stat.h>

#include <fcntl.h>
#include <grp.h>
#include <utmpx.h>

#include "environ.h"
#include "pty-helper.h"

#ifndef GROUP_TTY
#define GROUP_TTY "tty"
#endif

#ifndef GROUP_SYSTEM
#define GROUP_SYSTEM "system"
#endif

/* Includes user, group and other bits, as well as the suid, sgid and
   sticky bit. */
#ifndef ACCESS_MASK
#define ACCESS_MASK 07777
#endif

/* Desired tty access bits (rw--w----). Can we gain any portability by
   writing S_IRUSR | S_IWUSR | S_IWGRP ? */
#ifndef ACCESS_TTY
#define ACCESS_TTY 0620
#endif

static const char *wtmp_file;
Niels Möller's avatar
Niels Möller committed
71

72
static void
Niels Möller's avatar
Niels Möller committed
73
74
75
76
77
78
79
die(const char *format, ...)
#if __GNUC___
     __attribute__((__format__ (__printf__,1, 2)))
     __attribute__((__noreturn__))
#endif
     ;

80
static void
Niels Möller's avatar
Niels Möller committed
81
82
83
84
85
86
87
88
89
90
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  exit(EXIT_FAILURE);
}

91
92
93
94
95
96
97
98
99
static void
werror(const char *format, ...)
#if __GNUC___
     __attribute__((__format__ (__printf__,1, 2)))
#endif
     ;

static void
werror(const char *format, ...)
Niels Möller's avatar
Niels Möller committed
100
{
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);
}

/* The state associated with a pty and an utmp entry. */
struct pty_object
{
  char free;

  /* Non-zero if we have an utmp entry to clean up. */
  char active;
  
  /* The client's uid. */
Niels Möller's avatar
Niels Möller committed
116
117
  uid_t uid;

118
119
  /* Name of slave tty */
  const char *tty;
Niels Möller's avatar
Niels Möller committed
120

121
122
123
124
  struct utmpx entry;
};

struct pty_state
Niels Möller's avatar
Niels Möller committed
125
{
126
127
128
129
130
131
132
133
  /* Group that should own the slave tty. */
  gid_t tty_gid;

  /* Expected user id, or -1 if unset. */
  uid_t uid;

  unsigned nobjects;
  struct pty_object *objects;
Niels Möller's avatar
Niels Möller committed
134
135
};

136
137
static void
init_pty_state(struct pty_state *state, uid_t uid)
Niels Möller's avatar
Niels Möller committed
138
{
139
140
141
142
143
144
145
146
  struct group *grp;

  /* Points to static area */
  grp = getgrnam(GROUP_TTY);

  if (!grp)
    /* On AIX, tty:s have group "system", not "tty" */
    grp = getgrnam(GROUP_SYSTEM);
Niels Möller's avatar
Niels Möller committed
147

148
  state->tty_gid = grp ? grp->gr_gid : (gid_t) -1;
Niels Möller's avatar
Niels Möller committed
149

150
  state->uid = uid;
Niels Möller's avatar
Niels Möller committed
151

152
153
154
  state->nobjects = 0;
  state->objects = NULL;
}
Niels Möller's avatar
Niels Möller committed
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
static struct pty_object *
pty_object_alloc(struct pty_state *state, unsigned *index)
{
  struct pty_object *pty = NULL;
  unsigned i;
  
  for (i = 0; i < state->nobjects; i++)
    if (state->objects[i].free)
      {
	*index = i;
	pty = state->objects + i;
	break;
      }

  if (!pty)
Niels Möller's avatar
Niels Möller committed
171
    {
172
173
174
175
176
177
178
179
      /* Try reallocating */
      size_t n = 2*state->nobjects + 10;
      void *p = realloc(state->objects, n*sizeof(*state->objects));

      if (!p)
	return NULL;

      state->objects = p;
Niels Möller's avatar
Niels Möller committed
180
      
181
182
183
184
185
186
187
      *index = state->nobjects;
      pty = state->objects + state->nobjects;

      for (i = state->nobjects; i < n; i++)
	state->objects[i].free = 1;

      state->nobjects = n;
Niels Möller's avatar
Niels Möller committed
188
189
    }

190
191
192
  memset(pty, 0, sizeof(*pty));

  return pty;
Niels Möller's avatar
Niels Möller committed
193
194
}

195
196
static void
pty_object_free(struct pty_state *state, unsigned index)
Niels Möller's avatar
Niels Möller committed
197
{
198
199
200
201
  assert(index < state->nobjects);
  assert(!state->objects[index].free);
  
  free((void *) state->objects[index].tty);
Niels Möller's avatar
Niels Möller committed
202
  
203
204
  state->objects[index].free = 1;
}
Niels Möller's avatar
Niels Möller committed
205

206
207
208
/* Sets the permissions on the slave pty suitably for use by USER.
 * This function is derived from the grantpt function in
 * sysdeps/unix/grantpt.c in glibc-2.1. */
Niels Möller's avatar
Niels Möller committed
209

210
211
212
213
214
/* Returns errno value on error */
static int
pty_set_permissions(const char *name, uid_t uid, gid_t gid)
{
  struct stat st;
Niels Möller's avatar
Niels Möller committed
215

216
217
  if (stat(name, &st) < 0)
    return errno;
Niels Möller's avatar
Niels Möller committed
218

219
220
221
222
223
  /* Make sure that the user owns the device. */
  if (st.st_uid == uid)
    uid = -1;
  if (st.st_gid == gid)
    gid = -1;
Niels Möller's avatar
Niels Möller committed
224

225
226
227
228
229
230
231
232
233
234
235
236
237
  if (uid != (uid_t) -1 || gid != (gid_t) -1)
    if (chown(name, uid, gid) < 0)
      return errno;

  /* Make sure the permission mode is set to readable and writable
   * by the owner, and writable by the group. */

  if ( (st.st_mode & ACCESS_MASK) != ACCESS_TTY
       && chmod(name, ACCESS_TTY) < 0)
    return errno;

  /* Everything is fine */
  return 0;
Niels Möller's avatar
Niels Möller committed
238
239
240
}

static int
241
strprefix_p(const char *prefix, const char *s)
Niels Möller's avatar
Niels Möller committed
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
  unsigned i;

  for (i = 0; prefix[i]; i++)
    if (prefix[i] != s[i])
      return 0;
  return 1;
}

#define CP(dst, src) (strncpy((dst), (src), sizeof(dst)))
#define CPN(dst, offset, src) \
  (strncpy((dst)+(offset), (src), sizeof(dst)-(offset)))

static void
process_request(struct pty_state *state,
		const struct pty_message *request,
		struct pty_message *response)
{
  struct pty_object *pty;

  response->header.type = 0;
  response->header.ref = -1;
  response->header.length = 0;
  response->data = NULL;
  response->has_creds = 0;
  response->fd = -1;

  /* Require credentials for all requests */
  if (!request->has_creds)
    {
      werror("Missing credentials.\n");
      response->header.type = EPERM;
      return;
    }

  if (state->uid != (uid_t) -1
      && request->creds.uid != state->uid)
    {
      response->header.type = EPERM;
      return;
    }

  if (request->header.ref == -1)
    pty = NULL;
  else if (request->header.ref < 0
	   || (unsigned) request->header.ref >= state->nobjects)
    {
      response->header.type = EINVAL;
      return;
    }
  else
    {
      pty = &state->objects[request->header.ref];
      if (pty->free)
	{
	  response->header.type = EINVAL;
	  return;
	}
    }

  switch(request->header.type)
    {
    case PTY_REQUEST_CREATE:
      werror("PTY_REQUEST_CREATE\n");
      if (request->header.ref != -1)
	{
	  response->header.type = EINVAL;
	}
      else
	{
	  unsigned index;

	  pty = pty_object_alloc(state, &index);
	  if (!pty)
	    {
	      response->header.type = ENOMEM;
	    }
	  else
	    {
	      pty->uid = request->creds.uid;

	      if (request->fd != -1)
		{
		  /* Client can supply the master fd, so we can get
		     the slave tty name from there. */
		  char *tty = ptsname(request->fd);
		  if (tty)
		    tty = strdup(tty);
		  
		  if (!tty)
		    {
		      response->header.type = errno;
		      pty_object_free(state, index);
		      return;
		    }
		  pty->tty = tty;
		  /* FIXME: We could stat the tty and check ownership? */
		}
	      response->header.ref = index;
	    }
	}
      break;

    case PTY_REQUEST_MASTER:
      werror("PTY_REQUEST_MASTER\n");
      response->header.type = EOPNOTSUPP;

      /* Useful only for old bsd-style tty allocation. When using
	 /devptmx and grantpt, it's better to let the client create
	 the pty, since it will get the right ownership from the
	 start. */
#if 0
      if (request->header.ref != -1)
	{
	  response->header.type = EINVAL;
	}
      else
	{
	  unsigned index;
	  const char *tty;

	  pty = pty_object_alloc(state, &index);
	  if (!pty)
	    {
	      response->header.type = ENOMEM;
	      return;
	    }

	  response->fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
	  if (response->fd < 0)
	    {
	      pty_object_free(state, index);
	      response->header.type = errno;
	      return;
	    }
      
	  tty = ptsname(response->fd);
	  if (!tty)
	    {
	      response->header.type = errno;
	    fail_and_close:

	      pty_object_free(state, index);
	      close(response->fd);
	      response->fd = -1;
	      return;
	    }

	  /* Copy, since ptsname returns a statically allocated value */
	  pty->tty = strdup(tty);
	  if (!pty->tty)
	    {
	      response->header.type = errno;
	      goto fail_and_close;
	    }

	  pty->uid = request->creds.uid;
      
	  if (pty->uid == getuid())
	    {
	      /* Use standard grantpt call */
	      if (grantpt(response->fd) < 0)
		{
		  response->header.type = errno;
		  goto fail_and_close;
		} 
	    }
	  else
	    {
	      gid_t gid = state->tty_gid;
	      if (gid == (gid_t) -1)
		gid = request->creds.gid;
	  
	      response->header.type
		= pty_set_permissions(pty->tty, pty->uid, gid);

	      if (response->header.type)
		goto fail_and_close;
	    }

	  if (unlockpt(response->fd < 0))
	    {
	      response->header.type = errno;
	      goto fail_and_close;
	    } 

	  response->header.ref = index;
	  response->length = strlen(pty->tty);
	  response->data = pty->tty;
	}
#endif
      break;

    case PTY_REQUEST_LOGIN:
      werror("PTY_REQUEST_LOGIN\n");
      if (!pty)
	{
	  response->header.type = EINVAL;
	}
      else if (request->creds.uid != pty->uid)
	{
	  response->header.type = EPERM;
	}
      else if (pty->active)
	{
	  response->header.type = EEXIST;
	}
      else
	{
	  response->header.ref = request->header.ref;

	  memset(&pty->entry, 0, sizeof(pty->entry));

	  pty->entry.ut_type = USER_PROCESS;
	  pty->entry.ut_pid = request->creds.pid;

#if 0
	  CP(pty->entry.ut_user, state->user_name);
#endif
	  
	  gettimeofday(&pty->entry.ut_tv, NULL);
	  
	  if (pty->tty)
	    {
	      /* Set tty-related fields, and update utmp */

	      const char *line;
	      
	      if (strprefix_p("/dev/", pty->tty))
		line = pty->tty + 5;
	      else
		line = pty->tty;
	  
	      CP(pty->entry.ut_line, line);

	      if (strprefix_p("pts/", line))
		{
		  pty->entry.ut_id[0] = 'p';
		  CPN(pty->entry.ut_id, 1, line + 4);
		}
	      else if (strprefix_p("tty", line))
		CP(pty->entry.ut_id, line + 3);
	      else
		CP(pty->entry.ut_id, line);

	      setutxent();

	      if (!pututxline(&pty->entry))
		werror("pututxline failed.\n");
	    }
	  
	  updwtmpx(wtmp_file, &pty->entry);

	  pty->active = 1;
	  break;
	}

    case PTY_REQUEST_LOGOUT:
      werror("PTY_REQUEST_LOGOUT\n");
      if (!pty)
	{
	  response->header.type = EINVAL;
	}
      else
	{
	  if (pty->active)
	    {
	      pty->entry.ut_type = DEAD_PROCESS;
	      gettimeofday(&pty->entry.ut_tv, NULL);

	      if (pty->tty)
		{
		  setutxent();

		  if (!pututxline(&pty->entry))
		    werror("pututxline failed.\n");
		}
	      updwtmpx(wtmp_file, &pty->entry);
	    }
	  pty_object_free(state, request->header.ref);
	}
      break;

    case PTY_REQUEST_DESTROY:
      werror("PTY_REQUEST_DESTROY\n");
      if (!pty)
	{
	  response->header.type = EINVAL;
	}
      else
	{
	  /* This request shouldn't rellay be used for "active" ptys,
	     i.e., if we have an utmp entry to clean up. */
	     
	  pty_object_free(state, request->header.ref);
	}
      break;

    default:
      response->header.type = EINVAL;
      break;
    }
Niels Möller's avatar
Niels Möller committed
544
545
546
}

int
547
main (int argc UNUSED, char **argv UNUSED)
Niels Möller's avatar
Niels Möller committed
548
{
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  struct pty_state state;
  struct pty_message request;
  struct pty_message response;
  const char *utmp_file;
  int yes = 1;
  int err;

  utmp_file = getenv(ENV_LSHD_UTMP);
  if (utmp_file)
    {
      werror("utmp_file: %s\n", utmp_file);
      utmpxname(utmp_file);
    }
  
  wtmp_file = getenv(ENV_LSHD_WTMP);
  if (!wtmp_file)
    wtmp_file = _PATH_WTMPX;
Niels Möller's avatar
Niels Möller committed
566

567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  werror("wtmp_file: %s\n", wtmp_file);
  
  /* Don't know if this is standard, but it's required on linux. */
  if (setsockopt(STDIN_FILENO, SOL_SOCKET, SO_PASSCRED,
		 &yes, sizeof(yes)) < 0)
    {
      die("setsockopt SO_PASSCRED failed: %s.\n", strerror(errno));
      return EXIT_FAILURE;
    }
  
  init_pty_state(&state, getuid());
  if (!state.uid )
    {
      /* Currently, doesn't support running as root. And it makes no
	 sense to run as root unless SCM_CREDENTIALS passing is
	 supported. */
      return EXIT_FAILURE;
    }
Niels Möller's avatar
Niels Möller committed
585

586
587
588
589
590
  /* Do we need to clean up on exit? If the client, i.e.,
     lshd-connection, dies, the processes will be inherited by init.
     Than init should be able to clean up. The problematic case is if
     our client calls waitpid, and then dies before requesting a
     logout action, but we can probably just ignore that. */
Niels Möller's avatar
Niels Möller committed
591

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  for (;;)
    {
      err = pty_recv_message(STDIN_FILENO, &request);
      if (err == -1)
	break;
      else if (err)
	die("pty_recv_message failed: %s.\n", strerror(err));

      process_request(&state, &request, &response);
      if (request.fd != -1)
	close(request.fd);

      err = pty_send_message(STDOUT_FILENO, &response);

      if (response.fd != -1)
	close(response.fd);
      
      if (err)
	die("pty_recv_message failed: %s.\n", strerror(err));
    }
  return EXIT_SUCCESS;
Niels Möller's avatar
Niels Möller committed
613
}