1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
38 #include <glib/gprintf.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/resource.h>
51 #ifdef HAVE_GETOPT_LONG
56 #include "getopt_long.h"
74 #include "pwmd_error.h"
80 GCRY_THREAD_OPTION_PTH_IMPL
;
81 ASSUAN_SYSTEM_PTH_IMPL
;
83 static void clear_rcfile_keys()
89 groups
= g_key_file_get_groups(keyfileh
, &n
);
91 for (p
= groups
; *p
; p
++) {
94 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
)
95 g_key_file_set_string(keyfileh
, *p
, "key", "");
101 static void *reload_rcfile_thread(void *arg
)
103 gboolean b
= disable_list_and_dump
;
105 pth_attr_t attr
= pth_attr_of(pth_self());
107 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
108 pth_attr_destroy(attr
);
109 MUTEX_LOCK(&rcfile_mutex
);
110 log_write(N_("reloading configuration file '%s'"), rcfile
);
111 k
= parse_rcfile(FALSE
);
116 g_key_file_free(keyfileh
);
120 disable_list_and_dump
= b
;
121 startStopKeepAlive(FALSE
);
122 send_status_all(STATUS_CONFIG
);
124 MUTEX_UNLOCK(&rcfile_mutex
);
128 static void reload_rcfile()
131 pth_attr_t attr
= pth_attr_new();
135 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, 0);
136 tid
= pth_spawn(attr
, reload_rcfile_thread
, NULL
);
138 pth_attr_destroy(attr
);
141 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
142 _gpg_strerror(gpg_error_from_errno(n
)));
145 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
147 gpg_error_t n
= gpg_error_from_errno(e
);
148 struct client_s
*client
= assuan_get_pointer(ctx
);
151 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, _gpg_strerror(n
)));
154 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
156 gpg_err_code_t n
= gpg_err_code(e
);
157 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
158 struct client_s
*client
= assuan_get_pointer(ctx
);
164 return assuan_process_done(ctx
, 0);
167 log_write("%s", pwmd_strerror(e
));
171 if (n
== EPWMD_LIBXML_ERROR
) {
172 xmlErrorPtr xe
= client
->xml_error
;
175 xe
= xmlGetLastError();
177 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
178 log_write("%s", xe
->message
);
180 if (xe
== client
->xml_error
)
185 client
->xml_error
= NULL
;
189 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
192 void log_write(const gchar
*fmt
, ...)
202 pth_t tid
= pth_self();
205 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
208 if (!cmdline
&& logfile
) {
209 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
217 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
228 fprintf(stderr
, "%s\n", args
);
234 attr
= pth_attr_of(tid
);
236 if (pth_attr_get(attr
, PTH_ATTR_NAME
, &name
) == FALSE
)
239 pth_attr_destroy(attr
);
240 name
= print_fmt(buf
, sizeof(buf
), "%s(%p): ", name
, tid
);
242 if (!cmdline
&& log_syslog
== TRUE
)
243 syslog(LOG_INFO
, "%s%s", name
, args
);
246 tm
= localtime(&now
);
247 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
248 tbuf
[sizeof(tbuf
) - 1] = 0;
250 if (args
[strlen(args
)-1] == '\n')
251 args
[strlen(args
)-1] = 0;
253 line
= g_strdup_printf("%s %i %s%s\n", tbuf
, getpid(), name
, args
);
264 pth_write(fd
, line
, strlen(line
));
269 if (isatty(STDERR_FILENO
)) {
270 fprintf(stderr
, "%s", line
);
277 static void usage(gchar
*pn
, gint rc
)
279 g_fprintf(rc
== EXIT_FAILURE
? stderr
: stdout
, N_(
280 "Usage: %s [options] [file1] [...]\n"
282 " run as a foreground process\n"
283 " --rcfile/-f <filename>\n"
284 " load the specified rcfile (~/.pwmd/config)\n"
285 " --convert/-C <filename>\n"
286 " convert a version 1 data file to version 2\n"
287 " --import/-I <filename>\n"
288 " import an XML file\n"
290 " encrypt with the specified number of iterations when importing\n"
291 " (default is in the rcfile \"global\" section)\n"
292 " --key-file/-k <filename>\n"
293 " obtain the key from the specified file when importing or converting\n"
294 " --outfile/-o <filename>\n"
295 " output file to use when importing or converting (- for stdout)\n"
296 " --disable-dump/-D\n"
297 " disable use of the LIST, XPATH and DUMP commands\n"
298 " --no-pinentry/-P\n"
299 " disable use of pinentry\n"
306 static void setup_gcrypt()
308 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pth
);
310 if (!gcry_check_version(GCRYPT_VERSION
))
311 errx(EXIT_FAILURE
, N_("gcry_check_version(): Incompatible libgcrypt. Wanted %s, got %s."), GCRYPT_VERSION
, gcry_check_version(NULL
));
313 gcry_set_allocation_handler(xmalloc
, xmalloc
, NULL
, xrealloc
, xfree
);
316 static gint
new_connection(struct client_s
*cl
)
321 rc
= assuan_new(&cl
->ctx
);
326 rc
= assuan_init_socket_server(cl
->ctx
, cl
->thd
->fd
,
327 ASSUAN_SOCKET_SERVER_ACCEPTED
);
332 assuan_set_pointer(cl
->ctx
, cl
);
333 ver
= g_strdup_printf("%s", PACKAGE_STRING
);
334 assuan_set_hello_line(cl
->ctx
, ver
);
336 rc
= register_commands(cl
->ctx
);
341 rc
= assuan_accept(cl
->ctx
);
347 gchar
*str
= get_key_file_string("global", "debug_file");
350 assuan_set_log_stream(cl
->ctx
, debugfp
);
359 log_write("%s", _gpg_strerror(rc
));
365 static void xml_error_cb(void *data
, xmlErrorPtr e
)
367 struct client_s
*client
= data
;
370 * Keep the first reported error as the one to show in the error
371 * description. Reset in send_error().
373 if (client
->xml_error
)
376 xmlCopyError(e
, client
->xml_error
);
379 void close_file_header(file_header_internal_t
*fh
)
384 if (fh
->fd
!= -1 || (cmdline
== TRUE
&& fh
->fd
!= STDOUT_FILENO
))
393 void cleanup_crypto(struct client_crypto_s
**c
)
395 struct client_crypto_s
*cr
= *c
;
416 gcry_free(cr
->tkey2
);
421 gcry_free(cr
->inbuf
);
426 gcry_free(cr
->outbuf
);
430 close_file_header(cr
->fh
);
434 gcry_cipher_close(cr
->gh
);
442 * This is called after a child_thread terminates. Set with
443 * pth_cleanup_push().
445 static void cleanup_cb(void *arg
)
447 struct client_thread_s
*cn
= arg
;
448 struct client_s
*cl
= cn
->cl
;
450 MUTEX_LOCK(&cn_mutex
);
451 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
452 MUTEX_UNLOCK(&cn_mutex
);
455 MUTEX_LOCK(&cn
->mp_mutex
);
456 pth_cancel(cn
->msg_tid
);
457 MUTEX_UNLOCK(&cn
->mp_mutex
);
461 while (pth_msgport_pending(cn
->mp
)) {
462 pth_message_t
*msg
= pth_msgport_get(cn
->mp
);
468 pth_msgport_destroy(cn
->mp
);
482 assuan_release(cl
->ctx
);
483 else if (cl
->thd
&& cl
->thd
->fd
!= -1)
488 cleanup_pinentry(cl
->pinentry
);
492 cleanup_crypto(&cl
->crypto
);
496 log_write(N_("exiting, fd=%i"), cn
->fd
);
498 send_status_all(STATUS_CLIENTS
);
502 * Called every time a connection is made from init_new_connection(). This is
503 * the thread entry point.
505 static void *client_thread(void *data
)
507 struct client_thread_s
*thd
= data
;
508 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
514 * Prevent a race condition with init_new_connection() if this thread
515 * fails (returns) for some reason before init_new_connection() releases
518 MUTEX_LOCK(&cn_mutex
);
519 MUTEX_UNLOCK(&cn_mutex
);
520 pth_cleanup_push(cleanup_cb
, thd
);
523 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
527 attr
= pth_attr_of(pth_self());
528 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
529 pth_attr_destroy(attr
);
533 if (new_connection(cl
))
537 cl
->pinentry
= pinentry_init();
540 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
546 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
547 log_write("mlockall(): %s", strerror(errno
));
552 thd
->mp
= pth_msgport_create(NULL
);
553 pth_mutex_init(&thd
->mp_mutex
);
554 thd
->msg_tid
= pth_spawn(NULL
, client_msg_thread
, thd
);
558 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
559 _gpg_strerror(gpg_error_from_errno(n
)));
563 pth_yield(thd
->msg_tid
);
564 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
567 log_write("%s", _gpg_strerror(rc
));
571 send_status_all(STATUS_CLIENTS
);
572 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
576 pth_event_t pev
= NULL
;
578 pth_status_t st
, wst
;
579 pth_event_t wev
= NULL
;
580 pth_event_t rev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
,
582 pth_event_t ev
= rev
;
585 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
586 pev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->pinentry
->fd
);
587 ev
= pth_event_concat(ev
, pev
, NULL
);
591 if (cl
->inquire_status
== INQUIRE_BUSY
) {
592 wev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_WRITEABLE
, cl
->thd
->fd
);
593 ev
= pth_event_concat(ev
, wev
, NULL
);
596 pth_cleanup_push(cleanup_ev_cb
, ev
);
598 st
= pth_event_status(rev
);
599 wst
= pth_event_status(wev
);
601 if (st
== PTH_STATUS_OCCURRED
|| wst
== PTH_STATUS_OCCURRED
) {
604 rc
= assuan_process_next(cl
->ctx
, &ended
);
610 cl
->inquire_status
= INQUIRE_INIT
;
613 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
616 log_write("assuan_process_next(): %s", _gpg_strerror(rc
));
617 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
620 log_write("assuan_process_done(): %s", _gpg_strerror(rc
));
626 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
627 cl
->pinentry
->status
= PINENTRY_RUNNING
;
630 switch (cl
->inquire_status
) {
635 cl
->inquire_status
= INQUIRE_INIT
;
636 rc
= assuan_process_done(cl
->ctx
, 0);
644 st
= pth_event_status(pev
);
646 rc
= pinentry_iterate(cl
,
647 pev
&& cl
->pinentry
->fd
!= -1 && st
== PTH_STATUS_OCCURRED
);
653 * Client cleanup (including XML data) is done in cleanup_cb().
657 pth_exit(PTH_CANCELED
);
661 static void setup_logging(GKeyFile
*kf
)
663 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
666 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
672 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
678 logfile
= g_strdup(buf
);
688 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
692 * Make sure all settings are set to either the specified setting or a
695 static void set_rcfile_defaults(GKeyFile
*kf
)
699 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
700 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
701 g_key_file_set_string(kf
, "global", "socket_path", buf
);
704 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
705 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
706 g_key_file_set_string(kf
, "global", "data_directory", buf
);
709 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
710 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
712 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
713 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
714 g_key_file_set_string(kf
, "global", "log_path", buf
);
717 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
718 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
721 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
722 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
725 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
726 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
728 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
729 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0ULL)
730 g_key_file_set_integer(kf
, "global", "iterations", 1UL);
732 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
733 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
735 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
736 g_key_file_set_integer(kf
, "global", "iteration_progress", 0ULL);
738 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
739 g_key_file_set_integer(kf
, "global", "compression_level", 6);
741 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
742 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
744 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
745 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
747 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
749 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
750 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
753 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
756 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
757 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
759 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
760 g_key_file_set_integer(kf
, "global", "keepalive", DEFAULT_KEEPALIVE_TO
);
763 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
764 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
766 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
767 g_key_file_set_integer(kf
, "global", "pinentry_timeout",
768 DEFAULT_PIN_TIMEOUT
);
770 if (g_key_file_has_key(kf
, "global", "pinentry_path", NULL
) == FALSE
)
771 g_key_file_set_string(kf
, "global", "pinentry_path", PINENTRY_PATH
);
774 if (g_key_file_has_key(kf
, "global", "xfer_progress", NULL
) == FALSE
)
775 g_key_file_set_integer(kf
, "global", "xfer_progress", 8196);
777 if (g_key_file_has_key(kf
, "global", "cipher", NULL
) == FALSE
)
778 g_key_file_set_string(kf
, "global", "cipher", "AES256");
780 if (g_key_file_has_key(kf
, "global", "log_level", NULL
) == FALSE
)
781 g_key_file_set_integer(kf
, "global", "log_level", 0);
786 static GKeyFile
*parse_rcfile(gboolean specified
)
788 GKeyFile
*kf
= g_key_file_new();
791 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
792 log_write("%s: %s", rcfile
, rc
->message
);
794 if (cmdline
&& specified
) {
799 if (rc
->code
== G_FILE_ERROR_NOENT
) {
801 set_rcfile_defaults(kf
);
809 set_rcfile_defaults(kf
);
813 static gchar
*do_get_password(const gchar
*prompt
)
815 gchar buf
[LINE_MAX
] = {0}, *p
;
816 struct termios told
, tnew
;
819 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
820 log_write("tcgetattr(): %s", strerror(errno
));
824 memcpy(&tnew
, &told
, sizeof(struct termios
));
825 tnew
.c_lflag
&= ~(ECHO
);
826 tnew
.c_lflag
|= ICANON
|ECHONL
;
828 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
829 log_write("tcsetattr(): %s", strerror(errno
));
830 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
834 fprintf(stderr
, "%s", prompt
);
836 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
837 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
841 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
842 p
[strlen(p
) - 1] = 0;
845 key
= gcry_malloc(1);
849 key
= gcry_malloc(strlen(p
) + 1);
850 sprintf(key
, "%s", p
);
853 memset(&buf
, 0, sizeof(buf
));
857 /* Only used when "enable_pinentry" is "false" or -P. */
858 static gpg_error_t
get_input(const gchar
*filename
,
859 struct client_crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
863 if (which
== PINENTRY_SAVE
) {
864 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
865 crypto
->tkey
= do_get_password(prompt
);
869 log_write(N_("%s: Skipping file"), filename
);
870 return GPG_ERR_BAD_PASSPHRASE
;
873 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
874 crypto
->tkey2
= do_get_password(prompt
);
877 if (!crypto
->tkey2
) {
878 log_write(N_("%s: Skipping file"), filename
);
879 return GPG_ERR_BAD_PASSPHRASE
;
882 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
883 log_write(N_("%s: Passphrase mismatch"), filename
);
887 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
888 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
892 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
894 if ((crypto
->tkey
= do_get_password(prompt
)) == NULL
) {
895 log_write(N_("%s: Skipping file"), filename
);
897 return GPG_ERR_BAD_PASSPHRASE
;
900 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
901 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
907 * inbuf must have been allocated with gcry_malloc().
909 gpg_error_t
export_common(const gchar
*filename
, struct client_crypto_s
*crypto
,
910 gpointer inbuf
, gulong insize
)
917 rc
= update_save_flags(NULL
, crypto
);
922 level
= get_key_file_integer(filename
, "compression_level");
927 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
929 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
932 crypto
->inbuf
= outbuf
;
933 crypto
->insize
= outsize
;
934 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
938 static gpg_error_t
get_password(const gchar
*filename
,
939 struct client_crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
940 pinentry_cmd_t which
)
945 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
948 return get_input(filename
, crypto
, key
, which
);
952 gchar
*result
= NULL
;
953 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
955 pth_mutex_init(&pin
->status_mutex
);
956 set_pinentry_defaults(pin
);
958 pin
->filename
= g_strdup(filename
);
959 rc
= pinentry_getpin(pin
, &result
);
966 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, result
? strlen(result
) : 1);
968 cleanup_pinentry(pin
);
976 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
979 gchar buf
[LINE_MAX
] = {0}, *p
;
985 if ((fp
= fopen(file
, "r")) == NULL
) {
986 *rc
= gpg_error_from_syserror();
990 p
= fgets(buf
, sizeof(buf
), fp
);
994 if (len
&& buf
[len
- 1] == '\n')
997 str
= gcry_malloc(len
+ 1);
1000 *rc
= gpg_error_from_errno(ENOMEM
);
1004 memcpy(str
, buf
, len
? len
: 1);
1006 memset(&buf
, 0, sizeof(buf
));
1011 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
,
1015 gchar
*t
, *file
= NULL
, *str
;
1017 *rc
= GPG_ERR_UNKNOWN_ERRNO
;
1019 if (import
== FALSE
) {
1020 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1021 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1025 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1032 t
= expand_homedir(file
);
1035 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1036 *rc
= gpg_error_from_errno(ENOMEM
);
1045 /* -I or -C. The filename is a key file. */
1046 file
= g_strdup(filename
);
1049 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1050 *rc
= gpg_error_from_errno(ENOMEM
);
1056 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1064 if (_getline(file
, &str
, rc
) == FALSE
) {
1065 log_write("%s: %s: %s", filename
, file
, pwmd_strerror(*rc
));
1075 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1076 const gchar
*keyfile
, guint64 iter
)
1085 struct client_crypto_s
*crypto
;
1086 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1088 if (stat(filename
, &st
) == -1) {
1089 log_write("%s: %s", filename
, strerror(errno
));
1093 crypto
= init_client_crypto();
1098 crypto
->key
= gcry_malloc(hashlen
);
1099 memset(crypto
->key
, 0, hashlen
);
1102 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1106 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
1109 if (iter
&& keyfile
) {
1110 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1115 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1116 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1119 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
1125 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1126 log_write("%s: %s", filename
, strerror(errno
));
1130 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1132 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1136 if (pth_read(fd
, xmlbuf
, st
.st_size
) == -1) {
1140 log_write("%s: %s", filename
, strerror(errno
));
1145 xmlbuf
[st
.st_size
] = 0;
1148 * Make sure the document validates.
1150 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1151 log_write("xmlReadDoc() failed");
1157 xmlDocDumpMemory(doc
, &xml
, &len
);
1161 memset(crypto
->key
, '!', hashlen
);
1163 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1166 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1170 crypto
->fh
->ver
.fh2
.iter
= iter
;
1171 rc
= export_common(outfile
, crypto
, xml
, len
);
1175 send_error(NULL
, rc
);
1179 cleanup_crypto(&crypto
);
1183 cleanup_crypto(&crypto
);
1187 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1192 MUTEX_LOCK(&rcfile_mutex
);
1194 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1195 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1198 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1199 g_clear_error(&grc
);
1203 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1204 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1207 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1208 g_clear_error(&grc
);
1213 MUTEX_UNLOCK(&rcfile_mutex
);
1217 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1222 MUTEX_LOCK(&rcfile_mutex
);
1224 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1225 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
1228 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1229 g_clear_error(&grc
);
1233 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1234 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1237 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1238 g_clear_error(&grc
);
1243 MUTEX_UNLOCK(&rcfile_mutex
);
1247 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1249 gboolean val
= FALSE
;
1252 MUTEX_LOCK(&rcfile_mutex
);
1254 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
)
1256 val
= g_key_file_get_boolean(keyfileh
, section
? section
: "global",
1260 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1261 g_clear_error(&grc
);
1265 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1266 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1269 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1270 g_clear_error(&grc
);
1275 MUTEX_UNLOCK(&rcfile_mutex
);
1279 static gboolean
parse_rcfile_keys()
1286 groups
= g_key_file_get_groups(keyfileh
, &n
);
1288 for (p
= groups
; *p
; p
++) {
1291 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1292 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1296 log_write("%s: key: %s", rcfile
, rc
->message
);
1302 do_cache_push(*p
, str
);
1308 log_write("%s: key: %s", rcfile
, rc
->message
);
1314 str
= parse_rcfile_keyfile(*p
, FALSE
, &ret
);
1319 do_cache_push(*p
, str
);
1327 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1331 const gchar
*p
= filename
;
1332 struct client_crypto_s
*crypto
;
1334 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1342 if (valid_filename(p
) == FALSE
) {
1343 log_write(N_("%s: Invalid characters in filename"), p
);
1347 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1349 if (access(p
, R_OK
|W_OK
) != 0) {
1350 log_write("%s: %s", p
, strerror(errno
));
1354 crypto
= init_client_crypto();
1359 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1362 log_write("%s: %s", p
, pwmd_strerror(rc
));
1363 cleanup_crypto(&crypto
);
1367 crypto
->key
= gcry_malloc(hashlen
);
1370 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1371 cleanup_crypto(&crypto
);
1375 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1377 if (crypto
->fh
->ver
.fh2
.iter
<= 0ULL) {
1378 memset(crypto
->key
, '!', hashlen
);
1383 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1386 send_error(NULL
, rc
);
1387 cleanup_crypto(&crypto
);
1391 gcry_free(crypto
->fh
->doc
);
1392 crypto
->fh
->doc
= NULL
;
1395 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
,
1396 strlen(password
) ? strlen(password
) : 1);
1399 rc
= init_client_crypto2(filename
, crypto
);
1402 send_error(NULL
, rc
);
1403 cleanup_crypto(&crypto
);
1407 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, NULL
, NULL
);
1410 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1411 cleanup_crypto(&crypto
);
1415 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1416 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1417 cleanup_crypto(&crypto
);
1421 timeout
= get_key_file_integer(p
, "cache_timeout");
1422 cache_set_timeout(md5file
, timeout
);
1423 log_write(N_("File '%s' now cached"), filename
);
1424 cleanup_crypto(&crypto
);
1428 static void init_new_connection(gint fd
)
1431 struct client_thread_s
*new;
1434 new = g_malloc0(sizeof(struct client_thread_s
));
1437 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1442 MUTEX_LOCK(&cn_mutex
);
1444 attr
= pth_attr_new();
1445 pth_attr_init(attr
);
1446 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1447 new->tid
= pth_spawn(attr
, client_thread
, new);
1449 pth_attr_destroy(attr
);
1454 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1455 _gpg_strerror(gpg_error_from_errno(n
)));
1456 MUTEX_UNLOCK(&cn_mutex
);
1460 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1461 MUTEX_UNLOCK(&cn_mutex
);
1462 log_write(N_("new connection: tid=%p, fd=%i"), new->tid
, fd
);
1465 static void *accept_thread(void *arg
)
1467 gint sockfd
= *(gint
*)arg
;
1468 pth_attr_t attr
= pth_attr_of(pth_self());
1470 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1471 pth_attr_destroy(attr
);
1474 socklen_t slen
= sizeof(struct sockaddr_un
);
1475 struct sockaddr_un raddr
;
1478 fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1481 if (errno
!= EAGAIN
) {
1482 if (!quit
) // probably EBADF
1483 log_write("accept(): %s", strerror(errno
));
1491 init_new_connection(fd
);
1494 /* Just in case accept() failed for some reason other than EBADF */
1496 pth_exit(PTH_CANCELED
);
1500 static void *adjust_cache_timer_thread(void *arg
)
1502 pth_attr_t attr
= pth_attr_of(pth_self());
1504 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1505 pth_attr_destroy(attr
);
1510 cache_adjust_timer();
1517 void cleanup_mutex_cb(void *arg
)
1519 pth_mutex_t
*m
= arg
;
1524 void cleanup_ev_cb(void *arg
)
1526 pth_event_t ev
= arg
;
1528 pth_event_free(ev
, PTH_FREE_ALL
);
1531 void cleanup_fd_cb(void *arg
)
1533 gint fd
= *(gint
*)arg
;
1538 void cleanup_unlink_cb(void *arg
)
1545 void cleanup_cancel_cb(void *arg
)
1551 attr
= pth_attr_of(tid
);
1552 pth_attr_get(attr
, PTH_ATTR_JOINABLE
, &join
);
1557 pth_join(tid
, (void **)&rc
);
1561 static void *keepalive_thread(void *arg
)
1563 gint to
= *(gint
*)arg
;
1564 pth_attr_t attr
= pth_attr_of(pth_self());
1566 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1567 pth_attr_destroy(attr
);
1570 pth_event_t ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0));
1572 pth_cleanup_push(cleanup_ev_cb
, ev
);
1574 send_status_all(STATUS_KEEPALIVE
);
1581 static void startStopKeepAlive(gboolean term
)
1583 gint n
= get_key_file_integer("global", "keepalive");
1586 pth_cancel(keepalive_tid
);
1588 keepalive_tid
= NULL
;
1595 pth_attr_t attr
= pth_attr_new();
1597 pth_attr_init(attr
);
1598 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1599 keepalive_tid
= pth_spawn(attr
, keepalive_thread
, &n
);
1601 pth_attr_destroy(attr
);
1603 if (!keepalive_tid
) {
1604 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1605 _gpg_strerror(gpg_error_from_errno(e
)));
1609 pth_yield(keepalive_tid
);
1613 static gboolean
waiting_for_exit()
1616 pth_event_t evs
= NULL
;
1618 MUTEX_LOCK(&cn_mutex
);
1620 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
1621 struct client_thread_s
*thd
= g_slist_nth_data(cn_thread_list
, i
);
1622 pth_event_t ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, thd
->tid
);
1625 evs
= pth_event_concat(evs
, ev
, NULL
);
1630 MUTEX_UNLOCK(&cn_mutex
);
1636 MUTEX_LOCK(&cn_mutex
);
1637 i
= g_slist_length(cn_thread_list
);
1638 MUTEX_UNLOCK(&cn_mutex
);
1639 pth_event_free(evs
, PTH_FREE_ALL
);
1640 return i
? TRUE
: FALSE
;
1643 static void server_loop(gint sockfd
, gchar
**socketpath
)
1649 pth_t cache_timeout_tid
;
1651 sigemptyset(&sigset
);
1654 sigaddset(&sigset
, SIGTERM
);
1655 sigaddset(&sigset
, SIGINT
);
1657 /* Clears the file cache. */
1658 sigaddset(&sigset
, SIGUSR1
);
1660 /* Configuration file reloading. */
1661 sigaddset(&sigset
, SIGHUP
);
1663 /* Clears the cache and exits when something bad happens. */
1664 sigaddset(&sigset
, SIGABRT
);
1666 /* Ignored everywhere. When a client disconnects abnormally this signal
1667 * gets raised. It isn't needed though because client_thread() will check
1668 * for rcs even after the client disconnects. */
1669 signal(SIGPIPE
, SIG_IGN
);
1670 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1672 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1673 log_write(N_("Listening on %s"), *socketpath
);
1674 attr
= pth_attr_new();
1675 pth_attr_init(attr
);
1676 accept_tid
= pth_spawn(attr
, accept_thread
, &sockfd
);
1679 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1680 _gpg_strerror(gpg_error_from_errno(errno
)));
1681 pth_attr_destroy(attr
);
1685 pth_yield(accept_tid
);
1686 startStopKeepAlive(FALSE
);
1687 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1688 cache_timeout_tid
= pth_spawn(attr
, adjust_cache_timer_thread
, NULL
);
1690 if (!cache_timeout_tid
) {
1691 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1692 _gpg_strerror(gpg_error_from_errno(errno
)));
1693 pth_attr_destroy(attr
);
1697 pth_yield(cache_timeout_tid
);
1698 pth_attr_destroy(attr
);
1701 gchar
*str
= get_key_file_string("global", "debug_file");
1704 gchar
*f
= expand_homedir(str
);
1707 debugfp
= fopen(f
, "w");
1710 log_write("%s: %s", f
, pwmd_strerror(gpg_error_from_errno(errno
)));
1712 assuan_set_assuan_log_stream(debugfp
);
1721 pth_sigwait(&sigset
, &sig
);
1722 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1724 /* Caught a signal. */
1730 cache_clear(NULL
, 2);
1737 log_write(N_("clearing file cache"));
1738 cache_clear(NULL
, 2);
1749 * We're out of the main server loop. This happens when a signal was sent
1750 * to terminate the daemon. We'll wait for all clients to disconnect
1751 * before exiting and ignore any following signals.
1753 shutdown(sockfd
, SHUT_RDWR
);
1755 pth_cancel(accept_tid
);
1756 pth_join(accept_tid
, NULL
);
1757 unlink(*socketpath
);
1758 g_free(*socketpath
);
1760 MUTEX_LOCK(&cn_mutex
);
1761 n
= g_slist_length(cn_thread_list
);
1762 MUTEX_UNLOCK(&cn_mutex
);
1765 log_write(N_("waiting for all clients to disconnect"));
1768 MUTEX_LOCK(&cn_mutex
);
1769 n
= g_slist_length(cn_thread_list
);
1770 MUTEX_UNLOCK(&cn_mutex
);
1771 log_write(N_("%i clients remain"), n
);
1772 } while (waiting_for_exit());
1775 startStopKeepAlive(TRUE
);
1776 pth_cancel(cache_timeout_tid
);
1781 * Only called from pinentry_fork() in the child process.
1783 void free_client_list()
1785 gint i
, t
= g_slist_length(cn_thread_list
);
1787 for (i
= 0; i
< t
; i
++) {
1788 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1790 free_client(cn
->cl
);
1796 static guint
pwmd_cipher_to_gcrypt(guint64 flags
)
1798 if (flags
& PWMD_CIPHER_AES128
)
1799 return GCRY_CIPHER_AES128
;
1800 else if (flags
& PWMD_CIPHER_AES192
)
1801 return GCRY_CIPHER_AES192
;
1802 else if (flags
& PWMD_CIPHER_AES256
)
1803 return GCRY_CIPHER_AES256
;
1804 else if (flags
& PWMD_CIPHER_SERPENT128
)
1805 return GCRY_CIPHER_SERPENT128
;
1806 else if (flags
& PWMD_CIPHER_SERPENT192
)
1807 return GCRY_CIPHER_SERPENT192
;
1808 else if (flags
& PWMD_CIPHER_SERPENT256
)
1809 return GCRY_CIPHER_SERPENT256
;
1810 else if (flags
& PWMD_CIPHER_CAMELLIA128
)
1811 return GCRY_CIPHER_CAMELLIA128
;
1812 else if (flags
& PWMD_CIPHER_CAMELLIA192
)
1813 return GCRY_CIPHER_CAMELLIA192
;
1814 else if (flags
& PWMD_CIPHER_CAMELLIA256
)
1815 return GCRY_CIPHER_CAMELLIA256
;
1816 else if (flags
& PWMD_CIPHER_BLOWFISH
)
1817 return GCRY_CIPHER_BLOWFISH
;
1818 else if (flags
& PWMD_CIPHER_3DES
)
1819 return GCRY_CIPHER_3DES
;
1820 else if (flags
& PWMD_CIPHER_CAST5
)
1821 return GCRY_CIPHER_CAST5
;
1822 else if (flags
& PWMD_CIPHER_TWOFISH
)
1823 return GCRY_CIPHER_TWOFISH
;
1824 else if (flags
& PWMD_CIPHER_TWOFISH128
)
1825 return GCRY_CIPHER_TWOFISH128
;
1827 /* For backwards compatibility (no flags). */
1828 return GCRY_CIPHER_AES256
;
1831 guint
pwmd_cipher_str_to_cipher(const gchar
*str
)
1835 if (!g_strcasecmp(str
, "aes128"))
1836 flags
= PWMD_CIPHER_AES128
;
1837 else if (!g_strcasecmp(str
, "aes192"))
1838 flags
= PWMD_CIPHER_AES192
;
1839 else if (!g_strcasecmp(str
, "aes256"))
1840 flags
= PWMD_CIPHER_AES256
;
1841 if (!g_strcasecmp(str
, "serpent128"))
1842 flags
= PWMD_CIPHER_SERPENT128
;
1843 else if (!g_strcasecmp(str
, "serpent192"))
1844 flags
= PWMD_CIPHER_SERPENT192
;
1845 else if (!g_strcasecmp(str
, "serpent256"))
1846 flags
= PWMD_CIPHER_SERPENT256
;
1847 if (!g_strcasecmp(str
, "camellia128"))
1848 flags
= PWMD_CIPHER_CAMELLIA128
;
1849 else if (!g_strcasecmp(str
, "camellia192"))
1850 flags
= PWMD_CIPHER_CAMELLIA192
;
1851 else if (!g_strcasecmp(str
, "camellia256"))
1852 flags
= PWMD_CIPHER_CAMELLIA256
;
1853 else if (!g_strcasecmp(str
, "blowfish"))
1854 flags
= PWMD_CIPHER_BLOWFISH
;
1855 else if (!g_strcasecmp(str
, "cast5"))
1856 flags
= PWMD_CIPHER_CAST5
;
1857 else if (!g_strcasecmp(str
, "3des"))
1858 flags
= PWMD_CIPHER_3DES
;
1859 else if (!g_strcasecmp(str
, "twofish256"))
1860 flags
= PWMD_CIPHER_TWOFISH
;
1861 else if (!g_strcasecmp(str
, "twofish128"))
1862 flags
= PWMD_CIPHER_TWOFISH128
;
1867 /* To be called after read_file_header(). This sets the wanted algorithm from
1869 gpg_error_t
init_client_crypto2(const char *filename
,
1870 struct client_crypto_s
*crypto
)
1875 /* New file or conversion. */
1877 algo
= pwmd_cipher_to_gcrypt(PWMD_CIPHER_AES256
);
1878 else if (!crypto
->fh
->ver
.fh2
.flags
) {
1879 gchar
*tmp
= get_key_file_string(filename
? filename
: "global", "cipher");
1882 flags
= pwmd_cipher_str_to_cipher(tmp
);
1884 algo
= pwmd_cipher_to_gcrypt(flags
);
1885 crypto
->fh
->ver
.fh2
.flags
= flags
;
1888 algo
= pwmd_cipher_to_gcrypt(crypto
->fh
->ver
.fh2
.flags
);
1890 rc
= gcry_cipher_algo_info(algo
, GCRYCTL_TEST_ALGO
, NULL
, NULL
);
1895 rc
= gcry_cipher_algo_info(algo
, GCRYCTL_GET_KEYLEN
, NULL
,
1901 rc
= gcry_cipher_algo_info(algo
, GCRYCTL_GET_BLKLEN
, NULL
,
1902 &crypto
->blocksize
);
1908 gcry_cipher_close(crypto
->gh
);
1910 return gcry_cipher_open(&crypto
->gh
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
1913 struct client_crypto_s
*init_client_crypto()
1915 struct client_crypto_s
*new = g_malloc0(sizeof(struct client_crypto_s
));
1918 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1925 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1926 const gchar
*outfile
)
1929 guchar md5file
[gcry_md_get_algo_dlen(GCRY_MD_MD5
)];
1931 struct client_crypto_s
*crypto
= init_client_crypto();
1932 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1935 return GPG_ERR_ENOMEM
;
1937 crypto
->key
= gcry_malloc(hashlen
);
1940 cleanup_crypto(&crypto
);
1941 return GPG_ERR_ENOMEM
;
1944 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
1946 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
1951 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
1953 /* The header in version 1 had a bug where the iterations were off-by-one.
1954 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
1957 if (crypto
->fh
->ver
.fh1
.iter
!= -1) {
1959 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1964 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1965 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1968 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
1976 rc
= init_client_crypto2(NULL
, crypto
);
1981 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, &crypto
->fh
->doc
,
1987 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
1990 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1994 crypto
->fh
->v1
= FALSE
;
1996 iter
= crypto
->fh
->ver
.fh1
.iter
+1;
1997 memset(&crypto
->fh
->ver
.fh2
, 0, sizeof(crypto
->fh
->ver
.fh2
));
1998 /* Keep the iterations and key from the original file. */
1999 crypto
->fh
->ver
.fh2
.iter
= iter
;
2000 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
2004 send_error(NULL
, rc
);
2006 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
2007 cleanup_crypto(&crypto
);
2011 int main(int argc
, char *argv
[])
2014 struct sockaddr_un addr
;
2015 gchar buf
[PATH_MAX
];
2016 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
2017 gchar
*socketarg
= NULL
;
2018 gchar
*datadir
= NULL
;
2022 gchar
**cache_push
= NULL
;
2023 gchar
*import
= NULL
, *keyfile
= NULL
;
2024 guint64 cmd_iterations
= -1ULL;
2025 gint default_timeout
;
2026 gboolean rcfile_spec
= FALSE
;
2027 gint estatus
= EXIT_FAILURE
;
2029 gchar
*outfile
= NULL
;
2030 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
2032 gboolean secure
= FALSE
;
2033 gint background
= 1;
2034 gchar
*convert
= NULL
;
2035 gint show_version
= 0;
2037 #ifdef WITH_PINENTRY
2038 gboolean disable_pinentry
= FALSE
;
2041 const struct option long_opts
[] = {
2042 #ifdef WITH_PINENTRY
2043 { "no-pinentry", 0, 0, 'P' },
2045 { "outfile", 1, 0, 'o' },
2046 { "convert", 1, 0, 'C' },
2047 { "no-fork", 0, 0, 'n' },
2048 { "disable-dump", 0, 0, 'D' },
2049 { "import", 1, 0, 'I' },
2050 { "iterations", 1, 0, 'i' },
2051 { "key-file", 1, 0, 'k' },
2052 { "rcfile", 1, 0, 'f' },
2053 { "version", 0, &show_version
, 1 },
2054 { "help", 0, &show_help
, 1 },
2057 #ifdef WITH_PINENTRY
2058 const gchar
*optstring
= "Po:C:nDI:i:k:f:";
2060 const gchar
*optstring
= "o:C:nDI:i:k:f:";
2063 #ifdef HAVE_SETRLIMIT
2066 rl
.rlim_cur
= rl
.rlim_max
= 0;
2068 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
2069 err(EXIT_FAILURE
, "setrlimit()");
2074 setlocale(LC_ALL
, "");
2075 bindtextdomain("pwmd", LOCALEDIR
);
2084 assuan_set_gpg_err_source(GPG_ERR_SOURCE_DEFAULT
);
2085 g_mem_set_vtable(&mtable
);
2086 struct assuan_malloc_hooks mhooks
= {
2087 xmalloc
, xrealloc
, xfree
2089 assuan_set_system_hooks(ASSUAN_SYSTEM_PTH
);
2090 assuan_set_malloc_hooks(&mhooks
);
2091 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
2096 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", g_get_home_dir());
2098 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2099 err(EXIT_FAILURE
, "%s", buf
);
2101 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", g_get_home_dir());
2103 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2104 err(EXIT_FAILURE
, "%s", buf
);
2108 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
2112 usage(argv
[0], EXIT_SUCCESS
);
2115 printf(N_("%s\nCopyright (C) 2010 %s\nReleased under the terms of the GPL v2. Use at your own risk.\n\nCompile time features:\n%s"), PACKAGE_STRING
,
2117 #ifdef WITH_PINENTRY
2146 #ifdef WITH_PINENTRY
2148 disable_pinentry
= TRUE
;
2167 cmd_iterations
= strtol(optarg
, NULL
, 10);
2173 rcfile
= g_strdup(optarg
);
2177 usage(argv
[0], EXIT_FAILURE
);
2181 pth_mutex_init(&cn_mutex
);
2182 pth_mutex_init(&cache_mutex
);
2183 pth_mutex_init(&rcfile_mutex
);
2184 #ifdef WITH_PINENTRY
2185 pth_mutex_init(&pin_mutex
);
2189 rcfile
= g_strdup_printf("%s/.pwmd/config", g_get_home_dir());
2191 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2194 #ifdef WITH_PINENTRY
2195 if (disable_pinentry
== TRUE
)
2196 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2199 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2200 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2202 if (log_syslog
== TRUE
)
2203 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2205 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2206 x
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2209 if (setpriority(PRIO_PROCESS
, 0, x
) == -1) {
2210 log_write("setpriority(): %s", strerror(errno
));
2215 #ifdef HAVE_MLOCKALL
2216 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2217 log_write("mlockall(): %s", strerror(errno
));
2224 usage(argv
[0], EXIT_FAILURE
);
2226 opt
= convert_file(convert
, keyfile
, outfile
);
2227 g_key_file_free(keyfileh
);
2229 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2234 usage(argv
[0], EXIT_FAILURE
);
2236 if (cmd_iterations
== -1ULL)
2237 cmd_iterations
= (guint64
)get_key_file_integer("global", "iterations");
2239 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2240 g_key_file_free(keyfileh
);
2242 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2245 g_key_file_set_list_separator(keyfileh
, ',');
2247 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2248 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2250 socketarg
= expand_homedir(p
);
2253 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2254 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2256 datadir
= expand_homedir(p
);
2259 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2260 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2261 disable_list_and_dump
= n
;
2264 disable_list_and_dump
= secure
;
2266 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2267 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2269 default_timeout
= -1;
2271 setup_logging(keyfileh
);
2273 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2274 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2276 if (argc
!= optind
) {
2277 for (; optind
< argc
; optind
++) {
2278 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2279 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2283 if (strchr(socketarg
, '/') == NULL
) {
2284 socketdir
= g_get_current_dir();
2285 socketname
= g_strdup(socketarg
);
2286 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2289 socketname
= g_strdup(strrchr(socketarg
, '/'));
2291 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2292 socketdir
= g_strdup(socketarg
);
2293 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2296 if (chdir(datadir
)) {
2297 log_write("%s: %s", datadir
, strerror(errno
));
2302 if (parse_rcfile_keys() == FALSE
)
2305 clear_rcfile_keys();
2308 * Set the cache entry for a file. Prompts for the password.
2311 for (opt
= 0; cache_push
[opt
]; opt
++)
2312 do_cache_push(cache_push
[opt
], NULL
);
2314 g_strfreev(cache_push
);
2315 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2319 * bind() doesn't like the full pathname of the socket or any non alphanum
2320 * characters so change to the directory where the socket is wanted then
2321 * create it then change to datadir.
2323 if (chdir(socketdir
)) {
2324 log_write("%s: %s", socketdir
, strerror(errno
));
2330 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2331 log_write("socket(): %s", strerror(errno
));
2335 addr
.sun_family
= AF_UNIX
;
2336 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2338 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2339 log_write("bind(): %s", strerror(errno
));
2341 if (errno
== EADDRINUSE
)
2342 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2343 "stale socket. Please remove it manually."), socketpath
);
2349 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2350 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2351 mode_t mode
= strtol(t
, NULL
, 8);
2352 mode_t mask
= umask(0);
2356 if (chmod(socketname
, mode
) == -1) {
2357 log_write("%s: %s", socketname
, strerror(errno
));
2367 g_free(--socketname
);
2369 if (chdir(datadir
)) {
2370 log_write("%s: %s", datadir
, strerror(errno
));
2378 if (listen(sockfd
, 0) == -1) {
2379 log_write("listen(): %s", strerror(errno
));
2384 unsetenv("DISPLAY");
2390 log_write("fork(): %s", strerror(errno
));
2403 server_loop(sockfd
, &socketpath
);
2404 estatus
= EXIT_SUCCESS
;
2407 if (socketpath
&& do_unlink
) {
2412 g_key_file_free(keyfileh
);
2415 xmlCleanupGlobals();
2417 if (estatus
== EXIT_SUCCESS
)
2418 log_write(N_("pwmd exiting normally"));
2420 #if defined(DEBUG) && !defined(MEM_DEBUG)