Question

Following situation:

  • We are using LDAP/GOSA for managing users and their printers
  • I use a perl script to extract the user-datas from LDAP and parse it into a yaml-file (at each login of a user)
  • I use the parsed information in a python-script to configure the cups printer (at each login of a user)
  • The python-script cleans the old configured printers and configures the new ones. For cleaning I use a Bash-script:

    #!/bin/bash

lpstat -p | awk '/^printer/ {print $2}' | while read printer do echo "Deleting Printer/Class:" $printer lpadmin -U $SUDO_USER -x $printer done

My problem is, that all CUPS printers will be reconfigured when I switch the user. Is there a function to bind the CUPS printers to a specific user and only clean those printers which belong to the user?

Example: User1 logs in with printer dev1, dev2 and dev3. Now he switches to a new session with User2 who only has the dev1 printer. Now dev2 and dev3 will be deleted for both users and after switching back to User1 dev2 and 3 aren't existent.

Thanks in advance!

Greetings

Stefan

EDIT: Found an answer! Will post it in the afternoon when I have time to document everything.

EDIT2: Answer is posted ;)

Was it helpful?

Solution

I found an answer for the question:

I use a function to find every user who is logged in and use all the yaml-files of each logged in user (the userconfig.yaml is permanently saved and gets an update during login).

# using self made module system.current_users()
users = system.current_users()

printers_with_users = {}

for username in users:

    try:
        printer_yaml = config_data.retrieve_for(username).get(u"printers", [])
    except:
        print("Aborting: Can't find userconfig.yaml!\n")
        exit(1)

    for device, dev_list in printer_yaml.iteritems():
        # parsing datas...
        config_classes = dev_list.get("config_classes", None)
        descr = get_unicode(dev_list, u"descr")
        printer_id = get_unicode(dev_list, u"id")
        is_local = dev_list.get("is_local", None)
        listen_network = dev_list.get("listen_network", None)
        location = get_unicode(dev_list, u"location")
        ppd_file = get_unicode(dev_list, u"ppd_file")
        ppd_uri = get_unicode(dev_list, u"ppd_uri")
        printer_uri = get_unicode(dev_list, u"printer_uri")

        # here is the cool part ;)
        printers_with_users.setdefault(printer_id, []).append(username)

        #configuring printers...

# configure access rules in printers.conf
for printers, accepted_users in printers_with_users.iteritems():
    users_string = ",".join(accepted_users)
    try:
        subproc.call('lpadmin', ['-p', printers, '-u', 'allow:%s' % users_string])
    except:
        print ( "     - unable to set user access rules\n" )
        print (config_error) % printers
        continue

I'm doing the same thing for the classes, too.

I can still clean the whole config file during each login and can also assure that every user can use his own printers.

Hopefully this is helpful for everyone who has the same problem (even though it is only a small piece of the whole printer manager ;) ).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top