From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 53B876DE0999 for ; Wed, 7 Dec 2016 11:32:25 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0.52 X-Spam-Level: X-Spam-Status: No, score=0.52 tagged_above=-999 required=5 tests=[AWL=-0.132, SPF_NEUTRAL=0.652] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NQfbtKcoQJe7 for ; Wed, 7 Dec 2016 11:32:23 -0800 (PST) Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34]) by arlo.cworth.org (Postfix) with ESMTP id E7F886DE0943 for ; Wed, 7 Dec 2016 11:32:22 -0800 (PST) Received: from guru.guru-group.fi (localhost [IPv6:::1]) by guru.guru-group.fi (Postfix) with ESMTP id BFAE110005A; Wed, 7 Dec 2016 21:32:51 +0200 (EET) From: Tomi Ollila To: Ioan-Adrian Ratiu , notmuch@notmuchmail.org Subject: Re: [PATCH v4 2/2] notmuch-config: replace config reading function In-Reply-To: <20161205144606.4754-3-adi@adirat.com> References: <20161205144606.4754-1-adi@adirat.com> <20161205144606.4754-3-adi@adirat.com> User-Agent: Notmuch/0.23.1+52~ga6dbf3a (https://notmuchmail.org) Emacs/24.5.1 (x86_64-unknown-linux-gnu) X-Face: HhBM'cA~ MIME-Version: 1.0 Content-Type: text/plain X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Dec 2016 19:32:25 -0000 On Mon, Dec 05 2016, Ioan-Adrian Ratiu wrote: > 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 > --- > notmuch-config.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++---------- > 1 file changed, 48 insertions(+), 10 deletions(-) > > diff --git a/notmuch-config.c b/notmuch-config.c > index bd52790..fe16fa3 100644 > --- a/notmuch-config.c > +++ b/notmuch-config.c > @@ -205,32 +205,70 @@ 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; > } > - } else { > - fprintf (stderr, "Error reading configuration file %s: %s\n", > - config->filename, error->message); > } > > + 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; > + } > + } > + } > + > + 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, strlen(config_str), How sticky can this strlen() be ? 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: > + fclose(fp); need to check that fp != NULL -- fclose() manual page does not state calling fclose(NULL) is supported. > + if (config_str) talloc_free(config_str); You copied my code verbatim ;/ better write if() and talloc_free() to separate lines (also fclose() when updated) no other comments from me. Tomi > return ret; > } > > -- > 2.10.2