From: Ioan-Adrian Ratiu <adi@adirat.com>
To: <notmuch@notmuchmail.org>, <tomi.ollila@iki.fi>
Subject: [PATCH v5 2/2] notmuch-config: replace config reading function
Date: Thu, 8 Dec 2016 14:24:45 +0200 [thread overview]
Message-ID: <20161208122445.30918-3-adi@adirat.com> (raw)
In-Reply-To: <20161208122445.30918-1-adi@adirat.com>
Config files are currently read using glib's g_key_file_load_from_file
function which is very inconvenient because it's limited by design to read
only from "regular data files" in a filesystem. Because of this limitation
notmuch can't read configs from pipes, fifos, sockets, stdin, etc. Not even
"notmuch --config=/dev/stdin" works:
Error reading configuration file /dev/stdin: Not a regular file
So replace g_key_file_load_from_file with g_key_file_load_from_data which
gives us much more freedom to read configs from multiple sources.
This also helps the more security sensitive users: If someone has private
information in the config file, it can be encrypted on disk, then decrypted
in RAM and passed through a pipe directly to notmuch without the use of
intermediate plain text files.
Signed-off-by: Ioan-Adrian Ratiu <adi@adirat.com>
---
notmuch-config.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 52 insertions(+), 10 deletions(-)
diff --git a/notmuch-config.c b/notmuch-config.c
index bd52790..30823cb 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -205,32 +205,74 @@ get_username_from_passwd_file (void *ctx)
static notmuch_bool_t
get_config_from_file (notmuch_config_t *config, notmuch_bool_t create_new)
{
+ #define BUF_SIZE 4096
+ char *config_str = NULL;
+ int config_len = 0;
+ int config_bufsize = BUF_SIZE;
+ size_t len;
GError *error = NULL;
notmuch_bool_t ret = FALSE;
- if (g_key_file_load_from_file (config->key_file, config->filename,
- G_KEY_FILE_KEEP_COMMENTS, &error))
- return TRUE;
-
- if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) {
+ FILE *fp = fopen(config->filename, "r");
+ if (fp == NULL) {
/* If create_new is true, then the caller is prepared for a
* default configuration file in the case of FILE NOT FOUND.
*/
if (create_new) {
config->is_new = TRUE;
ret = TRUE;
+ goto out;
} else {
- fprintf (stderr, "Configuration file %s not found.\n"
+ fprintf (stderr, "Error opening config file '%s': %s\n"
"Try running 'notmuch setup' to create a configuration.\n",
- config->filename);
+ config->filename, strerror(errno));
+ goto out;
+ }
+ }
+
+ config_str = talloc_zero_array (config, char, config_bufsize);
+ if (config_str == NULL) {
+ fprintf (stderr, "Error reading '%s': Out of memory\n", config->filename);
+ goto out;
+ }
+
+ while ((len = fread (config_str + config_len, 1,
+ config_bufsize - config_len, fp)) > 0) {
+ config_len += len;
+ if (config_len == config_bufsize) {
+ config_bufsize += BUF_SIZE;
+ config_str = talloc_realloc (config, config_str, char, config_bufsize);
+ if (config_str == NULL) {
+ fprintf (stderr, "Error reading '%s': Failed to reallocate memory\n",
+ config->filename);
+ goto out;
+ }
}
- } else {
- fprintf (stderr, "Error reading configuration file %s: %s\n",
- config->filename, error->message);
}
+ if (ferror (fp)) {
+ fprintf (stderr, "Error reading '%s': I/O error\n", config->filename);
+ goto out;
+ }
+
+ if (g_key_file_load_from_data (config->key_file, config_str, config_len,
+ G_KEY_FILE_KEEP_COMMENTS, &error)) {
+ ret = TRUE;
+ goto out;
+ }
+
+ fprintf (stderr, "Error parsing config file '%s': %s\n",
+ config->filename, error->message);
+
g_error_free (error);
+out:
+ if (fp)
+ fclose(fp);
+
+ if (config_str)
+ talloc_free(config_str);
+
return ret;
}
--
2.10.2
next prev parent reply other threads:[~2016-12-08 12:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-08 12:24 [PATCH v5 0/2] Refactor config reading to support non-regular files Ioan-Adrian Ratiu
2016-12-08 12:24 ` [PATCH v5 1/2] cli: abstract config file reading to a separate function Ioan-Adrian Ratiu
2016-12-08 12:24 ` Ioan-Adrian Ratiu [this message]
2016-12-10 19:13 ` [PATCH v5 2/2] notmuch-config: replace config reading function Tomi Ollila
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161208122445.30918-3-adi@adirat.com \
--to=adi@adirat.com \
--cc=notmuch@notmuchmail.org \
--cc=tomi.ollila@iki.fi \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).