From patchwork Sat Apr 30 07:58:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Zaman X-Patchwork-Id: 8986641 Return-Path: X-Original-To: patchwork-selinux@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D0059BF29F for ; Sat, 30 Apr 2016 08:10:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D471A201B9 for ; Sat, 30 Apr 2016 08:10:36 +0000 (UTC) Received: from emsm-gh1-uea11.nsa.gov (emsm-gh1-uea11.nsa.gov [8.44.101.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7D44A20142 for ; Sat, 30 Apr 2016 08:10:35 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.24,555,1454976000"; d="scan'208";a="15771002" IronPort-PHdr: =?us-ascii?q?9a23=3AHDQYNhzxjJ0onm/XCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0e4WIJqq85mqBkHD//Il1AaPBtWLraIbwLOM6OjJYi8p39WoiDg6aptCVhsI24?= =?us-ascii?q?09vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6kO74TNaIBjjLw09?= =?us-ascii?q?fr2zQd6CyZTonLnooNX6WEZhunmUWftKNhK4rAHc5IE9oLBJDeIP8CbPuWZCYO?= =?us-ascii?q?9MxGlldhq5lhf44dqsrtY4q3wD86Fpy8kVSqj+fqIlXZREHT8mNCYz/8Stuh7d?= =?us-ascii?q?HiWV4X5JcGwNlRwAMQHA5ReyCpX4qSz8nvJwwiebO8zxS/Y/XjH0vPQjcwPhlC?= =?us-ascii?q?pSb21xy2rQkMEl1K8=3D?= X-IPAS-Result: =?us-ascii?q?A2EOBQDtZyRX/wHyM5BeHYJwK4FQu24fgXaFKUwBAQEBAQE?= =?us-ascii?q?CAmIngi19Wz0CAQMBAg8VEwYBAQwgCwECAwkBARcpCAgDAS0DAQUBCxEOCwUYB?= =?us-ascii?q?AGICAGkeIExPjGKVIUoAQSMLwEBCAIYBgqEDYplEQGFcod8hlOJSo4aY4hHJYV?= =?us-ascii?q?AjXMwgQ5ig3hfAYZHgTUBAQE?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Apr 2016 08:10:02 +0000 Received: from prometheus.infosec.tycho.ncsc.mil (prometheus [192.168.25.40]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u3U89CX2018704; Sat, 30 Apr 2016 04:09:39 -0400 Received: from tarius.tycho.ncsc.mil (tarius.infosec.tycho.ncsc.mil [144.51.242.1]) by prometheus.infosec.tycho.ncsc.mil (8.15.2/8.15.2) with ESMTP id u3U7wg8N229782 for ; Sat, 30 Apr 2016 03:58:42 -0400 Received: from goalie.tycho.ncsc.mil (goalie [144.51.242.250]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u3U7wahg016059 for ; Sat, 30 Apr 2016 03:58:42 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1DpAADiZCRXiMHAVdFdgw2Be7djhAsIF4V5AoEkTAEBAQEBARMBAQEICwsJH4RzAgEDEhUZAQE3AQ9RNAEFARwZIogIAaR2gTE+MYpUhSgBBIwvAQEBAQEBBAIBFwYKhA2NWwtAgkOHfIZTiUqOGmOIR4VljXMwgQ6CWh6BYl8Bh3wBAQE X-IPAS-Result: A1DpAADiZCRXiMHAVdFdgw2Be7djhAsIF4V5AoEkTAEBAQEBARMBAQEICwsJH4RzAgEDEhUZAQE3AQ9RNAEFARwZIogIAaR2gTE+MYpUhSgBBIwvAQEBAQEBBAIBFwYKhA2NWwtAgkOHfIZTiUqOGmOIR4VljXMwgQ6CWh6BYl8Bh3wBAQE X-IronPort-AV: E=Sophos;i="5.24,555,1454994000"; d="scan'208";a="5420258" Received: from emsm-gh1-uea11.corp.nsa.gov (HELO emsm-gh1-uea11.nsa.gov) ([10.208.41.37]) by goalie.tycho.ncsc.mil with ESMTP; 30 Apr 2016 03:58:42 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3A7rhOhRZ6AlPJ5o+3QjtvteT/LSx+4OfEezUN459i?= =?us-ascii?q?sYplN5qZpci6bnLW6fgltlLVR4KTs6sC0LqG9fC/EjVaut6oizMrTt9lb1c9k8?= =?us-ascii?q?IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUiv2OQc9?= =?us-ascii?q?HOnpAIma153xjLDivc2MKFwS33KUWvBbElaflU3prM4YgI9veO4a6yDihT92Qd?= =?us-ascii?q?lQ3n5iPlmJnhzxtY+a9Z9n9DlM6bp6r5YTGfayQ6NtVrFcDTI7I0gp9cbrsl/F?= =?us-ascii?q?VgLJ6XwCAUsMlR8dIQHe6xeybJ70uy2y4uh0wy6cFdX7V741XzWl6eFgTxq+23?= =?us-ascii?q?RPDCIw7GyC0p84t6lcuh/0/xE=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0HQAACqZCRXiMHAVdFdgw2Be7djhAsIF?= =?us-ascii?q?4F2hAMCgSRMAQEBAQEBAgIPAQEBCAsLCR8xgi19Wz0CAQMSFRkBATcBD1E0AQU?= =?us-ascii?q?BHBkiiAgBpHWBMT4xilSFKAEEjC8BAQEBAQEEAgEXBgqEDY1bC0CCQ4d8hlOJS?= =?us-ascii?q?o4aY4hHhWWNczCBDoJaHoFiXwGHfAEBAQ?= X-IPAS-Result: =?us-ascii?q?A0HQAACqZCRXiMHAVdFdgw2Be7djhAsIF4F2hAMCgSRMAQE?= =?us-ascii?q?BAQEBAgIPAQEBCAsLCR8xgi19Wz0CAQMSFRkBATcBD1E0AQUBHBkiiAgBpHWBM?= =?us-ascii?q?T4xilSFKAEEjC8BAQEBAQEEAgEXBgqEDY1bC0CCQ4d8hlOJSo4aY4hHhWWNczC?= =?us-ascii?q?BDoJaHoFiXwGHfAEBAQ?= X-IronPort-AV: E=Sophos;i="5.24,555,1454976000"; d="scan'208";a="15770940" Received: from mail-pf0-f193.google.com ([209.85.192.193]) by emsm-gh1-uea11.nsa.gov with ESMTP/TLS/AES128-GCM-SHA256; 30 Apr 2016 07:58:41 +0000 Received: by mail-pf0-f193.google.com with SMTP id e190so16806342pfe.0 for ; Sat, 30 Apr 2016 00:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perfinion-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=E4ZCYoo+DhdE22iUfIlRJSbjrYPcWjHXj6zJy19W0D8=; b=hKGFCJWXN6DXoRCEQwdTK/+lfulNLpyFTsQWjltLUCOhtBM4dy8XogfPYRjtZtMvLi ai/H9L3GUf0W6mdTvkKal59zSmS6X4OuNlquCuGeRgPXu+3+K9GDN2FgEpZuNzpJxAjH QMrqJYDdBhJ6bCtwgUYwx9d98WBOug59+4p8uPN9ETTwc1tkz/jfa3TBNxpKqyzzuQQV huyF1Rs2tY+8LzX3vEVnaKM10pdsgGfYqdLVzs4iTwynUeMQnKf1bzj/BdVeyv5hE1dF aGt23+WZX0g1oV4CWO18pDuPOeoq2kBlEO/+Abx7MlJGUVN6zs+S8g1uNql6dV3lvDyz g9ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=E4ZCYoo+DhdE22iUfIlRJSbjrYPcWjHXj6zJy19W0D8=; b=G+lm4+m9SRFISN8+1OmO3sndhK+BuW1G4bRIvS+ypRYvIEoBuCtqacbAp6k/p+weFG 48OJEmYV8FqCTUrYZhojtPgJhwCTDZeWmjNuMtl4JlXKAVLJzu9cYEVjrO6z1PIqwPn1 M51G2LRBqy/9ld8PhMNOt3Av20NaQzT+k/XJTCPjZjj3tHrf95mdizeBDtlSslFaJqfs aoqDMCyLKWc9TrUDR1RG9TpMbKNkftaGyc84EyzDKc04wmR0KNRL+UVb5gp+C63fmYbE H8UIl0gjEb5oOZd9TGtq5rbfKGOjl3FhOYKnWGV1Rh7nl1kqs/9/zr2UOyWx3yK8oiwS fu5Q== X-Gm-Message-State: AOPr4FWSivtcD4scWlt5rUwLFegvro3TIif1TsWXqr4tSkXFuqL/ViPPuYSwzSw9NqM7Xg== X-Received: by 10.98.66.145 with SMTP id h17mr16912347pfd.100.1462003120526; Sat, 30 Apr 2016 00:58:40 -0700 (PDT) Received: from localhost ([2404:e800:e600:38b:e5d4:5e0:f7e0:81be]) by smtp.gmail.com with ESMTPSA id 184sm29003000pfd.43.2016.04.30.00.58.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 30 Apr 2016 00:58:39 -0700 (PDT) From: Jason Zaman To: selinux@tycho.nsa.gov Subject: [PATCH v4 1/7] genhomedircon: factor out common replacement code Date: Sat, 30 Apr 2016 15:58:06 +0800 Message-Id: <1462003092-4611-2-git-send-email-jason@perfinion.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1462003092-4611-1-git-send-email-jason@perfinion.com> References: <1462003092-4611-1-git-send-email-jason@perfinion.com> X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: Cc: sds@tycho.nsa.gov MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP All the write_*_contexts() methods use exactly the same code. This splits it off into a common helper function. Signed-off-by: Jason Zaman --- libsemanage/src/genhomedircon.c | 70 ++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c index 1a9e87e..09c2a10 100644 --- a/libsemanage/src/genhomedircon.c +++ b/libsemanage/src/genhomedircon.c @@ -548,23 +548,12 @@ static int check_line(genhomedircon_settings_t * s, Ustr *line) return result; } -static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out, - semanage_list_t * tpl, const char *user, - const char *seuser, const char *home, - const char *role_prefix, const char *level) +static int write_replacements(genhomedircon_settings_t * s, FILE * out, + const semanage_list_t * tpl, + const replacement_pair_t *repl) { - replacement_pair_t repl[] = { - {.search_for = TEMPLATE_SEUSER,.replace_with = seuser}, - {.search_for = TEMPLATE_HOME_DIR,.replace_with = home}, - {.search_for = TEMPLATE_ROLE,.replace_with = role_prefix}, - {.search_for = TEMPLATE_LEVEL,.replace_with = level}, - {NULL, NULL} - }; Ustr *line = USTR_NULL; - if (fprintf(out, COMMENT_USER_HOME_CONTEXT, user) < 0) - return STATUS_ERR; - for (; tpl; tpl = tpl->next) { line = replace_all(tpl->data, repl); if (!line) @@ -582,6 +571,25 @@ static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out, return STATUS_ERR; } +static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out, + semanage_list_t * tpl, const char *user, + const char *seuser, const char *home, + const char *role_prefix, const char *level) +{ + replacement_pair_t repl[] = { + {.search_for = TEMPLATE_SEUSER,.replace_with = seuser}, + {.search_for = TEMPLATE_HOME_DIR,.replace_with = home}, + {.search_for = TEMPLATE_ROLE,.replace_with = role_prefix}, + {.search_for = TEMPLATE_LEVEL,.replace_with = level}, + {NULL, NULL} + }; + + if (fprintf(out, COMMENT_USER_HOME_CONTEXT, user) < 0) + return STATUS_ERR; + + return write_replacements(s, out, tpl, repl); +} + static int write_home_root_context(genhomedircon_settings_t * s, FILE * out, semanage_list_t * tpl, char *homedir) { @@ -589,23 +597,8 @@ static int write_home_root_context(genhomedircon_settings_t * s, FILE * out, {.search_for = TEMPLATE_HOME_ROOT,.replace_with = homedir}, {NULL, NULL} }; - Ustr *line = USTR_NULL; - - for (; tpl; tpl = tpl->next) { - line = replace_all(tpl->data, repl); - if (!line) - goto fail; - if (check_line(s, line) == STATUS_SUCCESS) { - if (!ustr_io_putfileline(&line, out)) - goto fail; - } - ustr_sc_free(&line); - } - return STATUS_SUCCESS; - fail: - ustr_sc_free(&line); - return STATUS_ERR; + return write_replacements(s, out, tpl, repl); } static int write_user_context(genhomedircon_settings_t * s, FILE * out, @@ -618,23 +611,8 @@ static int write_user_context(genhomedircon_settings_t * s, FILE * out, {.search_for = TEMPLATE_SEUSER,.replace_with = seuser}, {NULL, NULL} }; - Ustr *line = USTR_NULL; - - for (; tpl; tpl = tpl->next) { - line = replace_all(tpl->data, repl); - if (!line) - goto fail; - if (check_line(s, line) == STATUS_SUCCESS) { - if (!ustr_io_putfileline(&line, out)) - goto fail; - } - ustr_sc_free(&line); - } - return STATUS_SUCCESS; - fail: - ustr_sc_free(&line); - return STATUS_ERR; + return write_replacements(s, out, tpl, repl); } static int user_sort_func(semanage_user_t ** arg1, semanage_user_t ** arg2)