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(&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(&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 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
186 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
195 void log_write(const gchar
*fmt
, ...)
203 struct client_thread_s
*cn
;
207 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
210 if (!cmdline
&& logfile
) {
211 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
219 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
230 fprintf(stderr
, "%s\n", args
);
236 pthread_mutex_lock(&cn_mutex
);
237 cn
= find_cn(pthread_self());
241 name
= print_fmt(buf
, sizeof(buf
), "%i: ", cn
->fd
);
243 name
= print_fmt(buf
, sizeof(buf
), "%s(%i): ", cn
->name
, cn
->fd
);
246 pthread_mutex_unlock(&cn_mutex
);
248 if (!cmdline
&& log_syslog
== TRUE
)
249 syslog(LOG_INFO
, "%s%s", name
, args
);
252 tm
= localtime(&now
);
253 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
254 tbuf
[sizeof(tbuf
) - 1] = 0;
256 if (args
[strlen(args
)-1] == '\n')
257 args
[strlen(args
)-1] = 0;
259 line
= g_strdup_printf("%s %i %s%s\n", tbuf
, getpid(), name
, args
);
270 write(fd
, line
, strlen(line
));
275 if (isatty(STDERR_FILENO
)) {
276 fprintf(stderr
, "%s", line
);
283 static void usage(gchar
*pn
)
285 g_fprintf(stderr
, N_(
286 "Usage: %s [-hvDnP] [-f <rcfile>] [-C <filename>] "
287 "[-I <filename> [-i <iter>]]\n "
288 "[-k <keyfile>] [-o <outfile>] [file1] [...]\n"
289 " -n run as a foreground process\n"
290 " -f load the specified rcfile (~/.pwmd/config)\n"
291 " -C convert a version 1 data file to version 2\n"
292 " -I import an XML file\n"
293 " -i encrypt with the specified number of iterations when importing\n"
294 " (config default in the \"global\" section)\n"
295 " -k obtain the key from the specified file\n"
296 " -o output file for use with the -C and -I options\n"
297 " -D disable use of the LIST and DUMP commands\n"
298 " -P disable pinentry\n"
300 " -h this help text\n"
305 static int gcry_SecureCheck(const void *ptr
)
310 static void setup_gcrypt()
312 gcry_check_version(NULL
);
314 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
317 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
319 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
321 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
322 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
325 static gint
new_connection(struct client_s
*cl
)
328 gchar ver
[ASSUAN_LINELENGTH
];
330 rc
= assuan_init_socket_server_ext(&cl
->ctx
, cl
->thd
->fd
, 2);
335 assuan_set_pointer(cl
->ctx
, cl
);
336 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
337 assuan_set_hello_line(cl
->ctx
, ver
);
338 rc
= register_commands(cl
->ctx
);
343 rc
= assuan_accept(cl
->ctx
);
351 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
;
536 log_write(N_("exiting, fd=%i"), cn
->fd
);
538 if (cl
->thd
&& (cl
->thd
->msg_fd
[0] || cl
->thd
->msg_fd
[1])) {
539 pthread_cancel(cn
->msg_tid
);
540 pthread_cancel(cn
->msg_sender_tid
);
541 pthread_join(cn
->msg_sender_tid
, NULL
);
542 pthread_cond_destroy(&cn
->msg_cond
);
543 close(cl
->thd
->msg_fd
[0]);
544 close(cl
->thd
->msg_fd
[1]);
547 pthread_join(cn
->tid
, NULL
);
548 MUTEX_LOCK(&cn_mutex
);
549 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
550 MUTEX_UNLOCK(&cn_mutex
);
552 if (pthread_mutex_trylock(&cn
->msg_sender_mutex
) == EBUSY
) {
553 MUTEX_UNLOCK(&cn
->msg_sender_mutex
);
556 pthread_mutex_unlock(&cn
->msg_sender_mutex
);
558 pthread_mutex_destroy(&cn
->msg_sender_mutex
);
559 pthread_cond_destroy(&cn
->msg_sender_cond
);
562 struct status_msg_s
*m
= g_slist_nth_data(cn
->msg_queue
, 0);
567 cn
->msg_queue
= g_slist_remove(cn
->msg_queue
, m
);
571 if (cl
&& cl
->freed
== FALSE
)
576 gnutls_deinit(cn
->tls
->ses
);
586 assuan_deinit_server(cl
->ctx
);
587 else if (cl
&& cl
->thd
&& cl
->thd
->fd
!= -1)
591 if (cl
&& cl
->pinentry
)
592 cleanup_pinentry(cl
->pinentry
);
596 cleanup_crypto(&cl
->crypto
);
604 send_status_all(STATUS_CLIENTS
);
607 /* The msg_sender_mutex should have been locked before calling this thread
609 static void *client_msg_sender_thread(void *arg
)
611 struct client_thread_s
*thd
= arg
;
614 struct status_msg_s
*msg
;
617 pthread_cond_wait(&thd
->msg_sender_cond
, &thd
->msg_sender_mutex
);
618 pthread_testcancel();
620 /* The messages may have been stacked while waiting for send_status()
621 * to return. Send what's in the queue. */
623 msg
= g_slist_nth_data(thd
->msg_queue
, 0);
628 /* Unlock to prevent blocking in client_msg_thread(). */
629 MUTEX_UNLOCK(&thd
->msg_sender_mutex
);
630 rc
= send_status(thd
->cl
->ctx
, msg
->msg
, NULL
);
631 MUTEX_LOCK(&thd
->msg_sender_mutex
);
632 thd
->msg_queue
= g_slist_remove(thd
->msg_queue
, msg
);
634 pthread_testcancel();
637 log_write(N_("msg for %i failed: %s"), thd
->fd
,
639 pthread_cancel(thd
->tid
);
649 * This function waits for a signal from send_status_all() then appends a
650 * message read from a pipe to the clients message queue. The actual sending
651 * of the message is done in client_msg_sender_thread() which waits for a
652 * signal from this function. This prevents blocking in assuan_send_status()
653 * when sending to remote clients. This also avoids duplicate status messages.
654 * If an existing status message in the message queue has the same type as the
655 * current message the the current one will be skipped. This avoids flooding
656 * the client with old status messages.
658 static void *client_msg_thread(void *arg
)
660 struct client_thread_s
*thd
= arg
;
663 pthread_mutex_init(&mu
, NULL
);
664 pthread_mutex_lock(&mu
);
671 struct status_msg_s
*msg
;
673 pthread_cleanup_push(pthread_mutex_destroy
, &mu
);
674 pthread_cleanup_push(pthread_mutex_unlock
, &mu
);
675 pthread_cond_wait(&thd
->msg_cond
, &mu
);
676 pthread_testcancel();
678 FD_SET(thd
->msg_fd
[0], &rfds
);
679 n
= select(thd
->msg_fd
[0]+1, &rfds
, NULL
, NULL
, NULL
);
680 pthread_testcancel();
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
);
715 pthread_cleanup_pop(0);
716 pthread_cleanup_pop(0);
723 * Called every time a connection is made from init_new_connection(). This is
724 * the thread entry point.
726 static void *client_thread(void *data
)
728 struct client_thread_s
*thd
= data
;
729 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
734 * Prevent a race condition with init_new_connection() if this thread
735 * fails (returns) for some reason before init_new_connection() releases
738 MUTEX_LOCK(&cn_mutex
);
739 MUTEX_UNLOCK(&cn_mutex
);
740 pthread_cleanup_push(cleanup_cb
, thd
);
743 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
751 * Do the TLS handshake before anything else.
754 gchar
*prio
= get_key_file_string("global", "cipher_suite");
756 thd
->tls
= tls_init(thd
->fd
, prio
);
765 pthread_testcancel();
771 if (new_connection(cl
))
775 cl
->pinentry
= pinentry_init();
779 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
784 /* Require the client to explicity set OPTION PINENTRY since the DISPLAY
785 * might be automatically set from the client. Connections to the X11
786 * server usually aren't encrypted and the client might unintentionally
787 * send the passphrase in the clear.
790 cl
->pinentry
->enable
= FALSE
;
795 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
796 log_write("mlockall(): %s", strerror(errno
));
801 if (pipe(thd
->msg_fd
) == -1) {
802 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
806 pthread_attr_init(&attr
);
807 pthread_cond_init(&thd
->msg_sender_cond
, NULL
);
808 pthread_mutex_init(&thd
->msg_sender_mutex
, NULL
);
809 MUTEX_LOCK(&thd
->msg_sender_mutex
);
810 pthread_create(&thd
->msg_sender_tid
, &attr
, client_msg_sender_thread
, thd
);
811 pthread_attr_destroy(&attr
);
813 pthread_cond_init(&thd
->msg_cond
, NULL
);
814 pthread_attr_init(&attr
);
815 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
816 pthread_create(&thd
->msg_tid
, &attr
, client_msg_thread
, thd
);
817 pthread_attr_destroy(&attr
);
819 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
822 log_write("%s", gpg_strerror(rc
));
826 send_status_all(STATUS_CLIENTS
);
827 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
834 FD_SET(cl
->thd
->fd
, &rfds
);
837 if (cl
->pinentry
->status
== PINENTRY_RUNNING
)
838 FD_SET(cl
->pinentry
->fd
, &rfds
);
840 n
= cl
->thd
->fd
> cl
->pinentry
->fd
? cl
->thd
->fd
: cl
->pinentry
->fd
;
844 n
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
845 pthread_testcancel();
850 if (FD_ISSET(cl
->thd
->fd
, &rfds
)) {
851 rc
= assuan_process_next(cl
->ctx
);
852 pthread_testcancel();
855 cl
->inquire_status
= INQUIRE_INIT
;
857 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
860 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
861 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
864 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
870 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
871 cl
->pinentry
->status
= PINENTRY_RUNNING
;
874 switch (cl
->inquire_status
) {
879 cl
->inquire_status
= INQUIRE_INIT
;
880 pthread_testcancel();
881 rc
= assuan_process_done(cl
->ctx
, 0);
889 cl
->pinentry
->fd
!= -1 && FD_ISSET(cl
->pinentry
->fd
, &rfds
));
894 * Client cleanup (including XML data) is done in cleanup_cb() from
895 * the cleanup thread.
899 if (1) {} // Fixes compile time error with pthread_cleanup_push().
900 pthread_cleanup_pop(1);
901 pthread_exit(PTHREAD_CANCELED
);
905 static void setup_logging(GKeyFile
*kf
)
907 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
910 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
916 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
922 logfile
= g_strdup(buf
);
932 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
936 * Make sure all settings are set to either the specified setting or a
939 static void set_rcfile_defaults(GKeyFile
*kf
)
943 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
944 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
945 g_key_file_set_string(kf
, "global", "socket_path", buf
);
948 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
949 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
950 g_key_file_set_string(kf
, "global", "data_directory", buf
);
953 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
954 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
956 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
957 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
958 g_key_file_set_string(kf
, "global", "log_path", buf
);
961 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
962 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
965 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
966 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
969 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
970 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
972 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
973 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0)
974 g_key_file_set_integer(kf
, "global", "iterations", 1);
976 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
977 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
979 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
980 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
982 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
983 g_key_file_set_integer(kf
, "global", "compression_level", 6);
985 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
986 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
988 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
989 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
991 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
993 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
994 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
997 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
1000 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
1001 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
1003 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
1004 g_key_file_set_integer(kf
, "global", "keepalive", 30);
1006 #ifdef WITH_PINENTRY
1007 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
1008 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
1010 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
1011 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
1015 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
1016 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
1018 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
1019 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
1021 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
1022 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
1024 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
1025 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
1027 if (g_key_file_has_key(kf
, "global", "cipher_suite", NULL
) == FALSE
)
1028 g_key_file_set_string(kf
, "global", "cipher_suite", "SECURE256");
1030 if (g_key_file_has_key(kf
, "global", "tcp_use_crl", NULL
) == FALSE
)
1031 g_key_file_set_boolean(kf
, "global", "tcp_use_crl", FALSE
);
1037 static GKeyFile
*parse_rcfile(gboolean specified
)
1039 GKeyFile
*kf
= g_key_file_new();
1042 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
1043 log_write("%s: %s", rcfile
, rc
->message
);
1045 if (cmdline
&& specified
) {
1050 if (rc
->code
== G_FILE_ERROR_NOENT
) {
1052 set_rcfile_defaults(kf
);
1060 set_rcfile_defaults(kf
);
1064 static gchar
*do_get_password(const gchar
*prompt
)
1066 gchar buf
[LINE_MAX
] = {0}, *p
;
1067 struct termios told
, tnew
;
1070 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
1071 log_write("tcgetattr(): %s", strerror(errno
));
1075 memcpy(&tnew
, &told
, sizeof(struct termios
));
1076 tnew
.c_lflag
&= ~(ECHO
);
1077 tnew
.c_lflag
|= ICANON
|ECHONL
;
1079 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
1080 log_write("tcsetattr(): %s", strerror(errno
));
1081 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1085 fprintf(stderr
, "%s", prompt
);
1087 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
1088 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1092 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
1093 p
[strlen(p
) - 1] = 0;
1096 key
= gcry_malloc(1);
1100 key
= gcry_malloc(strlen(p
) + 1);
1101 sprintf(key
, "%s", p
);
1104 memset(&buf
, 0, sizeof(buf
));
1108 /* Only used when "enable_pinentry" is "false" or -P. */
1109 static gpg_error_t
get_input(const gchar
*filename
,
1110 struct client_crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
1114 if (which
== PINENTRY_SAVE
) {
1115 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
1116 crypto
->tkey
= do_get_password(prompt
);
1119 if (!crypto
->tkey
) {
1120 log_write(N_("%s: Skipping file"), filename
);
1121 return GPG_ERR_BAD_PASSPHRASE
;
1124 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
1125 crypto
->tkey2
= do_get_password(prompt
);
1128 if (!crypto
->tkey2
) {
1129 log_write(N_("%s: Skipping file"), filename
);
1130 return GPG_ERR_BAD_PASSPHRASE
;
1133 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
1134 log_write(N_("%s: Passphrase mismatch"), filename
);
1135 return EPWMD_BADKEY
;
1138 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
1139 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1143 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
1145 if ((crypto
->tkey
= do_get_password(prompt
)) == NULL
) {
1146 log_write(N_("%s: Skipping file"), filename
);
1148 return GPG_ERR_BAD_PASSPHRASE
;
1151 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
1152 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1158 * inbuf must have been allocated with gcry_malloc().
1160 gpg_error_t
export_common(const gchar
*filename
, struct client_crypto_s
*crypto
,
1161 gpointer inbuf
, gulong insize
)
1168 level
= get_key_file_integer(filename
, "compression_level");
1173 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
1175 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
1178 crypto
->inbuf
= outbuf
;
1179 crypto
->insize
= outsize
;
1180 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
1184 static gpg_error_t
get_password(const gchar
*filename
,
1185 struct client_crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
1186 pinentry_cmd_t which
)
1188 #ifdef WITH_PINENTRY
1191 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
1194 return get_input(filename
, crypto
, key
, which
);
1195 #ifdef WITH_PINENTRY
1198 gchar
*result
= NULL
;
1199 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
1201 set_pinentry_defaults(pin
);
1203 pin
->filename
= g_strdup(filename
);
1204 rc
= pinentry_getpin(pin
, &result
);
1211 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
1213 cleanup_pinentry(pin
);
1221 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
1224 gchar buf
[LINE_MAX
] = {0}, *p
;
1230 if ((fp
= fopen(file
, "r")) == NULL
) {
1231 *rc
= gpg_error_from_syserror();
1235 p
= fgets(buf
, sizeof(buf
), fp
);
1239 if (len
&& buf
[len
- 1] == '\n')
1242 str
= gcry_malloc(len
+ 1);
1245 *rc
= gpg_error_from_errno(ENOMEM
);
1249 memcpy(str
, buf
, len
? len
: 1);
1251 memset(&buf
, 0, sizeof(buf
));
1256 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
,
1260 gchar
*t
, *file
= NULL
, *str
;
1262 *rc
= GPG_ERR_UNKNOWN_ERRNO
;
1264 if (import
== FALSE
) {
1265 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1266 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1270 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1277 t
= expand_homedir(file
);
1280 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1281 *rc
= gpg_error_from_errno(ENOMEM
);
1290 /* -I or -C. The filename is a key file. */
1291 file
= g_strdup(filename
);
1294 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1295 *rc
= gpg_error_from_errno(ENOMEM
);
1301 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1309 if (_getline(file
, &str
, rc
) == FALSE
) {
1310 log_write("%s: %s: %s", filename
, file
, pwmd_strerror(*rc
));
1320 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1321 const gchar
*keyfile
, guint64 iter
)
1330 struct client_crypto_s
*crypto
;
1332 if (stat(filename
, &st
) == -1) {
1333 log_write("%s: %s", filename
, strerror(errno
));
1337 crypto
= init_client_crypto();
1342 crypto
->key
= gcry_malloc(gcrykeysize
);
1343 memset(crypto
->key
, 0, gcrykeysize
);
1346 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1350 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
1353 if (iter
&& keyfile
) {
1354 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1359 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1360 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1363 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
1369 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1370 log_write("%s: %s", filename
, strerror(errno
));
1374 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1376 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1380 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
1384 log_write("%s: %s", filename
, strerror(errno
));
1389 xmlbuf
[st
.st_size
] = 0;
1392 * Make sure the document validates.
1394 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1395 log_write("xmlReadDoc() failed");
1401 xmlDocDumpMemory(doc
, &xml
, &len
);
1405 memset(crypto
->key
, '!', gcrykeysize
);
1407 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1410 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1414 crypto
->fh
->fh2
.iter
= iter
;
1415 rc
= export_common(outfile
, crypto
, xml
, len
);
1419 send_error(NULL
, rc
);
1423 cleanup_crypto(&crypto
);
1427 cleanup_crypto(&crypto
);
1431 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1436 MUTEX_LOCK(&rcfile_mutex
);
1438 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1439 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1442 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1443 g_clear_error(&grc
);
1447 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1448 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1451 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1452 g_clear_error(&grc
);
1457 MUTEX_UNLOCK(&rcfile_mutex
);
1461 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1466 MUTEX_LOCK(&rcfile_mutex
);
1468 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1469 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
1472 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1473 g_clear_error(&grc
);
1477 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1478 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1481 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1482 g_clear_error(&grc
);
1487 MUTEX_UNLOCK(&rcfile_mutex
);
1491 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1493 gboolean val
= FALSE
;
1496 MUTEX_LOCK(&rcfile_mutex
);
1498 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1499 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1502 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1503 g_clear_error(&grc
);
1507 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1508 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1511 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1512 g_clear_error(&grc
);
1517 MUTEX_UNLOCK(&rcfile_mutex
);
1521 static gboolean
parse_rcfile_keys()
1528 groups
= g_key_file_get_groups(keyfileh
, &n
);
1530 for (p
= groups
; *p
; p
++) {
1533 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1534 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1538 log_write("%s: key: %s", rcfile
, rc
->message
);
1544 do_cache_push(*p
, str
);
1550 log_write("%s: key: %s", rcfile
, rc
->message
);
1556 str
= parse_rcfile_keyfile(*p
, FALSE
, &ret
);
1561 do_cache_push(*p
, str
);
1569 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1573 const gchar
*p
= filename
;
1574 struct client_crypto_s
*crypto
;
1583 if (valid_filename(p
) == FALSE
) {
1584 log_write(N_("%s: Invalid characters in filename"), p
);
1588 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1590 if (access(p
, R_OK
|W_OK
) != 0) {
1591 log_write("%s: %s", p
, strerror(errno
));
1595 crypto
= init_client_crypto();
1600 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1603 log_write("%s: %s", p
, pwmd_strerror(rc
));
1604 cleanup_crypto(&crypto
);
1608 crypto
->key
= gcry_malloc(gcrykeysize
);
1611 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1612 cleanup_crypto(&crypto
);
1616 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1618 if (crypto
->fh
->fh2
.iter
<= 0) {
1619 memset(crypto
->key
, '!', gcrykeysize
);
1624 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1627 send_error(NULL
, rc
);
1628 cleanup_crypto(&crypto
);
1632 gcry_free(crypto
->fh
->doc
);
1633 crypto
->fh
->doc
= NULL
;
1636 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
,
1637 strlen(password
) ? strlen(password
) : 1);
1640 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, NULL
, NULL
);
1643 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1644 cleanup_crypto(&crypto
);
1648 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1649 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1650 cleanup_crypto(&crypto
);
1654 timeout
= get_key_file_integer(p
, "cache_timeout");
1655 cache_set_timeout(md5file
, timeout
);
1656 log_write(N_("File '%s' now cached"), filename
);
1657 cleanup_crypto(&crypto
);
1661 static void init_new_connection(gint fd
, gchar
*addr
)
1664 pthread_attr_t attr
;
1665 struct client_thread_s
*new;
1668 new = g_malloc0(sizeof(struct client_thread_s
));
1671 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1676 MUTEX_LOCK(&cn_mutex
);
1684 pthread_attr_init(&attr
);
1685 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1686 n
= pthread_create(&tid
, &attr
, client_thread
, new);
1687 pthread_attr_destroy(&attr
);
1692 log_write("pthread_create(): %s", strerror(n
));
1693 MUTEX_UNLOCK(&cn_mutex
);
1698 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1699 MUTEX_UNLOCK(&cn_mutex
);
1702 log_write(N_("new connection: fd=%i, addr=%s"), fd
, addr
);
1704 log_write(N_("new connection: fd=%i"), fd
);
1708 /* From Beej's Guide to Network Programming. It's a good tutorial. */
1709 static void *get_in_addr(struct sockaddr
*sa
)
1711 if (sa
->sa_family
== AF_INET
)
1712 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
1714 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
1717 static void *tcp_accept_thread(void *arg
)
1719 gint sockfd
= (gint
)arg
;
1722 struct sockaddr_storage raddr
;
1723 socklen_t slen
= sizeof(raddr
);
1728 fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1729 pthread_testcancel();
1732 if (errno
!= EAGAIN
) {
1733 if (!quit
) // probably EBADF
1734 log_write("accept(): %s", strerror(errno
));
1746 gchar s
[INET6_ADDRSTRLEN
];
1748 inet_ntop(raddr
.ss_family
, get_in_addr((struct sockaddr
*)&raddr
),
1750 init_new_connection(fd
, s
);
1753 t
= get_key_file_string("global", "tcp_wait");
1754 n
= strtol(t
, NULL
, 10);
1763 /* Just in case accept() failed for some reason other than EBADF */
1765 pthread_exit(PTHREAD_CANCELED
);
1770 static void *accept_thread(void *arg
)
1772 gint sockfd
= (gint
)arg
;
1775 socklen_t slen
= sizeof(struct sockaddr_un
);
1776 struct sockaddr_un raddr
;
1779 fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1780 pthread_testcancel();
1783 if (errno
!= EAGAIN
) {
1784 if (!quit
) // probably EBADF
1785 log_write("accept(): %s", strerror(errno
));
1794 init_new_connection(fd
, NULL
);
1797 /* Just in case accept() failed for some reason other than EBADF */
1799 pthread_exit(PTHREAD_CANCELED
);
1803 static void *adjust_cache_timer_thread(void *arg
)
1807 pthread_testcancel();
1809 cache_adjust_timer();
1816 static void *keepalive_thread(void *arg
)
1819 gint to
= (gint
)arg
;
1822 clock_gettime(CLOCK_REALTIME
, &ts
);
1824 pthread_cond_timedwait(&keepalive_cond
, &keepalive_cond_mutex
, &ts
);
1825 pthread_testcancel();
1826 send_status_all(STATUS_KEEPALIVE
);
1832 static void startStopKeepAlive(gboolean term
)
1834 pthread_attr_t attr
;
1835 gint n
= get_key_file_integer("global", "keepalive");
1838 pthread_cancel(keepalive_tid
);
1841 pthread_mutex_trylock(&keepalive_cond_mutex
);
1847 pthread_attr_init(&attr
);
1848 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1849 pthread_create(&keepalive_tid
, &attr
, keepalive_thread
, (void *)n
);
1850 pthread_attr_destroy(&attr
);
1854 static void server_loop(gint sockfd
, gchar
**socketpath
)
1856 pthread_t accept_tid
;
1860 pthread_attr_t attr
;
1861 pthread_t cache_timeout_tid
;
1862 pthread_mutexattr_t mattr
;
1864 sigemptyset(&sigset
);
1867 sigaddset(&sigset
, SIGTERM
);
1868 sigaddset(&sigset
, SIGINT
);
1870 /* Clears the file cache. */
1871 sigaddset(&sigset
, SIGUSR1
);
1873 /* Configuration file reloading. */
1874 sigaddset(&sigset
, SIGHUP
);
1876 /* Caught in client_thread(). Sends a cache status message. */
1877 sigaddset(&sigset
, SIGUSR2
);
1879 /* Ignored everywhere. When a client disconnects abnormally this signal
1880 * gets raised. It isn't needed though because client_thread() will check
1881 * for rcs even after the client disconnects. */
1882 signal(SIGPIPE
, SIG_IGN
);
1883 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1885 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
1886 pthread_mutexattr_init(&mattr
);
1887 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
1888 pthread_mutex_init(&cn_mutex
, &mattr
);
1889 pthread_mutexattr_destroy(&mattr
);
1890 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1891 pthread_attr_init(&attr
);
1892 pthread_create(&accept_tid
, &attr
, accept_thread
, (void *)sockfd
);
1893 startStopKeepAlive(FALSE
);
1894 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1895 pthread_create(&cache_timeout_tid
, &attr
, adjust_cache_timer_thread
, NULL
);
1896 pthread_attr_destroy(&attr
);
1901 sigwait(&sigset
, &sig
);
1902 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1904 /* Caught a signal. */
1911 cache_clear(NULL
, 2);
1919 log_write(N_("clearing file cache"));
1920 cache_clear(NULL
, 2);
1930 * We're out of the main server loop. This happens when a signal was sent
1931 * to terminate the daemon. We'll wait for all clients to disconnect
1932 * before exiting and ignore any following signals.
1934 shutdown(sockfd
, SHUT_RDWR
);
1936 pthread_cancel(accept_tid
);
1937 pthread_join(accept_tid
, NULL
);
1941 unlink(*socketpath
);
1942 g_free(*socketpath
);
1944 MUTEX_LOCK(&cn_mutex
);
1945 n
= g_slist_length(cn_thread_list
);
1946 MUTEX_UNLOCK(&cn_mutex
);
1949 log_write(N_("waiting for all clients to disconnect"));
1952 if (n
!= n_clients
) {
1953 log_write(N_("%i clients remain"), n
);
1958 MUTEX_LOCK(&cn_mutex
);
1959 n
= g_slist_length(cn_thread_list
);
1960 MUTEX_UNLOCK(&cn_mutex
);
1963 startStopKeepAlive(TRUE
);
1964 pthread_cancel(cache_timeout_tid
);
1969 * Called from pinentry_fork() in the child process.
1971 void free_client_list()
1973 gint i
, t
= g_slist_length(cn_thread_list
);
1975 for (i
= 0; i
< t
; i
++) {
1976 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1978 free_client(cn
->cl
);
1984 struct client_crypto_s
*init_client_crypto()
1986 struct client_crypto_s
*new = g_malloc0(sizeof(struct client_crypto_s
));
1990 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1994 rc
= gcry_cipher_open(&new->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
1997 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2005 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
2006 const gchar
*outfile
)
2011 struct client_crypto_s
*crypto
= init_client_crypto();
2014 return GPG_ERR_ENOMEM
;
2016 crypto
->key
= gcry_malloc(gcrykeysize
);
2019 cleanup_crypto(&crypto
);
2020 return GPG_ERR_ENOMEM
;
2023 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
2025 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
2030 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
2032 /* The header in version 1 had a bug where the iterations were off-by-one.
2033 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
2036 if (crypto
->fh
->fh1
.iter
>= 0) {
2038 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
2043 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
2044 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
2047 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
2055 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, &crypto
->fh
->doc
,
2061 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
2064 log_write("%s: %s", filename
, pwmd_strerror(rc
));
2068 crypto
->fh
->v1
= FALSE
;
2069 iter
= crypto
->fh
->fh1
.iter
;
2070 memset(&crypto
->fh
->fh2
, 0, sizeof(crypto
->fh
->fh2
));
2071 /* Keep the iterations and key from the original file. */
2072 crypto
->fh
->fh2
.iter
= iter
+1; // Bugfix for v1 data files.
2073 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
2077 send_error(NULL
, rc
);
2079 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
2080 cleanup_crypto(&crypto
);
2085 static gboolean
startStopTcp(gboolean term
)
2087 struct addrinfo hints
, *servinfo
, *p
;
2088 gint port
= get_key_file_integer("global", "tcp_port");
2091 pthread_attr_t attr
;
2093 if (term
|| get_key_file_boolean("global", "enable_tcp") == FALSE
) {
2094 if (tcpSockFd
!= -1) {
2095 pthread_cancel(tcpAcceptTid
);
2096 pthread_join(tcpAcceptTid
, NULL
);
2097 shutdown(tcpSockFd
, SHUT_RDWR
);
2101 /* A client may still be connected. */
2109 if (tcpSockFd
!= -1)
2112 memset(&hints
, 0, sizeof(hints
));
2113 hints
.ai_family
= AF_UNSPEC
;
2114 hints
.ai_socktype
= SOCK_STREAM
;
2115 hints
.ai_flags
= AI_PASSIVE
;
2117 if ((n
= getaddrinfo(NULL
, print_fmt(buf
, sizeof(buf
), "%i", port
),
2118 &hints
, &servinfo
)) == -1) {
2119 log_write("getaddrinfo(): %s", gai_strerror(n
));
2123 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
2124 if ((tcpSockFd
= socket(p
->ai_family
, p
->ai_socktype
,
2125 p
->ai_protocol
)) == -1) {
2126 log_write("socket(): %s", strerror(errno
));
2132 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_REUSEADDR
, &n
,
2133 sizeof(int)) == -1) {
2134 log_write("setsockopt(): %s", strerror(errno
));
2135 freeaddrinfo(servinfo
);
2139 if (bind(tcpSockFd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
2141 log_write("bind(): %s", strerror(errno
));
2149 freeaddrinfo(servinfo
);
2152 log_write("%s", N_("could not bind"));
2156 if (g_key_file_has_key(keyfileh
, "global", "tcp_interface", NULL
)) {
2157 gchar
*tmp
= get_key_file_string("global", "tcp_interface");
2159 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
, 1)
2161 log_write("setsockopt(): %s", strerror(errno
));
2169 if (!initTlsParams())
2172 if (listen(tcpSockFd
, 0) == -1) {
2173 log_write("listen(): %s", strerror(errno
));
2177 pthread_attr_init(&attr
);
2178 pthread_create(&tcpAcceptTid
, &attr
, tcp_accept_thread
, (void *)tcpSockFd
);
2179 pthread_attr_destroy(&attr
);
2185 if (tcpSockFd
!= -1)
2193 int main(int argc
, char *argv
[])
2196 struct sockaddr_un addr
;
2197 struct passwd
*pw
= getpwuid(getuid());
2198 gchar buf
[PATH_MAX
];
2199 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
2200 gchar
*socketarg
= NULL
;
2201 gchar
*datadir
= NULL
;
2204 gchar
**cache_push
= NULL
;
2206 gchar
*import
= NULL
, *keyfile
= NULL
;
2207 guint64 cmd_iterations
= -1;
2208 gint default_timeout
;
2209 gboolean rcfile_spec
= FALSE
;
2210 gint estatus
= EXIT_FAILURE
;
2212 gchar
*outfile
= NULL
;
2213 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
2215 gboolean secure
= FALSE
;
2217 gint background
= 1;
2218 gchar
*convert
= NULL
;
2219 pthread_mutexattr_t mattr
;
2220 #ifdef WITH_PINENTRY
2221 gboolean disable_pinentry
= FALSE
;
2224 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
2228 #ifdef HAVE_SETRLIMIT
2231 rl
.rlim_cur
= rl
.rlim_max
= 0;
2233 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
2234 err(EXIT_FAILURE
, "setrlimit()");
2240 setlocale(LC_ALL
, "");
2241 bindtextdomain("pwmd", LOCALEDIR
);
2249 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
2250 g_mem_set_vtable(&mtable
);
2251 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
2252 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
2254 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
2255 gcry_check_version(GCRYPT_VERSION
);
2257 gnutls_global_set_mem_functions(xmalloc
, xmalloc
, gcry_SecureCheck
,
2259 gnutls_global_init();
2260 gnutls_global_set_log_function(tls_log
);
2261 gnutls_global_set_log_level(1);
2262 assuan_set_io_hooks(&io_hooks
);
2267 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
2269 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2270 err(EXIT_FAILURE
, "%s", buf
);
2272 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
2274 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2275 err(EXIT_FAILURE
, "%s", buf
);
2277 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
2283 while ((opt
= getopt(argc
, argv
, "Po:C:nI:i:k:hvf:D")) != EOF
) {
2285 #ifdef WITH_PINENTRY
2287 disable_pinentry
= TRUE
;
2306 cmd_iterations
= strtol(optarg
, NULL
, 10);
2313 rcfile
= g_strdup(optarg
);
2317 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
,
2319 #ifdef WITH_PINENTRY
2352 pthread_mutexattr_init(&mattr
);
2353 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
2354 pthread_mutex_init(&rcfile_mutex
, &mattr
);
2355 pthread_mutexattr_destroy(&mattr
);
2357 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2360 #ifdef WITH_PINENTRY
2361 if (disable_pinentry
== TRUE
)
2362 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2365 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2366 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2368 if (log_syslog
== TRUE
)
2369 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2371 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2372 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2375 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
2376 log_write("setpriority(): %s", strerror(errno
));
2381 #ifdef HAVE_MLOCKALL
2382 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2383 log_write("mlockall(): %s", strerror(errno
));
2394 opt
= convert_file(convert
, keyfile
, outfile
);
2395 g_key_file_free(keyfileh
);
2397 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2404 if (cmd_iterations
== -1)
2405 cmd_iterations
= (guint64
)get_key_file_integer("global", "iterations");
2407 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2408 g_key_file_free(keyfileh
);
2410 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2413 g_key_file_set_list_separator(keyfileh
, ',');
2415 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2416 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2420 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
2422 socketarg
= g_strdup(buf
);
2427 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2428 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2430 datadir
= expand_homedir(p
);
2433 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2434 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2435 disable_list_and_dump
= n
;
2438 disable_list_and_dump
= secure
;
2440 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2441 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2443 default_timeout
= -1;
2445 setup_logging(keyfileh
);
2447 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2448 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2450 if (argc
!= optind
) {
2452 ptotal
= g_strv_length(cache_push
);
2454 for (; optind
< argc
; optind
++) {
2455 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2456 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2460 if (strchr(socketarg
, '/') == NULL
) {
2461 socketdir
= g_get_current_dir();
2462 socketname
= g_strdup(socketarg
);
2463 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2466 socketname
= g_strdup(strrchr(socketarg
, '/'));
2468 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2469 socketdir
= g_strdup(socketarg
);
2470 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2473 if (chdir(datadir
)) {
2474 log_write("%s: %s", datadir
, strerror(errno
));
2479 if (parse_rcfile_keys() == FALSE
)
2482 clear_rcfile_keys();
2485 * Set the cache entry for a file. Prompts for the password.
2488 for (opt
= 0; cache_push
[opt
]; opt
++)
2489 do_cache_push(cache_push
[opt
], NULL
);
2491 g_strfreev(cache_push
);
2492 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2496 * bind() doesn't like the full pathname of the socket or any non alphanum
2497 * characters so change to the directory where the socket is wanted then
2498 * create it then change to datadir.
2500 if (chdir(socketdir
)) {
2501 log_write("%s: %s", socketdir
, strerror(errno
));
2507 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2508 log_write("socket(): %s", strerror(errno
));
2512 addr
.sun_family
= AF_UNIX
;
2513 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2515 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2516 log_write("bind(): %s", strerror(errno
));
2518 if (errno
== EADDRINUSE
)
2519 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2520 "stale socket. Please remove it manually."), socketpath
);
2526 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2527 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2528 mode_t mode
= strtol(t
, NULL
, 8);
2529 mode_t mask
= umask(0);
2533 if (chmod(socketname
, mode
) == -1) {
2534 log_write("%s: %s", socketname
, strerror(errno
));
2544 g_free(--socketname
);
2546 if (chdir(datadir
)) {
2547 log_write("%s: %s", datadir
, strerror(errno
));
2554 pthread_mutexattr_init(&mattr
);
2555 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
2556 pthread_mutex_init(&cache_mutex
, &mattr
);
2557 pthread_mutexattr_destroy(&mattr
);
2558 pthread_mutex_init(&keepalive_cond_mutex
, NULL
);
2559 pthread_cond_init(&keepalive_cond
, NULL
);
2560 #ifdef WITH_PINENTRY
2561 pthread_mutex_init(&pin_mutex
, NULL
);
2564 if (listen(sockfd
, 0) == -1) {
2565 log_write("listen(): %s", strerror(errno
));
2572 if (startStopTcp(FALSE
) == FALSE
)
2579 log_write("fork(): %s", strerror(errno
));
2592 server_loop(sockfd
, &socketpath
);
2593 estatus
= EXIT_SUCCESS
;
2596 if (socketpath
&& do_unlink
) {
2603 gnutls_global_deinit();
2606 g_key_file_free(keyfileh
);
2609 xmlCleanupGlobals();
2611 if (estatus
== EXIT_SUCCESS
)
2612 log_write(N_("pwmd exiting normally"));
2614 #if defined(DEBUG) && !defined(MEM_DEBUG)