1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 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>
69 #include "pwmd_error.h"
75 GCRY_THREAD_OPTION_PTHREAD_IMPL
;
77 static void clear_rcfile_keys()
83 groups
= g_key_file_get_groups(keyfileh
, &n
);
85 for (p
= groups
; *p
; p
++) {
88 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
)
89 g_key_file_set_string(keyfileh
, *p
, "key", "");
95 static void *reload_rcfile_thread(void *arg
)
97 gboolean b
= disable_list_and_dump
;
100 pthread_mutex_lock(&reload_rcfile_mutex
);
101 log_write(N_("reloading configuration file '%s'"), rcfile
);
102 k
= parse_rcfile(FALSE
);
107 g_key_file_free(keyfileh
);
111 disable_list_and_dump
= b
;
115 startStopKeepAlive(FALSE
);
116 send_status_all(STATUS_CONFIG
);
118 pthread_mutex_unlock(&reload_rcfile_mutex
);
122 static void reload_rcfile()
127 pthread_attr_init(&attr
);
128 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
129 pthread_create(&tid
, &attr
, reload_rcfile_thread
, NULL
);
130 pthread_attr_destroy(&attr
);
133 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
135 gpg_error_t n
= gpg_error_from_errno(e
);
137 pthread_testcancel();
138 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, gpg_strerror(n
)));
141 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
143 gpg_err_code_t n
= gpg_err_code(e
);
144 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
145 struct client_s
*client
= assuan_get_pointer(ctx
);
147 pthread_testcancel();
150 return assuan_process_done(ctx
, 0);
153 log_write("%s", pwmd_strerror(e
));
157 if (n
== EPWMD_LIBXML_ERROR
) {
158 xmlErrorPtr xe
= client
->xml_error
;
161 xe
= xmlGetLastError();
163 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
164 log_write("%s", xe
->message
);
166 if (xe
== client
->xml_error
)
171 client
->xml_error
= NULL
;
175 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
178 static struct client_thread_s
*find_cn(pthread_t tid
)
185 pthread_mutex_lock(&cn_mutex
);
187 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
188 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
190 if (cn
->tid
== tid
) {
191 pthread_mutex_unlock(&cn_mutex
);
196 pthread_mutex_unlock(&cn_mutex
);
200 void log_write(const gchar
*fmt
, ...)
208 struct client_thread_s
*cn
;
212 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
215 if (!cmdline
&& logfile
) {
216 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
224 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
235 fprintf(stderr
, "%s\n", args
);
241 cn
= find_cn(pthread_self());
245 name
= print_fmt(buf
, sizeof(buf
), "%i: ", cn
->fd
);
247 name
= print_fmt(buf
, sizeof(buf
), "%s(%i): ", cn
->name
, cn
->fd
);
250 if (!cmdline
&& log_syslog
== TRUE
)
251 syslog(LOG_INFO
, "%s%s", name
, args
);
254 tm
= localtime(&now
);
255 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
256 tbuf
[sizeof(tbuf
) - 1] = 0;
258 if (args
[strlen(args
)-1] == '\n')
259 args
[strlen(args
)-1] = 0;
261 line
= g_strdup_printf("%s %i %s%s\n", tbuf
, getpid(), name
, args
);
272 write(fd
, line
, strlen(line
));
277 if (isatty(STDERR_FILENO
)) {
278 fprintf(stderr
, "%s", line
);
285 static void usage(gchar
*pn
)
287 g_fprintf(stderr
, N_(
288 "Usage: %s [-hvDnP] [-f <rcfile>] [-C <filename>] "
289 "[-I <filename> [-i <iter>]]\n "
290 "[-k <keyfile>] [-o <outfile>] [file1] [...]\n"
291 " -n run as a foreground process\n"
292 " -f load the specified rcfile (~/.pwmd/config)\n"
293 " -C convert a version 1 data file to version 2\n"
294 " -I import an XML file\n"
295 " -i encrypt with the specified number of iterations when importing\n"
296 " (config default in the \"global\" section)\n"
297 " -k obtain the key from the specified file\n"
298 " -o output file for use with the -C and -I options\n"
299 " -D disable use of the LIST and DUMP commands\n"
300 " -P disable pinentry\n"
302 " -h this help text\n"
307 static int gcry_SecureCheck(const void *ptr
)
312 static void setup_gcrypt()
314 gcry_check_version(NULL
);
316 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
319 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
321 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
323 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
324 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
327 static gint
new_connection(struct client_s
*cl
)
330 gchar ver
[ASSUAN_LINELENGTH
];
332 rc
= assuan_init_socket_server_ext(&cl
->ctx
, cl
->thd
->fd
, 2);
337 assuan_set_pointer(cl
->ctx
, cl
);
338 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
339 assuan_set_hello_line(cl
->ctx
, ver
);
340 rc
= register_commands(cl
->ctx
);
345 rc
= assuan_accept(cl
->ctx
);
353 log_write("%s", gpg_strerror(rc
));
357 gpg_error_t
send_status(assuan_context_t ctx
, status_msg_t which
,
358 const gchar
*fmt
, ...)
361 struct client_s
*client
= assuan_get_pointer(ctx
);
362 gchar buf
[ASSUAN_LINELENGTH
];
363 gchar
*status
= NULL
;
368 g_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
375 CACHE_LOCK(client
->ctx
);
376 line
= print_fmt(buf
, sizeof(buf
), "%i", cache_file_count());
381 MUTEX_LOCK(&cn_mutex
);
382 line
= print_fmt(buf
, sizeof(buf
), "%i", g_slist_length(cn_thread_list
));
383 MUTEX_UNLOCK(&cn_mutex
);
389 case STATUS_KEEPALIVE
:
390 status
= "KEEPALIVE";
394 line
= N_("Waiting for lock");
402 case STATUS_DECOMPRESS
:
403 status
= "DECOMPRESS";
405 case STATUS_COMPRESS
:
411 log_write("%s %s", status
, line
);
415 return assuan_write_status(ctx
, status
, line
);
418 /* This is needed for FreeBSD systems (and maybe others). I'm guessing here,
419 * but I think it lets the scheduler let other threads do their work. If we
420 * were to do the write() in send_status_all() then write() would return
421 * before the signal was actually sent to client_msg_thread(). This is only
422 * called from send_status_all().
424 static void *write_status_msg(void *arg
)
426 struct client_thread_s
*cn
=arg
;
429 pthread_cond_signal(&cn
->msg_cond
);
430 len
= write(cn
->msg_fd
[1], &cn
->msg
, sizeof(status_msg_t
));
432 if (len
!= sizeof(status_msg_t
))
433 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
438 void send_status_all(status_msg_t which
)
442 MUTEX_LOCK(&cn_mutex
);
444 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
445 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
452 pthread_create(&tid
, NULL
, write_status_msg
, cn
);
453 pthread_join(tid
, NULL
);
456 MUTEX_UNLOCK(&cn_mutex
);
459 static void xml_error_cb(void *data
, xmlErrorPtr e
)
461 struct client_s
*client
= data
;
464 * Keep the first reported error as the one to show in the error
465 * description. Reset in send_error().
467 if (client
->xml_error
)
470 xmlCopyError(e
, client
->xml_error
);
473 static void cleanup_crypto_handler(void *arg
)
475 struct client_crypto_s
**a
= (struct client_crypto_s
**)arg
;
476 struct client_crypto_s
*cr
= *a
;
479 if (cr
->fh
->fd
!= -1 ||
480 (cmdline
== TRUE
&& cr
->fh
->fd
!= STDOUT_FILENO
))
484 gcry_free(cr
->fh
->doc
);
490 gcry_cipher_close(cr
->gh
);
496 void cleanup_crypto(struct client_crypto_s
**c
)
498 struct client_crypto_s
*cr
= *c
;
513 gcry_free(cr
->tkey2
);
516 gcry_free(cr
->inbuf
);
519 gcry_free(cr
->outbuf
);
521 pthread_cleanup_push(cleanup_crypto_handler
, c
);
522 pthread_testcancel();
523 pthread_cleanup_pop(1);
527 * This is called after a child_thread terminates. Set with
528 * pthread_cleanup_push().
530 static void cleanup_cb(void *arg
)
532 struct client_thread_s
*cn
= arg
;
533 struct client_s
*cl
= cn
->cl
;
535 MUTEX_LOCK(&cn_mutex
);
537 log_write(N_("exiting, fd=%i"), cn
->fd
);
540 pthread_cancel(cn
->msg_tid
);
541 pthread_join(cn
->msg_tid
, NULL
);
542 pthread_cancel(cn
->msg_sender_tid
);
543 pthread_join(cn
->msg_sender_tid
, NULL
);
544 close(cl
->thd
->msg_fd
[0]);
545 close(cl
->thd
->msg_fd
[1]);
548 pthread_join(cn
->tid
, NULL
);
550 if (pthread_mutex_trylock(&cn
->msg_mutex
) == EBUSY
) {
551 MUTEX_UNLOCK(&cn
->msg_mutex
);
554 pthread_mutex_unlock(&cn
->msg_mutex
);
556 pthread_mutex_destroy(&cn
->msg_mutex
);
557 pthread_cond_destroy(&cn
->msg_cond
);
559 if (pthread_mutex_trylock(&cn
->msg_sender_mutex
) == EBUSY
) {
560 MUTEX_UNLOCK(&cn
->msg_sender_mutex
);
563 pthread_mutex_unlock(&cn
->msg_sender_mutex
);
565 pthread_mutex_destroy(&cn
->msg_sender_mutex
);
566 pthread_cond_destroy(&cn
->msg_sender_cond
);
569 struct status_msg_s
*m
= g_slist_nth_data(cn
->msg_queue
, 0);
574 cn
->msg_queue
= g_slist_remove(cn
->msg_queue
, m
);
578 if (cl
&& cl
->freed
== FALSE
)
583 gnutls_deinit(cn
->tls
->ses
);
593 assuan_deinit_server(cl
->ctx
);
596 if (cl
&& cl
->pinentry
)
597 cleanup_pinentry(cl
->pinentry
);
601 cleanup_crypto(&cl
->crypto
);
608 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
610 MUTEX_UNLOCK(&cn_mutex
);
611 send_status_all(STATUS_CLIENTS
);
614 static void *client_msg_sender_thread(void *arg
)
616 struct client_thread_s
*thd
= arg
;
618 MUTEX_LOCK(&thd
->msg_sender_mutex
);
621 struct status_msg_s
*msg
;
624 pthread_cond_wait(&thd
->msg_sender_cond
, &thd
->msg_sender_mutex
);
625 pthread_testcancel();
627 /* The messages may have been stacked while waiting for send_status()
628 * to return. Send what's in the queue. */
630 msg
= g_slist_nth_data(thd
->msg_queue
, 0);
635 /* Unlock to prevent blocking in client_msg_thread(). */
636 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
637 rc
= send_status(thd
->cl
->ctx
, msg
->msg
, NULL
);
638 MUTEX_LOCK(&thd
->msg_sender_mutex
);
639 thd
->msg_queue
= g_slist_remove(thd
->msg_queue
, msg
);
643 log_write(N_("msg for %i failed: %s"), thd
->fd
,
645 pthread_cancel(thd
->tid
);
654 * This function waits for a signal from send_status_all() then appends a
655 * message read from a pipe to the clients message queue. The actual sending
656 * of the message is done in client_msg_sender_thread() which waits for a
657 * signal from this function. This prevents blocking in assuan_send_status()
658 * when sending to remote clients. This also avoids duplicate status messages.
659 * If an existing status message in the message queue has the same type as the
660 * current message the the current one will be skipped. This avoids flooding
661 * the client with old status messages.
663 static void *client_msg_thread(void *arg
)
665 struct client_thread_s
*thd
= arg
;
667 MUTEX_LOCK(&thd
->msg_mutex
);
674 struct status_msg_s
*msg
;
676 pthread_cond_wait(&thd
->msg_cond
, &thd
->msg_mutex
);
677 pthread_testcancel();
679 FD_SET(thd
->msg_fd
[0], &rfds
);
680 n
= select(thd
->msg_fd
[0]+1, &rfds
, NULL
, NULL
, NULL
);
682 if (n
<= 0 || !FD_ISSET(thd
->msg_fd
[0], &rfds
))
685 len
= read(thd
->msg_fd
[0], &m
, sizeof(status_msg_t
));
686 pthread_testcancel();
688 if (len
!= sizeof(status_msg_t
)) {
689 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
693 MUTEX_LOCK(&thd
->msg_sender_mutex
);
695 for (n
= 0; n
< g_slist_length(thd
->msg_queue
); n
++) {
696 msg
= g_slist_nth_data(thd
->msg_queue
, n
);
702 msg
= g_malloc(sizeof(struct status_msg_s
));
705 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
706 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
711 thd
->msg_queue
= g_slist_append(thd
->msg_queue
, msg
);
713 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
714 pthread_cond_signal(&thd
->msg_sender_cond
);
721 * Called every time a connection is made from init_new_connection(). This is
722 * the thread entry point.
724 static void *client_thread(void *data
)
726 struct client_thread_s
*thd
= data
;
727 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
732 * Prevent a race condition with init_new_connection() if this thread
733 * fails (returns) for some reason before init_new_connection() releases
736 MUTEX_LOCK(&cn_mutex
);
737 MUTEX_UNLOCK(&cn_mutex
);
738 pthread_cleanup_push(cleanup_cb
, thd
);
741 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
749 * Do the TLS handshake before anything else.
752 gchar
*prio
= get_key_file_string("global", "cipher_suite");
754 thd
->tls
= tls_init(thd
->fd
, prio
);
762 pthread_testcancel();
768 if (new_connection(cl
))
772 cl
->pinentry
= pinentry_init();
776 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
781 /* Require the client to explicity set OPTION PINENTRY since the DISPLAY
782 * might be automatically set from the client. Connections to the X11
783 * server usually aren't encrypted and the client might unintentionally
784 * send the passphrase in the clear.
787 cl
->pinentry
->enable
= FALSE
;
792 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
793 log_write("mlockall(): %s", strerror(errno
));
798 if (pipe(thd
->msg_fd
) == -1) {
799 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
803 pthread_cond_init(&thd
->msg_cond
, NULL
);
804 pthread_mutex_init(&thd
->msg_mutex
, NULL
);
805 pthread_attr_init(&attr
);
806 pthread_create(&thd
->msg_tid
, &attr
, client_msg_thread
, thd
);
808 pthread_cond_init(&thd
->msg_sender_cond
, NULL
);
809 pthread_mutex_init(&thd
->msg_sender_mutex
, NULL
);
810 pthread_create(&thd
->msg_sender_tid
, &attr
, client_msg_sender_thread
, thd
);
811 pthread_attr_destroy(&attr
);
812 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
815 log_write("%s", gpg_strerror(rc
));
819 send_status_all(STATUS_CLIENTS
);
820 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
827 FD_SET(cl
->thd
->fd
, &rfds
);
830 if (cl
->pinentry
->status
== PINENTRY_RUNNING
)
831 FD_SET(cl
->pinentry
->fd
, &rfds
);
833 n
= cl
->thd
->fd
> cl
->pinentry
->fd
? cl
->thd
->fd
: cl
->pinentry
->fd
;
837 n
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
838 pthread_testcancel();
843 if (FD_ISSET(cl
->thd
->fd
, &rfds
)) {
844 rc
= assuan_process_next(cl
->ctx
);
845 pthread_testcancel();
848 cl
->inquire_status
= INQUIRE_INIT
;
850 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
853 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
854 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
857 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
863 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
864 cl
->pinentry
->status
= PINENTRY_RUNNING
;
867 switch (cl
->inquire_status
) {
872 cl
->inquire_status
= INQUIRE_INIT
;
873 pthread_testcancel();
874 rc
= assuan_process_done(cl
->ctx
, 0);
882 cl
->pinentry
->fd
!= -1 && FD_ISSET(cl
->pinentry
->fd
, &rfds
));
887 * Client cleanup (including XML data) is done in cleanup_cb() from
888 * the cleanup thread.
892 if (1) {} // Fixes compile time error with pthread_cleanup_push().
893 pthread_cleanup_pop(1);
894 pthread_exit(PTHREAD_CANCELED
);
898 static void setup_logging(GKeyFile
*kf
)
900 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
903 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
909 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
915 logfile
= g_strdup(buf
);
925 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
929 * Make sure all settings are set to either the specified setting or a
932 static void set_rcfile_defaults(GKeyFile
*kf
)
936 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
937 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
938 g_key_file_set_string(kf
, "global", "socket_path", buf
);
941 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
942 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
943 g_key_file_set_string(kf
, "global", "data_directory", buf
);
946 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
947 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
949 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
950 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
951 g_key_file_set_string(kf
, "global", "log_path", buf
);
954 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
955 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
958 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
959 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
962 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
963 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
965 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
966 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0)
967 g_key_file_set_integer(kf
, "global", "iterations", 1);
969 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
970 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
972 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
973 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
975 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
976 g_key_file_set_integer(kf
, "global", "compression_level", 6);
978 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
979 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
981 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
982 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
984 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
986 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
987 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
990 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
993 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
994 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
996 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
997 g_key_file_set_integer(kf
, "global", "keepalive", 30);
1000 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
1001 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
1003 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
1004 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
1008 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
1009 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
1011 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
1012 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
1014 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
1015 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
1017 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
1018 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
1020 if (g_key_file_has_key(kf
, "global", "cipher_suite", NULL
) == FALSE
)
1021 g_key_file_set_string(kf
, "global", "cipher_suite", "SECURE256");
1023 if (g_key_file_has_key(kf
, "global", "tcp_use_crl", NULL
) == FALSE
)
1024 g_key_file_set_boolean(kf
, "global", "tcp_use_crl", FALSE
);
1030 static GKeyFile
*parse_rcfile(gboolean specified
)
1032 GKeyFile
*kf
= g_key_file_new();
1035 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
1036 log_write("%s: %s", rcfile
, rc
->message
);
1038 if (cmdline
&& specified
) {
1043 if (rc
->code
== G_FILE_ERROR_NOENT
) {
1045 set_rcfile_defaults(kf
);
1053 set_rcfile_defaults(kf
);
1057 static gchar
*do_get_password(const gchar
*prompt
)
1059 gchar buf
[LINE_MAX
] = {0}, *p
;
1060 struct termios told
, tnew
;
1063 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
1064 log_write("tcgetattr(): %s", strerror(errno
));
1068 memcpy(&tnew
, &told
, sizeof(struct termios
));
1069 tnew
.c_lflag
&= ~(ECHO
);
1070 tnew
.c_lflag
|= ICANON
|ECHONL
;
1072 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
1073 log_write("tcsetattr(): %s", strerror(errno
));
1074 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1078 fprintf(stderr
, "%s", prompt
);
1080 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
1081 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1085 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1086 p
[strlen(p
) - 1] = 0;
1089 key
= gcry_malloc(1);
1093 key
= gcry_malloc(strlen(p
) + 1);
1094 sprintf(key
, "%s", p
);
1097 memset(&buf
, 0, sizeof(buf
));
1101 /* Only used when "enable_pinentry" is "false" or -P. */
1102 static gpg_error_t
get_input(const gchar
*filename
,
1103 struct client_crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
1107 if (which
== PINENTRY_SAVE
) {
1108 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
1109 crypto
->tkey
= do_get_password(prompt
);
1112 if (!crypto
->tkey
) {
1113 log_write(N_("%s: Skipping file"), filename
);
1114 return GPG_ERR_BAD_PASSPHRASE
;
1117 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
1118 crypto
->tkey2
= do_get_password(prompt
);
1121 if (!crypto
->tkey2
) {
1122 log_write(N_("%s: Skipping file"), filename
);
1123 return GPG_ERR_BAD_PASSPHRASE
;
1126 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
1127 log_write(N_("%s: Passphrase mismatch"), filename
);
1128 return EPWMD_BADKEY
;
1131 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
1132 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1136 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
1138 if ((crypto
->tkey
= do_get_password(prompt
)) == NULL
) {
1139 log_write(N_("%s: Skipping file"), filename
);
1141 return GPG_ERR_BAD_PASSPHRASE
;
1144 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
1145 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1151 * inbuf must have been allocated with gcry_malloc().
1153 gpg_error_t
export_common(const gchar
*filename
, struct client_crypto_s
*crypto
,
1154 gpointer inbuf
, gulong insize
)
1161 level
= get_key_file_integer(filename
, "compression_level");
1166 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
1168 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
1171 crypto
->inbuf
= outbuf
;
1172 crypto
->insize
= outsize
;
1173 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
1177 static gpg_error_t
get_password(const gchar
*filename
,
1178 struct client_crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
1179 pinentry_cmd_t which
)
1181 #ifdef WITH_PINENTRY
1184 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
1187 return get_input(filename
, crypto
, key
, which
);
1188 #ifdef WITH_PINENTRY
1191 gchar
*result
= NULL
;
1192 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
1194 set_pinentry_defaults(pin
);
1196 pin
->filename
= g_strdup(filename
);
1197 rc
= pinentry_getpin(pin
, &result
);
1204 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
1206 cleanup_pinentry(pin
);
1214 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
1217 gchar buf
[LINE_MAX
] = {0}, *p
;
1223 if ((fp
= fopen(file
, "r")) == NULL
) {
1224 *rc
= gpg_error_from_syserror();
1228 p
= fgets(buf
, sizeof(buf
), fp
);
1232 if (len
&& buf
[len
- 1] == '\n')
1235 str
= gcry_malloc(len
+ 1);
1238 *rc
= gpg_error_from_errno(ENOMEM
);
1242 memcpy(str
, buf
, len
? len
: 1);
1244 memset(&buf
, 0, sizeof(buf
));
1249 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
,
1253 gchar
*t
, *file
= NULL
, *str
;
1255 *rc
= GPG_ERR_UNKNOWN_ERRNO
;
1257 if (import
== FALSE
) {
1258 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1259 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1263 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1270 t
= expand_homedir(file
);
1273 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1274 *rc
= gpg_error_from_errno(ENOMEM
);
1283 /* -I or -C. The filename is a key file. */
1284 file
= g_strdup(filename
);
1287 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1288 *rc
= gpg_error_from_errno(ENOMEM
);
1294 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1302 if (_getline(file
, &str
, rc
) == FALSE
) {
1303 log_write("%s: %s: %s", filename
, file
, pwmd_strerror(*rc
));
1313 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1314 const gchar
*keyfile
, guint64 iter
)
1323 struct client_crypto_s
*crypto
;
1325 if (stat(filename
, &st
) == -1) {
1326 log_write("%s: %s", filename
, strerror(errno
));
1330 crypto
= init_client_crypto();
1335 crypto
->key
= gcry_malloc(gcrykeysize
);
1336 memset(crypto
->key
, 0, gcrykeysize
);
1339 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1343 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
1346 if (iter
&& keyfile
) {
1347 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1352 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1353 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1356 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
1362 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1363 log_write("%s: %s", filename
, strerror(errno
));
1367 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1369 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1373 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
1377 log_write("%s: %s", filename
, strerror(errno
));
1382 xmlbuf
[st
.st_size
] = 0;
1385 * Make sure the document validates.
1387 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1388 log_write("xmlReadDoc() failed");
1394 xmlDocDumpMemory(doc
, &xml
, &len
);
1398 memset(crypto
->key
, '!', gcrykeysize
);
1400 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1403 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1407 crypto
->fh
->fh2
.iter
= iter
;
1408 rc
= export_common(outfile
, crypto
, xml
, len
);
1412 send_error(NULL
, rc
);
1416 cleanup_crypto(&crypto
);
1420 cleanup_crypto(&crypto
);
1424 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1429 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1430 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1433 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1434 g_clear_error(&grc
);
1438 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1439 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1442 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1443 g_clear_error(&grc
);
1451 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1456 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1457 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
1460 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1461 g_clear_error(&grc
);
1465 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1466 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1469 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1470 g_clear_error(&grc
);
1478 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1480 gboolean val
= FALSE
;
1483 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1484 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1487 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1488 g_clear_error(&grc
);
1492 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1493 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1496 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1497 g_clear_error(&grc
);
1505 static gboolean
parse_rcfile_keys()
1512 groups
= g_key_file_get_groups(keyfileh
, &n
);
1514 for (p
= groups
; *p
; p
++) {
1517 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1518 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1522 log_write("%s: key: %s", rcfile
, rc
->message
);
1528 do_cache_push(*p
, str
);
1534 log_write("%s: key: %s", rcfile
, rc
->message
);
1540 str
= parse_rcfile_keyfile(*p
, FALSE
, &ret
);
1545 do_cache_push(*p
, str
);
1553 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1557 const gchar
*p
= filename
;
1558 struct client_crypto_s
*crypto
;
1567 if (valid_filename(p
) == FALSE
) {
1568 log_write(N_("%s: Invalid characters in filename"), p
);
1572 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1574 if (access(p
, R_OK
|W_OK
) != 0) {
1575 log_write("%s: %s", p
, strerror(errno
));
1579 crypto
= init_client_crypto();
1584 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1587 log_write("%s: %s", p
, pwmd_strerror(rc
));
1588 cleanup_crypto(&crypto
);
1592 crypto
->key
= gcry_malloc(gcrykeysize
);
1595 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1596 cleanup_crypto(&crypto
);
1600 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1602 if (crypto
->fh
->fh2
.iter
<= 0) {
1603 memset(crypto
->key
, '!', gcrykeysize
);
1608 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1611 send_error(NULL
, rc
);
1612 cleanup_crypto(&crypto
);
1616 gcry_free(crypto
->fh
->doc
);
1617 crypto
->fh
->doc
= NULL
;
1620 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
,
1621 strlen(password
) ? strlen(password
) : 1);
1624 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, NULL
, NULL
);
1627 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1628 cleanup_crypto(&crypto
);
1632 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1633 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1634 cleanup_crypto(&crypto
);
1638 timeout
= get_key_file_integer(p
, "cache_timeout");
1639 cache_set_timeout(md5file
, timeout
);
1640 log_write(N_("File '%s' now cached"), filename
);
1641 cleanup_crypto(&crypto
);
1645 static void init_new_connection(gint fd
, gchar
*addr
)
1648 pthread_attr_t attr
;
1649 struct client_thread_s
*new;
1652 new = g_malloc0(sizeof(struct client_thread_s
));
1655 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1659 MUTEX_LOCK(&cn_mutex
);
1667 pthread_attr_init(&attr
);
1668 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1669 n
= pthread_create(&tid
, &attr
, client_thread
, new);
1670 pthread_attr_destroy(&attr
);
1674 log_write("pthread_create(): %s", strerror(n
));
1675 MUTEX_UNLOCK(&cn_mutex
);
1680 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1681 MUTEX_UNLOCK(&cn_mutex
);
1684 log_write(N_("new connection: fd=%i, addr=%s"), fd
, addr
);
1686 log_write(N_("new connection: fd=%i"), fd
);
1690 /* From Beej's Guide to Network Programming. It's a good tutorial. */
1691 static void *get_in_addr(struct sockaddr
*sa
)
1693 if (sa
->sa_family
== AF_INET
)
1694 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
1696 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
1699 static void *tcp_accept_thread(void *arg
)
1701 gint sockfd
= (gint
)arg
;
1704 struct sockaddr_storage raddr
;
1705 socklen_t slen
= sizeof(raddr
);
1710 fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1711 pthread_testcancel();
1714 if (errno
!= EAGAIN
) {
1715 if (!quit
) // probably EBADF
1716 log_write("accept(): %s", strerror(errno
));
1728 gchar s
[INET6_ADDRSTRLEN
];
1730 inet_ntop(raddr
.ss_family
, get_in_addr((struct sockaddr
*)&raddr
),
1732 init_new_connection(fd
, s
);
1735 t
= get_key_file_string("global", "tcp_wait");
1736 n
= strtol(t
, NULL
, 10);
1745 /* Just in case accept() failed for some reason other than EBADF */
1747 pthread_exit(PTHREAD_CANCELED
);
1752 static void *accept_thread(void *arg
)
1754 gint sockfd
= (gint
)arg
;
1757 socklen_t slen
= sizeof(struct sockaddr_un
);
1758 struct sockaddr_un raddr
;
1761 fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1762 pthread_testcancel();
1765 if (errno
!= EAGAIN
) {
1766 if (!quit
) // probably EBADF
1767 log_write("accept(): %s", strerror(errno
));
1776 init_new_connection(fd
, NULL
);
1779 /* Just in case accept() failed for some reason other than EBADF */
1781 pthread_exit(PTHREAD_CANCELED
);
1785 static void *adjust_cache_timer_thread(void *arg
)
1789 pthread_testcancel();
1791 cache_adjust_timer();
1798 static void *keepalive_thread(void *arg
)
1801 gint to
= (gint
)arg
;
1804 clock_gettime(CLOCK_REALTIME
, &ts
);
1806 pthread_cond_timedwait(&keepalive_cond
, &keepalive_cond_mutex
, &ts
);
1807 pthread_testcancel();
1808 send_status_all(STATUS_KEEPALIVE
);
1814 static void startStopKeepAlive(gboolean term
)
1816 pthread_attr_t attr
;
1817 gint n
= get_key_file_integer("global", "keepalive");
1820 pthread_cancel(keepalive_tid
);
1823 pthread_mutex_trylock(&keepalive_cond_mutex
);
1829 pthread_attr_init(&attr
);
1830 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1831 pthread_create(&keepalive_tid
, &attr
, keepalive_thread
, (void *)n
);
1832 pthread_attr_destroy(&attr
);
1836 static void server_loop(gint sockfd
, gchar
**socketpath
)
1838 pthread_t accept_tid
;
1842 pthread_attr_t attr
;
1843 pthread_t cache_timeout_tid
;
1844 pthread_mutexattr_t mattr
;
1846 sigemptyset(&sigset
);
1849 sigaddset(&sigset
, SIGTERM
);
1850 sigaddset(&sigset
, SIGINT
);
1852 /* Clears the file cache. */
1853 sigaddset(&sigset
, SIGUSR1
);
1855 /* Configuration file reloading. */
1856 sigaddset(&sigset
, SIGHUP
);
1858 /* Caught in client_thread(). Sends a cache status message. */
1859 sigaddset(&sigset
, SIGUSR2
);
1861 /* Ignored everywhere. When a client disconnects abnormally this signal
1862 * gets raised. It isn't needed though because client_thread() will check
1863 * for rcs even after the client disconnects. */
1864 signal(SIGPIPE
, SIG_IGN
);
1865 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1867 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
1868 pthread_mutexattr_init(&mattr
);
1869 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
1870 pthread_mutex_init(&cn_mutex
, &mattr
);
1871 pthread_mutexattr_destroy(&mattr
);
1872 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1873 pthread_attr_init(&attr
);
1874 pthread_create(&accept_tid
, &attr
, accept_thread
, (void *)sockfd
);
1875 startStopKeepAlive(FALSE
);
1876 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1877 pthread_create(&cache_timeout_tid
, &attr
, adjust_cache_timer_thread
, NULL
);
1878 pthread_attr_destroy(&attr
);
1879 pthread_mutex_init(&reload_rcfile_mutex
, NULL
);
1884 sigwait(&sigset
, &sig
);
1885 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1887 /* Caught a signal. */
1894 cache_clear(NULL
, 2);
1902 log_write(N_("clearing file cache"));
1903 cache_clear(NULL
, 2);
1913 * We're out of the main server loop. This happens when a signal was sent
1914 * to terminate the daemon. We'll wait for all clients to disconnect
1915 * before exiting and ignore any following signals.
1917 shutdown(sockfd
, SHUT_RDWR
);
1919 pthread_cancel(accept_tid
);
1920 pthread_join(accept_tid
, NULL
);
1924 unlink(*socketpath
);
1925 g_free(*socketpath
);
1927 MUTEX_LOCK(&cn_mutex
);
1928 n
= g_slist_length(cn_thread_list
);
1929 MUTEX_UNLOCK(&cn_mutex
);
1932 log_write(N_("waiting for all clients to disconnect"));
1935 if (n
!= n_clients
) {
1936 log_write(N_("%i clients remain"), n
);
1941 MUTEX_LOCK(&cn_mutex
);
1942 n
= g_slist_length(cn_thread_list
);
1943 MUTEX_UNLOCK(&cn_mutex
);
1946 startStopKeepAlive(TRUE
);
1947 pthread_cancel(cache_timeout_tid
);
1952 * Called from pinentry_fork() in the child process.
1954 void free_client_list()
1956 gint i
, t
= g_slist_length(cn_thread_list
);
1958 for (i
= 0; i
< t
; i
++) {
1959 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1961 free_client(cn
->cl
);
1967 struct client_crypto_s
*init_client_crypto()
1969 struct client_crypto_s
*new = g_malloc0(sizeof(struct client_crypto_s
));
1973 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1977 rc
= gcry_cipher_open(&new->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
1980 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
1988 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1989 const gchar
*outfile
)
1994 struct client_crypto_s
*crypto
= init_client_crypto();
1997 return GPG_ERR_ENOMEM
;
1999 crypto
->key
= gcry_malloc(gcrykeysize
);
2002 cleanup_crypto(&crypto
);
2003 return GPG_ERR_ENOMEM
;
2006 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
2008 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
2013 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
2015 /* The header in version 1 had a bug where the iterations were off-by-one.
2016 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
2019 if (crypto
->fh
->fh1
.iter
>= 0) {
2021 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
2026 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
2027 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
2030 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
2038 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, &crypto
->fh
->doc
,
2044 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
2047 log_write("%s: %s", filename
, pwmd_strerror(rc
));
2051 crypto
->fh
->v1
= FALSE
;
2052 iter
= crypto
->fh
->fh1
.iter
;
2053 memset(&crypto
->fh
->fh2
, 0, sizeof(crypto
->fh
->fh2
));
2054 /* Keep the iterations and key from the original file. */
2055 crypto
->fh
->fh2
.iter
= iter
+1; // Bugfix for v1 data files.
2056 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
2060 send_error(NULL
, rc
);
2062 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
2063 cleanup_crypto(&crypto
);
2068 static gboolean
startStopTcp(gboolean term
)
2070 struct addrinfo hints
, *servinfo
, *p
;
2071 gint port
= get_key_file_integer("global", "tcp_port");
2074 pthread_attr_t attr
;
2076 if (term
|| get_key_file_boolean("global", "enable_tcp") == FALSE
) {
2077 if (tcpSockFd
!= -1) {
2078 pthread_cancel(tcpAcceptTid
);
2079 pthread_join(tcpAcceptTid
, NULL
);
2080 shutdown(tcpSockFd
, SHUT_RDWR
);
2084 /* A client may still be connected. */
2092 if (tcpSockFd
!= -1)
2095 memset(&hints
, 0, sizeof(hints
));
2096 hints
.ai_family
= AF_UNSPEC
;
2097 hints
.ai_socktype
= SOCK_STREAM
;
2098 hints
.ai_flags
= AI_PASSIVE
;
2100 if ((n
= getaddrinfo(NULL
, print_fmt(buf
, sizeof(buf
), "%i", port
),
2101 &hints
, &servinfo
)) == -1) {
2102 log_write("getaddrinfo(): %s", gai_strerror(n
));
2106 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
2107 if ((tcpSockFd
= socket(p
->ai_family
, p
->ai_socktype
,
2108 p
->ai_protocol
)) == -1) {
2109 log_write("socket(): %s", strerror(errno
));
2115 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_REUSEADDR
, &n
,
2116 sizeof(int)) == -1) {
2117 log_write("setsockopt(): %s", strerror(errno
));
2118 freeaddrinfo(servinfo
);
2122 if (bind(tcpSockFd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
2124 log_write("bind(): %s", strerror(errno
));
2132 freeaddrinfo(servinfo
);
2135 log_write("%s", N_("could not bind"));
2139 if (g_key_file_has_key(keyfileh
, "global", "tcp_interface", NULL
)) {
2140 gchar
*tmp
= get_key_file_string("global", "tcp_interface");
2142 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
, 1)
2144 log_write("setsockopt(): %s", strerror(errno
));
2152 if (!initTlsParams())
2155 if (listen(tcpSockFd
, 0) == -1) {
2156 log_write("listen(): %s", strerror(errno
));
2160 pthread_attr_init(&attr
);
2161 pthread_create(&tcpAcceptTid
, &attr
, tcp_accept_thread
, (void *)tcpSockFd
);
2162 pthread_attr_destroy(&attr
);
2168 if (tcpSockFd
!= -1)
2176 int main(int argc
, char *argv
[])
2179 struct sockaddr_un addr
;
2180 struct passwd
*pw
= getpwuid(getuid());
2181 gchar buf
[PATH_MAX
];
2182 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
2183 gchar
*socketarg
= NULL
;
2184 gchar
*datadir
= NULL
;
2187 gchar
**cache_push
= NULL
;
2189 gchar
*import
= NULL
, *keyfile
= NULL
;
2190 guint64 cmd_iterations
= -1;
2191 gint default_timeout
;
2192 gboolean rcfile_spec
= FALSE
;
2193 gint estatus
= EXIT_FAILURE
;
2195 gchar
*outfile
= NULL
;
2196 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
2198 gboolean secure
= FALSE
;
2200 gint background
= 1;
2201 gchar
*convert
= NULL
;
2202 pthread_mutexattr_t mattr
;
2203 #ifdef WITH_PINENTRY
2204 gboolean disable_pinentry
= FALSE
;
2207 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
2211 #ifdef HAVE_SETRLIMIT
2214 rl
.rlim_cur
= rl
.rlim_max
= 0;
2216 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
2217 err(EXIT_FAILURE
, "setrlimit()");
2223 setlocale(LC_ALL
, "");
2224 bindtextdomain("pwmd", LOCALEDIR
);
2232 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
2233 g_mem_set_vtable(&mtable
);
2234 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
2235 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
2237 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
2238 gcry_check_version(GCRYPT_VERSION
);
2240 gnutls_global_set_mem_functions(xmalloc
, xmalloc
, gcry_SecureCheck
,
2242 gnutls_global_init();
2243 gnutls_global_set_log_function(tls_log
);
2244 gnutls_global_set_log_level(1);
2245 assuan_set_io_hooks(&io_hooks
);
2250 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
2252 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2253 err(EXIT_FAILURE
, "%s", buf
);
2255 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
2257 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2258 err(EXIT_FAILURE
, "%s", buf
);
2260 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
2266 while ((opt
= getopt(argc
, argv
, "Po:C:nI:i:k:hvf:D")) != EOF
) {
2268 #ifdef WITH_PINENTRY
2270 disable_pinentry
= TRUE
;
2289 cmd_iterations
= strtol(optarg
, NULL
, 10);
2296 rcfile
= g_strdup(optarg
);
2300 printf(N_("%s\nCopyright (c) %s\nReleased under the terms of the GPL v2. Use at your own risk.\n\nCompile time features:\n%s"), PACKAGE_STRING
,
2302 #ifdef WITH_PINENTRY
2335 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2338 #ifdef WITH_PINENTRY
2339 if (disable_pinentry
== TRUE
)
2340 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2343 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2344 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2346 if (log_syslog
== TRUE
)
2347 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2349 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2350 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2353 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
2354 log_write("setpriority(): %s", strerror(errno
));
2359 #ifdef HAVE_MLOCKALL
2360 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2361 log_write("mlockall(): %s", strerror(errno
));
2372 opt
= convert_file(convert
, keyfile
, outfile
);
2373 g_key_file_free(keyfileh
);
2375 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2382 if (cmd_iterations
== -1)
2383 cmd_iterations
= (guint64
)get_key_file_integer("global", "iterations");
2385 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2386 g_key_file_free(keyfileh
);
2388 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2391 g_key_file_set_list_separator(keyfileh
, ',');
2393 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2394 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2398 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
2400 socketarg
= g_strdup(buf
);
2405 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2406 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2408 datadir
= expand_homedir(p
);
2411 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2412 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2413 disable_list_and_dump
= n
;
2416 disable_list_and_dump
= secure
;
2418 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2419 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2421 default_timeout
= -1;
2423 setup_logging(keyfileh
);
2425 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2426 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2428 if (argc
!= optind
) {
2430 ptotal
= g_strv_length(cache_push
);
2432 for (; optind
< argc
; optind
++) {
2433 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2434 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2438 if (strchr(socketarg
, '/') == NULL
) {
2439 socketdir
= g_get_current_dir();
2440 socketname
= g_strdup(socketarg
);
2441 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2444 socketname
= g_strdup(strrchr(socketarg
, '/'));
2446 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2447 socketdir
= g_strdup(socketarg
);
2448 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2451 if (chdir(datadir
)) {
2452 log_write("%s: %s", datadir
, strerror(errno
));
2457 if (parse_rcfile_keys() == FALSE
)
2460 clear_rcfile_keys();
2463 * Set the cache entry for a file. Prompts for the password.
2466 for (opt
= 0; cache_push
[opt
]; opt
++)
2467 do_cache_push(cache_push
[opt
], NULL
);
2469 g_strfreev(cache_push
);
2470 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2474 * bind() doesn't like the full pathname of the socket or any non alphanum
2475 * characters so change to the directory where the socket is wanted then
2476 * create it then change to datadir.
2478 if (chdir(socketdir
)) {
2479 log_write("%s: %s", socketdir
, strerror(errno
));
2485 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2486 log_write("socket(): %s", strerror(errno
));
2490 addr
.sun_family
= AF_UNIX
;
2491 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2493 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2494 log_write("bind(): %s", strerror(errno
));
2496 if (errno
== EADDRINUSE
)
2497 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2498 "stale socket. Please remove it manually."), socketpath
);
2504 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2505 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2506 mode_t mode
= strtol(t
, NULL
, 8);
2507 mode_t mask
= umask(0);
2511 if (chmod(socketname
, mode
) == -1) {
2512 log_write("%s: %s", socketname
, strerror(errno
));
2522 g_free(--socketname
);
2524 if (chdir(datadir
)) {
2525 log_write("%s: %s", datadir
, strerror(errno
));
2532 pthread_mutexattr_init(&mattr
);
2533 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
2534 pthread_mutex_init(&cache_mutex
, &mattr
);
2535 pthread_mutexattr_destroy(&mattr
);
2536 pthread_mutex_init(&keepalive_cond_mutex
, NULL
);
2537 pthread_cond_init(&keepalive_cond
, NULL
);
2538 #ifdef WITH_PINENTRY
2539 pthread_mutex_init(&pin_mutex
, NULL
);
2542 if (listen(sockfd
, 0) == -1) {
2543 log_write("listen(): %s", strerror(errno
));
2550 if (startStopTcp(FALSE
) == FALSE
)
2557 log_write("fork(): %s", strerror(errno
));
2570 server_loop(sockfd
, &socketpath
);
2571 estatus
= EXIT_SUCCESS
;
2574 if (socketpath
&& do_unlink
) {
2581 gnutls_global_deinit();
2584 g_key_file_free(keyfileh
);
2587 xmlCleanupGlobals();
2589 if (estatus
== EXIT_SUCCESS
)
2590 log_write(N_("pwmd exiting normally"));
2592 #if defined(DEBUG) && !defined(MEM_DEBUG)