📄 mod_authnz_ldap.c
字号:
return sec;}static apr_status_t authnz_ldap_cleanup_connection_close(void *param){ util_ldap_connection_t *ldc = param; util_ldap_connection_close(ldc); return APR_SUCCESS;}/* * Authentication Phase * -------------------- * * This phase authenticates the credentials the user has sent with * the request (ie the username and password are checked). This is done * by making an attempt to bind to the LDAP server using this user's * DN and the supplied password. * */static authn_status authn_ldap_check_password(request_rec *r, const char *user, const char *password){ int failures = 0; const char **vals = NULL; char filtbuf[FILTER_LENGTH]; authn_ldap_config_t *sec = (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module); util_ldap_connection_t *ldc = NULL; int result = 0; int remote_user_attribute_set = 0; const char *dn = NULL; authn_ldap_request_t *req = (authn_ldap_request_t *)apr_pcalloc(r->pool, sizeof(authn_ldap_request_t)); ap_set_module_config(r->request_config, &authnz_ldap_module, req);/* if (!sec->enabled) { return AUTH_USER_NOT_FOUND; }*/ /* * Basic sanity checks before any LDAP operations even happen. */ if (!sec->have_ldap_url) { return AUTH_GENERAL_ERROR; }start_over: /* There is a good AuthLDAPURL, right? */ if (sec->host) { ldc = util_ldap_connection_find(r, sec->host, sec->port, sec->binddn, sec->bindpw, sec->deref, sec->secure); } else { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: no sec->host - weird...?", getpid()); return AUTH_GENERAL_ERROR; } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: using URL %s", getpid(), sec->url); /* Get the password that the client sent */ if (password == NULL) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: no password specified", getpid()); util_ldap_connection_close(ldc); return AUTH_GENERAL_ERROR; } if (user == NULL) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: no user specified", getpid()); util_ldap_connection_close(ldc); return AUTH_GENERAL_ERROR; } /* build the username filter */ authn_ldap_build_filter(filtbuf, r, user, NULL, sec); /* do the user search */ result = util_ldap_cache_checkuserid(r, ldc, sec->url, sec->basedn, sec->scope, sec->attributes, filtbuf, password, &dn, &vals); util_ldap_connection_close(ldc); /* sanity check - if server is down, retry it up to 5 times */ if (AP_LDAP_IS_SERVER_DOWN(result)) { if (failures++ <= 5) { goto start_over; } } /* handle bind failure */ if (result != LDAP_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: " "user %s authentication failed; URI %s [%s][%s]", getpid(), user, r->uri, ldc->reason, ldap_err2string(result)); return (LDAP_NO_SUCH_OBJECT == result) ? AUTH_USER_NOT_FOUND#ifdef LDAP_SECURITY_ERROR : (LDAP_SECURITY_ERROR(result)) ? AUTH_DENIED#else : (LDAP_INAPPROPRIATE_AUTH == result) ? AUTH_DENIED : (LDAP_INVALID_CREDENTIALS == result) ? AUTH_DENIED#ifdef LDAP_INSUFFICIENT_ACCESS : (LDAP_INSUFFICIENT_ACCESS == result) ? AUTH_DENIED#endif#ifdef LDAP_INSUFFICIENT_RIGHTS : (LDAP_INSUFFICIENT_RIGHTS == result) ? AUTH_DENIED#endif#endif : AUTH_GENERAL_ERROR; } /* mark the user and DN */ req->dn = apr_pstrdup(r->pool, dn); req->user = apr_pstrdup(r->pool, user); if (sec->user_is_dn) { r->user = req->dn; } /* add environment variables */ if (sec->attributes && vals) { apr_table_t *e = r->subprocess_env; int i = 0; while (sec->attributes[i]) { char *str = apr_pstrcat(r->pool, AUTHN_PREFIX, sec->attributes[i], NULL); int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */ while (str[j]) { str[j] = apr_toupper(str[j]); j++; } apr_table_setn(e, str, vals[i]); /* handle remote_user_attribute, if set */ if (sec->remote_user_attribute && !strcmp(sec->remote_user_attribute, sec->attributes[i])) { r->user = (char *)apr_pstrdup(r->pool, vals[i]); remote_user_attribute_set = 1; } i++; } } /* sanity check */ if (sec->remote_user_attribute && !remote_user_attribute_set) { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: " "REMOTE_USER was to be set with attribute '%s', " "but this attribute was not requested for in the " "LDAP query for the user. REMOTE_USER will fall " "back to username or DN as appropriate.", getpid(), sec->remote_user_attribute); } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authenticate: accepting %s", getpid(), user); return AUTH_GRANTED;}/* * Authorisation Phase * ------------------- * * After checking whether the username and password are correct, we need * to check whether that user is authorised to view this resource. The * require directive is used to do this: * * require valid-user Any authenticated is allowed in. * require user <username> This particular user is allowed in. * require group <groupname> The user must be a member of this group * in order to be allowed in. * require dn <dn> The user must have the following DN in the * LDAP tree to be let in. * */static int authz_ldap_check_user_access(request_rec *r){ int result = 0; authn_ldap_request_t *req = (authn_ldap_request_t *)ap_get_module_config(r->request_config, &authnz_ldap_module); authn_ldap_config_t *sec = (authn_ldap_config_t *)ap_get_module_config(r->per_dir_config, &authnz_ldap_module); util_ldap_connection_t *ldc = NULL; int m = r->method_number; const apr_array_header_t *reqs_arr = ap_requires(r); require_line *reqs = reqs_arr ? (require_line *)reqs_arr->elts : NULL; register int x; const char *t; char *w, *value; int method_restricted = 0; int required_ldap = 0; char filtbuf[FILTER_LENGTH]; const char *dn = NULL; const char **vals = NULL;/* if (!sec->enabled) { return DECLINED; }*/ if (!sec->have_ldap_url) { return DECLINED; } if (sec->host) { ldc = util_ldap_connection_find(r, sec->host, sec->port, sec->binddn, sec->bindpw, sec->deref, sec->secure); apr_pool_cleanup_register(r->pool, ldc, authnz_ldap_cleanup_connection_close, apr_pool_cleanup_null); } else { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authorise: no sec->host - weird...?", getpid()); return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED; } /* * If there are no elements in the group attribute array, the default should be * member and uniquemember; populate the array now. */ if (sec->groupattr->nelts == 0) { struct mod_auth_ldap_groupattr_entry_t *grp;#if APR_HAS_THREADS apr_thread_mutex_lock(sec->lock);#endif grp = apr_array_push(sec->groupattr); grp->name = "member"; grp = apr_array_push(sec->groupattr); grp->name = "uniquemember";#if APR_HAS_THREADS apr_thread_mutex_unlock(sec->lock);#endif } if (!reqs_arr) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "[%" APR_PID_T_FMT "] auth_ldap authorise: no requirements array", getpid()); return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED; } /* * If we have been authenticated by some other module than mod_auth_ldap, * the req structure needed for authorization needs to be created * and populated with the userid and DN of the account in LDAP */ /* Check that we have a userid to start with */ if ((!r->user) || (strlen(r->user) == 0)) { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "ldap authorize: Userid is blank, AuthType=%s", r->ap_auth_type); } if(!req) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "ldap authorize: Creating LDAP req structure"); /* Build the username filter */ authn_ldap_build_filter(filtbuf, r, r->user, NULL, sec); /* Search for the user DN */ result = util_ldap_cache_getuserdn(r, ldc, sec->url, sec->basedn, sec->scope, sec->attributes, filtbuf, &dn, &vals); /* Search failed, log error and return failure */ if(result != LDAP_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "auth_ldap authorise: User DN not found, %s", ldc->reason); return sec->auth_authoritative? HTTP_UNAUTHORIZED : DECLINED; } req = (authn_ldap_request_t *)apr_pcalloc(r->pool, sizeof(authn_ldap_request_t)); ap_set_module_config(r->request_config, &authnz_ldap_module, req); req->dn = apr_pstrdup(r->pool, dn); req->user = r->user; } /* Loop through the requirements array until there's no elements * left, or something causes a return from inside the loop */ for(x=0; x < reqs_arr->nelts; x++) { if (! (reqs[x].method_mask & (AP_METHOD_BIT << m))) { continue; } method_restricted = 1; t = reqs[x].requirement; w = ap_getword_white(r->pool, &t); if (strcmp(w, "ldap-user") == 0) { required_ldap = 1; if (req->dn == NULL || strlen(req->dn) == 0) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -