From patchwork Fri Apr 8 16:05:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Zaman X-Patchwork-Id: 8784381 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 4826DC0553 for ; Fri, 8 Apr 2016 16:13:00 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7FBFF202B8 for ; Fri, 8 Apr 2016 16:12:59 +0000 (UTC) Received: from emvm-gh1-uea09.nsa.gov (emvm-gh1-uea09.nsa.gov [8.44.101.9]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 56A932011D for ; Fri, 8 Apr 2016 16:12:58 +0000 (UTC) X-TM-IMSS-Message-ID: <6598b15a000a10ab@nsa.gov> Received: from tarius.tycho.ncsc.mil ([144.51.242.1]) by nsa.gov ([10.208.42.194]) with ESMTP (TREND IMSS SMTP Service 7.1) id 6598b15a000a10ab ; Fri, 8 Apr 2016 12:09:41 -0400 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 u38GAbpG022181; Fri, 8 Apr 2016 12:10: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 u38G6GQn083018 for ; Fri, 8 Apr 2016 12:06:16 -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 u38G6God021115 for ; Fri, 8 Apr 2016 12:06:16 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1AyCgBH1gdXd0LcVdFcgwyBe7g2gmyBJIYNAoF/AQEBAQEBEwEKCxQhcAwBg3QBAQEDEhUZAQE3AQ9RNAEFARw7iAUBoRuBMT4xik+FKAEEjT4BAQEBAQEEAgEXBgqEDo1bC0CCQ4d1hlGJQ44OZIhFhWKNaC+BDoJZgX1fAYEfiBkBAQE X-IPAS-Result: A1AyCgBH1gdXd0LcVdFcgwyBe7g2gmyBJIYNAoF/AQEBAQEBEwEKCxQhcAwBg3QBAQEDEhUZAQE3AQ9RNAEFARw7iAUBoRuBMT4xik+FKAEEjT4BAQEBAQEEAgEXBgqEDo1bC0CCQ4d1hlGJQ44OZIhFhWKNaC+BDoJZgX1fAYEfiBkBAQE X-IronPort-AV: E=Sophos;i="5.24,454,1454994000"; d="scan'208";a="5363083" Received: from emvm-gh1-uea09.nsa.gov ([10.208.42.194]) by goalie.tycho.ncsc.mil with ESMTP; 08 Apr 2016 12:06:14 -0400 X-TM-IMSS-Message-ID: <659494a6000a0e77@nsa.gov> Received: from mail-pa0-f66.google.com (mail-pa0-f66.google.com [209.85.220.66]) by nsa.gov ([10.208.42.194]) with ESMTP (TREND IMSS SMTP Service 7.1; TLSv1/SSLv3 AES128-SHA (128/128)) id 659494a6000a0e77 ; Fri, 8 Apr 2016 12:05:11 -0400 Received: by mail-pa0-f66.google.com with SMTP id hb4so9370408pac.1 for ; Fri, 08 Apr 2016 09:06:13 -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=HHFK3GHT4JzGhrlwrQ0HGUvSgpZRqVqJPYtNlMRpO94=; b=UwKJqMMISJ/8z6mXlirEtqMDaDS5H8/3SlZ5BanABoYi4FqGdyE0/nS1VPxcByi2kl JUw75JhWxPCLtSaKKJYITN5JJxqw4Wb3fYAHpok5ZhAcMHw5FuLozyj6nlGzPtU31LB9 DVJiInKKwxv5yvU85c5eInQd19Eu9O0mcgAYpREJOx4tT3i/JaVMilc6Z+aRfBSmEHnO alpz3cm8OZpItfvD3Afrol3fyVg9Uwq3T14IlYN1ITiVFhU7TXXOW8z230189etq4e+z NKtMNJZ0iOb4Eou0X3uzbH/o2gsOrV9JU0RFofJxnw5V8t3moE50rKfxPAtkqn0Zl11v UCng== 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=HHFK3GHT4JzGhrlwrQ0HGUvSgpZRqVqJPYtNlMRpO94=; b=kmuHucsDb1T8myagJ2Lv4kUAszkZ6Syk47Xh5DFARocgran2DxJ0x6dBajg/NLSEf8 /P9+hDVStUMwqQ4mwgGeUI4sNXMH9Lw1ug8NApgU3Pj0Je306GDMpI8FnJ1YGVRTnX1L RFNdIrUaHNq9I1X51/e8p5HsDt3da9z0beOsqD5lpFgIjaheHMpn80HegkhhV7filG5N yGPlFVGVK4e3F1xUqEBxWHixdUY6Y6pTxZ2R0LzPN+fghNPduRvMa3d6rKhnHjsu02nV wnkkBgfT1kBRsWmebro/hr2wUEagsKCS/QUcVW5niktar2eF1hzqP1hRvvHW8JnikTZD ZriQ== X-Gm-Message-State: AD7BkJLqrTo+QdGzMT5tK5tXf8UsGBLhljxeYyAbgJtX2o70JTu6olhuafHmrSZgsA6A9A== X-Received: by 10.66.136.41 with SMTP id px9mr13824700pab.80.1460131572429; Fri, 08 Apr 2016 09:06:12 -0700 (PDT) Received: from localhost ([2404:e800:e600:38b:5121:e6bd:6ddf:d757]) by smtp.gmail.com with ESMTPSA id p26sm19684211pfi.84.2016.04.08.09.06.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Apr 2016 09:06:11 -0700 (PDT) From: Jason Zaman To: selinux@tycho.nsa.gov Subject: [PATCH 1/7] genhomedircon: factor out common replacement code Date: Sat, 9 Apr 2016 00:05:29 +0800 Message-Id: <1460131535-15688-2-git-send-email-jason@perfinion.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1460131535-15688-1-git-send-email-jason@perfinion.com> References: <20160201093633.GB21978@meriadoc.perfinion.com> <1460131535-15688-1-git-send-email-jason@perfinion.com> X-TM-AS-MML: disable 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: 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. --- libsemanage/src/genhomedircon.c | 69 ++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 46 deletions(-) diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c index 1a9e87e..a95ab16 100644 --- a/libsemanage/src/genhomedircon.c +++ b/libsemanage/src/genhomedircon.c @@ -548,23 +548,11 @@ 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, + semanage_list_t * tpl, 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 +570,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 +596,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 +610,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)