4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina/*
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina Authors:
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina Pavel Březina <pbrezina@redhat.com>
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina Copyright (C) 2016 Red Hat
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina This program is free software; you can redistribute it and/or modify
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina it under the terms of the GNU General Public License as published by
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina the Free Software Foundation; either version 3 of the License, or
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina (at your option) any later version.
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina This program is distributed in the hope that it will be useful,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina GNU General Public License for more details.
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina You should have received a copy of the GNU General Public License
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina*/
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina#include "responder/nss/nss_protocol.h"
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina#include "util/sss_nss.h"
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinastatic uint32_t
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinanss_get_gid(struct sss_domain_info *domain,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct ldb_message *msg)
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina{
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t gid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* First, try to return overriden gid. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (DOM_HAS_VIEWS(domain)) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina gid = ldb_msg_find_attr_as_uint64(msg, OVERRIDE_PREFIX SYSDB_GIDNUM,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina 0);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (gid != 0) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return gid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Try to return domain gid override. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (domain->override_gid != 0) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return domain->override_gid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Return original gid. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return ldb_msg_find_attr_as_uint64(msg, SYSDB_GIDNUM, 0);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina}
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinastatic const char *
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinanss_get_homedir_override(TALLOC_CTX *mem_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct ldb_message *msg,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct nss_ctx *nctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sss_domain_info *dom,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sss_nss_homedir_ctx *homedir_ctx)
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina{
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *homedir;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina homedir = sss_view_ldb_msg_find_attr_as_string(dom, msg, SYSDB_HOMEDIR,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina NULL);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina homedir_ctx->original = homedir;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Check to see which homedir_prefix to use. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (dom->homedir_substr != NULL) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina homedir_ctx->config_homedir_substr = dom->homedir_substr;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina } else if (nctx->homedir_substr != NULL) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina homedir_ctx->config_homedir_substr = nctx->homedir_substr;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Check whether we are unconditionally overriding the server
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina * for home directory locations.
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (dom->override_homedir) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return expand_homedir_template(mem_ctx, dom->override_homedir,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina dom->case_preserve, homedir_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina } else if (nctx->override_homedir) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return expand_homedir_template(mem_ctx, nctx->override_homedir,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina dom->case_preserve, homedir_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (!homedir || *homedir == '\0') {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* In the case of a NULL or empty homedir, check to see if
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina * we have a fallback homedir to use.
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (dom->fallback_homedir) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return expand_homedir_template(mem_ctx, dom->fallback_homedir,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina dom->case_preserve, homedir_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina } else if (nctx->fallback_homedir) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return expand_homedir_template(mem_ctx, nctx->fallback_homedir,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina dom->case_preserve, homedir_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Provider can also return template, try to expand it.*/
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return expand_homedir_template(mem_ctx, homedir,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina dom->case_preserve, homedir_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina}
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinastatic const char *
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinanss_get_homedir(TALLOC_CTX *mem_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct nss_ctx *nss_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sss_domain_info *domain,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct ldb_message *msg,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *orig_name,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *upn,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uid_t uid)
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina{
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sss_nss_homedir_ctx hd_ctx = { 0 };
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *homedir;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina hd_ctx.username = orig_name;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina hd_ctx.uid = uid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina hd_ctx.domain = domain->name;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina hd_ctx.upn = upn;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina homedir = nss_get_homedir_override(mem_ctx, msg, nss_ctx, domain, &hd_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (homedir == NULL) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return "/";
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return homedir;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina}
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashovstatic errno_t
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashovnss_get_shell(struct nss_ctx *nss_ctx,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov struct sss_domain_info *domain,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov struct ldb_message *msg,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov const char *name,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov uint32_t uid,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov const char **_shell)
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov{
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov const char *shell = NULL;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov if (nss_ctx->rctx->sr_conf.scope == SESSION_RECORDING_SCOPE_ALL) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov shell = SESSION_RECORDING_SHELL;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov } else if (nss_ctx->rctx->sr_conf.scope ==
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov SESSION_RECORDING_SCOPE_SOME) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov const char *sr_enabled;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov sr_enabled = ldb_msg_find_attr_as_string(
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov msg, SYSDB_SESSION_RECORDING, NULL);
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov if (sr_enabled == NULL) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov "%s attribute not found for %s[%u]! Skipping\n",
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov SYSDB_SESSION_RECORDING, name, uid);
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov return EINVAL;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov } else if (strcmp(sr_enabled, "TRUE") == 0) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov shell = SESSION_RECORDING_SHELL;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov } else if (strcmp(sr_enabled, "FALSE") != 0) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov "Skipping %s[%u] "
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov "because its %s attribute value is invalid: %s\n",
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov name, uid, SYSDB_SESSION_RECORDING, sr_enabled);
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov return EINVAL;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov }
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov }
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov if (shell == NULL) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov shell = sss_resp_get_shell_override(msg, nss_ctx->rctx, domain);
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov }
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov *_shell = shell;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov return EOK;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov}
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinastatic errno_t
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinanss_get_pwent(TALLOC_CTX *mem_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct nss_ctx *nss_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sss_domain_info *domain,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct ldb_message *msg,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t *_uid,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t *_gid,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string **_name,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string *_gecos,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string *_homedir,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string *_shell)
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina{
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *upn;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *name;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *gecos;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *homedir;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina const char *shell;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t gid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t uid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina errno_t ret;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Get fields. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina upn = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
a012a71f21bf1a4687e58085f19c18cc5b2bbaddNikolai Kondrashov name = sss_get_name_from_msg(domain, msg);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina gid = nss_get_gid(domain, msg);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uid = sss_view_ldb_msg_find_attr_as_uint64(domain, msg, SYSDB_UIDNUM, 0);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (name == NULL || uid == 0 || gid == 0) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina DEBUG(SSSDBG_OP_FAILURE,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina "Incomplete user object for %s[%u]! Skipping\n",
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina name ? name : "<NULL>", uid);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return EINVAL;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina gecos = sss_view_ldb_msg_find_attr_as_string(domain, msg, SYSDB_GECOS,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina NULL);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina homedir = nss_get_homedir(mem_ctx, nss_ctx, domain, msg, name, upn, uid);
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov ret = nss_get_shell(nss_ctx, domain, msg, name, uid, &shell);
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov if (ret != EOK) {
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE,
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov "failed retrieving shell for %s[%u], skipping [%d]: %s\n",
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov name, uid, ret, sss_strerror(ret));
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov return ret;
836dae913497e150bd0ec11eee1e256e4fcc0bb7Nikolai Kondrashov }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Convert to sized strings. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret = sized_output_name(mem_ctx, nss_ctx->rctx, name, domain, _name);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (ret != EOK) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina "sized_output_name failed, skipping [%d]: %s\n",
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret, sss_strerror(ret));
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return ret;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina to_sized_string(_gecos, gecos == NULL ? "" : gecos);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina to_sized_string(_shell, shell);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina to_sized_string(_homedir, homedir);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina *_gid = gid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina *_uid = uid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return EOK;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina}
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinaerrno_t
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinanss_protocol_fill_pwent(struct nss_ctx *nss_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct nss_cmd_ctx *cmd_ctx,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sss_packet *packet,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct cache_req_result *result)
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina{
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina TALLOC_CTX *tmp_ctx;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct ldb_message *msg;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string pwfield;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string *name;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string gecos;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string homedir;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina struct sized_string shell;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t gid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t uid;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint32_t num_results;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina size_t rp;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina size_t body_len;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uint8_t *body;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina int i;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina errno_t ret;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina tmp_ctx = talloc_new(NULL);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (tmp_ctx == NULL) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return ENOMEM;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* First two fields (length and reserved), filled up later. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret = sss_packet_grow(packet, 2 * sizeof(uint32_t));
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (ret != EOK) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return ret;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina rp = 2 * sizeof(uint32_t);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina num_results = 0;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina for (i = 0; i < result->count; i++) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina talloc_free_children(tmp_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina msg = result->msgs[i];
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
c778c36c5170c2b9f1cf7a6e3b0811124534df03Jakub Hrozek /* Password field content. */
c778c36c5170c2b9f1cf7a6e3b0811124534df03Jakub Hrozek to_sized_string(&pwfield, nss_get_pwfield(nss_ctx, result->domain));
c778c36c5170c2b9f1cf7a6e3b0811124534df03Jakub Hrozek
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret = nss_get_pwent(tmp_ctx, nss_ctx, result->domain, msg, &uid, &gid,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina &name, &gecos, &homedir, &shell);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (ret != EOK) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina continue;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Adjust packet size: uid, gid + string fields. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret = sss_packet_grow(packet, 2 * sizeof(uint32_t)
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina + name->len + gecos.len + homedir.len
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina + shell.len + pwfield.len);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (ret != EOK) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina goto done;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina sss_packet_get_body(packet, &body, &body_len);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina /* Fill packet. */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_UINT32(&body[rp], uid, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_UINT32(&body[rp], gid, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_STRING(&body[rp], name->str, name->len, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_STRING(&body[rp], pwfield.str, pwfield.len, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_STRING(&body[rp], gecos.str, gecos.len, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_STRING(&body[rp], homedir.str, homedir.len, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SET_STRING(&body[rp], shell.str, shell.len, &rp);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina num_results++;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose /* Do not store entry in memory cache during enumeration or when
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose * requested. */
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose if (!cmd_ctx->enumeration
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose && (cmd_ctx->flags & SSS_NSS_EX_FLAG_INVALIDATE_CACHE) == 0) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret = sss_mmap_cache_pw_store(&nss_ctx->pwd_mc_ctx, name, &pwfield,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina uid, gid, &gecos, &homedir, &shell);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (ret != EOK) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina DEBUG(SSSDBG_MINOR_FAILURE,
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina "Failed to store user %s (%s) in mmap cache [%d]: %s!\n",
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina name->str, result->domain->name, ret, sss_strerror(ret));
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina ret = EOK;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březinadone:
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina talloc_free(tmp_ctx);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina if (ret != EOK) {
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina sss_packet_set_size(packet, 0);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return ret;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina }
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina sss_packet_get_body(packet, &body, &body_len);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_COPY_UINT32(body, &num_results, NULL);
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL); /* reserved */
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina return EOK;
4049b63f8c67ada17b453463b0451ca6be3d5de4Pavel Březina}