/usr/share/pyshared/foomatic/pysmb.py is in python-foomatic 0.7.9.5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #!/usr/bin/python
## redhat-config-printer
## CUPS backend
## Copyright (C) 2002, 2003 Red Hat, Inc.
## Copyright (C) 2002, 2003 Tim Waugh <twaugh@redhat.com>
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## This program is distributed 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 General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
## $Id: pysmb.py,v 1.3 2011/07/29 18:33:01 lordsutch Exp $
import os
import signal
import sys
import subprocess
nmblookup = "/usr/bin/nmblookup"
smbclient = "/usr/bin/smbclient"
def get_host_list ():
"""Return a dict, indexed by SMB name, of host dicts for this network.
A host dict has 'NAME' and 'IP' keys, and may have a 'GROUP' key."""
hosts = {}
if not os.access (nmblookup, os.X_OK):
return hosts
ips = []
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
subp = subprocess.Popen((nmblookup, '*'), env={'LANG' : 'C'},
stdout=subprocess.PIPE)
for l in subp.stdout.readlines():
if l.endswith (" *<00>\n"):
ips.append (l.split (" ")[0])
subp.stdout.close()
subp.wait() ## Reap the process
for ip in ips:
name = None
dct = { 'IP': ip }
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
subp = subprocess.Popen((nmblookup, '-A', ip), env={'LANG' : 'C'},
stdout=subprocess.PIPE)
for l in subp.stdout.readlines():
l = l.strip ()
if l.find (" <00> ") == -1:
continue
try:
val = l.split (" ")[0]
if val.find ("~") != -1:
continue
if l.find (" <GROUP> ") != -1 and not dct.has_key ('GROUP'):
dct['GROUP'] = l.split (" ")[0]
elif not dict.has_key ('NAME'):
name = l.split (" ")[0]
dct['NAME'] = name
except:
pass
subp.stdout.close()
subp.wait() ## Reap the process
if not name:
continue
hosts[name] = dct
return hosts
def get_host_info (smbname):
"""Given an SMB name, returns a host dict for it."""
dict = { 'NAME': smbname, 'IP': '', 'GROUP': '' }
subp = subprocess.Popen((nmblookup, '-S', smbname), env={'LANG' : 'C'},
stdout=subprocess.PIPE)
for l in subp.stdout.readlines():
l = l.strip ()
if l.endswith ("<00>"):
dict['IP'] = l.split (" ")[0]
continue
if l.find (" <00> ") == -1:
continue
if l.find (" <GROUP> ") != -1:
dict['GROUP'] = l.split (" ")[0]
else:
name = l.split (" ")[0]
dict['NAME'] = name
subp.stdout.close()
subp.wait()
return dict
def get_printer_list (host):
"""Given a host dict, returns a dict of printer shares for that host.
The value for a printer share name is its comment."""
printers = {}
if not os.access (smbclient, os.X_OK):
return printers
args = [smbclient, '-N', '-L', host['NAME']]
if host.has_key ('IP'):
args += ['-I', host['IP']]
if host.has_key ('GROUP'):
args += ['-W', host['GROUP']]
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
section = 0
subp = subprocess.Popen(args, env={'LANG' : 'C'},
stdout=subprocess.PIPE)
for l in subp.stdout.readlines():
l = l.strip ()
if l == "":
continue
if l[0] == '-':
section += 1
if section > 1:
break
continue
if section != 1:
continue
share = l[:l.find (" ")]
rest = l[len (share):].strip ()
end = rest.find (" ")
if end == -1:
type = rest
comment = ""
else:
type = rest[:rest.find (" ")]
comment = rest[len (type):].strip ()
if type == "Printer":
printers[share] = comment
subp.stdout.close()
subp.wait()
return printers
def printer_share_accessible (share, group = None, user = None, passwd = None):
"""Returns None if the share is inaccessible. Otherwise,
returns a dict with 'GROUP' associated with the workgroup name
of the server."""
if not os.access (smbclient, os.X_OK):
return None
args = [ smbclient, share ]
if passwd:
args.append (passwd)
if group:
args.extend (["-W", group])
args.extend (["-N", "-P", "-c", "quit"])
if user:
args.extend (["-U", user])
read, write = os.pipe ()
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
pid = os.fork ()
if pid == 0:
os.close (read)
if write != 1:
os.dup2 (write, 1)
os.environ['LANG'] = 'C'
os.execv (args[0], args)
sys.exit (1)
# Parent
dict = { 'GROUP': ''}
os.close (write)
for l in os.fdopen (read, 'r').readlines ():
if l.startswith ("Domain=[") and l.find ("]") != -1:
dict['GROUP'] = l[len("Domain=["):].split ("]")[0]
break
pid, status = os.waitpid (pid, 0)
if status:
return None
return dict
if __name__ == '__main__':
hosts = get_host_list ()
if hosts:
print get_printer_list (hosts[hosts.keys ()[0]])
else:
print 'No printers found.'
|