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 sigaddset(&set
, SIGINT
);
326 sigaddset(&set
, SIGUSR1
);
327 sigaddset(&set
, SIGHUP
);
328 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
330 sigaddset(&set
, SIGUSR2
);
331 sigaddset(&set
, SIGQUIT
);
333 cl
->ctx
= new_connection(fd
);
339 assuan_set_pointer(cl
->ctx
, cl
);
342 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
343 log_write("mlockall(): %s", strerror(errno
));
348 send_cache_status(cl
->ctx
);
349 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->fd
);
350 thd
->keepalive_tid
= pth_spawn(NULL
, keepalive_thread
, thd
);
356 pth_sigwait_ev(&set
, &sig
, ev
);
361 send_cache_status(cl
->ctx
);
370 if (pth_event_occurred(ev
)) {
371 rc
= assuan_process_next(cl
->ctx
);
374 cl
->inquire
= INQUIRE_NONE
;
376 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
379 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
380 rc
= assuan_process_done(cl
->ctx
, rc
);
383 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
388 if (cl
->inquire
== INQUIRE_OK
|| cl
->inquire_error
) {
389 /* Set from store_command_finalize(). */
390 cl
->inquire
= INQUIRE_NONE
;
391 rc
= assuan_process_done(cl
->ctx
, gpg_err_make(GPG_ERR_SOURCE_ANY
, cl
->inquire_error
));
392 cl
->inquire_error
= 0;
395 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
404 * Client cleanup (including XML data) is done in remove_connection() from
405 * the cleanup thread.
408 pth_event_free(ev
, PTH_FREE_ALL
);
416 * Make sure all settings are set to either the specified setting or a
419 static void set_rcfile_defaults(GKeyFile
*kf
)
423 if (g_key_file_has_key(kf
, "default", "socket_path", NULL
) == FALSE
) {
424 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
425 g_key_file_set_string(kf
, "default", "socket_path", buf
);
428 if (g_key_file_has_key(kf
, "default", "data_directory", NULL
) == FALSE
) {
429 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
430 g_key_file_set_string(kf
, "default", "data_directory", buf
);
433 if (g_key_file_has_key(kf
, "default", "log_path", NULL
) == FALSE
) {
434 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
435 g_key_file_set_string(kf
, "default", "log_path", buf
);
438 if (g_key_file_has_key(kf
, "default", "enable_logging", NULL
) == FALSE
)
439 g_key_file_set_boolean(kf
, "default", "enable_logging", FALSE
);
441 if (g_key_file_has_key(kf
, "default", "cache_size", NULL
) == FALSE
)
442 g_key_file_set_integer(kf
, "default", "cache_size", cache_size
);
445 if (g_key_file_has_key(kf
, "default", "disable_mlockall", NULL
) == FALSE
)
446 g_key_file_set_boolean(kf
, "default", "disable_mlockall", TRUE
);
449 if (g_key_file_has_key(kf
, "default", "cache_timeout", NULL
) == FALSE
)
450 g_key_file_set_integer(kf
, "default", "cache_timeout", -1);
452 if (g_key_file_has_key(kf
, "default", "iterations", NULL
) == FALSE
)
453 g_key_file_set_integer(kf
, "default", "iterations", 0);
455 if (g_key_file_has_key(kf
, "default", "disable_list_and_dump", NULL
) == FALSE
)
456 g_key_file_set_boolean(kf
, "default", "disable_list_and_dump", FALSE
);
458 if (g_key_file_has_key(kf
, "default", "iteration_progress", NULL
) == FALSE
)
459 g_key_file_set_integer(kf
, "default", "iteration_progress", 0);
461 if (g_key_file_has_key(kf
, "default", "compression_level", NULL
) == FALSE
)
462 g_key_file_set_integer(kf
, "default", "compression_level", 6);
464 if (g_key_file_has_key(kf
, "default", "recursion_depth", NULL
) == FALSE
)
465 g_key_file_set_integer(kf
, "default", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
467 if (g_key_file_has_key(kf
, "default", "zlib_bufsize", NULL
) == FALSE
)
468 g_key_file_set_integer(kf
, "default", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
470 zlib_bufsize
= g_key_file_get_integer(kf
, "default", "zlib_bufsize", NULL
);
472 max_recursion_depth
= g_key_file_get_integer(kf
, "default", "recursion_depth", NULL
);
473 disable_list_and_dump
= g_key_file_get_boolean(kf
, "default", "disable_list_and_dump", NULL
);
476 disable_mlock
= g_key_file_get_boolean(kf
, "default", "disable_mlockall", NULL
);
479 if (g_key_file_has_key(kf
, "default", "syslog", NULL
) == FALSE
)
480 g_key_file_set_boolean(kf
, "default", "syslog", FALSE
);
482 if (g_key_file_has_key(kf
, "default", "keepalive", NULL
) == FALSE
)
483 g_key_file_set_integer(kf
, "default", "keepalive", 5);
486 static GKeyFile
*parse_rcfile(int cmdline
)
488 GKeyFile
*kf
= g_key_file_new();
489 GError
*error
= NULL
;
491 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
492 log_write("%s: %s", rcfile
, error
->message
);
497 if (error
->code
== G_FILE_ERROR_NOENT
) {
498 g_clear_error(&error
);
499 set_rcfile_defaults(kf
);
503 g_clear_error(&error
);
507 set_rcfile_defaults(kf
);
512 static gchar
*get_password(const gchar
*prompt
)
514 gchar buf
[LINE_MAX
], *p
;
515 struct termios told
, tnew
;
518 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
519 err(EXIT_FAILURE
, "tcgetattr()");
521 memcpy(&tnew
, &told
, sizeof(struct termios
));
522 tnew
.c_lflag
&= ~(ECHO
);
523 tnew
.c_lflag
|= ICANON
|ECHONL
;
525 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
526 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
527 err(EXIT_FAILURE
, "tcsetattr()");
530 fprintf(stderr
, "%s", prompt
);
532 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
533 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
537 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
538 p
[strlen(p
) - 1] = 0;
543 key
= gcry_malloc(strlen(p
) + 1);
544 sprintf(key
, "%s", p
);
545 memset(&buf
, 0, sizeof(buf
));
549 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
555 if ((fd
= open_file(filename
, &st
)) == -1) {
556 warn("%s", filename
);
560 if (st
.st_size
== 0) {
561 warnx(N_("%s: skipping empty file"), filename
);
566 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
568 return error
? FALSE
: TRUE
;
571 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
577 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
580 if ((password
= get_password(prompt
)) == NULL
) {
581 warnx(N_("%s: skipping file"), filename
);
586 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
589 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
591 warnx(N_("%s: invalid password, skipping"), filename
);
596 warnx(N_("%s: invalid password"), filename
);
605 static gboolean
xml_import(const gchar
*filename
, gint iter
)
615 guchar shakey
[gcrykeysize
];
623 if (stat(filename
, &st
) == -1) {
624 warn("%s", filename
);
628 if ((gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
629 send_error(NULL
, gcryerrno
);
630 gcry_cipher_close(gh
);
631 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
635 if ((key
= get_password(N_("New password: "))) == NULL
) {
636 fprintf(stderr
, "%s\n", N_("Invalid password."));
637 gcry_cipher_close(gh
);
641 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
642 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
644 gcry_cipher_close(gh
);
648 if (g_utf8_collate(key
, key2
) != 0) {
649 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
652 gcry_cipher_close(gh
);
658 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
660 warn("%s", filename
);
661 gcry_cipher_close(gh
);
665 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
668 log_write("%s", strerror(ENOMEM
));
669 gcry_cipher_close(gh
);
673 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
677 gcry_cipher_close(gh
);
679 err(EXIT_FAILURE
, "read()");
683 xmlbuf
[st
.st_size
] = 0;
686 * Make sure the document validates.
688 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
689 log_write("xmlReadDoc()");
693 gcry_cipher_close(gh
);
698 xmlDocDumpMemory(doc
, &xml
, &len
);
701 level
= get_key_file_integer(filename
, "compression_level");
706 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
707 memset(shakey
, 0, sizeof(shakey
));
710 if (zerror
== Z_MEM_ERROR
)
711 warnx("%s", strerror(ENOMEM
));
713 warnx("do_compress() failed");
715 gcry_cipher_close(gh
);
724 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
));
726 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
727 gcry_cipher_close(gh
);
730 memset(shakey
, 0, sizeof(shakey
));
731 warnx("%s", gpg_strerror(error
));
735 memset(shakey
, 0, sizeof(shakey
));
739 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
742 GError
*gerror
= NULL
;
744 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
745 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
748 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
749 g_clear_error(&gerror
);
753 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
754 val
= g_key_file_get_string(keyfileh
, "default", what
, &gerror
);
757 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
758 g_clear_error(&gerror
);
766 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
769 GError
*gerror
= NULL
;
771 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
772 val
= g_key_file_get_integer(keyfileh
, section
, what
, &gerror
);
775 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
776 g_clear_error(&gerror
);
780 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
781 val
= g_key_file_get_integer(keyfileh
, "default", what
, &gerror
);
784 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
785 g_clear_error(&gerror
);
793 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
795 gboolean val
= FALSE
;
796 GError
*gerror
= NULL
;
798 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
799 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &gerror
);
802 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
803 g_clear_error(&gerror
);
807 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
808 val
= g_key_file_get_boolean(keyfileh
, "default", what
, &gerror
);
811 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
812 g_clear_error(&gerror
);
820 gchar
*expand_homedir(gchar
*str
)
825 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
827 return g_strdup(str
);
830 static gchar
*_getline(const gchar
*file
)
833 gchar buf
[LINE_MAX
], *p
;
836 if ((fp
= fopen(file
, "r")) == NULL
) {
841 if ((p
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
842 warnx(N_("%s: empty file?"), file
);
848 if (buf
[strlen(buf
) - 1] == '\n')
849 buf
[strlen(buf
) - 1] = 0;
851 str
= gcry_malloc(strlen(p
) + 1);
852 memcpy(str
, p
, strlen(p
));
854 memset(&buf
, 0, sizeof(buf
));
858 static gboolean
parse_keyfile_key()
865 groups
= g_key_file_get_groups(keyfileh
, &n
);
867 for (p
= groups
; *p
; p
++) {
868 GError
*error
= NULL
;
870 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
871 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
875 warnx("%s", error
->message
);
876 g_clear_error(&error
);
882 do_cache_push(*p
, str
);
888 warnx("%s", error
->message
);
889 g_clear_error(&error
);
893 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
895 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
899 warnx("%s", error
->message
);
900 g_clear_error(&error
);
906 t
= expand_homedir(file
);
910 if ((str
= _getline(file
)) == NULL
) {
916 do_cache_push(*p
, str
);
922 warnx("%s", error
->message
);
923 g_clear_error(&error
);
931 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
936 const gchar
*p
= filename
;
944 if (valid_filename(p
) == FALSE
) {
945 warnx(N_("%s: invalid characters in filename"), p
);
949 md5file
= gcry_malloc(16);
950 key
= gcry_malloc(gcrykeysize
);
951 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
953 if (cache_iscached(md5file
) == TRUE
) {
954 warnx(N_("%s: file already cached, skipping"), p
);
960 if (access(p
, R_OK
|W_OK
) != 0) {
964 if (errno
!= ENOENT
) {
974 if (get_input(p
, key
) == FALSE
) {
981 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
983 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
984 warnx(N_("%s: invalid password, skipping"), filename
);
991 if (cache_add_file(md5file
, key
) == FALSE
) {
992 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
998 timeout
= get_key_file_integer(p
, "cache_timeout");
999 cache_set_timeout(md5file
, timeout
);
1000 warnx(N_("%s: file added to the cache"), filename
);
1006 static GSList
*remove_connection(GSList
*list
, struct client_thread_s
*cn
)
1009 struct client_s
*cl
= cn
->cl
;
1011 if (cn
->keepalive_tid
) {
1012 pth_raise(cn
->keepalive_tid
, SIGALRM
);
1013 pth_join(cn
->keepalive_tid
, &value
);
1016 pth_join(cn
->tid
, &value
);
1019 if (cl
->freed
== FALSE
)
1020 cleanup_assuan(cl
->ctx
);
1023 assuan_deinit_server(cl
->ctx
);
1028 pth_event_isolate(cn
->ev
);
1029 pth_event_free(cn
->ev
, PTH_FREE_THIS
);
1030 log_write(N_("client exited: fd=%i"), cn
->fd
);
1031 list
= g_slist_remove(list
, cn
);
1037 * Can't pth_event_concat() to an empty event.
1039 static int event_ring_hack(void *data
)
1045 * See if any thread has entered the DEAD queue and remove it.
1047 static GSList
*cleanup_dead_queue(GSList
*threads
, pth_event_t tid_events
)
1051 for (n
= g_slist_length(threads
), i
= 0; i
< n
; i
++) {
1052 struct client_thread_s
*cn
= g_slist_nth_data(threads
, i
);
1054 if (pth_event_occurred(cn
->ev
)) {
1055 threads
= remove_connection(threads
, cn
);
1056 return cleanup_dead_queue(threads
, tid_events
);
1059 pth_event_concat(tid_events
, cn
->ev
, NULL
);
1065 static void *socket_thread(void *arg
)
1067 gint sockfd
= (gint
)arg
;
1071 * Thread priority is inherited from the calling thread. This thread is
1072 * PTH_PRIO_MAX. Keep the "child" threads at standard priority.
1074 attr
= pth_attr_new();
1075 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
1078 socklen_t slen
= sizeof(struct sockaddr_un
);
1079 struct sockaddr_un raddr
;
1082 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1083 if (errno
!= EAGAIN
) {
1084 if (!quit
) // probably EBADF
1085 log_write("accept(): %s", strerror(errno
));
1093 struct client_thread_s
*new;
1095 new = g_malloc0(sizeof(struct client_thread_s
));
1098 log_write("%s", strerror(ENOMEM
));
1102 log_write(N_("new connection: fd=%i"), fd
);
1104 tid
= pth_spawn(attr
, client_thread
, new);
1108 log_write(N_("pth_spawn() failed"));
1114 new->ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1115 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1116 pth_event_concat(cn_events
, new->ev
, NULL
);
1117 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1118 pth_mutex_release(&cn_mutex
);
1122 pth_attr_destroy(attr
);
1124 /* Just in case pth_accept() failed for some reason other than EBADF */
1126 pth_exit(PTH_CANCELED
);
1130 static void *cleanup_thread(void *arg
)
1132 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1135 if (pth_wait(cn_events
) && pth_event_occurred(cn_events
)) {
1136 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1137 pth_event_isolate(cn_events
);
1138 pth_event_free(cn_events
, PTH_FREE_THIS
);
1139 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1140 cn_thread_list
= cleanup_dead_queue(cn_thread_list
, cn_events
);
1141 pth_mutex_release(&cn_mutex
);
1145 pth_event_isolate(cn_events
);
1146 pth_event_free(cn_events
, PTH_FREE_THIS
);
1150 static void *adjust_cache_time_thread(void *arg
)
1152 CACHE_LOCK(NULL
, NULL
);
1153 cache_adjust_timer();
1158 static void server_loop(int sockfd
)
1160 pth_t socket_tid
, cleanup_tid
;
1165 pth_event_t timeout_event
;
1168 pth_mutex_init(&cn_mutex
);
1169 log_write(N_("%s started"), PACKAGE_STRING
);
1172 sigaddset(&set
, SIGTERM
);
1173 sigaddset(&set
, SIGINT
);
1174 sigaddset(&set
, SIGUSR1
);
1175 sigaddset(&set
, SIGUSR2
);
1176 sigaddset(&set
, SIGHUP
);
1178 attr
= pth_attr_new();
1179 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1180 socket_tid
= pth_spawn(attr
, socket_thread
, (void *)sockfd
);
1181 pth_attr_init(attr
);
1182 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1183 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1184 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
, PTH_CANCEL_ASYNCHRONOUS
);
1185 cleanup_tid
= pth_spawn(attr
, cleanup_thread
, NULL
);
1188 * For the cache_timeout configuration parameter. This replaces the old
1189 * SIGALRM stuff and is safer.
1191 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1192 pth_attr_init(attr
);
1193 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1194 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1199 pth_sigwait_ev(&set
, &sig
, timeout_event
);
1201 if (pth_event_occurred(timeout_event
)) {
1203 * The timer event has expired. Update the file cache. When the
1204 * cache mutex is locked and the timer expires again, the threads
1207 pth_spawn(attr
, adjust_cache_time_thread
, NULL
);
1208 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1209 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1213 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1215 /* Caught a signal. */
1221 CACHE_LOCK(NULL
, NULL
);
1222 cache_clear(NULL
, 2);
1229 CACHE_LOCK(NULL
, NULL
);
1230 log_write(N_("clearing file cache"));
1231 cache_clear(NULL
, 2);
1238 shutdown(sockfd
, SHUT_RDWR
);
1246 * We're out of the main server loop. This happens when a signal was sent
1247 * to terminate the daemon. We'll wait for all clients to disconnect
1248 * before exiting and ignore any following signals.
1250 pth_join(socket_tid
, &value
);
1251 pth_attr_destroy(attr
);
1252 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1254 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1256 /* 2 because the cleanup thread still exists, plus self. */
1258 log_write(N_("waiting for all threads to terminate"));
1264 if (n
!= n_clients
) {
1265 log_write(N_("%i threads remain"), n
-2);
1269 events
= pth_event(PTH_EVENT_FUNC
, event_ring_hack
, NULL
, pth_timeout(1, 0));
1270 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1272 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
1273 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1275 pth_event_concat(events
, cn
->ev
, NULL
);
1281 pth_mutex_release(&cn_mutex
);
1283 pth_yield(cleanup_tid
);
1285 pth_event_isolate(events
);
1286 pth_event_free(events
, PTH_FREE_THIS
);
1287 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1290 pth_cancel(cleanup_tid
);
1293 int main(int argc
, char *argv
[])
1296 struct sockaddr_un addr
;
1297 struct passwd
*pw
= getpwuid(getuid());
1298 gchar buf
[PATH_MAX
];
1299 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1300 gchar
*socketarg
= NULL
;
1301 gchar
*datadir
= NULL
;
1304 gchar
**cache_push
= NULL
;
1306 gchar
*import
= NULL
;
1307 gint default_timeout
;
1308 gint rcfile_spec
= 0;
1309 gint estatus
= EXIT_FAILURE
;
1312 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1315 gboolean secure
= FALSE
;
1317 gint background
= 0;
1320 #ifdef HAVE_SETRLIMIT
1323 rl
.rlim_cur
= rl
.rlim_max
= 0;
1325 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1326 err(EXIT_FAILURE
, "setrlimit()");
1331 setlocale(LC_ALL
, "");
1332 bindtextdomain("pwmd", LOCALEDIR
);
1337 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1339 g_mem_set_vtable(&mtable
);
1340 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1341 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1345 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
1347 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1348 err(EXIT_FAILURE
, "%s", buf
);
1350 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
1352 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1353 err(EXIT_FAILURE
, "%s", buf
);
1355 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1357 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1358 err(EXIT_FAILURE
, "sysconf()");
1360 cache_size
= page_size
;
1362 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1375 rcfile
= g_strdup(optarg
);
1379 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1387 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1390 if (g_key_file_has_key(keyfileh
, "default", "syslog", NULL
) == TRUE
)
1391 log_syslog
= g_key_file_get_boolean(keyfileh
, "default", "syslog", NULL
);
1393 if (log_syslog
== TRUE
)
1394 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
1396 if (g_key_file_has_key(keyfileh
, "default", "iterations", NULL
) == TRUE
)
1397 iter
= g_key_file_get_integer(keyfileh
, "default", "iterations", NULL
);
1399 #ifdef HAVE_MLOCKALL
1400 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
1409 opt
= xml_import(import
, iter
);
1410 g_key_file_free(keyfileh
);
1412 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1415 g_key_file_set_list_separator(keyfileh
, ',');
1417 if ((p
= g_key_file_get_string(keyfileh
, "default", "socket_path", NULL
)) == NULL
)
1418 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1422 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1424 socketarg
= g_strdup(buf
);
1429 if ((p
= g_key_file_get_string(keyfileh
, "default", "data_directory", NULL
)) == NULL
)
1430 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1432 datadir
= expand_homedir(p
);
1435 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "default", "disable_list_and_dump", NULL
) == TRUE
) {
1436 n
= g_key_file_get_boolean(keyfileh
, "default", "disable_list_and_dump", NULL
);
1437 disable_list_and_dump
= n
;
1440 disable_list_and_dump
= secure
;
1442 if (g_key_file_has_key(keyfileh
, "default", "cache_timeout", NULL
) == TRUE
)
1443 default_timeout
= g_key_file_get_integer(keyfileh
, "default", "cache_timeout", NULL
);
1445 default_timeout
= -1;
1447 if (g_key_file_has_key(keyfileh
, "default", "cache_size", NULL
) == TRUE
) {
1448 cache_size
= g_key_file_get_integer(keyfileh
, "default", "cache_size", NULL
);
1450 if (cache_size
< page_size
|| cache_size
% page_size
)
1451 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1454 if (g_key_file_has_key(keyfileh
, "default", "log_path", NULL
) == TRUE
) {
1455 if (g_key_file_has_key(keyfileh
, "default", "enable_logging", NULL
) == TRUE
) {
1456 n
= g_key_file_get_boolean(keyfileh
, "default", "enable_logging", NULL
);
1459 p
= g_key_file_get_string(keyfileh
, "default", "log_path", NULL
);
1463 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1465 logfile
= g_strdup(buf
);
1473 if (g_key_file_has_key(keyfileh
, "default", "cache_push", NULL
) == TRUE
)
1474 cache_push
= g_key_file_get_string_list(keyfileh
, "default", "cache_push", NULL
, NULL
);
1476 if (argc
!= optind
) {
1478 ptotal
= g_strv_length(cache_push
);
1480 for (; optind
< argc
; optind
++) {
1481 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1482 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1486 if (strchr(socketarg
, '/') == NULL
) {
1487 socketdir
= g_get_current_dir();
1488 socketname
= g_strdup(socketarg
);
1489 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1492 socketname
= g_strdup(strrchr(socketarg
, '/'));
1494 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1495 socketdir
= g_strdup(socketarg
);
1496 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1499 if ((key_cache
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1500 #ifdef MMAP_ANONYMOUS
1501 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1503 MAP_PRIVATE
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1505 err(EXIT_FAILURE
, "mmap()");
1508 if (mlock(key_cache
, cache_size
) == -1)
1511 memset(key_cache
, 0, cache_size
);
1513 if (chdir(datadir
)) {
1514 warn("%s", datadir
);
1519 if (parse_keyfile_key() == FALSE
)
1525 * Set the cache entry for a file. Prompts for the password.
1528 for (opt
= 0; cache_push
[opt
]; opt
++)
1529 do_cache_push(cache_push
[opt
], NULL
);
1531 g_strfreev(cache_push
);
1532 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1536 * bind() doesn't like the full pathname of the socket or any non alphanum
1537 * characters so change to the directory where the socket is wanted then
1538 * create it then change to datadir.
1540 if (chdir(socketdir
)) {
1541 warn("%s", socketdir
);
1547 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1552 addr
.sun_family
= AF_UNIX
;
1553 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1555 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1558 if (errno
== EADDRINUSE
)
1559 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1560 "stale socket. Please remove it manually."), socketpath
);
1566 if (g_key_file_has_key(keyfileh
, "default", "socket_perms", NULL
) == TRUE
) {
1567 gchar
*t
= g_key_file_get_string(keyfileh
, "default", "socket_perms", NULL
);
1568 mode_t mode
= strtol(t
, NULL
, 8);
1569 mode_t mask
= umask(0);
1573 if (chmod(socketname
, mode
) == -1) {
1574 warn("%s", socketname
);
1584 g_free(--socketname
);
1586 if (chdir(datadir
)) {
1587 warn("%s", datadir
);
1594 pth_mutex_init(&cache_mutex
);
1596 if (listen(sockfd
, 0) == -1) {
1618 sigaddset(&set
, SIGTERM
);
1619 sigaddset(&set
, SIGINT
);
1620 sigaddset(&set
, SIGUSR1
);
1621 sigaddset(&set
, SIGUSR2
);
1622 sigaddset(&set
, SIGHUP
);
1623 sigaddset(&set
, SIGQUIT
);
1624 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
1625 signal(SIGPIPE
, SIG_IGN
);
1626 signal(SIGQUIT
, SIG_IGN
);
1627 server_loop(sockfd
);
1628 estatus
= EXIT_SUCCESS
;
1631 if (socketpath
&& do_unlink
) {
1636 g_key_file_free(keyfileh
);
1640 memset(key_cache
, 0, cache_size
);
1642 if (key_cache
&& munmap(key_cache
, cache_size
) == -1)
1643 log_write("munmap(): %s", strerror(errno
));
1645 if (estatus
== EXIT_SUCCESS
)
1646 log_write(N_("pwmd exiting normally"));
1649 #if defined(DEBUG) && !defined(MEM_DEBUG)