diff -upr mod_auth_ldap.orig/mod_auth_ldap.c mod_auth_ldap/mod_auth_ldap.c --- mod_auth_ldap.orig/mod_auth_ldap.c 2003-06-08 07:10:33.000000000 +0200 +++ mod_auth_ldap/mod_auth_ldap.c 2004-10-13 03:17:52.000000000 +0200 @@ -150,7 +150,8 @@ typedef struct _ldap_auth_config_rec char *ldap_server, *base_dn, - *uid_attr; + *uid_attr, + *uid_filter; char *user_dn; @@ -187,6 +188,7 @@ static void *create_ldap_auth_dir_config cr->bind_dn=NULL; cr->bind_pass=NULL; cr->uid_attr=ap_pstrdup(p,"uid"); + cr->uid_filter=ap_pstrdup(p,"(uid=%u)"); cr->ldap_port=LDAP_PORT; cr->auth_ldapauthoritative=1; /* fortress is secure by default */ @@ -239,6 +241,13 @@ static const char *set_uid_attr(cmd_parm return (NULL); } +static const char *set_uid_filter(cmd_parms *cmd,ldap_auth_config_rec *cr, + char *arg) +{ + cr->uid_filter=ap_pstrdup(cmd->pool,arg); + return (NULL); +} + static const char *set_ldapauthoritative(cmd_parms *cmd,ldap_auth_config_rec *cr,char *arg) { @@ -292,6 +301,7 @@ static const command_rec ldap_auth_cmds[ {"LDAP_Port", set_ldap_port, NULL,OR_AUTHCFG,TAKE1,"LDAP port"}, {"Base_DN", set_base_dn, NULL,OR_AUTHCFG,TAKE1,"Base DN"}, {"UID_Attr", set_uid_attr, NULL,OR_AUTHCFG,TAKE1,"uid"}, + {"UID_Filter", set_uid_filter, NULL,OR_AUTHCFG,TAKE1,"Search Filter"}, {"Bind_DN", set_bind_dn, NULL,OR_AUTHCFG,TAKE1,"Bind DN"}, {"Bind_Pass", set_bind_pass, NULL,OR_AUTHCFG,TAKE1,"Bind Password"}, {"AuthLDAPAuthoritative",set_ldapauthoritative,NULL,OR_AUTHCFG,TAKE1, @@ -300,13 +310,52 @@ static const command_rec ldap_auth_cmds[ }; /* +** buildLdapFilter() +** return 0 on error, nonzero on success +** +** Parameters: +** char *szfilter A pointer to a buffer for storing the filter +** size_t *len The size of szfilter +** char *uid_filter LDAP filter to use with userid, e.g. "(|(uid=%u)(mail=%u))" +** char *userid the userid to replace %u with +*/ +static int buildLdapFilter( char* szfilter, size_t len, + char* uid_filter, char* userid ) +{ + char* p1; + char* p2; + size_t s = 0; + + szfilter[0] = 0; + p1 = uid_filter; + while( (p2=strstr(p1,"%u")) ) { + size_t d = p2-p1; + s += d; + s += strlen(userid); + if( s > len-1 ) { + /* about to overflow, just be safe and abort */ + return 0; + } + strncat( szfilter, p1, d ); + strcat( szfilter, userid ); + p1 = p2+2; + } + if( s+strlen(p1) > len-1 ) { + /* about to overflow, just be safe and abort */ + return 0; + } + strcat( szfilter, p1 ); + return 1; +} + +/* ** ldapFindUserDN() ** return the DN of the user ** ** Parameters: ** LDAP *ld valid handle to LDAP ** char *base_dn LDAP base Distinguised Name -** char *uid_attrib LDAP uid attribute, e.g. "uid" +** char *uid_filter LDAP filter to use with userid, e.g. "(|(uid=%u)(mail=%u))" ** char *userid the userid to check ** request_rect *r needed for writing log in Win2K ** char *bind_dn Bind_DN, can be NULL @@ -327,7 +376,7 @@ static const command_rec ldap_auth_cmds[ ** mhttpd ** muquit@muquit.com Mar-15-2001 bind with bind and pass if provided. */ -static char *ldapFindUserDN(LDAP *ld,char *base_dn,char *uid_attrib, +static char *ldapFindUserDN(LDAP *ld,char *base_dn,char *uid_filter, char *userid,request_rec *r,char *bind_dn,char *bind_pass) { int @@ -349,8 +398,12 @@ static char *ldapFindUserDN(LDAP *ld,cha entry=(LDAPMessage *) NULL; dn=(char *) NULL; - /* prepare filter with UidAttr. */ - ap_snprintf(szfilter,sizeof(szfilter)-1,"(%s=%s)",uid_attrib,userid); + /* prepare filter with UidFilter. */ + if( !buildLdapFilter( szfilter, sizeof(szfilter), uid_filter, userid ) ) { + ap_log_rerror(APLOG_MARK,APLOG_NOERRNO | APLOG_ERR,r, + "[mod_auth_ldap.c] - Error: LDAP filter \"%s\" too long", uid_filter); + return(NULL); + } #ifdef DEBUG_LDAP ap_log_rerror(APLOG_MARK,APLOG_NOERRNO | APLOG_ERR,r, @@ -423,6 +476,8 @@ static char *ldapFindUserDN(LDAP *ld,cha /* note: ldap_err2string() returns pointer to a static space */ ap_log_rerror(APLOG_MARK,APLOG_NOERRNO | APLOG_ERR,r, "[mod_auth_ldap.c] - Error: %s",ldap_err2string(rc)); + ap_log_rerror(APLOG_MARK,APLOG_NOERRNO | APLOG_ERR,r, + "[mod_auth_ldap.c] - Error: Filter was \"%s\"",szfilter); return (NULL); } @@ -457,7 +512,7 @@ static char *ldapFindUserDN(LDAP *ld,cha #ifdef DEBUG_LDAP ap_log_rerror(APLOG_MARK,APLOG_NOERRNO | APLOG_ERR,r, - "[mod_auth_ldap.c] - %s=%s Unknown in LDAP server",uid_attrib,userid); + "[mod_auth_ldap.c] - %s Unknown in LDAP server",szfilter); #endif /* DEBUG_LDAP */ if (result != (LDAPMessage *) NULL) @@ -689,13 +744,13 @@ static int ldap_authenticate_basic_user( /* now get the User DN */ - dn=ldapFindUserDN(cr->ld,cr->base_dn,cr->uid_attr,c->user,r, + dn=ldapFindUserDN(cr->ld,cr->base_dn,cr->uid_filter,c->user,r, cr->bind_dn,cr->bind_pass); if (dn == (char *) NULL) { #ifdef DEBUG_LDAP ap_log_rerror(APLOG_MARK,APLOG_NOERRNO | APLOG_ERR,r, - "[mod_auth_ldap.c] - ldapFindUserDN() didn't return any DN for user \"%s\" with attr \"%s\"",c->user,cr->uid_attr); + "[mod_auth_ldap.c] - ldapFindUserDN() didn't return any DN for user \"%s\" with filter \"%s\"",c->user,cr->uid_filter); #endif /* DEBUG_LDAP */ /* pass control to lower modules if AuthLDAPAuthoritative=no */ Kun i mod_auth_ldap: mod_auth_ldap.c~