2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
26 #include "include/conf.h"
27 #include "include/defs.h"
30 #include <ldap_cdefs.h>
31 #include <ldap_features.h>
33 #define LDAP_DEPRECATED 1
37 void init_ldap_usertab() {
38 /* Setting LDAP connection and initializing cache */
40 int ldap_port
= atoi(LDAPPort
);
41 if ((ldap_handle
= (LDAP
*)ldap_init(LDAPHost
, ldap_port
)) == NULL
) {
42 sprintf(msg
,"\nUnable to connect to LDAP server:%s port:%d\n", LDAPHost
, ldap_port
);
47 int ldap_protocol_version
= atoi(LDAPProtocolVersion
);
48 if (ldap_set_option(ldap_handle
, LDAP_OPT_PROTOCOL_VERSION
, &ldap_protocol_version
) != LDAP_SUCCESS
) {
49 sprintf(msg
, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", ldap_protocol_version
);
54 /* Bind to the LDAP server. */
56 rc
= ldap_simple_bind_s( ldap_handle
, LDAPBindDN
, LDAPBindPW
);
57 if ( rc
!= LDAP_SUCCESS
) {
58 sprintf(msg
, "ldap_simple_bind_s: %s\n", ldap_err2string(rc
));
63 /* Initializing cache */
68 void user_find(char *mappedname
, char *userlogin
) {
69 if(UserTabFile
[0] != '\0') {
70 if (strcasecmp(UserTabFile
, "ldap")) {
71 sprintf(warea
,":%s:",userlogin
);
72 if((str
=(char *) strstr(userfile
,warea
)) != (char *) NULL
) {
74 str2
=(char *) strstr(str
+1,":");
77 while(str2
[z1
] != ':') {
81 } else strcpy(mappedname
,userlogin
);
84 /* Start searching username in cache */
86 char filtersearch
[256], strictchars
[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr
= strictchars
, *searched_in_cache
;
88 LDAPMessage
*result
, *e
;
93 if ((foundchr
= strchr(userlogin
, *strictptr
)))
98 if (!(searched_in_cache
= search_in_cache(userlogin
))) {
99 snprintf(filtersearch
, sizeof(filtersearch
), LDAPFilterSearch
, userlogin
, userlogin
, userlogin
, userlogin
, userlogin
);
101 /* Search record(s) in LDAP base */
103 int rc
= ldap_search_s(ldap_handle
, LDAPBaseSearch
, LDAP_SCOPE_SUBTREE
, filtersearch
, NULL
, 0, &result
);
104 if ( rc
!= LDAP_SUCCESS
) {
105 sprintf(msg
, "ldap_search_s: %s\n", ldap_err2string(rc
));
107 strcpy(mappedname
,userlogin
);
111 if (!(e
= ldap_first_entry(ldap_handle
, result
)))
112 insert_to_cache(userlogin
, userlogin
);
114 for (attr
= ldap_first_attribute(ldap_handle
, e
, &ber
); attr
!= NULL
; attr
= ldap_next_attribute(ldap_handle
, e
, ber
)) {
115 if (!strcasecmp(attr
, LDAPTargetAttr
)) {
116 if ((vals
= (char **)ldap_get_values(ldap_handle
, e
, attr
))!=NULL
) {
117 insert_to_cache(userlogin
, vals
[0]);
118 strcpy(mappedname
, vals
[0]);
126 ldap_msgfree(result
);
128 strcpy(mappedname
, searched_in_cache
);
131 strcpy(mappedname
,userlogin
);
134 void close_usertab() {
135 if (!strcasecmp(UserTabFile
, "ldap")) {
137 ldap_unbind(ldap_handle
);