Move some infra bits of lib/krb5/ to lib/base/ (2)
This is the second of two commits in a series that must be picked together. This series of two commits moves parts of lib/krb5/ infrastructure functionality to lib/base/, leaving behind wrappers. Some parts of libkrb5 are entirely generic or easily made so, and could be useful in various parts of Heimdal that are not specific to the krb5 API, such as: - lib/gssapi/ (especially since the integration of NegoEx) - lib/hx509/ - bx509d (which should really move out of kdc/) For the above we need to move these bits of lib/krb5/: - lib/krb5/config_file.c (all of it, leaving forwardings behind) - lib/krb5/config_reg.c (all of it) - lib/krb5/plugin.c (all of it, leaving forwardings behind) - lib/krb5/log.c (all of it, ditto) - lib/krb5/heim_err.et (all of it) And because of those two, these too must also move: - lib/krb5/expand_path.c (all of it, leaving forwardings behind) - lib/krb5/warn.c (just the warning functions, ditto) The changes to the moved files are mostly quite straightforward and are best reviewed with --word-diff=color. We're also creating a heim_context and a heim API to go with it. But it's as thin as possible, with as little state as necessary to enable this move. Functions for dealing with error messages use callbacks. Moving plugin.c does have one knock-on effect on all users of the old krb5 plugin API (which remains), which is that a global search and replace of struct krb5_plugin_data to struct heim_plugin_data was needed, though the layout and size of that structure doesn't change, so the ABI doesn't either. As well, we now build lib/vers/ and lib/com_err/ before lib/base/ so as to be able to move lib/krb5/heim_err.et to lib/base/ so that we can make use of HEIM_ERR_* in lib/base/, specifically in the files that moved. Once this is all done we'll be able to use config files and plugins in lib/hx509/, we'll be able to move bx509d out of kdc/, and so on. Most if not all of the new functions in lib/base/ are Heimdal-private, thus calling conventions for them are not declared. Status: - builds and passes CIs (Travis, Appveyor) - ran make check-valgrind and no new leaks or other memory errors - ready for review HOW TO REVIEW: $ # Review file moves: $ git log --stat -n1 HEAD^ $ $ # Review changes to moved files using --word-diff=color $ git log -p -b -w --word-diff=color HEAD^..HEAD \ lib/base/config_file.c \ lib/base/config_reg.c \ lib/base/expand_path.c \ lib/base/warn.c \ lib/krb5/config_file.c \ lib/krb5/config_reg.c \ lib/krb5/expand_path.c \ lib/krb5/warn.c $ $ # Review the whole thing, possibly adding -b and/or -w, and $ # maybe --word-diff=color: $ git log -p origin/master..HEAD $ git log -p -b -w origin/master..HEAD $ git log -p -b -w --word-diff=color origin/master..HEAD TBD (future commits): - make lib/gssapi use the new heimbase functions - move kx509/bx509d common code to lib/hx509/ or other approp. location - move bx509d out of kdc/
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
|
||||
/***********************************************************************
|
||||
* Copyright (c) 2009, Secure Endpoints Inc.
|
||||
* Copyright (c) 2009-2020, Secure Endpoints Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -30,7 +30,7 @@
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#include "krb5_locl.h"
|
||||
#include "baselocl.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
@@ -51,35 +51,35 @@ typedef int PTYPE;
|
||||
* temporary directory until the user profile is loaded. In addition,
|
||||
* the returned path may or may not exist.
|
||||
*/
|
||||
static krb5_error_code
|
||||
_expand_temp_folder(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_temp_folder(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
TCHAR tpath[MAX_PATH];
|
||||
size_t len;
|
||||
|
||||
if (!GetTempPath(sizeof(tpath)/sizeof(tpath[0]), tpath)) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, EINVAL,
|
||||
"Failed to get temporary path (GLE=%d)",
|
||||
GetLastError());
|
||||
return EINVAL;
|
||||
if (context)
|
||||
heim_set_error_message(context, EINVAL,
|
||||
"Failed to get temporary path (GLE=%d)",
|
||||
GetLastError());
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
len = strlen(tpath);
|
||||
|
||||
if (len > 0 && tpath[len - 1] == '\\')
|
||||
tpath[len - 1] = '\0';
|
||||
tpath[len - 1] = '\0';
|
||||
|
||||
*ret = strdup(tpath);
|
||||
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern HINSTANCE _krb5_hInstance;
|
||||
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
|
||||
|
||||
/*
|
||||
* Expand a %{BINDIR} token
|
||||
@@ -87,40 +87,41 @@ extern HINSTANCE _krb5_hInstance;
|
||||
* This is also used to expand a few other tokens on Windows, since
|
||||
* most of the executable binaries end up in the same directory. The
|
||||
* "bin" directory is considered to be the directory in which the
|
||||
* krb5.dll is located.
|
||||
* containing DLL is located.
|
||||
*/
|
||||
static krb5_error_code
|
||||
_expand_bin_dir(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_bin_dir(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
TCHAR path[MAX_PATH];
|
||||
TCHAR *lastSlash;
|
||||
DWORD nc;
|
||||
|
||||
nc = GetModuleFileName(_krb5_hInstance, path, sizeof(path)/sizeof(path[0]));
|
||||
nc = GetModuleFileName((HINSTANCE)&__ImageBase, path,
|
||||
sizeof(path)/sizeof(path[0]));
|
||||
if (nc == 0 ||
|
||||
nc == sizeof(path)/sizeof(path[0])) {
|
||||
return EINVAL;
|
||||
nc == sizeof(path)/sizeof(path[0])) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
lastSlash = strrchr(path, '\\');
|
||||
if (lastSlash != NULL) {
|
||||
TCHAR *fslash = strrchr(lastSlash, '/');
|
||||
TCHAR *fslash = strrchr(lastSlash, '/');
|
||||
|
||||
if (fslash != NULL)
|
||||
lastSlash = fslash;
|
||||
if (fslash != NULL)
|
||||
lastSlash = fslash;
|
||||
|
||||
*lastSlash = '\0';
|
||||
*lastSlash = '\0';
|
||||
}
|
||||
|
||||
if (postfix) {
|
||||
if (strlcat(path, postfix, sizeof(path)/sizeof(path[0])) >= sizeof(path)/sizeof(path[0]))
|
||||
return EINVAL;
|
||||
if (strlcat(path, postfix, sizeof(path)/sizeof(path[0])) >= sizeof(path)/sizeof(path[0]))
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
*ret = strdup(path);
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -140,9 +141,9 @@ _expand_bin_dir(krb5_context context, PTYPE param, const char *postfix,
|
||||
* SecurityIdentification level the call will fail.
|
||||
*
|
||||
*/
|
||||
static krb5_error_code
|
||||
_expand_userid(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_userid(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
int rv = EINVAL;
|
||||
HANDLE hThread = NULL;
|
||||
@@ -154,83 +155,83 @@ _expand_userid(krb5_context context, PTYPE param, const char *postfix,
|
||||
hThread = GetCurrentThread();
|
||||
|
||||
if (!OpenThreadToken(hThread, TOKEN_QUERY,
|
||||
FALSE, /* Open the thread token as the
|
||||
current thread user. */
|
||||
&hToken)) {
|
||||
FALSE, /* Open the thread token as the
|
||||
current thread user. */
|
||||
&hToken)) {
|
||||
|
||||
DWORD le = GetLastError();
|
||||
DWORD le = GetLastError();
|
||||
|
||||
if (le == ERROR_NO_TOKEN) {
|
||||
HANDLE hProcess = GetCurrentProcess();
|
||||
if (le == ERROR_NO_TOKEN) {
|
||||
HANDLE hProcess = GetCurrentProcess();
|
||||
|
||||
le = 0;
|
||||
if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
|
||||
le = GetLastError();
|
||||
}
|
||||
le = 0;
|
||||
if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
|
||||
le = GetLastError();
|
||||
}
|
||||
|
||||
if (le != 0) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv,
|
||||
"Can't open thread token (GLE=%d)", le);
|
||||
goto _exit;
|
||||
}
|
||||
if (le != 0) {
|
||||
if (context)
|
||||
heim_set_error_message(context, rv,
|
||||
"Can't open thread token (GLE=%d)", le);
|
||||
goto _exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &len)) {
|
||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv,
|
||||
"Unexpected error reading token information (GLE=%d)",
|
||||
GetLastError());
|
||||
goto _exit;
|
||||
}
|
||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
||||
if (context)
|
||||
heim_set_error_message(context, rv,
|
||||
"Unexpected error reading token information (GLE=%d)",
|
||||
GetLastError());
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv,
|
||||
"GetTokenInformation() returned truncated buffer");
|
||||
goto _exit;
|
||||
}
|
||||
if (len == 0) {
|
||||
if (context)
|
||||
heim_set_error_message(context, rv,
|
||||
"GetTokenInformation() returned truncated buffer");
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
pOwner = malloc(len);
|
||||
if (pOwner == NULL) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv, "Out of memory");
|
||||
goto _exit;
|
||||
}
|
||||
pOwner = malloc(len);
|
||||
if (pOwner == NULL) {
|
||||
if (context)
|
||||
heim_set_error_message(context, rv, "Out of memory");
|
||||
goto _exit;
|
||||
}
|
||||
} else {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv, "GetTokenInformation() returned truncated buffer");
|
||||
goto _exit;
|
||||
if (context)
|
||||
heim_set_error_message(context, rv, "GetTokenInformation() returned truncated buffer");
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
if (!GetTokenInformation(hToken, TokenOwner, pOwner, len, &len)) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv, "GetTokenInformation() failed. GLE=%d", GetLastError());
|
||||
goto _exit;
|
||||
if (context)
|
||||
heim_set_error_message(context, rv, "GetTokenInformation() failed. GLE=%d", GetLastError());
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
if (!ConvertSidToStringSid(pOwner->Owner, &strSid)) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, rv, "Can't convert SID to string. GLE=%d", GetLastError());
|
||||
goto _exit;
|
||||
if (context)
|
||||
heim_set_error_message(context, rv, "Can't convert SID to string. GLE=%d", GetLastError());
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
*ret = strdup(strSid);
|
||||
if (*ret == NULL && context)
|
||||
krb5_set_error_message(context, rv, "Out of memory");
|
||||
heim_set_error_message(context, rv, "Out of memory");
|
||||
|
||||
rv = 0;
|
||||
|
||||
_exit:
|
||||
if (hToken != NULL)
|
||||
CloseHandle(hToken);
|
||||
CloseHandle(hToken);
|
||||
|
||||
if (pOwner != NULL)
|
||||
free (pOwner);
|
||||
free (pOwner);
|
||||
|
||||
if (strSid != NULL)
|
||||
LocalFree(strSid);
|
||||
LocalFree(strSid);
|
||||
|
||||
return rv;
|
||||
}
|
||||
@@ -239,129 +240,129 @@ _expand_userid(krb5_context context, PTYPE param, const char *postfix,
|
||||
* Expand a folder identified by a CSIDL
|
||||
*/
|
||||
|
||||
static krb5_error_code
|
||||
_expand_csidl(krb5_context context, PTYPE folder, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_csidl(heim_context context, PTYPE folder, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
TCHAR path[MAX_PATH];
|
||||
size_t len;
|
||||
|
||||
if (SHGetFolderPath(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path) != S_OK) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, EINVAL, "Unable to determine folder path");
|
||||
return EINVAL;
|
||||
if (context)
|
||||
heim_set_error_message(context, EINVAL, "Unable to determine folder path");
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
len = strlen(path);
|
||||
|
||||
if (len > 0 && path[len - 1] == '\\')
|
||||
path[len - 1] = '\0';
|
||||
path[len - 1] = '\0';
|
||||
|
||||
if (postfix &&
|
||||
strlcat(path, postfix, sizeof(path)/sizeof(path[0])) >= sizeof(path)/sizeof(path[0]))
|
||||
return krb5_enomem(context);
|
||||
strlcat(path, postfix, sizeof(path)/sizeof(path[0])) >= sizeof(path)/sizeof(path[0]))
|
||||
return heim_enomem(context);
|
||||
|
||||
*ret = strdup(path);
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static krb5_error_code
|
||||
_expand_path(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_path(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
*ret = strdup(postfix);
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static krb5_error_code
|
||||
_expand_temp_folder(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_temp_folder(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
const char *p = NULL;
|
||||
|
||||
p = secure_getenv("TEMP");
|
||||
|
||||
if (p)
|
||||
*ret = strdup(p);
|
||||
*ret = strdup(p);
|
||||
else
|
||||
*ret = strdup("/tmp");
|
||||
*ret = strdup("/tmp");
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static krb5_error_code
|
||||
_expand_userid(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
static heim_error_code
|
||||
expand_userid(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
{
|
||||
int ret = asprintf(str, "%ld", (unsigned long)getuid());
|
||||
if (ret < 0 || *str == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static krb5_error_code
|
||||
_expand_euid(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
static heim_error_code
|
||||
expand_euid(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
{
|
||||
int ret = asprintf(str, "%ld", (unsigned long)geteuid());
|
||||
if (ret < 0 || *str == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
#endif /* _WIN32 */
|
||||
|
||||
static krb5_error_code
|
||||
_expand_username(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
static heim_error_code
|
||||
expand_username(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
{
|
||||
char user[128];
|
||||
const char *username = roken_get_username(user, sizeof(user));
|
||||
|
||||
if (username == NULL) {
|
||||
krb5_set_error_message(context, ENOTTY,
|
||||
N_("unable to figure out current principal",
|
||||
""));
|
||||
return ENOTTY; /* XXX */
|
||||
heim_set_error_message(context, ENOTTY,
|
||||
N_("unable to figure out current principal",
|
||||
""));
|
||||
return ENOTTY; /* XXX */
|
||||
}
|
||||
|
||||
*str = strdup(username);
|
||||
if (*str == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static krb5_error_code
|
||||
_expand_loginname(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
static heim_error_code
|
||||
expand_loginname(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **str)
|
||||
{
|
||||
char user[128];
|
||||
const char *username = roken_get_loginname(user, sizeof(user));
|
||||
|
||||
if (username == NULL) {
|
||||
krb5_set_error_message(context, ENOTTY,
|
||||
N_("unable to figure out current principal",
|
||||
""));
|
||||
return ENOTTY; /* XXX */
|
||||
heim_set_error_message(context, ENOTTY,
|
||||
N_("unable to figure out current principal",
|
||||
""));
|
||||
return ENOTTY; /* XXX */
|
||||
}
|
||||
|
||||
*str = strdup(username);
|
||||
if (*str == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static krb5_error_code
|
||||
_expand_strftime(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_strftime(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
size_t len;
|
||||
time_t t;
|
||||
@@ -370,7 +371,7 @@ _expand_strftime(krb5_context context, PTYPE param, const char *postfix,
|
||||
t = time(NULL);
|
||||
len = strftime(buf, sizeof(buf), arg, localtime(&t));
|
||||
if (len == 0 || len >= sizeof(buf))
|
||||
return ENOMEM;
|
||||
return ENOMEM;
|
||||
*ret = strdup(buf);
|
||||
return 0;
|
||||
}
|
||||
@@ -379,12 +380,12 @@ _expand_strftime(krb5_context context, PTYPE param, const char *postfix,
|
||||
* Expand an extra token
|
||||
*/
|
||||
|
||||
static krb5_error_code
|
||||
_expand_extra_token(krb5_context context, const char *value, char **ret)
|
||||
static heim_error_code
|
||||
expand_extra_token(heim_context context, const char *value, char **ret)
|
||||
{
|
||||
*ret = strdup(value);
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -394,13 +395,13 @@ _expand_extra_token(krb5_context context, const char *value, char **ret)
|
||||
* The expansion of a %{null} token is always the empty string.
|
||||
*/
|
||||
|
||||
static krb5_error_code
|
||||
_expand_null(krb5_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
static heim_error_code
|
||||
expand_null(heim_context context, PTYPE param, const char *postfix,
|
||||
const char *arg, char **ret)
|
||||
{
|
||||
*ret = strdup("");
|
||||
if (*ret == NULL)
|
||||
return krb5_enomem(context);
|
||||
return heim_enomem(context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -414,14 +415,14 @@ static const struct {
|
||||
PTYPE param;
|
||||
const char * postfix;
|
||||
|
||||
int (*exp_func)(krb5_context, PTYPE, const char *, const char *, char **);
|
||||
int (*exp_func)(heim_context, PTYPE, const char *, const char *, char **);
|
||||
|
||||
#define SPECIALP(f, P) FTYPE_SPECIAL, 0, P, f
|
||||
#define SPECIAL(f) SPECIALP(f, NULL)
|
||||
|
||||
} tokens[] = {
|
||||
#ifdef _WIN32
|
||||
#define CSIDLP(C,P) FTYPE_CSIDL, C, P, _expand_csidl
|
||||
#define CSIDLP(C,P) FTYPE_CSIDL, C, P, expand_csidl
|
||||
#define CSIDL(C) CSIDLP(C, NULL)
|
||||
|
||||
{"APPDATA", CSIDL(CSIDL_APPDATA)}, /* Roaming application data (for current user) */
|
||||
@@ -431,36 +432,36 @@ static const struct {
|
||||
{"WINDOWS", CSIDL(CSIDL_WINDOWS)}, /* Windows folder */
|
||||
{"USERCONFIG", CSIDLP(CSIDL_APPDATA, "\\" PACKAGE)}, /* Per user Heimdal configuration file path */
|
||||
{"COMMONCONFIG", CSIDLP(CSIDL_COMMON_APPDATA, "\\" PACKAGE)}, /* Common Heimdal configuration file path */
|
||||
{"LIBDIR", SPECIAL(_expand_bin_dir)},
|
||||
{"BINDIR", SPECIAL(_expand_bin_dir)},
|
||||
{"LIBEXEC", SPECIAL(_expand_bin_dir)},
|
||||
{"SBINDIR", SPECIAL(_expand_bin_dir)},
|
||||
{"LIBDIR", SPECIAL(expand_bin_dir)},
|
||||
{"BINDIR", SPECIAL(expand_bin_dir)},
|
||||
{"LIBEXEC", SPECIAL(expand_bin_dir)},
|
||||
{"SBINDIR", SPECIAL(expand_bin_dir)},
|
||||
#else
|
||||
{"LOCALSTATEDIR", FTYPE_SPECIAL, 0, LOCALSTATEDIR, _expand_path},
|
||||
{"LIBDIR", FTYPE_SPECIAL, 0, LIBDIR, _expand_path},
|
||||
{"BINDIR", FTYPE_SPECIAL, 0, BINDIR, _expand_path},
|
||||
{"LIBEXEC", FTYPE_SPECIAL, 0, LIBEXECDIR, _expand_path},
|
||||
{"SBINDIR", FTYPE_SPECIAL, 0, SBINDIR, _expand_path},
|
||||
{"euid", SPECIAL(_expand_euid)},
|
||||
{"ruid", SPECIAL(_expand_userid)},
|
||||
{"loginname", SPECIAL(_expand_loginname)},
|
||||
{"LOCALSTATEDIR", FTYPE_SPECIAL, 0, LOCALSTATEDIR, expand_path},
|
||||
{"LIBDIR", FTYPE_SPECIAL, 0, LIBDIR, expand_path},
|
||||
{"BINDIR", FTYPE_SPECIAL, 0, BINDIR, expand_path},
|
||||
{"LIBEXEC", FTYPE_SPECIAL, 0, LIBEXECDIR, expand_path},
|
||||
{"SBINDIR", FTYPE_SPECIAL, 0, SBINDIR, expand_path},
|
||||
{"euid", SPECIAL(expand_euid)},
|
||||
{"ruid", SPECIAL(expand_userid)},
|
||||
{"loginname", SPECIAL(expand_loginname)},
|
||||
#endif
|
||||
{"username", SPECIAL(_expand_username)},
|
||||
{"TEMP", SPECIAL(_expand_temp_folder)},
|
||||
{"USERID", SPECIAL(_expand_userid)},
|
||||
{"uid", SPECIAL(_expand_userid)},
|
||||
{"null", SPECIAL(_expand_null)},
|
||||
{"strftime", SPECIAL(_expand_strftime)}
|
||||
{"username", SPECIAL(expand_username)},
|
||||
{"TEMP", SPECIAL(expand_temp_folder)},
|
||||
{"USERID", SPECIAL(expand_userid)},
|
||||
{"uid", SPECIAL(expand_userid)},
|
||||
{"null", SPECIAL(expand_null)},
|
||||
{"strftime", SPECIAL(expand_strftime)}
|
||||
};
|
||||
|
||||
static krb5_error_code
|
||||
_expand_token(krb5_context context,
|
||||
const char *token,
|
||||
const char *token_end,
|
||||
char **extra_tokens,
|
||||
char **ret)
|
||||
static heim_error_code
|
||||
expand_token(heim_context context,
|
||||
const char *token,
|
||||
const char *token_end,
|
||||
char **extra_tokens,
|
||||
char **ret)
|
||||
{
|
||||
krb5_error_code errcode;
|
||||
heim_error_code errcode;
|
||||
size_t i;
|
||||
char **p;
|
||||
const char *colon;
|
||||
@@ -468,62 +469,70 @@ _expand_token(krb5_context context,
|
||||
*ret = NULL;
|
||||
|
||||
if (token[0] != '%' || token[1] != '{' || token_end[0] != '}' ||
|
||||
token_end - token <= 2) {
|
||||
if (context)
|
||||
krb5_set_error_message(context, EINVAL,"Invalid token.");
|
||||
return EINVAL;
|
||||
token_end - token <= 2) {
|
||||
if (context)
|
||||
heim_set_error_message(context, EINVAL,"Invalid token.");
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
for (p = extra_tokens; p && p[0]; p += 2) {
|
||||
if (strncmp(token+2, p[0], (token_end - token) - 2) == 0)
|
||||
return _expand_extra_token(context, p[1], ret);
|
||||
if (strncmp(token+2, p[0], (token_end - token) - 2) == 0)
|
||||
return expand_extra_token(context, p[1], ret);
|
||||
}
|
||||
|
||||
for (colon=token+2; colon < token_end; colon++)
|
||||
if (*colon == ':')
|
||||
break;
|
||||
if (*colon == ':')
|
||||
break;
|
||||
|
||||
for (i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++)
|
||||
if (!strncmp(token+2, tokens[i].tok, (colon - token) - 2)) {
|
||||
char *arg = NULL;
|
||||
if (!strncmp(token+2, tokens[i].tok, (colon - token) - 2)) {
|
||||
char *arg = NULL;
|
||||
|
||||
errcode = 0;
|
||||
if (*colon == ':') {
|
||||
asprintf(&arg, "%.*s", (int)(token_end - colon - 1), colon + 1);
|
||||
if (!arg)
|
||||
errcode = ENOMEM;
|
||||
}
|
||||
if (!errcode)
|
||||
errcode = tokens[i].exp_func(context, tokens[i].param,
|
||||
tokens[i].postfix, arg, ret);
|
||||
free(arg);
|
||||
return errcode;
|
||||
}
|
||||
errcode = 0;
|
||||
if (*colon == ':') {
|
||||
asprintf(&arg, "%.*s", (int)(token_end - colon - 1), colon + 1);
|
||||
if (!arg)
|
||||
errcode = ENOMEM;
|
||||
}
|
||||
if (!errcode)
|
||||
errcode = tokens[i].exp_func(context, tokens[i].param,
|
||||
tokens[i].postfix, arg, ret);
|
||||
free(arg);
|
||||
return errcode;
|
||||
}
|
||||
|
||||
if (context)
|
||||
krb5_set_error_message(context, EINVAL, "Invalid token.");
|
||||
heim_set_error_message(context, EINVAL, "Invalid token.");
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to expand tokens in paths.
|
||||
*
|
||||
* Inputs:
|
||||
* Params:
|
||||
*
|
||||
* @context A krb5_context
|
||||
* @context A heim_context
|
||||
* @path_in The path to expand tokens from
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* @ppath_out Path with expanded tokens (caller must free() this)
|
||||
* @ppath_out The expanded path
|
||||
* @... Variable number of pairs of strings, the first of each
|
||||
* being a token (e.g., "luser") and the second a string to
|
||||
* replace it with. The list is terminated by a NULL.
|
||||
*/
|
||||
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
|
||||
_krb5_expand_path_tokens(krb5_context context,
|
||||
const char *path_in,
|
||||
int filepath,
|
||||
char **ppath_out)
|
||||
heim_error_code
|
||||
heim_expand_path_tokens(heim_context context,
|
||||
const char *path_in,
|
||||
int filepath,
|
||||
char **ppath_out,
|
||||
...)
|
||||
{
|
||||
return _krb5_expand_path_tokensv(context, path_in, filepath, ppath_out, NULL);
|
||||
heim_error_code ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, ppath_out);
|
||||
ret = heim_expand_path_tokensv(context, path_in, filepath, ppath_out, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -532,7 +541,7 @@ free_extra_tokens(char **extra_tokens)
|
||||
char **p;
|
||||
|
||||
for (p = extra_tokens; p && *p; p++)
|
||||
free(*p);
|
||||
free(*p);
|
||||
free(extra_tokens);
|
||||
}
|
||||
|
||||
@@ -541,29 +550,29 @@ free_extra_tokens(char **extra_tokens)
|
||||
*
|
||||
* Inputs:
|
||||
*
|
||||
* @context A krb5_context
|
||||
* @context A heim_context
|
||||
* @path_in The path to expand tokens from
|
||||
* @ppath_out The expanded path
|
||||
* @... Variable number of pairs of strings, the first of each
|
||||
* being a token (e.g., "luser") and the second a string to
|
||||
* replace it with. The list is terminated by a NULL.
|
||||
* @ap A NULL-terminated va_list of pairs of strings, the first of each
|
||||
* being a token (e.g., "luser") and the second a string to replace
|
||||
* it with.
|
||||
*
|
||||
* Outputs:
|
||||
*
|
||||
* @ppath_out Path with expanded tokens (caller must free() this)
|
||||
*/
|
||||
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
|
||||
_krb5_expand_path_tokensv(krb5_context context,
|
||||
const char *path_in,
|
||||
int filepath,
|
||||
char **ppath_out, ...)
|
||||
heim_error_code
|
||||
heim_expand_path_tokensv(heim_context context,
|
||||
const char *path_in,
|
||||
int filepath,
|
||||
char **ppath_out, va_list ap)
|
||||
{
|
||||
char *tok_begin, *tok_end, *append;
|
||||
char **extra_tokens = NULL;
|
||||
const char *path_left;
|
||||
size_t nargs = 0;
|
||||
size_t len = 0;
|
||||
va_list ap;
|
||||
va_list ap2;
|
||||
|
||||
if (path_in == NULL || *path_in == '\0') {
|
||||
*ppath_out = strdup("");
|
||||
@@ -572,126 +581,128 @@ _krb5_expand_path_tokensv(krb5_context context,
|
||||
|
||||
*ppath_out = NULL;
|
||||
|
||||
va_start(ap, ppath_out);
|
||||
while (va_arg(ap, const char *)) {
|
||||
nargs++;
|
||||
va_arg(ap, const char *);
|
||||
#if defined(_MSC_VER)
|
||||
ap2 = ap; /* Come on! See SO #558223 */
|
||||
#else
|
||||
va_copy(ap2, ap);
|
||||
#endif
|
||||
while (va_arg(ap2, const char *)) {
|
||||
nargs++;
|
||||
va_arg(ap2, const char *);
|
||||
}
|
||||
va_end(ap);
|
||||
va_end(ap2);
|
||||
nargs *= 2;
|
||||
|
||||
/* Get extra tokens */
|
||||
if (nargs) {
|
||||
size_t i;
|
||||
size_t i;
|
||||
|
||||
extra_tokens = calloc(nargs + 1, sizeof (*extra_tokens));
|
||||
if (extra_tokens == NULL)
|
||||
return krb5_enomem(context);
|
||||
va_start(ap, ppath_out);
|
||||
for (i = 0; i < nargs; i++) {
|
||||
const char *s = va_arg(ap, const char *); /* token key */
|
||||
if (s == NULL)
|
||||
break;
|
||||
extra_tokens[i] = strdup(s);
|
||||
if (extra_tokens[i++] == NULL) {
|
||||
va_end(ap);
|
||||
free_extra_tokens(extra_tokens);
|
||||
return krb5_enomem(context);
|
||||
}
|
||||
s = va_arg(ap, const char *); /* token value */
|
||||
if (s == NULL)
|
||||
s = "";
|
||||
extra_tokens[i] = strdup(s);
|
||||
if (extra_tokens[i] == NULL) {
|
||||
va_end(ap);
|
||||
free_extra_tokens(extra_tokens);
|
||||
return krb5_enomem(context);
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
extra_tokens = calloc(nargs + 1, sizeof (*extra_tokens));
|
||||
if (extra_tokens == NULL)
|
||||
return heim_enomem(context);
|
||||
for (i = 0; i < nargs; i++) {
|
||||
const char *s = va_arg(ap, const char *); /* token key */
|
||||
if (s == NULL)
|
||||
break;
|
||||
extra_tokens[i] = strdup(s);
|
||||
if (extra_tokens[i++] == NULL) {
|
||||
va_end(ap);
|
||||
free_extra_tokens(extra_tokens);
|
||||
return heim_enomem(context);
|
||||
}
|
||||
s = va_arg(ap, const char *); /* token value */
|
||||
if (s == NULL)
|
||||
s = "";
|
||||
extra_tokens[i] = strdup(s);
|
||||
if (extra_tokens[i] == NULL) {
|
||||
va_end(ap);
|
||||
free_extra_tokens(extra_tokens);
|
||||
return heim_enomem(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (path_left = path_in; path_left && *path_left; ) {
|
||||
|
||||
tok_begin = strstr(path_left, "%{");
|
||||
tok_begin = strstr(path_left, "%{");
|
||||
|
||||
if (tok_begin && tok_begin != path_left) {
|
||||
if (tok_begin && tok_begin != path_left) {
|
||||
|
||||
append = malloc((tok_begin - path_left) + 1);
|
||||
if (append) {
|
||||
memcpy(append, path_left, tok_begin - path_left);
|
||||
append[tok_begin - path_left] = '\0';
|
||||
}
|
||||
path_left = tok_begin;
|
||||
append = malloc((tok_begin - path_left) + 1);
|
||||
if (append) {
|
||||
memcpy(append, path_left, tok_begin - path_left);
|
||||
append[tok_begin - path_left] = '\0';
|
||||
}
|
||||
path_left = tok_begin;
|
||||
|
||||
} else if (tok_begin) {
|
||||
} else if (tok_begin) {
|
||||
|
||||
tok_end = strchr(tok_begin, '}');
|
||||
if (tok_end == NULL) {
|
||||
free_extra_tokens(extra_tokens);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
if (context)
|
||||
krb5_set_error_message(context, EINVAL, "variable missing }");
|
||||
return EINVAL;
|
||||
}
|
||||
tok_end = strchr(tok_begin, '}');
|
||||
if (tok_end == NULL) {
|
||||
free_extra_tokens(extra_tokens);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
if (context)
|
||||
heim_set_error_message(context, EINVAL, "variable missing }");
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (_expand_token(context, tok_begin, tok_end, extra_tokens,
|
||||
&append)) {
|
||||
free_extra_tokens(extra_tokens);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
return EINVAL;
|
||||
}
|
||||
if (expand_token(context, tok_begin, tok_end, extra_tokens,
|
||||
&append)) {
|
||||
free_extra_tokens(extra_tokens);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
path_left = tok_end + 1;
|
||||
} else {
|
||||
path_left = tok_end + 1;
|
||||
} else {
|
||||
|
||||
append = strdup(path_left);
|
||||
path_left = NULL;
|
||||
append = strdup(path_left);
|
||||
path_left = NULL;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (append == NULL) {
|
||||
if (append == NULL) {
|
||||
|
||||
free_extra_tokens(extra_tokens);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
return krb5_enomem(context);
|
||||
free_extra_tokens(extra_tokens);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
return heim_enomem(context);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
size_t append_len = strlen(append);
|
||||
char * new_str = realloc(*ppath_out, len + append_len + 1);
|
||||
{
|
||||
size_t append_len = strlen(append);
|
||||
char * new_str = realloc(*ppath_out, len + append_len + 1);
|
||||
|
||||
if (new_str == NULL) {
|
||||
free_extra_tokens(extra_tokens);
|
||||
free(append);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
return krb5_enomem(context);
|
||||
}
|
||||
if (new_str == NULL) {
|
||||
free_extra_tokens(extra_tokens);
|
||||
free(append);
|
||||
if (*ppath_out)
|
||||
free(*ppath_out);
|
||||
*ppath_out = NULL;
|
||||
return heim_enomem(context);
|
||||
}
|
||||
|
||||
*ppath_out = new_str;
|
||||
memcpy(*ppath_out + len, append, append_len + 1);
|
||||
len = len + append_len;
|
||||
free(append);
|
||||
}
|
||||
*ppath_out = new_str;
|
||||
memcpy(*ppath_out + len, append, append_len + 1);
|
||||
len = len + append_len;
|
||||
free(append);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
/* Also deal with slashes */
|
||||
if (filepath && *ppath_out) {
|
||||
char * c;
|
||||
char * c;
|
||||
|
||||
for (c = *ppath_out; *c; c++)
|
||||
if (*c == '/')
|
||||
*c = '\\';
|
||||
for (c = *ppath_out; *c; c++)
|
||||
if (*c == '/')
|
||||
*c = '\\';
|
||||
}
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user