aboutsummaryrefslogtreecommitdiffstats
path: root/src/passphrase.c
blob: 2978c80a4ce016e113f7462204c93067b06281f9 (plain) (blame)
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
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
544
545
546
547
548
549
550
/**
 * libpassphrase – Personalisable library for TTY passphrase reading
 * 
 * Copyright © 2013, 2014, 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <termios.h>
#include <sys/wait.h>

#define PASSPHRASE_USE_DEPRECATED
#include "passphrase.h"
#include "passphrase_helper.h"


#ifndef START_PASSPHRASE_LIMIT
# define START_PASSPHRASE_LIMIT  32
#endif
#ifndef DEFAULT_PASSPHRASE_METER
# define DEFAULT_PASSPHRASE_METER  "passcheck"
#endif



#ifdef PASSPHRASE_METER
struct passcheck_state
{
  int pipe_rw[2];
  pid_t pid;
  int flags;
};

static char* strength = NULL;
static size_t strength_size = 0;
#endif /* PASSPHRASE_METER */



#ifndef PASSPHRASE_REALLOC
static char* xrealloc(char* array, size_t cur_size, size_t new_size)
{
  char* rc = malloc(new_size * sizeof(char));
  size_t i;
  if (rc)
    for (i = 0; i < cur_size; i++)
	*(rc + i) = *(array + i);
  passphrase_wipe(array, cur_size);
  free(array);
  return rc;
}
#else /* !PASSPHRASE_REALLOC */
# define xrealloc(array, _cur_size, new_size)  realloc(array, (new_size) * sizeof(char))
#endif /* !PASSPHRASE_REALLOC */


#ifdef PASSPHRASE_METER
static void passcheck_start(struct passcheck_state* state, int flags)
{
  const char* command;
  int pipe_rw[2] = { -1, -1 };
  int exec_rw[2] = { -1, -1 };
  pid_t pid;
  ssize_t n;
  int i = 0;
  
  state->pid = -1;
  state->flags = (flags & PASSPHRASE_READ_NEW) ? (flags ^ PASSPHRASE_READ_NEW) : 0;
  if (state->flags == 0)
    return;
  
  if (state->flags & PASSPHRASE_READ_BELOW_FREE)
    state->flags &= ~PASSPHRASE_READ_SCREEN_FREE;
  
  command = getenv("LIBPASSPHRASE_METER");
  if (!command || !*command)
    command = DEFAULT_PASSPHRASE_METER;
  
  xpipe(state->pipe_rw);
  xpipe(pipe_rw);
  xpipe(exec_rw);
  /* ‘Their integer values shall be the two lowest available at the time of the pipe() call’ [man 3p pipe]
   * This guarantees (unless the application is doing something stupid) that the file desriptors
   * in exec_rw[1] is not stdin, stdout, stderr, or 0 (required by FD_CLOEXEC to take affect), assuming
   * stdin, stdout, and stderr are 0, 1, and 2, respectively, as specified in `man 3p stdin`. */
  
  if (fcntl(exec_rw[1], F_SETFD, FD_CLOEXEC) == -1)
    goto fail;
  
  pid = fork();
  if (pid == -1)
    {
      state->flags = 0;
      goto fail;
    }
  
  close(exec_rw[!!pid]), exec_rw[!!pid] = -1;
  close(state->pipe_rw[!!pid]), state->pipe_rw[!!pid] = -1;
  close(pipe_rw[!pid]), pipe_rw[!pid] = -1;
  state->pipe_rw[!!pid] = pipe_rw[!!pid], pipe_rw[!!pid] = -1;
  
  if (pid == 0)
    {
      gid_t gid = getgid(), egid = getegid();
      uid_t uid = getuid(), euid = geteuid();
      int fd;
      
      if ((state->pipe_rw[0] != STDIN_FILENO) && (state->pipe_rw[1] == STDIN_FILENO))
	{
	  fd = dup(state->pipe_rw[1]);
	  if (fd == -1)
	    goto child_fail;
	  state->pipe_rw[1] = fd;
	}
      for (i = 0; i <= 1; i++)
	if (state->pipe_rw[i] != i)
	  {
	    close(i);
	    fd = dup2(state->pipe_rw[i], i);
	    if (fd == -1)
	      goto child_fail;
	    close(state->pipe_rw[i]);
	    state->pipe_rw[i] = fd;
	  }
      
      close(STDERR_FILENO);
      
      if (egid != gid)
	if (setregid(gid, gid) && gid)
	  goto child_fail;
      if (euid != uid)
	if (setreuid(uid, uid) && uid)
	  goto child_fail;
      
      execlp(command, command, "-r", NULL);
    child_fail:
      n = write(exec_rw[1], &i, sizeof(i));
      _exit(!!n);
    }
  
 rewait:
  n = read(exec_rw[0], &i, sizeof(i));
  if ((n < 0) && (errno == EINTR))
    goto rewait;
  if (n)
    {
    rereap:
      if ((waitpid(pid, &i, 0) == -1) && (errno == EINTR))
	goto rereap;
      goto fail;
    }
  
  if (state->flags & PASSPHRASE_READ_SCREEN_FREE)
    {
      struct termios stty;
      struct termios saved_stty;
      tcgetattr(STDERR_FILENO, &stty);
      saved_stty = stty;
      stty.c_oflag &= (tcflag_t)~ONLCR;
      tcsetattr(STDERR_FILENO, TCSAFLUSH, &stty);
      fprintf(stderr, "\n\033[A");
      fflush(stderr);
      tcsetattr(STDERR_FILENO, TCSAFLUSH, &saved_stty);
    }
  
  close(exec_rw[0]);
  state->pid = pid;
  return;
 fail:
  if (state->pipe_rw[0] >= 0)  close(state->pipe_rw[0]);
  if (state->pipe_rw[1] >= 0)  close(state->pipe_rw[1]);
  if (pipe_rw[0] >= 0)  close(pipe_rw[0]);
  if (pipe_rw[1] >= 0)  close(pipe_rw[1]);
  if (exec_rw[0] >= 0)  close(exec_rw[0]);
  if (exec_rw[1] >= 0)  close(exec_rw[1]);
  state->pid = -1;
  state->flags = 0;
}


static void passcheck_stop(struct passcheck_state* state)
{
  int _status;
  
  if (state->flags == 0)
    return;
  
  close(state->pipe_rw[0]);
  close(state->pipe_rw[1]);
  
  free(strength), strength = NULL;
  strength_size = 0;
  
rereap:
  if ((waitpid(state->pid, &_status, 0) == -1) && (errno == EINTR))
    goto rereap;
  
  if (state->flags & PASSPHRASE_READ_SCREEN_FREE)
    fprintf(stderr, "\033[s\033[E\033[0K\033[u");
  else
    fprintf(stderr, "\033[B\033[0K\033[A");
  fflush(stderr);
  
  state->flags = 0;
}


static void passcheck_update(struct passcheck_state* state, const char* passphrase, size_t len)
{
  size_t strength_ptr = 0;
  ssize_t n;
  int i;
  void* new;
  char* p;
  unsigned long long int value;
  
  if (state->flags == 0)
    return;
  
  for (i = 0; i < 2; i++, passphrase = "\n", len = 1)
    while (len)
      {
	n = write(state->pipe_rw[1], passphrase, len);
	if (n < 0)
	  {
	    if (errno == EINTR)
	      continue;
	    goto fail;
	  }
	passphrase += (size_t)n;
	len -= (size_t)n;
      }
  
 again:
  if (strength_ptr == strength_size)
    {
      strength_size = strength_size ? (strength_size << 1) : 8;
      new = realloc(strength, strength_size * sizeof(char));
      if (new == NULL)
	goto fail;
      strength = new;
    }
  n = read(state->pipe_rw[0], strength + strength_ptr, strength_size - 1 - strength_ptr);
  if (n <= 0)
    {
      if (n && (errno == EINTR))
	goto again;
      goto fail;
    }
  strength_ptr += (size_t)n;
  strength[strength_ptr] = '\0';
  p = strpbrk(strength, " \t\r\f\n\v");
  if (p == NULL)
    goto again;
  if (*p == '\033')
    {
      p = strpbrk(strength, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm~");
      if (p == NULL)
	goto fail;
      p++;
    }
  else
    p = strength;
  *(strpbrk(p, " \t\r\f\n\v\033")) = '\0';
  errno = 0;
  value = strtoull(p, NULL, 10);
  if ((value == 0) && errno)
    {
      if (errno != ERANGE)
	goto fail;
      value = ULLONG_MAX;
    }
  while (memchr(strength, '\n', strength_ptr) == NULL)
    {
      strength_ptr = 0;
      n = read(state->pipe_rw[0], strength, strength_size);
      if (n <= 0)
	{
	  if (n && (errno == EINTR))
	    goto again;
	  goto fail;
	}
      strength_ptr = (size_t)n;
    }
  strength_ptr = 0;
  
  if (state->flags & PASSPHRASE_READ_SCREEN_FREE)
    fprintf(stderr, "\033[s\033[E\033[0K%s%lli\033[u", /*TODO locale*/"Strength: ", value);
  else
    fprintf(stderr, "\033[B\033[s\033[0K%lli\033[u\033[A", value);
  fflush(stderr);
  
  return;
 fail:
  passcheck_stop(state);
}
#endif /* PASSPHRASE_METER */


static int fdgetc(int fd)
{
  unsigned char c;
  if (read(fd, &c, sizeof(c)) <= 0)
    return -1;
  return (int)c;
}


#if defined(PASSPHRASE_DEDICATED) && defined(PASSPHRASE_MOVE)
static int get_dedicated_control_key(int fdin)
{
  int c = fdgetc(fdin);
  if (c == 'O')
    {
      c = fdgetc(fdin);
      if (c == 'H')  return KEY_HOME;
      if (c == 'F')  return KEY_END;
    }
  else if (c == '[')
    {
      c = fdgetc(fdin);
      if (c == 'C')  return KEY_RIGHT;
      if (c == 'D')  return KEY_LEFT;
      if (('1' <= c) && (c <= '4') && (fdgetc(fdin) == '~'))
	return -(c - '0');
    }
  return 0;
}
#endif /* PASSPHRASE_DEDICATED && PASSPHRASE_MOVE */



#ifdef PASSPHRASE_MOVE
static int get_key(int c, int fdin)
{
# ifdef PASSPHRASE_DEDICATED
  if (c == '\033')             return get_dedicated_control_key(fdin);
# else /* PASSPHRASE_DEDICATED */
  (void) fdin;
# endif /* PASSPHRASE_DEDICATED */
  if ((c == 8) || (c == 127))  return KEY_ERASE;
  if ((c < 0) || (c >= ' '))   return c & 255;
# ifdef PASSPHRASE_CONTROL
  if (c == 'A' - '@')          return KEY_HOME;
  if (c == 'B' - '@')          return KEY_LEFT;
  if (c == 'D' - '@')          return KEY_DELETE;
  if (c == 'E' - '@')          return KEY_END;
  if (c == 'F' - '@')          return KEY_RIGHT;
# endif /* PASSPHRASE_CONTROL */
  return 0;
}
#endif /* PASSPHRASE_MOVE */



/**
 * Reads the passphrase from stdin
 * 
 * @param   fdin   File descriptor for input
 * @param   flags  Settings, a combination of the constants:
 *                 * PASSPHRASE_READ_EXISTING
 *                 * PASSPHRASE_READ_NEW
 *                 * PASSPHRASE_READ_SCREEN_FREE
 *                 * PASSPHRASE_READ_BELOW_FREE
 *                 Invalid input is ignored, to make use the
 *                 application will work.
 * @return         The passphrase, should be wiped and `free`:ed, `NULL` on error
 */
char* passphrase_read2(int fdin, int flags)
{
  char* rc = malloc(START_PASSPHRASE_LIMIT * sizeof(char));
  size_t size = START_PASSPHRASE_LIMIT;
  size_t len =  0;
#ifdef PASSPHRASE_MOVE
  size_t point = 0;
  size_t i = 0;
# if defined(PASSPHRASE_OVERRIDE) && defined(PASSPHRASE_INSERT)
  char insert = DEFAULT_INSERT_VALUE;
# endif /* PASSPHRASE_OVERRIDE && PASSPHRASE_INSERT */
#endif /* PASSPHRASE_MOVE */
#ifdef PASSPHRASE_TEXT
  size_t printed_len = 0;
#endif /* PASSPHRASE_TEXT */
  int c;
#ifdef PASSPHRASE_MOVE
  int cc;
#endif
#ifdef PASSPHRASE_METER
  struct passcheck_state passcheck;
#endif /* PASSPHRASE_METER */
  
  if (rc == NULL)
    return NULL;
  
#ifdef PASSPHRASE_METER
  passcheck_start(&passcheck, flags);
#endif /* PASSPHRASE_METER */
  
#ifdef PASSPHRASE_TEXT
  xprintf("%s%zn", PASSPHRASE_TEXT_EMPTY, &printed_len);
  if (printed_len)
    xprintf("\e[%zuD", printed_len);
#endif /* PASSPHRASE_TEXT */
  
  /* Read password until EOF or Enter, skip all \0 as that
     is probably not a part of the passphrase (good luck typing
     that in X.org) and can be echoed into stdin by the kernel. */
  for (;;)
    {
      c = fdgetc(fdin);
      if ((c < 0) || (c == '\n'))
	{
#ifdef PASSPHRASE_METER
	  passcheck_stop(&passcheck);
#endif /* PASSPHRASE_METER */
	  break;
	}
      if (c == 0)
	continue;
      
#if defined(PASSPHRASE_MOVE)
      cc = get_key(c, fdin);
      if (cc > 0)
	{
	  c = (char)cc;
	  if (point == len)
	    append_char();
# ifdef PASSPHRASE_INSERT
	  else
#  ifdef PASSPHRASE_OVERRIDE
	    if (insert)
#  endif /* PASSPHRASE_OVERRIDE */
	      insert_char();
# endif /* PASSPHRASE_INSERT */
# ifdef PASSPHRASE_OVERRIDE
	    else
	      override_char();
# endif /* PASSPHRASE_OVERRIDE */
	}
# if defined(PASSPHRASE_INSERT) && defined(PASSPHRASE_OVERRIDE)
      else if (cc == KEY_INSERT)                      insert ^= 1;
# endif /* PASSPHRASE_INSERT && PASSPHRASE_OVERRIDE */
# ifdef PASSPHRASE_DELETE
      else if ((cc == KEY_DELETE) && (len != point))  { delete_next(); print_delete(); }
# endif /* PASSPHRASE_DELETE */
      else if ((cc == KEY_ERASE) && point)            { erase_prev(); print_erase(); }
      else if ((cc == KEY_HOME)  && (point != 0))     move_home();
      else if ((cc == KEY_END)   && (point != len))   move_end();
      else if ((cc == KEY_RIGHT) && (point != len))   move_right();
      else if ((cc == KEY_LEFT)  && (point != 0))     move_left();
      
#elif defined(PASSPHRASE_STAR) || defined(PASSPHRASE_TEXT) /* PASSPHRASE_MOVE */
      if ((c == 8) || (c == 127))
	{
	  if (len == 0)
	    continue;
	  erase_prev();
	  print_erase();
	  
#ifdef PASSPHRASE_METER
	  passcheck_update(&passcheck, rc, len);
#endif /* PASSPHRASE_METER */
	  
	  xflush();
# ifdef DEBUG
	  goto debug;
# else /* DEBUG */
	  continue;
# endif /* DEBUG */
	}
      append_char();
      
#else /* PASSPHRASE_MOVE, PASSPHRASE_STAR || PASSPHRASE_TEXT */
      append_char();
#endif /* PASSPHRASE_MOVE, PASSPHRASE_STAR || PASSPHRASE_TEXT */
      
#ifdef PASSPHRASE_METER
      passcheck_update(&passcheck, rc, len);
#endif /* PASSPHRASE_METER */
      
      xflush();
      if (len == size)
	{
	  if ((rc = xrealloc(rc, (size_t)size, (size_t)size << 1)) == NULL)
	    return NULL;
	  size <<= 1L;
	}
      
#ifdef DEBUG
# ifdef __GNUC__
#  pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wunused-label"
# endif
    debug:
      {
	size_t n = 0;
	for (i = point; i < len; i++)
	  if ((*(rc + i) & 0xC0) != 0x80)
	    n++;
	*(rc + len) = 0;
	if (n)
	  fprintf(stderr, "\033[s\033[H\033[K%s\033[%zuD\033[01;34m%s\033[00m\033[u", rc, n, rc + point);
	else
	  fprintf(stderr, "\033[s\033[H\033[K%s\033[01;34m%s\033[00m\033[u", rc, rc + point);
	fflush(stderr);
      }
#endif /* DEBUG */
    }
  
  /* NUL-terminate passphrase */
  *(rc + len) = 0;
  
#if !defined(PASSPHRASE_ECHO) || defined(PASSPHRASE_MOVE)
  fprintf(stderr, "\n");
#endif /* !PASSPHRASE_ECHO || PASSPHRASE_MOVE */
  return rc;
  
#ifndef PASSPHRASE_METER
  (void) flags;
#endif /* !PASSPHRASE_METER */
}


/**
 * Reads the passphrase from stdin
 * 
 * @return  The passphrase, should be wiped and `free`:ed, `NULL` on error
 */
char* passphrase_read(void)
{
  return passphrase_read2(STDIN_FILENO, 0);
}