document rlm_otp fd leak fix
[freeradius.git] / dialup_admin / lib / ldap / functions.php3
1 <?php
2 require_once('../lib/xlat.php3');
3
4 function da_ldap_bind($ds,$config)
5 {
6         if ($ds){
7                 if ($config[ldap_use_http_credentials] == 'yes'){
8                         $din = $_SERVER["PHP_AUTH_USER"];
9                         $pass = $_SERVER["PHP_AUTH_PW"];
10                         if ($config[ldap_map_to_directory_manager] != '' &&
11                         $din == $config[ldap_map_to_directory_manager] &&
12                         $config[ldap_directory_manager] != '')
13                                 $din = $config[ldap_directory_manager];
14                 }
15                 if ($config[ldap_use_http_credentials] != 'yes' ||
16                         ($din == '' && $pass == '')){
17                         $din = $config[ldap_binddn];
18                         $pass = $config[ldap_bindpw];
19                 }
20                 if (preg_match('/[\s,]/',$din)){        // It looks like a dn
21                         if ($config[ldap_debug] == 'true')
22                                 print "<b>DEBUG(LDAP): Bind Request: DN='$din',PASSWD='$pass'</b><br>\n";
23                         return @ldap_bind($ds,"$din","$pass");
24                 }
25                 else{                           // It's not a DN. Find a corresponding DN
26                         if ($config[ldap_debug] == 'true')
27                 print "<b>DEBUG(LDAP): Bind Request: DN='$config[ldap_binddn]',PASSWD='$config[ldap_bindpw]'</b><br>\n";
28                         $r=@ldap_bind($ds,"$config[ldap_binddn]",$config[ldap_bindpw]);
29                         if ($r){
30                                 $sr=@ldap_search($ds,"$config[ldap_base]", 'uid=' . $din);
31                                 $info = @ldap_get_entries($ds, $sr);
32                                 $din = $info[0]['dn'];
33                                 if ($din != ''){
34                                         if ($config[ldap_debug] == 'true')
35                                                 print "<b>DEBUG(LDAP): Bind Request: DN='$din',PASSWD='$pass'</b><br>\n";
36                                         return @ldap_bind($ds,"$din","$pass");
37                                 }
38                         }
39                 }
40         }
41 }
42
43 function connect2db($config)
44 {
45         $ds=@ldap_connect("$config[ldap_server]");  // must be a valid ldap server!
46         if ($ds)
47                 $r=@da_ldap_bind($ds,$config);
48         return $ds;
49 }
50
51 function get_user_info($ds,$user,$config,$decode_normal,$k)
52 {
53         if ($ds){
54                 $attrs = array('cn');
55                 if ($config[ldap_userdn] == ''){
56                         if ($config[ldap_filter] != '')
57                                 $filter = xlat($config[ldap_filter],$login,$config);
58                         else
59                                 $filter = 'uid=' . $login;
60                 }
61                 else
62                         $filter = xlat($config[ldap_userdn],$login,$config);
63                 if ($config[ldap_debug] == 'true'){
64                         if ($config[ldap_userdn] == '')
65         print "<b>DEBUG(LDAP): Search Query: BASE='$config[ldap_base]',FILTER='$filter'</b><br>\n";
66                         else
67         print "<b>DEBUG(LDAP): Search Query: BASE='$filter',FILTER='(objectclass=radiusprofile)'</b><br>\n";
68                 }
69                 if ($config[ldap_userdn] == '')
70                         $sr=@ldap_search($ds,"$config[ldap_base]", $filter,$attrs);
71                 else
72                         $sr=@ldap_read($ds,$filter, '(objectclass=radiusprofile)',$attrs);
73                 $info = @ldap_get_entries($ds, $sr);
74                 $cn = $info[0]["cn"][0];
75                 if ($cn != '' && $decode_normal == 1)
76                         $cn = decode_string($cn,$k);
77                 if ($cn == '')
78                         $cn = '-';
79                 return $cn;
80         }
81 }
82
83 function get_user_dn($ds,$user,$config)
84 {
85         if ($ds){
86                 $attrs = array('dn');
87                 if ($config[ldap_userdn] == ''){
88                         if ($config[ldap_filter] != '')
89                                 $filter = xlat($config[ldap_filter],$login,$config);
90                         else
91                                 $filter = 'uid=' . $login;
92                 }
93                 else
94                         $filter = xlat($config[ldap_userdn],$login,$config);
95                 if ($config[ldap_debug] == 'true'){
96                         if ($config[ldap_userdn] == '')
97         print "<b>DEBUG(LDAP): Search Query: BASE='$config[ldap_base]',FILTER='$filter'</b><br>\n";
98                         else
99         print "<b>DEBUG(LDAP): Search Query: BASE='$filter',FILTER='(objectclass=radiusprofile)'</b><br>\n";
100                 }
101                 if ($config[ldap_userdn] == '')
102                         $sr=@ldap_search($ds,"$config[ldap_base]", $filter,$attrs);
103                 else
104                         $sr=@ldap_read($ds,$filter, '(objectclass=radiusprofile)',$attrs);
105                 $entry = ldap_first_entry($ds, $sr);
106                 if ($entry)
107                         $dn = ldap_get_dn($ds,$entry);
108                 return $dn;
109         }
110 }
111
112 function check_user_passwd($dn,$passwd,$config)
113 {
114         $ds=@ldap_connect("$config[ldap_server]");
115         if ($ds && $dn != '' && $passwd != ''){
116                 $r = @ldap_bind($ds,$dn,$passwd);
117                 if ($r)
118                         return TRUE;
119                 else
120                         return FALSE;
121         }
122         else
123                 return FALSE;
124
125         return FALSE;
126 }      
127
128 function closedb($ds,$config)
129 {
130         if ($ds)
131                 @ldap_close($ds);
132 }
133 ?>