The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
user.c
Go to the documentation of this file.
1 /*
2  * This program is is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or (at
5  * your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: bbdd989b0f96f0da8f23e9179f16ebd92f85e796 $
19  * @file groups.c
20  * @brief LDAP module group functions.
21  *
22  * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23  *
24  * @copyright 2013 Network RADIUS SAS (legal@networkradius.com)
25  * @copyright 2013-2015 The FreeRADIUS Server Project.
26  */
27 RCSID("$Id: bbdd989b0f96f0da8f23e9179f16ebd92f85e796 $")
28 
30 
31 #include <freeradius-devel/util/debug.h>
32 
33 #define LOG_PREFIX mctx->mi->name
34 
35 #include "rlm_ldap.h"
36 
37 /** Holds state of user searches in progress
38  *
39  */
40 typedef struct {
41  rlm_ldap_t const *inst;
43  char const *base_dn;
44  char const *filter;
45  char const * const *attrs;
49 
50 /** Process the results of an async user lookup
51  *
52  */
53 static unlang_action_t ldap_find_user_async_result(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
54 {
55  ldap_user_find_ctx_t *user_ctx = talloc_get_type_abort(uctx, ldap_user_find_ctx_t);
56  fr_ldap_query_t *query = user_ctx->query;
57  LDAPMessage *entry;
58  int cnt, ldap_errno;
59  char *dn;
60  fr_pair_t *vp;
61 
62  switch (query->ret) {
64  break;
65 
67  /*
68  * DNs can now be dynamic, so a BAD DN often means the same thing as an empty result
69  */
70  case LDAP_RESULT_BAD_DN:
72 
73  default:
75  }
76 
77  cnt = ldap_count_entries(query->ldap_conn->handle, query->result);
78 
79  if ((!user_ctx->inst->user.obj_sort_ctrl) && (cnt > 1)) {
80  REDEBUG("Ambiguous search result, returned %i unsorted entries (should return 1 or 0). "
81  "Enable sorting, or specify a more restrictive base_dn, filter or scope", cnt);
82  REDEBUG("The following entries were returned:");
83  RINDENT();
84  for (entry = ldap_first_entry(query->ldap_conn->handle, query->result);
85  entry;
86  entry = ldap_next_entry(query->ldap_conn->handle, entry)) {
87  dn = ldap_get_dn(query->ldap_conn->handle, entry);
88  REDEBUG("%s", dn);
89  ldap_memfree(dn);
90  }
91  REXDENT();
93  }
94 
95  entry = ldap_first_entry(query->ldap_conn->handle, query->result);
96  if (!entry) {
97  ldap_get_option(query->ldap_conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
98  REDEBUG("Failed retrieving entry: %s",
99  ldap_err2string(ldap_errno));
100 
102  }
103 
104  dn = ldap_get_dn(query->ldap_conn->handle, entry);
105  if (!dn) {
106  ldap_get_option(query->ldap_conn->handle, LDAP_OPT_RESULT_CODE, &ldap_errno);
107  REDEBUG("Retrieving object DN from entry failed: %s", ldap_err2string(ldap_errno));
108 
110  }
112 
113  RDEBUG2("User object found at DN \"%s\"", dn);
114 
116  fr_pair_value_strdup(vp, dn, false);
117  ldap_memfree(dn);
118  if (user_ctx->out) *user_ctx->out = user_ctx->query;
119 
121 }
122 
123 /** Cancel a user search
124  */
126 {
127  ldap_user_find_ctx_t *user_ctx = talloc_get_type_abort(uctx, ldap_user_find_ctx_t);
128 
129  /*
130  * If the query is not in flight, just return
131  */
132  if (!user_ctx->query || !user_ctx->query->treq) return;
133 
135 }
136 
137 /** Initiate asynchronous retrieval of the DN of a user object
138  *
139  * Retrieves the DN of a user and adds it to the control list as LDAP-UserDN.
140  * Will also retrieve any attributes passed.
141  *
142  * This potentially allows for all authorization and authentication checks to be performed in one
143  * ldap search operation, which is a big bonus given the number of crappy, slow *cough*AD*cough*
144  * LDAP directory servers out there.
145  *
146  * @param[in] ctx in which to allocate the query.
147  * @param[in] inst rlm_ldap configuration.
148  * @param[in] request Current request.
149  * @param[in] base DN to search in.
150  * @param[in] filter to use in LDAP search.
151  * @param[in] ttrunk LDAP thread trunk to use.
152  * @param[in] attrs Additional attributes to retrieve, may be NULL.
153  * @param[in] query_out Where to put a pointer to the LDAP query structure -
154  * for extracting extra returned attributes, may be NULL.
155  * @return
156  * - UNLANG_ACTION_PUSHED_CHILD on success.
157  * - UNLANG_ACTION_FAIL on failure.
158  */
160  fr_value_box_t *base, fr_value_box_t *filter,
161  fr_ldap_thread_trunk_t *ttrunk, char const *attrs[], fr_ldap_query_t **query_out)
162 {
163  static char const *tmp_attrs[] = { NULL };
164  ldap_user_find_ctx_t *user_ctx;
165  LDAPControl *serverctrls[] = { inst->user.obj_sort_ctrl, NULL };
166 
167  if (!attrs) memset(&attrs, 0, sizeof(tmp_attrs));
168 
169  user_ctx = talloc_zero(ctx, ldap_user_find_ctx_t);
170  *user_ctx = (ldap_user_find_ctx_t) {
171  .inst = inst,
172  .ttrunk = ttrunk,
173  .base_dn = base->vb_strvalue,
174  .attrs = attrs,
175  .out = query_out
176  };
177 
178  if (filter) user_ctx->filter = filter->vb_strvalue;
180  ~FR_SIGNAL_CANCEL, UNLANG_SUB_FRAME, user_ctx) < 0) {
181  talloc_free(user_ctx);
182  return UNLANG_ACTION_FAIL;
183  }
184 
185  return fr_ldap_trunk_search(user_ctx, &user_ctx->query, request, user_ctx->ttrunk,
186  user_ctx->base_dn, user_ctx->inst->user.obj_scope, user_ctx->filter,
187  user_ctx->attrs, serverctrls, NULL);
188 }
189 
190 /** Check for presence of access attribute in result
191  *
192  * @param[in] inst rlm_ldap configuration.
193  * @param[in] request Current request.
194  * @param[in] entry retrieved by rlm_ldap_find_user or fr_ldap_search.
195  * @return
196  * - #RLM_MODULE_DISALLOW if the user was denied access.
197  * - #RLM_MODULE_OK otherwise.
198  */
200 {
202  struct berval **values = NULL;
203 
204  values = ldap_get_values_len(fr_ldap_handle_thread_local(), entry, inst->user.obj_access_attr);
205  if (values) {
206  size_t negate_value_len = talloc_array_length(inst->user.access_value_negate) - 1;
207  if (inst->user.access_positive) {
208  if ((values[0]->bv_len >= negate_value_len) &&
209  (strncasecmp(values[0]->bv_val, inst->user.access_value_negate, negate_value_len) == 0)) {
210  REDEBUG("\"%s\" attribute exists but is set to '%s' - user locked out",
211  inst->user.obj_access_attr, inst->user.access_value_negate);
213  goto done;
214  }
215  /* RLM_MODULE_OK set above... */
216  } else if ((values[0]->bv_len < negate_value_len) ||
217  (strncasecmp(values[0]->bv_val, inst->user.access_value_negate, negate_value_len) != 0)) {
218  REDEBUG("\"%s\" attribute exists - user locked out", inst->user.obj_access_attr);
220  goto done;
221  }
222  {
223  size_t suspend_value_len = talloc_array_length(inst->user.access_value_suspend) - 1;
224  if ((values[0]->bv_len == suspend_value_len) &&
225  (strncasecmp(values[0]->bv_val, inst->user.access_value_suspend, suspend_value_len) == 0)) {
226  RIDEBUG("\"%s\" attribute exists and indicates suspension", inst->user.obj_access_attr);
227  ret = LDAP_ACCESS_SUSPENDED;
228  goto done;
229  }
230  }
231  done:
232  ldap_value_free_len(values);
233  } else if (inst->user.access_positive) {
234  REDEBUG("No \"%s\" attribute - user locked out", inst->user.obj_access_attr);
236  }
237 
238  return ret;
239 }
240 
241 /** Verify we got a password from the search
242  *
243  * Checks to see if after the LDAP to RADIUS mapping has been completed that a reference password.
244  *
245  * @param[in] request Current request.
246  * @param[in] inst Current LDAP instance.
247  * @param[in] inst_name Name of LDAP module instance for debug messages.
248  * @param[in] expect_password Whether we should be expecting a password.
249  * @param[in] ttrunk the connection thread trunk.
250  */
251 void rlm_ldap_check_reply(request_t *request, rlm_ldap_t const *inst, char const *inst_name, bool expect_password, fr_ldap_thread_trunk_t const *ttrunk)
252 {
253  fr_pair_t *parent;
254 
255  /*
256  * More warning messages for people who can't be bothered to read the documentation.
257  *
258  * Expect_password is set when we process the mapping, and is only true if there was a mapping between
259  * an LDAP attribute and a password reference attribute in the control list.
260  */
261  if ((inst->user.expect_password_is_set && !inst->user.expect_password) || !expect_password || !RDEBUG_ENABLED2) return;
262 
263  parent = fr_pair_find_by_da(&request->control_pairs, NULL, attr_password);
264  if (!parent) goto warnings;
265 
266  if (!fr_pair_find_by_da(&parent->vp_group, NULL, attr_cleartext_password) &&
267  !fr_pair_find_by_da(&parent->vp_group, NULL, attr_nt_password) &&
269  !fr_pair_find_by_da(&parent->vp_group, NULL, attr_crypt_password)) {
270  warnings:
271  switch (ttrunk->directory->type) {
274  RWDEBUG2("!!! Found map between LDAP attribute and a FreeRADIUS password attribute");
275  RWDEBUG2("!!! %s does not allow passwords to be read via LDAP",
276  (ttrunk->directory->type == FR_LDAP_DIRECTORY_SAMBA ? "Samba" : "Active Directory"));
277  RWDEBUG2("!!! Remove the password map and either:");
278  RWDEBUG2("!!! - Configure authentication via ntlm_auth (mschapv2 only)");
279  RWDEBUG2("!!! - Configure authentication via wbclient (mschapv2 only)");
280  RWDEBUG2("!!! - Bind as the user by listing %s in an 'authenticate %s' section, and",
281  inst_name, inst_name);
282  RWDEBUG2("!!! setting attribute &control.Auth-Type := '%s' in the 'recv Access-Request' section",
283  inst_name);
284  RWDEBUG2("!!! (pap only)");
285 
286  break;
287 
289  RWDEBUG2("!!! Found map between LDAP attribute and a FreeRADIUS password attribute");
290  RWDEBUG2("!!! eDirectory does not allow passwords to be retrieved via LDAP search");
291  RWDEBUG2("!!! Remove the password map and either:");
292  RWDEBUG2("!!! - Set 'edir = yes' and enable the universal password feature on your");
293  RWDEBUG2("!!! eDir server (recommended)");
294  RWDEBUG2("!!! - Bind as the user by listing %s in an 'authenticate %s' section, and",
295  inst_name, inst_name);
296  RWDEBUG2("!!! setting attribute &control.Auth-Type := '%s' in the 'recv Access-Request' section",
297  inst_name);
298  RWDEBUG("!!! (pap only)");
299  break;
300 
301  default:
302  RWDEBUG2("!!! Found map between LDAP attribute and a FreeRADIUS password attribute");
303  RWDEBUG2("!!! but no password attribute found in search result");
304  RWDEBUG2("!!! Either:");
305  RWDEBUG2("!!! - Ensure the user object contains a password attribute, and that");
306  RWDEBUG2("!!! %c%s%c has permission to read that password attribute (recommended)",
307  ttrunk->config.admin_identity ? '"' : '\0',
308  ttrunk->config.admin_identity ? ttrunk->config.admin_identity : "the bind user",
309  ttrunk->config.admin_identity ? '"' : '\0');
310  RWDEBUG2("!!! - Bind as the user by listing %s in an 'authenticate %s' section, and",
311  inst_name, inst_name);
312  RWDEBUG2("!!! setting attribute &control.Auth-Type := '%s' in the 'recv Access-Request' section",
313  inst_name);
314  RWDEBUG2("!!! (pap only)");
315  break;
316  }
317  }
318 }
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition: action.h:36
#define USES_APPLE_DEPRECATED_API
Definition: build.h:468
#define RCSID(id)
Definition: build.h:481
#define UNUSED
Definition: build.h:313
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
Definition: function.h:111
#define UNLANG_SUB_FRAME
Definition: interpret.h:36
size_t fr_ldap_util_normalise_dn(char *out, char const *in)
Normalise escape sequences in a DN.
Definition: util.c:439
@ FR_LDAP_DIRECTORY_EDIRECTORY
Directory server is eDir.
Definition: base.h:144
@ FR_LDAP_DIRECTORY_ACTIVE_DIRECTORY
Directory server is Active Directory.
Definition: base.h:143
@ FR_LDAP_DIRECTORY_SAMBA
Directory server is Samba.
Definition: base.h:154
LDAP * handle
libldap handle.
Definition: base.h:333
char const * admin_identity
Identity we bind as when we need to query the LDAP directory.
Definition: base.h:229
fr_ldap_result_code_t ret
Result code.
Definition: base.h:470
trunk_request_t * treq
Trunk request this query is associated with.
Definition: base.h:456
fr_ldap_config_t config
Config used for this connection.
Definition: base.h:403
fr_ldap_connection_t * ldap_conn
LDAP connection this query is running on.
Definition: base.h:457
@ LDAP_RESULT_SUCCESS
Successfully got LDAP results.
Definition: base.h:190
@ LDAP_RESULT_NO_RESULT
No results returned.
Definition: base.h:194
@ LDAP_RESULT_BAD_DN
The requested DN does not exist.
Definition: base.h:193
LDAPMessage * result
Head of LDAP results list.
Definition: base.h:468
fr_ldap_directory_t * directory
The type of directory we're connected to.
Definition: base.h:404
fr_ldap_directory_type_t type
Canonical server implementation.
Definition: base.h:206
LDAP query structure.
Definition: base.h:422
Thread LDAP trunk structure.
Definition: base.h:399
LDAP * fr_ldap_handle_thread_local(void)
Get a thread local dummy LDAP handle.
Definition: base.c:1105
unlang_action_t fr_ldap_trunk_search(TALLOC_CTX *ctx, fr_ldap_query_t **out, request_t *request, fr_ldap_thread_trunk_t *ttrunk, char const *base_dn, int scope, char const *filter, char const *const *attrs, LDAPControl **serverctrls, LDAPControl **clientctrls)
Run an async search LDAP query on a trunk connection.
Definition: base.c:708
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition: log.h:443
#define RWDEBUG(fmt,...)
Definition: log.h:361
#define RWDEBUG2(fmt,...)
Definition: log.h:362
#define RINDENT()
Indent R* messages by one level.
Definition: log.h:430
talloc_free(reap)
int strncasecmp(char *s1, char *s2, int n)
Definition: missing.c:36
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition: pair.c:693
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition: pair.c:2634
static fr_dict_attr_t const * attr_cleartext_password
Definition: radclient-ng.c:116
static bool done
Definition: radclient.c:80
#define REDEBUG(fmt,...)
Definition: radclient.h:52
#define RDEBUG_ENABLED2()
Definition: radclient.h:50
#define RDEBUG2(fmt,...)
Definition: radclient.h:54
#define RIDEBUG(fmt,...)
Definition: radsniff.h:65
#define RETURN_MODULE_INVALID
Definition: rcode.h:59
#define RETURN_MODULE_OK
Definition: rcode.h:57
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
#define RETURN_MODULE_NOTFOUND
Definition: rcode.h:61
fr_dict_attr_t const * attr_nt_password
Definition: rlm_ldap.c:320
fr_dict_attr_t const * attr_ldap_userdn
Definition: rlm_ldap.c:319
fr_dict_attr_t const * attr_crypt_password
Definition: rlm_ldap.c:318
fr_dict_attr_t const * attr_password
Definition: rlm_ldap.c:316
fr_dict_attr_t const * attr_password_with_header
Definition: rlm_ldap.c:321
LDAP authorization and authentication module headers.
struct rlm_ldap_t::@160 user
ldap_access_state_t
User's access state.
Definition: rlm_ldap.h:184
@ LDAP_ACCESS_SUSPENDED
User account has been suspended.
Definition: rlm_ldap.h:187
@ LDAP_ACCESS_ALLOWED
User is allowed to login.
Definition: rlm_ldap.h:185
@ LDAP_ACCESS_DISALLOWED
User it not allow to login (disabled)
Definition: rlm_ldap.h:186
#define pair_update_control(_attr, _da)
Return or allocate a fr_pair_t in the control list.
Definition: pair.h:140
fr_signal_t
Definition: signal.h:48
RETURN_MODULE_FAIL
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
void trunk_request_signal_cancel(trunk_request_t *treq)
Cancel a trunk request.
Definition: trunk.c:2140
fr_ldap_thread_trunk_t * ttrunk
Definition: user.c:42
fr_ldap_query_t ** out
Definition: user.c:47
unlang_action_t rlm_ldap_find_user_async(TALLOC_CTX *ctx, rlm_ldap_t const *inst, request_t *request, fr_value_box_t *base, fr_value_box_t *filter, fr_ldap_thread_trunk_t *ttrunk, char const *attrs[], fr_ldap_query_t **query_out)
Initiate asynchronous retrieval of the DN of a user object.
Definition: user.c:159
static void ldap_find_user_async_cancel(UNUSED request_t *request, UNUSED fr_signal_t action, void *uctx)
Cancel a user search.
Definition: user.c:125
char const * filter
Definition: user.c:44
char const *const * attrs
Definition: user.c:45
void rlm_ldap_check_reply(request_t *request, rlm_ldap_t const *inst, char const *inst_name, bool expect_password, fr_ldap_thread_trunk_t const *ttrunk)
Verify we got a password from the search.
Definition: user.c:251
fr_ldap_query_t * query
Definition: user.c:46
char const * base_dn
Definition: user.c:43
rlm_ldap_t const * inst
Definition: user.c:41
static unlang_action_t ldap_find_user_async_result(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Process the results of an async user lookup.
Definition: user.c:53
ldap_access_state_t rlm_ldap_check_access(rlm_ldap_t const *inst, request_t *request, LDAPMessage *entry)
Check for presence of access attribute in result.
Definition: user.c:199
Holds state of user searches in progress.
Definition: user.c:40
static fr_slen_t parent
Definition: pair.h:851