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", "");
81 static void reload_rcfile()
83 log_write(N_("reloading configuration file '%s'"), rcfile
);
84 g_key_file_free(keyfileh
);
85 keyfileh
= parse_rcfile(0);
89 gpg_error_t
send_syserror(assuan_context_t ctx
, int e
)
91 gpg_error_t n
= gpg_error_from_errno(e
);
93 return assuan_set_error(ctx
, n
, gpg_strerror(n
));
96 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
98 gpg_err_code_t n
= gpg_err_code(e
);
99 gpg_error_t code
= gpg_err_make(GPG_ERR_SOURCE_USER_1
, n
);
105 log_write("%s\n", pwmd_strerror(e
));
109 if (n
== EPWMD_LIBXML_ERROR
) {
110 xmlErrorPtr xml_error
= xmlGetLastError();
111 return assuan_set_error(ctx
, code
, xml_error
->message
);
114 return assuan_set_error(ctx
, code
, pwmd_strerror(e
));
117 void log_write(const gchar
*fmt
, ...)
126 if ((!logfile
&& !isatty(STDERR_FILENO
)) || !fmt
)
130 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
138 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
145 tm
= localtime(&now
);
146 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
147 tbuf
[sizeof(tbuf
) - 1] = 0;
148 line
= g_strdup_printf("%s %i %s\n", tbuf
, getpid(), args
);
159 write(fd
, line
, strlen(line
));
164 if (isatty(STDERR_FILENO
)) {
165 fprintf(stderr
, "%s", line
);
173 static void catchsig(gint sig
)
177 if (sig
!= SIGALRM
&& sig
!= SIGCHLD
)
178 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
185 cache_clear(NULL
, 2);
191 cache_adjust_timer();
195 waitpid(-1, &status
, 0);
197 if (WIFEXITED(status
) || WIFSIGNALED(status
))
202 log_write(N_("clearing file cache"));
203 cache_clear(NULL
, 2);
206 signal(SIGALRM
, SIG_IGN
);
208 shutdown(sfd
, SHUT_RDWR
);
214 static void usage(gchar
*pn
)
217 "Usage: %s [-hvDb] [-f <rcfile>] [-I <filename>] [file1] [...]\n"
218 " -b run as a background process\n"
219 " -f load the specified rcfile (~/.pwmd/config)\n"
220 " -I import an XML file and write the encrypted data to stdout\n"
221 " -D disable use of the LIST and DUMP commands\n"
223 " -h this help text\n"
229 static int gcry_SecureCheck(const void *ptr
)
235 static void setup_gcrypt()
237 gcry_check_version(NULL
);
240 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
244 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
246 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
248 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
249 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
252 static void child_catchsig(int sig
)
254 assuan_context_t ctx
= global_client
->ctx
;
259 g_free(global_client
);
260 assuan_deinit_server(ctx
);
261 log_write(N_("exiting"));
275 * Called every time a connection is made.
277 static void doit(int fd
)
279 assuan_context_t ctx
;
281 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
283 signal(SIGCHLD
, SIG_DFL
);
284 signal(SIGHUP
, SIG_IGN
);
285 signal(SIGINT
, SIG_IGN
);
286 signal(SIGALRM
, SIG_IGN
);
287 signal(SIGTERM
, child_catchsig
);
288 signal(SIGABRT
, child_catchsig
);
290 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
292 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
293 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
298 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
299 log_write("mlockall(): %s", strerror(errno
));
304 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
307 log_write("%s", gpg_strerror(rc
));
311 assuan_set_pointer(ctx
, cl
);
312 rc
= register_commands(ctx
);
315 log_write("%s", gpg_strerror(rc
));
323 * It would be nice if there were an assuan_register_pre_cmd_notify().
324 * That way we can see if the file has been modified before calling the
327 rc
= assuan_accept(ctx
);
330 log_write("%s", gpg_strerror(rc
));
334 rc
= assuan_process(ctx
);
337 log_write("%s", gpg_strerror(rc
));
340 if (cl
->freed
== FALSE
)
344 assuan_deinit_server(ctx
);
345 log_write(N_("exiting"));
350 * Make sure all settings are set to either the specified setting or a
353 static void set_rcfile_defaults(GKeyFile
*kf
)
357 if (g_key_file_has_key(kf
, "default", "socket_path", NULL
) == FALSE
) {
358 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
359 g_key_file_set_string(kf
, "default", "socket_path", buf
);
362 if (g_key_file_has_key(kf
, "default", "data_directory", NULL
) == FALSE
) {
363 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
364 g_key_file_set_string(kf
, "default", "data_directory", buf
);
367 if (g_key_file_has_key(kf
, "default", "log_path", NULL
) == FALSE
) {
368 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
369 g_key_file_set_string(kf
, "default", "log_path", buf
);
372 if (g_key_file_has_key(kf
, "default", "enable_logging", NULL
) == FALSE
)
373 g_key_file_set_boolean(kf
, "default", "enable_logging", FALSE
);
375 if (g_key_file_has_key(kf
, "default", "cache_size", NULL
) == FALSE
)
376 g_key_file_set_integer(kf
, "default", "cache_size", cache_size
);
379 if (g_key_file_has_key(kf
, "default", "disable_mlockall", NULL
) == FALSE
)
380 g_key_file_set_boolean(kf
, "default", "disable_mlockall", TRUE
);
383 if (g_key_file_has_key(kf
, "default", "cache_timeout", NULL
) == FALSE
)
384 g_key_file_set_integer(kf
, "default", "cache_timeout", -1);
386 if (g_key_file_has_key(kf
, "default", "iterations", NULL
) == FALSE
)
387 g_key_file_set_integer(kf
, "default", "iterations", 0);
389 if (g_key_file_has_key(kf
, "default", "disable_list_and_dump", NULL
) == FALSE
)
390 g_key_file_set_boolean(kf
, "default", "disable_list_and_dump", FALSE
);
392 if (g_key_file_has_key(kf
, "default", "iteration_progress", NULL
) == FALSE
)
393 g_key_file_set_integer(kf
, "default", "iteration_progress", 0);
395 if (g_key_file_has_key(kf
, "default", "compression_level", NULL
) == FALSE
)
396 g_key_file_set_integer(kf
, "default", "compression_level", 6);
398 if (g_key_file_has_key(kf
, "default", "recursion_depth", NULL
) == FALSE
)
399 g_key_file_set_integer(kf
, "default", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
402 if (g_key_file_has_key(kf
, "default", "zlib_bufsize", NULL
) == FALSE
)
403 g_key_file_set_integer(kf
, "default", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
405 zlib_bufsize
= g_key_file_get_integer(kf
, "default", "zlib_bufsize", NULL
);
408 max_recursion_depth
= g_key_file_get_integer(kf
, "default", "recursion_depth", NULL
);
409 disable_list_and_dump
= g_key_file_get_boolean(kf
, "default", "disable_list_and_dump", NULL
);
412 disable_mlock
= g_key_file_get_boolean(kf
, "default", "disable_mlockall", NULL
);
416 static GKeyFile
*parse_rcfile(int cmdline
)
418 GKeyFile
*kf
= g_key_file_new();
419 GError
*error
= NULL
;
421 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
422 log_write("%s: %s", rcfile
, error
->message
);
427 if (error
->code
== G_FILE_ERROR_NOENT
) {
428 g_clear_error(&error
);
429 set_rcfile_defaults(kf
);
433 g_clear_error(&error
);
437 set_rcfile_defaults(kf
);
442 static gchar
*get_password(const gchar
*prompt
)
444 gchar buf
[LINE_MAX
], *p
;
445 struct termios told
, tnew
;
448 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
449 err(EXIT_FAILURE
, "tcgetattr()");
451 memcpy(&tnew
, &told
, sizeof(struct termios
));
452 tnew
.c_lflag
&= ~(ECHO
);
453 tnew
.c_lflag
|= ICANON
|ECHONL
;
455 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
456 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
457 err(EXIT_FAILURE
, "tcsetattr()");
460 fprintf(stderr
, "%s", prompt
);
462 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
463 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
467 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
468 p
[strlen(p
) - 1] = 0;
473 key
= gcry_malloc(strlen(p
) + 1);
474 sprintf(key
, "%s", p
);
475 memset(&buf
, 0, sizeof(buf
));
479 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
485 if ((fd
= open_file(filename
, &st
)) == -1) {
486 warn("%s", filename
);
490 if (st
.st_size
== 0) {
491 warnx(N_("%s: skipping empty file"), filename
);
496 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
498 return error
? FALSE
: TRUE
;
501 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
507 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
510 if ((password
= get_password(prompt
)) == NULL
) {
511 warnx(N_("%s: skipping file"), filename
);
516 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
519 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
521 warnx(N_("%s: invalid password, skipping"), filename
);
526 warnx(N_("%s: invalid password"), filename
);
535 static gboolean
xml_import(const gchar
*filename
, gint iter
)
545 guchar shakey
[gcrykeysize
];
555 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1)
556 err(EXIT_FAILURE
, "mlockall()");
559 if (stat(filename
, &st
) == -1) {
560 warn("%s", filename
);
565 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
569 if ((gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
570 send_error(NULL
, gcryerrno
);
571 gcry_cipher_close(gh
);
572 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
576 if ((key
= get_password(N_("New password: "))) == NULL
) {
577 fprintf(stderr
, "%s\n", N_("Invalid password."));
578 gcry_cipher_close(gh
);
582 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
583 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
585 gcry_cipher_close(gh
);
589 if (g_utf8_collate(key
, key2
) != 0) {
590 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
593 gcry_cipher_close(gh
);
599 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
601 warn("%s", filename
);
602 gcry_cipher_close(gh
);
606 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
609 log_write("%s", strerror(ENOMEM
));
610 gcry_cipher_close(gh
);
614 read(fd
, xmlbuf
, st
.st_size
);
616 xmlbuf
[st
.st_size
] = 0;
619 * Make sure the document validates.
621 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
622 log_write("xmlReadDoc()");
626 gcry_cipher_close(gh
);
631 xmlDocDumpMemory(doc
, &xml
, &len
);
635 level
= get_key_file_integer(filename
, "compression_level");
640 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
641 memset(shakey
, 0, sizeof(shakey
));
644 if (zerror
== Z_MEM_ERROR
)
645 warnx("%s", strerror(ENOMEM
));
647 warnx("do_compress() failed");
649 gcry_cipher_close(gh
);
660 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
));
662 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
663 gcry_cipher_close(gh
);
666 memset(shakey
, 0, sizeof(shakey
));
667 warnx("%s", gpg_strerror(error
));
671 memset(shakey
, 0, sizeof(shakey
));
675 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
678 GError
*gerror
= NULL
;
680 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
681 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
684 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
685 g_clear_error(&gerror
);
689 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
690 val
= g_key_file_get_string(keyfileh
, "default", what
, &gerror
);
693 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
694 g_clear_error(&gerror
);
702 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
705 GError
*gerror
= NULL
;
707 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
708 val
= g_key_file_get_integer(keyfileh
, section
, what
, &gerror
);
711 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
712 g_clear_error(&gerror
);
716 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
717 val
= g_key_file_get_integer(keyfileh
, "default", what
, &gerror
);
720 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
721 g_clear_error(&gerror
);
729 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
731 gboolean val
= FALSE
;
732 GError
*gerror
= NULL
;
734 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
735 val
= g_key_file_get_boolean(keyfileh
, section
, what
, &gerror
);
738 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
739 g_clear_error(&gerror
);
743 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
744 val
= g_key_file_get_boolean(keyfileh
, "default", what
, &gerror
);
747 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
748 g_clear_error(&gerror
);
756 gchar
*expand_homedir(gchar
*str
)
761 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
763 return g_strdup(str
);
766 static gchar
*_getline(const gchar
*file
)
769 gchar buf
[LINE_MAX
], *p
;
772 if ((fp
= fopen(file
, "r")) == NULL
) {
777 if ((p
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
778 warnx(N_("%s: empty file?"), file
);
784 if (buf
[strlen(buf
) - 1] == '\n')
785 buf
[strlen(buf
) - 1] = 0;
787 str
= gcry_malloc(strlen(p
) + 1);
788 memcpy(str
, p
, strlen(p
));
790 memset(&buf
, 0, sizeof(buf
));
794 static gboolean
parse_keyfile_key()
801 groups
= g_key_file_get_groups(keyfileh
, &n
);
803 for (p
= groups
; *p
; p
++) {
804 GError
*error
= NULL
;
806 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
807 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
811 warnx("%s", error
->message
);
812 g_clear_error(&error
);
818 do_cache_push(*p
, str
);
824 warnx("%s", error
->message
);
825 g_clear_error(&error
);
829 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
831 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
835 warnx("%s", error
->message
);
836 g_clear_error(&error
);
842 t
= expand_homedir(file
);
846 if ((str
= _getline(file
)) == NULL
) {
852 do_cache_push(*p
, str
);
858 warnx("%s", error
->message
);
859 g_clear_error(&error
);
867 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
872 const gchar
*p
= filename
;
880 if (valid_filename(p
) == FALSE
) {
881 warnx(N_("%s: invalid characters in filename"), p
);
885 md5file
= gcry_malloc(16);
886 key
= gcry_malloc(gcrykeysize
);
887 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
889 if (cache_iscached(md5file
) == TRUE
) {
890 warnx(N_("%s: file already cached, skipping"), p
);
896 if (access(p
, R_OK
|W_OK
) != 0) {
900 if (errno
!= ENOENT
) {
910 if (get_input(p
, key
) == FALSE
) {
917 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
919 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
920 warnx(N_("%s: invalid password, skipping"), filename
);
927 if (cache_add_file(md5file
, key
) == FALSE
) {
928 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
934 timeout
= get_key_file_integer(p
, "cache_timeout");
935 cache_set_timeout(md5file
, timeout
);
936 warnx(N_("%s: file added to the cache"), filename
);
942 int main(int argc
, char *argv
[])
945 struct sockaddr_un addr
;
946 struct passwd
*pw
= getpwuid(getuid());
948 #ifndef MMAP_ANONYMOUS_SHARED
949 gchar shm_path
[PATH_MAX
];
951 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
952 gchar
*socketarg
= NULL
;
953 gchar
*datadir
= NULL
;
957 gchar
**cache_push
= NULL
;
959 gchar
*import
= NULL
;
960 gint default_timeout
;
961 gint rcfile_spec
= 0;
962 gint estatus
= EXIT_FAILURE
;
964 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
967 gboolean secure
= FALSE
;
972 #ifdef HAVE_SETRLIMIT
975 rl
.rlim_cur
= rl
.rlim_max
= 0;
977 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
978 err(EXIT_FAILURE
, "setrlimit()");
983 setlocale(LC_ALL
, "");
984 bindtextdomain("pwmd", LOCALEDIR
);
989 g_mem_set_vtable(&mtable
);
991 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
993 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
994 err(EXIT_FAILURE
, "%s", buf
);
996 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
998 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
999 err(EXIT_FAILURE
, "%s", buf
);
1001 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
1003 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
1004 err(EXIT_FAILURE
, "sysconf()");
1006 cache_size
= page_size
;
1008 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1021 rcfile
= g_strdup(optarg
);
1025 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1033 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1036 if (g_key_file_has_key(keyfileh
, "default", "iterations", NULL
) == TRUE
)
1037 iter
= g_key_file_get_integer(keyfileh
, "default", "iterations", NULL
);
1042 opt
= xml_import(import
, iter
);
1043 g_key_file_free(keyfileh
);
1045 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1048 g_key_file_set_list_separator(keyfileh
, ',');
1050 if ((p
= g_key_file_get_string(keyfileh
, "default", "socket_path", NULL
)) == NULL
)
1051 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1055 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1057 socketarg
= g_strdup(buf
);
1062 if ((p
= g_key_file_get_string(keyfileh
, "default", "data_directory", NULL
)) == NULL
)
1063 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1065 datadir
= expand_homedir(p
);
1068 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "default", "disable_list_and_dump", NULL
) == TRUE
) {
1069 n
= g_key_file_get_boolean(keyfileh
, "default", "disable_list_and_dump", NULL
);
1070 disable_list_and_dump
= n
;
1073 disable_list_and_dump
= secure
;
1075 if (g_key_file_has_key(keyfileh
, "default", "cache_timeout", NULL
) == TRUE
)
1076 default_timeout
= g_key_file_get_integer(keyfileh
, "default", "cache_timeout", NULL
);
1078 default_timeout
= -1;
1080 if (g_key_file_has_key(keyfileh
, "default", "cache_size", NULL
) == TRUE
) {
1081 cache_size
= g_key_file_get_integer(keyfileh
, "default", "cache_size", NULL
);
1083 if (cache_size
< page_size
|| cache_size
% page_size
)
1084 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1087 if (g_key_file_has_key(keyfileh
, "default", "log_path", NULL
) == TRUE
) {
1088 if (g_key_file_has_key(keyfileh
, "default", "enable_logging", NULL
) == TRUE
) {
1089 n
= g_key_file_get_boolean(keyfileh
, "default", "enable_logging", NULL
);
1092 p
= g_key_file_get_string(keyfileh
, "default", "log_path", NULL
);
1096 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1098 logfile
= g_strdup(buf
);
1106 if (g_key_file_has_key(keyfileh
, "default", "cache_push", NULL
) == TRUE
)
1107 cache_push
= g_key_file_get_string_list(keyfileh
, "default", "cache_push", NULL
, NULL
);
1109 if (argc
!= optind
) {
1111 ptotal
= g_strv_length(cache_push
);
1113 for (; optind
< argc
; optind
++) {
1114 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1115 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1119 if (strchr(socketarg
, '/') == NULL
) {
1120 socketdir
= g_get_current_dir();
1121 socketname
= g_strdup(socketarg
);
1122 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1125 socketname
= g_strdup(strrchr(socketarg
, '/'));
1127 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1128 socketdir
= g_strdup(socketarg
);
1129 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1132 #ifdef MMAP_ANONYMOUS_SHARED
1133 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1134 #ifdef MMAP_ANONYMOUS
1135 MAP_SHARED
|MAP_ANONYMOUS
, -1, 0)) == MAP_FAILED
) {
1137 MAP_SHARED
|MAP_ANON
, -1, 0)) == MAP_FAILED
) {
1139 err(EXIT_FAILURE
, "mmap()");
1142 snprintf(shm_path
, sizeof(shm_path
), "/pwmd.%i", pw
->pw_uid
);
1144 if ((fd
= shm_open(shm_path
, O_CREAT
|O_RDWR
|O_EXCL
, 0600)) == -1)
1145 err(EXIT_FAILURE
, "shm_open(): %s", shm_path
);
1148 * Should be enough for the file cache.
1150 if (ftruncate(fd
, cache_size
) == -1) {
1151 warn("ftruncate()");
1152 shm_unlink(shm_path
);
1156 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
1157 fd
, 0)) == MAP_FAILED
) {
1159 shm_unlink(shm_path
);
1166 if (mlock(shm_data
, cache_size
) == -1)
1169 memset(shm_data
, 0, cache_size
);
1171 if (chdir(datadir
)) {
1172 warn("%s", datadir
);
1178 if (parse_keyfile_key() == FALSE
)
1184 * Set the cache entry for a file. Prompts for the password.
1187 for (opt
= 0; cache_push
[opt
]; opt
++)
1188 do_cache_push(cache_push
[opt
], NULL
);
1190 g_strfreev(cache_push
);
1191 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1195 * bind() doesn't like the full pathname of the socket or any non alphanum
1196 * characters so change to the directory where the socket is wanted then
1197 * create it then change to datadir.
1199 if (chdir(socketdir
)) {
1200 warn("%s", socketdir
);
1206 if ((sfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1211 addr
.sun_family
= AF_UNIX
;
1212 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1213 g_free(--socketname
);
1215 if (bind(sfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1218 if (errno
== EADDRINUSE
)
1219 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1220 "stale socket. Please remove it manually."), socketpath
);
1226 if (chdir(datadir
)) {
1227 warn("%s", datadir
);
1235 if (listen(sfd
, 0) == -1) {
1240 signal(SIGCHLD
, catchsig
);
1241 signal(SIGTERM
, catchsig
);
1242 signal(SIGINT
, catchsig
);
1243 signal(SIGHUP
, catchsig
);
1244 signal(SIGALRM
, catchsig
);
1245 signal(SIGABRT
, catchsig
);
1246 signal(SIGUSR1
, catchsig
);
1264 log_write("%s %s", PACKAGE_STRING
, N_("started"));
1265 send_cache_status(NULL
);
1268 socklen_t slen
= sizeof(struct sockaddr_un
);
1269 struct sockaddr_un raddr
;
1272 if ((fd
= accept(sfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1276 if (errno
== EAGAIN
)
1279 log_write("accept(): %s", strerror(errno
));
1283 switch ((pid
= fork())) {
1285 log_write("fork(): %s", strerror(errno
));
1295 log_write(N_("new connection: pid=%i"), pid
);
1299 estatus
= EXIT_SUCCESS
;
1302 if (socketpath
&& do_unlink
) {
1307 signal(SIGUSR1
, SIG_IGN
);
1308 g_key_file_free(keyfileh
);
1312 log_write(N_("waiting for all clients to disconnect"));
1315 if (clients
!= n_clients
) {
1316 log_write(N_("%i clients remain"), clients
);
1317 n_clients
= clients
;
1320 select(0, NULL
, NULL
, NULL
, NULL
);
1324 memset(shm_data
, 0, cache_size
);
1326 if (munmap(shm_data
, cache_size
) == -1)
1327 log_write("munmap(): %s", strerror(errno
));
1329 #ifndef MMAP_ANONYMOUS_SHARED
1330 if (shm_unlink(shm_path
) == -1)
1331 log_write("shm_unlink(): %s: %s", shm_path
, strerror(errno
));
1334 if (estatus
== EXIT_SUCCESS
)
1335 log_write(N_("pwmd exiting normally"));