Ticket #1066: ldap_leak_bugfix.patch

File ldap_leak_bugfix.patch, 6.2 kB (added by yann@…, 18 months ago)

Proposed patch to solve this bug

  • src/http_auth.c

    diff -ur lighttpd-1.4.13/src/http_auth.c lighttpd-1.4.13.new/src/http_auth.c
    old new  
    736736 
    737737 
    738738                /* build filter */ 
    739                 buffer_copy_string_buffer(p->ldap_filter, p->conf.ldap_filter_pre); 
     739                buffer_copy_string_buffer(p->ldap_filter, p->conf.ldap->ldap_filter_pre); 
    740740                buffer_append_string_buffer(p->ldap_filter, username); 
    741                 buffer_append_string_buffer(p->ldap_filter, p->conf.ldap_filter_post); 
     741                buffer_append_string_buffer(p->ldap_filter, p->conf.ldap->ldap_filter_post); 
    742742 
    743743 
    744744                /* 2. */ 
    745                 if (p->conf.ldap == NULL || 
    746                     LDAP_SUCCESS != (ret = ldap_search_s(p->conf.ldap, p->conf.auth_ldap_basedn->ptr, LDAP_SCOPE_SUBTREE, p->ldap_filter->ptr, attrs, 0, &lm))) { 
     745                if (p->conf.ldap->ldap == NULL || 
     746                    LDAP_SUCCESS != (ret = ldap_search_s(p->conf.ldap->ldap, p->conf.auth_ldap_basedn->ptr, LDAP_SCOPE_SUBTREE, p->ldap_filter->ptr, attrs, 0, &lm))) { 
    747747                        if (auth_ldap_init(srv, &p->conf) != HANDLER_GO_ON) 
    748748                                return -1; 
    749                         if (LDAP_SUCCESS != (ret = ldap_search_s(p->conf.ldap, p->conf.auth_ldap_basedn->ptr, LDAP_SCOPE_SUBTREE, p->ldap_filter->ptr, attrs, 0, &lm))) { 
     749                        if (LDAP_SUCCESS != (ret = ldap_search_s(p->conf.ldap->ldap, p->conf.auth_ldap_basedn->ptr, LDAP_SCOPE_SUBTREE, p->ldap_filter->ptr, attrs, 0, &lm))) { 
    750750 
    751751                        log_error_write(srv, __FILE__, __LINE__, "sssb", 
    752752                                        "ldap:", ldap_err2string(ret), "filter:", p->ldap_filter); 
     
    755755                        } 
    756756                } 
    757757 
    758                 if (NULL == (first = ldap_first_entry(p->conf.ldap, lm))) { 
     758                if (NULL == (first = ldap_first_entry(p->conf.ldap->ldap, lm))) { 
    759759                        log_error_write(srv, __FILE__, __LINE__, "s", "ldap ..."); 
    760760 
    761761                        ldap_msgfree(lm); 
     
    763763                        return -1; 
    764764                } 
    765765 
    766                 if (NULL == (dn = ldap_get_dn(p->conf.ldap, first))) { 
     766                if (NULL == (dn = ldap_get_dn(p->conf.ldap->ldap, first))) { 
    767767                        log_error_write(srv, __FILE__, __LINE__, "s", "ldap ..."); 
    768768 
    769769                        ldap_msgfree(lm); 
  • src/http_auth.h

    diff -ur lighttpd-1.4.13/src/http_auth.h lighttpd-1.4.13.new/src/http_auth.h
    old new  
    1717        AUTH_BACKEND_HTDIGEST 
    1818} auth_backend_t; 
    1919 
     20#ifdef USE_LDAP 
     21typedef struct { 
     22        LDAP *ldap; 
     23 
     24        buffer *ldap_filter_pre; 
     25        buffer *ldap_filter_post; 
     26} ldap_plugin_config; 
     27#endif 
     28 
    2029typedef struct { 
    2130        /* auth */ 
    2231        array  *auth_require; 
     
    4352        auth_backend_t auth_backend; 
    4453 
    4554#ifdef USE_LDAP 
    46         LDAP *ldap; 
    47  
    48         buffer *ldap_filter_pre; 
    49         buffer *ldap_filter_post; 
     55        ldap_plugin_config *ldap; 
    5056#endif 
    5157} mod_auth_plugin_config; 
    5258 
     59 
     60 
    5361typedef struct { 
    5462        PLUGIN_DATA; 
    5563        buffer *tmp_buf; 
  • src/mod_auth.c

    diff -ur lighttpd-1.4.13/src/mod_auth.c lighttpd-1.4.13.new/src/mod_auth.c
    old new  
    7777                        buffer_free(s->auth_ldap_cafile); 
    7878 
    7979#ifdef USE_LDAP 
    80                         buffer_free(s->ldap_filter_pre); 
    81                         buffer_free(s->ldap_filter_post); 
     80                        buffer_free(s->ldap->ldap_filter_pre); 
     81                        buffer_free(s->ldap->ldap_filter_post); 
    8282 
    83                         if (s->ldap) ldap_unbind_s(s->ldap); 
     83                        if (s->ldap->ldap) ldap_unbind_s(s->ldap->ldap); 
     84                        free (s->ldap); 
    8485#endif 
    8586 
    8687                        free(s); 
     
    115116        PATCH(auth_ldap_starttls); 
    116117#ifdef USE_LDAP 
    117118        PATCH(ldap); 
    118         PATCH(ldap_filter_pre); 
    119         PATCH(ldap_filter_post); 
    120119#endif 
    121120 
    122121        /* skip the first, the global context */ 
     
    149148                                PATCH(auth_ldap_hostname); 
    150149#ifdef USE_LDAP 
    151150                                PATCH(ldap); 
    152                                 PATCH(ldap_filter_pre); 
    153                                 PATCH(ldap_filter_post); 
    154151#endif 
    155152                        } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.base-dn"))) { 
    156153                                PATCH(auth_ldap_basedn); 
     
    345342                s->auth_require = array_init(); 
    346343 
    347344#ifdef USE_LDAP 
    348                 s->ldap_filter_pre = buffer_init(); 
    349                 s->ldap_filter_post = buffer_init(); 
    350                 s->ldap = NULL; 
     345                s->ldap = malloc (sizeof(ldap_plugin_config)); 
     346                s->ldap->ldap_filter_pre = buffer_init(); 
     347                s->ldap->ldap_filter_post = buffer_init(); 
     348                s->ldap->ldap = NULL; 
    351349#endif 
    352350 
    353351                cv[0].destination = s->auth_backend_conf; 
     
    573571                                        return HANDLER_ERROR; 
    574572                                } 
    575573 
    576                                 buffer_copy_string_len(s->ldap_filter_pre, s->auth_ldap_filter->ptr, dollar - s->auth_ldap_filter->ptr); 
    577                                 buffer_copy_string(s->ldap_filter_post, dollar+1); 
     574                                buffer_copy_string_len(s->ldap->ldap_filter_pre, s->auth_ldap_filter->ptr, dollar - s->auth_ldap_filter->ptr); 
     575                                buffer_copy_string(s->ldap->ldap_filter_post, dollar+1); 
    578576                        } 
    579577 
    580578                        if (s->auth_ldap_hostname->used) { 
    581                                 if (NULL == (s->ldap = ldap_init(s->auth_ldap_hostname->ptr, LDAP_PORT))) { 
     579                                if (NULL == (s->ldap->ldap = ldap_init(s->auth_ldap_hostname->ptr, LDAP_PORT))) { 
    582580                                        log_error_write(srv, __FILE__, __LINE__, "ss", "ldap ...", strerror(errno)); 
    583581 
    584582                                        return HANDLER_ERROR; 
    585583                                } 
    586584 
    587585                                ret = LDAP_VERSION3; 
    588                                 if (LDAP_OPT_SUCCESS != (ret = ldap_set_option(s->ldap, LDAP_OPT_PROTOCOL_VERSION, &ret))) { 
     586                                if (LDAP_OPT_SUCCESS != (ret = ldap_set_option(s->ldap->ldap, LDAP_OPT_PROTOCOL_VERSION, &ret))) { 
    589587                                        log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret)); 
    590588 
    591589                                        return HANDLER_ERROR; 
     
    604602                                                } 
    605603                                        } 
    606604 
    607                                         if (LDAP_OPT_SUCCESS != (ret = ldap_start_tls_s(s->ldap, NULL,  NULL))) { 
     605                                        if (LDAP_OPT_SUCCESS != (ret = ldap_start_tls_s(s->ldap->ldap, NULL,  NULL))) { 
    608606                                                log_error_write(srv, __FILE__, __LINE__, "ss", "ldap startTLS failed:", ldap_err2string(ret)); 
    609607 
    610608                                                return HANDLER_ERROR; 
     
    614612 
    615613                                /* 1. */ 
    616614                                if (s->auth_ldap_binddn->used) { 
    617                                         if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(s->ldap, s->auth_ldap_binddn->ptr, s->auth_ldap_bindpw->ptr))) { 
     615                                        if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(s->ldap->ldap, s->auth_ldap_binddn->ptr, s->auth_ldap_bindpw->ptr))) { 
    618616                                                log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret)); 
    619617 
    620618                                                return HANDLER_ERROR; 
    621619                                        } 
    622620                                } else { 
    623                                         if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(s->ldap, NULL, NULL))) { 
     621                                        if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(s->ldap->ldap, NULL, NULL))) { 
    624622                                                log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret)); 
    625623 
    626624                                                return HANDLER_ERROR;