#!/usr/bin/python3
# -*- 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-2022 Univention GmbH
#
# https://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
# <https://www.gnu.org/licenses/>.

from __future__ import print_function

import sys
import pwd
import os
import os.path

from pexpect import spawn, EOF

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,disable}.")
	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)

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

# read machine password
with open('/etc/machine.secret', 'r') as fd:
	machine_password = fd.readline().strip()

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

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