]> git.ipfire.org Git - thirdparty/sarg.git/blob - usertab.c
Disable __attribute__ for non gnu compiler
[thirdparty/sarg.git] / usertab.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 #ifdef HAVE_LDAP_H
31 #define LDAP_DEPRECATED 1
32
33 #include <ldap.h>
34 #include <ldap_cdefs.h>
35 #include <ldap_features.h>
36 #endif //HAVE_LDAP_H
37
38 enum UserTabEnum
39 {
40 //! Users matched against the ::UserTabFile file.
41 UTT_File,
42 //! Users matched agains a LDAP.
43 UTT_Ldap,
44 //! No user matching performed.
45 UTT_None
46 };
47
48 enum UserTabEnum which_usertab=UTT_None;
49
50 static char *userfile=NULL;
51
52 #ifdef HAVE_LDAP_H
53 static LDAP *ldap_handle=NULL;
54 #endif //HAVE_LDAP_H
55
56 static void init_file_usertab(const char *UserTabFile)
57 {
58 FILE *fp_usr;
59 long int nreg;
60 char buf[MAXLEN];
61 int z2;
62 int z1;
63
64 if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
65 debuga(_("(usertab) Cannot open file %s - %s\n"),UserTabFile,strerror(errno));
66 exit(EXIT_FAILURE);
67 }
68 fseek(fp_usr, 0, SEEK_END);
69 nreg = ftell(fp_usr);
70 if (nreg<0) {
71 debuga(_("Cannot get the size of file %s"),UserTabFile);
72 exit(EXIT_FAILURE);
73 }
74 nreg += 100;
75 fseek(fp_usr, 0, SEEK_SET);
76 if((userfile=(char *) malloc(nreg))==NULL){
77 debuga(_("ERROR: Cannot load. Memory fault"));
78 exit(EXIT_FAILURE);
79 }
80 userfile[0]='\t';
81 z2=1;
82 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
83 if (buf[0]=='#') continue;
84 fixendofline(buf);
85 z1=0;
86 while(buf[z1] && (unsigned char)buf[z1]>' ') {
87 if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0"
88 debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile);
89 exit(EXIT_FAILURE);
90 }
91 userfile[z2++]=buf[z1++];
92 }
93 while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++;
94 userfile[z2++]='\n';
95 while(buf[z1] && (unsigned char)buf[z1]>' ') {
96 if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0"
97 debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile);
98 exit(EXIT_FAILURE);
99 }
100 userfile[z2++]=buf[z1++];
101 }
102 userfile[z2++]='\t';
103 }
104 userfile[z2]='\0';
105 fclose(fp_usr);
106 }
107
108 static void get_usertab_name(const char *user,char *name,int namelen)
109 {
110 char warea[MAXLEN];
111 char *str;
112
113 namelen--;
114 sprintf(warea,"\t%s\n",user);
115 if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) {
116 strncpy(name,user,namelen);
117 name[namelen]=0;
118 } else {
119 str=strchr(str+1,'\n');
120 str++;
121 for(z1=0; *str != '\t' && z1<namelen ; z1++) {
122 name[z1]=*str++;
123 }
124 name[z1]=0;
125 }
126 }
127
128 #ifdef HAVE_LDAP_H
129 static void init_ldap_usertab(void) {
130 /* Setting LDAP connection and initializing cache */
131 ldap_handle = NULL;
132 if ((ldap_handle = ldap_init(LDAPHost, LDAPPort)) == NULL) {
133 debuga(_("Unable to connect to LDAP server %s on port %d\n"), LDAPHost, LDAPPort);
134 exit(EXIT_FAILURE);
135 }
136
137 int ldap_protocol_version = LDAPProtocolVersion;
138 if (ldap_set_option(ldap_handle, LDAP_OPT_PROTOCOL_VERSION, &ldap_protocol_version) != LDAP_SUCCESS) {
139 debuga(_("Could not set LDAP protocol version %d\n"), ldap_protocol_version);
140 exit(EXIT_FAILURE);
141 }
142
143 /* Bind to the LDAP server. */
144 int rc;
145 rc = ldap_simple_bind_s( ldap_handle, LDAPBindDN, LDAPBindPW );
146 if ( rc != LDAP_SUCCESS ) {
147 debuga(_("Cannot bind to LDAP server: %s\n"), ldap_err2string(rc));
148 exit(EXIT_FAILURE);
149 }
150
151 /* Initializing cache */
152
153 init_cache();
154 }
155
156 static void get_ldap_name(const char *userlogin,char *mappedname,int namelen)
157 {
158 /* Start searching username in cache */
159
160 char filtersearch[256], strictchars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr = strictchars, *searched_in_cache;
161 char *attr, **vals;
162 LDAPMessage *result, *e;
163 BerElement *ber;
164
165 while (*strictptr) {
166 char *foundchr;
167 if ((foundchr = strchr(userlogin, *strictptr)))
168 *foundchr = '\0';
169 strictptr++;
170 }
171
172 if (!(searched_in_cache = search_in_cache(userlogin))) {
173 snprintf(filtersearch, sizeof(filtersearch), LDAPFilterSearch, userlogin, userlogin, userlogin, userlogin, userlogin);
174
175 /* Search record(s) in LDAP base */
176
177 int rc= ldap_search_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, NULL, 0, &result);
178 if ( rc != LDAP_SUCCESS ) {
179 debuga(_("LDAP search failed: %s\n"), ldap_err2string(rc));
180 strcpy(mappedname,userlogin);
181 return;
182 }
183
184 if (!(e = ldap_first_entry(ldap_handle, result)))
185 insert_to_cache(userlogin, userlogin);
186 else
187 for (attr = ldap_first_attribute(ldap_handle, e, &ber); attr != NULL; attr = ldap_next_attribute(ldap_handle, e, ber)) {
188 if (!strcasecmp(attr, LDAPTargetAttr)) {
189 if ((vals = (char **)ldap_get_values(ldap_handle, e, attr))!=NULL) {
190 insert_to_cache(userlogin, vals[0]);
191 strncpy(mappedname, vals[0],namelen-1);
192 mappedname[namelen-1]='\0';
193 ldap_memfree(vals);
194 }
195 ldap_memfree(attr);
196 break;
197 }
198 ldap_memfree(attr);
199 }
200 ldap_msgfree(result);
201 } else {
202 strncpy(mappedname, searched_in_cache,namelen-1);
203 mappedname[namelen-1]='\0';
204 }
205 }
206 #endif //HAVE_LDAP_H
207
208 void init_usertab(const char *UserTabFile)
209 {
210 if (strcmp(UserTabFile, "ldap") == 0) {
211 if(debug)
212 debuga(_("Loading User table: %s\n"),UserTabFile);
213 #ifdef HAVE_LDAP_H
214 which_usertab=UTT_Ldap;
215 init_ldap_usertab();
216 #else
217 debuga(_("LDAP module not compiled in sarg\n"));
218 exit(EXIT_FAILURE);
219 #endif //HAVE_LDAP_H
220 } else if (UserTabFile[0] != '\0') {
221 if(debug)
222 debuga(_("Loading User table: %s\n"),UserTabFile);
223 which_usertab=UTT_File;
224 init_file_usertab(UserTabFile);
225 } else {
226 which_usertab=UTT_None;
227 }
228 }
229
230 void user_find(char *mappedname, int namelen, const char *userlogin)
231 {
232 if (which_usertab==UTT_File) {
233 get_usertab_name(userlogin,mappedname,namelen);
234 }
235 #ifdef HAVE_LDAP_H
236 else if (which_usertab==UTT_Ldap) {
237 get_ldap_name(userlogin,mappedname,namelen);
238 }
239 #endif //HAVE_LDAP_H
240 else {
241 strncpy(mappedname,userlogin,namelen-1);
242 mappedname[namelen-1]='\0';
243 }
244 }
245
246 void close_usertab(void)
247 {
248 #ifdef HAVE_LDAP_H
249 if (ldap_handle) {
250 destroy_cache();
251 ldap_unbind(ldap_handle);
252 ldap_handle=NULL;
253 }
254 #endif //HAVE_LDAP_H
255 if(userfile) {
256 free(userfile);
257 userfile=NULL;
258 }
259 }
260