1 /* server.c - server mode for gpg
2 * Copyright (C) 2006, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
35 #include "../common/sysutils.h"
39 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
42 /* Data used to associate an Assuan context with local server data. */
45 /* Our current Assuan context. */
46 assuan_context_t assuan_ctx
;
47 /* File descriptor as set by the MESSAGE command. */
48 gnupg_fd_t message_fd
;
50 /* List of prepared recipients. */
57 /* Helper to close the message fd if it is open. */
59 close_message_fd (ctrl_t ctrl
)
61 if (ctrl
->server_local
->message_fd
!= GNUPG_INVALID_FD
)
63 assuan_sock_close (ctrl
->server_local
->message_fd
);
64 ctrl
->server_local
->message_fd
= GNUPG_INVALID_FD
;
69 /* Skip over options. Blanks after the options are also removed. */
71 skip_options (const char *line
)
75 while ( *line
== '-' && line
[1] == '-' )
77 while (*line
&& !spacep (line
))
86 /* Check whether the option NAME appears in LINE. */
88 has_option (const char *line
, const char *name
)
91 int n
= strlen (name
);
93 s
= strstr (line
, name
);
94 if (s
&& s
>= skip_options (line
))
96 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
103 /* Called by libassuan for Assuan options. See the Assuan manual for
106 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
108 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
113 /* Fixme: Implement the tty and locale args. */
114 if (!strcmp (key
, "display"))
117 else if (!strcmp (key
, "ttyname"))
120 else if (!strcmp (key
, "ttytype"))
123 else if (!strcmp (key
, "lc-ctype"))
126 else if (!strcmp (key
, "lc-messages"))
129 else if (!strcmp (key
, "xauthority"))
132 else if (!strcmp (key
, "pinentry_user_data"))
135 else if (!strcmp (key
, "list-mode"))
137 /* This is for now a dummy option. */
140 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
146 /* Called by libassuan for RESET commands. */
148 reset_notify (assuan_context_t ctx
, char *line
)
150 ctrl_t ctrl
= assuan_get_pointer (ctx
);
154 release_pk_list (ctrl
->server_local
->recplist
);
155 ctrl
->server_local
->recplist
= NULL
;
157 close_message_fd (ctrl
);
158 assuan_close_input_fd (ctx
);
159 assuan_close_output_fd (ctx
);
164 /* Called by libassuan for INPUT commands. */
166 input_notify (assuan_context_t ctx
, char *line
)
168 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
172 if (strstr (line
, "--armor"))
174 else if (strstr (line
, "--base64"))
176 else if (strstr (line
, "--binary"))
180 /* FIXME (autodetect encoding) */
186 /* Called by libassuan for OUTPUT commands. */
188 output_notify (assuan_context_t ctx
, char *line
)
190 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
194 if (strstr (line
, "--armor"))
196 else if (strstr (line
, "--base64"))
206 /* RECIPIENT [--hidden] <userID>
208 Set the recipient for the encryption. <userID> should be the
209 internal representation of the key; the server may accept any other
210 way of specification. If this is a valid and trusted recipient the
211 server does respond with OK, otherwise the return is an ERR with
212 the reason why the recipient can't be used, the encryption will
213 then not be done for this recipient. If the policy is not to
214 encrypt at all if not all recipients are valid, the client has to
215 take care of this. All RECIPIENT commands are cumulative until a
216 RESET or an successful ENCRYPT command. */
218 cmd_recipient (assuan_context_t ctx
, char *line
)
220 ctrl_t ctrl
= assuan_get_pointer (ctx
);
224 hidden
= has_option (line
,"--hidden");
225 line
= skip_options (line
);
227 /* FIXME: Expand groups
229 remusr = expand_group (rcpts);
234 err
= find_and_check_key (line
, PUBKEY_USAGE_ENC
, hidden
,
235 &ctrl
->server_local
->recplist
);
238 log_error ("command '%s' failed: %s\n", "RECIPIENT", gpg_strerror (err
));
246 Set the signer's keys for the signature creation. <userID> should
247 be the internal representation of the key; the server may accept
248 any other way of specification. If this is a valid and usable
249 signing key the server does respond with OK, otherwise it returns
250 an ERR with the reason why the key can't be used, the signing will
251 then not be done for this key. If the policy is not to sign at all
252 if not all signer keys are valid, the client has to take care of
253 this. All SIGNER commands are cumulative until a RESET but they
254 are *not* reset by an SIGN command becuase it can be expected that
255 set of signers are used for more than one sign operation.
257 Note that this command returns an INV_RECP status which is a bit
258 strange, but they are very similar. */
260 cmd_signer (assuan_context_t ctx
, char *line
)
264 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
271 Do the actual encryption process. Takes the plaintext from the
272 INPUT command, writes the ciphertext to the file descriptor set
273 with the OUTPUT command, take the recipients from all the
274 recipients set so far with RECIPIENTS.
276 If this command fails the clients should try to delete all output
277 currently done or otherwise mark it as invalid. GPG does ensure
278 that there won't be any security problem with leftover data on the
281 In most cases this command won't fail because most necessary checks
282 have been done while setting the recipients. However some checks
283 can only be done right here and thus error may occur anyway (for
284 example, no recipients at all).
286 The input, output and message pipes are closed after this
289 cmd_encrypt (assuan_context_t ctx
, char *line
)
291 ctrl_t ctrl
= assuan_get_pointer (ctx
);
295 (void)line
; /* LINE is not used. */
297 if ( !ctrl
->server_local
->recplist
)
299 write_status_text (STATUS_NO_RECP
, "0");
300 err
= gpg_error (GPG_ERR_NO_USER_ID
);
304 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
307 err
= set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
310 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
313 err
= set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
317 /* Fixme: Check that we are using real files and not pipes if in
318 PGP-2 mode. Do all the other checks we do in gpg.c for aEncr.
319 Maybe we should drop the PGP2 compatibility. */
322 /* FIXME: GPGSM does this here: Add all encrypt-to marked recipients
323 from the default list. */
325 /* fixme: err = ctrl->audit? 0 : start_audit_session (ctrl);*/
327 err
= encrypt_crypt (inp_fd
, NULL
, NULL
, 0,
328 ctrl
->server_local
->recplist
,
332 /* Release the recipient list on success. */
335 release_pk_list (ctrl
->server_local
->recplist
);
336 ctrl
->server_local
->recplist
= NULL
;
339 /* Close and reset the fds. */
340 close_message_fd (ctrl
);
341 assuan_close_input_fd (ctx
);
342 assuan_close_output_fd (ctx
);
345 log_error ("command '%s' failed: %s\n", "ENCRYPT", gpg_strerror (err
));
353 This performs the decrypt operation. */
355 cmd_decrypt (assuan_context_t ctx
, char *line
)
357 ctrl_t ctrl
= assuan_get_pointer (ctx
);
361 (void)line
; /* LINE is not used. */
363 inp_fd
= translate_sys2libc_fd (assuan_get_input_fd (ctx
), 0);
365 return set_error (GPG_ERR_ASS_NO_INPUT
, NULL
);
366 out_fd
= translate_sys2libc_fd (assuan_get_output_fd (ctx
), 1);
368 return set_error (GPG_ERR_ASS_NO_OUTPUT
, NULL
);
370 glo_ctrl
.lasterr
= 0;
371 err
= decrypt_message_fd (inp_fd
, out_fd
);
373 err
= glo_ctrl
.lasterr
;
375 /* Close and reset the fds. */
376 close_message_fd (ctrl
);
377 assuan_close_input_fd (ctx
);
378 assuan_close_output_fd (ctx
);
381 log_error ("command '%s' failed: %s\n", "DECRYPT", gpg_strerror (err
));
389 This does a verify operation on the message send to the input-FD.
390 The result is written out using status lines. If an output FD was
391 given, the signed text will be written to that.
393 If the signature is a detached one, the server will inquire about
394 the signed material and the client must provide it.
397 cmd_verify (assuan_context_t ctx
, char *line
)
400 ctrl_t ctrl
= assuan_get_pointer (ctx
);
401 gnupg_fd_t fd
= assuan_get_input_fd (ctx
);
402 gnupg_fd_t out_fd
= assuan_get_output_fd (ctx
);
405 /* FIXME: Revamp this code it is nearly to 3 years old and was only
406 intended as a quick test. */
410 if (fd
== GNUPG_INVALID_FD
)
411 return gpg_error (GPG_ERR_ASS_NO_INPUT
);
413 if (out_fd
!= GNUPG_INVALID_FD
)
415 out_fp
= fdopen ( dup (FD2INT (out_fd
)), "w");
417 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
420 log_debug ("WARNING: The server mode is WORK "
421 "iN PROGRESS and not ready for use\n");
423 /* Need to dup it because it might get closed and libassuan won't
424 know about it then. */
425 rc
= gpg_verify (ctrl
,
427 dup ( FD2INT (ctrl
->server_local
->message_fd
)),
432 close_message_fd (ctrl
);
433 assuan_close_input_fd (ctx
);
434 assuan_close_output_fd (ctx
);
437 log_error ("command '%s' failed: %s\n", "VERIFY", gpg_strerror (rc
));
445 Sign the data set with the INPUT command and write it to the sink
446 set by OUTPUT. With "--detached" specified, a detached signature
449 cmd_sign (assuan_context_t ctx
, char *line
)
453 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
460 Import keys as read from the input-fd, return status message for
461 each imported one. The import checks the validity of the key. */
463 cmd_import (assuan_context_t ctx
, char *line
)
467 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
472 /* EXPORT [--data [--armor|--base64]] [--] pattern
474 Similar to the --export command line command, this command exports
475 public keys matching PATTERN. The output is send to the output fd
476 unless the --data option has been used in which case the output
477 gets send inline using regular data lines. The options "--armor"
478 and "--base" ospecify an output format if "--data" has been used.
479 Recall that in general the output format is set with the OUTPUT
483 cmd_export (assuan_context_t ctx
, char *line
)
487 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
497 cmd_delkeys (assuan_context_t ctx
, char *line
)
501 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
508 Set the file descriptor to read a message which is used with
509 detached signatures. */
511 cmd_message (assuan_context_t ctx
, char *line
)
515 ctrl_t ctrl
= assuan_get_pointer (ctx
);
517 rc
= assuan_command_parse_fd (ctx
, line
, &fd
);
520 if (fd
== GNUPG_INVALID_FD
)
521 return gpg_error (GPG_ERR_ASS_NO_INPUT
);
522 ctrl
->server_local
->message_fd
= fd
;
528 /* LISTKEYS [<patterns>]
529 LISTSECRETKEYS [<patterns>]
534 do_listkeys (assuan_context_t ctx
, char *line
, int mode
)
540 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
545 cmd_listkeys (assuan_context_t ctx
, char *line
)
547 return do_listkeys (ctx
, line
, 3);
552 cmd_listsecretkeys (assuan_context_t ctx
, char *line
)
554 return do_listkeys (ctx
, line
, 2);
561 Read the parameters in native format from the input fd and create a
565 cmd_genkey (assuan_context_t ctx
, char *line
)
569 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
575 Multipurpose function to return a variety of information.
576 Supported values for WHAT are:
578 version - Return the version of the program.
579 pid - Return the process id of the server.
583 cmd_getinfo (assuan_context_t ctx
, char *line
)
587 if (!strcmp (line
, "version"))
589 const char *s
= VERSION
;
590 rc
= assuan_send_data (ctx
, s
, strlen (s
));
592 else if (!strcmp (line
, "pid"))
596 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
597 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
600 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
606 /* Helper to register our commands with libassuan. */
608 register_commands (assuan_context_t ctx
)
613 assuan_handler_t handler
;
615 { "RECIPIENT", cmd_recipient
},
616 { "SIGNER", cmd_signer
},
617 { "ENCRYPT", cmd_encrypt
},
618 { "DECRYPT", cmd_decrypt
},
619 { "VERIFY", cmd_verify
},
620 { "SIGN", cmd_sign
},
621 { "IMPORT", cmd_import
},
622 { "EXPORT", cmd_export
},
625 { "MESSAGE", cmd_message
},
626 { "LISTKEYS", cmd_listkeys
},
627 { "LISTSECRETKEYS",cmd_listsecretkeys
},
628 { "GENKEY", cmd_genkey
},
629 { "DELKEYS", cmd_delkeys
},
630 { "GETINFO", cmd_getinfo
},
635 for (i
=0; table
[i
].name
; i
++)
637 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
, NULL
);
647 /* Startup the server. CTRL must have been allocated by the caller
648 and set to the default values. */
650 gpg_server (ctrl_t ctrl
)
654 assuan_context_t ctx
= NULL
;
655 static const char hello
[] = ("GNU Privacy Guard's OpenPGP server "
658 /* We use a pipe based server so that we can work from scripts.
659 assuan_init_pipe_server will automagically detect when we are
660 called with a socketpair and ignore FILEDES in this case. */
661 filedes
[0] = assuan_fdopen (0);
662 filedes
[1] = assuan_fdopen (1);
663 rc
= assuan_new (&ctx
);
666 log_error ("failed to allocate the assuan context: %s\n",
671 rc
= assuan_init_pipe_server (ctx
, filedes
);
674 log_error ("failed to initialize the server: %s\n", gpg_strerror (rc
));
678 rc
= register_commands (ctx
);
681 log_error ("failed to the register commands with Assuan: %s\n",
686 assuan_set_pointer (ctx
, ctrl
);
687 if (opt
.verbose
|| opt
.debug
)
690 const char *s1
= getenv ("GPG_AGENT_INFO");
692 tmp
= xtryasprintf ("Home: %s\n"
697 "fixme: need config filename",
702 assuan_set_hello_line (ctx
, tmp
);
707 assuan_set_hello_line (ctx
, hello
);
708 assuan_register_reset_notify (ctx
, reset_notify
);
709 assuan_register_input_notify (ctx
, input_notify
);
710 assuan_register_output_notify (ctx
, output_notify
);
711 assuan_register_option_handler (ctx
, option_handler
);
713 ctrl
->server_local
= xtrycalloc (1, sizeof *ctrl
->server_local
);
714 if (!ctrl
->server_local
)
716 rc
= gpg_error_from_syserror ();
719 ctrl
->server_local
->assuan_ctx
= ctx
;
720 ctrl
->server_local
->message_fd
= GNUPG_INVALID_FD
;
723 assuan_set_log_stream (ctx
, log_get_stream ());
727 rc
= assuan_accept (ctx
);
735 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
739 rc
= assuan_process (ctx
);
742 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
748 if (ctrl
->server_local
)
750 release_pk_list (ctrl
->server_local
->recplist
);
752 xfree (ctrl
->server_local
);
753 ctrl
->server_local
= NULL
;
755 assuan_release (ctx
);