aboutsummaryrefslogtreecommitdiffstats
path: root/src/passphrase_helper.h
blob: b8abb3ab31a42fbfa0167eb829d3dd1b87acbb81 (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
/**
 * 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/>.
 */
#ifndef PASSPHRASE_HELPER_H
#define PASSPHRASE_HELPER_H


/* Fix conflicting configurations */
#if defined(PASSPHRASE_TEXT) && defined(PASSPHRASE_STAR)
# warning You cannot have both PASSPHRASE_TEXT and PASSPHRASE_STAR
# undef PASSPHRASE_TEXT
#endif
#if defined(PASSPHRASE_STAR) && defined(PASSPHRASE_ECHO)
# warning You cannot have both PASSPHRASE_STAR and PASSPHRASE_ECHO
# undef PASSPHRASE_ECHO
#endif
#if defined(PASSPHRASE_TEXT) && defined(PASSPHRASE_ECHO)
# warning You cannot have both PASSPHRASE_TEXT and PASSPHRASE_ECHO
# undef PASSPHRASE_ECHO
#endif


/* Default texts */
#ifndef PASSPHRASE_STAR_CHAR
# define PASSPHRASE_STAR_CHAR  "*"
#endif
#ifndef PASSPHRASE_TEXT_EMPTY
# define PASSPHRASE_TEXT_EMPTY  "(empty)"
#endif
#ifndef PASSPHRASE_TEXT_NOT_EMPTY
# define PASSPHRASE_TEXT_NOT_EMPTY  "(not empty)"
#endif



/* Control keys. */

/**
 * Home-key.
 * Character sequences: \e[1~  \eOH
 * Control-key combination: ^A
 */
#define KEY_HOME    -1

/**
 * Insert-key.
 * Character sequences: \e[2~
 */
#define KEY_INSERT  -2

/**
 * Delete-key.
 * Character sequences: \e[3~
 * Control-key combination: ^D
 */
#define KEY_DELETE  -3

/**
 * End-key.
 * Character sequences: \e[4~  \eOF
 * Control-key combination: ^E
 */
#define KEY_END     -4

/**
 * Erase-key, also known as backspace.
 * Character sequences: \d8  \d127
 */
#define KEY_ERASE   -5

/**
 * Right-key.
 * Character sequences: \e[C
 * Control-key combination: ^F
 */
#define KEY_RIGHT   -6

/**
 * Left-key.
 * Character sequences: \e[D
 * Control-key combination: ^B
 */
#define KEY_LEFT    -7



/* Use by macros below to ensure that the result is not used. */
#define VOID(...)  do { __VA_ARGS__; } while (0)



/* Custom fflush and fprintf */
#if defined(PASSPHRASE_STAR) || defined(PASSPHRASE_TEXT)
# define xprintf(...)  VOID(fprintf(stderr, __VA_ARGS__))
# define xflush()      VOID(fflush(stderr))
#elif defined(PASSPHRASE_MOVE) && !defined(PASSPHRASE_ECHO)
# define xprintf(...)  VOID()
# define xflush()      VOID()
#elif defined(PASSPHRASE_MOVE)
# define xprintf(...)  VOID(fprintf(stderr, __VA_ARGS__))
# define xflush()      VOID(fflush(stderr))
#else
# define xflush()      VOID(fflush(stderr))
#endif



/* Custom putchar */
#if defined(PASSPHRASE_STAR)
# define xputchar(C)  VOID(((C & 0xC0) != 0x80) ? fprintf(stderr, "%s", PASSPHRASE_STAR_CHAR) : 0)
#elif defined(PASSPHRASE_ECHO) && defined(PASSPHRASE_MOVE)
# define xputchar(C)  VOID(fputc(C, stderr))
#else
# define xputchar(C)  VOID()
#endif



/* Is insert active by default? */
#if defined(PASSPHRASE_OVERRIDE) && defined(PASSPHRASE_INSERT)
# if defined(DEFAULT_INSERT)
#  define DEFAULT_INSERT_VALUE  1
# else
#  define DEFAULT_INSERT_VALUE  0
# endif
#endif



/* PASSPHRASE_INVALID's affect */
#if defined(PASSPHRASE_INVALID)
# define null_terminate()  VOID(*(rc + len) = 0)
#else
# define null_terminate()  VOID()
#endif



/* Implementation of the right-key's action */
#if defined(PASSPHRASE_TEXT)
# define move_right()						\
  do {								\
    do								\
      point++;							\
    while ((len != point) && ((*(rc + point) & 0xC0) == 0x80));	\
  } while (0)
#else
# define move_right()						\
  do {								\
    xprintf("\033[C");						\
    do								\
      point++;							\
    while ((len != point) && ((*(rc + point) & 0xC0) == 0x80));	\
  } while (0)
#endif


/* Implementation of the left-key's action */
#if defined(PASSPHRASE_TEXT)
# define move_left()					\
  do {							\
    point--;						\
    while (point && ((*(rc + point) & 0xC0) == 0x80))	\
      point--;						\
  } while (0)
#else
# define move_left()					\
  do {							\
    xprintf("\033[D");					\
    point--;						\
    while (point && ((*(rc + point) & 0xC0) == 0x80))	\
      point--;						\
  } while (0)
#endif


/* Implementation of the home-key's action */
#if defined(PASSPHRASE_TEXT)
# define move_home()  VOID(point = 0)
#else
# define move_home()			\
  do {					\
    size_t n = 0;			\
    for (i = 0; i < point; i++)		\
      if ((*(rc + i) & 0xC0) != 0x80)	\
	n++;				\
    xprintf("\033[%zuD", n);		\
    point = 0;				\
  } while (0)
#endif


/* Implementation of the end-key's action */
#if defined(PASSPHRASE_TEXT)
# define move_end()  VOID(point = len)
#else
# define move_end()			\
  do {					\
    size_t n = 0;			\
    for (i = point; i < len; i++)	\
      if ((*(rc + i) & 0xC0) != 0x80)	\
	n++;				\
    xprintf("\033[%zuC", n);		\
    point = len;			\
  } while (0)
#endif


/* Implementation of the delete-key's action upon the passphrase buffer */
#define delete_next()						\
  do {								\
    null_terminate();						\
    do								\
      {								\
	for (i = point; i < len; i++)				\
	  *(rc + i) = *(rc + i + 1);				\
	len--;							\
      }								\
    while ((len != point) && ((*(rc + point) & 0xC0) == 0x80));	\
  } while (0)


/* Implementation of the erase-key's action upon the passphrase buffer */
#if defined(PASSPHRASE_MOVE)
# define erase_prev()						\
  do {								\
    char redo = 1;						\
    null_terminate();						\
    while (redo)						\
      {								\
	redo = (*(rc + point - 1) & 0xC0) == 0x80;		\
	for (i = point; i < len; i++)				\
	  *(rc + i - 1) = *(rc + i);				\
	if (point <= len)					\
	  *(rc + len - 1) = *(rc + len);			\
	point--;						\
	len--;							\
      }								\
  } while (0)
#else
# define erase_prev()  VOID(*(rc + --len) = 0)
#endif


#ifdef PASSPHRASE_MOVE
# define move_point()  VOID(point++)
#else
# define move_point()  VOID()
#endif


#if defined(PASSPHRASE_TEXT)
# define append_char()							\
  do {									\
    if (len == 0)							\
      {									\
    	xprintf("\033[K");						\
	xprintf("%s%zn", PASSPHRASE_TEXT_NOT_EMPTY, &printed_len);	\
	if (printed_len)						\
	  xprintf("\033[%zuD", printed_len);				\
      }									\
    *(rc + len++) = (char)c;						\
    move_point();							\
  } while (0)
#else
# define append_char()		\
  do {				\
    xputchar(c);		\
    *(rc + len++) = (char)c;	\
    move_point();		\
  } while (0)
#endif


#if defined(PASSPHRASE_TEXT)
# define insert_char()			\
  do {					\
    for (i = len; i > point; i--)	\
      *(rc + i) = *(rc + i - 1);	\
    len++;				\
    *(rc + point++) = (char)c;		\
  } while(0)
#else
# define insert_char()			\
  do {					\
    if ((c & 0xC0) != 0x80)		\
      xprintf("\033[@");		\
    xputchar(c);			\
    for (i = len; i > point; i--)	\
      *(rc + i) = *(rc + i - 1);	\
    len++;				\
    *(rc + point++) = (char)c;		\
  } while (0)
#endif


#define override_char()						\
  do {								\
    size_t n = 1;						\
    char cn = (char)c;						\
    while ((*(rc + point + n) & 0xC0) == 0x80)			\
      n++;							\
    for (i = point + n; i < len; i++)				\
      *(rc + i - n) = *(rc + i);				\
    passphrase_wipe(rc + len - n, n);				\
    len -= n;							\
    n = 0;							\
    while (cn & 0x80)						\
      {								\
	cn = (char)(cn << 1);					\
	n++;							\
      }								\
    n = n ?: 1;							\
    if (len + n > size)						\
      {								\
	if ((rc = xrealloc(rc, size, size << 1L)) == NULL)	\
	  return NULL;						\
	size <<= 1L;						\
      }								\
    len += n;							\
    for (i = len - 1; i > point + n; i--)			\
      *(rc + i) = *(rc + i - n);				\
    if (len - 1 >= point + n)					\
      *(rc + point + n) = *(rc + point);			\
    for (i = 0; i < n; i++)					\
      {								\
	if (i)							\
	  c = getchar();					\
	xputchar(c);						\
	*(rc + point++) = (char)c;				\
      }								\
  } while (0)


/* Implementation of the delete-key's action upon the display */
#if defined(PASSPHRASE_TEXT)
# define print_delete()\
  do {									\
    if (len)								\
      break;								\
    xprintf("\033[K%s%zn", PASSPHRASE_TEXT_EMPTY, &printed_len);	\
    if (printed_len - 3)						\
      xprintf("\033[%zuD", printed_len - 3);				\
  } while (0)
#else
# define print_delete()  VOID(xprintf("\033[P"))
#endif


/* Implementation of the erase-key's action upon the display */
#if defined(PASSPHRASE_TEXT)
# define print_erase()							\
  do {									\
    if (len)								\
      break;								\
    xprintf("\033[K%s%zn", PASSPHRASE_TEXT_EMPTY, &printed_len);	\
    if (printed_len - 3)						\
      xprintf("\033[%zuD", printed_len - 3);				\
  } while (0)
#elif defined(PASSPHRASE_MOVE)
# define print_erase()  VOID(xprintf("\033[D\033[P"))
#elif defined(PASSPHRASE_STAR)
# define print_erase()  VOID(xprintf("\033[D \033[D"))
#endif


#define xpipe(pair)		\
  do {				\
    if (pipe(pair))		\
      {				\
	pair[0] = pair[1] = -1;	\
	goto fail;		\
      }				\
  } while (0)
  



#endif