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 02111-1307 USA
38 #include <glib/gprintf.h>
48 #include <sys/resource.h>
63 #include "pwmd_error.h"
67 static void clear_rcfile_key()
73 groups
= g_key_file_get_groups(keyfileh
, &n
);
75 for (p
= groups
; *p
; p
++) {
78 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
)
79 g_key_file_set_string(keyfileh
, *p
, "key", "");
85 static void reload_rcfile()
87 log_write(N_("reloading configuration file '%s'"), rcfile
);
88 g_key_file_free(keyfileh
);
89 keyfileh
= parse_rcfile(0);
93 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
95 gpg_error_t n
= gpg_error_from_errno(e
);
97 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, gpg_strerror(n
)));
100 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
102 gpg_err_code_t n
= gpg_err_code(e
);
103 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
106 return assuan_process_done(ctx
, 0);
109 log_write("%s\n", pwmd_strerror(e
));
113 if (n
== EPWMD_LIBXML_ERROR
) {
114 xmlErrorPtr xml_error
= xmlGetLastError();
115 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xml_error
->message
));
118 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
121 void log_write(const gchar
*fmt
, ...)
132 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
136 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
144 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
149 attr
= pth_attr_of(pth_self());
150 pth_attr_get(attr
, PTH_ATTR_NAME
, &name
);
152 if (log_syslog
== TRUE
)
153 syslog(LOG_INFO
, "%s: %s", name
, args
);
157 tm
= localtime(&now
);
158 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
159 tbuf
[sizeof(tbuf
) - 1] = 0;
160 line
= g_strdup_printf("%s %i %s: %s\n", tbuf
, getpid(), name
, args
);
171 write(fd
, line
, strlen(line
));
176 if (isatty(STDERR_FILENO
)) {
177 fprintf(stderr
, "%s", line
);
185 static void usage(gchar
*pn
)
188 "Usage: %s [-hvDb] [-f <rcfile>] [-I <filename>] [file1] [...]\n"
189 " -b run as a background process\n"
190 " -f load the specified rcfile (~/.pwmd/config)\n"
191 " -I import an XML file and write the encrypted data to stdout\n"
192 " -D disable use of the LIST and DUMP commands\n"
194 " -h this help text\n"
200 static int gcry_SecureCheck(const void *ptr
)
206 static void setup_gcrypt()
208 gcry_check_version(NULL
);
211 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
215 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
217 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
219 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
220 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
223 static assuan_context_t
new_connection(gint fd
)
226 gchar ver
[ASSUAN_LINELENGTH
];
227 assuan_context_t ctx
;
229 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
234 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
235 assuan_set_hello_line(ctx
, ver
);
236 assuan_register_post_cmd_notify(ctx
, command_finalize
);
237 rc
= register_commands(ctx
);
242 rc
= assuan_accept(ctx
);
250 assuan_deinit_server(ctx
);
251 log_write("%s", gpg_strerror(rc
));
255 void send_cache_status_all()
259 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
261 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
262 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
264 if (cn
->tid
== pth_self()) {
265 send_cache_status(cn
->cl
->ctx
);
269 pth_raise(cn
->tid
, SIGUSR2
);
272 pth_mutex_release(&cn_mutex
);
275 static void *keepalive_thread(void *arg
)
277 struct client_thread_s
*thd
= arg
;
282 gint k
= get_key_file_integer("global", "keepalive");
284 ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(k
<= 0 ? 1 : k
, 0));
286 pth_event_free(ev
, PTH_FREE_THIS
);
292 error
= assuan_write_status(thd
->cl
->ctx
, "KEEPALIVE", NULL
);
295 pth_raise(thd
->tid
, SIGQUIT
);
304 * Called every time a connection is made via pth_spawn(). This is the thread
307 static void *client_thread(void *data
)
309 struct client_thread_s
*thd
= data
;
313 pth_event_t pinentry_ev
= NULL
;
315 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
319 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
324 cl
->pinentry
= g_malloc0(sizeof(struct pinentry_s
));
328 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
332 set_pinentry_defaults(cl
->pinentry
);
338 * This is a "child" thread. Don't catch any signals. Let the master
339 * thread take care of signals in server_loop().
342 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
344 /* Sends a cache status message to the client. */
345 sigaddset(&set
, SIGUSR2
);
346 /* Terminates this thread. Raised from keepalive_thread(). */
347 sigaddset(&set
, SIGQUIT
);
349 cl
->ctx
= new_connection(fd
);
355 assuan_set_pointer(cl
->ctx
, cl
);
358 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
359 log_write("mlockall(): %s", strerror(errno
));
364 send_cache_status(cl
->ctx
);
365 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->fd
);
366 thd
->keepalive_tid
= pth_spawn(NULL
, keepalive_thread
, thd
);
372 pth_sigwait_ev(&set
, &sig
, ev
);
377 send_cache_status(cl
->ctx
);
386 pth_event_isolate(ev
);
388 if (pth_event_occurred(ev
)) {
389 rc
= assuan_process_next(cl
->ctx
);
392 cl
->inquire_status
= INQUIRE_INIT
;
394 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
397 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
398 rc
= assuan_process_done(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
401 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
407 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
) {
408 pinentry_ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->pinentry
->fd
);
409 pth_event_concat(ev
, pinentry_ev
, NULL
);
410 cl
->pinentry
->status
= PINENTRY_RUNNING
;
414 switch (cl
->inquire_status
) {
419 cl
->inquire_status
= INQUIRE_INIT
;
420 rc
= assuan_process_done(cl
->ctx
, 0);
427 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
428 pth_event_isolate(pinentry_ev
);
430 if (pth_event_occurred(pinentry_ev
)) {
431 guchar shakey
[gcrykeysize
];
433 gsize len
= pth_read(cl
->pinentry
->fd
, &pk
, sizeof(pk
));
436 pth_event_free(pinentry_ev
, PTH_FREE_THIS
);
438 cl
->pinentry
->status
= PINENTRY_NONE
;
440 if (len
== sizeof(pk
)) {
442 rc
= send_error(cl
->ctx
, pk
.error
);
444 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, pk
.key
,
445 strlen(pk
.key
) == 0 ? 1 : strlen(pk
.key
));
446 rc
= cl
->pinentry
->cb(cl
->ctx
, shakey
, FALSE
);
447 memset(shakey
, 0, sizeof(shakey
));
451 log_write("%s", strerror(errno
));
453 log_write("pth_read(): EOF");
455 log_write(N_("pth_read(): short byte count"));
457 memset(&pk
, 0, sizeof(pk
));
458 pth_waitpid(cl
->pinentry
->pid
, &status
, 0);
459 close(cl
->pinentry
->fd
);
460 cl
->pinentry
->fd
= -1;
461 cl
->pinentry
->pid
= 0;
462 unlock_file_mutex(cl
);
465 pth_event_concat(ev
, pinentry_ev
, NULL
);
471 * Client cleanup (including XML data) is done in remove_connection() from
472 * the cleanup thread.
475 pth_event_free(ev
, PTH_FREE_ALL
);
478 log_write(N_("exiting, fd=%i"), thd
->fd
);
484 * Make sure all settings are set to either the specified setting or a
487 static void set_rcfile_defaults(GKeyFile
*kf
)
491 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
492 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
493 g_key_file_set_string(kf
, "global", "socket_path", buf
);
496 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
497 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
498 g_key_file_set_string(kf
, "global", "data_directory", buf
);
501 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
502 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
503 g_key_file_set_string(kf
, "global", "log_path", buf
);
506 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
507 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
509 if (g_key_file_has_key(kf
, "global", "cache_size", NULL
) == FALSE
)
510 g_key_file_set_integer(kf
, "global", "cache_size", cache_size
);
513 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
514 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
517 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
518 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
520 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
)
521 g_key_file_set_integer(kf
, "global", "iterations", 0);
523 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
524 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
526 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
527 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
529 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
530 g_key_file_set_integer(kf
, "global", "compression_level", 6);
532 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
533 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
535 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
536 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
538 zlib_bufsize
= g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
540 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
541 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
544 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
547 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
548 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
550 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
551 g_key_file_set_integer(kf
, "global", "keepalive", 5);
554 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
555 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
559 static GKeyFile
*parse_rcfile(int cmdline
)
561 GKeyFile
*kf
= g_key_file_new();
562 GError
*error
= NULL
;
564 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
565 log_write("%s: %s", rcfile
, error
->message
);
570 if (error
->code
== G_FILE_ERROR_NOENT
) {
571 g_clear_error(&error
);
572 set_rcfile_defaults(kf
);
576 g_clear_error(&error
);
580 set_rcfile_defaults(kf
);
585 static gchar
*get_password(const gchar
*prompt
)
587 gchar buf
[LINE_MAX
] = {0}, *p
;
588 struct termios told
, tnew
;
591 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
592 err(EXIT_FAILURE
, "tcgetattr()");
594 memcpy(&tnew
, &told
, sizeof(struct termios
));
595 tnew
.c_lflag
&= ~(ECHO
);
596 tnew
.c_lflag
|= ICANON
|ECHONL
;
598 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
599 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
600 err(EXIT_FAILURE
, "tcsetattr()");
603 fprintf(stderr
, "%s", prompt
);
605 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
606 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
610 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
611 p
[strlen(p
) - 1] = 0;
614 key
= gcry_malloc(1);
618 key
= gcry_malloc(strlen(p
) + 1);
619 sprintf(key
, "%s", p
);
622 memset(&buf
, 0, sizeof(buf
));
626 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
632 if ((fd
= open_file(filename
, &st
)) == -1) {
633 warn("%s", filename
);
637 if (st
.st_size
== 0) {
638 warnx(N_("%s: skipping empty file"), filename
);
643 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
645 return error
? FALSE
: TRUE
;
648 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
654 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
657 if ((password
= get_password(prompt
)) == NULL
) {
658 warnx(N_("%s: skipping file"), filename
);
663 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
) ? strlen(password
) : 1);
666 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
668 warnx(N_("%s: invalid password, skipping"), filename
);
673 warnx(N_("%s: invalid password"), filename
);
682 static gboolean
xml_import(const gchar
*filename
, gint iter
)
692 guchar shakey
[gcrykeysize
];
700 if (stat(filename
, &st
) == -1) {
701 warn("%s", filename
);
705 if ((error
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
706 send_error(NULL
, error
);
707 gcry_cipher_close(gh
);
708 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
715 if ((key
= get_password(N_("New password: "))) == NULL
) {
716 fprintf(stderr
, "%s\n", N_("Invalid password."));
717 gcry_cipher_close(gh
);
721 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
722 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
724 gcry_cipher_close(gh
);
728 if (g_utf8_collate(key
, key2
) != 0) {
729 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
732 gcry_cipher_close(gh
);
739 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
741 warn("%s", filename
);
742 gcry_cipher_close(gh
);
746 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
749 log_write("%s", strerror(ENOMEM
));
750 gcry_cipher_close(gh
);
754 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
758 gcry_cipher_close(gh
);
760 err(EXIT_FAILURE
, "read()");
764 xmlbuf
[st
.st_size
] = 0;
767 * Make sure the document validates.
769 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
770 log_write("xmlReadDoc()");
774 gcry_cipher_close(gh
);
779 xmlDocDumpMemory(doc
, &xml
, &len
);
782 level
= get_key_file_integer(filename
, "compression_level");
787 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
788 memset(shakey
, 0, sizeof(shakey
));
791 if (zerror
== Z_MEM_ERROR
)
792 warnx("%s", strerror(ENOMEM
));
794 warnx("do_compress() failed");
796 gcry_cipher_close(gh
);
806 memset(shakey
, '!', sizeof(shakey
));
808 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
) ? strlen(key
) : 1);
812 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
813 gcry_cipher_close(gh
);
816 memset(shakey
, 0, sizeof(shakey
));
817 warnx("%s", gpg_strerror(error
));
821 memset(shakey
, 0, sizeof(shakey
));
825 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
828 GError
*gerror
= NULL
;
830 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
831 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
834 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
835 g_clear_error(&gerror
);
839 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
840 val
= g_key_file_get_string(keyfileh
, "global", what
, &gerror
);
843 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
844 g_clear_error(&gerror
);
852 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
855 GError
*gerror
= NULL
;
857 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
858 val
= g_key_file_get_integer(keyfileh
, section
, what
, &gerror
);
861 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
862 g_clear_error(&gerror
);
866 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
867 val
= g_key_file_get_integer(keyfileh
, "global", what
, &gerror
);
870 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
871 g_clear_error(&gerror
);
879 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
881 gboolean val
= FALSE
;
882 GError
*gerror
= NULL
;
884 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
885 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &gerror
);
888 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
889 g_clear_error(&gerror
);
893 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
894 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &gerror
);
897 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
898 g_clear_error(&gerror
);
906 gchar
*expand_homedir(gchar
*str
)
911 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
913 return g_strdup(str
);
916 static gboolean
_getline(const gchar
*file
, gchar
**result
)
919 gchar buf
[LINE_MAX
] = {0}, *p
;
923 if ((fp
= fopen(file
, "r")) == NULL
) {
928 p
= fgets(buf
, sizeof(buf
), fp
);
932 if (len
&& buf
[len
- 1] == '\n')
935 str
= gcry_malloc(len
+ 1);
936 memcpy(str
, buf
, len
? len
: 1);
938 memset(&buf
, 0, sizeof(buf
));
943 static gboolean
parse_keyfile_key()
950 groups
= g_key_file_get_groups(keyfileh
, &n
);
952 for (p
= groups
; *p
; p
++) {
953 GError
*error
= NULL
;
955 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
956 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
960 warnx("%s", error
->message
);
961 g_clear_error(&error
);
967 do_cache_push(*p
, str
);
973 warnx("%s", error
->message
);
974 g_clear_error(&error
);
978 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
980 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
984 warnx("%s", error
->message
);
985 g_clear_error(&error
);
991 t
= expand_homedir(file
);
995 if (_getline(file
, &str
) == FALSE
) {
1001 do_cache_push(*p
, str
);
1007 warnx("%s", error
->message
);
1008 g_clear_error(&error
);
1016 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1021 const gchar
*p
= filename
;
1022 file_header_t file_header
;
1031 if (valid_filename(p
) == FALSE
) {
1032 warnx(N_("%s: invalid characters in filename"), p
);
1036 md5file
= gcry_malloc(16);
1037 key
= gcry_malloc(gcrykeysize
);
1038 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1040 if (cache_iscached(md5file
) == TRUE
) {
1041 warnx(N_("%s: file already cached, skipping"), p
);
1047 if (access(p
, R_OK
|W_OK
) != 0) {
1051 if (errno
!= ENOENT
) {
1060 error
= read_file_header(filename
, &file_header
);
1065 warnx("%s", pwmd_strerror(error
));
1069 if (file_header
.iter
== -1) {
1070 memset(key
, '!', gcrykeysize
);
1075 #ifdef WITH_PINENTRY
1076 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
) == FALSE
) {
1078 if (get_input(p
, key
) == FALSE
) {
1083 #ifdef WITH_PINENTRY
1086 gchar
*result
= NULL
;
1087 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
1091 pin
->which
= PINENTRY_OPEN
;
1092 pin
->filename
= g_strdup(filename
);
1094 rc
= pinentry_getpin(pin
, &result
);
1097 warnx("%s: %s", filename
, gpg_strerror(rc
));
1098 cleanup_pinentry(pin
);
1105 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
1108 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
1110 cleanup_pinentry(pin
);
1113 warnx(N_("%s: invalid password, skipping"), filename
);
1118 pin
->title
= g_strdup(N_("Incorrect password. Please try again."));
1123 cleanup_pinentry(pin
);
1128 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
) ? strlen(password
) : 1);
1131 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
1132 warnx(N_("%s: invalid password, skipping"), filename
);
1139 if (cache_add_file(md5file
, key
) == FALSE
) {
1140 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1146 timeout
= get_key_file_integer(p
, "cache_timeout");
1147 cache_set_timeout(md5file
, timeout
);
1148 warnx(N_("%s: file added to the cache"), filename
);
1154 static GSList
*remove_connection(GSList
*list
, struct client_thread_s
*cn
)
1157 struct client_s
*cl
= cn
->cl
;
1159 if (cn
->keepalive_tid
) {
1160 pth_cancel(cn
->keepalive_tid
);
1161 pth_join(cn
->keepalive_tid
, &value
);
1164 log_write("%p", cn
->tid
);
1165 pth_join(cn
->tid
, &value
);
1168 if (cl
->freed
== FALSE
)
1169 cleanup_assuan(cl
->ctx
);
1172 assuan_deinit_server(cl
->ctx
);
1174 #ifdef WITH_PINENTRY
1176 cleanup_pinentry(cl
->pinentry
);
1182 pth_event_isolate(cn
->ev
);
1183 pth_event_free(cn
->ev
, PTH_FREE_THIS
);
1184 list
= g_slist_remove(list
, cn
);
1190 * Can't pth_event_concat() to an empty event.
1192 static int event_ring_hack(void *data
)
1198 * See if any thread has entered the DEAD queue and remove it.
1200 static GSList
*cleanup_dead_queue(GSList
*threads
, pth_event_t tid_events
)
1204 for (n
= g_slist_length(threads
), i
= 0; i
< n
; i
++) {
1205 struct client_thread_s
*cn
= g_slist_nth_data(threads
, i
);
1207 if (pth_event_occurred(cn
->ev
)) {
1208 threads
= remove_connection(threads
, cn
);
1209 return cleanup_dead_queue(threads
, tid_events
);
1212 pth_event_concat(tid_events
, cn
->ev
, NULL
);
1218 static void *socket_thread(void *arg
)
1220 gint sockfd
= (gint
)arg
;
1223 socklen_t slen
= sizeof(struct sockaddr_un
);
1224 struct sockaddr_un raddr
;
1228 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1229 if (errno
!= EAGAIN
) {
1230 if (!quit
) // probably EBADF
1231 log_write("accept(): %s", strerror(errno
));
1239 struct client_thread_s
*new;
1242 new = g_malloc0(sizeof(struct client_thread_s
));
1245 log_write("%s", strerror(ENOMEM
));
1250 * Thread priority is inherited from the calling thread. This
1251 * thread is PTH_PRIO_MAX. Keep the "child" threads at standard
1254 attr
= pth_attr_new();
1255 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
1256 tid
= pth_spawn(attr
, client_thread
, new);
1257 pth_attr_destroy(attr
);
1261 log_write(N_("pth_spawn() failed"));
1265 snprintf(buf
, sizeof(buf
), "%p", tid
);
1266 attr
= pth_attr_of(tid
);
1267 pth_attr_set(attr
, PTH_ATTR_NAME
, buf
);
1268 log_write(N_("new tid=%s, fd=%i"), buf
, fd
);
1272 new->ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1273 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1274 pth_event_concat(cn_events
, new->ev
, NULL
);
1275 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1276 pth_mutex_release(&cn_mutex
);
1280 /* Just in case pth_accept() failed for some reason other than EBADF */
1282 pth_exit(PTH_CANCELED
);
1287 * There needs to be a pth_timeout() here so pth_wait() can wait for new
1288 * cn_events which is updated from cleanup_dead_queue().
1290 static void *cleanup_thread(void *arg
)
1292 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1295 if (pth_wait(cn_events
) && pth_event_occurred(cn_events
)) {
1296 pth_event_isolate(cn_events
);
1297 pth_event_free(cn_events
, PTH_FREE_THIS
);
1299 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1300 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1301 cn_thread_list
= cleanup_dead_queue(cn_thread_list
, cn_events
);
1302 pth_mutex_release(&cn_mutex
);
1306 pth_event_isolate(cn_events
);
1307 pth_event_free(cn_events
, PTH_FREE_THIS
);
1312 * This thread isn't joinable. For operations that block, these threads will
1315 static void *adjust_timer_thread(void *arg
)
1318 cache_adjust_timer();
1323 static void server_loop(int sockfd
)
1325 pth_t socket_tid
, cleanup_tid
;
1330 pth_event_t timeout_event
;
1333 pth_mutex_init(&cn_mutex
);
1334 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1337 sigaddset(&set
, SIGTERM
);
1338 sigaddset(&set
, SIGINT
);
1339 sigaddset(&set
, SIGUSR1
);
1340 sigaddset(&set
, SIGHUP
);
1341 sigaddset(&set
, SIGABRT
);
1343 attr
= pth_attr_new();
1344 pth_attr_init(attr
);
1345 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1346 pth_attr_set(attr
, PTH_ATTR_NAME
, "socket");
1347 socket_tid
= pth_spawn(attr
, socket_thread
, (void *)sockfd
);
1348 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1349 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
, PTH_CANCEL_ASYNCHRONOUS
);
1350 pth_attr_set(attr
, PTH_ATTR_NAME
, "cleanup");
1351 cleanup_tid
= pth_spawn(attr
, cleanup_thread
, NULL
);
1354 * For the cache_timeout configuration parameter. This replaces the old
1355 * SIGALRM stuff and is safer.
1357 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1358 pth_attr_init(attr
);
1359 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1360 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1365 pth_sigwait_ev(&set
, &sig
, timeout_event
);
1367 if (pth_event_occurred(timeout_event
)) {
1369 * The timer event has expired. Update the file cache. When the
1370 * cache mutex is locked and the timer expires again, the threads
1373 pth_spawn(attr
, adjust_timer_thread
, NULL
);
1374 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1375 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1379 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1381 /* Caught a signal. */
1388 cache_clear(NULL
, 2);
1396 log_write(N_("clearing file cache"));
1397 cache_clear(NULL
, 2);
1402 shutdown(sockfd
, SHUT_RDWR
);
1410 * We're out of the main server loop. This happens when a signal was sent
1411 * to terminate the daemon. We'll wait for all clients to disconnect
1412 * before exiting and ignore any following signals.
1414 pth_join(socket_tid
, &value
);
1415 pth_attr_destroy(attr
);
1416 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1418 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1420 /* 2 because the cleanup thread still exists, plus self. */
1422 log_write(N_("waiting for all threads to terminate"));
1428 if (n
!= n_clients
) {
1429 log_write(N_("%i threads remain"), n
-2);
1433 events
= pth_event(PTH_EVENT_FUNC
, event_ring_hack
, NULL
, pth_timeout(1, 0));
1434 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1436 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
1437 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1439 pth_event_concat(events
, cn
->ev
, NULL
);
1445 pth_mutex_release(&cn_mutex
);
1447 pth_yield(cleanup_tid
);
1449 pth_event_isolate(events
);
1450 pth_event_free(events
, PTH_FREE_THIS
);
1451 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1454 pth_cancel(cleanup_tid
);
1458 * Called from pinentry_fork() in the child process.
1460 void free_client_list()
1462 gint i
, t
= g_slist_length(cn_thread_list
);
1464 for (i
= 0; i
< t
; i
++) {
1465 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1467 free_client(cn
->cl
);
1470 memset(key_cache
, 0, cache_size
);
1473 int main(int argc
, char *argv
[])
1476 struct sockaddr_un addr
;
1477 struct passwd
*pw
= getpwuid(getuid());
1478 gchar buf
[PATH_MAX
];
1479 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1480 gchar
*socketarg
= NULL
;
1481 gchar
*datadir
= NULL
;
1484 gchar
**cache_push
= NULL
;
1486 gchar
*import
= NULL
;
1487 gint default_timeout
;
1488 gint rcfile_spec
= 0;
1489 gint estatus
= EXIT_FAILURE
;
1492 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1495 gboolean secure
= FALSE
;
1497 gint background
= 0;
1500 #ifdef HAVE_SETRLIMIT
1503 rl
.rlim_cur
= rl
.rlim_max
= 0;
1505 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1506 err(EXIT_FAILURE
, "setrlimit()");
1511 setlocale(LC_ALL
, "");
1512 bindtextdomain("pwmd", LOCALEDIR
);
1517 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1519 g_mem_set_vtable(&mtable
);
1520 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1521 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1525 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
1527 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1528 err(EXIT_FAILURE
, "%s", buf
);
1530 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
1532 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1533 err(EXIT_FAILURE
, "%s", buf
);
1535 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1537 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1538 err(EXIT_FAILURE
, "sysconf()");
1540 cache_size
= page_size
;
1542 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1555 rcfile
= g_strdup(optarg
);
1559 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1567 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1570 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
1571 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
1573 if (log_syslog
== TRUE
)
1574 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
1576 if (g_key_file_has_key(keyfileh
, "global", "iterations", NULL
) == TRUE
)
1577 iter
= g_key_file_get_integer(keyfileh
, "global", "iterations", NULL
);
1579 #ifdef HAVE_MLOCKALL
1580 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
1589 opt
= xml_import(import
, iter
);
1590 g_key_file_free(keyfileh
);
1592 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1595 g_key_file_set_list_separator(keyfileh
, ',');
1597 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
1598 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1602 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1604 socketarg
= g_strdup(buf
);
1609 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
1610 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1612 datadir
= expand_homedir(p
);
1615 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
1616 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
1617 disable_list_and_dump
= n
;
1620 disable_list_and_dump
= secure
;
1622 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
1623 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
1625 default_timeout
= -1;
1627 if (g_key_file_has_key(keyfileh
, "global", "cache_size", NULL
) == TRUE
) {
1628 cache_size
= g_key_file_get_integer(keyfileh
, "global", "cache_size", NULL
);
1630 if (cache_size
< page_size
|| cache_size
% page_size
)
1631 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1634 if (g_key_file_has_key(keyfileh
, "global", "log_path", NULL
) == TRUE
) {
1635 if (g_key_file_has_key(keyfileh
, "global", "enable_logging", NULL
) == TRUE
) {
1636 n
= g_key_file_get_boolean(keyfileh
, "global", "enable_logging", NULL
);
1639 p
= g_key_file_get_string(keyfileh
, "global", "log_path", NULL
);
1643 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1645 logfile
= g_strdup(buf
);
1653 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
1654 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
1656 if (argc
!= optind
) {
1658 ptotal
= g_strv_length(cache_push
);
1660 for (; optind
< argc
; optind
++) {
1661 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1662 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1666 if (strchr(socketarg
, '/') == NULL
) {
1667 socketdir
= g_get_current_dir();
1668 socketname
= g_strdup(socketarg
);
1669 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1672 socketname
= g_strdup(strrchr(socketarg
, '/'));
1674 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1675 socketdir
= g_strdup(socketarg
);
1676 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1679 if ((key_cache
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1680 #ifdef MMAP_ANONYMOUS
1681 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1683 MAP_PRIVATE
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1685 err(EXIT_FAILURE
, "mmap()");
1688 if (mlock(key_cache
, cache_size
) == -1)
1689 log_write("mlock(): %s", strerror(errno
));
1691 memset(key_cache
, 0, cache_size
);
1693 if (chdir(datadir
)) {
1694 warn("%s", datadir
);
1699 if (parse_keyfile_key() == FALSE
)
1705 * Set the cache entry for a file. Prompts for the password.
1708 for (opt
= 0; cache_push
[opt
]; opt
++)
1709 do_cache_push(cache_push
[opt
], NULL
);
1711 g_strfreev(cache_push
);
1712 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1716 * bind() doesn't like the full pathname of the socket or any non alphanum
1717 * characters so change to the directory where the socket is wanted then
1718 * create it then change to datadir.
1720 if (chdir(socketdir
)) {
1721 warn("%s", socketdir
);
1727 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1732 addr
.sun_family
= AF_UNIX
;
1733 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1735 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1738 if (errno
== EADDRINUSE
)
1739 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1740 "stale socket. Please remove it manually."), socketpath
);
1746 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
1747 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
1748 mode_t mode
= strtol(t
, NULL
, 8);
1749 mode_t mask
= umask(0);
1753 if (chmod(socketname
, mode
) == -1) {
1754 warn("%s", socketname
);
1764 g_free(--socketname
);
1766 if (chdir(datadir
)) {
1767 warn("%s", datadir
);
1774 pth_mutex_init(&cache_mutex
);
1776 if (listen(sockfd
, 0) == -1) {
1798 * These are the signals that we use in threads. libpth can catch signals
1799 * itself so ignore them everywhere else. Note that using
1800 * signal(N, SIG_IGN) doesn't work like you might think.
1805 sigaddset(&set
, SIGTERM
);
1806 sigaddset(&set
, SIGINT
);
1808 /* Configuration file reloading. */
1809 sigaddset(&set
, SIGUSR1
);
1811 /* Clears the file cache. */
1812 sigaddset(&set
, SIGHUP
);
1814 /* Caught in client_thread(). Sends a cache status message. */
1815 sigaddset(&set
, SIGUSR2
);
1817 /* Caught in client_thread(). When keepalive_thread() fails, this signal
1818 * is raised to terminate the client. More of a failsafe than anything. */
1819 sigaddset(&set
, SIGQUIT
);
1821 /* Ignored everywhere. When a client disconnects abnormally this signal
1822 * gets raised. It isn't needed though because client_thread() will check
1823 * for errors even after the client disconnects. */
1824 signal(SIGPIPE
, SIG_IGN
);
1825 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
1826 server_loop(sockfd
);
1827 estatus
= EXIT_SUCCESS
;
1830 if (socketpath
&& do_unlink
) {
1835 g_key_file_free(keyfileh
);
1839 cache_clear(NULL
, 2);
1840 memset(key_cache
, 0, cache_size
);
1843 if (key_cache
&& munmap(key_cache
, cache_size
) == -1)
1844 log_write("munmap(): %s", strerror(errno
));
1846 if (estatus
== EXIT_SUCCESS
)
1847 log_write(N_("pwmd exiting normally"));
1850 #if defined(DEBUG) && !defined(MEM_DEBUG)