2007-07-05 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / server.c
bloba5793c79a3c20cae6283fa5f7a552c20cd90643f
1 /* server.c - server mode for gpg
2 * Copyright (C) 2006 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/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <ctype.h>
27 #include <unistd.h>
29 #include <assuan.h>
31 #include "gpg.h"
32 #include "util.h"
33 #include "i18n.h"
34 #include "options.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. */
42 struct server_local_s
44 /* Our current Assuan context. */
45 assuan_context_t assuan_ctx;
46 /* File descriptor as set by the MESSAGE command. */
47 int message_fd;
52 /* Helper to close the message fd if it is open. */
53 static void
54 close_message_fd (ctrl_t ctrl)
56 if (ctrl->server_local->message_fd != -1)
58 close (ctrl->server_local->message_fd);
59 ctrl->server_local->message_fd = -1;
65 /* Called by libassuan for Assuan options. See the Assuan manual for
66 details. */
67 static int
68 option_handler (assuan_context_t ctx, const char *key, const char *value)
70 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
72 /* Fixme: Implement the tty and locale args. */
73 if (!strcmp (key, "display"))
76 else if (!strcmp (key, "ttyname"))
79 else if (!strcmp (key, "ttytype"))
82 else if (!strcmp (key, "lc-ctype"))
85 else if (!strcmp (key, "lc-messages"))
88 else if (!strcmp (key, "list-mode"))
90 /* This is for now a dummy option. */
92 else
93 return gpg_error (GPG_ERR_UNKNOWN_OPTION);
95 return 0;
99 /* Called by libassuan for RESET commands. */
100 static void
101 reset_notify (assuan_context_t ctx)
103 ctrl_t ctrl = assuan_get_pointer (ctx);
105 close_message_fd (ctrl);
106 assuan_close_input_fd (ctx);
107 assuan_close_output_fd (ctx);
111 /* Called by libassuan for INPUT commands. */
112 static void
113 input_notify (assuan_context_t ctx, const char *line)
115 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
117 if (strstr (line, "--armor"))
118 ; /* FIXME */
119 else if (strstr (line, "--base64"))
120 ; /* FIXME */
121 else if (strstr (line, "--binary"))
123 else
124 ; /* FIXME (autodetect encoding) */
128 /* Called by libassuan for OUTPUT commands. */
129 static void
130 output_notify (assuan_context_t ctx, const char *line)
132 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
134 if (strstr (line, "--armor"))
135 ; /* FIXME */
136 else if (strstr (line, "--base64"))
137 ; /* FIXME */
143 /* RECIPIENT <userID>
145 Set the recipient for the encryption. <userID> should be the
146 internal representation of the key; the server may accept any other
147 way of specification. If this is a valid and trusted recipient the
148 server does respond with OK, otherwise the return is an ERR with
149 the reason why the recipient can't be used, the encryption will
150 then not be done for this recipient. If the policy is not to
151 encrypt at all if not all recipients are valid, the client has to
152 take care of this. All RECIPIENT commands are cumulative until a
153 RESET or an successful ENCRYPT command. */
154 static int
155 cmd_recipient (assuan_context_t ctx, char *line)
157 return gpg_error (GPG_ERR_NOT_SUPPORTED);
162 /* SIGNER <userID>
164 Set the signer's keys for the signature creation. <userID> should
165 be the internal representation of the key; the server may accept
166 any other way of specification. If this is a valid and usable
167 signing key the server does respond with OK, otherwise it returns
168 an ERR with the reason why the key can't be used, the signing will
169 then not be done for this key. If the policy is not to sign at all
170 if not all signer keys are valid, the client has to take care of
171 this. All SIGNER commands are cumulative until a RESET but they
172 are *not* reset by an SIGN command becuase it can be expected that
173 set of signers are used for more than one sign operation.
175 Note that this command returns an INV_RECP status which is a bit
176 strange, but they are very similar. */
177 static int
178 cmd_signer (assuan_context_t ctx, char *line)
180 return gpg_error (GPG_ERR_NOT_SUPPORTED);
185 /* ENCRYPT
187 Do the actual encryption process. Takes the plaintext from the
188 INPUT command, writes to the ciphertext to the file descriptor set
189 with the OUTPUT command, take the recipients form all the
190 recipients set so far. If this command fails the clients should
191 try to delete all output currently done or otherwise mark it as
192 invalid. GPG does ensure that there won't be any security problem
193 with leftover data on the output in this case.
195 This command should in general not fail, as all necessary checks
196 have been done while setting the recipients. The input and output
197 pipes are closed. */
198 static int
199 cmd_encrypt (assuan_context_t ctx, char *line)
201 return gpg_error (GPG_ERR_NOT_SUPPORTED);
206 /* DECRYPT
208 This performs the decrypt operation after doing some checks on the
209 internal state (e.g. that only needed data has been set). */
210 static int
211 cmd_decrypt (assuan_context_t ctx, char *line)
213 return gpg_error (GPG_ERR_NOT_SUPPORTED);
218 /* VERIFY
220 This does a verify operation on the message send to the input-FD.
221 The result is written out using status lines. If an output FD was
222 given, the signed text will be written to that.
224 If the signature is a detached one, the server will inquire about
225 the signed material and the client must provide it.
227 static int
228 cmd_verify (assuan_context_t ctx, char *line)
230 int rc;
231 ctrl_t ctrl = assuan_get_pointer (ctx);
232 int fd = assuan_get_input_fd (ctx);
233 int out_fd = assuan_get_output_fd (ctx);
234 FILE *out_fp = NULL;
236 if (fd == -1)
237 return gpg_error (GPG_ERR_ASS_NO_INPUT);
239 if (out_fd != -1)
241 out_fp = fdopen ( dup(out_fd), "w");
242 if (!out_fp)
243 return set_error (GPG_ERR_ASS_GENERAL, "fdopen() failed");
246 log_debug ("WARNING: The server mode work in progress and not ready for use\n");
248 /* Need to dup it because it might get closed and libassuan won't
249 know about it then. */
250 rc = gpg_verify (ctrl,
251 dup (fd),
252 dup (ctrl->server_local->message_fd),
253 out_fp);
255 if (out_fp)
256 fclose (out_fp);
257 close_message_fd (ctrl);
258 assuan_close_input_fd (ctx);
259 assuan_close_output_fd (ctx);
261 return rc;
266 /* SIGN [--detached]
268 Sign the data set with the INPUT command and write it to the sink
269 set by OUTPUT. With "--detached" specified, a detached signature
270 is created. */
271 static int
272 cmd_sign (assuan_context_t ctx, char *line)
274 return gpg_error (GPG_ERR_NOT_SUPPORTED);
279 /* IMPORT
281 Import keys as read from the input-fd, return status message for
282 each imported one. The import checks the validity of the key. */
283 static int
284 cmd_import (assuan_context_t ctx, char *line)
286 return gpg_error (GPG_ERR_NOT_SUPPORTED);
291 /* EXPORT [--data [--armor|--base64]] [--] pattern
293 Similar to the --export command line command, this command exports
294 public keys matching PATTERN. The output is send to the output fd
295 unless the --data option has been used in which case the output
296 gets send inline using regular data lines. The options "--armor"
297 and "--base" ospecify an output format if "--data" has been used.
298 Recall that in general the output format is set with the OUTPUT
299 command.
301 static int
302 cmd_export (assuan_context_t ctx, char *line)
304 return gpg_error (GPG_ERR_NOT_SUPPORTED);
309 /* DELKEYS
311 Fixme
313 static int
314 cmd_delkeys (assuan_context_t ctx, char *line)
316 return gpg_error (GPG_ERR_NOT_SUPPORTED);
321 /* MESSAGE FD[=<n>]
323 Set the file descriptor to read a message which is used with
324 detached signatures. */
325 static int
326 cmd_message (assuan_context_t ctx, char *line)
328 int rc;
329 int fd;
330 ctrl_t ctrl = assuan_get_pointer (ctx);
332 rc = assuan_command_parse_fd (ctx, line, &fd);
333 if (rc)
334 return rc;
335 if (fd == -1)
336 return gpg_error (GPG_ERR_ASS_NO_INPUT);
337 ctrl->server_local->message_fd = fd;
338 return 0;
343 /* LISTKEYS [<patterns>]
344 LISTSECRETKEYS [<patterns>]
346 fixme
348 static int
349 do_listkeys (assuan_context_t ctx, char *line, int mode)
351 return gpg_error (GPG_ERR_NOT_SUPPORTED);
355 static int
356 cmd_listkeys (assuan_context_t ctx, char *line)
358 return do_listkeys (ctx, line, 3);
362 static int
363 cmd_listsecretkeys (assuan_context_t ctx, char *line)
365 return do_listkeys (ctx, line, 2);
370 /* GENKEY
372 Read the parameters in native format from the input fd and create a
373 new OpenPGP key.
375 static int
376 cmd_genkey (assuan_context_t ctx, char *line)
378 return gpg_error (GPG_ERR_NOT_SUPPORTED);
386 /* Helper to register our commands with libassuan. */
387 static int
388 register_commands (assuan_context_t ctx)
390 static struct
392 const char *name;
393 int (*handler)(assuan_context_t, char *line);
394 } table[] = {
395 { "RECIPIENT", cmd_recipient },
396 { "SIGNER", cmd_signer },
397 { "ENCRYPT", cmd_encrypt },
398 { "DECRYPT", cmd_decrypt },
399 { "VERIFY", cmd_verify },
400 { "SIGN", cmd_sign },
401 { "IMPORT", cmd_import },
402 { "EXPORT", cmd_export },
403 { "INPUT", NULL },
404 { "OUTPUT", NULL },
405 { "MESSAGE", cmd_message },
406 { "LISTKEYS", cmd_listkeys },
407 { "LISTSECRETKEYS",cmd_listsecretkeys },
408 { "GENKEY", cmd_genkey },
409 { "DELKEYS", cmd_delkeys },
410 { NULL }
412 int i, rc;
414 for (i=0; table[i].name; i++)
416 rc = assuan_register_command (ctx, table[i].name, table[i].handler);
417 if (rc)
418 return rc;
420 return 0;
426 /* Startup the server. CTRL must have been allocated by the caller
427 and set to the default values. */
429 gpg_server (ctrl_t ctrl)
431 int rc;
432 int filedes[2];
433 assuan_context_t ctx;
434 static const char hello[] = ("GNU Privacy Guard's OpenPGP server "
435 VERSION " ready");
437 /* We use a pipe based server so that we can work from scripts.
438 assuan_init_pipe_server will automagically detect when we are
439 called with a socketpair and ignore FILEDES in this case. */
440 filedes[0] = 0;
441 filedes[1] = 1;
442 rc = assuan_init_pipe_server (&ctx, filedes);
443 if (rc)
445 log_error ("failed to initialize the server: %s\n", gpg_strerror (rc));
446 goto leave;
449 rc = register_commands (ctx);
450 if (rc)
452 log_error ("failed to the register commands with Assuan: %s\n",
453 gpg_strerror(rc));
454 goto leave;
457 assuan_set_pointer (ctx, ctrl);
458 if (opt.verbose || opt.debug)
460 char *tmp = NULL;
461 const char *s1 = getenv ("GPG_AGENT_INFO");
463 if (asprintf (&tmp,
464 "Home: %s\n"
465 "Config: %s\n"
466 "AgentInfo: %s\n"
467 "%s",
468 opt.homedir,
469 "fixme: need config filename",
470 s1?s1:"[not set]",
471 hello) > 0)
473 assuan_set_hello_line (ctx, tmp);
474 free (tmp);
477 else
478 assuan_set_hello_line (ctx, hello);
479 assuan_register_reset_notify (ctx, reset_notify);
480 assuan_register_input_notify (ctx, input_notify);
481 assuan_register_output_notify (ctx, output_notify);
482 assuan_register_option_handler (ctx, option_handler);
484 ctrl->server_local = xtrycalloc (1, sizeof *ctrl->server_local);
485 if (!ctrl->server_local)
487 rc = gpg_error_from_syserror ();
488 goto leave;
490 ctrl->server_local->assuan_ctx = ctx;
491 ctrl->server_local->message_fd = -1;
493 if (DBG_ASSUAN)
494 assuan_set_log_stream (ctx, log_get_stream ());
496 for (;;)
498 rc = assuan_accept (ctx);
499 if (rc == -1)
501 rc = 0;
502 break;
504 else if (rc)
506 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
507 break;
510 rc = assuan_process (ctx);
511 if (rc)
513 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc));
514 continue;
518 leave:
519 xfree (ctrl->server_local);
520 ctrl->server_local = NULL;
521 assuan_deinit_server (ctx);
522 return rc;