1
1
/* command.c - gpg-agent command handler
2
* Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2
* Copyright (C) 2001, 2002, 2003, 2004, 2005,
3
* 2006, 2008 Free Software Foundation, Inc.
4
5
* This file is part of GnuPG.
6
7
* GnuPG is free software; you can redistribute it and/or modify
7
8
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* the Free Software Foundation; either version 3 of the License, or
9
10
* (at your option) any later version.
11
12
* GnuPG is distributed in the hope that it will be useful,
14
15
* GNU General Public License for more details.
16
17
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
* along with this program; if not, see <http://www.gnu.org/licenses/>.
21
21
/* FIXME: we should not use the default assuan buffering but setup
50
50
/* Data used to associate an Assuan context with local server data */
51
struct server_local_s {
52
ASSUAN_CONTEXT assuan_ctx;
53
assuan_context_t assuan_ctx;
54
55
int use_cache_for_signing;
55
56
char *keydesc; /* Allocated description for the next key
58
int pause_io_logging; /* Used to suppress I/O logging during a command */
59
#ifdef HAVE_W32_SYSTEM
60
int stopme; /* If set to true the agent will be terminated after
61
the end of this session. */
63
int allow_pinentry_notify; /* Set if pinentry notifications should
68
/* An entry for the getval/putval commands. */
71
struct putval_item_s *next;
72
size_t off; /* Offset to the value into DATA. */
73
size_t len; /* Length of the value. */
74
char d[1]; /* Key | Nul | value. */
78
/* A list of key value pairs fpr the getval/putval commands. */
79
static struct putval_item_s *putval_list;
83
/* To help polling clients, we keep tarck of the number of certain
84
events. This structure keeps those counters. The counters are
85
integers and there should be no problem if they are overflowing as
86
callers need to check only whether a counter changed. The actual
87
values are not meaningful. */
90
/* Incremented if any of the other counters below changed. */
93
/* Incremented if a key is added or removed from the internal privat
97
/* Incremented if a change of the card readers stati has been
120
164
return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
167
/* Same as has_option but does only test for the name of the option
168
and ignores an argument, i.e. with NAME being "--hash" it would
169
return true for "--hash" as well as for "--hash=foo". */
171
has_option_name (const char *line, const char *name)
174
int n = strlen (name);
176
s = strstr (line, name);
177
return (s && (s == line || spacep (s-1))
178
&& (!s[n] || spacep (s+n) || s[n] == '='));
182
/* Skip over options. It is assumed that leading spaces have been
183
removed (this is the case for lines passed to a handler from
184
assuan). Blanks after the options are also removed. */
186
skip_options (char *line)
188
while ( *line == '-' && line[1] == '-' )
190
while (*line && !spacep (line))
192
while (spacep (line))
123
199
/* Replace all '+' by a blank. */
125
201
plus_to_blank (char *s)
211
/* Do the percent and plus/space unescaping in place and return the
212
length of the valid buffer. */
214
percent_plus_unescape (char *string)
216
unsigned char *p = (unsigned char *)string;
221
if (*string == '%' && string[1] && string[2])
224
*p++ = xtoi_2 (string);
228
else if (*string == '+')
135
247
/* Parse a hex string. Return an Assuan error code or 0 on success and the
136
248
length of the parsed string in LEN. */
138
parse_hexstring (ASSUAN_CONTEXT ctx, const char *string, size_t *len)
250
parse_hexstring (assuan_context_t ctx, const char *string, size_t *len)
144
256
for (p=string, n=0; hexdigitp (p); p++, n++)
146
258
if (*p != ' ' && *p != '\t' && *p)
147
return set_error (Parameter_Error, "invalid hexstring");
259
return set_error (GPG_ERR_ASS_PARAMETER, "invalid hexstring");
149
return set_error (Parameter_Error, "odd number of digits");
261
return set_error (GPG_ERR_ASS_PARAMETER, "odd number of digits");
154
266
/* Parse the keygrip in STRING into the provided buffer BUF. BUF must
155
provide space for 20 bytes. BUF is not changed if the fucntions
267
provide space for 20 bytes. BUF is not changed if the function
156
268
returns an error. */
158
parse_keygrip (ASSUAN_CONTEXT ctx, const char *string, unsigned char *buf)
270
parse_keygrip (assuan_context_t ctx, const char *string, unsigned char *buf)
290
/* Write an assuan status line. */
292
agent_write_status (ctrl_t ctrl, const char *keyword, ...)
297
assuan_context_t ctx = ctrl->server_local->assuan_ctx;
301
va_start (arg_ptr, keyword);
305
while ( (text = va_arg (arg_ptr, const char *)) )
312
for ( ; *text && n < DIM (buf)-2; n++)
316
err = assuan_write_status (ctx, keyword, buf);
323
/* Helper to notify the client about a launched Pinentry. Because
324
that might disturb some older clients, this is only done if enabled
325
via an option. Returns an gpg error code. */
327
agent_inq_pinentry_launched (ctrl_t ctrl, unsigned long pid)
331
if (!ctrl || !ctrl->server_local
332
|| !ctrl->server_local->allow_pinentry_notify)
334
snprintf (line, DIM(line)-1, "PINENTRY_LAUNCHED %lu", pid);
335
return assuan_inquire (ctrl->server_local->assuan_ctx, line, NULL, NULL, 0);
342
Return a a status line named EVENTCOUNTER with the current values
343
of all event counters. The values are decimal numbers in the range
344
0 to UINT_MAX and wrapping around to 0. The actual values should
345
not be relied upon, they shall only be used to detect a change.
347
The currently defined counters are:
349
ANY - Incremented with any change of any of the other counters.
350
KEY - Incremented for added or removed private keys.
351
CARD - Incremented for changes of the card readers stati.
354
cmd_geteventcounter (assuan_context_t ctx, char *line)
356
ctrl_t ctrl = assuan_get_pointer (ctx);
357
char any_counter[25];
358
char key_counter[25];
359
char card_counter[25];
361
snprintf (any_counter, sizeof any_counter, "%u", eventcounter.any);
362
snprintf (key_counter, sizeof key_counter, "%u", eventcounter.key);
363
snprintf (card_counter, sizeof card_counter, "%u", eventcounter.card);
365
return agent_write_status (ctrl, "EVENTCOUNTER",
373
/* This function should be called once for all key removals or
374
additions. This function is assured not to do any context
377
bump_key_eventcounter (void)
383
/* This function should be called for all card reader status
384
changes. This function is assured not to do any context
387
bump_card_eventcounter (void)
180
396
/* ISTRUSTED <hexstring_with_fingerprint>
182
398
Return OK when we have an entry with this fingerprint in our
185
cmd_istrusted (ASSUAN_CONTEXT ctx, char *line)
401
cmd_istrusted (assuan_context_t ctx, char *line)
403
ctrl_t ctrl = assuan_get_pointer (ctx);
191
/* parse the fingerprint value */
408
/* Parse the fingerprint value. */
192
409
for (p=line,n=0; hexdigitp (p); p++, n++)
194
411
if (*p || !(n == 40 || n == 32))
195
return set_error (Parameter_Error, "invalid fingerprint");
412
return set_error (GPG_ERR_ASS_PARAMETER, "invalid fingerprint");
202
419
for (p=line; i < 40; p++, i++)
203
420
fpr[i] = *p >= 'a'? (*p & 0xdf): *p;
205
rc = agent_istrusted (fpr);
209
return ASSUAN_Not_Trusted;
422
rc = agent_istrusted (ctrl, fpr);
423
if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_TRUSTED)
425
else if (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF )
426
return gpg_error (GPG_ERR_NOT_TRUSTED);
212
429
log_error ("command is_trusted failed: %s\n", gpg_strerror (rc));
213
return map_to_assuan_status (rc);
348
565
xfree (ctrl->server_local->keydesc);
349
566
ctrl->server_local->keydesc = xtrystrdup (desc);
350
567
if (!ctrl->server_local->keydesc)
351
return map_to_assuan_status (gpg_error_from_errno (errno));
568
return out_of_core ();
356
/* SETHASH <algonumber> <hexstring>
573
/* SETHASH --hash=<name>|<algonumber> <hexstring>
358
575
The client can use this command to tell the server about the data
359
576
(which usually is a hash) to be signed. */
361
cmd_sethash (ASSUAN_CONTEXT ctx, char *line)
578
cmd_sethash (assuan_context_t ctx, char *line)
371
/* parse the algo number and check it */
372
algo = (int)strtoul (line, &endp, 10);
373
for (line = endp; *line == ' ' || *line == '\t'; line++)
375
if (!algo || gcry_md_test_algo (algo))
376
return set_error (Unsupported_Algorithm, NULL);
588
/* Parse the alternative hash options which may be used instead of
590
if (has_option_name (line, "--hash"))
592
if (has_option (line, "--hash=sha1"))
594
else if (has_option (line, "--hash=sha256"))
595
algo = GCRY_MD_SHA256;
596
else if (has_option (line, "--hash=rmd160"))
597
algo = GCRY_MD_RMD160;
598
else if (has_option (line, "--hash=md5"))
600
else if (has_option (line, "--hash=tls-md5sha1"))
601
algo = GCRY_MD_USER_TLS_MD5SHA1;
603
return set_error (GPG_ERR_ASS_PARAMETER, "invalid hash algorithm");
608
line = skip_options (line);
612
/* No hash option has been given: require an algo number instead */
613
algo = (int)strtoul (line, &endp, 10);
614
for (line = endp; *line == ' ' || *line == '\t'; line++)
616
if (!algo || gcry_md_test_algo (algo))
617
return set_error (GPG_ERR_UNSUPPORTED_ALGORITHM, NULL);
377
619
ctrl->digest.algo = algo;
379
/* parse the hash value */
621
/* Parse the hash value. */
380
622
rc = parse_hexstring (ctx, line, &n);
384
if (n != 16 && n != 20 && n != 24 && n != 32)
385
return set_error (Parameter_Error, "unsupported length of hash");
626
if (algo == GCRY_MD_USER_TLS_MD5SHA1 && n == 36)
628
else if (n != 16 && n != 20 && n != 24 && n != 32)
629
return set_error (GPG_ERR_ASS_PARAMETER, "unsupported length of hash");
386
631
if (n > MAX_DIGEST_LEN)
387
return set_error (Parameter_Error, "hash value to long");
632
return set_error (GPG_ERR_ASS_PARAMETER, "hash value to long");
389
634
buf = ctrl->digest.value;
390
635
ctrl->digest.valuelen = n;
558
/* GET_PASSPHRASE <cache_id> [<error_message> <prompt> <description>]
803
send_back_passphrase (assuan_context_t ctx, int via_data, const char *pw)
808
assuan_begin_confidential (ctx);
811
rc = assuan_send_data (ctx, pw, n);
814
char *p = xtrymalloc_secure (n*2+1);
816
rc = gpg_error_from_syserror ();
820
rc = assuan_set_okay_line (ctx, p);
828
/* GET_PASSPHRASE [--data] [--check] <cache_id>
829
[<error_message> <prompt> <description>]
560
831
This function is usually used to ask for a passphrase to be used
561
832
for conventional encryption, but may also be used by programs which
564
835
agent either returns with an error or with a OK followed by the hex
565
836
encoded passphrase. Note that the length of the strings is
566
837
implicitly limited by the maximum length of a command.
839
If the option "--data" is used the passphrase is returned by usual
840
data lines and not on the okay line.
842
If the option "--check" is used the passphrase constraints checks as
843
implemented by gpg-agent are applied. A check is not done if the
844
passphrase has been found in the cache.
570
cmd_get_passphrase (ASSUAN_CONTEXT ctx, char *line)
848
cmd_get_passphrase (assuan_context_t ctx, char *line)
572
850
ctrl_t ctrl = assuan_get_pointer (ctx);
605
885
p = strchr (desc, ' ');
607
*p = 0; /* ignore garbage */
887
*p = 0; /* Ignore trailing garbage. */
611
891
if (!cacheid || !*cacheid || strlen (cacheid) > 50)
612
return set_error (Parameter_Error, "invalid length of cacheID");
892
return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of cacheID");
614
return set_error (Parameter_Error, "no description given");
894
return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
616
896
if (!strcmp (cacheid, "X"))
645
923
plus_to_blank (desc);
647
rc = agent_get_passphrase (ctrl, &response, desc, prompt, errtext);
929
rc = agent_get_passphrase (ctrl, &response, desc, prompt, errtext);
933
&& check_passphrase_constraints (ctrl, response, 0));
651
938
agent_put_cache (cacheid, CACHE_MODE_USER, response, 0);
652
assuan_begin_confidential (ctx);
653
rc = assuan_set_okay_line (ctx, response);
939
rc = send_back_passphrase (ctx, opt_data, response);
654
940
xfree (response);
659
945
log_error ("command get_passphrase failed: %s\n", gpg_strerror (rc));
660
return map_to_assuan_status (rc);
785
1071
rc = agent_protect_and_store (ctrl, s_skey);
787
1074
xfree (ctrl->server_local->keydesc);
788
1075
ctrl->server_local->keydesc = NULL;
789
1078
gcry_sexp_release (s_skey);
790
1079
xfree (shadow_info);
792
1081
log_error ("command passwd failed: %s\n", gpg_strerror (rc));
793
return map_to_assuan_status (rc);
796
/* PRESET_PASSPHRASE <hexstring_with_keygrip> <timeout> <passwd>
1085
/* PRESET_PASSPHRASE <hexstring_with_keygrip> <timeout> <hexstring>
798
1087
Set the cached passphrase/PIN for the key identified by the keygrip
799
1088
to passwd for the given time, where -1 means infinite and 0 means
830
1120
/* Currently, only infinite timeouts are allowed. */
832
1122
if (line[0] != '-' || line[1] != '1')
833
return map_to_assuan_status (gpg_error (GPG_ERR_NOT_IMPLEMENTED));
1123
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
836
1126
while (!(*line != ' ' && *line != '\t'))
1129
/* Syntax check the hexstring. */
1130
rc = parse_hexstring (ctx, line, &len);
839
1135
/* If there is a passphrase, use it. Currently, a passphrase is
842
1138
passphrase = line;
844
return map_to_assuan_status (gpg_error (GPG_ERR_NOT_IMPLEMENTED));
1140
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
846
1142
rc = agent_put_cache (grip_clear, CACHE_MODE_ANY, passphrase, ttl);
849
1145
log_error ("command preset_passwd failed: %s\n", gpg_strerror (rc));
851
return map_to_assuan_status (rc);
857
1153
This is a general quote command to redirect everything to the
860
cmd_scd (ASSUAN_CONTEXT ctx, char *line)
1156
cmd_scd (assuan_context_t ctx, char *line)
862
1158
ctrl_t ctrl = assuan_get_pointer (ctx);
865
1161
rc = divert_generic_cmd (ctrl, line, ctx);
867
return map_to_assuan_status (rc);
1170
Return the value for KEY from the special environment as created by
1174
cmd_getval (assuan_context_t ctx, char *line)
1179
struct putval_item_s *vl;
1181
for (p=line; *p == ' '; p++)
1184
p = strchr (key, ' ');
1188
for (; *p == ' '; p++)
1191
return set_error (GPG_ERR_ASS_PARAMETER, "too many arguments");
1194
return set_error (GPG_ERR_ASS_PARAMETER, "no key given");
1197
for (vl=putval_list; vl; vl = vl->next)
1198
if ( !strcmp (vl->d, key) )
1201
if (vl) /* Got an entry. */
1202
rc = assuan_send_data (ctx, vl->d+vl->off, vl->len);
1204
return gpg_error (GPG_ERR_NO_DATA);
1207
log_error ("command getval failed: %s\n", gpg_strerror (rc));
1212
/* PUTVAL <key> [<percent_escaped_value>]
1214
The gpg-agent maintains a kind of environment which may be used to
1215
store key/value pairs in it, so that they can be retrieved later.
1216
This may be used by helper daemons to daemonize themself on
1217
invocation and register them with gpg-agent. Callers of the
1218
daemon's service may now first try connect to get the information
1219
for that service from gpg-agent through the GETVAL command and then
1220
try to connect to that daemon. Only if that fails they may start
1221
an own instance of the service daemon.
1223
KEY is an an arbitrary symbol with the same syntax rules as keys
1224
for shell environment variables. PERCENT_ESCAPED_VALUE is the
1225
corresponsing value; they should be similar to the values of
1226
envronment variables but gpg-agent does not enforce any
1227
restrictions. If that value is not given any value under that KEY
1228
is removed from this special environment.
1231
cmd_putval (assuan_context_t ctx, char *line)
1236
size_t valuelen = 0;
1238
struct putval_item_s *vl, *vlprev;
1240
for (p=line; *p == ' '; p++)
1243
p = strchr (key, ' ');
1247
for (; *p == ' '; p++)
1252
p = strchr (value, ' ');
1255
valuelen = percent_plus_unescape (value);
1259
return set_error (GPG_ERR_ASS_PARAMETER, "no key given");
1262
for (vl=putval_list,vlprev=NULL; vl; vlprev=vl, vl = vl->next)
1263
if ( !strcmp (vl->d, key) )
1266
if (vl) /* Delete old entry. */
1269
vlprev->next = vl->next;
1271
putval_list = vl->next;
1275
if (valuelen) /* Add entry. */
1277
vl = xtrymalloc (sizeof *vl + strlen (key) + valuelen);
1279
rc = gpg_error_from_syserror ();
1283
vl->off = strlen (key) + 1;
1284
strcpy (vl->d, key);
1285
memcpy (vl->d + vl->off, value, valuelen);
1286
vl->next = putval_list;
1292
log_error ("command putval failed: %s\n", gpg_strerror (rc));
896
1324
opt.startup_lc_ctype = xtrystrdup (ctrl->lc_ctype);
897
1325
if (ctrl->lc_messages)
898
1326
opt.startup_lc_messages = xtrystrdup (ctrl->lc_messages);
906
option_handler (ASSUAN_CONTEXT ctx, const char *key, const char *value)
1327
if (ctrl->xauthority)
1328
opt.startup_xauthority = xtrystrdup (ctrl->xauthority);
1329
if (ctrl->pinentry_user_data)
1330
opt.startup_pinentry_user_data = xtrystrdup (ctrl->pinentry_user_data);
1337
#ifdef HAVE_W32_SYSTEM
1340
Under Windows we start the agent on the fly. Thus it also make
1341
sense to allow a client to stop the agent. */
1343
cmd_killagent (assuan_context_t ctx, char *line)
1345
ctrl_t ctrl = assuan_get_pointer (ctx);
1346
ctrl->server_local->stopme = 1;
1352
As signals are inconvenient under Windows, we provide this command
1353
to allow reloading of the configuration. */
1355
cmd_reloadagent (assuan_context_t ctx, char *line)
1357
agent_sighup_action ();
1360
#endif /*HAVE_W32_SYSTEM*/
1366
Multipurpose function to return a variety of information.
1367
Supported values for WHAT are:
1369
version - Return the version of the program.
1370
pid - Return the process id of the server.
1371
socket_name - Return the name of the socket.
1372
ssh_socket_name - Return the name of the ssh socket.
1376
cmd_getinfo (assuan_context_t ctx, char *line)
1380
if (!strcmp (line, "version"))
1382
const char *s = VERSION;
1383
rc = assuan_send_data (ctx, s, strlen (s));
1385
else if (!strcmp (line, "pid"))
1389
snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
1390
rc = assuan_send_data (ctx, numbuf, strlen (numbuf));
1392
else if (!strcmp (line, "socket_name"))
1394
const char *s = get_agent_socket_name ();
1397
rc = assuan_send_data (ctx, s, strlen (s));
1399
rc = gpg_error (GPG_ERR_NO_DATA);
1401
else if (!strcmp (line, "ssh_socket_name"))
1403
const char *s = get_agent_ssh_socket_name ();
1406
rc = assuan_send_data (ctx, s, strlen (s));
1408
rc = gpg_error (GPG_ERR_NO_DATA);
1411
rc = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
1418
option_handler (assuan_context_t ctx, const char *key, const char *value)
908
1420
ctrl_t ctrl = assuan_get_pointer (ctx);
951
1463
free (ctrl->lc_messages);
952
1464
ctrl->lc_messages = strdup (value);
953
1465
if (!ctrl->lc_messages)
954
return ASSUAN_Out_Of_Core;
1466
return out_of_core ();
1468
else if (!strcmp (key, "xauthority"))
1470
if (ctrl->xauthority)
1471
free (ctrl->xauthority);
1472
ctrl->xauthority = strdup (value);
1473
if (!ctrl->xauthority)
1474
return out_of_core ();
1476
else if (!strcmp (key, "pinentry-user-data"))
1478
if (ctrl->pinentry_user_data)
1479
free (ctrl->pinentry_user_data);
1480
ctrl->pinentry_user_data = strdup (value);
1481
if (!ctrl->pinentry_user_data)
1482
return out_of_core ();
956
1484
else if (!strcmp (key, "use-cache-for-signing"))
957
1485
ctrl->server_local->use_cache_for_signing = *value? atoi (value) : 0;
1486
else if (!strcmp (key, "allow-pinentry-notify"))
1487
ctrl->server_local->allow_pinentry_notify = 1;
959
return ASSUAN_Invalid_Option;
1489
return gpg_error (GPG_ERR_UNKNOWN_OPTION);
1497
/* Called by libassuan after all commands. ERR is the error from the
1498
last assuan operation and not the one returned from the command. */
1500
post_cmd_notify (assuan_context_t ctx, int err)
1502
ctrl_t ctrl = assuan_get_pointer (ctx);
1504
/* Switch off any I/O monitor controlled logging pausing. */
1505
ctrl->server_local->pause_io_logging = 0;
1509
/* This function is called by libassuan for all I/O. We use it here
1510
to disable logging for the GETEVENTCOUNTER commands. This is so
1511
that the debug output won't get cluttered by this primitive
1514
io_monitor (assuan_context_t ctx, int direction,
1515
const char *line, size_t linelen)
1517
ctrl_t ctrl = assuan_get_pointer (ctx);
1519
/* Note that we only check for the uppercase name. This allows to
1520
see the logging for debugging if using a non-upercase command
1522
if (ctx && !direction
1524
&& !strncmp (line, "GETEVENTCOUNTER", 15)
1525
&& (linelen == 15 || spacep (line+15)))
1527
ctrl->server_local->pause_io_logging = 1;
1530
return ctrl->server_local->pause_io_logging? 1:0;
965
1534
/* Tell the assuan library about our commands */
967
register_commands (ASSUAN_CONTEXT ctx)
1536
register_commands (assuan_context_t ctx)
970
1539
const char *name;
971
int (*handler)(ASSUAN_CONTEXT, char *line);
1540
int (*handler)(assuan_context_t, char *line);
1542
{ "GETEVENTCOUNTER",cmd_geteventcounter },
973
1543
{ "ISTRUSTED", cmd_istrusted },
974
1544
{ "HAVEKEY", cmd_havekey },
975
1545
{ "SIGKEY", cmd_sigkey },
991
1561
{ "INPUT", NULL },
992
1562
{ "OUTPUT", NULL },
993
1563
{ "SCD", cmd_scd },
1564
{ "GETVAL", cmd_getval },
1565
{ "PUTVAL", cmd_putval },
994
1566
{ "UPDATESTARTUPTTY", cmd_updatestartuptty },
1567
#ifdef HAVE_W32_SYSTEM
1568
{ "KILLAGENT", cmd_killagent },
1569
{ "RELOADAGENT", cmd_reloadagent },
1571
{ "GETINFO", cmd_getinfo },
1582
#ifdef HAVE_ASSUAN_SET_IO_MONITOR
1583
assuan_register_post_cmd_notify (ctx, post_cmd_notify);
1005
1585
assuan_register_reset_notify (ctx, reset_notify);
1006
1586
assuan_register_option_handler (ctx, option_handler);
1011
/* Startup the server. If LISTEN_FD and FD is given as -1, this is a simple
1012
piper server, otherwise it is a regular server */
1591
/* Startup the server. If LISTEN_FD and FD is given as -1, this is a
1592
simple piper server, otherwise it is a regular server. CTRL is the
1593
control structure for this connection; it has only the basic
1014
start_command_handler (int listen_fd, int fd)
1596
start_command_handler (ctrl_t ctrl, gnupg_fd_t listen_fd, gnupg_fd_t fd)
1018
struct server_control_s ctrl;
1599
assuan_context_t ctx;
1020
memset (&ctrl, 0, sizeof ctrl);
1021
agent_init_default_ctrl (&ctrl);
1023
if (listen_fd == -1 && fd == -1)
1601
if (listen_fd == GNUPG_INVALID_FD && fd == GNUPG_INVALID_FD)
1025
1603
int filedes[2];
1028
1606
filedes[1] = 1;
1029
1607
rc = assuan_init_pipe_server (&ctx, filedes);
1031
else if (listen_fd != -1)
1609
else if (listen_fd != GNUPG_INVALID_FD)
1033
rc = assuan_init_socket_server (&ctx, listen_fd);
1611
rc = assuan_init_socket_server_ext (&ctx, listen_fd, 0);
1037
rc = assuan_init_connected_socket_server (&ctx, fd);
1038
ctrl.connection_fd = fd;
1615
rc = assuan_init_socket_server_ext (&ctx, fd, 2);
1042
1619
log_error ("failed to initialize the server: %s\n",
1043
assuan_strerror(rc));
1044
1621
agent_exit (2);
1046
1623
rc = register_commands (ctx);
1049
1626
log_error ("failed to register commands with Assuan: %s\n",
1050
assuan_strerror(rc));
1051
1628
agent_exit (2);
1054
assuan_set_pointer (ctx, &ctrl);
1055
ctrl.server_local = xcalloc (1, sizeof *ctrl.server_local);
1056
ctrl.server_local->assuan_ctx = ctx;
1057
ctrl.server_local->message_fd = -1;
1058
ctrl.server_local->use_cache_for_signing = 1;
1059
ctrl.digest.raw_value = 0;
1631
assuan_set_pointer (ctx, ctrl);
1632
ctrl->server_local = xcalloc (1, sizeof *ctrl->server_local);
1633
ctrl->server_local->assuan_ctx = ctx;
1634
ctrl->server_local->message_fd = -1;
1635
ctrl->server_local->use_cache_for_signing = 1;
1636
ctrl->digest.raw_value = 0;
1061
1638
if (DBG_ASSUAN)
1062
1639
assuan_set_log_stream (ctx, log_get_stream ());
1641
#ifdef HAVE_ASSUAN_SET_IO_MONITOR
1642
assuan_set_io_monitor (ctx, io_monitor);
1066
1647
rc = assuan_accept (ctx);
1073
log_info ("Assuan accept problem: %s\n", assuan_strerror (rc));
1654
log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
1077
1658
rc = assuan_process (ctx);
1080
log_info ("Assuan processing failed: %s\n", assuan_strerror (rc));
1661
log_info ("Assuan processing failed: %s\n", gpg_strerror (rc));
1085
1666
/* Reset the SCD if needed. */
1086
agent_reset_scd (&ctrl);
1667
agent_reset_scd (ctrl);
1088
1669
/* Reset the pinentry (in case of popup messages). */
1089
agent_reset_query (&ctrl);
1670
agent_reset_query (ctrl);
1091
1673
assuan_deinit_server (ctx);
1093
free (ctrl.display);
1095
free (ctrl.ttyname);
1097
free (ctrl.ttytype);
1099
free (ctrl.lc_ctype);
1100
if (ctrl.lc_messages)
1101
free (ctrl.lc_messages);
1102
xfree (ctrl.server_local);
1674
#ifdef HAVE_W32_SYSTEM
1675
if (ctrl->server_local->stopme)
1678
xfree (ctrl->server_local);
1679
ctrl->server_local = NULL;