|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* CIFS user-space helper.
|
|
Packit Service |
09cdfc |
* Copyright (C) Igor Mammedov (niallain@gmail.com) 2007
|
|
Packit Service |
09cdfc |
* Copyright (C) Jeff Layton (jlayton@samba.org) 2010
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* Used by /sbin/request-key for handling
|
|
Packit Service |
09cdfc |
* cifs upcall for kerberos authorization of access to share and
|
|
Packit Service |
09cdfc |
* cifs upcall for DFS srver name resolving (IPv4/IPv6 aware).
|
|
Packit Service |
09cdfc |
* You should have keyutils installed and add something like the
|
|
Packit Service |
09cdfc |
* following lines to /etc/request-key.conf file:
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
create cifs.spnego * * /usr/local/sbin/cifs.upcall %k
|
|
Packit Service |
09cdfc |
create dns_resolver * * /usr/local/sbin/cifs.upcall %k
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
09cdfc |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
09cdfc |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit Service |
09cdfc |
* (at your option) any later version.
|
|
Packit Service |
09cdfc |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
09cdfc |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
09cdfc |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
09cdfc |
* GNU General Public License for more details.
|
|
Packit Service |
09cdfc |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
09cdfc |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
09cdfc |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
09cdfc |
#include "config.h"
|
|
Packit Service |
09cdfc |
#endif /* HAVE_CONFIG_H */
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#include <string.h>
|
|
Packit Service |
09cdfc |
#include <getopt.h>
|
|
Packit Service |
09cdfc |
#ifdef HAVE_KRB5_KRB5_H
|
|
Packit Service |
09cdfc |
#include <krb5/krb5.h>
|
|
Packit Service |
09cdfc |
#elif defined(HAVE_KRB5_H)
|
|
Packit Service |
09cdfc |
#include <krb5.h>
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
#include <syslog.h>
|
|
Packit Service |
09cdfc |
#include <dirent.h>
|
|
Packit Service |
09cdfc |
#include <sys/types.h>
|
|
Packit Service |
09cdfc |
#include <sys/stat.h>
|
|
Packit Service |
09cdfc |
#include <fcntl.h>
|
|
Packit Service |
09cdfc |
#include <unistd.h>
|
|
Packit Service |
09cdfc |
#include <keyutils.h>
|
|
Packit Service |
09cdfc |
#include <time.h>
|
|
Packit Service |
09cdfc |
#include <netdb.h>
|
|
Packit Service |
09cdfc |
#include <arpa/inet.h>
|
|
Packit Service |
09cdfc |
#include <ctype.h>
|
|
Packit Service |
09cdfc |
#include <pwd.h>
|
|
Packit Service |
09cdfc |
#include <grp.h>
|
|
Packit Service |
09cdfc |
#include <stdbool.h>
|
|
Packit Service |
09cdfc |
#include <errno.h>
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#include "data_blob.h"
|
|
Packit Service |
09cdfc |
#include "spnego.h"
|
|
Packit Service |
09cdfc |
#include "cifs_spnego.h"
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#ifdef HAVE_LIBCAP_NG
|
|
Packit Service |
09cdfc |
#include <cap-ng.h>
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static krb5_context context;
|
|
Packit Service |
09cdfc |
static const char *prog = "cifs.upcall";
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
typedef enum _sectype {
|
|
Packit Service |
09cdfc |
NONE = 0,
|
|
Packit Service |
09cdfc |
KRB5,
|
|
Packit Service |
09cdfc |
MS_KRB5
|
|
Packit Service |
09cdfc |
} sectype_t;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* These macros unify the keyblock handling of Heimdal and MIT somewhat */
|
|
Packit Service |
09cdfc |
#ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_TYPE(k) ((k)->keytype)
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_LENGTH(k) ((k)->keyvalue.length)
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_DATA(k) ((k)->keyvalue.data)
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_DATA_CAST void
|
|
Packit Service |
09cdfc |
#else /* MIT */
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_TYPE(k) ((k)->enctype)
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_LENGTH(k) ((k)->length)
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_DATA(k) ((k)->contents)
|
|
Packit Service |
09cdfc |
#define KRB5_KEY_DATA_CAST krb5_octet
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#ifdef HAVE_LIBCAP_NG
|
|
Packit Service |
09cdfc |
static int
|
|
Packit Service |
09cdfc |
trim_capabilities(bool need_environ)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
capng_clear(CAPNG_SELECT_BOTH);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* SETUID and SETGID to change uid, gid, and grouplist */
|
|
Packit Service |
09cdfc |
if (capng_updatev(CAPNG_ADD, CAPNG_PERMITTED|CAPNG_EFFECTIVE,
|
|
Packit Service |
09cdfc |
CAP_SETUID, CAP_SETGID, -1)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "%s: Unable to update capability set: %m\n", __func__);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* Need PTRACE and READ_SEARCH for /proc/pid/environ scraping */
|
|
Packit Service |
09cdfc |
if (need_environ &&
|
|
Packit Service |
09cdfc |
capng_updatev(CAPNG_ADD, CAPNG_PERMITTED|CAPNG_EFFECTIVE,
|
|
Packit Service |
09cdfc |
CAP_SYS_PTRACE, CAP_DAC_READ_SEARCH, -1)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "%s: Unable to update capability set: %m\n", __func__);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (capng_apply(CAPNG_SELECT_BOTH)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "%s: Unable to apply capability set: %m\n", __func__);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
return 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static int
|
|
Packit Service |
09cdfc |
drop_all_capabilities(void)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
capng_clear(CAPNG_SELECT_BOTH);
|
|
Packit Service |
09cdfc |
if (capng_apply(CAPNG_SELECT_BOTH)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "%s: Unable to apply capability set: %m\n", __func__);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
return 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
#else /* HAVE_LIBCAP_NG */
|
|
Packit Service |
09cdfc |
static int
|
|
Packit Service |
09cdfc |
trim_capabilities(bool unused)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
return 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static int
|
|
Packit Service |
09cdfc |
drop_all_capabilities(void)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
return 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
#endif /* HAVE_LIBCAP_NG */
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* smb_krb5_principal_get_realm
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* @brief Get realm of a principal
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* @param[in] context The krb5_context
|
|
Packit Service |
09cdfc |
* @param[in] principal The principal
|
|
Packit Service |
09cdfc |
* @return pointer to the realm
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
static char *cifs_krb5_principal_get_realm(krb5_principal principal)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
#ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
|
|
Packit Service |
09cdfc |
return krb5_principal_get_realm(context, principal);
|
|
Packit Service |
09cdfc |
#elif defined(krb5_princ_realm) /* MIT */
|
|
Packit Service |
09cdfc |
krb5_data *realm;
|
|
Packit Service |
09cdfc |
realm = krb5_princ_realm(context, principal);
|
|
Packit Service |
09cdfc |
return (char *)realm->data;
|
|
Packit Service |
09cdfc |
#else
|
|
Packit Service |
09cdfc |
return NULL;
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
|
|
Packit Service |
09cdfc |
static void krb5_free_unparsed_name(krb5_context context, char *val)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
free(val);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#if !defined(HAVE_KRB5_AUTH_CON_GETSENDSUBKEY) /* Heimdal */
|
|
Packit Service |
09cdfc |
static krb5_error_code
|
|
Packit Service |
09cdfc |
krb5_auth_con_getsendsubkey(krb5_context context,
|
|
Packit Service |
09cdfc |
krb5_auth_context auth_context,
|
|
Packit Service |
09cdfc |
krb5_keyblock **keyblock)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
return krb5_auth_con_getlocalsubkey(context, auth_context, keyblock);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* does the ccache have a valid TGT? */
|
|
Packit Service |
09cdfc |
static time_t get_tgt_time(krb5_ccache ccache)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
krb5_cc_cursor cur;
|
|
Packit Service |
09cdfc |
krb5_creds creds;
|
|
Packit Service |
09cdfc |
krb5_principal principal;
|
|
Packit Service |
09cdfc |
time_t credtime = 0;
|
|
Packit Service |
09cdfc |
char *realm = NULL;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (krb5_cc_set_flags(context, ccache, 0)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to set flags", __func__);
|
|
Packit Service |
09cdfc |
goto err_cache;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (krb5_cc_get_principal(context, ccache, &principal)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to get principal", __func__);
|
|
Packit Service |
09cdfc |
goto err_cache;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (krb5_cc_start_seq_get(context, ccache, &cur)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to seq start", __func__);
|
|
Packit Service |
09cdfc |
goto err_ccstart;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if ((realm = cifs_krb5_principal_get_realm(principal)) == NULL) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to get realm", __func__);
|
|
Packit Service |
09cdfc |
goto err_ccstart;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
while (!credtime && !krb5_cc_next_cred(context, ccache, &cur, &creds)) {
|
|
Packit Service |
09cdfc |
char *name;
|
|
Packit Service |
09cdfc |
if (krb5_unparse_name(context, creds.server, &name)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to unparse name",
|
|
Packit Service |
09cdfc |
__func__);
|
|
Packit Service |
09cdfc |
goto err_endseq;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
if (krb5_realm_compare(context, creds.server, principal) &&
|
|
Packit Service |
09cdfc |
!strncasecmp(name, KRB5_TGS_NAME, KRB5_TGS_NAME_SIZE) &&
|
|
Packit Service |
09cdfc |
!strncasecmp(name + KRB5_TGS_NAME_SIZE + 1, realm,
|
|
Packit Service |
09cdfc |
strlen(realm))
|
|
Packit Service |
09cdfc |
&& creds.times.endtime > time(NULL))
|
|
Packit Service |
09cdfc |
credtime = creds.times.endtime;
|
|
Packit Service |
09cdfc |
krb5_free_cred_contents(context, &creds);
|
|
Packit Service |
09cdfc |
krb5_free_unparsed_name(context, name);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
err_endseq:
|
|
Packit Service |
09cdfc |
krb5_cc_end_seq_get(context, ccache, &cur);
|
|
Packit Service |
09cdfc |
err_ccstart:
|
|
Packit Service |
09cdfc |
krb5_free_principal(context, principal);
|
|
Packit Service |
09cdfc |
err_cache:
|
|
Packit Service |
09cdfc |
return credtime;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#define ENV_PATH_FMT "/proc/%d/environ"
|
|
Packit Service |
09cdfc |
#define ENV_PATH_MAXLEN (6 + 10 + 8 + 1)
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#define ENV_NAME "KRB5CCNAME"
|
|
Packit Service |
09cdfc |
#define ENV_PREFIX "KRB5CCNAME="
|
|
Packit Service |
09cdfc |
#define ENV_PREFIX_LEN 11
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#define ENV_BUF_START (4096)
|
|
Packit Service |
09cdfc |
#define ENV_BUF_MAX (131072)
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/**
|
|
Packit Service |
09cdfc |
* get_cachename_from_process_env - scrape value of $KRB5CCNAME out of the
|
|
Packit Service |
09cdfc |
* initiating process' environment.
|
|
Packit Service |
09cdfc |
* @pid: initiating pid value from the upcall string
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* Open the /proc/<pid>/environ file for the given pid, and scrape it for
|
|
Packit Service |
09cdfc |
* KRB5CCNAME entries.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* We start with a page-size buffer, and then progressively double it until
|
|
Packit Service |
09cdfc |
* we can slurp in the whole thing.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* Note that this is not entirely reliable. If the process is sitting in a
|
|
Packit Service |
09cdfc |
* container or something, then this is almost certainly not going to point
|
|
Packit Service |
09cdfc |
* where you expect.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* Probably it just won't work, but could a user use this to trick cifs.upcall
|
|
Packit Service |
09cdfc |
* into reading a file outside the container, by setting KRB5CCNAME in a
|
|
Packit Service |
09cdfc |
* crafty way?
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
static char *
|
|
Packit Service |
09cdfc |
get_cachename_from_process_env(pid_t pid)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int fd, ret;
|
|
Packit Service |
09cdfc |
ssize_t buflen;
|
|
Packit Service |
09cdfc |
ssize_t bufsize = ENV_BUF_START;
|
|
Packit Service |
09cdfc |
char pathname[ENV_PATH_MAXLEN];
|
|
Packit Service |
09cdfc |
char *cachename = NULL;
|
|
Packit Service |
09cdfc |
char *buf = NULL, *pos;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (!pid) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: pid == 0\n", __func__);
|
|
Packit Service |
09cdfc |
return NULL;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
pathname[ENV_PATH_MAXLEN - 1] = '\0';
|
|
Packit Service |
09cdfc |
ret = snprintf(pathname, ENV_PATH_MAXLEN, ENV_PATH_FMT, pid);
|
|
Packit Service |
09cdfc |
if (ret >= ENV_PATH_MAXLEN) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unterminated path!\n", __func__);
|
|
Packit Service |
09cdfc |
return NULL;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: pathname=%s\n", __func__, pathname);
|
|
Packit Service |
09cdfc |
fd = open(pathname, O_RDONLY);
|
|
Packit Service |
09cdfc |
if (fd < 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: open failed: %d\n", __func__, errno);
|
|
Packit Service |
09cdfc |
return NULL;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retry:
|
|
Packit Service |
09cdfc |
if (bufsize > ENV_BUF_MAX) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: buffer too big: %zd\n",
|
|
Packit Service |
09cdfc |
__func__, bufsize);
|
|
Packit Service |
09cdfc |
goto out_close;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
buf = malloc(bufsize);
|
|
Packit Service |
09cdfc |
if (!buf) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: malloc failure\n", __func__);
|
|
Packit Service |
09cdfc |
goto out_close;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
buflen = read(fd, buf, bufsize);
|
|
Packit Service |
09cdfc |
if (buflen < 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: read failed: %d\n", __func__, errno);
|
|
Packit Service |
09cdfc |
goto out_close;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (buflen >= bufsize) {
|
|
Packit Service |
09cdfc |
/* We read to the end of the buffer. Double and try again */
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: read to end of buffer (%zu bytes)\n",
|
|
Packit Service |
09cdfc |
__func__, bufsize);
|
|
Packit Service |
09cdfc |
free(buf);
|
|
Packit Service |
09cdfc |
bufsize *= 2;
|
|
Packit Service |
09cdfc |
if (lseek(fd, 0, SEEK_SET) < 0)
|
|
Packit Service |
09cdfc |
goto out_close;
|
|
Packit Service |
09cdfc |
goto retry;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
pos = buf;
|
|
Packit Service |
09cdfc |
while (buflen > 0) {
|
|
Packit Service |
09cdfc |
size_t len = strnlen(pos, buflen);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (len > ENV_PREFIX_LEN &&
|
|
Packit Service |
09cdfc |
!memcmp(pos, ENV_PREFIX, ENV_PREFIX_LEN)) {
|
|
Packit Service |
09cdfc |
cachename = strndup(pos + ENV_PREFIX_LEN,
|
|
Packit Service |
09cdfc |
len - ENV_PREFIX_LEN);
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: cachename = %s\n",
|
|
Packit Service |
09cdfc |
__func__, cachename);
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
buflen -= (len + 1);
|
|
Packit Service |
09cdfc |
pos += (len + 1);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
out_close:
|
|
Packit Service |
09cdfc |
free(buf);
|
|
Packit Service |
09cdfc |
close(fd);
|
|
Packit Service |
09cdfc |
return cachename;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static krb5_ccache
|
|
Packit Service |
09cdfc |
get_existing_cc(const char *env_cachename)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
krb5_error_code ret;
|
|
Packit Service |
09cdfc |
krb5_ccache cc;
|
|
Packit Service |
09cdfc |
char *cachename;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (env_cachename) {
|
|
Packit Service |
09cdfc |
if (setenv(ENV_NAME, env_cachename, 1))
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: failed to setenv %d\n", __func__, errno);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_cc_default(context, &cc);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: krb5_cc_default returned %d", __func__, ret);
|
|
Packit Service |
09cdfc |
return NULL;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_cc_get_full_name(context, cc, &cachename);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: krb5_cc_get_full_name failed: %d\n", __func__, ret);
|
|
Packit Service |
09cdfc |
} else {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: default ccache is %s\n", __func__, cachename);
|
|
Packit Service |
09cdfc |
krb5_free_string(context, cachename);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (!get_tgt_time(cc)) {
|
|
Packit Service |
09cdfc |
krb5_cc_close(context, cc);
|
|
Packit Service |
09cdfc |
cc = NULL;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
return cc;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static krb5_ccache
|
|
Packit Service |
09cdfc |
init_cc_from_keytab(const char *keytab_name, const char *user)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
krb5_error_code ret;
|
|
Packit Service |
09cdfc |
krb5_creds my_creds;
|
|
Packit Service |
09cdfc |
krb5_keytab keytab = NULL;
|
|
Packit Service |
09cdfc |
krb5_principal me = NULL;
|
|
Packit Service |
09cdfc |
krb5_ccache cc = NULL;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
memset((char *) &my_creds, 0, sizeof(my_creds));
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* Unset the environment variable, if any. If we're creating our own
|
|
Packit Service |
09cdfc |
* credcache here, stick it in the default location.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
unsetenv(ENV_NAME);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (keytab_name)
|
|
Packit Service |
09cdfc |
ret = krb5_kt_resolve(context, keytab_name, &keytab);
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
ret = krb5_kt_default(context, &keytab);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: %d",
|
|
Packit Service |
09cdfc |
keytab_name ? "krb5_kt_resolve" : "krb5_kt_default",
|
|
Packit Service |
09cdfc |
(int)ret);
|
|
Packit Service |
09cdfc |
goto icfk_cleanup;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_parse_name(context, user, &me);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "krb5_parse_name: %d", (int)ret);
|
|
Packit Service |
09cdfc |
goto icfk_cleanup;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_get_init_creds_keytab(context, &my_creds, me,
|
|
Packit Service |
09cdfc |
keytab, 0, NULL, NULL);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "krb5_get_init_creds_keytab: %d", (int)ret);
|
|
Packit Service |
09cdfc |
goto icfk_cleanup;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_cc_resolve(context, "MEMORY:", &cc);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "krb5_cc_resolve: %d", (int)ret);
|
|
Packit Service |
09cdfc |
goto icfk_cleanup;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_cc_initialize(context, cc, me);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "krb5_cc_initialize: %d", (int)ret);
|
|
Packit Service |
09cdfc |
goto icfk_cleanup;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_cc_store_cred(context, cc, &my_creds);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "krb5_cc_store_cred: %d", (int)ret);
|
|
Packit Service |
09cdfc |
goto icfk_cleanup;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
out:
|
|
Packit Service |
09cdfc |
my_creds.client = (krb5_principal)0;
|
|
Packit Service |
09cdfc |
krb5_free_cred_contents(context, &my_creds);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (me)
|
|
Packit Service |
09cdfc |
krb5_free_principal(context, me);
|
|
Packit Service |
09cdfc |
if (keytab)
|
|
Packit Service |
09cdfc |
krb5_kt_close(context, keytab);
|
|
Packit Service |
09cdfc |
return cc;
|
|
Packit Service |
09cdfc |
icfk_cleanup:
|
|
Packit Service |
09cdfc |
if (cc) {
|
|
Packit Service |
09cdfc |
krb5_cc_close(context, cc);
|
|
Packit Service |
09cdfc |
cc = NULL;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static int
|
|
Packit Service |
09cdfc |
cifs_krb5_get_req(const char *host, krb5_ccache ccache,
|
|
Packit Service |
09cdfc |
DATA_BLOB * mechtoken, DATA_BLOB * sess_key)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
krb5_error_code ret;
|
|
Packit Service |
09cdfc |
krb5_keyblock *tokb;
|
|
Packit Service |
09cdfc |
krb5_creds in_creds, *out_creds;
|
|
Packit Service |
09cdfc |
krb5_data apreq_pkt, in_data;
|
|
Packit Service |
09cdfc |
krb5_auth_context auth_context = NULL;
|
|
Packit Service |
09cdfc |
#if defined(HAVE_KRB5_AUTH_CON_SETADDRS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
|
|
Packit Service |
09cdfc |
static char gss_cksum[24] = { 0x10, 0x00, /* ... */};
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
memset(&in_creds, 0, sizeof(in_creds));
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_cc_get_principal(context, ccache, &in_creds.client);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to get client principal name",
|
|
Packit Service |
09cdfc |
__func__);
|
|
Packit Service |
09cdfc |
return ret;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_sname_to_principal(context, host, "cifs", KRB5_NT_UNKNOWN,
|
|
Packit Service |
09cdfc |
&in_creds.server);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to convert sname to princ (%s).",
|
|
Packit Service |
09cdfc |
__func__, host);
|
|
Packit Service |
09cdfc |
goto out_free_principal;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_get_credentials(context, 0, ccache, &in_creds, &out_creds);
|
|
Packit Service |
09cdfc |
krb5_free_principal(context, in_creds.server);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to get credentials for %s",
|
|
Packit Service |
09cdfc |
__func__, host);
|
|
Packit Service |
09cdfc |
goto out_free_principal;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
in_data.length = 0;
|
|
Packit Service |
09cdfc |
in_data.data = NULL;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_auth_con_init(context, &auth_context);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to create auth_context: %d",
|
|
Packit Service |
09cdfc |
__func__, ret);
|
|
Packit Service |
09cdfc |
goto out_free_creds;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#if defined(HAVE_KRB5_AUTH_CON_SETADDRS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
|
|
Packit Service |
09cdfc |
/* Ensure we will get an addressless ticket. */
|
|
Packit Service |
09cdfc |
ret = krb5_auth_con_setaddrs(context, auth_context, NULL, NULL);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to set NULL addrs: %d",
|
|
Packit Service |
09cdfc |
__func__, ret);
|
|
Packit Service |
09cdfc |
goto out_free_auth;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* Create a GSSAPI checksum (0x8003), see RFC 4121.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* The current layout is
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* 0x10, 0x00, 0x00, 0x00 - length = 16
|
|
Packit Service |
09cdfc |
* 0x00, 0x00, 0x00, 0x00 - channel binding info - 16 zero bytes
|
|
Packit Service |
09cdfc |
* 0x00, 0x00, 0x00, 0x00
|
|
Packit Service |
09cdfc |
* 0x00, 0x00, 0x00, 0x00
|
|
Packit Service |
09cdfc |
* 0x00, 0x00, 0x00, 0x00
|
|
Packit Service |
09cdfc |
* 0x00, 0x00, 0x00, 0x00 - flags
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes,
|
|
Packit Service |
09cdfc |
* this is needed to work against some closed source
|
|
Packit Service |
09cdfc |
* SMB servers.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* See https://bugzilla.samba.org/show_bug.cgi?id=7890
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
in_data.data = gss_cksum;
|
|
Packit Service |
09cdfc |
in_data.length = 24;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* MIT krb5 < 1.7 is missing the prototype, but still has the symbol */
|
|
Packit Service |
09cdfc |
#if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
|
|
Packit Service |
09cdfc |
krb5_error_code krb5_auth_con_set_req_cksumtype(
|
|
Packit Service |
09cdfc |
krb5_auth_context auth_context,
|
|
Packit Service |
09cdfc |
krb5_cksumtype cksumtype);
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
ret = krb5_auth_con_set_req_cksumtype(context, auth_context, 0x8003);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to set 0x8003 checksum",
|
|
Packit Service |
09cdfc |
__func__);
|
|
Packit Service |
09cdfc |
goto out_free_auth;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
#endif
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
apreq_pkt.length = 0;
|
|
Packit Service |
09cdfc |
apreq_pkt.data = NULL;
|
|
Packit Service |
09cdfc |
ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
|
|
Packit Service |
09cdfc |
&in_data, out_creds, &apreq_pkt);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to make AP-REQ for %s",
|
|
Packit Service |
09cdfc |
__func__, host);
|
|
Packit Service |
09cdfc |
goto out_free_auth;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ret = krb5_auth_con_getsendsubkey(context, auth_context, &tokb);
|
|
Packit Service |
09cdfc |
if (ret) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: unable to get session key for %s",
|
|
Packit Service |
09cdfc |
__func__, host);
|
|
Packit Service |
09cdfc |
goto out_free_auth;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
*mechtoken = data_blob(apreq_pkt.data, apreq_pkt.length);
|
|
Packit Service |
09cdfc |
*sess_key = data_blob(KRB5_KEY_DATA(tokb), KRB5_KEY_LENGTH(tokb));
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
krb5_free_keyblock(context, tokb);
|
|
Packit Service |
09cdfc |
out_free_auth:
|
|
Packit Service |
09cdfc |
krb5_auth_con_free(context, auth_context);
|
|
Packit Service |
09cdfc |
out_free_creds:
|
|
Packit Service |
09cdfc |
krb5_free_creds(context, out_creds);
|
|
Packit Service |
09cdfc |
out_free_principal:
|
|
Packit Service |
09cdfc |
krb5_free_principal(context, in_creds.client);
|
|
Packit Service |
09cdfc |
return ret;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* Prepares AP-REQ data for mechToken and gets session key
|
|
Packit Service |
09cdfc |
* Uses credentials from cache. It will not ask for password
|
|
Packit Service |
09cdfc |
* you should receive credentials for yuor name manually using
|
|
Packit Service |
09cdfc |
* kinit or whatever you wish.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* in:
|
|
Packit Service |
09cdfc |
* oid - string with OID/ Could be OID_KERBEROS5
|
|
Packit Service |
09cdfc |
* or OID_KERBEROS5_OLD
|
|
Packit Service |
09cdfc |
* principal - Service name.
|
|
Packit Service |
09cdfc |
* Could be "cifs/FQDN" for KRB5 OID
|
|
Packit Service |
09cdfc |
* or for MS_KRB5 OID style server principal
|
|
Packit Service |
09cdfc |
* like "pdc$@YOUR.REALM.NAME"
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* out:
|
|
Packit Service |
09cdfc |
* secblob - pointer for spnego wrapped AP-REQ data to be stored
|
|
Packit Service |
09cdfc |
* sess_key- pointer for SessionKey data to be stored
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* ret: 0 - success, others - failure
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
static int
|
|
Packit Service |
09cdfc |
handle_krb5_mech(const char *oid, const char *host, DATA_BLOB * secblob,
|
|
Packit Service |
09cdfc |
DATA_BLOB * sess_key, krb5_ccache ccache)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int retval;
|
|
Packit Service |
09cdfc |
DATA_BLOB tkt, tkt_wrapped;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: getting service ticket for %s", __func__, host);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* get a kerberos ticket for the service and extract the session key */
|
|
Packit Service |
09cdfc |
retval = cifs_krb5_get_req(host, ccache, &tkt, sess_key);
|
|
Packit Service |
09cdfc |
if (retval) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: failed to obtain service ticket (%d)",
|
|
Packit Service |
09cdfc |
__func__, retval);
|
|
Packit Service |
09cdfc |
return retval;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: obtained service ticket", __func__);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* wrap that up in a nice GSS-API wrapping */
|
|
Packit Service |
09cdfc |
tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* and wrap that in a shiny SPNEGO wrapper */
|
|
Packit Service |
09cdfc |
*secblob = gen_negTokenInit(oid, tkt_wrapped);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
data_blob_free(&tkt_wrapped);
|
|
Packit Service |
09cdfc |
data_blob_free(&tkt);
|
|
Packit Service |
09cdfc |
return retval;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_HOSTNAME 0x1
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_VERSION 0x2
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_SEC 0x4
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_IP 0x8
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_UID 0x10
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_PID 0x20
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_CREDUID 0x40
|
|
Packit Service |
09cdfc |
#define DKD_HAVE_USERNAME 0x80
|
|
Packit Service |
09cdfc |
#define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC)
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
struct decoded_args {
|
|
Packit Service |
09cdfc |
int ver;
|
|
Packit Service |
09cdfc |
char *hostname;
|
|
Packit Service |
09cdfc |
char *ip;
|
|
Packit Service |
09cdfc |
char *username;
|
|
Packit Service |
09cdfc |
uid_t uid;
|
|
Packit Service |
09cdfc |
uid_t creduid;
|
|
Packit Service |
09cdfc |
pid_t pid;
|
|
Packit Service |
09cdfc |
sectype_t sec;
|
|
Packit Service |
09cdfc |
};
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static unsigned int
|
|
Packit Service |
09cdfc |
decode_key_description(const char *desc, struct decoded_args *arg)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int len;
|
|
Packit Service |
09cdfc |
int retval = 0;
|
|
Packit Service |
09cdfc |
char *pos;
|
|
Packit Service |
09cdfc |
const char *tkn = desc;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
do {
|
|
Packit Service |
09cdfc |
pos = index(tkn, ';');
|
|
Packit Service |
09cdfc |
if (strncmp(tkn, "host=", 5) == 0) {
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (pos == NULL)
|
|
Packit Service |
09cdfc |
len = strlen(tkn);
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
len = pos - tkn;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
len -= 5;
|
|
Packit Service |
09cdfc |
free(arg->hostname);
|
|
Packit Service |
09cdfc |
arg->hostname = strndup(tkn + 5, len);
|
|
Packit Service |
09cdfc |
if (arg->hostname == NULL) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Unable to allocate memory");
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_HOSTNAME;
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "host=%s", arg->hostname);
|
|
Packit Service |
09cdfc |
} else if (!strncmp(tkn, "ip4=", 4) || !strncmp(tkn, "ip6=", 4)) {
|
|
Packit Service |
09cdfc |
if (pos == NULL)
|
|
Packit Service |
09cdfc |
len = strlen(tkn);
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
len = pos - tkn;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
len -= 4;
|
|
Packit Service |
09cdfc |
free(arg->ip);
|
|
Packit Service |
09cdfc |
arg->ip = strndup(tkn + 4, len);
|
|
Packit Service |
09cdfc |
if (arg->ip == NULL) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Unable to allocate memory");
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_IP;
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "ip=%s", arg->ip);
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn, "user=", 5) == 0) {
|
|
Packit Service |
09cdfc |
if (pos == NULL)
|
|
Packit Service |
09cdfc |
len = strlen(tkn);
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
len = pos - tkn;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
len -= 5;
|
|
Packit Service |
09cdfc |
free(arg->username);
|
|
Packit Service |
09cdfc |
arg->username = strndup(tkn + 5, len);
|
|
Packit Service |
09cdfc |
if (arg->username == NULL) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Unable to allocate memory");
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_USERNAME;
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "user=%s", arg->username);
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn, "pid=", 4) == 0) {
|
|
Packit Service |
09cdfc |
errno = 0;
|
|
Packit Service |
09cdfc |
arg->pid = strtol(tkn + 4, NULL, 0);
|
|
Packit Service |
09cdfc |
if (errno != 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Invalid pid format: %s",
|
|
Packit Service |
09cdfc |
strerror(errno));
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "pid=%u", arg->pid);
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_PID;
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn, "sec=", 4) == 0) {
|
|
Packit Service |
09cdfc |
if (strncmp(tkn + 4, "krb5", 4) == 0) {
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_SEC;
|
|
Packit Service |
09cdfc |
arg->sec = KRB5;
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn + 4, "mskrb5", 6) == 0) {
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_SEC;
|
|
Packit Service |
09cdfc |
arg->sec = MS_KRB5;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "sec=%d", arg->sec);
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn, "uid=", 4) == 0) {
|
|
Packit Service |
09cdfc |
errno = 0;
|
|
Packit Service |
09cdfc |
arg->uid = strtol(tkn + 4, NULL, 16);
|
|
Packit Service |
09cdfc |
if (errno != 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Invalid uid format: %s",
|
|
Packit Service |
09cdfc |
strerror(errno));
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_UID;
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "uid=%u", arg->uid);
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn, "creduid=", 8) == 0) {
|
|
Packit Service |
09cdfc |
errno = 0;
|
|
Packit Service |
09cdfc |
arg->creduid = strtol(tkn + 8, NULL, 16);
|
|
Packit Service |
09cdfc |
if (errno != 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Invalid creduid format: %s",
|
|
Packit Service |
09cdfc |
strerror(errno));
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_CREDUID;
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "creduid=%u", arg->creduid);
|
|
Packit Service |
09cdfc |
} else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */
|
|
Packit Service |
09cdfc |
errno = 0;
|
|
Packit Service |
09cdfc |
arg->ver = strtol(tkn + 4, NULL, 16);
|
|
Packit Service |
09cdfc |
if (errno != 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Invalid version format: %s",
|
|
Packit Service |
09cdfc |
strerror(errno));
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
retval |= DKD_HAVE_VERSION;
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "ver=%d", arg->ver);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
if (pos == NULL)
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
tkn = pos + 1;
|
|
Packit Service |
09cdfc |
} while (tkn);
|
|
Packit Service |
09cdfc |
return retval;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static int cifs_resolver(const key_serial_t key, const char *key_descr)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int c;
|
|
Packit Service |
09cdfc |
struct addrinfo *addr;
|
|
Packit Service |
09cdfc |
char ip[INET6_ADDRSTRLEN];
|
|
Packit Service |
09cdfc |
void *p;
|
|
Packit Service |
09cdfc |
const char *keyend = key_descr;
|
|
Packit Service |
09cdfc |
/* skip next 4 ';' delimiters to get to description */
|
|
Packit Service |
09cdfc |
for (c = 1; c <= 4; c++) {
|
|
Packit Service |
09cdfc |
keyend = index(keyend + 1, ';');
|
|
Packit Service |
09cdfc |
if (!keyend) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "invalid key description: %s",
|
|
Packit Service |
09cdfc |
key_descr);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
keyend++;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* resolve name to ip */
|
|
Packit Service |
09cdfc |
c = getaddrinfo(keyend, NULL, NULL, &addr);
|
|
Packit Service |
09cdfc |
if (c) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "unable to resolve hostname: %s [%s]",
|
|
Packit Service |
09cdfc |
keyend, gai_strerror(c));
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* conver ip to string form */
|
|
Packit Service |
09cdfc |
if (addr->ai_family == AF_INET)
|
|
Packit Service |
09cdfc |
p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr);
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "%s: inet_ntop: %s", __func__, strerror(errno));
|
|
Packit Service |
09cdfc |
freeaddrinfo(addr);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* setup key */
|
|
Packit Service |
09cdfc |
c = keyctl_instantiate(key, ip, strlen(ip) + 1, 0);
|
|
Packit Service |
09cdfc |
if (c == -1) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "%s: keyctl_instantiate: %s", __func__,
|
|
Packit Service |
09cdfc |
strerror(errno));
|
|
Packit Service |
09cdfc |
freeaddrinfo(addr);
|
|
Packit Service |
09cdfc |
return 1;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
freeaddrinfo(addr);
|
|
Packit Service |
09cdfc |
return 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* Older kernels sent IPv6 addresses without colons. Well, at least
|
|
Packit Service |
09cdfc |
* they're fixed-length strings. Convert these addresses to have colon
|
|
Packit Service |
09cdfc |
* delimiters to make getaddrinfo happy.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
static void convert_inet6_addr(const char *from, char *to)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int i = 1;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
while (*from) {
|
|
Packit Service |
09cdfc |
*to++ = *from++;
|
|
Packit Service |
09cdfc |
if (!(i++ % 4) && *from)
|
|
Packit Service |
09cdfc |
*to++ = ':';
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
*to = 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static int ip_to_fqdn(const char *addrstr, char *host, size_t hostlen)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int rc;
|
|
Packit Service |
09cdfc |
struct addrinfo hints = {.ai_flags = AI_NUMERICHOST };
|
|
Packit Service |
09cdfc |
struct addrinfo *res;
|
|
Packit Service |
09cdfc |
const char *ipaddr = addrstr;
|
|
Packit Service |
09cdfc |
char converted[INET6_ADDRSTRLEN + 1];
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if ((strlen(ipaddr) > INET_ADDRSTRLEN) && !strchr(ipaddr, ':')) {
|
|
Packit Service |
09cdfc |
convert_inet6_addr(ipaddr, converted);
|
|
Packit Service |
09cdfc |
ipaddr = converted;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = getaddrinfo(ipaddr, NULL, &hints, &res;;
|
|
Packit Service |
09cdfc |
if (rc) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: failed to resolve %s to "
|
|
Packit Service |
09cdfc |
"ipaddr: %s", __func__, ipaddr,
|
|
Packit Service |
09cdfc |
rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
|
|
Packit Service |
09cdfc |
return rc;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = getnameinfo(res->ai_addr, res->ai_addrlen, host, hostlen,
|
|
Packit Service |
09cdfc |
NULL, 0, NI_NAMEREQD);
|
|
Packit Service |
09cdfc |
freeaddrinfo(res);
|
|
Packit Service |
09cdfc |
if (rc) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: failed to resolve %s to fqdn: %s",
|
|
Packit Service |
09cdfc |
__func__, ipaddr,
|
|
Packit Service |
09cdfc |
rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
|
|
Packit Service |
09cdfc |
return rc;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "%s: resolved %s to %s", __func__, ipaddr, host);
|
|
Packit Service |
09cdfc |
return 0;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* walk a string and lowercase it in-place */
|
|
Packit Service |
09cdfc |
static void
|
|
Packit Service |
09cdfc |
lowercase_string(char *c)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
while(*c) {
|
|
Packit Service |
09cdfc |
*c = tolower(*c);
|
|
Packit Service |
09cdfc |
++c;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static void usage(void)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
fprintf(stderr, "Usage: %s [ -K /path/to/keytab] [-k /path/to/krb5.conf] [-E] [-t] [-v] [-l] key_serial\n", prog);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
static const struct option long_options[] = {
|
|
Packit Service |
09cdfc |
{"no-env-probe", 0, NULL, 'E'},
|
|
Packit Service |
09cdfc |
{"krb5conf", 1, NULL, 'k'},
|
|
Packit Service |
09cdfc |
{"legacy-uid", 0, NULL, 'l'},
|
|
Packit Service |
09cdfc |
{"trust-dns", 0, NULL, 't'},
|
|
Packit Service |
09cdfc |
{"keytab", 1, NULL, 'K'},
|
|
Packit Service |
09cdfc |
{"version", 0, NULL, 'v'},
|
|
Packit Service |
09cdfc |
{NULL, 0, NULL, 0}
|
|
Packit Service |
09cdfc |
};
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
int main(const int argc, char *const argv[])
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
struct cifs_spnego_msg *keydata = NULL;
|
|
Packit Service |
09cdfc |
DATA_BLOB secblob = data_blob_null;
|
|
Packit Service |
09cdfc |
DATA_BLOB sess_key = data_blob_null;
|
|
Packit Service |
09cdfc |
key_serial_t key = 0;
|
|
Packit Service |
09cdfc |
size_t datalen;
|
|
Packit Service |
09cdfc |
unsigned int have;
|
|
Packit Service |
09cdfc |
long rc = 1;
|
|
Packit Service |
09cdfc |
int c;
|
|
Packit Service |
09cdfc |
bool try_dns = false, legacy_uid = false , env_probe = true;
|
|
Packit Service |
09cdfc |
char *buf;
|
|
Packit Service |
09cdfc |
char hostbuf[NI_MAXHOST], *host;
|
|
Packit Service |
09cdfc |
struct decoded_args arg;
|
|
Packit Service |
09cdfc |
const char *oid;
|
|
Packit Service |
09cdfc |
uid_t uid;
|
|
Packit Service |
09cdfc |
char *keytab_name = NULL;
|
|
Packit Service |
09cdfc |
char *env_cachename = NULL;
|
|
Packit Service |
09cdfc |
krb5_ccache ccache = NULL;
|
|
Packit Service |
09cdfc |
struct passwd *pw;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
hostbuf[0] = '\0';
|
|
Packit Service |
09cdfc |
memset(&arg, 0, sizeof(arg));
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
openlog(prog, 0, LOG_DAEMON);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
while ((c = getopt_long(argc, argv, "cEk:K:ltv", long_options, NULL)) != -1) {
|
|
Packit Service |
09cdfc |
switch (c) {
|
|
Packit Service |
09cdfc |
case 'c':
|
|
Packit Service |
09cdfc |
/* legacy option -- skip it */
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case 'E':
|
|
Packit Service |
09cdfc |
/* skip probing initiating process env */
|
|
Packit Service |
09cdfc |
env_probe = false;
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case 't':
|
|
Packit Service |
09cdfc |
try_dns = true;
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case 'k':
|
|
Packit Service |
09cdfc |
if (setenv("KRB5_CONFIG", optarg, 1) != 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "unable to set $KRB5_CONFIG: %d", errno);
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case 'K':
|
|
Packit Service |
09cdfc |
keytab_name = optarg;
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case 'l':
|
|
Packit Service |
09cdfc |
legacy_uid = true;
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case 'v':
|
|
Packit Service |
09cdfc |
rc = 0;
|
|
Packit Service |
09cdfc |
printf("version: %s\n", VERSION);
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
default:
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "unknown option: %c", c);
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (trim_capabilities(env_probe))
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* is there a key? */
|
|
Packit Service |
09cdfc |
if (argc <= optind) {
|
|
Packit Service |
09cdfc |
usage();
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* get key and keyring values */
|
|
Packit Service |
09cdfc |
errno = 0;
|
|
Packit Service |
09cdfc |
key = strtol(argv[optind], NULL, 10);
|
|
Packit Service |
09cdfc |
if (errno != 0) {
|
|
Packit Service |
09cdfc |
key = 0;
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Invalid key format: %s", strerror(errno));
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = keyctl_describe_alloc(key, &buf;;
|
|
Packit Service |
09cdfc |
if (rc == -1) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "keyctl_describe_alloc failed: %s",
|
|
Packit Service |
09cdfc |
strerror(errno));
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "key description: %s", buf);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver") - 1) == 0) ||
|
|
Packit Service |
09cdfc |
(strncmp(buf, "dns_resolver", sizeof("dns_resolver") - 1) == 0)) {
|
|
Packit Service |
09cdfc |
rc = cifs_resolver(key, buf);
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
have = decode_key_description(buf, &arg;;
|
|
Packit Service |
09cdfc |
free(buf);
|
|
Packit Service |
09cdfc |
if ((have & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "unable to get necessary params from key "
|
|
Packit Service |
09cdfc |
"description (0x%x)", have);
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (arg.ver > CIFS_SPNEGO_UPCALL_VERSION) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "incompatible kernel upcall version: 0x%x",
|
|
Packit Service |
09cdfc |
arg.ver);
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (strlen(arg.hostname) >= NI_MAXHOST) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "hostname provided by kernel is too long");
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (!legacy_uid && (have & DKD_HAVE_CREDUID))
|
|
Packit Service |
09cdfc |
uid = arg.creduid;
|
|
Packit Service |
09cdfc |
else if (have & DKD_HAVE_UID)
|
|
Packit Service |
09cdfc |
uid = arg.uid;
|
|
Packit Service |
09cdfc |
else {
|
|
Packit Service |
09cdfc |
/* no uid= or creduid= parm -- something is wrong */
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "No uid= or creduid= parm specified");
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* The kernel doesn't pass down the gid, so we resort here to scraping
|
|
Packit Service |
09cdfc |
* one out of the passwd nss db. Note that this might not reflect the
|
|
Packit Service |
09cdfc |
* actual gid of the process that initiated the upcall. While we could
|
|
Packit Service |
09cdfc |
* scrape that out of /proc, relying on that is a bit more risky.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
pw = getpwuid(uid);
|
|
Packit Service |
09cdfc |
if (!pw) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Unable to find pw entry for uid %d: %s\n",
|
|
Packit Service |
09cdfc |
uid, strerror(errno));
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* The kernel should send down a zero-length grouplist already, but
|
|
Packit Service |
09cdfc |
* just to be on the safe side...
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
rc = setgroups(0, NULL);
|
|
Packit Service |
09cdfc |
if (rc == -1) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "setgroups: %s", strerror(errno));
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = setgid(pw->pw_gid);
|
|
Packit Service |
09cdfc |
if (rc == -1) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "setgid: %s", strerror(errno));
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* We can't reasonably do this for root. When mounting a DFS share,
|
|
Packit Service |
09cdfc |
* for instance we can end up with creds being overridden, but the env
|
|
Packit Service |
09cdfc |
* variable left intact.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
if (uid == 0)
|
|
Packit Service |
09cdfc |
env_probe = false;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* Must do this before setuid, as we need elevated capabilities to
|
|
Packit Service |
09cdfc |
* look at the environ file.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
env_cachename =
|
|
Packit Service |
09cdfc |
get_cachename_from_process_env(env_probe ? arg.pid : 0);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = setuid(uid);
|
|
Packit Service |
09cdfc |
if (rc == -1) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "setuid: %s", strerror(errno));
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = drop_all_capabilities();
|
|
Packit Service |
09cdfc |
if (rc)
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = krb5_init_context(&context);
|
|
Packit Service |
09cdfc |
if (rc) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "unable to init krb5 context: %ld", rc);
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
ccache = get_existing_cc(env_cachename);
|
|
Packit Service |
09cdfc |
/* Couldn't find credcache? Try to use keytab */
|
|
Packit Service |
09cdfc |
if (ccache == NULL && arg.username != NULL)
|
|
Packit Service |
09cdfc |
ccache = init_cc_from_keytab(keytab_name, arg.username);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (ccache == NULL) {
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
host = arg.hostname;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
// do mech specific authorization
|
|
Packit Service |
09cdfc |
switch (arg.sec) {
|
|
Packit Service |
09cdfc |
case MS_KRB5:
|
|
Packit Service |
09cdfc |
case KRB5:
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* Andrew Bartlett's suggested scheme for picking a principal
|
|
Packit Service |
09cdfc |
* name, based on a supplied hostname.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* INPUT: fooo
|
|
Packit Service |
09cdfc |
* TRY in order:
|
|
Packit Service |
09cdfc |
* cifs/fooo@REALM
|
|
Packit Service |
09cdfc |
* cifs/fooo.<guessed domain ?>@REALM
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* INPUT: bar.example.com
|
|
Packit Service |
09cdfc |
* TRY only:
|
|
Packit Service |
09cdfc |
* cifs/bar.example.com@REALM
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
if (arg.sec == MS_KRB5)
|
|
Packit Service |
09cdfc |
oid = OID_KERBEROS5_OLD;
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
oid = OID_KERBEROS5;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
retry_new_hostname:
|
|
Packit Service |
09cdfc |
lowercase_string(host);
|
|
Packit Service |
09cdfc |
rc = handle_krb5_mech(oid, host, &secblob, &sess_key, ccache);
|
|
Packit Service |
09cdfc |
if (!rc)
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* If hostname has a '.', assume it's a FQDN, otherwise we
|
|
Packit Service |
09cdfc |
* want to guess the domainname.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
if (!strchr(host, '.')) {
|
|
Packit Service |
09cdfc |
struct addrinfo hints;
|
|
Packit Service |
09cdfc |
struct addrinfo *ai;
|
|
Packit Service |
09cdfc |
char *domainname;
|
|
Packit Service |
09cdfc |
char fqdn[NI_MAXHOST];
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* use getaddrinfo() to resolve the hostname of the
|
|
Packit Service |
09cdfc |
* server and set ai_canonname.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
memset(&hints, 0, sizeof(hints));
|
|
Packit Service |
09cdfc |
hints.ai_family = AF_UNSPEC;
|
|
Packit Service |
09cdfc |
hints.ai_flags = AI_CANONNAME;
|
|
Packit Service |
09cdfc |
rc = getaddrinfo(host, NULL, &hints, &ai;;
|
|
Packit Service |
09cdfc |
if (rc) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Unable to resolve host address: %s [%s]",
|
|
Packit Service |
09cdfc |
host, gai_strerror(rc));
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* scan forward to first '.' in ai_canonnname */
|
|
Packit Service |
09cdfc |
domainname = strchr(ai->ai_canonname, '.');
|
|
Packit Service |
09cdfc |
if (!domainname) {
|
|
Packit Service |
09cdfc |
rc = -EINVAL;
|
|
Packit Service |
09cdfc |
freeaddrinfo(ai);
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
lowercase_string(domainname);
|
|
Packit Service |
09cdfc |
rc = snprintf(fqdn, sizeof(fqdn), "%s%s",
|
|
Packit Service |
09cdfc |
host, domainname);
|
|
Packit Service |
09cdfc |
freeaddrinfo(ai);
|
|
Packit Service |
09cdfc |
if (rc < 0 || (size_t)rc >= sizeof(fqdn)) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "Problem setting hostname in string: %ld", rc);
|
|
Packit Service |
09cdfc |
rc = -EINVAL;
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = handle_krb5_mech(oid, fqdn, &secblob, &sess_key, ccache);
|
|
Packit Service |
09cdfc |
if (!rc)
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (!try_dns || !(have & DKD_HAVE_IP))
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = ip_to_fqdn(arg.ip, hostbuf, sizeof(hostbuf));
|
|
Packit Service |
09cdfc |
if (rc)
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
try_dns = false;
|
|
Packit Service |
09cdfc |
host = hostbuf;
|
|
Packit Service |
09cdfc |
goto retry_new_hostname;
|
|
Packit Service |
09cdfc |
default:
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "sectype: %d is not implemented", arg.sec);
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (rc) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "Unable to obtain service ticket");
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* pack SecurityBlob and SessionKey into downcall packet */
|
|
Packit Service |
09cdfc |
datalen =
|
|
Packit Service |
09cdfc |
sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length;
|
|
Packit Service |
09cdfc |
keydata = (struct cifs_spnego_msg *)calloc(sizeof(char), datalen);
|
|
Packit Service |
09cdfc |
if (!keydata) {
|
|
Packit Service |
09cdfc |
rc = 1;
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
keydata->version = arg.ver;
|
|
Packit Service |
09cdfc |
keydata->flags = 0;
|
|
Packit Service |
09cdfc |
keydata->sesskey_len = sess_key.length;
|
|
Packit Service |
09cdfc |
keydata->secblob_len = secblob.length;
|
|
Packit Service |
09cdfc |
memcpy(&(keydata->data), sess_key.data, sess_key.length);
|
|
Packit Service |
09cdfc |
memcpy(&(keydata->data) + keydata->sesskey_len,
|
|
Packit Service |
09cdfc |
secblob.data, secblob.length);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* setup key */
|
|
Packit Service |
09cdfc |
rc = keyctl_instantiate(key, keydata, datalen, 0);
|
|
Packit Service |
09cdfc |
if (rc == -1) {
|
|
Packit Service |
09cdfc |
syslog(LOG_ERR, "keyctl_instantiate: %s", strerror(errno));
|
|
Packit Service |
09cdfc |
goto out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/* BB: maybe we need use timeout for key: for example no more then
|
|
Packit Service |
09cdfc |
* ticket lifietime? */
|
|
Packit Service |
09cdfc |
/* keyctl_set_timeout( key, 60); */
|
|
Packit Service |
09cdfc |
out:
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* on error, negatively instantiate the key ourselves so that we can
|
|
Packit Service |
09cdfc |
* make sure the kernel doesn't hang it off of a searchable keyring
|
|
Packit Service |
09cdfc |
* and interfere with the next attempt to instantiate the key.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
if (rc != 0 && key == 0) {
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "Negating key");
|
|
Packit Service |
09cdfc |
keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
data_blob_free(&secblob);
|
|
Packit Service |
09cdfc |
data_blob_free(&sess_key);
|
|
Packit Service |
09cdfc |
if (ccache)
|
|
Packit Service |
09cdfc |
krb5_cc_close(context, ccache);
|
|
Packit Service |
09cdfc |
if (context)
|
|
Packit Service |
09cdfc |
krb5_free_context(context);
|
|
Packit Service |
09cdfc |
free(arg.hostname);
|
|
Packit Service |
09cdfc |
free(arg.ip);
|
|
Packit Service |
09cdfc |
free(arg.username);
|
|
Packit Service |
09cdfc |
free(keydata);
|
|
Packit Service |
09cdfc |
free(env_cachename);
|
|
Packit Service |
09cdfc |
syslog(LOG_DEBUG, "Exit status %ld", rc);
|
|
Packit Service |
09cdfc |
return rc;
|
|
Packit Service |
09cdfc |
}
|