| 1 |
From: Brian May <[email protected]> |
| 2 |
Date: Thu, 8 Aug 2024 10:43:18 +1000 |
| 3 |
Subject: add-include-support-1 |
| 4 |
|
| 5 |
commit fe43be85587f834266623adb0ecf2793d212a7ca |
| 6 |
Author: Nicolas Williams <[email protected]> |
| 7 |
Date: Thu Feb 16 11:49:05 2017 -0600 |
| 8 |
|
| 9 |
Add include/includedir directives for krb5.conf |
| 10 |
|
| 11 |
Origin: upstream, https://github.com/heimdal/heimdal/commit/fe43be85587f834266623adb0ecf2793d212a7ca |
| 12 |
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1074775 |
| 13 |
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/heimdal/+bug/2037321 |
| 14 |
Last-Update: 2024-07-01 |
| 15 |
--- |
| 16 |
doc/setup.texi | 7 ++++ |
| 17 |
lib/krb5/config_file.c | 100 ++++++++++++++++++++++++++++++++++++++++++-- |
| 18 |
lib/krb5/krb5.conf.5 | 13 ++++++ |
| 19 |
lib/krb5/krb5_locl.h | 1 + |
| 20 |
tests/gss/include-krb5.conf | 17 ++++++++ |
| 21 |
tests/gss/krb5.conf.in | 17 +------- |
| 22 |
6 files changed, 136 insertions(+), 19 deletions(-) |
| 23 |
create mode 100644 tests/gss/include-krb5.conf |
| 24 |
|
| 25 |
diff --git a/doc/setup.texi b/doc/setup.texi |
| 26 |
index 1d620ce..f02dd2e 100644 |
| 27 |
--- a/doc/setup.texi |
| 28 |
+++ b/doc/setup.texi |
| 29 |
@@ -52,7 +52,14 @@ separated from the equal sign with some whitespace). Subsections have a |
| 30 |
other bindings are treated as variable assignments. The value of a |
| 31 |
variable extends to the end of the line. |
| 32 |
|
| 33 |
+Configuration files can also include other files, or all files in a |
| 34 |
+directory. Use absolute paths in include directives. When including a |
| 35 |
+directoty, only files whose names consist of alphanumeric, hyphen, or |
| 36 |
+underscore characters are allowed, though they may end in '.conf'. |
| 37 |
+ |
| 38 |
@example |
| 39 |
+include /some/config/file |
| 40 |
+includedir /some/config/directory |
| 41 |
[section1] |
| 42 |
a-subsection = @{ |
| 43 |
var = value1 |
| 44 |
diff --git a/lib/krb5/config_file.c b/lib/krb5/config_file.c |
| 45 |
index 6293bd7..3c4b0b6 100644 |
| 46 |
--- a/lib/krb5/config_file.c |
| 47 |
+++ b/lib/krb5/config_file.c |
| 48 |
@@ -41,6 +41,7 @@ |
| 49 |
|
| 50 |
/* Gaah! I want a portable funopen */ |
| 51 |
struct fileptr { |
| 52 |
+ krb5_context context; |
| 53 |
const char *s; |
| 54 |
FILE *f; |
| 55 |
}; |
| 56 |
@@ -366,7 +367,7 @@ krb5_config_parse_debug (struct fileptr *f, |
| 57 |
++p; |
| 58 |
if (*p == '#' || *p == ';') |
| 59 |
continue; |
| 60 |
- if (*p == '[') { |
| 61 |
+ if (*p == '[') { |
| 62 |
ret = parse_section(p, &s, res, err_message); |
| 63 |
if (ret) |
| 64 |
return ret; |
| 65 |
@@ -374,6 +375,22 @@ krb5_config_parse_debug (struct fileptr *f, |
| 66 |
} else if (*p == '}') { |
| 67 |
*err_message = "unmatched }"; |
| 68 |
return KRB5_CONFIG_BADFORMAT; |
| 69 |
+ } else if (strncmp(p, "include", sizeof("include") - 1) == 0 && |
| 70 |
+ isspace(p[sizeof("include") - 1])) { |
| 71 |
+ p += sizeof("include"); |
| 72 |
+ while (isspace(*p)) |
| 73 |
+ p++; |
| 74 |
+ ret = krb5_config_parse_file_multi(f->context, p, res); |
| 75 |
+ if (ret) |
| 76 |
+ return ret; |
| 77 |
+ } else if (strncmp(p, "includedir", sizeof("includedir") - 1) == 0 && |
| 78 |
+ isspace(p[sizeof("includedir") - 1])) { |
| 79 |
+ p += sizeof("includedir"); |
| 80 |
+ while (isspace(*p)) |
| 81 |
+ p++; |
| 82 |
+ ret = krb5_config_parse_dir_multi(f->context, p, res); |
| 83 |
+ if (ret) |
| 84 |
+ return ret; |
| 85 |
} else if(*p != '\0') { |
| 86 |
if (s == NULL) { |
| 87 |
*err_message = "binding before section"; |
| 88 |
@@ -399,6 +416,64 @@ is_plist_file(const char *fname) |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
+/** |
| 93 |
+ * Parse configuration files in the given directory and add the result |
| 94 |
+ * into res. Only files whose names consist only of alphanumeric |
| 95 |
+ * characters, hyphen, and underscore, will be parsed, though files |
| 96 |
+ * ending in ".conf" will also be parsed. |
| 97 |
+ * |
| 98 |
+ * This interface can be used to parse several configuration directories |
| 99 |
+ * into one resulting krb5_config_section by calling it repeatably. |
| 100 |
+ * |
| 101 |
+ * @param context a Kerberos 5 context. |
| 102 |
+ * @param dname a directory name to a Kerberos configuration file |
| 103 |
+ * @param res the returned result, must be free with krb5_free_config_files(). |
| 104 |
+ * @return Return an error code or 0, see krb5_get_error_message(). |
| 105 |
+ * |
| 106 |
+ * @ingroup krb5_support |
| 107 |
+ */ |
| 108 |
+ |
| 109 |
+KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 110 |
+krb5_config_parse_dir_multi(krb5_context context, |
| 111 |
+ const char *dname, |
| 112 |
+ krb5_config_section **res) |
| 113 |
+{ |
| 114 |
+ struct dirent *entry; |
| 115 |
+ krb5_error_code ret; |
| 116 |
+ DIR *d; |
| 117 |
+ |
| 118 |
+ if ((d = opendir(dname)) == NULL) |
| 119 |
+ return errno; |
| 120 |
+ |
| 121 |
+ while ((entry = readdir(d)) != NULL) { |
| 122 |
+ char *p = entry->d_name; |
| 123 |
+ char *path; |
| 124 |
+ int is_valid = 1; |
| 125 |
+ |
| 126 |
+ while (*p) { |
| 127 |
+ if (!isalpha(*p) && *p != '_' && *p != '-' && |
| 128 |
+ strcmp(p, ".conf") != 0) { |
| 129 |
+ is_valid = 0; |
| 130 |
+ break; |
| 131 |
+ } |
| 132 |
+ p++; |
| 133 |
+ } |
| 134 |
+ if (!is_valid) |
| 135 |
+ continue; |
| 136 |
+ |
| 137 |
+ if (asprintf(&path, "%s/%s", dname, entry->d_name) == -1 || |
| 138 |
+ path == NULL) |
| 139 |
+ return krb5_enomem(context); |
| 140 |
+ ret = krb5_config_parse_file_multi(context, path, res); |
| 141 |
+ free(path); |
| 142 |
+ if (ret == ENOMEM) |
| 143 |
+ return krb5_enomem(context);; |
| 144 |
+ /* Ignore malformed config files */ |
| 145 |
+ } |
| 146 |
+ (void) closedir(d); |
| 147 |
+ return 0; |
| 148 |
+} |
| 149 |
+ |
| 150 |
/** |
| 151 |
* Parse a configuration file and add the result into res. This |
| 152 |
* interface can be used to parse several configuration files into one |
| 153 |
@@ -423,6 +498,13 @@ krb5_config_parse_file_multi (krb5_context context, |
| 154 |
krb5_error_code ret; |
| 155 |
struct fileptr f; |
| 156 |
|
| 157 |
+ if (context->config_include_depth > 5) { |
| 158 |
+ krb5_warnx(context, "Maximum config file include depth reached; " |
| 159 |
+ "not including %s", fname); |
| 160 |
+ return 0; |
| 161 |
+ } |
| 162 |
+ context->config_include_depth++; |
| 163 |
+ |
| 164 |
/** |
| 165 |
* If the fname starts with "~/" parse configuration file in the |
| 166 |
* current users home directory. The behavior can be disabled and |
| 167 |
@@ -433,6 +515,7 @@ krb5_config_parse_file_multi (krb5_context context, |
| 168 |
const char *home = NULL; |
| 169 |
|
| 170 |
if (!_krb5_homedir_access(context)) { |
| 171 |
+ context->config_include_depth--; |
| 172 |
krb5_set_error_message(context, EPERM, |
| 173 |
"Access to home directory not allowed"); |
| 174 |
return EPERM; |
| 175 |
@@ -450,14 +533,18 @@ krb5_config_parse_file_multi (krb5_context context, |
| 176 |
int aret; |
| 177 |
|
| 178 |
aret = asprintf(&newfname, "%s%s", home, &fname[1]); |
| 179 |
- if (aret == -1 || newfname == NULL) |
| 180 |
+ if (aret == -1 || newfname == NULL) { |
| 181 |
+ context->config_include_depth--; |
| 182 |
return krb5_enomem(context); |
| 183 |
+ } |
| 184 |
fname = newfname; |
| 185 |
} |
| 186 |
#else /* KRB5_USE_PATH_TOKENS */ |
| 187 |
if (asprintf(&newfname, "%%{USERCONFIG}%s", &fname[1]) < 0 || |
| 188 |
- newfname == NULL) |
| 189 |
+ newfname == NULL) { |
| 190 |
+ context->config_include_depth--; |
| 191 |
return krb5_enomem(context); |
| 192 |
+ } |
| 193 |
fname = newfname; |
| 194 |
#endif |
| 195 |
} |
| 196 |
@@ -465,6 +552,7 @@ krb5_config_parse_file_multi (krb5_context context, |
| 197 |
if (is_plist_file(fname)) { |
| 198 |
#ifdef __APPLE__ |
| 199 |
ret = parse_plist_config(context, fname, res); |
| 200 |
+ context->config_include_depth--; |
| 201 |
if (ret) { |
| 202 |
krb5_set_error_message(context, ret, |
| 203 |
"Failed to parse plist %s", fname); |
| 204 |
@@ -483,6 +571,7 @@ krb5_config_parse_file_multi (krb5_context context, |
| 205 |
|
| 206 |
ret = _krb5_expand_path_tokens(context, fname, 1, &exp_fname); |
| 207 |
if (ret) { |
| 208 |
+ context->config_include_depth--; |
| 209 |
if (newfname) |
| 210 |
free(newfname); |
| 211 |
return ret; |
| 212 |
@@ -493,9 +582,11 @@ krb5_config_parse_file_multi (krb5_context context, |
| 213 |
fname = newfname = exp_fname; |
| 214 |
#endif |
| 215 |
|
| 216 |
+ f.context = context; |
| 217 |
f.f = fopen(fname, "r"); |
| 218 |
f.s = NULL; |
| 219 |
if(f.f == NULL) { |
| 220 |
+ context->config_include_depth--; |
| 221 |
ret = errno; |
| 222 |
krb5_set_error_message (context, ret, "open %s: %s", |
| 223 |
fname, strerror(ret)); |
| 224 |
@@ -505,6 +596,7 @@ krb5_config_parse_file_multi (krb5_context context, |
| 225 |
} |
| 226 |
|
| 227 |
ret = krb5_config_parse_debug (&f, res, &lineno, &str); |
| 228 |
+ context->config_include_depth--; |
| 229 |
fclose(f.f); |
| 230 |
if (ret) { |
| 231 |
krb5_set_error_message (context, ret, "%s:%u: %s", |
| 232 |
@@ -1312,6 +1404,8 @@ krb5_config_parse_string_multi(krb5_context context, |
| 233 |
unsigned lineno = 0; |
| 234 |
krb5_error_code ret; |
| 235 |
struct fileptr f; |
| 236 |
+ |
| 237 |
+ f.context = context; |
| 238 |
f.f = NULL; |
| 239 |
f.s = string; |
| 240 |
|
| 241 |
diff --git a/lib/krb5/krb5.conf.5 b/lib/krb5/krb5.conf.5 |
| 242 |
index 8a0f084..63ee8cf 100644 |
| 243 |
--- a/lib/krb5/krb5.conf.5 |
| 244 |
+++ b/lib/krb5/krb5.conf.5 |
| 245 |
@@ -54,6 +54,7 @@ The grammar looks like: |
| 246 |
file: |
| 247 |
/* empty */ |
| 248 |
sections |
| 249 |
+ includes |
| 250 |
|
| 251 |
sections: |
| 252 |
section sections |
| 253 |
@@ -76,10 +77,22 @@ binding: |
| 254 |
name: |
| 255 |
STRING |
| 256 |
|
| 257 |
+includes: |
| 258 |
+ 'include' path |
| 259 |
+ 'includedir' path |
| 260 |
+ |
| 261 |
+path: STRING |
| 262 |
+ |
| 263 |
.Ed |
| 264 |
.Li STRINGs |
| 265 |
consists of one or more non-whitespace characters. |
| 266 |
.Pp |
| 267 |
+Files and directories may be included by absolute path. Including a |
| 268 |
+directory causes all files in the directory to be included as if each |
| 269 |
+file had been included separately, but only files whose names consist of |
| 270 |
+alphanumeric, hyphen, and underscore are included, though they may also |
| 271 |
+end in '.conf'. |
| 272 |
+.Pp |
| 273 |
STRINGs that are specified later in this man-page uses the following |
| 274 |
notation. |
| 275 |
.Bl -tag -width "xxx" -offset indent |
| 276 |
diff --git a/lib/krb5/krb5_locl.h b/lib/krb5/krb5_locl.h |
| 277 |
index b64f3a9..3a388bd 100644 |
| 278 |
--- a/lib/krb5/krb5_locl.h |
| 279 |
+++ b/lib/krb5/krb5_locl.h |
| 280 |
@@ -233,6 +233,7 @@ typedef struct krb5_context_data { |
| 281 |
int32_t kdc_sec_offset; |
| 282 |
int32_t kdc_usec_offset; |
| 283 |
krb5_config_section *cf; |
| 284 |
+ size_t config_include_depth; |
| 285 |
struct et_list *et_list; |
| 286 |
struct krb5_log_facility *warn_dest; |
| 287 |
struct krb5_log_facility *debug_dest; |
| 288 |
diff --git a/tests/gss/include-krb5.conf b/tests/gss/include-krb5.conf |
| 289 |
new file mode 100644 |
| 290 |
index 0000000..ae21e9e |
| 291 |
--- /dev/null |
| 292 |
+++ b/tests/gss/include-krb5.conf |
| 293 |
@@ -0,0 +1,17 @@ |
| 294 |
+[libdefaults] |
| 295 |
+ default_realm = TEST.H5L.SE |
| 296 |
+ no-addresses = TRUE |
| 297 |
+ dns_canonicalize_hostname = false |
| 298 |
+ dns_lookup_realm = false |
| 299 |
+ name_canon_rules = as-is:realm=TEST.H5L.SE |
| 300 |
+ name_canon_rules = qualify:domain=test.h5l.se |
| 301 |
+ |
| 302 |
+[domain_realms] |
| 303 |
+ .test.h5l.se = TEST.H5L.SE |
| 304 |
+ |
| 305 |
+[kdc] |
| 306 |
+ enable-digest = true |
| 307 |
+ digests_allowed = ntlm-v2,ntlm-v1-session,ntlm-v1 |
| 308 |
+ |
| 309 |
+[kadmin] |
| 310 |
+ save-password = true |
| 311 |
diff --git a/tests/gss/krb5.conf.in b/tests/gss/krb5.conf.in |
| 312 |
index d372c9a..d6b9a06 100644 |
| 313 |
--- a/tests/gss/krb5.conf.in |
| 314 |
+++ b/tests/gss/krb5.conf.in |
| 315 |
@@ -1,26 +1,14 @@ |
| 316 |
-# $Id$ |
| 317 |
+include @srcdir@/include-krb5.conf |
| 318 |
|
| 319 |
[libdefaults] |
| 320 |
- default_realm = TEST.H5L.SE |
| 321 |
- no-addresses = TRUE |
| 322 |
default_keytab_name = @objdir@/server.keytab |
| 323 |
- dns_canonicalize_hostname = false |
| 324 |
- dns_lookup_realm = false |
| 325 |
- name_canon_rules = as-is:realm=TEST.H5L.SE |
| 326 |
- name_canon_rules = qualify:domain=test.h5l.se |
| 327 |
|
| 328 |
[realms] |
| 329 |
TEST.H5L.SE = { |
| 330 |
kdc = localhost:@port@ |
| 331 |
} |
| 332 |
|
| 333 |
-[domain_realms] |
| 334 |
- .test.h5l.se = TEST.H5L.SE |
| 335 |
- |
| 336 |
[kdc] |
| 337 |
- enable-digest = true |
| 338 |
- digests_allowed = ntlm-v2,ntlm-v1-session,ntlm-v1 |
| 339 |
- |
| 340 |
database = { |
| 341 |
dbname = @objdir@/current-db |
| 342 |
realm = TEST.H5L.SE |
| 343 |
@@ -34,6 +22,3 @@ |
| 344 |
[logging] |
| 345 |
kdc = 0-/FILE:@objdir@/messages.log |
| 346 |
default = 0-/FILE:@objdir@/messages.log |
| 347 |
- |
| 348 |
-[kadmin] |
| 349 |
- save-password = true |