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
26 #include <sys/socket.h>
35 #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
)) || !fmt
)
132 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
140 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
147 tm
= localtime(&now
);
148 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
149 tbuf
[sizeof(tbuf
) - 1] = 0;
150 line
= g_strdup_printf("%s %i %s\n", tbuf
, getpid(), args
);
161 write(fd
, line
, strlen(line
));
166 if (isatty(STDERR_FILENO
)) {
167 fprintf(stderr
, "%s", line
);
175 static void usage(gchar
*pn
)
178 "Usage: %s [-hvDb] [-f <rcfile>] [-I <filename>] [file1] [...]\n"
179 " -b run as a background process\n"
180 " -f load the specified rcfile (~/.pwmd/config)\n"
181 " -I import an XML file and write the encrypted data to stdout\n"
182 " -D disable use of the LIST and DUMP commands\n"
184 " -h this help text\n"
190 static int gcry_SecureCheck(const void *ptr
)
196 static void setup_gcrypt()
198 gcry_check_version(NULL
);
201 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
205 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
207 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
209 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
210 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
214 * This is called after every Assuan command.
216 static void command_finalize(assuan_context_t ctx
, gint error
)
221 static assuan_context_t
new_connection(gint fd
)
224 gchar ver
[ASSUAN_LINELENGTH
];
225 assuan_context_t ctx
;
227 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
232 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
233 assuan_set_hello_line(ctx
, ver
);
234 assuan_register_post_cmd_notify(ctx
, command_finalize
);
235 rc
= register_commands(ctx
);
240 rc
= assuan_accept(ctx
);
248 assuan_deinit_server(ctx
);
249 log_write("%s", gpg_strerror(rc
));
253 void send_cache_status_all()
257 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
258 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
260 if (pth_self() == cn
->tid
)
263 pth_raise(cn
->tid
, SIGUSR2
);
268 * Called every time a connection is made via pth_spawn(). This is the thread
271 static void *doit(void *data
)
273 gint fd
= (gint
)data
;
275 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
279 * This is a "child" thread. Don't catch any signals. Let the master
280 * thread take care of signals in server_loop().
283 sigaddset(&set
, SIGTERM
);
284 sigaddset(&set
, SIGINT
);
285 sigaddset(&set
, SIGUSR1
);
286 sigaddset(&set
, SIGHUP
);
287 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
289 sigaddset(&set
, SIGUSR2
);
290 sigaddset(&set
, SIGPIPE
);
292 cl
->ctx
= new_connection(fd
);
298 assuan_set_pointer(cl
->ctx
, cl
);
301 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
302 log_write("mlockall(): %s", strerror(errno
));
307 send_cache_status(cl
->ctx
);
308 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->fd
);
314 pth_sigwait_ev(&set
, &sig
, ev
);
319 send_cache_status(cl
->ctx
);
326 if (pth_event_occurred(ev
)) {
327 rc
= assuan_process_next(cl
->ctx
);
330 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
333 log_write("assuan_process_next(): %s", gpg_strerror(rc
));
334 rc
= assuan_process_done(cl
->ctx
, rc
);
337 log_write("assuan_process_done(): %s", gpg_strerror(rc
));
345 pth_event_free(ev
, PTH_FREE_ALL
);
347 if (cl
->freed
== FALSE
)
348 cleanup_assuan(cl
->ctx
);
351 assuan_deinit_server(cl
->ctx
);
358 * Make sure all settings are set to either the specified setting or a
361 static void set_rcfile_defaults(GKeyFile
*kf
)
365 if (g_key_file_has_key(kf
, "default", "socket_path", NULL
) == FALSE
) {
366 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
367 g_key_file_set_string(kf
, "default", "socket_path", buf
);
370 if (g_key_file_has_key(kf
, "default", "data_directory", NULL
) == FALSE
) {
371 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
372 g_key_file_set_string(kf
, "default", "data_directory", buf
);
375 if (g_key_file_has_key(kf
, "default", "log_path", NULL
) == FALSE
) {
376 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
377 g_key_file_set_string(kf
, "default", "log_path", buf
);
380 if (g_key_file_has_key(kf
, "default", "enable_logging", NULL
) == FALSE
)
381 g_key_file_set_boolean(kf
, "default", "enable_logging", FALSE
);
383 if (g_key_file_has_key(kf
, "default", "cache_size", NULL
) == FALSE
)
384 g_key_file_set_integer(kf
, "default", "cache_size", cache_size
);
387 if (g_key_file_has_key(kf
, "default", "disable_mlockall", NULL
) == FALSE
)
388 g_key_file_set_boolean(kf
, "default", "disable_mlockall", TRUE
);
391 if (g_key_file_has_key(kf
, "default", "cache_timeout", NULL
) == FALSE
)
392 g_key_file_set_integer(kf
, "default", "cache_timeout", -1);
394 if (g_key_file_has_key(kf
, "default", "iterations", NULL
) == FALSE
)
395 g_key_file_set_integer(kf
, "default", "iterations", 0);
397 if (g_key_file_has_key(kf
, "default", "disable_list_and_dump", NULL
) == FALSE
)
398 g_key_file_set_boolean(kf
, "default", "disable_list_and_dump", FALSE
);
400 if (g_key_file_has_key(kf
, "default", "iteration_progress", NULL
) == FALSE
)
401 g_key_file_set_integer(kf
, "default", "iteration_progress", 0);
403 if (g_key_file_has_key(kf
, "default", "compression_level", NULL
) == FALSE
)
404 g_key_file_set_integer(kf
, "default", "compression_level", 6);
406 if (g_key_file_has_key(kf
, "default", "recursion_depth", NULL
) == FALSE
)
407 g_key_file_set_integer(kf
, "default", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
409 if (g_key_file_has_key(kf
, "default", "zlib_bufsize", NULL
) == FALSE
)
410 g_key_file_set_integer(kf
, "default", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
412 zlib_bufsize
= g_key_file_get_integer(kf
, "default", "zlib_bufsize", NULL
);
414 max_recursion_depth
= g_key_file_get_integer(kf
, "default", "recursion_depth", NULL
);
415 disable_list_and_dump
= g_key_file_get_boolean(kf
, "default", "disable_list_and_dump", NULL
);
418 disable_mlock
= g_key_file_get_boolean(kf
, "default", "disable_mlockall", NULL
);
422 static GKeyFile
*parse_rcfile(int cmdline
)
424 GKeyFile
*kf
= g_key_file_new();
425 GError
*error
= NULL
;
427 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
428 log_write("%s: %s", rcfile
, error
->message
);
433 if (error
->code
== G_FILE_ERROR_NOENT
) {
434 g_clear_error(&error
);
435 set_rcfile_defaults(kf
);
439 g_clear_error(&error
);
443 set_rcfile_defaults(kf
);
448 static gchar
*get_password(const gchar
*prompt
)
450 gchar buf
[LINE_MAX
], *p
;
451 struct termios told
, tnew
;
454 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
455 err(EXIT_FAILURE
, "tcgetattr()");
457 memcpy(&tnew
, &told
, sizeof(struct termios
));
458 tnew
.c_lflag
&= ~(ECHO
);
459 tnew
.c_lflag
|= ICANON
|ECHONL
;
461 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
462 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
463 err(EXIT_FAILURE
, "tcsetattr()");
466 fprintf(stderr
, "%s", prompt
);
468 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
469 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
473 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
474 p
[strlen(p
) - 1] = 0;
479 key
= gcry_malloc(strlen(p
) + 1);
480 sprintf(key
, "%s", p
);
481 memset(&buf
, 0, sizeof(buf
));
485 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
491 if ((fd
= open_file(filename
, &st
)) == -1) {
492 warn("%s", filename
);
496 if (st
.st_size
== 0) {
497 warnx(N_("%s: skipping empty file"), filename
);
502 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
504 return error
? FALSE
: TRUE
;
507 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
513 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
516 if ((password
= get_password(prompt
)) == NULL
) {
517 warnx(N_("%s: skipping file"), filename
);
522 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
525 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
527 warnx(N_("%s: invalid password, skipping"), filename
);
532 warnx(N_("%s: invalid password"), filename
);
541 static gboolean
xml_import(const gchar
*filename
, gint iter
)
551 guchar shakey
[gcrykeysize
];
560 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1)
561 err(EXIT_FAILURE
, "mlockall()");
564 if (stat(filename
, &st
) == -1) {
565 warn("%s", filename
);
570 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
574 if ((gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
575 send_error(NULL
, gcryerrno
);
576 gcry_cipher_close(gh
);
577 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
581 if ((key
= get_password(N_("New password: "))) == NULL
) {
582 fprintf(stderr
, "%s\n", N_("Invalid password."));
583 gcry_cipher_close(gh
);
587 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
588 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
590 gcry_cipher_close(gh
);
594 if (g_utf8_collate(key
, key2
) != 0) {
595 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
598 gcry_cipher_close(gh
);
604 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
606 warn("%s", filename
);
607 gcry_cipher_close(gh
);
611 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
614 log_write("%s", strerror(ENOMEM
));
615 gcry_cipher_close(gh
);
619 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
623 gcry_cipher_close(gh
);
625 err(EXIT_FAILURE
, "read()");
629 xmlbuf
[st
.st_size
] = 0;
632 * Make sure the document validates.
634 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
635 log_write("xmlReadDoc()");
639 gcry_cipher_close(gh
);
644 xmlDocDumpMemory(doc
, &xml
, &len
);
647 level
= get_key_file_integer(filename
, "compression_level");
652 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
653 memset(shakey
, 0, sizeof(shakey
));
656 if (zerror
== Z_MEM_ERROR
)
657 warnx("%s", strerror(ENOMEM
));
659 warnx("do_compress() failed");
661 gcry_cipher_close(gh
);
670 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
));
672 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
673 gcry_cipher_close(gh
);
676 memset(shakey
, 0, sizeof(shakey
));
677 warnx("%s", gpg_strerror(error
));
681 memset(shakey
, 0, sizeof(shakey
));
685 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
688 GError
*gerror
= NULL
;
690 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
691 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
694 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
695 g_clear_error(&gerror
);
699 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
700 val
= g_key_file_get_string(keyfileh
, "default", what
, &gerror
);
703 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
704 g_clear_error(&gerror
);
712 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
715 GError
*gerror
= NULL
;
717 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
718 val
= g_key_file_get_integer(keyfileh
, section
, what
, &gerror
);
721 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
722 g_clear_error(&gerror
);
726 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
727 val
= g_key_file_get_integer(keyfileh
, "default", what
, &gerror
);
730 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
731 g_clear_error(&gerror
);
739 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
741 gboolean val
= FALSE
;
742 GError
*gerror
= NULL
;
744 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
745 val
= g_key_file_get_boolean(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_boolean(keyfileh
, "default", what
, &gerror
);
757 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
758 g_clear_error(&gerror
);
766 gchar
*expand_homedir(gchar
*str
)
771 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
773 return g_strdup(str
);
776 static gchar
*_getline(const gchar
*file
)
779 gchar buf
[LINE_MAX
], *p
;
782 if ((fp
= fopen(file
, "r")) == NULL
) {
787 if ((p
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
788 warnx(N_("%s: empty file?"), file
);
794 if (buf
[strlen(buf
) - 1] == '\n')
795 buf
[strlen(buf
) - 1] = 0;
797 str
= gcry_malloc(strlen(p
) + 1);
798 memcpy(str
, p
, strlen(p
));
800 memset(&buf
, 0, sizeof(buf
));
804 static gboolean
parse_keyfile_key()
811 groups
= g_key_file_get_groups(keyfileh
, &n
);
813 for (p
= groups
; *p
; p
++) {
814 GError
*error
= NULL
;
816 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
817 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
821 warnx("%s", error
->message
);
822 g_clear_error(&error
);
828 do_cache_push(*p
, str
);
834 warnx("%s", error
->message
);
835 g_clear_error(&error
);
839 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
841 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
845 warnx("%s", error
->message
);
846 g_clear_error(&error
);
852 t
= expand_homedir(file
);
856 if ((str
= _getline(file
)) == NULL
) {
862 do_cache_push(*p
, str
);
868 warnx("%s", error
->message
);
869 g_clear_error(&error
);
877 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
882 const gchar
*p
= filename
;
890 if (valid_filename(p
) == FALSE
) {
891 warnx(N_("%s: invalid characters in filename"), p
);
895 md5file
= gcry_malloc(16);
896 key
= gcry_malloc(gcrykeysize
);
897 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
899 if (cache_iscached(md5file
) == TRUE
) {
900 warnx(N_("%s: file already cached, skipping"), p
);
906 if (access(p
, R_OK
|W_OK
) != 0) {
910 if (errno
!= ENOENT
) {
920 if (get_input(p
, key
) == FALSE
) {
927 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
929 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
930 warnx(N_("%s: invalid password, skipping"), filename
);
937 if (cache_add_file(md5file
, key
) == FALSE
) {
938 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
944 timeout
= get_key_file_integer(p
, "cache_timeout");
945 cache_set_timeout(md5file
, timeout
);
946 warnx(N_("%s: file added to the cache"), filename
);
952 static GSList
*remove_connection(GSList
*list
, struct client_thread_s
*cn
)
956 pth_join(cn
->tid
, &value
);
958 pth_event_isolate(cn
->ev
);
959 pth_event_free(cn
->ev
, PTH_FREE_THIS
);
960 log_write(N_("client exited: fd=%i"), cn
->fd
);
961 list
= g_slist_remove(list
, cn
);
967 * Can't pth_event_concat() to an empty event.
969 static int event_ring_hack(void *data
)
975 * See if any thread has entered the DEAD queue and remove it.
977 static GSList
*cleanup_dead_queue(GSList
*threads
, pth_event_t tid_events
)
981 for (n
= g_slist_length(threads
), i
= 0; i
< n
; i
++) {
982 struct client_thread_s
*cn
= g_slist_nth_data(threads
, i
);
984 if (pth_event_occurred(cn
->ev
)) {
985 threads
= remove_connection(threads
, cn
);
986 return cleanup_dead_queue(threads
, tid_events
);
989 pth_event_concat(tid_events
, cn
->ev
, NULL
);
995 static void *socket_thread(void *arg
)
998 gint sockfd
= (gint
)arg
;
1002 * Thread priority is inherited from the calling thread. This thread is
1003 * PTH_PRIO_MAX. Keep the "child" threads at standard priority.
1005 attr
= pth_attr_new();
1006 pth_attr_init(attr
);
1007 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_STD
);
1009 ev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
|PTH_UNTIL_FD_EXCEPTION
, sockfd
);
1012 socklen_t slen
= sizeof(struct sockaddr_un
);
1013 struct sockaddr_un raddr
;
1017 if (pth_wait(ev
) && pth_event_occurred(ev
)) {
1018 if ((fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1019 if (errno
!= EAGAIN
) {
1020 if (!quit
) // probably EBADF
1021 log_write("accept(): %s", strerror(errno
));
1030 struct client_thread_s
*new;
1032 log_write(N_("new connection: fd=%i"), fd
);
1033 tid
= pth_spawn(attr
, doit
, (void *)fd
);
1036 log_write(N_("pth_spawn() failed"));
1040 new = g_malloc0(sizeof(struct client_thread_s
));
1043 log_write("%s", strerror(ENOMEM
));
1045 pth_join(tid
, &value
);
1051 new->ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1052 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1053 pth_event_concat(cn_events
, new->ev
, NULL
);
1054 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1055 pth_mutex_release(&cn_mutex
);
1059 pth_event_free(ev
, PTH_FREE_THIS
);
1060 pth_attr_destroy(attr
);
1061 pth_exit(PTH_CANCELED
);
1065 static void *cleanup_thread(void *arg
)
1067 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1070 if (pth_wait(cn_events
) && pth_event_occurred(cn_events
)) {
1071 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1072 pth_event_isolate(cn_events
);
1073 pth_event_free(cn_events
, PTH_FREE_THIS
);
1074 cn_events
= pth_event(PTH_EVENT_TIME
, pth_timeout(0, 500000));
1075 cn_thread_list
= cleanup_dead_queue(cn_thread_list
, cn_events
);
1076 pth_mutex_release(&cn_mutex
);
1080 pth_event_isolate(cn_events
);
1081 pth_event_free(cn_events
, PTH_FREE_THIS
);
1085 static void *adjust_cache_time_thread(void *arg
)
1087 MUTEX_LOCK(NULL
, PTH_RWLOCK_RW
, NULL
);
1088 cache_adjust_timer();
1093 static void server_loop(int sockfd
)
1095 pth_t socket_tid
, cleanup_tid
;
1100 pth_event_t timeout_event
;
1103 pth_mutex_init(&cn_mutex
);
1104 log_write(N_("%s started"), PACKAGE_STRING
);
1107 sigaddset(&set
, SIGPIPE
);
1108 sigaddset(&set
, SIGTERM
);
1109 sigaddset(&set
, SIGINT
);
1110 sigaddset(&set
, SIGUSR1
);
1111 sigaddset(&set
, SIGUSR2
);
1112 sigaddset(&set
, SIGHUP
);
1115 * pth_accept() doesn't timeout so we set the socket file descriptor to
1118 pth_fdmode(sockfd
, PTH_FDMODE_NONBLOCK
);
1120 attr
= pth_attr_new();
1121 pth_attr_init(attr
);
1122 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1123 socket_tid
= pth_spawn(attr
, socket_thread
, (void *)sockfd
);
1124 pth_attr_destroy(attr
);
1125 attr
= pth_attr_new();
1126 pth_attr_init(attr
);
1127 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MAX
);
1128 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1129 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
, PTH_CANCEL_ASYNCHRONOUS
);
1130 cleanup_tid
= pth_spawn(attr
, cleanup_thread
, NULL
);
1131 pth_attr_init(attr
);
1134 * For the cache_timeout configuration parameter. This replaces the old
1135 * SIGALRM stuff and is safer.
1137 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1138 attr
= pth_attr_new();
1139 pth_attr_init(attr
);
1140 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1141 pth_attr_set(attr
, PTH_ATTR_PRIO
, PTH_PRIO_MIN
);
1146 pth_sigwait_ev(&set
, &sig
, timeout_event
);
1148 if (pth_event_occurred(timeout_event
)) {
1150 * The timer event has expired. Update the file cache. When the
1151 * cache mutex is locked and the timer expires again, the threads
1154 pth_spawn(attr
, adjust_cache_time_thread
, NULL
);
1155 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1156 timeout_event
= pth_event(PTH_EVENT_TIME
, pth_timeout(1, 0));
1160 /* Caught a signal. */
1166 MUTEX_LOCK(NULL
, PTH_RWLOCK_RW
, NULL
);
1167 cache_clear(NULL
, 2);
1174 MUTEX_LOCK(NULL
, PTH_RWLOCK_RW
, NULL
);
1175 log_write(N_("clearing file cache"));
1176 cache_clear(NULL
, 2);
1184 shutdown(sockfd
, SHUT_RDWR
);
1186 pth_join(socket_tid
, &value
);
1193 * We're out of the main server loop. This happens when a signal was sent
1194 * to terminate the daemon. We'll wait for all clients to disconnect
1195 * before exiting and ignore any following signals.
1197 pth_attr_destroy(attr
);
1198 pth_event_free(timeout_event
, PTH_FREE_THIS
);
1200 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1202 /* 2 because the cleanup thread still exists, plus self. */
1204 log_write(N_("waiting for all threads to terminate"));
1210 if (n
!= n_clients
) {
1211 log_write(N_("%i clients remain"), n
-2);
1215 events
= pth_event(PTH_EVENT_FUNC
, event_ring_hack
, NULL
, pth_timeout(1, 0));
1216 pth_mutex_acquire(&cn_mutex
, FALSE
, NULL
);
1218 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
1219 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1221 pth_event_concat(events
, cn
->ev
, NULL
);
1224 pth_mutex_release(&cn_mutex
);
1226 pth_yield(cleanup_tid
);
1227 pth_event_isolate(events
);
1228 pth_event_free(events
, PTH_FREE_THIS
);
1229 n
= pth_ctrl(PTH_CTRL_GETTHREADS
);
1232 pth_cancel(cleanup_tid
);
1235 int main(int argc
, char *argv
[])
1238 struct sockaddr_un addr
;
1239 struct passwd
*pw
= getpwuid(getuid());
1240 gchar buf
[PATH_MAX
];
1241 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1242 gchar
*socketarg
= NULL
;
1243 gchar
*datadir
= NULL
;
1246 gchar
**cache_push
= NULL
;
1248 gchar
*import
= NULL
;
1249 gint default_timeout
;
1250 gint rcfile_spec
= 0;
1251 gint estatus
= EXIT_FAILURE
;
1254 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1257 gboolean secure
= FALSE
;
1259 gint background
= 0;
1263 #ifdef HAVE_SETRLIMIT
1266 rl
.rlim_cur
= rl
.rlim_max
= 0;
1268 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1269 err(EXIT_FAILURE
, "setrlimit()");
1275 setlocale(LC_ALL
, "");
1276 bindtextdomain("pwmd", LOCALEDIR
);
1281 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1283 g_mem_set_vtable(&mtable
);
1284 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1285 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1289 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
1291 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1292 err(EXIT_FAILURE
, "%s", buf
);
1294 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
1296 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1297 err(EXIT_FAILURE
, "%s", buf
);
1299 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1301 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1302 err(EXIT_FAILURE
, "sysconf()");
1304 cache_size
= page_size
;
1306 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1319 rcfile
= g_strdup(optarg
);
1323 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1331 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1334 if (g_key_file_has_key(keyfileh
, "default", "iterations", NULL
) == TRUE
)
1335 iter
= g_key_file_get_integer(keyfileh
, "default", "iterations", NULL
);
1340 opt
= xml_import(import
, iter
);
1341 g_key_file_free(keyfileh
);
1343 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1346 g_key_file_set_list_separator(keyfileh
, ',');
1348 if ((p
= g_key_file_get_string(keyfileh
, "default", "socket_path", NULL
)) == NULL
)
1349 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1353 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1355 socketarg
= g_strdup(buf
);
1360 if ((p
= g_key_file_get_string(keyfileh
, "default", "data_directory", NULL
)) == NULL
)
1361 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1363 datadir
= expand_homedir(p
);
1366 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "default", "disable_list_and_dump", NULL
) == TRUE
) {
1367 n
= g_key_file_get_boolean(keyfileh
, "default", "disable_list_and_dump", NULL
);
1368 disable_list_and_dump
= n
;
1371 disable_list_and_dump
= secure
;
1373 if (g_key_file_has_key(keyfileh
, "default", "cache_timeout", NULL
) == TRUE
)
1374 default_timeout
= g_key_file_get_integer(keyfileh
, "default", "cache_timeout", NULL
);
1376 default_timeout
= -1;
1378 if (g_key_file_has_key(keyfileh
, "default", "cache_size", NULL
) == TRUE
) {
1379 cache_size
= g_key_file_get_integer(keyfileh
, "default", "cache_size", NULL
);
1381 if (cache_size
< page_size
|| cache_size
% page_size
)
1382 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1385 if (g_key_file_has_key(keyfileh
, "default", "log_path", NULL
) == TRUE
) {
1386 if (g_key_file_has_key(keyfileh
, "default", "enable_logging", NULL
) == TRUE
) {
1387 n
= g_key_file_get_boolean(keyfileh
, "default", "enable_logging", NULL
);
1390 p
= g_key_file_get_string(keyfileh
, "default", "log_path", NULL
);
1394 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1396 logfile
= g_strdup(buf
);
1404 if (g_key_file_has_key(keyfileh
, "default", "cache_push", NULL
) == TRUE
)
1405 cache_push
= g_key_file_get_string_list(keyfileh
, "default", "cache_push", NULL
, NULL
);
1407 if (argc
!= optind
) {
1409 ptotal
= g_strv_length(cache_push
);
1411 for (; optind
< argc
; optind
++) {
1412 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1413 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1417 if (strchr(socketarg
, '/') == NULL
) {
1418 socketdir
= g_get_current_dir();
1419 socketname
= g_strdup(socketarg
);
1420 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1423 socketname
= g_strdup(strrchr(socketarg
, '/'));
1425 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1426 socketdir
= g_strdup(socketarg
);
1427 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1430 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1431 #ifdef MMAP_ANONYMOUS
1432 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1434 MAP_PRIVATE
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1436 err(EXIT_FAILURE
, "mmap()");
1439 if (mlock(shm_data
, cache_size
) == -1)
1442 memset(shm_data
, 0, cache_size
);
1444 if (chdir(datadir
)) {
1445 warn("%s", datadir
);
1450 if (parse_keyfile_key() == FALSE
)
1456 * Set the cache entry for a file. Prompts for the password.
1459 for (opt
= 0; cache_push
[opt
]; opt
++)
1460 do_cache_push(cache_push
[opt
], NULL
);
1462 g_strfreev(cache_push
);
1463 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1467 * bind() doesn't like the full pathname of the socket or any non alphanum
1468 * characters so change to the directory where the socket is wanted then
1469 * create it then change to datadir.
1471 if (chdir(socketdir
)) {
1472 warn("%s", socketdir
);
1478 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1483 addr
.sun_family
= AF_UNIX
;
1484 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1486 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1489 if (errno
== EADDRINUSE
)
1490 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1491 "stale socket. Please remove it manually."), socketpath
);
1497 if (g_key_file_has_key(keyfileh
, "default", "socket_perms", NULL
) == TRUE
) {
1498 gchar
*t
= g_key_file_get_string(keyfileh
, "default", "socket_perms", NULL
);
1499 mode_t mode
= strtol(t
, NULL
, 8);
1500 mode_t mask
= umask(0);
1504 if (chmod(socketname
, mode
) == -1) {
1505 warn("%s", socketname
);
1515 g_free(--socketname
);
1517 if (chdir(datadir
)) {
1518 warn("%s", datadir
);
1525 pth_rwlock_init(&cache_rwlock
);
1527 if (listen(sockfd
, 0) == -1) {
1549 sigaddset(&set
, SIGTERM
);
1550 sigaddset(&set
, SIGINT
);
1551 sigaddset(&set
, SIGUSR1
);
1552 sigaddset(&set
, SIGUSR2
);
1553 sigaddset(&set
, SIGHUP
);
1554 sigaddset(&set
, SIGPIPE
);
1555 pth_sigmask(SIG_BLOCK
, &set
, NULL
);
1556 server_loop(sockfd
);
1557 estatus
= EXIT_SUCCESS
;
1560 if (socketpath
&& do_unlink
) {
1565 g_key_file_free(keyfileh
);
1567 memset(shm_data
, 0, cache_size
);
1569 if (munmap(shm_data
, cache_size
) == -1)
1570 log_write("munmap(): %s", strerror(errno
));
1572 if (estatus
== EXIT_SUCCESS
)
1573 log_write(N_("pwmd exiting normally"));
1575 #if defined(DEBUG) && !defined(MEM_DEBUG)