2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
31 #define LDAP_DEPRECATED 1
34 #include <ldap_cdefs.h>
35 #include <ldap_features.h>
40 //! Users matched against the ::UserTabFile file.
42 //! Users matched agains a LDAP.
44 //! No user matching performed.
48 enum UserTabEnum which_usertab
=UTT_None
;
50 static char *userfile
=NULL
;
53 static LDAP
*ldap_handle
=NULL
;
56 static void init_file_usertab(const char *UserTabFile
)
63 if((fp_usr
=fopen(UserTabFile
,"r"))==NULL
) {
64 debuga(_("(usertab) Cannot open file %s - %s\n"),UserTabFile
,strerror(errno
));
67 if (fseek(fp_usr
, 0, SEEK_END
)==-1) {
68 debuga(_("Failed to move till the end of the usertab file %s: %s\n"),UserTabFile
,strerror(errno
));
73 debuga(_("Cannot get the size of file %s"),UserTabFile
);
77 if (fseek(fp_usr
, 0, SEEK_SET
)==-1) {
78 debuga(_("Failed to rewind the usertab file %s: %s\n"),UserTabFile
,strerror(errno
));
81 if((userfile
=(char *) malloc(nreg
))==NULL
){
82 debuga(_("ERROR: Cannot load. Memory fault"));
87 while(fgets(buf
,sizeof(buf
),fp_usr
)!=NULL
) {
88 if (buf
[0]=='#') continue;
91 while(buf
[z1
] && (unsigned char)buf
[z1
]>' ') {
92 if (z2
+3>=nreg
) { //need at least 3 additional bytes for the minimum string "\n\t\0"
93 debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile
);
96 userfile
[z2
++]=buf
[z1
++];
98 while(buf
[z1
] && (unsigned char)buf
[z1
]<=' ') z1
++;
100 while(buf
[z1
] && (unsigned char)buf
[z1
]>=' ') {
101 if (z2
+2>=nreg
) { //need at least 2 additional bytes for "\t\0"
102 debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile
);
105 userfile
[z2
++]=buf
[z1
++];
107 while(userfile
[z2
-1]==' ') z2
--;
114 static void get_usertab_name(const char *user
,char *name
,int namelen
)
120 sprintf(warea
,"\t%s\n",user
);
121 if((str
=(char *) strstr(userfile
,warea
)) == (char *) NULL
) {
122 strncpy(name
,user
,namelen
);
125 str
=strchr(str
+1,'\n');
127 for(z1
=0; *str
!= '\t' && z1
<namelen
; z1
++) {
135 static void init_ldap_usertab(void) {
136 /* Setting LDAP connection and initializing cache */
138 if ((ldap_handle
= ldap_init(LDAPHost
, LDAPPort
)) == NULL
) {
139 debuga(_("Unable to connect to LDAP server %s on port %d\n"), LDAPHost
, LDAPPort
);
143 int ldap_protocol_version
= LDAPProtocolVersion
;
144 if (ldap_set_option(ldap_handle
, LDAP_OPT_PROTOCOL_VERSION
, &ldap_protocol_version
) != LDAP_SUCCESS
) {
145 debuga(_("Could not set LDAP protocol version %d\n"), ldap_protocol_version
);
149 /* Bind to the LDAP server. */
151 rc
= ldap_simple_bind_s( ldap_handle
, LDAPBindDN
, LDAPBindPW
);
152 if ( rc
!= LDAP_SUCCESS
) {
153 debuga(_("Cannot bind to LDAP server: %s\n"), ldap_err2string(rc
));
157 /* Initializing cache */
162 static void get_ldap_name(const char *userlogin
,char *mappedname
,int namelen
)
164 /* Start searching username in cache */
166 char filtersearch
[256], strictchars
[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr
= strictchars
, *searched_in_cache
;
168 LDAPMessage
*result
, *e
;
173 if ((foundchr
= strchr(userlogin
, *strictptr
)))
178 if (!(searched_in_cache
= search_in_cache(userlogin
))) {
179 snprintf(filtersearch
, sizeof(filtersearch
), LDAPFilterSearch
, userlogin
, userlogin
, userlogin
, userlogin
, userlogin
);
181 /* Search record(s) in LDAP base */
183 int rc
= ldap_search_s(ldap_handle
, LDAPBaseSearch
, LDAP_SCOPE_SUBTREE
, filtersearch
, NULL
, 0, &result
);
184 if ( rc
!= LDAP_SUCCESS
) {
185 debuga(_("LDAP search failed: %s\n"), ldap_err2string(rc
));
186 strcpy(mappedname
,userlogin
);
190 if (!(e
= ldap_first_entry(ldap_handle
, result
)))
191 insert_to_cache(userlogin
, userlogin
);
193 for (attr
= ldap_first_attribute(ldap_handle
, e
, &ber
); attr
!= NULL
; attr
= ldap_next_attribute(ldap_handle
, e
, ber
)) {
194 if (!strcasecmp(attr
, LDAPTargetAttr
)) {
195 if ((vals
= (char **)ldap_get_values(ldap_handle
, e
, attr
))!=NULL
) {
196 insert_to_cache(userlogin
, vals
[0]);
197 strncpy(mappedname
, vals
[0],namelen
-1);
198 mappedname
[namelen
-1]='\0';
206 ldap_msgfree(result
);
208 strncpy(mappedname
, searched_in_cache
,namelen
-1);
209 mappedname
[namelen
-1]='\0';
214 void init_usertab(const char *UserTabFile
)
216 if (strcmp(UserTabFile
, "ldap") == 0) {
218 debuga(_("Loading User table: %s\n"),UserTabFile
);
220 which_usertab
=UTT_Ldap
;
223 debuga(_("LDAP module not compiled in sarg\n"));
226 } else if (UserTabFile
[0] != '\0') {
228 debuga(_("Loading User table: %s\n"),UserTabFile
);
229 which_usertab
=UTT_File
;
230 init_file_usertab(UserTabFile
);
232 which_usertab
=UTT_None
;
236 void user_find(char *mappedname
, int namelen
, const char *userlogin
)
238 if (which_usertab
==UTT_File
) {
239 get_usertab_name(userlogin
,mappedname
,namelen
);
242 else if (which_usertab
==UTT_Ldap
) {
243 get_ldap_name(userlogin
,mappedname
,namelen
);
247 strncpy(mappedname
,userlogin
,namelen
-1);
248 mappedname
[namelen
-1]='\0';
252 void close_usertab(void)
257 ldap_unbind(ldap_handle
);