]> git.ipfire.org Git - thirdparty/sarg.git/blob - usertab.c
30afc256e43d0ecfd6c5364d6387e9eb809bad7d
[thirdparty/sarg.git] / usertab.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2009
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
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.
14 *
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.
19 *
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.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 #include <ldap.h>
30 #include <ldap_cdefs.h>
31 #include <ldap_features.h>
32
33 #define LDAP_DEPRECATED 1
34
35 LDAP *ldap_handle;
36
37 void init_ldap_usertab() {
38 /* Setting LDAP connection and initializing cache */
39 ldap_handle = NULL;
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);
43 debuga(msg);
44 exit(1);
45 }
46
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);
50 debuga(msg);
51 exit(1);
52 }
53
54 /* Bind to the LDAP server. */
55 int rc;
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));
59 debuga(msg);
60 exit(1);
61 }
62
63 /* Initializing cache */
64
65 init_cache();
66 }
67
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 ) {
73 z1=0;
74 str2=(char *) strstr(str+1,":");
75 str2++;
76 bzero(name, MAXLEN);
77 while(str2[z1] != ':') {
78 name[z1]=str2[z1];
79 z1++;
80 }
81 } else strcpy(mappedname,userlogin);
82 } else {
83
84 /* Start searching username in cache */
85
86 char filtersearch[256], strictchars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr = strictchars, *searched_in_cache;
87 char *attr, **vals;
88 LDAPMessage *result, *e;
89 BerElement *ber;
90
91 while (*strictptr) {
92 char *foundchr;
93 if ((foundchr = strchr(userlogin, *strictptr)))
94 *foundchr = '\0';
95 strictptr++;
96 }
97
98 if (!(searched_in_cache = search_in_cache(userlogin))) {
99 snprintf(filtersearch, sizeof(filtersearch), LDAPFilterSearch, userlogin, userlogin, userlogin, userlogin, userlogin);
100
101 /* Search record(s) in LDAP base */
102
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));
106 debuga(msg);
107 strcpy(mappedname,userlogin);
108 return;
109 }
110
111 if (!(e = ldap_first_entry(ldap_handle, result)))
112 insert_to_cache(userlogin, userlogin);
113 else
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]);
119 ldap_memfree(vals);
120 }
121 ldap_memfree(attr);
122 break;
123 }
124 ldap_memfree(attr);
125 }
126 ldap_msgfree(result);
127 } else
128 strcpy(mappedname, searched_in_cache);
129 }
130 } else
131 strcpy(mappedname,userlogin);
132 }
133
134 void close_usertab() {
135 if (!strcasecmp(UserTabFile, "ldap")) {
136 destroy_cache();
137 ldap_unbind(ldap_handle);
138 } else {
139 if(userfile)
140 free(userfile);
141 }
142 }
143