Blame src/plugins/kdb/ldap/libkdb_ldap/ldap_service_stash.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* plugins/kdb/ldap/libkdb_ldap/ldap_service_stash.c */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (c) 2004-2005, Novell, Inc.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Redistribution and use in source and binary forms, with or without
Packit fd8b60
 * modification, are permitted provided that the following conditions are met:
Packit fd8b60
 *
Packit fd8b60
 *   * Redistributions of source code must retain the above copyright notice,
Packit fd8b60
 *       this list of conditions and the following disclaimer.
Packit fd8b60
 *   * Redistributions in binary form must reproduce the above copyright
Packit fd8b60
 *       notice, this list of conditions and the following disclaimer in the
Packit fd8b60
 *       documentation and/or other materials provided with the distribution.
Packit fd8b60
 *   * The copyright holder's name is not used to endorse or promote products
Packit fd8b60
 *       derived from this software without specific prior written permission.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit fd8b60
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit fd8b60
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit fd8b60
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit fd8b60
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit fd8b60
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit fd8b60
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit fd8b60
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit fd8b60
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit fd8b60
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit fd8b60
 * POSSIBILITY OF SUCH DAMAGE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "ldap_main.h"
Packit fd8b60
#include "kdb_ldap.h"
Packit fd8b60
#include "ldap_service_stash.h"
Packit fd8b60
#include <k5-hex.h>
Packit fd8b60
#include <ctype.h>
Packit fd8b60
Packit fd8b60
/* Decode a password of the form {HEX}<hexstring>. */
Packit fd8b60
static krb5_error_code
Packit fd8b60
dec_password(krb5_context context, const char *str, char **password_out)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code ret;
Packit fd8b60
    uint8_t *bytes;
Packit fd8b60
    size_t len;
Packit fd8b60
Packit fd8b60
    *password_out = NULL;
Packit fd8b60
Packit fd8b60
    if (strncmp(str, "{HEX}", 5) != 0) {
Packit fd8b60
        k5_setmsg(context, EINVAL, _("Not a hexadecimal password"));
Packit fd8b60
        return EINVAL;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    ret = k5_hex_decode(str + 5, &bytes, &len;;
Packit fd8b60
    if (ret) {
Packit fd8b60
        if (ret == EINVAL)
Packit fd8b60
            k5_setmsg(context, ret, _("Password corrupt"));
Packit fd8b60
        return ret;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    *password_out = (char *)bytes;
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
krb5_error_code
Packit fd8b60
krb5_ldap_readpassword(krb5_context context, const char *filename,
Packit fd8b60
                       const char *name, char **password_out)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code ret;
Packit fd8b60
    char line[RECORDLEN], *end;
Packit fd8b60
    const char *start, *sep, *val = NULL;
Packit fd8b60
    int namelen = strlen(name);
Packit fd8b60
    FILE *fp;
Packit fd8b60
Packit fd8b60
    *password_out = NULL;
Packit fd8b60
Packit fd8b60
    fp = fopen(filename, "r");
Packit fd8b60
    if (fp == NULL) {
Packit fd8b60
        ret = errno;
Packit fd8b60
        k5_setmsg(context, ret, _("Cannot open LDAP password file '%s': %s"),
Packit fd8b60
                  filename, error_message(ret));
Packit fd8b60
        return ret;
Packit fd8b60
    }
Packit fd8b60
    set_cloexec_file(fp);
Packit fd8b60
Packit fd8b60
    while (fgets(line, RECORDLEN, fp) != NULL) {
Packit fd8b60
        /* Remove trailing newline. */
Packit fd8b60
        end = line + strlen(line);
Packit fd8b60
        if (end > line && end[-1] == '\n')
Packit fd8b60
            end[-1] = '\0';
Packit fd8b60
Packit fd8b60
        /* Skip past leading whitespace. */
Packit fd8b60
        for (start = line; isspace(*start); ++start);
Packit fd8b60
Packit fd8b60
        /* Ignore comment lines */
Packit fd8b60
        if (*start == '!' || *start == '#')
Packit fd8b60
            continue;
Packit fd8b60
Packit fd8b60
        sep = strchr(start, '#');
Packit fd8b60
        if (sep != NULL && sep - start == namelen &&
Packit fd8b60
            strncasecmp(start, name, namelen) == 0) {
Packit fd8b60
            val = sep + 1;
Packit fd8b60
            break;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
    fclose(fp);
Packit fd8b60
Packit fd8b60
    if (val == NULL) {
Packit fd8b60
        k5_setmsg(context, KRB5_KDB_SERVER_INTERNAL_ERR,
Packit fd8b60
                  _("Bind DN entry '%s' missing in LDAP password file '%s'"),
Packit fd8b60
                  name, filename);
Packit fd8b60
        return KRB5_KDB_SERVER_INTERNAL_ERR;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Extract the plain password information. */
Packit fd8b60
    return dec_password(context, val, password_out);
Packit fd8b60
}