1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 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 02110-1301 USA
39 #include <glib/gprintf.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/resource.h>
52 #ifdef HAVE_GETOPT_LONG
57 #include "getopt_long.h"
75 #include "pwmd_error.h"
82 GCRY_THREAD_OPTION_PTH_IMPL
;
84 static void *reload_rcfile_thread(void *arg
)
86 gboolean b
= disable_list_and_dump
;
87 gchar
**users
= g_key_file_get_string_list(keyfileh
, "global", "allowed",
90 pth_attr_t attr
= pth_attr_of(pth_self());
92 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
93 pth_attr_destroy(attr
);
94 MUTEX_LOCK(&rcfile_mutex
);
95 log_write(N_("reloading configuration file '%s'"), rcfile
);
96 k
= parse_rcfile(FALSE
);
101 g_key_file_free(keyfileh
);
105 startStopKeepAlive(FALSE
);
106 send_status_all(STATUS_CONFIG
);
108 disable_list_and_dump
= !disable_list_and_dump
? b
: TRUE
;
109 g_key_file_set_string_list(keyfileh
, "global", "allowed",
110 (const gchar
**)users
, g_strv_length(users
));
112 MUTEX_UNLOCK(&rcfile_mutex
);
116 static void reload_rcfile()
119 pth_attr_t attr
= pth_attr_new();
123 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, 0);
124 tid
= pth_spawn(attr
, reload_rcfile_thread
, NULL
);
126 pth_attr_destroy(attr
);
129 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
130 _gpg_strerror(gpg_error_from_errno(n
)));
133 gpg_error_t
send_syserror(assuan_context_t ctx
, gint e
)
135 gpg_error_t n
= gpg_error_from_errno(e
);
136 struct client_s
*client
= assuan_get_pointer(ctx
);
139 return assuan_process_done(ctx
, assuan_set_error(ctx
, n
, _gpg_strerror(n
)));
142 gpg_error_t
send_error(assuan_context_t ctx
, gpg_error_t e
)
144 gpg_err_code_t n
= gpg_err_code(e
);
145 gpg_error_t code
= gpg_err_make(PWMD_ERR_SOURCE
, n
);
146 struct client_s
*client
= assuan_get_pointer(ctx
);
152 return assuan_process_done(ctx
, 0);
155 log_write("%s", pwmd_strerror(e
));
159 if (n
== EPWMD_LIBXML_ERROR
) {
160 xmlErrorPtr xe
= client
->xml_error
;
163 xe
= xmlGetLastError();
165 e
= assuan_process_done(ctx
, assuan_set_error(ctx
, code
, xe
->message
));
166 log_write("%s", xe
->message
);
168 if (xe
== client
->xml_error
)
173 client
->xml_error
= NULL
;
177 return assuan_process_done(ctx
, assuan_set_error(ctx
, code
, pwmd_strerror(e
)));
180 void log_write(const gchar
*fmt
, ...)
190 pth_t tid
= pth_self();
193 if ((!logfile
&& !isatty(STDERR_FILENO
) && log_syslog
== FALSE
) || !fmt
)
196 if (!cmdline
&& logfile
) {
197 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
205 if (g_vasprintf(&args
, fmt
, ap
) == -1) {
216 fprintf(stderr
, "%s\n", args
);
222 attr
= pth_attr_of(tid
);
224 if (pth_attr_get(attr
, PTH_ATTR_NAME
, &name
) == FALSE
)
227 pth_attr_destroy(attr
);
228 name
= print_fmt(buf
, sizeof(buf
), "%s(%p): ", name
, tid
);
230 if (!cmdline
&& log_syslog
== TRUE
)
231 syslog(LOG_INFO
, "%s%s", name
, args
);
234 tm
= localtime(&now
);
235 strftime(tbuf
, sizeof(tbuf
), "%b %d %Y %H:%M:%S ", tm
);
236 tbuf
[sizeof(tbuf
) - 1] = 0;
238 if (args
[strlen(args
)-1] == '\n')
239 args
[strlen(args
)-1] = 0;
241 line
= g_strdup_printf("%s %i %s%s\n", tbuf
, getpid(), name
, args
);
252 pth_write(fd
, line
, strlen(line
));
257 if (isatty(STDERR_FILENO
)) {
258 fprintf(stderr
, "%s", line
);
265 static void usage(gchar
*pn
, gint rc
)
267 g_fprintf(rc
== EXIT_FAILURE
? stderr
: stdout
, N_(
268 "Usage: %s [options] [file1] [...]\n"
270 " run as a foreground process\n"
271 " --rcfile/-f <filename>\n"
272 " load the specified rcfile (~/.pwmd/config)\n"
273 " --convert/-C <filename>\n"
274 " convert a version 1 data file to version 2\n"
275 " --import/-I <filename>\n"
276 " import an XML file\n"
278 " encrypt with the specified number of iterations when importing\n"
279 " (default is in the rcfile \"global\" section)\n"
280 " --key-file/-k <filename>\n"
281 " obtain the key from the specified file when importing or converting\n"
282 " --outfile/-o <filename>\n"
283 " output file to use when importing or converting (- for stdout)\n"
284 " --disable-dump/-D\n"
285 " disable use of the LIST, XPATH and DUMP commands\n"
286 " --no-pinentry/-P\n"
287 " disable use of pinentry\n"
294 static void setup_gcrypt()
296 gcry_control(GCRYCTL_SET_THREAD_CBS
, &gcry_threads_pth
);
298 if (!gcry_check_version(GCRYPT_VERSION
))
299 errx(EXIT_FAILURE
, N_("gcry_check_version(): Incompatible libgcrypt. Wanted %s, got %s."), GCRYPT_VERSION
, gcry_check_version(NULL
));
301 gcry_set_allocation_handler(xmalloc
, xmalloc
, NULL
, xrealloc
, xfree
);
304 static gpg_error_t
validate_peer(struct client_s
*cl
)
307 gboolean allowed
= FALSE
;
311 gpg_error_t rc
= assuan_get_peercred(cl
->ctx
, &pid
, &uid
, &gid
);
316 users
= g_key_file_get_string_list(keyfileh
, "global", "allowed", NULL
, NULL
);
319 for (gchar
**p
= users
; *p
; p
++) {
320 struct passwd pw
, *result
;
321 struct group gr
, *gresult
;
325 size_t len
= sysconf(_SC_GETGR_R_SIZE_MAX
);
334 return gpg_error_from_errno(ENOMEM
);
337 if (!getgrnam_r(*(p
)+1, &gr
, buf
, len
, &gresult
) && gresult
) {
338 if (gresult
->gr_gid
== gid
) {
344 len
= sysconf(_SC_GETPW_R_SIZE_MAX
);
349 gchar
*tbuf
= g_malloc(len
);
351 for (gchar
**t
= gresult
->gr_mem
; *t
; t
++) {
352 if (!getpwnam_r(*t
, &pw
, tbuf
, len
, &result
) && result
) {
353 if (result
->pw_uid
== uid
) {
368 size_t len
= sysconf(_SC_GETPW_R_SIZE_MAX
);
377 return gpg_error_from_errno(ENOMEM
);
380 if (!getpwnam_r(*p
, &pw
, buf
, len
, &result
) && result
) {
381 if (result
->pw_uid
== uid
) {
395 log_write("peer %s: uid=%i, gid=%i, pid=%i",
396 allowed
? N_("accepted") : N_("rejected"), uid
, gid
,pid
);
397 return allowed
? 0 : GPG_ERR_INV_USER_ID
;
400 static gboolean
new_connection(struct client_s
*cl
)
406 rc
= assuan_init_socket_server_ext(&cl
->ctx
, cl
->thd
->fd
, 2);
411 assuan_set_pointer(cl
->ctx
, cl
);
412 ver
= g_strdup_printf("%s", PACKAGE_STRING
);
413 assuan_set_hello_line(cl
->ctx
, ver
);
415 rc
= register_commands(cl
->ctx
);
420 rc
= assuan_accept(cl
->ctx
);
425 rc
= validate_peer(cl
);
427 /* May not be implemented on all platforms. */
428 if (rc
&& gpg_err_code(rc
) != GPG_ERR_ASS_GENERAL
)
431 str
= get_key_file_string("global", "debug_file");
434 assuan_set_log_stream(cl
->ctx
, debugfp
);
442 log_write("%s", _gpg_strerror(rc
));
446 static void xml_error_cb(void *data
, xmlErrorPtr e
)
448 struct client_s
*client
= data
;
451 * Keep the first reported error as the one to show in the error
452 * description. Reset in send_error().
454 if (client
->xml_error
)
457 xmlCopyError(e
, client
->xml_error
);
460 void close_file_header(file_header_internal_t
*fh
)
465 if (fh
->fd
!= -1 || (cmdline
== TRUE
&& fh
->fd
!= STDOUT_FILENO
))
474 void cleanup_crypto(struct crypto_s
**c
)
476 struct crypto_s
*cr
= *c
;
497 gcry_free(cr
->tkey2
);
502 gcry_free(cr
->inbuf
);
507 gcry_free(cr
->outbuf
);
511 close_file_header(cr
->fh
);
515 gcry_cipher_close(cr
->gh
);
523 * This is called after a client_thread terminates. Set with
524 * pth_cleanup_push().
526 static void cleanup_cb(void *arg
)
528 struct client_thread_s
*cn
= arg
;
529 struct client_s
*cl
= cn
->cl
;
531 MUTEX_LOCK(&cn_mutex
);
532 cn_thread_list
= g_slist_remove(cn_thread_list
, cn
);
533 MUTEX_UNLOCK(&cn_mutex
);
536 MUTEX_LOCK(&cn
->mp_mutex
);
537 pth_cancel(cn
->msg_tid
);
538 MUTEX_UNLOCK(&cn
->mp_mutex
);
542 while (pth_msgport_pending(cn
->mp
)) {
543 pth_message_t
*msg
= pth_msgport_get(cn
->mp
);
549 pth_msgport_destroy(cn
->mp
);
563 assuan_deinit_server(cl
->ctx
);
564 else if (cl
->thd
&& cl
->thd
->fd
!= -1)
569 cleanup_pinentry(cl
->pinentry
);
573 cleanup_crypto(&cl
->crypto
);
577 log_write(N_("exiting, fd=%i"), cn
->fd
);
579 send_status_all(STATUS_CLIENTS
);
583 * Called every time a connection is made from init_new_connection(). This is
584 * the thread entry point.
586 static void *client_thread(void *data
)
588 struct client_thread_s
*thd
= data
;
589 struct client_s
*cl
= g_malloc0(sizeof(struct client_s
));
595 * Prevent a race condition with init_new_connection() if this thread
596 * fails (returns) for some reason before init_new_connection() releases
599 MUTEX_LOCK(&cn_mutex
);
600 MUTEX_UNLOCK(&cn_mutex
);
601 pth_cleanup_push(cleanup_cb
, thd
);
604 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
608 attr
= pth_attr_of(pth_self());
609 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
610 pth_attr_destroy(attr
);
614 if (!new_connection(cl
))
618 cl
->pinentry
= pinentry_init();
621 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
627 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
628 log_write("mlockall(): %s", strerror(errno
));
633 thd
->mp
= pth_msgport_create(NULL
);
634 pth_mutex_init(&thd
->mp_mutex
);
635 thd
->msg_tid
= pth_spawn(NULL
, client_msg_thread
, thd
);
639 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
640 _gpg_strerror(gpg_error_from_errno(n
)));
644 pth_yield(thd
->msg_tid
);
645 rc
= send_status(cl
->ctx
, STATUS_CACHE
, NULL
);
648 log_write("%s", _gpg_strerror(rc
));
652 send_status_all(STATUS_CLIENTS
);
653 xmlSetStructuredErrorFunc(cl
, xml_error_cb
);
657 pth_event_t pev
= NULL
;
659 pth_status_t st
, wst
;
660 pth_event_t wev
= NULL
;
661 pth_event_t rev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
,
663 pth_event_t ev
= rev
;
666 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
667 pev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_READABLE
, cl
->pinentry
->fd
);
668 ev
= pth_event_concat(ev
, pev
, NULL
);
672 if (cl
->inquire_status
== INQUIRE_BUSY
) {
673 wev
= pth_event(PTH_EVENT_FD
|PTH_UNTIL_FD_WRITEABLE
, cl
->thd
->fd
);
674 ev
= pth_event_concat(ev
, wev
, NULL
);
677 pth_cleanup_push(cleanup_ev_cb
, ev
);
679 st
= pth_event_status(rev
);
680 wst
= pth_event_status(wev
);
682 if (st
== PTH_STATUS_OCCURRED
|| wst
== PTH_STATUS_OCCURRED
) {
683 rc
= assuan_process_next(cl
->ctx
);
686 cl
->inquire_status
= INQUIRE_INIT
;
689 if (gpg_err_code(rc
) == GPG_ERR_EOF
)
692 log_write("assuan_process_next(): %s", _gpg_strerror(rc
));
693 rc
= send_error(cl
->ctx
, gpg_err_make(PWMD_ERR_SOURCE
, rc
));
696 log_write("assuan_process_done(): %s", _gpg_strerror(rc
));
702 if (cl
->pinentry
->pid
&& cl
->pinentry
->status
== PINENTRY_INIT
)
703 cl
->pinentry
->status
= PINENTRY_RUNNING
;
706 switch (cl
->inquire_status
) {
711 cl
->inquire_status
= INQUIRE_INIT
;
712 rc
= assuan_process_done(cl
->ctx
, 0);
720 st
= pth_event_status(pev
);
722 rc
= pinentry_iterate(cl
,
723 pev
&& cl
->pinentry
->fd
!= -1 && st
== PTH_STATUS_OCCURRED
);
729 * Client cleanup (including XML data) is done in cleanup_cb().
733 pth_exit(PTH_CANCELED
);
737 static gchar
*do_read_password(const gchar
*prompt
)
739 gchar buf
[LINE_MAX
] = {0}, *p
;
740 struct termios told
, tnew
;
743 if (tcgetattr(STDIN_FILENO
, &told
) == -1) {
744 log_write("tcgetattr(): %s", strerror(errno
));
748 memcpy(&tnew
, &told
, sizeof(struct termios
));
749 tnew
.c_lflag
&= ~(ECHO
);
750 tnew
.c_lflag
|= ICANON
|ECHONL
;
752 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tnew
) == -1) {
753 log_write("tcsetattr(): %s", strerror(errno
));
754 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
758 fprintf(stderr
, "%s", prompt
);
760 if ((p
= fgets(buf
, sizeof(buf
), stdin
)) == NULL
) {
761 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
765 tcsetattr(STDIN_FILENO
, TCSANOW
, &told
);
766 p
[strlen(p
) - 1] = 0;
769 key
= gcry_malloc(1);
773 key
= gcry_malloc(strlen(p
) + 1);
774 sprintf(key
, "%s", p
);
777 memset(&buf
, 0, sizeof(buf
));
781 /* Only used when "enable_pinentry" is "false" or -P. */
782 static gpg_error_t
read_password(const gchar
*filename
,
783 struct crypto_s
*crypto
, guchar
*key
, pinentry_cmd_t which
)
787 if (which
== PINENTRY_SAVE
) {
788 prompt
= g_strdup_printf(N_("New passphrase for file %s: "), filename
);
789 crypto
->tkey
= do_read_password(prompt
);
793 log_write(N_("%s: Skipping file"), filename
);
794 return GPG_ERR_BAD_PASSPHRASE
;
797 prompt
= g_strdup_printf(N_("Repeat passphrase: "));
798 crypto
->tkey2
= do_read_password(prompt
);
801 if (!crypto
->tkey2
) {
802 log_write(N_("%s: Skipping file"), filename
);
803 return GPG_ERR_BAD_PASSPHRASE
;
806 if (strcmp(crypto
->tkey
, crypto
->tkey2
)) {
807 log_write(N_("%s: Passphrase mismatch"), filename
);
808 return GPG_ERR_INV_PASSPHRASE
;
811 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
812 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
816 prompt
= g_strdup_printf(N_("Passphrase required for %s: "), filename
);
818 if ((crypto
->tkey
= do_read_password(prompt
)) == NULL
) {
819 log_write(N_("%s: Skipping file"), filename
);
821 return GPG_ERR_BAD_PASSPHRASE
;
824 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, crypto
->tkey
,
825 strlen(crypto
->tkey
) ? strlen(crypto
->tkey
) : 1);
831 * inbuf must have been allocated with gcry_malloc().
833 gpg_error_t
export_common(const gchar
*filename
, struct crypto_s
*crypto
,
834 gpointer inbuf
, gulong insize
)
841 rc
= update_save_flags(NULL
, crypto
);
846 level
= get_key_file_integer(filename
, "compression_level");
851 if (do_compress(NULL
, level
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
)
853 return zrc
== Z_MEM_ERROR
? GPG_ERR_ENOMEM
: GPG_ERR_COMPR_ALGO
;
856 crypto
->inbuf
= outbuf
;
857 crypto
->insize
= outsize
;
858 rc
= do_xml_encrypt(NULL
, crypto
, filename
);
862 static gpg_error_t
get_password(const gchar
*filename
,
863 struct crypto_s
*crypto
, guchar
*md5file
, guchar
*key
,
864 pinentry_cmd_t which
)
869 if (g_key_file_get_boolean(keyfileh
, "global", "enable_pinentry", NULL
)
872 return read_password(filename
, crypto
, key
, which
);
876 gchar
*result
= NULL
;
877 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
879 pth_mutex_init(&pin
->status_mutex
);
880 set_pinentry_defaults(pin
);
882 pin
->filename
= g_strdup(filename
);
883 rc
= pinentry_getpin(pin
, &result
);
890 gcry_md_hash_buffer(GCRY_MD_SHA256
, key
, result
, result
? strlen(result
) : 1);
892 cleanup_pinentry(pin
);
900 static gboolean
xml_import(const gchar
*filename
, const gchar
*outfile
,
901 const gchar
*keyfile
, guint64 iter
)
910 struct crypto_s
*crypto
;
911 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
913 if (stat(filename
, &st
) == -1) {
914 log_write("%s: %s", filename
, strerror(errno
));
918 crypto
= init_client_crypto();
923 crypto
->key
= gcry_malloc(hashlen
);
924 memset(crypto
->key
, 0, hashlen
);
927 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
931 log_write(N_("Importing XML from '%s'. Output will be written to '%s' ..."),
934 if (iter
&& keyfile
) {
935 rc
= parse_rcfile_keyfile(crypto
, keyfile
, TRUE
);
940 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
944 rc
= get_password(outfile
, crypto
, NULL
, crypto
->key
, PINENTRY_SAVE
);
946 if (rc
== GPG_ERR_ASSUAN_SERVER_FAULT
) {
947 log_write(N_("%s. Maybe disabling pinentry (-P) will help?"),
952 log_write("%s", pwmd_strerror(rc
));
957 if ((fd
= open(filename
, O_RDONLY
)) == -1) {
958 log_write("%s: %s", filename
, strerror(errno
));
962 if ((xmlbuf
= gcry_malloc(st
.st_size
+1)) == NULL
) {
964 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
968 if (pth_read(fd
, xmlbuf
, st
.st_size
) == -1) {
972 log_write("%s: %s", filename
, strerror(errno
));
977 xmlbuf
[st
.st_size
] = 0;
980 * Make sure the document validates.
982 if ((doc
= xmlReadDoc(xmlbuf
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
)) == NULL
) {
983 log_write("xmlReadDoc() failed");
989 xmlNodePtr n
= xmlDocGetRootElement(doc
);
990 rc
= validate_import(n
? n
->children
: n
);
993 log_write("%s", pwmd_strerror(rc
));
998 xmlDocDumpMemory(doc
, &xml
, &len
);
1002 memset(crypto
->key
, '!', hashlen
);
1004 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1007 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1011 crypto
->fh
->ver
.fh2
.iter
= iter
;
1012 rc
= export_common(outfile
, crypto
, xml
, len
);
1016 send_error(NULL
, rc
);
1020 cleanup_crypto(&crypto
);
1024 cleanup_crypto(&crypto
);
1028 gboolean
do_cache_push(const gchar
*filename
, const void *password
,
1033 const gchar
*p
= filename
;
1034 struct crypto_s
*crypto
;
1036 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1044 if (valid_filename(p
) == FALSE
) {
1045 log_write(N_("%s: Invalid characters in filename"), p
);
1049 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, p
, strlen(p
));
1051 if (cache_iscached(md5file
)) {
1052 log_write(N_("%s: already cached, skipping"), p
);
1056 if (access(p
, R_OK
|W_OK
) != 0) {
1057 log_write("%s: %s", p
, strerror(errno
));
1061 crypto
= init_client_crypto();
1066 crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
1069 log_write("%s: %s", p
, pwmd_strerror(rc
));
1070 cleanup_crypto(&crypto
);
1074 crypto
->key
= gcry_malloc(hashlen
);
1077 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1078 cleanup_crypto(&crypto
);
1082 log_write(N_("Adding '%s' to the file cache ..."), filename
);
1084 if (crypto
->fh
->ver
.fh2
.iter
<= 0ULL) {
1085 memset(crypto
->key
, '!', hashlen
);
1090 rc
= get_password(p
, crypto
, md5file
, crypto
->key
, PINENTRY_OPEN
);
1093 send_error(NULL
, rc
);
1094 cleanup_crypto(&crypto
);
1098 gcry_free(crypto
->fh
->doc
);
1099 crypto
->fh
->doc
= NULL
;
1102 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, password
, len
);
1105 rc
= init_client_crypto2(filename
, crypto
);
1108 send_error(NULL
, rc
);
1109 cleanup_crypto(&crypto
);
1113 rc
= try_xml_decrypt(NULL
, crypto
, NULL
, NULL
);
1116 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1117 cleanup_crypto(&crypto
);
1121 if (cache_update_key(md5file
, crypto
->key
) == FALSE
) {
1122 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1123 cleanup_crypto(&crypto
);
1127 timeout
= get_key_file_integer(p
, "cache_timeout");
1128 cache_set_timeout(md5file
, timeout
);
1129 log_write(N_("File '%s' now cached"), filename
);
1130 cleanup_crypto(&crypto
);
1134 static void init_new_connection(gint fd
)
1137 struct client_thread_s
*new;
1140 new = g_malloc0(sizeof(struct client_thread_s
));
1143 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1148 MUTEX_LOCK(&cn_mutex
);
1150 attr
= pth_attr_new();
1151 pth_attr_init(attr
);
1152 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1153 new->tid
= pth_spawn(attr
, client_thread
, new);
1155 pth_attr_destroy(attr
);
1160 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1161 _gpg_strerror(gpg_error_from_errno(n
)));
1162 MUTEX_UNLOCK(&cn_mutex
);
1166 cn_thread_list
= g_slist_append(cn_thread_list
, new);
1167 MUTEX_UNLOCK(&cn_mutex
);
1168 log_write(N_("new connection: tid=%p, fd=%i"), new->tid
, fd
);
1171 static void *accept_thread(void *arg
)
1173 gint sockfd
= *(gint
*)arg
;
1174 pth_attr_t attr
= pth_attr_of(pth_self());
1176 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1177 pth_attr_destroy(attr
);
1180 socklen_t slen
= sizeof(struct sockaddr_un
);
1181 struct sockaddr_un raddr
;
1184 fd
= pth_accept(sockfd
, (struct sockaddr
*)&raddr
, &slen
);
1187 if (errno
!= EAGAIN
) {
1188 if (!quit
) // probably EBADF
1189 log_write("accept(): %s", strerror(errno
));
1197 init_new_connection(fd
);
1200 /* Just in case accept() failed for some reason other than EBADF */
1202 pth_exit(PTH_CANCELED
);
1206 static void *adjust_cache_timer_thread(void *arg
)
1208 pth_attr_t attr
= pth_attr_of(pth_self());
1210 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1211 pth_attr_destroy(attr
);
1216 cache_adjust_timer();
1223 void cleanup_mutex_cb(void *arg
)
1225 pth_mutex_t
*m
= arg
;
1230 void cleanup_ev_cb(void *arg
)
1232 pth_event_t ev
= arg
;
1234 pth_event_free(ev
, PTH_FREE_ALL
);
1237 void cleanup_fd_cb(void *arg
)
1239 gint fd
= *(gint
*)arg
;
1244 void cleanup_unlink_cb(void *arg
)
1251 void cleanup_cancel_cb(void *arg
)
1257 attr
= pth_attr_of(tid
);
1258 pth_attr_get(attr
, PTH_ATTR_JOINABLE
, &join
);
1269 static void *keepalive_thread(void *arg
)
1271 gint to
= *(gint
*)arg
;
1272 pth_attr_t attr
= pth_attr_of(pth_self());
1274 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
1275 pth_attr_destroy(attr
);
1278 pth_event_t ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0));
1280 pth_cleanup_push(cleanup_ev_cb
, ev
);
1282 send_status_all(STATUS_KEEPALIVE
);
1289 static void startStopKeepAlive(gboolean term
)
1291 gint n
= get_key_file_integer("global", "keepalive");
1294 pth_cancel(keepalive_tid
);
1296 keepalive_tid
= NULL
;
1303 pth_attr_t attr
= pth_attr_new();
1305 pth_attr_init(attr
);
1306 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1307 keepalive_tid
= pth_spawn(attr
, keepalive_thread
, &n
);
1309 pth_attr_destroy(attr
);
1311 if (!keepalive_tid
) {
1312 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1313 _gpg_strerror(gpg_error_from_errno(e
)));
1317 pth_yield(keepalive_tid
);
1321 static gboolean
waiting_for_exit()
1324 pth_event_t evs
= NULL
;
1326 MUTEX_LOCK(&cn_mutex
);
1328 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
1329 struct client_thread_s
*thd
= g_slist_nth_data(cn_thread_list
, i
);
1330 pth_event_t ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, thd
->tid
);
1333 evs
= pth_event_concat(evs
, ev
, NULL
);
1338 MUTEX_UNLOCK(&cn_mutex
);
1344 MUTEX_LOCK(&cn_mutex
);
1345 i
= g_slist_length(cn_thread_list
);
1346 MUTEX_UNLOCK(&cn_mutex
);
1347 pth_event_free(evs
, PTH_FREE_ALL
);
1348 return i
? TRUE
: FALSE
;
1351 static void catch_sigabrt(int sig
)
1353 cache_clear(NULL
, 2);
1359 static void server_loop(gint sockfd
, gchar
**socketpath
)
1365 pth_t cache_timeout_tid
;
1367 sigemptyset(&sigset
);
1370 sigaddset(&sigset
, SIGTERM
);
1371 sigaddset(&sigset
, SIGINT
);
1373 /* Clears the file cache. */
1374 sigaddset(&sigset
, SIGUSR1
);
1376 /* Configuration file reloading. */
1377 sigaddset(&sigset
, SIGHUP
);
1379 /* Clears the cache and exits when something bad happens. */
1380 signal(SIGABRT
, catch_sigabrt
);
1381 sigaddset(&sigset
, SIGABRT
);
1383 /* Ignored everywhere. When a client disconnects abnormally this signal
1384 * gets raised. It isn't needed though because client_thread() will check
1385 * for rcs even after the client disconnects. */
1386 signal(SIGPIPE
, SIG_IGN
);
1387 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1389 log_write(N_("%s started for user %s"), PACKAGE_STRING
, g_get_user_name());
1390 log_write(N_("Listening on %s"), *socketpath
);
1391 attr
= pth_attr_new();
1392 pth_attr_init(attr
);
1393 accept_tid
= pth_spawn(attr
, accept_thread
, &sockfd
);
1396 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1397 _gpg_strerror(gpg_error_from_errno(errno
)));
1398 pth_attr_destroy(attr
);
1402 pth_yield(accept_tid
);
1403 startStopKeepAlive(FALSE
);
1404 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
1405 cache_timeout_tid
= pth_spawn(attr
, adjust_cache_timer_thread
, NULL
);
1407 if (!cache_timeout_tid
) {
1408 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1409 _gpg_strerror(gpg_error_from_errno(errno
)));
1410 pth_attr_destroy(attr
);
1414 pth_yield(cache_timeout_tid
);
1415 pth_attr_destroy(attr
);
1418 gchar
*str
= get_key_file_string("global", "debug_file");
1421 gchar
*f
= expand_homedir(str
);
1424 debugfp
= fopen(f
, "w");
1427 log_write("%s: %s", f
, pwmd_strerror(gpg_error_from_errno(errno
)));
1429 assuan_set_assuan_log_stream(debugfp
);
1438 pth_sigwait(&sigset
, &sig
);
1439 log_write(N_("caught signal %i (%s)"), sig
, strsignal(sig
));
1441 /* Caught a signal. */
1447 // not really handled here.
1448 catch_sigabrt(SIGABRT
);
1452 log_write(N_("clearing file cache"));
1453 cache_clear(NULL
, 2);
1464 * We're out of the main server loop. This happens when a signal was sent
1465 * to terminate the daemon. We'll wait for all clients to disconnect
1466 * before exiting and ignore any following signals.
1468 shutdown(sockfd
, SHUT_RDWR
);
1470 pth_cancel(accept_tid
);
1471 pth_join(accept_tid
, NULL
);
1472 unlink(*socketpath
);
1473 g_free(*socketpath
);
1475 MUTEX_LOCK(&cn_mutex
);
1476 n
= g_slist_length(cn_thread_list
);
1477 MUTEX_UNLOCK(&cn_mutex
);
1480 log_write(N_("waiting for all clients to disconnect"));
1483 MUTEX_LOCK(&cn_mutex
);
1484 n
= g_slist_length(cn_thread_list
);
1485 MUTEX_UNLOCK(&cn_mutex
);
1486 log_write(N_("%i clients remain"), n
);
1487 } while (waiting_for_exit());
1490 startStopKeepAlive(TRUE
);
1491 pth_cancel(cache_timeout_tid
);
1496 * Only called from pinentry_fork() in the child process.
1498 void free_client_list()
1500 gint i
, t
= g_slist_length(cn_thread_list
);
1502 for (i
= 0; i
< t
; i
++) {
1503 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
1505 free_client(cn
->cl
);
1511 static guint
pwmd_cipher_to_gcrypt(guint64 flags
)
1513 if (flags
& PWMD_CIPHER_AES128
)
1514 return GCRY_CIPHER_AES128
;
1515 else if (flags
& PWMD_CIPHER_AES192
)
1516 return GCRY_CIPHER_AES192
;
1517 else if (flags
& PWMD_CIPHER_AES256
)
1518 return GCRY_CIPHER_AES256
;
1519 else if (flags
& PWMD_CIPHER_SERPENT128
)
1520 return GCRY_CIPHER_SERPENT128
;
1521 else if (flags
& PWMD_CIPHER_SERPENT192
)
1522 return GCRY_CIPHER_SERPENT192
;
1523 else if (flags
& PWMD_CIPHER_SERPENT256
)
1524 return GCRY_CIPHER_SERPENT256
;
1525 else if (flags
& PWMD_CIPHER_CAMELLIA128
)
1526 return GCRY_CIPHER_CAMELLIA128
;
1527 else if (flags
& PWMD_CIPHER_CAMELLIA192
)
1528 return GCRY_CIPHER_CAMELLIA192
;
1529 else if (flags
& PWMD_CIPHER_CAMELLIA256
)
1530 return GCRY_CIPHER_CAMELLIA256
;
1531 else if (flags
& PWMD_CIPHER_BLOWFISH
)
1532 return GCRY_CIPHER_BLOWFISH
;
1533 else if (flags
& PWMD_CIPHER_3DES
)
1534 return GCRY_CIPHER_3DES
;
1535 else if (flags
& PWMD_CIPHER_CAST5
)
1536 return GCRY_CIPHER_CAST5
;
1537 else if (flags
& PWMD_CIPHER_TWOFISH
)
1538 return GCRY_CIPHER_TWOFISH
;
1539 else if (flags
& PWMD_CIPHER_TWOFISH128
)
1540 return GCRY_CIPHER_TWOFISH128
;
1542 /* For backwards compatibility (no flags). */
1543 return GCRY_CIPHER_AES256
;
1546 guint
pwmd_cipher_str_to_cipher(const gchar
*str
)
1550 if (!g_strcasecmp(str
, "aes128"))
1551 flags
= PWMD_CIPHER_AES128
;
1552 else if (!g_strcasecmp(str
, "aes192"))
1553 flags
= PWMD_CIPHER_AES192
;
1554 else if (!g_strcasecmp(str
, "aes256"))
1555 flags
= PWMD_CIPHER_AES256
;
1556 if (!g_strcasecmp(str
, "serpent128"))
1557 flags
= PWMD_CIPHER_SERPENT128
;
1558 else if (!g_strcasecmp(str
, "serpent192"))
1559 flags
= PWMD_CIPHER_SERPENT192
;
1560 else if (!g_strcasecmp(str
, "serpent256"))
1561 flags
= PWMD_CIPHER_SERPENT256
;
1562 if (!g_strcasecmp(str
, "camellia128"))
1563 flags
= PWMD_CIPHER_CAMELLIA128
;
1564 else if (!g_strcasecmp(str
, "camellia192"))
1565 flags
= PWMD_CIPHER_CAMELLIA192
;
1566 else if (!g_strcasecmp(str
, "camellia256"))
1567 flags
= PWMD_CIPHER_CAMELLIA256
;
1568 else if (!g_strcasecmp(str
, "blowfish"))
1569 flags
= PWMD_CIPHER_BLOWFISH
;
1570 else if (!g_strcasecmp(str
, "cast5"))
1571 flags
= PWMD_CIPHER_CAST5
;
1572 else if (!g_strcasecmp(str
, "3des"))
1573 flags
= PWMD_CIPHER_3DES
;
1574 else if (!g_strcasecmp(str
, "twofish256"))
1575 flags
= PWMD_CIPHER_TWOFISH
;
1576 else if (!g_strcasecmp(str
, "twofish128"))
1577 flags
= PWMD_CIPHER_TWOFISH128
;
1582 const gchar
*pwmd_cipher_to_str(guint64 flags
)
1584 if (flags
& PWMD_CIPHER_AES128
)
1586 else if (flags
& PWMD_CIPHER_AES192
)
1588 else if (flags
& PWMD_CIPHER_AES256
)
1590 else if (flags
& PWMD_CIPHER_SERPENT128
)
1591 return "serpent128";
1592 else if (flags
& PWMD_CIPHER_SERPENT192
)
1593 return "serpent192";
1594 else if (flags
& PWMD_CIPHER_SERPENT256
)
1595 return "serpent256";
1596 else if (flags
& PWMD_CIPHER_CAMELLIA128
)
1597 return "camellia128";
1598 else if (flags
& PWMD_CIPHER_CAMELLIA192
)
1599 return "camellia192";
1600 else if (flags
& PWMD_CIPHER_CAMELLIA256
)
1601 return "camellia256";
1602 else if (flags
& PWMD_CIPHER_BLOWFISH
)
1604 else if (flags
& PWMD_CIPHER_CAST5
)
1606 else if (flags
& PWMD_CIPHER_3DES
)
1608 else if (flags
& PWMD_CIPHER_TWOFISH
)
1609 return "twofish256";
1610 else if (flags
& PWMD_CIPHER_TWOFISH128
)
1611 return "twofish128";
1616 /* To be called after read_file_header(). This sets the wanted algorithm from
1618 gpg_error_t
init_client_crypto2(const char *filename
,
1619 struct crypto_s
*crypto
)
1624 /* New file or conversion. */
1626 algo
= pwmd_cipher_to_gcrypt(PWMD_CIPHER_AES256
);
1627 else if (!crypto
->fh
->ver
.fh2
.flags
) {
1628 gchar
*tmp
= get_key_file_string(filename
? filename
: "global", "cipher");
1631 flags
= pwmd_cipher_str_to_cipher(tmp
);
1633 algo
= pwmd_cipher_to_gcrypt(flags
);
1634 crypto
->fh
->ver
.fh2
.flags
= flags
;
1637 algo
= pwmd_cipher_to_gcrypt(crypto
->fh
->ver
.fh2
.flags
);
1639 rc
= gcry_cipher_algo_info(algo
, GCRYCTL_TEST_ALGO
, NULL
, NULL
);
1644 rc
= gcry_cipher_algo_info(algo
, GCRYCTL_GET_KEYLEN
, NULL
,
1650 rc
= gcry_cipher_algo_info(algo
, GCRYCTL_GET_BLKLEN
, NULL
,
1651 &crypto
->blocksize
);
1657 gcry_cipher_close(crypto
->gh
);
1659 return gcry_cipher_open(&crypto
->gh
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
1662 struct crypto_s
*init_client_crypto()
1664 struct crypto_s
*new = g_malloc0(sizeof(struct crypto_s
));
1667 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1674 static gpg_error_t
convert_file(const gchar
*filename
, const gchar
*keyfile
,
1675 const gchar
*outfile
)
1678 guchar md5file
[gcry_md_get_algo_dlen(GCRY_MD_MD5
)];
1680 struct crypto_s
*crypto
= init_client_crypto();
1681 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1684 return GPG_ERR_ENOMEM
;
1686 crypto
->key
= gcry_malloc(hashlen
);
1689 cleanup_crypto(&crypto
);
1690 return GPG_ERR_ENOMEM
;
1693 log_write(N_("Converting version 1 data file '%s' to version 2 ..."),
1695 crypto
->fh
= read_file_header(filename
, TRUE
, &rc
);
1700 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
1702 /* The header in version 1 had a bug where the iterations were off-by-one.
1703 * So 0 iterations was really -1 in the header. This is fixed in v2 of the
1706 if (crypto
->fh
->ver
.fh1
.iter
!= -1) {
1708 rc
= parse_rcfile_keyfile(crypto
, keyfile
, TRUE
);
1713 gcry_md_hash_buffer(GCRY_MD_SHA256
, crypto
->key
, crypto
->tkey
,
1715 gcry_free(crypto
->tkey
);
1716 crypto
->tkey
= NULL
;
1719 rc
= get_password(filename
, crypto
, md5file
, crypto
->key
,
1727 rc
= init_client_crypto2(NULL
, crypto
);
1732 rc
= try_xml_decrypt(NULL
, crypto
, &crypto
->fh
->doc
, &crypto
->fh
->len
);
1737 rc
= convert_xml((gchar
**)&crypto
->fh
->doc
, &crypto
->fh
->len
);
1740 log_write("%s: %s", filename
, pwmd_strerror(rc
));
1744 crypto
->fh
->v1
= FALSE
;
1746 iter
= crypto
->fh
->ver
.fh1
.iter
+1;
1747 memset(&crypto
->fh
->ver
.fh2
, 0, sizeof(crypto
->fh
->ver
.fh2
));
1748 /* Keep the iterations and key from the original file. */
1749 crypto
->fh
->ver
.fh2
.iter
= iter
;
1750 rc
= export_common(outfile
, crypto
, crypto
->fh
->doc
, crypto
->fh
->len
);
1754 send_error(NULL
, rc
);
1756 /* fh->doc is freed from do_xml_decrypt() via the inbuf pointer. */
1757 cleanup_crypto(&crypto
);
1761 int main(int argc
, char *argv
[])
1764 struct sockaddr_un addr
;
1765 gchar buf
[PATH_MAX
];
1766 gchar
*socketpath
= NULL
, *socketdir
, *socketname
= NULL
;
1767 gchar
*socketarg
= NULL
;
1768 gchar
*datadir
= NULL
;
1772 gchar
**cache_push
= NULL
;
1773 gchar
*import
= NULL
, *keyfile
= NULL
;
1774 guint64 cmd_iterations
= 0UL;
1775 gboolean iterations_arg
= FALSE
;
1776 gint default_timeout
;
1777 gboolean rcfile_spec
= FALSE
;
1778 gint estatus
= EXIT_FAILURE
;
1780 gchar
*outfile
= NULL
;
1781 GMemVTable mtable
= { xmalloc
, xrealloc
, xfree
, xcalloc
, NULL
, NULL
};
1783 gboolean secure
= FALSE
;
1784 gint background
= 1;
1785 gchar
*convert
= NULL
;
1786 gint show_version
= 0;
1788 #ifdef WITH_PINENTRY
1789 gboolean disable_pinentry
= FALSE
;
1792 const struct option long_opts
[] = {
1793 #ifdef WITH_PINENTRY
1794 { "no-pinentry", 0, 0, 'P' },
1796 { "outfile", 1, 0, 'o' },
1797 { "convert", 1, 0, 'C' },
1798 { "no-fork", 0, 0, 'n' },
1799 { "disable-dump", 0, 0, 'D' },
1800 { "import", 1, 0, 'I' },
1801 { "iterations", 1, 0, 'i' },
1802 { "key-file", 1, 0, 'k' },
1803 { "rcfile", 1, 0, 'f' },
1804 { "version", 0, &show_version
, 1 },
1805 { "help", 0, &show_help
, 1 },
1808 #ifdef WITH_PINENTRY
1809 const gchar
*optstring
= "Po:C:nDI:i:k:f:";
1811 const gchar
*optstring
= "o:C:nDI:i:k:f:";
1814 #ifdef HAVE_SETRLIMIT
1817 rl
.rlim_cur
= rl
.rlim_max
= 0;
1819 if (setrlimit(RLIMIT_CORE
, &rl
) != 0)
1820 err(EXIT_FAILURE
, "setrlimit()");
1825 setlocale(LC_ALL
, "");
1826 bindtextdomain("pwmd", LOCALEDIR
);
1835 assuan_set_assuan_err_source(GPG_ERR_SOURCE_DEFAULT
);
1836 g_mem_set_vtable(&mtable
);
1837 assuan_set_malloc_hooks(xmalloc
, xrealloc
, xfree
);
1838 xmlMemSetup(xfree
, xmalloc
, xrealloc
, xstrdup
);
1843 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd", g_get_home_dir());
1845 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1846 err(EXIT_FAILURE
, "%s", buf
);
1848 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/data", g_get_home_dir());
1850 if (mkdir(buf
, 0700) == -1 && errno
!= EEXIST
)
1851 err(EXIT_FAILURE
, "%s", buf
);
1855 while ((opt
= getopt_long(argc
, argv
, optstring
, long_opts
, &opt_index
)) != -1) {
1859 usage(argv
[0], EXIT_SUCCESS
);
1862 printf(N_("%s\nCopyright (C) 2010 %s\nReleased under the terms of the GPL v2. Use at your own risk.\n\nCompile time features:\n%s"), PACKAGE_STRING
,
1864 #ifdef WITH_PINENTRY
1893 #ifdef WITH_PINENTRY
1895 disable_pinentry
= TRUE
;
1914 cmd_iterations
= strtoul(optarg
, NULL
, 10);
1916 if (cmd_iterations
== G_MAXULONG
) {
1917 log_write("%s", N_("invalid iteration count"));
1918 usage(argv
[0], EXIT_FAILURE
);
1921 iterations_arg
= TRUE
;
1927 rcfile
= g_strdup(optarg
);
1931 usage(argv
[0], EXIT_FAILURE
);
1935 pth_mutex_init(&cn_mutex
);
1936 pth_mutex_init(&cache_mutex
);
1937 pth_mutex_init(&rcfile_mutex
);
1938 #ifdef WITH_PINENTRY
1939 pth_mutex_init(&pin_mutex
);
1943 rcfile
= g_strdup_printf("%s/.pwmd/config", g_get_home_dir());
1945 if ((keyfileh
= parse_rcfile(rcfile_spec
)) == NULL
)
1948 #ifdef WITH_PINENTRY
1949 if (disable_pinentry
== TRUE
)
1950 g_key_file_set_boolean(keyfileh
, "global", "enable_pinentry", FALSE
);
1953 if (g_key_file_has_key(keyfileh
, "global", "syslog", NULL
) == TRUE
)
1954 log_syslog
= g_key_file_get_boolean(keyfileh
, "global", "syslog", NULL
);
1956 if (log_syslog
== TRUE
)
1957 openlog("pwmd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
1959 if (g_key_file_has_key(keyfileh
, "global", "priority", NULL
)) {
1960 x
= g_key_file_get_integer(keyfileh
, "global", "priority", NULL
);
1963 if (setpriority(PRIO_PROCESS
, 0, x
) == -1) {
1964 log_write("setpriority(): %s", strerror(errno
));
1969 #ifdef HAVE_MLOCKALL
1970 if (disable_mlock
== FALSE
&& mlockall(MCL_FUTURE
) == -1) {
1971 log_write("mlockall(): %s", strerror(errno
));
1978 usage(argv
[0], EXIT_FAILURE
);
1980 opt
= convert_file(convert
, keyfile
, outfile
);
1981 g_key_file_free(keyfileh
);
1983 exit(opt
? EXIT_FAILURE
: EXIT_SUCCESS
);
1988 usage(argv
[0], EXIT_FAILURE
);
1990 if (!iterations_arg
)
1991 cmd_iterations
= (guint64
)get_key_file_double("global", "iterations");
1993 opt
= xml_import(import
, outfile
, keyfile
, cmd_iterations
);
1994 g_key_file_free(keyfileh
);
1996 exit(opt
== FALSE
? EXIT_FAILURE
: EXIT_SUCCESS
);
1999 if ((p
= g_key_file_get_string(keyfileh
, "global", "socket_path", NULL
)) == NULL
)
2000 errx(EXIT_FAILURE
, N_("%s: socket_path not defined"), rcfile
);
2002 socketarg
= expand_homedir(p
);
2005 if ((p
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
)) == NULL
)
2006 errx(EXIT_FAILURE
, N_("%s: data_directory not defined"), rcfile
);
2008 datadir
= expand_homedir(p
);
2011 if (secure
== FALSE
&& g_key_file_has_key(keyfileh
, "global", "disable_list_and_dump", NULL
) == TRUE
) {
2012 n
= g_key_file_get_boolean(keyfileh
, "global", "disable_list_and_dump", NULL
);
2013 disable_list_and_dump
= n
;
2016 disable_list_and_dump
= secure
;
2018 if (g_key_file_has_key(keyfileh
, "global", "cache_timeout", NULL
) == TRUE
)
2019 default_timeout
= g_key_file_get_integer(keyfileh
, "global", "cache_timeout", NULL
);
2021 default_timeout
= -1;
2023 setup_logging(keyfileh
);
2025 if (g_key_file_has_key(keyfileh
, "global", "cache_push", NULL
) == TRUE
)
2026 cache_push
= g_key_file_get_string_list(keyfileh
, "global", "cache_push", NULL
, NULL
);
2028 if (argc
!= optind
) {
2029 for (; optind
< argc
; optind
++) {
2030 if (strv_printf(&cache_push
, "%s", argv
[optind
]) == FALSE
)
2031 errx(EXIT_FAILURE
, "%s", strerror(ENOMEM
));
2035 if (strchr(socketarg
, '/') == NULL
) {
2036 socketdir
= g_get_current_dir();
2037 socketname
= g_strdup(socketarg
);
2038 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2041 socketname
= g_strdup(strrchr(socketarg
, '/'));
2043 socketarg
[strlen(socketarg
) - strlen(socketname
) -1] = 0;
2044 socketdir
= g_strdup(socketarg
);
2045 socketpath
= g_strdup_printf("%s/%s", socketdir
, socketname
);
2048 if (chdir(datadir
)) {
2049 log_write("%s: %s", datadir
, strerror(errno
));
2054 if (parse_rcfile_keys() == FALSE
)
2057 clear_rcfile_keys();
2060 * Set the cache entry for a file. Prompts for the password.
2063 for (opt
= 0; cache_push
[opt
]; opt
++)
2064 do_cache_push(cache_push
[opt
], NULL
, 0);
2066 g_strfreev(cache_push
);
2067 log_write(background
? N_("Done. Daemonizing...") : N_("Done. Waiting for connections..."));
2071 * bind() doesn't like the full pathname of the socket or any non alphanum
2072 * characters so change to the directory where the socket is wanted then
2073 * create it then change to datadir.
2075 if (chdir(socketdir
)) {
2076 log_write("%s: %s", socketdir
, strerror(errno
));
2082 if ((sockfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
2083 log_write("socket(): %s", strerror(errno
));
2087 addr
.sun_family
= AF_UNIX
;
2088 g_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socketname
);
2090 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr
)) == -1) {
2091 log_write("bind(): %s", strerror(errno
));
2093 if (errno
== EADDRINUSE
)
2094 log_write(N_("Either there is another pwmd running or '%s' is a \n"
2095 "stale socket. Please remove it manually."), socketpath
);
2101 if (g_key_file_has_key(keyfileh
, "global", "socket_perms", NULL
) == TRUE
) {
2102 gchar
*t
= g_key_file_get_string(keyfileh
, "global", "socket_perms", NULL
);
2103 mode_t mode
= strtol(t
, NULL
, 8);
2104 mode_t mask
= umask(0);
2108 if (chmod(socketname
, mode
) == -1) {
2109 log_write("%s: %s", socketname
, strerror(errno
));
2119 g_free(--socketname
);
2121 if (chdir(datadir
)) {
2122 log_write("%s: %s", datadir
, strerror(errno
));
2130 if (listen(sockfd
, 0) == -1) {
2131 log_write("listen(): %s", strerror(errno
));
2136 unsetenv("DISPLAY");
2142 log_write("fork(): %s", strerror(errno
));
2155 server_loop(sockfd
, &socketpath
);
2156 estatus
= EXIT_SUCCESS
;
2159 if (socketpath
&& do_unlink
) {
2165 g_key_file_free(keyfileh
);
2168 xmlCleanupGlobals();
2170 if (estatus
== EXIT_SUCCESS
)
2171 log_write(N_("pwmd exiting normally"));
2173 #if defined(DEBUG) && !defined(MEM_DEBUG)