~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to agent/command.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
 
81
81
 
82
82
 
83
 
/* To help polling clients, we keep tarck of the number of certain
 
83
/* To help polling clients, we keep track of the number of certain
84
84
   events.  This structure keeps those counters.  The counters are
85
85
   integers and there should be no problem if they are overflowing as
86
86
   callers need to check only whether a counter changed.  The actual
271
271
{
272
272
  int rc;
273
273
  size_t n;
274
 
  const unsigned char *p;
275
274
 
276
275
  rc = parse_hexstring (ctx, string, &n);
277
276
  if (rc)
280
279
  if (n != 20)
281
280
    return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of keygrip");
282
281
 
283
 
  for (p=(const unsigned char*)string, n=0; n < 20; p += 2, n++)
284
 
    buf[n] = xtoi_2 (p);
 
282
  if (hex2bin (string, buf, 20) < 0)
 
283
    return set_error (GPG_ERR_BUG, "hex2bin");
285
284
 
286
285
  return 0;
287
286
}
358
357
  char key_counter[25];
359
358
  char card_counter[25];
360
359
 
 
360
  (void)line;
 
361
 
361
362
  snprintf (any_counter, sizeof any_counter, "%u", eventcounter.any);
362
363
  snprintf (key_counter, sizeof key_counter, "%u", eventcounter.key);
363
364
  snprintf (card_counter, sizeof card_counter, "%u", eventcounter.card);
437
438
static int
438
439
cmd_listtrusted (assuan_context_t ctx, char *line)
439
440
{
440
 
  int rc = agent_listtrusted (ctx);
 
441
  int rc;
 
442
  
 
443
  (void)line;
 
444
 
 
445
  rc = agent_listtrusted (ctx);
441
446
  if (rc)
442
447
    log_error ("command listtrusted failed: %s\n", gpg_strerror (rc));
443
448
  return rc;
598
603
      else if (has_option (line, "--hash=md5"))
599
604
        algo = GCRY_MD_MD5;
600
605
      else if (has_option (line, "--hash=tls-md5sha1"))
601
 
        algo = GCRY_MD_USER_TLS_MD5SHA1;
 
606
        algo = MD_USER_TLS_MD5SHA1;
602
607
      else
603
608
        return set_error (GPG_ERR_ASS_PARAMETER, "invalid hash algorithm");
604
609
    }
623
628
  if (rc)
624
629
    return rc;
625
630
  n /= 2;
626
 
  if (algo == GCRY_MD_USER_TLS_MD5SHA1 && n == 36)
 
631
  if (algo == MD_USER_TLS_MD5SHA1 && n == 36)
627
632
    ;
628
633
  else if (n != 16 && n != 20 && n != 24 && n != 32)
629
634
    return set_error (GPG_ERR_ASS_PARAMETER, "unsupported length of hash");
653
658
  ctrl_t ctrl = assuan_get_pointer (ctx);
654
659
  membuf_t outbuf;
655
660
  
 
661
  (void)line;
 
662
  
656
663
  if (opt.ignore_cache_for_signing)
657
664
    cache_mode = CACHE_MODE_IGNORE;
658
665
  else if (!ctrl->server_local->use_cache_for_signing)
686
693
  size_t valuelen;
687
694
  membuf_t outbuf;
688
695
 
 
696
  (void)line;
 
697
 
689
698
  /* First inquire the data to decrypt */
690
699
  rc = assuan_inquire (ctx, "CIPHERTEXT",
691
700
                       &value, &valuelen, MAXLEN_CIPHERTEXT);
732
741
  size_t valuelen;
733
742
  membuf_t outbuf;
734
743
 
 
744
  (void)line;
 
745
 
735
746
  /* First inquire the parameters */
736
747
  rc = assuan_inquire (ctx, "KEYPARAM", &value, &valuelen, MAXLEN_KEYPARAM);
737
748
  if (rc)
825
836
}
826
837
 
827
838
 
828
 
/* GET_PASSPHRASE [--data] [--check] <cache_id>
 
839
/* GET_PASSPHRASE [--data] [--check] [--no-ask] <cache_id>
829
840
                  [<error_message> <prompt> <description>]
830
841
 
831
842
   This function is usually used to ask for a passphrase to be used
842
853
   If the option "--check" is used the passphrase constraints checks as
843
854
   implemented by gpg-agent are applied.  A check is not done if the
844
855
   passphrase has been found in the cache.
 
856
 
 
857
   If the option "--no-ask" is used and the passphrase is not in the
 
858
   cache the user will not be asked to enter a passphrase but the error
 
859
   code GPG_ERR_NO_DATA is returned.  
845
860
*/
846
861
 
847
862
static int
854
869
  char *cacheid = NULL, *desc = NULL, *prompt = NULL, *errtext = NULL;
855
870
  char *p;
856
871
  void *cache_marker;
857
 
  int opt_data, opt_check;
 
872
  int opt_data, opt_check, opt_no_ask;
858
873
 
859
874
  opt_data = has_option (line, "--data");
860
875
  opt_check = has_option (line, "--check");
 
876
  opt_no_ask = has_option (line, "--no-ask");
861
877
  line = skip_options (line);
862
878
 
863
879
  cacheid = line;
909
925
      rc = send_back_passphrase (ctx, opt_data, pw);
910
926
      agent_unlock_cache_entry (&cache_marker);
911
927
    }
 
928
  else if (opt_no_ask)
 
929
    rc = gpg_error (GPG_ERR_NO_DATA);
912
930
  else
913
931
    {
914
932
      /* Note, that we only need to replace the + characters and
1082
1100
  return rc;
1083
1101
}
1084
1102
 
1085
 
/* PRESET_PASSPHRASE <hexstring_with_keygrip> <timeout> <hexstring>
 
1103
/* PRESET_PASSPHRASE <string_or_keygrip> <timeout> <hexstring>
1086
1104
  
1087
1105
   Set the cached passphrase/PIN for the key identified by the keygrip
1088
1106
   to passwd for the given time, where -1 means infinite and 0 means
1093
1111
cmd_preset_passphrase (assuan_context_t ctx, char *line)
1094
1112
{
1095
1113
  int rc;
1096
 
  unsigned char grip[20];
1097
1114
  char *grip_clear = NULL;
1098
1115
  char *passphrase = NULL;
1099
1116
  int ttl;
1100
1117
  size_t len;
1101
1118
 
1102
1119
  if (!opt.allow_preset_passphrase)
1103
 
    return gpg_error (GPG_ERR_NOT_SUPPORTED);
1104
 
 
1105
 
  rc = parse_keygrip (ctx, line, grip);
1106
 
  if (rc)
1107
 
    return rc;
1108
 
 
1109
 
  /* FIXME: parse_keygrip should return a tail pointer.  */
 
1120
    return set_error (GPG_ERR_NOT_SUPPORTED, "no --allow-preset-passphrase");
 
1121
 
1110
1122
  grip_clear = line;
1111
1123
  while (*line && (*line != ' ' && *line != '\t'))
1112
1124
    line++;
1135
1147
  /* If there is a passphrase, use it.  Currently, a passphrase is
1136
1148
     required.  */
1137
1149
  if (*line)
1138
 
    passphrase = line;
 
1150
    {
 
1151
      /* Do in-place conversion.  */
 
1152
      passphrase = line;
 
1153
      if (!hex2str (passphrase, passphrase, strlen (passphrase)+1, NULL))
 
1154
        rc = set_error (GPG_ERR_ASS_PARAMETER, "invalid hexstring");
 
1155
    }
1139
1156
  else
1140
 
    return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
 
1157
    rc = set_error (GPG_ERR_NOT_IMPLEMENTED, "passphrase is required");
1141
1158
 
1142
 
  rc = agent_put_cache (grip_clear, CACHE_MODE_ANY, passphrase, ttl);
 
1159
  if (!rc)
 
1160
    rc = agent_put_cache (grip_clear, CACHE_MODE_ANY, passphrase, ttl);
1143
1161
 
1144
1162
  if (rc)
1145
 
    log_error ("command preset_passwd failed: %s\n", gpg_strerror (rc));
 
1163
    log_error ("command preset_passphrase failed: %s\n", gpg_strerror (rc));
1146
1164
 
1147
1165
  return rc;
1148
1166
}
1307
1325
{
1308
1326
  ctrl_t ctrl = assuan_get_pointer (ctx);
1309
1327
 
 
1328
  (void)line;
 
1329
 
1310
1330
  xfree (opt.startup_display); opt.startup_display = NULL;
1311
1331
  xfree (opt.startup_ttyname); opt.startup_ttyname = NULL;
1312
1332
  xfree (opt.startup_ttytype); opt.startup_ttytype = NULL;
1343
1363
cmd_killagent (assuan_context_t ctx, char *line)
1344
1364
{
1345
1365
  ctrl_t ctrl = assuan_get_pointer (ctx);
 
1366
 
 
1367
  (void)line;
 
1368
 
1346
1369
  ctrl->server_local->stopme = 1;
1347
 
  return 0;
 
1370
  return gpg_error (GPG_ERR_EOF);
1348
1371
}
1349
1372
 
1350
1373
/* RELOADAGENT
1354
1377
static int
1355
1378
cmd_reloadagent (assuan_context_t ctx, char *line)
1356
1379
{
 
1380
  (void)ctx;
 
1381
  (void)line;
 
1382
 
1357
1383
  agent_sighup_action ();
1358
1384
  return 0;
1359
1385
}
1370
1396
     pid         - Return the process id of the server.
1371
1397
     socket_name - Return the name of the socket.
1372
1398
     ssh_socket_name - Return the name of the ssh socket.
1373
 
 
 
1399
     scd_running - Return OK if the SCdaemon is already running.
1374
1400
 */
1375
1401
static int
1376
1402
cmd_getinfo (assuan_context_t ctx, char *line)
1407
1433
      else
1408
1434
        rc = gpg_error (GPG_ERR_NO_DATA);
1409
1435
    }
 
1436
  else if (!strcmp (line, "scd_running"))
 
1437
    {
 
1438
      rc = agent_scd_check_running ()? 0 : gpg_error (GPG_ERR_GENERAL);
 
1439
    }
1410
1440
  else
1411
1441
    rc = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
1412
1442
  return rc;
1422
1452
  if (!strcmp (key, "display"))
1423
1453
    {
1424
1454
      if (ctrl->display)
1425
 
        free (ctrl->display);
1426
 
      ctrl->display = strdup (value);
 
1455
        xfree (ctrl->display);
 
1456
      ctrl->display = xtrystrdup (value);
1427
1457
      if (!ctrl->display)
1428
1458
        return out_of_core ();
1429
1459
    }
1432
1462
      if (!opt.keep_tty)
1433
1463
        {
1434
1464
          if (ctrl->ttyname)
1435
 
            free (ctrl->ttyname);
1436
 
          ctrl->ttyname = strdup (value);
 
1465
            xfree (ctrl->ttyname);
 
1466
          ctrl->ttyname = xtrystrdup (value);
1437
1467
          if (!ctrl->ttyname)
1438
1468
            return out_of_core ();
1439
1469
        }
1443
1473
      if (!opt.keep_tty)
1444
1474
        {
1445
1475
          if (ctrl->ttytype)
1446
 
            free (ctrl->ttytype);
1447
 
          ctrl->ttytype = strdup (value);
 
1476
            xfree (ctrl->ttytype);
 
1477
          ctrl->ttytype = xtrystrdup (value);
1448
1478
          if (!ctrl->ttytype)
1449
1479
            return out_of_core ();
1450
1480
        }
1452
1482
  else if (!strcmp (key, "lc-ctype"))
1453
1483
    {
1454
1484
      if (ctrl->lc_ctype)
1455
 
        free (ctrl->lc_ctype);
1456
 
      ctrl->lc_ctype = strdup (value);
 
1485
        xfree (ctrl->lc_ctype);
 
1486
      ctrl->lc_ctype = xtrystrdup (value);
1457
1487
      if (!ctrl->lc_ctype)
1458
1488
        return out_of_core ();
1459
1489
    }
1460
1490
  else if (!strcmp (key, "lc-messages"))
1461
1491
    {
1462
1492
      if (ctrl->lc_messages)
1463
 
        free (ctrl->lc_messages);
1464
 
      ctrl->lc_messages = strdup (value);
 
1493
        xfree (ctrl->lc_messages);
 
1494
      ctrl->lc_messages = xtrystrdup (value);
1465
1495
      if (!ctrl->lc_messages)
1466
1496
        return out_of_core ();
1467
1497
    }
1468
1498
  else if (!strcmp (key, "xauthority"))
1469
1499
    {
1470
1500
      if (ctrl->xauthority)
1471
 
        free (ctrl->xauthority);
1472
 
      ctrl->xauthority = strdup (value);
 
1501
        xfree (ctrl->xauthority);
 
1502
      ctrl->xauthority = xtrystrdup (value);
1473
1503
      if (!ctrl->xauthority)
1474
1504
        return out_of_core ();
1475
1505
    }
1476
1506
  else if (!strcmp (key, "pinentry-user-data"))
1477
1507
    {
1478
1508
      if (ctrl->pinentry_user_data)
1479
 
        free (ctrl->pinentry_user_data);
1480
 
      ctrl->pinentry_user_data = strdup (value);
 
1509
        xfree (ctrl->pinentry_user_data);
 
1510
      ctrl->pinentry_user_data = xtrystrdup (value);
1481
1511
      if (!ctrl->pinentry_user_data)
1482
1512
        return out_of_core ();
1483
1513
    }
1500
1530
post_cmd_notify (assuan_context_t ctx, int err)
1501
1531
{
1502
1532
  ctrl_t ctrl = assuan_get_pointer (ctx);
 
1533
  
 
1534
  (void)err;
1503
1535
 
1504
1536
  /* Switch off any I/O monitor controlled logging pausing. */
1505
1537
  ctrl->server_local->pause_io_logging = 0;
1515
1547
            const char *line, size_t linelen)
1516
1548
{
1517
1549
  ctrl_t ctrl = assuan_get_pointer (ctx);
1518
 
 
 
1550
  
1519
1551
  /* Note that we only check for the uppercase name.  This allows to
1520
1552
     see the logging for debugging if using a non-upercase command
1521
1553
     name. */
1645
1677
  for (;;)
1646
1678
    {
1647
1679
      rc = assuan_accept (ctx);
1648
 
      if (rc == -1)
 
1680
      if (gpg_err_code (rc) == GPG_ERR_EOF || rc == -1)
1649
1681
        {
1650
1682
          break;
1651
1683
        }