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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
|
/* gpg-auth.c - Authenticate using GnuPG
* Copyright (C) 2022 g10 Code GmbH
*
* This file is part of GnuPG.
*
* This file 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 file 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 Lesser 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 <https://gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INCLUDED_BY_MAIN_MODULE 1
#include "../common/util.h"
#include "../common/status.h"
#include "../common/i18n.h"
#include "../common/init.h"
#include "../common/sysutils.h"
#include "../common/asshelp.h"
#include "../common/session-env.h"
#include "../common/membuf.h"
#include "../common/exechelp.h"
/* We keep all global options in the structure OPT. */
struct
{
int interactive;
int verbose;
unsigned int debug;
int quiet;
int with_colons;
const char *agent_program;
int autostart;
/* Options passed to the gpg-agent: */
char *lc_ctype;
char *lc_messages;
} opt;
/* Debug values and macros. */
#define DBG_IPC_VALUE 1024 /* Debug assuan communication. */
#define DBG_EXTPROG_VALUE 16384 /* Debug external program calls */
#define DBG_IPC (opt.debug & DBG_IPC_VALUE)
#define DBG_EXTPROG (opt.debug & DBG_EXTPROG_VALUE)
/* Constants to identify the commands and options. */
enum opt_values
{
aNull = 0,
oQuiet = 'q',
oVerbose = 'v',
oDebug = 500,
oGpgProgram,
oGpgsmProgram,
oAgentProgram,
oStatusFD,
oWithColons,
oNoAutostart,
oLCctype,
oLCmessages,
oChUid,
oDummy
};
/* The list of commands and options. */
static gpgrt_opt_t opts[] = {
ARGPARSE_group (301, ("@\nOptions:\n ")),
ARGPARSE_s_n (oVerbose, "verbose", ("verbose")),
ARGPARSE_s_n (oQuiet, "quiet", ("be somewhat more quiet")),
ARGPARSE_s_s (oDebug, "debug", "@"),
ARGPARSE_s_i (oStatusFD, "status-fd", N_("|FD|write status info to this FD")),
ARGPARSE_s_n (oWithColons, "with-colons", "@"),
ARGPARSE_s_n (oNoAutostart, "no-autostart", "@"),
ARGPARSE_s_s (oAgentProgram, "agent-program", "@"),
ARGPARSE_s_s (oLCctype, "lc-ctype", "@"),
ARGPARSE_s_s (oLCmessages, "lc-messages","@"),
ARGPARSE_end ()
};
/* The list of supported debug flags. */
static struct debug_flags_s debug_flags [] =
{
{ DBG_IPC_VALUE , "ipc" },
{ DBG_EXTPROG_VALUE, "extprog" },
{ 0, NULL }
};
/* Print usage information and provide strings for help. */
static const char *
my_strusage( int level )
{
const char *p;
switch (level)
{
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "gpg-auth"; break;
case 12: p = "@GNUPG@"; break;
case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = ("Please report bugs to <@EMAIL@>.\n"); break;
case 1:
case 40:
p = ("Usage: gpg-auth"
" [options] (-h for help)");
break;
case 41:
p = ("Syntax: gpg-auth"
" [options] \n\n"
"Tool to authenticate a user using a smartcard.\n"
"Use command \"help\" to list all commands.");
break;
default: p = NULL; break;
}
return p;
}
/* Command line parsing. */
static void
parse_arguments (gpgrt_argparse_t *pargs, gpgrt_opt_t *popts)
{
while (gpgrt_argparse (NULL, pargs, popts))
{
switch (pargs->r_opt)
{
case oQuiet: opt.quiet = 1; break;
case oVerbose: opt.verbose++; break;
case oDebug:
if (parse_debug_flag (pargs->r.ret_str, &opt.debug, debug_flags))
{
pargs->r_opt = ARGPARSE_INVALID_ARG;
pargs->err = ARGPARSE_PRINT_ERROR;
}
break;
case oAgentProgram: opt.agent_program = pargs->r.ret_str; break;
case oStatusFD:
gnupg_set_status_fd (translate_sys2libc_fd_int (pargs->r.ret_int, 1));
break;
case oWithColons: opt.with_colons = 1; break;
case oNoAutostart: opt.autostart = 0; break;
case oLCctype: opt.lc_ctype = pargs->r.ret_str; break;
case oLCmessages: opt.lc_messages = pargs->r.ret_str; break;
default: pargs->err = ARGPARSE_PRINT_ERROR; break;
}
}
}
struct ga_key_list {
struct ga_key_list *next;
char keygrip[41]; /* Keygrip to identify a key. */
size_t pubkey_len;
char *pubkey; /* Public key in SSH format. */
};
/* Local prototypes. */
static gpg_error_t scd_passwd_reset (assuan_context_t ctx, const char *keygrip);
static gpg_error_t ga_scd_connect (assuan_context_t *r_scd_ctx, int use_agent);
static gpg_error_t ga_scd_get_auth_keys (assuan_context_t ctx,
struct ga_key_list **r_key_list);
static gpg_error_t ga_filter_by_authorized_keys (struct ga_key_list **r_key_list);
static void ga_release_auth_keys (struct ga_key_list *key_list);
static gpg_error_t scd_pkauth (assuan_context_t ctx, const char *keygrip);
static gpg_error_t authenticate (assuan_context_t ctx, struct ga_key_list *key_list);
static int getpin (const char *info, char *buf, size_t *r_len);
/* gpg-auth main. */
int
main (int argc, char **argv)
{
gpg_error_t err;
gpgrt_argparse_t pargs;
assuan_context_t scd_ctx = NULL;
struct ga_key_list *key_list = NULL;
gnupg_reopen_std ("gpg-auth");
gpgrt_set_strusage (my_strusage);
log_set_prefix ("gpg-auth", GPGRT_LOG_WITH_PREFIX);
/* Make sure that our subsystems are ready. */
i18n_init();
init_common_subsystems (&argc, &argv);
assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT);
setup_libassuan_logging (&opt.debug, NULL);
/* Setup default options. */
opt.autostart = 1;
/* Parse the command line. */
pargs.argc = &argc;
pargs.argv = &argv;
pargs.flags = ARGPARSE_FLAG_KEEP;
parse_arguments (&pargs, opts);
gpgrt_argparse (NULL, &pargs, NULL); /* Release internal state. */
if (log_get_errorcount (0))
exit (2);
if (argc != 0)
gpgrt_usage (1); /* Never returns. */
err = ga_scd_connect (&scd_ctx, 1);
if (!err)
err = ga_scd_get_auth_keys (scd_ctx, &key_list);
if (!err)
err = ga_filter_by_authorized_keys (&key_list);
if (!err)
err = authenticate (scd_ctx, key_list);
ga_release_auth_keys (key_list);
if (scd_ctx)
assuan_release (scd_ctx);
if (err)
exit (1);
return 0;
}
static gpg_error_t
authenticate (assuan_context_t ctx, struct ga_key_list *key_list)
{
gpg_error_t err;
while (key_list)
{
err = scd_passwd_reset (ctx, key_list->keygrip);
if (err)
return err;
err = scd_pkauth (ctx, key_list->keygrip);
if (!err)
/* Success! */
return 0;
key_list = key_list->next;
}
return gpg_error (GPG_ERR_NOT_FOUND);
}
static gpg_error_t
get_serialno_cb (void *opaque, const char *line)
{
char **serialno = opaque;
const char *keyword = line;
const char *s;
int keywordlen, n;
for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
;
while (spacep (line))
line++;
if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
{
if (*serialno)
return gpg_error (GPG_ERR_CONFLICT); /* Unexpected status line. */
for (n=0,s=line; hexdigitp (s); s++, n++)
;
if (!n || (n&1)|| !(spacep (s) || !*s) )
return gpg_error (GPG_ERR_ASS_PARAMETER);
*serialno = xtrymalloc (n+1);
if (!*serialno)
return gpg_error_from_syserror ();
memcpy (*serialno, line, n);
(*serialno)[n] = 0;
}
return 0;
}
/* Helper function, which is used by scd_connect.
Try to retrieve the SCDaemon's socket name from the gpg-agent
context CTX. On success, *SOCKET_NAME is filled with a copy of the
socket name. Return proper error code or zero on success. */
static gpg_error_t
agent_scd_getinfo_socket_name (assuan_context_t ctx, char **socket_name)
{
membuf_t data;
gpg_error_t err = 0;
unsigned char *databuf;
size_t datalen;
init_membuf (&data, 256);
*socket_name = NULL;
err = assuan_transact (ctx, "SCD GETINFO socket_name", put_membuf_cb, &data,
NULL, NULL, NULL, NULL);
databuf = get_membuf (&data, &datalen);
if (!err)
{
if (databuf && datalen)
{
char *res = xtrymalloc (datalen + 1);
if (!res)
err = gpg_error_from_syserror ();
else
{
memcpy (res, databuf, datalen);
res[datalen] = 0;
*socket_name = res;
}
}
}
xfree (databuf);
return err;
}
/* Callback parameter for learn card */
struct learn_parm_s
{
void (*kpinfo_cb)(void*, const char *);
void *kpinfo_cb_arg;
void (*certinfo_cb)(void*, const char *);
void *certinfo_cb_arg;
void (*sinfo_cb)(void*, const char *, size_t, const char *);
void *sinfo_cb_arg;
};
/* Connect to the agent and send the standard options. */
static gpg_error_t
start_agent (assuan_context_t *ctx_p)
{
gpg_error_t err;
session_env_t session_env;
session_env = session_env_new ();
if (!session_env)
log_fatal ("error allocating session environment block: %s\n",
strerror (errno));
err = start_new_gpg_agent (ctx_p,
GPG_ERR_SOURCE_DEFAULT,
opt.agent_program,
NULL, NULL,
session_env,
opt.autostart,
!opt.quiet, 0,
NULL, NULL);
session_env_release (session_env);
return err;
}
static gpg_error_t
scd_serialno (assuan_context_t ctx)
{
char *serialno = NULL;
gpg_error_t err;
err = assuan_transact (ctx, "SERIALNO", NULL, NULL, NULL, NULL,
get_serialno_cb, &serialno);
xfree (serialno);
return err;
}
static gpg_error_t
scd_passwd_reset (assuan_context_t ctx, const char *keygrip)
{
char line[ASSUAN_LINELENGTH];
gpg_error_t err;
snprintf (line, DIM(line), "PASSWD --clear OPENPGP.2 %s", keygrip);
err = assuan_transact (ctx, line, NULL, NULL, NULL, NULL,
NULL, NULL);
return err;
}
/* Connect to scdaemon by pipe or socket. Execute initial "SEREIALNO"
command to enable all connected token under scdaemon control. */
static gpg_error_t
ga_scd_connect (assuan_context_t *r_scd_ctx, int use_agent)
{
assuan_context_t assuan_ctx;
gpg_error_t err;
err = assuan_new (&assuan_ctx);
if (err)
return err;
if (use_agent)
/* Use scdaemon under gpg-agent. */
{
char *scd_socket_name = NULL;
assuan_context_t ctx;
err = start_agent (&ctx);
if (err)
return err;
/* Note that if gpg-agent is there but no scdaemon yet,
* gpg-agent automatically invokes scdaemon by this query
* itself.
*/
err = agent_scd_getinfo_socket_name (ctx, &scd_socket_name);
assuan_release (ctx);
if (!err)
err = assuan_socket_connect (assuan_ctx, scd_socket_name, 0, 0);
if (!err && DBG_IPC)
log_debug ("got scdaemon socket name from gpg-agent, "
"connected to socket '%s'", scd_socket_name);
xfree (scd_socket_name);
}
else
{
const char *scd_path;
const char *pgmname;
const char *argv[3];
int no_close_list[2];
scd_path = gnupg_module_name (GNUPG_MODULE_NAME_SCDAEMON);
if (!(pgmname = strrchr (scd_path, '/')))
pgmname = scd_path;
else
pgmname++;
/* Fill argument vector for scdaemon. */
argv[0] = pgmname;
argv[1] = "--server";
argv[2] = NULL;
no_close_list[0] = assuan_fd_from_posix_fd (fileno (stderr));
no_close_list[1] = ASSUAN_INVALID_FD;
/* Connect to the scdaemon */
err = assuan_pipe_connect (assuan_ctx, scd_path, argv, no_close_list,
NULL, NULL, 0);
if (err)
{
log_error ("could not spawn scdaemon: %s", gpg_strerror (err));
return err;
}
if (DBG_IPC)
log_debug ("spawned a new scdaemon (path: '%s')", scd_path);
}
if (err)
assuan_release (assuan_ctx);
else
{
scd_serialno (assuan_ctx);
*r_scd_ctx = assuan_ctx;
}
return err;
}
/* Handle the NEEDPIN inquiry. */
static gpg_error_t
inq_needpin (void *opaque, const char *line)
{
assuan_context_t ctx = opaque;
const char *s;
char *pin;
size_t pinlen;
int rc;
rc = 0;
if ((s = has_leading_keyword (line, "NEEDPIN")))
{
line = s;
pinlen = 90;
pin = gcry_malloc_secure (pinlen);
if (!pin)
return out_of_core ();
rc = getpin (line, pin, &pinlen);
if (!rc)
{
assuan_begin_confidential (ctx);
rc = assuan_send_data (ctx, pin, pinlen);
assuan_end_confidential (ctx);
}
wipememory (pin, pinlen);
xfree (pin);
}
else if ((s = has_leading_keyword (line, "POPUPPINPADPROMPT")))
{
/* i 18 */;
/* Please use PINPAD! */;
}
else if ((s = has_leading_keyword (line, "DISMISSPINPADPROMPT")))
{
;
}
else
{
log_error ("unsupported inquiry '%s'\n", line);
rc = gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
}
return gpg_error (rc);
}
struct card_keyinfo_parm_s {
int error;
struct ga_key_list *list;
};
/* Callback function for scd_keyinfo_list. */
static gpg_error_t
card_keyinfo_cb (void *opaque, const char *line)
{
gpg_error_t err = 0;
struct card_keyinfo_parm_s *parm = opaque;
const char *keyword = line;
int keywordlen;
struct ga_key_list *keyinfo = NULL;
for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
;
while (spacep (line))
line++;
if (keywordlen == 7 && !memcmp (keyword, "KEYINFO", keywordlen))
{
const char *s;
int n;
struct ga_key_list **l_p = &parm->list;
/* It's going to append the information at the end. */
while ((*l_p))
l_p = &(*l_p)->next;
keyinfo = xtrycalloc (1, sizeof *keyinfo);
if (!keyinfo)
goto alloc_error;
for (n=0,s=line; hexdigitp (s); s++, n++)
;
if (n != 40)
goto parm_error;
memcpy (keyinfo->keygrip, line, 40);
keyinfo->keygrip[40] = 0;
line = s;
if (!*line)
goto parm_error;
while (spacep (line))
line++;
if (*line++ != 'T')
goto parm_error;
if (!*line)
goto parm_error;
while (spacep (line))
line++;
for (n=0,s=line; hexdigitp (s); s++, n++)
;
if (!n)
goto skip;
skip:
*l_p = keyinfo;
}
return err;
alloc_error:
xfree (keyinfo);
if (!parm->error)
parm->error = gpg_error_from_syserror ();
return 0;
parm_error:
xfree (keyinfo);
if (!parm->error)
parm->error = gpg_error (GPG_ERR_ASS_PARAMETER);
return 0;
}
/* Call the scdaemon to retrieve list of available keys on cards. On
success, the allocated structure is stored at R_KEY_LIST. On
error, an error code is returned and NULL is stored at R_KEY_LIST. */
static gpg_error_t
scd_keyinfo_list (assuan_context_t ctx, struct ga_key_list **r_key_list)
{
int err;
struct card_keyinfo_parm_s parm;
memset (&parm, 0, sizeof parm);
err = assuan_transact (ctx, "KEYINFO --list=auth", NULL, NULL, NULL, NULL,
card_keyinfo_cb, &parm);
if (!err && parm.error)
err = parm.error;
if (!err)
*r_key_list = parm.list;
else
ga_release_auth_keys (parm.list);
return err;
}
/* A variant of put_membuf_cb, which only put the second field. */
static gpg_error_t
put_second_field_cb (void *opaque, const void *buf, size_t len)
{
char line[ASSUAN_LINELENGTH];
membuf_t *data = opaque;
if (buf && len < ASSUAN_LINELENGTH)
{
const char *fields[3];
size_t field_len;
memcpy (line, buf, len);
if (split_fields (line, fields, DIM (fields)) < 2)
return 0;
field_len = strlen (fields[1]);
put_membuf (data, fields[1], field_len);
}
return 0;
}
static gpg_error_t
scd_get_pubkey (assuan_context_t ctx, struct ga_key_list *key)
{
char line[ASSUAN_LINELENGTH];
membuf_t data;
unsigned char *databuf;
size_t datalen;
gpg_error_t err = 0;
init_membuf (&data, 256);
snprintf (line, DIM(line), "READKEY --format=ssh %s", key->keygrip);
err = assuan_transact (ctx, line, put_second_field_cb, &data,
NULL, NULL, NULL, NULL);
databuf = get_membuf (&data, &datalen);
if (!err)
{
key->pubkey_len = datalen;
key->pubkey = databuf;
}
else
xfree (databuf);
return err;
}
static gpg_error_t
ga_scd_get_auth_keys (assuan_context_t ctx, struct ga_key_list **r_key_list)
{
gpg_error_t err;
struct ga_key_list *kl, *key_list = NULL;
/* Get list of auth keys with their keygrips. */
err = scd_keyinfo_list (ctx, &key_list);
/* And retrieve public key for each key. */
kl = key_list;
while (kl)
{
err = scd_get_pubkey (ctx, kl);
if (err)
break;
kl = kl->next;
}
if (err)
ga_release_auth_keys (key_list);
else
*r_key_list = key_list;
return err;
}
struct ssh_key_list {
struct ssh_key_list *next;
char *pubkey; /* Public key in SSH format. */
};
static void
release_ssh_key_list (struct ssh_key_list *key_list)
{
struct ssh_key_list *key;
while (key_list)
{
key = key_list;
key_list = key_list->next;
xfree (key->pubkey);
xfree (key);
}
}
static gpg_error_t
ssh_authorized_keys (struct ssh_key_list **r_ssh_key_list)
{
gpg_error_t err = 0;
char *fname = NULL;
estream_t fp = NULL;
char *line = NULL;
size_t length_of_line = 0;
size_t maxlen;
ssize_t len;
const char *fields[3];
struct ssh_key_list *ssh_key_list = NULL;
struct ssh_key_list *ssh_key_prev = NULL;
struct ssh_key_list *ssh_key = NULL;
fname = make_absfilename_try ("~", ".ssh", "authorized_keys", NULL);
if (fname == NULL)
return gpg_error (GPG_ERR_INV_NAME);
fp = es_fopen (fname, "r");
if (!fp)
{
err = gpg_error_from_syserror ();
xfree (fname);
return err;
}
xfree (fname);
maxlen = 2048; /* Set limit. */
while ((len = es_read_line (fp, &line, &length_of_line, &maxlen)) > 0)
{
if (!maxlen)
{
err = gpg_error (GPG_ERR_LINE_TOO_LONG);
log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (err));
goto leave;
}
/* Strip newline and carriage return, if present. */
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
line[--len] = '\0';
if (split_fields (line, fields, DIM (fields)) < 2)
continue; /* Skip empty lines or line with only a field. */
if (*fields[0] == '#')
continue; /* Skip comments. */
ssh_key = xtrycalloc (1, sizeof *ssh_key);
if (!ssh_key)
{
err = gpg_error_from_syserror ();
release_ssh_key_list (ssh_key_list);
goto leave;
}
ssh_key->pubkey = strdup (fields[1]);
if (ssh_key_list)
ssh_key_prev->next = ssh_key;
else
ssh_key_list = ssh_key;
ssh_key_prev = ssh_key;
}
*r_ssh_key_list = ssh_key_list;
leave:
xfree (line);
es_fclose (fp);
return err;
}
static gpg_error_t
ga_filter_by_authorized_keys (struct ga_key_list **r_key_list)
{
gpg_error_t err;
struct ga_key_list *cur = *r_key_list;
struct ga_key_list *key_list = NULL;
struct ga_key_list *prev = NULL;
struct ssh_key_list *ssh_key_list = NULL;
err = ssh_authorized_keys (&ssh_key_list);
if (err)
return err;
if (ssh_key_list == NULL)
return gpg_error (GPG_ERR_NOT_FOUND);
while (cur)
{
struct ssh_key_list *skl = ssh_key_list;
while (skl)
if (!strncmp (cur->pubkey, skl->pubkey, cur->pubkey_len))
break;
else
skl = skl->next;
/* valid? */
if (skl)
{
if (key_list)
prev->next = cur;
else
key_list = cur;
prev = cur;
cur = cur->next;
}
else
{
struct ga_key_list *k = cur;
cur = cur->next;
xfree (k->pubkey);
xfree (k);
}
}
if (prev && prev->next)
prev->next = NULL;
release_ssh_key_list (ssh_key_list);
*r_key_list = key_list;
return 0;
}
static void
ga_release_auth_keys (struct ga_key_list *key_list)
{
struct ga_key_list *key;
while (key_list)
{
key = key_list;
key_list = key_list->next;
xfree (key->pubkey);
xfree (key);
}
}
static int
getpin (const char *info, char *buf, size_t *r_len)
{
int rc = 0;
char line[ASSUAN_LINELENGTH];
const char *fields[2];
(void)info;
fputs ("P 18\n", stdout);
fputs ("Please input PIN: \n", stdout);
fflush (stdout);
fgets (line, ASSUAN_LINELENGTH, stdin);
if (split_fields (line, fields, DIM (fields)) < DIM (fields))
rc = GPG_ERR_PROTOCOL_VIOLATION;
else if (strcmp (fields[0], "p") != 0)
rc = GPG_ERR_CANCELED;
if (!fgets (line, ASSUAN_LINELENGTH, stdin))
rc = GPG_ERR_PROTOCOL_VIOLATION;
if (!rc)
{
size_t len = strlen (line);
/* Strip newline and carriage return, if present. */
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
line[--len] = '\0';
len++; /* Include last '\0' in the data. */
if (len > *r_len)
rc = GPG_ERR_BUFFER_TOO_SHORT;
else
memcpy (buf, line, len);
*r_len = len;
}
return rc;
}
static gpg_error_t
scd_pkauth (assuan_context_t ctx, const char *keygrip)
{
char line[ASSUAN_LINELENGTH];
gpg_error_t err;
snprintf (line, DIM(line), "PKAUTH --challenge-response %s", keygrip);
err = assuan_transact (ctx, line, NULL, NULL, inq_needpin, ctx,
NULL, NULL);
return err;
}
|