This file is indexed.

/usr/share/sbnc/scripts/iface2.tcl is in sbnc-tcl 1.3.9-3.

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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# shroudBNC - an object-oriented framework for IRC
# Copyright (C) 2005-2014 Gunnar Beutner
# Copyright (C) 2010 Christoph Wiese
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Interface 2

# Configuration options are availble through iface2 commands. The RPC interface
# (Version 2) uses the same port(s) like shroudBNC.

source "/usr/share/sbnc/scripts/itype.tcl"

internalbind client iface:hijackclient RPC_IFACE
internalbind command iface:ircclientcommands
internaltimer 600 1 iface:expireblocks

proc iface:hijackclient {client params} {
	if {$client == "" && [string equal -nocase [lindex $params 0] "RPC_IFACE"]} {
		set idx [hijacksocket]
		if {[bncgetglobaltag iface2initpass] == "" || [bncgetglobaltag iface2initpass] == [lindex $params 1]} {
			control $idx iface:line
		} else {
			putdcc $idx "RPC_INVALIDINITPASS"
			internaltimer 1 0 "killdcc" $idx
		}
	}
}

set ::ifacecmds [list]
set ::ifaceblocks [list]

proc registerifacecmd {module command proc {accessproc "access:anyone"} {paramcount -1}} {
	global ifacecmds

	if {[catch [list info args $proc]] && $paramcount < 0} {
		return -code error "You need to specify a parameter count for native functions."
	}

	foreach cmd $ifacecmds {
		if {[string equal [lindex $cmd 0] $module] && [string equal [lindex $cmd 1] $command]} {
			unregisterifacecmd $module $command
		}
	}

	set index [lsearch -exact [reflect:commands] $command]

	if {$index != -1} {
		set cmd [lindex $ifacecmds $index]

		if {[catch [list info args $proc] arguments]} {
			set count $paramcount
		} else {
			set count [llength $arguments]

			if {[llength $arguments] == 1 && [lindex $arguments 0] == "args"} {
				return -code error "Proc must not have variable number of arguments."
			}
		}

		if {[lindex $cmd 4] != $paramcount} {
			set out "Invalid parameter count. Another function called \"$command\" which has "
			append out "[lindex $cmd 4] parameters already exists in module \"[lindex $cmd 0]\". "
			append out "Cannot create an override command which has $paramcount parameters."
			return -code error $out
		}
	}

	lappend ifacecmds [list $module $command $proc $accessproc $paramcount]

	return
}

proc unregisterifacecmd {module command} {
	global ifacecmds

	set i [expr [llength $ifacecmds] - 1]
	while {$i >= 0} {
		set cmd [lindex $ifacecmds $i]

		if {[string equal [lindex $cmd 0] $module] && [string equal [lindex $cmd 1] $command]} {
			set ifacecmds [lreplace $ifacecmds $i $i]
		}

		incr i -1
	}

	return
}

proc reflect:cancall {command user} {
	global ifacecmds

	foreach cmd $ifacecmds {
		if {[string equal [lindex $cmd 0] "core"] && [string equal [lindex $cmd 1] $command]} {
			if {[[lindex $cmd 3] $user]} {
				return 1
			} else {
				return 0
			}
		}
	}

	foreach cmd $ifacecmds {
		if {![string equal [lindex $cmd 0] "core"] && [string equal [lindex $cmd 1] $command] && [[lindex $cmd 3] $user]} {
			return 1
		}
	}

	return 0
}

proc reflect:commands {} {
	global ifacecmds

	set commands [list]

	foreach cmd $ifacecmds {
		if {[lsearch -exact $commands [lindex $cmd 1]] == -1} {
			lappend commands [lindex $cmd 1]
		}
	}

	return $commands
}

proc reflect:params {command} {
	global ifacecmds

	foreach cmd $ifacecmds {
		if {[string equal [lindex $cmd 1] $command]} {
			if {[catch [list info args [lindex $cmd 2]] arguments]} {
				set arguments [list]

				set i 0
				while {$i < [lindex $cmd 4]} {
					lappend arguments _$i
					incr i
				}
			}

			return $arguments
		}
	}

	return [list]
}

proc reflect:overrides {command} {
	global ifacecmds

	set results [list]

	foreach cmd $ifacecmds {
		if {[string equal [lindex $cmd 1] $command]} {
			lappend results [list [lindex $cmd 0] [lindex $cmd 3]]
		}
	}

	return $results
}

proc reflect:call2 {cmditem user arguments} {
	set oldcontext [getctx]
	set script [concat [list [lindex $cmditem 2]] $arguments]

	setctx $user
	set error [catch [list eval $script] result]
	setctx $oldcontext

	if {$error} {
		return [itype_exception "RPC_ERROR $result"]
	} elseif {$result == ""} {
		return [itype_string ""]
	} else {
		return $result
	}
}

proc reflect:call {command user arguments} {
	global ifacecmds

	if {![reflect:cancall $command $user]} {
		return [itype_exception "RPC_UNKNOWN_FUNCTION"]
	}

	if {[llength [reflect:params $command]] != [llength $arguments]} {
		return "RPC_PARAMCOUNT"
	}

	foreach cmd $ifacecmds {
		if {![string equal -nocase [lindex $cmd 0] "core"] && [string equal [lindex $cmd 1] $command]} {
			if {[[lindex $cmd 3] $user]} {
				set result [reflect:call2 $cmd $user $arguments]

				if {$result != "RPC_NORESULT"} {
					return $result
				}
			}
		}
	}

	foreach cmd $ifacecmds {
		if {[string equal -nocase [lindex $cmd 0] "core"] && [string equal [lindex $cmd 1] $command]} {
			if {[[lindex $cmd 3] $user]} {
				set result [reflect:call2 $cmd $user $arguments]

				if {$result != "RPC_NORESULT"} {
					return $result
				}
			}
		}
	}

	return "RPC_OK"
}

proc access:admin {username} {
	return [getbncuser $username admin]
}

proc access:anyone {username} {
	return 1
}

proc iface:evalline {line disconnectVar blockVar} {
	global ifaceoverride

	upvar $disconnectVar disconnect
	upvar $blockVar block

	if {[string equal -nocase [lindex [split $line] 0] "RPC_IFACE"]} {
		return "RPC_IFACE_OK"
	}

	set parsedtoks [itype_parse $line]

	set validparameters 0

	if {[lindex $parsedtoks 0] == "list"} {
		set listitems [lindex $parsedtoks 1]

		if {[llength $listitems] >= 4} {
			if {[lindex [lindex $listitems 0] 0] == "string" && [lindex [lindex $listitems 1] 0] == "string"
			  && [lindex [lindex $listitems 2] 0] == "string" && [lindex [lindex $listitems 3] 0] == "list"} {
				set validparameters 1
			}
		}
	}

	if {!$validparameters} {
		set disconnect 1

		return [itype_exception "RPC_INVALIDLINE"]
	}

	set toks [itype_flat $parsedtoks]

	set adm [split [lindex $toks 1] ":"]

	if {[llength $adm] < 2 || ![bncvaliduser [lindex $adm 0]] == -1} {
		set ifaceoverride 0
	} elseif {![getbncuser [lindex $adm 0] admin]} {
		set ifaceoverride 0
	} elseif {[catch [list getbncuser [lindex $adm 0] server]] || ![bnccheckpassword [lindex $adm 0] [lindex $adm 1]]} {
		set ifaceoverride 0
	} else {
		set ifaceoverride 1
	}

	if {[catch [list getbncuser [lindex $toks 0] server]] || ((![bnccheckpassword [lindex $toks 0] [lindex $toks 1]] || [getbncuser [lindex $toks 0] lock]) && !$ifaceoverride)} {
		set disconnect 1
		set block 1

		return [itype_exception "RPC_INVALIDUSERPASS"]
	}

	return [reflect:call [lindex $toks 2] [lindex $toks 0] [lindex $toks 3]]
}

proc iface:isoverride {} {
	global ifaceoverride

	return $ifaceoverride
}

proc iface:isipblocked {ip} {
	global ifaceblocks
	set count 0

	foreach block $ifaceblocks {
		if {[string equal -nocase $ip $block]} {
			incr count

			if {$count >= 3} {
				return 1
			}
		}
	}

	return 0
}

proc iface:logbadlogin {ip} {
	global ifaceblocks

	lappend ifaceblocks $ip
}

proc iface:expireblocks {} {
	global ifaceblocks

	set ifaceblocks [list]
}

proc iface:line {idx line} {
	if {$line == ""} {
		return
	}

	set disconnect 0
	set block 0

	set ip [internalgetipforsocket $idx]

	if {[iface:isipblocked $ip] && ![iface:istrustedip $ip]} {
		putdcc $idx [itype_exception "RPC_BLOCK"]
		set disconnect 1
	} else {
		putdcc $idx [iface:evalline $line disconnect block]
	}

	if {$block && ![iface:istrustedip $ip]} {
		iface:logbadlogin $ip
	}

	if {$disconnect} {
		internaltimer 1 0 "killdcc" $idx
	}
}

# IRC client commands

proc iface:ircclientcommands {client params} {
	if {[string equal -nocase [lindex $params 0] "globalset"] && [getbncuser $client admin]} {
		if {[llength $params] < 3} {
			if {[bncgetglobaltag iface2initpass] == ""} {
				set reply "Not set"
			} else {
				set reply [bncgetglobaltag iface2initpass]
			}
			internaltimer 0 0 bncreply "iface2initpass - $reply"
		} elseif {[string equal -nocase [lindex $params 1] "iface2initpass"]} {
			bncsetglobaltag iface2initpass [lindex $params 2]
			bncreply "Done."
			haltoutput
		}
	}
}

# core interface commands

proc iface-reflect:commands {} {
	set commands [reflect:commands]
	set result [itype_list_create]

	foreach command $commands {
		if {[reflect:cancall $command [getctx]]} {
			itype_list_insert result [itype_string $command]
		}
	}

	itype_list_end result

	return $result
}

registerifacecmd "reflect" "commands" "iface-reflect:commands"

proc iface-reflect:params {command} {
	if {[reflect:cancall $command [getctx]]} {
		return [itype_list_strings [reflect:params $command]]
	}

	return -code error "Unknown command."
}

registerifacecmd "reflect" "params" "iface-reflect:params"

proc iface-reflect:overrides {command} {
	if {[reflect:cancall $command [getctx]]} {
		set result [itype_list_create]
		set overrides [reflect:overrides $command]

		foreach override $overrides {
			itype_list_insert result [itype_list_strings $override]
		}

		itype_list_end result

		return $result
	}

	return -code error "Unknown command."
}

registerifacecmd "reflect" "overrides" "iface-reflect:overrides"

proc iface-reflect:modules {} {
	global ifacecmds

	set modules [list]

	foreach cmd $ifacecmds {
		if {[lsearch -exact $modules [lindex $cmd 0]] == -1} {
			lappend modules [lindex $cmd 0]
		}
	}

	return [itype_list_strings $modules]
}

registerifacecmd "reflect" "modules" "iface-reflect:modules"

proc iface-reflect:multicall {calls} {
	set results [itype_list_create]

	if {[llength $calls] > 10} {
		return -code error "You must not try to execute more than 10 calls at once."
	}

	set ctx [getctx]

	foreach call $calls {
		setctx $ctx
		itype_list_insert results [reflect:call [lindex $call 0] [getctx] [lindex $call 1]]
	}

	itype_list_end results

	return $results
}

registerifacecmd "reflect" "multicall" "iface-reflect:multicall"

proc iface-trust:addtrustedip {ip} {
	set ips [bncgetglobaltag iface2_trustedips]

	catch [list iface-trust:removetrustedip $ip]

	lappend ips $ip

	bncsetglobaltag iface2_trustedips $ips

	return ""
}

registerifacecmd "trust" "addtrustedip" "iface-trust:addtrustedip" "access:admin"

proc iface-trust:removetrustedip {ip} {
	set ips [bncgetglobaltag iface2_trustedips]

	set index [lsearch -exact [string tolower $ips] $ip]

	if {$index == -1} {
		return -code error "IP address not found."
	}

	set ips [lreplace $ips $index $index]

	bncsetglobaltag iface2_trustedips $ips

	return ""
}

registerifacecmd "trust" "removetrustedip" "iface-trust:removetrustedip" "access:admin"

proc iface-trust:gettrustedips {} {
	return [itype_list_strings [bncgetglobaltag iface2_trustedips]]
}

registerifacecmd "trust" "gettrustedips" "iface-trust:gettrustedips"  "access:admin"

proc iface:istrustedip {ip} {
	set ips [bncgetglobaltag iface2_trustedips]

	set index [lsearch -exact [string tolower $ips] $ip]

	if {$index == -1} {
		return 0
	} else {
		return 1
	}
}

proc iface-trust:istrustedip {ip} {
	return [itype_string [iface:istrustedip $ip]]
}

registerifacecmd "trust" "istrustedip" "iface-trust:istrustedip" "access:admin"