#!/usr/bin/env python

# For some information about the interface used, see :
# http://cr.yp.to/checkpwd/interface.html

import os
import sys

INPUT_FD = 3
data = ""

try:
	file = os.fdopen(INPUT_FD)
	data = os.read(file.fileno(),512)
	file.close()
except:
	print "Could not get data from file descriptor 3"
	# If there is a temporary problem checking the password, checkpassword exits 111
	sys.exit(111)

res = 0

for check in ['/usr/bin/checkvpw', '/var/qmail/bin/auth_pop']: # '/bin/checkpassword'
	sys.argv[0] = check.split('/')[-1]

	# r is 3, w is 4
	(r,w) = os.pipe()

	pid = os.fork()
		
	if pid:
		os.close(r)
		os.write(w,data)
		os.close(w)

		(pid, res) = os.waitpid(pid,0)
		res = res >> 8
		if res == 0:
			break
	else:
		os.close(w)
		os.execve(check,sys.argv,os.environ)


sys.exit(res)
