1 /* engine-gpgsm.c - GpgSM engine
2 * Copyright (C) 2000 Werner Koch (dd9jn)
3 * Copyright (C) 2001, 2002 g10 Code GmbH
5 * This file is part of GPGME.
7 * GPGME is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GPGME is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 /* FIXME: Correct check? */
28 #define ENABLE_GPGSM 1
35 #include <sys/types.h>
39 #include <fcntl.h> /* FIXME */
42 #include "status-table.h"
53 #include "engine-gpgsm.h"
57 #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
58 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
59 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
64 ASSUAN_CONTEXT assuan_ctx;
66 /* Input, output etc are from the servers perspective. */
72 GpgmeData output_data;
74 int message_fd_server;
75 GpgmeData message_data;
87 GpgColonLineHandler fnc;
100 _gpgme_gpgsm_get_version (void)
102 static const char *gpgsm_version;
103 DEFINE_STATIC_LOCK (gpgsm_version_lock);
105 LOCK (gpgsm_version_lock);
107 gpgsm_version = _gpgme_get_program_version (_gpgme_get_gpgsm_path ());
108 UNLOCK (gpgsm_version_lock);
110 return gpgsm_version;
115 _gpgme_gpgsm_check_version (void)
117 return _gpgme_compare_versions (_gpgme_gpgsm_get_version (),
119 ? 0 : mk_error (Invalid_Engine);
124 close_notify_handler (int fd, void *opaque)
126 GpgsmObject gpgsm = opaque;
129 if (gpgsm->input_fd == fd)
130 gpgsm->input_fd = -1;
131 else if (gpgsm->output_fd == fd)
132 gpgsm->output_fd = -1;
133 else if (gpgsm->message_fd == fd)
134 gpgsm->message_fd = -1;
139 map_assuan_error (AssuanError err)
143 case ASSUAN_No_Error:
144 return mk_error (No_Error);
145 case ASSUAN_General_Error:
146 return mk_error (General_Error);
147 case ASSUAN_Out_Of_Core:
148 return mk_error (Out_Of_Core);
149 case ASSUAN_Invalid_Value:
150 return mk_error (Invalid_Value);
151 case ASSUAN_Read_Error:
152 return mk_error (Read_Error);
153 case ASSUAN_Write_Error:
154 return mk_error (Write_Error);
157 case ASSUAN_Problem_Starting_Server:
158 case ASSUAN_Not_A_Server:
159 case ASSUAN_Not_A_Client:
160 case ASSUAN_Nested_Commands:
161 case ASSUAN_Invalid_Response:
162 case ASSUAN_No_Data_Callback:
163 case ASSUAN_No_Inquire_Callback:
164 case ASSUAN_Connect_Failed:
165 case ASSUAN_Accept_Failed:
166 return mk_error (General_Error);
168 /* The following error codes are meant as status codes. */
169 case ASSUAN_Not_Implemented:
170 return mk_error (Not_Implemented);
171 case ASSUAN_Canceled:
172 return mk_error (Canceled);
173 case ASSUAN_Unsupported_Algorithm:
174 return mk_error (Not_Implemented); /* XXX Argh. */
176 /* These are errors internal to GPGME. */
177 case ASSUAN_No_Data_Available:
178 case ASSUAN_No_Input:
179 case ASSUAN_No_Output:
180 case ASSUAN_Invalid_Command:
181 case ASSUAN_Unknown_Command:
182 case ASSUAN_Syntax_Error:
183 case ASSUAN_Parameter_Error:
184 case ASSUAN_Parameter_Conflict:
185 case ASSUAN_Line_Too_Long:
186 case ASSUAN_Line_Not_Terminated:
187 case ASSUAN_Invalid_Data:
188 case ASSUAN_Unexpected_Command:
189 case ASSUAN_Too_Much_Data:
190 case ASSUAN_Inquire_Unknown:
191 case ASSUAN_Inquire_Error:
192 case ASSUAN_Invalid_Option:
193 case ASSUAN_Invalid_Index:
194 case ASSUAN_Unexpected_Status:
195 case ASSUAN_Unexpected_Data:
196 case ASSUAN_Invalid_Status:
197 case ASSUAN_Not_Confirmed:
198 return mk_error (General_Error);
200 /* These are errors in the server. */
201 case ASSUAN_Server_Fault:
202 case ASSUAN_Server_Resource_Problem:
203 case ASSUAN_Server_IO_Error:
204 case ASSUAN_Server_Bug:
205 case ASSUAN_No_Agent:
206 case ASSUAN_Agent_Error:
207 return mk_error (Invalid_Engine); /* XXX: Need something more useful. */
209 case ASSUAN_Bad_Certificate:
210 case ASSUAN_Bad_Certificate_Path:
211 case ASSUAN_Missing_Certificate:
212 case ASSUAN_No_Public_Key:
213 case ASSUAN_No_Secret_Key:
214 case ASSUAN_Invalid_Name:
215 case ASSUAN_Card_Error: /* XXX: Oh well. */
216 case ASSUAN_Invalid_Card: /* XXX: Oh well. */
217 case ASSUAN_No_PKCS15_App: /* XXX: Oh well. */
218 case ASSUAN_Card_Not_Present: /* XXX: Oh well. */
219 case ASSUAN_Invalid_Id: /* XXX: Oh well. */
220 return mk_error (Invalid_Key);
222 case ASSUAN_Bad_Signature:
223 return mk_error (Invalid_Key); /* XXX: This is wrong. */
225 case ASSUAN_Cert_Revoked:
226 case ASSUAN_No_CRL_For_Cert:
227 case ASSUAN_CRL_Too_Old:
228 case ASSUAN_Not_Trusted:
229 return mk_error (Invalid_Key); /* XXX Some more details would be good. */
232 return mk_error (General_Error);
238 _gpgme_gpgsm_new (GpgsmObject *r_gpgsm)
245 char *dft_display = NULL;
246 char *dft_ttyname = NULL;
247 char *dft_ttytype = NULL;
253 gpgsm = xtrycalloc (1, sizeof *gpgsm);
256 err = mk_error (Out_Of_Core);
260 gpgsm->input_fd = -1;
261 gpgsm->input_fd_server = -1;
262 gpgsm->output_fd = -1;
263 gpgsm->output_fd_server = -1;
264 gpgsm->message_fd = -1;
265 gpgsm->message_fd_server = -1;
267 gpgsm->status.fnc = 0;
268 gpgsm->colon.fnc = 0;
269 gpgsm->colon.attic.line = 0;
270 gpgsm->colon.attic.linesize = 0;
271 gpgsm->colon.attic.linelen = 0;
273 if (_gpgme_io_pipe (fds, 0) < 0)
275 err = mk_error (Pipe_Error);
278 gpgsm->input_fd = fds[1];
279 gpgsm->input_fd_server = fds[0];
281 if (_gpgme_io_pipe (fds, 1) < 0)
283 err = mk_error (Pipe_Error);
286 gpgsm->output_fd = fds[0];
287 gpgsm->output_fd_server = fds[1];
289 if (_gpgme_io_pipe (fds, 0) < 0)
291 err = mk_error (Pipe_Error);
294 gpgsm->message_fd = fds[1];
295 gpgsm->message_fd_server = fds[0];
297 child_fds[0] = gpgsm->input_fd_server;
298 child_fds[1] = gpgsm->output_fd_server;
299 child_fds[2] = gpgsm->message_fd_server;
303 argv[1] = "--server";
306 err = assuan_pipe_connect2 (&gpgsm->assuan_ctx,
307 _gpgme_get_gpgsm_path (), argv, child_fds,
308 1 /* dup stderr to /dev/null */);
310 dft_display = getenv ("DISPLAY");
313 if (asprintf (&optstr, "OPTION display=%s", dft_display) < 0)
315 err = mk_error (Out_Of_Core);
318 err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
323 err = map_assuan_error (err);
327 dft_ttyname = ttyname (1);
330 if (asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0)
332 err = mk_error (Out_Of_Core);
335 err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
340 err = map_assuan_error (err);
344 dft_ttytype = getenv ("TERM");
347 if (asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype) < 0)
349 err = mk_error (Out_Of_Core);
352 err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
357 err = map_assuan_error (err);
361 old_lc = setlocale (LC_CTYPE, NULL);
362 dft_lc = setlocale (LC_CTYPE, "");
365 if (asprintf (&optstr, "OPTION lc-ctype=%s", dft_lc) < 0)
366 err = mk_error (Out_Of_Core);
369 err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
373 err = map_assuan_error (err);
377 setlocale (LC_CTYPE, old_lc);
381 old_lc = setlocale (LC_MESSAGES, NULL);
382 dft_lc = setlocale (LC_MESSAGES, "");
385 if (asprintf (&optstr, "OPTION lc-messages=%s", dft_lc) < 0)
386 err = mk_error (Out_Of_Core);
389 err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
393 err = map_assuan_error (err);
397 setlocale (LC_MESSAGES, old_lc);
403 (_gpgme_io_set_close_notify (gpgsm->input_fd,
404 close_notify_handler, gpgsm)
405 || _gpgme_io_set_close_notify (gpgsm->output_fd,
406 close_notify_handler, gpgsm)
407 || _gpgme_io_set_close_notify (gpgsm->message_fd,
408 close_notify_handler, gpgsm)))
410 err = mk_error (General_Error);
415 /* Close the server ends of the pipes. Our ends are closed in
416 _gpgme_gpgsm_release. */
417 if (gpgsm->input_fd_server != -1)
418 _gpgme_io_close (gpgsm->input_fd_server);
419 if (gpgsm->output_fd_server != -1)
420 _gpgme_io_close (gpgsm->output_fd_server);
421 if (gpgsm->message_fd_server != -1)
422 _gpgme_io_close (gpgsm->message_fd_server);
425 _gpgme_gpgsm_release (gpgsm);
434 _gpgme_gpgsm_release (GpgsmObject gpgsm)
441 pid = assuan_get_pid (gpgsm->assuan_ctx);
443 _gpgme_remove_proc_from_wait_queue (pid);
445 if (gpgsm->input_fd != -1)
446 _gpgme_io_close (gpgsm->input_fd);
447 if (gpgsm->output_fd != -1)
448 _gpgme_io_close (gpgsm->output_fd);
449 if (gpgsm->message_fd != -1)
450 _gpgme_io_close (gpgsm->message_fd);
452 assuan_disconnect (gpgsm->assuan_ctx);
454 xfree (gpgsm->colon.attic.line);
455 xfree (gpgsm->command);
461 gpgsm_assuan_simple_command (ASSUAN_CONTEXT ctx, char *cmd)
467 err = assuan_write_line (ctx, cmd);
469 return map_assuan_error (err);
473 err = assuan_read_line (ctx, &line, &linelen);
475 return map_assuan_error (err);
477 while (*line == '#' || !linelen);
480 && line[0] == 'O' && line[1] == 'K'
481 && (line[2] == '\0' || line[2] == ' '))
485 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
487 err = map_assuan_error (atoi (&line[4]));
490 err = mk_error (General_Error);
495 #define COMMANDLINELEN 40
497 gpgsm_set_fd (ASSUAN_CONTEXT ctx, const char *which, int fd, const char *opt)
499 char line[COMMANDLINELEN];
502 snprintf (line, COMMANDLINELEN, "%s FD=%i %s", which, fd, opt);
504 snprintf (line, COMMANDLINELEN, "%s FD=%i", which, fd);
506 return gpgsm_assuan_simple_command (ctx, line);
511 map_input_enc (GpgmeData d)
513 switch (gpgme_data_get_encoding (d))
515 case GPGME_DATA_ENCODING_NONE: break;
516 case GPGME_DATA_ENCODING_BINARY: return "--binary";
517 case GPGME_DATA_ENCODING_BASE64: return "--base64";
518 case GPGME_DATA_ENCODING_ARMOR: return "--armor";
526 _gpgme_gpgsm_op_decrypt (GpgsmObject gpgsm, GpgmeData ciph, GpgmeData plain)
531 return mk_error (Invalid_Value);
533 gpgsm->command = xtrystrdup ("DECRYPT");
535 return mk_error (Out_Of_Core);
537 gpgsm->input_data = ciph;
538 err = gpgsm_set_fd (gpgsm->assuan_ctx, "INPUT", gpgsm->input_fd_server,
539 map_input_enc (gpgsm->input_data));
541 return mk_error (General_Error); /* FIXME */
542 gpgsm->output_data = plain;
543 err = gpgsm_set_fd (gpgsm->assuan_ctx, "OUTPUT", gpgsm->output_fd_server, 0);
545 return mk_error (General_Error); /* FIXME */
546 _gpgme_io_close (gpgsm->message_fd);
553 _gpgme_gpgsm_op_delete (GpgsmObject gpgsm, GpgmeKey key, int allow_secret)
556 return mk_error (Not_Implemented);
561 gpgsm_set_recipients (GpgsmObject gpgsm, GpgmeRecipients recp)
564 ASSUAN_CONTEXT ctx = gpgsm->assuan_ctx;
568 int valid_recipients = 0;
570 linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */
571 line = xtrymalloc (10 + 40 + 1);
573 return mk_error (Out_Of_Core);
574 strcpy (line, "RECIPIENT ");
575 for (r = recp->list; r; r = r->next)
577 int newlen = 11 + strlen (r->name);
578 if (linelen < newlen)
580 char *newline = xtryrealloc (line, newlen);
584 return mk_error (Out_Of_Core);
589 strcpy (&line[10], r->name);
591 err = gpgsm_assuan_simple_command (ctx, line);
593 valid_recipients = 1;
594 else if (err == GPGME_Invalid_Key && gpgsm->status.fnc)
596 /* FIXME: Include other reasons. */
597 line[8] = '0'; /* FIXME: Report detailed reason. */
598 gpgsm->status.fnc (gpgsm->status.fnc_value, STATUS_INV_RECP, &line[8]);
601 else if (err != GPGME_Invalid_Key)
608 if (!valid_recipients && gpgsm->status.fnc)
609 gpgsm->status.fnc (gpgsm->status.fnc_value, STATUS_NO_RECP, "");
615 _gpgme_gpgsm_op_encrypt (GpgsmObject gpgsm, GpgmeRecipients recp,
616 GpgmeData plain, GpgmeData ciph, int use_armor)
621 return mk_error (Invalid_Value);
623 return mk_error (Not_Implemented);
625 gpgsm->command = xtrystrdup ("ENCRYPT");
627 return mk_error (Out_Of_Core);
629 gpgsm->input_data = plain;
630 err = gpgsm_set_fd (gpgsm->assuan_ctx, "INPUT", gpgsm->input_fd_server,
631 map_input_enc (gpgsm->input_data));
634 gpgsm->output_data = ciph;
635 err = gpgsm_set_fd (gpgsm->assuan_ctx, "OUTPUT", gpgsm->output_fd_server,
636 use_armor ? "--armor" : 0);
639 _gpgme_io_close (gpgsm->message_fd);
641 err = gpgsm_set_recipients (gpgsm, recp);
650 _gpgme_gpgsm_op_export (GpgsmObject gpgsm, GpgmeRecipients recp,
651 GpgmeData keydata, int use_armor)
654 return mk_error (Not_Implemented);
659 _gpgme_gpgsm_op_genkey (GpgsmObject gpgsm, GpgmeData help_data, int use_armor,
660 GpgmeData pubkey, GpgmeData seckey)
664 if (!gpgsm || !pubkey || seckey)
665 return mk_error (Invalid_Value);
667 gpgsm->command = xtrystrdup ("GENKEY");
669 return mk_error (Out_Of_Core);
671 gpgsm->input_data = help_data;
672 err = gpgsm_set_fd (gpgsm->assuan_ctx, "INPUT", gpgsm->input_fd_server,
673 map_input_enc (gpgsm->input_data));
676 gpgsm->output_data = pubkey;
677 err = gpgsm_set_fd (gpgsm->assuan_ctx, "OUTPUT", gpgsm->output_fd_server,
678 use_armor ? "--armor" : 0);
681 _gpgme_io_close (gpgsm->message_fd);
688 _gpgme_gpgsm_op_import (GpgsmObject gpgsm, GpgmeData keydata)
693 return mk_error (Invalid_Value);
695 gpgsm->command = xtrystrdup ("IMPORT");
697 return mk_error (Out_Of_Core);
699 gpgsm->input_data = keydata;
700 err = gpgsm_set_fd (gpgsm->assuan_ctx, "INPUT", gpgsm->input_fd_server,
701 map_input_enc (gpgsm->input_data));
704 _gpgme_io_close (gpgsm->output_fd);
705 _gpgme_io_close (gpgsm->message_fd);
712 _gpgme_gpgsm_op_keylist (GpgsmObject gpgsm, const char *pattern,
713 int secret_only, int keylist_mode)
721 if (asprintf (&line, "OPTION list-mode=%d", (keylist_mode & 3)) < 0)
722 return mk_error (Out_Of_Core);
723 err = gpgsm_assuan_simple_command (gpgsm->assuan_ctx, line);
728 /* Length is "LISTSECRETKEYS " + p + '\0'. */
729 line = xtrymalloc (15 + strlen (pattern) + 1);
731 return mk_error (Out_Of_Core);
734 strcpy (line, "LISTSECRETKEYS ");
735 strcpy (&line[15], pattern);
739 strcpy (line, "LISTKEYS ");
740 strcpy (&line[9], pattern);
743 _gpgme_io_close (gpgsm->input_fd);
744 _gpgme_io_close (gpgsm->output_fd);
745 _gpgme_io_close (gpgsm->message_fd);
747 gpgsm->command = line;
753 _gpgme_gpgsm_op_keylist_ext (GpgsmObject gpgsm, const char *pattern[],
754 int secret_only, int reserved, int keylist_mode)
758 /* Length is "LISTSECRETKEYS " + p + '\0'. */
763 return mk_error (Invalid_Value);
765 if (asprintf (&line, "OPTION list-mode=%d", (keylist_mode & 3)) < 0)
766 return mk_error (Out_Of_Core);
767 err = gpgsm_assuan_simple_command (gpgsm->assuan_ctx, line);
772 if (pattern && *pattern)
774 const char **pat = pattern;
778 const char *patlet = *pat;
783 if (*patlet == '%' || *patlet == ' ' || *patlet == '+')
788 /* This will allocate one byte more than necessary. */
792 line = xtrymalloc (length);
794 return mk_error (Out_Of_Core);
797 strcpy (line, "LISTSECRETKEYS ");
802 strcpy (line, "LISTKEYS ");
806 if (pattern && *pattern)
810 const char *patlet = *pattern;
832 *(linep++) = *patlet;
842 _gpgme_io_close (gpgsm->input_fd);
843 _gpgme_io_close (gpgsm->output_fd);
844 _gpgme_io_close (gpgsm->message_fd);
846 gpgsm->command = line;
852 _gpgme_gpgsm_op_sign (GpgsmObject gpgsm, GpgmeData in, GpgmeData out,
853 GpgmeSigMode mode, int use_armor,
854 int use_textmode, int include_certs,
855 GpgmeCtx ctx /* FIXME */)
861 return mk_error (Invalid_Value);
863 gpgsm->command = xtrystrdup (mode == GPGME_SIG_MODE_DETACH
864 ? "SIGN --detached" : "SIGN");
866 return mk_error (Out_Of_Core);
868 if (asprintf (&assuan_cmd, "OPTION include-certs %i", include_certs) < 0)
869 return mk_error (Out_Of_Core);
870 err = gpgsm_assuan_simple_command (gpgsm->assuan_ctx, assuan_cmd);
875 gpgsm->input_data = in;
876 err = gpgsm_set_fd (gpgsm->assuan_ctx, "INPUT", gpgsm->input_fd_server,
877 map_input_enc (gpgsm->input_data));
880 gpgsm->output_data = out;
881 err = gpgsm_set_fd (gpgsm->assuan_ctx, "OUTPUT", gpgsm->output_fd_server,
882 use_armor ? "--armor" : 0);
885 _gpgme_io_close (gpgsm->message_fd);
892 _gpgme_gpgsm_op_trustlist (GpgsmObject gpgsm, const char *pattern)
895 return mk_error (Not_Implemented);
900 _gpgme_gpgsm_op_verify (GpgsmObject gpgsm, GpgmeData sig, GpgmeData text)
905 return mk_error (Invalid_Value);
907 gpgsm->command = xtrystrdup ("VERIFY");
909 return mk_error (Out_Of_Core);
911 gpgsm->input_data = sig;
912 err = gpgsm_set_fd (gpgsm->assuan_ctx, "INPUT", gpgsm->input_fd_server,
913 map_input_enc (gpgsm->input_data));
916 if (_gpgme_data_get_mode (text) == GPGME_DATA_MODE_IN)
918 /* Normal or cleartext signature. */
919 gpgsm->output_data = text;
920 err = gpgsm_set_fd (gpgsm->assuan_ctx, "OUTPUT", gpgsm->output_fd_server,
922 _gpgme_io_close (gpgsm->message_fd);
926 /* Detached signature. */
927 gpgsm->message_data = text;
928 err = gpgsm_set_fd (gpgsm->assuan_ctx, "MESSAGE",
929 gpgsm->message_fd_server, 0);
930 _gpgme_io_close (gpgsm->output_fd);
940 status_cmp (const void *ap, const void *bp)
942 const struct status_table_s *a = ap;
943 const struct status_table_s *b = bp;
945 return strcmp (a->name, b->name);
950 gpgsm_status_handler (void *opaque, int pid, int fd)
953 GpgsmObject gpgsm = opaque;
959 err = assuan_read_line (gpgsm->assuan_ctx, &line, &linelen);
963 && line[0] == 'O' && line[1] == 'K'
964 && (line[2] == '\0' || line[2] == ' '))
966 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
967 && (line[3] == '\0' || line[3] == ' ')))
969 /* XXX: If an error occured, find out what happened, then save the error value
970 before running the status handler (so it takes precedence). */
971 if (!err && line[0] == 'E' && line[3] == ' ')
973 err = map_assuan_error (atoi (&line[4]));
975 err = mk_error (General_Error);
979 /* XXX Kludge ahead. We really, really, really must not
980 make use of status.fnc_value. */
981 GpgmeCtx ctx = (GpgmeCtx) gpgsm->status.fnc_value;
986 if (gpgsm->status.fnc)
987 gpgsm->status.fnc (gpgsm->status.fnc_value, STATUS_EOF, "");
989 /* XXX: Try our best to terminate the connection. */
991 assuan_write_line (gpgsm->assuan_ctx, "BYE");
997 && line[0] == 'D' && line[1] == ' '
1000 /* We are using the colon handler even for plain inline data
1001 - strange name for that function but for historic reasons
1003 /* FIXME We can't use this for binary data because we
1004 assume this is a string. For the current usage of colon
1005 output it is correct. */
1006 unsigned char *src = line + 2;
1007 unsigned char *end = line + linelen;
1009 unsigned char **aline = &gpgsm->colon.attic.line;
1010 int *alinelen = &gpgsm->colon.attic.linelen;
1012 if (gpgsm->colon.attic.linesize
1013 < *alinelen + linelen + 1)
1015 unsigned char *newline = xtryrealloc (*aline,
1016 *alinelen + linelen + 1);
1018 return mk_error (Out_Of_Core);
1020 gpgsm->colon.attic.linesize += linelen + 1;
1023 dst = *aline + *alinelen;
1027 if (*src == '%' && src + 2 < end)
1029 /* Handle escaped characters. */
1031 *dst = xtoi_2 (src);
1043 /* Terminate the pending line, pass it to the colon
1044 handler and reset it. */
1046 if (*alinelen > 1 && *(dst - 1) == '\r')
1050 /* FIXME How should we handle the return code? */
1051 gpgsm->colon.fnc (gpgsm->colon.fnc_value, *aline);
1059 else if (linelen > 2
1060 && line[0] == 'S' && line[1] == ' ')
1062 struct status_table_s t, *r;
1065 rest = strchr (line + 2, ' ');
1067 rest = line + linelen; /* set to an empty string */
1072 r = bsearch (&t, status_table, DIM(status_table) - 1,
1073 sizeof t, status_cmp);
1077 if (gpgsm->status.fnc)
1078 gpgsm->status.fnc (gpgsm->status.fnc_value, r->code, rest);
1081 fprintf (stderr, "[UNKNOWN STATUS]%s %s", t.name, rest);
1084 while (assuan_pending_line (gpgsm->assuan_ctx));
1091 _gpgme_gpgsm_set_status_handler (GpgsmObject gpgsm,
1092 GpgStatusHandler fnc, void *fnc_value)
1096 gpgsm->status.fnc = fnc;
1097 gpgsm->status.fnc_value = fnc_value;
1102 _gpgme_gpgsm_set_colon_line_handler (GpgsmObject gpgsm,
1103 GpgColonLineHandler fnc, void *fnc_value)
1107 gpgsm->colon.fnc = fnc;
1108 gpgsm->colon.fnc_value = fnc_value;
1113 _gpgme_gpgsm_start (GpgsmObject gpgsm, void *opaque)
1121 return mk_error (Invalid_Value);
1123 pid = assuan_get_pid (gpgsm->assuan_ctx);
1125 /* We need to know the fd used by assuan for reads. We do this by
1126 using the assumption that the first returned fd from
1127 assuan_get_active_fds() is always this one. */
1128 nfds = assuan_get_active_fds (gpgsm->assuan_ctx, 0 /* read fds */,
1129 fdlist, DIM (fdlist));
1131 return mk_error (General_Error); /* FIXME */
1132 err = _gpgme_register_pipe_handler (opaque, gpgsm_status_handler, gpgsm, pid,
1136 if (gpgsm->input_fd != -1)
1138 err = _gpgme_register_pipe_handler (opaque, _gpgme_data_outbound_handler,
1139 gpgsm->input_data, pid,
1140 gpgsm->input_fd, 0);
1141 if (!err) /* FIXME Kludge around poll() problem. */
1142 err = _gpgme_io_set_nonblocking (gpgsm->input_fd);
1144 if (!err && gpgsm->output_fd != -1)
1145 err = _gpgme_register_pipe_handler (opaque, _gpgme_data_inbound_handler,
1146 gpgsm->output_data, pid,
1147 gpgsm->output_fd, 1);
1148 if (!err && gpgsm->message_fd != -1)
1150 err = _gpgme_register_pipe_handler (opaque, _gpgme_data_outbound_handler,
1151 gpgsm->message_data, pid,
1152 gpgsm->message_fd, 0);
1153 if (!err) /* FIXME Kludge around poll() problem. */
1154 err = _gpgme_io_set_nonblocking (gpgsm->message_fd);
1158 err = assuan_write_line (gpgsm->assuan_ctx, gpgsm->command);
1164 #else /* ENABLE_GPGSM */
1170 #include "engine-gpgsm.h"
1174 _gpgme_gpgsm_get_version (void)
1181 _gpgme_gpgsm_check_version (void)
1183 return mk_error (Invalid_Engine);
1188 _gpgme_gpgsm_new (GpgsmObject *r_gpgsm)
1190 return mk_error (Invalid_Engine);
1195 _gpgme_gpgsm_release (GpgsmObject gpgsm)
1202 _gpgme_gpgsm_set_status_handler (GpgsmObject gpgsm,
1203 GpgStatusHandler fnc, void *fnc_value)
1210 _gpgme_gpgsm_op_decrypt (GpgsmObject gpgsm, GpgmeData ciph, GpgmeData plain)
1212 return mk_error (Invalid_Engine);
1217 _gpgme_gpgsm_op_delete (GpgsmObject gpgsm, GpgmeKey key, int allow_secret)
1219 return mk_error (Invalid_Engine);
1224 _gpgme_gpgsm_op_encrypt (GpgsmObject gpgsm, GpgmeRecipients recp,
1225 GpgmeData plain, GpgmeData ciph, int use_armor)
1227 return mk_error (Invalid_Engine);
1232 _gpgme_gpgsm_op_export (GpgsmObject gpgsm, GpgmeRecipients recp,
1233 GpgmeData keydata, int use_armor)
1235 return mk_error (Invalid_Engine);
1240 _gpgme_gpgsm_op_genkey (GpgsmObject gpgsm, GpgmeData help_data, int use_armor,
1241 GpgmeData pubkey, GpgmeData seckey)
1243 return mk_error (Invalid_Engine);
1248 _gpgme_gpgsm_op_import (GpgsmObject gpgsm, GpgmeData keydata)
1250 return mk_error (Invalid_Engine);
1255 _gpgme_gpgsm_op_keylist (GpgsmObject gpgsm, const char *pattern,
1256 int secret_only, int keylist_mode)
1258 return mk_error (Invalid_Engine);
1263 _gpgme_gpgsm_op_keylist_ext (GpgsmObject gpgsm, const char *pattern[],
1264 int secret_only, int reserved, int keylist_mode)
1266 return mk_error (Invalid_Engine);
1270 _gpgme_gpgsm_op_sign (GpgsmObject gpgsm, GpgmeData in, GpgmeData out,
1271 GpgmeSigMode mode, int use_armor,
1272 int use_textmode, int include_certs,
1273 GpgmeCtx ctx /* FIXME */)
1275 return mk_error (Invalid_Engine);
1280 _gpgme_gpgsm_op_trustlist (GpgsmObject gpgsm, const char *pattern)
1282 return mk_error (Invalid_Engine);
1287 _gpgme_gpgsm_op_verify (GpgsmObject gpgsm, GpgmeData sig, GpgmeData text)
1289 return mk_error (Invalid_Engine);
1294 _gpgme_gpgsm_set_colon_line_handler (GpgsmObject gpgsm,
1295 GpgColonLineHandler fnc, void *fnc_value)
1301 _gpgme_gpgsm_start (GpgsmObject gpgsm, void *opaque)
1303 return mk_error (Invalid_Engine);
1307 #endif /* ! ENABLE_GPGSM */