This file is indexed.

/usr/share/zabbix/include/discovery.inc.php is in zabbix-frontend-php 1:1.8.11-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/*
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
?>
<?php

require_once('include/perm.inc.php');

?>
<?php
	function check_right_on_discovery($permission){
		global $USER_DETAILS;

		if( $USER_DETAILS['type'] >= USER_TYPE_ZABBIX_ADMIN ){
			if(count(get_accessible_nodes_by_user($USER_DETAILS, $permission, PERM_RES_IDS_ARRAY)))
				return true;
		}
	return false;
	}

	function svc_default_port($type_int){
		$port = 0;

		switch($type_int){
			case SVC_SSH: $port = '22'; break;
			case SVC_LDAP: $port = '389'; break;
			case SVC_SMTP: $port = '25'; break;
			case SVC_FTP: $port = '21'; break;
			case SVC_HTTP: $port = '80'; break;
			case SVC_POP: $port = '110'; break;
			case SVC_NNTP: $port = '119'; break;
			case SVC_IMAP: $port = '143'; break;
			case SVC_AGENT: $port = '10050'; break;
			case SVC_SNMPv1: $port = '161'; break;
			case SVC_SNMPv2: $port = '161'; break;
			case SVC_SNMPv3: $port = '161'; break;
		}
		return $port;
	}

	function discovery_check_type2str($type=null){
		$discovery_types = array(
			SVC_SSH => S_SSH,
			SVC_LDAP => S_LDAP,
			SVC_SMTP => S_SMTP,
			SVC_FTP => S_FTP,
			SVC_HTTP => S_HTTP,
			SVC_POP => S_POP,
			SVC_NNTP => S_NNTP,
			SVC_IMAP => S_IMAP,
			SVC_TCP => S_TCP,
			SVC_AGENT => S_ZABBIX_AGENT,
			SVC_SNMPv1 => S_SNMPV1_AGENT,
			SVC_SNMPv2 => S_SNMPV2_AGENT,
			SVC_SNMPv3 => S_SNMPV3_AGENT,
			SVC_ICMPPING => S_ICMPPING,
		);

		if(is_null($type)){
			order_result($discovery_types);
			return $discovery_types;
		}
		else if(isset($discovery_types[$type]))
			return $discovery_types[$type];
		else
			return S_UNKNOWN;
	}

	function discovery_check2str($type, $snmp_community, $key_, $port){
		$external_param = '';

		switch($type){
			case SVC_SNMPv1:
			case SVC_SNMPv2:
			case SVC_SNMPv3:
			case SVC_AGENT:
				$external_param = ' "'.$key_.'"';
				break;
		}
		$result = discovery_check_type2str($type);
		if((svc_default_port($type) != $port) || ($type == SVC_TCP))
			$result .= ' ('.$port.')';
		$result .= $external_param;

		return $result;
	}

	function discovery_port2str($type_int, $port){
		$port_def = svc_default_port($type_int);

		if($port != $port_def){
			return ' ('.$port.')';
		}

	return '';
	}

	function discovery_status2str($status_int){
		switch($status_int){
			case DRULE_STATUS_ACTIVE:	$status = S_ACTIVE;		break;
			case DRULE_STATUS_DISABLED:	$status = S_DISABLED;		break;
			default:
				$status = S_UNKNOWN;		break;
		}

	return $status;
	}

	function discovery_status2style($status){
		switch($status){
			case DRULE_STATUS_ACTIVE:	$status = 'off';	break;
			case DRULE_STATUS_DISABLED:	$status = 'on';		break;
			default:
				$status = 'unknown';	break;
		}

	return $status;
	}

	function discovery_object_status2str($status){
		$str_stat[DOBJECT_STATUS_UP] = S_UP;
		$str_stat[DOBJECT_STATUS_DOWN] = S_DOWN;
		$str_stat[DOBJECT_STATUS_DISCOVER] = S_DISCOVERED;
		$str_stat[DOBJECT_STATUS_LOST] = S_LOST;

		if(isset($str_stat[$status]))
			return $str_stat[$status];

		return S_UNKNOWN;
	}

	function get_discovery_rule_by_druleid($druleid){
		return DBfetch(DBselect('select * from drules where druleid='.$druleid));
	}

	function set_discovery_rule_status($druleid, $status){
		return DBexecute('update drules set status='.$status.' where druleid='.$druleid);
	}

	function add_discovery_check($druleid, $type, $ports, $key, $snmp_community,
			$snmpv3_securityname, $snmpv3_securitylevel, $snmpv3_authpassphrase, $snmpv3_privpassphrase)
	{
		// no need to store those items in DB if they will not be used
		if($snmpv3_securitylevel == ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV){
			$snmpv3_authpassphrase = '';
			$snmpv3_privpassphrase = '';
		}
		if($snmpv3_securitylevel == ITEM_SNMPV3_SECURITYLEVEL_AUTHNOPRIV){
			$snmpv3_privpassphrase = '';
		}

		$dcheckid = get_dbid('dchecks', 'dcheckid');
		$result = DBexecute('insert into dchecks (dcheckid,druleid,type,ports,key_,snmp_community'.
				',snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,snmpv3_privpassphrase) '.
				' values ('.$dcheckid.','.$druleid.','.$type.','.zbx_dbstr($ports).','.
				zbx_dbstr($key).','.zbx_dbstr($snmp_community).','.zbx_dbstr($snmpv3_securityname).','.
				$snmpv3_securitylevel.','.zbx_dbstr($snmpv3_authpassphrase).','.zbx_dbstr($snmpv3_privpassphrase).')');

		if(!$result)
			return $result;

		return $dcheckid;
	}

	function add_discovery_rule($proxy_hostid, $name, $iprange, $delay, $status, $dchecks, $uniqueness_criteria){
		if(!validate_ip_range($iprange)){
			error(S_INCORRECT_IP_RANGE);
			return false;
		}

		// checking to the duplicate of the name
		if(CDRule::exists(array('name' => $name))){
			error(S_DISCOVERY_RULE.SPACE.'['.$name.']'.SPACE.S_ALREADY_EXISTS_SMALL);
			return false;
		}

		$druleid = get_dbid('drules', 'druleid');
		$result = DBexecute('insert into drules (druleid,proxy_hostid,name,iprange,delay,status) '.
			' values ('.$druleid.','.$proxy_hostid.','.zbx_dbstr($name).','.zbx_dbstr($iprange).','.$delay.','.$status.')');

		if($result && isset($dchecks)){
			$unique_dcheckid = 0;
			foreach($dchecks as $id => $data){
				$data['dcheckid'] = add_discovery_check($druleid, $data['type'], $data['ports'], $data['key'],
						$data['snmp_community'], $data['snmpv3_securityname'], $data['snmpv3_securitylevel'],
						$data['snmpv3_authpassphrase'], $data['snmpv3_privpassphrase']);
				if ($uniqueness_criteria == $id && $data['dcheckid'])
					$unique_dcheckid = $data['dcheckid'];
			}
			DBexecute('UPDATE drules'.
					' SET unique_dcheckid='.$unique_dcheckid.
					' WHERE druleid='.$druleid);

			$result = $druleid;
		}

		return $result;
	}

	function update_discovery_rule($druleid, $proxy_hostid, $name, $iprange, $delay, $status, $dchecks,	$uniqueness_criteria, $dchecks_deleted){
		if( !validate_ip_range($iprange) ){
			error(S_INCORRECT_IP_RANGE);
			return false;

		}

		// checking to the duplicate of the name
		$drule = get_discovery_rule_by_druleid($druleid);
		if(strcmp($drule['name'], $name)){
			if(CDRule::exists(array('name' => $name))){
				error(S_DISCOVERY_RULE.SPACE.'['.$name.']'.SPACE.S_ALREADY_EXISTS_SMALL);
				return false;
			}
		}

		$result = DBexecute('update drules set proxy_hostid='.$proxy_hostid.',name='.zbx_dbstr($name).',iprange='.zbx_dbstr($iprange).','.
			'delay='.$delay.',status='.$status.' where druleid='.$druleid);

		if($result && isset($dchecks)){
			$unique_dcheckid = 0;
			foreach($dchecks as $id => $data){
				if(!isset($data['dcheckid'])){
					$data['dcheckid'] = add_discovery_check($druleid, $data['type'], $data['ports'], $data['key'],
							$data['snmp_community'], $data['snmpv3_securityname'], $data['snmpv3_securitylevel'],
							$data['snmpv3_authpassphrase'], $data['snmpv3_privpassphrase']);
				}
				if ($uniqueness_criteria == $id && $data['dcheckid'])
					$unique_dcheckid = $data['dcheckid'];
			}

			DBexecute('UPDATE drules'.
					' SET unique_dcheckid='.$unique_dcheckid.
					' WHERE druleid='.$druleid);
		}

		if($result && isset($dchecks_deleted) && !empty($dchecks_deleted))
			delete_discovery_check($dchecks_deleted);

	return $result;
	}

	function delete_discovery_check($dcheckids){
		$actionids = array();
// conditions
		$sql = 'SELECT DISTINCT actionid '.
				' FROM conditions '.
				' WHERE conditiontype='.CONDITION_TYPE_DCHECK.
					' AND '.DBcondition('value', $dcheckids, false, true);	// FIXED[POSIBLE value type violation]!!!

		$db_actions = DBselect($sql);
		while($db_action = DBfetch($db_actions))
			$actionids[] = $db_action['actionid'];

// disabling actions with deleted conditions
		if (!empty($actionids)){
			DBexecute('UPDATE actions '.
					' SET status='.ACTION_STATUS_DISABLED.
					' WHERE '.DBcondition('actionid', $actionids));

// delete action conditions
			DBexecute('DELETE FROM conditions '.
					' WHERE conditiontype='.CONDITION_TYPE_DCHECK.
					' AND '.DBcondition('value', $dcheckids, false, true));	// FIXED[POSIBLE value type violation]!!!
		}

		DBexecute('DELETE FROM dservices WHERE '.DBcondition('dcheckid', $dcheckids));

		DBexecute('DELETE FROM dchecks WHERE '.DBcondition('dcheckid', $dcheckids));
	}

	function delete_discovery_rule($druleid){
		$result = true;

		$actionids = array();
// conditions
		$sql = 'SELECT DISTINCT actionid '.
				' FROM conditions '.
				' WHERE conditiontype='.CONDITION_TYPE_DRULE.
					" AND value='$druleid'";

		$db_actions = DBselect($sql);
		while($db_action = DBfetch($db_actions))
			$actionids[] = $db_action['actionid'];

// disabling actions with deleted conditions
		if(!empty($actionids)){
			DBexecute('UPDATE actions '.
					' SET status='.ACTION_STATUS_DISABLED.
					' WHERE '.DBcondition('actionid', $actionids));

// delete action conditions
			DBexecute('DELETE FROM conditions '.
					' WHERE conditiontype='.CONDITION_TYPE_DRULE.
					" AND value='$druleid'");
		}

		if($result){
			$db_dhosts = DBselect('select dhostid from dhosts'.
					' where druleid='.$druleid.' and '.DBin_node('dhostid'));

			while ($result && ($db_dhost = DBfetch($db_dhosts)))
				$result = DBexecute('delete from dservices where'.
						' dhostid='.$db_dhost['dhostid']);
		}

		if ($result)
			$result = DBexecute('delete from dhosts where druleid='.$druleid);

		if ($result)
			$result = DBexecute('delete from dchecks where druleid='.$druleid);

		if ($result)
			$result = DBexecute('delete from drules where druleid='.$druleid);

		return $result;
	}
?>