1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2008 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>
51 #include <sys/resource.h>
75 #include "pwmd_error.h"
80 GCRY_THREAD_OPTION_PTH_IMPL
;
82 static void clear_rcfile_keys()
88 groups
= g_key_file_get_groups(keyfileh
, &n
);
90 for (p
= groups
; *p
; p
++) {
93 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
)
94 g_key_file_set_string(keyfileh
, *p
, "key", "");
100 static void reload_rcfile()
102 gboolean b
= disable_list_and_dump
;
105 log_write(N_("reloading configuration file '%s'"), rcfile
);
111 g_key_file_free(keyfileh
);
113 parse_rcfile_keys(FALSE
);
115 disable_list_and_dump
= b
;
116 send_status_all(STATUS_CONFIG
);
119 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
121 gpg_error_t n
= gpg_error_from_errno(e
);
123 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, gpg_strerror(n
)));
126 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
128 gpg_err_code_t n
= gpg_err_code(e
);
129 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
130 struct client_s
*client
= assuan_get_pointer(ctx
);
133 return assuan_process_done(ctx
, 0);
136 log_write("%s", pwmd_strerror(e
));
140 if (n
== EPWMD_LIBXML_ERROR
) {
141 xmlErrorPtr xe
= client
->xml_error
;
144 xe
= xmlGetLastError();
146 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
147 log_write("%s", xe
->message
);
149 if (xe
== client
->xml_error
)
154 client
->xml_error
= NULL
;
158 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
161 void log_write(const gchar
*fmt
, ...)
171 pth_t tid
= pth_self();
172 gchar tid_str
[12], *p
;
174 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
177 if (!cmdline
&& logfile
) {
178 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
186 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
197 fprintf(stderr
, "%s\n", args
);
203 attr
= pth_attr_of(tid
);
205 if (pth_attr_get(attr
, PTH_ATTR_NAME
, &name
) == FALSE
)
208 pth_attr_destroy(attr
);
209 g_snprintf(tid_str
, sizeof(tid_str
), "(%p)", tid
);
212 if (strncmp(p
+1, name
, 9) == 0)
215 if (!cmdline
&& log_syslog
== TRUE
)
216 syslog(LOG_INFO
, "%s%s: %s", name
, p
? p
: "", args
);
219 tm
= localtime(&now
);
220 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
221 tbuf
[sizeof(tbuf
) - 1] = 0;
223 if (args
[strlen(args
)-1] == '\n')
224 args
[strlen(args
)-1] = 0;
226 line
= g_strdup_printf("%s %i %s%s: %s\n", tbuf
, getpid(), name
,
238 write(fd
, line
, strlen(line
));
243 if (isatty(STDERR_FILENO
)) {
244 fprintf(stderr
, "%s", line
);
251 static void usage(gchar
*pn
)
253 g_fprintf(stderr
, N_(
254 "Usage: %s [-hvDnP] [-f <rcfile>] [-C <filename>] "
255 "[-I <filename> [-i <iter>]]\n "
256 "[-k <keyfile>] [-o <outfile>] [file1] [...]\n"
257 " -n run as a foreground process\n"
258 " -f load the specified rcfile (~/.pwmd/config)\n"
259 " -C convert a version 1 data file to version 2\n"
260 " -I import an XML file\n"
261 " -i encrypt with the specified number of iterations when importing\n"
262 " (config default in the \"global\" section)\n"
263 " -k obtain the key from the specified file\n"
264 " -o output file for use with the -C and -I options\n"
265 " -D disable use of the LIST and DUMP commands\n"
266 " -P disable pinentry\n"
268 " -h this help text\n"
274 static int gcry_SecureCheck(const void *ptr
)
280 static void setup_gcrypt()
282 gcry_check_version(NULL
);
285 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
289 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
291 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
293 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
294 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
297 static gint
new_connection(struct client_s
*cl
)
300 gchar ver
[ASSUAN_LINELENGTH
];
302 rc
= assuan_init_socket_server_ext(&cl
->ctx
, cl
->thd
->fd
, 2);
307 assuan_set_pointer(cl
->ctx
, cl
);
308 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
309 assuan_set_hello_line(cl
->ctx
, ver
);
310 rc
= register_commands(cl
->ctx
);
315 rc
= assuan_accept(cl
->ctx
);
323 log_write("%s", gpg_strerror(rc
));
327 gpg_error_t
send_status(assuan_context_t ctx
, status_msg_t which
,
328 const gchar
*fmt
, ...)
331 struct client_s
*client
= assuan_get_pointer(ctx
);
332 gchar buf
[ASSUAN_LINELENGTH
];
333 gchar
*status
= NULL
;
338 g_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
345 CACHE_LOCK(client
->ctx
);
346 line
= print_fmt(buf
, sizeof(buf
), "%i", cache_file_count());
351 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
352 line
= print_fmt(buf
, sizeof(buf
), "%i", g_slist_length(cn_thread_list
));
353 pth_mutex_release(&cn_mutex
);
359 case STATUS_KEEPALIVE
:
360 status
= "KEEPALIVE";
364 line
= N_("Waiting for lock");
372 case STATUS_DECOMPRESS
:
373 status
= "DECOMPRESS";
375 case STATUS_COMPRESS
:
381 log_write("%s %s", status
, line
);
385 return assuan_write_status(ctx
, status
, line
);
388 void send_status_all(status_msg_t which
)
392 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
394 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
395 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
396 pth_msgport_t m
= pth_msgport_find(cn
->msg_name
);
399 pth_message_t
*msg
= g_malloc0(sizeof(pth_message_t
));
401 msg
->m_data
= (status_msg_t
*)which
;
402 pth_msgport_put(m
, msg
);
406 pth_mutex_release(&cn_mutex
);
409 static void xml_error_cb(void *data
, xmlErrorPtr e
)
411 struct client_s
*client
= data
;
414 * Keep the first reported error as the one to show in the error
415 * description. Reset in send_error().
417 if (client
->xml_error
)
420 xmlCopyError(e
, client
->xml_error
);
424 * This is called after a child_thread terminates. Set with
425 * pth_cleanup_push().
427 static void cleanup_cb(void *arg
)
429 struct client_thread_s
*cn
= arg
;
431 struct client_s
*cl
= cn
->cl
;
433 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
434 log_write(N_("exiting, fd=%i"), cn
->fd
);
437 pth_cancel(cn
->msg_tid
);
438 pth_join(cn
->msg_tid
, &value
);
439 pth_event_free(cn
->msg_ev
, PTH_FREE_THIS
);
443 g_free(cn
->msg_name
);
446 while (pth_msgport_pending(cn
->msg
) > 0) {
447 pth_message_t
*m
= pth_msgport_get(cn
->msg
);
452 pth_msgport_destroy(cn
->msg
);
455 pth_join(cn
->tid
, &value
);
457 if (cl
&& cl
->freed
== FALSE
)
462 gnutls_deinit(cn
->tls
->ses
);
472 assuan_deinit_server(cl
->ctx
);
475 if (cl
&& cl
->pinentry
)
476 cleanup_pinentry(cl
->pinentry
);
480 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
482 pth_mutex_release(&cn_mutex
);
483 send_status_all(STATUS_CLIENTS
);
486 static void *client_msg_thread(void *data
)
488 struct client_s
*cl
= data
;
489 pth_status_t ev_status
;
492 pth_wait(cl
->thd
->msg_ev
);
493 ev_status
= pth_event_status(cl
->thd
->msg_ev
);
495 if (ev_status
== PTH_STATUS_FAILED
) {
496 log_write("%s(%i): %s: PTH_STATUS_FAILED", __FILE__
, __LINE__
,
501 if (ev_status
== PTH_STATUS_OCCURRED
) {
502 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
504 while (pth_msgport_pending(cl
->thd
->msg
) > 0) {
505 pth_message_t
*m
= pth_msgport_get(cl
->thd
->msg
);
506 status_msg_t n
= (status_msg_t
)m
->m_data
;
507 gpg_error_t rc
= send_status(cl
->ctx
, n
, NULL
);
512 pth_mutex_release(&cn_mutex
);
513 log_write("%s", gpg_strerror(rc
));
518 pth_mutex_release(&cn_mutex
);
524 * Called every time a connection is made via pth_spawn(). This is the thread
527 static void *client_thread(void *data
)
529 struct client_thread_s
*thd
= data
;
531 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
537 * Prevent a race condition with init_new_connection() if this thread
538 * fails (returns) for some reason before init_new_connection() releases
541 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
542 pth_mutex_release(&cn_mutex
);
545 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
551 pth_cleanup_push(cleanup_cb
, thd
);
555 * Do the TLS handshake before anything else.
558 gchar
*prio
= get_key_file_string("global", "cipher_suite");
560 thd
->tls
= tls_init(thd
->fd
, prio
);
571 * This is a "child" thread. Don't catch any signals. Let the master
572 * thread take care of signals in server_loop().
574 if (new_connection(cl
))
578 cl
->pinentry
= pinentry_init();
582 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
587 thd
->msg_name
= g_strdup_printf("%p", thd
->tid
);
589 if (!thd
->msg_name
) {
590 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
594 thd
->msg
= pth_msgport_create(thd
->msg_name
);
597 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
601 thd
->msg_ev
= pth_event(PTH_EVENT_MSG
, thd
->msg
);
602 attr
= pth_attr_new();
603 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
604 pth_attr_set(attr
, PTH_ATTR_NAME
, "client_msg");
605 thd
->msg_tid
= pth_spawn(attr
, client_msg_thread
, cl
);
607 pth_attr_destroy(attr
);
610 log_write("pth_spawn(): %s", strerror(n
));
615 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
616 log_write("mlockall(): %s", strerror(errno
));
621 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
624 log_write("%s", gpg_strerror(rc
));
628 send_status_all(STATUS_CLIENTS
);
629 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->thd
->fd
);
632 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
635 pth_status_t ev_status
;
638 ev_status
= pth_event_status(ev
);
640 if (ev_status
== PTH_STATUS_FAILED
) {
641 log_write("%s(%i): %s: PTH_STATUS_FAILED", __FILE__
, __LINE__
,
645 else if (ev_status
== PTH_STATUS_OCCURRED
) {
646 rc
= assuan_process_next(cl
->ctx
);
649 cl
->inquire_status
= INQUIRE_INIT
;
651 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
654 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
655 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
658 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
664 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
) {
665 cl
->pinentry
->ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->pinentry
->fd
);
666 pth_event_concat(ev
, cl
->pinentry
->ev
, NULL
);
667 cl
->pinentry
->status
= PINENTRY_RUNNING
;
671 switch (cl
->inquire_status
) {
676 cl
->inquire_status
= INQUIRE_INIT
;
677 rc
= assuan_process_done(cl
->ctx
, 0);
684 ev
= pinentry_iterate(cl
, ev
);
689 * Client cleanup (including XML data) is done in cleanup_cb() from
690 * the cleanup thread.
693 pth_event_free(ev
, PTH_FREE_ALL
);
700 static void setup_logging(GKeyFile
*kf
)
702 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
705 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
711 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
717 logfile
= g_strdup(buf
);
727 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
731 * Make sure all settings are set to either the specified setting or a
734 static void set_rcfile_defaults(GKeyFile
*kf
)
738 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
739 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
740 g_key_file_set_string(kf
, "global", "socket_path", buf
);
743 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
744 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
745 g_key_file_set_string(kf
, "global", "data_directory", buf
);
748 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
749 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
751 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
752 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
753 g_key_file_set_string(kf
, "global", "log_path", buf
);
756 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
757 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
760 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
761 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
764 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
765 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
767 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
768 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0)
769 g_key_file_set_integer(kf
, "global", "iterations", 1);
771 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
772 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
774 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
775 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
777 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
778 g_key_file_set_integer(kf
, "global", "compression_level", 6);
780 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
781 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
783 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
784 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
786 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
788 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
789 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
792 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
795 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
796 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
798 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
799 g_key_file_set_integer(kf
, "global", "keepalive", 30);
802 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
803 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
805 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
806 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
810 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
811 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
813 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
814 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
816 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
817 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
819 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
820 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
822 if (g_key_file_has_key(kf
, "global", "cipher_suite", NULL
) == FALSE
)
823 g_key_file_set_string(kf
, "global", "cipher_suite", "SECURE256");
829 static GKeyFile
*parse_rcfile()
831 GKeyFile
*kf
= g_key_file_new();
834 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
835 log_write("%s: %s", rcfile
, rc
->message
);
841 if (rc
->code
== G_FILE_ERROR_NOENT
) {
842 set_rcfile_defaults(kf
);
849 set_rcfile_defaults(kf
);
853 static gchar
*do_get_password(const gchar
*prompt
)
855 gchar buf
[LINE_MAX
] = {0}, *p
;
856 struct termios told
, tnew
;
859 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
860 log_write("tcgetattr(): %s", strerror(errno
));
864 memcpy(&tnew
, &told
, sizeof(struct termios
));
865 tnew
.c_lflag
&= ~(ECHO
);
866 tnew
.c_lflag
|= ICANON
|ECHONL
;
868 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
869 log_write("tcsetattr(): %s", strerror(errno
));
870 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
874 fprintf(stderr
, "%s", prompt
);
876 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
877 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
881 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
882 p
[strlen(p
) - 1] = 0;
885 key
= gcry_malloc(1);
889 key
= gcry_malloc(strlen(p
) + 1);
890 sprintf(key
, "%s", p
);
893 memset(&buf
, 0, sizeof(buf
));
897 /* Only used when "enable_pinentry" is "false". */
898 static gboolean
get_input(const gchar
*filename
, file_header_internal_t
*fh
,
899 guchar
*key
, pinentry_cmd_t which
)
905 if (which
== PINENTRY_SAVE
) {
908 prompt
= g_strdup_printf(N_("New passphrase for %s: "), filename
);
909 key1
= do_get_password(prompt
);
913 log_write(N_("%s: Skipping file"), filename
);
917 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
918 key2
= do_get_password(prompt
);
923 log_write(N_("%s: Skipping file"), filename
);
927 if (strcmp(key1
, key2
)) {
930 log_write(N_("%s: Passphrase mismatch"), filename
);
934 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, key1
,
935 strlen(key1
) ? strlen(key1
) : 1);
941 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
946 if ((password
= do_get_password(prompt
)) == NULL
) {
947 log_write(N_("%s: Skipping file"), filename
);
952 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
) ? strlen(password
) : 1);
955 rc
= try_xml_decrypt(NULL
, key
, fh
, &fh
->doc
, &fh
->len
);
960 log_write(N_("%s: Invalid passphrase"), filename
);
972 * inbuf must have been allocated with gcry_malloc().
974 gpg_error_t
export_common(const gchar
*filename
, file_header_internal_t
*fh
,
975 guchar
*shakey
, gpointer inbuf
, gulong insize
)
983 rc
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
988 level
= get_key_file_integer(filename
, "compression_level");
993 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
995 gcry_cipher_close(gh
);
996 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
1004 rc
= do_xml_encrypt(NULL
, gh
, filename
, inbuf
, insize
, shakey
, fh
->fh2
.iter
);
1005 gcry_cipher_close(gh
);
1009 static gboolean
get_password(const gchar
*filename
, file_header_internal_t
*fh
,
1010 guchar
*md5file
, guchar
*key
, pinentry_cmd_t which
)
1012 #ifdef WITH_PINENTRY
1015 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
1018 return get_input(filename
, fh
, key
, which
);
1019 #ifdef WITH_PINENTRY
1022 gchar
*result
= NULL
;
1023 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
1026 set_pinentry_defaults(pin
);
1028 pin
->filename
= g_strdup(filename
);
1030 rc
= pinentry_getpin(pin
, &result
);
1033 log_write("%s", gpg_strerror(rc
));
1038 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
1041 if (which
== PINENTRY_SAVE
) {
1042 cleanup_pinentry(pin
);
1046 rc
= try_xml_decrypt(NULL
, key
, fh
, &fh
->doc
, &fh
->len
);
1048 if (rc
== EPWMD_BADKEY
) {
1050 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1053 pin
->title
= g_strdup(N_("Incorrect passphrase. Please try again."));
1058 log_write("%s", pwmd_strerror(rc
));
1060 cleanup_pinentry(pin
);
1064 return rc
? FALSE
: TRUE
;
1068 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
1071 gchar buf
[LINE_MAX
] = {0}, *p
;
1077 if ((fp
= fopen(file
, "r")) == NULL
) {
1078 *rc
= gpg_error_from_syserror();
1082 p
= fgets(buf
, sizeof(buf
), fp
);
1086 if (len
&& buf
[len
- 1] == '\n')
1089 str
= gcry_malloc(len
+ 1);
1092 *rc
= gpg_error_from_errno(ENOMEM
);
1096 memcpy(str
, buf
, len
? len
: 1);
1098 memset(&buf
, 0, sizeof(buf
));
1103 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
)
1106 gchar
*t
, *file
= NULL
, *str
;
1109 if (import
== FALSE
) {
1110 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1111 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1115 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1122 t
= expand_homedir(file
);
1125 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1134 /* -I or -C. The filename is a key file. */
1135 file
= g_strdup(filename
);
1138 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1144 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1152 if (_getline(file
, &str
, &rc
) == FALSE
) {
1153 log_write("%s: %s", file
, pwmd_strerror(rc
));
1162 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1163 const gchar
*keyfile
, gulong iter
)
1171 guchar shakey
[gcrykeysize
];
1173 file_header_internal_t fh
;
1175 if (stat(filename
, &st
) == -1) {
1176 log_write("%s: %s", filename
, strerror(errno
));
1180 log_write(N_("Importing '%s'. Output will be written to '%s' ..."),
1183 if (iter
&& keyfile
) {
1184 gchar
*tmp
= parse_rcfile_keyfile(keyfile
, TRUE
);
1189 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, tmp
,
1190 strlen(tmp
) ? strlen(tmp
) : 1);
1192 else if (iter
&& get_password(outfile
, NULL
, NULL
, shakey
, PINENTRY_SAVE
)
1196 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1197 memset(shakey
, 0, sizeof(shakey
));
1198 log_write("%s: %s", filename
, strerror(errno
));
1202 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1204 memset(shakey
, 0, sizeof(shakey
));
1205 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1209 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
1210 memset(shakey
, 0, sizeof(shakey
));
1214 err(EXIT_FAILURE
, "%s", filename
);
1218 xmlbuf
[st
.st_size
] = 0;
1221 * Make sure the document validates.
1223 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1224 log_write("xmlReadDoc() failed");
1227 memset(shakey
, 0, sizeof(shakey
));
1232 xmlDocDumpMemory(doc
, &xml
, &len
);
1236 memset(shakey
, '!', sizeof(shakey
));
1238 memset(&fh
, 0, sizeof(fh
));
1240 rc
= export_common(outfile
, &fh
, shakey
, xml
, len
);
1241 memset(shakey
, 0, sizeof(shakey
));
1244 send_error(NULL
, rc
);
1246 return rc
? FALSE
: TRUE
;
1249 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1254 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1255 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1258 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1259 g_clear_error(&grc
);
1263 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1264 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1267 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1268 g_clear_error(&grc
);
1276 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1281 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1282 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", 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_integer(keyfileh
, "global", what
, &grc
);
1294 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1295 g_clear_error(&grc
);
1303 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1305 gboolean val
= FALSE
;
1308 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1309 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1312 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1313 g_clear_error(&grc
);
1317 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1318 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1321 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1322 g_clear_error(&grc
);
1330 static gboolean
parse_rcfile_keys()
1337 groups
= g_key_file_get_groups(keyfileh
, &n
);
1339 for (p
= groups
; *p
; p
++) {
1342 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1343 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1347 log_write("%s: key: %s", rcfile
, rc
->message
);
1353 do_cache_push(*p
, str
);
1359 log_write("%s: key: %s", rcfile
, rc
->message
);
1364 str
= parse_rcfile_keyfile(*p
, FALSE
);
1369 do_cache_push(*p
, str
);
1377 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1382 const gchar
*p
= filename
;
1383 file_header_internal_t
*fh
;
1392 if (valid_filename(p
) == FALSE
) {
1393 log_write(N_("%s: Invalid characters in filename"), p
);
1397 md5file
= gcry_malloc(16);
1398 key
= gcry_malloc(gcrykeysize
);
1399 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1401 if (access(p
, R_OK
|W_OK
) != 0) {
1405 if (errno
!= ENOENT
) {
1406 log_write("%s: %s", p
, strerror(errno
));
1413 fh
= read_file_header(filename
, FALSE
, &rc
);
1418 log_write("%s: %s", p
, pwmd_strerror(rc
));
1422 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1424 if (fh
->fh2
.iter
<= 0) {
1425 memset(key
, '!', gcrykeysize
);
1430 if (!get_password(p
, fh
, md5file
, key
, PINENTRY_OPEN
)) {
1439 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
,
1440 strlen(password
) ? strlen(password
) : 1);
1443 rc
= try_xml_decrypt(NULL
, key
, fh
, NULL
, NULL
);
1446 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1454 if (cache_update_key(md5file
, key
) == FALSE
) {
1455 log_write("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1462 timeout
= get_key_file_integer(p
, "cache_timeout");
1463 cache_set_timeout(md5file
, timeout
);
1464 log_write(N_("File '%s' now cached"), filename
);
1471 static void init_new_connection(gint fd
, gchar
*addr
)
1475 struct client_thread_s
*new;
1479 new = g_malloc0(sizeof(struct client_thread_s
));
1482 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1487 * Thread priority is inherited from the calling thread. This
1488 * thread is PTH_PRIO_MAX. Keep the "child" threads at standard
1491 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1499 attr
= pth_attr_new();
1500 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
1501 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1502 pth_attr_set(attr
, PTH_ATTR_NAME
, "client");
1503 tid
= pth_spawn(attr
, client_thread
, new);
1505 pth_attr_destroy(attr
);
1509 log_write("pth_spawn(): %s", strerror(n
));
1510 pth_mutex_release(&cn_mutex
);
1514 g_snprintf(buf
, sizeof(buf
), "%p", tid
);
1517 log_write(N_("new tid=%s, fd=%i, addr=%s"), buf
, fd
, addr
);
1519 log_write(N_("new tid=%s, fd=%i"), buf
, fd
);
1521 attr
= pth_attr_of(tid
);
1522 pth_attr_set(attr
, PTH_ATTR_NAME
, buf
);
1523 pth_attr_destroy(attr
);
1525 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1526 pth_mutex_release(&cn_mutex
);
1530 /* From Beej's Guide to Network Programming. It's a good tutorial. */
1531 static void *get_in_addr(struct sockaddr
*sa
)
1533 if (sa
->sa_family
== AF_INET
)
1534 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
1536 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
1539 static void *tcp_accept_thread(void *arg
)
1541 gint sockfd
= (gint
)arg
;
1544 struct sockaddr_storage raddr
;
1545 socklen_t slen
= sizeof(raddr
);
1550 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1551 if (errno
!= EAGAIN
) {
1552 if (!quit
) // probably EBADF
1553 log_write("accept(): %s", strerror(errno
));
1563 gchar s
[INET6_ADDRSTRLEN
];
1565 inet_ntop(raddr
.ss_family
, get_in_addr((struct sockaddr
*)&raddr
),
1567 init_new_connection(fd
, s
);
1570 t
= get_key_file_string("global", "tcp_wait");
1571 n
= strtol(t
, NULL
, 10);
1577 pth_usleep(n
*100000);
1580 /* Just in case pth_accept() failed for some reason other than EBADF */
1582 pth_exit(PTH_CANCELED
);
1587 static void *accept_thread(void *arg
)
1589 gint sockfd
= (gint
)arg
;
1592 socklen_t slen
= sizeof(struct sockaddr_un
);
1593 struct sockaddr_un raddr
;
1596 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1597 if (errno
!= EAGAIN
) {
1598 if (!quit
) // probably EBADF
1599 log_write("accept(): %s", strerror(errno
));
1606 init_new_connection(fd
, NULL
);
1609 /* Just in case pth_accept() failed for some reason other than EBADF */
1611 pth_exit(PTH_CANCELED
);
1616 * This thread isn't joinable. For operations that block, these threads will
1619 static void *adjust_timer_thread(void *arg
)
1622 cache_adjust_timer();
1627 static pth_event_t
timeout_event_iterate(pth_event_t timeout_ev
,
1630 pth_status_t ev_status
;
1633 pth_event_isolate(timeout_ev
);
1634 ev_status
= pth_event_status(timeout_ev
);
1635 pth_event_free(timeout_ev
, PTH_FREE_THIS
);
1637 if (ev_status
== PTH_STATUS_OCCURRED
) {
1639 * The timer event has expired. Update the file cache. When the
1640 * cache mutex is locked and the timer expires again, the threads
1643 pth_t tid
= pth_spawn(attr
, adjust_timer_thread
, NULL
);
1646 log_write("pth_spawn(): %s", strerror(errno
));
1650 return pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1653 static pth_event_t
keepalive_event_iterate(pth_event_t keepalive_ev
,
1656 pth_event_t ev
= NULL
;
1657 pth_status_t ev_status
;
1660 pth_event_isolate(keepalive_ev
);
1661 ev_status
= pth_event_status(keepalive_ev
);
1663 if (ev_status
== PTH_STATUS_OCCURRED
|| ev_status
== PTH_STATUS_FAILED
) {
1664 if (ev_status
== PTH_STATUS_OCCURRED
)
1665 send_status_all(STATUS_KEEPALIVE
);
1667 pth_event_free(keepalive_ev
, PTH_FREE_THIS
);
1673 if (keepalive
> 0 && !ev
)
1674 ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(keepalive
, 0));
1679 static void server_loop(gint sockfd
, gint sockfd_r
, gchar
**socketpath
)
1683 pth_t tcp_accept_tid
;
1689 pth_event_t timeout_ev
, keepalive_ev
= NULL
, ev_quit
;
1690 gint keepalive
= get_key_file_integer("global", "keepalive");
1693 pth_mutex_init(&cn_mutex
);
1694 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1696 sigaddset(&set
, SIGTERM
);
1697 sigaddset(&set
, SIGINT
);
1698 sigaddset(&set
, SIGUSR1
);
1699 sigaddset(&set
, SIGHUP
);
1700 sigaddset(&set
, SIGABRT
);
1702 attr
= pth_attr_new();
1703 pth_attr_init(attr
);
1704 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MIN
);
1705 pth_attr_set(attr
, PTH_ATTR_NAME
, "accept");
1706 accept_tid
= pth_spawn(attr
, accept_thread
, (void *)sockfd
);
1709 log_write("pth_spawn(): %s", strerror(errno
));
1714 if (sockfd_r
!= -1) {
1715 pth_attr_set(attr
, PTH_ATTR_NAME
, "tcp_accept");
1716 tcp_accept_tid
= pth_spawn(attr
, tcp_accept_thread
, (void *)sockfd_r
);
1718 if (!tcp_accept_tid
) {
1719 log_write("pth_spawn(): %s", strerror(errno
));
1725 pth_attr_init(attr
);
1726 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1727 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1730 * For the cache_timeout configuration parameter.
1732 timeout_ev
= timeout_event_iterate(NULL
, attr
);
1733 keepalive_ev
= keepalive_event_iterate(NULL
, keepalive
);
1734 timeout_ev
= pth_event_concat(timeout_ev
, keepalive_ev
, NULL
);
1739 pth_sigwait_ev(&set
, &sig
, timeout_ev
);
1740 timeout_ev
= timeout_event_iterate(timeout_ev
, attr
);
1741 keepalive_ev
= keepalive_event_iterate(keepalive_ev
, keepalive
);
1742 timeout_ev
= pth_event_concat(timeout_ev
, keepalive_ev
, NULL
);
1745 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1747 /* Caught a signal. */
1751 keepalive
= get_key_file_integer("global", "keepalive");
1755 cache_clear(NULL
, 2);
1763 log_write(N_("clearing file cache"));
1764 cache_clear(NULL
, 2);
1769 shutdown(sockfd
, SHUT_RDWR
);
1772 if (sockfd_r
!= -1) {
1773 shutdown(sockfd_r
, SHUT_RDWR
);
1782 * We're out of the main server loop. This happens when a signal was sent
1783 * to terminate the daemon. We'll wait for all clients to disconnect
1784 * before exiting and ignore any following signals.
1786 pth_join(accept_tid
, &value
);
1789 if (sockfd_r
!= -1) {
1790 pth_cancel(tcp_accept_tid
);
1791 pth_join(tcp_accept_tid
, &value
);
1796 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1797 unlink(*socketpath
);
1798 g_free(*socketpath
);
1802 log_write(N_("waiting for all threads to terminate"));
1804 ev_quit
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1807 if (n
!= n_clients
) {
1808 log_write(N_("%i threads remain"), n
-1);
1813 pth_event_isolate(ev_quit
);
1814 timeout_ev
= timeout_event_iterate(timeout_ev
, attr
);
1815 keepalive_ev
= keepalive_event_iterate(keepalive_ev
, keepalive
);
1816 ev_quit
= pth_event_concat(ev_quit
, timeout_ev
, keepalive_ev
, NULL
);
1817 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1820 pth_event_free(timeout_ev
, PTH_FREE_THIS
);
1821 pth_event_free(keepalive_ev
, PTH_FREE_THIS
);
1822 pth_event_free(ev_quit
, PTH_FREE_THIS
);
1823 pth_attr_destroy(attr
);
1828 * Called from pinentry_fork() in the child process.
1830 void free_client_list()
1832 gint i
, t
= g_slist_length(cn_thread_list
);
1834 for (i
= 0; i
< t
; i
++) {
1835 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1837 free_client(cn
->cl
);
1843 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1844 const gchar
*outfile
)
1846 file_header_internal_t
*fh
;
1852 md5file
= gcry_malloc(16);
1854 return GPG_ERR_ENOMEM
;
1856 shakey
= gcry_malloc(gcrykeysize
);
1859 return GPG_ERR_ENOMEM
;
1862 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
1864 fh
= read_file_header(filename
, TRUE
, &rc
);
1869 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
1871 /* The header in version 1 had a bug where the iterations were off-by-one.
1872 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
1875 if (fh
->fh1
.iter
>= 0) {
1877 gchar
*tmp
= parse_rcfile_keyfile(keyfile
, TRUE
);
1880 rc
= GPG_ERR_UNKNOWN_ERRNO
;
1884 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, tmp
,
1885 strlen(tmp
) ? strlen(tmp
) : 1);
1887 rc
= try_xml_decrypt(NULL
, shakey
, fh
, &fh
->doc
, &fh
->len
);
1890 if (get_password(filename
, fh
, md5file
, shakey
, PINENTRY_OPEN
)
1892 rc
= GPG_ERR_UNKNOWN_ERRNO
;
1899 rc
= try_xml_decrypt(NULL
, shakey
, fh
, &fh
->doc
, &fh
->len
);
1906 rc
= convert_xml((gchar
**)&fh
->doc
, &fh
->len
);
1910 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1915 iter
= fh
->fh1
.iter
;
1916 memset(&fh
->fh2
, 0, sizeof(fh
->fh2
));
1917 /* Keep the iterations and key from the original file. */
1918 fh
->fh2
.iter
= iter
+1;
1919 rc
= export_common(outfile
, fh
, shakey
, fh
->doc
, fh
->len
);
1922 send_error(NULL
, rc
);
1925 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
1932 int main(int argc
, char *argv
[])
1935 struct sockaddr_un addr
;
1936 struct passwd
*pw
= getpwuid(getuid());
1937 gchar buf
[PATH_MAX
];
1938 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1939 gchar
*socketarg
= NULL
;
1940 gchar
*datadir
= NULL
;
1943 gchar
**cache_push
= NULL
;
1945 gchar
*import
= NULL
, *keyfile
= NULL
;
1946 gulong cmd_iterations
= -1;
1947 gint default_timeout
;
1948 gint rcfile_spec
= 0;
1949 gint estatus
= EXIT_FAILURE
;
1950 gint sockfd
, sockfd_r
= -1;
1951 gchar
*outfile
= NULL
;
1953 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1956 gboolean secure
= FALSE
;
1958 gint background
= 1;
1960 gchar
*convert
= NULL
;
1961 #ifdef WITH_PINENTRY
1962 gboolean disable_pinentry
= FALSE
;
1965 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
1969 #ifdef HAVE_SETRLIMIT
1972 rl
.rlim_cur
= rl
.rlim_max
= 0;
1974 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1975 err(EXIT_FAILURE
, "setrlimit()");
1980 setlocale(LC_ALL
, "");
1981 bindtextdomain("pwmd", LOCALEDIR
);
1986 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1988 g_mem_set_vtable(&mtable
);
1989 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1990 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1993 gnutls_global_set_mem_functions(xmalloc
, xmalloc
, gcry_SecureCheck
,
1997 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pth
);
1999 gnutls_global_init();
2000 gnutls_global_set_log_function(tls_log
);
2001 gnutls_global_set_log_level(1);
2002 assuan_set_io_hooks(&io_hooks
);
2005 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
2007 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2008 err(EXIT_FAILURE
, "%s", buf
);
2010 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
2012 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2013 err(EXIT_FAILURE
, "%s", buf
);
2015 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
2017 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
2018 err(EXIT_FAILURE
, "sysconf()");
2022 while ((opt
= getopt(argc
, argv
, "Po:C:bnI:i:k:hvf:D")) != EOF
) {
2024 #ifdef WITH_PINENTRY
2026 disable_pinentry
= TRUE
;
2036 /* Compatibility for version < 1.11 */
2048 cmd_iterations
= strtol(optarg
, NULL
, 10);
2055 rcfile
= g_strdup(optarg
);
2059 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
,
2061 #ifdef WITH_PINENTRY
2094 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2097 #ifdef WITH_PINENTRY
2098 if (disable_pinentry
== TRUE
)
2099 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2102 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2103 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2105 if (log_syslog
== TRUE
)
2106 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2108 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2109 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2112 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
2113 log_write("setpriority(): %s", strerror(errno
));
2118 #ifdef HAVE_MLOCKALL
2119 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2120 log_write("mlockall(): %s", strerror(errno
));
2131 opt
= convert_file(convert
, keyfile
, outfile
);
2132 g_key_file_free(keyfileh
);
2134 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2141 if (cmd_iterations
== -1)
2142 cmd_iterations
= get_key_file_integer("global", "iterations");
2144 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2145 g_key_file_free(keyfileh
);
2147 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2150 g_key_file_set_list_separator(keyfileh
, ',');
2152 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2153 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2157 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
2159 socketarg
= g_strdup(buf
);
2164 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2165 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2167 datadir
= expand_homedir(p
);
2170 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2171 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2172 disable_list_and_dump
= n
;
2175 disable_list_and_dump
= secure
;
2177 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2178 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2180 default_timeout
= -1;
2182 setup_logging(keyfileh
);
2184 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2185 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2187 if (argc
!= optind
) {
2189 ptotal
= g_strv_length(cache_push
);
2191 for (; optind
< argc
; optind
++) {
2192 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2193 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2197 if (strchr(socketarg
, '/') == NULL
) {
2198 socketdir
= g_get_current_dir();
2199 socketname
= g_strdup(socketarg
);
2200 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2203 socketname
= g_strdup(strrchr(socketarg
, '/'));
2205 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2206 socketdir
= g_strdup(socketarg
);
2207 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2210 if (chdir(datadir
)) {
2211 log_write("%s: %s", datadir
, strerror(errno
));
2216 if (parse_rcfile_keys(TRUE
) == FALSE
)
2219 clear_rcfile_keys();
2222 * Set the cache entry for a file. Prompts for the password.
2225 for (opt
= 0; cache_push
[opt
]; opt
++)
2226 do_cache_push(cache_push
[opt
], NULL
);
2228 g_strfreev(cache_push
);
2229 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2233 * bind() doesn't like the full pathname of the socket or any non alphanum
2234 * characters so change to the directory where the socket is wanted then
2235 * create it then change to datadir.
2237 if (chdir(socketdir
)) {
2238 log_write("%s: %s", socketdir
, strerror(errno
));
2244 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2245 log_write("socket(): %s", strerror(errno
));
2249 addr
.sun_family
= AF_UNIX
;
2250 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2252 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2253 log_write("bind(): %s", strerror(errno
));
2255 if (errno
== EADDRINUSE
)
2256 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2257 "stale socket. Please remove it manually."), socketpath
);
2263 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2264 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2265 mode_t mode
= strtol(t
, NULL
, 8);
2266 mode_t mask
= umask(0);
2270 if (chmod(socketname
, mode
) == -1) {
2271 log_write("%s: %s", socketname
, strerror(errno
));
2281 g_free(--socketname
);
2283 if (chdir(datadir
)) {
2284 log_write("%s: %s", datadir
, strerror(errno
));
2291 pth_mutex_init(&cache_mutex
);
2292 #ifdef WITH_PINENTRY
2293 pth_mutex_init(&pin_mutex
);
2296 if (listen(sockfd
, 0) == -1) {
2297 log_write("listen(): %s", strerror(errno
));
2302 if (get_key_file_boolean("global", "enable_tcp")) {
2304 struct addrinfo hints
, *servinfo
, *p
;
2305 gint port
= get_key_file_integer("global", "tcp_port");
2308 memset(&hints
, 0, sizeof(hints
));
2309 hints
.ai_family
= AF_UNSPEC
;
2310 hints
.ai_socktype
= SOCK_STREAM
;
2311 hints
.ai_flags
= AI_PASSIVE
;
2313 if ((opt
= getaddrinfo(NULL
, print_fmt(buf
, sizeof(buf
), "%i", port
),
2314 &hints
, &servinfo
)) == -1) {
2315 log_write("getaddrinfo(): %s", gai_strerror(opt
));
2319 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
2320 if ((sockfd_r
= socket(p
->ai_family
, p
->ai_socktype
,
2321 p
->ai_protocol
)) == -1) {
2322 log_write("socket(): %s", strerror(errno
));
2328 if (setsockopt(sockfd_r
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,
2329 sizeof(int)) == -1) {
2330 log_write("setsockopt(): %s", strerror(errno
));
2334 if (bind(sockfd_r
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
2336 log_write("bind(): %s", strerror(errno
));
2344 freeaddrinfo(servinfo
);
2347 log_write("%s", N_("could not bind"));
2351 if (g_key_file_has_key(keyfileh
, "global", "tcp_interface", NULL
)) {
2352 gchar
*tmp
= get_key_file_string("global", "tcp_interface");
2354 if (setsockopt(sockfd_r
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
, 1)
2356 log_write("setsockopt(): %s", strerror(errno
));
2364 ret
= gnutls_certificate_allocate_credentials(&x509_cred
);
2366 if (ret
!= GNUTLS_E_SUCCESS
) {
2367 log_write("%s", gnutls_strerror(ret
));
2371 tmp
= expand_homedir("~/.pwmd/ca-cert.pem");
2374 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2378 ret
= gnutls_certificate_set_x509_trust_file(x509_cred
, tmp
,
2379 GNUTLS_X509_FMT_PEM
);
2383 log_write("%s", gnutls_strerror(ret
));
2387 tmp
= expand_homedir("~/.pwmd/server-cert.pem");
2390 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2394 tmp2
= expand_homedir("~/.pwmd/server-key.pem");
2398 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2402 ret
= gnutls_certificate_set_x509_key_file (x509_cred
, tmp
, tmp2
,
2403 GNUTLS_X509_FMT_PEM
);
2407 if (ret
!= GNUTLS_E_SUCCESS
) {
2408 log_write("%s", gnutls_strerror(ret
));
2417 log_write("fork(): %s", strerror(errno
));
2433 if (get_key_file_boolean("global", "enable_tcp")) {
2434 log_write("%s", N_("Generating key exchange parameters..."));
2435 ret
= gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM
, 0);
2438 log_write("%s", gpg_strerror(ret
));
2442 ret
= gnutls_dh_params_init(&dh_params
);
2444 if (ret
!= GNUTLS_E_SUCCESS
) {
2445 log_write("%s", gnutls_strerror(ret
));
2449 ret
= gnutls_dh_params_generate2(dh_params
, 1024);
2451 if (ret
!= GNUTLS_E_SUCCESS
) {
2452 log_write("%s", gnutls_strerror(ret
));
2456 gnutls_certificate_set_dh_params(x509_cred
, dh_params
);
2457 ret
= gnutls_rsa_params_init(&rsa_params
);
2459 if (ret
!= GNUTLS_E_SUCCESS
) {
2460 log_write("%s", gnutls_strerror(ret
));
2464 ret
= gnutls_rsa_params_generate2(rsa_params
, 512);
2466 if (ret
!= GNUTLS_E_SUCCESS
) {
2467 log_write("%s", gnutls_strerror(ret
));
2471 gnutls_certificate_set_rsa_export_params(x509_cred
, rsa_params
);
2472 gnutls_certificate_set_params_function(x509_cred
, tls_get_params
);
2474 if (listen(sockfd_r
, 0) == -1) {
2475 log_write("listen(): %s", strerror(errno
));
2482 * These are the signals that we use in threads. libpth can catch signals
2483 * itself so ignore them everywhere else. Note that using
2484 * signal(N, SIG_IGN) doesn't work like you might think.
2489 sigaddset(&set
, SIGTERM
);
2490 sigaddset(&set
, SIGINT
);
2492 /* Configuration file reloading. */
2493 sigaddset(&set
, SIGUSR1
);
2495 /* Clears the file cache. */
2496 sigaddset(&set
, SIGHUP
);
2498 /* Caught in client_thread(). Sends a cache status message. */
2499 sigaddset(&set
, SIGUSR2
);
2501 /* Ignored everywhere. When a client disconnects abnormally this signal
2502 * gets raised. It isn't needed though because client_thread() will check
2503 * for rcs even after the client disconnects. */
2504 signal(SIGPIPE
, SIG_IGN
);
2505 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
2506 server_loop(sockfd
, sockfd_r
, &socketpath
);
2507 estatus
= EXIT_SUCCESS
;
2510 if (socketpath
&& do_unlink
) {
2516 if (sockfd_r
!= -1) {
2517 gnutls_dh_params_deinit(dh_params
);
2518 gnutls_rsa_params_deinit(rsa_params
);
2521 gnutls_certificate_free_credentials(x509_cred
);
2523 gnutls_global_deinit();
2527 g_key_file_free(keyfileh
);
2531 if (estatus
== EXIT_SUCCESS
)
2532 log_write(N_("pwmd exiting normally"));
2535 #if defined(DEBUG) && !defined(MEM_DEBUG)