1 /* server.c - Server mode and main entry point
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007, 2008, 2009 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
35 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
38 /* The filepointer for status message used in non-server mode */
39 static FILE *statusfp
;
41 /* Data used to assuciate an Assuan context with local server data */
42 struct server_local_s
{
43 assuan_context_t assuan_ctx
;
47 int list_to_output
; /* Write keylistings to the output fd. */
48 int enable_audit_log
; /* Use an audit log. */
50 certlist_t signerlist
;
51 certlist_t default_recplist
; /* As set by main() - don't release. */
52 int allow_pinentry_notify
; /* Set if pinentry notifications should
53 be passed back to the client. */
54 int no_encrypt_to
; /* Local version of option. */
58 /* Cookie definition for assuan data line output. */
59 static ssize_t
data_line_cookie_write (void *cookie
,
60 const void *buffer
, size_t size
);
61 static int data_line_cookie_close (void *cookie
);
62 static es_cookie_io_functions_t data_line_cookie_functions
=
65 data_line_cookie_write
,
67 data_line_cookie_close
72 static int command_has_option (const char *cmd
, const char *cmdopt
);
77 /* Note that it is sufficient to allocate the target string D as
78 long as the source string S, i.e.: strlen(s)+1; */
80 strcpy_escaped_plus (char *d
, const char *s
)
84 if (*s
== '%' && s
[1] && s
[2])
100 Blanks after the options are also removed. */
102 skip_options (const char *line
)
104 while (spacep (line
))
106 while ( *line
== '-' && line
[1] == '-' )
108 while (*line
&& !spacep (line
))
110 while (spacep (line
))
117 /* Check whether the option NAME appears in LINE */
119 has_option (const char *line
, const char *name
)
122 int n
= strlen (name
);
124 s
= strstr (line
, name
);
125 if (s
&& s
>= skip_options (line
))
127 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
131 /* A write handler used by es_fopencookie to write assuan data
134 data_line_cookie_write (void *cookie
, const void *buffer
, size_t size
)
136 assuan_context_t ctx
= cookie
;
138 if (assuan_send_data (ctx
, buffer
, size
))
148 data_line_cookie_close (void *cookie
)
150 assuan_context_t ctx
= cookie
;
152 if (assuan_send_data (ctx
, NULL
, 0))
163 close_message_fd (ctrl_t ctrl
)
165 if (ctrl
->server_local
->message_fd
!= -1)
167 close (ctrl
->server_local
->message_fd
);
168 ctrl
->server_local
->message_fd
= -1;
173 /* Start a new audit session if this has been enabled. */
175 start_audit_session (ctrl_t ctrl
)
177 audit_release (ctrl
->audit
);
179 if (ctrl
->server_local
->enable_audit_log
&& !(ctrl
->audit
= audit_new ()) )
180 return gpg_error_from_syserror ();
187 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
189 ctrl_t ctrl
= assuan_get_pointer (ctx
);
192 if (!strcmp (key
, "putenv"))
194 /* Change the session's environment to be used for the
195 Pinentry. Valid values are:
196 <NAME> Delete envvar NAME
197 <KEY>= Set envvar NAME to the empty string
198 <KEY>=<VALUE> Set envvar NAME to VALUE
200 err
= session_env_putenv (opt
.session_env
, value
);
202 else if (!strcmp (key
, "display"))
204 err
= session_env_setenv (opt
.session_env
, "DISPLAY", value
);
206 else if (!strcmp (key
, "ttyname"))
208 err
= session_env_setenv (opt
.session_env
, "GPG_TTY", value
);
210 else if (!strcmp (key
, "ttytype"))
212 err
= session_env_setenv (opt
.session_env
, "TERM", value
);
214 else if (!strcmp (key
, "lc-ctype"))
216 xfree (opt
.lc_ctype
);
217 opt
.lc_ctype
= xtrystrdup (value
);
219 err
= gpg_error_from_syserror ();
221 else if (!strcmp (key
, "lc-messages"))
223 xfree (opt
.lc_messages
);
224 opt
.lc_messages
= xtrystrdup (value
);
225 if (!opt
.lc_messages
)
226 err
= gpg_error_from_syserror ();
228 else if (!strcmp (key
, "xauthority"))
230 err
= session_env_setenv (opt
.session_env
, "XAUTHORITY", value
);
232 else if (!strcmp (key
, "pinentry-user-data"))
234 err
= session_env_setenv (opt
.session_env
, "PINENTRY_USER_DATA", value
);
236 else if (!strcmp (key
, "include-certs"))
238 int i
= *value
? atoi (value
) : -1;
239 if (ctrl
->include_certs
< -2)
240 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
242 ctrl
->include_certs
= i
;
244 else if (!strcmp (key
, "list-mode"))
246 int i
= *value
? atoi (value
) : 0;
247 if (!i
|| i
== 1) /* default and mode 1 */
249 ctrl
->server_local
->list_internal
= 1;
250 ctrl
->server_local
->list_external
= 0;
254 ctrl
->server_local
->list_internal
= 0;
255 ctrl
->server_local
->list_external
= 1;
259 ctrl
->server_local
->list_internal
= 1;
260 ctrl
->server_local
->list_external
= 1;
263 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
265 else if (!strcmp (key
, "list-to-output"))
267 int i
= *value
? atoi (value
) : 0;
268 ctrl
->server_local
->list_to_output
= i
;
270 else if (!strcmp (key
, "with-validation"))
272 int i
= *value
? atoi (value
) : 0;
273 ctrl
->with_validation
= i
;
275 else if (!strcmp (key
, "validation-model"))
277 int i
= gpgsm_parse_validation_model (value
);
278 if ( i
>= 0 && i
<= 1 )
279 ctrl
->validation_model
= i
;
281 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
283 else if (!strcmp (key
, "with-key-data"))
285 opt
.with_key_data
= 1;
287 else if (!strcmp (key
, "enable-audit-log"))
289 int i
= *value
? atoi (value
) : 0;
290 ctrl
->server_local
->enable_audit_log
= i
;
292 else if (!strcmp (key
, "allow-pinentry-notify"))
294 ctrl
->server_local
->allow_pinentry_notify
= 1;
296 else if (!strcmp (key
, "with-ephemeral-keys"))
298 int i
= *value
? atoi (value
) : 0;
299 ctrl
->with_ephemeral_keys
= i
;
301 else if (!strcmp (key
, "no-encrypt-to"))
303 ctrl
->server_local
->no_encrypt_to
= 1;
306 err
= gpg_error (GPG_ERR_UNKNOWN_OPTION
);
313 reset_notify (assuan_context_t ctx
)
315 ctrl_t ctrl
= assuan_get_pointer (ctx
);
317 gpgsm_release_certlist (ctrl
->server_local
->recplist
);
318 gpgsm_release_certlist (ctrl
->server_local
->signerlist
);
319 ctrl
->server_local
->recplist
= NULL
;
320 ctrl
->server_local
->signerlist
= NULL
;
321 close_message_fd (ctrl
);
322 assuan_close_input_fd (ctx
);
323 assuan_close_output_fd (ctx
);
328 input_notify (assuan_context_t ctx
, const char *line
)
330 ctrl_t ctrl
= assuan_get_pointer (ctx
);
332 ctrl
->autodetect_encoding
= 0;
335 if (strstr (line
, "--armor"))
337 else if (strstr (line
, "--base64"))
339 else if (strstr (line
, "--binary"))
342 ctrl
->autodetect_encoding
= 1;
346 output_notify (assuan_context_t ctx
, const char *line
)
348 ctrl_t ctrl
= assuan_get_pointer (ctx
);
350 ctrl
->create_pem
= 0;
351 ctrl
->create_base64
= 0;
352 if (strstr (line
, "--armor"))
353 ctrl
->create_pem
= 1;
354 else if (strstr (line
, "--base64"))
355 ctrl
->create_base64
= 1; /* just the raw output */
360 /* RECIPIENT <userID>
362 Set the recipient for the encryption. <userID> should be the
363 internal representation of the key; the server may accept any other
364 way of specification [we will support this]. If this is a valid and
365 trusted recipient the server does respond with OK, otherwise the
366 return is an ERR with the reason why the recipient can't be used,
367 the encryption will then not be done for this recipient. If the
368 policy is not to encrypt at all if not all recipients are valid, the
369 client has to take care of this. All RECIPIENT commands are
370 cumulative until a RESET or an successful ENCRYPT command. */
372 cmd_recipient (assuan_context_t ctx
, char *line
)
374 ctrl_t ctrl
= assuan_get_pointer (ctx
);
378 rc
= start_audit_session (ctrl
);
383 rc
= gpgsm_add_to_certlist (ctrl
, line
, 0,
384 &ctrl
->server_local
->recplist
, 0);
387 gpgsm_status2 (ctrl
, STATUS_INV_RECP
,
388 get_inv_recpsgnr_code (rc
), line
, NULL
);
396 Set the signer's keys for the signature creation. <userID> should
397 be the internal representation of the key; the server may accept any
398 other way of specification [we will support this]. If this is a
399 valid and usable signing key the server does respond with OK,
400 otherwise it returns an ERR with the reason why the key can't be
401 used, the signing will then not be done for this key. If the policy
402 is not to sign at all if not all signer keys are valid, the client
403 has to take care of this. All SIGNER commands are cumulative until
404 a RESET but they are *not* reset by an SIGN command becuase it can
405 be expected that set of signers are used for more than one sign
408 cmd_signer (assuan_context_t ctx
, char *line
)
410 ctrl_t ctrl
= assuan_get_pointer (ctx
);
413 rc
= gpgsm_add_to_certlist (ctrl
, line
, 1,
414 &ctrl
->server_local
->signerlist
, 0);
417 gpgsm_status2 (ctrl
, STATUS_INV_SGNR
,
418 get_inv_recpsgnr_code (rc
), line
, NULL
);
419 /* For compatibiliy reasons we also issue the old code after the
421 gpgsm_status2 (ctrl
, STATUS_INV_RECP
,
422 get_inv_recpsgnr_code (rc
), line
, NULL
);
430 Do the actual encryption process. Takes the plaintext from the INPUT
431 command, writes to the ciphertext to the file descriptor set with
432 the OUTPUT command, take the recipients form all the recipients set
433 so far. If this command fails the clients should try to delete all
434 output currently done or otherwise mark it as invalid. GPGSM does
435 ensure that there won't be any security problem with leftover data
436 on the output in this case.
438 This command should in general not fail, as all necessary checks
439 have been done while setting the recipients. The input and output
442 cmd_encrypt (assuan_context_t ctx
, char *line
)
444 ctrl_t ctrl
= assuan_get_pointer (ctx
);
452 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
454 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
455 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
457 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
459 out_fp
= fdopen (dup (out_fd
), "w");
461 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
463 /* Now add all encrypt-to marked recipients from the default
466 if (!opt
.no_encrypt_to
&& !ctrl
->server_local
->no_encrypt_to
)
468 for (cl
=ctrl
->server_local
->default_recplist
; !rc
&& cl
; cl
= cl
->next
)
469 if (cl
->is_encrypt_to
)
470 rc
= gpgsm_add_cert_to_certlist (ctrl
, cl
->cert
,
471 &ctrl
->server_local
->recplist
, 1);
474 rc
= ctrl
->audit
? 0 : start_audit_session (ctrl
);
476 rc
= gpgsm_encrypt (assuan_get_pointer (ctx
),
477 ctrl
->server_local
->recplist
,
481 gpgsm_release_certlist (ctrl
->server_local
->recplist
);
482 ctrl
->server_local
->recplist
= NULL
;
483 /* Close and reset the fd */
484 close_message_fd (ctrl
);
485 assuan_close_input_fd (ctx
);
486 assuan_close_output_fd (ctx
);
493 This performs the decrypt operation after doing some check on the
494 internal state. (e.g. that only needed data has been set). Because
495 it utilizes the GPG-Agent for the session key decryption, there is
496 no need to ask the client for a protecting passphrase - GpgAgent
497 does take care of this by requesting this from the user. */
499 cmd_decrypt (assuan_context_t ctx
, char *line
)
501 ctrl_t ctrl
= assuan_get_pointer (ctx
);
508 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
510 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
511 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
513 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
515 out_fp
= fdopen (dup(out_fd
), "w");
517 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
519 rc
= start_audit_session (ctrl
);
521 rc
= gpgsm_decrypt (ctrl
, inp_fd
, out_fp
);
524 /* close and reset the fd */
525 close_message_fd (ctrl
);
526 assuan_close_input_fd (ctx
);
527 assuan_close_output_fd (ctx
);
535 This does a verify operation on the message send to the input-FD.
536 The result is written out using status lines. If an output FD was
537 given, the signed text will be written to that.
539 If the signature is a detached one, the server will inquire about
540 the signed material and the client must provide it.
543 cmd_verify (assuan_context_t ctx
, char *line
)
546 ctrl_t ctrl
= assuan_get_pointer (ctx
);
547 int fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
548 int out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
554 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
558 out_fp
= fdopen ( dup(out_fd
), "w");
560 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
563 rc
= start_audit_session (ctrl
);
565 rc
= gpgsm_verify (assuan_get_pointer (ctx
), fd
,
566 ctrl
->server_local
->message_fd
, out_fp
);
570 /* close and reset the fd */
571 close_message_fd (ctrl
);
572 assuan_close_input_fd (ctx
);
573 assuan_close_output_fd (ctx
);
581 Sign the data set with the INPUT command and write it to the sink
582 set by OUTPUT. With "--detached" specified, a detached signature is
583 created (surprise). */
585 cmd_sign (assuan_context_t ctx
, char *line
)
587 ctrl_t ctrl
= assuan_get_pointer (ctx
);
593 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
595 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
596 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
598 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
600 detached
= has_option (line
, "--detached");
602 out_fp
= fdopen ( dup(out_fd
), "w");
604 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
606 rc
= start_audit_session (ctrl
);
608 rc
= gpgsm_sign (assuan_get_pointer (ctx
), ctrl
->server_local
->signerlist
,
609 inp_fd
, detached
, out_fp
);
612 /* close and reset the fd */
613 close_message_fd (ctrl
);
614 assuan_close_input_fd (ctx
);
615 assuan_close_output_fd (ctx
);
621 /* IMPORT [--re-import]
623 Import the certificates read form the input-fd, return status
624 message for each imported one. The import checks the validity of
625 the certificate but not of the entire chain. It is possible to
626 import expired certificates.
628 With the option --re-import the input data is expected to a be a LF
629 separated list of fingerprints. The command will re-import these
630 certificates, meaning that they are made permanent by removing
631 their ephemeral flag. */
633 cmd_import (assuan_context_t ctx
, char *line
)
635 ctrl_t ctrl
= assuan_get_pointer (ctx
);
637 int fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
638 int reimport
= has_option (line
, "--re-import");
643 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
645 rc
= gpgsm_import (assuan_get_pointer (ctx
), fd
, reimport
);
647 /* close and reset the fd */
648 close_message_fd (ctrl
);
649 assuan_close_input_fd (ctx
);
650 assuan_close_output_fd (ctx
);
656 /* EXPORT [--data [--armor|--base64]] [--] pattern
661 cmd_export (assuan_context_t ctx
, char *line
)
663 ctrl_t ctrl
= assuan_get_pointer (ctx
);
668 use_data
= has_option (line
, "--data");
672 /* We need to override any possible setting done by an OUTPUT command. */
673 ctrl
->create_pem
= has_option (line
, "--armor");
674 ctrl
->create_base64
= has_option (line
, "--base64");
677 line
= skip_options (line
);
679 /* Break the line down into an strlist_t. */
681 for (p
=line
; *p
; line
= p
)
683 while (*p
&& *p
!= ' ')
689 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
693 return out_of_core ();
696 strcpy_escaped_plus (sl
->d
, line
);
706 stream
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
710 return set_error (GPG_ERR_ASS_GENERAL
,
711 "error setting up a data stream");
713 gpgsm_export (ctrl
, list
, NULL
, stream
);
718 int fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
724 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
726 out_fp
= fdopen ( dup(fd
), "w");
730 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
733 gpgsm_export (ctrl
, list
, out_fp
, NULL
);
738 /* Close and reset the fds. */
739 close_message_fd (ctrl
);
740 assuan_close_input_fd (ctx
);
741 assuan_close_output_fd (ctx
);
747 cmd_delkeys (assuan_context_t ctx
, char *line
)
749 ctrl_t ctrl
= assuan_get_pointer (ctx
);
754 /* break the line down into an strlist_t */
756 for (p
=line
; *p
; line
= p
)
758 while (*p
&& *p
!= ' ')
764 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
768 return out_of_core ();
771 strcpy_escaped_plus (sl
->d
, line
);
777 rc
= gpgsm_delete (ctrl
, list
);
780 /* close and reset the fd */
781 close_message_fd (ctrl
);
782 assuan_close_input_fd (ctx
);
783 assuan_close_output_fd (ctx
);
792 Set the file descriptor to read a message which is used with
793 detached signatures */
795 cmd_message (assuan_context_t ctx
, char *line
)
800 ctrl_t ctrl
= assuan_get_pointer (ctx
);
802 rc
= assuan_command_parse_fd (ctx
, line
, &sysfd
);
805 fd
= translate_sys2libc_fd (sysfd
, 0);
807 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
808 ctrl
->server_local
->message_fd
= fd
;
812 /* LISTKEYS [<patterns>]
813 DUMPKEYS [<patterns>]
814 LISTSECRETKEYS [<patterns>]
815 DUMPSECRETKEYS [<patterns>]
818 do_listkeys (assuan_context_t ctx
, char *line
, int mode
)
820 ctrl_t ctrl
= assuan_get_pointer (ctx
);
824 unsigned int listmode
;
827 /* Break the line down into an strlist. */
829 for (p
=line
; *p
; line
= p
)
831 while (*p
&& *p
!= ' ')
837 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
841 return out_of_core ();
844 strcpy_escaped_plus (sl
->d
, line
);
850 if (ctrl
->server_local
->list_to_output
)
852 int outfd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
855 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
856 fp
= es_fdopen ( dup (outfd
), "w");
858 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen() failed");
862 fp
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
864 return set_error (GPG_ERR_ASS_GENERAL
,
865 "error setting up a data stream");
868 ctrl
->with_colons
= 1;
870 if (ctrl
->server_local
->list_internal
)
872 if (ctrl
->server_local
->list_external
)
874 err
= gpgsm_list_keys (assuan_get_pointer (ctx
), list
, fp
, listmode
);
877 if (ctrl
->server_local
->list_to_output
)
878 assuan_close_output_fd (ctx
);
883 cmd_listkeys (assuan_context_t ctx
, char *line
)
885 return do_listkeys (ctx
, line
, 3);
889 cmd_dumpkeys (assuan_context_t ctx
, char *line
)
891 return do_listkeys (ctx
, line
, 259);
895 cmd_listsecretkeys (assuan_context_t ctx
, char *line
)
897 return do_listkeys (ctx
, line
, 2);
901 cmd_dumpsecretkeys (assuan_context_t ctx
, char *line
)
903 return do_listkeys (ctx
, line
, 258);
909 Read the parameters in native format from the input fd and write a
910 certificate request to the output.
913 cmd_genkey (assuan_context_t ctx
, char *line
)
915 ctrl_t ctrl
= assuan_get_pointer (ctx
);
923 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
925 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
926 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
928 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
930 in_stream
= es_fdopen_nc (inp_fd
, "r");
932 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen failed");
934 out_fp
= fdopen ( dup(out_fd
), "w");
937 es_fclose (in_stream
);
938 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
940 rc
= gpgsm_genkey (ctrl
, in_stream
, out_fp
);
943 /* close and reset the fds */
944 assuan_close_input_fd (ctx
);
945 assuan_close_output_fd (ctx
);
952 /* GETAUDITLOG [--data] [--html]
954 !!!WORK in PROGRESS!!!
956 If --data is used, the output is send using D-lines and not to the
957 source given by an OUTPUT command.
959 If --html is used the output is formated as an XHTML block. This is
960 designed to be incorporated into a HTML document.
963 cmd_getauditlog (assuan_context_t ctx
, char *line
)
965 ctrl_t ctrl
= assuan_get_pointer (ctx
);
967 estream_t out_stream
;
968 int opt_data
, opt_html
;
971 opt_data
= has_option (line
, "--data");
972 opt_html
= has_option (line
, "--html");
973 line
= skip_options (line
);
976 return gpg_error (GPG_ERR_NO_DATA
);
980 out_stream
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
982 return set_error (GPG_ERR_ASS_GENERAL
,
983 "error setting up a data stream");
987 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
989 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
991 out_stream
= es_fdopen_nc ( dup (out_fd
), "w");
994 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen() failed");
998 audit_print_result (ctrl
->audit
, out_stream
, opt_html
);
1001 es_fclose (out_stream
);
1003 /* Close and reset the fd. */
1005 assuan_close_output_fd (ctx
);
1012 Multipurpose function to return a variety of information.
1013 Supported values for WHAT are:
1015 version - Return the version of the program.
1016 pid - Return the process id of the server.
1017 agent-check - Return success if the agent is running.
1018 cmd_has_option CMD OPT
1019 - Returns OK if the command CMD implements the option OPT.
1023 cmd_getinfo (assuan_context_t ctx
, char *line
)
1027 if (!strcmp (line
, "version"))
1029 const char *s
= VERSION
;
1030 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1032 else if (!strcmp (line
, "pid"))
1036 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
1037 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
1039 else if (!strcmp (line
, "agent-check"))
1041 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1042 rc
= gpgsm_agent_send_nop (ctrl
);
1044 else if (!strncmp (line
, "cmd_has_option", 14)
1045 && (line
[14] == ' ' || line
[14] == '\t' || !line
[14]))
1049 while (*line
== ' ' || *line
== '\t')
1052 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1056 while (*line
&& (*line
!= ' ' && *line
!= '\t'))
1059 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1063 while (*line
== ' ' || *line
== '\t')
1066 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1070 if (!command_has_option (cmd
, cmdopt
))
1071 rc
= gpg_error (GPG_ERR_GENERAL
);
1077 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
1084 /* Return true if the command CMD implements the option OPT. */
1086 command_has_option (const char *cmd
, const char *cmdopt
)
1088 if (!strcmp (cmd
, "IMPORT"))
1090 if (!strcmp (cmdopt
, "re-import"))
1098 /* Tell the assuan library about our commands */
1100 register_commands (assuan_context_t ctx
)
1104 int (*handler
)(assuan_context_t
, char *line
);
1106 { "RECIPIENT", cmd_recipient
},
1107 { "SIGNER", cmd_signer
},
1108 { "ENCRYPT", cmd_encrypt
},
1109 { "DECRYPT", cmd_decrypt
},
1110 { "VERIFY", cmd_verify
},
1111 { "SIGN", cmd_sign
},
1112 { "IMPORT", cmd_import
},
1113 { "EXPORT", cmd_export
},
1116 { "MESSAGE", cmd_message
},
1117 { "LISTKEYS", cmd_listkeys
},
1118 { "DUMPKEYS", cmd_dumpkeys
},
1119 { "LISTSECRETKEYS",cmd_listsecretkeys
},
1120 { "DUMPSECRETKEYS",cmd_dumpsecretkeys
},
1121 { "GENKEY", cmd_genkey
},
1122 { "DELKEYS", cmd_delkeys
},
1123 { "GETAUDITLOG", cmd_getauditlog
},
1124 { "GETINFO", cmd_getinfo
},
1129 for (i
=0; table
[i
].name
; i
++)
1131 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
1138 /* Startup the server. DEFAULT_RECPLIST is the list of recipients as
1139 set from the command line or config file. We only require those
1140 marked as encrypt-to. */
1142 gpgsm_server (certlist_t default_recplist
)
1146 assuan_context_t ctx
;
1147 struct server_control_s ctrl
;
1148 static const char hello
[] = ("GNU Privacy Guard's S/M server "
1151 memset (&ctrl
, 0, sizeof ctrl
);
1152 gpgsm_init_default_ctrl (&ctrl
);
1154 /* We use a pipe based server so that we can work from scripts.
1155 assuan_init_pipe_server will automagically detect when we are
1156 called with a socketpair and ignore FIELDES in this case. */
1159 rc
= assuan_init_pipe_server (&ctx
, filedes
);
1162 log_error ("failed to initialize the server: %s\n",
1166 rc
= register_commands (ctx
);
1169 log_error ("failed to the register commands with Assuan: %s\n",
1173 if (opt
.verbose
|| opt
.debug
)
1176 const char *s1
= getenv ("GPG_AGENT_INFO");
1177 const char *s2
= getenv ("DIRMNGR_INFO");
1186 opt
.config_filename
,
1191 assuan_set_hello_line (ctx
, tmp
);
1196 assuan_set_hello_line (ctx
, hello
);
1198 assuan_register_reset_notify (ctx
, reset_notify
);
1199 assuan_register_input_notify (ctx
, input_notify
);
1200 assuan_register_output_notify (ctx
, output_notify
);
1201 assuan_register_option_handler (ctx
, option_handler
);
1203 assuan_set_pointer (ctx
, &ctrl
);
1204 ctrl
.server_local
= xcalloc (1, sizeof *ctrl
.server_local
);
1205 ctrl
.server_local
->assuan_ctx
= ctx
;
1206 ctrl
.server_local
->message_fd
= -1;
1207 ctrl
.server_local
->list_internal
= 1;
1208 ctrl
.server_local
->list_external
= 0;
1209 ctrl
.server_local
->default_recplist
= default_recplist
;
1212 assuan_set_log_stream (ctx
, log_get_stream ());
1216 rc
= assuan_accept (ctx
);
1223 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
1227 rc
= assuan_process (ctx
);
1230 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
1235 gpgsm_release_certlist (ctrl
.server_local
->recplist
);
1236 ctrl
.server_local
->recplist
= NULL
;
1237 gpgsm_release_certlist (ctrl
.server_local
->signerlist
);
1238 ctrl
.server_local
->signerlist
= NULL
;
1239 xfree (ctrl
.server_local
);
1241 audit_release (ctrl
.audit
);
1244 assuan_deinit_server (ctx
);
1250 gpgsm_status2 (ctrl_t ctrl
, int no
, ...)
1252 gpg_error_t err
= 0;
1256 va_start (arg_ptr
, no
);
1258 if (ctrl
->no_server
&& ctrl
->status_fd
== -1)
1259 ; /* No status wanted. */
1260 else if (ctrl
->no_server
)
1264 if (ctrl
->status_fd
== 1)
1266 else if (ctrl
->status_fd
== 2)
1269 statusfp
= fdopen (ctrl
->status_fd
, "w");
1273 log_fatal ("can't open fd %d for status output: %s\n",
1274 ctrl
->status_fd
, strerror(errno
));
1278 fputs ("[GNUPG:] ", statusfp
);
1279 fputs (get_status_string (no
), statusfp
);
1281 while ( (text
= va_arg (arg_ptr
, const char*) ))
1283 putc ( ' ', statusfp
);
1284 for (; *text
; text
++)
1287 fputs ( "\\n", statusfp
);
1288 else if (*text
== '\r')
1289 fputs ( "\\r", statusfp
);
1291 putc ( *(const byte
*)text
, statusfp
);
1294 putc ('\n', statusfp
);
1299 assuan_context_t ctx
= ctrl
->server_local
->assuan_ctx
;
1305 while ( (text
= va_arg (arg_ptr
, const char *)) )
1312 for ( ; *text
&& n
< DIM (buf
)-2; n
++)
1316 err
= assuan_write_status (ctx
, get_status_string (no
), buf
);
1324 gpgsm_status (ctrl_t ctrl
, int no
, const char *text
)
1326 return gpgsm_status2 (ctrl
, no
, text
, NULL
);
1330 gpgsm_status_with_err_code (ctrl_t ctrl
, int no
, const char *text
,
1335 sprintf (buf
, "%u", (unsigned int)ec
);
1337 return gpgsm_status2 (ctrl
, no
, text
, buf
, NULL
);
1339 return gpgsm_status2 (ctrl
, no
, buf
, NULL
);
1343 /* Helper to notify the client about Pinentry events. Because that
1344 might disturb some older clients, this is only done when enabled
1345 via an option. Returns an gpg error code. */
1347 gpgsm_proxy_pinentry_notify (ctrl_t ctrl
, const unsigned char *line
)
1349 if (!ctrl
|| !ctrl
->server_local
1350 || !ctrl
->server_local
->allow_pinentry_notify
)
1352 return assuan_inquire (ctrl
->server_local
->assuan_ctx
, line
, NULL
, NULL
, 0);