1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2007 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
34 #include <glib/gprintf.h>
47 #include <sys/resource.h>
61 #include "pwmd_error.h"
65 static void clear_rcfile_key()
71 groups
= g_key_file_get_groups(keyfileh
, &n
);
73 for (p
= groups
; *p
; p
++) {
76 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
)
77 g_key_file_set_string(keyfileh
, *p
, "key", "");
83 static void reload_rcfile()
85 log_write(N_("reloading configuration file '%s'"), rcfile
);
86 g_key_file_free(keyfileh
);
87 keyfileh
= parse_rcfile(0);
91 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
93 gpg_error_t n
= gpg_error_from_errno(e
);
95 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, gpg_strerror(n
)));
98 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
100 gpg_err_code_t n
= gpg_err_code(e
);
101 gpg_error_t code
= gpg_err_make(GPG_ERR_SOURCE_USER_1
, n
);
104 return assuan_process_done(ctx
, 0);
107 log_write("%s\n", pwmd_strerror(e
));
111 if (n
== EPWMD_LIBXML_ERROR
) {
112 xmlErrorPtr xml_error
= xmlGetLastError();
113 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xml_error
->message
));
116 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
119 void log_write(const gchar
*fmt
, ...)
128 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
132 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
140 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
145 if (log_syslog
== TRUE
)
146 syslog(LOG_INFO
, "%s", args
);
150 tm
= localtime(&now
);
151 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
152 tbuf
[sizeof(tbuf
) - 1] = 0;
153 line
= g_strdup_printf("%s %i %s\n", tbuf
, getpid(), args
);
164 write(fd
, line
, strlen(line
));
169 if (isatty(STDERR_FILENO
)) {
170 fprintf(stderr
, "%s", line
);
178 static void usage(gchar
*pn
)
181 "Usage: %s [-hvDb] [-f <rcfile>] [-I <filename>] [file1] [...]\n"
182 " -b run as a background process\n"
183 " -f load the specified rcfile (~/.pwmd/config)\n"
184 " -I import an XML file and write the encrypted data to stdout\n"
185 " -D disable use of the LIST and DUMP commands\n"
187 " -h this help text\n"
193 static int gcry_SecureCheck(const void *ptr
)
199 static void setup_gcrypt()
201 gcry_check_version(NULL
);
204 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
208 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
210 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
212 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
213 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
216 static assuan_context_t
new_connection(gint fd
)
219 gchar ver
[ASSUAN_LINELENGTH
];
220 assuan_context_t ctx
;
222 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
227 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
228 assuan_set_hello_line(ctx
, ver
);
229 assuan_register_post_cmd_notify(ctx
, command_finalize
);
230 rc
= register_commands(ctx
);
235 rc
= assuan_accept(ctx
);
243 assuan_deinit_server(ctx
);
244 log_write("%s", gpg_strerror(rc
));
248 void send_cache_status_all()
252 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
253 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
255 if (cn
->tid
== pth_self()) {
256 send_cache_status(cn
->cl
->ctx
);
260 pth_raise(cn
->tid
, SIGUSR2
);
264 static void *keepalive_thread(void *arg
)
266 struct client_thread_s
*thd
= arg
;
267 gint k
= get_key_file_integer("default", "keepalive");
274 ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(k
, 0));
276 sigaddset(&set
, SIGALRM
);
282 pth_sigwait_ev(&set
, &sig
, ev
);
283 pth_event_free(ev
, PTH_FREE_THIS
);
284 ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(k
, 0));
289 error
= assuan_write_status(thd
->cl
->ctx
, "KEEPALIVE", NULL
);
292 pth_raise(thd
->tid
, SIGQUIT
);
297 pth_event_free(ev
, PTH_FREE_THIS
);
302 * Called every time a connection is made via pth_spawn(). This is the thread
305 static void *client_thread(void *data
)
307 struct client_thread_s
*thd
= data
;
310 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
314 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
321 * This is a "child" thread. Don't catch any signals. Let the master
322 * thread take care of signals in server_loop().
325 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
327 /* Sends a cache status message to the client. */
328 sigaddset(&set
, SIGUSR2
);
329 /* Terminates this thread. Raised from keepalive_thread(). */
330 sigaddset(&set
, SIGQUIT
);
332 cl
->ctx
= new_connection(fd
);
338 assuan_set_pointer(cl
->ctx
, cl
);
341 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
342 log_write("mlockall(): %s", strerror(errno
));
347 send_cache_status(cl
->ctx
);
348 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->fd
);
349 thd
->keepalive_tid
= pth_spawn(NULL
, keepalive_thread
, thd
);
355 pth_sigwait_ev(&set
, &sig
, ev
);
360 send_cache_status(cl
->ctx
);
369 if (pth_event_occurred(ev
)) {
370 rc
= assuan_process_next(cl
->ctx
);
373 cl
->inquire
= INQUIRE_NONE
;
375 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
378 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
379 rc
= assuan_process_done(cl
->ctx
, rc
);
382 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
387 /* Set from store_command_finalize(). */
388 if (cl
->inquire
== INQUIRE_OK
|| cl
->inquire_error
) {
389 cl
->inquire
= INQUIRE_NONE
;
390 rc
= assuan_process_done(cl
->ctx
, gpg_err_make(GPG_ERR_SOURCE_ANY
, cl
->inquire_error
));
391 cl
->inquire_error
= 0;
394 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
403 * Client cleanup (including XML data) is done in remove_connection() from
404 * the cleanup thread.
407 pth_event_free(ev
, PTH_FREE_ALL
);
415 * Make sure all settings are set to either the specified setting or a
418 static void set_rcfile_defaults(GKeyFile
*kf
)
422 if (g_key_file_has_key(kf
, "default", "socket_path", NULL
) == FALSE
) {
423 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
424 g_key_file_set_string(kf
, "default", "socket_path", buf
);
427 if (g_key_file_has_key(kf
, "default", "data_directory", NULL
) == FALSE
) {
428 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
429 g_key_file_set_string(kf
, "default", "data_directory", buf
);
432 if (g_key_file_has_key(kf
, "default", "log_path", NULL
) == FALSE
) {
433 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
434 g_key_file_set_string(kf
, "default", "log_path", buf
);
437 if (g_key_file_has_key(kf
, "default", "enable_logging", NULL
) == FALSE
)
438 g_key_file_set_boolean(kf
, "default", "enable_logging", FALSE
);
440 if (g_key_file_has_key(kf
, "default", "cache_size", NULL
) == FALSE
)
441 g_key_file_set_integer(kf
, "default", "cache_size", cache_size
);
444 if (g_key_file_has_key(kf
, "default", "disable_mlockall", NULL
) == FALSE
)
445 g_key_file_set_boolean(kf
, "default", "disable_mlockall", TRUE
);
448 if (g_key_file_has_key(kf
, "default", "cache_timeout", NULL
) == FALSE
)
449 g_key_file_set_integer(kf
, "default", "cache_timeout", -1);
451 if (g_key_file_has_key(kf
, "default", "iterations", NULL
) == FALSE
)
452 g_key_file_set_integer(kf
, "default", "iterations", 0);
454 if (g_key_file_has_key(kf
, "default", "disable_list_and_dump", NULL
) == FALSE
)
455 g_key_file_set_boolean(kf
, "default", "disable_list_and_dump", FALSE
);
457 if (g_key_file_has_key(kf
, "default", "iteration_progress", NULL
) == FALSE
)
458 g_key_file_set_integer(kf
, "default", "iteration_progress", 0);
460 if (g_key_file_has_key(kf
, "default", "compression_level", NULL
) == FALSE
)
461 g_key_file_set_integer(kf
, "default", "compression_level", 6);
463 if (g_key_file_has_key(kf
, "default", "recursion_depth", NULL
) == FALSE
)
464 g_key_file_set_integer(kf
, "default", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
466 if (g_key_file_has_key(kf
, "default", "zlib_bufsize", NULL
) == FALSE
)
467 g_key_file_set_integer(kf
, "default", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
469 zlib_bufsize
= g_key_file_get_integer(kf
, "default", "zlib_bufsize", NULL
);
471 max_recursion_depth
= g_key_file_get_integer(kf
, "default", "recursion_depth", NULL
);
472 disable_list_and_dump
= g_key_file_get_boolean(kf
, "default", "disable_list_and_dump", NULL
);
475 disable_mlock
= g_key_file_get_boolean(kf
, "default", "disable_mlockall", NULL
);
478 if (g_key_file_has_key(kf
, "default", "syslog", NULL
) == FALSE
)
479 g_key_file_set_boolean(kf
, "default", "syslog", FALSE
);
481 if (g_key_file_has_key(kf
, "default", "keepalive", NULL
) == FALSE
)
482 g_key_file_set_integer(kf
, "default", "keepalive", 5);
485 static GKeyFile
*parse_rcfile(int cmdline
)
487 GKeyFile
*kf
= g_key_file_new();
488 GError
*error
= NULL
;
490 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
491 log_write("%s: %s", rcfile
, error
->message
);
496 if (error
->code
== G_FILE_ERROR_NOENT
) {
497 g_clear_error(&error
);
498 set_rcfile_defaults(kf
);
502 g_clear_error(&error
);
506 set_rcfile_defaults(kf
);
511 static gchar
*get_password(const gchar
*prompt
)
513 gchar buf
[LINE_MAX
], *p
;
514 struct termios told
, tnew
;
517 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
518 err(EXIT_FAILURE
, "tcgetattr()");
520 memcpy(&tnew
, &told
, sizeof(struct termios
));
521 tnew
.c_lflag
&= ~(ECHO
);
522 tnew
.c_lflag
|= ICANON
|ECHONL
;
524 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
525 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
526 err(EXIT_FAILURE
, "tcsetattr()");
529 fprintf(stderr
, "%s", prompt
);
531 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
532 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
536 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
537 p
[strlen(p
) - 1] = 0;
542 key
= gcry_malloc(strlen(p
) + 1);
543 sprintf(key
, "%s", p
);
544 memset(&buf
, 0, sizeof(buf
));
548 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
554 if ((fd
= open_file(filename
, &st
)) == -1) {
555 warn("%s", filename
);
559 if (st
.st_size
== 0) {
560 warnx(N_("%s: skipping empty file"), filename
);
565 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
567 return error
? FALSE
: TRUE
;
570 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
576 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
579 if ((password
= get_password(prompt
)) == NULL
) {
580 warnx(N_("%s: skipping file"), filename
);
585 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
588 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
590 warnx(N_("%s: invalid password, skipping"), filename
);
595 warnx(N_("%s: invalid password"), filename
);
604 static gboolean
xml_import(const gchar
*filename
, gint iter
)
614 guchar shakey
[gcrykeysize
];
622 if (stat(filename
, &st
) == -1) {
623 warn("%s", filename
);
627 if ((error
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
628 send_error(NULL
, error
);
629 gcry_cipher_close(gh
);
630 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
634 if ((key
= get_password(N_("New password: "))) == NULL
) {
635 fprintf(stderr
, "%s\n", N_("Invalid password."));
636 gcry_cipher_close(gh
);
640 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
641 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
643 gcry_cipher_close(gh
);
647 if (g_utf8_collate(key
, key2
) != 0) {
648 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
651 gcry_cipher_close(gh
);
657 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
659 warn("%s", filename
);
660 gcry_cipher_close(gh
);
664 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
667 log_write("%s", strerror(ENOMEM
));
668 gcry_cipher_close(gh
);
672 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
676 gcry_cipher_close(gh
);
678 err(EXIT_FAILURE
, "read()");
682 xmlbuf
[st
.st_size
] = 0;
685 * Make sure the document validates.
687 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
688 log_write("xmlReadDoc()");
692 gcry_cipher_close(gh
);
697 xmlDocDumpMemory(doc
, &xml
, &len
);
700 level
= get_key_file_integer(filename
, "compression_level");
705 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
706 memset(shakey
, 0, sizeof(shakey
));
709 if (zerror
== Z_MEM_ERROR
)
710 warnx("%s", strerror(ENOMEM
));
712 warnx("do_compress() failed");
714 gcry_cipher_close(gh
);
723 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
));
725 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
726 gcry_cipher_close(gh
);
729 memset(shakey
, 0, sizeof(shakey
));
730 warnx("%s", gpg_strerror(error
));
734 memset(shakey
, 0, sizeof(shakey
));
738 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
741 GError
*gerror
= NULL
;
743 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
744 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
747 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
748 g_clear_error(&gerror
);
752 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
753 val
= g_key_file_get_string(keyfileh
, "default", what
, &gerror
);
756 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
757 g_clear_error(&gerror
);
765 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
768 GError
*gerror
= NULL
;
770 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
771 val
= g_key_file_get_integer(keyfileh
, section
, what
, &gerror
);
774 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
775 g_clear_error(&gerror
);
779 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
780 val
= g_key_file_get_integer(keyfileh
, "default", what
, &gerror
);
783 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
784 g_clear_error(&gerror
);
792 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
794 gboolean val
= FALSE
;
795 GError
*gerror
= NULL
;
797 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
798 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &gerror
);
801 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
802 g_clear_error(&gerror
);
806 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
807 val
= g_key_file_get_boolean(keyfileh
, "default", what
, &gerror
);
810 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
811 g_clear_error(&gerror
);
819 gchar
*expand_homedir(gchar
*str
)
824 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
826 return g_strdup(str
);
829 static gchar
*_getline(const gchar
*file
)
832 gchar buf
[LINE_MAX
], *p
;
835 if ((fp
= fopen(file
, "r")) == NULL
) {
840 if ((p
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
841 warnx(N_("%s: empty file?"), file
);
847 if (buf
[strlen(buf
) - 1] == '\n')
848 buf
[strlen(buf
) - 1] = 0;
850 str
= gcry_malloc(strlen(p
) + 1);
851 memcpy(str
, p
, strlen(p
));
853 memset(&buf
, 0, sizeof(buf
));
857 static gboolean
parse_keyfile_key()
864 groups
= g_key_file_get_groups(keyfileh
, &n
);
866 for (p
= groups
; *p
; p
++) {
867 GError
*error
= NULL
;
869 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
870 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
874 warnx("%s", error
->message
);
875 g_clear_error(&error
);
881 do_cache_push(*p
, str
);
887 warnx("%s", error
->message
);
888 g_clear_error(&error
);
892 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
894 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
898 warnx("%s", error
->message
);
899 g_clear_error(&error
);
905 t
= expand_homedir(file
);
909 if ((str
= _getline(file
)) == NULL
) {
915 do_cache_push(*p
, str
);
921 warnx("%s", error
->message
);
922 g_clear_error(&error
);
930 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
935 const gchar
*p
= filename
;
943 if (valid_filename(p
) == FALSE
) {
944 warnx(N_("%s: invalid characters in filename"), p
);
948 md5file
= gcry_malloc(16);
949 key
= gcry_malloc(gcrykeysize
);
950 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
952 if (cache_iscached(md5file
) == TRUE
) {
953 warnx(N_("%s: file already cached, skipping"), p
);
959 if (access(p
, R_OK
|W_OK
) != 0) {
963 if (errno
!= ENOENT
) {
973 if (get_input(p
, key
) == FALSE
) {
980 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
982 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
983 warnx(N_("%s: invalid password, skipping"), filename
);
990 if (cache_add_file(md5file
, key
) == FALSE
) {
991 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
997 timeout
= get_key_file_integer(p
, "cache_timeout");
998 cache_set_timeout(md5file
, timeout
);
999 warnx(N_("%s: file added to the cache"), filename
);
1005 static GSList
*remove_connection(GSList
*list
, struct client_thread_s
*cn
)
1008 struct client_s
*cl
= cn
->cl
;
1010 if (cn
->keepalive_tid
) {
1011 pth_raise(cn
->keepalive_tid
, SIGALRM
);
1012 pth_join(cn
->keepalive_tid
, &value
);
1015 pth_join(cn
->tid
, &value
);
1018 if (cl
->freed
== FALSE
)
1019 cleanup_assuan(cl
->ctx
);
1022 assuan_deinit_server(cl
->ctx
);
1027 pth_event_isolate(cn
->ev
);
1028 pth_event_free(cn
->ev
, PTH_FREE_THIS
);
1029 log_write(N_("client exited: fd=%i"), cn
->fd
);
1030 list
= g_slist_remove(list
, cn
);
1036 * Can't pth_event_concat() to an empty event.
1038 static int event_ring_hack(void *data
)
1044 * See if any thread has entered the DEAD queue and remove it.
1046 static GSList
*cleanup_dead_queue(GSList
*threads
, pth_event_t tid_events
)
1050 for (n
= g_slist_length(threads
), i
= 0; i
< n
; i
++) {
1051 struct client_thread_s
*cn
= g_slist_nth_data(threads
, i
);
1053 if (pth_event_occurred(cn
->ev
)) {
1054 threads
= remove_connection(threads
, cn
);
1055 return cleanup_dead_queue(threads
, tid_events
);
1058 pth_event_concat(tid_events
, cn
->ev
, NULL
);
1064 static void *socket_thread(void *arg
)
1066 gint sockfd
= (gint
)arg
;
1070 * Thread priority is inherited from the calling thread. This thread is
1071 * PTH_PRIO_MAX. Keep the "child" threads at standard priority.
1073 attr
= pth_attr_new();
1074 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
1077 socklen_t slen
= sizeof(struct sockaddr_un
);
1078 struct sockaddr_un raddr
;
1081 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1082 if (errno
!= EAGAIN
) {
1083 if (!quit
) // probably EBADF
1084 log_write("accept(): %s", strerror(errno
));
1092 struct client_thread_s
*new;
1094 new = g_malloc0(sizeof(struct client_thread_s
));
1097 log_write("%s", strerror(ENOMEM
));
1101 log_write(N_("new connection: fd=%i"), fd
);
1103 tid
= pth_spawn(attr
, client_thread
, new);
1107 log_write(N_("pth_spawn() failed"));
1113 new->ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1114 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1115 pth_event_concat(cn_events
, new->ev
, NULL
);
1116 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1117 pth_mutex_release(&cn_mutex
);
1121 pth_attr_destroy(attr
);
1123 /* Just in case pth_accept() failed for some reason other than EBADF */
1125 pth_exit(PTH_CANCELED
);
1129 static void *cleanup_thread(void *arg
)
1131 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1134 if (pth_wait(cn_events
) && pth_event_occurred(cn_events
)) {
1135 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1136 pth_event_isolate(cn_events
);
1137 pth_event_free(cn_events
, PTH_FREE_THIS
);
1138 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1139 cn_thread_list
= cleanup_dead_queue(cn_thread_list
, cn_events
);
1140 pth_mutex_release(&cn_mutex
);
1144 pth_event_isolate(cn_events
);
1145 pth_event_free(cn_events
, PTH_FREE_THIS
);
1149 static void *adjust_cache_time_thread(void *arg
)
1151 CACHE_LOCK(NULL
, NULL
);
1152 cache_adjust_timer();
1157 static void server_loop(int sockfd
)
1159 pth_t socket_tid
, cleanup_tid
;
1164 pth_event_t timeout_event
;
1167 pth_mutex_init(&cn_mutex
);
1168 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1171 sigaddset(&set
, SIGTERM
);
1172 sigaddset(&set
, SIGINT
);
1173 sigaddset(&set
, SIGUSR1
);
1174 sigaddset(&set
, SIGHUP
);
1175 sigaddset(&set
, SIGABRT
);
1177 attr
= pth_attr_new();
1178 pth_attr_init(attr
);
1179 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1180 socket_tid
= pth_spawn(attr
, socket_thread
, (void *)sockfd
);
1181 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1182 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
, PTH_CANCEL_ASYNCHRONOUS
);
1183 cleanup_tid
= pth_spawn(attr
, cleanup_thread
, NULL
);
1186 * For the cache_timeout configuration parameter. This replaces the old
1187 * SIGALRM stuff and is safer.
1189 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1190 pth_attr_init(attr
);
1191 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1192 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1197 pth_sigwait_ev(&set
, &sig
, timeout_event
);
1199 if (pth_event_occurred(timeout_event
)) {
1201 * The timer event has expired. Update the file cache. When the
1202 * cache mutex is locked and the timer expires again, the threads
1205 pth_spawn(attr
, adjust_cache_time_thread
, NULL
);
1206 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1207 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1211 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1213 /* Caught a signal. */
1219 CACHE_LOCK(NULL
, NULL
);
1220 cache_clear(NULL
, 2);
1227 CACHE_LOCK(NULL
, NULL
);
1228 log_write(N_("clearing file cache"));
1229 cache_clear(NULL
, 2);
1234 shutdown(sockfd
, SHUT_RDWR
);
1242 * We're out of the main server loop. This happens when a signal was sent
1243 * to terminate the daemon. We'll wait for all clients to disconnect
1244 * before exiting and ignore any following signals.
1246 pth_join(socket_tid
, &value
);
1247 pth_attr_destroy(attr
);
1248 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1250 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1252 /* 2 because the cleanup thread still exists, plus self. */
1254 log_write(N_("waiting for all threads to terminate"));
1260 if (n
!= n_clients
) {
1261 log_write(N_("%i threads remain"), n
-2);
1265 events
= pth_event(PTH_EVENT_FUNC
, event_ring_hack
, NULL
, pth_timeout(1, 0));
1266 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1268 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
1269 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1271 pth_event_concat(events
, cn
->ev
, NULL
);
1277 pth_mutex_release(&cn_mutex
);
1279 pth_yield(cleanup_tid
);
1281 pth_event_isolate(events
);
1282 pth_event_free(events
, PTH_FREE_THIS
);
1283 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1286 pth_cancel(cleanup_tid
);
1289 int main(int argc
, char *argv
[])
1292 struct sockaddr_un addr
;
1293 struct passwd
*pw
= getpwuid(getuid());
1294 gchar buf
[PATH_MAX
];
1295 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1296 gchar
*socketarg
= NULL
;
1297 gchar
*datadir
= NULL
;
1300 gchar
**cache_push
= NULL
;
1302 gchar
*import
= NULL
;
1303 gint default_timeout
;
1304 gint rcfile_spec
= 0;
1305 gint estatus
= EXIT_FAILURE
;
1308 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1311 gboolean secure
= FALSE
;
1313 gint background
= 0;
1316 #ifdef HAVE_SETRLIMIT
1319 rl
.rlim_cur
= rl
.rlim_max
= 0;
1321 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1322 err(EXIT_FAILURE
, "setrlimit()");
1327 setlocale(LC_ALL
, "");
1328 bindtextdomain("pwmd", LOCALEDIR
);
1333 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1335 g_mem_set_vtable(&mtable
);
1336 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1337 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1341 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
1343 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1344 err(EXIT_FAILURE
, "%s", buf
);
1346 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
1348 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1349 err(EXIT_FAILURE
, "%s", buf
);
1351 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1353 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1354 err(EXIT_FAILURE
, "sysconf()");
1356 cache_size
= page_size
;
1358 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1371 rcfile
= g_strdup(optarg
);
1375 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1383 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1386 if (g_key_file_has_key(keyfileh
, "default", "syslog", NULL
) == TRUE
)
1387 log_syslog
= g_key_file_get_boolean(keyfileh
, "default", "syslog", NULL
);
1389 if (log_syslog
== TRUE
)
1390 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
1392 if (g_key_file_has_key(keyfileh
, "default", "iterations", NULL
) == TRUE
)
1393 iter
= g_key_file_get_integer(keyfileh
, "default", "iterations", NULL
);
1395 #ifdef HAVE_MLOCKALL
1396 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
1405 opt
= xml_import(import
, iter
);
1406 g_key_file_free(keyfileh
);
1408 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1411 g_key_file_set_list_separator(keyfileh
, ',');
1413 if ((p
= g_key_file_get_string(keyfileh
, "default", "socket_path", NULL
)) == NULL
)
1414 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1418 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1420 socketarg
= g_strdup(buf
);
1425 if ((p
= g_key_file_get_string(keyfileh
, "default", "data_directory", NULL
)) == NULL
)
1426 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1428 datadir
= expand_homedir(p
);
1431 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "default", "disable_list_and_dump", NULL
) == TRUE
) {
1432 n
= g_key_file_get_boolean(keyfileh
, "default", "disable_list_and_dump", NULL
);
1433 disable_list_and_dump
= n
;
1436 disable_list_and_dump
= secure
;
1438 if (g_key_file_has_key(keyfileh
, "default", "cache_timeout", NULL
) == TRUE
)
1439 default_timeout
= g_key_file_get_integer(keyfileh
, "default", "cache_timeout", NULL
);
1441 default_timeout
= -1;
1443 if (g_key_file_has_key(keyfileh
, "default", "cache_size", NULL
) == TRUE
) {
1444 cache_size
= g_key_file_get_integer(keyfileh
, "default", "cache_size", NULL
);
1446 if (cache_size
< page_size
|| cache_size
% page_size
)
1447 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1450 if (g_key_file_has_key(keyfileh
, "default", "log_path", NULL
) == TRUE
) {
1451 if (g_key_file_has_key(keyfileh
, "default", "enable_logging", NULL
) == TRUE
) {
1452 n
= g_key_file_get_boolean(keyfileh
, "default", "enable_logging", NULL
);
1455 p
= g_key_file_get_string(keyfileh
, "default", "log_path", NULL
);
1459 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1461 logfile
= g_strdup(buf
);
1469 if (g_key_file_has_key(keyfileh
, "default", "cache_push", NULL
) == TRUE
)
1470 cache_push
= g_key_file_get_string_list(keyfileh
, "default", "cache_push", NULL
, NULL
);
1472 if (argc
!= optind
) {
1474 ptotal
= g_strv_length(cache_push
);
1476 for (; optind
< argc
; optind
++) {
1477 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1478 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1482 if (strchr(socketarg
, '/') == NULL
) {
1483 socketdir
= g_get_current_dir();
1484 socketname
= g_strdup(socketarg
);
1485 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1488 socketname
= g_strdup(strrchr(socketarg
, '/'));
1490 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1491 socketdir
= g_strdup(socketarg
);
1492 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1495 if ((key_cache
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1496 #ifdef MMAP_ANONYMOUS
1497 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1499 MAP_PRIVATE
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1501 err(EXIT_FAILURE
, "mmap()");
1504 if (mlock(key_cache
, cache_size
) == -1)
1507 memset(key_cache
, 0, cache_size
);
1509 if (chdir(datadir
)) {
1510 warn("%s", datadir
);
1515 if (parse_keyfile_key() == FALSE
)
1521 * Set the cache entry for a file. Prompts for the password.
1524 for (opt
= 0; cache_push
[opt
]; opt
++)
1525 do_cache_push(cache_push
[opt
], NULL
);
1527 g_strfreev(cache_push
);
1528 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1532 * bind() doesn't like the full pathname of the socket or any non alphanum
1533 * characters so change to the directory where the socket is wanted then
1534 * create it then change to datadir.
1536 if (chdir(socketdir
)) {
1537 warn("%s", socketdir
);
1543 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1548 addr
.sun_family
= AF_UNIX
;
1549 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1551 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1554 if (errno
== EADDRINUSE
)
1555 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1556 "stale socket. Please remove it manually."), socketpath
);
1562 if (g_key_file_has_key(keyfileh
, "default", "socket_perms", NULL
) == TRUE
) {
1563 gchar
*t
= g_key_file_get_string(keyfileh
, "default", "socket_perms", NULL
);
1564 mode_t mode
= strtol(t
, NULL
, 8);
1565 mode_t mask
= umask(0);
1569 if (chmod(socketname
, mode
) == -1) {
1570 warn("%s", socketname
);
1580 g_free(--socketname
);
1582 if (chdir(datadir
)) {
1583 warn("%s", datadir
);
1590 pth_mutex_init(&cache_mutex
);
1592 if (listen(sockfd
, 0) == -1) {
1614 * These are the signals that we use in threads. libpth can catch signals
1615 * itself so ignore them everywhere else. Note that using
1616 * signal(N, SIG_IGN) doesn't work like you might think.
1621 sigaddset(&set
, SIGTERM
);
1622 sigaddset(&set
, SIGINT
);
1624 /* Configuration file reloading. */
1625 sigaddset(&set
, SIGUSR1
);
1627 /* Clears the file cache. */
1628 sigaddset(&set
, SIGHUP
);
1630 /* Caught in client_thread(). Sends a cache status message. */
1631 sigaddset(&set
, SIGUSR2
);
1633 /* Caught in client_thread(). When keepalive_thread() fails, this signal
1634 * is raised to terminate the client. More of a failsafe than anything. */
1635 sigaddset(&set
, SIGQUIT
);
1637 /* Ignored everywhere. When a client disconnects abnormally this signal
1638 * gets raised. It isn't needed though because client_thread() will check
1639 * for errors even after the client disconnects. */
1640 sigaddset(&set
, SIGPIPE
);
1641 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
1642 server_loop(sockfd
);
1643 estatus
= EXIT_SUCCESS
;
1646 if (socketpath
&& do_unlink
) {
1651 g_key_file_free(keyfileh
);
1655 memset(key_cache
, 0, cache_size
);
1657 if (key_cache
&& munmap(key_cache
, cache_size
) == -1)
1658 log_write("munmap(): %s", strerror(errno
));
1660 if (estatus
== EXIT_SUCCESS
)
1661 log_write(N_("pwmd exiting normally"));
1664 #if defined(DEBUG) && !defined(MEM_DEBUG)