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
, int e
)
93 gpg_error_t n
= gpg_error_from_errno(e
);
95 return 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
);
107 log_write("%s\n", pwmd_strerror(e
));
111 if (n
== EPWMD_LIBXML_ERROR
) {
112 xmlErrorPtr xml_error
= xmlGetLastError();
113 return assuan_set_error(ctx
, code
, xml_error
->message
);
116 return 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 catchsig(gint sig
)
179 if (sig
!= SIGALRM
&& sig
!= SIGCHLD
)
180 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
187 cache_clear(NULL
, 2);
193 cache_adjust_timer();
197 waitpid(-1, &status
, 0);
199 if (WIFEXITED(status
) || WIFSIGNALED(status
))
204 log_write(N_("clearing file cache"));
205 cache_clear(NULL
, 2);
208 signal(SIGALRM
, SIG_IGN
);
210 shutdown(sfd
, SHUT_RDWR
);
216 static void usage(gchar
*pn
)
219 "Usage: %s [-hvDb] [-f <rcfile>] [-I <filename>] [file1] [...]\n"
220 " -b run as a background process\n"
221 " -f load the specified rcfile (~/.pwmd/config)\n"
222 " -I import an XML file and write the encrypted data to stdout\n"
223 " -D disable use of the LIST and DUMP commands\n"
225 " -h this help text\n"
231 static int gcry_SecureCheck(const void *ptr
)
237 static void setup_gcrypt()
239 gcry_check_version(NULL
);
242 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
246 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
248 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
250 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
251 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
254 static void child_catchsig(int sig
)
256 assuan_context_t ctx
= global_client
->ctx
;
261 g_free(global_client
);
262 assuan_deinit_server(ctx
);
263 log_write(N_("exiting"));
277 * Called every time a connection is made.
279 static void doit(int fd
)
281 assuan_context_t ctx
;
283 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
284 gchar ver
[ASSUAN_LINELENGTH
];
286 signal(SIGCHLD
, SIG_DFL
);
287 signal(SIGHUP
, SIG_IGN
);
288 signal(SIGINT
, SIG_IGN
);
289 signal(SIGALRM
, SIG_IGN
);
290 signal(SIGTERM
, child_catchsig
);
291 signal(SIGABRT
, child_catchsig
);
293 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
295 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
296 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
301 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
302 log_write("mlockall(): %s", strerror(errno
));
307 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
310 log_write("%s", gpg_strerror(rc
));
314 g_snprintf(ver
, sizeof(ver
), "%s", PACKAGE_STRING
);
315 assuan_set_hello_line(ctx
, ver
);
316 assuan_set_pointer(ctx
, cl
);
317 rc
= register_commands(ctx
);
320 log_write("%s", gpg_strerror(rc
));
328 * It would be nice if there were an assuan_register_pre_cmd_notify().
329 * That way we can see if the file has been modified before calling the
332 rc
= assuan_accept(ctx
);
335 log_write("%s", gpg_strerror(rc
));
339 rc
= assuan_process(ctx
);
342 log_write("%s", gpg_strerror(rc
));
345 if (cl
->freed
== FALSE
)
349 assuan_deinit_server(ctx
);
350 log_write(N_("exiting"));
355 * Make sure all settings are set to either the specified setting or a
358 static void set_rcfile_defaults(GKeyFile
*kf
)
362 if (g_key_file_has_key(kf
, "default", "socket_path", NULL
) == FALSE
) {
363 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
364 g_key_file_set_string(kf
, "default", "socket_path", buf
);
367 if (g_key_file_has_key(kf
, "default", "data_directory", NULL
) == FALSE
) {
368 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
369 g_key_file_set_string(kf
, "default", "data_directory", buf
);
372 if (g_key_file_has_key(kf
, "default", "log_path", NULL
) == FALSE
) {
373 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
374 g_key_file_set_string(kf
, "default", "log_path", buf
);
377 if (g_key_file_has_key(kf
, "default", "enable_logging", NULL
) == FALSE
)
378 g_key_file_set_boolean(kf
, "default", "enable_logging", FALSE
);
380 if (g_key_file_has_key(kf
, "default", "cache_size", NULL
) == FALSE
)
381 g_key_file_set_integer(kf
, "default", "cache_size", cache_size
);
384 if (g_key_file_has_key(kf
, "default", "disable_mlockall", NULL
) == FALSE
)
385 g_key_file_set_boolean(kf
, "default", "disable_mlockall", TRUE
);
388 if (g_key_file_has_key(kf
, "default", "cache_timeout", NULL
) == FALSE
)
389 g_key_file_set_integer(kf
, "default", "cache_timeout", -1);
391 if (g_key_file_has_key(kf
, "default", "iterations", NULL
) == FALSE
)
392 g_key_file_set_integer(kf
, "default", "iterations", 0);
394 if (g_key_file_has_key(kf
, "default", "disable_list_and_dump", NULL
) == FALSE
)
395 g_key_file_set_boolean(kf
, "default", "disable_list_and_dump", FALSE
);
397 if (g_key_file_has_key(kf
, "default", "iteration_progress", NULL
) == FALSE
)
398 g_key_file_set_integer(kf
, "default", "iteration_progress", 0);
400 if (g_key_file_has_key(kf
, "default", "compression_level", NULL
) == FALSE
)
401 g_key_file_set_integer(kf
, "default", "compression_level", 6);
403 if (g_key_file_has_key(kf
, "default", "recursion_depth", NULL
) == FALSE
)
404 g_key_file_set_integer(kf
, "default", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
407 if (g_key_file_has_key(kf
, "default", "zlib_bufsize", NULL
) == FALSE
)
408 g_key_file_set_integer(kf
, "default", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
410 zlib_bufsize
= g_key_file_get_integer(kf
, "default", "zlib_bufsize", NULL
);
413 max_recursion_depth
= g_key_file_get_integer(kf
, "default", "recursion_depth", NULL
);
414 disable_list_and_dump
= g_key_file_get_boolean(kf
, "default", "disable_list_and_dump", NULL
);
417 disable_mlock
= g_key_file_get_boolean(kf
, "default", "disable_mlockall", NULL
);
421 static GKeyFile
*parse_rcfile(int cmdline
)
423 GKeyFile
*kf
= g_key_file_new();
424 GError
*error
= NULL
;
426 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
427 log_write("%s: %s", rcfile
, error
->message
);
432 if (error
->code
== G_FILE_ERROR_NOENT
) {
433 g_clear_error(&error
);
434 set_rcfile_defaults(kf
);
438 g_clear_error(&error
);
442 set_rcfile_defaults(kf
);
447 static gchar
*get_password(const gchar
*prompt
)
449 gchar buf
[LINE_MAX
], *p
;
450 struct termios told
, tnew
;
453 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
454 err(EXIT_FAILURE
, "tcgetattr()");
456 memcpy(&tnew
, &told
, sizeof(struct termios
));
457 tnew
.c_lflag
&= ~(ECHO
);
458 tnew
.c_lflag
|= ICANON
|ECHONL
;
460 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
461 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
462 err(EXIT_FAILURE
, "tcsetattr()");
465 fprintf(stderr
, "%s", prompt
);
467 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
468 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
472 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
473 p
[strlen(p
) - 1] = 0;
478 key
= gcry_malloc(strlen(p
) + 1);
479 sprintf(key
, "%s", p
);
480 memset(&buf
, 0, sizeof(buf
));
484 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
490 if ((fd
= open_file(filename
, &st
)) == -1) {
491 warn("%s", filename
);
495 if (st
.st_size
== 0) {
496 warnx(N_("%s: skipping empty file"), filename
);
501 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
503 return error
? FALSE
: TRUE
;
506 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
512 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
515 if ((password
= get_password(prompt
)) == NULL
) {
516 warnx(N_("%s: skipping file"), filename
);
521 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
524 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
526 warnx(N_("%s: invalid password, skipping"), filename
);
531 warnx(N_("%s: invalid password"), filename
);
540 static gboolean
xml_import(const gchar
*filename
, gint iter
)
550 guchar shakey
[gcrykeysize
];
561 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1)
562 err(EXIT_FAILURE
, "mlockall()");
565 if (stat(filename
, &st
) == -1) {
566 warn("%s", filename
);
571 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
575 if ((gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
576 send_error(NULL
, gcryerrno
);
577 gcry_cipher_close(gh
);
578 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
582 if ((key
= get_password(N_("New password: "))) == NULL
) {
583 fprintf(stderr
, "%s\n", N_("Invalid password."));
584 gcry_cipher_close(gh
);
588 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
589 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
591 gcry_cipher_close(gh
);
595 if (g_utf8_collate(key
, key2
) != 0) {
596 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
599 gcry_cipher_close(gh
);
605 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
607 warn("%s", filename
);
608 gcry_cipher_close(gh
);
612 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
615 log_write("%s", strerror(ENOMEM
));
616 gcry_cipher_close(gh
);
620 if (read(fd
, xmlbuf
, st
.st_size
) == -1) {
624 gcry_cipher_close(gh
);
626 err(EXIT_FAILURE
, "read()");
630 xmlbuf
[st
.st_size
] = 0;
633 * Make sure the document validates.
635 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
636 log_write("xmlReadDoc()");
640 gcry_cipher_close(gh
);
645 xmlDocDumpMemory(doc
, &xml
, &len
);
649 level
= get_key_file_integer(filename
, "compression_level");
654 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
655 memset(shakey
, 0, sizeof(shakey
));
658 if (zerror
== Z_MEM_ERROR
)
659 warnx("%s", strerror(ENOMEM
));
661 warnx("do_compress() failed");
663 gcry_cipher_close(gh
);
673 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
));
675 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
676 gcry_cipher_close(gh
);
679 memset(shakey
, 0, sizeof(shakey
));
680 warnx("%s", gpg_strerror(error
));
684 memset(shakey
, 0, sizeof(shakey
));
688 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
691 GError
*gerror
= NULL
;
693 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
694 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
697 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
698 g_clear_error(&gerror
);
702 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
703 val
= g_key_file_get_string(keyfileh
, "default", what
, &gerror
);
706 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
707 g_clear_error(&gerror
);
715 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
718 GError
*gerror
= NULL
;
720 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
721 val
= g_key_file_get_integer(keyfileh
, section
, what
, &gerror
);
724 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
725 g_clear_error(&gerror
);
729 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
730 val
= g_key_file_get_integer(keyfileh
, "default", what
, &gerror
);
733 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
734 g_clear_error(&gerror
);
742 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
744 gboolean val
= FALSE
;
745 GError
*gerror
= NULL
;
747 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
748 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &gerror
);
751 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
752 g_clear_error(&gerror
);
756 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
757 val
= g_key_file_get_boolean(keyfileh
, "default", what
, &gerror
);
760 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
761 g_clear_error(&gerror
);
769 gchar
*expand_homedir(gchar
*str
)
774 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
776 return g_strdup(str
);
779 static gchar
*_getline(const gchar
*file
)
782 gchar buf
[LINE_MAX
], *p
;
785 if ((fp
= fopen(file
, "r")) == NULL
) {
790 if ((p
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
791 warnx(N_("%s: empty file?"), file
);
797 if (buf
[strlen(buf
) - 1] == '\n')
798 buf
[strlen(buf
) - 1] = 0;
800 str
= gcry_malloc(strlen(p
) + 1);
801 memcpy(str
, p
, strlen(p
));
803 memset(&buf
, 0, sizeof(buf
));
807 static gboolean
parse_keyfile_key()
814 groups
= g_key_file_get_groups(keyfileh
, &n
);
816 for (p
= groups
; *p
; p
++) {
817 GError
*error
= NULL
;
819 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
820 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
824 warnx("%s", error
->message
);
825 g_clear_error(&error
);
831 do_cache_push(*p
, str
);
837 warnx("%s", error
->message
);
838 g_clear_error(&error
);
842 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
844 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
848 warnx("%s", error
->message
);
849 g_clear_error(&error
);
855 t
= expand_homedir(file
);
859 if ((str
= _getline(file
)) == NULL
) {
865 do_cache_push(*p
, str
);
871 warnx("%s", error
->message
);
872 g_clear_error(&error
);
880 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
885 const gchar
*p
= filename
;
893 if (valid_filename(p
) == FALSE
) {
894 warnx(N_("%s: invalid characters in filename"), p
);
898 md5file
= gcry_malloc(16);
899 key
= gcry_malloc(gcrykeysize
);
900 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
902 if (cache_iscached(md5file
) == TRUE
) {
903 warnx(N_("%s: file already cached, skipping"), p
);
909 if (access(p
, R_OK
|W_OK
) != 0) {
913 if (errno
!= ENOENT
) {
923 if (get_input(p
, key
) == FALSE
) {
930 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
932 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
933 warnx(N_("%s: invalid password, skipping"), filename
);
940 if (cache_add_file(md5file
, key
) == FALSE
) {
941 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
947 timeout
= get_key_file_integer(p
, "cache_timeout");
948 cache_set_timeout(md5file
, timeout
);
949 warnx(N_("%s: file added to the cache"), filename
);
955 int main(int argc
, char *argv
[])
958 struct sockaddr_un addr
;
959 struct passwd
*pw
= getpwuid(getuid());
961 #ifndef MMAP_ANONYMOUS_SHARED
962 gchar shm_path
[PATH_MAX
];
964 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
965 gchar
*socketarg
= NULL
;
966 gchar
*datadir
= NULL
;
970 gchar
**cache_push
= NULL
;
972 gchar
*import
= NULL
;
973 gint default_timeout
;
974 gint rcfile_spec
= 0;
975 gint estatus
= EXIT_FAILURE
;
977 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
980 gboolean secure
= FALSE
;
984 pthread_mutexattr_t cache_mutex_attr
;
985 pthread_mutex_t cache_mutex
;
987 #ifdef HAVE_SETRLIMIT
990 rl
.rlim_cur
= rl
.rlim_max
= 0;
992 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
993 err(EXIT_FAILURE
, "setrlimit()");
998 setlocale(LC_ALL
, "");
999 bindtextdomain("pwmd", LOCALEDIR
);
1004 g_mem_set_vtable(&mtable
);
1006 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
1008 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1009 err(EXIT_FAILURE
, "%s", buf
);
1011 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
1013 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1014 err(EXIT_FAILURE
, "%s", buf
);
1016 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1018 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1019 err(EXIT_FAILURE
, "sysconf()");
1021 cache_size
= page_size
;
1023 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1036 rcfile
= g_strdup(optarg
);
1040 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1048 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1051 if (g_key_file_has_key(keyfileh
, "default", "iterations", NULL
) == TRUE
)
1052 iter
= g_key_file_get_integer(keyfileh
, "default", "iterations", NULL
);
1057 opt
= xml_import(import
, iter
);
1058 g_key_file_free(keyfileh
);
1060 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1063 g_key_file_set_list_separator(keyfileh
, ',');
1065 if ((p
= g_key_file_get_string(keyfileh
, "default", "socket_path", NULL
)) == NULL
)
1066 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1070 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1072 socketarg
= g_strdup(buf
);
1077 if ((p
= g_key_file_get_string(keyfileh
, "default", "data_directory", NULL
)) == NULL
)
1078 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1080 datadir
= expand_homedir(p
);
1083 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "default", "disable_list_and_dump", NULL
) == TRUE
) {
1084 n
= g_key_file_get_boolean(keyfileh
, "default", "disable_list_and_dump", NULL
);
1085 disable_list_and_dump
= n
;
1088 disable_list_and_dump
= secure
;
1090 if (g_key_file_has_key(keyfileh
, "default", "cache_timeout", NULL
) == TRUE
)
1091 default_timeout
= g_key_file_get_integer(keyfileh
, "default", "cache_timeout", NULL
);
1093 default_timeout
= -1;
1095 if (g_key_file_has_key(keyfileh
, "default", "cache_size", NULL
) == TRUE
) {
1096 cache_size
= g_key_file_get_integer(keyfileh
, "default", "cache_size", NULL
);
1098 if (cache_size
< page_size
|| cache_size
% page_size
)
1099 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1102 if (g_key_file_has_key(keyfileh
, "default", "log_path", NULL
) == TRUE
) {
1103 if (g_key_file_has_key(keyfileh
, "default", "enable_logging", NULL
) == TRUE
) {
1104 n
= g_key_file_get_boolean(keyfileh
, "default", "enable_logging", NULL
);
1107 p
= g_key_file_get_string(keyfileh
, "default", "log_path", NULL
);
1111 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1113 logfile
= g_strdup(buf
);
1121 if (g_key_file_has_key(keyfileh
, "default", "cache_push", NULL
) == TRUE
)
1122 cache_push
= g_key_file_get_string_list(keyfileh
, "default", "cache_push", NULL
, NULL
);
1124 if (argc
!= optind
) {
1126 ptotal
= g_strv_length(cache_push
);
1128 for (; optind
< argc
; optind
++) {
1129 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1130 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1134 if (strchr(socketarg
, '/') == NULL
) {
1135 socketdir
= g_get_current_dir();
1136 socketname
= g_strdup(socketarg
);
1137 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1140 socketname
= g_strdup(strrchr(socketarg
, '/'));
1142 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1143 socketdir
= g_strdup(socketarg
);
1144 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1147 #ifdef MMAP_ANONYMOUS_SHARED
1148 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1149 #ifdef MMAP_ANONYMOUS
1150 MAP_SHARED
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1152 MAP_SHARED
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1154 err(EXIT_FAILURE
, "mmap()");
1157 snprintf(shm_path
, sizeof(shm_path
), "/pwmd.%i", pw
->pw_uid
);
1159 if ((fd
= shm_open(shm_path
, O_CREAT
|O_RDWR
|O_EXCL
, 0600)) == -1)
1160 err(EXIT_FAILURE
, "shm_open(): %s", shm_path
);
1163 * Should be enough for the file cache.
1165 if (ftruncate(fd
, cache_size
) == -1) {
1166 warn("ftruncate()");
1167 shm_unlink(shm_path
);
1171 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
1172 fd
, 0)) == MAP_FAILED
) {
1174 shm_unlink(shm_path
);
1181 if (mlock(shm_data
, cache_size
) == -1)
1184 memset(shm_data
, 0, cache_size
);
1186 if (chdir(datadir
)) {
1187 warn("%s", datadir
);
1193 if (parse_keyfile_key() == FALSE
)
1199 * Set the cache entry for a file. Prompts for the password.
1202 for (opt
= 0; cache_push
[opt
]; opt
++)
1203 do_cache_push(cache_push
[opt
], NULL
);
1205 g_strfreev(cache_push
);
1206 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1210 * bind() doesn't like the full pathname of the socket or any non alphanum
1211 * characters so change to the directory where the socket is wanted then
1212 * create it then change to datadir.
1214 if (chdir(socketdir
)) {
1215 warn("%s", socketdir
);
1221 if ((sfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1226 addr
.sun_family
= AF_UNIX
;
1227 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1228 g_free(--socketname
);
1230 if (bind(sfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1233 if (errno
== EADDRINUSE
)
1234 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1235 "stale socket. Please remove it manually."), socketpath
);
1241 if (chdir(datadir
)) {
1242 warn("%s", datadir
);
1249 pthread_mutexattr_init(&cache_mutex_attr
);
1250 pthread_mutexattr_settype(&cache_mutex_attr
, PTHREAD_MUTEX_FAST_NP
);
1251 pthread_mutex_init(&cache_mutex
, &cache_mutex_attr
);
1252 pthread_mutexattr_destroy(&cache_mutex_attr
);
1253 cache_mutexp
= shm_data
+ MUTEX_OFFSET
;
1254 memcpy(cache_mutexp
, &cache_mutex
, sizeof(cache_mutex
));
1256 if (listen(sfd
, 0) == -1) {
1261 signal(SIGCHLD
, catchsig
);
1262 signal(SIGTERM
, catchsig
);
1263 signal(SIGINT
, catchsig
);
1264 signal(SIGHUP
, catchsig
);
1265 signal(SIGALRM
, catchsig
);
1266 signal(SIGABRT
, catchsig
);
1267 signal(SIGUSR1
, catchsig
);
1285 log_write("%s %s", PACKAGE_STRING
, N_("started"));
1286 send_cache_status(NULL
);
1289 socklen_t slen
= sizeof(struct sockaddr_un
);
1290 struct sockaddr_un raddr
;
1293 if ((fd
= accept(sfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1297 if (errno
== EAGAIN
)
1300 log_write("accept(): %s", strerror(errno
));
1304 switch ((pid
= fork())) {
1306 log_write("fork(): %s", strerror(errno
));
1316 log_write(N_("new connection: pid=%i"), pid
);
1320 estatus
= EXIT_SUCCESS
;
1323 if (socketpath
&& do_unlink
) {
1328 signal(SIGUSR1
, SIG_IGN
);
1329 g_key_file_free(keyfileh
);
1333 log_write(N_("waiting for all clients to disconnect"));
1336 if (clients
!= n_clients
) {
1337 log_write(N_("%i clients remain"), clients
);
1338 n_clients
= clients
;
1341 select(0, NULL
, NULL
, NULL
, NULL
);
1345 memset(shm_data
, 0, cache_size
);
1347 if (munmap(shm_data
, cache_size
) == -1)
1348 log_write("munmap(): %s", strerror(errno
));
1350 #ifndef MMAP_ANONYMOUS_SHARED
1351 if (shm_unlink(shm_path
) == -1)
1352 log_write("shm_unlink(): %s: %s", shm_path
, strerror(errno
));
1355 if (estatus
== EXIT_SUCCESS
)
1356 log_write(N_("pwmd exiting normally"));