From patchwork Sat Apr 23 06:04:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Zaman X-Patchwork-Id: 8916941 Return-Path: X-Original-To: patchwork-selinux@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 4A8DF9FC74 for ; Sat, 23 Apr 2016 06:10:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7DA3620225 for ; Sat, 23 Apr 2016 06:10:17 +0000 (UTC) Received: from emvm-gh1-uea08.nsa.gov (smtp.nsa.gov [8.44.101.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 24DE12026F for ; Sat, 23 Apr 2016 06:10:16 +0000 (UTC) X-TM-IMSS-Message-ID: <64d17e4600007543@nsa.gov> Received: from tarius.tycho.ncsc.mil ([144.51.242.1]) by nsa.gov ([10.208.42.193]) with ESMTP (TREND IMSS SMTP Service 7.1) id 64d17e4600007543 ; Sat, 23 Apr 2016 02:07:26 -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 u3N684HF007815; Sat, 23 Apr 2016 02:08:04 -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 u3N65M05117826 for ; Sat, 23 Apr 2016 02:05:22 -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 u3N65MDJ007341 for ; Sat, 23 Apr 2016 02:05:22 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CtBACSDxtX/yQp0Apegw2Be7d2gmqBIAgXhXcCgW4BAQEBAQFmJ4RCAQEBAxIVGQEBNwEPUTQBBQEcGSKICAGgd4ExPjGKT4UoAQSMSQEBAQEBAQQCARcGCoQNjVwLQIJDh3mGUolJjhZjiEaFZY1xMIEOYoF4ggBfAYh6AQEB X-IPAS-Result: A0CtBACSDxtX/yQp0Apegw2Be7d2gmqBIAgXhXcCgW4BAQEBAQFmJ4RCAQEBAxIVGQEBNwEPUTQBBQEcGSKICAGgd4ExPjGKT4UoAQSMSQEBAQEBAQQCARcGCoQNjVwLQIJDh3mGUolJjhZjiEaFZY1xMIEOYoF4ggBfAYh6AQEB X-IronPort-AV: E=Sophos;i="5.24,520,1454994000"; d="scan'208";a="5403911" Received: from emsm-gh1-uea10.corp.nsa.gov (HELO emsm-gh1-uea10.nsa.gov) ([10.208.41.36]) by goalie.tycho.ncsc.mil with ESMTP; 23 Apr 2016 02:05:21 -0400 IronPort-PHdr: =?us-ascii?q?9a23=3Ace58xRxRkQCjmxnXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0ekeIJqq85mqBkHD//Il1AaPBtWLra0dwLuG+4nbGkU+or+5+EgYd5JNUxJXwe?= =?us-ascii?q?43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6anHS+4HYoFwnlMkIt?= =?us-ascii?q?f6KuSt6U0J38hrD60qaQSjsLrQL1Wal1IhSyoFeZnegtqqwmFJwMzADUqGBDYe?= =?us-ascii?q?VcyDAgD1uSmxHh+pX4p8Y7oGwD884motVNVaT8YrQQUa1TDDNgNXs8osLsq0rt?= =?us-ascii?q?VwyKs1kVSWIQ2ihBBgHDpEX4V4z2tAPiu/Bz2S+bOsuwRrcxD2fxp5x3QQPl3X?= =?us-ascii?q?9UfwUy93va35R9?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A1EQAQBwDxtXiELcVdFegw2Be7d2gmqBI?= =?us-ascii?q?AgXgXaEAQKBbgEBAQEBAQICDwEBAQgLCwkfgl59Wz0BAQEDEhUZAQE3AQ9RNAE?= =?us-ascii?q?FARwZIogIAaB3gTE+MYpPhSgBBIxJAQEBAQEBBAIBFwYKhA2NXAtAgkOHeYZSi?= =?us-ascii?q?UmOFmOIRoVljXEwgQ6CWh6BYl8BiHoBAQE?= X-IPAS-Result: =?us-ascii?q?A1EQAQBwDxtXiELcVdFegw2Be7d2gmqBIAgXgXaEAQKBbgE?= =?us-ascii?q?BAQEBAQICDwEBAQgLCwkfgl59Wz0BAQEDEhUZAQE3AQ9RNAEFARwZIogIAaB3g?= =?us-ascii?q?TE+MYpPhSgBBIxJAQEBAQEBBAIBFwYKhA2NXAtAgkOHeYZSiUmOFmOIRoVljXE?= =?us-ascii?q?wgQ6CWh6BYl8BiHoBAQE?= X-IronPort-AV: E=Sophos;i="5.24,520,1454976000"; d="scan'208";a="12950728" Received: from emvm-gh1-uea09.nsa.gov ([10.208.42.194]) by emsm-gh1-uea10.nsa.gov with ESMTP/TLS/DHE-RSA-AES256-SHA; 23 Apr 2016 06:05:21 +0000 X-TM-IMSS-Message-ID: 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 b0ad5b1a00006426 ; Sat, 23 Apr 2016 02:04:52 -0400 Received: by mail-pa0-f66.google.com with SMTP id zy2so12078199pac.2 for ; Fri, 22 Apr 2016 23:05:19 -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=vgvkY/gYQ67eBEkNpUF4tdlyL7s7gVangP1KonmcVu25/gY/pY5cuI4bolVzLZPyy7 u64VX3W8tFvrDFL/DeYS3yhuyoQWJs2ycH4Fi2e6A3j6W/ie5mdQrGf0v9DVNFxrmuCD KFvxz3SubZ1KcfosHLVIvSvp4Fk1kyl+PbsXmeUwl1E4S8Y46i4hacxWx+Xv4kV2+EjT N2BlhBq8bbwEQNJNUg5AQdOmlhtHqhFTm8mbmXlhAefafDkxXlt2zdCHCa7HTiDnuy6C ubf01nCr4nHE/N+Tu03BwhL/j8ty2ZaSnFdXlAXpAdQQDx5+OI0aLeiPIluIC27xf4Xm OxLA== 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=e0KeHC/KCvPGwChEkJqQJL25G8EZMuFu/A0lpk8HXDagHzEQg0fowQuuxqUhX1LWPj yHD70aObl6TEBHvb5GtIYJE6qaciN7h1uZkzsNolXQUdbVoBR9+DbSVgKQOheQ/AlbL9 lcREM+UEuOw63eUWrTxfRFg0zf8JDrHs9+wI65tRDEsBYCZCn2j8lfEnQgQ4drThNNni rKyPMdWhwWYsEUJGPQv8ONjEYC5gc6wvL/NqVTDjMPoau2hKzHwMXtaQCi2h2fCA6UdA hGFwsUI2HUSOX925AOwzeSKWWSw2al5aGnpaa+2StfXfUUn6pq0fT4Qr6DWnUNFdVKQ6 DN0Q== X-Gm-Message-State: AOPr4FWs9w5W6dKlzxl43BHmKpzv/W0zuJJKbv9pJSp7KjEcWXtxCWvZCuph0+o7opYMjQ== X-Received: by 10.66.148.35 with SMTP id tp3mr34236157pab.159.1461391518763; Fri, 22 Apr 2016 23:05:18 -0700 (PDT) Received: from localhost ([2404:e800:e600:38b:b52d:863:7600:b418]) by smtp.gmail.com with ESMTPSA id y27sm12347884pfi.11.2016.04.22.23.05.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Apr 2016 23:05:17 -0700 (PDT) From: Jason Zaman To: selinux@tycho.nsa.gov Subject: [PATCH v2 1/8] genhomedircon: factor out common replacement code Date: Sat, 23 Apr 2016 14:04:52 +0800 Message-Id: <1461391499-20593-2-git-send-email-jason@perfinion.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1461391499-20593-1-git-send-email-jason@perfinion.com> References: <1460131535-15688-1-git-send-email-jason@perfinion.com> <1461391499-20593-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. 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)