~ubuntu-branches/ubuntu/raring/gnupg2/raring

« back to all changes in this revision

Viewing changes to agent/command.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2012-11-06 11:25:58 UTC
  • mfrom: (1.1.16) (7.1.8 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121106112558-lkpndvv4gqthgrn4
Tags: 2.0.19-1ubuntu1
* Resynchronize on Debian, remaining changes:
  - Add udev rules to give gpg access to some smartcard readers;
    Debian #543217.
    . debian/gnupg2.dev: udev rules to set ACLs on SCM smartcard readers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "agent.h"
38
38
#include <assuan.h>
39
39
#include "i18n.h"
 
40
#include "../common/ssh-utils.h"
40
41
 
41
42
/* maximum allowed size of the inquired ciphertext */
42
43
#define MAXLEN_CIPHERTEXT 4096
72
73
  struct putval_item_s *next;
73
74
  size_t off;  /* Offset to the value into DATA.  */
74
75
  size_t len;  /* Length of the value.  */
75
 
  char d[1];   /* Key | Nul | value.  */ 
 
76
  char d[1];   /* Key | Nul | value.  */
76
77
};
77
78
 
78
79
 
86
87
   integers and there should be no problem if they are overflowing as
87
88
   callers need to check only whether a counter changed.  The actual
88
89
   values are not meaningful. */
89
 
struct 
 
90
struct
90
91
{
91
92
  /* Incremented if any of the other counters below changed. */
92
93
  unsigned int any;
93
94
 
94
95
  /* Incremented if a key is added or removed from the internal privat
95
96
     key database. */
96
 
  unsigned int key; 
 
97
  unsigned int key;
97
98
 
98
99
  /* Incremented if a change of the card readers stati has been
99
100
     detected. */
291
292
 
292
293
  va_start (arg_ptr, keyword);
293
294
 
294
 
  p = buf; 
 
295
  p = buf;
295
296
  n = 0;
296
297
  while ( (text = va_arg (arg_ptr, const char *)) )
297
298
    {
332
333
{
333
334
  char line[100];
334
335
 
335
 
  if (!ctrl || !ctrl->server_local 
 
336
  if (!ctrl || !ctrl->server_local
336
337
      || !ctrl->server_local->allow_pinentry_notify)
337
338
    return 0;
338
339
  snprintf (line, DIM(line)-1, "PINENTRY_LAUNCHED %lu", pid);
341
342
 
342
343
 
343
344
 
344
 
static const char hlp_geteventcounter[] = 
 
345
static const char hlp_geteventcounter[] =
345
346
  "GETEVENTCOUNTER\n"
346
347
  "\n"
347
348
  "Return a a status line named EVENTCOUNTER with the current values\n"
399
400
 
400
401
 
401
402
 
402
 
static const char hlp_istrusted[] = 
 
403
static const char hlp_istrusted[] =
403
404
  "ISTRUSTED <hexstring_with_fingerprint>\n"
404
405
  "\n"
405
406
  "Return OK when we have an entry with this fingerprint in our\n"
439
440
}
440
441
 
441
442
 
442
 
static const char hlp_listtrusted[] = 
 
443
static const char hlp_listtrusted[] =
443
444
  "LISTTRUSTED\n"
444
445
  "\n"
445
446
  "List all entries from the trustlist.";
447
448
cmd_listtrusted (assuan_context_t ctx, char *line)
448
449
{
449
450
  int rc;
450
 
  
 
451
 
451
452
  (void)line;
452
453
 
453
454
  rc = agent_listtrusted (ctx);
457
458
}
458
459
 
459
460
 
460
 
static const char hlp_martrusted[] = 
 
461
static const char hlp_martrusted[] =
461
462
  "MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>\n"
462
463
  "\n"
463
464
  "Store a new key in into the trustlist.";
484
485
  for (p=line; i < 40; p++, i++)
485
486
    fpr[i] = *p >= 'a'? (*p & 0xdf): *p;
486
487
  fpr[i] = 0;
487
 
  
 
488
 
488
489
  while (spacep (p))
489
490
    p++;
490
491
  flag = *p++;
542
543
}
543
544
 
544
545
 
545
 
static const char hlp_setkeydesc[] = 
 
546
static const char hlp_setkeydesc[] =
546
547
  "SETKEYDESC plus_percent_escaped_string\n"
547
548
  "\n"
548
549
  "Set a description to be used for the next PKSIGN or PKDECRYPT\n"
629
630
    algo = 0;
630
631
 
631
632
  line = skip_options (line);
632
 
  
 
633
 
633
634
  if (!algo)
634
635
    {
635
636
      /* No hash option has been given: require an algo number instead  */
649
650
  n /= 2;
650
651
  if (algo == MD_USER_TLS_MD5SHA1 && n == 36)
651
652
    ;
652
 
  else if (n != 16 && n != 20 && n != 24 
 
653
  else if (n != 16 && n != 20 && n != 24
653
654
           && n != 28 && n != 32 && n != 48 && n != 64)
654
655
    return set_error (GPG_ERR_ASS_PARAMETER, "unsupported length of hash");
655
656
 
666
667
}
667
668
 
668
669
 
669
 
static const char hlp_pksign[] = 
 
670
static const char hlp_pksign[] =
670
671
  "PKSIGN [options]\n"
671
672
  "\n"
672
673
  "Perform the actual sign operation.  Neither input nor output are\n"
678
679
  cache_mode_t cache_mode = CACHE_MODE_NORMAL;
679
680
  ctrl_t ctrl = assuan_get_pointer (ctx);
680
681
  membuf_t outbuf;
681
 
  
 
682
 
682
683
  (void)line;
683
 
  
 
684
 
684
685
  if (opt.ignore_cache_for_signing)
685
686
    cache_mode = CACHE_MODE_IGNORE;
686
687
  else if (!ctrl->server_local->use_cache_for_signing)
702
703
}
703
704
 
704
705
 
705
 
static const char hlp_pkdecrypt[] = 
 
706
static const char hlp_pkdecrypt[] =
706
707
  "PKDECRYPT <options>\n"
707
708
  "\n"
708
709
  "Perform the actual decrypt operation.  Input is not\n"
741
742
}
742
743
 
743
744
 
744
 
static const char hlp_genkey[] = 
 
745
static const char hlp_genkey[] =
745
746
  "GENKEY\n"
746
747
  "\n"
747
748
  "Generate a new key, store the secret part and return the public\n"
787
788
 
788
789
 
789
790
 
790
 
static const char hlp_readkey[] = 
 
791
static const char hlp_readkey[] =
791
792
  "READKEY <hexstring_with_keygrip>\n"
792
793
  "\n"
793
794
  "Return the public key for the given keygrip.";
831
832
 
832
833
 
833
834
 
834
 
static const char hlp_keyinfo[] = 
835
 
  "KEYINFO [--list] <keygrip>\n"
 
835
static const char hlp_keyinfo[] =
 
836
  "KEYINFO [--list] [--data] [--ssh-fpr] <keygrip>\n"
836
837
  "\n"
837
838
  "Return information about the key specified by the KEYGRIP.  If the\n"
838
839
  "key is not available GPG_ERR_NOT_FOUND is returned.  If the option\n"
839
840
  "--list is given the keygrip is ignored and information about all\n"
840
841
  "available keys are returned.  The information is returned as a\n"
841
 
  "status line with this format:\n"
 
842
  "status line unless --data was specified, with this format:\n"
842
843
  "\n"
843
 
  "  KEYINFO <keygrip> <type> <serialno> <idstr>\n"
 
844
  "  KEYINFO <keygrip> <type> <serialno> <idstr> - - <fpr>\n"
844
845
  "\n"
845
846
  "KEYGRIP is the keygrip.\n"
846
847
  "\n"
856
857
  "IDSTR is the IDSTR used to distinguish keys on a smartcard.  If it\n"
857
858
  "      is not known a dash is used instead.\n"
858
859
  "\n"
 
860
  "FPR returns the formatted ssh-style fingerprint of the key.  It is only\n"
 
861
  "    print if the option --ssh-fpr has been used. '-' is printed if the\n"
 
862
  "    fingerprint is not available.\n"
 
863
  "\n"
859
864
  "More information may be added in the future.";
860
865
static gpg_error_t
861
 
do_one_keyinfo (ctrl_t ctrl, const unsigned char *grip)
 
866
do_one_keyinfo (ctrl_t ctrl, const unsigned char *grip, assuan_context_t ctx,
 
867
                int data, int with_ssh_fpr)
862
868
{
863
869
  gpg_error_t err;
864
870
  char hexgrip[40+1];
 
871
  char *fpr = NULL;
865
872
  int keytype;
866
873
  unsigned char *shadow_info = NULL;
867
874
  char *serialno = NULL;
874
881
 
875
882
  /* Reformat the grip so that we use uppercase as good style. */
876
883
  bin2hex (grip, 20, hexgrip);
877
 
      
878
 
  if (keytype == PRIVATE_KEY_CLEAR 
 
884
 
 
885
  if (keytype == PRIVATE_KEY_CLEAR
879
886
      || keytype == PRIVATE_KEY_PROTECTED)
880
887
    keytypestr = "D";
881
888
  else if (keytype == PRIVATE_KEY_SHADOWED)
882
889
    keytypestr = "T";
883
 
  else 
 
890
  else
884
891
    keytypestr = "-";
885
 
      
 
892
 
 
893
  /* Compute the ssh fingerprint if requested.  */
 
894
  if (with_ssh_fpr)
 
895
    {
 
896
      gcry_sexp_t key;
 
897
 
 
898
      if (!agent_raw_key_from_file (ctrl, grip, &key))
 
899
        {
 
900
          ssh_get_fingerprint_string (key, &fpr);
 
901
          gcry_sexp_release (key);
 
902
        }
 
903
    }
 
904
 
886
905
  if (shadow_info)
887
906
    {
888
907
      err = parse_shadow_info (shadow_info, &serialno, &idstr);
889
908
      if (err)
890
909
        goto leave;
891
910
    }
892
 
      
893
 
  err = agent_write_status (ctrl, "KEYINFO",
894
 
                            hexgrip,
895
 
                            keytypestr,
896
 
                            serialno? serialno : "-",
897
 
                            idstr? idstr : "-",
898
 
                            NULL);
 
911
 
 
912
  /* Note that we don't support the CACHED and PROTECTION values as
 
913
     gnupg 2.1 does.  We print '-' instead.  However we support the
 
914
     ssh fingerprint.  */
 
915
  if (!data)
 
916
    err = agent_write_status (ctrl, "KEYINFO",
 
917
                              hexgrip,
 
918
                              keytypestr,
 
919
                              serialno? serialno : "-",
 
920
                              idstr? idstr : "-",
 
921
                              "-",
 
922
                              "-",
 
923
                              fpr? fpr : "-",
 
924
                              NULL);
 
925
  else
 
926
    {
 
927
      char *string;
 
928
 
 
929
      string = xtryasprintf ("%s %s %s %s - - %s\n",
 
930
                             hexgrip, keytypestr,
 
931
                             serialno? serialno : "-",
 
932
                             idstr? idstr : "-",
 
933
                             fpr? fpr : "-");
 
934
      if (!string)
 
935
        err = gpg_error_from_syserror ();
 
936
      else
 
937
        err = assuan_send_data (ctx, string, strlen(string));
 
938
      xfree (string);
 
939
    }
 
940
 
899
941
 leave:
 
942
  xfree (fpr);
900
943
  xfree (shadow_info);
901
944
  xfree (serialno);
902
945
  xfree (idstr);
912
955
  unsigned char grip[20];
913
956
  DIR *dir = NULL;
914
957
  int list_mode;
 
958
  int opt_data, opt_ssh_fpr;
915
959
 
916
960
  list_mode = has_option (line, "--list");
 
961
  opt_data = has_option (line, "--data");
 
962
  opt_ssh_fpr = has_option (line, "--ssh-fpr");
917
963
  line = skip_options (line);
918
964
 
919
965
  if (list_mode)
921
967
      char *dirname;
922
968
      struct dirent *dir_entry;
923
969
      char hexgrip[41];
924
 
      
 
970
 
925
971
      dirname = make_filename_try (opt.homedir, GNUPG_PRIVATE_KEYS_DIR, NULL);
926
972
      if (!dirname)
927
973
        {
948
994
          if ( hex2bin (hexgrip, grip, 20) < 0 )
949
995
            continue; /* Bad hex string.  */
950
996
 
951
 
          err = do_one_keyinfo (ctrl, grip);
 
997
          err = do_one_keyinfo (ctrl, grip, ctx, opt_data, opt_ssh_fpr);
952
998
          if (err)
953
999
            goto leave;
954
1000
        }
959
1005
      err = parse_keygrip (ctx, line, grip);
960
1006
      if (err)
961
1007
        goto leave;
962
 
      err = do_one_keyinfo (ctrl, grip);
 
1008
      err = do_one_keyinfo (ctrl, grip, ctx, opt_data, opt_ssh_fpr);
963
1009
    }
964
 
      
 
1010
 
965
1011
 leave:
966
1012
  if (dir)
967
1013
    closedir (dir);
998
1044
}
999
1045
 
1000
1046
 
1001
 
static const char hlp_get_passphrase[] = 
 
1047
static const char hlp_get_passphrase[] =
1002
1048
  "GET_PASSPHRASE [--data] [--check] [--no-ask] [--repeat[=N]]\n"
1003
1049
  "               [--qualitybar] <cache_id>\n"
1004
1050
  "               [<error_message> <prompt> <description>]\n"
1119
1165
        plus_to_blank (desc);
1120
1166
 
1121
1167
    next_try:
1122
 
      rc = agent_get_passphrase (ctrl, &response, desc, prompt, 
1123
 
                                 repeat_errtext? repeat_errtext:errtext, 
 
1168
      rc = agent_get_passphrase (ctrl, &response, desc, prompt,
 
1169
                                 repeat_errtext? repeat_errtext:errtext,
1124
1170
                                 opt_qualbar);
1125
1171
      xfree (repeat_errtext);
1126
1172
      repeat_errtext = NULL;
1145
1191
                {
1146
1192
                  xfree (response2);
1147
1193
                  xfree (response);
1148
 
                  repeat_errtext = try_percent_escape 
 
1194
                  repeat_errtext = try_percent_escape
1149
1195
                    (_("does not match - try again"), NULL);
1150
1196
                  if (!repeat_errtext)
1151
1197
                    {
1172
1218
}
1173
1219
 
1174
1220
 
1175
 
static const char hlp_clear_passphrase[] = 
 
1221
static const char hlp_clear_passphrase[] =
1176
1222
  "CLEAR_PASSPHRASE <cache_id>\n"
1177
1223
  "\n"
1178
1224
  "may be used to invalidate the cache entry for a passphrase.  The\n"
1198
1244
}
1199
1245
 
1200
1246
 
1201
 
static const char hlp_get_confirmation[] = 
 
1247
static const char hlp_get_confirmation[] =
1202
1248
  "GET_CONFIRMATION <description>\n"
1203
1249
  "\n"
1204
1250
  "This command may be used to ask for a simple confirmation.\n"
1265
1311
 
1266
1312
 
1267
1313
 
1268
 
static const char hlp_passwd[] = 
 
1314
static const char hlp_passwd[] =
1269
1315
  "PASSWD <hexstring_with_keygrip>\n"
1270
1316
  "\n"
1271
1317
  "Change the passphrase/PIN for the key identified by keygrip in LINE.";
1284
1330
 
1285
1331
  ctrl->in_passwd++;
1286
1332
  rc = agent_key_from_file (ctrl, ctrl->server_local->keydesc,
1287
 
                            grip, &shadow_info, CACHE_MODE_IGNORE, NULL, 
 
1333
                            grip, &shadow_info, CACHE_MODE_IGNORE, NULL,
1288
1334
                            &s_skey);
1289
1335
  if (rc)
1290
1336
    ;
1309
1355
}
1310
1356
 
1311
1357
 
1312
 
static const char hlp_preset_passphrase[] = 
 
1358
static const char hlp_preset_passphrase[] =
1313
1359
  "PRESET_PASSPHRASE <string_or_keygrip> <timeout> <hexstring>\n"
1314
1360
  "\n"
1315
1361
  "Set the cached passphrase/PIN for the key identified by the keygrip\n"
1338
1384
  line++;
1339
1385
  while (*line && (*line == ' ' || *line == '\t'))
1340
1386
    line++;
1341
 
  
 
1387
 
1342
1388
  /* Currently, only infinite timeouts are allowed.  */
1343
1389
  ttl = -1;
1344
1390
  if (line[0] != '-' || line[1] != '1')
1378
1424
 
1379
1425
 
1380
1426
 
1381
 
static const char hlp_scd[] = 
 
1427
static const char hlp_scd[] =
1382
1428
  "SCD <commands to pass to the scdaemon>\n"
1383
1429
  " \n"
1384
1430
  "This is a general quote command to redirect everything to the\n"
1396
1442
 
1397
1443
 
1398
1444
 
1399
 
static const char hlp_getval[] = 
 
1445
static const char hlp_getval[] =
1400
1446
  "GETVAL <key>\n"
1401
1447
  "\n"
1402
1448
  "Return the value for KEY from the special environment as created by\n"
1415
1461
  p = strchr (key, ' ');
1416
1462
  if (p)
1417
1463
    {
1418
 
      *p++ = 0; 
 
1464
      *p++ = 0;
1419
1465
      for (; *p == ' '; p++)
1420
1466
        ;
1421
1467
      if (*p)
1440
1486
}
1441
1487
 
1442
1488
 
1443
 
static const char hlp_putval[] = 
 
1489
static const char hlp_putval[] =
1444
1490
  "PUTVAL <key> [<percent_escaped_value>]\n"
1445
1491
  "\n"
1446
1492
  "The gpg-agent maintains a kind of environment which may be used to\n"
1474
1520
  p = strchr (key, ' ');
1475
1521
  if (p)
1476
1522
    {
1477
 
      *p++ = 0; 
 
1523
      *p++ = 0;
1478
1524
      for (; *p == ' '; p++)
1479
1525
        ;
1480
1526
      if (*p)
1503
1549
      xfree (vl);
1504
1550
    }
1505
1551
 
1506
 
  if (valuelen) /* Add entry. */  
 
1552
  if (valuelen) /* Add entry. */
1507
1553
    {
1508
1554
      vl = xtrymalloc (sizeof *vl + strlen (key) + valuelen);
1509
1555
      if (!vl)
1527
1573
 
1528
1574
 
1529
1575
 
1530
 
static const char hlp_updatestartuptty[] = 
 
1576
static const char hlp_updatestartuptty[] =
1531
1577
  "UPDATESTARTUPTTY\n"
1532
1578
  "\n"
1533
1579
  "Set startup TTY and X11 DISPLAY variables to the values of this\n"
1537
1583
static gpg_error_t
1538
1584
cmd_updatestartuptty (assuan_context_t ctx, char *line)
1539
1585
{
1540
 
  static const char *names[] = 
 
1586
  static const char *names[] =
1541
1587
    { "GPG_TTY", "DISPLAY", "TERM", "XAUTHORITY", "PINENTRY_USER_DATA", NULL };
1542
1588
  ctrl_t ctrl = assuan_get_pointer (ctx);
1543
1589
  gpg_error_t err = 0;
1545
1591
  int idx;
1546
1592
  char *lc_ctype = NULL;
1547
1593
  char *lc_messages = NULL;
1548
 
  
 
1594
 
1549
1595
  (void)line;
1550
1596
 
1551
1597
  se = session_env_new ();
1559
1605
        err = session_env_setenv (se, names[idx], value);
1560
1606
    }
1561
1607
 
1562
 
  if (!err && ctrl->lc_ctype) 
 
1608
  if (!err && ctrl->lc_ctype)
1563
1609
    if (!(lc_ctype = xtrystrdup (ctrl->lc_ctype)))
1564
1610
      err = gpg_error_from_syserror ();
1565
1611
 
1566
1612
  if (!err && ctrl->lc_messages)
1567
1613
    if (!(lc_messages = xtrystrdup (ctrl->lc_messages)))
1568
1614
      err = gpg_error_from_syserror ();
1569
 
   
 
1615
 
1570
1616
  if (err)
1571
1617
    {
1572
1618
      session_env_release (se);
1599
1645
  ctrl_t ctrl = assuan_get_pointer (ctx);
1600
1646
 
1601
1647
  (void)line;
1602
 
  
 
1648
 
1603
1649
  if (!opt.use_standard_socket)
1604
1650
    return set_error (GPG_ERR_NOT_SUPPORTED, "no --use-standard-socket");
1605
1651
 
1625
1671
 
1626
1672
 
1627
1673
 
1628
 
static const char hlp_getinfo[] = 
 
1674
static const char hlp_getinfo[] =
1629
1675
  "GETINFO <what>\n"
1630
1676
  "\n"
1631
1677
  "Multipurpose function to return a variety of information.\n"
1693
1739
      int iterator;
1694
1740
      const char *name, *value;
1695
1741
      char *string;
1696
 
      
1697
 
      iterator = 0; 
 
1742
 
 
1743
      iterator = 0;
1698
1744
      while ((name = session_env_list_stdenvnames (&iterator, NULL)))
1699
1745
        {
1700
1746
          value = session_env_getenv_or_default
1701
1747
            (line[5] == 't'? opt.startup_env:ctrl->session_env, name, NULL);
1702
1748
          if (value)
1703
1749
            {
1704
 
              string = xtryasprintf ("%s=%s", name, value); 
 
1750
              string = xtryasprintf ("%s=%s", name, value);
1705
1751
              if (!string)
1706
1752
                rc = gpg_error_from_syserror ();
1707
1753
              else
1827
1873
post_cmd_notify (assuan_context_t ctx, gpg_error_t err)
1828
1874
{
1829
1875
  ctrl_t ctrl = assuan_get_pointer (ctx);
1830
 
  
 
1876
 
1831
1877
  (void)err;
1832
1878
 
1833
1879
  /* Switch off any I/O monitor controlled logging pausing. */
1844
1890
            const char *line, size_t linelen)
1845
1891
{
1846
1892
  ctrl_t ctrl = assuan_get_pointer (ctx);
1847
 
  
 
1893
 
1848
1894
  (void) hook;
1849
1895
 
1850
1896
  /* Note that we only check for the uppercase name.  This allows to
1871
1917
      if (!strcmp (cmdopt, "repeat"))
1872
1918
          return 1;
1873
1919
    }
1874
 
      
 
1920
 
1875
1921
  return 0;
1876
1922
}
1877
1923
 
1905
1951
    { "MARKTRUSTED",    cmd_marktrusted, hlp_martrusted },
1906
1952
    { "LEARN",          cmd_learn,     hlp_learn },
1907
1953
    { "PASSWD",         cmd_passwd,    hlp_passwd },
1908
 
    { "INPUT",          NULL }, 
1909
 
    { "OUTPUT",         NULL }, 
 
1954
    { "INPUT",          NULL },
 
1955
    { "OUTPUT",         NULL },
1910
1956
    { "SCD",            cmd_scd,       hlp_scd },
1911
1957
    { "GETVAL",         cmd_getval,    hlp_getval },
1912
1958
    { "PUTVAL",         cmd_putval,    hlp_putval },
1924
1970
                                    table[i].help);
1925
1971
      if (rc)
1926
1972
        return rc;
1927
 
    } 
 
1973
    }
1928
1974
  assuan_register_post_cmd_notify (ctx, post_cmd_notify);
1929
1975
  assuan_register_reset_notify (ctx, reset_notify);
1930
1976
  assuan_register_option_handler (ctx, option_handler);
1963
2009
      /* FIXME: Need to call assuan_sock_set_nonce for Windows.  But
1964
2010
         this branch is currently not used.  */
1965
2011
    }
1966
 
  else 
 
2012
  else
1967
2013
    {
1968
2014
      rc = assuan_init_socket_server (ctx, fd, ASSUAN_SOCKET_SERVER_ACCEPTED);
1969
2015
    }
2002
2048
          log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
2003
2049
          break;
2004
2050
        }
2005
 
      
 
2051
 
2006
2052
      rc = assuan_process (ctx);
2007
2053
      if (rc)
2008
2054
        {