19d3aba12c70528708be9440aca66038a291f29eYassir Elley/*
19d3aba12c70528708be9440aca66038a291f29eYassir Elley SSSD
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley AD GPO Backend Module -- perform SMB and CSE processing in a child process
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley Authors:
19d3aba12c70528708be9440aca66038a291f29eYassir Elley Yassir Elley <yelley@redhat.com>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley Copyright (C) 2013 Red Hat
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley This program is free software; you can redistribute it and/or modify
19d3aba12c70528708be9440aca66038a291f29eYassir Elley it under the terms of the GNU General Public License as published by
19d3aba12c70528708be9440aca66038a291f29eYassir Elley the Free Software Foundation; either version 3 of the License, or
19d3aba12c70528708be9440aca66038a291f29eYassir Elley (at your option) any later version.
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley This program is distributed in the hope that it will be useful,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley but WITHOUT ANY WARRANTY; without even the implied warranty of
19d3aba12c70528708be9440aca66038a291f29eYassir Elley MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19d3aba12c70528708be9440aca66038a291f29eYassir Elley GNU General Public License for more details.
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley You should have received a copy of the GNU General Public License
19d3aba12c70528708be9440aca66038a291f29eYassir Elley along with this program. If not, see <http://www.gnu.org/licenses/>.
19d3aba12c70528708be9440aca66038a291f29eYassir Elley*/
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include <sys/types.h>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include <unistd.h>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include <sys/stat.h>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include <popt.h>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include <libsmbclient.h>
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley#include <ini_configobj.h>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include <security/pam_modules.h>
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include "util/util.h"
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include "util/child_common.h"
cc2d77d5218c188119fa954c856e858cbde76947Pavel Březina#include "providers/backend.h"
f00a61b6079d8de81432077a59daf015d85800d2Jakub Hrozek#include "providers/ad/ad_gpo.h"
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#include "sss_cli.h"
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley#define SMB_BUFFER_SIZE 65536
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley#define GPT_INI "/GPT.INI"
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley#define INI_GENERAL_SECTION "General"
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley#define GPT_INI_VERSION "Version"
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elleystruct input_buffer {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int cached_gpt_version;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_server;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_share;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_path;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_cse_suffix;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley};
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elleystatic errno_t
19d3aba12c70528708be9440aca66038a291f29eYassir Elleyunpack_buffer(uint8_t *buf,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley size_t size,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley struct input_buffer *ibuf)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
19d3aba12c70528708be9440aca66038a291f29eYassir Elley size_t p = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley uint32_t len;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley uint32_t cached_gpt_version;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* cached_gpt_version */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SAFEALIGN_COPY_UINT32_CHECK(&cached_gpt_version, buf + p, size, &p);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "cached_gpt_version: %d\n", cached_gpt_version);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->cached_gpt_version = cached_gpt_version;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* smb_server */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SAFEALIGN_COPY_UINT32_CHECK(&len, buf + p, size, &p);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_server length: %d\n", len);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (len == 0) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return EINVAL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley } else {
9f0bffebd070115ab47a92eadc6890a721c7b78dMichal Židek if (len > size - p) return EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_server = talloc_strndup(ibuf, (char *)(buf + p), len);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ibuf->smb_server == NULL) return ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_server: %s\n", ibuf->smb_server);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley p += len;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley }
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* smb_share */
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley SAFEALIGN_COPY_UINT32_CHECK(&len, buf + p, size, &p);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_share length: %d\n", len);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (len == 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley return EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley } else {
9f0bffebd070115ab47a92eadc6890a721c7b78dMichal Židek if (len > size - p) return EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_share = talloc_strndup(ibuf, (char *)(buf + p), len);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ibuf->smb_share == NULL) return ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_share: %s\n", ibuf->smb_share);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley p += len;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* smb_path */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SAFEALIGN_COPY_UINT32_CHECK(&len, buf + p, size, &p);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_path length: %d\n", len);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (len == 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley return EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley } else {
9f0bffebd070115ab47a92eadc6890a721c7b78dMichal Židek if (len > size - p) return EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_path = talloc_strndup(ibuf, (char *)(buf + p), len);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ibuf->smb_path == NULL) return ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_path: %s\n", ibuf->smb_path);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley p += len;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* smb_cse_suffix */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SAFEALIGN_COPY_UINT32_CHECK(&len, buf + p, size, &p);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_cse_suffix length: %d\n", len);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (len == 0) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley return EINVAL;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley } else {
9f0bffebd070115ab47a92eadc6890a721c7b78dMichal Židek if (len > size - p) return EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_cse_suffix = talloc_strndup(ibuf, (char *)(buf + p), len);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ibuf->smb_cse_suffix == NULL) return ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_cse_suffix: %s\n", ibuf->smb_cse_suffix);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley p += len;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return EOK;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elleystatic errno_t
19d3aba12c70528708be9440aca66038a291f29eYassir Elleypack_buffer(struct response *r,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int sysvol_gpt_version,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int result)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
19d3aba12c70528708be9440aca66038a291f29eYassir Elley size_t p = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley /* A buffer with the following structure must be created:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * uint32_t sysvol_gpt_version (required)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley * uint32_t status of the request (required)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley r->size = 2 * sizeof(uint32_t);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley r->buf = talloc_array(r, uint8_t, r->size);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if(r->buf == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley DEBUG(SSSDBG_TRACE_FUNC, "result [%d]\n", result);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* sysvol_gpt_version */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SAFEALIGN_SET_UINT32(&r->buf[p], sysvol_gpt_version, &p);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley /* result */
19d3aba12c70528708be9440aca66038a291f29eYassir Elley SAFEALIGN_SET_UINT32(&r->buf[p], result, &p);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return EOK;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elleystatic errno_t
19d3aba12c70528708be9440aca66038a291f29eYassir Elleyprepare_response(TALLOC_CTX *mem_ctx,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int sysvol_gpt_version,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int result,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley struct response **rsp)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int ret;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley struct response *r = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley r = talloc_zero(mem_ctx, struct response);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (r == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley r->buf = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley r->size = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = pack_buffer(r, sysvol_gpt_version, result);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ret != EOK) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "pack_buffer failed\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return ret;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley *rsp = r;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "r->size: %zu\n", r->size);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return EOK;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elleystatic void
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elleysssd_krb_get_auth_data_fn(const char * pServer,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley const char * pShare,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char * pWorkgroup,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int maxLenWorkgroup,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char * pUsername,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int maxLenUsername,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char * pPassword,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int maxLenPassword)
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley{
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley /* since we are using kerberos for authentication, we simply return */
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley return;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley}
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley/*
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley * This function prepares the gpo_cache by:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - parsing the input_smb_path into its component directories
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley * - creating each component directory (if it doesn't already exist)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley */
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elleystatic errno_t prepare_gpo_cache(TALLOC_CTX *mem_ctx,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley const char *cache_dir,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *input_smb_path_with_suffix)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char *current_dir;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char *ptr;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley const char delim = '/';
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int num_dirs = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int i;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char *first = NULL;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char *last = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley char *smb_path_with_suffix = NULL;
2fec5f131ff3086d50e9e06e24951aa9f7d6ab5eJakub Hrozek errno_t ret;
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik mode_t old_umask;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smb_path_with_suffix = talloc_strdup(mem_ctx, input_smb_path_with_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (smb_path_with_suffix == NULL) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley return ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_path_with_suffix: %s\n", smb_path_with_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley current_dir = talloc_strdup(mem_ctx, cache_dir);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (current_dir == NULL) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley return ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ptr = smb_path_with_suffix + 1;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley while ((ptr = strchr(ptr, delim))) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ptr++;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley num_dirs++;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ptr = smb_path_with_suffix + 1;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik old_umask = umask(SSS_DFL_X_UMASK);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley for (i = 0; i < num_dirs; i++) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley first = ptr;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley last = strchr(first, delim);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (last == NULL) {
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik ret = EINVAL;
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley *last = '\0';
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley last++;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley current_dir = talloc_asprintf(mem_ctx, "%s/%s", current_dir, first);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (current_dir == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik ret = ENOMEM;
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
2fec5f131ff3086d50e9e06e24951aa9f7d6ab5eJakub Hrozek DEBUG(SSSDBG_TRACE_FUNC, "Storing GPOs in %s\n", current_dir);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
d0e0cf6ee47ab538efc47c7882f498f1b5e0f0c7Lukas Slebodnik if ((mkdir(current_dir, 0700)) < 0 && errno != EEXIST) {
2fec5f131ff3086d50e9e06e24951aa9f7d6ab5eJakub Hrozek ret = errno;
2fec5f131ff3086d50e9e06e24951aa9f7d6ab5eJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
2fec5f131ff3086d50e9e06e24951aa9f7d6ab5eJakub Hrozek "mkdir(%s) failed: %d\n", current_dir, ret);
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik goto done;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ptr = last;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik ret = EOK;
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik
192126738fa82c5624f4740147426c552126c602Lukas Slebodnikdone:
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik umask(old_umask);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
192126738fa82c5624f4740147426c552126c602Lukas Slebodnik return ret;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley/*
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley * This function stores the input buf to a local file, whose file path
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * is constructed by concatenating:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * GPO_CACHE_PATH,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * input smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * input smb_cse_suffix
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * Note that the backend will later read the file from the same file path.
19d3aba12c70528708be9440aca66038a291f29eYassir Elley */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleystatic errno_t gpo_cache_store_file(const char *smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_cse_suffix,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley uint8_t *buf,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int buflen)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int ret;
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek int fret;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int fd = -1;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char *tmp_name = NULL;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ssize_t written;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley char *filename = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley char *smb_path_with_suffix = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley TALLOC_CTX *tmp_ctx = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley tmp_ctx = talloc_new(NULL);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (tmp_ctx == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smb_path_with_suffix =
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley talloc_asprintf(tmp_ctx, "%s%s", smb_path, smb_cse_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (smb_path_with_suffix == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* create component directories of smb_path, if needed */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = prepare_gpo_cache(tmp_ctx, GPO_CACHE_PATH, smb_path_with_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != EOK) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "prepare_gpo_cache failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley filename = talloc_asprintf(tmp_ctx, GPO_CACHE_PATH"%s", smb_path_with_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (filename == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley tmp_name = talloc_asprintf(tmp_ctx, "%sXXXXXX", filename);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (tmp_name == NULL) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = ENOMEM;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley goto done;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley }
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek fd = sss_unique_file(tmp_ctx, tmp_name, &ret);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (fd == -1) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek "sss_unique_file failed [%d][%s].\n", ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley errno = 0;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley written = sss_atomic_write_s(fd, buf, buflen);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (written == -1) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = errno;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley "write failed [%d][%s].\n", ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (written != buflen) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley "Write error, wrote [%zd] bytes, expected [%d]\n",
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley written, buflen);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = EIO;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (ret == -1) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = errno;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley "fchmod failed [%d][%s].\n", ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = rename(tmp_name, filename);
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley if (ret == -1) {
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley ret = errno;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley "rename failed [%d][%s].\n", ret, strerror(ret));
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley goto done;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek ret = EOK;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley done:
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ret != EOK) {
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "Error encountered: %d.\n", ret);
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek }
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek if (fd != -1) {
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek fret = close(fd);
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek if (fret == -1) {
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek fret = errno;
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek "close failed [%d][%s].\n", fret, strerror(fret));
3954cd07dae78bf79136f0854472757d1ed26897Jakub Hrozek }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley talloc_free(tmp_ctx);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return ret;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleystatic errno_t
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleyparse_ini_file_with_libini(struct ini_cfgobj *ini_config,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int *_gpt_version)
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley{
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley struct value_obj *vobj = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int gpt_version;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ini_get_config_valueobj(INI_GENERAL_SECTION, GPT_INI_VERSION,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ini_config, INI_GET_FIRST_VALUE, &vobj);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "ini_get_config_valueobj failed [%d][%s]\n", ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (vobj == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "section/name not found: [%s][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley INI_GENERAL_SECTION, GPT_INI_VERSION);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = EINVAL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley gpt_version = ini_get_int32_config_value(vobj, 0, -1, &ret);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "ini_get_int32_config_value failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley *_gpt_version = gpt_version;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = EOK;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley done:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley return ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley}
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley/*
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * This function parses the GPT_INI file stored in the gpo_cache, and uses the
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * results to populate the output parameters ...
19d3aba12c70528708be9440aca66038a291f29eYassir Elley */
19d3aba12c70528708be9440aca66038a291f29eYassir Elleystatic errno_t
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleyad_gpo_parse_ini_file(const char *smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int *_gpt_version)
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley struct ini_cfgfile *file_ctx = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley struct ini_cfgobj *ini_config = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *ini_filename;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int gpt_version = -1;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley TALLOC_CTX *tmp_ctx = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley tmp_ctx = talloc_new(NULL);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (tmp_ctx == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ini_filename = talloc_asprintf(tmp_ctx, GPO_CACHE_PATH"%s%s",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smb_path, GPT_INI);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ini_filename == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "ini_filename:%s\n", ini_filename);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ini_config_create(&ini_config);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "ini_config_create failed [%d][%s]\n", ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ini_config_file_open(ini_filename, 0, &file_ctx);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "ini_config_file_open failed [%d][%s]\n", ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ini_config_parse(file_ctx, INI_STOP_ON_NONE, 0, 0, ini_config);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != 0) {
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek int lret;
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek char **errors;
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek "[%s]: ini_config_parse failed [%d][%s]\n",
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek ini_filename, ret, strerror(ret));
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek /* Now get specific errors if there are any */
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek lret = ini_config_get_errors(ini_config, &errors);
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek if (lret != 0) {
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek DEBUG(SSSDBG_CRIT_FAILURE,
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek "Failed to get specific parse error [%d][%s]\n", lret,
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek strerror(lret));
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek goto done;
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek }
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek for (int i = 0; errors[i]; i++) {
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek DEBUG(SSSDBG_CRIT_FAILURE, "%s\n", errors[i]);
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek }
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek ini_config_free_errors(errors);
dad416a9b0095e1c423b7da65db7c636fa69e614Michal Židek
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = parse_ini_file_with_libini(ini_config, &gpt_version);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "parse_ini_file_with_libini failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley *_gpt_version = gpt_version;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley done:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != EOK) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "Error encountered: %d.\n", ret);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ini_config_file_destroy(file_ctx);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ini_config_destroy(ini_config);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley talloc_free(tmp_ctx);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley return ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley}
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley/*
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * This function uses the input smb uri components to download a sysvol file
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * (e.g. INI file, policy file, etc) and store it to the GPO_CACHE directory.
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleystatic errno_t
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleycopy_smb_file_to_gpo_cache(SMBCCTX *smbc_ctx,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_server,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_share,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_cse_suffix)
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley{
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley char *smb_uri = NULL;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SMBCFILE *file;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int ret;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley uint8_t *buf = NULL;
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley int buflen = 0;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley TALLOC_CTX *tmp_ctx = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley tmp_ctx = talloc_new(NULL);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (tmp_ctx == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smb_uri = talloc_asprintf(tmp_ctx, "%s%s%s%s", smb_server,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smb_share, smb_path, smb_cse_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (smb_uri == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "smb_uri: %s\n", smb_uri);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley errno = 0;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley file = smbc_getFunctionOpen(smbc_ctx)(smbc_ctx, smb_uri, O_RDONLY, 0755);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (file == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = errno;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "smbc_getFunctionOpen failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley buf = talloc_array(tmp_ctx, uint8_t, SMB_BUFFER_SIZE);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (buf == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_array failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley errno = 0;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley buflen = smbc_getFunctionRead(smbc_ctx)(smbc_ctx, file, buf, SMB_BUFFER_SIZE);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (buflen < 0) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = errno;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "smbc_getFunctionRead failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_ALL, "smb_buflen: %d\n", buflen);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = gpo_cache_store_file(smb_path, smb_cse_suffix, buf, buflen);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != EOK) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "gpo_cache_store_file failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley done:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley talloc_free(tmp_ctx);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley return ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley}
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley/*
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * Using its smb_uri components and cached_gpt_version inputs, this function
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * does several things:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - it downloads the GPT_INI file to GPO_CACHE
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - it parses the sysvol_gpt_version field from the GPT_INI file
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - if the sysvol_gpt_version is greater than the cached_gpt_version
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - it downloads the policy file to GPO_CACHE
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - else
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - it doesn't retrieve the policy file
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - in this case, the backend will use the existing policy file in GPO_CACHE
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - it returns the sysvol_gpt_version in the _sysvol_gpt_version output param
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley *
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * Note that if the cached_gpt_version sent by the backend is -1 (to indicate
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * that no gpt_version has been set in the cache for the corresponding gpo_guid),
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * then the parsed sysvol_gpt_version (which must be at least 0) will be greater
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * than the cached_gpt_version, thereby triggering a fresh download.
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley *
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * Note that the backend will later do the following:
f91029dd8d7dbc026a5c73e222926db957240cb4Yuri Chornoivan * - backend will save the sysvol_gpt_version to sysdb cache
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley * - backend will read the policy file from the GPO_CACHE
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleystatic errno_t
b47449d8596d9573ff13e74b6984d080b414715aYassir Elleyperform_smb_operations(int cached_gpt_version,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_server,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_share,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley const char *smb_cse_suffix,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int *_sysvol_gpt_version)
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley{
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley SMBCCTX *smbc_ctx;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int sysvol_gpt_version;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smbc_ctx = smbc_new_context();
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (smbc_ctx == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "Could not allocate new smbc context\n");
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ENOMEM;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
bb7ddd2be9847bfb07395341c7623da1b104b8a6Jakub Hrozek smbc_setOptionDebugToStderr(smbc_ctx, 1);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smbc_setFunctionAuthData(smbc_ctx, sssd_krb_get_auth_data_fn);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smbc_setOptionUseKerberos(smbc_ctx, 1);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* Initialize the context using the previously specified options */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (smbc_init_context(smbc_ctx) == NULL) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "Could not initialize smbc context\n");
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ENOMEM;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* download ini file */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = copy_smb_file_to_gpo_cache(smbc_ctx, smb_server, smb_share, smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley GPT_INI);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ret != EOK) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "copy_smb_file_to_gpo_cache failed [%d][%s]\n",
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto done;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = ad_gpo_parse_ini_file(smb_path, &sysvol_gpt_version);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != EOK) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "Cannot parse ini file: [%d][%s]\n", ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "sysvol_gpt_version: %d\n", sysvol_gpt_version);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (sysvol_gpt_version > cached_gpt_version) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley /* download policy file */
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = copy_smb_file_to_gpo_cache(smbc_ctx, smb_server, smb_share,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smb_path, smb_cse_suffix);
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley if (ret != EOK) {
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "copy_smb_file_to_gpo_cache failed [%d][%s]\n",
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret, strerror(ret));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto done;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley }
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley *_sysvol_gpt_version = sysvol_gpt_version;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley done:
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley smbc_free_context(smbc_ctx, 0);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return ret;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elleyint
19d3aba12c70528708be9440aca66038a291f29eYassir Elleymain(int argc, const char *argv[])
19d3aba12c70528708be9440aca66038a291f29eYassir Elley{
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int opt;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley poptContext pc;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int debug_fd = -1;
a24954cc19285b197fb287bfa7aa01949c92b17dLukas Slebodnik const char *opt_logger = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley errno_t ret;
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley int sysvol_gpt_version;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley int result;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley TALLOC_CTX *main_ctx = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley uint8_t *buf = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ssize_t len = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley struct input_buffer *ibuf = NULL;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley struct response *resp = NULL;
8e44ddfccebe61728d8a2c1dafce36dfa944bc90Jakub Hrozek ssize_t written;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley struct poptOption long_options[] = {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley POPT_AUTOHELP
19d3aba12c70528708be9440aca66038a291f29eYassir Elley {"debug-level", 'd', POPT_ARG_INT, &debug_level, 0,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley _("Debug level"), NULL},
19d3aba12c70528708be9440aca66038a291f29eYassir Elley {"debug-timestamps", 0, POPT_ARG_INT, &debug_timestamps, 0,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley _("Add debug timestamps"), NULL},
19d3aba12c70528708be9440aca66038a291f29eYassir Elley {"debug-microseconds", 0, POPT_ARG_INT, &debug_microseconds, 0,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley _("Show timestamps with microseconds"), NULL},
19d3aba12c70528708be9440aca66038a291f29eYassir Elley {"debug-fd", 0, POPT_ARG_INT, &debug_fd, 0,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley _("An open file descriptor for the debug logs"), NULL},
b31b0a468ddbc9be4fc78289e5a0a108c31ec620Sumit Bose {"debug-to-stderr", 0, POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN,
b31b0a468ddbc9be4fc78289e5a0a108c31ec620Sumit Bose &debug_to_stderr, 0,
b31b0a468ddbc9be4fc78289e5a0a108c31ec620Sumit Bose _("Send the debug output to stderr directly."), NULL },
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik SSSD_LOGGER_OPTS
19d3aba12c70528708be9440aca66038a291f29eYassir Elley POPT_TABLEEND
19d3aba12c70528708be9440aca66038a291f29eYassir Elley };
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley /* Set debug level to invalid value so we can decide if -d 0 was used. */
19d3aba12c70528708be9440aca66038a291f29eYassir Elley debug_level = SSSDBG_INVALID;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley pc = poptGetContext(argv[0], argc, argv, long_options, 0);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley while((opt = poptGetNextOpt(pc)) != -1) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley switch(opt) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley default:
19d3aba12c70528708be9440aca66038a291f29eYassir Elley fprintf(stderr, "\nInvalid option %s: %s\n\n",
19d3aba12c70528708be9440aca66038a291f29eYassir Elley poptBadOption(pc, 0), poptStrerror(opt));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley poptPrintUsage(pc, stderr, 0);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley _exit(-1);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley poptFreeContext(pc);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG_INIT(debug_level);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley debug_prg_name = talloc_asprintf(NULL, "[sssd[gpo_child[%d]]]", getpid());
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (debug_prg_name == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (debug_fd != -1) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = set_debug_file_from_fd(debug_fd);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ret != EOK) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "set_debug_file_from_fd failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
a24954cc19285b197fb287bfa7aa01949c92b17dLukas Slebodnik opt_logger = sss_logger_str[FILES_LOGGER];
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik sss_set_logger(opt_logger);
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "gpo_child started.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley main_ctx = talloc_new(NULL);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (main_ctx == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley talloc_free(discard_const(debug_prg_name));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley talloc_steal(main_ctx, debug_prg_name);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley buf = talloc_size(main_ctx, sizeof(uint8_t)*IN_BUF_SIZE);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (buf == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_size failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ibuf = talloc_zero(main_ctx, struct input_buffer);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ibuf == NULL) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "context initialized\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley errno = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley len = sss_atomic_read_s(STDIN_FILENO, buf, IN_BUF_SIZE);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (len == -1) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = errno;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "read failed [%d][%s].\n", ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley close(STDIN_FILENO);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = unpack_buffer(buf, len, ibuf);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ret != EOK) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley "unpack_buffer failed.[%d][%s].\n", ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "performing smb operations\n");
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley result = perform_smb_operations(ibuf->cached_gpt_version,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_server,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_share,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_path,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ibuf->smb_cse_suffix,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley &sysvol_gpt_version);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (result != EOK) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE,
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley "perform_smb_operations failed.[%d][%s].\n",
32381402a4a9afc003782c9e2301fc59c9bda2a9Yassir Elley result, strerror(result));
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
b47449d8596d9573ff13e74b6984d080b414715aYassir Elley ret = prepare_response(main_ctx, sysvol_gpt_version, result, &resp);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (ret != EOK) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "prepare_response failed. [%d][%s].\n",
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret, strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley errno = 0;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
f00a61b6079d8de81432077a59daf015d85800d2Jakub Hrozek written = sss_atomic_write_s(AD_GPO_CHILD_OUT_FILENO, resp->buf, resp->size);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (written == -1) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley ret = errno;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "write failed [%d][%s].\n", ret,
19d3aba12c70528708be9440aca66038a291f29eYassir Elley strerror(ret));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley if (written != resp->size) {
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "Expected to write %zu bytes, wrote %zu\n",
19d3aba12c70528708be9440aca66038a291f29eYassir Elley resp->size, written);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley goto fail;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley }
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_TRACE_FUNC, "gpo_child completed successfully\n");
f00a61b6079d8de81432077a59daf015d85800d2Jakub Hrozek close(AD_GPO_CHILD_OUT_FILENO);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley talloc_free(main_ctx);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return EXIT_SUCCESS;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley
19d3aba12c70528708be9440aca66038a291f29eYassir Elleyfail:
19d3aba12c70528708be9440aca66038a291f29eYassir Elley DEBUG(SSSDBG_CRIT_FAILURE, "gpo_child failed!\n");
f00a61b6079d8de81432077a59daf015d85800d2Jakub Hrozek close(AD_GPO_CHILD_OUT_FILENO);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley talloc_free(main_ctx);
19d3aba12c70528708be9440aca66038a291f29eYassir Elley return EXIT_FAILURE;
19d3aba12c70528708be9440aca66038a291f29eYassir Elley}