This file is indexed.

/usr/lib/ipsec/verify is in libreswan 3.23-4.

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

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
#!/usr/bin/python3
#
# Copyright (C) 2012 - 2013 Paul Wouters <pwouters@redhat.com>
#
# Based on old perl and shell code:
# Copyright (C) 2003 Sam Sgro <sam@freeswan.org>
# Copyright (C) 2005-2008 Michael Richardson <mcr@xelerance.com>
# Copyright (C) 2005-2009 Paul Wouters <paul@xelerance.com>
# Copyright (C) 2012-2014 Paul Wouters <paul@libreswan.org>
#
# Based on "verify" from the FreeS/WAN distribution, (C) 2001 Michael
# Richardson <mcr@freeswan.org>
#
# 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.

import os, sys, subprocess, glob, socket, locale

retcode = 0

conffile = "/etc/ipsec.conf"
confdir = "/etc"
ipsecbin = "/usr/sbin/ipsec"

prefencoding = locale.getpreferredencoding(False)

if not os.path.isfile(ipsecbin):
	# hopefully somewhere in our path then
	ipsecbin = "ipsec"

if not os.path.isfile(conffile):
	if not os.path.isfile("%s/ipsec.conf"%confdir):
		# try some fall backs
		if os.path.isfile("/etc/ipsec.conf"):
			print("WARNING: ipsec.conf not found at compiled-in location '%s/ipsec.conf', using /etc/ipsec.conf instead" %confdir)
			confdir = "/etc/"
		elif os.path.isfile("/usr/local/etc/ipsec.conf"):
			print("WARNING: ipsec.conf not found at compiled-in location '%s/ipsec.conf', using /etc/ipsec.conf instead"%confdir)
			confdir = "/usr/local/etc/"
		else:
			sys.exit("Failed to find ipsec.conf - checked %s, /etc and /usr/local/etc"%confdir)

# Should we print in colour by default?
colour = 0
try:
	p = subprocess.Popen("consoletype", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	output, err = p.communicate()
	output = output.decode(prefencoding).strip()
	if output in ("vc","tty","pty"):
		colour = 1
except:
	try:
		p = subprocess.Popen("tput colors", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		output, err = p.communicate()
		if int(output) > 0:
			colour = 1
	except:
		pass


def printfun(text):
	# suppress newline
	sys.stdout.write("%-50s"%text)

def print_result(rcode, rtext):
	global colour
	global retcode
	OK = '\033[92m'
	WARN = '\033[93m'
	FAIL = '\033[91m'
	ENDC = '\033[0m'

	if rcode == "FAIL":
		retcode += 1
		if not rtext:
			rtext = "FAILED"
		if colour:
			print("\t[%s%s%s]"%(FAIL,rtext,ENDC))
		else:
			print("\t[%s]"%rtext)
	elif rcode == "WARN":
		if not rtext:
			rtext = "WARNING"
		if colour:
			print("\t[%s%s%s]"%(WARN,rtext,ENDC))
		else:
			print("\t[%s]"%rtext)
	elif rcode == "OK":
		if not rtext:
			rtext = "OK"
		if colour:
			print("\t[%s%s%s]"%(OK,rtext,ENDC))
		else:
			print("\t[%s]"%rtext)
	else:
		print("INTERNAL ERROR - unknown rcode:%s"%rcode)


def plutocheck():
	global retcode
	printfun("Checking that pluto is running")
	p = subprocess.Popen(["pidof", "pluto"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	output, err = p.communicate()
	if not output:
		retcode += 1
		print_result("FAIL","FAILED")
		return
	else:
		print_result("OK","OK")

	# only if pluto is running, do the listen tests and ipsec  secrets test
	udp500check()
	udp4500check()
	ipsecsecretcheck()

# This is pretty broken, as you can enable forwarding specificaly via iptables as well
# It also won't find/exclude all kinds of interfaces. Also, lots of people have one
# ethernet and one wifi interface, but don't want to bridge these.
# So, mostly left here for historic reasons - candidate to be changed/removed
def forwardcheck():
	global retcode
	try:
		output = open("/proc/net/dev","r").read().strip()
	except:
		printfun("Checking for multiple interfaces")
		retcode += 1
		print_result("FAIL","UNEXPECTED KERNEL DEV LIST")
	count = 0
	for line in output.split("\n"):
		if ":" in line:
			if not "lo:" in line and not "ipsec" in line and not "mast" in line and not "virbr:" in line:
				# let's count this as a real physical interface
				count += 1
	if count > 1:
		printfun("Two or more interfaces found, checking IP forwarding")
		try:
			output = open("/proc/sys/net/ipv4/ip_forward","r").read().strip()
		except:
			print_result("FAIL","MISSING ip_forward proc file")
			retcode += 1
			return
		if output == "1":
			print_result("OK","OK")
		else:
			print_result("FAIL","FAILED")
			retcode += 1

def rpfiltercheck():
	global retcode
	fail = 0
	printfun("Checking rp_filter")
	for dirname in glob.glob("/proc/sys/net/ipv4/conf/*"):
		val = open("%s/rp_filter"%dirname,"r").read().strip()
		if val == "1":
			if fail == 0:
				print_result("FAIL","ENABLED")
			fail = 1
			printfun(" %s/rp_filter"%dirname)
			print_result("FAIL","ENABLED")
			retcode += 1
	if fail == 0:
			print_result("OK","OK")
	else:
		print("  rp_filter is not fully aware of IPsec and should be disabled")

def cmdchecks():
	global retcode
	printfun("Checking 'ip' command")
	if not os.path.isfile("/sbin/ip") and not os.path.isfile("/usr/sbin/ip") \
	   and not os.path.isfile("/bin/ip"):
		print_result("FAIL","FAILED")
		retcode += 1
	p = subprocess.Popen(["ip", "xfrm"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	output, err = p.communicate()
	err = err.decode(prefencoding)
	if not "XFRM" in err:
		print_result("FAIL","IP XFRM BROKEN")
		retcode += 1
	else:
		print_result("OK","OK")

	printfun("Checking 'iptables' command")
	if not os.path.isfile("/sbin/iptables") and not os.path.isfile("/usr/sbin/iptables"):
		print_result("WARN","MISSING")
	else:
		print_result("OK","OK")

	printfun("Checking 'prelink' command does not interfere with FIPS")
	if os.path.isfile("/sbin/prelink") or os.path.isfile("/usr/sbin/prelink"):
		if os.path.isfile("/etc/prelink.cache"):
			print_result("WARN","PRESENT")
		else:
			print_result("FAIL","IN USE")
			retcode += 1
	else:
		print_result("OK","OK")

def udp500check():
	global retcode
	printfun(" Pluto listening for IKE on udp 500")
	try:
		p = subprocess.Popen(["ss", "-n", "-a", "-u", "sport = :500"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		output, err = p.communicate()
		output = output.decode(prefencoding)
		if ":500" in output:
			print_result("OK","OK")
		else:
			print_result("FAIL","FAILED")
			retcode += 1
	except:
		print_result("FAIL","FAILED")
		retcode += 1

def udp4500check():
	global retcode
	global sscmd
	printfun(" Pluto listening for IKE/NAT-T on udp 4500")
	if not sscmd:
		print_result("WARN","UNKNOWN")
		print("(install the 'ss' command to activate this test)")
		return
	try:
		p = subprocess.Popen([sscmd, "-n", "-a", "-u", "sport = :4500"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		output, err = p.communicate()
		output = output.decode(prefencoding)
		if ":4500" in output:
			print_result("OK","OK")
		else:
			print_result("WARN","DISABLED")
	except:
		print_result("FAIL","DISABLED")
		retcode += 1

def installstartcheck():
	global retcode
	print("Verifying installed system and configuration files\n")
	printfun("Version check and ipsec on-path")
	try:
		p = subprocess.Popen(["ipsec", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		output, err = p.communicate()
		output = output.decode(prefencoding)
		if "swan" in output:
			print_result("OK","OK")
			print(output.replace("Linux","").strip())
		else:
			print_result("FAIL","FAILED")
	except:
		print_result("FAIL","FAILED")

	printfun("Checking for IPsec support in kernel")
	if not os.path.isfile("/proc/net/ipsec_eroute") and not os.path.isfile("/proc/net/pfkey"):
		print_result("FAIL","FAILED")
		if "no kernel code presently loaded" in output:
			print("\n The ipsec service should be started before running 'ipsec verify'\n")
		return
	else:
		print_result("OK","OK")

	# we know we have some stack, so continue
	if os.path.isfile("/proc/net/ipsec_eroute"):
		installcheckklips()
	else:
		installchecknetkey()

def installcheckklips():
	global retcode
	# NAT-T patch check
	printfun(" KLIPS: checking for NAT Traversal support")
	try:
		natt = open("/sys/module/ipsec/parameters/natt_available","r").read().strip()
		if natt == "1":
			print_result("WARN","OLD STYLE")
		elif natt == "2":
			print_result("OK","OK")
		else:
			print_result("FAIL","UNKNOWN CODE")
	except:
		print_result("WARN","OLD/MISSING")

	# OCF patch check
	printfun(" KLIPS: checking for OCF crypto offload support ")
	try:
		ocf = open("/sys/module/ipsec/parameters/ocf_available","r").read().strip()
		if ocf == "1":
			print_result("OK","OK")
		elif ocf == "0":
			print_result("WARN","N/A")
		else:
			print_result("FAIL","UNKNOWN CODE")
	except:
		print_result("WARN","N/A")

	# SAref patch check
	saref = ""
	printfun(" KLIPS: IPsec SAref kernel support")
	try:
		saref = open("/proc/net/ipsec/saref","r").read().strip()
		if "refinfo patch applied" in saref:
			print_result("OK","OK")
		else:
			print_result("WARN","N/A")

	except:
		print_result("WARN","N/A")

	# SAref bind patch check
	printfun(" KLIPS: IPsec SAref Bind kernel support")
	if "bindref patch applied" in saref:
		print_result("OK","OK")
	else:
		print_result("WARN","N/A")

def installchecknetkey():
	global retcode
	print(" NETKEY: Testing XFRM related proc values")
	for option in ( "send_redirects", "accept_redirects"):
		printfun("         ICMP default/%s"%option)
		try:
			redir = open("/proc/sys/net/ipv4/conf/default/%s"%option,"r").read().strip()
		except:
			print_result("FAIL","VERY BROKEN KERNEL")
			return
		if redir == "0":
			print_result("OK","OK")
		else:
			print_result("FAIL","NOT DISABLED")
			print("\n  Disable /proc/sys/net/ipv4/conf/*/%s or NETKEY will act on or cause sending of bogus ICMP redirects!\n"%option)

	printfun("         XFRM larval drop")
	try:
		larval = open("/proc/sys/net/core/xfrm_larval_drop","r").read().strip()
	except:
		print_result("FAIL","OLD OR BROKEN KERNEL")
		return
	if larval == "1":
		print_result("OK","OK")
	else:
		print_result("FAIL","NOT ENABLED")

def ipsecsecretcheck():
	global retcode
	# we need to be root, because the only way to check is to reload them
	printfun(" Pluto ipsec.secret syntax")
	uid = os.getuid()
	if uid != 0:
		print_result("WARN","UNKNOWN")
		print(" (run ipsec verify as root to test ipsec.secrets)")
		return

	p = subprocess.Popen(["ipsec","secrets"], universal_newlines=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	output, err = p.communicate()
	output = output.decode(prefencoding)
	if "ERROR" in output:
		print_result("FAIL","PARSE ERROR")
		for line in output.split("\n"):
			line = line.strip()
			if line and "ERROR" in line:
				print("  %s"%line)
	elif "WARNING" in output:
		print_result("WARN","OBSOLETE")
		for line in output.split("\n"):
			line = line.strip()
			if line and "WARNING" in line:
				print("  %s"%line)
	else:
		print_result("OK","OK")

def ipsecconfcheck():
	global retcode
	printfun("Pluto ipsec.conf syntax")
	p = subprocess.Popen(["ipsec","addconn","--checkconfig"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	output, err = p.communicate()
	err = err.decode(prefencoding)
	if "syntax error" in err:
		print_result("FAIL","PARSE ERROR")
		print(err)
	else:
		print_result("OK","OK")

def configsetupcheck():
	global retcode
	p = subprocess.Popen(["ipsec","addconn","--configsetup"], universal_newlines=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	configsetup, err = p.communicate()

	# grab obsolete settings
	printfun("Checking for obsolete ipsec.conf options")
	if not err:
		print_result("OK","OK")
	else:
		print_result("WARN","OBSOLETE KEYWORD")
		err = err.replace("Warning"," Warning")
		print(err[:-1])

def main():
	global retcode
	global sscmd
	if os.path.isfile("/usr/sbin/ss"):
		sscmd = "/usr/sbin/ss"
	elif os.path.isfile("/bin/ss"):
		sscmd = "/bin/ss"
	elif os.path.isfile("/sbin/ss"):
		sscmd = "/sbin/ss"

	installstartcheck()
	ipsecconfcheck()
	forwardcheck()
	rpfiltercheck()
	plutocheck()
	cmdchecks()
	configsetupcheck()
	if retcode:
		plural = ""
		if retcode > 1:
			plural = "s"
		sys.stderr.write("\nipsec verify: encountered %s error%s - see 'man ipsec_verify' for help\n"%(retcode,plural))
		sys.exit(retcode)

if __name__ == "__main__":
	main()