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>
48 #include <sys/resource.h>
67 #include "pwmd_error.h"
72 GCRY_THREAD_OPTION_PTH_IMPL
;
74 static void clear_keyfile_key()
80 groups
= g_key_file_get_groups(keyfileh
, &n
);
82 for (p
= groups
; *p
; p
++) {
85 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
)
86 g_key_file_set_string(keyfileh
, *p
, "key", "");
92 static void reload_rcfile()
94 gboolean b
= disable_list_and_dump
;
97 log_write(N_("reloading configuration file '%s'"), rcfile
);
103 g_key_file_free(keyfileh
);
106 disable_list_and_dump
= b
;
107 send_status_all(STATUS_CONFIG
);
110 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
112 gpg_error_t n
= gpg_error_from_errno(e
);
114 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, _gpg_strerror(n
)));
117 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
119 gpg_err_code_t n
= gpg_err_code(e
);
120 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
121 struct client_s
*client
= assuan_get_pointer(ctx
);
124 return assuan_process_done(ctx
, 0);
127 log_write("%s\n", pwmd_strerror(e
));
131 if (n
== EPWMD_LIBXML_ERROR
) {
132 xmlErrorPtr xe
= client
->xml_error
;
135 xe
= xmlGetLastError();
137 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
138 log_write("%s", xe
->message
);
140 if (xe
== client
->xml_error
)
145 client
->xml_error
= NULL
;
149 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
152 void log_write(const gchar
*fmt
, ...)
162 pth_t tid
= pth_self();
163 gchar tid_str
[12], *p
;
165 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
169 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
177 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
185 attr
= pth_attr_of(tid
);
187 if (pth_attr_get(attr
, PTH_ATTR_NAME
, &name
) == FALSE
)
190 pth_attr_destroy(attr
);
191 g_snprintf(tid_str
, sizeof(tid_str
), "(%p)", tid
);
194 if (strncmp(p
+1, name
, 9) == 0)
197 if (log_syslog
== TRUE
)
198 syslog(LOG_INFO
, "%s%s: %s", name
, p
? p
: "", args
);
202 tm
= localtime(&now
);
203 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
204 tbuf
[sizeof(tbuf
) - 1] = 0;
206 if (args
[strlen(args
)-1] == '\n')
207 args
[strlen(args
)-1] = 0;
209 line
= g_strdup_printf("%s %i %s%s: %s\n", tbuf
, getpid(), name
,
221 write(fd
, line
, strlen(line
));
226 if (isatty(STDERR_FILENO
)) {
227 fprintf(stderr
, "%s", line
);
234 static void usage(gchar
*pn
)
237 "Usage: %s [-hvDn] [-f <rcfile>] [-I <filename> [-i <iter>] [-k <keyfile>]]\n [file1] [...]\n"
238 " -n run as a foreground process\n"
239 " -f load the specified rcfile (~/.pwmd/config)\n"
240 " -I import an XML file and write the encrypted data to stdout\n"
241 " -i encrypt with the specified number of iterations when importing\n"
242 " (config default in the \"global\" section)\n"
243 " -k when importing, obtain the key from the specified file\n"
244 " -D disable use of the LIST and DUMP commands\n"
246 " -h this help text\n"
251 static int gcry_SecureCheck(const void *ptr
)
256 static void setup_gcrypt()
258 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pth
);
259 gcry_check_version(NULL
);
261 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
264 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
266 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
268 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
269 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
272 static assuan_context_t
new_connection(gint fd
)
275 gchar ver
[ASSUAN_LINELENGTH
];
276 assuan_context_t ctx
;
278 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
283 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
284 assuan_set_hello_line(ctx
, ver
);
285 rc
= register_commands(ctx
);
290 rc
= assuan_accept(ctx
);
298 log_write("%s", _gpg_strerror(rc
));
302 gpg_error_t
send_status(assuan_context_t ctx
, status_msg_t which
)
304 const gchar
*line
= NULL
;
305 struct client_s
*client
= assuan_get_pointer(ctx
);
306 gchar buf
[ASSUAN_LINELENGTH
];
307 gchar
*status
= NULL
;
311 CACHE_LOCK(client
->ctx
);
312 line
= print_fmt(buf
, sizeof(buf
), "%i %i",
314 (cache_size
% sizeof(file_cache_t
)) ?
315 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count()-1 :
316 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count());
321 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
322 line
= print_fmt(buf
, sizeof(buf
), "%i", g_slist_length(cn_thread_list
));
323 pth_mutex_release(&cn_mutex
);
329 case STATUS_KEEPALIVE
:
330 status
= "KEEPALIVE";
334 line
= N_("Waiting for lock");
338 return assuan_write_status(ctx
, status
, line
);
341 void send_status_all(status_msg_t which
)
345 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
347 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
348 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
349 pth_msgport_t m
= pth_msgport_find(cn
->msg_name
);
352 pth_message_t
*msg
= g_malloc0(sizeof(pth_message_t
));
354 msg
->m_data
= (status_msg_t
*)which
;
355 pth_msgport_put(m
, msg
);
359 pth_mutex_release(&cn_mutex
);
362 static void xml_error_cb(void *data
, xmlErrorPtr e
)
364 struct client_s
*client
= data
;
367 * Keep the first reported error as the one to show in the error
368 * description. Reset in send_error().
370 if (client
->xml_error
)
373 xmlCopyError(e
, client
->xml_error
);
377 * This is called after a child_thread terminates. Set with
378 * pth_cleanup_push().
380 static void cleanup_cb(void *arg
)
382 struct client_thread_s
*cn
= arg
;
384 struct client_s
*cl
= cn
->cl
;
386 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
387 log_write(N_("exiting, fd=%i"), cn
->fd
);
390 pth_cancel(cn
->msg_tid
);
391 pth_join(cn
->msg_tid
, &value
);
392 pth_event_free(cn
->msg_ev
, PTH_FREE_THIS
);
396 g_free(cn
->msg_name
);
399 while (pth_msgport_pending(cn
->msg
) > 0) {
400 pth_message_t
*m
= pth_msgport_get(cn
->msg
);
405 pth_msgport_destroy(cn
->msg
);
408 pth_join(cn
->tid
, &value
);
410 if (cl
&& cl
->freed
== FALSE
)
411 cleanup_assuan(cl
->ctx
);
414 assuan_deinit_server(cl
->ctx
);
417 if (cl
&& cl
->pinentry
)
418 cleanup_pinentry(cl
->pinentry
);
422 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
424 pth_mutex_release(&cn_mutex
);
425 send_status_all(STATUS_CLIENTS
);
428 static void *client_msg_thread(void *data
)
430 struct client_s
*cl
= data
;
431 pth_status_t ev_status
;
434 pth_wait(cl
->thd
->msg_ev
);
435 ev_status
= pth_event_status(cl
->thd
->msg_ev
);
437 if (ev_status
== PTH_STATUS_FAILED
) {
438 log_write("%s(%i): %s: PTH_STATUS_FAILED", __FILE__
, __LINE__
,
443 if (ev_status
== PTH_STATUS_OCCURRED
) {
444 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
446 while (pth_msgport_pending(cl
->thd
->msg
) > 0) {
447 pth_message_t
*m
= pth_msgport_get(cl
->thd
->msg
);
448 status_msg_t n
= (status_msg_t
)m
->m_data
;
449 gpg_error_t rc
= send_status(cl
->ctx
, n
);
454 pth_mutex_release(&cn_mutex
);
455 log_write("%s", _gpg_strerror(rc
));
460 pth_mutex_release(&cn_mutex
);
466 * Called every time a connection is made via pth_spawn(). This is the thread
469 static void *client_thread(void *data
)
471 struct client_thread_s
*thd
= data
;
474 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
479 * Prevent a race condition with accept_thread() if this thread fails
480 * (returns) for some reason before accept_thread() releases the cn_mutex.
482 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
483 pth_mutex_release(&cn_mutex
);
486 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
491 cl
->pinentry
= pinentry_init();
495 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
502 pth_cleanup_push(cleanup_cb
, thd
);
503 thd
->msg_name
= g_strdup_printf("%p", thd
->tid
);
505 if (!thd
->msg_name
) {
506 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
510 thd
->msg
= pth_msgport_create(thd
->msg_name
);
513 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
517 thd
->msg_ev
= pth_event(PTH_EVENT_MSG
, thd
->msg
);
518 attr
= pth_attr_new();
519 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
520 thd
->msg_tid
= pth_spawn(attr
, client_msg_thread
, cl
);
521 pth_attr_destroy(attr
);
524 log_write(N_("pth_spawn() failed"));
529 * This is a "child" thread. Don't catch any signals. Let the master
530 * thread take care of signals in accept_thread().
532 cl
->ctx
= new_connection(fd
);
538 assuan_set_pointer(cl
->ctx
, cl
);
541 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
542 log_write("mlockall(): %s", strerror(errno
));
547 rc
= send_status(cl
->ctx
, STATUS_CACHE
);
550 log_write("%s", _gpg_strerror(rc
));
554 send_status_all(STATUS_CLIENTS
);
555 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->fd
);
558 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
561 pth_status_t ev_status
;
564 ev_status
= pth_event_status(ev
);
566 if (ev_status
== PTH_STATUS_FAILED
) {
567 log_write("%s(%i): %s: PTH_STATUS_FAILED", __FILE__
, __LINE__
,
571 else if (ev_status
== PTH_STATUS_OCCURRED
) {
572 rc
= assuan_process_next(cl
->ctx
);
575 cl
->inquire_status
= INQUIRE_INIT
;
577 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
580 log_write("assuan_process_next(): %s", _gpg_strerror(rc
));
581 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
584 log_write("assuan_process_done(): %s", _gpg_strerror(rc
));
590 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
) {
591 cl
->pinentry
->ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->pinentry
->fd
);
592 pth_event_concat(ev
, cl
->pinentry
->ev
, NULL
);
593 cl
->pinentry
->status
= PINENTRY_RUNNING
;
597 switch (cl
->inquire_status
) {
602 cl
->inquire_status
= INQUIRE_INIT
;
603 rc
= assuan_process_done(cl
->ctx
, 0);
610 ev
= pinentry_iterate(cl
, ev
);
615 * Client cleanup (including XML data) is done in cleanup_cb() from
616 * the cleanup thread.
619 pth_event_free(ev
, PTH_FREE_ALL
);
626 static void setup_logging(GKeyFile
*kf
)
628 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
631 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
637 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
643 logfile
= g_strdup(buf
);
653 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
657 * Make sure all settings are set to either the specified setting or a
660 static void set_rcfile_defaults(GKeyFile
*kf
)
664 if (g_key_file_has_key(kf
, "global", "socket_path", NULL
) == FALSE
) {
665 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
666 g_key_file_set_string(kf
, "global", "socket_path", buf
);
669 if (g_key_file_has_key(kf
, "global", "data_directory", NULL
) == FALSE
) {
670 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
671 g_key_file_set_string(kf
, "global", "data_directory", buf
);
674 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
675 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
677 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
678 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
679 g_key_file_set_string(kf
, "global", "log_path", buf
);
682 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
683 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
685 if (g_key_file_has_key(kf
, "global", "cache_size", NULL
) == FALSE
)
686 g_key_file_set_integer(kf
, "global", "cache_size", cache_size
);
689 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
690 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
693 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
694 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
696 if (g_key_file_has_key(kf
, "global", "iterations", NULL
) == FALSE
)
697 g_key_file_set_integer(kf
, "global", "iterations", 0);
699 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
700 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
702 if (g_key_file_has_key(kf
, "global", "iteration_progress", NULL
) == FALSE
)
703 g_key_file_set_integer(kf
, "global", "iteration_progress", 0);
705 if (g_key_file_has_key(kf
, "global", "compression_level", NULL
) == FALSE
)
706 g_key_file_set_integer(kf
, "global", "compression_level", 6);
708 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
709 g_key_file_set_integer(kf
, "global", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
711 if (g_key_file_has_key(kf
, "global", "zlib_bufsize", NULL
) == FALSE
)
712 g_key_file_set_integer(kf
, "global", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
714 zlib_bufsize
= g_key_file_get_integer(kf
, "global", "zlib_bufsize", NULL
);
716 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
717 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
720 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
723 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
724 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
726 if (g_key_file_has_key(kf
, "global", "keepalive", NULL
) == FALSE
)
727 g_key_file_set_integer(kf
, "global", "keepalive", 30);
730 if (g_key_file_has_key(kf
, "global", "enable_pinentry", NULL
) == FALSE
)
731 g_key_file_set_boolean(kf
, "global", "enable_pinentry", TRUE
);
733 if (g_key_file_has_key(kf
, "global", "pinentry_timeout", NULL
) == FALSE
)
734 g_key_file_set_integer(kf
, "global", "pinentry_timeout", 20);
740 static GKeyFile
*parse_rcfile(int cmdline
)
742 GKeyFile
*kf
= g_key_file_new();
745 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
746 log_write("%s: %s", rcfile
, rc
->message
);
752 if (rc
->code
== G_FILE_ERROR_NOENT
) {
753 set_rcfile_defaults(kf
);
760 set_rcfile_defaults(kf
);
764 static gchar
*get_password(const gchar
*prompt
)
766 gchar buf
[LINE_MAX
] = {0}, *p
;
767 struct termios told
, tnew
;
770 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
771 err(EXIT_FAILURE
, "tcgetattr()");
773 memcpy(&tnew
, &told
, sizeof(struct termios
));
774 tnew
.c_lflag
&= ~(ECHO
);
775 tnew
.c_lflag
|= ICANON
|ECHONL
;
777 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
778 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
779 err(EXIT_FAILURE
, "tcsetattr()");
782 fprintf(stderr
, "%s", prompt
);
784 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
785 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
789 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
790 p
[strlen(p
) - 1] = 0;
793 key
= gcry_malloc(1);
797 key
= gcry_malloc(strlen(p
) + 1);
798 sprintf(key
, "%s", p
);
801 memset(&buf
, 0, sizeof(buf
));
805 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
812 if ((fd
= open_file(filename
, &st
)) == -1) {
813 warn("%s", filename
);
817 if (st
.st_size
== 0) {
818 warnx(N_("%s: Skipping empty file"), filename
);
823 rc
= try_xml_decrypt(NULL
, fd
, st
, key
, &iter
);
825 return rc
? FALSE
: TRUE
;
828 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
834 prompt
= g_strdup_printf(N_("Passphrase for '%s': "), filename
);
837 if ((password
= get_password(prompt
)) == NULL
) {
838 warnx(N_("%s: Skipping file"), filename
);
843 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
) ? strlen(password
) : 1);
846 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
848 warnx(N_("%s: Invalid passphrase, skipping"), filename
);
853 warnx(N_("%s: Invalid passphrase"), filename
);
862 static gboolean
_getline(const gchar
*file
, gchar
**result
)
865 gchar buf
[LINE_MAX
] = {0}, *p
;
869 if ((fp
= fopen(file
, "r")) == NULL
) {
874 p
= fgets(buf
, sizeof(buf
), fp
);
878 if (len
&& buf
[len
- 1] == '\n')
881 str
= gcry_malloc(len
+ 1);
882 memcpy(str
, buf
, len
? len
: 1);
884 memset(&buf
, 0, sizeof(buf
));
889 static gchar
*parse_keyfile(const gchar
*filename
, gboolean import
)
895 if (import
== FALSE
&&
896 g_key_file_has_key(keyfileh
, filename
, "key_file", &rc
) == TRUE
) {
897 file
= g_key_file_get_string(keyfileh
, filename
, "key_file", &rc
);
901 warnx("%s", rc
->message
);
908 t
= expand_homedir(file
);
912 if (_getline(file
, &key
) == FALSE
) {
919 else if (import
== TRUE
) {
920 t
= g_strdup(filename
);
921 file
= expand_homedir(t
);
924 if (_getline(file
, &key
) == FALSE
) {
933 warnx("%s", rc
->message
);
940 static gboolean
xml_import(const gchar
*filename
, const char *keyfile
,
951 guchar shakey
[gcrykeysize
];
959 if (stat(filename
, &st
) == -1) {
960 warn("%s", filename
);
964 if ((rc
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
965 send_error(NULL
, rc
);
966 gcry_cipher_close(gh
);
967 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
975 if ((key
= get_password(N_("New passphrase: "))) == NULL
) {
976 fprintf(stderr
, "%s\n", N_("Invalid passphrase"));
977 gcry_cipher_close(gh
);
981 if ((key2
= get_password(N_("Verify passphrase: "))) == NULL
) {
982 fprintf(stderr
, "%s\n", N_("Passphrases do not match"));
984 gcry_cipher_close(gh
);
988 if (g_utf8_collate(key
, key2
) != 0) {
989 fprintf(stderr
, "%s\n", N_("Passphrases do not match"));
992 gcry_cipher_close(gh
);
999 key
= parse_keyfile(keyfile
, TRUE
);
1002 gcry_cipher_close(gh
);
1008 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
1010 warn("%s", filename
);
1011 gcry_cipher_close(gh
);
1015 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
1018 log_write("%s", strerror(ENOMEM
));
1019 gcry_cipher_close(gh
);
1023 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
1027 gcry_cipher_close(gh
);
1029 err(EXIT_FAILURE
, "read()");
1033 xmlbuf
[st
.st_size
] = 0;
1036 * Make sure the document validates.
1038 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
1039 log_write("xmlReadDoc()");
1043 gcry_cipher_close(gh
);
1048 xmlDocDumpMemory(doc
, &xml
, &len
);
1051 level
= get_key_file_integer(filename
, "compression_level");
1056 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
1057 memset(shakey
, 0, sizeof(shakey
));
1060 if (zrc
== Z_MEM_ERROR
)
1061 warnx("%s", strerror(ENOMEM
));
1063 warnx("do_compress() failed");
1065 gcry_cipher_close(gh
);
1075 memset(shakey
, '!', sizeof(shakey
));
1077 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
) ? strlen(key
) : 1);
1081 rc
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
1082 gcry_cipher_close(gh
);
1085 memset(shakey
, 0, sizeof(shakey
));
1086 warnx("%s", _gpg_strerror(rc
));
1090 memset(shakey
, 0, sizeof(shakey
));
1094 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
1099 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1100 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
1103 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1104 g_clear_error(&grc
);
1108 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1109 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
1112 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1113 g_clear_error(&grc
);
1121 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
1126 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1127 val
= g_key_file_get_integer(keyfileh
, section
, what
, &grc
);
1130 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1131 g_clear_error(&grc
);
1135 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1136 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
1139 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1140 g_clear_error(&grc
);
1148 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
1150 gboolean val
= FALSE
;
1153 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
1154 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &grc
);
1157 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1158 g_clear_error(&grc
);
1162 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
1163 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
1166 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
1167 g_clear_error(&grc
);
1175 static gboolean
parse_keyfile_key()
1182 groups
= g_key_file_get_groups(keyfileh
, &n
);
1184 for (p
= groups
; *p
; p
++) {
1187 if (g_key_file_has_key(keyfileh
, *p
, "key", &rc
) == TRUE
) {
1188 str
= g_key_file_get_string(keyfileh
, *p
, "key", &rc
);
1192 warnx("%s", rc
->message
);
1199 do_cache_push(*p
, str
);
1205 warnx("%s", rc
->message
);
1210 str
= parse_keyfile(*p
, FALSE
);
1213 do_cache_push(*p
, str
);
1222 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
1227 const gchar
*p
= filename
;
1228 file_header_t file_header
;
1237 if (valid_filename(p
) == FALSE
) {
1238 warnx(N_("%s: invalid characters in filename"), p
);
1242 md5file
= gcry_malloc(16);
1243 key
= gcry_malloc(gcrykeysize
);
1244 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1246 if (cache_iscached(md5file
) == TRUE
) {
1247 warnx(N_("%s: file already cached, skipping"), p
);
1253 if (access(p
, R_OK
|W_OK
) != 0) {
1257 if (errno
!= ENOENT
) {
1266 rc
= read_file_header(filename
, &file_header
);
1271 warnx("%s", pwmd_strerror(rc
));
1275 if (file_header
.iter
== -1) {
1276 memset(key
, '!', gcrykeysize
);
1281 #ifdef WITH_PINENTRY
1282 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
) == FALSE
) {
1284 if (get_input(p
, key
) == FALSE
) {
1289 #ifdef WITH_PINENTRY
1292 gchar
*result
= NULL
;
1293 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
1296 set_pinentry_defaults(pin
);
1297 pin
->which
= PINENTRY_OPEN
;
1298 pin
->filename
= g_strdup(filename
);
1300 rc
= pinentry_getpin(pin
, &result
);
1303 warnx("%s: %s", filename
, _gpg_strerror(rc
));
1304 cleanup_pinentry(pin
);
1311 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, strlen(result
) ? strlen(result
) : 1);
1314 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
1316 cleanup_pinentry(pin
);
1319 warnx(N_("%s: Invalid passphrase, skipping"), filename
);
1324 pin
->title
= g_strdup(N_("Incorrect passphrase, try again"));
1329 cleanup_pinentry(pin
);
1334 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
) ? strlen(password
) : 1);
1337 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
1338 warnx(N_("%s: Invalid passphrase, skipping"), filename
);
1345 if (cache_add_file(md5file
, key
) == FALSE
) {
1346 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1352 timeout
= get_key_file_integer(p
, "cache_timeout");
1353 cache_set_timeout(md5file
, timeout
);
1354 warnx(N_("%s: file added to the cache"), filename
);
1360 static void *accept_thread(void *arg
)
1362 gint sockfd
= (gint
)arg
;
1365 socklen_t slen
= sizeof(struct sockaddr_un
);
1366 struct sockaddr_un raddr
;
1370 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1371 if (errno
!= EAGAIN
) {
1372 if (!quit
) // probably EBADF
1373 log_write("accept(): %s", strerror(errno
));
1381 struct client_thread_s
*new;
1384 new = g_malloc0(sizeof(struct client_thread_s
));
1387 log_write("%s", strerror(ENOMEM
));
1392 * Thread priority is inherited from the calling thread. This
1393 * thread is PTH_PRIO_MAX. Keep the "child" threads at standard
1397 attr
= pth_attr_new();
1398 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
1399 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1400 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1401 tid
= pth_spawn(attr
, client_thread
, new);
1402 pth_attr_destroy(attr
);
1406 log_write(N_("pth_spawn() failed"));
1407 pth_mutex_release(&cn_mutex
);
1411 g_snprintf(buf
, sizeof(buf
), "%p", tid
);
1412 log_write(N_("new tid=%s, fd=%i"), buf
, fd
);
1413 attr
= pth_attr_of(tid
);
1414 pth_attr_set(attr
, PTH_ATTR_NAME
, buf
);
1415 pth_attr_destroy(attr
);
1417 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1418 pth_mutex_release(&cn_mutex
);
1422 /* Just in case pth_accept() failed for some reason other than EBADF */
1424 pth_exit(PTH_CANCELED
);
1429 * This thread isn't joinable. For operations that block, these threads will
1432 static void *adjust_timer_thread(void *arg
)
1435 cache_adjust_timer();
1440 static pth_event_t
timeout_event_iterate(pth_event_t timeout_ev
,
1443 pth_status_t ev_status
;
1446 pth_event_isolate(timeout_ev
);
1447 ev_status
= pth_event_status(timeout_ev
);
1448 pth_event_free(timeout_ev
, PTH_FREE_THIS
);
1450 if (ev_status
== PTH_STATUS_OCCURRED
) {
1452 * The timer event has expired. Update the file cache. When the
1453 * cache mutex is locked and the timer expires again, the threads
1456 pth_spawn(attr
, adjust_timer_thread
, NULL
);
1460 return pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1463 static pth_event_t
keepalive_event_iterate(pth_event_t keepalive_ev
,
1466 pth_event_t ev
= NULL
;
1467 pth_status_t ev_status
;
1470 pth_event_isolate(keepalive_ev
);
1471 ev_status
= pth_event_status(keepalive_ev
);
1473 if (ev_status
== PTH_STATUS_OCCURRED
|| ev_status
== PTH_STATUS_FAILED
) {
1474 if (ev_status
== PTH_STATUS_OCCURRED
)
1475 send_status_all(STATUS_KEEPALIVE
);
1477 pth_event_free(keepalive_ev
, PTH_FREE_THIS
);
1483 if (keepalive
> 0 && !ev
)
1484 ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(keepalive
, 0));
1489 static void server_loop(gint sockfd
, gchar
**socketpath
)
1496 pth_event_t timeout_ev
, keepalive_ev
= NULL
, ev_quit
;
1497 gint keepalive
= get_key_file_integer("global", "keepalive");
1500 pth_mutex_init(&cn_mutex
);
1501 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1504 sigaddset(&set
, SIGTERM
);
1505 sigaddset(&set
, SIGINT
);
1506 sigaddset(&set
, SIGUSR1
);
1507 sigaddset(&set
, SIGHUP
);
1508 sigaddset(&set
, SIGABRT
);
1510 attr
= pth_attr_new();
1511 pth_attr_init(attr
);
1512 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MIN
);
1513 pth_attr_set(attr
, PTH_ATTR_NAME
, "accept");
1514 accept_tid
= pth_spawn(attr
, accept_thread
, (void *)sockfd
);
1515 pth_attr_init(attr
);
1516 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1517 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1520 * For the cache_timeout configuration parameter.
1522 timeout_ev
= timeout_event_iterate(NULL
, attr
);
1523 keepalive_ev
= keepalive_event_iterate(NULL
, keepalive
);
1524 timeout_ev
= pth_event_concat(timeout_ev
, keepalive_ev
, NULL
);
1529 pth_sigwait_ev(&set
, &sig
, timeout_ev
);
1530 timeout_ev
= timeout_event_iterate(timeout_ev
, attr
);
1531 keepalive_ev
= keepalive_event_iterate(keepalive_ev
, keepalive
);
1532 timeout_ev
= pth_event_concat(timeout_ev
, keepalive_ev
, NULL
);
1535 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1537 /* Caught a signal. */
1541 keepalive
= get_key_file_integer("global", "keepalive");
1545 cache_clear(NULL
, 2);
1551 log_write(N_("clearing file cache"));
1552 cache_clear(NULL
, 2);
1557 shutdown(sockfd
, SHUT_RDWR
);
1565 * We're out of the main server loop. This happens when a signal was sent
1566 * to terminate the daemon. We'll wait for all clients to disconnect
1567 * before exiting and ignore any following signals.
1569 pth_join(accept_tid
, &value
);
1570 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1571 unlink(*socketpath
);
1572 g_free(*socketpath
);
1576 log_write(N_("waiting for all threads to terminate"));
1578 ev_quit
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1581 if (n
!= n_clients
) {
1582 log_write(N_("%i threads remain"), n
-1);
1587 pth_event_isolate(ev_quit
);
1588 timeout_ev
= timeout_event_iterate(timeout_ev
, attr
);
1589 keepalive_ev
= keepalive_event_iterate(keepalive_ev
, keepalive
);
1590 ev_quit
= pth_event_concat(ev_quit
, timeout_ev
, keepalive_ev
, NULL
);
1591 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1594 pth_event_free(timeout_ev
, PTH_FREE_THIS
);
1595 pth_event_free(keepalive_ev
, PTH_FREE_THIS
);
1596 pth_event_free(ev_quit
, PTH_FREE_THIS
);
1597 pth_attr_destroy(attr
);
1601 * Called from pinentry_fork() in the child process.
1603 void free_client_list()
1605 gint i
, t
= g_slist_length(cn_thread_list
);
1607 for (i
= 0; i
< t
; i
++) {
1608 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1610 free_client(cn
->cl
);
1613 memset(key_cache
, 0, cache_size
);
1616 int main(int argc
, char *argv
[])
1619 struct sockaddr_un addr
;
1620 struct passwd
*pw
= getpwuid(getuid());
1621 gchar buf
[PATH_MAX
];
1622 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1623 gchar
*socketarg
= NULL
;
1624 gchar
*datadir
= NULL
;
1627 gchar
**cache_push
= NULL
;
1629 gchar
*import
= NULL
, *keyfile
= NULL
;
1630 gint cmd_iterations
= -2;
1631 gint default_timeout
;
1632 gint rcfile_spec
= 0;
1633 gint estatus
= EXIT_FAILURE
;
1635 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1637 gboolean secure
= FALSE
;
1639 gint background
= 1;
1642 #ifdef HAVE_SETRLIMIT
1645 rl
.rlim_cur
= rl
.rlim_max
= 0;
1647 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1648 err(EXIT_FAILURE
, "setrlimit()");
1653 setlocale(LC_ALL
, "");
1654 bindtextdomain("pwmd", LOCALEDIR
);
1661 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1662 g_mem_set_vtable(&mtable
);
1663 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1664 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1667 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
1669 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1670 err(EXIT_FAILURE
, "%s", buf
);
1672 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
1674 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1675 err(EXIT_FAILURE
, "%s", buf
);
1677 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1679 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1680 err(EXIT_FAILURE
, "sysconf()");
1682 cache_size
= page_size
;
1684 while ((opt
= getopt(argc
, argv
, "bnI:i:k:hvf:D")) != EOF
) {
1687 /* Compatibility for version < 1.11 */
1702 cmd_iterations
= atoi(optarg
);
1706 rcfile
= g_strdup(optarg
);
1710 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1718 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1721 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
1722 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
1724 if (log_syslog
== TRUE
)
1725 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
1727 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
1728 iter
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
1731 if (setpriority(PRIO_PROCESS
, 0, iter
) == -1) {
1732 warn("setpriority()");
1737 if (g_key_file_has_key(keyfileh
, "global", "iterations", NULL
) == TRUE
)
1738 iter
= g_key_file_get_integer(keyfileh
, "global", "iterations", NULL
);
1740 #ifdef HAVE_MLOCKALL
1741 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
1748 opt
= xml_import(import
, keyfile
, cmd_iterations
< -1 ? iter
: cmd_iterations
);
1749 g_key_file_free(keyfileh
);
1751 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1754 g_key_file_set_list_separator(keyfileh
, ',');
1756 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
1757 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1761 g_snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1763 socketarg
= g_strdup(buf
);
1768 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
1769 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1771 datadir
= expand_homedir(p
);
1774 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
1775 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
1776 disable_list_and_dump
= n
;
1779 disable_list_and_dump
= secure
;
1781 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
1782 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
1784 default_timeout
= -1;
1786 if (g_key_file_has_key(keyfileh
, "global", "cache_size", NULL
) == TRUE
) {
1787 cache_size
= g_key_file_get_integer(keyfileh
, "global", "cache_size", NULL
);
1789 if (cache_size
< page_size
|| cache_size
% page_size
)
1790 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1793 setup_logging(keyfileh
);
1795 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
1796 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
1798 if (argc
!= optind
) {
1800 ptotal
= g_strv_length(cache_push
);
1802 for (; optind
< argc
; optind
++) {
1803 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1804 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1808 if (strchr(socketarg
, '/') == NULL
) {
1809 socketdir
= g_get_current_dir();
1810 socketname
= g_strdup(socketarg
);
1811 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1814 socketname
= g_strdup(strrchr(socketarg
, '/'));
1816 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1817 socketdir
= g_strdup(socketarg
);
1818 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1821 if ((key_cache
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1822 #ifdef MMAP_ANONYMOUS
1823 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1825 MAP_PRIVATE
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1827 err(EXIT_FAILURE
, "mmap()");
1830 if (mlock(key_cache
, cache_size
) == -1)
1831 log_write("mlock(): %s", strerror(errno
));
1833 memset(key_cache
, 0, cache_size
);
1835 if (chdir(datadir
)) {
1836 warn("%s", datadir
);
1841 if (parse_keyfile_key() == FALSE
)
1844 clear_keyfile_key();
1847 * Set the cache entry for a file. Prompts for the password.
1850 for (opt
= 0; cache_push
[opt
]; opt
++)
1851 do_cache_push(cache_push
[opt
], NULL
);
1853 g_strfreev(cache_push
);
1854 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1858 * bind() doesn't like the full pathname of the socket or any non alphanum
1859 * characters so change to the directory where the socket is wanted then
1860 * create it then change to datadir.
1862 if (chdir(socketdir
)) {
1863 warn("%s", socketdir
);
1869 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1874 addr
.sun_family
= AF_UNIX
;
1875 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1877 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1880 if (errno
== EADDRINUSE
)
1881 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1882 "stale socket. Please remove it manually."), socketpath
);
1888 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
1889 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
1890 mode_t mode
= strtol(t
, NULL
, 8);
1891 mode_t mask
= umask(0);
1895 if (chmod(socketname
, mode
) == -1) {
1896 warn("%s", socketname
);
1906 g_free(--socketname
);
1908 if (chdir(datadir
)) {
1909 warn("%s", datadir
);
1916 pth_mutex_init(&cache_mutex
);
1917 #ifdef WITH_PINENTRY
1918 pth_mutex_init(&pin_mutex
);
1921 if (listen(sockfd
, 0) == -1) {
1943 * These are the signals that we use in threads. libpth can catch signals
1944 * itself so ignore them everywhere else. Note that using
1945 * signal(N, SIG_IGN) doesn't work like you might think.
1950 sigaddset(&set
, SIGTERM
);
1951 sigaddset(&set
, SIGINT
);
1953 /* Configuration file reloading. */
1954 sigaddset(&set
, SIGUSR1
);
1956 /* Clears the file cache. */
1957 sigaddset(&set
, SIGHUP
);
1959 /* Caught in client_thread(). Sends a cache status message. */
1960 sigaddset(&set
, SIGUSR2
);
1962 /* Ignored everywhere. When a client disconnects abnormally this signal
1963 * gets raised. It isn't needed though because client_thread() will check
1964 * for rcs even after the client disconnects. */
1965 signal(SIGPIPE
, SIG_IGN
);
1966 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
1967 server_loop(sockfd
, &socketpath
);
1968 estatus
= EXIT_SUCCESS
;
1971 if (socketpath
&& do_unlink
) {
1976 g_key_file_free(keyfileh
);
1981 cache_clear(NULL
, 2);
1982 memset(key_cache
, 0, cache_size
);
1985 if (key_cache
&& munmap(key_cache
, cache_size
) == -1)
1986 log_write("munmap(): %s", strerror(errno
));
1988 if (estatus
== EXIT_SUCCESS
)
1989 log_write(N_("pwmd exiting normally"));
1992 #if defined(DEBUG) && !defined(MEM_DEBUG)