Cups : Interface script for virtual printer not executing commands

My script:

#!/bin/bash

echo "Interface script running" >> /home/user/printer.log
cat "$6"

My printer :

lpadmin -p testprint -i script.sh -v file:/dev/null -E

It goes into the script and works but it doesn’t create the file printer.log. I tried various commands beside echo ( start another script, launch gedit in background ) and it looks like it doesn’t execute any of the commands in the script except the cat "$6". It doesn’t give any error. If I comment out the cat "$6" it gives an error when printing so it obviously goes through the script.

What am I doing wrong ?

Answer

Interface scripts are run under the control of cups and are not run as the user doing the printing. This means that it’s not connected to your terminal or X-display; it can not open windows or write to your home directory.

If you look at /etc/cups/cups-files.conf (maybe different location on your OS) there are lines such as

# Default user and group for filters/backends/helper programs; this cannot be
# any user or group that resolves to ID 0 for security reasons...
User lp
Group lp

This means, in my case, the scripts are run as user lp:lp.

Depending on how cups is run (eg systemd) it may also run with some further layer of isolation; e.g. an “overlay” of /tmp which means that the files writing here are not visible outside.

However, /var/spool/cups/tmp may be available for writing and visible.

eg

#!/bin/bash

echo "Interface script running $$" >> /var/spool/cups/tmp/my_log
/usr/bin/mount >> /var/spool/cups/tmp/my_log
/bin/id >> /var/spool/cups/tmp/my_log
df >> /var/spool/cups/tmp/my_log
cat "$6"

Now in /var/spool/cups/tmp/my_log we can see some results:

Interface script running 22462
/dev/vda3 on / type ext4 (rw,relatime,data=ordered)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=499340k,nr_inodes=124835,mode=755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
....
uid=4(lp) gid=7(lp) groups=7(lp)
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda3        3041808 1380564   1487012  49% /
devtmpfs          499340       0    499340   0% /dev
tmpfs             508452       0    508452   0% /dev/shm
tmpfs             508452       0    508452   0% /sys/fs/cgroup
tmpfs             508452   13016    495436   3% /run
tmpfs             101692       0    101692   0% /run/user/500
/dev/vda1         487634  119954    337984  27% /boot

This was tested on CentOS 7; it should work on Debian as well.

Leave a Reply

Your email address will not be published. Required fields are marked *