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
)
112 ctrl_t ctrl
= assuan_get_pointer (ctx
);
114 close_message_fd (ctrl
);
115 assuan_close_input_fd (ctx
);
116 assuan_close_output_fd (ctx
);
120 /* Called by libassuan for INPUT commands. */
122 input_notify (assuan_context_t ctx
, const char *line
)
124 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
128 if (strstr (line
, "--armor"))
130 else if (strstr (line
, "--base64"))
132 else if (strstr (line
, "--binary"))
136 /* FIXME (autodetect encoding) */
141 /* Called by libassuan for OUTPUT commands. */
143 output_notify (assuan_context_t ctx
, const char *line
)
145 /* ctrl_t ctrl = assuan_get_pointer (ctx); */
149 if (strstr (line
, "--armor"))
151 else if (strstr (line
, "--base64"))
160 /* RECIPIENT <userID>
162 Set the recipient for the encryption. <userID> should be the
163 internal representation of the key; the server may accept any other
164 way of specification. If this is a valid and trusted recipient the
165 server does respond with OK, otherwise the return is an ERR with
166 the reason why the recipient can't be used, the encryption will
167 then not be done for this recipient. If the policy is not to
168 encrypt at all if not all recipients are valid, the client has to
169 take care of this. All RECIPIENT commands are cumulative until a
170 RESET or an successful ENCRYPT command. */
172 cmd_recipient (assuan_context_t ctx
, char *line
)
176 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
183 Set the signer's keys for the signature creation. <userID> should
184 be the internal representation of the key; the server may accept
185 any other way of specification. If this is a valid and usable
186 signing key the server does respond with OK, otherwise it returns
187 an ERR with the reason why the key can't be used, the signing will
188 then not be done for this key. If the policy is not to sign at all
189 if not all signer keys are valid, the client has to take care of
190 this. All SIGNER commands are cumulative until a RESET but they
191 are *not* reset by an SIGN command becuase it can be expected that
192 set of signers are used for more than one sign operation.
194 Note that this command returns an INV_RECP status which is a bit
195 strange, but they are very similar. */
197 cmd_signer (assuan_context_t ctx
, char *line
)
201 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
208 Do the actual encryption process. Takes the plaintext from the
209 INPUT command, writes to the ciphertext to the file descriptor set
210 with the OUTPUT command, take the recipients form all the
211 recipients set so far. If this command fails the clients should
212 try to delete all output currently done or otherwise mark it as
213 invalid. GPG does ensure that there won't be any security problem
214 with leftover data on the output in this case.
216 This command should in general not fail, as all necessary checks
217 have been done while setting the recipients. The input and output
220 cmd_encrypt (assuan_context_t ctx
, char *line
)
224 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
231 This performs the decrypt operation after doing some checks on the
232 internal state (e.g. that only needed data has been set). */
234 cmd_decrypt (assuan_context_t ctx
, char *line
)
238 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
245 This does a verify operation on the message send to the input-FD.
246 The result is written out using status lines. If an output FD was
247 given, the signed text will be written to that.
249 If the signature is a detached one, the server will inquire about
250 the signed material and the client must provide it.
253 cmd_verify (assuan_context_t ctx
, char *line
)
256 ctrl_t ctrl
= assuan_get_pointer (ctx
);
257 gnupg_fd_t fd
= assuan_get_input_fd (ctx
);
258 gnupg_fd_t out_fd
= assuan_get_output_fd (ctx
);
263 if (fd
== GNUPG_INVALID_FD
)
264 return gpg_error (GPG_ERR_ASS_NO_INPUT
);
266 if (out_fd
!= GNUPG_INVALID_FD
)
268 out_fp
= fdopen ( dup (FD2INT (out_fd
)), "w");
270 return set_error (GPG_ERR_ASS_GENERAL
, "fdopen() failed");
273 log_debug ("WARNING: The server mode work "
274 "in progress and not ready for use\n");
276 /* Need to dup it because it might get closed and libassuan won't
277 know about it then. */
278 rc
= gpg_verify (ctrl
,
280 dup ( FD2INT (ctrl
->server_local
->message_fd
)),
285 close_message_fd (ctrl
);
286 assuan_close_input_fd (ctx
);
287 assuan_close_output_fd (ctx
);
296 Sign the data set with the INPUT command and write it to the sink
297 set by OUTPUT. With "--detached" specified, a detached signature
300 cmd_sign (assuan_context_t ctx
, char *line
)
304 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
311 Import keys as read from the input-fd, return status message for
312 each imported one. The import checks the validity of the key. */
314 cmd_import (assuan_context_t ctx
, char *line
)
318 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
323 /* EXPORT [--data [--armor|--base64]] [--] pattern
325 Similar to the --export command line command, this command exports
326 public keys matching PATTERN. The output is send to the output fd
327 unless the --data option has been used in which case the output
328 gets send inline using regular data lines. The options "--armor"
329 and "--base" ospecify an output format if "--data" has been used.
330 Recall that in general the output format is set with the OUTPUT
334 cmd_export (assuan_context_t ctx
, char *line
)
338 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
348 cmd_delkeys (assuan_context_t ctx
, char *line
)
352 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
359 Set the file descriptor to read a message which is used with
360 detached signatures. */
362 cmd_message (assuan_context_t ctx
, char *line
)
366 ctrl_t ctrl
= assuan_get_pointer (ctx
);
368 rc
= assuan_command_parse_fd (ctx
, line
, &fd
);
371 if (fd
== GNUPG_INVALID_FD
)
372 return gpg_error (GPG_ERR_ASS_NO_INPUT
);
373 ctrl
->server_local
->message_fd
= fd
;
379 /* LISTKEYS [<patterns>]
380 LISTSECRETKEYS [<patterns>]
385 do_listkeys (assuan_context_t ctx
, char *line
, int mode
)
391 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
396 cmd_listkeys (assuan_context_t ctx
, char *line
)
398 return do_listkeys (ctx
, line
, 3);
403 cmd_listsecretkeys (assuan_context_t ctx
, char *line
)
405 return do_listkeys (ctx
, line
, 2);
412 Read the parameters in native format from the input fd and create a
416 cmd_genkey (assuan_context_t ctx
, char *line
)
420 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
426 Multipurpose function to return a variety of information.
427 Supported values for WHAT are:
429 version - Return the version of the program.
430 pid - Return the process id of the server.
434 cmd_getinfo (assuan_context_t ctx
, char *line
)
438 if (!strcmp (line
, "version"))
440 const char *s
= VERSION
;
441 rc
= assuan_send_data (ctx
, s
, strlen (s
));
443 else if (!strcmp (line
, "pid"))
447 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
448 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
451 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
457 /* Helper to register our commands with libassuan. */
459 register_commands (assuan_context_t ctx
)
464 int (*handler
)(assuan_context_t
, char *line
);
466 { "RECIPIENT", cmd_recipient
},
467 { "SIGNER", cmd_signer
},
468 { "ENCRYPT", cmd_encrypt
},
469 { "DECRYPT", cmd_decrypt
},
470 { "VERIFY", cmd_verify
},
471 { "SIGN", cmd_sign
},
472 { "IMPORT", cmd_import
},
473 { "EXPORT", cmd_export
},
476 { "MESSAGE", cmd_message
},
477 { "LISTKEYS", cmd_listkeys
},
478 { "LISTSECRETKEYS",cmd_listsecretkeys
},
479 { "GENKEY", cmd_genkey
},
480 { "DELKEYS", cmd_delkeys
},
481 { "GETINFO", cmd_getinfo
},
486 for (i
=0; table
[i
].name
; i
++)
488 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
498 /* Startup the server. CTRL must have been allocated by the caller
499 and set to the default values. */
501 gpg_server (ctrl_t ctrl
)
505 assuan_context_t ctx
;
506 static const char hello
[] = ("GNU Privacy Guard's OpenPGP server "
509 /* We use a pipe based server so that we can work from scripts.
510 assuan_init_pipe_server will automagically detect when we are
511 called with a socketpair and ignore FILEDES in this case. */
514 rc
= assuan_init_pipe_server (&ctx
, filedes
);
517 log_error ("failed to initialize the server: %s\n", gpg_strerror (rc
));
521 rc
= register_commands (ctx
);
524 log_error ("failed to the register commands with Assuan: %s\n",
529 assuan_set_pointer (ctx
, ctrl
);
530 if (opt
.verbose
|| opt
.debug
)
533 const char *s1
= getenv ("GPG_AGENT_INFO");
541 "fixme: need config filename",
545 assuan_set_hello_line (ctx
, tmp
);
550 assuan_set_hello_line (ctx
, hello
);
551 assuan_register_reset_notify (ctx
, reset_notify
);
552 assuan_register_input_notify (ctx
, input_notify
);
553 assuan_register_output_notify (ctx
, output_notify
);
554 assuan_register_option_handler (ctx
, option_handler
);
556 ctrl
->server_local
= xtrycalloc (1, sizeof *ctrl
->server_local
);
557 if (!ctrl
->server_local
)
559 rc
= gpg_error_from_syserror ();
562 ctrl
->server_local
->assuan_ctx
= ctx
;
563 ctrl
->server_local
->message_fd
= GNUPG_INVALID_FD
;
566 assuan_set_log_stream (ctx
, log_get_stream ());
570 rc
= assuan_accept (ctx
);
578 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
582 rc
= assuan_process (ctx
);
585 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
591 xfree (ctrl
->server_local
);
592 ctrl
->server_local
= NULL
;
593 assuan_deinit_server (ctx
);