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
)
64 if((fp_usr
=fopen(UserTabFile
,"r"))==NULL
) {
65 fprintf(stderr
, "SARG: (log) %s: %s - %s\n",_("Cannot open file"),UserTabFile
,strerror(errno
));
68 fseek(fp_usr
, 0, SEEK_END
);
71 fprintf(stderr
,"SARG: Cannot get the size of file %s",UserTabFile
);
75 fseek(fp_usr
, 0, SEEK_SET
);
76 if((userfile
=(char *) malloc(nreg
))==NULL
){
77 fprintf(stderr
, "SARG ERROR: %s",_("Cannot load. Memory fault"));
82 while(fgets(buf
,sizeof(buf
),fp_usr
)!=NULL
) {
83 if (buf
[0]=='#') continue;
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 fprintf(stderr
,"SARG: The list of the users is too long in your %s file.\n",UserTabFile
);
91 userfile
[z2
++]=buf
[z1
++];
93 while(buf
[z1
] && (unsigned char)buf
[z1
]<=' ') z1
++;
95 while(buf
[z1
] && (unsigned char)buf
[z1
]>' ') {
96 if (z2
+2>=nreg
) { //need at least 2 additional bytes for "\t\0"
97 fprintf(stderr
,"SARG: The list of the users is too long in your %s file.\n",UserTabFile
);
100 userfile
[z2
++]=buf
[z1
++];
108 static void get_usertab_name(const char *user
,char *name
,int namelen
)
114 sprintf(warea
,"\t%s\n",user
);
115 if((str
=(char *) strstr(userfile
,warea
)) == (char *) NULL
) {
116 strncpy(name
,user
,namelen
);
119 str
=strchr(str
+1,'\n');
121 for(z1
=0; *str
!= '\t' && z1
<namelen
; z1
++) {
129 static void init_ldap_usertab(void) {
130 /* Setting LDAP connection and initializing cache */
132 if ((ldap_handle
= ldap_init(LDAPHost
, LDAPPort
)) == NULL
) {
133 debuga("\nUnable to connect to LDAP server:%s port:%d\n", LDAPHost
, LDAPPort
);
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_OPT_PROTOCOL_VERSION %d\n", ldap_protocol_version
);
143 /* Bind to the LDAP server. */
145 rc
= ldap_simple_bind_s( ldap_handle
, LDAPBindDN
, LDAPBindPW
);
146 if ( rc
!= LDAP_SUCCESS
) {
147 debuga("ldap_simple_bind_s: %s\n", ldap_err2string(rc
));
151 /* Initializing cache */
156 static void get_ldap_name(const char *userlogin
,char *mappedname
,int namelen
)
158 /* Start searching username in cache */
160 char filtersearch
[256], strictchars
[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr
= strictchars
, *searched_in_cache
;
162 LDAPMessage
*result
, *e
;
167 if ((foundchr
= strchr(userlogin
, *strictptr
)))
172 if (!(searched_in_cache
= search_in_cache(userlogin
))) {
173 snprintf(filtersearch
, sizeof(filtersearch
), LDAPFilterSearch
, userlogin
, userlogin
, userlogin
, userlogin
, userlogin
);
175 /* Search record(s) in LDAP base */
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_s: %s\n", ldap_err2string(rc
));
180 strcpy(mappedname
,userlogin
);
184 if (!(e
= ldap_first_entry(ldap_handle
, result
)))
185 insert_to_cache(userlogin
, userlogin
);
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';
200 ldap_msgfree(result
);
202 strncpy(mappedname
, searched_in_cache
,namelen
-1);
203 mappedname
[namelen
-1]='\0';
208 void init_usertab(const char *UserTabFile
)
210 if (strcmp(UserTabFile
, "ldap") == 0) {
212 debuga(_("Loading User table: %s\n"),UserTabFile
);
214 which_usertab
=UTT_Ldap
;
217 debuga(_("LDAP module not compiled in sarg\n"));
220 } else if (UserTabFile
[0] != '\0') {
222 debuga(("Loading User table: %s\n"),UserTabFile
);
223 which_usertab
=UTT_File
;
224 init_file_usertab(UserTabFile
);
226 which_usertab
=UTT_None
;
230 void user_find(char *mappedname
, int namelen
, const char *userlogin
)
232 if (which_usertab
==UTT_File
) {
233 get_usertab_name(userlogin
,mappedname
,namelen
);
236 else if (which_usertab
==UTT_Ldap
) {
237 get_ldap_name(userlogin
,mappedname
,namelen
);
241 strncpy(mappedname
,userlogin
,namelen
-1);
242 mappedname
[namelen
-1]='\0';
246 void close_usertab(void)
251 ldap_unbind(ldap_handle
);