Example #1
0
File: main.py Project: gonicus/gosa
def get_local_ppd_path(url):
    server_url = get_server_url()
    if url[0:len(server_url)] == server_url:
        http_part = "%s/ppd/modified/" % get_server_url()
        dir = Environment.getInstance().config.get("cups.spool", default="/tmp/spool")
        return "%s/%s" % (dir, url[len(http_part):])
    elif url[0:1] == "/":
        # is already a local path
        return url
    return None
Example #2
0
def get_local_ppd_path(url):
    server_url = get_server_url()
    if url[0:len(server_url)] == server_url:
        http_part = "%s/ppd/modified/" % get_server_url()
        dir = Environment.getInstance().config.get("cups.spool",
                                                   default="/tmp/spool")
        return "%s/%s" % (dir, url[len(http_part):])
    elif url[0:1] == "/":
        # is already a local path
        return url
    return None
Example #3
0
File: filter.py Project: GOsa3/gosa
    def get_local_ppd_path(self, url):
        if self.get_ppd_type(url) == "local":
            # remove server path
            http_part = "%s/ppd/modified/" % get_server_url()
            return url[len(http_part):]

        return None
Example #4
0
 def __init__(self):
     self.env = Environment.getInstance()
     self.log = logging.getLogger(__name__)
     self.ppd_dir = self.env.config.get("cups.spool", default="/tmp/spool")
     if not path.exists(self.ppd_dir):
         makedirs(self.ppd_dir)
     self.base_url = "%s/ppd-proxy/" % get_server_url()
Example #5
0
 def __init__(self):
     self.env = Environment.getInstance()
     self.log = logging.getLogger(__name__)
     self.ppd_dir = self.env.config.get("cups.spool", default="/tmp/spool")
     if not path.exists(self.ppd_dir):
         makedirs(self.ppd_dir)
     self.base_url = "%s/ppd-proxy/" % get_server_url()
Example #6
0
File: filter.py Project: GOsa3/gosa
 def get_ppd_type(self, url):
     """
     Determines the PPD type from its URL. Can be either:
     1. Remote URL (points to a foreign server)
     2. Lokal URL (points to the local CUPS server)
     """
     server_url = get_server_url()
     return "local" if url[0:len(server_url)] == server_url else "remote"
Example #7
0
    def __gen_state_event(self, state):
        if not self.__hostname and self.client_type in ['proxy', 'backend']:
            from gosa.backend.components.httpd import get_server_url
            url = urlparse(get_server_url())
            self.__hostname = url.hostname

        if self.client_type in ['proxy', 'backend']:
            return self.e.Event(self.e.BusClientState(
                self.e.Id(self.client_id),
                self.e.Hostname(self.__hostname),
                self.e.State(state),
                self.e.Type(self.client_type)
            ))
        else:
            return self.e.Event(self.e.BusClientState(
                self.e.Id(self.client_id),
                self.e.State(state),
                self.e.Type(self.client_type)
            ))
Example #8
0
File: main.py Project: GOsa3/gosa
    def __init__(self):
        self.env = Environment.getInstance()
        self.log = logging.getLogger(__name__)
        self.factory = ObjectFactory.getInstance()
        incoming_base = self.env.config.get("foreman.host-rdn")
        if incoming_base is None or len(incoming_base) == 0:
            incoming_base = self.env.base
        else:
            incoming_base = "%s,%s" % (incoming_base, self.env.base)

        group_rdn = self.env.config.get("foreman.group-rdn")
        self.type_bases = {"ForemanHost": incoming_base}
        if group_rdn is not None and len(group_rdn) > 0:
            self.type_bases["ForemanHostGroup"] = "%s,%s" % (group_rdn, self.env.base)
        else:
            self.type_bases["ForemanHostGroup"] = self.env.base

        self.__marked_hosts = {}
        if self.env.config.get("foreman.host") is None:
            self.log.warning("no foreman host configured")
        else:
            self.init_client(self.env.config.get("foreman.host"))
            self.gosa_server = "%s/rpc" % get_server_url()
            self.mqtt_host = None

            mqtt_host = self.env.config.get('mqtt.host')
            if mqtt_host is not None:
                if mqtt_host == "localhost":
                    mqtt_host = socket.getfqdn()
                self.mqtt_host = "%s:%s" % (mqtt_host, self.env.config.get('mqtt.port', default=1883))

            # Listen for object events
            self.log.info("Initial-Sync: %s, startpassive: %s" % (self.env.config.getboolean("foreman.initial-sync", default=True), self.env.config.getboolean("core.startpassive", default=False)))
            if not hasattr(sys, '_called_from_test') and \
                    self.env.config.getboolean("foreman.initial-sync", default=True) is True and \
                    self.env.mode != "proxy":
                zope.event.subscribers.append(self.__handle_events)
Example #9
0
    def writePPD(self, printer_cn, server_ppd_file, custom_ppd_file, data):
        if self.client is None:
            return

        server_ppd = None
        dir = self.env.config.get("cups.spool", default="/tmp/spool")
        if not os.path.exists(dir):
            os.makedirs(dir)
        try:
            server_ppd = self.client.getServerPPD(server_ppd_file)
            is_server_ppd = True
            ppd = cups.PPD(server_ppd)
        except Exception as e:
            self.log.error(str(e))
            is_server_ppd = False

            if custom_ppd_file is not None:
                ppd = cups.PPD(os.path.join(dir, custom_ppd_file))
            else:
                raise PPDException(C.make_error('COULD_NOT_READ_SOURCE_PPD'))

        if isinstance(data, str):
            data = loads(data)

        # apply options
        for option_name, value in data.items():
            option = ppd.findOption(option_name)
            if option is not None:
                conflicts = ppd.markOption(option_name, value)
                if conflicts > 0:
                    raise PPDException(
                        C.make_error('OPTION_CONFLICT',
                                     option=option_name,
                                     value=value,
                                     conflicts=conflicts))
            else:
                raise PPDException(
                    C.make_error('OPTION_NOT_FOUND', option=option_name))

        # calculate hash value for new PPD

        temp_file = tempfile.NamedTemporaryFile(delete=False)
        try:
            with open(temp_file.name, "w") as tf:
                ppd.writeFd(tf.fileno())

            with open(temp_file.name, "r") as tf:
                result = tf.read()

            hash = hashlib.md5(repr(result).encode('utf-8')).hexdigest()
            index = PluginRegistry.getInstance("ObjectIndex")

            new_file = os.path.join(dir, "%s.ppd" % hash)
            if new_file == custom_ppd_file:
                # nothing to to
                return {}

            if not is_server_ppd:
                # check if anyone else is using a file with this hash value and delete the old file if not
                query = {
                    "_type": "GotoPrinter",
                    "gotoPrinterPPD": "%s.ppd" % hash
                }
                if printer_cn is not None:
                    query["not_"] = {"cn": printer_cn}
                res = index.search(query, {"dn": 1})
                if len(res) == 0:
                    # delete file
                    os.unlink(custom_ppd_file)

            with open(new_file, "w") as f:
                f.write(result)

            return {
                "gotoPrinterPPD":
                ["%s/ppd/modified/%s.ppd" % (get_server_url(), hash)],
                "configured": [True]
            }

        except Exception as e:
            self.log.error(str(e))
            return {}
        finally:
            os.unlink(temp_file.name)
            if server_ppd is not None:
                os.unlink(server_ppd)
Example #10
0
File: main.py Project: gonicus/gosa
    def writePPD(self, printer_cn, server_ppd_file, custom_ppd_file, data):
        if self.client is None:
            return

        server_ppd = None
        dir = self.env.config.get("cups.spool", default="/tmp/spool")
        if not os.path.exists(dir):
            os.makedirs(dir)
        try:
            server_ppd = self.client.getServerPPD(server_ppd_file)
            is_server_ppd = True
            ppd = cups.PPD(server_ppd)
        except Exception as e:
            self.log.error(str(e))
            is_server_ppd = False

            if custom_ppd_file is not None:
                ppd = cups.PPD(os.path.join(dir, custom_ppd_file))
            else:
                raise PPDException(C.make_error('COULD_NOT_READ_SOURCE_PPD'))

        if isinstance(data, str):
            data = loads(data)

        # apply options
        for option_name, value in data.items():
            option = ppd.findOption(option_name)
            if option is not None:
                conflicts = ppd.markOption(option_name, value)
                if conflicts > 0:
                    raise PPDException(C.make_error('OPTION_CONFLICT', option=option_name, value=value, conflicts=conflicts))
            else:
                raise PPDException(C.make_error('OPTION_NOT_FOUND', option=option_name))

        # calculate hash value for new PPD

        temp_file = tempfile.NamedTemporaryFile(delete=False)
        try:
            with open(temp_file.name, "w") as tf:
                ppd.writeFd(tf.fileno())

            with open(temp_file.name, "r") as tf:
                result = tf.read()

            hash = hashlib.md5(repr(result).encode('utf-8')).hexdigest()
            index = PluginRegistry.getInstance("ObjectIndex")

            new_file = os.path.join(dir, "%s.ppd" % hash)
            if new_file == custom_ppd_file:
                # nothing to to
                return {}

            if not is_server_ppd:
                # check if anyone else is using a file with this hash value and delete the old file if not
                query = {"_type": "GotoPrinter", "gotoPrinterPPD": "%s.ppd" % hash}
                if printer_cn is not None:
                    query["not_"] = {"cn": printer_cn}
                res = index.search(query, {"dn": 1})
                if len(res) == 0:
                    # delete file
                    os.unlink(custom_ppd_file)

            with open(new_file, "w") as f:
                f.write(result)

            return {
                "gotoPrinterPPD": ["%s/ppd/modified/%s.ppd" % (get_server_url(), hash)],
                "configured": [True]
            }

        except Exception as e:
            self.log.error(str(e))
            return {}
        finally:
            os.unlink(temp_file.name)
            if server_ppd is not None:
                os.unlink(server_ppd)