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 static void xml_error_cb(void *data
, xmlErrorPtr e
)
359 struct client_s
*client
= data
;
362 * Keep the first reported error as the one to show in the error
363 * description. Reset in send_error().
365 if (client
->xml_error
)
368 xmlCopyError(e
, client
->xml_error
);
371 static void cleanup_crypto_handler(void *arg
)
373 struct client_crypto_s
**a
= (struct client_crypto_s
**)arg
;
374 struct client_crypto_s
*cr
= *a
;
377 if (cr
->fh
->fd
!= -1 ||
378 (cmdline
== TRUE
&& cr
->fh
->fd
!= STDOUT_FILENO
))
382 gcry_free(cr
->fh
->doc
);
388 gcry_cipher_close(cr
->gh
);
394 void cleanup_crypto(struct client_crypto_s
**c
)
396 struct client_crypto_s
*cr
= *c
;
411 gcry_free(cr
->tkey2
);
414 gcry_free(cr
->inbuf
);
417 gcry_free(cr
->outbuf
);
419 pthread_cleanup_push(cleanup_crypto_handler
, c
);
420 pthread_testcancel();
421 pthread_cleanup_pop(1);
425 * This is called after a child_thread terminates. Set with
426 * pthread_cleanup_push().
428 static void cleanup_cb(void *arg
)
430 struct client_thread_s
*cn
= arg
;
431 struct client_s
*cl
= cn
->cl
;
434 log_write(N_("exiting, fd=%i"), cn
->fd
);
436 if (cl
->thd
&& (cl
->thd
->msg_fd
[0] || cl
->thd
->msg_fd
[1])) {
437 pthread_cancel(cn
->msg_tid
);
438 pthread_cancel(cn
->msg_sender_tid
);
439 pthread_join(cn
->msg_sender_tid
, NULL
);
440 pthread_cond_destroy(&cn
->msg_cond
);
441 close(cl
->thd
->msg_fd
[0]);
442 close(cl
->thd
->msg_fd
[1]);
445 pthread_join(cn
->tid
, NULL
);
446 MUTEX_LOCK(&cn_mutex
);
447 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
448 MUTEX_UNLOCK(&cn_mutex
);
450 if (pthread_mutex_trylock(&cn
->msg_sender_mutex
) == EBUSY
) {
451 MUTEX_UNLOCK(&cn
->msg_sender_mutex
);
454 pthread_mutex_unlock(&cn
->msg_sender_mutex
);
456 pthread_mutex_destroy(&cn
->msg_sender_mutex
);
457 pthread_cond_destroy(&cn
->msg_sender_cond
);
460 struct status_msg_s
*m
= g_slist_nth_data(cn
->msg_queue
, 0);
465 cn
->msg_queue
= g_slist_remove(cn
->msg_queue
, m
);
469 if (cl
&& cl
->freed
== FALSE
)
474 gnutls_deinit(cn
->tls
->ses
);
484 assuan_deinit_server(cl
->ctx
);
485 else if (cl
&& cl
->thd
&& cl
->thd
->fd
!= -1)
489 if (cl
&& cl
->pinentry
)
490 cleanup_pinentry(cl
->pinentry
);
494 cleanup_crypto(&cl
->crypto
);
502 send_status_all(STATUS_CLIENTS
);
506 * Called every time a connection is made from init_new_connection(). This is
507 * the thread entry point.
509 static void *client_thread(void *data
)
511 struct client_thread_s
*thd
= data
;
512 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
517 * Prevent a race condition with init_new_connection() if this thread
518 * fails (returns) for some reason before init_new_connection() releases
521 MUTEX_LOCK(&cn_mutex
);
522 MUTEX_UNLOCK(&cn_mutex
);
523 pthread_cleanup_push(cleanup_cb
, thd
);
526 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
534 * Do the TLS handshake before anything else.
537 gchar
*prio
= get_key_file_string("global", "cipher_suite");
539 thd
->tls
= tls_init(thd
->fd
, prio
);
548 pthread_testcancel();
554 if (new_connection(cl
))
558 cl
->pinentry
= pinentry_init();
562 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
567 /* Require the client to explicity set OPTION PINENTRY since the DISPLAY
568 * might be automatically set from the client. Connections to the X11
569 * server usually aren't encrypted and the client might unintentionally
570 * send the passphrase in the clear.
573 cl
->pinentry
->enable
= FALSE
;
578 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
579 log_write("mlockall(): %s", strerror(errno
));
584 if (pipe(thd
->msg_fd
) == -1) {
585 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
589 pthread_attr_init(&attr
);
590 pthread_cond_init(&thd
->msg_sender_cond
, NULL
);
591 pthread_mutex_init(&thd
->msg_sender_mutex
, NULL
);
592 MUTEX_LOCK(&thd
->msg_sender_mutex
);
593 pthread_create(&thd
->msg_sender_tid
, &attr
, client_msg_sender_thread
, thd
);
594 pthread_attr_destroy(&attr
);
596 pthread_cond_init(&thd
->msg_cond
, NULL
);
597 pthread_attr_init(&attr
);
598 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
599 pthread_create(&thd
->msg_tid
, &attr
, client_msg_thread
, thd
);
600 pthread_attr_destroy(&attr
);
602 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
605 log_write("%s", gpg_strerror(rc
));
609 send_status_all(STATUS_CLIENTS
);
610 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
617 FD_SET(cl
->thd
->fd
, &rfds
);
620 if (cl
->pinentry
->status
== PINENTRY_RUNNING
)
621 FD_SET(cl
->pinentry
->fd
, &rfds
);
623 n
= cl
->thd
->fd
> cl
->pinentry
->fd
? cl
->thd
->fd
: cl
->pinentry
->fd
;
627 n
= select(n
+1, &rfds
, NULL
, NULL
, NULL
);
628 pthread_testcancel();
633 if (FD_ISSET(cl
->thd
->fd
, &rfds
)) {
634 rc
= assuan_process_next(cl
->ctx
);
635 pthread_testcancel();
638 cl
->inquire_status
= INQUIRE_INIT
;
640 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
643 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
644 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
647 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
653 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
654 cl
->pinentry
->status
= PINENTRY_RUNNING
;
657 switch (cl
->inquire_status
) {
662 cl
->inquire_status
= INQUIRE_INIT
;
663 pthread_testcancel();
664 rc
= assuan_process_done(cl
->ctx
, 0);
672 cl
->pinentry
->fd
!= -1 && FD_ISSET(cl
->pinentry
->fd
, &rfds
));
677 * Client cleanup (including XML data) is done in cleanup_cb() from
678 * the cleanup thread.
682 if (1) {} // Fixes compile time error with pthread_cleanup_push().
683 pthread_cleanup_pop(1);
684 pthread_exit(PTHREAD_CANCELED
);
688 static void setup_logging(GKeyFile
*kf
)
690 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
693 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
699 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
705 logfile
= g_strdup(buf
);
715 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
719 * Make sure all settings are set to either the specified setting or a
722 static void set_rcfile_defaults(GKeyFile
*kf
)
726 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
727 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
728 g_key_file_set_string(kf
, "global", "socket_path", buf
);
731 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
732 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
733 g_key_file_set_string(kf
, "global", "data_directory", buf
);
736 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
737 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
739 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
740 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
741 g_key_file_set_string(kf
, "global", "log_path", buf
);
744 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
745 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
748 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
749 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
752 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
753 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
755 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
756 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0)
757 g_key_file_set_integer(kf
, "global", "iterations", 1);
759 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
760 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
762 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
763 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
765 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
766 g_key_file_set_integer(kf
, "global", "compression_level", 6);
768 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
769 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
771 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
772 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
774 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
776 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
777 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
780 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
783 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
784 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
786 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
787 g_key_file_set_integer(kf
, "global", "keepalive", DEFAULT_KEEPALIVE_TO
);
790 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
791 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
793 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
794 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
798 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
799 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
801 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
802 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
804 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
805 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
807 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
808 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
810 if (g_key_file_has_key(kf
, "global", "cipher_suite", NULL
) == FALSE
)
811 g_key_file_set_string(kf
, "global", "cipher_suite", "SECURE256");
813 if (g_key_file_has_key(kf
, "global", "tcp_use_crl", NULL
) == FALSE
)
814 g_key_file_set_boolean(kf
, "global", "tcp_use_crl", FALSE
);
820 static GKeyFile
*parse_rcfile(gboolean specified
)
822 GKeyFile
*kf
= g_key_file_new();
825 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
826 log_write("%s: %s", rcfile
, rc
->message
);
828 if (cmdline
&& specified
) {
833 if (rc
->code
== G_FILE_ERROR_NOENT
) {
835 set_rcfile_defaults(kf
);
843 set_rcfile_defaults(kf
);
847 static gchar
*do_get_password(const gchar
*prompt
)
849 gchar buf
[LINE_MAX
] = {0}, *p
;
850 struct termios told
, tnew
;
853 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
854 log_write("tcgetattr(): %s", strerror(errno
));
858 memcpy(&tnew
, &told
, sizeof(struct termios
));
859 tnew
.c_lflag
&= ~(ECHO
);
860 tnew
.c_lflag
|= ICANON
|ECHONL
;
862 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
863 log_write("tcsetattr(): %s", strerror(errno
));
864 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
868 fprintf(stderr
, "%s", prompt
);
870 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
871 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
875 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
876 p
[strlen(p
) - 1] = 0;
879 key
= gcry_malloc(1);
883 key
= gcry_malloc(strlen(p
) + 1);
884 sprintf(key
, "%s", p
);
887 memset(&buf
, 0, sizeof(buf
));
891 /* Only used when "enable_pinentry" is "false" or -P. */
892 static gpg_error_t
get_input(const gchar
*filename
,
893 struct client_crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
897 if (which
== PINENTRY_SAVE
) {
898 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
899 crypto
->tkey
= do_get_password(prompt
);
903 log_write(N_("%s: Skipping file"), filename
);
904 return GPG_ERR_BAD_PASSPHRASE
;
907 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
908 crypto
->tkey2
= do_get_password(prompt
);
911 if (!crypto
->tkey2
) {
912 log_write(N_("%s: Skipping file"), filename
);
913 return GPG_ERR_BAD_PASSPHRASE
;
916 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
917 log_write(N_("%s: Passphrase mismatch"), filename
);
921 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
922 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
926 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
928 if ((crypto
->tkey
= do_get_password(prompt
)) == NULL
) {
929 log_write(N_("%s: Skipping file"), filename
);
931 return GPG_ERR_BAD_PASSPHRASE
;
934 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
935 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
941 * inbuf must have been allocated with gcry_malloc().
943 gpg_error_t
export_common(const gchar
*filename
, struct client_crypto_s
*crypto
,
944 gpointer inbuf
, gulong insize
)
951 level
= get_key_file_integer(filename
, "compression_level");
956 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
958 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
961 crypto
->inbuf
= outbuf
;
962 crypto
->insize
= outsize
;
963 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
967 static gpg_error_t
get_password(const gchar
*filename
,
968 struct client_crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
969 pinentry_cmd_t which
)
974 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
977 return get_input(filename
, crypto
, key
, which
);
981 gchar
*result
= NULL
;
982 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
984 set_pinentry_defaults(pin
);
986 pin
->filename
= g_strdup(filename
);
987 rc
= pinentry_getpin(pin
, &result
);
994 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
996 cleanup_pinentry(pin
);
1004 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
1007 gchar buf
[LINE_MAX
] = {0}, *p
;
1013 if ((fp
= fopen(file
, "r")) == NULL
) {
1014 *rc
= gpg_error_from_syserror();
1018 p
= fgets(buf
, sizeof(buf
), fp
);
1022 if (len
&& buf
[len
- 1] == '\n')
1025 str
= gcry_malloc(len
+ 1);
1028 *rc
= gpg_error_from_errno(ENOMEM
);
1032 memcpy(str
, buf
, len
? len
: 1);
1034 memset(&buf
, 0, sizeof(buf
));
1039 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
,
1043 gchar
*t
, *file
= NULL
, *str
;
1045 *rc
= GPG_ERR_UNKNOWN_ERRNO
;
1047 if (import
== FALSE
) {
1048 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1049 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1053 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1060 t
= expand_homedir(file
);
1063 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1064 *rc
= gpg_error_from_errno(ENOMEM
);
1073 /* -I or -C. The filename is a key file. */
1074 file
= g_strdup(filename
);
1077 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1078 *rc
= gpg_error_from_errno(ENOMEM
);
1084 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1092 if (_getline(file
, &str
, rc
) == FALSE
) {
1093 log_write("%s: %s: %s", filename
, file
, pwmd_strerror(*rc
));
1103 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1104 const gchar
*keyfile
, guint64 iter
)
1113 struct client_crypto_s
*crypto
;
1115 if (stat(filename
, &st
) == -1) {
1116 log_write("%s: %s", filename
, strerror(errno
));
1120 crypto
= init_client_crypto();
1125 crypto
->key
= gcry_malloc(gcrykeysize
);
1126 memset(crypto
->key
, 0, gcrykeysize
);
1129 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1133 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
1136 if (iter
&& keyfile
) {
1137 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1142 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1143 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1146 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
1152 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1153 log_write("%s: %s", filename
, strerror(errno
));
1157 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1159 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1163 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
1167 log_write("%s: %s", filename
, strerror(errno
));
1172 xmlbuf
[st
.st_size
] = 0;
1175 * Make sure the document validates.
1177 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1178 log_write("xmlReadDoc() failed");
1184 xmlDocDumpMemory(doc
, &xml
, &len
);
1188 memset(crypto
->key
, '!', gcrykeysize
);
1190 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1193 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1197 crypto
->fh
->fh2
.iter
= iter
;
1198 rc
= export_common(outfile
, crypto
, xml
, len
);
1202 send_error(NULL
, rc
);
1206 cleanup_crypto(&crypto
);
1210 cleanup_crypto(&crypto
);
1214 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1219 MUTEX_LOCK(&rcfile_mutex
);
1221 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1222 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1225 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1226 g_clear_error(&grc
);
1230 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1231 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1234 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1235 g_clear_error(&grc
);
1240 MUTEX_UNLOCK(&rcfile_mutex
);
1244 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1249 MUTEX_LOCK(&rcfile_mutex
);
1251 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1252 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
1255 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1256 g_clear_error(&grc
);
1260 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1261 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1264 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1265 g_clear_error(&grc
);
1270 MUTEX_UNLOCK(&rcfile_mutex
);
1274 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1276 gboolean val
= FALSE
;
1279 MUTEX_LOCK(&rcfile_mutex
);
1281 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1282 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1285 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1286 g_clear_error(&grc
);
1290 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1291 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1294 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1295 g_clear_error(&grc
);
1300 MUTEX_UNLOCK(&rcfile_mutex
);
1304 static gboolean
parse_rcfile_keys()
1311 groups
= g_key_file_get_groups(keyfileh
, &n
);
1313 for (p
= groups
; *p
; p
++) {
1316 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1317 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1321 log_write("%s: key: %s", rcfile
, rc
->message
);
1327 do_cache_push(*p
, str
);
1333 log_write("%s: key: %s", rcfile
, rc
->message
);
1339 str
= parse_rcfile_keyfile(*p
, FALSE
, &ret
);
1344 do_cache_push(*p
, str
);
1352 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1356 const gchar
*p
= filename
;
1357 struct client_crypto_s
*crypto
;
1366 if (valid_filename(p
) == FALSE
) {
1367 log_write(N_("%s: Invalid characters in filename"), p
);
1371 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1373 if (access(p
, R_OK
|W_OK
) != 0) {
1374 log_write("%s: %s", p
, strerror(errno
));
1378 crypto
= init_client_crypto();
1383 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1386 log_write("%s: %s", p
, pwmd_strerror(rc
));
1387 cleanup_crypto(&crypto
);
1391 crypto
->key
= gcry_malloc(gcrykeysize
);
1394 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1395 cleanup_crypto(&crypto
);
1399 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1401 if (crypto
->fh
->fh2
.iter
<= 0) {
1402 memset(crypto
->key
, '!', gcrykeysize
);
1407 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1410 send_error(NULL
, rc
);
1411 cleanup_crypto(&crypto
);
1415 gcry_free(crypto
->fh
->doc
);
1416 crypto
->fh
->doc
= NULL
;
1419 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
,
1420 strlen(password
) ? strlen(password
) : 1);
1423 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, NULL
, NULL
);
1426 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1427 cleanup_crypto(&crypto
);
1431 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1432 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1433 cleanup_crypto(&crypto
);
1437 timeout
= get_key_file_integer(p
, "cache_timeout");
1438 cache_set_timeout(md5file
, timeout
);
1439 log_write(N_("File '%s' now cached"), filename
);
1440 cleanup_crypto(&crypto
);
1444 static void init_new_connection(gint fd
, gchar
*addr
)
1447 pthread_attr_t attr
;
1448 struct client_thread_s
*new;
1451 new = g_malloc0(sizeof(struct client_thread_s
));
1454 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1459 MUTEX_LOCK(&cn_mutex
);
1467 pthread_attr_init(&attr
);
1468 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1469 n
= pthread_create(&tid
, &attr
, client_thread
, new);
1470 pthread_attr_destroy(&attr
);
1475 log_write("pthread_create(): %s", strerror(n
));
1476 MUTEX_UNLOCK(&cn_mutex
);
1481 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1482 MUTEX_UNLOCK(&cn_mutex
);
1485 log_write(N_("new connection: fd=%i, addr=%s"), fd
, addr
);
1487 log_write(N_("new connection: fd=%i"), fd
);
1491 /* From Beej's Guide to Network Programming. It's a good tutorial. */
1492 static void *get_in_addr(struct sockaddr
*sa
)
1494 if (sa
->sa_family
== AF_INET
)
1495 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
1497 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
1500 static void *tcp_accept_thread(void *arg
)
1502 gint sockfd
= (gint
)arg
;
1505 struct sockaddr_storage raddr
;
1506 socklen_t slen
= sizeof(raddr
);
1511 fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1512 pthread_testcancel();
1515 if (errno
!= EAGAIN
) {
1516 if (!quit
) // probably EBADF
1517 log_write("accept(): %s", strerror(errno
));
1529 gchar s
[INET6_ADDRSTRLEN
];
1531 inet_ntop(raddr
.ss_family
, get_in_addr((struct sockaddr
*)&raddr
),
1533 init_new_connection(fd
, s
);
1536 t
= get_key_file_string("global", "tcp_wait");
1537 n
= strtol(t
, NULL
, 10);
1546 /* Just in case accept() failed for some reason other than EBADF */
1548 pthread_exit(PTHREAD_CANCELED
);
1553 static void *accept_thread(void *arg
)
1555 gint sockfd
= (gint
)arg
;
1558 socklen_t slen
= sizeof(struct sockaddr_un
);
1559 struct sockaddr_un raddr
;
1562 fd
= accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1563 pthread_testcancel();
1566 if (errno
!= EAGAIN
) {
1567 if (!quit
) // probably EBADF
1568 log_write("accept(): %s", strerror(errno
));
1577 init_new_connection(fd
, NULL
);
1580 /* Just in case accept() failed for some reason other than EBADF */
1582 pthread_exit(PTHREAD_CANCELED
);
1586 static void *adjust_cache_timer_thread(void *arg
)
1590 pthread_testcancel();
1592 cache_adjust_timer();
1599 static void *keepalive_thread(void *arg
)
1602 gint to
= (gint
)arg
;
1604 pthread_cond_t cond
;
1606 pthread_cond_init(&cond
, NULL
);
1607 pthread_mutex_init(&m
, NULL
);
1608 pthread_mutex_lock(&m
);
1611 pthread_cleanup_push(pthread_mutex_destroy
, &m
);
1612 pthread_cleanup_push(pthread_cond_destroy
, &m
);
1613 pthread_cleanup_push(pthread_mutex_unlock
, &m
);
1614 clock_gettime(CLOCK_REALTIME
, &ts
);
1616 pthread_cond_timedwait(&cond
, &m
, &ts
);
1617 pthread_testcancel();
1618 send_status_all(STATUS_KEEPALIVE
);
1619 pthread_cleanup_pop(0);
1620 pthread_cleanup_pop(0);
1621 pthread_cleanup_pop(0);
1627 static void startStopKeepAlive(gboolean term
)
1629 pthread_attr_t attr
;
1630 gint n
= get_key_file_integer("global", "keepalive");
1633 pthread_cancel(keepalive_tid
);
1641 pthread_attr_init(&attr
);
1642 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1643 pthread_create(&keepalive_tid
, &attr
, keepalive_thread
, (void *)n
);
1644 pthread_attr_destroy(&attr
);
1648 static void server_loop(gint sockfd
, gchar
**socketpath
)
1650 pthread_t accept_tid
;
1654 pthread_attr_t attr
;
1655 pthread_t cache_timeout_tid
;
1656 pthread_mutexattr_t mattr
;
1658 sigemptyset(&sigset
);
1661 sigaddset(&sigset
, SIGTERM
);
1662 sigaddset(&sigset
, SIGINT
);
1664 /* Clears the file cache. */
1665 sigaddset(&sigset
, SIGUSR1
);
1667 /* Configuration file reloading. */
1668 sigaddset(&sigset
, SIGHUP
);
1670 /* Caught in client_thread(). Sends a cache status message. */
1671 sigaddset(&sigset
, SIGUSR2
);
1673 /* Ignored everywhere. When a client disconnects abnormally this signal
1674 * gets raised. It isn't needed though because client_thread() will check
1675 * for rcs even after the client disconnects. */
1676 signal(SIGPIPE
, SIG_IGN
);
1677 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1679 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
1680 pthread_mutexattr_init(&mattr
);
1681 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
1682 pthread_mutex_init(&cn_mutex
, &mattr
);
1683 pthread_mutexattr_destroy(&mattr
);
1684 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1685 pthread_attr_init(&attr
);
1686 pthread_create(&accept_tid
, &attr
, accept_thread
, (void *)sockfd
);
1687 startStopKeepAlive(FALSE
);
1688 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
1689 pthread_create(&cache_timeout_tid
, &attr
, adjust_cache_timer_thread
, NULL
);
1690 pthread_attr_destroy(&attr
);
1695 sigwait(&sigset
, &sig
);
1696 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1698 /* Caught a signal. */
1705 cache_clear(NULL
, 2);
1713 log_write(N_("clearing file cache"));
1714 cache_clear(NULL
, 2);
1724 * We're out of the main server loop. This happens when a signal was sent
1725 * to terminate the daemon. We'll wait for all clients to disconnect
1726 * before exiting and ignore any following signals.
1728 shutdown(sockfd
, SHUT_RDWR
);
1730 pthread_cancel(accept_tid
);
1731 pthread_join(accept_tid
, NULL
);
1735 unlink(*socketpath
);
1736 g_free(*socketpath
);
1738 MUTEX_LOCK(&cn_mutex
);
1739 n
= g_slist_length(cn_thread_list
);
1740 MUTEX_UNLOCK(&cn_mutex
);
1743 log_write(N_("waiting for all clients to disconnect"));
1746 if (n
!= n_clients
) {
1747 log_write(N_("%i clients remain"), n
);
1752 MUTEX_LOCK(&cn_mutex
);
1753 n
= g_slist_length(cn_thread_list
);
1754 MUTEX_UNLOCK(&cn_mutex
);
1757 startStopKeepAlive(TRUE
);
1758 pthread_cancel(cache_timeout_tid
);
1763 * Called from pinentry_fork() in the child process.
1765 void free_client_list()
1767 gint i
, t
= g_slist_length(cn_thread_list
);
1769 for (i
= 0; i
< t
; i
++) {
1770 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1772 free_client(cn
->cl
);
1778 struct client_crypto_s
*init_client_crypto()
1780 struct client_crypto_s
*new = g_malloc0(sizeof(struct client_crypto_s
));
1784 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1788 rc
= gcry_cipher_open(&new->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
1791 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
1799 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1800 const gchar
*outfile
)
1805 struct client_crypto_s
*crypto
= init_client_crypto();
1808 return GPG_ERR_ENOMEM
;
1810 crypto
->key
= gcry_malloc(gcrykeysize
);
1813 cleanup_crypto(&crypto
);
1814 return GPG_ERR_ENOMEM
;
1817 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
1819 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
1824 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
1826 /* The header in version 1 had a bug where the iterations were off-by-one.
1827 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
1830 if (crypto
->fh
->fh1
.iter
>= 0) {
1832 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1837 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1838 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1841 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
1849 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, &crypto
->fh
->doc
,
1855 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
1858 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1862 crypto
->fh
->v1
= FALSE
;
1863 iter
= crypto
->fh
->fh1
.iter
;
1864 memset(&crypto
->fh
->fh2
, 0, sizeof(crypto
->fh
->fh2
));
1865 /* Keep the iterations and key from the original file. */
1866 crypto
->fh
->fh2
.iter
= iter
+1; // Bugfix for v1 data files.
1867 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
1871 send_error(NULL
, rc
);
1873 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
1874 cleanup_crypto(&crypto
);
1879 static gboolean
startStopTcp(gboolean term
)
1881 struct addrinfo hints
, *servinfo
, *p
;
1882 gint port
= get_key_file_integer("global", "tcp_port");
1885 pthread_attr_t attr
;
1887 if (term
|| get_key_file_boolean("global", "enable_tcp") == FALSE
) {
1888 if (tcpSockFd
!= -1) {
1889 pthread_cancel(tcpAcceptTid
);
1890 pthread_join(tcpAcceptTid
, NULL
);
1891 shutdown(tcpSockFd
, SHUT_RDWR
);
1895 /* A client may still be connected. */
1903 if (tcpSockFd
!= -1)
1906 memset(&hints
, 0, sizeof(hints
));
1907 hints
.ai_family
= AF_UNSPEC
;
1908 hints
.ai_socktype
= SOCK_STREAM
;
1909 hints
.ai_flags
= AI_PASSIVE
;
1911 if ((n
= getaddrinfo(NULL
, print_fmt(buf
, sizeof(buf
), "%i", port
),
1912 &hints
, &servinfo
)) == -1) {
1913 log_write("getaddrinfo(): %s", gai_strerror(n
));
1917 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
1918 if ((tcpSockFd
= socket(p
->ai_family
, p
->ai_socktype
,
1919 p
->ai_protocol
)) == -1) {
1920 log_write("socket(): %s", strerror(errno
));
1926 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_REUSEADDR
, &n
,
1927 sizeof(int)) == -1) {
1928 log_write("setsockopt(): %s", strerror(errno
));
1929 freeaddrinfo(servinfo
);
1933 if (bind(tcpSockFd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
1935 log_write("bind(): %s", strerror(errno
));
1943 freeaddrinfo(servinfo
);
1946 log_write("%s", N_("could not bind"));
1950 if (g_key_file_has_key(keyfileh
, "global", "tcp_interface", NULL
)) {
1951 gchar
*tmp
= get_key_file_string("global", "tcp_interface");
1953 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
, 1)
1955 log_write("setsockopt(): %s", strerror(errno
));
1963 if (!initTlsParams())
1966 if (listen(tcpSockFd
, 0) == -1) {
1967 log_write("listen(): %s", strerror(errno
));
1971 pthread_attr_init(&attr
);
1972 pthread_create(&tcpAcceptTid
, &attr
, tcp_accept_thread
, (void *)tcpSockFd
);
1973 pthread_attr_destroy(&attr
);
1979 if (tcpSockFd
!= -1)
1987 int main(int argc
, char *argv
[])
1990 struct sockaddr_un addr
;
1991 struct passwd
*pw
= getpwuid(getuid());
1992 gchar buf
[PATH_MAX
];
1993 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1994 gchar
*socketarg
= NULL
;
1995 gchar
*datadir
= NULL
;
1998 gchar
**cache_push
= NULL
;
2000 gchar
*import
= NULL
, *keyfile
= NULL
;
2001 guint64 cmd_iterations
= -1;
2002 gint default_timeout
;
2003 gboolean rcfile_spec
= FALSE
;
2004 gint estatus
= EXIT_FAILURE
;
2006 gchar
*outfile
= NULL
;
2007 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
2009 gboolean secure
= FALSE
;
2011 gint background
= 1;
2012 gchar
*convert
= NULL
;
2013 pthread_mutexattr_t mattr
;
2014 #ifdef WITH_PINENTRY
2015 gboolean disable_pinentry
= FALSE
;
2018 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
2022 #ifdef HAVE_SETRLIMIT
2025 rl
.rlim_cur
= rl
.rlim_max
= 0;
2027 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
2028 err(EXIT_FAILURE
, "setrlimit()");
2034 setlocale(LC_ALL
, "");
2035 bindtextdomain("pwmd", LOCALEDIR
);
2043 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
2044 g_mem_set_vtable(&mtable
);
2045 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
2046 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
2048 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pthread
);
2049 gcry_check_version(GCRYPT_VERSION
);
2051 gnutls_global_set_mem_functions(xmalloc
, xmalloc
, gcry_SecureCheck
,
2053 gnutls_global_init();
2054 gnutls_global_set_log_function(tls_log
);
2055 gnutls_global_set_log_level(1);
2056 assuan_set_io_hooks(&io_hooks
);
2061 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
2063 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2064 err(EXIT_FAILURE
, "%s", buf
);
2066 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
2068 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2069 err(EXIT_FAILURE
, "%s", buf
);
2071 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
2077 while ((opt
= getopt(argc
, argv
, "Po:C:nI:i:k:hvf:D")) != EOF
) {
2079 #ifdef WITH_PINENTRY
2081 disable_pinentry
= TRUE
;
2100 cmd_iterations
= strtol(optarg
, NULL
, 10);
2107 rcfile
= g_strdup(optarg
);
2111 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
,
2113 #ifdef WITH_PINENTRY
2146 pthread_mutexattr_init(&mattr
);
2147 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
2148 pthread_mutex_init(&rcfile_mutex
, &mattr
);
2149 pthread_mutexattr_destroy(&mattr
);
2151 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2154 #ifdef WITH_PINENTRY
2155 if (disable_pinentry
== TRUE
)
2156 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2159 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2160 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2162 if (log_syslog
== TRUE
)
2163 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2165 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2166 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2169 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
2170 log_write("setpriority(): %s", strerror(errno
));
2175 #ifdef HAVE_MLOCKALL
2176 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2177 log_write("mlockall(): %s", strerror(errno
));
2188 opt
= convert_file(convert
, keyfile
, outfile
);
2189 g_key_file_free(keyfileh
);
2191 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2198 if (cmd_iterations
== -1)
2199 cmd_iterations
= (guint64
)get_key_file_integer("global", "iterations");
2201 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2202 g_key_file_free(keyfileh
);
2204 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2207 g_key_file_set_list_separator(keyfileh
, ',');
2209 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2210 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2214 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
2216 socketarg
= g_strdup(buf
);
2221 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2222 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2224 datadir
= expand_homedir(p
);
2227 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2228 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2229 disable_list_and_dump
= n
;
2232 disable_list_and_dump
= secure
;
2234 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2235 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2237 default_timeout
= -1;
2239 setup_logging(keyfileh
);
2241 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2242 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2244 if (argc
!= optind
) {
2246 ptotal
= g_strv_length(cache_push
);
2248 for (; optind
< argc
; optind
++) {
2249 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2250 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2254 if (strchr(socketarg
, '/') == NULL
) {
2255 socketdir
= g_get_current_dir();
2256 socketname
= g_strdup(socketarg
);
2257 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2260 socketname
= g_strdup(strrchr(socketarg
, '/'));
2262 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2263 socketdir
= g_strdup(socketarg
);
2264 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2267 if (chdir(datadir
)) {
2268 log_write("%s: %s", datadir
, strerror(errno
));
2273 if (parse_rcfile_keys() == FALSE
)
2276 clear_rcfile_keys();
2279 * Set the cache entry for a file. Prompts for the password.
2282 for (opt
= 0; cache_push
[opt
]; opt
++)
2283 do_cache_push(cache_push
[opt
], NULL
);
2285 g_strfreev(cache_push
);
2286 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2290 * bind() doesn't like the full pathname of the socket or any non alphanum
2291 * characters so change to the directory where the socket is wanted then
2292 * create it then change to datadir.
2294 if (chdir(socketdir
)) {
2295 log_write("%s: %s", socketdir
, strerror(errno
));
2301 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2302 log_write("socket(): %s", strerror(errno
));
2306 addr
.sun_family
= AF_UNIX
;
2307 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2309 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2310 log_write("bind(): %s", strerror(errno
));
2312 if (errno
== EADDRINUSE
)
2313 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2314 "stale socket. Please remove it manually."), socketpath
);
2320 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2321 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2322 mode_t mode
= strtol(t
, NULL
, 8);
2323 mode_t mask
= umask(0);
2327 if (chmod(socketname
, mode
) == -1) {
2328 log_write("%s: %s", socketname
, strerror(errno
));
2338 g_free(--socketname
);
2340 if (chdir(datadir
)) {
2341 log_write("%s: %s", datadir
, strerror(errno
));
2348 pthread_mutexattr_init(&mattr
);
2349 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
2350 pthread_mutex_init(&cache_mutex
, &mattr
);
2351 pthread_mutexattr_destroy(&mattr
);
2352 #ifdef WITH_PINENTRY
2353 pthread_mutex_init(&pin_mutex
, NULL
);
2356 if (listen(sockfd
, 0) == -1) {
2357 log_write("listen(): %s", strerror(errno
));
2364 if (startStopTcp(FALSE
) == FALSE
)
2371 log_write("fork(): %s", strerror(errno
));
2384 server_loop(sockfd
, &socketpath
);
2385 estatus
= EXIT_SUCCESS
;
2388 if (socketpath
&& do_unlink
) {
2395 gnutls_global_deinit();
2398 g_key_file_free(keyfileh
);
2401 xmlCleanupGlobals();
2403 if (estatus
== EXIT_SUCCESS
)
2404 log_write(N_("pwmd exiting normally"));
2406 #if defined(DEBUG) && !defined(MEM_DEBUG)