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()
97 gboolean b
= disable_list_and_dump
;
100 log_write(N_("reloading configuration file '%s'"), rcfile
);
101 k
= parse_rcfile(FALSE
);
106 g_key_file_free(keyfileh
);
110 disable_list_and_dump
= b
;
114 startStopKeepAlive(FALSE
);
115 send_status_all(STATUS_CONFIG
);
118 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
120 gpg_error_t n
= gpg_error_from_errno(e
);
122 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, gpg_strerror(n
)));
125 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
127 gpg_err_code_t n
= gpg_err_code(e
);
128 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
129 struct client_s
*client
= assuan_get_pointer(ctx
);
132 return assuan_process_done(ctx
, 0);
135 log_write("%s", pwmd_strerror(e
));
139 if (n
== EPWMD_LIBXML_ERROR
) {
140 xmlErrorPtr xe
= client
->xml_error
;
143 xe
= xmlGetLastError();
145 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
146 log_write("%s", xe
->message
);
148 if (xe
== client
->xml_error
)
153 client
->xml_error
= NULL
;
157 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
160 static struct client_thread_s
*find_cn(pthread_t tid
)
167 pthread_mutex_lock(&cn_mutex
);
169 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
170 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
172 if (cn
->tid
== tid
) {
173 pthread_mutex_unlock(&cn_mutex
);
178 pthread_mutex_unlock(&cn_mutex
);
182 void log_write(const gchar
*fmt
, ...)
190 struct client_thread_s
*cn
;
194 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
197 if (!cmdline
&& logfile
) {
198 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
206 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
217 fprintf(stderr
, "%s\n", args
);
223 cn
= find_cn(pthread_self());
227 name
= print_fmt(buf
, sizeof(buf
), "%i: ", cn
->fd
);
229 name
= print_fmt(buf
, sizeof(buf
), "%s(%i): ", cn
->name
, cn
->fd
);
232 if (!cmdline
&& log_syslog
== TRUE
)
233 syslog(LOG_INFO
, "%s%s", name
, args
);
236 tm
= localtime(&now
);
237 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
238 tbuf
[sizeof(tbuf
) - 1] = 0;
240 if (args
[strlen(args
)-1] == '\n')
241 args
[strlen(args
)-1] = 0;
243 line
= g_strdup_printf("%s %i %s%s\n", tbuf
, getpid(), name
, args
);
254 write(fd
, line
, strlen(line
));
259 if (isatty(STDERR_FILENO
)) {
260 fprintf(stderr
, "%s", line
);
267 static void usage(gchar
*pn
)
269 g_fprintf(stderr
, N_(
270 "Usage: %s [-hvDnP] [-f <rcfile>] [-C <filename>] "
271 "[-I <filename> [-i <iter>]]\n "
272 "[-k <keyfile>] [-o <outfile>] [file1] [...]\n"
273 " -n run as a foreground process\n"
274 " -f load the specified rcfile (~/.pwmd/config)\n"
275 " -C convert a version 1 data file to version 2\n"
276 " -I import an XML file\n"
277 " -i encrypt with the specified number of iterations when importing\n"
278 " (config default in the \"global\" section)\n"
279 " -k obtain the key from the specified file\n"
280 " -o output file for use with the -C and -I options\n"
281 " -D disable use of the LIST and DUMP commands\n"
282 " -P disable pinentry\n"
284 " -h this help text\n"
289 static int gcry_SecureCheck(const void *ptr
)
294 static void setup_gcrypt()
296 gcry_check_version(NULL
);
298 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
301 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
303 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
305 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
306 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
309 static gint
new_connection(struct client_s
*cl
)
312 gchar ver
[ASSUAN_LINELENGTH
];
314 rc
= assuan_init_socket_server_ext(&cl
->ctx
, cl
->thd
->fd
, 2);
319 assuan_set_pointer(cl
->ctx
, cl
);
320 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
321 assuan_set_hello_line(cl
->ctx
, ver
);
322 rc
= register_commands(cl
->ctx
);
327 rc
= assuan_accept(cl
->ctx
);
335 log_write("%s", gpg_strerror(rc
));
339 gpg_error_t
send_status(assuan_context_t ctx
, status_msg_t which
,
340 const gchar
*fmt
, ...)
343 struct client_s
*client
= assuan_get_pointer(ctx
);
344 gchar buf
[ASSUAN_LINELENGTH
];
345 gchar
*status
= NULL
;
350 g_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
357 CACHE_LOCK(client
->ctx
);
358 line
= print_fmt(buf
, sizeof(buf
), "%i", cache_file_count());
363 MUTEX_LOCK(&cn_mutex
);
364 line
= print_fmt(buf
, sizeof(buf
), "%i", g_slist_length(cn_thread_list
));
365 MUTEX_UNLOCK(&cn_mutex
);
371 case STATUS_KEEPALIVE
:
372 status
= "KEEPALIVE";
376 line
= N_("Waiting for lock");
384 case STATUS_DECOMPRESS
:
385 status
= "DECOMPRESS";
387 case STATUS_COMPRESS
:
393 log_write("%s %s", status
, line
);
397 return assuan_write_status(ctx
, status
, line
);
400 /* This is needed for FreeBSD systems (and maybe others). I'm guessing here,
401 * but I think it lets the scheduler let other threads do their work. If we
402 * were to do the write() in send_status_all() then write() would return
403 * before the signal was actually sent to client_msg_thread(). This is only
404 * called from send_status_all().
406 static void *write_status_msg(void *arg
)
408 struct client_thread_s
*cn
=arg
;
411 pthread_cond_signal(&cn
->msg_cond
);
412 len
= write(cn
->msg_fd
[1], &cn
->msg
, sizeof(status_msg_t
));
414 if (len
!= sizeof(status_msg_t
))
415 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
420 void send_status_all(status_msg_t which
)
424 MUTEX_LOCK(&cn_mutex
);
426 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
427 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
431 pthread_create(&tid
, NULL
, write_status_msg
, cn
);
432 pthread_join(tid
, NULL
);
435 MUTEX_UNLOCK(&cn_mutex
);
438 static void xml_error_cb(void *data
, xmlErrorPtr e
)
440 struct client_s
*client
= data
;
443 * Keep the first reported error as the one to show in the error
444 * description. Reset in send_error().
446 if (client
->xml_error
)
449 xmlCopyError(e
, client
->xml_error
);
452 static void cleanup_crypto_handler(void *arg
)
454 struct client_crypto_s
**a
= (struct client_crypto_s
**)arg
;
455 struct client_crypto_s
*cr
= *a
;
458 if (cr
->fh
->fd
!= -1 ||
459 (cmdline
== TRUE
&& cr
->fh
->fd
!= STDOUT_FILENO
))
463 gcry_free(cr
->fh
->doc
);
469 gcry_cipher_close(cr
->gh
);
475 void cleanup_crypto(struct client_crypto_s
**c
)
477 struct client_crypto_s
*cr
= *c
;
492 gcry_free(cr
->tkey2
);
495 gcry_free(cr
->inbuf
);
498 gcry_free(cr
->outbuf
);
500 pthread_cleanup_push(cleanup_crypto_handler
, c
);
501 pthread_testcancel();
502 pthread_cleanup_pop(1);
506 * This is called after a child_thread terminates. Set with
507 * pthread_cleanup_push().
509 static void cleanup_cb(void *arg
)
511 struct client_thread_s
*cn
= arg
;
512 struct client_s
*cl
= cn
->cl
;
514 MUTEX_LOCK(&cn_mutex
);
515 log_write(N_("exiting, fd=%i"), cn
->fd
);
518 pthread_cancel(cn
->msg_tid
);
519 pthread_join(cn
->msg_tid
, NULL
);
520 pthread_cancel(cn
->msg_sender_tid
);
521 pthread_join(cn
->msg_sender_tid
, NULL
);
522 close(cl
->thd
->msg_fd
[0]);
523 close(cl
->thd
->msg_fd
[1]);
526 pthread_join(cn
->tid
, NULL
);
528 if (pthread_mutex_trylock(&cn
->msg_mutex
) == EBUSY
) {
529 MUTEX_UNLOCK(&cn
->msg_mutex
);
532 pthread_mutex_unlock(&cn
->msg_mutex
);
534 pthread_mutex_destroy(&cn
->msg_mutex
);
535 pthread_cond_destroy(&cn
->msg_cond
);
537 if (pthread_mutex_trylock(&cn
->msg_sender_mutex
) == EBUSY
) {
538 MUTEX_UNLOCK(&cn
->msg_sender_mutex
);
541 pthread_mutex_unlock(&cn
->msg_sender_mutex
);
543 pthread_mutex_destroy(&cn
->msg_sender_mutex
);
544 pthread_cond_destroy(&cn
->msg_sender_cond
);
547 struct status_msg_s
*m
= g_slist_nth_data(cn
->msg_queue
, 0);
552 cn
->msg_queue
= g_slist_remove(cn
->msg_queue
, m
);
556 if (cl
&& cl
->freed
== FALSE
)
561 gnutls_deinit(cn
->tls
->ses
);
571 assuan_deinit_server(cl
->ctx
);
574 if (cl
&& cl
->pinentry
)
575 cleanup_pinentry(cl
->pinentry
);
579 cleanup_crypto(&cl
->crypto
);
586 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
588 MUTEX_UNLOCK(&cn_mutex
);
589 send_status_all(STATUS_CLIENTS
);
592 static void *client_msg_sender_thread(void *arg
)
594 struct client_thread_s
*thd
= arg
;
596 MUTEX_LOCK(&thd
->msg_sender_mutex
);
599 struct status_msg_s
*msg
;
602 pthread_cond_wait(&thd
->msg_sender_cond
, &thd
->msg_sender_mutex
);
603 pthread_testcancel();
605 /* The messages may have been stacked while waiting for send_status()
606 * to return. Send what's in the queue. */
608 msg
= g_slist_nth_data(thd
->msg_queue
, 0);
613 /* Unlock to prevent blocking in client_msg_thread(). */
614 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
615 rc
= send_status(thd
->cl
->ctx
, msg
->msg
, NULL
);
616 MUTEX_LOCK(&thd
->msg_sender_mutex
);
617 thd
->msg_queue
= g_slist_remove(thd
->msg_queue
, msg
);
619 pthread_testcancel();
622 log_write(N_("msg for %i failed: %s"), thd
->fd
,
624 pthread_cancel(thd
->tid
);
633 * This function waits for a signal from send_status_all() then appends a
634 * message read from a pipe to the clients message queue. The actual sending
635 * of the message is done in client_msg_sender_thread() which waits for a
636 * signal from this function. This prevents blocking in assuan_send_status()
637 * when sending to remote clients. This also avoids duplicate status messages.
638 * If an existing status message in the message queue has the same type as the
639 * current message the the current one will be skipped. This avoids flooding
640 * the client with old status messages.
642 static void *client_msg_thread(void *arg
)
644 struct client_thread_s
*thd
= arg
;
646 MUTEX_LOCK(&thd
->msg_mutex
);
653 struct status_msg_s
*msg
;
655 pthread_cond_wait(&thd
->msg_cond
, &thd
->msg_mutex
);
656 pthread_testcancel();
658 FD_SET(thd
->msg_fd
[0], &rfds
);
659 n
= select(thd
->msg_fd
[0]+1, &rfds
, NULL
, NULL
, NULL
);
661 if (n
<= 0 || !FD_ISSET(thd
->msg_fd
[0], &rfds
))
664 len
= read(thd
->msg_fd
[0], &m
, sizeof(status_msg_t
));
665 pthread_testcancel();
667 if (len
!= sizeof(status_msg_t
)) {
668 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
672 MUTEX_LOCK(&thd
->msg_sender_mutex
);
674 for (n
= 0; n
< g_slist_length(thd
->msg_queue
); n
++) {
675 msg
= g_slist_nth_data(thd
->msg_queue
, n
);
681 msg
= g_malloc(sizeof(struct status_msg_s
));
684 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
685 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
690 thd
->msg_queue
= g_slist_append(thd
->msg_queue
, msg
);
692 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
693 pthread_cond_signal(&thd
->msg_sender_cond
);
700 * Called every time a connection is made from init_new_connection(). This is
701 * the thread entry point.
703 static void *client_thread(void *data
)
705 struct client_thread_s
*thd
= data
;
706 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
711 * Prevent a race condition with init_new_connection() if this thread
712 * fails (returns) for some reason before init_new_connection() releases
715 MUTEX_LOCK(&cn_mutex
);
716 MUTEX_UNLOCK(&cn_mutex
);
717 pthread_cleanup_push(cleanup_cb
, thd
);
720 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
728 * Do the TLS handshake before anything else.
731 gchar
*prio
= get_key_file_string("global", "cipher_suite");
733 thd
->tls
= tls_init(thd
->fd
, prio
);
745 if (new_connection(cl
))
749 cl
->pinentry
= pinentry_init();
753 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
758 /* Require the client to explicity set OPTION PINENTRY since the DISPLAY
759 * might be automatically set from the client. Connections to the X11
760 * server usually aren't encrypted and the client might unintentionally
761 * send the passphrase in the clear.
764 cl
->pinentry
->enable
= FALSE
;
769 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
770 log_write("mlockall(): %s", strerror(errno
));
775 if (pipe(thd
->msg_fd
) == -1) {
776 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
780 pthread_cond_init(&thd
->msg_cond
, NULL
);
781 pthread_mutex_init(&thd
->msg_mutex
, NULL
);
782 pthread_attr_init(&attr
);
783 pthread_create(&thd
->msg_tid
, &attr
, client_msg_thread
, thd
);
785 pthread_cond_init(&thd
->msg_sender_cond
, NULL
);
786 pthread_mutex_init(&thd
->msg_sender_mutex
, NULL
);
787 pthread_create(&thd
->msg_sender_tid
, &attr
, client_msg_sender_thread
, thd
);
788 pthread_attr_destroy(&attr
);
789 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
792 log_write("%s", gpg_strerror(rc
));
796 send_status_all(STATUS_CLIENTS
);
797 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
804 FD_SET(cl
->thd
->fd
, &rfds
);
807 if (cl
->pinentry
->status
== PINENTRY_RUNNING
)
808 FD_SET(cl
->pinentry
->fd
, &rfds
);
810 n
= cl
->thd
->fd
> cl
->pinentry
->fd
? cl
->thd
->fd
: cl
->pinentry
->fd
;
814 n
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
815 pthread_testcancel();
820 if (FD_ISSET(cl
->thd
->fd
, &rfds
)) {
821 rc
= assuan_process_next(cl
->ctx
);
822 pthread_testcancel();
825 cl
->inquire_status
= INQUIRE_INIT
;
827 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
830 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
831 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
834 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
840 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
841 cl
->pinentry
->status
= PINENTRY_RUNNING
;
844 switch (cl
->inquire_status
) {
849 cl
->inquire_status
= INQUIRE_INIT
;
850 rc
= assuan_process_done(cl
->ctx
, 0);
851 pthread_testcancel();
859 cl
->pinentry
->fd
!= -1 && FD_ISSET(cl
->pinentry
->fd
, &rfds
));
864 * Client cleanup (including XML data) is done in cleanup_cb() from
865 * the cleanup thread.
869 if (1) {} // Fixes compile time error with pthread_cleanup_push().
870 pthread_cleanup_pop(1);
871 pthread_exit(PTHREAD_CANCELED
);
875 static void setup_logging(GKeyFile
*kf
)
877 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
880 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
886 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
892 logfile
= g_strdup(buf
);
902 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
906 * Make sure all settings are set to either the specified setting or a
909 static void set_rcfile_defaults(GKeyFile
*kf
)
913 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
914 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
915 g_key_file_set_string(kf
, "global", "socket_path", buf
);
918 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
919 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
920 g_key_file_set_string(kf
, "global", "data_directory", buf
);
923 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
924 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
926 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
927 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
928 g_key_file_set_string(kf
, "global", "log_path", buf
);
931 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
932 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
935 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
936 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
939 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
940 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
942 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
943 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0)
944 g_key_file_set_integer(kf
, "global", "iterations", 1);
946 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
947 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
949 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
950 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
952 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
953 g_key_file_set_integer(kf
, "global", "compression_level", 6);
955 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
956 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
958 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
959 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
961 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
963 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
964 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
967 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
970 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
971 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
973 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
974 g_key_file_set_integer(kf
, "global", "keepalive", 30);
977 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
978 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
980 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
981 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
985 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
986 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
988 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
989 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
991 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
992 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
994 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
995 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
997 if (g_key_file_has_key(kf
, "global", "cipher_suite", NULL
) == FALSE
)
998 g_key_file_set_string(kf
, "global", "cipher_suite", "SECURE256");
1000 if (g_key_file_has_key(kf
, "global", "tcp_use_crl", NULL
) == FALSE
)
1001 g_key_file_set_boolean(kf
, "global", "tcp_use_crl", FALSE
);
1007 static GKeyFile
*parse_rcfile(gboolean specified
)
1009 GKeyFile
*kf
= g_key_file_new();
1012 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
1013 log_write("%s: %s", rcfile
, rc
->message
);
1015 if (cmdline
&& specified
) {
1020 if (rc
->code
== G_FILE_ERROR_NOENT
) {
1022 set_rcfile_defaults(kf
);
1030 set_rcfile_defaults(kf
);
1034 static gchar
*do_get_password(const gchar
*prompt
)
1036 gchar buf
[LINE_MAX
] = {0}, *p
;
1037 struct termios told
, tnew
;
1040 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
1041 log_write("tcgetattr(): %s", strerror(errno
));
1045 memcpy(&tnew
, &told
, sizeof(struct termios
));
1046 tnew
.c_lflag
&= ~(ECHO
);
1047 tnew
.c_lflag
|= ICANON
|ECHONL
;
1049 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
1050 log_write("tcsetattr(): %s", strerror(errno
));
1051 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1055 fprintf(stderr
, "%s", prompt
);
1057 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
1058 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1062 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1063 p
[strlen(p
) - 1] = 0;
1066 key
= gcry_malloc(1);
1070 key
= gcry_malloc(strlen(p
) + 1);
1071 sprintf(key
, "%s", p
);
1074 memset(&buf
, 0, sizeof(buf
));
1078 /* Only used when "enable_pinentry" is "false" or -P. */
1079 static gpg_error_t
get_input(const gchar
*filename
,
1080 struct client_crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
1084 if (which
== PINENTRY_SAVE
) {
1085 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
1086 crypto
->tkey
= do_get_password(prompt
);
1089 if (!crypto
->tkey
) {
1090 log_write(N_("%s: Skipping file"), filename
);
1091 return GPG_ERR_BAD_PASSPHRASE
;
1094 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
1095 crypto
->tkey2
= do_get_password(prompt
);
1098 if (!crypto
->tkey2
) {
1099 log_write(N_("%s: Skipping file"), filename
);
1100 return GPG_ERR_BAD_PASSPHRASE
;
1103 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
1104 log_write(N_("%s: Passphrase mismatch"), filename
);
1105 return EPWMD_BADKEY
;
1108 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
1109 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1113 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
1115 if ((crypto
->tkey
= do_get_password(prompt
)) == NULL
) {
1116 log_write(N_("%s: Skipping file"), filename
);
1118 return GPG_ERR_BAD_PASSPHRASE
;
1121 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
1122 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1128 * inbuf must have been allocated with gcry_malloc().
1130 gpg_error_t
export_common(const gchar
*filename
, struct client_crypto_s
*crypto
,
1131 gpointer inbuf
, gulong insize
)
1138 level
= get_key_file_integer(filename
, "compression_level");
1143 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
1145 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
1148 crypto
->inbuf
= outbuf
;
1149 crypto
->insize
= outsize
;
1150 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
1154 static gpg_error_t
get_password(const gchar
*filename
,
1155 struct client_crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
1156 pinentry_cmd_t which
)
1158 #ifdef WITH_PINENTRY
1161 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
1164 return get_input(filename
, crypto
, key
, which
);
1165 #ifdef WITH_PINENTRY
1168 gchar
*result
= NULL
;
1169 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
1171 set_pinentry_defaults(pin
);
1173 pin
->filename
= g_strdup(filename
);
1174 rc
= pinentry_getpin(pin
, &result
);
1181 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
1183 cleanup_pinentry(pin
);
1191 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
1194 gchar buf
[LINE_MAX
] = {0}, *p
;
1200 if ((fp
= fopen(file
, "r")) == NULL
) {
1201 *rc
= gpg_error_from_syserror();
1205 p
= fgets(buf
, sizeof(buf
), fp
);
1209 if (len
&& buf
[len
- 1] == '\n')
1212 str
= gcry_malloc(len
+ 1);
1215 *rc
= gpg_error_from_errno(ENOMEM
);
1219 memcpy(str
, buf
, len
? len
: 1);
1221 memset(&buf
, 0, sizeof(buf
));
1226 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
,
1230 gchar
*t
, *file
= NULL
, *str
;
1232 *rc
= GPG_ERR_UNKNOWN_ERRNO
;
1234 if (import
== FALSE
) {
1235 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1236 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1240 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1247 t
= expand_homedir(file
);
1250 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1251 *rc
= gpg_error_from_errno(ENOMEM
);
1260 /* -I or -C. The filename is a key file. */
1261 file
= g_strdup(filename
);
1264 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1265 *rc
= gpg_error_from_errno(ENOMEM
);
1271 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1279 if (_getline(file
, &str
, rc
) == FALSE
) {
1280 log_write("%s: %s: %s", filename
, file
, pwmd_strerror(*rc
));
1290 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1291 const gchar
*keyfile
, gulong iter
)
1300 struct client_crypto_s
*crypto
;
1302 if (stat(filename
, &st
) == -1) {
1303 log_write("%s: %s", filename
, strerror(errno
));
1307 crypto
= init_client_crypto();
1312 crypto
->key
= gcry_malloc(gcrykeysize
);
1313 memset(crypto
->key
, 0, gcrykeysize
);
1316 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1320 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
1323 if (iter
&& keyfile
) {
1324 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1329 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1330 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1333 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
1339 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1340 log_write("%s: %s", filename
, strerror(errno
));
1344 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1346 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1350 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
1354 log_write("%s: %s", filename
, strerror(errno
));
1359 xmlbuf
[st
.st_size
] = 0;
1362 * Make sure the document validates.
1364 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1365 log_write("xmlReadDoc() failed");
1371 xmlDocDumpMemory(doc
, &xml
, &len
);
1375 memset(crypto
->key
, '!', gcrykeysize
);
1377 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1380 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1384 crypto
->fh
->fh2
.iter
= iter
;
1385 rc
= export_common(outfile
, crypto
, xml
, len
);
1389 send_error(NULL
, rc
);
1393 cleanup_crypto(&crypto
);
1397 cleanup_crypto(&crypto
);
1401 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1406 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1407 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1410 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1411 g_clear_error(&grc
);
1415 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1416 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1419 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1420 g_clear_error(&grc
);
1428 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1433 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1434 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
1437 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1438 g_clear_error(&grc
);
1442 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1443 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1446 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1447 g_clear_error(&grc
);
1455 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1457 gboolean val
= FALSE
;
1460 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1461 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1464 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1465 g_clear_error(&grc
);
1469 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1470 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1473 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1474 g_clear_error(&grc
);
1482 static gboolean
parse_rcfile_keys()
1489 groups
= g_key_file_get_groups(keyfileh
, &n
);
1491 for (p
= groups
; *p
; p
++) {
1494 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1495 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1499 log_write("%s: key: %s", rcfile
, rc
->message
);
1505 do_cache_push(*p
, str
);
1511 log_write("%s: key: %s", rcfile
, rc
->message
);
1517 str
= parse_rcfile_keyfile(*p
, FALSE
, &ret
);
1522 do_cache_push(*p
, str
);
1530 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1534 const gchar
*p
= filename
;
1535 struct client_crypto_s
*crypto
;
1544 if (valid_filename(p
) == FALSE
) {
1545 log_write(N_("%s: Invalid characters in filename"), p
);
1549 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1551 if (access(p
, R_OK
|W_OK
) != 0) {
1552 log_write("%s: %s", p
, strerror(errno
));
1556 crypto
= init_client_crypto();
1561 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1564 log_write("%s: %s", p
, pwmd_strerror(rc
));
1565 cleanup_crypto(&crypto
);
1569 crypto
->key
= gcry_malloc(gcrykeysize
);
1572 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1573 cleanup_crypto(&crypto
);
1577 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1579 if (crypto
->fh
->fh2
.iter
<= 0) {
1580 memset(crypto
->key
, '!', gcrykeysize
);
1585 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1588 send_error(NULL
, rc
);
1589 cleanup_crypto(&crypto
);
1593 gcry_free(crypto
->fh
->doc
);
1594 crypto
->fh
->doc
= NULL
;
1597 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
,
1598 strlen(password
) ? strlen(password
) : 1);
1601 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, NULL
, NULL
);
1604 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1605 cleanup_crypto(&crypto
);
1609 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1610 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1611 cleanup_crypto(&crypto
);
1615 timeout
= get_key_file_integer(p
, "cache_timeout");
1616 cache_set_timeout(md5file
, timeout
);
1617 log_write(N_("File '%s' now cached"), filename
);
1618 cleanup_crypto(&crypto
);
1622 static void init_new_connection(gint fd
, gchar
*addr
)
1625 pthread_attr_t attr
;
1626 struct client_thread_s
*new;
1629 new = g_malloc0(sizeof(struct client_thread_s
));
1632 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1636 MUTEX_LOCK(&cn_mutex
);
1644 pthread_attr_init(&attr
);
1645 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1646 n
= pthread_create(&tid
, &attr
, client_thread
, new);
1647 pthread_attr_destroy(&attr
);
1651 log_write("pthread_create(): %s", strerror(n
));
1652 MUTEX_UNLOCK(&cn_mutex
);
1657 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1658 MUTEX_UNLOCK(&cn_mutex
);
1661 log_write(N_("new connection: fd=%i, addr=%s"), fd
, addr
);
1663 log_write(N_("new connection: fd=%i"), fd
);
1667 /* From Beej's Guide to Network Programming. It's a good tutorial. */
1668 static void *get_in_addr(struct sockaddr
*sa
)
1670 if (sa
->sa_family
== AF_INET
)
1671 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
1673 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
1676 static void *tcp_accept_thread(void *arg
)
1678 gint sockfd
= (gint
)arg
;
1681 struct sockaddr_storage raddr
;
1682 socklen_t slen
= sizeof(raddr
);
1687 if ((fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1688 if (errno
!= EAGAIN
) {
1689 if (!quit
) // probably EBADF
1690 log_write("accept(): %s", strerror(errno
));
1700 gchar s
[INET6_ADDRSTRLEN
];
1702 inet_ntop(raddr
.ss_family
, get_in_addr((struct sockaddr
*)&raddr
),
1704 init_new_connection(fd
, s
);
1707 t
= get_key_file_string("global", "tcp_wait");
1708 n
= strtol(t
, NULL
, 10);
1717 /* Just in case accept() failed for some reason other than EBADF */
1719 pthread_exit(PTHREAD_CANCELED
);
1724 static void *accept_thread(void *arg
)
1726 gint sockfd
= (gint
)arg
;
1729 socklen_t slen
= sizeof(struct sockaddr_un
);
1730 struct sockaddr_un raddr
;
1733 if ((fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1734 if (errno
!= EAGAIN
) {
1735 if (!quit
) // probably EBADF
1736 log_write("accept(): %s", strerror(errno
));
1743 init_new_connection(fd
, NULL
);
1746 /* Just in case accept() failed for some reason other than EBADF */
1748 pthread_exit(PTHREAD_CANCELED
);
1752 static void *adjust_cache_timer_thread(void *arg
)
1756 pthread_testcancel();
1758 cache_adjust_timer();
1765 static void *keepalive_thread(void *arg
)
1768 gint to
= (gint
)arg
;
1771 clock_gettime(CLOCK_REALTIME
, &ts
);
1773 pthread_cond_timedwait(&keepalive_cond
, &keepalive_cond_mutex
, &ts
);
1774 pthread_testcancel();
1775 send_status_all(STATUS_KEEPALIVE
);
1781 static void startStopKeepAlive(gboolean term
)
1783 pthread_attr_t attr
;
1784 gint n
= get_key_file_integer("global", "keepalive");
1787 pthread_cancel(keepalive_tid
);
1790 pthread_mutex_trylock(&keepalive_cond_mutex
);
1796 pthread_attr_init(&attr
);
1797 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1798 pthread_create(&keepalive_tid
, &attr
, keepalive_thread
, (void *)n
);
1799 pthread_attr_destroy(&attr
);
1803 static void server_loop(gint sockfd
, gchar
**socketpath
)
1805 pthread_t accept_tid
;
1809 pthread_attr_t attr
;
1810 pthread_t cache_timeout_tid
;
1811 pthread_mutexattr_t mattr
;
1813 sigemptyset(&sigset
);
1816 sigaddset(&sigset
, SIGTERM
);
1817 sigaddset(&sigset
, SIGINT
);
1819 /* Clears the file cache. */
1820 sigaddset(&sigset
, SIGUSR1
);
1822 /* Configuration file reloading. */
1823 sigaddset(&sigset
, SIGHUP
);
1825 /* Caught in client_thread(). Sends a cache status message. */
1826 sigaddset(&sigset
, SIGUSR2
);
1828 /* Ignored everywhere. When a client disconnects abnormally this signal
1829 * gets raised. It isn't needed though because client_thread() will check
1830 * for rcs even after the client disconnects. */
1831 signal(SIGPIPE
, SIG_IGN
);
1832 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1834 pthread_mutexattr_init(&mattr
);
1835 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
1836 pthread_mutex_init(&cn_mutex
, &mattr
);
1837 pthread_mutexattr_destroy(&mattr
);
1838 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1839 pthread_attr_init(&attr
);
1840 pthread_create(&accept_tid
, &attr
, accept_thread
, (void *)sockfd
);
1841 startStopKeepAlive(FALSE
);
1842 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1843 pthread_create(&cache_timeout_tid
, &attr
, adjust_cache_timer_thread
, NULL
);
1848 sigwait(&sigset
, &sig
);
1849 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1851 /* Caught a signal. */
1858 cache_clear(NULL
, 2);
1866 log_write(N_("clearing file cache"));
1867 cache_clear(NULL
, 2);
1872 shutdown(sockfd
, SHUT_RDWR
);
1876 if (tcpSockFd
!= -1) {
1877 shutdown(tcpSockFd
, SHUT_RDWR
);
1886 * We're out of the main server loop. This happens when a signal was sent
1887 * to terminate the daemon. We'll wait for all clients to disconnect
1888 * before exiting and ignore any following signals.
1890 pthread_join(accept_tid
, NULL
);
1891 pthread_attr_destroy(&attr
);
1895 unlink(*socketpath
);
1896 g_free(*socketpath
);
1898 MUTEX_LOCK(&cn_mutex
);
1899 n
= g_slist_length(cn_thread_list
);
1900 MUTEX_UNLOCK(&cn_mutex
);
1903 log_write(N_("waiting for all clients to disconnect"));
1906 if (n
!= n_clients
) {
1907 log_write(N_("%i clients remain"), n
);
1912 MUTEX_LOCK(&cn_mutex
);
1913 n
= g_slist_length(cn_thread_list
);
1914 MUTEX_UNLOCK(&cn_mutex
);
1917 startStopKeepAlive(TRUE
);
1918 pthread_cancel(cache_timeout_tid
);
1923 * Called from pinentry_fork() in the child process.
1925 void free_client_list()
1927 gint i
, t
= g_slist_length(cn_thread_list
);
1929 for (i
= 0; i
< t
; i
++) {
1930 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1932 free_client(cn
->cl
);
1938 struct client_crypto_s
*init_client_crypto()
1940 struct client_crypto_s
*new = g_malloc0(sizeof(struct client_crypto_s
));
1944 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1948 rc
= gcry_cipher_open(&new->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
1951 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
1959 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1960 const gchar
*outfile
)
1965 struct client_crypto_s
*crypto
= init_client_crypto();
1968 return GPG_ERR_ENOMEM
;
1970 crypto
->key
= gcry_malloc(gcrykeysize
);
1973 cleanup_crypto(&crypto
);
1974 return GPG_ERR_ENOMEM
;
1977 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
1979 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
1984 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
1986 /* The header in version 1 had a bug where the iterations were off-by-one.
1987 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
1990 if (crypto
->fh
->fh1
.iter
>= 0) {
1992 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1997 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1998 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
2001 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
2009 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, &crypto
->fh
->doc
,
2015 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
2018 log_write("%s: %s", filename
, pwmd_strerror(rc
));
2022 crypto
->fh
->v1
= FALSE
;
2023 iter
= crypto
->fh
->fh1
.iter
;
2024 memset(&crypto
->fh
->fh2
, 0, sizeof(crypto
->fh
->fh2
));
2025 /* Keep the iterations and key from the original file. */
2026 crypto
->fh
->fh2
.iter
= iter
+1; // Bugfix for v1 data files.
2027 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
2031 send_error(NULL
, rc
);
2033 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
2034 cleanup_crypto(&crypto
);
2039 static gboolean
startStopTcp(gboolean term
)
2041 struct addrinfo hints
, *servinfo
, *p
;
2042 gint port
= get_key_file_integer("global", "tcp_port");
2045 pthread_attr_t attr
;
2047 if (term
|| get_key_file_boolean("global", "enable_tcp") == FALSE
) {
2048 if (tcpSockFd
!= -1) {
2049 pthread_cancel(tcpAcceptTid
);
2050 pthread_join(tcpAcceptTid
, NULL
);
2054 /* A client may still be connected. */
2062 if (tcpSockFd
!= -1)
2065 memset(&hints
, 0, sizeof(hints
));
2066 hints
.ai_family
= AF_UNSPEC
;
2067 hints
.ai_socktype
= SOCK_STREAM
;
2068 hints
.ai_flags
= AI_PASSIVE
;
2070 if ((n
= getaddrinfo(NULL
, print_fmt(buf
, sizeof(buf
), "%i", port
),
2071 &hints
, &servinfo
)) == -1) {
2072 log_write("getaddrinfo(): %s", gai_strerror(n
));
2076 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
2077 if ((tcpSockFd
= socket(p
->ai_family
, p
->ai_socktype
,
2078 p
->ai_protocol
)) == -1) {
2079 log_write("socket(): %s", strerror(errno
));
2085 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_REUSEADDR
, &n
,
2086 sizeof(int)) == -1) {
2087 log_write("setsockopt(): %s", strerror(errno
));
2088 freeaddrinfo(servinfo
);
2092 if (bind(tcpSockFd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
2094 log_write("bind(): %s", strerror(errno
));
2102 freeaddrinfo(servinfo
);
2105 log_write("%s", N_("could not bind"));
2109 if (g_key_file_has_key(keyfileh
, "global", "tcp_interface", NULL
)) {
2110 gchar
*tmp
= get_key_file_string("global", "tcp_interface");
2112 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
, 1)
2114 log_write("setsockopt(): %s", strerror(errno
));
2122 if (!initTlsParams())
2125 if (listen(tcpSockFd
, 0) == -1) {
2126 log_write("listen(): %s", strerror(errno
));
2130 pthread_attr_init(&attr
);
2131 pthread_create(&tcpAcceptTid
, &attr
, tcp_accept_thread
, (void *)tcpSockFd
);
2132 pthread_attr_destroy(&attr
);
2138 if (tcpSockFd
!= -1)
2146 int main(int argc
, char *argv
[])
2149 struct sockaddr_un addr
;
2150 struct passwd
*pw
= getpwuid(getuid());
2151 gchar buf
[PATH_MAX
];
2152 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
2153 gchar
*socketarg
= NULL
;
2154 gchar
*datadir
= NULL
;
2157 gchar
**cache_push
= NULL
;
2159 gchar
*import
= NULL
, *keyfile
= NULL
;
2160 gulong cmd_iterations
= -1;
2161 gint default_timeout
;
2162 gboolean rcfile_spec
= FALSE
;
2163 gint estatus
= EXIT_FAILURE
;
2165 gchar
*outfile
= NULL
;
2166 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
2168 gboolean secure
= FALSE
;
2170 gint background
= 1;
2171 gchar
*convert
= NULL
;
2172 pthread_mutexattr_t mattr
;
2173 #ifdef WITH_PINENTRY
2174 gboolean disable_pinentry
= FALSE
;
2177 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
2181 #ifdef HAVE_SETRLIMIT
2184 rl
.rlim_cur
= rl
.rlim_max
= 0;
2186 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
2187 err(EXIT_FAILURE
, "setrlimit()");
2193 setlocale(LC_ALL
, "");
2194 bindtextdomain("pwmd", LOCALEDIR
);
2199 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
2200 g_mem_set_vtable(&mtable
);
2201 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
2202 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
2204 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
2205 gcry_check_version(GCRYPT_VERSION
);
2207 gnutls_global_set_mem_functions(xmalloc
, xmalloc
, gcry_SecureCheck
,
2209 gnutls_global_init();
2210 gnutls_global_set_log_function(tls_log
);
2211 gnutls_global_set_log_level(1);
2212 assuan_set_io_hooks(&io_hooks
);
2217 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
2219 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2220 err(EXIT_FAILURE
, "%s", buf
);
2222 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
2224 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2225 err(EXIT_FAILURE
, "%s", buf
);
2227 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
2233 while ((opt
= getopt(argc
, argv
, "Po:C:nI:i:k:hvf:D")) != EOF
) {
2235 #ifdef WITH_PINENTRY
2237 disable_pinentry
= TRUE
;
2256 cmd_iterations
= strtol(optarg
, NULL
, 10);
2263 rcfile
= g_strdup(optarg
);
2267 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
,
2269 #ifdef WITH_PINENTRY
2302 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2305 #ifdef WITH_PINENTRY
2306 if (disable_pinentry
== TRUE
)
2307 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2310 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2311 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2313 if (log_syslog
== TRUE
)
2314 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2316 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2317 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2320 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
2321 log_write("setpriority(): %s", strerror(errno
));
2326 #ifdef HAVE_MLOCKALL
2327 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2328 log_write("mlockall(): %s", strerror(errno
));
2339 opt
= convert_file(convert
, keyfile
, outfile
);
2340 g_key_file_free(keyfileh
);
2342 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2349 if (cmd_iterations
== -1)
2350 cmd_iterations
= get_key_file_integer("global", "iterations");
2352 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2353 g_key_file_free(keyfileh
);
2355 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2358 g_key_file_set_list_separator(keyfileh
, ',');
2360 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2361 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2365 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
2367 socketarg
= g_strdup(buf
);
2372 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2373 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2375 datadir
= expand_homedir(p
);
2378 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2379 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2380 disable_list_and_dump
= n
;
2383 disable_list_and_dump
= secure
;
2385 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2386 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2388 default_timeout
= -1;
2390 setup_logging(keyfileh
);
2392 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2393 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2395 if (argc
!= optind
) {
2397 ptotal
= g_strv_length(cache_push
);
2399 for (; optind
< argc
; optind
++) {
2400 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2401 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2405 if (strchr(socketarg
, '/') == NULL
) {
2406 socketdir
= g_get_current_dir();
2407 socketname
= g_strdup(socketarg
);
2408 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2411 socketname
= g_strdup(strrchr(socketarg
, '/'));
2413 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2414 socketdir
= g_strdup(socketarg
);
2415 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2418 if (chdir(datadir
)) {
2419 log_write("%s: %s", datadir
, strerror(errno
));
2424 if (parse_rcfile_keys() == FALSE
)
2427 clear_rcfile_keys();
2430 * Set the cache entry for a file. Prompts for the password.
2433 for (opt
= 0; cache_push
[opt
]; opt
++)
2434 do_cache_push(cache_push
[opt
], NULL
);
2436 g_strfreev(cache_push
);
2437 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2441 * bind() doesn't like the full pathname of the socket or any non alphanum
2442 * characters so change to the directory where the socket is wanted then
2443 * create it then change to datadir.
2445 if (chdir(socketdir
)) {
2446 log_write("%s: %s", socketdir
, strerror(errno
));
2452 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2453 log_write("socket(): %s", strerror(errno
));
2457 addr
.sun_family
= AF_UNIX
;
2458 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2460 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2461 log_write("bind(): %s", strerror(errno
));
2463 if (errno
== EADDRINUSE
)
2464 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2465 "stale socket. Please remove it manually."), socketpath
);
2471 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2472 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2473 mode_t mode
= strtol(t
, NULL
, 8);
2474 mode_t mask
= umask(0);
2478 if (chmod(socketname
, mode
) == -1) {
2479 log_write("%s: %s", socketname
, strerror(errno
));
2489 g_free(--socketname
);
2491 if (chdir(datadir
)) {
2492 log_write("%s: %s", datadir
, strerror(errno
));
2499 pthread_mutexattr_init(&mattr
);
2500 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
2501 pthread_mutex_init(&cache_mutex
, &mattr
);
2502 pthread_mutexattr_destroy(&mattr
);
2503 pthread_mutex_init(&keepalive_cond_mutex
, NULL
);
2504 pthread_cond_init(&keepalive_cond
, NULL
);
2505 #ifdef WITH_PINENTRY
2506 pthread_mutex_init(&pin_mutex
, NULL
);
2509 if (listen(sockfd
, 0) == -1) {
2510 log_write("listen(): %s", strerror(errno
));
2517 if (startStopTcp(FALSE
) == FALSE
)
2524 log_write("fork(): %s", strerror(errno
));
2537 server_loop(sockfd
, &socketpath
);
2538 estatus
= EXIT_SUCCESS
;
2541 if (socketpath
&& do_unlink
) {
2548 gnutls_global_deinit();
2551 g_key_file_free(keyfileh
);
2554 xmlCleanupGlobals();
2556 if (estatus
== EXIT_SUCCESS
)
2557 log_write(N_("pwmd exiting normally"));
2559 #if defined(DEBUG) && !defined(MEM_DEBUG)