2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2004, 2005 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG 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 * GnuPG 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
30 #ifdef USE_SHM_COPROCESSING
31 #ifdef USE_CAPABILITIES
32 #include <sys/capability.h>
35 #include <sys/types.h>
41 #if defined(HAVE_MLOCK)
51 #include "cipher.h" /* for progress functions */
53 #define CONTROL_D ('D' - 'A' + 1)
57 static FILE *statusfp
;
59 #ifdef USE_SHM_COPROCESSING
60 static int shm_id
= -1;
61 static volatile char *shm_area
;
62 static size_t shm_size
;
63 static int shm_is_locked
;
64 #endif /*USE_SHM_COPROCESSING*/
68 progress_cb ( void *ctx
, int c
)
73 sprintf ( buf
, "%.20s X 100 100", (char*)ctx
);
75 sprintf ( buf
, "%.20s %c 0 0", (char*)ctx
, c
);
76 write_status_text ( STATUS_PROGRESS
, buf
);
80 get_status_string ( int no
)
86 case STATUS_ENTER
: s
= "ENTER"; break;
87 case STATUS_LEAVE
: s
= "LEAVE"; break;
88 case STATUS_ABORT
: s
= "ABORT"; break;
89 case STATUS_NEWSIG
: s
= "NEWSIG"; break;
90 case STATUS_GOODSIG
: s
= "GOODSIG"; break;
91 case STATUS_KEYEXPIRED
: s
= "KEYEXPIRED"; break;
92 case STATUS_KEYREVOKED
: s
= "KEYREVOKED"; break;
93 case STATUS_BADSIG
: s
= "BADSIG"; break;
94 case STATUS_ERRSIG
: s
= "ERRSIG"; break;
95 case STATUS_BADARMOR
: s
= "BADARMOR"; break;
96 case STATUS_RSA_OR_IDEA
: s
= "RSA_OR_IDEA"; break;
97 case STATUS_TRUST_UNDEFINED
: s
= "TRUST_UNDEFINED"; break;
98 case STATUS_TRUST_NEVER
: s
= "TRUST_NEVER"; break;
99 case STATUS_TRUST_MARGINAL
: s
= "TRUST_MARGINAL"; break;
100 case STATUS_TRUST_FULLY
: s
= "TRUST_FULLY"; break;
101 case STATUS_TRUST_ULTIMATE
: s
= "TRUST_ULTIMATE"; break;
102 case STATUS_GET_BOOL
: s
= "GET_BOOL"; break;
103 case STATUS_GET_LINE
: s
= "GET_LINE"; break;
104 case STATUS_GET_HIDDEN
: s
= "GET_HIDDEN"; break;
105 case STATUS_GOT_IT
: s
= "GOT_IT"; break;
106 case STATUS_SHM_INFO
: s
= "SHM_INFO"; break;
107 case STATUS_SHM_GET
: s
= "SHM_GET"; break;
108 case STATUS_SHM_GET_BOOL
: s
= "SHM_GET_BOOL"; break;
109 case STATUS_SHM_GET_HIDDEN
: s
= "SHM_GET_HIDDEN"; break;
110 case STATUS_NEED_PASSPHRASE
: s
= "NEED_PASSPHRASE"; break;
111 case STATUS_VALIDSIG
: s
= "VALIDSIG"; break;
112 case STATUS_SIG_ID
: s
= "SIG_ID"; break;
113 case STATUS_ENC_TO
: s
= "ENC_TO"; break;
114 case STATUS_NODATA
: s
= "NODATA"; break;
115 case STATUS_BAD_PASSPHRASE
: s
= "BAD_PASSPHRASE"; break;
116 case STATUS_NO_PUBKEY
: s
= "NO_PUBKEY"; break;
117 case STATUS_NO_SECKEY
: s
= "NO_SECKEY"; break;
118 case STATUS_NEED_PASSPHRASE_SYM
: s
= "NEED_PASSPHRASE_SYM"; break;
119 case STATUS_NEED_PASSPHRASE_PIN
: s
= "NEED_PASSPHRASE_PIN"; break;
120 case STATUS_DECRYPTION_FAILED
: s
= "DECRYPTION_FAILED"; break;
121 case STATUS_DECRYPTION_OKAY
: s
= "DECRYPTION_OKAY"; break;
122 case STATUS_MISSING_PASSPHRASE
: s
= "MISSING_PASSPHRASE"; break;
123 case STATUS_GOOD_PASSPHRASE
: s
= "GOOD_PASSPHRASE"; break;
124 case STATUS_GOODMDC
: s
= "GOODMDC"; break;
125 case STATUS_BADMDC
: s
= "BADMDC"; break;
126 case STATUS_ERRMDC
: s
= "ERRMDC"; break;
127 case STATUS_IMPORTED
: s
= "IMPORTED"; break;
128 case STATUS_IMPORT_OK
: s
= "IMPORT_OK"; break;
129 case STATUS_IMPORT_CHECK
: s
= "IMPORT_CHECK"; break;
130 case STATUS_IMPORT_RES
: s
= "IMPORT_RES"; break;
131 case STATUS_FILE_START
: s
= "FILE_START"; break;
132 case STATUS_FILE_DONE
: s
= "FILE_DONE"; break;
133 case STATUS_FILE_ERROR
: s
= "FILE_ERROR"; break;
134 case STATUS_BEGIN_DECRYPTION
:s
= "BEGIN_DECRYPTION"; break;
135 case STATUS_END_DECRYPTION
: s
= "END_DECRYPTION"; break;
136 case STATUS_BEGIN_ENCRYPTION
:s
= "BEGIN_ENCRYPTION"; break;
137 case STATUS_END_ENCRYPTION
: s
= "END_ENCRYPTION"; break;
138 case STATUS_DELETE_PROBLEM
: s
= "DELETE_PROBLEM"; break;
139 case STATUS_PROGRESS
: s
= "PROGRESS"; break;
140 case STATUS_SIG_CREATED
: s
= "SIG_CREATED"; break;
141 case STATUS_SESSION_KEY
: s
= "SESSION_KEY"; break;
142 case STATUS_NOTATION_NAME
: s
= "NOTATION_NAME" ; break;
143 case STATUS_NOTATION_DATA
: s
= "NOTATION_DATA" ; break;
144 case STATUS_POLICY_URL
: s
= "POLICY_URL" ; break;
145 case STATUS_BEGIN_STREAM
: s
= "BEGIN_STREAM"; break;
146 case STATUS_END_STREAM
: s
= "END_STREAM"; break;
147 case STATUS_KEY_CREATED
: s
= "KEY_CREATED"; break;
148 case STATUS_KEY_NOT_CREATED
: s
= "KEY_NOT_CREATED"; break;
149 case STATUS_USERID_HINT
: s
= "USERID_HINT"; break;
150 case STATUS_UNEXPECTED
: s
= "UNEXPECTED"; break;
151 case STATUS_INV_RECP
: s
= "INV_RECP"; break;
152 case STATUS_NO_RECP
: s
= "NO_RECP"; break;
153 case STATUS_ALREADY_SIGNED
: s
= "ALREADY_SIGNED"; break;
154 case STATUS_SIGEXPIRED
: s
= "SIGEXPIRED deprecated-use-keyexpired-instead"; break;
155 case STATUS_EXPSIG
: s
= "EXPSIG"; break;
156 case STATUS_EXPKEYSIG
: s
= "EXPKEYSIG"; break;
157 case STATUS_REVKEYSIG
: s
= "REVKEYSIG"; break;
158 case STATUS_ATTRIBUTE
: s
= "ATTRIBUTE"; break;
159 case STATUS_CARDCTRL
: s
= "CARDCTRL"; break;
160 case STATUS_PLAINTEXT
: s
= "PLAINTEXT"; break;
161 case STATUS_PLAINTEXT_LENGTH
:s
= "PLAINTEXT_LENGTH"; break;
162 case STATUS_SIG_SUBPACKET
: s
= "SIG_SUBPACKET"; break;
163 case STATUS_SC_OP_SUCCESS
: s
= "SC_OP_SUCCESS"; break;
164 case STATUS_SC_OP_FAILURE
: s
= "SC_OP_FAILURE"; break;
165 case STATUS_BACKUP_KEY_CREATED
:s
="BACKUP_KEY_CREATED"; break;
166 case STATUS_PKA_TRUST_BAD
: s
= "PKA_TRUST_BAD"; break;
167 case STATUS_PKA_TRUST_GOOD
: s
= "PKA_TRUST_GOOD"; break;
168 case STATUS_BEGIN_SIGNING
: s
= "BEGIN_SIGNING"; break;
169 default: s
= "?"; break;
175 /* Return true if the status message NO may currently be issued. We
176 need this to avoid syncronisation problem while auto retrieving a
177 key. There it may happen that a status NODATA is issued for a non
178 available key and the user may falsely interpret this has a missing
181 status_currently_allowed (int no
)
183 if (!glo_ctrl
.in_auto_key_retrieve
)
186 /* We allow some statis anyway, so that import statistics are
187 correct and to avoid problems if the retriebval subsystem will
191 case STATUS_GET_BOOL
:
192 case STATUS_GET_LINE
:
193 case STATUS_GET_HIDDEN
:
195 case STATUS_IMPORTED
:
196 case STATUS_IMPORT_OK
:
197 case STATUS_IMPORT_CHECK
:
198 case STATUS_IMPORT_RES
:
208 set_status_fd ( int fd
)
210 static int last_fd
= -1;
212 if ( fd
!= -1 && last_fd
== fd
)
215 if ( statusfp
&& statusfp
!= stdout
&& statusfp
!= stderr
)
226 statusfp
= fdopen( fd
, "w" );
228 log_fatal("can't open fd %d for status output: %s\n",
229 fd
, strerror(errno
));
232 register_primegen_progress ( progress_cb
, "primegen" );
233 register_pk_dsa_progress ( progress_cb
, "pk_dsa" );
234 register_pk_elg_progress ( progress_cb
, "pk_elg" );
244 write_status ( int no
)
246 write_status_text( no
, NULL
);
250 write_status_text ( int no
, const char *text
)
252 if( !statusfp
|| !status_currently_allowed (no
) )
253 return; /* Not enabled or allowed. */
255 fputs ( "[GNUPG:] ", statusfp
);
256 fputs ( get_status_string (no
), statusfp
);
258 putc ( ' ', statusfp
);
259 for (; *text
; text
++) {
261 fputs ( "\\n", statusfp
);
262 else if (*text
== '\r')
263 fputs ( "\\r", statusfp
);
265 putc ( *(const byte
*)text
, statusfp
);
268 putc ('\n',statusfp
);
269 if ( fflush (statusfp
) && opt
.exit_on_status_write_error
)
275 * Write a status line with a buffer using %XX escapes. If WRAP is >
276 * 0 wrap the line after this length. If STRING is not NULL it will
277 * be prepended to the buffer, no escaping is done for string.
278 * A wrap of -1 forces spaces not to be encoded as %20.
281 write_status_text_and_buffer ( int no
, const char *string
,
282 const char *buffer
, size_t len
, int wrap
)
284 const char *s
, *text
;
286 int lower_limit
= ' ';
287 size_t n
, count
, dowrap
;
289 if( !statusfp
|| !status_currently_allowed (no
) )
290 return; /* Not enabled or allowed. */
297 text
= get_status_string (no
);
298 count
= dowrap
= first
= 1;
301 fprintf (statusfp
, "[GNUPG:] %s ", text
);
303 if (first
&& string
) {
304 fputs (string
, statusfp
);
305 count
+= strlen (string
);
309 for (esc
=0, s
=buffer
, n
=len
; n
&& !esc
; s
++, n
-- ) {
310 if ( *s
== '%' || *(const byte
*)s
<= lower_limit
311 || *(const byte
*)s
== 127 )
313 if ( wrap
&& ++count
> wrap
) {
322 fwrite (buffer
, s
-buffer
, 1, statusfp
);
324 fprintf (statusfp
, "%%%02X", *(const byte
*)s
);
330 putc ( '\n', statusfp
);
333 putc ('\n',statusfp
);
334 if ( fflush (statusfp
) && opt
.exit_on_status_write_error
)
339 write_status_buffer ( int no
, const char *buffer
, size_t len
, int wrap
)
341 write_status_text_and_buffer (no
, NULL
, buffer
, len
, wrap
);
345 #ifdef USE_SHM_COPROCESSING
347 #ifndef IPC_RMID_DEFERRED_RELEASE
352 shmctl ( shm_id
, IPC_RMID
, 0);
359 init_shm_coprocessing ( ulong requested_shm_size
, int lock_mem
)
362 struct shmid_ds shmds
;
364 #ifndef IPC_RMID_DEFERRED_RELEASE
365 atexit( remove_shmid
);
367 requested_shm_size
= (requested_shm_size
+ 4095) & ~4095;
368 if ( requested_shm_size
> 2 * 4096 )
369 log_fatal("too much shared memory requested; only 8k are allowed\n");
370 shm_size
= 4096 /* one page for us */ + requested_shm_size
;
372 shm_id
= shmget( IPC_PRIVATE
, shm_size
, IPC_CREAT
| 0700 );
374 log_fatal("can't get %uk of shared memory: %s\n",
375 (unsigned)shm_size
/1024, strerror(errno
));
377 #if !defined(IPC_HAVE_SHM_LOCK) \
378 && defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
379 /* part of the old code which uses mlock */
380 shm_area
= shmat( shm_id
, 0, 0 );
381 if ( shm_area
== (char*)-1 )
382 log_fatal("can't attach %uk shared memory: %s\n",
383 (unsigned)shm_size
/1024, strerror(errno
));
384 log_debug("mapped %uk shared memory at %p, id=%d\n",
385 (unsigned)shm_size
/1024, shm_area
, shm_id
);
387 #ifdef USE_CAPABILITIES
388 cap_set_proc( cap_from_text("cap_ipc_lock+ep") );
390 /* (need the cast for Solaris with Sun's workshop compilers) */
391 if ( mlock ( (char*)shm_area
, shm_size
) )
392 log_info("locking shared memory %d failed: %s\n",
393 shm_id
, strerror(errno
));
396 #ifdef USE_CAPABILITIES
397 cap_set_proc( cap_from_text("cap_ipc_lock+p") );
401 #ifdef IPC_RMID_DEFERRED_RELEASE
402 if( shmctl( shm_id
, IPC_RMID
, 0) )
403 log_fatal("shmctl IPC_RMDID of %d failed: %s\n",
404 shm_id
, strerror(errno
));
407 if( shmctl( shm_id
, IPC_STAT
, &shmds
) )
408 log_fatal("shmctl IPC_STAT of %d failed: %s\n",
409 shm_id
, strerror(errno
));
410 if( shmds
.shm_perm
.uid
!= getuid() ) {
411 shmds
.shm_perm
.uid
= getuid();
412 if( shmctl( shm_id
, IPC_SET
, &shmds
) )
413 log_fatal("shmctl IPC_SET of %d failed: %s\n",
414 shm_id
, strerror(errno
));
417 #else /* this is the new code which handles the changes in the SHM
418 * semantics introduced with Linux 2.4. The changes is that we
419 * now change the permissions and then attach to the memory.
423 #ifdef USE_CAPABILITIES
424 cap_set_proc( cap_from_text("cap_ipc_lock+ep") );
426 #ifdef IPC_HAVE_SHM_LOCK
427 if ( shmctl (shm_id
, SHM_LOCK
, 0) )
428 log_info("locking shared memory %d failed: %s\n",
429 shm_id
, strerror(errno
));
433 log_info("Locking shared memory %d failed: No way to do it\n", shm_id
);
435 #ifdef USE_CAPABILITIES
436 cap_set_proc( cap_from_text("cap_ipc_lock+p") );
440 if( shmctl( shm_id
, IPC_STAT
, &shmds
) )
441 log_fatal("shmctl IPC_STAT of %d failed: %s\n",
442 shm_id
, strerror(errno
));
443 if( shmds
.shm_perm
.uid
!= getuid() ) {
444 shmds
.shm_perm
.uid
= getuid();
445 if( shmctl( shm_id
, IPC_SET
, &shmds
) )
446 log_fatal("shmctl IPC_SET of %d failed: %s\n",
447 shm_id
, strerror(errno
));
450 shm_area
= shmat( shm_id
, 0, 0 );
451 if ( shm_area
== (char*)-1 )
452 log_fatal("can't attach %uk shared memory: %s\n",
453 (unsigned)shm_size
/1024, strerror(errno
));
454 log_debug("mapped %uk shared memory at %p, id=%d\n",
455 (unsigned)shm_size
/1024, shm_area
, shm_id
);
457 #ifdef IPC_RMID_DEFERRED_RELEASE
458 if( shmctl( shm_id
, IPC_RMID
, 0) )
459 log_fatal("shmctl IPC_RMDID of %d failed: %s\n",
460 shm_id
, strerror(errno
));
464 /* write info; Protocol version, id, size, locked size */
465 sprintf( buf
, "pv=1 pid=%d shmid=%d sz=%u lz=%u", (int)getpid(),
466 shm_id
, (unsigned)shm_size
, shm_is_locked
? (unsigned)shm_size
:0 );
467 write_status_text( STATUS_SHM_INFO
, buf
);
471 * Request a string from client
472 * If bool, returns static string on true (do not free) or NULL for false
475 do_shm_get( const char *keyword
, int hidden
, int bool )
484 shm_area
[0] = 0; /* msb of length of control block */
485 shm_area
[1] = 32; /* and lsb */
486 shm_area
[2] = 1; /* indicate that we are waiting on a reply */
487 shm_area
[3] = 0; /* clear data available flag */
489 write_status_text( bool? STATUS_SHM_GET_BOOL
:
490 hidden
? STATUS_SHM_GET_HIDDEN
: STATUS_SHM_GET
, keyword
);
494 if( shm_area
[0] || shm_area
[1] != 32 || shm_area
[2] != 1 )
495 log_fatal("client modified shm control block - abort\n");
496 } while( !shm_area
[3] );
497 shm_area
[2] = 0; /* reset request flag */
498 p
= (byte
*)shm_area
+32;
499 n
= p
[0] << 8 | p
[1];
501 if( n
+32+2+1 > 4095 )
502 log_fatal("client returns too large data (%u bytes)\n", (unsigned)n
);
505 return p
[0]? "" : NULL
;
507 string
= hidden
? xmalloc_secure( n
+1 ) : xmalloc( n
+1 );
508 memcpy(string
, p
, n
);
509 string
[n
] = 0; /* make sure it is a string */
510 if( hidden
) /* invalidate the memory */
516 #endif /* USE_SHM_COPROCESSING */
519 myread(int fd
, void *buf
, size_t count
)
523 rc
= read( fd
, buf
, count
);
524 } while ( rc
== -1 && errno
== EINTR
);
525 if ( !rc
&& count
) {
526 static int eof_emmited
=0;
527 if ( eof_emmited
< 3 ) {
528 *(char*)buf
= CONTROL_D
;
532 else { /* Ctrl-D not caught - do something reasonable */
533 #ifdef HAVE_DOSISH_SYSTEM
534 raise (SIGINT
); /* nothing to hangup under DOS */
536 raise (SIGHUP
); /* no more input data */
546 * Request a string from the client over the command-fd
547 * If bool, returns static string on true (do not free) or NULL for false
550 do_get_from_fd( const char *keyword
, int hidden
, int bool )
558 write_status_text( bool? STATUS_GET_BOOL
:
559 hidden
? STATUS_GET_HIDDEN
: STATUS_GET_LINE
, keyword
);
561 for( string
= NULL
, i
= len
= 200; ; i
++ ) {
565 string
= hidden
? xmalloc_secure ( len
) : xmalloc ( len
);
567 memcpy(string
, save
, i
);
571 /* Hmmm: why not use our read_line function here */
572 if( myread( opt
.command_fd
, string
+i
, 1) != 1 || string
[i
] == '\n' )
574 else if ( string
[i
] == CONTROL_D
) {
575 /* found ETX - cancel the line and return a sole ETX */
576 string
[0] = CONTROL_D
;
583 write_status( STATUS_GOT_IT
);
585 if( bool ) /* Fixme: is this correct??? */
586 return (string
[0] == 'Y' || string
[0] == 'y') ? "" : NULL
;
596 if( opt
.command_fd
!= -1 )
598 #ifdef USE_SHM_COPROCESSING
599 if( opt
.shm_coprocess
)
606 cpr_get_no_help( const char *keyword
, const char *prompt
)
610 if( opt
.command_fd
!= -1 )
611 return do_get_from_fd ( keyword
, 0, 0 );
612 #ifdef USE_SHM_COPROCESSING
613 if( opt
.shm_coprocess
)
614 return do_shm_get( keyword
, 0, 0 );
617 p
= tty_get( prompt
);
623 cpr_get( const char *keyword
, const char *prompt
)
627 if( opt
.command_fd
!= -1 )
628 return do_get_from_fd ( keyword
, 0, 0 );
629 #ifdef USE_SHM_COPROCESSING
630 if( opt
.shm_coprocess
)
631 return do_shm_get( keyword
, 0, 0 );
634 p
= tty_get( prompt
);
635 if( *p
=='?' && !p
[1] && !(keyword
&& !*keyword
)) {
637 display_online_help( keyword
);
646 cpr_get_utf8( const char *keyword
, const char *prompt
)
649 p
= cpr_get( keyword
, prompt
);
651 char *utf8
= native_to_utf8( p
);
659 cpr_get_hidden( const char *keyword
, const char *prompt
)
663 if( opt
.command_fd
!= -1 )
664 return do_get_from_fd ( keyword
, 1, 0 );
665 #ifdef USE_SHM_COPROCESSING
666 if( opt
.shm_coprocess
)
667 return do_shm_get( keyword
, 1, 0 );
670 p
= tty_get_hidden( prompt
);
671 if( *p
== '?' && !p
[1] ) {
673 display_online_help( keyword
);
681 cpr_kill_prompt(void)
683 if( opt
.command_fd
!= -1 )
685 #ifdef USE_SHM_COPROCESSING
686 if( opt
.shm_coprocess
)
694 cpr_get_answer_is_yes( const char *keyword
, const char *prompt
)
699 if( opt
.command_fd
!= -1 )
700 return !!do_get_from_fd ( keyword
, 0, 1 );
701 #ifdef USE_SHM_COPROCESSING
702 if( opt
.shm_coprocess
)
703 return !!do_shm_get( keyword
, 0, 1 );
706 p
= tty_get( prompt
);
707 trim_spaces(p
); /* it is okay to do this here */
708 if( *p
== '?' && !p
[1] ) {
710 display_online_help( keyword
);
714 yes
= answer_is_yes(p
);
722 cpr_get_answer_yes_no_quit( const char *keyword
, const char *prompt
)
727 if( opt
.command_fd
!= -1 )
728 return !!do_get_from_fd ( keyword
, 0, 1 );
729 #ifdef USE_SHM_COPROCESSING
730 if( opt
.shm_coprocess
)
731 return !!do_shm_get( keyword
, 0, 1 );
734 p
= tty_get( prompt
);
735 trim_spaces(p
); /* it is okay to do this here */
736 if( *p
== '?' && !p
[1] ) {
738 display_online_help( keyword
);
742 yes
= answer_is_yes_no_quit(p
);
751 cpr_get_answer_okay_cancel (const char *keyword
,
759 if( opt
.command_fd
!= -1 )
760 answer
= do_get_from_fd ( keyword
, 0, 0 );
761 #ifdef USE_SHM_COPROCESSING
762 else if( opt
.shm_coprocess
)
763 answer
= do_shm_get( keyword
, 0, 0 );
768 yes
= answer_is_okay_cancel (answer
, def_answer
);
775 p
= tty_get( prompt
);
776 trim_spaces(p
); /* it is okay to do this here */
777 if (*p
== '?' && !p
[1])
780 display_online_help (keyword
);
785 yes
= answer_is_okay_cancel (p
, def_answer
);