26RCSID(
"$Id: 698a9a2bd3b0d7d3cce558f03309d4579c5c6362 $")
30#include <freeradius-devel/ldap/base.h>
31#include <freeradius-devel/util/base16.h>
33#include <freeradius-devel/util/value.h>
38static const char hextab[] =
"0123456789abcdef";
108 return outlen - left;
123 if (!fr_sbuff_init_talloc(vb, &sbuff, &sbuff_ctx, vb->vb_length * 3, vb->vb_length * 3)) {
133 if (len == vb->vb_length) {
162 size_t freespace = outlen;
164 if (outlen <= 1)
return 0;
167 while (*p && (--freespace > 0)) {
183 if (!(c1 = memchr(
hextab, tolower(p[0]), 16)) ||
184 !(c2 = memchr(
hextab, tolower(p[1]), 16)))
goto next;
193 return outlen - freespace;
210 bool too_soon =
true;
222 if (
inlen < 2)
return false;
245 if (
inlen < 3)
return false;
264 if (too_soon || (*p != want))
return false;
270 if (too_soon || (*p != want))
return false;
288 if (too_soon || (
comp < 2))
return false;
305 LDAPControl **sss_p = sss, **sss_end = sss_p + sss_len;
316 for (i = 0; extensions[i]; i++) {
318 bool is_critical =
false;
320 if (sss_p == sss_end) {
337 "in the format \"[!]sss=<key>[,key]\"");
341 if (*s) ldap_control_free(*s);
348 if (ret != LDAP_SUCCESS) {
354 if (*sss_p) ldap_control_free(*sss_p);
357 ldap_free_sort_keylist(keys);
358 if (ret != LDAP_SUCCESS) {
360 ldap_err2string(ret));
371 struct berval attr_value;
377 "in the format \"[!]vlv=<before>/<after>(/<offset>/<count>|:<value>)");
381 vlvinfo.ldvlv_context = NULL;
383 if (
fr_sbuff_out(NULL, &ext_value, &sbuff) <= 0)
goto vlv_error;
385 vlvinfo.ldvlv_before_count = ext_value;
387 if (
fr_sbuff_out(NULL, &ext_value, &sbuff) <= 0)
goto vlv_error;
388 vlvinfo.ldvlv_after_count = ext_value;
393 vlvinfo.ldvlv_attrvalue = NULL;
395 if (
fr_sbuff_out(NULL, &ext_value, &sbuff) <= 0)
goto vlv_error;
397 vlvinfo.ldvlv_offset = ext_value;
399 if (
fr_sbuff_out(NULL, &ext_value, &sbuff) <= 0)
goto vlv_error;
400 vlvinfo.ldvlv_count = ext_value;
406 vlvinfo.ldvlv_attrvalue = &attr_value;
412 if (ret != LDAP_SUCCESS) {
414 ldap_err2string(ret));
427 return (sss_end - sss_p);
446 out = talloc_array(ctx,
char,
in->bv_len + 1);
447 if (!
out)
return NULL;
449 memcpy(
out,
in->bv_val,
in->bv_len);
450 out[
in->bv_len] =
'\0';
466 if (!
out)
return NULL;
468 memcpy(
out,
in->bv_val,
in->bv_len);
496 for (p =
in; *p !=
'\0'; p++) {
542 size_t f_len, p_len, i;
544 if (!full)
return -1;
546 f_len = strlen(full);
548 if (!part)
return -1;
550 p_len = strlen(part);
551 if (!p_len)
return f_len;
553 if ((f_len < p_len) || !f_len)
return -1;
555 for (i = 0; i < p_len; i++)
if (part[p_len - i] != full[f_len - i])
return -1;
557 return f_len - p_len;
572 char const *
in = NULL;
583 for (i = 0; i < sublen; i++) {
584 if (sub[i] && *sub[i]) {
587 len += strlen(sub[i]);
597 buffer = talloc_array(ctx,
char, len + 4);
600 for (i = 0; i < sublen; i++) {
601 if (sub[i] && (*sub[i] !=
'\0')) {
635 if (!attr)
return -1;
637 len = talloc_array_length(attrs);
639 for (i = 0; i < len; i++) {
640 if (!attrs[i])
continue;
641 if (
strcasecmp(attrs[i], attr) == 0)
return 1;
659 LDAPURLDesc *ldap_url;
660 bool set_port_maybe =
true;
661 int default_port = LDAP_PORT;
665 if (ldap_url_parse(server, &ldap_url)) {
666 cf_log_err(ci,
"Parsing LDAP URL \"%s\" failed", server);
668 ldap_free_urldesc(ldap_url);
672 if (ldap_url->lud_dn && (ldap_url->lud_dn[0] !=
'\0')) {
673 cf_log_err(ci,
"Base DN cannot be specified via server URL");
677 if (ldap_url->lud_attrs && ldap_url->lud_attrs[0]) {
678 cf_log_err(ci,
"Attribute list cannot be speciried via server URL");
685 if (ldap_url->lud_scope != LDAP_SCOPE_BASE) {
686 cf_log_err(ci,
"Scope cannot be specified via server URL");
689 ldap_url->lud_scope = -1;
696 if ((p = strchr(server,
']')) && (p[1] ==
':')) {
697 set_port_maybe =
false;
698 }
else if ((p = strchr(server,
':')) && (strchr(p+1,
':') != NULL)) {
699 set_port_maybe =
false;
705 if (ldap_url->lud_scheme) {
706 if (strcmp(ldap_url->lud_scheme,
"ldaps") == 0) {
708 cf_log_err(ci,
"ldaps:// scheme is not compatible with 'start_tls'");
711 default_port = LDAPS_PORT;
712 handle_config->
tls_mode = LDAP_OPT_X_TLS_HARD;
713 }
else if (strcmp(ldap_url->lud_scheme,
"ldapi") == 0) {
714 set_port_maybe =
false;
718 if (set_port_maybe) {
722 ldap_url->lud_port = handle_config->
port;
729 if (!ldap_url->lud_port) ldap_url->lud_port = default_port;
732 url = ldap_url_desc2str(ldap_url);
734 cf_log_err(ci,
"Failed recombining URL components");
737 handle_config->
server = talloc_asprintf_append(handle_config->
server,
"%s ",
url);
739 ldap_free_urldesc(ldap_url);
760 port = handle_config->
port;
766 if (strchr(server,
'/')) {
770 cf_log_err(ci,
"Invalid 'server' entry, must be in format <server>[:<port>] or "
771 "an ldap URI (ldap|cldap|ldaps|ldapi)://<server>:<port>");
775 p = strrchr(server,
':');
777 port = (int)strtol((p + 1), &q, 10);
778 if ((p == server) || ((p + 1) == q) || (*q !=
'\0'))
goto bad_server_fmt;
781 len = strlen(server);
783 if (port == 0) port = LDAP_PORT;
785 handle_config->
server = talloc_asprintf_append(handle_config->
server,
"ldap://%.*s:%i ",
786 (
int)len, server, port);
796 switch (ldap_url_err) {
797 case LDAP_URL_SUCCESS:
800 case LDAP_URL_ERR_MEM:
803 case LDAP_URL_ERR_PARAM:
804 return "parameter is bad";
806 case LDAP_URL_ERR_BADSCHEME:
807 return "URL doesn't begin with \"[c]ldap[si]://\"";
809 case LDAP_URL_ERR_BADENCLOSURE:
810 return "URL is missing trailing \">\"";
812 case LDAP_URL_ERR_BADURL:
815 case LDAP_URL_ERR_BADHOST:
816 return "host/port is bad";
818 case LDAP_URL_ERR_BADATTRS:
819 return "bad (or missing) attributes";
821 case LDAP_URL_ERR_BADSCOPE:
822 return "scope string is invalid (or missing)";
824 case LDAP_URL_ERR_BADFILTER:
825 return "bad or missing filter";
827 case LDAP_URL_ERR_BADEXTS:
828 return "bad or missing extensions";
831 return "unknown reason";
844 BerElement *ber = NULL;
846 struct berval **vals;
851 msgtype = ldap_msgtype(entry);
853 case LDAP_RES_SEARCH_ENTRY:
854 dn = ldap_get_dn(ld, entry);
860 for (attr = ldap_first_attribute(ld, entry, &ber);
862 attr = ldap_next_attribute(ld, entry, ber)) {
863 vals = ldap_get_values_len(ld, entry, attr);
865 DEBUG(
"%s: no values", attr);
870 for (i = 0; vals[i] != NULL; i++) {
874 for (j = 0; j < vals[i]->bv_len; j++) {
875 char c = vals[i]->bv_val[j];
876 if ((c < 32) || (c > 126)) {
890 ldap_value_free_len(vals);
895 case LDAP_RES_SEARCH_RESULT:
897 case LDAP_RES_MODIFY:
899 case LDAP_RES_DELETE:
900 case LDAP_RES_COMPARE:
901 case LDAP_RES_EXTENDED:
904 char *matched = NULL;
908 rc = ldap_parse_result(ld, entry, &msgtype, &matched, &errmsg, &refs, NULL, 0);
909 if (rc != LDAP_SUCCESS) {
910 DEBUG(
"failed to parse result: %s", ldap_err2string(rc));
914 DEBUG(
"result code: %d (%s)", msgtype, ldap_err2string(msgtype));
916 if (matched && *matched) {
917 DEBUG(
"matched DN: %s", matched);
919 if (errmsg && *errmsg) {
920 DEBUG(
"error message: %s", errmsg);
923 for (i = 0; refs[i] != NULL; i++) {
924 DEBUG(
"referral: %s", refs[i]);
928 if (matched) ldap_memfree(matched);
929 if (errmsg) ldap_memfree(errmsg);
930 if (refs) ldap_memvfree((
void **)refs);
935 DEBUG(
"unhandled LDAP message type: %d", msgtype);
939 if (ber) ber_free(ber, 0);
static int const char char buffer[256]
strcpy(log_entry->msg, buffer)
#define fr_base16_decode(_err, _out, _in, _no_trailing)
#define USES_APPLE_DEPRECATED_API
Common header for all CONF_* types.
A section grouping multiple CONF_PAIR.
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
#define cf_log_err(_cf, _fmt,...)
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
char * server
Initial server to bind to.
bool start_tls
Send the Start TLS message to the LDAP directory to start encrypted communications using the standard...
uint16_t port
Port to use when binding to the server.
Connection configuration.
LDAP * fr_ldap_handle_thread_local(void)
Get a thread local dummy LDAP handle.
char const * fr_ldap_url_err_to_str(int ldap_url_err)
Translate the error code emitted from ldap_url_parse and friends into something accessible with fr_st...
static const char hextab[]
size_t fr_ldap_util_normalise_dn(char *out, char const *in)
Normalise escape sequences in a DN.
static const bool escapes[SBUFF_CHAR_CLASS]
size_t fr_ldap_uri_escape_func(UNUSED request_t *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
Converts "bad" strings into ones which are safe for LDAP.
bool fr_ldap_util_is_dn(char const *in, size_t inlen)
Check whether a string looks like a DN.
size_t fr_ldap_common_dn(char const *full, char const *part)
Find the place at which the two DN strings diverge.
int fr_ldap_attrs_check(char const **attrs, char const *attr)
Check that a particular attribute is included in an attribute list.
static USES_APPLE_DEPRECATED_API const char specials[]
uint8_t * fr_ldap_berval_to_bin(TALLOC_CTX *ctx, struct berval const *in)
Convert a berval to a talloced buffer.
int fr_ldap_server_url_check(fr_ldap_config_t *handle_config, char const *server, CONF_SECTION const *cs)
Check an LDAP server entry in URL format is valid.
char * fr_ldap_berval_to_string(TALLOC_CTX *ctx, struct berval const *in)
Convert a berval to a talloced string.
int fr_ldap_box_escape(fr_value_box_t *vb, UNUSED void *uctx)
int fr_ldap_server_config_check(fr_ldap_config_t *handle_config, char const *server, CONF_SECTION *cs)
Check an LDAP server config in server:port format is valid.
size_t fr_ldap_uri_unescape_func(UNUSED request_t *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
Converts escaped DNs and filter strings into normal.
void fr_ldap_entry_dump(LDAPMessage *entry)
Dump out the contents of an LDAPMessage.
int fr_ldap_parse_url_extensions(LDAPControl **sss, size_t sss_len, char *extensions[])
Parse a subset (just server side sort and virtual list view for now) of LDAP URL extensions.
int fr_ldap_filter_to_tmpl(TALLOC_CTX *ctx, tmpl_rules_t const *t_rules, char const **sub, size_t sublen, tmpl_t **out)
Combine filters and tokenize to a tmpl.
#define EMARKER(_str, _marker_idx, _marker)
@ FR_TYPE_STRING
String of printable characters.
int strcasecmp(char *s1, char *s2)
static int encode(bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t id)
static char const * url[FR_RADIUS_FAIL_MAX+1]
static int8_t comp(void const *a, void const *b)
int fr_sbuff_trim_talloc(fr_sbuff_t *sbuff, size_t len)
Trim a talloced sbuff to the minimum length required to represent the contained string.
size_t fr_sbuff_adv_past_str(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
Return true and advance past the end of the needle if needle occurs next in the sbuff.
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_buff(_sbuff_or_marker)
#define FR_SBUFF_IN_STR(_start)
#define fr_sbuff_out(_err, _out, _in)
#define fr_sbuff_remaining(_sbuff_or_marker)
Talloc sbuff extension structure.
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
Optional arguments passed to vp_tmpl functions.
char const * fr_strerror(void)
Get the last library error.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
#define fr_strerror_const(_msg)
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
void fr_value_box_strdup_shallow_replace(fr_value_box_t *vb, char const *src, ssize_t len)
Free the existing buffer (if talloced) associated with the valuebox, and replace it with a new one.
#define fr_box_strvalue_len(_val, _len)
#define fr_value_box_is_safe_for(_box, _safe_for)
static size_t char fr_sbuff_t size_t inlen
static size_t char ** out
#define fr_box_octets(_val, _len)