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 reload_rcfile()
67 log_write(N_("reloading configuration file '%s'"), rcfile
);
68 g_key_file_free(keyfileh
);
69 keyfileh
= parse_rcfile(0);
72 gpg_error_t
send_syserror(assuan_context_t ctx
, int e
)
74 gpg_error_t n
= gpg_error_from_errno(e
);
76 return assuan_set_error(ctx
, n
, gpg_strerror(n
));
79 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
81 gpg_err_code_t n
= gpg_err_code(e
);
82 gpg_error_t code
= gpg_err_make(GPG_ERR_SOURCE_USER_1
, n
);
88 log_write("%s\n", pwmd_strerror(e
));
92 if (n
== EPWMD_LIBXML_ERROR
) {
93 xmlErrorPtr xml_error
= xmlGetLastError();
94 return assuan_set_error(ctx
, code
, xml_error
->message
);
97 return assuan_set_error(ctx
, code
, pwmd_strerror(e
));
100 void log_write(const gchar
*fmt
, ...)
109 if ((!logfile
&& !isatty(STDERR_FILENO
)) || !fmt
)
113 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
121 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
128 tm
= localtime(&now
);
129 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
130 tbuf
[sizeof(tbuf
) - 1] = 0;
131 line
= g_strdup_printf("%s %i %s\n", tbuf
, getpid(), args
);
142 write(fd
, line
, strlen(line
));
147 if (isatty(STDERR_FILENO
)) {
148 fprintf(stderr
, "%s", line
);
156 static void catchsig(gint sig
)
160 if (sig
!= SIGALRM
&& sig
!= SIGCHLD
)
161 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
168 cache_clear(NULL
, 2);
174 cache_adjust_timer();
178 waitpid(-1, &status
, 0);
180 if (WIFEXITED(status
) || WIFSIGNALED(status
))
185 log_write(N_("clearing file cache"));
186 cache_clear(NULL
, 2);
189 signal(SIGALRM
, SIG_IGN
);
191 shutdown(sfd
, SHUT_RDWR
);
197 static void usage(gchar
*pn
)
200 "Usage: %s [-hvDb] [-f <rcfile>] [-I <filename>] [file1] [...]\n"
201 " -b run as a background process\n"
202 " -f load the specified rcfile (~/.pwmd/config)\n"
203 " -I import an XML file and write the encrypted data to stdout\n"
204 " -D disable use of the LIST and DUMP commands\n"
206 " -h this help text\n"
212 static int gcry_SecureCheck(const void *ptr
)
218 static void setup_gcrypt()
220 gcry_check_version(NULL
);
223 gcry_set_allocation_handler(xmalloc
, xmalloc
, gcry_SecureCheck
, xrealloc
,
227 if (gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_TEST_ALGO
, NULL
,
229 errx(EXIT_FAILURE
, N_("Required AES cipher not supported by libgcrypt."));
231 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_KEYLEN
, NULL
, &gcrykeysize
);
232 gcry_cipher_algo_info(GCRY_CIPHER_AES256
, GCRYCTL_GET_BLKLEN
, NULL
, &gcryblocksize
);
235 static void child_catchsig(int sig
)
237 assuan_context_t ctx
= global_client
->ctx
;
242 g_free(global_client
);
243 assuan_deinit_server(ctx
);
244 log_write(N_("exiting"));
258 * Called every time a connection is made.
260 static void doit(int fd
)
262 assuan_context_t ctx
;
264 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
266 signal(SIGCHLD
, SIG_DFL
);
267 signal(SIGHUP
, SIG_IGN
);
268 signal(SIGINT
, SIG_IGN
);
269 signal(SIGALRM
, SIG_IGN
);
270 signal(SIGTERM
, child_catchsig
);
271 signal(SIGABRT
, child_catchsig
);
273 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
275 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
276 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
281 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
282 log_write("mlockall(): %s", strerror(errno
));
287 rc
= assuan_init_socket_server_ext(&ctx
, fd
, 2);
290 log_write("%s", gpg_strerror(rc
));
294 assuan_set_pointer(ctx
, cl
);
295 rc
= register_commands(ctx
);
298 log_write("%s", gpg_strerror(rc
));
306 * It would be nice if there were an assuan_register_pre_cmd_notify().
307 * That way we can see if the file has been modified before calling the
310 rc
= assuan_accept(ctx
);
313 log_write("%s", gpg_strerror(rc
));
317 rc
= assuan_process(ctx
);
320 log_write("%s", gpg_strerror(rc
));
323 if (cl
->freed
== FALSE
)
327 assuan_deinit_server(ctx
);
328 log_write(N_("exiting"));
333 * Make sure all settings are set to either the specified setting or a
336 static void set_rcfile_defaults(GKeyFile
*kf
)
340 if (g_key_file_has_key(kf
, "default", "socket_path", NULL
) == FALSE
) {
341 snprintf(buf
, sizeof(buf
), "~/.pwmd/socket");
342 g_key_file_set_string(kf
, "default", "socket_path", buf
);
345 if (g_key_file_has_key(kf
, "default", "data_directory", NULL
) == FALSE
) {
346 snprintf(buf
, sizeof(buf
), "~/.pwmd/data");
347 g_key_file_set_string(kf
, "default", "data_directory", buf
);
350 if (g_key_file_has_key(kf
, "default", "log_path", NULL
) == FALSE
) {
351 snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
352 g_key_file_set_string(kf
, "default", "log_path", buf
);
355 if (g_key_file_has_key(kf
, "default", "enable_logging", NULL
) == FALSE
)
356 g_key_file_set_boolean(kf
, "default", "enable_logging", FALSE
);
358 if (g_key_file_has_key(kf
, "default", "cache_size", NULL
) == FALSE
)
359 g_key_file_set_integer(kf
, "default", "cache_size", cache_size
);
362 if (g_key_file_has_key(kf
, "default", "disable_mlockall", NULL
) == FALSE
)
363 g_key_file_set_boolean(kf
, "default", "disable_mlockall", TRUE
);
366 if (g_key_file_has_key(kf
, "default", "cache_timeout", NULL
) == FALSE
)
367 g_key_file_set_integer(kf
, "default", "cache_timeout", -1);
369 if (g_key_file_has_key(kf
, "default", "iterations", NULL
) == FALSE
)
370 g_key_file_set_integer(kf
, "default", "iterations", 0);
372 if (g_key_file_has_key(kf
, "default", "disable_list_and_dump", NULL
) == FALSE
)
373 g_key_file_set_boolean(kf
, "default", "disable_list_and_dump", FALSE
);
375 if (g_key_file_has_key(kf
, "default", "iteration_progress", NULL
) == FALSE
)
376 g_key_file_set_integer(kf
, "default", "iteration_progress", 0);
378 if (g_key_file_has_key(kf
, "default", "compression_level", NULL
) == FALSE
)
379 g_key_file_set_integer(kf
, "default", "compression_level", 6);
381 if (g_key_file_has_key(kf
, "default", "recursion_depth", NULL
) == FALSE
)
382 g_key_file_set_integer(kf
, "default", "recursion_depth", DEFAULT_RECURSION_DEPTH
);
385 if (g_key_file_has_key(kf
, "default", "zlib_bufsize", NULL
) == FALSE
)
386 g_key_file_set_integer(kf
, "default", "zlib_bufsize", DEFAULT_ZLIB_BUFSIZE
);
388 zlib_bufsize
= g_key_file_get_integer(kf
, "default", "zlib_bufsize", NULL
);
391 max_recursion_depth
= g_key_file_get_integer(kf
, "default", "recursion_depth", NULL
);
392 disable_list_and_dump
= g_key_file_get_boolean(kf
, "default", "disable_list_and_dump", NULL
);
395 disable_mlock
= g_key_file_get_boolean(kf
, "default", "disable_mlockall", NULL
);
399 static GKeyFile
*parse_rcfile(int cmdline
)
401 GKeyFile
*kf
= g_key_file_new();
402 GError
*error
= NULL
;
404 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &error
) == FALSE
) {
405 log_write("%s: %s", rcfile
, error
->message
);
410 if (error
->code
== G_FILE_ERROR_NOENT
) {
411 g_clear_error(&error
);
412 set_rcfile_defaults(kf
);
416 g_clear_error(&error
);
420 set_rcfile_defaults(kf
);
425 static gchar
*get_password(const gchar
*prompt
)
427 gchar buf
[LINE_MAX
], *p
;
428 struct termios told
, tnew
;
431 if (tcgetattr(STDIN_FILENO
, &told
) == -1)
432 err(EXIT_FAILURE
, "tcgetattr()");
434 memcpy(&tnew
, &told
, sizeof(struct termios
));
435 tnew
.c_lflag
&= ~(ECHO
);
436 tnew
.c_lflag
|= ICANON
|ECHONL
;
438 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
439 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
440 err(EXIT_FAILURE
, "tcsetattr()");
443 fprintf(stderr
, "%s", prompt
);
445 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
446 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
450 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
451 p
[strlen(p
) - 1] = 0;
456 key
= gcry_malloc(strlen(p
) + 1);
457 sprintf(key
, "%s", p
);
458 memset(&buf
, 0, sizeof(buf
));
462 static gboolean
do_try_xml_decrypt(const gchar
*filename
, guchar
*key
)
468 if ((fd
= open_file(filename
, &st
)) == -1) {
469 warn("%s", filename
);
473 if (st
.st_size
== 0) {
474 warnx(N_("%s: skipping empty file"), filename
);
479 error
= try_xml_decrypt(NULL
, fd
, st
, key
);
481 return error
? FALSE
: TRUE
;
484 static gboolean
get_input(const gchar
*filename
, guchar
*key
)
490 prompt
= g_strdup_printf(N_("Password for '%s': "), filename
);
493 if ((password
= get_password(prompt
)) == NULL
) {
494 warnx(N_("%s: skipping file"), filename
);
499 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
502 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
504 warnx(N_("%s: invalid password, skipping"), filename
);
509 warnx(N_("%s: invalid password"), filename
);
518 static gboolean
xml_import(const gchar
*filename
, gint iter
)
528 guchar shakey
[gcrykeysize
];
538 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1)
539 err(EXIT_FAILURE
, "mlockall()");
542 if (stat(filename
, &st
) == -1) {
543 warn("%s", filename
);
548 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
552 if ((gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
553 send_error(NULL
, gcryerrno
);
554 gcry_cipher_close(gh
);
555 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
559 if ((key
= get_password(N_("New password: "))) == NULL
) {
560 fprintf(stderr
, "%s\n", N_("Invalid password."));
561 gcry_cipher_close(gh
);
565 if ((key2
= get_password(N_("Verify password: "))) == NULL
) {
566 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
568 gcry_cipher_close(gh
);
572 if (g_utf8_collate(key
, key2
) != 0) {
573 fprintf(stderr
, "%s\n", N_("Passwords do not match."));
576 gcry_cipher_close(gh
);
582 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
584 warn("%s", filename
);
585 gcry_cipher_close(gh
);
589 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
592 log_write("%s", strerror(ENOMEM
));
593 gcry_cipher_close(gh
);
597 read(fd
, xmlbuf
, st
.st_size
);
599 xmlbuf
[st
.st_size
] = 0;
602 * Make sure the document validates.
604 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
605 log_write("xmlReadDoc()");
609 gcry_cipher_close(gh
);
614 xmlDocDumpMemory(doc
, &xml
, &len
);
618 level
= get_key_file_integer(filename
, "compression_level");
623 if (do_compress(NULL
, level
, xml
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
624 memset(shakey
, 0, sizeof(shakey
));
627 if (zerror
== Z_MEM_ERROR
)
628 warnx("%s", strerror(ENOMEM
));
630 warnx("do_compress() failed");
632 gcry_cipher_close(gh
);
643 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, key
, strlen(key
));
645 error
= do_xml_encrypt(NULL
, gh
, NULL
, xml
, len
, shakey
, iter
);
646 gcry_cipher_close(gh
);
649 memset(shakey
, 0, sizeof(shakey
));
650 warnx("%s", gpg_strerror(error
));
654 memset(shakey
, 0, sizeof(shakey
));
658 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
661 GError
*gerror
= NULL
;
663 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
664 val
= g_key_file_get_string(keyfileh
, section
, what
, &gerror
);
667 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
668 g_clear_error(&gerror
);
672 if (g_key_file_has_key(keyfileh
, "default", what
, NULL
) == TRUE
) {
673 val
= g_key_file_get_string(keyfileh
, "default", what
, &gerror
);
676 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
677 g_clear_error(&gerror
);
685 gint
get_key_file_integer(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_integer(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_integer(keyfileh
, "default", what
, &gerror
);
703 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
704 g_clear_error(&gerror
);
712 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
714 gboolean val
= FALSE
;
715 GError
*gerror
= NULL
;
717 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
718 val
= g_key_file_get_boolean(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_boolean(keyfileh
, "default", what
, &gerror
);
730 log_write("%s(%i): %s", __FILE__
, __LINE__
, gerror
->message
);
731 g_clear_error(&gerror
);
739 gchar
*expand_homedir(gchar
*str
)
744 return g_strdup_printf("%s%s", g_get_home_dir(), p
);
746 return g_strdup(str
);
749 static gchar
*_getline(const gchar
*file
)
752 gchar buf
[LINE_MAX
], *p
;
755 if ((fp
= fopen(file
, "r")) == NULL
) {
760 if ((p
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
761 warnx(N_("%s: empty file?"), file
);
767 if (buf
[strlen(buf
) - 1] == '\n')
768 buf
[strlen(buf
) - 1] = 0;
770 str
= gcry_malloc(strlen(p
) + 1);
771 memcpy(str
, p
, strlen(p
));
773 memset(&buf
, 0, sizeof(buf
));
777 static gboolean
parse_keyfile_key()
784 groups
= g_key_file_get_groups(keyfileh
, &n
);
786 for (p
= groups
; *p
; p
++) {
787 GError
*error
= NULL
;
789 if (g_key_file_has_key(keyfileh
, *p
, "key", &error
) == TRUE
) {
790 str
= g_key_file_get_string(keyfileh
, *p
, "key", &error
);
794 warnx("%s", error
->message
);
795 g_clear_error(&error
);
801 do_cache_push(*p
, str
);
807 warnx("%s", error
->message
);
808 g_clear_error(&error
);
812 if (g_key_file_has_key(keyfileh
, *p
, "key_file", &error
) == TRUE
) {
814 gchar
*file
= g_key_file_get_string(keyfileh
, *p
, "key_file", &error
);
818 warnx("%s", error
->message
);
819 g_clear_error(&error
);
825 t
= expand_homedir(file
);
829 if ((str
= _getline(file
)) == NULL
) {
835 do_cache_push(*p
, str
);
841 warnx("%s", error
->message
);
842 g_clear_error(&error
);
850 static gboolean
do_cache_push(const gchar
*filename
, const gchar
*password
)
855 const gchar
*p
= filename
;
863 if (valid_filename(p
) == FALSE
) {
864 warnx(N_("%s: invalid characters in filename"), p
);
868 md5file
= gcry_malloc(16);
869 key
= gcry_malloc(gcrykeysize
);
870 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
872 if (cache_iscached(md5file
) == TRUE
) {
873 warnx(N_("%s: file already cached, skipping"), p
);
879 if (access(p
, R_OK
|W_OK
) != 0) {
883 if (errno
!= ENOENT
) {
893 if (get_input(p
, key
) == FALSE
) {
900 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, password
, strlen(password
));
902 if (do_try_xml_decrypt(filename
, key
) == FALSE
) {
903 warnx(N_("%s: invalid password, skipping"), filename
);
910 if (cache_add_file(md5file
, key
) == FALSE
) {
911 warnx("%s: %s", p
, pwmd_strerror(EPWMD_MAX_SLOTS
));
917 timeout
= get_key_file_integer(p
, "cache_timeout");
918 cache_set_timeout(md5file
, timeout
);
919 warnx(N_("%s: file added to the cache"), filename
);
925 int main(int argc
, char *argv
[])
928 struct sockaddr_un addr
;
929 struct passwd
*pw
= getpwuid(getuid());
931 #ifndef MMAP_ANONYMOUS_SHARED
932 gchar shm_path
[PATH_MAX
];
934 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
935 gchar
*socketarg
= NULL
;
936 gchar
*datadir
= NULL
;
940 gchar
**cache_push
= NULL
;
942 gchar
*import
= NULL
;
943 gint default_timeout
;
944 gint rcfile_spec
= 0;
945 gint estatus
= EXIT_FAILURE
;
947 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
950 gboolean secure
= FALSE
;
955 #ifdef HAVE_SETRLIMIT
958 rl
.rlim_cur
= rl
.rlim_max
= 0;
960 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
961 err(EXIT_FAILURE
, "setrlimit()");
966 setlocale(LC_ALL
, "");
967 bindtextdomain("pwmd", LOCALEDIR
);
972 g_mem_set_vtable(&mtable
);
974 snprintf(buf
, sizeof(buf
), "%s/.pwmd", pw
->pw_dir
);
976 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
977 err(EXIT_FAILURE
, "%s", buf
);
979 snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", pw
->pw_dir
);
981 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
982 err(EXIT_FAILURE
, "%s", buf
);
984 rcfile
= g_strdup_printf("%s/.pwmd/config", pw
->pw_dir
);
986 if ((page_size
= sysconf(_SC_PAGESIZE
)) == -1)
987 err(EXIT_FAILURE
, "sysconf()");
989 cache_size
= page_size
;
991 while ((opt
= getopt(argc
, argv
, "bI:hvf:D")) != EOF
) {
1004 rcfile
= g_strdup(optarg
);
1008 printf("%s\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
1016 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1019 if (g_key_file_has_key(keyfileh
, "default", "iterations", NULL
) == TRUE
)
1020 iter
= g_key_file_get_integer(keyfileh
, "default", "iterations", NULL
);
1025 opt
= xml_import(import
, iter
);
1026 g_key_file_free(keyfileh
);
1028 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1031 g_key_file_set_list_separator(keyfileh
, ',');
1033 if ((p
= g_key_file_get_string(keyfileh
, "default", "socket_path", NULL
)) == NULL
)
1034 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
1038 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1040 socketarg
= g_strdup(buf
);
1045 if ((p
= g_key_file_get_string(keyfileh
, "default", "data_directory", NULL
)) == NULL
)
1046 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
1048 datadir
= expand_homedir(p
);
1051 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "default", "disable_list_and_dump", NULL
) == TRUE
) {
1052 n
= g_key_file_get_boolean(keyfileh
, "default", "disable_list_and_dump", NULL
);
1053 disable_list_and_dump
= n
;
1056 disable_list_and_dump
= secure
;
1058 if (g_key_file_has_key(keyfileh
, "default", "cache_timeout", NULL
) == TRUE
)
1059 default_timeout
= g_key_file_get_integer(keyfileh
, "default", "cache_timeout", NULL
);
1061 default_timeout
= -1;
1063 if (g_key_file_has_key(keyfileh
, "default", "cache_size", NULL
) == TRUE
) {
1064 cache_size
= g_key_file_get_integer(keyfileh
, "default", "cache_size", NULL
);
1066 if (cache_size
< page_size
|| cache_size
% page_size
)
1067 errx(EXIT_FAILURE
, N_("cache size must be in multiples of %li"), page_size
);
1070 if (g_key_file_has_key(keyfileh
, "default", "log_path", NULL
) == TRUE
) {
1071 if (g_key_file_has_key(keyfileh
, "default", "enable_logging", NULL
) == TRUE
) {
1072 n
= g_key_file_get_boolean(keyfileh
, "default", "enable_logging", NULL
);
1075 p
= g_key_file_get_string(keyfileh
, "default", "log_path", NULL
);
1079 snprintf(buf
, sizeof(buf
), "%s%s", g_get_home_dir(), p
--);
1081 logfile
= g_strdup(buf
);
1089 if (g_key_file_has_key(keyfileh
, "default", "cache_push", NULL
) == TRUE
)
1090 cache_push
= g_key_file_get_string_list(keyfileh
, "default", "cache_push", NULL
, NULL
);
1092 if (argc
!= optind
) {
1094 ptotal
= g_strv_length(cache_push
);
1096 for (; optind
< argc
; optind
++) {
1097 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
1098 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
1102 if (strchr(socketarg
, '/') == NULL
) {
1103 socketdir
= g_get_current_dir();
1104 socketname
= g_strdup(socketarg
);
1105 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1108 socketname
= g_strdup(strrchr(socketarg
, '/'));
1110 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
1111 socketdir
= g_strdup(socketarg
);
1112 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
1115 #ifdef MMAP_ANONYMOUS_SHARED
1116 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
,
1117 #ifdef MMAP_ANONYMOUS
1118 MAP_SHARED
|MAP_ANONYMOUS
, -1, 0)) == NULL
) {
1120 MAP_SHARED
|MAP_ANON
, -1, 0)) == NULL
) {
1122 err(EXIT_FAILURE
, "mmap()");
1125 snprintf(shm_path
, sizeof(shm_path
), "/pwmd.%i", pw
->pw_uid
);
1127 if ((fd
= shm_open(shm_path
, O_CREAT
|O_RDWR
|O_EXCL
, 0600)) == -1)
1128 err(EXIT_FAILURE
, "shm_open(): %s", shm_path
);
1131 * Should be enough for the file cache.
1133 if (ftruncate(fd
, cache_size
) == -1) {
1134 warn("ftruncate()");
1135 shm_unlink(shm_path
);
1139 if ((shm_data
= mmap(NULL
, cache_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
1142 shm_unlink(shm_path
);
1149 if (mlock(shm_data
, cache_size
) == -1)
1152 memset(shm_data
, 0, cache_size
);
1154 if (chdir(datadir
)) {
1155 warn("%s", datadir
);
1161 if (parse_keyfile_key() == FALSE
)
1165 * Set the cache entry for a file. Prompts for the password.
1168 for (opt
= 0; cache_push
[opt
]; opt
++)
1169 do_cache_push(cache_push
[opt
], NULL
);
1171 g_strfreev(cache_push
);
1172 warnx(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
1176 * bind() doesn't like the full pathname of the socket or any non alphanum
1177 * characters so change to the directory where the socket is wanted then
1178 * create it then change to datadir.
1180 if (chdir(socketdir
)) {
1181 warn("%s", socketdir
);
1187 if ((sfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
1192 addr
.sun_family
= AF_UNIX
;
1193 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
1194 g_free(--socketname
);
1196 if (bind(sfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
1199 if (errno
== EADDRINUSE
)
1200 warnx(N_("Either there is another pwmd running or '%s' is a \n"
1201 "stale socket. Please remove it manually."), socketpath
);
1207 if (chdir(datadir
)) {
1208 warn("%s", datadir
);
1216 if (listen(sfd
, 0) == -1) {
1221 signal(SIGCHLD
, catchsig
);
1222 signal(SIGTERM
, catchsig
);
1223 signal(SIGINT
, catchsig
);
1224 signal(SIGHUP
, catchsig
);
1225 signal(SIGALRM
, catchsig
);
1226 signal(SIGABRT
, catchsig
);
1227 signal(SIGUSR1
, catchsig
);
1245 log_write("%s %s", PACKAGE_STRING
, N_("started"));
1246 send_cache_status(NULL
);
1249 socklen_t slen
= sizeof(struct sockaddr_un
);
1250 struct sockaddr_un raddr
;
1253 if ((fd
= accept(sfd
, (struct sockaddr
*)&raddr
, &slen
)) == -1) {
1257 if (errno
== EAGAIN
)
1260 log_write("accept(): %s", strerror(errno
));
1264 switch ((pid
= fork())) {
1266 log_write("fork(): %s", strerror(errno
));
1276 log_write(N_("new connection: pid=%i"), pid
);
1280 estatus
= EXIT_SUCCESS
;
1283 if (socketpath
&& do_unlink
) {
1288 signal(SIGUSR1
, SIG_IGN
);
1289 g_key_file_free(keyfileh
);
1293 log_write(N_("waiting for all clients to disconnect"));
1296 if (clients
!= n_clients
) {
1297 log_write(N_("%i clients remain"), clients
);
1298 n_clients
= clients
;
1301 select(0, NULL
, NULL
, NULL
, NULL
);
1305 memset(shm_data
, 0, cache_size
);
1307 if (munmap(shm_data
, cache_size
) == -1)
1308 log_write("munmap(): %s", strerror(errno
));
1310 #ifndef MMAP_ANONYMOUS_SHARED
1311 if (shm_unlink(shm_path
) == -1)
1312 log_write("shm_unlink(): %s: %s", shm_path
, strerror(errno
));
1315 if (estatus
== EXIT_SUCCESS
)
1316 log_write(N_("pwmd exiting normally"));