#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Print Server
#  master script for several administrative task for the CUPS
#  server. The performed task depends on the name it is invoked with
#
# Copyright 2004-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

import sys, string, re
import pwd, os, os.path
from pexpect import *
import univention.config_registry

args=sys.argv[1:]
cmd=sys.argv[0]

# check if we are called directly
if cmd[-len("univention-cupsadmin"):]=="univention-cupsadmin":
	print("univention-cupsadmin is not to be called directly, but rather by")
	print("links from one of univention-cups-{accept,reject,enable,disalbe}.")
	print("See man accept, man reject, man enable, and man disable for")
	print("calling conventions")
	sys.exit(1)

# argv[0] should be something like .../univention-cups... Cut
# univention-cups from it
#
# cupsenable and -disable aren't located in /usr/bin anymore
#if os.path.basename(cmd)=="univention-cups-enable" or os.path.basename(cmd)=="univention-cups-disable":
#	cmd=os.path.basename(cmd).replace("univention-cups-", "/usr/bin/cups")
#else:

cmd=os.path.basename(cmd).replace("univention-cups-", "/usr/sbin/cups")

# check if target executable exists
if not(os.path.exists(cmd)):
	print("Target executable %s does not exist. Exiting."%cmd)
	sys.exit(2)

baseConfig=univention.config_registry.ConfigRegistry()
baseConfig.load()

# read machine password
secretFile=open('/etc/machine.secret','r')
pwdLine=secretFile.readline()
machine_password=re.sub('\n','',pwdLine)

machine_uid=pwd.getpwnam('%s$' % (baseConfig['hostname']))[2]
old_uid=os.getuid()
os.setuid(machine_uid)

child = spawn('%s %s' % (cmd, string.join(args, ' ')))
i=0
timeout=60
while not i == 2:
	i = child.expect(['%s\.%s.*\?' %(baseConfig['hostname'],baseConfig['domainname']), 'localhost.*\?', EOF], timeout=timeout)
	if i in [0,1]:
		child.sendline(machine_password)
	elif i == 2:
		if child.before:
			print child.before
