From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id C2C55429E35 for ; Sat, 22 Jun 2013 21:24:52 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.099 X-Spam-Level: X-Spam-Status: No, score=-0.099 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XdtX7ZoVhYNC for ; Sat, 22 Jun 2013 21:24:46 -0700 (PDT) Received: from mail-pd0-f180.google.com (mail-pd0-f180.google.com [209.85.192.180]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 756EE431FAE for ; Sat, 22 Jun 2013 21:24:32 -0700 (PDT) Received: by mail-pd0-f180.google.com with SMTP id 10so193959pdi.11 for ; Sat, 22 Jun 2013 21:24:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=TdXdXI6sKbuEWN5l8tT+T9wOym3gupUFWXjcwz1c4AA=; b=PI8q6xWpUq46+LANj2AA8l4EpOZk5BIfhbHKlIk1pdrKhOSDy8LOO/1NWHR+WJb5mQ q2hB4/wpzqyoUF+rx28k2DBM3aQP+W6WTFw6ahidbr8pfXALmSl4Fa+hiMY4G7V3r50f v5lmCgMnnOwi975qh1aRdzD0VoT4ga7bbtVoQVeaaf/50e4jv7kNPxpBWDHnd/VfdLCg q8IaRrlBO0vaKplJy1jJc9paWUWxLT6rMmeAU3XiiccdxhrclyVVJpIutb9B25m/i3EG kzDHoBy4LQdnagWQCDpM7g8Fjym/I0gqF1hCn9eFoFWOM3fThfCbw8d361Jv3TdhF1WU ZKmg== X-Received: by 10.67.21.229 with SMTP id hn5mr22609201pad.135.1371961471623; Sat, 22 Jun 2013 21:24:31 -0700 (PDT) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPSA id kc8sm5959550pbc.18.2013.06.22.21.24.29 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 22 Jun 2013 21:24:30 -0700 (PDT) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH v7 07/12] insert: add --folder option Date: Sun, 23 Jun 2013 14:24:00 +1000 Message-Id: <1371961445-15182-8-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1371961445-15182-1-git-send-email-novalazy@gmail.com> References: <1371961445-15182-1-git-send-email-novalazy@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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: Sun, 23 Jun 2013 04:24:53 -0000 Allow the new message to be inserted into a folder within the Maildir hierarchy instead of the top-level folder. --- notmuch-insert.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 591189f..5a4b3ea 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -83,6 +83,23 @@ sync_dir (const char *dir) return ret; } +/* Check the specified folder name does not contain a directory + * component ".." to prevent writes outside of the Maildir hierarchy. */ +static notmuch_bool_t +check_folder_name (const char *folder) +{ + const char *p = folder; + + for (;;) { + if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/')) + return FALSE; + p = strchr (p, '/'); + if (!p) + return TRUE; + p++; + } +} + /* Open a unique file in the 'tmp' sub-directory of dir. * Returns the file descriptor on success, or -1 on failure. * On success, file paths for the message in the 'tmp' and 'new' @@ -286,11 +303,24 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) size_t new_tags_length; tag_op_list_t *tag_ops; char *query_string = NULL; + const char *folder = NULL; const char *maildir; - int opt_index = 1; + int opt_index; unsigned int i; notmuch_bool_t ret; + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, + { NOTMUCH_OPT_END, 0, 0, 0, 0 } + }; + + opt_index = parse_arguments (argc, argv, options, 1); + + if (opt_index < 0) { + /* diagnostics already printed */ + return 1; + } + db_path = notmuch_config_get_database_path (config); new_tags = notmuch_config_get_new_tags (config, &new_tags_length); @@ -313,7 +343,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) return 1; } - maildir = db_path; + if (folder == NULL) { + maildir = db_path; + } else { + if (! check_folder_name (folder)) { + fprintf (stderr, "Error: bad folder name: %s\n", folder); + return 1; + } + maildir = talloc_asprintf (config, "%s/%s", db_path, folder); + if (! maildir) { + fprintf (stderr, "Out of memory\n"); + return 1; + } + } /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying * from standard input may be interrupted. */ -- 1.7.12.1