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/>.
34 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
37 /* The filepointer for status message used in non-server mode */
38 static FILE *statusfp
;
40 /* Data used to assuciate an Assuan context with local server data */
41 struct server_local_s
{
42 assuan_context_t assuan_ctx
;
46 int list_to_output
; /* Write keylistings to the output fd. */
47 int enable_audit_log
; /* Use an audit log. */
49 certlist_t signerlist
;
50 certlist_t default_recplist
; /* As set by main() - don't release. */
51 int allow_pinentry_notify
; /* Set if pinentry notifications should
52 be passed back to the client. */
53 int no_encrypt_to
; /* Local version of option. */
57 /* Cookie definition for assuan data line output. */
58 static ssize_t
data_line_cookie_write (void *cookie
,
59 const void *buffer
, size_t size
);
60 static int data_line_cookie_close (void *cookie
);
61 static es_cookie_io_functions_t data_line_cookie_functions
=
64 data_line_cookie_write
,
66 data_line_cookie_close
71 static int command_has_option (const char *cmd
, const char *cmdopt
);
76 /* Note that it is sufficient to allocate the target string D as
77 long as the source string S, i.e.: strlen(s)+1; */
79 strcpy_escaped_plus (char *d
, const char *s
)
83 if (*s
== '%' && s
[1] && s
[2])
99 Blanks after the options are also removed. */
101 skip_options (const char *line
)
103 while (spacep (line
))
105 while ( *line
== '-' && line
[1] == '-' )
107 while (*line
&& !spacep (line
))
109 while (spacep (line
))
116 /* Check whether the option NAME appears in LINE */
118 has_option (const char *line
, const char *name
)
121 int n
= strlen (name
);
123 s
= strstr (line
, name
);
124 if (s
&& s
>= skip_options (line
))
126 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
130 /* A write handler used by es_fopencookie to write assuan data
133 data_line_cookie_write (void *cookie
, const void *buffer
, size_t size
)
135 assuan_context_t ctx
= cookie
;
137 if (assuan_send_data (ctx
, buffer
, size
))
147 data_line_cookie_close (void *cookie
)
149 assuan_context_t ctx
= cookie
;
151 if (assuan_send_data (ctx
, NULL
, 0))
162 close_message_fd (ctrl_t ctrl
)
164 if (ctrl
->server_local
->message_fd
!= -1)
166 close (ctrl
->server_local
->message_fd
);
167 ctrl
->server_local
->message_fd
= -1;
172 /* Start a new audit session if this has been enabled. */
174 start_audit_session (ctrl_t ctrl
)
176 audit_release (ctrl
->audit
);
178 if (ctrl
->server_local
->enable_audit_log
&& !(ctrl
->audit
= audit_new ()) )
179 return gpg_error_from_syserror ();
186 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
188 ctrl_t ctrl
= assuan_get_pointer (ctx
);
191 if (!strcmp (key
, "putenv"))
193 /* Change the session's environment to be used for the
194 Pinentry. Valid values are:
195 <NAME> Delete envvar NAME
196 <KEY>= Set envvar NAME to the empty string
197 <KEY>=<VALUE> Set envvar NAME to VALUE
199 err
= session_env_putenv (opt
.session_env
, value
);
201 else if (!strcmp (key
, "display"))
203 err
= session_env_setenv (opt
.session_env
, "DISPLAY", value
);
205 else if (!strcmp (key
, "ttyname"))
207 err
= session_env_setenv (opt
.session_env
, "GPG_TTY", value
);
209 else if (!strcmp (key
, "ttytype"))
211 err
= session_env_setenv (opt
.session_env
, "TERM", value
);
213 else if (!strcmp (key
, "lc-ctype"))
215 xfree (opt
.lc_ctype
);
216 opt
.lc_ctype
= xtrystrdup (value
);
218 err
= gpg_error_from_syserror ();
220 else if (!strcmp (key
, "lc-messages"))
222 xfree (opt
.lc_messages
);
223 opt
.lc_messages
= xtrystrdup (value
);
224 if (!opt
.lc_messages
)
225 err
= gpg_error_from_syserror ();
227 else if (!strcmp (key
, "xauthority"))
229 err
= session_env_setenv (opt
.session_env
, "XAUTHORITY", value
);
231 else if (!strcmp (key
, "pinentry-user-data"))
233 err
= session_env_setenv (opt
.session_env
, "PINENTRY_USER_DATA", value
);
235 else if (!strcmp (key
, "include-certs"))
237 int i
= *value
? atoi (value
) : -1;
238 if (ctrl
->include_certs
< -2)
239 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
241 ctrl
->include_certs
= i
;
243 else if (!strcmp (key
, "list-mode"))
245 int i
= *value
? atoi (value
) : 0;
246 if (!i
|| i
== 1) /* default and mode 1 */
248 ctrl
->server_local
->list_internal
= 1;
249 ctrl
->server_local
->list_external
= 0;
253 ctrl
->server_local
->list_internal
= 0;
254 ctrl
->server_local
->list_external
= 1;
258 ctrl
->server_local
->list_internal
= 1;
259 ctrl
->server_local
->list_external
= 1;
262 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
264 else if (!strcmp (key
, "list-to-output"))
266 int i
= *value
? atoi (value
) : 0;
267 ctrl
->server_local
->list_to_output
= i
;
269 else if (!strcmp (key
, "with-validation"))
271 int i
= *value
? atoi (value
) : 0;
272 ctrl
->with_validation
= i
;
274 else if (!strcmp (key
, "validation-model"))
276 int i
= gpgsm_parse_validation_model (value
);
277 if ( i
>= 0 && i
<= 1 )
278 ctrl
->validation_model
= i
;
280 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
282 else if (!strcmp (key
, "with-key-data"))
284 opt
.with_key_data
= 1;
286 else if (!strcmp (key
, "enable-audit-log"))
288 int i
= *value
? atoi (value
) : 0;
289 ctrl
->server_local
->enable_audit_log
= i
;
291 else if (!strcmp (key
, "allow-pinentry-notify"))
293 ctrl
->server_local
->allow_pinentry_notify
= 1;
295 else if (!strcmp (key
, "with-ephemeral-keys"))
297 int i
= *value
? atoi (value
) : 0;
298 ctrl
->with_ephemeral_keys
= i
;
300 else if (!strcmp (key
, "no-encrypt-to"))
302 ctrl
->server_local
->no_encrypt_to
= 1;
305 err
= gpg_error (GPG_ERR_UNKNOWN_OPTION
);
312 reset_notify (assuan_context_t ctx
, char *line
)
314 ctrl_t ctrl
= assuan_get_pointer (ctx
);
318 gpgsm_release_certlist (ctrl
->server_local
->recplist
);
319 gpgsm_release_certlist (ctrl
->server_local
->signerlist
);
320 ctrl
->server_local
->recplist
= NULL
;
321 ctrl
->server_local
->signerlist
= NULL
;
322 close_message_fd (ctrl
);
323 assuan_close_input_fd (ctx
);
324 assuan_close_output_fd (ctx
);
330 input_notify (assuan_context_t ctx
, char *line
)
332 ctrl_t ctrl
= assuan_get_pointer (ctx
);
334 ctrl
->autodetect_encoding
= 0;
337 if (strstr (line
, "--armor"))
339 else if (strstr (line
, "--base64"))
341 else if (strstr (line
, "--binary"))
344 ctrl
->autodetect_encoding
= 1;
349 output_notify (assuan_context_t ctx
, char *line
)
351 ctrl_t ctrl
= assuan_get_pointer (ctx
);
353 ctrl
->create_pem
= 0;
354 ctrl
->create_base64
= 0;
355 if (strstr (line
, "--armor"))
356 ctrl
->create_pem
= 1;
357 else if (strstr (line
, "--base64"))
358 ctrl
->create_base64
= 1; /* just the raw output */
364 /* RECIPIENT <userID>
366 Set the recipient for the encryption. <userID> should be the
367 internal representation of the key; the server may accept any other
368 way of specification [we will support this]. If this is a valid and
369 trusted recipient the server does respond with OK, otherwise the
370 return is an ERR with the reason why the recipient can't be used,
371 the encryption will then not be done for this recipient. If the
372 policy is not to encrypt at all if not all recipients are valid, the
373 client has to take care of this. All RECIPIENT commands are
374 cumulative until a RESET or an successful ENCRYPT command. */
376 cmd_recipient (assuan_context_t ctx
, char *line
)
378 ctrl_t ctrl
= assuan_get_pointer (ctx
);
382 rc
= start_audit_session (ctrl
);
387 rc
= gpgsm_add_to_certlist (ctrl
, line
, 0,
388 &ctrl
->server_local
->recplist
, 0);
391 gpgsm_status2 (ctrl
, STATUS_INV_RECP
,
392 get_inv_recpsgnr_code (rc
), line
, NULL
);
400 Set the signer's keys for the signature creation. <userID> should
401 be the internal representation of the key; the server may accept any
402 other way of specification [we will support this]. If this is a
403 valid and usable signing key the server does respond with OK,
404 otherwise it returns an ERR with the reason why the key can't be
405 used, the signing will then not be done for this key. If the policy
406 is not to sign at all if not all signer keys are valid, the client
407 has to take care of this. All SIGNER commands are cumulative until
408 a RESET but they are *not* reset by an SIGN command becuase it can
409 be expected that set of signers are used for more than one sign
412 cmd_signer (assuan_context_t ctx
, char *line
)
414 ctrl_t ctrl
= assuan_get_pointer (ctx
);
417 rc
= gpgsm_add_to_certlist (ctrl
, line
, 1,
418 &ctrl
->server_local
->signerlist
, 0);
421 gpgsm_status2 (ctrl
, STATUS_INV_SGNR
,
422 get_inv_recpsgnr_code (rc
), line
, NULL
);
423 /* For compatibiliy reasons we also issue the old code after the
425 gpgsm_status2 (ctrl
, STATUS_INV_RECP
,
426 get_inv_recpsgnr_code (rc
), line
, NULL
);
434 Do the actual encryption process. Takes the plaintext from the INPUT
435 command, writes to the ciphertext to the file descriptor set with
436 the OUTPUT command, take the recipients form all the recipients set
437 so far. If this command fails the clients should try to delete all
438 output currently done or otherwise mark it as invalid. GPGSM does
439 ensure that there won't be any security problem with leftover data
440 on the output in this case.
442 This command should in general not fail, as all necessary checks
443 have been done while setting the recipients. The input and output
446 cmd_encrypt (assuan_context_t ctx
, char *line
)
448 ctrl_t ctrl
= assuan_get_pointer (ctx
);
456 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
458 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
459 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
461 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
463 out_fp
= fdopen (dup (out_fd
), "w");
465 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
467 /* Now add all encrypt-to marked recipients from the default
470 if (!opt
.no_encrypt_to
&& !ctrl
->server_local
->no_encrypt_to
)
472 for (cl
=ctrl
->server_local
->default_recplist
; !rc
&& cl
; cl
= cl
->next
)
473 if (cl
->is_encrypt_to
)
474 rc
= gpgsm_add_cert_to_certlist (ctrl
, cl
->cert
,
475 &ctrl
->server_local
->recplist
, 1);
478 rc
= ctrl
->audit
? 0 : start_audit_session (ctrl
);
480 rc
= gpgsm_encrypt (assuan_get_pointer (ctx
),
481 ctrl
->server_local
->recplist
,
485 gpgsm_release_certlist (ctrl
->server_local
->recplist
);
486 ctrl
->server_local
->recplist
= NULL
;
487 /* Close and reset the fd */
488 close_message_fd (ctrl
);
489 assuan_close_input_fd (ctx
);
490 assuan_close_output_fd (ctx
);
497 This performs the decrypt operation after doing some check on the
498 internal state. (e.g. that only needed data has been set). Because
499 it utilizes the GPG-Agent for the session key decryption, there is
500 no need to ask the client for a protecting passphrase - GpgAgent
501 does take care of this by requesting this from the user. */
503 cmd_decrypt (assuan_context_t ctx
, char *line
)
505 ctrl_t ctrl
= assuan_get_pointer (ctx
);
512 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
514 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
515 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
517 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
519 out_fp
= fdopen (dup(out_fd
), "w");
521 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
523 rc
= start_audit_session (ctrl
);
525 rc
= gpgsm_decrypt (ctrl
, inp_fd
, out_fp
);
528 /* close and reset the fd */
529 close_message_fd (ctrl
);
530 assuan_close_input_fd (ctx
);
531 assuan_close_output_fd (ctx
);
539 This does a verify operation on the message send to the input-FD.
540 The result is written out using status lines. If an output FD was
541 given, the signed text will be written to that.
543 If the signature is a detached one, the server will inquire about
544 the signed material and the client must provide it.
547 cmd_verify (assuan_context_t ctx
, char *line
)
550 ctrl_t ctrl
= assuan_get_pointer (ctx
);
551 int fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
552 int out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
558 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
562 out_fp
= fdopen ( dup(out_fd
), "w");
564 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
567 rc
= start_audit_session (ctrl
);
569 rc
= gpgsm_verify (assuan_get_pointer (ctx
), fd
,
570 ctrl
->server_local
->message_fd
, out_fp
);
574 /* close and reset the fd */
575 close_message_fd (ctrl
);
576 assuan_close_input_fd (ctx
);
577 assuan_close_output_fd (ctx
);
585 Sign the data set with the INPUT command and write it to the sink
586 set by OUTPUT. With "--detached" specified, a detached signature is
587 created (surprise). */
589 cmd_sign (assuan_context_t ctx
, char *line
)
591 ctrl_t ctrl
= assuan_get_pointer (ctx
);
597 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
599 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
600 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
602 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
604 detached
= has_option (line
, "--detached");
606 out_fp
= fdopen ( dup(out_fd
), "w");
608 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
610 rc
= start_audit_session (ctrl
);
612 rc
= gpgsm_sign (assuan_get_pointer (ctx
), ctrl
->server_local
->signerlist
,
613 inp_fd
, detached
, out_fp
);
616 /* close and reset the fd */
617 close_message_fd (ctrl
);
618 assuan_close_input_fd (ctx
);
619 assuan_close_output_fd (ctx
);
625 /* IMPORT [--re-import]
627 Import the certificates read form the input-fd, return status
628 message for each imported one. The import checks the validity of
629 the certificate but not of the entire chain. It is possible to
630 import expired certificates.
632 With the option --re-import the input data is expected to a be a LF
633 separated list of fingerprints. The command will re-import these
634 certificates, meaning that they are made permanent by removing
635 their ephemeral flag. */
637 cmd_import (assuan_context_t ctx
, char *line
)
639 ctrl_t ctrl
= assuan_get_pointer (ctx
);
641 int fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
642 int reimport
= has_option (line
, "--re-import");
647 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
649 rc
= gpgsm_import (assuan_get_pointer (ctx
), fd
, reimport
);
651 /* close and reset the fd */
652 close_message_fd (ctrl
);
653 assuan_close_input_fd (ctx
);
654 assuan_close_output_fd (ctx
);
660 /* EXPORT [--data [--armor|--base64]] [--] pattern
665 cmd_export (assuan_context_t ctx
, char *line
)
667 ctrl_t ctrl
= assuan_get_pointer (ctx
);
672 use_data
= has_option (line
, "--data");
676 /* We need to override any possible setting done by an OUTPUT command. */
677 ctrl
->create_pem
= has_option (line
, "--armor");
678 ctrl
->create_base64
= has_option (line
, "--base64");
681 line
= skip_options (line
);
683 /* Break the line down into an strlist_t. */
685 for (p
=line
; *p
; line
= p
)
687 while (*p
&& *p
!= ' ')
693 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
697 return out_of_core ();
700 strcpy_escaped_plus (sl
->d
, line
);
710 stream
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
714 return set_error (GPG_ERR_ASS_GENERAL
,
715 "error setting up a data stream");
717 gpgsm_export (ctrl
, list
, NULL
, stream
);
722 int fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
728 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
730 out_fp
= fdopen ( dup(fd
), "w");
734 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
737 gpgsm_export (ctrl
, list
, out_fp
, NULL
);
742 /* Close and reset the fds. */
743 close_message_fd (ctrl
);
744 assuan_close_input_fd (ctx
);
745 assuan_close_output_fd (ctx
);
751 cmd_delkeys (assuan_context_t ctx
, char *line
)
753 ctrl_t ctrl
= assuan_get_pointer (ctx
);
758 /* break the line down into an strlist_t */
760 for (p
=line
; *p
; line
= p
)
762 while (*p
&& *p
!= ' ')
768 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
772 return out_of_core ();
775 strcpy_escaped_plus (sl
->d
, line
);
781 rc
= gpgsm_delete (ctrl
, list
);
784 /* close and reset the fd */
785 close_message_fd (ctrl
);
786 assuan_close_input_fd (ctx
);
787 assuan_close_output_fd (ctx
);
796 Set the file descriptor to read a message which is used with
797 detached signatures */
799 cmd_message (assuan_context_t ctx
, char *line
)
804 ctrl_t ctrl
= assuan_get_pointer (ctx
);
806 rc
= assuan_command_parse_fd (ctx
, line
, &sysfd
);
809 fd
= translate_sys2libc_fd (sysfd
, 0);
811 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
812 ctrl
->server_local
->message_fd
= fd
;
816 /* LISTKEYS [<patterns>]
817 DUMPKEYS [<patterns>]
818 LISTSECRETKEYS [<patterns>]
819 DUMPSECRETKEYS [<patterns>]
822 do_listkeys (assuan_context_t ctx
, char *line
, int mode
)
824 ctrl_t ctrl
= assuan_get_pointer (ctx
);
828 unsigned int listmode
;
831 /* Break the line down into an strlist. */
833 for (p
=line
; *p
; line
= p
)
835 while (*p
&& *p
!= ' ')
841 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
845 return out_of_core ();
848 strcpy_escaped_plus (sl
->d
, line
);
854 if (ctrl
->server_local
->list_to_output
)
856 int outfd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
859 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
860 fp
= es_fdopen ( dup (outfd
), "w");
862 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen() failed");
866 fp
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
868 return set_error (GPG_ERR_ASS_GENERAL
,
869 "error setting up a data stream");
872 ctrl
->with_colons
= 1;
874 if (ctrl
->server_local
->list_internal
)
876 if (ctrl
->server_local
->list_external
)
878 err
= gpgsm_list_keys (assuan_get_pointer (ctx
), list
, fp
, listmode
);
881 if (ctrl
->server_local
->list_to_output
)
882 assuan_close_output_fd (ctx
);
887 cmd_listkeys (assuan_context_t ctx
, char *line
)
889 return do_listkeys (ctx
, line
, 3);
893 cmd_dumpkeys (assuan_context_t ctx
, char *line
)
895 return do_listkeys (ctx
, line
, 259);
899 cmd_listsecretkeys (assuan_context_t ctx
, char *line
)
901 return do_listkeys (ctx
, line
, 2);
905 cmd_dumpsecretkeys (assuan_context_t ctx
, char *line
)
907 return do_listkeys (ctx
, line
, 258);
913 Read the parameters in native format from the input fd and write a
914 certificate request to the output.
917 cmd_genkey (assuan_context_t ctx
, char *line
)
919 ctrl_t ctrl
= assuan_get_pointer (ctx
);
927 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
929 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
930 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
932 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
934 in_stream
= es_fdopen_nc (inp_fd
, "r");
936 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen failed");
938 out_fp
= fdopen ( dup(out_fd
), "w");
941 es_fclose (in_stream
);
942 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
944 rc
= gpgsm_genkey (ctrl
, in_stream
, out_fp
);
947 /* close and reset the fds */
948 assuan_close_input_fd (ctx
);
949 assuan_close_output_fd (ctx
);
956 /* GETAUDITLOG [--data] [--html]
958 !!!WORK in PROGRESS!!!
960 If --data is used, the output is send using D-lines and not to the
961 source given by an OUTPUT command.
963 If --html is used the output is formated as an XHTML block. This is
964 designed to be incorporated into a HTML document.
967 cmd_getauditlog (assuan_context_t ctx
, char *line
)
969 ctrl_t ctrl
= assuan_get_pointer (ctx
);
971 estream_t out_stream
;
972 int opt_data
, opt_html
;
975 opt_data
= has_option (line
, "--data");
976 opt_html
= has_option (line
, "--html");
977 line
= skip_options (line
);
980 return gpg_error (GPG_ERR_NO_DATA
);
984 out_stream
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
986 return set_error (GPG_ERR_ASS_GENERAL
,
987 "error setting up a data stream");
991 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
993 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
995 out_stream
= es_fdopen_nc ( dup (out_fd
), "w");
998 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen() failed");
1002 audit_print_result (ctrl
->audit
, out_stream
, opt_html
);
1005 es_fclose (out_stream
);
1007 /* Close and reset the fd. */
1009 assuan_close_output_fd (ctx
);
1016 Multipurpose function to return a variety of information.
1017 Supported values for WHAT are:
1019 version - Return the version of the program.
1020 pid - Return the process id of the server.
1021 agent-check - Return success if the agent is running.
1022 cmd_has_option CMD OPT
1023 - Returns OK if the command CMD implements the option OPT.
1027 cmd_getinfo (assuan_context_t ctx
, char *line
)
1031 if (!strcmp (line
, "version"))
1033 const char *s
= VERSION
;
1034 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1036 else if (!strcmp (line
, "pid"))
1040 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
1041 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
1043 else if (!strcmp (line
, "agent-check"))
1045 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1046 rc
= gpgsm_agent_send_nop (ctrl
);
1048 else if (!strncmp (line
, "cmd_has_option", 14)
1049 && (line
[14] == ' ' || line
[14] == '\t' || !line
[14]))
1053 while (*line
== ' ' || *line
== '\t')
1056 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1060 while (*line
&& (*line
!= ' ' && *line
!= '\t'))
1063 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1067 while (*line
== ' ' || *line
== '\t')
1070 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1074 if (!command_has_option (cmd
, cmdopt
))
1075 rc
= gpg_error (GPG_ERR_GENERAL
);
1081 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
1088 /* Return true if the command CMD implements the option OPT. */
1090 command_has_option (const char *cmd
, const char *cmdopt
)
1092 if (!strcmp (cmd
, "IMPORT"))
1094 if (!strcmp (cmdopt
, "re-import"))
1102 /* Tell the assuan library about our commands */
1104 register_commands (assuan_context_t ctx
)
1108 assuan_handler_t handler
;
1110 { "RECIPIENT", cmd_recipient
},
1111 { "SIGNER", cmd_signer
},
1112 { "ENCRYPT", cmd_encrypt
},
1113 { "DECRYPT", cmd_decrypt
},
1114 { "VERIFY", cmd_verify
},
1115 { "SIGN", cmd_sign
},
1116 { "IMPORT", cmd_import
},
1117 { "EXPORT", cmd_export
},
1120 { "MESSAGE", cmd_message
},
1121 { "LISTKEYS", cmd_listkeys
},
1122 { "DUMPKEYS", cmd_dumpkeys
},
1123 { "LISTSECRETKEYS",cmd_listsecretkeys
},
1124 { "DUMPSECRETKEYS",cmd_dumpsecretkeys
},
1125 { "GENKEY", cmd_genkey
},
1126 { "DELKEYS", cmd_delkeys
},
1127 { "GETAUDITLOG", cmd_getauditlog
},
1128 { "GETINFO", cmd_getinfo
},
1133 for (i
=0; table
[i
].name
; i
++)
1135 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
, NULL
);
1142 /* Startup the server. DEFAULT_RECPLIST is the list of recipients as
1143 set from the command line or config file. We only require those
1144 marked as encrypt-to. */
1146 gpgsm_server (certlist_t default_recplist
)
1150 assuan_context_t ctx
;
1151 struct server_control_s ctrl
;
1152 static const char hello
[] = ("GNU Privacy Guard's S/M server "
1155 memset (&ctrl
, 0, sizeof ctrl
);
1156 gpgsm_init_default_ctrl (&ctrl
);
1158 /* We use a pipe based server so that we can work from scripts.
1159 assuan_init_pipe_server will automagically detect when we are
1160 called with a socketpair and ignore FIELDES in this case. */
1163 rc
= assuan_new (&ctx
);
1166 log_error ("failed to allocate assuan context: %s\n",
1171 rc
= assuan_init_pipe_server (ctx
, filedes
);
1174 log_error ("failed to initialize the server: %s\n",
1178 rc
= register_commands (ctx
);
1181 log_error ("failed to the register commands with Assuan: %s\n",
1185 if (opt
.verbose
|| opt
.debug
)
1188 const char *s1
= getenv ("GPG_AGENT_INFO");
1189 const char *s2
= getenv ("DIRMNGR_INFO");
1198 opt
.config_filename
,
1203 assuan_set_hello_line (ctx
, tmp
);
1208 assuan_set_hello_line (ctx
, hello
);
1210 assuan_register_reset_notify (ctx
, reset_notify
);
1211 assuan_register_input_notify (ctx
, input_notify
);
1212 assuan_register_output_notify (ctx
, output_notify
);
1213 assuan_register_option_handler (ctx
, option_handler
);
1215 assuan_set_pointer (ctx
, &ctrl
);
1216 ctrl
.server_local
= xcalloc (1, sizeof *ctrl
.server_local
);
1217 ctrl
.server_local
->assuan_ctx
= ctx
;
1218 ctrl
.server_local
->message_fd
= -1;
1219 ctrl
.server_local
->list_internal
= 1;
1220 ctrl
.server_local
->list_external
= 0;
1221 ctrl
.server_local
->default_recplist
= default_recplist
;
1224 assuan_set_log_stream (ctx
, log_get_stream ());
1228 rc
= assuan_accept (ctx
);
1235 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
1239 rc
= assuan_process (ctx
);
1242 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
1247 gpgsm_release_certlist (ctrl
.server_local
->recplist
);
1248 ctrl
.server_local
->recplist
= NULL
;
1249 gpgsm_release_certlist (ctrl
.server_local
->signerlist
);
1250 ctrl
.server_local
->signerlist
= NULL
;
1251 xfree (ctrl
.server_local
);
1253 audit_release (ctrl
.audit
);
1256 assuan_release (ctx
);
1262 gpgsm_status2 (ctrl_t ctrl
, int no
, ...)
1264 gpg_error_t err
= 0;
1268 va_start (arg_ptr
, no
);
1270 if (ctrl
->no_server
&& ctrl
->status_fd
== -1)
1271 ; /* No status wanted. */
1272 else if (ctrl
->no_server
)
1276 if (ctrl
->status_fd
== 1)
1278 else if (ctrl
->status_fd
== 2)
1281 statusfp
= fdopen (ctrl
->status_fd
, "w");
1285 log_fatal ("can't open fd %d for status output: %s\n",
1286 ctrl
->status_fd
, strerror(errno
));
1290 fputs ("[GNUPG:] ", statusfp
);
1291 fputs (get_status_string (no
), statusfp
);
1293 while ( (text
= va_arg (arg_ptr
, const char*) ))
1295 putc ( ' ', statusfp
);
1296 for (; *text
; text
++)
1299 fputs ( "\\n", statusfp
);
1300 else if (*text
== '\r')
1301 fputs ( "\\r", statusfp
);
1303 putc ( *(const byte
*)text
, statusfp
);
1306 putc ('\n', statusfp
);
1311 assuan_context_t ctx
= ctrl
->server_local
->assuan_ctx
;
1317 while ( (text
= va_arg (arg_ptr
, const char *)) )
1324 for ( ; *text
&& n
< DIM (buf
)-2; n
++)
1328 err
= assuan_write_status (ctx
, get_status_string (no
), buf
);
1336 gpgsm_status (ctrl_t ctrl
, int no
, const char *text
)
1338 return gpgsm_status2 (ctrl
, no
, text
, NULL
);
1342 gpgsm_status_with_err_code (ctrl_t ctrl
, int no
, const char *text
,
1347 sprintf (buf
, "%u", (unsigned int)ec
);
1349 return gpgsm_status2 (ctrl
, no
, text
, buf
, NULL
);
1351 return gpgsm_status2 (ctrl
, no
, buf
, NULL
);
1355 /* Helper to notify the client about Pinentry events. Because that
1356 might disturb some older clients, this is only done when enabled
1357 via an option. Returns an gpg error code. */
1359 gpgsm_proxy_pinentry_notify (ctrl_t ctrl
, const unsigned char *line
)
1361 if (!ctrl
|| !ctrl
->server_local
1362 || !ctrl
->server_local
->allow_pinentry_notify
)
1364 return assuan_inquire (ctrl
->server_local
->assuan_ctx
, line
, NULL
, NULL
, 0);