~ubuntu-branches/ubuntu/maverick/cacti/maverick-security

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/*
  V4.54 5 Nov 2004  (c) 2000-2004 John Lim (jlim#natsoft.com.my). All rights reserved.
   Released under both BSD license and Lesser GPL library license. 
  Whenever there is any discrepancy between the two licenses, 
  the BSD license will take precedence.
  Set tabs to 8.
  
  
  Joshua Eldridge (joshuae74#hotmail.com)
*/ 

// security - hide paths
if (!defined('ADODB_DIR')) die();

class ADODB_ldap extends ADOConnection {
    var $databaseType = 'ldap';
	var $dataProvider = 'ldap';
	
	# Connection information
    var $username = false;
    var $password = false;
    
    # Used during searches
    var $filter;
    var $dn;


	function ADODB_ldap() 
	{		

	}
  		
	// returns true or false
	
	function _connect( $host, $username, $password, $ldapbase )
	{

	   if ( !function_exists( 'ldap_connect' ) ) return null;
	   
	   $conn_info = array( $host );
	   
	   if ( strstr( $host, ':' ) ) {
	       $conn_info = split( ':', $host );
	   } 

	   $this->_connectionID = ldap_connect( $conn_info[0], $conn_info[1] ) 
	       or die( 'Could not connect to ' . $this->_connectionID );
	   if ($username && $password) {
	       $bind = ldap_bind( $this->_connectionID, $username, $password ) 
	           or die( 'Could not bind to ' . $this->_connectionID . ' with $username & $password');
	   } else {
	       $bind = ldap_bind( $this->_connectionID ) 
	           or die( 'Could not bind anonymously to ' . $this->_connectionID );
	   }
	   return $this->_connectionID;
    }
    
	
	/* returns _queryID or false */
	function _query($sql,$inputarr)
	{
	   $rs = ldap_search( $this->_connectionID, $this->database, $sql );
       return $rs; 
		
	}

    /* closes the LDAP connection */
	function _close()
	{
		@ldap_close( $this->_connectionID );
		$this->_connectionID = false;
	}
    
    function ServerInfo()
    {
        if( is_array( $this->version ) ) return $this->version;
        $version = array();
        /*
        Determines how aliases are handled during search. 
        LDAP_DEREF_NEVER (0x00)
        LDAP_DEREF_SEARCHING (0x01)
        LDAP_DEREF_FINDING (0x02)
        LDAP_DEREF_ALWAYS (0x03)
        The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but 
        not when locating the base object of the search. The LDAP_DEREF_FINDING value means 
        aliases are dereferenced when locating the base object but not during the search.  
        Default: LDAP_DEREF_NEVER
        */
        ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
        switch ( $version['LDAP_OPT_DEREF'] ) {
          case 0:
            $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
          case 1:
            $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
          case 2:
            $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
          case 3:
            $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
        }
        
        /* 
        A limit on the number of entries to return from a search. 
        LDAP_NO_LIMIT (0) means no limit.
        Default: LDAP_NO_LIMIT
        */
        ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
        if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
           $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
        }
        
        /*
        A limit on the number of seconds to spend on a search. 
        LDAP_NO_LIMIT (0) means no limit.
        Default: LDAP_NO_LIMIT
        */
        ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
        if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
           $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
        }
        
        /*
        Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not. 
        LDAP_OPT_ON
        LDAP_OPT_OFF
        Default: ON
        */
        ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
        if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
           $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
        } else {
           $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
        
        }
        /*
        Determines whether LDAP I/O operations are automatically restarted if they abort prematurely. 
        LDAP_OPT_ON
        LDAP_OPT_OFF
        Default: OFF
        */
        ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
        if ( $version['LDAP_OPT_RESTART'] == 0 ) {
           $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
        } else {
           $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
        
        }
        /*
        This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
        LDAP_VERSION2 (2)
        LDAP_VERSION3 (3)
        Default: LDAP_VERSION2 (2)
        */
        ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
        if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
           $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
        } else {
           $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
        
        }
        /* The host name (or list of hosts) for the primary LDAP server. */
        ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] ); 
        ldap_get_option( $this->_connectionID, OPT_ERROR_NUMBER, $version['OPT_ERROR_NUMBER'] ); 
        ldap_get_option( $this->_connectionID, OPT_ERROR_STRING, $version['OPT_ERROR_STRING'] ); 
        ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] ); 
        
        return $this->version = $version;
    
    }
}
	
/*--------------------------------------------------------------------------------------
	 Class Name: Recordset
--------------------------------------------------------------------------------------*/

class ADORecordSet_ldap extends ADORecordSet{	
	
	var $databaseType = "ldap";
	var $canSeek = false;
	var $_entryID; /* keeps track of the entry resource identifier */
	
	function ADORecordSet_ldap($queryID,$mode=false) 
	{
		if ($mode === false) { 
			global $ADODB_FETCH_MODE;
			$mode = $ADODB_FETCH_MODE;
		}
		switch ($mode)
		{
		case ADODB_FETCH_NUM: 
		  $this->fetchMode = LDAP_NUM; 
		break;
		case ADODB_FETCH_ASSOC: 
		  $this->fetchMode = LDAP_ASSOC; 
		break;
		default:
		case ADODB_FETCH_DEFAULT:
		case ADODB_FETCH_BOTH: 
		  $this->fetchMode = LDAP_BOTH; 
		break;
		}
	
		$this->ADORecordSet($queryID);	
	}
	
	function _initrs()
	{
	   /* 
	   This could be teaked to respect the $COUNTRECS directive from ADODB
	   It's currently being used in the _fetch() function and the
	   GetAssoc() function
       */
	    $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );

	}

    /*
    Return whole recordset as a multi-dimensional associative array
	*/
	function &GetAssoc($force_array = false, $first2cols = false) 
	{
		$records = $this->_numOfRows;
        $results = array();
            for ( $i=0; $i < $records; $i++ ) {
                foreach ( $this->fields as $k=>$v ) {
                    if ( is_array( $v ) ) {
                        if ( $v['count'] == 1 ) {
                            $results[$i][$k] = $v[0];
                        } else {
                            array_shift( $v );
                            $results[$i][$k] = $v;
                        } 
                    }
                }
            }
        
		return $results; 
	}
    
    function &GetRowAssoc()
	{
        $results = array();
        foreach ( $this->fields as $k=>$v ) {
            if ( is_array( $v ) ) {
                if ( $v['count'] == 1 ) {
                    $results[$k] = $v[0];
                } else {
                    array_shift( $v );
                    $results[$k] = $v;
                } 
            }
        }
 
		return $results; 
	}
		
    function GetRowNums()
    {
        $results = array();
        foreach ( $this->fields as $k=>$v ) {
        static $i = 0;
            if (is_array( $v )) {
                if ( $v['count'] == 1 ) {
                    $results[$i] = $v[0];
                } else {
                    array_shift( $v );
                    $results[$i] = $v;
                } 
            $i++;
            }
        }
        return $results;
    }
	
	function _fetch()
	{		
		if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 )
        	return false;
        	
        if ( $this->_currentRow == 0 ) {
		  $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
        } else {
          $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
        }
	    
	    $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
	    $this->_numOfFields = $this->fields['count'];	
	    switch ( $this->fetchMode ) {
            
            case LDAP_ASSOC:
            $this->fields = $this->GetRowAssoc();
            break;
            
            case LDAP_NUM:
            $this->fields = $this->GetRowNums();
            break;
            
            case LDAP_BOTH:
            default:
            break;
        }
        return ( is_array( $this->fields ) );        
	}
	
	function _close() {
		@ldap_free_result( $this->_queryID );	
		$this->_queryID = false;
	}
	
}
?>