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"
38 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
41 /* Data used to associate an Assuan context with local server data. */
44 /* Our current Assuan context. */
45 assuan_context_t assuan_ctx;
46 /* File descriptor as set by the MESSAGE command. */
47 gnupg_fd_t message_fd;
52 /* Helper to close the message fd if it is open. */
54 close_message_fd (ctrl_t ctrl)
56 if (ctrl->server_local->message_fd != GNUPG_INVALID_FD)
58 assuan_sock_close (ctrl->server_local->message_fd);
59 ctrl->server_local->message_fd = GNUPG_INVALID_FD;
65 /* Called by libassuan for Assuan options. See the Assuan manual for
68 option_handler (assuan_context_t ctx, const char *key, const char *value)
70 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
75 /* Fixme: Implement the tty and locale args. */
76 if (!strcmp (key, "display"))
79 else if (!strcmp (key, "ttyname"))
82 else if (!strcmp (key, "ttytype"))
85 else if (!strcmp (key, "lc-ctype"))
88 else if (!strcmp (key, "lc-messages"))
91 else if (!strcmp (key, "xauthority"))
94 else if (!strcmp (key, "pinentry_user_data"))
97 else if (!strcmp (key, "list-mode"))
99 /* This is for now a dummy option. */
102 return gpg_error (GPG_ERR_UNKNOWN_OPTION);
108 /* Called by libassuan for RESET commands. */
110 reset_notify (assuan_context_t ctx, char *line)
112 ctrl_t ctrl = assuan_get_pointer (ctx);
116 close_message_fd (ctrl);
117 assuan_close_input_fd (ctx);
118 assuan_close_output_fd (ctx);
123 /* Called by libassuan for INPUT commands. */
125 input_notify (assuan_context_t ctx, char *line)
127 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
131 if (strstr (line, "--armor"))
133 else if (strstr (line, "--base64"))
135 else if (strstr (line, "--binary"))
139 /* FIXME (autodetect encoding) */
145 /* Called by libassuan for OUTPUT commands. */
147 output_notify (assuan_context_t ctx, char *line)
149 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
153 if (strstr (line, "--armor"))
155 else if (strstr (line, "--base64"))
165 /* RECIPIENT <userID>
167 Set the recipient for the encryption. <userID> should be the
168 internal representation of the key; the server may accept any other
169 way of specification. If this is a valid and trusted recipient the
170 server does respond with OK, otherwise the return is an ERR with
171 the reason why the recipient can't be used, the encryption will
172 then not be done for this recipient. If the policy is not to
173 encrypt at all if not all recipients are valid, the client has to
174 take care of this. All RECIPIENT commands are cumulative until a
175 RESET or an successful ENCRYPT command. */
177 cmd_recipient (assuan_context_t ctx, char *line)
181 return gpg_error (GPG_ERR_NOT_SUPPORTED);
188 Set the signer's keys for the signature creation. <userID> should
189 be the internal representation of the key; the server may accept
190 any other way of specification. If this is a valid and usable
191 signing key the server does respond with OK, otherwise it returns
192 an ERR with the reason why the key can't be used, the signing will
193 then not be done for this key. If the policy is not to sign at all
194 if not all signer keys are valid, the client has to take care of
195 this. All SIGNER commands are cumulative until a RESET but they
196 are *not* reset by an SIGN command becuase it can be expected that
197 set of signers are used for more than one sign operation.
199 Note that this command returns an INV_RECP status which is a bit
200 strange, but they are very similar. */
202 cmd_signer (assuan_context_t ctx, char *line)
206 return gpg_error (GPG_ERR_NOT_SUPPORTED);
213 Do the actual encryption process. Takes the plaintext from the
214 INPUT command, writes to the ciphertext to the file descriptor set
215 with the OUTPUT command, take the recipients form all the
216 recipients set so far. If this command fails the clients should
217 try to delete all output currently done or otherwise mark it as
218 invalid. GPG does ensure that there won't be any security problem
219 with leftover data on the output in this case.
221 This command should in general not fail, as all necessary checks
222 have been done while setting the recipients. The input and output
225 cmd_encrypt (assuan_context_t ctx, char *line)
229 return gpg_error (GPG_ERR_NOT_SUPPORTED);
236 This performs the decrypt operation after doing some checks on the
237 internal state (e.g. that only needed data has been set). */
239 cmd_decrypt (assuan_context_t ctx, char *line)
243 return gpg_error (GPG_ERR_NOT_SUPPORTED);
250 This does a verify operation on the message send to the input-FD.
251 The result is written out using status lines. If an output FD was
252 given, the signed text will be written to that.
254 If the signature is a detached one, the server will inquire about
255 the signed material and the client must provide it.
258 cmd_verify (assuan_context_t ctx, char *line)
261 ctrl_t ctrl = assuan_get_pointer (ctx);
262 gnupg_fd_t fd = assuan_get_input_fd (ctx);
263 gnupg_fd_t out_fd = assuan_get_output_fd (ctx);
268 if (fd == GNUPG_INVALID_FD)
269 return gpg_error (GPG_ERR_ASS_NO_INPUT);
271 if (out_fd != GNUPG_INVALID_FD)
273 out_fp = fdopen ( dup (FD2INT (out_fd)), "w");
275 return set_error (GPG_ERR_ASS_GENERAL, "fdopen() failed");
278 log_debug ("WARNING: The server mode work "
279 "in progress and not ready for use\n");
281 /* Need to dup it because it might get closed and libassuan won't
282 know about it then. */
283 rc = gpg_verify (ctrl,
285 dup ( FD2INT (ctrl->server_local->message_fd)),
290 close_message_fd (ctrl);
291 assuan_close_input_fd (ctx);
292 assuan_close_output_fd (ctx);
301 Sign the data set with the INPUT command and write it to the sink
302 set by OUTPUT. With "--detached" specified, a detached signature
305 cmd_sign (assuan_context_t ctx, char *line)
309 return gpg_error (GPG_ERR_NOT_SUPPORTED);
316 Import keys as read from the input-fd, return status message for
317 each imported one. The import checks the validity of the key. */
319 cmd_import (assuan_context_t ctx, char *line)
323 return gpg_error (GPG_ERR_NOT_SUPPORTED);
328 /* EXPORT [--data [--armor|--base64]] [--] pattern
330 Similar to the --export command line command, this command exports
331 public keys matching PATTERN. The output is send to the output fd
332 unless the --data option has been used in which case the output
333 gets send inline using regular data lines. The options "--armor"
334 and "--base" ospecify an output format if "--data" has been used.
335 Recall that in general the output format is set with the OUTPUT
339 cmd_export (assuan_context_t ctx, char *line)
343 return gpg_error (GPG_ERR_NOT_SUPPORTED);
353 cmd_delkeys (assuan_context_t ctx, char *line)
357 return gpg_error (GPG_ERR_NOT_SUPPORTED);
364 Set the file descriptor to read a message which is used with
365 detached signatures. */
367 cmd_message (assuan_context_t ctx, char *line)
371 ctrl_t ctrl = assuan_get_pointer (ctx);
373 rc = assuan_command_parse_fd (ctx, line, &fd);
376 if (fd == GNUPG_INVALID_FD)
377 return gpg_error (GPG_ERR_ASS_NO_INPUT);
378 ctrl->server_local->message_fd = fd;
384 /* LISTKEYS [<patterns>]
385 LISTSECRETKEYS [<patterns>]
390 do_listkeys (assuan_context_t ctx, char *line, int mode)
396 return gpg_error (GPG_ERR_NOT_SUPPORTED);
401 cmd_listkeys (assuan_context_t ctx, char *line)
403 return do_listkeys (ctx, line, 3);
408 cmd_listsecretkeys (assuan_context_t ctx, char *line)
410 return do_listkeys (ctx, line, 2);
417 Read the parameters in native format from the input fd and create a
421 cmd_genkey (assuan_context_t ctx, char *line)
425 return gpg_error (GPG_ERR_NOT_SUPPORTED);
431 Multipurpose function to return a variety of information.
432 Supported values for WHAT are:
434 version - Return the version of the program.
435 pid - Return the process id of the server.
439 cmd_getinfo (assuan_context_t ctx, char *line)
443 if (!strcmp (line, "version"))
445 const char *s = VERSION;
446 rc = assuan_send_data (ctx, s, strlen (s));
448 else if (!strcmp (line, "pid"))
452 snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
453 rc = assuan_send_data (ctx, numbuf, strlen (numbuf));
456 rc = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
462 /* Helper to register our commands with libassuan. */
464 register_commands (assuan_context_t ctx)
469 assuan_handler_t handler;
471 { "RECIPIENT", cmd_recipient },
472 { "SIGNER", cmd_signer },
473 { "ENCRYPT", cmd_encrypt },
474 { "DECRYPT", cmd_decrypt },
475 { "VERIFY", cmd_verify },
476 { "SIGN", cmd_sign },
477 { "IMPORT", cmd_import },
478 { "EXPORT", cmd_export },
481 { "MESSAGE", cmd_message },
482 { "LISTKEYS", cmd_listkeys },
483 { "LISTSECRETKEYS",cmd_listsecretkeys },
484 { "GENKEY", cmd_genkey },
485 { "DELKEYS", cmd_delkeys },
486 { "GETINFO", cmd_getinfo },
491 for (i=0; table[i].name; i++)
493 rc = assuan_register_command (ctx, table[i].name, table[i].handler, NULL);
503 /* Startup the server. CTRL must have been allocated by the caller
504 and set to the default values. */
506 gpg_server (ctrl_t ctrl)
510 assuan_context_t ctx = NULL;
511 static const char hello[] = ("GNU Privacy Guard's OpenPGP server "
514 /* We use a pipe based server so that we can work from scripts.
515 assuan_init_pipe_server will automagically detect when we are
516 called with a socketpair and ignore FILEDES in this case. */
517 filedes[0] = assuan_fdopen (0);
518 filedes[1] = assuan_fdopen (1);
519 rc = assuan_new (&ctx);
522 log_error ("failed to allocate the assuan context: %s\n",
527 rc = assuan_init_pipe_server (ctx, filedes);
530 log_error ("failed to initialize the server: %s\n", gpg_strerror (rc));
534 rc = register_commands (ctx);
537 log_error ("failed to the register commands with Assuan: %s\n",
542 assuan_set_pointer (ctx, ctrl);
543 if (opt.verbose || opt.debug)
546 const char *s1 = getenv ("GPG_AGENT_INFO");
554 "fixme: need config filename",
558 assuan_set_hello_line (ctx, tmp);
563 assuan_set_hello_line (ctx, hello);
564 assuan_register_reset_notify (ctx, reset_notify);
565 assuan_register_input_notify (ctx, input_notify);
566 assuan_register_output_notify (ctx, output_notify);
567 assuan_register_option_handler (ctx, option_handler);
569 ctrl->server_local = xtrycalloc (1, sizeof *ctrl->server_local);
570 if (!ctrl->server_local)
572 rc = gpg_error_from_syserror ();
575 ctrl->server_local->assuan_ctx = ctx;
576 ctrl->server_local->message_fd = GNUPG_INVALID_FD;
579 assuan_set_log_stream (ctx, log_get_stream ());
583 rc = assuan_accept (ctx);
591 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
595 rc = assuan_process (ctx);
598 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc));
604 xfree (ctrl->server_local);
605 ctrl->server_local = NULL;
606 assuan_release (ctx);