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/>.
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. */
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
!= -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
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. */
93 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
99 /* Called by libassuan for RESET commands. */
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. */
113 input_notify (assuan_context_t ctx
, const char *line
)
115 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
117 if (strstr (line
, "--armor"))
119 else if (strstr (line
, "--base64"))
121 else if (strstr (line
, "--binary"))
124 ; /* FIXME (autodetect encoding) */
128 /* Called by libassuan for OUTPUT commands. */
130 output_notify (assuan_context_t ctx
, const char *line
)
132 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
134 if (strstr (line
, "--armor"))
136 else if (strstr (line
, "--base64"))
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. */
155 cmd_recipient (assuan_context_t ctx
, char *line
)
157 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
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. */
178 cmd_signer (assuan_context_t ctx
, char *line
)
180 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
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
199 cmd_encrypt (assuan_context_t ctx
, char *line
)
201 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
208 This performs the decrypt operation after doing some checks on the
209 internal state (e.g. that only needed data has been set). */
211 cmd_decrypt (assuan_context_t ctx
, char *line
)
213 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
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.
228 cmd_verify (assuan_context_t ctx
, char *line
)
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
);
237 return gpg_error (GPG_ERR_ASS_NO_INPUT
);
241 out_fp
= fdopen ( dup(out_fd
), "w");
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
,
252 dup (ctrl
->server_local
->message_fd
),
257 close_message_fd (ctrl
);
258 assuan_close_input_fd (ctx
);
259 assuan_close_output_fd (ctx
);
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
272 cmd_sign (assuan_context_t ctx
, char *line
)
274 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
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. */
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
302 cmd_export (assuan_context_t ctx
, char *line
)
304 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
314 cmd_delkeys (assuan_context_t ctx
, char *line
)
316 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
323 Set the file descriptor to read a message which is used with
324 detached signatures. */
326 cmd_message (assuan_context_t ctx
, char *line
)
330 ctrl_t ctrl
= assuan_get_pointer (ctx
);
332 rc
= assuan_command_parse_fd (ctx
, line
, &fd
);
336 return gpg_error (GPG_ERR_ASS_NO_INPUT
);
337 ctrl
->server_local
->message_fd
= fd
;
343 /* LISTKEYS [<patterns>]
344 LISTSECRETKEYS [<patterns>]
349 do_listkeys (assuan_context_t ctx
, char *line
, int mode
)
351 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
356 cmd_listkeys (assuan_context_t ctx
, char *line
)
358 return do_listkeys (ctx
, line
, 3);
363 cmd_listsecretkeys (assuan_context_t ctx
, char *line
)
365 return do_listkeys (ctx
, line
, 2);
372 Read the parameters in native format from the input fd and create a
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. */
388 register_commands (assuan_context_t ctx
)
393 int (*handler
)(assuan_context_t
, char *line
);
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
},
405 { "MESSAGE", cmd_message
},
406 { "LISTKEYS", cmd_listkeys
},
407 { "LISTSECRETKEYS",cmd_listsecretkeys
},
408 { "GENKEY", cmd_genkey
},
409 { "DELKEYS", cmd_delkeys
},
414 for (i
=0; table
[i
].name
; i
++)
416 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
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
)
433 assuan_context_t ctx
;
434 static const char hello
[] = ("GNU Privacy Guard's OpenPGP server "
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. */
442 rc
= assuan_init_pipe_server (&ctx
, filedes
);
445 log_error ("failed to initialize the server: %s\n", gpg_strerror (rc
));
449 rc
= register_commands (ctx
);
452 log_error ("failed to the register commands with Assuan: %s\n",
457 assuan_set_pointer (ctx
, ctrl
);
458 if (opt
.verbose
|| opt
.debug
)
461 const char *s1
= getenv ("GPG_AGENT_INFO");
469 "fixme: need config filename",
473 assuan_set_hello_line (ctx
, tmp
);
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 ();
490 ctrl
->server_local
->assuan_ctx
= ctx
;
491 ctrl
->server_local
->message_fd
= -1;
494 assuan_set_log_stream (ctx
, log_get_stream ());
498 rc
= assuan_accept (ctx
);
506 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
510 rc
= assuan_process (ctx
);
513 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
519 xfree (ctrl
->server_local
);
520 ctrl
->server_local
= NULL
;
521 assuan_deinit_server (ctx
);