1 /* server.c - Server mode and main entry point
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007, 2008 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
73 /* Note that it is sufficient to allocate the target string D as
74 long as the source string S, i.e.: strlen(s)+1; */
76 strcpy_escaped_plus (char *d
, const char *s
)
80 if (*s
== '%' && s
[1] && s
[2])
96 Blanks after the options are also removed. */
98 skip_options (const char *line
)
100 while (spacep (line
))
102 while ( *line
== '-' && line
[1] == '-' )
104 while (*line
&& !spacep (line
))
106 while (spacep (line
))
113 /* Check whether the option NAME appears in LINE */
115 has_option (const char *line
, const char *name
)
118 int n
= strlen (name
);
120 s
= strstr (line
, name
);
121 if (s
&& s
>= skip_options (line
))
123 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
127 /* A write handler used by es_fopencookie to write assuan data
130 data_line_cookie_write (void *cookie
, const void *buffer
, size_t size
)
132 assuan_context_t ctx
= cookie
;
134 if (assuan_send_data (ctx
, buffer
, size
))
144 data_line_cookie_close (void *cookie
)
146 assuan_context_t ctx
= cookie
;
148 if (assuan_send_data (ctx
, NULL
, 0))
159 close_message_fd (ctrl_t ctrl
)
161 if (ctrl
->server_local
->message_fd
!= -1)
163 close (ctrl
->server_local
->message_fd
);
164 ctrl
->server_local
->message_fd
= -1;
169 /* Start a new audit session if this has been enabled. */
171 start_audit_session (ctrl_t ctrl
)
173 audit_release (ctrl
->audit
);
175 if (ctrl
->server_local
->enable_audit_log
&& !(ctrl
->audit
= audit_new ()) )
176 return gpg_error_from_syserror ();
183 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
185 ctrl_t ctrl
= assuan_get_pointer (ctx
);
188 if (!strcmp (key
, "putenv"))
190 /* Change the session's environment to be used for the
191 Pinentry. Valid values are:
192 <NAME> Delete envvar NAME
193 <KEY>= Set envvar NAME to the empty string
194 <KEY>=<VALUE> Set envvar NAME to VALUE
196 err
= session_env_putenv (opt
.session_env
, value
);
198 else if (!strcmp (key
, "display"))
200 err
= session_env_setenv (opt
.session_env
, "DISPLAY", value
);
202 else if (!strcmp (key
, "ttyname"))
204 err
= session_env_setenv (opt
.session_env
, "GPG_TTY", value
);
206 else if (!strcmp (key
, "ttytype"))
208 err
= session_env_setenv (opt
.session_env
, "TERM", value
);
210 else if (!strcmp (key
, "lc-ctype"))
212 xfree (opt
.lc_ctype
);
213 opt
.lc_ctype
= xtrystrdup (value
);
215 err
= gpg_error_from_syserror ();
217 else if (!strcmp (key
, "lc-messages"))
219 xfree (opt
.lc_messages
);
220 opt
.lc_messages
= xtrystrdup (value
);
221 if (!opt
.lc_messages
)
222 err
= gpg_error_from_syserror ();
224 else if (!strcmp (key
, "xauthority"))
226 err
= session_env_setenv (opt
.session_env
, "XAUTHORITY", value
);
228 else if (!strcmp (key
, "pinentry-user-data"))
230 err
= session_env_setenv (opt
.session_env
, "PINENTRY_USER_DATA", value
);
232 else if (!strcmp (key
, "include-certs"))
234 int i
= *value
? atoi (value
) : -1;
235 if (ctrl
->include_certs
< -2)
236 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
238 ctrl
->include_certs
= i
;
240 else if (!strcmp (key
, "list-mode"))
242 int i
= *value
? atoi (value
) : 0;
243 if (!i
|| i
== 1) /* default and mode 1 */
245 ctrl
->server_local
->list_internal
= 1;
246 ctrl
->server_local
->list_external
= 0;
250 ctrl
->server_local
->list_internal
= 0;
251 ctrl
->server_local
->list_external
= 1;
255 ctrl
->server_local
->list_internal
= 1;
256 ctrl
->server_local
->list_external
= 1;
259 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
261 else if (!strcmp (key
, "list-to-output"))
263 int i
= *value
? atoi (value
) : 0;
264 ctrl
->server_local
->list_to_output
= i
;
266 else if (!strcmp (key
, "with-validation"))
268 int i
= *value
? atoi (value
) : 0;
269 ctrl
->with_validation
= i
;
271 else if (!strcmp (key
, "validation-model"))
273 int i
= gpgsm_parse_validation_model (value
);
274 if ( i
>= 0 && i
<= 1 )
275 ctrl
->validation_model
= i
;
277 err
= gpg_error (GPG_ERR_ASS_PARAMETER
);
279 else if (!strcmp (key
, "with-key-data"))
281 opt
.with_key_data
= 1;
283 else if (!strcmp (key
, "enable-audit-log"))
285 int i
= *value
? atoi (value
) : 0;
286 ctrl
->server_local
->enable_audit_log
= i
;
288 else if (!strcmp (key
, "allow-pinentry-notify"))
290 ctrl
->server_local
->allow_pinentry_notify
= 1;
292 else if (!strcmp (key
, "with-ephemeral-keys"))
294 int i
= *value
? atoi (value
) : 0;
295 ctrl
->with_ephemeral_keys
= i
;
297 else if (!strcmp (key
, "no-encrypt-to"))
299 ctrl
->server_local
->no_encrypt_to
= 1;
302 err
= gpg_error (GPG_ERR_UNKNOWN_OPTION
);
309 reset_notify (assuan_context_t ctx
)
311 ctrl_t ctrl
= assuan_get_pointer (ctx
);
313 gpgsm_release_certlist (ctrl
->server_local
->recplist
);
314 gpgsm_release_certlist (ctrl
->server_local
->signerlist
);
315 ctrl
->server_local
->recplist
= NULL
;
316 ctrl
->server_local
->signerlist
= NULL
;
317 close_message_fd (ctrl
);
318 assuan_close_input_fd (ctx
);
319 assuan_close_output_fd (ctx
);
324 input_notify (assuan_context_t ctx
, const char *line
)
326 ctrl_t ctrl
= assuan_get_pointer (ctx
);
328 ctrl
->autodetect_encoding
= 0;
331 if (strstr (line
, "--armor"))
333 else if (strstr (line
, "--base64"))
335 else if (strstr (line
, "--binary"))
338 ctrl
->autodetect_encoding
= 1;
342 output_notify (assuan_context_t ctx
, const char *line
)
344 ctrl_t ctrl
= assuan_get_pointer (ctx
);
346 ctrl
->create_pem
= 0;
347 ctrl
->create_base64
= 0;
348 if (strstr (line
, "--armor"))
349 ctrl
->create_pem
= 1;
350 else if (strstr (line
, "--base64"))
351 ctrl
->create_base64
= 1; /* just the raw output */
356 /* RECIPIENT <userID>
358 Set the recipient for the encryption. <userID> should be the
359 internal representation of the key; the server may accept any other
360 way of specification [we will support this]. If this is a valid and
361 trusted recipient the server does respond with OK, otherwise the
362 return is an ERR with the reason why the recipient can't be used,
363 the encryption will then not be done for this recipient. If the
364 policy is not to encrypt at all if not all recipients are valid, the
365 client has to take care of this. All RECIPIENT commands are
366 cumulative until a RESET or an successful ENCRYPT command. */
368 cmd_recipient (assuan_context_t ctx
, char *line
)
370 ctrl_t ctrl
= assuan_get_pointer (ctx
);
374 rc
= start_audit_session (ctrl
);
379 rc
= gpgsm_add_to_certlist (ctrl
, line
, 0,
380 &ctrl
->server_local
->recplist
, 0);
383 gpg_err_code_t r
= gpg_err_code (rc
);
384 gpgsm_status2 (ctrl
, STATUS_INV_RECP
,
386 r
== GPG_ERR_NO_PUBKEY
? "1":
387 r
== GPG_ERR_AMBIGUOUS_NAME
? "2":
388 r
== GPG_ERR_WRONG_KEY_USAGE
? "3":
389 r
== GPG_ERR_CERT_REVOKED
? "4":
390 r
== GPG_ERR_CERT_EXPIRED
? "5":
391 r
== GPG_ERR_NO_CRL_KNOWN
? "6":
392 r
== GPG_ERR_CRL_TOO_OLD
? "7":
393 r
== GPG_ERR_NO_POLICY_MATCH
? "8":
394 r
== GPG_ERR_MISSING_CERT
? "11":
404 Set the signer's keys for the signature creation. <userID> should
405 be the internal representation of the key; the server may accept any
406 other way of specification [we will support this]. If this is a
407 valid and usable signing key the server does respond with OK,
408 otherwise it returns an ERR with the reason why the key can't be
409 used, the signing will then not be done for this key. If the policy
410 is not to sign at all if not all signer keys are valid, the client
411 has to take care of this. All SIGNER commands are cumulative until
412 a RESET but they are *not* reset by an SIGN command becuase it can
413 be expected that set of signers are used for more than one sign
416 Note that this command returns an INV_RECP status which is a bit
417 strange, but they are very similar. */
419 cmd_signer (assuan_context_t ctx
, char *line
)
421 ctrl_t ctrl
= assuan_get_pointer (ctx
);
424 rc
= gpgsm_add_to_certlist (ctrl
, line
, 1,
425 &ctrl
->server_local
->signerlist
, 0);
428 gpg_err_code_t r
= gpg_err_code (rc
);
429 gpgsm_status2 (ctrl
, STATUS_INV_RECP
,
431 r
== GPG_ERR_NO_PUBKEY
? "1":
432 r
== GPG_ERR_AMBIGUOUS_NAME
? "2":
433 r
== GPG_ERR_WRONG_KEY_USAGE
? "3":
434 r
== GPG_ERR_CERT_REVOKED
? "4":
435 r
== GPG_ERR_CERT_EXPIRED
? "5":
436 r
== GPG_ERR_NO_CRL_KNOWN
? "6":
437 r
== GPG_ERR_CRL_TOO_OLD
? "7":
438 r
== GPG_ERR_NO_POLICY_MATCH
? "8":
439 r
== GPG_ERR_NO_SECKEY
? "9":
440 r
== GPG_ERR_MISSING_CERT
? "11":
450 Do the actual encryption process. Takes the plaintext from the INPUT
451 command, writes to the ciphertext to the file descriptor set with
452 the OUTPUT command, take the recipients form all the recipients set
453 so far. If this command fails the clients should try to delete all
454 output currently done or otherwise mark it as invalid. GPGSM does
455 ensure that there won't be any security problem with leftover data
456 on the output in this case.
458 This command should in general not fail, as all necessary checks
459 have been done while setting the recipients. The input and output
462 cmd_encrypt (assuan_context_t ctx
, char *line
)
464 ctrl_t ctrl
= assuan_get_pointer (ctx
);
472 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
474 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
475 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
477 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
479 out_fp
= fdopen (dup (out_fd
), "w");
481 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
483 /* Now add all encrypt-to marked recipients from the default
486 if (!opt
.no_encrypt_to
&& !ctrl
->server_local
->no_encrypt_to
)
488 for (cl
=ctrl
->server_local
->default_recplist
; !rc
&& cl
; cl
= cl
->next
)
489 if (cl
->is_encrypt_to
)
490 rc
= gpgsm_add_cert_to_certlist (ctrl
, cl
->cert
,
491 &ctrl
->server_local
->recplist
, 1);
494 rc
= ctrl
->audit
? 0 : start_audit_session (ctrl
);
496 rc
= gpgsm_encrypt (assuan_get_pointer (ctx
),
497 ctrl
->server_local
->recplist
,
501 gpgsm_release_certlist (ctrl
->server_local
->recplist
);
502 ctrl
->server_local
->recplist
= NULL
;
503 /* Close and reset the fd */
504 close_message_fd (ctrl
);
505 assuan_close_input_fd (ctx
);
506 assuan_close_output_fd (ctx
);
513 This performs the decrypt operation after doing some check on the
514 internal state. (e.g. that only needed data has been set). Because
515 it utilizes the GPG-Agent for the session key decryption, there is
516 no need to ask the client for a protecting passphrase - GpgAgent
517 does take care of this by requesting this from the user. */
519 cmd_decrypt (assuan_context_t ctx
, char *line
)
521 ctrl_t ctrl
= assuan_get_pointer (ctx
);
528 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
530 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
531 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
533 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
535 out_fp
= fdopen (dup(out_fd
), "w");
537 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
539 rc
= start_audit_session (ctrl
);
541 rc
= gpgsm_decrypt (ctrl
, inp_fd
, out_fp
);
544 /* close and reset the fd */
545 close_message_fd (ctrl
);
546 assuan_close_input_fd (ctx
);
547 assuan_close_output_fd (ctx
);
555 This does a verify operation on the message send to the input-FD.
556 The result is written out using status lines. If an output FD was
557 given, the signed text will be written to that.
559 If the signature is a detached one, the server will inquire about
560 the signed material and the client must provide it.
563 cmd_verify (assuan_context_t ctx
, char *line
)
566 ctrl_t ctrl
= assuan_get_pointer (ctx
);
567 int fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
568 int out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
574 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
578 out_fp
= fdopen ( dup(out_fd
), "w");
580 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
583 rc
= start_audit_session (ctrl
);
585 rc
= gpgsm_verify (assuan_get_pointer (ctx
), fd
,
586 ctrl
->server_local
->message_fd
, out_fp
);
590 /* close and reset the fd */
591 close_message_fd (ctrl
);
592 assuan_close_input_fd (ctx
);
593 assuan_close_output_fd (ctx
);
601 Sign the data set with the INPUT command and write it to the sink
602 set by OUTPUT. With "--detached" specified, a detached signature is
603 created (surprise). */
605 cmd_sign (assuan_context_t ctx
, char *line
)
607 ctrl_t ctrl
= assuan_get_pointer (ctx
);
613 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
615 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
616 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
618 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
620 detached
= has_option (line
, "--detached");
622 out_fp
= fdopen ( dup(out_fd
), "w");
624 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
626 rc
= start_audit_session (ctrl
);
628 rc
= gpgsm_sign (assuan_get_pointer (ctx
), ctrl
->server_local
->signerlist
,
629 inp_fd
, detached
, out_fp
);
632 /* close and reset the fd */
633 close_message_fd (ctrl
);
634 assuan_close_input_fd (ctx
);
635 assuan_close_output_fd (ctx
);
643 Import the certificates read form the input-fd, return status
644 message for each imported one. The import checks the validity of
645 the certificate but not of the entire chain. It is possible to
646 import expired certificates. */
648 cmd_import (assuan_context_t ctx
, char *line
)
650 ctrl_t ctrl
= assuan_get_pointer (ctx
);
652 int fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
657 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
659 rc
= gpgsm_import (assuan_get_pointer (ctx
), fd
);
661 /* close and reset the fd */
662 close_message_fd (ctrl
);
663 assuan_close_input_fd (ctx
);
664 assuan_close_output_fd (ctx
);
670 /* EXPORT [--data [--armor|--base64]] [--] pattern
675 cmd_export (assuan_context_t ctx
, char *line
)
677 ctrl_t ctrl
= assuan_get_pointer (ctx
);
682 use_data
= has_option (line
, "--data");
686 /* We need to override any possible setting done by an OUTPUT command. */
687 ctrl
->create_pem
= has_option (line
, "--armor");
688 ctrl
->create_base64
= has_option (line
, "--base64");
691 line
= skip_options (line
);
693 /* Break the line down into an strlist_t. */
695 for (p
=line
; *p
; line
= p
)
697 while (*p
&& *p
!= ' ')
703 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
707 return out_of_core ();
710 strcpy_escaped_plus (sl
->d
, line
);
720 stream
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
724 return set_error (GPG_ERR_ASS_GENERAL
,
725 "error setting up a data stream");
727 gpgsm_export (ctrl
, list
, NULL
, stream
);
732 int fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
738 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
740 out_fp
= fdopen ( dup(fd
), "w");
744 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
747 gpgsm_export (ctrl
, list
, out_fp
, NULL
);
752 /* Close and reset the fds. */
753 close_message_fd (ctrl
);
754 assuan_close_input_fd (ctx
);
755 assuan_close_output_fd (ctx
);
761 cmd_delkeys (assuan_context_t ctx
, char *line
)
763 ctrl_t ctrl
= assuan_get_pointer (ctx
);
768 /* break the line down into an strlist_t */
770 for (p
=line
; *p
; line
= p
)
772 while (*p
&& *p
!= ' ')
778 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
782 return out_of_core ();
785 strcpy_escaped_plus (sl
->d
, line
);
791 rc
= gpgsm_delete (ctrl
, list
);
794 /* close and reset the fd */
795 close_message_fd (ctrl
);
796 assuan_close_input_fd (ctx
);
797 assuan_close_output_fd (ctx
);
806 Set the file descriptor to read a message which is used with
807 detached signatures */
809 cmd_message (assuan_context_t ctx
, char *line
)
814 ctrl_t ctrl
= assuan_get_pointer (ctx
);
816 rc
= assuan_command_parse_fd (ctx
, line
, &sysfd
);
819 fd
= translate_sys2libc_fd (sysfd
, 0);
821 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
822 ctrl
->server_local
->message_fd
= fd
;
826 /* LISTKEYS [<patterns>]
827 DUMPKEYS [<patterns>]
828 LISTSECRETKEYS [<patterns>]
829 DUMPSECRETKEYS [<patterns>]
832 do_listkeys (assuan_context_t ctx
, char *line
, int mode
)
834 ctrl_t ctrl
= assuan_get_pointer (ctx
);
838 unsigned int listmode
;
841 /* Break the line down into an strlist. */
843 for (p
=line
; *p
; line
= p
)
845 while (*p
&& *p
!= ' ')
851 sl
= xtrymalloc (sizeof *sl
+ strlen (line
));
855 return out_of_core ();
858 strcpy_escaped_plus (sl
->d
, line
);
864 if (ctrl
->server_local
->list_to_output
)
866 int outfd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
869 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
870 fp
= es_fdopen ( dup (outfd
), "w");
872 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen() failed");
876 fp
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
878 return set_error (GPG_ERR_ASS_GENERAL
,
879 "error setting up a data stream");
882 ctrl
->with_colons
= 1;
884 if (ctrl
->server_local
->list_internal
)
886 if (ctrl
->server_local
->list_external
)
888 err
= gpgsm_list_keys (assuan_get_pointer (ctx
), list
, fp
, listmode
);
891 if (ctrl
->server_local
->list_to_output
)
892 assuan_close_output_fd (ctx
);
897 cmd_listkeys (assuan_context_t ctx
, char *line
)
899 return do_listkeys (ctx
, line
, 3);
903 cmd_dumpkeys (assuan_context_t ctx
, char *line
)
905 return do_listkeys (ctx
, line
, 259);
909 cmd_listsecretkeys (assuan_context_t ctx
, char *line
)
911 return do_listkeys (ctx
, line
, 2);
915 cmd_dumpsecretkeys (assuan_context_t ctx
, char *line
)
917 return do_listkeys (ctx
, line
, 258);
923 Read the parameters in native format from the input fd and write a
924 certificate request to the output.
927 cmd_genkey (assuan_context_t ctx
, char *line
)
929 ctrl_t ctrl
= assuan_get_pointer (ctx
);
937 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
939 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
940 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
942 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
944 in_stream
= es_fdopen_nc (inp_fd
, "r");
946 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen failed");
948 out_fp
= fdopen ( dup(out_fd
), "w");
951 es_fclose (in_stream
);
952 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
954 rc
= gpgsm_genkey (ctrl
, in_stream
, out_fp
);
957 /* close and reset the fds */
958 assuan_close_input_fd (ctx
);
959 assuan_close_output_fd (ctx
);
966 /* GETAUDITLOG [--data] [--html]
968 !!!WORK in PROGRESS!!!
970 If --data is used, the output is send using D-lines and not to the
971 source given by an OUTPUT command.
973 If --html is used the output is formated as an XHTML block. This is
974 designed to be incorporated into a HTML document.
977 cmd_getauditlog (assuan_context_t ctx
, char *line
)
979 ctrl_t ctrl
= assuan_get_pointer (ctx
);
981 estream_t out_stream
;
982 int opt_data
, opt_html
;
985 opt_data
= has_option (line
, "--data");
986 opt_html
= has_option (line
, "--html");
987 line
= skip_options (line
);
990 return gpg_error (GPG_ERR_NO_DATA
);
994 out_stream
= es_fopencookie (ctx
, "w", data_line_cookie_functions
);
996 return set_error (GPG_ERR_ASS_GENERAL
,
997 "error setting up a data stream");
1001 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
1003 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
1005 out_stream
= es_fdopen_nc ( dup (out_fd
), "w");
1008 return set_error (GPG_ERR_ASS_GENERAL
, "es_fdopen() failed");
1012 audit_print_result (ctrl
->audit
, out_stream
, opt_html
);
1015 es_fclose (out_stream
);
1017 /* Close and reset the fd. */
1019 assuan_close_output_fd (ctx
);
1026 Multipurpose function to return a variety of information.
1027 Supported values for WHAT are:
1029 version - Return the version of the program.
1030 pid - Return the process id of the server.
1031 agent-check - Return success if the agent is running.
1035 cmd_getinfo (assuan_context_t ctx
, char *line
)
1039 if (!strcmp (line
, "version"))
1041 const char *s
= VERSION
;
1042 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1044 else if (!strcmp (line
, "pid"))
1048 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
1049 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
1051 else if (!strcmp (line
, "agent-check"))
1053 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1054 rc
= gpgsm_agent_send_nop (ctrl
);
1057 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
1063 /* Tell the assuan library about our commands */
1065 register_commands (assuan_context_t ctx
)
1069 int (*handler
)(assuan_context_t
, char *line
);
1071 { "RECIPIENT", cmd_recipient
},
1072 { "SIGNER", cmd_signer
},
1073 { "ENCRYPT", cmd_encrypt
},
1074 { "DECRYPT", cmd_decrypt
},
1075 { "VERIFY", cmd_verify
},
1076 { "SIGN", cmd_sign
},
1077 { "IMPORT", cmd_import
},
1078 { "EXPORT", cmd_export
},
1081 { "MESSAGE", cmd_message
},
1082 { "LISTKEYS", cmd_listkeys
},
1083 { "DUMPKEYS", cmd_dumpkeys
},
1084 { "LISTSECRETKEYS",cmd_listsecretkeys
},
1085 { "DUMPSECRETKEYS",cmd_dumpsecretkeys
},
1086 { "GENKEY", cmd_genkey
},
1087 { "DELKEYS", cmd_delkeys
},
1088 { "GETAUDITLOG", cmd_getauditlog
},
1089 { "GETINFO", cmd_getinfo
},
1094 for (i
=0; table
[i
].name
; i
++)
1096 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
1103 /* Startup the server. DEFAULT_RECPLIST is the list of recipients as
1104 set from the command line or config file. We only require those
1105 marked as encrypt-to. */
1107 gpgsm_server (certlist_t default_recplist
)
1111 assuan_context_t ctx
;
1112 struct server_control_s ctrl
;
1113 static const char hello
[] = ("GNU Privacy Guard's S/M server "
1116 memset (&ctrl
, 0, sizeof ctrl
);
1117 gpgsm_init_default_ctrl (&ctrl
);
1119 /* We use a pipe based server so that we can work from scripts.
1120 assuan_init_pipe_server will automagically detect when we are
1121 called with a socketpair and ignore FIELDES in this case. */
1124 rc
= assuan_init_pipe_server (&ctx
, filedes
);
1127 log_error ("failed to initialize the server: %s\n",
1131 rc
= register_commands (ctx
);
1134 log_error ("failed to the register commands with Assuan: %s\n",
1138 if (opt
.verbose
|| opt
.debug
)
1141 const char *s1
= getenv ("GPG_AGENT_INFO");
1142 const char *s2
= getenv ("DIRMNGR_INFO");
1151 opt
.config_filename
,
1156 assuan_set_hello_line (ctx
, tmp
);
1161 assuan_set_hello_line (ctx
, hello
);
1163 assuan_register_reset_notify (ctx
, reset_notify
);
1164 assuan_register_input_notify (ctx
, input_notify
);
1165 assuan_register_output_notify (ctx
, output_notify
);
1166 assuan_register_option_handler (ctx
, option_handler
);
1168 assuan_set_pointer (ctx
, &ctrl
);
1169 ctrl
.server_local
= xcalloc (1, sizeof *ctrl
.server_local
);
1170 ctrl
.server_local
->assuan_ctx
= ctx
;
1171 ctrl
.server_local
->message_fd
= -1;
1172 ctrl
.server_local
->list_internal
= 1;
1173 ctrl
.server_local
->list_external
= 0;
1174 ctrl
.server_local
->default_recplist
= default_recplist
;
1177 assuan_set_log_stream (ctx
, log_get_stream ());
1181 rc
= assuan_accept (ctx
);
1188 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
1192 rc
= assuan_process (ctx
);
1195 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
1200 gpgsm_release_certlist (ctrl
.server_local
->recplist
);
1201 ctrl
.server_local
->recplist
= NULL
;
1202 gpgsm_release_certlist (ctrl
.server_local
->signerlist
);
1203 ctrl
.server_local
->signerlist
= NULL
;
1204 xfree (ctrl
.server_local
);
1206 audit_release (ctrl
.audit
);
1209 assuan_deinit_server (ctx
);
1215 gpgsm_status2 (ctrl_t ctrl
, int no
, ...)
1217 gpg_error_t err
= 0;
1221 va_start (arg_ptr
, no
);
1223 if (ctrl
->no_server
&& ctrl
->status_fd
== -1)
1224 ; /* No status wanted. */
1225 else if (ctrl
->no_server
)
1229 if (ctrl
->status_fd
== 1)
1231 else if (ctrl
->status_fd
== 2)
1234 statusfp
= fdopen (ctrl
->status_fd
, "w");
1238 log_fatal ("can't open fd %d for status output: %s\n",
1239 ctrl
->status_fd
, strerror(errno
));
1243 fputs ("[GNUPG:] ", statusfp
);
1244 fputs (get_status_string (no
), statusfp
);
1246 while ( (text
= va_arg (arg_ptr
, const char*) ))
1248 putc ( ' ', statusfp
);
1249 for (; *text
; text
++)
1252 fputs ( "\\n", statusfp
);
1253 else if (*text
== '\r')
1254 fputs ( "\\r", statusfp
);
1256 putc ( *(const byte
*)text
, statusfp
);
1259 putc ('\n', statusfp
);
1264 assuan_context_t ctx
= ctrl
->server_local
->assuan_ctx
;
1270 while ( (text
= va_arg (arg_ptr
, const char *)) )
1277 for ( ; *text
&& n
< DIM (buf
)-2; n
++)
1281 err
= assuan_write_status (ctx
, get_status_string (no
), buf
);
1289 gpgsm_status (ctrl_t ctrl
, int no
, const char *text
)
1291 return gpgsm_status2 (ctrl
, no
, text
, NULL
);
1295 gpgsm_status_with_err_code (ctrl_t ctrl
, int no
, const char *text
,
1300 sprintf (buf
, "%u", (unsigned int)ec
);
1302 return gpgsm_status2 (ctrl
, no
, text
, buf
, NULL
);
1304 return gpgsm_status2 (ctrl
, no
, buf
, NULL
);
1308 /* Helper to notify the client about Pinentry events. Because that
1309 might disturb some older clients, this is only done when enabled
1310 via an option. Returns an gpg error code. */
1312 gpgsm_proxy_pinentry_notify (ctrl_t ctrl
, const unsigned char *line
)
1314 if (!ctrl
|| !ctrl
->server_local
1315 || !ctrl
->server_local
->allow_pinentry_notify
)
1317 return assuan_inquire (ctrl
->server_local
->assuan_ctx
, line
, NULL
, NULL
, 0);