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_PTH_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
;
99 pth_attr_t attr
= pth_attr_of(pth_self());
101 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
102 pth_attr_destroy(attr
);
103 MUTEX_LOCK(&rcfile_mutex
);
104 log_write(N_("reloading configuration file '%s'"), rcfile
);
105 k
= parse_rcfile(FALSE
);
110 g_key_file_free(keyfileh
);
114 disable_list_and_dump
= b
;
118 startStopKeepAlive(FALSE
);
119 send_status_all(STATUS_CONFIG
);
121 MUTEX_UNLOCK(&rcfile_mutex
);
125 static void reload_rcfile()
128 pth_attr_t attr
= pth_attr_new();
131 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, 0);
132 tid
= pth_spawn(attr
, reload_rcfile_thread
, NULL
);
133 pth_attr_destroy(attr
);
136 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
138 gpg_error_t n
= gpg_error_from_errno(e
);
141 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, _gpg_strerror(n
)));
144 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
146 gpg_err_code_t n
= gpg_err_code(e
);
147 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
148 struct client_s
*client
= assuan_get_pointer(ctx
);
153 return assuan_process_done(ctx
, 0);
156 log_write("%s", pwmd_strerror(e
));
160 if (n
== EPWMD_LIBXML_ERROR
) {
161 xmlErrorPtr xe
= client
->xml_error
;
164 xe
= xmlGetLastError();
166 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
167 log_write("%s", xe
->message
);
169 if (xe
== client
->xml_error
)
174 client
->xml_error
= NULL
;
178 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
181 void log_write(const gchar
*fmt
, ...)
191 pth_t tid
= pth_self();
194 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
197 if (!cmdline
&& logfile
) {
198 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
206 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
217 fprintf(stderr
, "%s\n", args
);
223 attr
= pth_attr_of(tid
);
225 if (pth_attr_get(attr
, PTH_ATTR_NAME
, &name
) == FALSE
)
228 pth_attr_destroy(attr
);
229 name
= print_fmt(buf
, sizeof(buf
), "%s(%p): ", name
, tid
);
231 if (!cmdline
&& log_syslog
== TRUE
)
232 syslog(LOG_INFO
, "%s%s", name
, args
);
235 tm
= localtime(&now
);
236 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
237 tbuf
[sizeof(tbuf
) - 1] = 0;
239 if (args
[strlen(args
)-1] == '\n')
240 args
[strlen(args
)-1] = 0;
242 line
= g_strdup_printf("%s %i %s%s\n", tbuf
, getpid(), name
, args
);
253 write(fd
, line
, strlen(line
));
258 if (isatty(STDERR_FILENO
)) {
259 fprintf(stderr
, "%s", line
);
266 static void usage(gchar
*pn
)
268 g_fprintf(stderr
, N_(
269 "Usage: %s [-hvDnP] [-f <rcfile>] [-C <filename>] "
270 "[-I <filename> [-i <iter>]]\n "
271 "[-k <keyfile>] [-o <outfile>] [file1] [...]\n"
272 " -n run as a foreground process\n"
273 " -f load the specified rcfile (~/.pwmd/config)\n"
274 " -C convert a version 1 data file to version 2\n"
275 " -I import an XML file\n"
276 " -i encrypt with the specified number of iterations when importing\n"
277 " (config default in the \"global\" section)\n"
278 " -k obtain the key from the specified file\n"
279 " -o output file for use with the -C and -I options\n"
280 " -D disable use of the LIST and DUMP commands\n"
281 " -P disable pinentry\n"
283 " -h this help text\n"
288 static int gcry_SecureCheck(const void *ptr
)
293 static void setup_gcrypt()
295 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pth
);
296 gcry_check_version(GCRYPT_VERSION
);
298 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
301 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
303 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
305 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
306 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
309 static gint
new_connection(struct client_s
*cl
)
312 gchar ver
[ASSUAN_LINELENGTH
];
314 rc
= assuan_init_socket_server_ext(&cl
->ctx
, cl
->thd
->fd
, 2);
319 assuan_set_pointer(cl
->ctx
, cl
);
320 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
321 assuan_set_hello_line(cl
->ctx
, ver
);
322 rc
= register_commands(cl
->ctx
);
327 rc
= assuan_accept(cl
->ctx
);
335 log_write("%s", _gpg_strerror(rc
));
341 static void xml_error_cb(void *data
, xmlErrorPtr e
)
343 struct client_s
*client
= data
;
346 * Keep the first reported error as the one to show in the error
347 * description. Reset in send_error().
349 if (client
->xml_error
)
352 xmlCopyError(e
, client
->xml_error
);
355 static void cleanup_crypto_handler(void *arg
)
357 struct client_crypto_s
**a
= (struct client_crypto_s
**)arg
;
358 struct client_crypto_s
*cr
= *a
;
361 if (cr
->fh
->fd
!= -1 ||
362 (cmdline
== TRUE
&& cr
->fh
->fd
!= STDOUT_FILENO
))
366 gcry_free(cr
->fh
->doc
);
372 gcry_cipher_close(cr
->gh
);
378 void cleanup_crypto(struct client_crypto_s
**c
)
380 struct client_crypto_s
*cr
= *c
;
395 gcry_free(cr
->tkey2
);
398 gcry_free(cr
->inbuf
);
401 gcry_free(cr
->outbuf
);
403 cleanup_crypto_handler(c
);
407 * This is called after a child_thread terminates. Set with
408 * pth_cleanup_push().
410 static void cleanup_cb(void *arg
)
412 struct client_thread_s
*cn
= arg
;
413 struct client_s
*cl
= cn
->cl
;
415 MUTEX_LOCK(&cn_mutex
);
416 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
417 MUTEX_UNLOCK(&cn_mutex
);
420 pth_cancel(cn
->msg_tid
);
421 pth_join(cn
->msg_tid
, NULL
);
425 struct status_msg_s
*m
= g_slist_nth_data(cn
->msg_list
, 0);
430 cn
->msg_list
= g_slist_remove(cn
->msg_list
, m
);
434 if (cl
&& cl
->freed
== FALSE
)
439 gnutls_deinit(cn
->tls
->ses
);
449 assuan_deinit_server(cl
->ctx
);
450 else if (cl
&& cl
->thd
&& cl
->thd
->fd
!= -1)
454 if (cl
&& cl
->pinentry
)
455 cleanup_pinentry(cl
->pinentry
);
459 cleanup_crypto(&cl
->crypto
);
463 log_write(N_("exiting, fd=%i"), cn
->fd
);
464 send_status_all(STATUS_CLIENTS
);
468 * Called every time a connection is made from init_new_connection(). This is
469 * the thread entry point.
471 static void *client_thread(void *data
)
473 struct client_thread_s
*thd
= data
;
474 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
476 pth_attr_t attr
= pth_attr_of(pth_self());
478 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
479 pth_attr_destroy(attr
);
482 * Prevent a race condition with init_new_connection() if this thread
483 * fails (returns) for some reason before init_new_connection() releases
486 MUTEX_LOCK(&cn_mutex
);
487 MUTEX_UNLOCK(&cn_mutex
);
488 pth_cleanup_push(cleanup_cb
, thd
);
491 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
499 * Do the TLS handshake before anything else.
502 gchar
*prio
= get_key_file_string("global", "cipher_suite");
504 thd
->tls
= tls_init(thd
->fd
, prio
);
519 if (new_connection(cl
))
523 cl
->pinentry
= pinentry_init();
526 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
531 /* Require the client to explicity set OPTION PINENTRY since the DISPLAY
532 * might be automatically set from the client. Connections to the X11
533 * server usually aren't encrypted and the client might unintentionally
534 * send the passphrase in the clear.
537 cl
->pinentry
->enable
= FALSE
;
542 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
543 log_write("mlockall(): %s", strerror(errno
));
548 pth_mutex_init(&thd
->msg_list_mutex
);
549 attr
= pth_attr_new();
551 thd
->msg_tid
= pth_spawn(attr
, client_msg_thread
, thd
);
552 pth_attr_destroy(attr
);
553 pth_yield(thd
->msg_tid
);
554 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
557 log_write("%s", _gpg_strerror(rc
));
561 send_status_all(STATUS_CLIENTS
);
562 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
566 pth_event_t pev
= NULL
;
569 pth_event_t ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
,
573 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
574 pev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->pinentry
->fd
);
575 ev
= pth_event_concat(ev
, pev
, NULL
);
580 pth_event_isolate(ev
);
581 st
= pth_event_status(ev
);
583 if (st
== PTH_STATUS_OCCURRED
) {
584 rc
= assuan_process_next(cl
->ctx
);
588 cl
->inquire_status
= INQUIRE_INIT
;
590 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
593 log_write("assuan_process_next(): %s", _gpg_strerror(rc
));
594 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
597 log_write("assuan_process_done(): %s", _gpg_strerror(rc
));
603 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
604 cl
->pinentry
->status
= PINENTRY_RUNNING
;
607 switch (cl
->inquire_status
) {
612 cl
->inquire_status
= INQUIRE_INIT
;
613 rc
= assuan_process_done(cl
->ctx
, 0);
620 pth_event_free(ev
, PTH_FREE_THIS
);
624 pth_event_isolate(pev
);
625 st
= pth_event_status(pev
);
626 pth_event_free(pev
, PTH_FREE_THIS
);
629 rc
= pinentry_iterate(cl
,
630 pev
&& cl
->pinentry
->fd
!= -1 && st
== PTH_STATUS_OCCURRED
);
635 * Client cleanup (including XML data) is done in cleanup_cb() from
636 * the cleanup thread.
641 pth_exit(PTH_CANCELED
);
645 static void setup_logging(GKeyFile
*kf
)
647 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
650 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
656 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
662 logfile
= g_strdup(buf
);
672 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
676 * Make sure all settings are set to either the specified setting or a
679 static void set_rcfile_defaults(GKeyFile
*kf
)
683 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
684 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
685 g_key_file_set_string(kf
, "global", "socket_path", buf
);
688 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
689 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
690 g_key_file_set_string(kf
, "global", "data_directory", buf
);
693 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
694 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
696 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
697 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
698 g_key_file_set_string(kf
, "global", "log_path", buf
);
701 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
702 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
705 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
706 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
709 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
710 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
712 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
||
713 g_key_file_get_integer(kf
, "global", "iterations", 0) < 0)
714 g_key_file_set_integer(kf
, "global", "iterations", 1);
716 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
717 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
719 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
720 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
722 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
723 g_key_file_set_integer(kf
, "global", "compression_level", 6);
725 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
726 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
728 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
729 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
731 zlib_bufsize
= (uInt
)g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
733 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
734 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
737 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
740 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
741 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
743 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
744 g_key_file_set_integer(kf
, "global", "keepalive", DEFAULT_KEEPALIVE_TO
);
747 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
748 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
750 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
751 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
755 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
756 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
758 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
759 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
761 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
762 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
764 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
765 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
767 if (g_key_file_has_key(kf
, "global", "cipher_suite", NULL
) == FALSE
)
768 g_key_file_set_string(kf
, "global", "cipher_suite", "SECURE256");
770 if (g_key_file_has_key(kf
, "global", "tcp_use_crl", NULL
) == FALSE
)
771 g_key_file_set_boolean(kf
, "global", "tcp_use_crl", FALSE
);
777 static GKeyFile
*parse_rcfile(gboolean specified
)
779 GKeyFile
*kf
= g_key_file_new();
782 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
783 log_write("%s: %s", rcfile
, rc
->message
);
785 if (cmdline
&& specified
) {
790 if (rc
->code
== G_FILE_ERROR_NOENT
) {
792 set_rcfile_defaults(kf
);
800 set_rcfile_defaults(kf
);
804 static gchar
*do_get_password(const gchar
*prompt
)
806 gchar buf
[LINE_MAX
] = {0}, *p
;
807 struct termios told
, tnew
;
810 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
811 log_write("tcgetattr(): %s", strerror(errno
));
815 memcpy(&tnew
, &told
, sizeof(struct termios
));
816 tnew
.c_lflag
&= ~(ECHO
);
817 tnew
.c_lflag
|= ICANON
|ECHONL
;
819 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
820 log_write("tcsetattr(): %s", strerror(errno
));
821 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
825 fprintf(stderr
, "%s", prompt
);
827 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
828 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
832 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
833 p
[strlen(p
) - 1] = 0;
836 key
= gcry_malloc(1);
840 key
= gcry_malloc(strlen(p
) + 1);
841 sprintf(key
, "%s", p
);
844 memset(&buf
, 0, sizeof(buf
));
848 /* Only used when "enable_pinentry" is "false" or -P. */
849 static gpg_error_t
get_input(const gchar
*filename
,
850 struct client_crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
854 if (which
== PINENTRY_SAVE
) {
855 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
856 crypto
->tkey
= do_get_password(prompt
);
860 log_write(N_("%s: Skipping file"), filename
);
861 return GPG_ERR_BAD_PASSPHRASE
;
864 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
865 crypto
->tkey2
= do_get_password(prompt
);
868 if (!crypto
->tkey2
) {
869 log_write(N_("%s: Skipping file"), filename
);
870 return GPG_ERR_BAD_PASSPHRASE
;
873 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
874 log_write(N_("%s: Passphrase mismatch"), filename
);
878 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
879 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
883 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
885 if ((crypto
->tkey
= do_get_password(prompt
)) == NULL
) {
886 log_write(N_("%s: Skipping file"), filename
);
888 return GPG_ERR_BAD_PASSPHRASE
;
891 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
892 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
898 * inbuf must have been allocated with gcry_malloc().
900 gpg_error_t
export_common(const gchar
*filename
, struct client_crypto_s
*crypto
,
901 gpointer inbuf
, gulong insize
)
908 level
= get_key_file_integer(filename
, "compression_level");
913 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
915 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
918 crypto
->inbuf
= outbuf
;
919 crypto
->insize
= outsize
;
920 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
924 static gpg_error_t
get_password(const gchar
*filename
,
925 struct client_crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
926 pinentry_cmd_t which
)
931 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
934 return get_input(filename
, crypto
, key
, which
);
938 gchar
*result
= NULL
;
939 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
941 set_pinentry_defaults(pin
);
943 pin
->filename
= g_strdup(filename
);
944 rc
= pinentry_getpin(pin
, &result
);
951 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
953 cleanup_pinentry(pin
);
961 static gboolean
_getline(const gchar
*file
, gchar
**result
, gpg_error_t
*rc
)
964 gchar buf
[LINE_MAX
] = {0}, *p
;
970 if ((fp
= fopen(file
, "r")) == NULL
) {
971 *rc
= gpg_error_from_syserror();
975 p
= fgets(buf
, sizeof(buf
), fp
);
979 if (len
&& buf
[len
- 1] == '\n')
982 str
= gcry_malloc(len
+ 1);
985 *rc
= gpg_error_from_errno(ENOMEM
);
989 memcpy(str
, buf
, len
? len
: 1);
991 memset(&buf
, 0, sizeof(buf
));
996 static gchar
*parse_rcfile_keyfile(const gchar
*filename
, gboolean import
,
1000 gchar
*t
, *file
= NULL
, *str
;
1002 *rc
= GPG_ERR_UNKNOWN_ERRNO
;
1004 if (import
== FALSE
) {
1005 if (g_key_file_has_key(keyfileh
, filename
, "key_file", &rv
) == TRUE
) {
1006 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rv
);
1010 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1017 t
= expand_homedir(file
);
1020 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1021 *rc
= gpg_error_from_errno(ENOMEM
);
1030 /* -I or -C. The filename is a key file. */
1031 file
= g_strdup(filename
);
1034 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1035 *rc
= gpg_error_from_errno(ENOMEM
);
1041 log_write("%s: key_file: %s", rcfile
, rv
->message
);
1049 if (_getline(file
, &str
, rc
) == FALSE
) {
1050 log_write("%s: %s: %s", filename
, file
, pwmd_strerror(*rc
));
1060 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
1061 const gchar
*keyfile
, guint64 iter
)
1070 struct client_crypto_s
*crypto
;
1072 if (stat(filename
, &st
) == -1) {
1073 log_write("%s: %s", filename
, strerror(errno
));
1077 crypto
= init_client_crypto();
1082 crypto
->key
= gcry_malloc(gcrykeysize
);
1083 memset(crypto
->key
, 0, gcrykeysize
);
1086 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1090 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
1093 if (iter
&& keyfile
) {
1094 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1099 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1100 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1103 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
1109 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1110 log_write("%s: %s", filename
, strerror(errno
));
1114 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1116 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1120 if (pth_read(fd
, xmlbuf
, st
.st_size
) == -1) {
1124 log_write("%s: %s", filename
, strerror(errno
));
1129 xmlbuf
[st
.st_size
] = 0;
1132 * Make sure the document validates.
1134 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1135 log_write("xmlReadDoc() failed");
1141 xmlDocDumpMemory(doc
, &xml
, &len
);
1145 memset(crypto
->key
, '!', gcrykeysize
);
1147 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1150 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1154 crypto
->fh
->fh2
.iter
= iter
;
1155 rc
= export_common(outfile
, crypto
, xml
, len
);
1159 send_error(NULL
, rc
);
1163 cleanup_crypto(&crypto
);
1167 cleanup_crypto(&crypto
);
1171 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1176 MUTEX_LOCK(&rcfile_mutex
);
1178 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1179 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1182 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1183 g_clear_error(&grc
);
1187 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1188 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1191 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1192 g_clear_error(&grc
);
1197 MUTEX_UNLOCK(&rcfile_mutex
);
1201 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1206 MUTEX_LOCK(&rcfile_mutex
);
1208 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
1209 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
1212 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1213 g_clear_error(&grc
);
1217 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1218 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1221 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1222 g_clear_error(&grc
);
1227 MUTEX_UNLOCK(&rcfile_mutex
);
1231 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1233 gboolean val
= FALSE
;
1236 MUTEX_LOCK(&rcfile_mutex
);
1238 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1239 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1242 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1243 g_clear_error(&grc
);
1247 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1248 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1251 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1252 g_clear_error(&grc
);
1257 MUTEX_UNLOCK(&rcfile_mutex
);
1261 static gboolean
parse_rcfile_keys()
1268 groups
= g_key_file_get_groups(keyfileh
, &n
);
1270 for (p
= groups
; *p
; p
++) {
1273 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1274 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1278 log_write("%s: key: %s", rcfile
, rc
->message
);
1284 do_cache_push(*p
, str
);
1290 log_write("%s: key: %s", rcfile
, rc
->message
);
1296 str
= parse_rcfile_keyfile(*p
, FALSE
, &ret
);
1301 do_cache_push(*p
, str
);
1309 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1313 const gchar
*p
= filename
;
1314 struct client_crypto_s
*crypto
;
1323 if (valid_filename(p
) == FALSE
) {
1324 log_write(N_("%s: Invalid characters in filename"), p
);
1328 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1330 if (access(p
, R_OK
|W_OK
) != 0) {
1331 log_write("%s: %s", p
, strerror(errno
));
1335 crypto
= init_client_crypto();
1340 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1343 log_write("%s: %s", p
, pwmd_strerror(rc
));
1344 cleanup_crypto(&crypto
);
1348 crypto
->key
= gcry_malloc(gcrykeysize
);
1351 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1352 cleanup_crypto(&crypto
);
1356 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1358 if (crypto
->fh
->fh2
.iter
<= 0) {
1359 memset(crypto
->key
, '!', gcrykeysize
);
1364 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1367 send_error(NULL
, rc
);
1368 cleanup_crypto(&crypto
);
1372 gcry_free(crypto
->fh
->doc
);
1373 crypto
->fh
->doc
= NULL
;
1376 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
,
1377 strlen(password
) ? strlen(password
) : 1);
1380 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, NULL
, NULL
);
1383 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1384 cleanup_crypto(&crypto
);
1388 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1389 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1390 cleanup_crypto(&crypto
);
1394 timeout
= get_key_file_integer(p
, "cache_timeout");
1395 cache_set_timeout(md5file
, timeout
);
1396 log_write(N_("File '%s' now cached"), filename
);
1397 cleanup_crypto(&crypto
);
1401 static void init_new_connection(gint fd
, gchar
*addr
)
1404 struct client_thread_s
*new;
1406 new = g_malloc0(sizeof(struct client_thread_s
));
1409 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1414 MUTEX_LOCK(&cn_mutex
);
1422 attr
= pth_attr_new();
1423 pth_attr_init(attr
);
1424 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1425 new->tid
= pth_spawn(attr
, client_thread
, new);
1426 pth_attr_destroy(attr
);
1431 log_write("pth_spawn() failed");
1432 MUTEX_UNLOCK(&cn_mutex
);
1436 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1437 MUTEX_UNLOCK(&cn_mutex
);
1440 log_write(N_("new connection: tid=%p, fd=%i, addr=%s"), new->tid
, fd
,
1443 log_write(N_("new connection: tid=%p, fd=%i"), new->tid
, fd
);
1447 /* From Beej's Guide to Network Programming. It's a good tutorial. */
1448 static void *get_in_addr(struct sockaddr
*sa
)
1450 if (sa
->sa_family
== AF_INET
)
1451 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
1453 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
1456 static void *tcp_accept_thread(void *arg
)
1458 gint sockfd
= (gint
)arg
;
1459 pth_attr_t attr
= pth_attr_of(pth_self());
1461 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1462 pth_attr_destroy(attr
);
1465 struct sockaddr_storage raddr
;
1466 socklen_t slen
= sizeof(raddr
);
1471 fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1475 if (errno
!= EAGAIN
) {
1476 if (!quit
) // probably EBADF
1477 log_write("accept(): %s", strerror(errno
));
1489 gchar s
[INET6_ADDRSTRLEN
];
1491 inet_ntop(raddr
.ss_family
, get_in_addr((struct sockaddr
*)&raddr
),
1493 init_new_connection(fd
, s
);
1496 t
= get_key_file_string("global", "tcp_wait");
1497 n
= strtol(t
, NULL
, 10);
1503 pth_usleep(n
*100000);
1506 /* Just in case accept() failed for some reason other than EBADF */
1508 pth_exit(PTH_CANCELED
);
1513 static void *accept_thread(void *arg
)
1515 gint sockfd
= (gint
)arg
;
1516 pth_attr_t attr
= pth_attr_of(pth_self());
1518 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1519 pth_attr_destroy(attr
);
1522 socklen_t slen
= sizeof(struct sockaddr_un
);
1523 struct sockaddr_un raddr
;
1526 fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1530 if (errno
!= EAGAIN
) {
1531 if (!quit
) // probably EBADF
1532 log_write("accept(): %s", strerror(errno
));
1541 init_new_connection(fd
, NULL
);
1544 /* Just in case accept() failed for some reason other than EBADF */
1546 pth_exit(PTH_CANCELED
);
1550 static void *adjust_cache_timer_thread(void *arg
)
1552 pth_attr_t attr
= pth_attr_of(pth_self());
1554 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1555 pth_attr_destroy(attr
);
1561 cache_adjust_timer();
1568 static void keepalive_cleanup(void *arg
)
1570 pth_event_t ev
= arg
;
1572 pth_event_free(ev
, PTH_FREE_ALL
);
1575 static void *keepalive_thread(void *arg
)
1577 gint to
= (gint
)arg
;
1580 pth_attr_t attr
= pth_attr_of(pth_self());
1582 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1583 pth_attr_destroy(attr
);
1584 pth_cond_init(&cond
);
1586 pth_mutex_acquire(&m
, FALSE
, NULL
);
1589 pth_event_t ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0));
1590 pth_cleanup_push(pth_mutex_release
, (void *)&m
);
1591 pth_cleanup_push(keepalive_cleanup
, ev
);
1592 pth_cond_await(&cond
, &m
, ev
);
1594 send_status_all(STATUS_KEEPALIVE
);
1602 static void startStopKeepAlive(gboolean term
)
1604 gint n
= get_key_file_integer("global", "keepalive");
1607 pth_cancel(keepalive_tid
);
1609 keepalive_tid
= NULL
;
1615 pth_attr_t attr
= pth_attr_new();
1616 pth_attr_init(attr
);
1617 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1618 keepalive_tid
= pth_spawn(attr
, keepalive_thread
, (void *)n
);
1619 pth_yield(keepalive_tid
);
1620 pth_attr_destroy(attr
);
1624 static void server_loop(gint sockfd
, gchar
**socketpath
)
1631 pth_t cache_timeout_tid
;
1633 sigemptyset(&sigset
);
1636 sigaddset(&sigset
, SIGTERM
);
1637 sigaddset(&sigset
, SIGINT
);
1639 /* Clears the file cache. */
1640 sigaddset(&sigset
, SIGUSR1
);
1642 /* Configuration file reloading. */
1643 sigaddset(&sigset
, SIGHUP
);
1645 /* Caught in client_thread(). Sends a cache status message. */
1646 sigaddset(&sigset
, SIGUSR2
);
1648 /* Ignored everywhere. When a client disconnects abnormally this signal
1649 * gets raised. It isn't needed though because client_thread() will check
1650 * for rcs even after the client disconnects. */
1651 signal(SIGPIPE
, SIG_IGN
);
1652 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1654 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1655 attr
= pth_attr_new();
1656 pth_attr_init(attr
);
1657 accept_tid
= pth_spawn(attr
, accept_thread
, (void *)sockfd
);
1658 pth_yield(accept_tid
);
1659 startStopKeepAlive(FALSE
);
1660 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1661 cache_timeout_tid
= pth_spawn(attr
, adjust_cache_timer_thread
, NULL
);
1662 pth_yield(cache_timeout_tid
);
1663 pth_attr_destroy(attr
);
1668 pth_sigwait(&sigset
, &sig
);
1669 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1671 /* Caught a signal. */
1678 cache_clear(NULL
, 2);
1686 log_write(N_("clearing file cache"));
1687 cache_clear(NULL
, 2);
1697 * We're out of the main server loop. This happens when a signal was sent
1698 * to terminate the daemon. We'll wait for all clients to disconnect
1699 * before exiting and ignore any following signals.
1701 shutdown(sockfd
, SHUT_RDWR
);
1703 pth_cancel(accept_tid
);
1704 pth_join(accept_tid
, NULL
);
1708 unlink(*socketpath
);
1709 g_free(*socketpath
);
1711 MUTEX_LOCK(&cn_mutex
);
1712 n
= g_slist_length(cn_thread_list
);
1713 MUTEX_UNLOCK(&cn_mutex
);
1716 log_write(N_("waiting for all clients to disconnect"));
1719 if (n
!= n_clients
) {
1720 log_write(N_("%i clients remain"), n
);
1725 MUTEX_LOCK(&cn_mutex
);
1726 n
= g_slist_length(cn_thread_list
);
1727 MUTEX_UNLOCK(&cn_mutex
);
1730 startStopKeepAlive(TRUE
);
1731 pth_cancel(cache_timeout_tid
);
1736 * Called from pinentry_fork() in the child process.
1738 void free_client_list()
1740 gint i
, t
= g_slist_length(cn_thread_list
);
1742 for (i
= 0; i
< t
; i
++) {
1743 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1745 free_client(cn
->cl
);
1751 struct client_crypto_s
*init_client_crypto()
1753 struct client_crypto_s
*new = g_malloc0(sizeof(struct client_crypto_s
));
1757 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1761 rc
= gcry_cipher_open(&new->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
1764 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
1772 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1773 const gchar
*outfile
)
1778 struct client_crypto_s
*crypto
= init_client_crypto();
1781 return GPG_ERR_ENOMEM
;
1783 crypto
->key
= gcry_malloc(gcrykeysize
);
1786 cleanup_crypto(&crypto
);
1787 return GPG_ERR_ENOMEM
;
1790 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
1792 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
1797 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
1799 /* The header in version 1 had a bug where the iterations were off-by-one.
1800 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
1803 if (crypto
->fh
->fh1
.iter
>= 0) {
1805 crypto
->tkey
= parse_rcfile_keyfile(keyfile
, TRUE
, &rc
);
1810 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1811 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
1814 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
1822 rc
= try_xml_decrypt(NULL
, crypto
->key
, crypto
, &crypto
->fh
->doc
,
1828 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
1831 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1835 crypto
->fh
->v1
= FALSE
;
1836 iter
= crypto
->fh
->fh1
.iter
;
1837 memset(&crypto
->fh
->fh2
, 0, sizeof(crypto
->fh
->fh2
));
1838 /* Keep the iterations and key from the original file. */
1839 crypto
->fh
->fh2
.iter
= iter
+1; // Bugfix for v1 data files.
1840 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
1844 send_error(NULL
, rc
);
1846 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
1847 cleanup_crypto(&crypto
);
1852 static gboolean
startStopTcp(gboolean term
)
1854 struct addrinfo hints
, *servinfo
, *p
;
1855 gint port
= get_key_file_integer("global", "tcp_port");
1859 if (term
|| get_key_file_boolean("global", "enable_tcp") == FALSE
) {
1860 if (tcpSockFd
!= -1) {
1861 pth_cancel(tcpAcceptTid
);
1862 pth_join(tcpAcceptTid
, NULL
);
1863 shutdown(tcpSockFd
, SHUT_RDWR
);
1867 /* A client may still be connected. */
1875 if (tcpSockFd
!= -1)
1878 memset(&hints
, 0, sizeof(hints
));
1879 hints
.ai_family
= AF_UNSPEC
;
1880 hints
.ai_socktype
= SOCK_STREAM
;
1881 hints
.ai_flags
= AI_PASSIVE
;
1883 if ((n
= getaddrinfo(NULL
, print_fmt(buf
, sizeof(buf
), "%i", port
),
1884 &hints
, &servinfo
)) == -1) {
1885 log_write("getaddrinfo(): %s", gai_strerror(n
));
1889 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
1890 if ((tcpSockFd
= socket(p
->ai_family
, p
->ai_socktype
,
1891 p
->ai_protocol
)) == -1) {
1892 log_write("socket(): %s", strerror(errno
));
1898 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_REUSEADDR
, &n
,
1899 sizeof(int)) == -1) {
1900 log_write("setsockopt(): %s", strerror(errno
));
1901 freeaddrinfo(servinfo
);
1905 if (bind(tcpSockFd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
1907 log_write("bind(): %s", strerror(errno
));
1915 freeaddrinfo(servinfo
);
1918 log_write("%s", N_("could not bind"));
1922 if (g_key_file_has_key(keyfileh
, "global", "tcp_interface", NULL
)) {
1923 gchar
*tmp
= get_key_file_string("global", "tcp_interface");
1925 if (setsockopt(tcpSockFd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
, 1)
1927 log_write("setsockopt(): %s", strerror(errno
));
1935 if (!initTlsParams())
1938 if (listen(tcpSockFd
, 0) == -1) {
1939 log_write("listen(): %s", strerror(errno
));
1943 tcpAcceptTid
= pth_spawn(NULL
, tcp_accept_thread
, (void *)tcpSockFd
);
1944 pth_yield(tcpAcceptTid
);
1950 if (tcpSockFd
!= -1)
1958 int main(int argc
, char *argv
[])
1961 struct sockaddr_un addr
;
1962 struct passwd
*pw
= getpwuid(getuid());
1963 gchar buf
[PATH_MAX
];
1964 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1965 gchar
*socketarg
= NULL
;
1966 gchar
*datadir
= NULL
;
1969 gchar
**cache_push
= NULL
;
1971 gchar
*import
= NULL
, *keyfile
= NULL
;
1972 guint64 cmd_iterations
= -1;
1973 gint default_timeout
;
1974 gboolean rcfile_spec
= FALSE
;
1975 gint estatus
= EXIT_FAILURE
;
1977 gchar
*outfile
= NULL
;
1978 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1980 gboolean secure
= FALSE
;
1982 gint background
= 1;
1983 gchar
*convert
= NULL
;
1984 #ifdef WITH_PINENTRY
1985 gboolean disable_pinentry
= FALSE
;
1988 struct assuan_io_hooks io_hooks
= {read_hook
, write_hook
};
1992 #ifdef HAVE_SETRLIMIT
1995 rl
.rlim_cur
= rl
.rlim_max
= 0;
1997 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1998 err(EXIT_FAILURE
, "setrlimit()");
2004 setlocale(LC_ALL
, "");
2005 bindtextdomain("pwmd", LOCALEDIR
);
2014 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
2015 g_mem_set_vtable(&mtable
);
2016 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
2017 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
2020 gnutls_global_set_mem_functions(xmalloc
, xmalloc
, gcry_SecureCheck
,
2022 gnutls_global_init();
2023 gnutls_global_set_log_function(tls_log
);
2024 gnutls_global_set_log_level(1);
2025 assuan_set_io_hooks(&io_hooks
);
2030 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
2032 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2033 err(EXIT_FAILURE
, "%s", buf
);
2035 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
2037 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
2038 err(EXIT_FAILURE
, "%s", buf
);
2040 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
2046 while ((opt
= getopt(argc
, argv
, "Po:C:nI:i:k:hvf:D")) != EOF
) {
2048 #ifdef WITH_PINENTRY
2050 disable_pinentry
= TRUE
;
2069 cmd_iterations
= strtol(optarg
, NULL
, 10);
2076 rcfile
= g_strdup(optarg
);
2080 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
,
2082 #ifdef WITH_PINENTRY
2115 pth_mutex_init(&cn_mutex
);
2116 pth_mutex_init(&cache_mutex
);
2117 pth_mutex_init(&rcfile_mutex
);
2118 #ifdef WITH_PINENTRY
2119 pth_mutex_init(&pin_mutex
);
2122 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
2125 #ifdef WITH_PINENTRY
2126 if (disable_pinentry
== TRUE
)
2127 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
2130 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
2131 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
2133 if (log_syslog
== TRUE
)
2134 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
2136 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
2137 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
2140 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
2141 log_write("setpriority(): %s", strerror(errno
));
2146 #ifdef HAVE_MLOCKALL
2147 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
2148 log_write("mlockall(): %s", strerror(errno
));
2157 opt
= convert_file(convert
, keyfile
, outfile
);
2158 g_key_file_free(keyfileh
);
2160 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
2167 if (cmd_iterations
== -1)
2168 cmd_iterations
= (guint64
)get_key_file_integer("global", "iterations");
2170 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
2171 g_key_file_free(keyfileh
);
2173 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
2176 g_key_file_set_list_separator(keyfileh
, ',');
2178 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2179 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2183 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
2185 socketarg
= g_strdup(buf
);
2190 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2191 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2193 datadir
= expand_homedir(p
);
2196 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2197 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2198 disable_list_and_dump
= n
;
2201 disable_list_and_dump
= secure
;
2203 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2204 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2206 default_timeout
= -1;
2208 setup_logging(keyfileh
);
2210 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2211 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2213 if (argc
!= optind
) {
2215 ptotal
= g_strv_length(cache_push
);
2217 for (; optind
< argc
; optind
++) {
2218 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2219 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2223 if (strchr(socketarg
, '/') == NULL
) {
2224 socketdir
= g_get_current_dir();
2225 socketname
= g_strdup(socketarg
);
2226 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2229 socketname
= g_strdup(strrchr(socketarg
, '/'));
2231 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2232 socketdir
= g_strdup(socketarg
);
2233 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2236 if (chdir(datadir
)) {
2237 log_write("%s: %s", datadir
, strerror(errno
));
2242 if (parse_rcfile_keys() == FALSE
)
2245 clear_rcfile_keys();
2248 * Set the cache entry for a file. Prompts for the password.
2251 for (opt
= 0; cache_push
[opt
]; opt
++)
2252 do_cache_push(cache_push
[opt
], NULL
);
2254 g_strfreev(cache_push
);
2255 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2259 * bind() doesn't like the full pathname of the socket or any non alphanum
2260 * characters so change to the directory where the socket is wanted then
2261 * create it then change to datadir.
2263 if (chdir(socketdir
)) {
2264 log_write("%s: %s", socketdir
, strerror(errno
));
2270 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2271 log_write("socket(): %s", strerror(errno
));
2275 addr
.sun_family
= AF_UNIX
;
2276 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2278 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2279 log_write("bind(): %s", strerror(errno
));
2281 if (errno
== EADDRINUSE
)
2282 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2283 "stale socket. Please remove it manually."), socketpath
);
2289 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2290 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2291 mode_t mode
= strtol(t
, NULL
, 8);
2292 mode_t mask
= umask(0);
2296 if (chmod(socketname
, mode
) == -1) {
2297 log_write("%s: %s", socketname
, strerror(errno
));
2307 g_free(--socketname
);
2309 if (chdir(datadir
)) {
2310 log_write("%s: %s", datadir
, strerror(errno
));
2318 if (listen(sockfd
, 0) == -1) {
2319 log_write("listen(): %s", strerror(errno
));
2326 if (startStopTcp(FALSE
) == FALSE
)
2333 log_write("fork(): %s", strerror(errno
));
2346 server_loop(sockfd
, &socketpath
);
2347 estatus
= EXIT_SUCCESS
;
2350 if (socketpath
&& do_unlink
) {
2357 gnutls_global_deinit();
2360 g_key_file_free(keyfileh
);
2363 xmlCleanupGlobals();
2365 if (estatus
== EXIT_SUCCESS
)
2366 log_write(N_("pwmd exiting normally"));
2368 #if defined(DEBUG) && !defined(MEM_DEBUG)